xref: /openbmc/linux/drivers/usb/serial/garmin_gps.c (revision f3539c12)
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 (memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
241 			sizeof(GARMIN_STOP_TRANSFER_REQ)) == 0 ||
242 	    memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
243 			sizeof(GARMIN_STOP_TRANSFER_REQ_V2)) == 0)
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 			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++ = (-cksum) & 0xFF;
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 (((cksum + *recpkt) & 0xff) != 0) {
427 		dev_dbg(dev, "%s - invalid checksum, expected %02x, got %02x\n",
428 			__func__, -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 							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 (getLayerId(garmin_data_p->outbuffer) != GARMIN_LAYERID_APPL) {
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 = -cksum & 0xFF;
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 
864 	/* flush all queued data */
865 	pkt_clear(garmin_data_p);
866 
867 	spin_lock_irqsave(&garmin_data_p->lock, flags);
868 	garmin_data_p->insize = 0;
869 	garmin_data_p->outsize = 0;
870 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
871 
872 	return 0;
873 }
874 
875 
876 static int garmin_init_session(struct usb_serial_port *port)
877 {
878 	struct usb_serial *serial = port->serial;
879 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
880 	int status = 0;
881 	int i = 0;
882 
883 	if (status == 0) {
884 		usb_kill_urb(port->interrupt_in_urb);
885 
886 		dev_dbg(&serial->dev->dev, "%s - adding interrupt input\n", __func__);
887 		status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
888 		if (status)
889 			dev_err(&serial->dev->dev,
890 			  "%s - failed submitting interrupt urb, error %d\n",
891 							__func__, status);
892 	}
893 
894 	/*
895 	 * using the initialization method from gpsbabel. See comments in
896 	 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
897 	 */
898 	if (status == 0) {
899 		dev_dbg(&serial->dev->dev, "%s - starting session ...\n", __func__);
900 		garmin_data_p->state = STATE_ACTIVE;
901 
902 		for (i = 0; i < 3; i++) {
903 			status = garmin_write_bulk(port,
904 					GARMIN_START_SESSION_REQ,
905 					sizeof(GARMIN_START_SESSION_REQ), 0);
906 
907 			if (status < 0)
908 				break;
909 		}
910 
911 		if (status > 0)
912 			status = 0;
913 	}
914 
915 	return status;
916 }
917 
918 
919 
920 static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
921 {
922 	unsigned long flags;
923 	int status = 0;
924 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
925 
926 	spin_lock_irqsave(&garmin_data_p->lock, flags);
927 	garmin_data_p->mode  = initial_mode;
928 	garmin_data_p->count = 0;
929 	garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
930 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
931 
932 	/* shutdown any bulk reads that might be going on */
933 	usb_kill_urb(port->write_urb);
934 	usb_kill_urb(port->read_urb);
935 
936 	if (garmin_data_p->state == STATE_RESET)
937 		status = garmin_init_session(port);
938 
939 	garmin_data_p->state = STATE_ACTIVE;
940 	return status;
941 }
942 
943 
944 static void garmin_close(struct usb_serial_port *port)
945 {
946 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
947 
948 	dev_dbg(&port->dev, "%s - mode=%d state=%d flags=0x%X\n",
949 		__func__, garmin_data_p->mode, garmin_data_p->state,
950 		garmin_data_p->flags);
951 
952 	garmin_clear(garmin_data_p);
953 
954 	/* shutdown our urbs */
955 	usb_kill_urb(port->read_urb);
956 	usb_kill_urb(port->write_urb);
957 
958 	/* keep reset state so we know that we must start a new session */
959 	if (garmin_data_p->state != STATE_RESET)
960 		garmin_data_p->state = STATE_DISCONNECTED;
961 }
962 
963 
964 static void garmin_write_bulk_callback(struct urb *urb)
965 {
966 	struct usb_serial_port *port = urb->context;
967 
968 	if (port) {
969 		struct garmin_data *garmin_data_p =
970 					usb_get_serial_port_data(port);
971 
972 		if (getLayerId(urb->transfer_buffer) == GARMIN_LAYERID_APPL) {
973 
974 			if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
975 				gsp_send_ack(garmin_data_p,
976 					((__u8 *)urb->transfer_buffer)[4]);
977 			}
978 		}
979 		usb_serial_port_softint(port);
980 	}
981 
982 	/* Ignore errors that resulted from garmin_write_bulk with
983 	   dismiss_ack = 1 */
984 
985 	/* free up the transfer buffer, as usb_free_urb() does not do this */
986 	kfree(urb->transfer_buffer);
987 }
988 
989 
990 static int garmin_write_bulk(struct usb_serial_port *port,
991 			      const unsigned char *buf, int count,
992 			      int dismiss_ack)
993 {
994 	unsigned long flags;
995 	struct usb_serial *serial = port->serial;
996 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
997 	struct urb *urb;
998 	unsigned char *buffer;
999 	int status;
1000 
1001 	spin_lock_irqsave(&garmin_data_p->lock, flags);
1002 	garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1003 	spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1004 
1005 	buffer = kmalloc(count, GFP_ATOMIC);
1006 	if (!buffer)
1007 		return -ENOMEM;
1008 
1009 	urb = usb_alloc_urb(0, GFP_ATOMIC);
1010 	if (!urb) {
1011 		kfree(buffer);
1012 		return -ENOMEM;
1013 	}
1014 
1015 	memcpy(buffer, buf, count);
1016 
1017 	usb_serial_debug_data(&port->dev, __func__, count, buffer);
1018 
1019 	usb_fill_bulk_urb(urb, serial->dev,
1020 				usb_sndbulkpipe(serial->dev,
1021 					port->bulk_out_endpointAddress),
1022 				buffer, count,
1023 				garmin_write_bulk_callback,
1024 				dismiss_ack ? NULL : port);
1025 	urb->transfer_flags |= URB_ZERO_PACKET;
1026 
1027 	if (getLayerId(buffer) == GARMIN_LAYERID_APPL) {
1028 
1029 		spin_lock_irqsave(&garmin_data_p->lock, flags);
1030 		garmin_data_p->flags |= APP_REQ_SEEN;
1031 		spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1032 
1033 		if (garmin_data_p->mode == MODE_GARMIN_SERIAL)  {
1034 			pkt_clear(garmin_data_p);
1035 			garmin_data_p->state = STATE_GSP_WAIT_DATA;
1036 		}
1037 	}
1038 
1039 	/* send it down the pipe */
1040 	status = usb_submit_urb(urb, GFP_ATOMIC);
1041 	if (status) {
1042 		dev_err(&port->dev,
1043 		   "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1044 				__func__, status);
1045 		count = status;
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(unsigned long data)
1373 {
1374 	struct garmin_data *garmin_data_p = (struct garmin_data *) data;
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 	init_timer(&garmin_data_p->timer);
1394 	spin_lock_init(&garmin_data_p->lock);
1395 	INIT_LIST_HEAD(&garmin_data_p->pktlist);
1396 	/* garmin_data_p->timer.expires = jiffies + session_timeout; */
1397 	garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1398 	garmin_data_p->timer.function = timeout_handler;
1399 	garmin_data_p->port = port;
1400 	garmin_data_p->state = 0;
1401 	garmin_data_p->flags = 0;
1402 	garmin_data_p->count = 0;
1403 	usb_set_serial_port_data(port, garmin_data_p);
1404 
1405 	status = garmin_init_session(port);
1406 
1407 	return status;
1408 }
1409 
1410 
1411 static int garmin_port_remove(struct usb_serial_port *port)
1412 {
1413 	struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1414 
1415 	usb_kill_urb(port->interrupt_in_urb);
1416 	del_timer_sync(&garmin_data_p->timer);
1417 	kfree(garmin_data_p);
1418 	return 0;
1419 }
1420 
1421 
1422 /* All of the device info needed */
1423 static struct usb_serial_driver garmin_device = {
1424 	.driver = {
1425 		.owner       = THIS_MODULE,
1426 		.name        = "garmin_gps",
1427 	},
1428 	.description         = "Garmin GPS usb/tty",
1429 	.id_table            = id_table,
1430 	.num_ports           = 1,
1431 	.open                = garmin_open,
1432 	.close               = garmin_close,
1433 	.throttle            = garmin_throttle,
1434 	.unthrottle          = garmin_unthrottle,
1435 	.port_probe		= garmin_port_probe,
1436 	.port_remove		= garmin_port_remove,
1437 	.write               = garmin_write,
1438 	.write_room          = garmin_write_room,
1439 	.write_bulk_callback = garmin_write_bulk_callback,
1440 	.read_bulk_callback  = garmin_read_bulk_callback,
1441 	.read_int_callback   = garmin_read_int_callback,
1442 };
1443 
1444 static struct usb_serial_driver * const serial_drivers[] = {
1445 	&garmin_device, NULL
1446 };
1447 
1448 module_usb_serial_driver(serial_drivers, id_table);
1449 
1450 MODULE_AUTHOR(DRIVER_AUTHOR);
1451 MODULE_DESCRIPTION(DRIVER_DESC);
1452 MODULE_LICENSE("GPL");
1453 
1454 module_param(initial_mode, int, S_IRUGO);
1455 MODULE_PARM_DESC(initial_mode, "Initial mode");
1456