V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / usb / serial / garmin_gps.c
blobf1c90cfe72515365236c9ddf61e1c41ee90fe82a
1 /*
2 * Garmin GPS driver
4 * Copyright (C) 2006,2007 Hermann Kneissel herkne@users.sourceforge.net
6 * The latest version of the driver can be found at
7 * http://sourceforge.net/projects/garmin-gps/
9 * This driver has been derived from v2.1 of the visor driver.
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.
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.
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
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 <asm/uaccess.h>
37 #include <asm/atomic.h>
38 #include <linux/usb.h>
39 #include <linux/usb/serial.h>
41 #include <linux/version.h>
43 /* the mode to be set when the port ist opened */
44 static int initial_mode = 1;
46 /* debug flag */
47 static int debug = 0;
49 #define GARMIN_VENDOR_ID 0x091E
52 * Version Information
55 #define VERSION_MAJOR 0
56 #define VERSION_MINOR 31
58 #define _STR(s) #s
59 #define _DRIVER_VERSION(a,b) "v" _STR(a) "." _STR(b)
60 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
61 #define DRIVER_AUTHOR "hermann kneissel"
62 #define DRIVER_DESC "garmin gps driver"
64 /* error codes returned by the driver */
65 #define EINVPKT 1000 /* invalid packet structure */
68 // size of the header of a packet using the usb protocol
69 #define GARMIN_PKTHDR_LENGTH 12
71 // max. possible size of a packet using the serial protocol
72 #define MAX_SERIAL_PKT_SIZ (3+255+3)
74 // max. possible size of a packet with worst case stuffing
75 #define MAX_SERIAL_PKT_SIZ_STUFFED MAX_SERIAL_PKT_SIZ+256
77 // size of a buffer able to hold a complete (no stuffing) packet
78 // (the document protocol does not contain packets with a larger
79 // size, but in theory a packet may be 64k+12 bytes - if in
80 // later protocol versions larger packet sizes occur, this value
81 // should be increased accordingly, so the input buffer is always
82 // large enough the store a complete packet inclusive header)
83 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
85 // size of a buffer able to hold a complete (incl. stuffing) packet
86 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
88 // where to place the packet id of a serial packet, so we can
89 // prepend the usb-packet header without the need to move the
90 // packets data
91 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
93 // max. size of incoming private packets (header+1 param)
94 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
96 #define GARMIN_LAYERID_TRANSPORT 0
97 #define GARMIN_LAYERID_APPL 20
98 // our own layer-id to use for some control mechanisms
99 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
101 #define GARMIN_PKTID_PVT_DATA 51
102 #define GARMIN_PKTID_L001_COMMAND_DATA 10
104 #define CMND_ABORT_TRANSFER 0
106 // packet ids used in private layer
107 #define PRIV_PKTID_SET_DEBUG 1
108 #define PRIV_PKTID_SET_MODE 2
109 #define PRIV_PKTID_INFO_REQ 3
110 #define PRIV_PKTID_INFO_RESP 4
111 #define PRIV_PKTID_RESET_REQ 5
112 #define PRIV_PKTID_SET_DEF_MODE 6
115 #define ETX 0x03
116 #define DLE 0x10
117 #define ACK 0x06
118 #define NAK 0x15
120 /* structure used to queue incoming packets */
121 struct garmin_packet {
122 struct list_head list;
123 int seq;
124 int size; // the real size of the data array, always > 0
125 __u8 data[1];
128 /* structure used to keep the current state of the driver */
129 struct garmin_data {
130 __u8 state;
131 __u16 flags;
132 __u8 mode;
133 __u8 ignorePkts;
134 __u8 count;
135 __u8 pkt_id;
136 __u32 serial_num;
137 struct timer_list timer;
138 struct usb_serial_port *port;
139 int seq_counter;
140 int insize;
141 int outsize;
142 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
143 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
144 __u8 privpkt[4*6];
145 atomic_t req_count;
146 atomic_t resp_count;
147 spinlock_t lock;
148 struct list_head pktlist;
152 #define STATE_NEW 0
153 #define STATE_INITIAL_DELAY 1
154 #define STATE_TIMEOUT 2
155 #define STATE_SESSION_REQ1 3
156 #define STATE_SESSION_REQ2 4
157 #define STATE_ACTIVE 5
159 #define STATE_RESET 8
160 #define STATE_DISCONNECTED 9
161 #define STATE_WAIT_TTY_ACK 10
162 #define STATE_GSP_WAIT_DATA 11
164 #define MODE_NATIVE 0
165 #define MODE_GARMIN_SERIAL 1
167 // Flags used in garmin_data.flags:
168 #define FLAGS_SESSION_REPLY_MASK 0x00C0
169 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
170 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
171 #define FLAGS_BULK_IN_ACTIVE 0x0020
172 #define FLAGS_BULK_IN_RESTART 0x0010
173 #define FLAGS_THROTTLED 0x0008
174 #define CLEAR_HALT_REQUIRED 0x0001
176 #define FLAGS_QUEUING 0x0100
177 #define FLAGS_DROP_DATA 0x0800
179 #define FLAGS_GSP_SKIP 0x1000
180 #define FLAGS_GSP_DLESEEN 0x2000
187 /* function prototypes */
188 static void gsp_next_packet(struct garmin_data * garmin_data_p);
189 static int garmin_write_bulk(struct usb_serial_port *port,
190 const unsigned char *buf, int count,
191 int dismiss_ack);
193 /* some special packets to be send or received */
194 static unsigned char const GARMIN_START_SESSION_REQ[]
195 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
196 static unsigned char const GARMIN_START_SESSION_REQ2[]
197 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
198 static unsigned char const GARMIN_START_SESSION_REPLY[]
199 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
200 static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
201 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
202 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
203 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
204 static unsigned char const GARMIN_APP_LAYER_REPLY[]
205 = { 0x14, 0, 0, 0 };
206 static unsigned char const GARMIN_START_PVT_REQ[]
207 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
208 static unsigned char const GARMIN_STOP_PVT_REQ[]
209 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
210 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
211 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
212 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
213 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
214 static unsigned char const PRIVATE_REQ[]
215 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
219 static struct usb_device_id id_table [] = {
220 /* the same device id seems to be used by all usb enabled gps devices */
221 { USB_DEVICE(GARMIN_VENDOR_ID, 3 ) },
222 { } /* Terminating entry */
225 MODULE_DEVICE_TABLE (usb, id_table);
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,
236 static inline int noResponseFromAppLayer(struct garmin_data * garmin_data_p)
238 return atomic_read(&garmin_data_p->req_count) == atomic_read(&garmin_data_p->resp_count);
242 static inline int getLayerId(const __u8 *usbPacket)
244 return __le32_to_cpup((__le32 *)(usbPacket));
247 static inline int getPacketId(const __u8 *usbPacket)
249 return __le32_to_cpup((__le32 *)(usbPacket+4));
252 static inline int getDataLength(const __u8 *usbPacket)
254 return __le32_to_cpup((__le32 *)(usbPacket+8));
259 * check if the usb-packet in buf contains an abort-transfer command.
260 * (if yes, all queued data will be dropped)
262 static inline int isAbortTrfCmnd(const unsigned char *buf)
264 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
265 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
266 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
267 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
268 return 1;
269 else
270 return 0;
275 static void send_to_tty(struct usb_serial_port *port,
276 char *data, unsigned int actual_length)
278 struct tty_struct *tty = port->tty;
280 if (tty && actual_length) {
282 usb_serial_debug_data(debug, &port->dev,
283 __FUNCTION__, actual_length, data);
285 tty_buffer_request_room(tty, actual_length);
286 tty_insert_flip_string(tty, data, actual_length);
287 tty_flip_buffer_push(tty);
292 /******************************************************************************
293 * packet queue handling
294 ******************************************************************************/
297 * queue a received (usb-)packet for later processing
299 static int pkt_add(struct garmin_data * garmin_data_p,
300 unsigned char *data, unsigned int data_length)
302 int state = 0;
303 int result = 0;
304 unsigned long flags;
305 struct garmin_packet *pkt;
307 /* process only packets containg data ... */
308 if (data_length) {
309 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
310 GFP_ATOMIC);
311 if (pkt == NULL) {
312 dev_err(&garmin_data_p->port->dev, "out of memory\n");
313 return 0;
315 pkt->size = data_length;
316 memcpy(pkt->data, data, data_length);
318 spin_lock_irqsave(&garmin_data_p->lock, flags);
319 garmin_data_p->flags |= FLAGS_QUEUING;
320 result = list_empty(&garmin_data_p->pktlist);
321 pkt->seq = garmin_data_p->seq_counter++;
322 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
323 state = garmin_data_p->state;
324 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
326 /* in serial mode, if someone is waiting for data from
327 the device, iconvert and send the next packet to tty. */
328 if (result && (state == STATE_GSP_WAIT_DATA)) {
329 gsp_next_packet(garmin_data_p);
332 return result;
336 /* get the next pending packet */
337 static struct garmin_packet *pkt_pop(struct garmin_data * garmin_data_p)
339 unsigned long flags;
340 struct garmin_packet *result = NULL;
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);
347 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
348 return result;
352 /* free up all queued data */
353 static void pkt_clear(struct garmin_data * garmin_data_p)
355 unsigned long flags;
356 struct garmin_packet *result = NULL;
358 dbg("%s", __FUNCTION__);
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);
366 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
370 /******************************************************************************
371 * garmin serial protocol handling handling
372 ******************************************************************************/
374 /* send an ack packet back to the tty */
375 static int gsp_send_ack(struct garmin_data * garmin_data_p, __u8 pkt_id)
377 __u8 pkt[10];
378 __u8 cksum = 0;
379 __u8 *ptr = pkt;
380 unsigned l = 0;
382 dbg("%s - pkt-id: 0x%X.", __FUNCTION__, 0xFF & pkt_id);
384 *ptr++ = DLE;
385 *ptr++ = ACK;
386 cksum += ACK;
388 *ptr++ = 2;
389 cksum += 2;
391 *ptr++ = pkt_id;
392 cksum += pkt_id;
394 if (pkt_id == DLE) {
395 *ptr++ = DLE;
398 *ptr++ = 0;
399 *ptr++ = 0xFF & (-cksum);
400 *ptr++ = DLE;
401 *ptr++ = ETX;
403 l = ptr-pkt;
405 send_to_tty(garmin_data_p->port, pkt, l);
406 return 0;
412 * called for a complete packet received from tty layer
414 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
415 * at GSP_INITIAL_OFFSET.
417 * count - number of bytes in the input buffer including space reserved for
418 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
419 * (including pkt-id, data-length a. cksum)
421 static int gsp_rec_packet(struct garmin_data * garmin_data_p, int count)
423 const __u8* recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
424 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
426 int cksum = 0;
427 int n = 0;
428 int pktid = recpkt[0];
429 int size = recpkt[1];
431 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
432 __FUNCTION__, count-GSP_INITIAL_OFFSET, recpkt);
434 if (size != (count-GSP_INITIAL_OFFSET-3)) {
435 dbg("%s - invalid size, expected %d bytes, got %d",
436 __FUNCTION__, size, (count-GSP_INITIAL_OFFSET-3));
437 return -EINVPKT;
440 cksum += *recpkt++;
441 cksum += *recpkt++;
443 // sanity check, remove after test ...
444 if ((__u8*)&(usbdata[3]) != recpkt) {
445 dbg("%s - ptr mismatch %p - %p",
446 __FUNCTION__, &(usbdata[4]), recpkt);
447 return -EINVPKT;
450 while (n < size) {
451 cksum += *recpkt++;
452 n++;
455 if ((0xff & (cksum + *recpkt)) != 0) {
456 dbg("%s - invalid checksum, expected %02x, got %02x",
457 __FUNCTION__, 0xff & -cksum, 0xff & *recpkt);
458 return -EINVPKT;
461 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
462 usbdata[1] = __cpu_to_le32(pktid);
463 usbdata[2] = __cpu_to_le32(size);
465 garmin_write_bulk (garmin_data_p->port, garmin_data_p->inbuffer,
466 GARMIN_PKTHDR_LENGTH+size, 0);
468 /* if this was an abort-transfer command, flush all
469 queued data. */
470 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
471 garmin_data_p->flags |= FLAGS_DROP_DATA;
472 pkt_clear(garmin_data_p);
475 return count;
481 * Called for data received from tty
483 * buf contains the data read, it may span more than one packet or even
484 * incomplete packets
486 * input record should be a serial-record, but it may not be complete.
487 * Copy it into our local buffer, until an etx is seen (or an error
488 * occurs).
489 * Once the record is complete, convert into a usb packet and send it
490 * to the bulk pipe, send an ack back to the tty.
492 * If the input is an ack, just send the last queued packet to the
493 * tty layer.
495 * if the input is an abort command, drop all queued data.
498 static int gsp_receive(struct garmin_data * garmin_data_p,
499 const unsigned char *buf, int count)
501 unsigned long flags;
502 int offs = 0;
503 int ack_or_nak_seen = 0;
504 int i = 0;
505 __u8 *dest;
506 int size;
507 // dleSeen: set if last byte read was a DLE
508 int dleSeen;
509 // skip: if set, skip incoming data until possible start of
510 // new packet
511 int skip;
512 __u8 data;
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);
521 dbg("%s - dle=%d skip=%d size=%d count=%d",
522 __FUNCTION__, dleSeen, skip, size, count);
524 if (size == 0) {
525 size = GSP_INITIAL_OFFSET;
528 while (offs < count) {
530 data = *(buf+offs);
531 offs ++;
533 if (data == DLE) {
534 if (skip) { /* start of a new pkt */
535 skip = 0;
536 size = GSP_INITIAL_OFFSET;
537 dleSeen = 1;
538 } else if (dleSeen) {
539 dest[size++] = data;
540 dleSeen = 0;
541 } else {
542 dleSeen = 1;
544 } else if (data == ETX) {
545 if (dleSeen) {
546 /* packet complete */
548 data = dest[GSP_INITIAL_OFFSET];
550 if (data == ACK) {
551 ack_or_nak_seen = ACK;
552 dbg("ACK packet complete.");
553 } else if (data == NAK) {
554 ack_or_nak_seen = NAK;
555 dbg("NAK packet complete.");
556 } else {
557 dbg("packet complete "
558 "- id=0x%X.",
559 0xFF & data);
560 gsp_rec_packet(garmin_data_p, size);
563 skip = 1;
564 size = GSP_INITIAL_OFFSET;
565 dleSeen = 0;
566 } else {
567 dest[size++] = data;
569 } else if (!skip) {
571 if (dleSeen) {
572 dbg("non-masked DLE at %d - restarting", i);
573 size = GSP_INITIAL_OFFSET;
574 dleSeen = 0;
577 dest[size++] = data;
580 if (size >= GPS_IN_BUFSIZ) {
581 dbg("%s - packet too large.", __FUNCTION__);
582 skip = 1;
583 size = GSP_INITIAL_OFFSET;
584 dleSeen = 0;
588 spin_lock_irqsave(&garmin_data_p->lock, flags);
590 garmin_data_p->insize = size;
592 // copy flags back to structure
593 if (skip)
594 garmin_data_p->flags |= FLAGS_GSP_SKIP;
595 else
596 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
598 if (dleSeen)
599 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
600 else
601 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
603 if (ack_or_nak_seen) {
604 garmin_data_p->state = STATE_GSP_WAIT_DATA;
607 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
609 if (ack_or_nak_seen) {
610 gsp_next_packet(garmin_data_p);
613 return count;
620 * Sends a usb packet to the tty
622 * Assumes, that all packages and at an usb-packet boundary.
624 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
626 static int gsp_send(struct garmin_data * garmin_data_p,
627 const unsigned char *buf, int count)
629 const unsigned char *src;
630 unsigned char *dst;
631 int pktid = 0;
632 int datalen = 0;
633 int cksum = 0;
634 int i=0;
635 int k;
637 dbg("%s - state %d - %d bytes.", __FUNCTION__,
638 garmin_data_p->state, count);
640 k = garmin_data_p->outsize;
641 if ((k+count) > GPS_OUT_BUFSIZ) {
642 dbg("packet too large");
643 garmin_data_p->outsize = 0;
644 return -4;
647 memcpy(garmin_data_p->outbuffer+k, buf, count);
648 k += count;
649 garmin_data_p->outsize = k;
651 if (k >= GARMIN_PKTHDR_LENGTH) {
652 pktid = getPacketId(garmin_data_p->outbuffer);
653 datalen= getDataLength(garmin_data_p->outbuffer);
654 i = GARMIN_PKTHDR_LENGTH + datalen;
655 if (k < i)
656 return 0;
657 } else {
658 return 0;
661 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __FUNCTION__,
662 k, i);
664 /* garmin_data_p->outbuffer now contains a complete packet */
666 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
667 __FUNCTION__, k, garmin_data_p->outbuffer);
669 garmin_data_p->outsize = 0;
671 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
672 dbg("not an application packet (%d)",
673 getLayerId(garmin_data_p->outbuffer));
674 return -1;
677 if (pktid > 255) {
678 dbg("packet-id %d too large", pktid);
679 return -2;
682 if (datalen > 255) {
683 dbg("packet-size %d too large", datalen);
684 return -3;
687 /* the serial protocol should be able to handle this packet */
689 k = 0;
690 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
691 for (i=0; i<datalen; i++) {
692 if (*src++ == DLE)
693 k++;
696 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
697 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
698 /* can't add stuffing DLEs in place, move data to end
699 of buffer ... */
700 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
701 memcpy(dst, src, datalen);
702 src = dst;
705 dst = garmin_data_p->outbuffer;
707 *dst++ = DLE;
708 *dst++ = pktid;
709 cksum += pktid;
710 *dst++ = datalen;
711 cksum += datalen;
712 if (datalen == DLE)
713 *dst++ = DLE;
715 for (i=0; i<datalen; i++) {
716 __u8 c = *src++;
717 *dst++ = c;
718 cksum += c;
719 if (c == DLE)
720 *dst++ = DLE;
723 cksum = 0xFF & -cksum;
724 *dst++ = cksum;
725 if (cksum == DLE)
726 *dst++ = DLE;
727 *dst++ = DLE;
728 *dst++ = ETX;
730 i = dst-garmin_data_p->outbuffer;
732 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
734 garmin_data_p->pkt_id = pktid;
735 garmin_data_p->state = STATE_WAIT_TTY_ACK;
737 return i;
745 * Process the next pending data packet - if there is one
747 static void gsp_next_packet(struct garmin_data * garmin_data_p)
749 struct garmin_packet *pkt = NULL;
751 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
752 dbg("%s - next pkt: %d", __FUNCTION__, pkt->seq);
753 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
754 kfree(pkt);
755 return;
757 kfree(pkt);
764 /******************************************************************************
765 * garmin native mode
766 ******************************************************************************/
770 * Called for data received from tty
772 * The input data is expected to be in garmin usb-packet format.
774 * buf contains the data read, it may span more than one packet
775 * or even incomplete packets
777 static int nat_receive(struct garmin_data * garmin_data_p,
778 const unsigned char *buf, int count)
780 unsigned long flags;
781 __u8 * dest;
782 int offs = 0;
783 int result = count;
784 int len;
786 while (offs < count) {
787 // if buffer contains header, copy rest of data
788 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
789 len = GARMIN_PKTHDR_LENGTH
790 +getDataLength(garmin_data_p->inbuffer);
791 else
792 len = GARMIN_PKTHDR_LENGTH;
794 if (len >= GPS_IN_BUFSIZ) {
795 /* seem to be an invalid packet, ignore rest of input */
796 dbg("%s - packet size too large: %d",
797 __FUNCTION__, len);
798 garmin_data_p->insize = 0;
799 count = 0;
800 result = -EINVPKT;
801 } else {
802 len -= garmin_data_p->insize;
803 if (len > (count-offs))
804 len = (count-offs);
805 if (len > 0) {
806 dest = garmin_data_p->inbuffer
807 +garmin_data_p->insize;
808 memcpy(dest, buf+offs, len);
809 garmin_data_p->insize += len;
810 offs += len;
814 /* do we have a complete packet ? */
815 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
816 len = GARMIN_PKTHDR_LENGTH+
817 getDataLength(garmin_data_p->inbuffer);
818 if (garmin_data_p->insize >= len) {
819 garmin_write_bulk (garmin_data_p->port,
820 garmin_data_p->inbuffer,
821 len, 0);
822 garmin_data_p->insize = 0;
824 /* if this was an abort-transfer command,
825 flush all queued data. */
826 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
827 spin_lock_irqsave(&garmin_data_p->lock, flags);
828 garmin_data_p->flags |= FLAGS_DROP_DATA;
829 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
830 pkt_clear(garmin_data_p);
835 return result;
839 /******************************************************************************
840 * private packets
841 ******************************************************************************/
843 static void priv_status_resp(struct usb_serial_port *port)
845 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
846 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
848 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
849 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
850 pkt[2] = __cpu_to_le32(12);
851 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
852 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
853 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
855 send_to_tty(port, (__u8*)pkt, 6*4);
859 /******************************************************************************
860 * Garmin specific driver functions
861 ******************************************************************************/
863 static int process_resetdev_request(struct usb_serial_port *port)
865 unsigned long flags;
866 int status;
867 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
869 spin_lock_irqsave(&garmin_data_p->lock, flags);
870 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
871 garmin_data_p->state = STATE_RESET;
872 garmin_data_p->serial_num = 0;
873 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
875 usb_kill_urb (port->interrupt_in_urb);
876 dbg("%s - usb_reset_device", __FUNCTION__ );
877 status = usb_reset_device(port->serial->dev);
878 if (status)
879 dbg("%s - usb_reset_device failed: %d",
880 __FUNCTION__, status);
881 return status;
887 * clear all cached data
889 static int garmin_clear(struct garmin_data * garmin_data_p)
891 unsigned long flags;
892 int status = 0;
894 struct usb_serial_port *port = garmin_data_p->port;
896 if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
897 /* send a terminate command */
898 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
899 sizeof(GARMIN_STOP_TRANSFER_REQ),
903 /* flush all queued data */
904 pkt_clear(garmin_data_p);
906 spin_lock_irqsave(&garmin_data_p->lock, flags);
907 garmin_data_p->insize = 0;
908 garmin_data_p->outsize = 0;
909 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
911 return status;
919 static int garmin_init_session(struct usb_serial_port *port)
921 unsigned long flags;
922 struct usb_serial *serial = port->serial;
923 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
924 int status = 0;
926 if (status == 0) {
927 usb_kill_urb (port->interrupt_in_urb);
929 dbg("%s - adding interrupt input", __FUNCTION__);
930 port->interrupt_in_urb->dev = serial->dev;
931 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
932 if (status)
933 dev_err(&serial->dev->dev,
934 "%s - failed submitting interrupt urb,"
935 " error %d\n",
936 __FUNCTION__, status);
939 if (status == 0) {
940 dbg("%s - starting session ...", __FUNCTION__);
941 garmin_data_p->state = STATE_ACTIVE;
942 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
943 sizeof(GARMIN_START_SESSION_REQ),
946 if (status >= 0) {
948 spin_lock_irqsave(&garmin_data_p->lock, flags);
949 garmin_data_p->ignorePkts++;
950 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
952 /* not needed, but the win32 driver does it too ... */
953 status = garmin_write_bulk(port,
954 GARMIN_START_SESSION_REQ2,
955 sizeof(GARMIN_START_SESSION_REQ2),
957 if (status >= 0) {
958 status = 0;
959 spin_lock_irqsave(&garmin_data_p->lock, flags);
960 garmin_data_p->ignorePkts++;
961 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
966 return status;
973 static int garmin_open (struct usb_serial_port *port, struct file *filp)
975 unsigned long flags;
976 int status = 0;
977 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
979 dbg("%s - port %d", __FUNCTION__, port->number);
982 * Force low_latency on so that our tty_push actually forces the data
983 * through, otherwise it is scheduled, and with high data rates (like
984 * with OHCI) data can get lost.
986 if (port->tty)
987 port->tty->low_latency = 1;
989 spin_lock_irqsave(&garmin_data_p->lock, flags);
990 garmin_data_p->mode = initial_mode;
991 garmin_data_p->count = 0;
992 garmin_data_p->flags = 0;
993 atomic_set(&garmin_data_p->req_count, 0);
994 atomic_set(&garmin_data_p->resp_count, 0);
995 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
997 /* shutdown any bulk reads that might be going on */
998 usb_kill_urb (port->write_urb);
999 usb_kill_urb (port->read_urb);
1001 if (garmin_data_p->state == STATE_RESET) {
1002 status = garmin_init_session(port);
1005 garmin_data_p->state = STATE_ACTIVE;
1007 return status;
1011 static void garmin_close (struct usb_serial_port *port, struct file * filp)
1013 struct usb_serial *serial = port->serial;
1014 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1016 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __FUNCTION__,
1017 port->number, garmin_data_p->mode,
1018 garmin_data_p->state, garmin_data_p->flags);
1020 if (!serial)
1021 return;
1023 garmin_clear(garmin_data_p);
1025 /* shutdown our urbs */
1026 usb_kill_urb (port->read_urb);
1027 usb_kill_urb (port->write_urb);
1029 if (noResponseFromAppLayer(garmin_data_p) ||
1030 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1031 process_resetdev_request(port);
1032 garmin_data_p->state = STATE_RESET;
1033 } else {
1034 garmin_data_p->state = STATE_DISCONNECTED;
1039 static void garmin_write_bulk_callback (struct urb *urb)
1041 unsigned long flags;
1042 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1043 int status = urb->status;
1045 if (port) {
1046 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1048 dbg("%s - port %d", __FUNCTION__, port->number);
1050 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)
1051 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1052 gsp_send_ack(garmin_data_p, ((__u8 *)urb->transfer_buffer)[4]);
1055 if (status) {
1056 dbg("%s - nonzero write bulk status received: %d",
1057 __FUNCTION__, urb->status);
1058 spin_lock_irqsave(&garmin_data_p->lock, flags);
1059 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1060 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1063 usb_serial_port_softint(port);
1066 /* Ignore errors that resulted from garmin_write_bulk with dismiss_ack=1 */
1068 /* free up the transfer buffer, as usb_free_urb() does not do this */
1069 kfree (urb->transfer_buffer);
1073 static int garmin_write_bulk (struct usb_serial_port *port,
1074 const unsigned char *buf, int count,
1075 int dismiss_ack)
1077 unsigned long flags;
1078 struct usb_serial *serial = port->serial;
1079 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1080 struct urb *urb;
1081 unsigned char *buffer;
1082 int status;
1084 dbg("%s - port %d, state %d", __FUNCTION__, port->number,
1085 garmin_data_p->state);
1087 spin_lock_irqsave(&garmin_data_p->lock, flags);
1088 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1089 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1091 buffer = kmalloc (count, GFP_ATOMIC);
1092 if (!buffer) {
1093 dev_err(&port->dev, "out of memory\n");
1094 return -ENOMEM;
1097 urb = usb_alloc_urb(0, GFP_ATOMIC);
1098 if (!urb) {
1099 dev_err(&port->dev, "no more free urbs\n");
1100 kfree (buffer);
1101 return -ENOMEM;
1104 memcpy (buffer, buf, count);
1106 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
1108 usb_fill_bulk_urb (urb, serial->dev,
1109 usb_sndbulkpipe (serial->dev,
1110 port->bulk_out_endpointAddress),
1111 buffer, count,
1112 garmin_write_bulk_callback,
1113 dismiss_ack ? NULL : port);
1114 urb->transfer_flags |= URB_ZERO_PACKET;
1116 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1117 atomic_inc(&garmin_data_p->req_count);
1118 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1119 pkt_clear(garmin_data_p);
1120 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1124 /* send it down the pipe */
1125 status = usb_submit_urb(urb, GFP_ATOMIC);
1126 if (status) {
1127 dev_err(&port->dev,
1128 "%s - usb_submit_urb(write bulk) "
1129 "failed with status = %d\n",
1130 __FUNCTION__, status);
1131 count = status;
1134 /* we are done with this urb, so let the host driver
1135 * really free it when it is finished with it */
1136 usb_free_urb (urb);
1138 return count;
1143 static int garmin_write (struct usb_serial_port *port,
1144 const unsigned char *buf, int count)
1146 int pktid, pktsiz, len;
1147 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1148 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1150 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buf);
1152 /* check for our private packets */
1153 if (count >= GARMIN_PKTHDR_LENGTH) {
1155 len = PRIVPKTSIZ;
1156 if (count < len)
1157 len = count;
1159 memcpy(garmin_data_p->privpkt, buf, len);
1161 pktsiz = getDataLength(garmin_data_p->privpkt);
1162 pktid = getPacketId(garmin_data_p->privpkt);
1164 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1165 && GARMIN_LAYERID_PRIVATE == getLayerId(garmin_data_p->privpkt)) {
1167 dbg("%s - processing private request %d",
1168 __FUNCTION__, pktid);
1170 // drop all unfinished transfers
1171 garmin_clear(garmin_data_p);
1173 switch(pktid) {
1175 case PRIV_PKTID_SET_DEBUG:
1176 if (pktsiz != 4)
1177 return -EINVPKT;
1178 debug = __le32_to_cpu(privpkt[3]);
1179 dbg("%s - debug level set to 0x%X",
1180 __FUNCTION__, debug);
1181 break;
1183 case PRIV_PKTID_SET_MODE:
1184 if (pktsiz != 4)
1185 return -EINVPKT;
1186 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1187 dbg("%s - mode set to %d",
1188 __FUNCTION__, garmin_data_p->mode);
1189 break;
1191 case PRIV_PKTID_INFO_REQ:
1192 priv_status_resp(port);
1193 break;
1195 case PRIV_PKTID_RESET_REQ:
1196 atomic_inc(&garmin_data_p->req_count);
1197 break;
1199 case PRIV_PKTID_SET_DEF_MODE:
1200 if (pktsiz != 4)
1201 return -EINVPKT;
1202 initial_mode = __le32_to_cpu(privpkt[3]);
1203 dbg("%s - initial_mode set to %d",
1204 __FUNCTION__,
1205 garmin_data_p->mode);
1206 break;
1208 return count;
1212 garmin_data_p->ignorePkts = 0;
1214 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1215 return gsp_receive(garmin_data_p, buf, count);
1216 } else { /* MODE_NATIVE */
1217 return nat_receive(garmin_data_p, buf, count);
1222 static int garmin_write_room (struct usb_serial_port *port)
1225 * Report back the bytes currently available in the output buffer.
1227 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1228 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1232 static int garmin_chars_in_buffer (struct usb_serial_port *port)
1235 * Report back the number of bytes currently in our input buffer.
1236 * Will this lock up the driver - the buffer contains an incomplete
1237 * package which will not be written to the device until it
1238 * has been completed ?
1240 //struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1241 //return garmin_data_p->insize;
1242 return 0;
1246 static void garmin_read_process(struct garmin_data * garmin_data_p,
1247 unsigned char *data, unsigned data_length)
1249 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1250 /* abort-transfer cmd is actice */
1251 dbg("%s - pkt dropped", __FUNCTION__);
1252 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1253 garmin_data_p->state != STATE_RESET ) {
1255 /* remember any appl.layer packets, so we know
1256 if a reset is required or not when closing
1257 the device */
1258 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1259 sizeof(GARMIN_APP_LAYER_REPLY))) {
1260 atomic_inc(&garmin_data_p->resp_count);
1263 /* if throttling is active or postprecessing is required
1264 put the received data in the input queue, otherwise
1265 send it directly to the tty port */
1266 if (garmin_data_p->flags & FLAGS_QUEUING) {
1267 pkt_add(garmin_data_p, data, data_length);
1268 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1269 if (getLayerId(data) == GARMIN_LAYERID_APPL) {
1270 pkt_add(garmin_data_p, data, data_length);
1272 } else {
1273 send_to_tty(garmin_data_p->port, data, data_length);
1279 static void garmin_read_bulk_callback (struct urb *urb)
1281 unsigned long flags;
1282 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1283 struct usb_serial *serial = port->serial;
1284 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1285 unsigned char *data = urb->transfer_buffer;
1286 int status = urb->status;
1287 int retval;
1289 dbg("%s - port %d", __FUNCTION__, port->number);
1291 if (!serial) {
1292 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1293 return;
1296 if (status) {
1297 dbg("%s - nonzero read bulk status received: %d",
1298 __FUNCTION__, status);
1299 return;
1302 usb_serial_debug_data(debug, &port->dev,
1303 __FUNCTION__, urb->actual_length, data);
1305 garmin_read_process(garmin_data_p, data, urb->actual_length);
1307 if (urb->actual_length == 0 &&
1308 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1309 spin_lock_irqsave(&garmin_data_p->lock, flags);
1310 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1311 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1312 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1313 if (retval)
1314 dev_err(&port->dev,
1315 "%s - failed resubmitting read urb, error %d\n",
1316 __FUNCTION__, retval);
1317 } else if (urb->actual_length > 0) {
1318 /* Continue trying to read until nothing more is received */
1319 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1320 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1321 if (retval)
1322 dev_err(&port->dev,
1323 "%s - failed resubmitting read urb, "
1324 "error %d\n", __FUNCTION__, retval);
1326 } else {
1327 dbg("%s - end of bulk data", __FUNCTION__);
1328 spin_lock_irqsave(&garmin_data_p->lock, flags);
1329 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1330 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1332 return;
1336 static void garmin_read_int_callback (struct urb *urb)
1338 unsigned long flags;
1339 int retval;
1340 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1341 struct usb_serial *serial = port->serial;
1342 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1343 unsigned char *data = urb->transfer_buffer;
1344 int status = urb->status;
1346 switch (status) {
1347 case 0:
1348 /* success */
1349 break;
1350 case -ECONNRESET:
1351 case -ENOENT:
1352 case -ESHUTDOWN:
1353 /* this urb is terminated, clean up */
1354 dbg("%s - urb shutting down with status: %d",
1355 __FUNCTION__, status);
1356 return;
1357 default:
1358 dbg("%s - nonzero urb status received: %d",
1359 __FUNCTION__, status);
1360 return;
1363 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1364 urb->actual_length, urb->transfer_buffer);
1366 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1367 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1368 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1370 dbg("%s - bulk data available.", __FUNCTION__);
1372 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1374 /* bulk data available */
1375 usb_fill_bulk_urb (port->read_urb, serial->dev,
1376 usb_rcvbulkpipe (serial->dev,
1377 port->bulk_in_endpointAddress),
1378 port->read_urb->transfer_buffer,
1379 port->read_urb->transfer_buffer_length,
1380 garmin_read_bulk_callback, port);
1381 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1382 if (retval) {
1383 dev_err(&port->dev,
1384 "%s - failed submitting read urb, error %d\n",
1385 __FUNCTION__, retval);
1386 } else {
1387 spin_lock_irqsave(&garmin_data_p->lock, flags);
1388 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1389 /* do not send this packet to the user */
1390 garmin_data_p->ignorePkts = 1;
1391 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1393 } else {
1394 /* bulk-in transfer still active */
1395 spin_lock_irqsave(&garmin_data_p->lock, flags);
1396 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1397 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1400 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1401 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1402 sizeof(GARMIN_START_SESSION_REPLY))) {
1404 spin_lock_irqsave(&garmin_data_p->lock, flags);
1405 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1406 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1408 /* save the serial number */
1409 garmin_data_p->serial_num
1410 = __le32_to_cpup((__le32*)(data+GARMIN_PKTHDR_LENGTH));
1412 dbg("%s - start-of-session reply seen - serial %u.",
1413 __FUNCTION__, garmin_data_p->serial_num);
1416 if (garmin_data_p->ignorePkts) {
1417 /* this reply belongs to a request generated by the driver,
1418 ignore it. */
1419 dbg("%s - pkt ignored (%d)",
1420 __FUNCTION__, garmin_data_p->ignorePkts);
1421 spin_lock_irqsave(&garmin_data_p->lock, flags);
1422 garmin_data_p->ignorePkts--;
1423 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1424 } else {
1425 garmin_read_process(garmin_data_p, data, urb->actual_length);
1428 port->interrupt_in_urb->dev = port->serial->dev;
1429 retval = usb_submit_urb (urb, GFP_ATOMIC);
1430 if (retval)
1431 dev_err(&urb->dev->dev,
1432 "%s - Error %d submitting interrupt urb\n",
1433 __FUNCTION__, retval);
1438 * Sends the next queued packt to the tty port (garmin native mode only)
1439 * and then sets a timer to call itself again until all queued data
1440 * is sent.
1442 static int garmin_flush_queue(struct garmin_data * garmin_data_p)
1444 unsigned long flags;
1445 struct garmin_packet *pkt;
1447 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1448 pkt = pkt_pop(garmin_data_p);
1449 if (pkt != NULL) {
1450 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1451 kfree(pkt);
1452 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1454 } else {
1455 spin_lock_irqsave(&garmin_data_p->lock, flags);
1456 garmin_data_p->flags &= ~FLAGS_QUEUING;
1457 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1460 return 0;
1464 static void garmin_throttle (struct usb_serial_port *port)
1466 unsigned long flags;
1467 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1469 dbg("%s - port %d", __FUNCTION__, port->number);
1470 /* set flag, data received will be put into a queue
1471 for later processing */
1472 spin_lock_irqsave(&garmin_data_p->lock, flags);
1473 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1474 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1478 static void garmin_unthrottle (struct usb_serial_port *port)
1480 unsigned long flags;
1481 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1482 int status;
1484 dbg("%s - port %d", __FUNCTION__, port->number);
1485 spin_lock_irqsave(&garmin_data_p->lock, flags);
1486 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1487 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1489 /* in native mode send queued data to tty, in
1490 serial mode nothing needs to be done here */
1491 if (garmin_data_p->mode == MODE_NATIVE)
1492 garmin_flush_queue(garmin_data_p);
1494 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1495 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1496 if (status)
1497 dev_err(&port->dev,
1498 "%s - failed resubmitting read urb, error %d\n",
1499 __FUNCTION__, status);
1506 * The timer is currently only used to send queued packets to
1507 * the tty in cases where the protocol provides no own handshaking
1508 * to initiate the transfer.
1510 static void timeout_handler(unsigned long data)
1512 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1514 /* send the next queued packet to the tty port */
1515 if (garmin_data_p->mode == MODE_NATIVE)
1516 if (garmin_data_p->flags & FLAGS_QUEUING)
1517 garmin_flush_queue(garmin_data_p);
1522 static int garmin_attach (struct usb_serial *serial)
1524 int status = 0;
1525 struct usb_serial_port *port = serial->port[0];
1526 struct garmin_data * garmin_data_p = NULL;
1528 dbg("%s", __FUNCTION__);
1530 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1531 if (garmin_data_p == NULL) {
1532 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
1533 return -ENOMEM;
1535 init_timer(&garmin_data_p->timer);
1536 spin_lock_init(&garmin_data_p->lock);
1537 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1538 //garmin_data_p->timer.expires = jiffies + session_timeout;
1539 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1540 garmin_data_p->timer.function = timeout_handler;
1541 garmin_data_p->port = port;
1542 garmin_data_p->state = 0;
1543 garmin_data_p->count = 0;
1544 usb_set_serial_port_data(port, garmin_data_p);
1546 status = garmin_init_session(port);
1548 return status;
1552 static void garmin_shutdown (struct usb_serial *serial)
1554 struct usb_serial_port *port = serial->port[0];
1555 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1557 dbg("%s", __FUNCTION__);
1559 usb_kill_urb (port->interrupt_in_urb);
1560 del_timer_sync(&garmin_data_p->timer);
1561 kfree (garmin_data_p);
1562 usb_set_serial_port_data(port, NULL);
1566 /* All of the device info needed */
1567 static struct usb_serial_driver garmin_device = {
1568 .driver = {
1569 .owner = THIS_MODULE,
1570 .name = "garmin_gps",
1572 .description = "Garmin GPS usb/tty",
1573 .usb_driver = &garmin_driver,
1574 .id_table = id_table,
1575 .num_interrupt_in = 1,
1576 .num_bulk_in = 1,
1577 .num_bulk_out = 1,
1578 .num_ports = 1,
1579 .open = garmin_open,
1580 .close = garmin_close,
1581 .throttle = garmin_throttle,
1582 .unthrottle = garmin_unthrottle,
1583 .attach = garmin_attach,
1584 .shutdown = garmin_shutdown,
1585 .write = garmin_write,
1586 .write_room = garmin_write_room,
1587 .chars_in_buffer = garmin_chars_in_buffer,
1588 .write_bulk_callback = garmin_write_bulk_callback,
1589 .read_bulk_callback = garmin_read_bulk_callback,
1590 .read_int_callback = garmin_read_int_callback,
1595 static int __init garmin_init (void)
1597 int retval;
1599 retval = usb_serial_register(&garmin_device);
1600 if (retval)
1601 goto failed_garmin_register;
1602 retval = usb_register(&garmin_driver);
1603 if (retval)
1604 goto failed_usb_register;
1605 info(DRIVER_DESC " " DRIVER_VERSION);
1607 return 0;
1608 failed_usb_register:
1609 usb_serial_deregister(&garmin_device);
1610 failed_garmin_register:
1611 return retval;
1615 static void __exit garmin_exit (void)
1617 usb_deregister (&garmin_driver);
1618 usb_serial_deregister (&garmin_device);
1624 module_init(garmin_init);
1625 module_exit(garmin_exit);
1627 MODULE_AUTHOR( DRIVER_AUTHOR );
1628 MODULE_DESCRIPTION( DRIVER_DESC );
1629 MODULE_LICENSE("GPL");
1631 module_param(debug, bool, S_IWUSR | S_IRUGO);
1632 MODULE_PARM_DESC(debug, "Debug enabled or not");
1633 module_param(initial_mode, int, S_IRUGO);
1634 MODULE_PARM_DESC(initial_mode, "Initial mode");