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