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