]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/usb/serial/garmin_gps.c
Merge tag 'gadget-for-v3.5' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi...
[linux-beck.git] / drivers / usb / serial / garmin_gps.c
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
230 static inline int getLayerId(const __u8 *usbPacket)
231 {
232         return __le32_to_cpup((__le32 *)(usbPacket));
233 }
234
235 static inline int getPacketId(const __u8 *usbPacket)
236 {
237         return __le32_to_cpup((__le32 *)(usbPacket+4));
238 }
239
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  */
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
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  */
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 */
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 */
343 static void pkt_clear(struct garmin_data *garmin_data_p)
344 {
345         unsigned long flags;
346         struct garmin_packet *result = NULL;
347
348         spin_lock_irqsave(&garmin_data_p->lock, flags);
349         while (!list_empty(&garmin_data_p->pktlist)) {
350                 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
351                 list_del(&result->list);
352                 kfree(result);
353         }
354         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
355 }
356
357
358 /******************************************************************************
359  * garmin serial protocol handling handling
360  ******************************************************************************/
361
362 /* send an ack packet back to the tty */
363 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
364 {
365         __u8 pkt[10];
366         __u8 cksum = 0;
367         __u8 *ptr = pkt;
368         unsigned  l = 0;
369
370         dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
371
372         *ptr++ = DLE;
373         *ptr++ = ACK;
374         cksum += ACK;
375
376         *ptr++ = 2;
377         cksum += 2;
378
379         *ptr++ = pkt_id;
380         cksum += pkt_id;
381
382         if (pkt_id == DLE)
383                 *ptr++ = DLE;
384
385         *ptr++ = 0;
386         *ptr++ = 0xFF & (-cksum);
387         *ptr++ = DLE;
388         *ptr++ = ETX;
389
390         l = ptr-pkt;
391
392         send_to_tty(garmin_data_p->port, pkt, l);
393         return 0;
394 }
395
396
397
398 /*
399  * called for a complete packet received from tty layer
400  *
401  * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
402  * at GSP_INITIAL_OFFSET.
403  *
404  * count - number of bytes in the input buffer including space reserved for
405  *         the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
406  *         (including pkt-id, data-length a. cksum)
407  */
408 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
409 {
410         unsigned long flags;
411         const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
412         __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
413
414         int cksum = 0;
415         int n = 0;
416         int pktid = recpkt[0];
417         int size = recpkt[1];
418
419         usb_serial_debug_data(debug, &garmin_data_p->port->dev,
420                                __func__, count-GSP_INITIAL_OFFSET, recpkt);
421
422         if (size != (count-GSP_INITIAL_OFFSET-3)) {
423                 dbg("%s - invalid size, expected %d bytes, got %d",
424                         __func__, size, (count-GSP_INITIAL_OFFSET-3));
425                 return -EINVPKT;
426         }
427
428         cksum += *recpkt++;
429         cksum += *recpkt++;
430
431         /* sanity check, remove after test ... */
432         if ((__u8 *)&(usbdata[3]) != recpkt) {
433                 dbg("%s - ptr mismatch %p - %p",
434                         __func__, &(usbdata[4]), recpkt);
435                 return -EINVPKT;
436         }
437
438         while (n < size) {
439                 cksum += *recpkt++;
440                 n++;
441         }
442
443         if ((0xff & (cksum + *recpkt)) != 0) {
444                 dbg("%s - invalid checksum, expected %02x, got %02x",
445                         __func__, 0xff & -cksum, 0xff & *recpkt);
446                 return -EINVPKT;
447         }
448
449         usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
450         usbdata[1] = __cpu_to_le32(pktid);
451         usbdata[2] = __cpu_to_le32(size);
452
453         garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
454                            GARMIN_PKTHDR_LENGTH+size, 0);
455
456         /* if this was an abort-transfer command, flush all
457            queued data. */
458         if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
459                 spin_lock_irqsave(&garmin_data_p->lock, flags);
460                 garmin_data_p->flags |= FLAGS_DROP_DATA;
461                 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
462                 pkt_clear(garmin_data_p);
463         }
464
465         return count;
466 }
467
468
469
470 /*
471  * Called for data received from tty
472  *
473  * buf contains the data read, it may span more than one packet or even
474  * incomplete packets
475  *
476  * input record should be a serial-record, but it may not be complete.
477  * Copy it into our local buffer, until an etx is seen (or an error
478  * occurs).
479  * Once the record is complete, convert into a usb packet and send it
480  * to the bulk pipe, send an ack back to the tty.
481  *
482  * If the input is an ack, just send the last queued packet to the
483  * tty layer.
484  *
485  * if the input is an abort command, drop all queued data.
486  */
487
488 static int gsp_receive(struct garmin_data *garmin_data_p,
489                        const unsigned char *buf, int count)
490 {
491         unsigned long flags;
492         int offs = 0;
493         int ack_or_nak_seen = 0;
494         __u8 *dest;
495         int size;
496         /* dleSeen: set if last byte read was a DLE */
497         int dleSeen;
498         /* skip: if set, skip incoming data until possible start of
499          *       new packet
500          */
501         int skip;
502         __u8 data;
503
504         spin_lock_irqsave(&garmin_data_p->lock, flags);
505         dest = garmin_data_p->inbuffer;
506         size = garmin_data_p->insize;
507         dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
508         skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
509         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
510
511         /* dbg("%s - dle=%d skip=%d size=%d count=%d",
512                 __func__, dleSeen, skip, size, count); */
513
514         if (size == 0)
515                 size = GSP_INITIAL_OFFSET;
516
517         while (offs < count) {
518
519                 data = *(buf+offs);
520                 offs++;
521
522                 if (data == DLE) {
523                         if (skip) { /* start of a new pkt */
524                                 skip = 0;
525                                 size = GSP_INITIAL_OFFSET;
526                                 dleSeen = 1;
527                         } else if (dleSeen) {
528                                 dest[size++] = data;
529                                 dleSeen = 0;
530                         } else {
531                                 dleSeen = 1;
532                         }
533                 } else if (data == ETX) {
534                         if (dleSeen) {
535                                 /* packet complete */
536
537                                 data = dest[GSP_INITIAL_OFFSET];
538
539                                 if (data == ACK) {
540                                         ack_or_nak_seen = ACK;
541                                         dbg("ACK packet complete.");
542                                 } else if (data == NAK) {
543                                         ack_or_nak_seen = NAK;
544                                         dbg("NAK packet complete.");
545                                 } else {
546                                         dbg("packet complete - id=0x%X.",
547                                                 0xFF & data);
548                                         gsp_rec_packet(garmin_data_p, size);
549                                 }
550
551                                 skip = 1;
552                                 size = GSP_INITIAL_OFFSET;
553                                 dleSeen = 0;
554                         } else {
555                                 dest[size++] = data;
556                         }
557                 } else if (!skip) {
558
559                         if (dleSeen) {
560                                 size = GSP_INITIAL_OFFSET;
561                                 dleSeen = 0;
562                         }
563
564                         dest[size++] = data;
565                 }
566
567                 if (size >= GPS_IN_BUFSIZ) {
568                         dbg("%s - packet too large.", __func__);
569                         skip = 1;
570                         size = GSP_INITIAL_OFFSET;
571                         dleSeen = 0;
572                 }
573         }
574
575         spin_lock_irqsave(&garmin_data_p->lock, flags);
576
577         garmin_data_p->insize = size;
578
579         /* copy flags back to structure */
580         if (skip)
581                 garmin_data_p->flags |= FLAGS_GSP_SKIP;
582         else
583                 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
584
585         if (dleSeen)
586                 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
587         else
588                 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
589
590         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
591
592         if (ack_or_nak_seen) {
593                 if (gsp_next_packet(garmin_data_p) > 0)
594                         garmin_data_p->state = STATE_ACTIVE;
595                 else
596                         garmin_data_p->state = STATE_GSP_WAIT_DATA;
597         }
598         return count;
599 }
600
601
602
603 /*
604  * Sends a usb packet to the tty
605  *
606  * Assumes, that all packages and at an usb-packet boundary.
607  *
608  * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
609  */
610 static int gsp_send(struct garmin_data *garmin_data_p,
611                     const unsigned char *buf, int count)
612 {
613         const unsigned char *src;
614         unsigned char *dst;
615         int pktid = 0;
616         int datalen = 0;
617         int cksum = 0;
618         int i = 0;
619         int k;
620
621         dbg("%s - state %d - %d bytes.", __func__,
622                                         garmin_data_p->state, count);
623
624         k = garmin_data_p->outsize;
625         if ((k+count) > GPS_OUT_BUFSIZ) {
626                 dbg("packet too large");
627                 garmin_data_p->outsize = 0;
628                 return -4;
629         }
630
631         memcpy(garmin_data_p->outbuffer+k, buf, count);
632         k += count;
633         garmin_data_p->outsize = k;
634
635         if (k >= GARMIN_PKTHDR_LENGTH) {
636                 pktid  = getPacketId(garmin_data_p->outbuffer);
637                 datalen = getDataLength(garmin_data_p->outbuffer);
638                 i = GARMIN_PKTHDR_LENGTH + datalen;
639                 if (k < i)
640                         return 0;
641         } else {
642                 return 0;
643         }
644
645         dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
646
647         /* garmin_data_p->outbuffer now contains a complete packet */
648
649         usb_serial_debug_data(debug, &garmin_data_p->port->dev,
650                                 __func__, k, garmin_data_p->outbuffer);
651
652         garmin_data_p->outsize = 0;
653
654         if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
655                 dbg("not an application packet (%d)",
656                                 getLayerId(garmin_data_p->outbuffer));
657                 return -1;
658         }
659
660         if (pktid > 255) {
661                 dbg("packet-id %d too large", pktid);
662                 return -2;
663         }
664
665         if (datalen > 255) {
666                 dbg("packet-size %d too large", datalen);
667                 return -3;
668         }
669
670         /* the serial protocol should be able to handle this packet */
671
672         k = 0;
673         src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
674         for (i = 0; i < datalen; i++) {
675                 if (*src++ == DLE)
676                         k++;
677         }
678
679         src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
680         if (k > (GARMIN_PKTHDR_LENGTH-2)) {
681                 /* can't add stuffing DLEs in place, move data to end
682                    of buffer ... */
683                 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
684                 memcpy(dst, src, datalen);
685                 src = dst;
686         }
687
688         dst = garmin_data_p->outbuffer;
689
690         *dst++ = DLE;
691         *dst++ = pktid;
692         cksum += pktid;
693         *dst++ = datalen;
694         cksum += datalen;
695         if (datalen == DLE)
696                 *dst++ = DLE;
697
698         for (i = 0; i < datalen; i++) {
699                 __u8 c = *src++;
700                 *dst++ = c;
701                 cksum += c;
702                 if (c == DLE)
703                         *dst++ = DLE;
704         }
705
706         cksum = 0xFF & -cksum;
707         *dst++ = cksum;
708         if (cksum == DLE)
709                 *dst++ = DLE;
710         *dst++ = DLE;
711         *dst++ = ETX;
712
713         i = dst-garmin_data_p->outbuffer;
714
715         send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
716
717         garmin_data_p->pkt_id = pktid;
718         garmin_data_p->state  = STATE_WAIT_TTY_ACK;
719
720         return i;
721 }
722
723
724 /*
725  * Process the next pending data packet - if there is one
726  */
727 static int gsp_next_packet(struct garmin_data *garmin_data_p)
728 {
729         int result = 0;
730         struct garmin_packet *pkt = NULL;
731
732         while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
733                 dbg("%s - next pkt: %d", __func__, pkt->seq);
734                 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
735                 if (result > 0) {
736                         kfree(pkt);
737                         return result;
738                 }
739                 kfree(pkt);
740         }
741         return result;
742 }
743
744
745
746 /******************************************************************************
747  * garmin native mode
748  ******************************************************************************/
749
750
751 /*
752  * Called for data received from tty
753  *
754  * The input data is expected to be in garmin usb-packet format.
755  *
756  * buf contains the data read, it may span more than one packet
757  * or even incomplete packets
758  */
759 static int nat_receive(struct garmin_data *garmin_data_p,
760                        const unsigned char *buf, int count)
761 {
762         unsigned long flags;
763         __u8 *dest;
764         int offs = 0;
765         int result = count;
766         int len;
767
768         while (offs < count) {
769                 /* if buffer contains header, copy rest of data */
770                 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
771                         len = GARMIN_PKTHDR_LENGTH
772                               +getDataLength(garmin_data_p->inbuffer);
773                 else
774                         len = GARMIN_PKTHDR_LENGTH;
775
776                 if (len >= GPS_IN_BUFSIZ) {
777                         /* seems to be an invalid packet, ignore rest
778                            of input */
779                         dbg("%s - packet size too large: %d", __func__, len);
780                         garmin_data_p->insize = 0;
781                         count = 0;
782                         result = -EINVPKT;
783                 } else {
784                         len -= garmin_data_p->insize;
785                         if (len > (count-offs))
786                                 len = (count-offs);
787                         if (len > 0) {
788                                 dest = garmin_data_p->inbuffer
789                                                 + garmin_data_p->insize;
790                                 memcpy(dest, buf+offs, len);
791                                 garmin_data_p->insize += len;
792                                 offs += len;
793                         }
794                 }
795
796                 /* do we have a complete packet ? */
797                 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
798                         len = GARMIN_PKTHDR_LENGTH+
799                            getDataLength(garmin_data_p->inbuffer);
800                         if (garmin_data_p->insize >= len) {
801                                 garmin_write_bulk(garmin_data_p->port,
802                                                    garmin_data_p->inbuffer,
803                                                    len, 0);
804                                 garmin_data_p->insize = 0;
805
806                                 /* if this was an abort-transfer command,
807                                    flush all queued data. */
808                                 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
809                                         spin_lock_irqsave(&garmin_data_p->lock,
810                                                                         flags);
811                                         garmin_data_p->flags |= FLAGS_DROP_DATA;
812                                         spin_unlock_irqrestore(
813                                                 &garmin_data_p->lock, flags);
814                                         pkt_clear(garmin_data_p);
815                                 }
816                         }
817                 }
818         }
819         return result;
820 }
821
822
823 /******************************************************************************
824  * private packets
825  ******************************************************************************/
826
827 static void priv_status_resp(struct usb_serial_port *port)
828 {
829         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
830         __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
831
832         pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
833         pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
834         pkt[2] = __cpu_to_le32(12);
835         pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
836         pkt[4] = __cpu_to_le32(garmin_data_p->mode);
837         pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
838
839         send_to_tty(port, (__u8 *)pkt, 6 * 4);
840 }
841
842
843 /******************************************************************************
844  * Garmin specific driver functions
845  ******************************************************************************/
846
847 static int process_resetdev_request(struct usb_serial_port *port)
848 {
849         unsigned long flags;
850         int status;
851         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
852
853         spin_lock_irqsave(&garmin_data_p->lock, flags);
854         garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
855         garmin_data_p->state = STATE_RESET;
856         garmin_data_p->serial_num = 0;
857         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
858
859         usb_kill_urb(port->interrupt_in_urb);
860         dbg("%s - usb_reset_device", __func__);
861         status = usb_reset_device(port->serial->dev);
862         if (status)
863                 dbg("%s - usb_reset_device failed: %d",
864                         __func__, status);
865         return status;
866 }
867
868
869
870 /*
871  * clear all cached data
872  */
873 static int garmin_clear(struct garmin_data *garmin_data_p)
874 {
875         unsigned long flags;
876         int status = 0;
877
878         /* flush all queued data */
879         pkt_clear(garmin_data_p);
880
881         spin_lock_irqsave(&garmin_data_p->lock, flags);
882         garmin_data_p->insize = 0;
883         garmin_data_p->outsize = 0;
884         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
885
886         return status;
887 }
888
889
890 static int garmin_init_session(struct usb_serial_port *port)
891 {
892         struct usb_serial *serial = port->serial;
893         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
894         int status = 0;
895         int i = 0;
896
897         if (status == 0) {
898                 usb_kill_urb(port->interrupt_in_urb);
899
900                 dbg("%s - adding interrupt input", __func__);
901                 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
902                 if (status)
903                         dev_err(&serial->dev->dev,
904                           "%s - failed submitting interrupt urb, error %d\n",
905                                                         __func__, status);
906         }
907
908         /*
909          * using the initialization method from gpsbabel. See comments in
910          * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
911          */
912         if (status == 0) {
913                 dbg("%s - starting session ...", __func__);
914                 garmin_data_p->state = STATE_ACTIVE;
915
916                 for (i = 0; i < 3; i++) {
917                         status = garmin_write_bulk(port,
918                                         GARMIN_START_SESSION_REQ,
919                                         sizeof(GARMIN_START_SESSION_REQ), 0);
920
921                         if (status < 0)
922                                 break;
923                 }
924
925                 if (status > 0)
926                         status = 0;
927         }
928
929         return status;
930 }
931
932
933
934 static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
935 {
936         unsigned long flags;
937         int status = 0;
938         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
939
940         spin_lock_irqsave(&garmin_data_p->lock, flags);
941         garmin_data_p->mode  = initial_mode;
942         garmin_data_p->count = 0;
943         garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
944         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
945
946         /* shutdown any bulk reads that might be going on */
947         usb_kill_urb(port->write_urb);
948         usb_kill_urb(port->read_urb);
949
950         if (garmin_data_p->state == STATE_RESET)
951                 status = garmin_init_session(port);
952
953         garmin_data_p->state = STATE_ACTIVE;
954         return status;
955 }
956
957
958 static void garmin_close(struct usb_serial_port *port)
959 {
960         struct usb_serial *serial = port->serial;
961         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
962
963         dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
964                 port->number, garmin_data_p->mode,
965                 garmin_data_p->state, garmin_data_p->flags);
966
967         if (!serial)
968                 return;
969
970         mutex_lock(&port->serial->disc_mutex);
971
972         if (!port->serial->disconnected)
973                 garmin_clear(garmin_data_p);
974
975         /* shutdown our urbs */
976         usb_kill_urb(port->read_urb);
977         usb_kill_urb(port->write_urb);
978
979         /* keep reset state so we know that we must start a new session */
980         if (garmin_data_p->state != STATE_RESET)
981                 garmin_data_p->state = STATE_DISCONNECTED;
982
983         mutex_unlock(&port->serial->disc_mutex);
984 }
985
986
987 static void garmin_write_bulk_callback(struct urb *urb)
988 {
989         struct usb_serial_port *port = urb->context;
990
991         if (port) {
992                 struct garmin_data *garmin_data_p =
993                                         usb_get_serial_port_data(port);
994
995                 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
996
997                         if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
998                                 gsp_send_ack(garmin_data_p,
999                                         ((__u8 *)urb->transfer_buffer)[4]);
1000                         }
1001                 }
1002                 usb_serial_port_softint(port);
1003         }
1004
1005         /* Ignore errors that resulted from garmin_write_bulk with
1006            dismiss_ack = 1 */
1007
1008         /* free up the transfer buffer, as usb_free_urb() does not do this */
1009         kfree(urb->transfer_buffer);
1010 }
1011
1012
1013 static int garmin_write_bulk(struct usb_serial_port *port,
1014                               const unsigned char *buf, int count,
1015                               int dismiss_ack)
1016 {
1017         unsigned long flags;
1018         struct usb_serial *serial = port->serial;
1019         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1020         struct urb *urb;
1021         unsigned char *buffer;
1022         int status;
1023
1024         spin_lock_irqsave(&garmin_data_p->lock, flags);
1025         garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1026         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1027
1028         buffer = kmalloc(count, GFP_ATOMIC);
1029         if (!buffer) {
1030                 dev_err(&port->dev, "out of memory\n");
1031                 return -ENOMEM;
1032         }
1033
1034         urb = usb_alloc_urb(0, GFP_ATOMIC);
1035         if (!urb) {
1036                 dev_err(&port->dev, "no more free urbs\n");
1037                 kfree(buffer);
1038                 return -ENOMEM;
1039         }
1040
1041         memcpy(buffer, buf, count);
1042
1043         usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
1044
1045         usb_fill_bulk_urb(urb, serial->dev,
1046                                 usb_sndbulkpipe(serial->dev,
1047                                         port->bulk_out_endpointAddress),
1048                                 buffer, count,
1049                                 garmin_write_bulk_callback,
1050                                 dismiss_ack ? NULL : port);
1051         urb->transfer_flags |= URB_ZERO_PACKET;
1052
1053         if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1054
1055                 spin_lock_irqsave(&garmin_data_p->lock, flags);
1056                 garmin_data_p->flags |= APP_REQ_SEEN;
1057                 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1058
1059                 if (garmin_data_p->mode == MODE_GARMIN_SERIAL)  {
1060                         pkt_clear(garmin_data_p);
1061                         garmin_data_p->state = STATE_GSP_WAIT_DATA;
1062                 }
1063         }
1064
1065         /* send it down the pipe */
1066         status = usb_submit_urb(urb, GFP_ATOMIC);
1067         if (status) {
1068                 dev_err(&port->dev,
1069                    "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1070                                 __func__, status);
1071                 count = status;
1072         }
1073
1074         /* we are done with this urb, so let the host driver
1075          * really free it when it is finished with it */
1076         usb_free_urb(urb);
1077
1078         return count;
1079 }
1080
1081 static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1082                                          const unsigned char *buf, int count)
1083 {
1084         int pktid, pktsiz, len;
1085         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1086         __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1087
1088         usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
1089
1090         if (garmin_data_p->state == STATE_RESET)
1091                 return -EIO;
1092
1093         /* check for our private packets */
1094         if (count >= GARMIN_PKTHDR_LENGTH) {
1095                 len = PRIVPKTSIZ;
1096                 if (count < len)
1097                         len = count;
1098
1099                 memcpy(garmin_data_p->privpkt, buf, len);
1100
1101                 pktsiz = getDataLength(garmin_data_p->privpkt);
1102                 pktid  = getPacketId(garmin_data_p->privpkt);
1103
1104                 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1105                     && GARMIN_LAYERID_PRIVATE ==
1106                                 getLayerId(garmin_data_p->privpkt)) {
1107
1108                         dbg("%s - processing private request %d",
1109                                 __func__, pktid);
1110
1111                         /* drop all unfinished transfers */
1112                         garmin_clear(garmin_data_p);
1113
1114                         switch (pktid) {
1115
1116                         case PRIV_PKTID_SET_DEBUG:
1117                                 if (pktsiz != 4)
1118                                         return -EINVPKT;
1119                                 debug = __le32_to_cpu(privpkt[3]);
1120                                 dbg("%s - debug level set to 0x%X",
1121                                         __func__, debug);
1122                                 break;
1123
1124                         case PRIV_PKTID_SET_MODE:
1125                                 if (pktsiz != 4)
1126                                         return -EINVPKT;
1127                                 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1128                                 dbg("%s - mode set to %d",
1129                                         __func__, garmin_data_p->mode);
1130                                 break;
1131
1132                         case PRIV_PKTID_INFO_REQ:
1133                                 priv_status_resp(port);
1134                                 break;
1135
1136                         case PRIV_PKTID_RESET_REQ:
1137                                 process_resetdev_request(port);
1138                                 break;
1139
1140                         case PRIV_PKTID_SET_DEF_MODE:
1141                                 if (pktsiz != 4)
1142                                         return -EINVPKT;
1143                                 initial_mode = __le32_to_cpu(privpkt[3]);
1144                                 dbg("%s - initial_mode set to %d",
1145                                         __func__,
1146                                         garmin_data_p->mode);
1147                                 break;
1148                         }
1149                         return count;
1150                 }
1151         }
1152
1153         if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1154                 return gsp_receive(garmin_data_p, buf, count);
1155         } else {        /* MODE_NATIVE */
1156                 return nat_receive(garmin_data_p, buf, count);
1157         }
1158 }
1159
1160
1161 static int garmin_write_room(struct tty_struct *tty)
1162 {
1163         struct usb_serial_port *port = tty->driver_data;
1164         /*
1165          * Report back the bytes currently available in the output buffer.
1166          */
1167         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1168         return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1169 }
1170
1171
1172 static void garmin_read_process(struct garmin_data *garmin_data_p,
1173                                  unsigned char *data, unsigned data_length,
1174                                  int bulk_data)
1175 {
1176         unsigned long flags;
1177
1178         if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1179                 /* abort-transfer cmd is actice */
1180                 dbg("%s - pkt dropped", __func__);
1181         } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1182                 garmin_data_p->state != STATE_RESET) {
1183
1184                 /* if throttling is active or postprecessing is required
1185                    put the received data in the input queue, otherwise
1186                    send it directly to the tty port */
1187                 if (garmin_data_p->flags & FLAGS_QUEUING) {
1188                         pkt_add(garmin_data_p, data, data_length);
1189                 } else if (bulk_data || 
1190                            getLayerId(data) == GARMIN_LAYERID_APPL) {
1191
1192                         spin_lock_irqsave(&garmin_data_p->lock, flags);
1193                         garmin_data_p->flags |= APP_RESP_SEEN;
1194                         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1195
1196                         if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1197                                 pkt_add(garmin_data_p, data, data_length);
1198                         } else {
1199                                 send_to_tty(garmin_data_p->port, data,
1200                                                 data_length);
1201                         }
1202                 }
1203                 /* ignore system layer packets ... */
1204         }
1205 }
1206
1207
1208 static void garmin_read_bulk_callback(struct urb *urb)
1209 {
1210         unsigned long flags;
1211         struct usb_serial_port *port = urb->context;
1212         struct usb_serial *serial =  port->serial;
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         int retval;
1217
1218         if (!serial) {
1219                 dbg("%s - bad serial pointer, exiting", __func__);
1220                 return;
1221         }
1222
1223         if (status) {
1224                 dbg("%s - nonzero read bulk status received: %d",
1225                         __func__, status);
1226                 return;
1227         }
1228
1229         usb_serial_debug_data(debug, &port->dev,
1230                                 __func__, urb->actual_length, data);
1231
1232         garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
1233
1234         if (urb->actual_length == 0 &&
1235                         0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1236                 spin_lock_irqsave(&garmin_data_p->lock, flags);
1237                 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1238                 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1239                 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1240                 if (retval)
1241                         dev_err(&port->dev,
1242                                 "%s - failed resubmitting read urb, error %d\n",
1243                                 __func__, retval);
1244         } else if (urb->actual_length > 0) {
1245                 /* Continue trying to read until nothing more is received  */
1246                 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1247                         retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1248                         if (retval)
1249                                 dev_err(&port->dev,
1250                                         "%s - failed resubmitting read urb, "
1251                                         "error %d\n", __func__, retval);
1252                 }
1253         } else {
1254                 dbg("%s - end of bulk data", __func__);
1255                 spin_lock_irqsave(&garmin_data_p->lock, flags);
1256                 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1257                 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1258         }
1259 }
1260
1261
1262 static void garmin_read_int_callback(struct urb *urb)
1263 {
1264         unsigned long flags;
1265         int retval;
1266         struct usb_serial_port *port = urb->context;
1267         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1268         unsigned char *data = urb->transfer_buffer;
1269         int status = urb->status;
1270
1271         switch (status) {
1272         case 0:
1273                 /* success */
1274                 break;
1275         case -ECONNRESET:
1276         case -ENOENT:
1277         case -ESHUTDOWN:
1278                 /* this urb is terminated, clean up */
1279                 dbg("%s - urb shutting down with status: %d",
1280                         __func__, status);
1281                 return;
1282         default:
1283                 dbg("%s - nonzero urb status received: %d",
1284                         __func__, status);
1285                 return;
1286         }
1287
1288         usb_serial_debug_data(debug, &port->dev, __func__,
1289                                 urb->actual_length, urb->transfer_buffer);
1290
1291         if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1292             0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1293                                 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1294
1295                 dbg("%s - bulk data available.", __func__);
1296
1297                 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1298
1299                         /* bulk data available */
1300                         retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1301                         if (retval) {
1302                                 dev_err(&port->dev,
1303                                  "%s - failed submitting read urb, error %d\n",
1304                                                         __func__, retval);
1305                         } else {
1306                                 spin_lock_irqsave(&garmin_data_p->lock, flags);
1307                                 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1308                                 spin_unlock_irqrestore(&garmin_data_p->lock,
1309                                                                         flags);
1310                         }
1311                 } else {
1312                         /* bulk-in transfer still active */
1313                         spin_lock_irqsave(&garmin_data_p->lock, flags);
1314                         garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1315                         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1316                 }
1317
1318         } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1319                          && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1320                                         sizeof(GARMIN_START_SESSION_REPLY))) {
1321
1322                 spin_lock_irqsave(&garmin_data_p->lock, flags);
1323                 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1324                 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1325
1326                 /* save the serial number */
1327                 garmin_data_p->serial_num = __le32_to_cpup(
1328                                         (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
1329
1330                 dbg("%s - start-of-session reply seen - serial %u.",
1331                         __func__, garmin_data_p->serial_num);
1332         }
1333
1334         garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
1335
1336         retval = usb_submit_urb(urb, GFP_ATOMIC);
1337         if (retval)
1338                 dev_err(&urb->dev->dev,
1339                         "%s - Error %d submitting interrupt urb\n",
1340                         __func__, retval);
1341 }
1342
1343
1344 /*
1345  * Sends the next queued packt to the tty port (garmin native mode only)
1346  * and then sets a timer to call itself again until all queued data
1347  * is sent.
1348  */
1349 static int garmin_flush_queue(struct garmin_data *garmin_data_p)
1350 {
1351         unsigned long flags;
1352         struct garmin_packet *pkt;
1353
1354         if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1355                 pkt = pkt_pop(garmin_data_p);
1356                 if (pkt != NULL) {
1357                         send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1358                         kfree(pkt);
1359                         mod_timer(&garmin_data_p->timer, (1)+jiffies);
1360
1361                 } else {
1362                         spin_lock_irqsave(&garmin_data_p->lock, flags);
1363                         garmin_data_p->flags &= ~FLAGS_QUEUING;
1364                         spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1365                 }
1366         }
1367         return 0;
1368 }
1369
1370
1371 static void garmin_throttle(struct tty_struct *tty)
1372 {
1373         struct usb_serial_port *port = tty->driver_data;
1374         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1375
1376         /* set flag, data received will be put into a queue
1377            for later processing */
1378         spin_lock_irq(&garmin_data_p->lock);
1379         garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1380         spin_unlock_irq(&garmin_data_p->lock);
1381 }
1382
1383
1384 static void garmin_unthrottle(struct tty_struct *tty)
1385 {
1386         struct usb_serial_port *port = tty->driver_data;
1387         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1388         int status;
1389
1390         spin_lock_irq(&garmin_data_p->lock);
1391         garmin_data_p->flags &= ~FLAGS_THROTTLED;
1392         spin_unlock_irq(&garmin_data_p->lock);
1393
1394         /* in native mode send queued data to tty, in
1395            serial mode nothing needs to be done here */
1396         if (garmin_data_p->mode == MODE_NATIVE)
1397                 garmin_flush_queue(garmin_data_p);
1398
1399         if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1400                 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1401                 if (status)
1402                         dev_err(&port->dev,
1403                                 "%s - failed resubmitting read urb, error %d\n",
1404                                 __func__, status);
1405         }
1406 }
1407
1408 /*
1409  * The timer is currently only used to send queued packets to
1410  * the tty in cases where the protocol provides no own handshaking
1411  * to initiate the transfer.
1412  */
1413 static void timeout_handler(unsigned long data)
1414 {
1415         struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1416
1417         /* send the next queued packet to the tty port */
1418         if (garmin_data_p->mode == MODE_NATIVE)
1419                 if (garmin_data_p->flags & FLAGS_QUEUING)
1420                         garmin_flush_queue(garmin_data_p);
1421 }
1422
1423
1424
1425 static int garmin_attach(struct usb_serial *serial)
1426 {
1427         int status = 0;
1428         struct usb_serial_port *port = serial->port[0];
1429         struct garmin_data *garmin_data_p = NULL;
1430
1431         garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1432         if (garmin_data_p == NULL) {
1433                 dev_err(&port->dev, "%s - Out of memory\n", __func__);
1434                 return -ENOMEM;
1435         }
1436         init_timer(&garmin_data_p->timer);
1437         spin_lock_init(&garmin_data_p->lock);
1438         INIT_LIST_HEAD(&garmin_data_p->pktlist);
1439         /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1440         garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1441         garmin_data_p->timer.function = timeout_handler;
1442         garmin_data_p->port = port;
1443         garmin_data_p->state = 0;
1444         garmin_data_p->flags = 0;
1445         garmin_data_p->count = 0;
1446         usb_set_serial_port_data(port, garmin_data_p);
1447
1448         status = garmin_init_session(port);
1449
1450         return status;
1451 }
1452
1453
1454 static void garmin_disconnect(struct usb_serial *serial)
1455 {
1456         struct usb_serial_port *port = serial->port[0];
1457         struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1458
1459         usb_kill_urb(port->interrupt_in_urb);
1460         del_timer_sync(&garmin_data_p->timer);
1461 }
1462
1463
1464 static void garmin_release(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         kfree(garmin_data_p);
1470 }
1471
1472
1473 /* All of the device info needed */
1474 static struct usb_serial_driver garmin_device = {
1475         .driver = {
1476                 .owner       = THIS_MODULE,
1477                 .name        = "garmin_gps",
1478         },
1479         .description         = "Garmin GPS usb/tty",
1480         .id_table            = id_table,
1481         .num_ports           = 1,
1482         .open                = garmin_open,
1483         .close               = garmin_close,
1484         .throttle            = garmin_throttle,
1485         .unthrottle          = garmin_unthrottle,
1486         .attach              = garmin_attach,
1487         .disconnect          = garmin_disconnect,
1488         .release             = garmin_release,
1489         .write               = garmin_write,
1490         .write_room          = garmin_write_room,
1491         .write_bulk_callback = garmin_write_bulk_callback,
1492         .read_bulk_callback  = garmin_read_bulk_callback,
1493         .read_int_callback   = garmin_read_int_callback,
1494 };
1495
1496 static struct usb_serial_driver * const serial_drivers[] = {
1497         &garmin_device, NULL
1498 };
1499
1500 module_usb_serial_driver(garmin_driver, serial_drivers);
1501
1502 MODULE_AUTHOR(DRIVER_AUTHOR);
1503 MODULE_DESCRIPTION(DRIVER_DESC);
1504 MODULE_LICENSE("GPL");
1505
1506 module_param(debug, bool, S_IWUSR | S_IRUGO);
1507 MODULE_PARM_DESC(debug, "Debug enabled or not");
1508 module_param(initial_mode, int, S_IRUGO);
1509 MODULE_PARM_DESC(initial_mode, "Initial mode");