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