blob: d95382088075923ab47ae4fc869784e03c0520d4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Garmin GPS driver
3 *
Hermann Kneissel468d1362007-08-03 20:20:33 +02004 * Copyright (C) 2006,2007 Hermann Kneissel herkne@users.sourceforge.net
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * The latest version of the driver can be found at
7 * http://sourceforge.net/projects/garmin-gps/
8 *
9 * This driver has been derived from v2.1 of the visor driver.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
24 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/timer.h>
31#include <linux/tty.h>
32#include <linux/tty_driver.h>
33#include <linux/tty_flip.h>
34#include <linux/module.h>
35#include <linux/spinlock.h>
Alan Coxaf6d7802008-07-22 11:11:44 +010036#include <linux/uaccess.h>
Hermann Kneissel468d1362007-08-03 20:20:33 +020037#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070039#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* the mode to be set when the port ist opened */
42static int initial_mode = 1;
43
44/* debug flag */
Alan Coxaf6d7802008-07-22 11:11:44 +010045static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define GARMIN_VENDOR_ID 0x091E
48
49/*
50 * Version Information
51 */
52
53#define VERSION_MAJOR 0
Hermann Kneissel468d1362007-08-03 20:20:33 +020054#define VERSION_MINOR 31
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define _STR(s) #s
Alan Coxaf6d7802008-07-22 11:11:44 +010057#define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
59#define DRIVER_AUTHOR "hermann kneissel"
60#define DRIVER_DESC "garmin gps driver"
61
62/* error codes returned by the driver */
63#define EINVPKT 1000 /* invalid packet structure */
64
65
Alan Coxaf6d7802008-07-22 11:11:44 +010066/* size of the header of a packet using the usb protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define GARMIN_PKTHDR_LENGTH 12
68
Alan Coxaf6d7802008-07-22 11:11:44 +010069/* max. possible size of a packet using the serial protocol */
70#define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Alan Coxaf6d7802008-07-22 11:11:44 +010072/* max. possible size of a packet with worst case stuffing */
73#define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Alan Coxaf6d7802008-07-22 11:11:44 +010075/* size of a buffer able to hold a complete (no stuffing) packet
76 * (the document protocol does not contain packets with a larger
77 * size, but in theory a packet may be 64k+12 bytes - if in
78 * later protocol versions larger packet sizes occur, this value
79 * should be increased accordingly, so the input buffer is always
80 * large enough the store a complete packet inclusive header) */
81#define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Alan Coxaf6d7802008-07-22 11:11:44 +010083/* size of a buffer able to hold a complete (incl. stuffing) packet */
84#define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Alan Coxaf6d7802008-07-22 11:11:44 +010086/* where to place the packet id of a serial packet, so we can
87 * prepend the usb-packet header without the need to move the
88 * packets data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
90
Alan Coxaf6d7802008-07-22 11:11:44 +010091/* max. size of incoming private packets (header+1 param) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
93
94#define GARMIN_LAYERID_TRANSPORT 0
95#define GARMIN_LAYERID_APPL 20
Alan Coxaf6d7802008-07-22 11:11:44 +010096/* our own layer-id to use for some control mechanisms */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define GARMIN_LAYERID_PRIVATE 0x01106E4B
98
99#define GARMIN_PKTID_PVT_DATA 51
100#define GARMIN_PKTID_L001_COMMAND_DATA 10
101
102#define CMND_ABORT_TRANSFER 0
103
Alan Coxaf6d7802008-07-22 11:11:44 +0100104/* packet ids used in private layer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define PRIV_PKTID_SET_DEBUG 1
106#define PRIV_PKTID_SET_MODE 2
107#define PRIV_PKTID_INFO_REQ 3
108#define PRIV_PKTID_INFO_RESP 4
109#define PRIV_PKTID_RESET_REQ 5
110#define PRIV_PKTID_SET_DEF_MODE 6
111
112
113#define ETX 0x03
114#define DLE 0x10
115#define ACK 0x06
116#define NAK 0x15
117
118/* structure used to queue incoming packets */
119struct garmin_packet {
120 struct list_head list;
121 int seq;
Alan Coxaf6d7802008-07-22 11:11:44 +0100122 /* the real size of the data array, always > 0 */
123 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 __u8 data[1];
125};
126
127/* structure used to keep the current state of the driver */
128struct garmin_data {
129 __u8 state;
130 __u16 flags;
131 __u8 mode;
132 __u8 ignorePkts;
133 __u8 count;
134 __u8 pkt_id;
135 __u32 serial_num;
136 struct timer_list timer;
137 struct usb_serial_port *port;
138 int seq_counter;
139 int insize;
140 int outsize;
141 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
142 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
143 __u8 privpkt[4*6];
Hermann Kneissel468d1362007-08-03 20:20:33 +0200144 atomic_t req_count;
145 atomic_t resp_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 spinlock_t lock;
147 struct list_head pktlist;
148};
149
150
151#define STATE_NEW 0
152#define STATE_INITIAL_DELAY 1
153#define STATE_TIMEOUT 2
154#define STATE_SESSION_REQ1 3
155#define STATE_SESSION_REQ2 4
156#define STATE_ACTIVE 5
157
158#define STATE_RESET 8
159#define STATE_DISCONNECTED 9
160#define STATE_WAIT_TTY_ACK 10
161#define STATE_GSP_WAIT_DATA 11
162
163#define MODE_NATIVE 0
164#define MODE_GARMIN_SERIAL 1
165
Alan Coxaf6d7802008-07-22 11:11:44 +0100166/* Flags used in garmin_data.flags: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#define FLAGS_SESSION_REPLY_MASK 0x00C0
168#define FLAGS_SESSION_REPLY1_SEEN 0x0080
169#define FLAGS_SESSION_REPLY2_SEEN 0x0040
170#define FLAGS_BULK_IN_ACTIVE 0x0020
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200171#define FLAGS_BULK_IN_RESTART 0x0010
172#define FLAGS_THROTTLED 0x0008
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define CLEAR_HALT_REQUIRED 0x0001
174
175#define FLAGS_QUEUING 0x0100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#define FLAGS_DROP_DATA 0x0800
177
178#define FLAGS_GSP_SKIP 0x1000
179#define FLAGS_GSP_DLESEEN 0x2000
180
181
182
183
184
185
186/* function prototypes */
Alan Coxaf6d7802008-07-22 11:11:44 +0100187static void gsp_next_packet(struct garmin_data *garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200189 const unsigned char *buf, int count,
190 int dismiss_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/* some special packets to be send or received */
193static unsigned char const GARMIN_START_SESSION_REQ[]
194 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
195static unsigned char const GARMIN_START_SESSION_REQ2[]
196 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
197static unsigned char const GARMIN_START_SESSION_REPLY[]
198 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
199static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
200 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
201static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
202 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
203static unsigned char const GARMIN_APP_LAYER_REPLY[]
204 = { 0x14, 0, 0, 0 };
205static unsigned char const GARMIN_START_PVT_REQ[]
206 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
207static unsigned char const GARMIN_STOP_PVT_REQ[]
208 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
209static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
210 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
211static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
212 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
213static unsigned char const PRIVATE_REQ[]
214 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
215
216
217
218static struct usb_device_id id_table [] = {
Alan Coxaf6d7802008-07-22 11:11:44 +0100219 /* the same device id seems to be used by all
220 usb enabled GPS devices */
221 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 { } /* Terminating entry */
223};
224
Alan Coxaf6d7802008-07-22 11:11:44 +0100225MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227static struct usb_driver garmin_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .name = "garmin_gps",
229 .probe = usb_serial_probe,
230 .disconnect = usb_serial_disconnect,
231 .id_table = id_table,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200232 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
235
Alan Coxaf6d7802008-07-22 11:11:44 +0100236static inline int noResponseFromAppLayer(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Alan Coxaf6d7802008-07-22 11:11:44 +0100238 return atomic_read(&garmin_data_p->req_count) ==
239 atomic_read(&garmin_data_p->resp_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242
243static inline int getLayerId(const __u8 *usbPacket)
244{
245 return __le32_to_cpup((__le32 *)(usbPacket));
246}
247
248static inline int getPacketId(const __u8 *usbPacket)
249{
250 return __le32_to_cpup((__le32 *)(usbPacket+4));
251}
252
253static inline int getDataLength(const __u8 *usbPacket)
254{
255 return __le32_to_cpup((__le32 *)(usbPacket+8));
256}
257
258
259/*
260 * check if the usb-packet in buf contains an abort-transfer command.
261 * (if yes, all queued data will be dropped)
262 */
263static inline int isAbortTrfCmnd(const unsigned char *buf)
264{
Alan Coxaf6d7802008-07-22 11:11:44 +0100265 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
266 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
267 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
268 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 1;
270 else
271 return 0;
272}
273
274
275
276static void send_to_tty(struct usb_serial_port *port,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200277 char *data, unsigned int actual_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Alan Cox95da3102008-07-22 11:09:07 +0100279 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (tty && actual_length) {
282
Alan Coxaf6d7802008-07-22 11:11:44 +0100283 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800284 __func__, actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Alan Cox33f0f882006-01-09 20:54:13 -0800286 tty_buffer_request_room(tty, actual_length);
287 tty_insert_flip_string(tty, data, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 tty_flip_buffer_push(tty);
289 }
290}
291
292
293/******************************************************************************
294 * packet queue handling
295 ******************************************************************************/
296
297/*
298 * queue a received (usb-)packet for later processing
299 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100300static int pkt_add(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200301 unsigned char *data, unsigned int data_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200303 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int result = 0;
305 unsigned long flags;
306 struct garmin_packet *pkt;
307
308 /* process only packets containg data ... */
309 if (data_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
Alan Coxaf6d7802008-07-22 11:11:44 +0100311 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (pkt == NULL) {
313 dev_err(&garmin_data_p->port->dev, "out of memory\n");
314 return 0;
315 }
316 pkt->size = data_length;
317 memcpy(pkt->data, data, data_length);
318
319 spin_lock_irqsave(&garmin_data_p->lock, flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200320 garmin_data_p->flags |= FLAGS_QUEUING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 result = list_empty(&garmin_data_p->pktlist);
322 pkt->seq = garmin_data_p->seq_counter++;
323 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200324 state = garmin_data_p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
326
327 /* in serial mode, if someone is waiting for data from
328 the device, iconvert and send the next packet to tty. */
Alan Coxaf6d7802008-07-22 11:11:44 +0100329 if (result && (state == STATE_GSP_WAIT_DATA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332 return result;
333}
334
335
336/* get the next pending packet */
Alan Coxaf6d7802008-07-22 11:11:44 +0100337static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 unsigned long flags;
340 struct garmin_packet *result = NULL;
341
342 spin_lock_irqsave(&garmin_data_p->lock, flags);
343 if (!list_empty(&garmin_data_p->pktlist)) {
344 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
345 list_del(&result->list);
346 }
347 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
348 return result;
349}
350
351
352/* free up all queued data */
Alan Coxaf6d7802008-07-22 11:11:44 +0100353static void pkt_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 unsigned long flags;
356 struct garmin_packet *result = NULL;
357
Harvey Harrison441b62c2008-03-03 16:08:34 -0800358 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 spin_lock_irqsave(&garmin_data_p->lock, flags);
361 while (!list_empty(&garmin_data_p->pktlist)) {
362 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
363 list_del(&result->list);
364 kfree(result);
365 }
366 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
367}
368
369
370/******************************************************************************
371 * garmin serial protocol handling handling
372 ******************************************************************************/
373
374/* send an ack packet back to the tty */
Alan Coxaf6d7802008-07-22 11:11:44 +0100375static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 __u8 pkt[10];
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200378 __u8 cksum = 0;
379 __u8 *ptr = pkt;
380 unsigned l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Harvey Harrison441b62c2008-03-03 16:08:34 -0800382 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 *ptr++ = DLE;
385 *ptr++ = ACK;
386 cksum += ACK;
387
388 *ptr++ = 2;
389 cksum += 2;
390
391 *ptr++ = pkt_id;
392 cksum += pkt_id;
393
Alan Coxaf6d7802008-07-22 11:11:44 +0100394 if (pkt_id == DLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *ptr++ = DLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 *ptr++ = 0;
398 *ptr++ = 0xFF & (-cksum);
399 *ptr++ = DLE;
400 *ptr++ = ETX;
401
402 l = ptr-pkt;
403
404 send_to_tty(garmin_data_p->port, pkt, l);
405 return 0;
406}
407
408
409
410/*
411 * called for a complete packet received from tty layer
412 *
413 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
414 * at GSP_INITIAL_OFFSET.
415 *
416 * count - number of bytes in the input buffer including space reserved for
Alan Coxaf6d7802008-07-22 11:11:44 +0100417 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * (including pkt-id, data-length a. cksum)
419 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100420static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Alan Coxaf6d7802008-07-22 11:11:44 +0100422 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200423 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 int cksum = 0;
426 int n = 0;
427 int pktid = recpkt[0];
428 int size = recpkt[1];
429
430 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800431 __func__, count-GSP_INITIAL_OFFSET, recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (size != (count-GSP_INITIAL_OFFSET-3)) {
434 dbg("%s - invalid size, expected %d bytes, got %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800435 __func__, size, (count-GSP_INITIAL_OFFSET-3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EINVPKT;
437 }
438
439 cksum += *recpkt++;
440 cksum += *recpkt++;
441
Alan Coxaf6d7802008-07-22 11:11:44 +0100442 /* sanity check, remove after test ... */
443 if ((__u8 *)&(usbdata[3]) != recpkt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 dbg("%s - ptr mismatch %p - %p",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800445 __func__, &(usbdata[4]), recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -EINVPKT;
447 }
448
449 while (n < size) {
450 cksum += *recpkt++;
451 n++;
452 }
453
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200454 if ((0xff & (cksum + *recpkt)) != 0) {
455 dbg("%s - invalid checksum, expected %02x, got %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800456 __func__, 0xff & -cksum, 0xff & *recpkt);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200457 return -EINVPKT;
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
461 usbdata[1] = __cpu_to_le32(pktid);
462 usbdata[2] = __cpu_to_le32(size);
463
Alan Coxaf6d7802008-07-22 11:11:44 +0100464 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200465 GARMIN_PKTHDR_LENGTH+size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* if this was an abort-transfer command, flush all
468 queued data. */
469 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
470 garmin_data_p->flags |= FLAGS_DROP_DATA;
471 pkt_clear(garmin_data_p);
472 }
473
474 return count;
475}
476
477
478
479/*
480 * Called for data received from tty
481 *
482 * buf contains the data read, it may span more than one packet or even
483 * incomplete packets
484 *
485 * input record should be a serial-record, but it may not be complete.
486 * Copy it into our local buffer, until an etx is seen (or an error
487 * occurs).
488 * Once the record is complete, convert into a usb packet and send it
489 * to the bulk pipe, send an ack back to the tty.
490 *
491 * If the input is an ack, just send the last queued packet to the
492 * tty layer.
493 *
494 * if the input is an abort command, drop all queued data.
495 */
496
Alan Coxaf6d7802008-07-22 11:11:44 +0100497static int gsp_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200498 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200500 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int offs = 0;
502 int ack_or_nak_seen = 0;
503 int i = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200504 __u8 *dest;
505 int size;
Alan Coxaf6d7802008-07-22 11:11:44 +0100506 /* dleSeen: set if last byte read was a DLE */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200507 int dleSeen;
Alan Coxaf6d7802008-07-22 11:11:44 +0100508 /* skip: if set, skip incoming data until possible start of
509 * new packet
510 */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200511 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 __u8 data;
513
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200514 spin_lock_irqsave(&garmin_data_p->lock, flags);
515 dest = garmin_data_p->inbuffer;
516 size = garmin_data_p->insize;
517 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
518 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
519 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 dbg("%s - dle=%d skip=%d size=%d count=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800522 __func__, dleSeen, skip, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Alan Coxaf6d7802008-07-22 11:11:44 +0100524 if (size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 size = GSP_INITIAL_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 while (offs < count) {
528
529 data = *(buf+offs);
Alan Coxaf6d7802008-07-22 11:11:44 +0100530 offs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (data == DLE) {
533 if (skip) { /* start of a new pkt */
534 skip = 0;
535 size = GSP_INITIAL_OFFSET;
536 dleSeen = 1;
537 } else if (dleSeen) {
538 dest[size++] = data;
539 dleSeen = 0;
540 } else {
541 dleSeen = 1;
542 }
543 } else if (data == ETX) {
544 if (dleSeen) {
545 /* packet complete */
546
547 data = dest[GSP_INITIAL_OFFSET];
548
549 if (data == ACK) {
550 ack_or_nak_seen = ACK;
551 dbg("ACK packet complete.");
552 } else if (data == NAK) {
553 ack_or_nak_seen = NAK;
554 dbg("NAK packet complete.");
555 } else {
Alan Coxaf6d7802008-07-22 11:11:44 +0100556 dbg("packet complete - id=0x%X.",
557 0xFF & data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 gsp_rec_packet(garmin_data_p, size);
559 }
560
561 skip = 1;
562 size = GSP_INITIAL_OFFSET;
563 dleSeen = 0;
564 } else {
565 dest[size++] = data;
566 }
567 } else if (!skip) {
568
569 if (dleSeen) {
570 dbg("non-masked DLE at %d - restarting", i);
571 size = GSP_INITIAL_OFFSET;
572 dleSeen = 0;
573 }
574
575 dest[size++] = data;
576 }
577
578 if (size >= GPS_IN_BUFSIZ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800579 dbg("%s - packet too large.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 skip = 1;
581 size = GSP_INITIAL_OFFSET;
582 dleSeen = 0;
583 }
584 }
585
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200586 spin_lock_irqsave(&garmin_data_p->lock, flags);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 garmin_data_p->insize = size;
589
Alan Coxaf6d7802008-07-22 11:11:44 +0100590 /* copy flags back to structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (skip)
592 garmin_data_p->flags |= FLAGS_GSP_SKIP;
593 else
594 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
595
596 if (dleSeen)
597 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
598 else
599 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
600
Alan Coxaf6d7802008-07-22 11:11:44 +0100601 if (ack_or_nak_seen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 garmin_data_p->state = STATE_GSP_WAIT_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200603
604 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
605
Alan Coxaf6d7802008-07-22 11:11:44 +0100606 if (ack_or_nak_seen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return count;
609}
610
611
612
613
614/*
615 * Sends a usb packet to the tty
616 *
617 * Assumes, that all packages and at an usb-packet boundary.
618 *
619 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
620 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100621static int gsp_send(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 const unsigned char *buf, int count)
623{
624 const unsigned char *src;
625 unsigned char *dst;
626 int pktid = 0;
627 int datalen = 0;
628 int cksum = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100629 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int k;
631
Harvey Harrison441b62c2008-03-03 16:08:34 -0800632 dbg("%s - state %d - %d bytes.", __func__,
Alan Coxaf6d7802008-07-22 11:11:44 +0100633 garmin_data_p->state, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 k = garmin_data_p->outsize;
636 if ((k+count) > GPS_OUT_BUFSIZ) {
637 dbg("packet too large");
638 garmin_data_p->outsize = 0;
639 return -4;
640 }
641
642 memcpy(garmin_data_p->outbuffer+k, buf, count);
643 k += count;
644 garmin_data_p->outsize = k;
645
646 if (k >= GARMIN_PKTHDR_LENGTH) {
647 pktid = getPacketId(garmin_data_p->outbuffer);
Alan Coxaf6d7802008-07-22 11:11:44 +0100648 datalen = getDataLength(garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 i = GARMIN_PKTHDR_LENGTH + datalen;
650 if (k < i)
651 return 0;
652 } else {
653 return 0;
654 }
655
Alan Coxaf6d7802008-07-22 11:11:44 +0100656 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* garmin_data_p->outbuffer now contains a complete packet */
659
660 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100661 __func__, k, garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 garmin_data_p->outsize = 0;
664
665 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100666 dbg("not an application packet (%d)",
667 getLayerId(garmin_data_p->outbuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -1;
669 }
670
671 if (pktid > 255) {
672 dbg("packet-id %d too large", pktid);
673 return -2;
674 }
675
676 if (datalen > 255) {
677 dbg("packet-size %d too large", datalen);
678 return -3;
679 }
680
681 /* the serial protocol should be able to handle this packet */
682
683 k = 0;
684 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
Alan Coxaf6d7802008-07-22 11:11:44 +0100685 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (*src++ == DLE)
687 k++;
688 }
689
690 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
691 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100692 /* can't add stuffing DLEs in place, move data to end
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200693 of buffer ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
695 memcpy(dst, src, datalen);
696 src = dst;
697 }
698
699 dst = garmin_data_p->outbuffer;
700
701 *dst++ = DLE;
702 *dst++ = pktid;
703 cksum += pktid;
704 *dst++ = datalen;
705 cksum += datalen;
706 if (datalen == DLE)
707 *dst++ = DLE;
708
Alan Coxaf6d7802008-07-22 11:11:44 +0100709 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 __u8 c = *src++;
711 *dst++ = c;
712 cksum += c;
713 if (c == DLE)
714 *dst++ = DLE;
715 }
Alan Coxaf6d7802008-07-22 11:11:44 +0100716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 cksum = 0xFF & -cksum;
718 *dst++ = cksum;
719 if (cksum == DLE)
720 *dst++ = DLE;
721 *dst++ = DLE;
722 *dst++ = ETX;
723
724 i = dst-garmin_data_p->outbuffer;
725
726 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
727
728 garmin_data_p->pkt_id = pktid;
729 garmin_data_p->state = STATE_WAIT_TTY_ACK;
730
731 return i;
732}
733
734
735
736
737
738/*
739 * Process the next pending data packet - if there is one
740 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100741static void gsp_next_packet(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 struct garmin_packet *pkt = NULL;
744
745 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800746 dbg("%s - next pkt: %d", __func__, pkt->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
748 kfree(pkt);
749 return;
750 }
751 kfree(pkt);
752 }
753}
754
755
756
757
758/******************************************************************************
759 * garmin native mode
760 ******************************************************************************/
761
762
763/*
764 * Called for data received from tty
765 *
766 * The input data is expected to be in garmin usb-packet format.
767 *
768 * buf contains the data read, it may span more than one packet
769 * or even incomplete packets
770 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100771static int nat_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200772 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200774 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100775 __u8 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 int offs = 0;
777 int result = count;
778 int len;
779
780 while (offs < count) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100781 /* if buffer contains header, copy rest of data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
783 len = GARMIN_PKTHDR_LENGTH
784 +getDataLength(garmin_data_p->inbuffer);
785 else
786 len = GARMIN_PKTHDR_LENGTH;
787
788 if (len >= GPS_IN_BUFSIZ) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100789 /* seems to be an invalid packet, ignore rest
790 of input */
791 dbg("%s - packet size too large: %d", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 garmin_data_p->insize = 0;
793 count = 0;
794 result = -EINVPKT;
795 } else {
796 len -= garmin_data_p->insize;
797 if (len > (count-offs))
798 len = (count-offs);
799 if (len > 0) {
800 dest = garmin_data_p->inbuffer
Alan Coxaf6d7802008-07-22 11:11:44 +0100801 + garmin_data_p->insize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 memcpy(dest, buf+offs, len);
803 garmin_data_p->insize += len;
804 offs += len;
805 }
806 }
807
808 /* do we have a complete packet ? */
809 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
810 len = GARMIN_PKTHDR_LENGTH+
811 getDataLength(garmin_data_p->inbuffer);
812 if (garmin_data_p->insize >= len) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100813 garmin_write_bulk(garmin_data_p->port,
814 garmin_data_p->inbuffer,
815 len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 garmin_data_p->insize = 0;
817
818 /* if this was an abort-transfer command,
819 flush all queued data. */
820 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100821 spin_lock_irqsave(&garmin_data_p->lock,
822 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 garmin_data_p->flags |= FLAGS_DROP_DATA;
Alan Coxaf6d7802008-07-22 11:11:44 +0100824 spin_unlock_irqrestore(
825 &garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 pkt_clear(garmin_data_p);
827 }
828 }
829 }
830 }
831 return result;
832}
833
834
835/******************************************************************************
836 * private packets
837 ******************************************************************************/
838
839static void priv_status_resp(struct usb_serial_port *port)
840{
Alan Coxaf6d7802008-07-22 11:11:44 +0100841 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
843
844 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
845 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
846 pkt[2] = __cpu_to_le32(12);
847 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
848 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
849 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
850
Alan Coxaf6d7802008-07-22 11:11:44 +0100851 send_to_tty(port, (__u8 *)pkt, 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854
855/******************************************************************************
856 * Garmin specific driver functions
857 ******************************************************************************/
858
859static int process_resetdev_request(struct usb_serial_port *port)
860{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200861 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 int status;
Alan Coxaf6d7802008-07-22 11:11:44 +0100863 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200865 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
867 garmin_data_p->state = STATE_RESET;
868 garmin_data_p->serial_num = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200869 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Alan Coxaf6d7802008-07-22 11:11:44 +0100871 usb_kill_urb(port->interrupt_in_urb);
872 dbg("%s - usb_reset_device", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 status = usb_reset_device(port->serial->dev);
874 if (status)
875 dbg("%s - usb_reset_device failed: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800876 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return status;
878}
879
880
881
882/*
883 * clear all cached data
884 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100885static int garmin_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200887 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int status = 0;
889
890 struct usb_serial_port *port = garmin_data_p->port;
891
Hermann Kneissel468d1362007-08-03 20:20:33 +0200892 if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* send a terminate command */
894 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100895 sizeof(GARMIN_STOP_TRANSFER_REQ), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 /* flush all queued data */
899 pkt_clear(garmin_data_p);
900
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200901 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 garmin_data_p->insize = 0;
903 garmin_data_p->outsize = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200904 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 return status;
907}
908
909
910
911
912
913
914static int garmin_init_session(struct usb_serial_port *port)
915{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200916 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100918 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int status = 0;
920
921 if (status == 0) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100922 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Harvey Harrison441b62c2008-03-03 16:08:34 -0800924 dbg("%s - adding interrupt input", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 port->interrupt_in_urb->dev = serial->dev;
926 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
927 if (status)
928 dev_err(&serial->dev->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100929 "%s - failed submitting interrupt urb, error %d\n",
930 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933 if (status == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800934 dbg("%s - starting session ...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 garmin_data_p->state = STATE_ACTIVE;
936 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100937 sizeof(GARMIN_START_SESSION_REQ), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (status >= 0) {
940
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200941 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 garmin_data_p->ignorePkts++;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200943 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* not needed, but the win32 driver does it too ... */
946 status = garmin_write_bulk(port,
Alan Coxaf6d7802008-07-22 11:11:44 +0100947 GARMIN_START_SESSION_REQ2,
948 sizeof(GARMIN_START_SESSION_REQ2), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (status >= 0) {
950 status = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200951 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 garmin_data_p->ignorePkts++;
Alan Coxaf6d7802008-07-22 11:11:44 +0100953 spin_unlock_irqrestore(&garmin_data_p->lock,
954 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 }
957 }
958
959 return status;
960}
961
962
963
964
965
Alan Coxaf6d7802008-07-22 11:11:44 +0100966static int garmin_open(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100967 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200969 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int status = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100971 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Harvey Harrison441b62c2008-03-03 16:08:34 -0800973 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /*
976 * Force low_latency on so that our tty_push actually forces the data
977 * through, otherwise it is scheduled, and with high data rates (like
978 * with OHCI) data can get lost.
979 */
Alan Cox95da3102008-07-22 11:09:07 +0100980 if (tty)
981 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200983 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 garmin_data_p->mode = initial_mode;
985 garmin_data_p->count = 0;
986 garmin_data_p->flags = 0;
Hermann Kneissel468d1362007-08-03 20:20:33 +0200987 atomic_set(&garmin_data_p->req_count, 0);
988 atomic_set(&garmin_data_p->resp_count, 0);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200989 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* shutdown any bulk reads that might be going on */
Alan Coxaf6d7802008-07-22 11:11:44 +0100992 usb_kill_urb(port->write_urb);
993 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Alan Cox95da3102008-07-22 11:09:07 +0100995 if (garmin_data_p->state == STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 status = garmin_init_session(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 garmin_data_p->state = STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return status;
1000}
1001
1002
Alan Cox95da3102008-07-22 11:09:07 +01001003static void garmin_close(struct tty_struct *tty,
Alan Coxaf6d7802008-07-22 11:11:44 +01001004 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001007 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Harvey Harrison441b62c2008-03-03 16:08:34 -08001009 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 port->number, garmin_data_p->mode,
1011 garmin_data_p->state, garmin_data_p->flags);
1012
1013 if (!serial)
1014 return;
1015
Oliver Neukum95bef012008-01-22 13:56:18 +01001016 mutex_lock(&port->serial->disc_mutex);
1017 if (!port->serial->disconnected)
1018 garmin_clear(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /* shutdown our urbs */
Alan Coxaf6d7802008-07-22 11:11:44 +01001021 usb_kill_urb(port->read_urb);
1022 usb_kill_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Oliver Neukum95bef012008-01-22 13:56:18 +01001024 if (!port->serial->disconnected) {
1025 if (noResponseFromAppLayer(garmin_data_p) ||
1026 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1027 process_resetdev_request(port);
1028 garmin_data_p->state = STATE_RESET;
1029 } else {
1030 garmin_data_p->state = STATE_DISCONNECTED;
1031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 } else {
1033 garmin_data_p->state = STATE_DISCONNECTED;
1034 }
Oliver Neukum95bef012008-01-22 13:56:18 +01001035 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Alan Coxaf6d7802008-07-22 11:11:44 +01001038static void garmin_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001040 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001041 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001042 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Hermann Kneissel468d1362007-08-03 20:20:33 +02001044 if (port) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001045 struct garmin_data *garmin_data_p =
1046 usb_get_serial_port_data(port);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001047
Harvey Harrison441b62c2008-03-03 16:08:34 -08001048 dbg("%s - port %d", __func__, port->number);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001049
1050 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)
1051 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001052 gsp_send_ack(garmin_data_p,
1053 ((__u8 *)urb->transfer_buffer)[4]);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001054 }
1055
1056 if (status) {
1057 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001058 __func__, urb->status);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001059 spin_lock_irqsave(&garmin_data_p->lock, flags);
1060 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1061 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1062 }
1063
1064 usb_serial_port_softint(port);
1065 }
1066
Alan Coxaf6d7802008-07-22 11:11:44 +01001067 /* Ignore errors that resulted from garmin_write_bulk with
1068 dismiss_ack = 1 */
Hermann Kneissel468d1362007-08-03 20:20:33 +02001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxaf6d7802008-07-22 11:11:44 +01001071 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074
Alan Coxaf6d7802008-07-22 11:11:44 +01001075static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001076 const unsigned char *buf, int count,
1077 int dismiss_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001079 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001081 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct urb *urb;
1083 unsigned char *buffer;
1084 int status;
1085
Harvey Harrison441b62c2008-03-03 16:08:34 -08001086 dbg("%s - port %d, state %d", __func__, port->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 garmin_data_p->state);
1088
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001089 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001091 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Alan Coxaf6d7802008-07-22 11:11:44 +01001093 buffer = kmalloc(count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!buffer) {
1095 dev_err(&port->dev, "out of memory\n");
1096 return -ENOMEM;
1097 }
1098
1099 urb = usb_alloc_urb(0, GFP_ATOMIC);
1100 if (!urb) {
1101 dev_err(&port->dev, "no more free urbs\n");
Alan Coxaf6d7802008-07-22 11:11:44 +01001102 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -ENOMEM;
1104 }
1105
Alan Coxaf6d7802008-07-22 11:11:44 +01001106 memcpy(buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Harvey Harrison441b62c2008-03-03 16:08:34 -08001108 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Alan Coxaf6d7802008-07-22 11:11:44 +01001110 usb_fill_bulk_urb(urb, serial->dev,
1111 usb_sndbulkpipe(serial->dev,
1112 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 buffer, count,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001114 garmin_write_bulk_callback,
1115 dismiss_ack ? NULL : port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 urb->transfer_flags |= URB_ZERO_PACKET;
1117
1118 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
Hermann Kneissel468d1362007-08-03 20:20:33 +02001119 atomic_inc(&garmin_data_p->req_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1121 pkt_clear(garmin_data_p);
1122 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1123 }
1124 }
1125
1126 /* send it down the pipe */
1127 status = usb_submit_urb(urb, GFP_ATOMIC);
1128 if (status) {
1129 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001130 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001131 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 count = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
1135 /* we are done with this urb, so let the host driver
1136 * really free it when it is finished with it */
Alan Coxaf6d7802008-07-22 11:11:44 +01001137 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 return count;
1140}
1141
Alan Coxaf6d7802008-07-22 11:11:44 +01001142static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +01001143 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 int pktid, pktsiz, len;
Alan Coxaf6d7802008-07-22 11:11:44 +01001146 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1148
Harvey Harrison441b62c2008-03-03 16:08:34 -08001149 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* check for our private packets */
1152 if (count >= GARMIN_PKTHDR_LENGTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 len = PRIVPKTSIZ;
1154 if (count < len)
1155 len = count;
1156
1157 memcpy(garmin_data_p->privpkt, buf, len);
1158
1159 pktsiz = getDataLength(garmin_data_p->privpkt);
1160 pktid = getPacketId(garmin_data_p->privpkt);
1161
1162 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
Alan Coxaf6d7802008-07-22 11:11:44 +01001163 && GARMIN_LAYERID_PRIVATE ==
1164 getLayerId(garmin_data_p->privpkt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 dbg("%s - processing private request %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001167 __func__, pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Alan Coxaf6d7802008-07-22 11:11:44 +01001169 /* drop all unfinished transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 garmin_clear(garmin_data_p);
1171
Alan Coxaf6d7802008-07-22 11:11:44 +01001172 switch (pktid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 case PRIV_PKTID_SET_DEBUG:
1175 if (pktsiz != 4)
1176 return -EINVPKT;
1177 debug = __le32_to_cpu(privpkt[3]);
1178 dbg("%s - debug level set to 0x%X",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001179 __func__, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
1181
1182 case PRIV_PKTID_SET_MODE:
1183 if (pktsiz != 4)
1184 return -EINVPKT;
1185 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1186 dbg("%s - mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001187 __func__, garmin_data_p->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 break;
1189
1190 case PRIV_PKTID_INFO_REQ:
1191 priv_status_resp(port);
1192 break;
1193
1194 case PRIV_PKTID_RESET_REQ:
Hermann Kneissel468d1362007-08-03 20:20:33 +02001195 atomic_inc(&garmin_data_p->req_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197
1198 case PRIV_PKTID_SET_DEF_MODE:
1199 if (pktsiz != 4)
1200 return -EINVPKT;
1201 initial_mode = __le32_to_cpu(privpkt[3]);
1202 dbg("%s - initial_mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001203 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 garmin_data_p->mode);
1205 break;
1206 }
1207 return count;
1208 }
1209 }
1210
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001211 garmin_data_p->ignorePkts = 0;
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1214 return gsp_receive(garmin_data_p, buf, count);
1215 } else { /* MODE_NATIVE */
1216 return nat_receive(garmin_data_p, buf, count);
1217 }
1218}
1219
1220
Alan Cox95da3102008-07-22 11:09:07 +01001221static int garmin_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Alan Cox95da3102008-07-22 11:09:07 +01001223 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /*
1225 * Report back the bytes currently available in the output buffer.
1226 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001227 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1229}
1230
1231
Alan Coxaf6d7802008-07-22 11:11:44 +01001232static void garmin_read_process(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned char *data, unsigned data_length)
1234{
1235 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1236 /* abort-transfer cmd is actice */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001237 dbg("%s - pkt dropped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
Alan Coxaf6d7802008-07-22 11:11:44 +01001239 garmin_data_p->state != STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* remember any appl.layer packets, so we know
1242 if a reset is required or not when closing
1243 the device */
1244 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001245 sizeof(GARMIN_APP_LAYER_REPLY))) {
Hermann Kneissel468d1362007-08-03 20:20:33 +02001246 atomic_inc(&garmin_data_p->resp_count);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /* if throttling is active or postprecessing is required
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001250 put the received data in the input queue, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 send it directly to the tty port */
1252 if (garmin_data_p->flags & FLAGS_QUEUING) {
1253 pkt_add(garmin_data_p, data, data_length);
1254 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001255 if (getLayerId(data) == GARMIN_LAYERID_APPL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 pkt_add(garmin_data_p, data, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 } else {
1258 send_to_tty(garmin_data_p->port, data, data_length);
1259 }
1260 }
1261}
1262
1263
Alan Coxaf6d7802008-07-22 11:11:44 +01001264static void garmin_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001266 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001267 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001269 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001271 int status = urb->status;
1272 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Harvey Harrison441b62c2008-03-03 16:08:34 -08001274 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 if (!serial) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001277 dbg("%s - bad serial pointer, exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return;
1279 }
1280
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001281 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001283 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return;
1285 }
1286
Alan Coxaf6d7802008-07-22 11:11:44 +01001287 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -08001288 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 garmin_read_process(garmin_data_p, data, urb->actual_length);
1291
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001292 if (urb->actual_length == 0 &&
1293 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1294 spin_lock_irqsave(&garmin_data_p->lock, flags);
1295 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1296 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001297 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1298 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 dev_err(&port->dev,
1300 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001301 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001302 } else if (urb->actual_length > 0) {
1303 /* Continue trying to read until nothing more is received */
1304 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001305 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1306 if (retval)
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001307 dev_err(&port->dev,
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001308 "%s - failed resubmitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001309 "error %d\n", __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001310 }
1311 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001312 dbg("%s - end of bulk data", __func__);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001313 spin_lock_irqsave(&garmin_data_p->lock, flags);
1314 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1315 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 return;
1318}
1319
1320
Alan Coxaf6d7802008-07-22 11:11:44 +01001321static void garmin_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001323 unsigned long flags;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001324 int retval;
Ming Leicdc97792008-02-24 18:41:47 +08001325 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001327 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001329 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001331 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 case 0:
1333 /* success */
1334 break;
1335 case -ECONNRESET:
1336 case -ENOENT:
1337 case -ESHUTDOWN:
1338 /* this urb is terminated, clean up */
1339 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001340 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return;
1342 default:
1343 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001344 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return;
1346 }
1347
Harvey Harrison441b62c2008-03-03 16:08:34 -08001348 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 urb->actual_length, urb->transfer_buffer);
1350
1351 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1352 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001353 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Harvey Harrison441b62c2008-03-03 16:08:34 -08001355 dbg("%s - bulk data available.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001357 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1358
1359 /* bulk data available */
Alan Coxaf6d7802008-07-22 11:11:44 +01001360 usb_fill_bulk_urb(port->read_urb, serial->dev,
1361 usb_rcvbulkpipe(serial->dev,
1362 port->bulk_in_endpointAddress),
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001363 port->read_urb->transfer_buffer,
1364 port->read_urb->transfer_buffer_length,
1365 garmin_read_bulk_callback, port);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001366 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1367 if (retval) {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001368 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001369 "%s - failed submitting read urb, error %d\n",
1370 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001371 } else {
1372 spin_lock_irqsave(&garmin_data_p->lock, flags);
1373 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1374 /* do not send this packet to the user */
1375 garmin_data_p->ignorePkts = 1;
Alan Coxaf6d7802008-07-22 11:11:44 +01001376 spin_unlock_irqrestore(&garmin_data_p->lock,
1377 flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001378 }
1379 } else {
1380 /* bulk-in transfer still active */
1381 spin_lock_irqsave(&garmin_data_p->lock, flags);
1382 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1383 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385
1386 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1387 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001388 sizeof(GARMIN_START_SESSION_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001390 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001392 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* save the serial number */
Alan Coxaf6d7802008-07-22 11:11:44 +01001395 garmin_data_p->serial_num = __le32_to_cpup(
1396 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 dbg("%s - start-of-session reply seen - serial %u.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001399 __func__, garmin_data_p->serial_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 if (garmin_data_p->ignorePkts) {
1403 /* this reply belongs to a request generated by the driver,
1404 ignore it. */
1405 dbg("%s - pkt ignored (%d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001406 __func__, garmin_data_p->ignorePkts);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001407 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 garmin_data_p->ignorePkts--;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001409 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 } else {
1411 garmin_read_process(garmin_data_p, data, urb->actual_length);
1412 }
1413
1414 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxaf6d7802008-07-22 11:11:44 +01001415 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001416 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 dev_err(&urb->dev->dev,
1418 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001419 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
1422
1423/*
1424 * Sends the next queued packt to the tty port (garmin native mode only)
1425 * and then sets a timer to call itself again until all queued data
1426 * is sent.
1427 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001428static int garmin_flush_queue(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001430 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 struct garmin_packet *pkt;
1432
1433 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1434 pkt = pkt_pop(garmin_data_p);
1435 if (pkt != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1437 kfree(pkt);
1438 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1439
1440 } else {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001441 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 garmin_data_p->flags &= ~FLAGS_QUEUING;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001443 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 }
1446 return 0;
1447}
1448
1449
Alan Cox95da3102008-07-22 11:09:07 +01001450static void garmin_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Alan Cox95da3102008-07-22 11:09:07 +01001452 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001453 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001454 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Harvey Harrison441b62c2008-03-03 16:08:34 -08001456 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* set flag, data received will be put into a queue
1458 for later processing */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001459 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001461 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464
Alan Coxaf6d7802008-07-22 11:11:44 +01001465static void garmin_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Alan Cox95da3102008-07-22 11:09:07 +01001467 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001468 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001469 unsigned long flags;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001470 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Harvey Harrison441b62c2008-03-03 16:08:34 -08001472 dbg("%s - port %d", __func__, port->number);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001473 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 garmin_data_p->flags &= ~FLAGS_THROTTLED;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001475 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /* in native mode send queued data to tty, in
1478 serial mode nothing needs to be done here */
1479 if (garmin_data_p->mode == MODE_NATIVE)
1480 garmin_flush_queue(garmin_data_p);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001481
1482 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1483 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1484 if (status)
1485 dev_err(&port->dev,
1486 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001487 __func__, status);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491/*
1492 * The timer is currently only used to send queued packets to
1493 * the tty in cases where the protocol provides no own handshaking
1494 * to initiate the transfer.
1495 */
1496static void timeout_handler(unsigned long data)
1497{
1498 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1499
1500 /* send the next queued packet to the tty port */
1501 if (garmin_data_p->mode == MODE_NATIVE)
1502 if (garmin_data_p->flags & FLAGS_QUEUING)
1503 garmin_flush_queue(garmin_data_p);
1504}
1505
1506
1507
Alan Cox95da3102008-07-22 11:09:07 +01001508static int garmin_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 int status = 0;
1511 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001512 struct garmin_data *garmin_data_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Harvey Harrison441b62c2008-03-03 16:08:34 -08001514 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Burman Yan7ac9da12006-11-22 20:54:38 +02001516 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (garmin_data_p == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001518 dev_err(&port->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -ENOMEM;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 init_timer(&garmin_data_p->timer);
1522 spin_lock_init(&garmin_data_p->lock);
1523 INIT_LIST_HEAD(&garmin_data_p->pktlist);
Alan Coxaf6d7802008-07-22 11:11:44 +01001524 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1526 garmin_data_p->timer.function = timeout_handler;
1527 garmin_data_p->port = port;
1528 garmin_data_p->state = 0;
1529 garmin_data_p->count = 0;
1530 usb_set_serial_port_data(port, garmin_data_p);
1531
1532 status = garmin_init_session(port);
1533
1534 return status;
1535}
1536
1537
Alan Cox95da3102008-07-22 11:09:07 +01001538static void garmin_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001541 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Harvey Harrison441b62c2008-03-03 16:08:34 -08001543 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Alan Coxaf6d7802008-07-22 11:11:44 +01001545 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 del_timer_sync(&garmin_data_p->timer);
Alan Coxaf6d7802008-07-22 11:11:44 +01001547 kfree(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 usb_set_serial_port_data(port, NULL);
1549}
1550
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001553static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001554 .driver = {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001555 .owner = THIS_MODULE,
1556 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001557 },
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001558 .description = "Garmin GPS usb/tty",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001559 .usb_driver = &garmin_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 .num_ports = 1,
1562 .open = garmin_open,
1563 .close = garmin_close,
1564 .throttle = garmin_throttle,
1565 .unthrottle = garmin_unthrottle,
1566 .attach = garmin_attach,
1567 .shutdown = garmin_shutdown,
1568 .write = garmin_write,
1569 .write_room = garmin_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 .write_bulk_callback = garmin_write_bulk_callback,
1571 .read_bulk_callback = garmin_read_bulk_callback,
1572 .read_int_callback = garmin_read_int_callback,
1573};
1574
1575
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001576
Alan Coxaf6d7802008-07-22 11:11:44 +01001577static int __init garmin_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 int retval;
1580
1581 retval = usb_serial_register(&garmin_device);
1582 if (retval)
1583 goto failed_garmin_register;
1584 retval = usb_register(&garmin_driver);
1585 if (retval)
1586 goto failed_usb_register;
1587 info(DRIVER_DESC " " DRIVER_VERSION);
1588
1589 return 0;
1590failed_usb_register:
1591 usb_serial_deregister(&garmin_device);
1592failed_garmin_register:
1593 return retval;
1594}
1595
1596
Alan Coxaf6d7802008-07-22 11:11:44 +01001597static void __exit garmin_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Alan Coxaf6d7802008-07-22 11:11:44 +01001599 usb_deregister(&garmin_driver);
1600 usb_serial_deregister(&garmin_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603
1604
1605
1606module_init(garmin_init);
1607module_exit(garmin_exit);
1608
Alan Coxaf6d7802008-07-22 11:11:44 +01001609MODULE_AUTHOR(DRIVER_AUTHOR);
1610MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611MODULE_LICENSE("GPL");
1612
1613module_param(debug, bool, S_IWUSR | S_IRUGO);
1614MODULE_PARM_DESC(debug, "Debug enabled or not");
1615module_param(initial_mode, int, S_IRUGO);
1616MODULE_PARM_DESC(initial_mode, "Initial mode");
1617