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