xref: /linux/drivers/usb/serial/garmin_gps.c (revision 972c5ae961d6e5103e2b33d935cfa4145fd47140)
1 /*
2  * Garmin GPS driver
3  *
4  * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de
5  *
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 
26 #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>
36 #include <linux/uaccess.h>
37 #include <linux/atomic.h>
38 #include <linux/usb.h>
39 #include <linux/usb/serial.h>
40 
41 /* the mode to be set when the port ist opened */
42 static int initial_mode = 1;
43 
44 /* debug flag */
45 static bool debug;
46 
47 #define GARMIN_VENDOR_ID             0x091E
48 
49 /*
50  * Version Information
51  */
52 
53 #define VERSION_MAJOR	0
54 #define VERSION_MINOR	36
55 
56 #define _STR(s) #s
57 #define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
58 #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 
66 /* size of the header of a packet using the usb protocol */
67 #define GARMIN_PKTHDR_LENGTH	12
68 
69 /* max. possible size of a packet using the serial protocol */
70 #define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
71 
72 /*  max. possible size of a packet with worst case stuffing */
73 #define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
74 
75 /* 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)
82 
83 /* 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)
85 
86 /* 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 */
89 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
90 
91 /* max. size of incoming private packets (header+1 param) */
92 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
93 
94 #define GARMIN_LAYERID_TRANSPORT  0
95 #define GARMIN_LAYERID_APPL      20
96 /* our own layer-id to use for some control mechanisms */
97 #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 
104 /* packet ids used in private layer */
105 #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 */
119 struct garmin_packet {
120 	struct list_head  list;
121 	int               seq;
122 	/* the real size of the data array, always > 0 */
123 	int               size;
124 	__u8              data[1];
125 };
126 
127 /* structure used to keep the current state of the driver */
128 struct garmin_data {
129 	__u8   state;
130 	__u16  flags;
131 	__u8   mode;
132 	__u8   count;
133 	__u8   pkt_id;
134 	__u32  serial_num;
135 	struct timer_list timer;
136 	struct usb_serial_port *port;
137 	int    seq_counter;
138 	int    insize;
139 	int    outsize;
140 	__u8   inbuffer [GPS_IN_BUFSIZ];  /* tty -> usb */
141 	__u8   outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
142 	__u8   privpkt[4*6];
143 	spinlock_t lock;
144 	struct list_head pktlist;
145 };
146 
147 
148 #define STATE_NEW            0
149 #define STATE_INITIAL_DELAY  1
150 #define STATE_TIMEOUT        2
151 #define STATE_SESSION_REQ1   3
152 #define STATE_SESSION_REQ2   4
153 #define STATE_ACTIVE         5
154 
155 #define STATE_RESET	     8
156 #define STATE_DISCONNECTED   9
157 #define STATE_WAIT_TTY_ACK  10
158 #define STATE_GSP_WAIT_DATA 11
159 
160 #define MODE_NATIVE          0
161 #define MODE_GARMIN_SERIAL   1
162 
163 /* Flags used in garmin_data.flags: */
164 #define FLAGS_SESSION_REPLY_MASK  0x00C0
165 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
166 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
167 #define FLAGS_BULK_IN_ACTIVE      0x0020
168 #define FLAGS_BULK_IN_RESTART     0x0010
169 #define FLAGS_THROTTLED           0x0008
170 #define APP_REQ_SEEN              0x0004
171 #define APP_RESP_SEEN             0x0002
172 #define CLEAR_HALT_REQUIRED       0x0001
173 
174 #define FLAGS_QUEUING             0x0100
175 #define FLAGS_DROP_DATA           0x0800
176 
177 #define FLAGS_GSP_SKIP            0x1000
178 #define FLAGS_GSP_DLESEEN         0x2000
179 
180 
181 
182 
183 
184 
185 /* function prototypes */
186 static int gsp_next_packet(struct garmin_data *garmin_data_p);
187 static int garmin_write_bulk(struct usb_serial_port *port,
188 			     const unsigned char *buf, int count,
189 			     int dismiss_ack);
190 
191 /* some special packets to be send or received */
192 static unsigned char const GARMIN_START_SESSION_REQ[]
193 	= { 0, 0, 0, 0,  5, 0, 0, 0, 0, 0, 0, 0 };
194 static unsigned char const GARMIN_START_SESSION_REPLY[]
195 	= { 0, 0, 0, 0,  6, 0, 0, 0, 4, 0, 0, 0 };
196 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
197 	= { 0, 0, 0, 0,  2, 0, 0, 0, 0, 0, 0, 0 };
198 static unsigned char const GARMIN_APP_LAYER_REPLY[]
199 	= { 0x14, 0, 0, 0 };
200 static unsigned char const GARMIN_START_PVT_REQ[]
201 	= { 20, 0, 0, 0,  10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
202 static unsigned char const GARMIN_STOP_PVT_REQ[]
203 	= { 20, 0, 0, 0,  10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
204 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
205 	= { 20, 0, 0, 0,  10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
206 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
207 	= { 20, 0, 0, 0,  10, 0, 0, 0, 1, 0, 0, 0, 0 };
208 static unsigned char const PRIVATE_REQ[]
209 	=    { 0x4B, 0x6E, 0x10, 0x01,  0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
210 
211 
212 
213 static const struct usb_device_id id_table[] = {
214 	/* the same device id seems to be used by all
215 	   usb enabled GPS devices */
216 	{ USB_DEVICE(GARMIN_VENDOR_ID, 3) },
217 	{ }					/* Terminating entry */
218 };
219 
220 MODULE_DEVICE_TABLE(usb, id_table);
221 
222 static struct usb_driver garmin_driver = {
223 	.name =		"garmin_gps",
224 	.probe =	usb_serial_probe,
225 	.disconnect =	usb_serial_disconnect,
226 	.id_table =	id_table,
227 	.no_dynamic_id = 1,
228 };
229 
230 
231 static inline int getLayerId(const __u8 *usbPacket)
232 {
233 	return __le32_to_cpup((__le32 *)(usbPacket));
234 }
235 
236 static inline int getPacketId(const __u8 *usbPacket)
237 {
238 	return __le32_to_cpup((__le32 *)(usbPacket+4));
239 }
240 
241 static inline int getDataLength(const __u8 *usbPacket)
242 {
243 	return __le32_to_cpup((__le32 *)(usbPacket+8));
244 }
245 
246 
247 /*
248  * check if the usb-packet in buf contains an abort-transfer command.
249  * (if yes, all queued data will be dropped)
250  */
251 static inline int isAbortTrfCmnd(const unsigned char *buf)
252 {
253 	if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
254 					sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
255 	    0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
256 					sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
257 		return 1;
258 	else
259 		return 0;
260 }
261 
262 
263 
264 static void send_to_tty(struct usb_serial_port *port,
265 			char *data, unsigned int actual_length)
266 {
267 	struct tty_struct *tty = tty_port_tty_get(&port->port);
268 
269 	if (tty && actual_length) {
270 
271 		usb_serial_debug_data(debug, &port->dev,
272 					__func__, actual_length, data);
273 
274 		tty_insert_flip_string(tty, data, actual_length);
275 		tty_flip_buffer_push(tty);
276 	}
277 	tty_kref_put(tty);
278 }
279 
280 
281 /******************************************************************************
282  * packet queue handling
283  ******************************************************************************/
284 
285 /*
286  * queue a received (usb-)packet for later processing
287  */
288 static int pkt_add(struct garmin_data *garmin_data_p,
289 		   unsigned char *data, unsigned int data_length)
290 {
291 	int state = 0;
292 	int result = 0;
293 	unsigned long flags;
294 	struct garmin_packet *pkt;
295 
296 	/* process only packets containg data ... */
297 	if (data_length) {
298 		pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
299 								GFP_ATOMIC);
300 		if (pkt == NULL) {
301 			dev_err(&garmin_data_p->port->dev, "out of memory\n");
302 			return 0;
303 		}
304 		pkt->size = data_length;
305 		memcpy(pkt->data, data, data_length);
306 
307 		spin_lock_irqsave(&garmin_data_p->lock, flags);
308 		garmin_data_p->flags |= FLAGS_QUEUING;
309 		result = list_empty(&garmin_data_p->pktlist);
310 		pkt->seq = garmin_data_p->seq_counter++;
311 		list_add_tail(&pkt->list, &garmin_data_p->pktlist);
312 		state = garmin_data_p->state;
313 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
314 
315 		dbg("%s - added: pkt: %d - %d bytes",
316 			__func__, pkt->seq, data_length);
317 
318 		/* in serial mode, if someone is waiting for data from
319 		   the device, convert and send the next packet to tty. */
320 		if (result && (state == STATE_GSP_WAIT_DATA))
321 			gsp_next_packet(garmin_data_p);
322 	}
323 	return result;
324 }
325 
326 
327 /* get the next pending packet */
328 static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
329 {
330 	unsigned long flags;
331 	struct garmin_packet *result = NULL;
332 
333 	spin_lock_irqsave(&garmin_data_p->lock, flags);
334 	if (!list_empty(&garmin_data_p->pktlist)) {
335 		result = (struct garmin_packet *)garmin_data_p->pktlist.next;
336 		list_del(&result->list);
337 	}
338 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
339 	return result;
340 }
341 
342 
343 /* free up all queued data */
344 static void pkt_clear(struct garmin_data *garmin_data_p)
345 {
346 	unsigned long flags;
347 	struct garmin_packet *result = NULL;
348 
349 	dbg("%s", __func__);
350 
351 	spin_lock_irqsave(&garmin_data_p->lock, flags);
352 	while (!list_empty(&garmin_data_p->pktlist)) {
353 		result = (struct garmin_packet *)garmin_data_p->pktlist.next;
354 		list_del(&result->list);
355 		kfree(result);
356 	}
357 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
358 }
359 
360 
361 /******************************************************************************
362  * garmin serial protocol handling handling
363  ******************************************************************************/
364 
365 /* send an ack packet back to the tty */
366 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
367 {
368 	__u8 pkt[10];
369 	__u8 cksum = 0;
370 	__u8 *ptr = pkt;
371 	unsigned  l = 0;
372 
373 	dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
374 
375 	*ptr++ = DLE;
376 	*ptr++ = ACK;
377 	cksum += ACK;
378 
379 	*ptr++ = 2;
380 	cksum += 2;
381 
382 	*ptr++ = pkt_id;
383 	cksum += pkt_id;
384 
385 	if (pkt_id == DLE)
386 		*ptr++ = DLE;
387 
388 	*ptr++ = 0;
389 	*ptr++ = 0xFF & (-cksum);
390 	*ptr++ = DLE;
391 	*ptr++ = ETX;
392 
393 	l = ptr-pkt;
394 
395 	send_to_tty(garmin_data_p->port, pkt, l);
396 	return 0;
397 }
398 
399 
400 
401 /*
402  * called for a complete packet received from tty layer
403  *
404  * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
405  * at GSP_INITIAL_OFFSET.
406  *
407  * count - number of bytes in the input buffer including space reserved for
408  *         the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
409  *         (including pkt-id, data-length a. cksum)
410  */
411 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
412 {
413 	unsigned long flags;
414 	const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
415 	__le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
416 
417 	int cksum = 0;
418 	int n = 0;
419 	int pktid = recpkt[0];
420 	int size = recpkt[1];
421 
422 	usb_serial_debug_data(debug, &garmin_data_p->port->dev,
423 			       __func__, count-GSP_INITIAL_OFFSET, recpkt);
424 
425 	if (size != (count-GSP_INITIAL_OFFSET-3)) {
426 		dbg("%s - invalid size, expected %d bytes, got %d",
427 			__func__, size, (count-GSP_INITIAL_OFFSET-3));
428 		return -EINVPKT;
429 	}
430 
431 	cksum += *recpkt++;
432 	cksum += *recpkt++;
433 
434 	/* sanity check, remove after test ... */
435 	if ((__u8 *)&(usbdata[3]) != recpkt) {
436 		dbg("%s - ptr mismatch %p - %p",
437 			__func__, &(usbdata[4]), recpkt);
438 		return -EINVPKT;
439 	}
440 
441 	while (n < size) {
442 		cksum += *recpkt++;
443 		n++;
444 	}
445 
446 	if ((0xff & (cksum + *recpkt)) != 0) {
447 		dbg("%s - invalid checksum, expected %02x, got %02x",
448 			__func__, 0xff & -cksum, 0xff & *recpkt);
449 		return -EINVPKT;
450 	}
451 
452 	usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
453 	usbdata[1] = __cpu_to_le32(pktid);
454 	usbdata[2] = __cpu_to_le32(size);
455 
456 	garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
457 			   GARMIN_PKTHDR_LENGTH+size, 0);
458 
459 	/* if this was an abort-transfer command, flush all
460 	   queued data. */
461 	if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
462 		spin_lock_irqsave(&garmin_data_p->lock, flags);
463 		garmin_data_p->flags |= FLAGS_DROP_DATA;
464 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
465 		pkt_clear(garmin_data_p);
466 	}
467 
468 	return count;
469 }
470 
471 
472 
473 /*
474  * Called for data received from tty
475  *
476  * buf contains the data read, it may span more than one packet or even
477  * incomplete packets
478  *
479  * input record should be a serial-record, but it may not be complete.
480  * Copy it into our local buffer, until an etx is seen (or an error
481  * occurs).
482  * Once the record is complete, convert into a usb packet and send it
483  * to the bulk pipe, send an ack back to the tty.
484  *
485  * If the input is an ack, just send the last queued packet to the
486  * tty layer.
487  *
488  * if the input is an abort command, drop all queued data.
489  */
490 
491 static int gsp_receive(struct garmin_data *garmin_data_p,
492 		       const unsigned char *buf, int count)
493 {
494 	unsigned long flags;
495 	int offs = 0;
496 	int ack_or_nak_seen = 0;
497 	__u8 *dest;
498 	int size;
499 	/* dleSeen: set if last byte read was a DLE */
500 	int dleSeen;
501 	/* skip: if set, skip incoming data until possible start of
502 	 *       new packet
503 	 */
504 	int skip;
505 	__u8 data;
506 
507 	spin_lock_irqsave(&garmin_data_p->lock, flags);
508 	dest = garmin_data_p->inbuffer;
509 	size = garmin_data_p->insize;
510 	dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
511 	skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
512 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
513 
514 	/* dbg("%s - dle=%d skip=%d size=%d count=%d",
515 		__func__, dleSeen, skip, size, count); */
516 
517 	if (size == 0)
518 		size = GSP_INITIAL_OFFSET;
519 
520 	while (offs < count) {
521 
522 		data = *(buf+offs);
523 		offs++;
524 
525 		if (data == DLE) {
526 			if (skip) { /* start of a new pkt */
527 				skip = 0;
528 				size = GSP_INITIAL_OFFSET;
529 				dleSeen = 1;
530 			} else if (dleSeen) {
531 				dest[size++] = data;
532 				dleSeen = 0;
533 			} else {
534 				dleSeen = 1;
535 			}
536 		} else if (data == ETX) {
537 			if (dleSeen) {
538 				/* packet complete */
539 
540 				data = dest[GSP_INITIAL_OFFSET];
541 
542 				if (data == ACK) {
543 					ack_or_nak_seen = ACK;
544 					dbg("ACK packet complete.");
545 				} else if (data == NAK) {
546 					ack_or_nak_seen = NAK;
547 					dbg("NAK packet complete.");
548 				} else {
549 					dbg("packet complete - id=0x%X.",
550 						0xFF & data);
551 					gsp_rec_packet(garmin_data_p, size);
552 				}
553 
554 				skip = 1;
555 				size = GSP_INITIAL_OFFSET;
556 				dleSeen = 0;
557 			} else {
558 				dest[size++] = data;
559 			}
560 		} else if (!skip) {
561 
562 			if (dleSeen) {
563 				size = GSP_INITIAL_OFFSET;
564 				dleSeen = 0;
565 			}
566 
567 			dest[size++] = data;
568 		}
569 
570 		if (size >= GPS_IN_BUFSIZ) {
571 			dbg("%s - packet too large.", __func__);
572 			skip = 1;
573 			size = GSP_INITIAL_OFFSET;
574 			dleSeen = 0;
575 		}
576 	}
577 
578 	spin_lock_irqsave(&garmin_data_p->lock, flags);
579 
580 	garmin_data_p->insize = size;
581 
582 	/* copy flags back to structure */
583 	if (skip)
584 		garmin_data_p->flags |= FLAGS_GSP_SKIP;
585 	else
586 		garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
587 
588 	if (dleSeen)
589 		garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
590 	else
591 		garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
592 
593 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
594 
595 	if (ack_or_nak_seen) {
596 		if (gsp_next_packet(garmin_data_p) > 0)
597 			garmin_data_p->state = STATE_ACTIVE;
598 		else
599 			garmin_data_p->state = STATE_GSP_WAIT_DATA;
600 	}
601 	return count;
602 }
603 
604 
605 
606 /*
607  * Sends a usb packet to the tty
608  *
609  * Assumes, that all packages and at an usb-packet boundary.
610  *
611  * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
612  */
613 static int gsp_send(struct garmin_data *garmin_data_p,
614 		    const unsigned char *buf, int count)
615 {
616 	const unsigned char *src;
617 	unsigned char *dst;
618 	int pktid = 0;
619 	int datalen = 0;
620 	int cksum = 0;
621 	int i = 0;
622 	int k;
623 
624 	dbg("%s - state %d - %d bytes.", __func__,
625 					garmin_data_p->state, count);
626 
627 	k = garmin_data_p->outsize;
628 	if ((k+count) > GPS_OUT_BUFSIZ) {
629 		dbg("packet too large");
630 		garmin_data_p->outsize = 0;
631 		return -4;
632 	}
633 
634 	memcpy(garmin_data_p->outbuffer+k, buf, count);
635 	k += count;
636 	garmin_data_p->outsize = k;
637 
638 	if (k >= GARMIN_PKTHDR_LENGTH) {
639 		pktid  = getPacketId(garmin_data_p->outbuffer);
640 		datalen = getDataLength(garmin_data_p->outbuffer);
641 		i = GARMIN_PKTHDR_LENGTH + datalen;
642 		if (k < i)
643 			return 0;
644 	} else {
645 		return 0;
646 	}
647 
648 	dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
649 
650 	/* garmin_data_p->outbuffer now contains a complete packet */
651 
652 	usb_serial_debug_data(debug, &garmin_data_p->port->dev,
653 				__func__, k, garmin_data_p->outbuffer);
654 
655 	garmin_data_p->outsize = 0;
656 
657 	if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
658 		dbg("not an application packet (%d)",
659 				getLayerId(garmin_data_p->outbuffer));
660 		return -1;
661 	}
662 
663 	if (pktid > 255) {
664 		dbg("packet-id %d too large", pktid);
665 		return -2;
666 	}
667 
668 	if (datalen > 255) {
669 		dbg("packet-size %d too large", datalen);
670 		return -3;
671 	}
672 
673 	/* the serial protocol should be able to handle this packet */
674 
675 	k = 0;
676 	src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
677 	for (i = 0; i < datalen; i++) {
678 		if (*src++ == DLE)
679 			k++;
680 	}
681 
682 	src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
683 	if (k > (GARMIN_PKTHDR_LENGTH-2)) {
684 		/* can't add stuffing DLEs in place, move data to end
685 		   of buffer ... */
686 		dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
687 		memcpy(dst, src, datalen);
688 		src = dst;
689 	}
690 
691 	dst = garmin_data_p->outbuffer;
692 
693 	*dst++ = DLE;
694 	*dst++ = pktid;
695 	cksum += pktid;
696 	*dst++ = datalen;
697 	cksum += datalen;
698 	if (datalen == DLE)
699 		*dst++ = DLE;
700 
701 	for (i = 0; i < datalen; i++) {
702 		__u8 c = *src++;
703 		*dst++ = c;
704 		cksum += c;
705 		if (c == DLE)
706 			*dst++ = DLE;
707 	}
708 
709 	cksum = 0xFF & -cksum;
710 	*dst++ = cksum;
711 	if (cksum == DLE)
712 		*dst++ = DLE;
713 	*dst++ = DLE;
714 	*dst++ = ETX;
715 
716 	i = dst-garmin_data_p->outbuffer;
717 
718 	send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
719 
720 	garmin_data_p->pkt_id = pktid;
721 	garmin_data_p->state  = STATE_WAIT_TTY_ACK;
722 
723 	return i;
724 }
725 
726 
727 /*
728  * Process the next pending data packet - if there is one
729  */
730 static int gsp_next_packet(struct garmin_data *garmin_data_p)
731 {
732 	int result = 0;
733 	struct garmin_packet *pkt = NULL;
734 
735 	while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
736 		dbg("%s - next pkt: %d", __func__, pkt->seq);
737 		result = gsp_send(garmin_data_p, pkt->data, pkt->size);
738 		if (result > 0) {
739 			kfree(pkt);
740 			return result;
741 		}
742 		kfree(pkt);
743 	}
744 	return result;
745 }
746 
747 
748 
749 /******************************************************************************
750  * garmin native mode
751  ******************************************************************************/
752 
753 
754 /*
755  * Called for data received from tty
756  *
757  * The input data is expected to be in garmin usb-packet format.
758  *
759  * buf contains the data read, it may span more than one packet
760  * or even incomplete packets
761  */
762 static int nat_receive(struct garmin_data *garmin_data_p,
763 		       const unsigned char *buf, int count)
764 {
765 	unsigned long flags;
766 	__u8 *dest;
767 	int offs = 0;
768 	int result = count;
769 	int len;
770 
771 	while (offs < count) {
772 		/* if buffer contains header, copy rest of data */
773 		if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
774 			len = GARMIN_PKTHDR_LENGTH
775 			      +getDataLength(garmin_data_p->inbuffer);
776 		else
777 			len = GARMIN_PKTHDR_LENGTH;
778 
779 		if (len >= GPS_IN_BUFSIZ) {
780 			/* seems to be an invalid packet, ignore rest
781 			   of input */
782 			dbg("%s - packet size too large: %d", __func__, len);
783 			garmin_data_p->insize = 0;
784 			count = 0;
785 			result = -EINVPKT;
786 		} else {
787 			len -= garmin_data_p->insize;
788 			if (len > (count-offs))
789 				len = (count-offs);
790 			if (len > 0) {
791 				dest = garmin_data_p->inbuffer
792 						+ garmin_data_p->insize;
793 				memcpy(dest, buf+offs, len);
794 				garmin_data_p->insize += len;
795 				offs += len;
796 			}
797 		}
798 
799 		/* do we have a complete packet ? */
800 		if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
801 			len = GARMIN_PKTHDR_LENGTH+
802 			   getDataLength(garmin_data_p->inbuffer);
803 			if (garmin_data_p->insize >= len) {
804 				garmin_write_bulk(garmin_data_p->port,
805 						   garmin_data_p->inbuffer,
806 						   len, 0);
807 				garmin_data_p->insize = 0;
808 
809 				/* if this was an abort-transfer command,
810 				   flush all queued data. */
811 				if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
812 					spin_lock_irqsave(&garmin_data_p->lock,
813 									flags);
814 					garmin_data_p->flags |= FLAGS_DROP_DATA;
815 					spin_unlock_irqrestore(
816 						&garmin_data_p->lock, flags);
817 					pkt_clear(garmin_data_p);
818 				}
819 			}
820 		}
821 	}
822 	return result;
823 }
824 
825 
826 /******************************************************************************
827  * private packets
828  ******************************************************************************/
829 
830 static void priv_status_resp(struct usb_serial_port *port)
831 {
832 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
833 	__le32 *pkt = (__le32 *)garmin_data_p->privpkt;
834 
835 	pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
836 	pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
837 	pkt[2] = __cpu_to_le32(12);
838 	pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
839 	pkt[4] = __cpu_to_le32(garmin_data_p->mode);
840 	pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
841 
842 	send_to_tty(port, (__u8 *)pkt, 6 * 4);
843 }
844 
845 
846 /******************************************************************************
847  * Garmin specific driver functions
848  ******************************************************************************/
849 
850 static int process_resetdev_request(struct usb_serial_port *port)
851 {
852 	unsigned long flags;
853 	int status;
854 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
855 
856 	spin_lock_irqsave(&garmin_data_p->lock, flags);
857 	garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
858 	garmin_data_p->state = STATE_RESET;
859 	garmin_data_p->serial_num = 0;
860 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
861 
862 	usb_kill_urb(port->interrupt_in_urb);
863 	dbg("%s - usb_reset_device", __func__);
864 	status = usb_reset_device(port->serial->dev);
865 	if (status)
866 		dbg("%s - usb_reset_device failed: %d",
867 			__func__, status);
868 	return status;
869 }
870 
871 
872 
873 /*
874  * clear all cached data
875  */
876 static int garmin_clear(struct garmin_data *garmin_data_p)
877 {
878 	unsigned long flags;
879 	int status = 0;
880 
881 	/* flush all queued data */
882 	pkt_clear(garmin_data_p);
883 
884 	spin_lock_irqsave(&garmin_data_p->lock, flags);
885 	garmin_data_p->insize = 0;
886 	garmin_data_p->outsize = 0;
887 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
888 
889 	return status;
890 }
891 
892 
893 static int garmin_init_session(struct usb_serial_port *port)
894 {
895 	struct usb_serial *serial = port->serial;
896 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
897 	int status = 0;
898 	int i = 0;
899 
900 	if (status == 0) {
901 		usb_kill_urb(port->interrupt_in_urb);
902 
903 		dbg("%s - adding interrupt input", __func__);
904 		status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
905 		if (status)
906 			dev_err(&serial->dev->dev,
907 			  "%s - failed submitting interrupt urb, error %d\n",
908 							__func__, status);
909 	}
910 
911 	/*
912 	 * using the initialization method from gpsbabel. See comments in
913 	 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
914 	 */
915 	if (status == 0) {
916 		dbg("%s - starting session ...", __func__);
917 		garmin_data_p->state = STATE_ACTIVE;
918 
919 		for (i = 0; i < 3; i++) {
920 			status = garmin_write_bulk(port,
921 					GARMIN_START_SESSION_REQ,
922 					sizeof(GARMIN_START_SESSION_REQ), 0);
923 
924 			if (status < 0)
925 				break;
926 		}
927 
928 		if (status > 0)
929 			status = 0;
930 	}
931 
932 	return status;
933 }
934 
935 
936 
937 static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
938 {
939 	unsigned long flags;
940 	int status = 0;
941 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
942 
943 	dbg("%s - port %d", __func__, port->number);
944 
945 	spin_lock_irqsave(&garmin_data_p->lock, flags);
946 	garmin_data_p->mode  = initial_mode;
947 	garmin_data_p->count = 0;
948 	garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
949 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
950 
951 	/* shutdown any bulk reads that might be going on */
952 	usb_kill_urb(port->write_urb);
953 	usb_kill_urb(port->read_urb);
954 
955 	if (garmin_data_p->state == STATE_RESET)
956 		status = garmin_init_session(port);
957 
958 	garmin_data_p->state = STATE_ACTIVE;
959 	return status;
960 }
961 
962 
963 static void garmin_close(struct usb_serial_port *port)
964 {
965 	struct usb_serial *serial = port->serial;
966 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
967 
968 	dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
969 		port->number, garmin_data_p->mode,
970 		garmin_data_p->state, garmin_data_p->flags);
971 
972 	if (!serial)
973 		return;
974 
975 	mutex_lock(&port->serial->disc_mutex);
976 
977 	if (!port->serial->disconnected)
978 		garmin_clear(garmin_data_p);
979 
980 	/* shutdown our urbs */
981 	usb_kill_urb(port->read_urb);
982 	usb_kill_urb(port->write_urb);
983 
984 	/* keep reset state so we know that we must start a new session */
985 	if (garmin_data_p->state != STATE_RESET)
986 		garmin_data_p->state = STATE_DISCONNECTED;
987 
988 	mutex_unlock(&port->serial->disc_mutex);
989 }
990 
991 
992 static void garmin_write_bulk_callback(struct urb *urb)
993 {
994 	struct usb_serial_port *port = urb->context;
995 
996 	if (port) {
997 		struct garmin_data *garmin_data_p =
998 					usb_get_serial_port_data(port);
999 
1000 		dbg("%s - port %d", __func__, port->number);
1001 
1002 		if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
1003 
1004 			if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1005 				gsp_send_ack(garmin_data_p,
1006 					((__u8 *)urb->transfer_buffer)[4]);
1007 			}
1008 		}
1009 		usb_serial_port_softint(port);
1010 	}
1011 
1012 	/* Ignore errors that resulted from garmin_write_bulk with
1013 	   dismiss_ack = 1 */
1014 
1015 	/* free up the transfer buffer, as usb_free_urb() does not do this */
1016 	kfree(urb->transfer_buffer);
1017 }
1018 
1019 
1020 static int garmin_write_bulk(struct usb_serial_port *port,
1021 			      const unsigned char *buf, int count,
1022 			      int dismiss_ack)
1023 {
1024 	unsigned long flags;
1025 	struct usb_serial *serial = port->serial;
1026 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1027 	struct urb *urb;
1028 	unsigned char *buffer;
1029 	int status;
1030 
1031 	dbg("%s - port %d, state %d", __func__, port->number,
1032 		garmin_data_p->state);
1033 
1034 	spin_lock_irqsave(&garmin_data_p->lock, flags);
1035 	garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1036 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1037 
1038 	buffer = kmalloc(count, GFP_ATOMIC);
1039 	if (!buffer) {
1040 		dev_err(&port->dev, "out of memory\n");
1041 		return -ENOMEM;
1042 	}
1043 
1044 	urb = usb_alloc_urb(0, GFP_ATOMIC);
1045 	if (!urb) {
1046 		dev_err(&port->dev, "no more free urbs\n");
1047 		kfree(buffer);
1048 		return -ENOMEM;
1049 	}
1050 
1051 	memcpy(buffer, buf, count);
1052 
1053 	usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
1054 
1055 	usb_fill_bulk_urb(urb, serial->dev,
1056 				usb_sndbulkpipe(serial->dev,
1057 					port->bulk_out_endpointAddress),
1058 				buffer, count,
1059 				garmin_write_bulk_callback,
1060 				dismiss_ack ? NULL : port);
1061 	urb->transfer_flags |= URB_ZERO_PACKET;
1062 
1063 	if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1064 
1065 		spin_lock_irqsave(&garmin_data_p->lock, flags);
1066 		garmin_data_p->flags |= APP_REQ_SEEN;
1067 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1068 
1069 		if (garmin_data_p->mode == MODE_GARMIN_SERIAL)  {
1070 			pkt_clear(garmin_data_p);
1071 			garmin_data_p->state = STATE_GSP_WAIT_DATA;
1072 		}
1073 	}
1074 
1075 	/* send it down the pipe */
1076 	status = usb_submit_urb(urb, GFP_ATOMIC);
1077 	if (status) {
1078 		dev_err(&port->dev,
1079 		   "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1080 				__func__, status);
1081 		count = status;
1082 	}
1083 
1084 	/* we are done with this urb, so let the host driver
1085 	 * really free it when it is finished with it */
1086 	usb_free_urb(urb);
1087 
1088 	return count;
1089 }
1090 
1091 static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1092 					 const unsigned char *buf, int count)
1093 {
1094 	int pktid, pktsiz, len;
1095 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1096 	__le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1097 
1098 	usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
1099 
1100 	if (garmin_data_p->state == STATE_RESET)
1101 		return -EIO;
1102 
1103 	/* check for our private packets */
1104 	if (count >= GARMIN_PKTHDR_LENGTH) {
1105 		len = PRIVPKTSIZ;
1106 		if (count < len)
1107 			len = count;
1108 
1109 		memcpy(garmin_data_p->privpkt, buf, len);
1110 
1111 		pktsiz = getDataLength(garmin_data_p->privpkt);
1112 		pktid  = getPacketId(garmin_data_p->privpkt);
1113 
1114 		if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1115 		    && GARMIN_LAYERID_PRIVATE ==
1116 				getLayerId(garmin_data_p->privpkt)) {
1117 
1118 			dbg("%s - processing private request %d",
1119 				__func__, pktid);
1120 
1121 			/* drop all unfinished transfers */
1122 			garmin_clear(garmin_data_p);
1123 
1124 			switch (pktid) {
1125 
1126 			case PRIV_PKTID_SET_DEBUG:
1127 				if (pktsiz != 4)
1128 					return -EINVPKT;
1129 				debug = __le32_to_cpu(privpkt[3]);
1130 				dbg("%s - debug level set to 0x%X",
1131 					__func__, debug);
1132 				break;
1133 
1134 			case PRIV_PKTID_SET_MODE:
1135 				if (pktsiz != 4)
1136 					return -EINVPKT;
1137 				garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1138 				dbg("%s - mode set to %d",
1139 					__func__, garmin_data_p->mode);
1140 				break;
1141 
1142 			case PRIV_PKTID_INFO_REQ:
1143 				priv_status_resp(port);
1144 				break;
1145 
1146 			case PRIV_PKTID_RESET_REQ:
1147 				process_resetdev_request(port);
1148 				break;
1149 
1150 			case PRIV_PKTID_SET_DEF_MODE:
1151 				if (pktsiz != 4)
1152 					return -EINVPKT;
1153 				initial_mode = __le32_to_cpu(privpkt[3]);
1154 				dbg("%s - initial_mode set to %d",
1155 					__func__,
1156 					garmin_data_p->mode);
1157 				break;
1158 			}
1159 			return count;
1160 		}
1161 	}
1162 
1163 	if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1164 		return gsp_receive(garmin_data_p, buf, count);
1165 	} else {	/* MODE_NATIVE */
1166 		return nat_receive(garmin_data_p, buf, count);
1167 	}
1168 }
1169 
1170 
1171 static int garmin_write_room(struct tty_struct *tty)
1172 {
1173 	struct usb_serial_port *port = tty->driver_data;
1174 	/*
1175 	 * Report back the bytes currently available in the output buffer.
1176 	 */
1177 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1178 	return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1179 }
1180 
1181 
1182 static void garmin_read_process(struct garmin_data *garmin_data_p,
1183 				 unsigned char *data, unsigned data_length,
1184 				 int bulk_data)
1185 {
1186 	unsigned long flags;
1187 
1188 	if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1189 		/* abort-transfer cmd is actice */
1190 		dbg("%s - pkt dropped", __func__);
1191 	} else if (garmin_data_p->state != STATE_DISCONNECTED &&
1192 		garmin_data_p->state != STATE_RESET) {
1193 
1194 		/* if throttling is active or postprecessing is required
1195 		   put the received data in the input queue, otherwise
1196 		   send it directly to the tty port */
1197 		if (garmin_data_p->flags & FLAGS_QUEUING) {
1198 			pkt_add(garmin_data_p, data, data_length);
1199 		} else if (bulk_data ||
1200 			   getLayerId(data) == GARMIN_LAYERID_APPL) {
1201 
1202 			spin_lock_irqsave(&garmin_data_p->lock, flags);
1203 			garmin_data_p->flags |= APP_RESP_SEEN;
1204 			spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1205 
1206 			if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1207 				pkt_add(garmin_data_p, data, data_length);
1208 			} else {
1209 				send_to_tty(garmin_data_p->port, data,
1210 						data_length);
1211 			}
1212 		}
1213 		/* ignore system layer packets ... */
1214 	}
1215 }
1216 
1217 
1218 static void garmin_read_bulk_callback(struct urb *urb)
1219 {
1220 	unsigned long flags;
1221 	struct usb_serial_port *port = urb->context;
1222 	struct usb_serial *serial =  port->serial;
1223 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1224 	unsigned char *data = urb->transfer_buffer;
1225 	int status = urb->status;
1226 	int retval;
1227 
1228 	dbg("%s - port %d", __func__, port->number);
1229 
1230 	if (!serial) {
1231 		dbg("%s - bad serial pointer, exiting", __func__);
1232 		return;
1233 	}
1234 
1235 	if (status) {
1236 		dbg("%s - nonzero read bulk status received: %d",
1237 			__func__, status);
1238 		return;
1239 	}
1240 
1241 	usb_serial_debug_data(debug, &port->dev,
1242 				__func__, urb->actual_length, data);
1243 
1244 	garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
1245 
1246 	if (urb->actual_length == 0 &&
1247 			0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1248 		spin_lock_irqsave(&garmin_data_p->lock, flags);
1249 		garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1250 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1251 		retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1252 		if (retval)
1253 			dev_err(&port->dev,
1254 				"%s - failed resubmitting read urb, error %d\n",
1255 				__func__, retval);
1256 	} else if (urb->actual_length > 0) {
1257 		/* Continue trying to read until nothing more is received  */
1258 		if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1259 			retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1260 			if (retval)
1261 				dev_err(&port->dev,
1262 					"%s - failed resubmitting read urb, "
1263 					"error %d\n", __func__, retval);
1264 		}
1265 	} else {
1266 		dbg("%s - end of bulk data", __func__);
1267 		spin_lock_irqsave(&garmin_data_p->lock, flags);
1268 		garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1269 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1270 	}
1271 }
1272 
1273 
1274 static void garmin_read_int_callback(struct urb *urb)
1275 {
1276 	unsigned long flags;
1277 	int retval;
1278 	struct usb_serial_port *port = urb->context;
1279 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1280 	unsigned char *data = urb->transfer_buffer;
1281 	int status = urb->status;
1282 
1283 	switch (status) {
1284 	case 0:
1285 		/* success */
1286 		break;
1287 	case -ECONNRESET:
1288 	case -ENOENT:
1289 	case -ESHUTDOWN:
1290 		/* this urb is terminated, clean up */
1291 		dbg("%s - urb shutting down with status: %d",
1292 			__func__, status);
1293 		return;
1294 	default:
1295 		dbg("%s - nonzero urb status received: %d",
1296 			__func__, status);
1297 		return;
1298 	}
1299 
1300 	usb_serial_debug_data(debug, &port->dev, __func__,
1301 				urb->actual_length, urb->transfer_buffer);
1302 
1303 	if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1304 	    0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1305 				sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1306 
1307 		dbg("%s - bulk data available.", __func__);
1308 
1309 		if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1310 
1311 			/* bulk data available */
1312 			retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1313 			if (retval) {
1314 				dev_err(&port->dev,
1315 				 "%s - failed submitting read urb, error %d\n",
1316 							__func__, retval);
1317 			} else {
1318 				spin_lock_irqsave(&garmin_data_p->lock, flags);
1319 				garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1320 				spin_unlock_irqrestore(&garmin_data_p->lock,
1321 									flags);
1322 			}
1323 		} else {
1324 			/* bulk-in transfer still active */
1325 			spin_lock_irqsave(&garmin_data_p->lock, flags);
1326 			garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1327 			spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1328 		}
1329 
1330 	} else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1331 			 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1332 					sizeof(GARMIN_START_SESSION_REPLY))) {
1333 
1334 		spin_lock_irqsave(&garmin_data_p->lock, flags);
1335 		garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1336 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1337 
1338 		/* save the serial number */
1339 		garmin_data_p->serial_num = __le32_to_cpup(
1340 					(__le32 *)(data+GARMIN_PKTHDR_LENGTH));
1341 
1342 		dbg("%s - start-of-session reply seen - serial %u.",
1343 			__func__, garmin_data_p->serial_num);
1344 	}
1345 
1346 	garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
1347 
1348 	retval = usb_submit_urb(urb, GFP_ATOMIC);
1349 	if (retval)
1350 		dev_err(&urb->dev->dev,
1351 			"%s - Error %d submitting interrupt urb\n",
1352 			__func__, retval);
1353 }
1354 
1355 
1356 /*
1357  * Sends the next queued packt to the tty port (garmin native mode only)
1358  * and then sets a timer to call itself again until all queued data
1359  * is sent.
1360  */
1361 static int garmin_flush_queue(struct garmin_data *garmin_data_p)
1362 {
1363 	unsigned long flags;
1364 	struct garmin_packet *pkt;
1365 
1366 	if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1367 		pkt = pkt_pop(garmin_data_p);
1368 		if (pkt != NULL) {
1369 			send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1370 			kfree(pkt);
1371 			mod_timer(&garmin_data_p->timer, (1)+jiffies);
1372 
1373 		} else {
1374 			spin_lock_irqsave(&garmin_data_p->lock, flags);
1375 			garmin_data_p->flags &= ~FLAGS_QUEUING;
1376 			spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1377 		}
1378 	}
1379 	return 0;
1380 }
1381 
1382 
1383 static void garmin_throttle(struct tty_struct *tty)
1384 {
1385 	struct usb_serial_port *port = tty->driver_data;
1386 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1387 
1388 	dbg("%s - port %d", __func__, port->number);
1389 	/* set flag, data received will be put into a queue
1390 	   for later processing */
1391 	spin_lock_irq(&garmin_data_p->lock);
1392 	garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1393 	spin_unlock_irq(&garmin_data_p->lock);
1394 }
1395 
1396 
1397 static void garmin_unthrottle(struct tty_struct *tty)
1398 {
1399 	struct usb_serial_port *port = tty->driver_data;
1400 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1401 	int status;
1402 
1403 	dbg("%s - port %d", __func__, port->number);
1404 	spin_lock_irq(&garmin_data_p->lock);
1405 	garmin_data_p->flags &= ~FLAGS_THROTTLED;
1406 	spin_unlock_irq(&garmin_data_p->lock);
1407 
1408 	/* in native mode send queued data to tty, in
1409 	   serial mode nothing needs to be done here */
1410 	if (garmin_data_p->mode == MODE_NATIVE)
1411 		garmin_flush_queue(garmin_data_p);
1412 
1413 	if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1414 		status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1415 		if (status)
1416 			dev_err(&port->dev,
1417 				"%s - failed resubmitting read urb, error %d\n",
1418 				__func__, status);
1419 	}
1420 }
1421 
1422 /*
1423  * The timer is currently only used to send queued packets to
1424  * the tty in cases where the protocol provides no own handshaking
1425  * to initiate the transfer.
1426  */
1427 static void timeout_handler(unsigned long data)
1428 {
1429 	struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1430 
1431 	/* send the next queued packet to the tty port */
1432 	if (garmin_data_p->mode == MODE_NATIVE)
1433 		if (garmin_data_p->flags & FLAGS_QUEUING)
1434 			garmin_flush_queue(garmin_data_p);
1435 }
1436 
1437 
1438 
1439 static int garmin_attach(struct usb_serial *serial)
1440 {
1441 	int status = 0;
1442 	struct usb_serial_port *port = serial->port[0];
1443 	struct garmin_data *garmin_data_p = NULL;
1444 
1445 	dbg("%s", __func__);
1446 
1447 	garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1448 	if (garmin_data_p == NULL) {
1449 		dev_err(&port->dev, "%s - Out of memory\n", __func__);
1450 		return -ENOMEM;
1451 	}
1452 	init_timer(&garmin_data_p->timer);
1453 	spin_lock_init(&garmin_data_p->lock);
1454 	INIT_LIST_HEAD(&garmin_data_p->pktlist);
1455 	/* garmin_data_p->timer.expires = jiffies + session_timeout; */
1456 	garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1457 	garmin_data_p->timer.function = timeout_handler;
1458 	garmin_data_p->port = port;
1459 	garmin_data_p->state = 0;
1460 	garmin_data_p->flags = 0;
1461 	garmin_data_p->count = 0;
1462 	usb_set_serial_port_data(port, garmin_data_p);
1463 
1464 	status = garmin_init_session(port);
1465 
1466 	return status;
1467 }
1468 
1469 
1470 static void garmin_disconnect(struct usb_serial *serial)
1471 {
1472 	struct usb_serial_port *port = serial->port[0];
1473 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1474 
1475 	dbg("%s", __func__);
1476 
1477 	usb_kill_urb(port->interrupt_in_urb);
1478 	del_timer_sync(&garmin_data_p->timer);
1479 }
1480 
1481 
1482 static void garmin_release(struct usb_serial *serial)
1483 {
1484 	struct usb_serial_port *port = serial->port[0];
1485 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1486 
1487 	dbg("%s", __func__);
1488 
1489 	kfree(garmin_data_p);
1490 }
1491 
1492 
1493 /* All of the device info needed */
1494 static struct usb_serial_driver garmin_device = {
1495 	.driver = {
1496 		.owner       = THIS_MODULE,
1497 		.name        = "garmin_gps",
1498 	},
1499 	.description         = "Garmin GPS usb/tty",
1500 	.usb_driver          = &garmin_driver,
1501 	.id_table            = id_table,
1502 	.num_ports           = 1,
1503 	.open                = garmin_open,
1504 	.close               = garmin_close,
1505 	.throttle            = garmin_throttle,
1506 	.unthrottle          = garmin_unthrottle,
1507 	.attach              = garmin_attach,
1508 	.disconnect          = garmin_disconnect,
1509 	.release             = garmin_release,
1510 	.write               = garmin_write,
1511 	.write_room          = garmin_write_room,
1512 	.write_bulk_callback = garmin_write_bulk_callback,
1513 	.read_bulk_callback  = garmin_read_bulk_callback,
1514 	.read_int_callback   = garmin_read_int_callback,
1515 };
1516 
1517 
1518 
1519 static int __init garmin_init(void)
1520 {
1521 	int retval;
1522 
1523 	retval = usb_serial_register(&garmin_device);
1524 	if (retval)
1525 		goto failed_garmin_register;
1526 	retval = usb_register(&garmin_driver);
1527 	if (retval)
1528 		goto failed_usb_register;
1529 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1530 	       DRIVER_DESC "\n");
1531 
1532 	return 0;
1533 failed_usb_register:
1534 	usb_serial_deregister(&garmin_device);
1535 failed_garmin_register:
1536 	return retval;
1537 }
1538 
1539 
1540 static void __exit garmin_exit(void)
1541 {
1542 	usb_deregister(&garmin_driver);
1543 	usb_serial_deregister(&garmin_device);
1544 }
1545 
1546 
1547 
1548 
1549 module_init(garmin_init);
1550 module_exit(garmin_exit);
1551 
1552 MODULE_AUTHOR(DRIVER_AUTHOR);
1553 MODULE_DESCRIPTION(DRIVER_DESC);
1554 MODULE_LICENSE("GPL");
1555 
1556 module_param(debug, bool, S_IWUSR | S_IRUGO);
1557 MODULE_PARM_DESC(debug, "Debug enabled or not");
1558 module_param(initial_mode, int, S_IRUGO);
1559 MODULE_PARM_DESC(initial_mode, "Initial mode");
1560 
1561