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/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 <linux/usb.h>
39 /* the mode to be set when the port ist opened */
40 static int initial_mode = 1;
45 #include "usb-serial.h"
47 #define GARMIN_VENDOR_ID 0x091E
53 #define VERSION_MAJOR 0
54 #define VERSION_MINOR 23
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"
62 /* error codes returned by the driver */
63 #define EINVPKT 1000 /* invalid packet structure */
66 // size of the header of a packet using the usb protocol
67 #define GARMIN_PKTHDR_LENGTH 12
69 // max. possible size of a packet using the serial protocol
70 #define MAX_SERIAL_PKT_SIZ (3+255+3)
72 // max. possible size of a packet with worst case stuffing
73 #define MAX_SERIAL_PKT_SIZ_STUFFED MAX_SERIAL_PKT_SIZ+256
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)
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)
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
89 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
91 // max. size of incoming private packets (header+1 param)
92 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
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
99 #define GARMIN_PKTID_PVT_DATA 51
100 #define GARMIN_PKTID_L001_COMMAND_DATA 10
102 #define CMND_ABORT_TRANSFER 0
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
118 /* structure used to queue incoming packets */
119 struct garmin_packet {
120 struct list_head list;
122 int size; // the real size of the data array, always > 0
126 /* structure used to keep the current state of the driver */
135 struct timer_list timer;
136 struct usb_serial_port *port;
140 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
141 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
144 struct list_head pktlist;
149 #define STATE_INITIAL_DELAY 1
150 #define STATE_TIMEOUT 2
151 #define STATE_SESSION_REQ1 3
152 #define STATE_SESSION_REQ2 4
153 #define STATE_ACTIVE 5
155 #define STATE_RESET 8
156 #define STATE_DISCONNECTED 9
157 #define STATE_WAIT_TTY_ACK 10
158 #define STATE_GSP_WAIT_DATA 11
160 #define MODE_NATIVE 0
161 #define MODE_GARMIN_SERIAL 1
163 // Flags used in garmin_data.flags:
164 #define FLAGS_SESSION_REPLY_MASK 0x00C0
165 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
166 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
167 #define FLAGS_BULK_IN_ACTIVE 0x0020
168 #define FLAGS_THROTTLED 0x0010
169 #define CLEAR_HALT_REQUIRED 0x0001
171 #define FLAGS_QUEUING 0x0100
172 #define FLAGS_APP_RESP_SEEN 0x0200
173 #define FLAGS_APP_REQ_SEEN 0x0400
174 #define FLAGS_DROP_DATA 0x0800
176 #define FLAGS_GSP_SKIP 0x1000
177 #define FLAGS_GSP_DLESEEN 0x2000
184 /* function prototypes */
185 static void gsp_next_packet(struct garmin_data * garmin_data_p);
186 static int garmin_write_bulk(struct usb_serial_port *port,
187 const unsigned char *buf, int count);
189 /* some special packets to be send or received */
190 static unsigned char const GARMIN_START_SESSION_REQ[]
191 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
192 static unsigned char const GARMIN_START_SESSION_REQ2[]
193 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
194 static unsigned char const GARMIN_START_SESSION_REPLY[]
195 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
196 static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
197 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
198 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
199 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
200 static unsigned char const GARMIN_APP_LAYER_REPLY[]
202 static unsigned char const GARMIN_START_PVT_REQ[]
203 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
204 static unsigned char const GARMIN_STOP_PVT_REQ[]
205 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
206 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
207 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
208 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
209 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
210 static unsigned char const PRIVATE_REQ[]
211 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
215 static struct usb_device_id id_table [] = {
216 /* the same device id seems to be used by all usb enabled gps devices */
217 { USB_DEVICE(GARMIN_VENDOR_ID, 3 ) },
218 { } /* Terminating entry */
221 MODULE_DEVICE_TABLE (usb, id_table);
223 static struct usb_driver garmin_driver = {
224 .name = "garmin_gps",
225 .probe = usb_serial_probe,
226 .disconnect = usb_serial_disconnect,
227 .id_table = id_table,
232 static inline int noResponseFromAppLayer(struct garmin_data * garmin_data_p)
234 return ((garmin_data_p->flags
235 & (FLAGS_APP_REQ_SEEN|FLAGS_APP_RESP_SEEN))
236 == FLAGS_APP_REQ_SEEN);
240 static inline int getLayerId(const __u8 *usbPacket)
242 return __le32_to_cpup((__le32 *)(usbPacket));
245 static inline int getPacketId(const __u8 *usbPacket)
247 return __le32_to_cpup((__le32 *)(usbPacket+4));
250 static inline int getDataLength(const __u8 *usbPacket)
252 return __le32_to_cpup((__le32 *)(usbPacket+8));
257 * check if the usb-packet in buf contains an abort-transfer command.
258 * (if yes, all queued data will be dropped)
260 static inline int isAbortTrfCmnd(const unsigned char *buf)
262 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
263 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
264 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
265 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
273 static void send_to_tty(struct usb_serial_port *port,
274 char *data, unsigned int actual_length)
276 struct tty_struct *tty = port->tty;
278 if (tty && actual_length) {
280 usb_serial_debug_data(debug, &port->dev,
281 __FUNCTION__, actual_length, data);
283 tty_buffer_request_room(tty, actual_length);
284 tty_insert_flip_string(tty, data, actual_length);
285 tty_flip_buffer_push(tty);
290 /******************************************************************************
291 * packet queue handling
292 ******************************************************************************/
295 * queue a received (usb-)packet for later processing
297 static int pkt_add(struct garmin_data * garmin_data_p,
298 unsigned char *data, unsigned int data_length)
302 struct garmin_packet *pkt;
304 /* process only packets containg data ... */
306 garmin_data_p->flags |= FLAGS_QUEUING;
307 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
310 dev_err(&garmin_data_p->port->dev, "out of memory\n");
313 pkt->size = data_length;
314 memcpy(pkt->data, data, data_length);
316 spin_lock_irqsave(&garmin_data_p->lock, flags);
317 result = list_empty(&garmin_data_p->pktlist);
318 pkt->seq = garmin_data_p->seq_counter++;
319 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
320 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
322 /* in serial mode, if someone is waiting for data from
323 the device, iconvert and send the next packet to tty. */
324 if (result && (garmin_data_p->state == STATE_GSP_WAIT_DATA)) {
325 gsp_next_packet(garmin_data_p);
332 /* get the next pending packet */
333 static struct garmin_packet *pkt_pop(struct garmin_data * garmin_data_p)
336 struct garmin_packet *result = NULL;
338 spin_lock_irqsave(&garmin_data_p->lock, flags);
339 if (!list_empty(&garmin_data_p->pktlist)) {
340 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
341 list_del(&result->list);
343 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
348 /* free up all queued data */
349 static void pkt_clear(struct garmin_data * garmin_data_p)
352 struct garmin_packet *result = NULL;
354 dbg("%s", __FUNCTION__);
356 spin_lock_irqsave(&garmin_data_p->lock, flags);
357 while (!list_empty(&garmin_data_p->pktlist)) {
358 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
359 list_del(&result->list);
362 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
366 /******************************************************************************
367 * garmin serial protocol handling handling
368 ******************************************************************************/
370 /* send an ack packet back to the tty */
371 static int gsp_send_ack(struct garmin_data * garmin_data_p, __u8 pkt_id)
378 dbg("%s - pkt-id: 0x%X.", __FUNCTION__, 0xFF & pkt_id);
395 *ptr++ = 0xFF & (-cksum);
401 send_to_tty(garmin_data_p->port, pkt, l);
408 * called for a complete packet received from tty layer
410 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
411 * at GSP_INITIAL_OFFSET.
413 * count - number of bytes in the input buffer including space reserved for
414 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
415 * (including pkt-id, data-length a. cksum)
417 static int gsp_rec_packet(struct garmin_data * garmin_data_p, int count)
419 const __u8* recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
420 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
424 int pktid = recpkt[0];
425 int size = recpkt[1];
427 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
428 __FUNCTION__, count-GSP_INITIAL_OFFSET, recpkt);
430 if (size != (count-GSP_INITIAL_OFFSET-3)) {
431 dbg("%s - invalid size, expected %d bytes, got %d",
432 __FUNCTION__, size, (count-GSP_INITIAL_OFFSET-3));
439 // sanity check, remove after test ...
440 if ((__u8*)&(usbdata[3]) != recpkt) {
441 dbg("%s - ptr mismatch %p - %p",
442 __FUNCTION__, &(usbdata[4]), recpkt);
451 if ((0xff & (cksum + *recpkt)) != 0) {
452 dbg("%s - invalid checksum, expected %02x, got %02x",
453 __FUNCTION__, 0xff & -cksum, 0xff & *recpkt);
457 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
458 usbdata[1] = __cpu_to_le32(pktid);
459 usbdata[2] = __cpu_to_le32(size);
461 garmin_write_bulk (garmin_data_p->port, garmin_data_p->inbuffer,
462 GARMIN_PKTHDR_LENGTH+size);
464 /* if this was an abort-transfer command, flush all
466 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
467 garmin_data_p->flags |= FLAGS_DROP_DATA;
468 pkt_clear(garmin_data_p);
477 * Called for data received from tty
479 * buf contains the data read, it may span more than one packet or even
482 * input record should be a serial-record, but it may not be complete.
483 * Copy it into our local buffer, until an etx is seen (or an error
485 * Once the record is complete, convert into a usb packet and send it
486 * to the bulk pipe, send an ack back to the tty.
488 * If the input is an ack, just send the last queued packet to the
491 * if the input is an abort command, drop all queued data.
494 static int gsp_receive(struct garmin_data * garmin_data_p,
495 const unsigned char *buf, int count)
498 int ack_or_nak_seen = 0;
500 __u8 *dest = garmin_data_p->inbuffer;
501 int size = garmin_data_p->insize;
502 // dleSeen: set if last byte read was a DLE
503 int dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
504 // skip: if set, skip incoming data until possible start of
506 int skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
509 dbg("%s - dle=%d skip=%d size=%d count=%d",
510 __FUNCTION__, dleSeen, skip, size, count);
513 size = GSP_INITIAL_OFFSET;
516 while (offs < count) {
522 if (skip) { /* start of a new pkt */
524 size = GSP_INITIAL_OFFSET;
526 } else if (dleSeen) {
532 } else if (data == ETX) {
534 /* packet complete */
536 data = dest[GSP_INITIAL_OFFSET];
539 ack_or_nak_seen = ACK;
540 dbg("ACK packet complete.");
541 } else if (data == NAK) {
542 ack_or_nak_seen = NAK;
543 dbg("NAK packet complete.");
545 dbg("packet complete "
548 gsp_rec_packet(garmin_data_p, size);
552 size = GSP_INITIAL_OFFSET;
560 dbg("non-masked DLE at %d - restarting", i);
561 size = GSP_INITIAL_OFFSET;
568 if (size >= GPS_IN_BUFSIZ) {
569 dbg("%s - packet too large.", __FUNCTION__);
571 size = GSP_INITIAL_OFFSET;
576 garmin_data_p->insize = size;
578 // copy flags back to structure
580 garmin_data_p->flags |= FLAGS_GSP_SKIP;
582 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
585 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
587 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
589 if (ack_or_nak_seen) {
590 garmin_data_p->state = STATE_GSP_WAIT_DATA;
591 gsp_next_packet(garmin_data_p);
601 * Sends a usb packet to the tty
603 * Assumes, that all packages and at an usb-packet boundary.
605 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
607 static int gsp_send(struct garmin_data * garmin_data_p,
608 const unsigned char *buf, int count)
610 const unsigned char *src;
618 dbg("%s - state %d - %d bytes.", __FUNCTION__,
619 garmin_data_p->state, count);
621 k = garmin_data_p->outsize;
622 if ((k+count) > GPS_OUT_BUFSIZ) {
623 dbg("packet too large");
624 garmin_data_p->outsize = 0;
628 memcpy(garmin_data_p->outbuffer+k, buf, count);
630 garmin_data_p->outsize = k;
632 if (k >= GARMIN_PKTHDR_LENGTH) {
633 pktid = getPacketId(garmin_data_p->outbuffer);
634 datalen= getDataLength(garmin_data_p->outbuffer);
635 i = GARMIN_PKTHDR_LENGTH + datalen;
642 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __FUNCTION__,
645 /* garmin_data_p->outbuffer now contains a complete packet */
647 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
648 __FUNCTION__, k, garmin_data_p->outbuffer);
650 garmin_data_p->outsize = 0;
652 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
653 dbg("not an application packet (%d)",
654 getLayerId(garmin_data_p->outbuffer));
659 dbg("packet-id %d too large", pktid);
664 dbg("packet-size %d too large", datalen);
668 /* the serial protocol should be able to handle this packet */
671 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
672 for (i=0; i<datalen; i++) {
677 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
678 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
679 /* can't add stuffing DLEs in place, move data to end
681 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
682 memcpy(dst, src, datalen);
686 dst = garmin_data_p->outbuffer;
696 for (i=0; i<datalen; i++) {
704 cksum = 0xFF & -cksum;
711 i = dst-garmin_data_p->outbuffer;
713 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
715 garmin_data_p->pkt_id = pktid;
716 garmin_data_p->state = STATE_WAIT_TTY_ACK;
726 * Process the next pending data packet - if there is one
728 static void gsp_next_packet(struct garmin_data * garmin_data_p)
730 struct garmin_packet *pkt = NULL;
732 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
733 dbg("%s - next pkt: %d", __FUNCTION__, pkt->seq);
734 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
745 /******************************************************************************
747 ******************************************************************************/
751 * Called for data received from tty
753 * The input data is expected to be in garmin usb-packet format.
755 * buf contains the data read, it may span more than one packet
756 * or even incomplete packets
758 static int nat_receive(struct garmin_data * garmin_data_p,
759 const unsigned char *buf, int count)
766 while (offs < count) {
767 // if buffer contains header, copy rest of data
768 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
769 len = GARMIN_PKTHDR_LENGTH
770 +getDataLength(garmin_data_p->inbuffer);
772 len = GARMIN_PKTHDR_LENGTH;
774 if (len >= GPS_IN_BUFSIZ) {
775 /* seem to be an invalid packet, ignore rest of input */
776 dbg("%s - packet size too large: %d",
778 garmin_data_p->insize = 0;
782 len -= garmin_data_p->insize;
783 if (len > (count-offs))
786 dest = garmin_data_p->inbuffer
787 +garmin_data_p->insize;
788 memcpy(dest, buf+offs, len);
789 garmin_data_p->insize += len;
794 /* do we have a complete packet ? */
795 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
796 len = GARMIN_PKTHDR_LENGTH+
797 getDataLength(garmin_data_p->inbuffer);
798 if (garmin_data_p->insize >= len) {
799 garmin_write_bulk (garmin_data_p->port,
800 garmin_data_p->inbuffer,
802 garmin_data_p->insize = 0;
804 /* if this was an abort-transfer command,
805 flush all queued data. */
806 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
807 garmin_data_p->flags |= FLAGS_DROP_DATA;
808 pkt_clear(garmin_data_p);
817 /******************************************************************************
819 ******************************************************************************/
821 static void priv_status_resp(struct usb_serial_port *port)
823 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
824 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
826 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
827 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
828 pkt[2] = __cpu_to_le32(12);
829 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
830 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
831 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
833 send_to_tty(port, (__u8*)pkt, 6*4);
837 /******************************************************************************
838 * Garmin specific driver functions
839 ******************************************************************************/
841 static int process_resetdev_request(struct usb_serial_port *port)
844 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
846 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
847 garmin_data_p->state = STATE_RESET;
848 garmin_data_p->serial_num = 0;
850 usb_kill_urb (port->interrupt_in_urb);
851 dbg("%s - usb_reset_device", __FUNCTION__ );
852 status = usb_reset_device(port->serial->dev);
854 dbg("%s - usb_reset_device failed: %d",
855 __FUNCTION__, status);
862 * clear all cached data
864 static int garmin_clear(struct garmin_data * garmin_data_p)
868 struct usb_serial_port *port = garmin_data_p->port;
870 if (port != NULL && garmin_data_p->flags & FLAGS_APP_RESP_SEEN) {
871 /* send a terminate command */
872 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
873 sizeof(GARMIN_STOP_TRANSFER_REQ));
876 /* flush all queued data */
877 pkt_clear(garmin_data_p);
879 garmin_data_p->insize = 0;
880 garmin_data_p->outsize = 0;
890 static int garmin_init_session(struct usb_serial_port *port)
892 struct usb_serial *serial = port->serial;
893 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
897 usb_kill_urb (port->interrupt_in_urb);
899 dbg("%s - adding interrupt input", __FUNCTION__);
900 port->interrupt_in_urb->dev = serial->dev;
901 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
903 dev_err(&serial->dev->dev,
904 "%s - failed submitting interrupt urb,"
906 __FUNCTION__, status);
910 dbg("%s - starting session ...", __FUNCTION__);
911 garmin_data_p->state = STATE_ACTIVE;
912 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
913 sizeof(GARMIN_START_SESSION_REQ));
917 garmin_data_p->ignorePkts++;
919 /* not needed, but the win32 driver does it too ... */
920 status = garmin_write_bulk(port,
921 GARMIN_START_SESSION_REQ2,
922 sizeof(GARMIN_START_SESSION_REQ2));
925 garmin_data_p->ignorePkts++;
937 static int garmin_open (struct usb_serial_port *port, struct file *filp)
940 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
942 dbg("%s - port %d", __FUNCTION__, port->number);
945 * Force low_latency on so that our tty_push actually forces the data
946 * through, otherwise it is scheduled, and with high data rates (like
947 * with OHCI) data can get lost.
950 port->tty->low_latency = 1;
952 garmin_data_p->mode = initial_mode;
953 garmin_data_p->count = 0;
954 garmin_data_p->flags = 0;
956 /* shutdown any bulk reads that might be going on */
957 usb_kill_urb (port->write_urb);
958 usb_kill_urb (port->read_urb);
960 if (garmin_data_p->state == STATE_RESET) {
961 status = garmin_init_session(port);
964 garmin_data_p->state = STATE_ACTIVE;
970 static void garmin_close (struct usb_serial_port *port, struct file * filp)
972 struct usb_serial *serial = port->serial;
973 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
975 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __FUNCTION__,
976 port->number, garmin_data_p->mode,
977 garmin_data_p->state, garmin_data_p->flags);
982 garmin_clear(garmin_data_p);
984 /* shutdown our urbs */
985 usb_kill_urb (port->read_urb);
986 usb_kill_urb (port->write_urb);
988 if (noResponseFromAppLayer(garmin_data_p) ||
989 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
990 process_resetdev_request(port);
991 garmin_data_p->state = STATE_RESET;
993 garmin_data_p->state = STATE_DISCONNECTED;
998 static void garmin_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
1000 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1001 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1003 /* free up the transfer buffer, as usb_free_urb() does not do this */
1004 kfree (urb->transfer_buffer);
1006 dbg("%s - port %d", __FUNCTION__, port->number);
1009 dbg("%s - nonzero write bulk status received: %d",
1010 __FUNCTION__, urb->status);
1011 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1014 usb_serial_port_softint(port);
1018 static int garmin_write_bulk (struct usb_serial_port *port,
1019 const unsigned char *buf, int count)
1021 struct usb_serial *serial = port->serial;
1022 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1024 unsigned char *buffer;
1027 dbg("%s - port %d, state %d", __FUNCTION__, port->number,
1028 garmin_data_p->state);
1030 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1032 buffer = kmalloc (count, GFP_ATOMIC);
1034 dev_err(&port->dev, "out of memory\n");
1038 urb = usb_alloc_urb(0, GFP_ATOMIC);
1040 dev_err(&port->dev, "no more free urbs\n");
1045 memcpy (buffer, buf, count);
1047 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
1049 usb_fill_bulk_urb (urb, serial->dev,
1050 usb_sndbulkpipe (serial->dev,
1051 port->bulk_out_endpointAddress),
1053 garmin_write_bulk_callback, port);
1054 urb->transfer_flags |= URB_ZERO_PACKET;
1056 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1057 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1058 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1059 pkt_clear(garmin_data_p);
1060 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1064 /* send it down the pipe */
1065 status = usb_submit_urb(urb, GFP_ATOMIC);
1068 "%s - usb_submit_urb(write bulk) "
1069 "failed with status = %d\n",
1070 __FUNCTION__, status);
1074 if (GARMIN_LAYERID_APPL == getLayerId(buffer)
1075 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1077 gsp_send_ack(garmin_data_p, buffer[4]);
1081 /* we are done with this urb, so let the host driver
1082 * really free it when it is finished with it */
1090 static int garmin_write (struct usb_serial_port *port,
1091 const unsigned char *buf, int count)
1093 int pktid, pktsiz, len;
1094 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1095 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1097 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buf);
1099 /* check for our private packets */
1100 if (count >= GARMIN_PKTHDR_LENGTH) {
1106 memcpy(garmin_data_p->privpkt, buf, len);
1108 pktsiz = getDataLength(garmin_data_p->privpkt);
1109 pktid = getPacketId(garmin_data_p->privpkt);
1111 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1112 && GARMIN_LAYERID_PRIVATE == getLayerId(garmin_data_p->privpkt)) {
1114 dbg("%s - processing private request %d",
1115 __FUNCTION__, pktid);
1117 // drop all unfinished transfers
1118 garmin_clear(garmin_data_p);
1122 case PRIV_PKTID_SET_DEBUG:
1125 debug = __le32_to_cpu(privpkt[3]);
1126 dbg("%s - debug level set to 0x%X",
1127 __FUNCTION__, debug);
1130 case PRIV_PKTID_SET_MODE:
1133 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1134 dbg("%s - mode set to %d",
1135 __FUNCTION__, garmin_data_p->mode);
1138 case PRIV_PKTID_INFO_REQ:
1139 priv_status_resp(port);
1142 case PRIV_PKTID_RESET_REQ:
1143 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1146 case PRIV_PKTID_SET_DEF_MODE:
1149 initial_mode = __le32_to_cpu(privpkt[3]);
1150 dbg("%s - initial_mode set to %d",
1152 garmin_data_p->mode);
1159 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1160 return gsp_receive(garmin_data_p, buf, count);
1161 } else { /* MODE_NATIVE */
1162 return nat_receive(garmin_data_p, buf, count);
1167 static int garmin_write_room (struct usb_serial_port *port)
1170 * Report back the bytes currently available in the output buffer.
1172 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1173 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1177 static int garmin_chars_in_buffer (struct usb_serial_port *port)
1180 * Report back the number of bytes currently in our input buffer.
1181 * Will this lock up the driver - the buffer contains an incomplete
1182 * package which will not be written to the device until it
1183 * has been completed ?
1185 //struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1186 //return garmin_data_p->insize;
1191 static void garmin_read_process(struct garmin_data * garmin_data_p,
1192 unsigned char *data, unsigned data_length)
1194 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1195 /* abort-transfer cmd is actice */
1196 dbg("%s - pkt dropped", __FUNCTION__);
1197 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1198 garmin_data_p->state != STATE_RESET ) {
1200 /* remember any appl.layer packets, so we know
1201 if a reset is required or not when closing
1203 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1204 sizeof(GARMIN_APP_LAYER_REPLY)))
1205 garmin_data_p->flags |= FLAGS_APP_RESP_SEEN;
1207 /* if throttling is active or postprecessing is required
1208 put the received data in th input queue, otherwise
1209 send it directly to the tty port */
1210 if (garmin_data_p->flags & FLAGS_QUEUING) {
1211 pkt_add(garmin_data_p, data, data_length);
1212 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1213 if (getLayerId(data) == GARMIN_LAYERID_APPL) {
1214 pkt_add(garmin_data_p, data, data_length);
1217 send_to_tty(garmin_data_p->port, data, data_length);
1223 static void garmin_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
1225 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1226 struct usb_serial *serial = port->serial;
1227 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1228 unsigned char *data = urb->transfer_buffer;
1231 dbg("%s - port %d", __FUNCTION__, port->number);
1234 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1239 dbg("%s - nonzero read bulk status received: %d",
1240 __FUNCTION__, urb->status);
1244 usb_serial_debug_data(debug, &port->dev,
1245 __FUNCTION__, urb->actual_length, data);
1247 garmin_read_process(garmin_data_p, data, urb->actual_length);
1249 /* Continue trying to read until nothing more is received */
1250 if (urb->actual_length > 0) {
1251 usb_fill_bulk_urb (port->read_urb, serial->dev,
1252 usb_rcvbulkpipe (serial->dev,
1253 port->bulk_in_endpointAddress),
1254 port->read_urb->transfer_buffer,
1255 port->read_urb->transfer_buffer_length,
1256 garmin_read_bulk_callback, port);
1257 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1260 "%s - failed resubmitting read urb, error %d\n",
1261 __FUNCTION__, status);
1267 static void garmin_read_int_callback (struct urb *urb, struct pt_regs *regs)
1270 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1271 struct usb_serial *serial = port->serial;
1272 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1273 unsigned char *data = urb->transfer_buffer;
1275 switch (urb->status) {
1282 /* this urb is terminated, clean up */
1283 dbg("%s - urb shutting down with status: %d",
1284 __FUNCTION__, urb->status);
1287 dbg("%s - nonzero urb status received: %d",
1288 __FUNCTION__, urb->status);
1292 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1293 urb->actual_length, urb->transfer_buffer);
1295 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1296 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1297 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1299 dbg("%s - bulk data available.", __FUNCTION__);
1301 /* bulk data available */
1302 usb_fill_bulk_urb (port->read_urb, serial->dev,
1303 usb_rcvbulkpipe (serial->dev,
1304 port->bulk_in_endpointAddress),
1305 port->read_urb->transfer_buffer,
1306 port->read_urb->transfer_buffer_length,
1307 garmin_read_bulk_callback, port);
1308 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1311 "%s - failed submitting read urb, error %d\n",
1312 __FUNCTION__, status);
1315 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1316 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1317 sizeof(GARMIN_START_SESSION_REPLY))) {
1319 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1321 /* save the serial number */
1322 garmin_data_p->serial_num
1323 = __le32_to_cpup((__le32*)(data+GARMIN_PKTHDR_LENGTH));
1325 dbg("%s - start-of-session reply seen - serial %u.",
1326 __FUNCTION__, garmin_data_p->serial_num);
1329 if (garmin_data_p->ignorePkts) {
1330 /* this reply belongs to a request generated by the driver,
1332 dbg("%s - pkt ignored (%d)",
1333 __FUNCTION__, garmin_data_p->ignorePkts);
1334 garmin_data_p->ignorePkts--;
1336 garmin_read_process(garmin_data_p, data, urb->actual_length);
1339 port->interrupt_in_urb->dev = port->serial->dev;
1340 status = usb_submit_urb (urb, GFP_ATOMIC);
1342 dev_err(&urb->dev->dev,
1343 "%s - Error %d submitting interrupt urb\n",
1344 __FUNCTION__, status);
1349 * Sends the next queued packt to the tty port (garmin native mode only)
1350 * and then sets a timer to call itself again until all queued data
1353 static int garmin_flush_queue(struct garmin_data * garmin_data_p)
1355 struct garmin_packet *pkt;
1357 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1358 pkt = pkt_pop(garmin_data_p);
1361 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1363 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1366 garmin_data_p->flags &= ~FLAGS_QUEUING;
1373 static void garmin_throttle (struct usb_serial_port *port)
1375 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1377 dbg("%s - port %d", __FUNCTION__, port->number);
1378 /* set flag, data received will be put into a queue
1379 for later processing */
1380 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1384 static void garmin_unthrottle (struct usb_serial_port *port)
1386 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1388 dbg("%s - port %d", __FUNCTION__, port->number);
1389 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1391 /* in native mode send queued data to tty, in
1392 serial mode nothing needs to be done here */
1393 if (garmin_data_p->mode == MODE_NATIVE)
1394 garmin_flush_queue(garmin_data_p);
1400 * The timer is currently only used to send queued packets to
1401 * the tty in cases where the protocol provides no own handshaking
1402 * to initiate the transfer.
1404 static void timeout_handler(unsigned long data)
1406 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1408 /* send the next queued packet to the tty port */
1409 if (garmin_data_p->mode == MODE_NATIVE)
1410 if (garmin_data_p->flags & FLAGS_QUEUING)
1411 garmin_flush_queue(garmin_data_p);
1416 static int garmin_attach (struct usb_serial *serial)
1419 struct usb_serial_port *port = serial->port[0];
1420 struct garmin_data * garmin_data_p = NULL;
1422 dbg("%s", __FUNCTION__);
1424 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1425 if (garmin_data_p == NULL) {
1426 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
1429 init_timer(&garmin_data_p->timer);
1430 spin_lock_init(&garmin_data_p->lock);
1431 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1432 //garmin_data_p->timer.expires = jiffies + session_timeout;
1433 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1434 garmin_data_p->timer.function = timeout_handler;
1435 garmin_data_p->port = port;
1436 garmin_data_p->state = 0;
1437 garmin_data_p->count = 0;
1438 usb_set_serial_port_data(port, garmin_data_p);
1440 status = garmin_init_session(port);
1446 static void garmin_shutdown (struct usb_serial *serial)
1448 struct usb_serial_port *port = serial->port[0];
1449 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1451 dbg("%s", __FUNCTION__);
1453 usb_kill_urb (port->interrupt_in_urb);
1454 del_timer_sync(&garmin_data_p->timer);
1455 kfree (garmin_data_p);
1456 usb_set_serial_port_data(port, NULL);
1460 /* All of the device info needed */
1461 static struct usb_serial_driver garmin_device = {
1463 .owner = THIS_MODULE,
1464 .name = "garmin_gps",
1466 .description = "Garmin GPS usb/tty",
1467 .id_table = id_table,
1468 .num_interrupt_in = 1,
1472 .open = garmin_open,
1473 .close = garmin_close,
1474 .throttle = garmin_throttle,
1475 .unthrottle = garmin_unthrottle,
1476 .attach = garmin_attach,
1477 .shutdown = garmin_shutdown,
1478 .write = garmin_write,
1479 .write_room = garmin_write_room,
1480 .chars_in_buffer = garmin_chars_in_buffer,
1481 .write_bulk_callback = garmin_write_bulk_callback,
1482 .read_bulk_callback = garmin_read_bulk_callback,
1483 .read_int_callback = garmin_read_int_callback,
1487 static int __init garmin_init (void)
1491 retval = usb_serial_register(&garmin_device);
1493 goto failed_garmin_register;
1494 retval = usb_register(&garmin_driver);
1496 goto failed_usb_register;
1497 info(DRIVER_DESC " " DRIVER_VERSION);
1500 failed_usb_register:
1501 usb_serial_deregister(&garmin_device);
1502 failed_garmin_register:
1507 static void __exit garmin_exit (void)
1509 usb_deregister (&garmin_driver);
1510 usb_serial_deregister (&garmin_device);
1516 module_init(garmin_init);
1517 module_exit(garmin_exit);
1519 MODULE_AUTHOR( DRIVER_AUTHOR );
1520 MODULE_DESCRIPTION( DRIVER_DESC );
1521 MODULE_LICENSE("GPL");
1523 module_param(debug, bool, S_IWUSR | S_IRUGO);
1524 MODULE_PARM_DESC(debug, "Debug enabled or not");
1525 module_param(initial_mode, int, S_IRUGO);
1526 MODULE_PARM_DESC(initial_mode, "Initial mode");