4 * Copyright (C) 2004 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/config.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/timer.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
35 #include <linux/module.h>
36 #include <linux/spinlock.h>
37 #include <asm/uaccess.h>
38 #include <linux/usb.h>
40 /* the mode to be set when the port ist opened */
41 static int initial_mode = 1;
46 #include "usb-serial.h"
48 #define GARMIN_VENDOR_ID 0x091E
54 #define VERSION_MAJOR 0
55 #define VERSION_MINOR 23
58 #define _DRIVER_VERSION(a,b) "v" _STR(a) "." _STR(b)
59 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
60 #define DRIVER_AUTHOR "hermann kneissel"
61 #define DRIVER_DESC "garmin gps driver"
63 /* error codes returned by the driver */
64 #define EINVPKT 1000 /* invalid packet structure */
67 // size of the header of a packet using the usb protocol
68 #define GARMIN_PKTHDR_LENGTH 12
70 // max. possible size of a packet using the serial protocol
71 #define MAX_SERIAL_PKT_SIZ (3+255+3)
73 // max. possible size of a packet with worst case stuffing
74 #define MAX_SERIAL_PKT_SIZ_STUFFED MAX_SERIAL_PKT_SIZ+256
76 // size of a buffer able to hold a complete (no stuffing) packet
77 // (the document protocol does not contain packets with a larger
78 // size, but in theory a packet may be 64k+12 bytes - if in
79 // later protocol versions larger packet sizes occur, this value
80 // should be increased accordingly, so the input buffer is always
81 // large enough the store a complete packet inclusive header)
82 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
84 // size of a buffer able to hold a complete (incl. stuffing) packet
85 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
87 // where to place the packet id of a serial packet, so we can
88 // prepend the usb-packet header without the need to move the
90 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
92 // max. size of incoming private packets (header+1 param)
93 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
95 #define GARMIN_LAYERID_TRANSPORT 0
96 #define GARMIN_LAYERID_APPL 20
97 // our own layer-id to use for some control mechanisms
98 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
100 #define GARMIN_PKTID_PVT_DATA 51
101 #define GARMIN_PKTID_L001_COMMAND_DATA 10
103 #define CMND_ABORT_TRANSFER 0
105 // packet ids used in private layer
106 #define PRIV_PKTID_SET_DEBUG 1
107 #define PRIV_PKTID_SET_MODE 2
108 #define PRIV_PKTID_INFO_REQ 3
109 #define PRIV_PKTID_INFO_RESP 4
110 #define PRIV_PKTID_RESET_REQ 5
111 #define PRIV_PKTID_SET_DEF_MODE 6
119 /* structure used to queue incoming packets */
120 struct garmin_packet {
121 struct list_head list;
123 int size; // the real size of the data array, always > 0
127 /* structure used to keep the current state of the driver */
136 struct timer_list timer;
137 struct usb_serial_port *port;
141 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
142 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
145 struct list_head pktlist;
150 #define STATE_INITIAL_DELAY 1
151 #define STATE_TIMEOUT 2
152 #define STATE_SESSION_REQ1 3
153 #define STATE_SESSION_REQ2 4
154 #define STATE_ACTIVE 5
156 #define STATE_RESET 8
157 #define STATE_DISCONNECTED 9
158 #define STATE_WAIT_TTY_ACK 10
159 #define STATE_GSP_WAIT_DATA 11
161 #define MODE_NATIVE 0
162 #define MODE_GARMIN_SERIAL 1
164 // Flags used in garmin_data.flags:
165 #define FLAGS_SESSION_REPLY_MASK 0x00C0
166 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
167 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
168 #define FLAGS_BULK_IN_ACTIVE 0x0020
169 #define FLAGS_THROTTLED 0x0010
170 #define CLEAR_HALT_REQUIRED 0x0001
172 #define FLAGS_QUEUING 0x0100
173 #define FLAGS_APP_RESP_SEEN 0x0200
174 #define FLAGS_APP_REQ_SEEN 0x0400
175 #define FLAGS_DROP_DATA 0x0800
177 #define FLAGS_GSP_SKIP 0x1000
178 #define FLAGS_GSP_DLESEEN 0x2000
185 /* function prototypes */
186 static void gsp_next_packet(struct garmin_data * garmin_data_p);
187 static int garmin_write_bulk(struct usb_serial_port *port,
188 const unsigned char *buf, int count);
190 /* some special packets to be send or received */
191 static unsigned char const GARMIN_START_SESSION_REQ[]
192 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
193 static unsigned char const GARMIN_START_SESSION_REQ2[]
194 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
195 static unsigned char const GARMIN_START_SESSION_REPLY[]
196 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
197 static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
198 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
199 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
200 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
201 static unsigned char const GARMIN_APP_LAYER_REPLY[]
203 static unsigned char const GARMIN_START_PVT_REQ[]
204 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
205 static unsigned char const GARMIN_STOP_PVT_REQ[]
206 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
207 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
208 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
209 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
210 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
211 static unsigned char const PRIVATE_REQ[]
212 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
216 static struct usb_device_id id_table [] = {
217 /* the same device id seems to be used by all usb enabled gps devices */
218 { USB_DEVICE(GARMIN_VENDOR_ID, 3 ) },
219 { } /* Terminating entry */
222 MODULE_DEVICE_TABLE (usb, id_table);
224 static struct usb_driver garmin_driver = {
225 .owner = THIS_MODULE,
226 .name = "garmin_gps",
227 .probe = usb_serial_probe,
228 .disconnect = usb_serial_disconnect,
229 .id_table = id_table,
233 static inline int noResponseFromAppLayer(struct garmin_data * garmin_data_p)
235 return ((garmin_data_p->flags
236 & (FLAGS_APP_REQ_SEEN|FLAGS_APP_RESP_SEEN))
237 == FLAGS_APP_REQ_SEEN);
241 static inline int getLayerId(const __u8 *usbPacket)
243 return __le32_to_cpup((__le32 *)(usbPacket));
246 static inline int getPacketId(const __u8 *usbPacket)
248 return __le32_to_cpup((__le32 *)(usbPacket+4));
251 static inline int getDataLength(const __u8 *usbPacket)
253 return __le32_to_cpup((__le32 *)(usbPacket+8));
258 * check if the usb-packet in buf contains an abort-transfer command.
259 * (if yes, all queued data will be dropped)
261 static inline int isAbortTrfCmnd(const unsigned char *buf)
263 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
264 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
265 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
266 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
274 static void send_to_tty(struct usb_serial_port *port,
275 char *data, unsigned int actual_length)
277 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 for (i = 0; i < actual_length ; ++i) {
286 /* if we insert more than TTY_FLIPBUF_SIZE characters,
288 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
289 tty_flip_buffer_push(tty);
291 /* this doesn't actually push the data through unless
292 tty->low_latency is set */
293 tty_insert_flip_char(tty, data[i], 0);
295 tty_flip_buffer_push(tty);
300 /******************************************************************************
301 * packet queue handling
302 ******************************************************************************/
305 * queue a received (usb-)packet for later processing
307 static int pkt_add(struct garmin_data * garmin_data_p,
308 unsigned char *data, unsigned int data_length)
312 struct garmin_packet *pkt;
314 /* process only packets containg data ... */
316 garmin_data_p->flags |= FLAGS_QUEUING;
317 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
320 dev_err(&garmin_data_p->port->dev, "out of memory\n");
323 pkt->size = data_length;
324 memcpy(pkt->data, data, data_length);
326 spin_lock_irqsave(&garmin_data_p->lock, flags);
327 result = list_empty(&garmin_data_p->pktlist);
328 pkt->seq = garmin_data_p->seq_counter++;
329 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
330 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
332 /* in serial mode, if someone is waiting for data from
333 the device, iconvert and send the next packet to tty. */
334 if (result && (garmin_data_p->state == STATE_GSP_WAIT_DATA)) {
335 gsp_next_packet(garmin_data_p);
342 /* get the next pending packet */
343 static struct garmin_packet *pkt_pop(struct garmin_data * garmin_data_p)
346 struct garmin_packet *result = NULL;
348 spin_lock_irqsave(&garmin_data_p->lock, flags);
349 if (!list_empty(&garmin_data_p->pktlist)) {
350 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
351 list_del(&result->list);
353 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
358 /* free up all queued data */
359 static void pkt_clear(struct garmin_data * garmin_data_p)
362 struct garmin_packet *result = NULL;
364 dbg("%s", __FUNCTION__);
366 spin_lock_irqsave(&garmin_data_p->lock, flags);
367 while (!list_empty(&garmin_data_p->pktlist)) {
368 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
369 list_del(&result->list);
372 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
376 /******************************************************************************
377 * garmin serial protocol handling handling
378 ******************************************************************************/
380 /* send an ack packet back to the tty */
381 static int gsp_send_ack(struct garmin_data * garmin_data_p, __u8 pkt_id)
388 dbg("%s - pkt-id: 0x%X.", __FUNCTION__, 0xFF & pkt_id);
405 *ptr++ = 0xFF & (-cksum);
411 send_to_tty(garmin_data_p->port, pkt, l);
418 * called for a complete packet received from tty layer
420 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
421 * at GSP_INITIAL_OFFSET.
423 * count - number of bytes in the input buffer including space reserved for
424 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
425 * (including pkt-id, data-length a. cksum)
427 static int gsp_rec_packet(struct garmin_data * garmin_data_p, int count)
429 const __u8* recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
430 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
434 int pktid = recpkt[0];
435 int size = recpkt[1];
437 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
438 __FUNCTION__, count-GSP_INITIAL_OFFSET, recpkt);
440 if (size != (count-GSP_INITIAL_OFFSET-3)) {
441 dbg("%s - invalid size, expected %d bytes, got %d",
442 __FUNCTION__, size, (count-GSP_INITIAL_OFFSET-3));
449 // sanity check, remove after test ...
450 if ((__u8*)&(usbdata[3]) != recpkt) {
451 dbg("%s - ptr mismatch %p - %p",
452 __FUNCTION__, &(usbdata[4]), recpkt);
461 if ((0xff & (cksum + *recpkt)) != 0) {
462 dbg("%s - invalid checksum, expected %02x, got %02x",
463 __FUNCTION__, 0xff & -cksum, 0xff & *recpkt);
467 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
468 usbdata[1] = __cpu_to_le32(pktid);
469 usbdata[2] = __cpu_to_le32(size);
471 garmin_write_bulk (garmin_data_p->port, garmin_data_p->inbuffer,
472 GARMIN_PKTHDR_LENGTH+size);
474 /* if this was an abort-transfer command, flush all
476 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
477 garmin_data_p->flags |= FLAGS_DROP_DATA;
478 pkt_clear(garmin_data_p);
487 * Called for data received from tty
489 * buf contains the data read, it may span more than one packet or even
492 * input record should be a serial-record, but it may not be complete.
493 * Copy it into our local buffer, until an etx is seen (or an error
495 * Once the record is complete, convert into a usb packet and send it
496 * to the bulk pipe, send an ack back to the tty.
498 * If the input is an ack, just send the last queued packet to the
501 * if the input is an abort command, drop all queued data.
504 static int gsp_receive(struct garmin_data * garmin_data_p,
505 const unsigned char *buf, int count)
508 int ack_or_nak_seen = 0;
510 __u8 *dest = garmin_data_p->inbuffer;
511 int size = garmin_data_p->insize;
512 // dleSeen: set if last byte read was a DLE
513 int dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
514 // skip: if set, skip incoming data until possible start of
516 int skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
519 dbg("%s - dle=%d skip=%d size=%d count=%d",
520 __FUNCTION__, dleSeen, skip, size, count);
523 size = GSP_INITIAL_OFFSET;
526 while (offs < count) {
532 if (skip) { /* start of a new pkt */
534 size = GSP_INITIAL_OFFSET;
536 } else if (dleSeen) {
542 } else if (data == ETX) {
544 /* packet complete */
546 data = dest[GSP_INITIAL_OFFSET];
549 ack_or_nak_seen = ACK;
550 dbg("ACK packet complete.");
551 } else if (data == NAK) {
552 ack_or_nak_seen = NAK;
553 dbg("NAK packet complete.");
555 dbg("packet complete "
558 gsp_rec_packet(garmin_data_p, size);
562 size = GSP_INITIAL_OFFSET;
570 dbg("non-masked DLE at %d - restarting", i);
571 size = GSP_INITIAL_OFFSET;
578 if (size >= GPS_IN_BUFSIZ) {
579 dbg("%s - packet too large.", __FUNCTION__);
581 size = GSP_INITIAL_OFFSET;
586 garmin_data_p->insize = size;
588 // copy flags back to structure
590 garmin_data_p->flags |= FLAGS_GSP_SKIP;
592 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
595 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
597 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
599 if (ack_or_nak_seen) {
600 garmin_data_p->state = STATE_GSP_WAIT_DATA;
601 gsp_next_packet(garmin_data_p);
611 * Sends a usb packet to the tty
613 * Assumes, that all packages and at an usb-packet boundary.
615 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
617 static int gsp_send(struct garmin_data * garmin_data_p,
618 const unsigned char *buf, int count)
620 const unsigned char *src;
628 dbg("%s - state %d - %d bytes.", __FUNCTION__,
629 garmin_data_p->state, count);
631 k = garmin_data_p->outsize;
632 if ((k+count) > GPS_OUT_BUFSIZ) {
633 dbg("packet too large");
634 garmin_data_p->outsize = 0;
638 memcpy(garmin_data_p->outbuffer+k, buf, count);
640 garmin_data_p->outsize = k;
642 if (k >= GARMIN_PKTHDR_LENGTH) {
643 pktid = getPacketId(garmin_data_p->outbuffer);
644 datalen= getDataLength(garmin_data_p->outbuffer);
645 i = GARMIN_PKTHDR_LENGTH + datalen;
652 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __FUNCTION__,
655 /* garmin_data_p->outbuffer now contains a complete packet */
657 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
658 __FUNCTION__, k, garmin_data_p->outbuffer);
660 garmin_data_p->outsize = 0;
662 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
663 dbg("not an application packet (%d)",
664 getLayerId(garmin_data_p->outbuffer));
669 dbg("packet-id %d too large", pktid);
674 dbg("packet-size %d too large", datalen);
678 /* the serial protocol should be able to handle this packet */
681 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
682 for (i=0; i<datalen; i++) {
687 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
688 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
689 /* can't add stuffing DLEs in place, move data to end
691 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
692 memcpy(dst, src, datalen);
696 dst = garmin_data_p->outbuffer;
706 for (i=0; i<datalen; i++) {
714 cksum = 0xFF & -cksum;
721 i = dst-garmin_data_p->outbuffer;
723 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
725 garmin_data_p->pkt_id = pktid;
726 garmin_data_p->state = STATE_WAIT_TTY_ACK;
736 * Process the next pending data packet - if there is one
738 static void gsp_next_packet(struct garmin_data * garmin_data_p)
740 struct garmin_packet *pkt = NULL;
742 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
743 dbg("%s - next pkt: %d", __FUNCTION__, pkt->seq);
744 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
755 /******************************************************************************
757 ******************************************************************************/
761 * Called for data received from tty
763 * The input data is expected to be in garmin usb-packet format.
765 * buf contains the data read, it may span more than one packet
766 * or even incomplete packets
768 static int nat_receive(struct garmin_data * garmin_data_p,
769 const unsigned char *buf, int count)
776 while (offs < count) {
777 // if buffer contains header, copy rest of data
778 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
779 len = GARMIN_PKTHDR_LENGTH
780 +getDataLength(garmin_data_p->inbuffer);
782 len = GARMIN_PKTHDR_LENGTH;
784 if (len >= GPS_IN_BUFSIZ) {
785 /* seem to be an invalid packet, ignore rest of input */
786 dbg("%s - packet size too large: %d",
788 garmin_data_p->insize = 0;
792 len -= garmin_data_p->insize;
793 if (len > (count-offs))
796 dest = garmin_data_p->inbuffer
797 +garmin_data_p->insize;
798 memcpy(dest, buf+offs, len);
799 garmin_data_p->insize += len;
804 /* do we have a complete packet ? */
805 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
806 len = GARMIN_PKTHDR_LENGTH+
807 getDataLength(garmin_data_p->inbuffer);
808 if (garmin_data_p->insize >= len) {
809 garmin_write_bulk (garmin_data_p->port,
810 garmin_data_p->inbuffer,
812 garmin_data_p->insize = 0;
814 /* if this was an abort-transfer command,
815 flush all queued data. */
816 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
817 garmin_data_p->flags |= FLAGS_DROP_DATA;
818 pkt_clear(garmin_data_p);
827 /******************************************************************************
829 ******************************************************************************/
831 static void priv_status_resp(struct usb_serial_port *port)
833 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
834 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
836 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
837 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
838 pkt[2] = __cpu_to_le32(12);
839 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
840 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
841 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
843 send_to_tty(port, (__u8*)pkt, 6*4);
847 /******************************************************************************
848 * Garmin specific driver functions
849 ******************************************************************************/
851 static int process_resetdev_request(struct usb_serial_port *port)
854 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
856 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
857 garmin_data_p->state = STATE_RESET;
858 garmin_data_p->serial_num = 0;
860 usb_kill_urb (port->interrupt_in_urb);
861 dbg("%s - usb_reset_device", __FUNCTION__ );
862 status = usb_reset_device(port->serial->dev);
864 dbg("%s - usb_reset_device failed: %d",
865 __FUNCTION__, status);
872 * clear all cached data
874 static int garmin_clear(struct garmin_data * garmin_data_p)
878 struct usb_serial_port *port = garmin_data_p->port;
880 if (port != NULL && garmin_data_p->flags & FLAGS_APP_RESP_SEEN) {
881 /* send a terminate command */
882 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
883 sizeof(GARMIN_STOP_TRANSFER_REQ));
886 /* flush all queued data */
887 pkt_clear(garmin_data_p);
889 garmin_data_p->insize = 0;
890 garmin_data_p->outsize = 0;
900 static int garmin_init_session(struct usb_serial_port *port)
902 struct usb_serial *serial = port->serial;
903 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
907 usb_kill_urb (port->interrupt_in_urb);
909 dbg("%s - adding interrupt input", __FUNCTION__);
910 port->interrupt_in_urb->dev = serial->dev;
911 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
913 dev_err(&serial->dev->dev,
914 "%s - failed submitting interrupt urb,"
916 __FUNCTION__, status);
920 dbg("%s - starting session ...", __FUNCTION__);
921 garmin_data_p->state = STATE_ACTIVE;
922 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
923 sizeof(GARMIN_START_SESSION_REQ));
927 garmin_data_p->ignorePkts++;
929 /* not needed, but the win32 driver does it too ... */
930 status = garmin_write_bulk(port,
931 GARMIN_START_SESSION_REQ2,
932 sizeof(GARMIN_START_SESSION_REQ2));
935 garmin_data_p->ignorePkts++;
947 static int garmin_open (struct usb_serial_port *port, struct file *filp)
950 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
952 dbg("%s - port %d", __FUNCTION__, port->number);
955 * Force low_latency on so that our tty_push actually forces the data
956 * through, otherwise it is scheduled, and with high data rates (like
957 * with OHCI) data can get lost.
960 port->tty->low_latency = 1;
962 garmin_data_p->mode = initial_mode;
963 garmin_data_p->count = 0;
964 garmin_data_p->flags = 0;
966 /* shutdown any bulk reads that might be going on */
967 usb_kill_urb (port->write_urb);
968 usb_kill_urb (port->read_urb);
970 if (garmin_data_p->state == STATE_RESET) {
971 status = garmin_init_session(port);
974 garmin_data_p->state = STATE_ACTIVE;
980 static void garmin_close (struct usb_serial_port *port, struct file * filp)
982 struct usb_serial *serial = port->serial;
983 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
985 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __FUNCTION__,
986 port->number, garmin_data_p->mode,
987 garmin_data_p->state, garmin_data_p->flags);
992 garmin_clear(garmin_data_p);
994 /* shutdown our urbs */
995 usb_kill_urb (port->read_urb);
996 usb_kill_urb (port->write_urb);
998 if (noResponseFromAppLayer(garmin_data_p) ||
999 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1000 process_resetdev_request(port);
1001 garmin_data_p->state = STATE_RESET;
1003 garmin_data_p->state = STATE_DISCONNECTED;
1008 static void garmin_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
1010 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1011 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1013 /* free up the transfer buffer, as usb_free_urb() does not do this */
1014 kfree (urb->transfer_buffer);
1016 dbg("%s - port %d", __FUNCTION__, port->number);
1019 dbg("%s - nonzero write bulk status received: %d",
1020 __FUNCTION__, urb->status);
1021 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1024 schedule_work(&port->work);
1028 static int garmin_write_bulk (struct usb_serial_port *port,
1029 const unsigned char *buf, int count)
1031 struct usb_serial *serial = port->serial;
1032 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1034 unsigned char *buffer;
1037 dbg("%s - port %d, state %d", __FUNCTION__, port->number,
1038 garmin_data_p->state);
1040 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1042 buffer = kmalloc (count, GFP_ATOMIC);
1044 dev_err(&port->dev, "out of memory\n");
1048 urb = usb_alloc_urb(0, GFP_ATOMIC);
1050 dev_err(&port->dev, "no more free urbs\n");
1055 memcpy (buffer, buf, count);
1057 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
1059 usb_fill_bulk_urb (urb, serial->dev,
1060 usb_sndbulkpipe (serial->dev,
1061 port->bulk_out_endpointAddress),
1063 garmin_write_bulk_callback, port);
1064 urb->transfer_flags |= URB_ZERO_PACKET;
1066 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1067 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1068 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1069 pkt_clear(garmin_data_p);
1070 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1074 /* send it down the pipe */
1075 status = usb_submit_urb(urb, GFP_ATOMIC);
1078 "%s - usb_submit_urb(write bulk) "
1079 "failed with status = %d\n",
1080 __FUNCTION__, status);
1084 if (GARMIN_LAYERID_APPL == getLayerId(buffer)
1085 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1087 gsp_send_ack(garmin_data_p, buffer[4]);
1091 /* we are done with this urb, so let the host driver
1092 * really free it when it is finished with it */
1100 static int garmin_write (struct usb_serial_port *port,
1101 const unsigned char *buf, int count)
1103 int pktid, pktsiz, len;
1104 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1105 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1107 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buf);
1109 /* check for our private packets */
1110 if (count >= GARMIN_PKTHDR_LENGTH) {
1116 memcpy(garmin_data_p->privpkt, buf, len);
1118 pktsiz = getDataLength(garmin_data_p->privpkt);
1119 pktid = getPacketId(garmin_data_p->privpkt);
1121 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1122 && GARMIN_LAYERID_PRIVATE == getLayerId(garmin_data_p->privpkt)) {
1124 dbg("%s - processing private request %d",
1125 __FUNCTION__, pktid);
1127 // drop all unfinished transfers
1128 garmin_clear(garmin_data_p);
1132 case PRIV_PKTID_SET_DEBUG:
1135 debug = __le32_to_cpu(privpkt[3]);
1136 dbg("%s - debug level set to 0x%X",
1137 __FUNCTION__, debug);
1140 case PRIV_PKTID_SET_MODE:
1143 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1144 dbg("%s - mode set to %d",
1145 __FUNCTION__, garmin_data_p->mode);
1148 case PRIV_PKTID_INFO_REQ:
1149 priv_status_resp(port);
1152 case PRIV_PKTID_RESET_REQ:
1153 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1156 case PRIV_PKTID_SET_DEF_MODE:
1159 initial_mode = __le32_to_cpu(privpkt[3]);
1160 dbg("%s - initial_mode set to %d",
1162 garmin_data_p->mode);
1169 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1170 return gsp_receive(garmin_data_p, buf, count);
1171 } else { /* MODE_NATIVE */
1172 return nat_receive(garmin_data_p, buf, count);
1177 static int garmin_write_room (struct usb_serial_port *port)
1180 * Report back the bytes currently available in the output buffer.
1182 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1183 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1187 static int garmin_chars_in_buffer (struct usb_serial_port *port)
1190 * Report back the number of bytes currently in our input buffer.
1191 * Will this lock up the driver - the buffer contains an incomplete
1192 * package which will not be written to the device until it
1193 * has been completed ?
1195 //struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1196 //return garmin_data_p->insize;
1201 static void garmin_read_process(struct garmin_data * garmin_data_p,
1202 unsigned char *data, unsigned data_length)
1204 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1205 /* abort-transfer cmd is actice */
1206 dbg("%s - pkt dropped", __FUNCTION__);
1207 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1208 garmin_data_p->state != STATE_RESET ) {
1210 /* remember any appl.layer packets, so we know
1211 if a reset is required or not when closing
1213 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1214 sizeof(GARMIN_APP_LAYER_REPLY)))
1215 garmin_data_p->flags |= FLAGS_APP_RESP_SEEN;
1217 /* if throttling is active or postprecessing is required
1218 put the received data in th input queue, otherwise
1219 send it directly to the tty port */
1220 if (garmin_data_p->flags & FLAGS_QUEUING) {
1221 pkt_add(garmin_data_p, data, data_length);
1222 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1223 if (getLayerId(data) == GARMIN_LAYERID_APPL) {
1224 pkt_add(garmin_data_p, data, data_length);
1227 send_to_tty(garmin_data_p->port, data, data_length);
1233 static void garmin_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
1235 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1236 struct usb_serial *serial = port->serial;
1237 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1238 unsigned char *data = urb->transfer_buffer;
1241 dbg("%s - port %d", __FUNCTION__, port->number);
1244 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1249 dbg("%s - nonzero read bulk status received: %d",
1250 __FUNCTION__, urb->status);
1254 usb_serial_debug_data(debug, &port->dev,
1255 __FUNCTION__, urb->actual_length, data);
1257 garmin_read_process(garmin_data_p, data, urb->actual_length);
1259 /* Continue trying to read until nothing more is received */
1260 if (urb->actual_length > 0) {
1261 usb_fill_bulk_urb (port->read_urb, serial->dev,
1262 usb_rcvbulkpipe (serial->dev,
1263 port->bulk_in_endpointAddress),
1264 port->read_urb->transfer_buffer,
1265 port->read_urb->transfer_buffer_length,
1266 garmin_read_bulk_callback, port);
1267 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1270 "%s - failed resubmitting read urb, error %d\n",
1271 __FUNCTION__, status);
1277 static void garmin_read_int_callback (struct urb *urb, struct pt_regs *regs)
1280 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1281 struct usb_serial *serial = port->serial;
1282 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1283 unsigned char *data = urb->transfer_buffer;
1285 switch (urb->status) {
1292 /* this urb is terminated, clean up */
1293 dbg("%s - urb shutting down with status: %d",
1294 __FUNCTION__, urb->status);
1297 dbg("%s - nonzero urb status received: %d",
1298 __FUNCTION__, urb->status);
1302 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1303 urb->actual_length, urb->transfer_buffer);
1305 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1306 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1307 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1309 dbg("%s - bulk data available.", __FUNCTION__);
1311 /* bulk data available */
1312 usb_fill_bulk_urb (port->read_urb, serial->dev,
1313 usb_rcvbulkpipe (serial->dev,
1314 port->bulk_in_endpointAddress),
1315 port->read_urb->transfer_buffer,
1316 port->read_urb->transfer_buffer_length,
1317 garmin_read_bulk_callback, port);
1318 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1321 "%s - failed submitting read urb, error %d\n",
1322 __FUNCTION__, status);
1325 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1326 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1327 sizeof(GARMIN_START_SESSION_REPLY))) {
1329 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1331 /* save the serial number */
1332 garmin_data_p->serial_num
1333 = __le32_to_cpup((__le32*)(data+GARMIN_PKTHDR_LENGTH));
1335 dbg("%s - start-of-session reply seen - serial %u.",
1336 __FUNCTION__, garmin_data_p->serial_num);
1339 if (garmin_data_p->ignorePkts) {
1340 /* this reply belongs to a request generated by the driver,
1342 dbg("%s - pkt ignored (%d)",
1343 __FUNCTION__, garmin_data_p->ignorePkts);
1344 garmin_data_p->ignorePkts--;
1346 garmin_read_process(garmin_data_p, data, urb->actual_length);
1349 port->interrupt_in_urb->dev = port->serial->dev;
1350 status = usb_submit_urb (urb, GFP_ATOMIC);
1352 dev_err(&urb->dev->dev,
1353 "%s - Error %d submitting interrupt urb\n",
1354 __FUNCTION__, status);
1359 * Sends the next queued packt to the tty port (garmin native mode only)
1360 * and then sets a timer to call itself again until all queued data
1363 static int garmin_flush_queue(struct garmin_data * garmin_data_p)
1365 struct garmin_packet *pkt;
1367 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1368 pkt = pkt_pop(garmin_data_p);
1371 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1373 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1376 garmin_data_p->flags &= ~FLAGS_QUEUING;
1383 static void garmin_throttle (struct usb_serial_port *port)
1385 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1387 dbg("%s - port %d", __FUNCTION__, port->number);
1388 /* set flag, data received will be put into a queue
1389 for later processing */
1390 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1394 static void garmin_unthrottle (struct usb_serial_port *port)
1396 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1398 dbg("%s - port %d", __FUNCTION__, port->number);
1399 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1401 /* in native mode send queued data to tty, in
1402 serial mode nothing needs to be done here */
1403 if (garmin_data_p->mode == MODE_NATIVE)
1404 garmin_flush_queue(garmin_data_p);
1410 * The timer is currently only used to send queued packets to
1411 * the tty in cases where the protocol provides no own handshaking
1412 * to initiate the transfer.
1414 static void timeout_handler(unsigned long data)
1416 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1418 /* send the next queued packet to the tty port */
1419 if (garmin_data_p->mode == MODE_NATIVE)
1420 if (garmin_data_p->flags & FLAGS_QUEUING)
1421 garmin_flush_queue(garmin_data_p);
1426 static int garmin_attach (struct usb_serial *serial)
1429 struct usb_serial_port *port = serial->port[0];
1430 struct garmin_data * garmin_data_p = NULL;
1432 dbg("%s", __FUNCTION__);
1434 garmin_data_p = kmalloc (sizeof(struct garmin_data), GFP_KERNEL);
1435 if (garmin_data_p == NULL) {
1436 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
1439 memset (garmin_data_p, 0, sizeof(struct garmin_data));
1440 init_timer(&garmin_data_p->timer);
1441 spin_lock_init(&garmin_data_p->lock);
1442 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1443 //garmin_data_p->timer.expires = jiffies + session_timeout;
1444 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1445 garmin_data_p->timer.function = timeout_handler;
1446 garmin_data_p->port = port;
1447 garmin_data_p->state = 0;
1448 garmin_data_p->count = 0;
1449 usb_set_serial_port_data(port, garmin_data_p);
1451 status = garmin_init_session(port);
1457 static void garmin_shutdown (struct usb_serial *serial)
1459 struct usb_serial_port *port = serial->port[0];
1460 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1462 dbg("%s", __FUNCTION__);
1464 usb_kill_urb (port->interrupt_in_urb);
1465 del_timer_sync(&garmin_data_p->timer);
1466 kfree (garmin_data_p);
1467 usb_set_serial_port_data(port, NULL);
1476 /* All of the device info needed */
1477 static struct usb_serial_device_type garmin_device = {
1478 .owner = THIS_MODULE,
1479 .name = "Garmin GPS usb/tty",
1480 .short_name = "garmin_gps",
1481 .id_table = id_table,
1482 .num_interrupt_in = 1,
1486 .open = garmin_open,
1487 .close = garmin_close,
1488 .throttle = garmin_throttle,
1489 .unthrottle = garmin_unthrottle,
1490 .attach = garmin_attach,
1491 .shutdown = garmin_shutdown,
1492 .write = garmin_write,
1493 .write_room = garmin_write_room,
1494 .chars_in_buffer = garmin_chars_in_buffer,
1495 .write_bulk_callback = garmin_write_bulk_callback,
1496 .read_bulk_callback = garmin_read_bulk_callback,
1497 .read_int_callback = garmin_read_int_callback,
1501 static int __init garmin_init (void)
1505 retval = usb_serial_register(&garmin_device);
1507 goto failed_garmin_register;
1508 retval = usb_register(&garmin_driver);
1510 goto failed_usb_register;
1511 info(DRIVER_DESC " " DRIVER_VERSION);
1514 failed_usb_register:
1515 usb_serial_deregister(&garmin_device);
1516 failed_garmin_register:
1521 static void __exit garmin_exit (void)
1523 usb_deregister (&garmin_driver);
1524 usb_serial_deregister (&garmin_device);
1530 module_init(garmin_init);
1531 module_exit(garmin_exit);
1533 MODULE_AUTHOR( DRIVER_AUTHOR );
1534 MODULE_DESCRIPTION( DRIVER_DESC );
1535 MODULE_LICENSE("GPL");
1537 module_param(debug, bool, S_IWUSR | S_IRUGO);
1538 MODULE_PARM_DESC(debug, "Debug enabled or not");
1539 module_param(initial_mode, int, S_IRUGO);
1540 MODULE_PARM_DESC(initial_mode, "Initial mode");