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