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