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>
38 #include <linux/usb/serial.h>
40 /* the mode to be set when the port ist opened */
41 static int initial_mode = 1;
46 #define GARMIN_VENDOR_ID 0x091E
52 #define VERSION_MAJOR 0
53 #define VERSION_MINOR 23
56 #define _DRIVER_VERSION(a,b) "v" _STR(a) "." _STR(b)
57 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
58 #define DRIVER_AUTHOR "hermann kneissel"
59 #define DRIVER_DESC "garmin gps driver"
61 /* error codes returned by the driver */
62 #define EINVPKT 1000 /* invalid packet structure */
65 // size of the header of a packet using the usb protocol
66 #define GARMIN_PKTHDR_LENGTH 12
68 // max. possible size of a packet using the serial protocol
69 #define MAX_SERIAL_PKT_SIZ (3+255+3)
71 // max. possible size of a packet with worst case stuffing
72 #define MAX_SERIAL_PKT_SIZ_STUFFED MAX_SERIAL_PKT_SIZ+256
74 // size of a buffer able to hold a complete (no stuffing) packet
75 // (the document protocol does not contain packets with a larger
76 // size, but in theory a packet may be 64k+12 bytes - if in
77 // later protocol versions larger packet sizes occur, this value
78 // should be increased accordingly, so the input buffer is always
79 // large enough the store a complete packet inclusive header)
80 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
82 // size of a buffer able to hold a complete (incl. stuffing) packet
83 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
85 // where to place the packet id of a serial packet, so we can
86 // prepend the usb-packet header without the need to move the
88 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
90 // max. size of incoming private packets (header+1 param)
91 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
93 #define GARMIN_LAYERID_TRANSPORT 0
94 #define GARMIN_LAYERID_APPL 20
95 // our own layer-id to use for some control mechanisms
96 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
98 #define GARMIN_PKTID_PVT_DATA 51
99 #define GARMIN_PKTID_L001_COMMAND_DATA 10
101 #define CMND_ABORT_TRANSFER 0
103 // packet ids used in private layer
104 #define PRIV_PKTID_SET_DEBUG 1
105 #define PRIV_PKTID_SET_MODE 2
106 #define PRIV_PKTID_INFO_REQ 3
107 #define PRIV_PKTID_INFO_RESP 4
108 #define PRIV_PKTID_RESET_REQ 5
109 #define PRIV_PKTID_SET_DEF_MODE 6
117 /* structure used to queue incoming packets */
118 struct garmin_packet {
119 struct list_head list;
121 int size; // the real size of the data array, always > 0
125 /* structure used to keep the current state of the driver */
134 struct timer_list timer;
135 struct usb_serial_port *port;
139 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
140 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
143 struct list_head pktlist;
148 #define STATE_INITIAL_DELAY 1
149 #define STATE_TIMEOUT 2
150 #define STATE_SESSION_REQ1 3
151 #define STATE_SESSION_REQ2 4
152 #define STATE_ACTIVE 5
154 #define STATE_RESET 8
155 #define STATE_DISCONNECTED 9
156 #define STATE_WAIT_TTY_ACK 10
157 #define STATE_GSP_WAIT_DATA 11
159 #define MODE_NATIVE 0
160 #define MODE_GARMIN_SERIAL 1
162 // Flags used in garmin_data.flags:
163 #define FLAGS_SESSION_REPLY_MASK 0x00C0
164 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
165 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
166 #define FLAGS_BULK_IN_ACTIVE 0x0020
167 #define FLAGS_THROTTLED 0x0010
168 #define CLEAR_HALT_REQUIRED 0x0001
170 #define FLAGS_QUEUING 0x0100
171 #define FLAGS_APP_RESP_SEEN 0x0200
172 #define FLAGS_APP_REQ_SEEN 0x0400
173 #define FLAGS_DROP_DATA 0x0800
175 #define FLAGS_GSP_SKIP 0x1000
176 #define FLAGS_GSP_DLESEEN 0x2000
183 /* function prototypes */
184 static void gsp_next_packet(struct garmin_data * garmin_data_p);
185 static int garmin_write_bulk(struct usb_serial_port *port,
186 const unsigned char *buf, int count);
188 /* some special packets to be send or received */
189 static unsigned char const GARMIN_START_SESSION_REQ[]
190 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
191 static unsigned char const GARMIN_START_SESSION_REQ2[]
192 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
193 static unsigned char const GARMIN_START_SESSION_REPLY[]
194 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
195 static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
196 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
197 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
198 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
199 static unsigned char const GARMIN_APP_LAYER_REPLY[]
201 static unsigned char const GARMIN_START_PVT_REQ[]
202 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
203 static unsigned char const GARMIN_STOP_PVT_REQ[]
204 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
205 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
206 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
207 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
208 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
209 static unsigned char const PRIVATE_REQ[]
210 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
214 static struct usb_device_id id_table [] = {
215 /* the same device id seems to be used by all usb enabled gps devices */
216 { USB_DEVICE(GARMIN_VENDOR_ID, 3 ) },
217 { } /* Terminating entry */
220 MODULE_DEVICE_TABLE (usb, id_table);
222 static struct usb_driver garmin_driver = {
223 .name = "garmin_gps",
224 .probe = usb_serial_probe,
225 .disconnect = usb_serial_disconnect,
226 .id_table = id_table,
231 static inline int noResponseFromAppLayer(struct garmin_data * garmin_data_p)
233 return ((garmin_data_p->flags
234 & (FLAGS_APP_REQ_SEEN|FLAGS_APP_RESP_SEEN))
235 == FLAGS_APP_REQ_SEEN);
239 static inline int getLayerId(const __u8 *usbPacket)
241 return __le32_to_cpup((__le32 *)(usbPacket));
244 static inline int getPacketId(const __u8 *usbPacket)
246 return __le32_to_cpup((__le32 *)(usbPacket+4));
249 static inline int getDataLength(const __u8 *usbPacket)
251 return __le32_to_cpup((__le32 *)(usbPacket+8));
256 * check if the usb-packet in buf contains an abort-transfer command.
257 * (if yes, all queued data will be dropped)
259 static inline int isAbortTrfCmnd(const unsigned char *buf)
261 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
262 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
263 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
264 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
272 static void send_to_tty(struct usb_serial_port *port,
273 char *data, unsigned int actual_length)
275 struct tty_struct *tty = port->tty;
277 if (tty && actual_length) {
279 usb_serial_debug_data(debug, &port->dev,
280 __FUNCTION__, actual_length, data);
282 tty_buffer_request_room(tty, actual_length);
283 tty_insert_flip_string(tty, data, actual_length);
284 tty_flip_buffer_push(tty);
289 /******************************************************************************
290 * packet queue handling
291 ******************************************************************************/
294 * queue a received (usb-)packet for later processing
296 static int pkt_add(struct garmin_data * garmin_data_p,
297 unsigned char *data, unsigned int data_length)
301 struct garmin_packet *pkt;
303 /* process only packets containg data ... */
305 garmin_data_p->flags |= FLAGS_QUEUING;
306 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
309 dev_err(&garmin_data_p->port->dev, "out of memory\n");
312 pkt->size = data_length;
313 memcpy(pkt->data, data, data_length);
315 spin_lock_irqsave(&garmin_data_p->lock, flags);
316 result = list_empty(&garmin_data_p->pktlist);
317 pkt->seq = garmin_data_p->seq_counter++;
318 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
319 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
321 /* in serial mode, if someone is waiting for data from
322 the device, iconvert and send the next packet to tty. */
323 if (result && (garmin_data_p->state == STATE_GSP_WAIT_DATA)) {
324 gsp_next_packet(garmin_data_p);
331 /* get the next pending packet */
332 static struct garmin_packet *pkt_pop(struct garmin_data * garmin_data_p)
335 struct garmin_packet *result = NULL;
337 spin_lock_irqsave(&garmin_data_p->lock, flags);
338 if (!list_empty(&garmin_data_p->pktlist)) {
339 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
340 list_del(&result->list);
342 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
347 /* free up all queued data */
348 static void pkt_clear(struct garmin_data * garmin_data_p)
351 struct garmin_packet *result = NULL;
353 dbg("%s", __FUNCTION__);
355 spin_lock_irqsave(&garmin_data_p->lock, flags);
356 while (!list_empty(&garmin_data_p->pktlist)) {
357 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
358 list_del(&result->list);
361 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
365 /******************************************************************************
366 * garmin serial protocol handling handling
367 ******************************************************************************/
369 /* send an ack packet back to the tty */
370 static int gsp_send_ack(struct garmin_data * garmin_data_p, __u8 pkt_id)
377 dbg("%s - pkt-id: 0x%X.", __FUNCTION__, 0xFF & pkt_id);
394 *ptr++ = 0xFF & (-cksum);
400 send_to_tty(garmin_data_p->port, pkt, l);
407 * called for a complete packet received from tty layer
409 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
410 * at GSP_INITIAL_OFFSET.
412 * count - number of bytes in the input buffer including space reserved for
413 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
414 * (including pkt-id, data-length a. cksum)
416 static int gsp_rec_packet(struct garmin_data * garmin_data_p, int count)
418 const __u8* recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
419 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
423 int pktid = recpkt[0];
424 int size = recpkt[1];
426 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
427 __FUNCTION__, count-GSP_INITIAL_OFFSET, recpkt);
429 if (size != (count-GSP_INITIAL_OFFSET-3)) {
430 dbg("%s - invalid size, expected %d bytes, got %d",
431 __FUNCTION__, size, (count-GSP_INITIAL_OFFSET-3));
438 // sanity check, remove after test ...
439 if ((__u8*)&(usbdata[3]) != recpkt) {
440 dbg("%s - ptr mismatch %p - %p",
441 __FUNCTION__, &(usbdata[4]), recpkt);
450 if ((0xff & (cksum + *recpkt)) != 0) {
451 dbg("%s - invalid checksum, expected %02x, got %02x",
452 __FUNCTION__, 0xff & -cksum, 0xff & *recpkt);
456 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
457 usbdata[1] = __cpu_to_le32(pktid);
458 usbdata[2] = __cpu_to_le32(size);
460 garmin_write_bulk (garmin_data_p->port, garmin_data_p->inbuffer,
461 GARMIN_PKTHDR_LENGTH+size);
463 /* if this was an abort-transfer command, flush all
465 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
466 garmin_data_p->flags |= FLAGS_DROP_DATA;
467 pkt_clear(garmin_data_p);
476 * Called for data received from tty
478 * buf contains the data read, it may span more than one packet or even
481 * input record should be a serial-record, but it may not be complete.
482 * Copy it into our local buffer, until an etx is seen (or an error
484 * Once the record is complete, convert into a usb packet and send it
485 * to the bulk pipe, send an ack back to the tty.
487 * If the input is an ack, just send the last queued packet to the
490 * if the input is an abort command, drop all queued data.
493 static int gsp_receive(struct garmin_data * garmin_data_p,
494 const unsigned char *buf, int count)
497 int ack_or_nak_seen = 0;
499 __u8 *dest = garmin_data_p->inbuffer;
500 int size = garmin_data_p->insize;
501 // dleSeen: set if last byte read was a DLE
502 int dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
503 // skip: if set, skip incoming data until possible start of
505 int skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
508 dbg("%s - dle=%d skip=%d size=%d count=%d",
509 __FUNCTION__, dleSeen, skip, size, count);
512 size = GSP_INITIAL_OFFSET;
515 while (offs < count) {
521 if (skip) { /* start of a new pkt */
523 size = GSP_INITIAL_OFFSET;
525 } else if (dleSeen) {
531 } else if (data == ETX) {
533 /* packet complete */
535 data = dest[GSP_INITIAL_OFFSET];
538 ack_or_nak_seen = ACK;
539 dbg("ACK packet complete.");
540 } else if (data == NAK) {
541 ack_or_nak_seen = NAK;
542 dbg("NAK packet complete.");
544 dbg("packet complete "
547 gsp_rec_packet(garmin_data_p, size);
551 size = GSP_INITIAL_OFFSET;
559 dbg("non-masked DLE at %d - restarting", i);
560 size = GSP_INITIAL_OFFSET;
567 if (size >= GPS_IN_BUFSIZ) {
568 dbg("%s - packet too large.", __FUNCTION__);
570 size = GSP_INITIAL_OFFSET;
575 garmin_data_p->insize = size;
577 // copy flags back to structure
579 garmin_data_p->flags |= FLAGS_GSP_SKIP;
581 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
584 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
586 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
588 if (ack_or_nak_seen) {
589 garmin_data_p->state = STATE_GSP_WAIT_DATA;
590 gsp_next_packet(garmin_data_p);
600 * Sends a usb packet to the tty
602 * Assumes, that all packages and at an usb-packet boundary.
604 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
606 static int gsp_send(struct garmin_data * garmin_data_p,
607 const unsigned char *buf, int count)
609 const unsigned char *src;
617 dbg("%s - state %d - %d bytes.", __FUNCTION__,
618 garmin_data_p->state, count);
620 k = garmin_data_p->outsize;
621 if ((k+count) > GPS_OUT_BUFSIZ) {
622 dbg("packet too large");
623 garmin_data_p->outsize = 0;
627 memcpy(garmin_data_p->outbuffer+k, buf, count);
629 garmin_data_p->outsize = k;
631 if (k >= GARMIN_PKTHDR_LENGTH) {
632 pktid = getPacketId(garmin_data_p->outbuffer);
633 datalen= getDataLength(garmin_data_p->outbuffer);
634 i = GARMIN_PKTHDR_LENGTH + datalen;
641 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __FUNCTION__,
644 /* garmin_data_p->outbuffer now contains a complete packet */
646 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
647 __FUNCTION__, k, garmin_data_p->outbuffer);
649 garmin_data_p->outsize = 0;
651 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
652 dbg("not an application packet (%d)",
653 getLayerId(garmin_data_p->outbuffer));
658 dbg("packet-id %d too large", pktid);
663 dbg("packet-size %d too large", datalen);
667 /* the serial protocol should be able to handle this packet */
670 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
671 for (i=0; i<datalen; i++) {
676 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
677 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
678 /* can't add stuffing DLEs in place, move data to end
680 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
681 memcpy(dst, src, datalen);
685 dst = garmin_data_p->outbuffer;
695 for (i=0; i<datalen; i++) {
703 cksum = 0xFF & -cksum;
710 i = dst-garmin_data_p->outbuffer;
712 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
714 garmin_data_p->pkt_id = pktid;
715 garmin_data_p->state = STATE_WAIT_TTY_ACK;
725 * Process the next pending data packet - if there is one
727 static void gsp_next_packet(struct garmin_data * garmin_data_p)
729 struct garmin_packet *pkt = NULL;
731 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
732 dbg("%s - next pkt: %d", __FUNCTION__, pkt->seq);
733 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
744 /******************************************************************************
746 ******************************************************************************/
750 * Called for data received from tty
752 * The input data is expected to be in garmin usb-packet format.
754 * buf contains the data read, it may span more than one packet
755 * or even incomplete packets
757 static int nat_receive(struct garmin_data * garmin_data_p,
758 const unsigned char *buf, int count)
765 while (offs < count) {
766 // if buffer contains header, copy rest of data
767 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
768 len = GARMIN_PKTHDR_LENGTH
769 +getDataLength(garmin_data_p->inbuffer);
771 len = GARMIN_PKTHDR_LENGTH;
773 if (len >= GPS_IN_BUFSIZ) {
774 /* seem to be an invalid packet, ignore rest of input */
775 dbg("%s - packet size too large: %d",
777 garmin_data_p->insize = 0;
781 len -= garmin_data_p->insize;
782 if (len > (count-offs))
785 dest = garmin_data_p->inbuffer
786 +garmin_data_p->insize;
787 memcpy(dest, buf+offs, len);
788 garmin_data_p->insize += len;
793 /* do we have a complete packet ? */
794 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
795 len = GARMIN_PKTHDR_LENGTH+
796 getDataLength(garmin_data_p->inbuffer);
797 if (garmin_data_p->insize >= len) {
798 garmin_write_bulk (garmin_data_p->port,
799 garmin_data_p->inbuffer,
801 garmin_data_p->insize = 0;
803 /* if this was an abort-transfer command,
804 flush all queued data. */
805 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
806 garmin_data_p->flags |= FLAGS_DROP_DATA;
807 pkt_clear(garmin_data_p);
816 /******************************************************************************
818 ******************************************************************************/
820 static void priv_status_resp(struct usb_serial_port *port)
822 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
823 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
825 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
826 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
827 pkt[2] = __cpu_to_le32(12);
828 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
829 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
830 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
832 send_to_tty(port, (__u8*)pkt, 6*4);
836 /******************************************************************************
837 * Garmin specific driver functions
838 ******************************************************************************/
840 static int process_resetdev_request(struct usb_serial_port *port)
843 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
845 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
846 garmin_data_p->state = STATE_RESET;
847 garmin_data_p->serial_num = 0;
849 usb_kill_urb (port->interrupt_in_urb);
850 dbg("%s - usb_reset_device", __FUNCTION__ );
851 status = usb_reset_device(port->serial->dev);
853 dbg("%s - usb_reset_device failed: %d",
854 __FUNCTION__, status);
861 * clear all cached data
863 static int garmin_clear(struct garmin_data * garmin_data_p)
867 struct usb_serial_port *port = garmin_data_p->port;
869 if (port != NULL && garmin_data_p->flags & FLAGS_APP_RESP_SEEN) {
870 /* send a terminate command */
871 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
872 sizeof(GARMIN_STOP_TRANSFER_REQ));
875 /* flush all queued data */
876 pkt_clear(garmin_data_p);
878 garmin_data_p->insize = 0;
879 garmin_data_p->outsize = 0;
889 static int garmin_init_session(struct usb_serial_port *port)
891 struct usb_serial *serial = port->serial;
892 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
896 usb_kill_urb (port->interrupt_in_urb);
898 dbg("%s - adding interrupt input", __FUNCTION__);
899 port->interrupt_in_urb->dev = serial->dev;
900 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
902 dev_err(&serial->dev->dev,
903 "%s - failed submitting interrupt urb,"
905 __FUNCTION__, status);
909 dbg("%s - starting session ...", __FUNCTION__);
910 garmin_data_p->state = STATE_ACTIVE;
911 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
912 sizeof(GARMIN_START_SESSION_REQ));
916 garmin_data_p->ignorePkts++;
918 /* not needed, but the win32 driver does it too ... */
919 status = garmin_write_bulk(port,
920 GARMIN_START_SESSION_REQ2,
921 sizeof(GARMIN_START_SESSION_REQ2));
924 garmin_data_p->ignorePkts++;
936 static int garmin_open (struct usb_serial_port *port, struct file *filp)
939 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
941 dbg("%s - port %d", __FUNCTION__, port->number);
944 * Force low_latency on so that our tty_push actually forces the data
945 * through, otherwise it is scheduled, and with high data rates (like
946 * with OHCI) data can get lost.
949 port->tty->low_latency = 1;
951 garmin_data_p->mode = initial_mode;
952 garmin_data_p->count = 0;
953 garmin_data_p->flags = 0;
955 /* shutdown any bulk reads that might be going on */
956 usb_kill_urb (port->write_urb);
957 usb_kill_urb (port->read_urb);
959 if (garmin_data_p->state == STATE_RESET) {
960 status = garmin_init_session(port);
963 garmin_data_p->state = STATE_ACTIVE;
969 static void garmin_close (struct usb_serial_port *port, struct file * filp)
971 struct usb_serial *serial = port->serial;
972 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
974 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __FUNCTION__,
975 port->number, garmin_data_p->mode,
976 garmin_data_p->state, garmin_data_p->flags);
981 garmin_clear(garmin_data_p);
983 /* shutdown our urbs */
984 usb_kill_urb (port->read_urb);
985 usb_kill_urb (port->write_urb);
987 if (noResponseFromAppLayer(garmin_data_p) ||
988 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
989 process_resetdev_request(port);
990 garmin_data_p->state = STATE_RESET;
992 garmin_data_p->state = STATE_DISCONNECTED;
997 static void garmin_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
999 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1000 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1002 /* free up the transfer buffer, as usb_free_urb() does not do this */
1003 kfree (urb->transfer_buffer);
1005 dbg("%s - port %d", __FUNCTION__, port->number);
1008 dbg("%s - nonzero write bulk status received: %d",
1009 __FUNCTION__, urb->status);
1010 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1013 usb_serial_port_softint(port);
1017 static int garmin_write_bulk (struct usb_serial_port *port,
1018 const unsigned char *buf, int count)
1020 struct usb_serial *serial = port->serial;
1021 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1023 unsigned char *buffer;
1026 dbg("%s - port %d, state %d", __FUNCTION__, port->number,
1027 garmin_data_p->state);
1029 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1031 buffer = kmalloc (count, GFP_ATOMIC);
1033 dev_err(&port->dev, "out of memory\n");
1037 urb = usb_alloc_urb(0, GFP_ATOMIC);
1039 dev_err(&port->dev, "no more free urbs\n");
1044 memcpy (buffer, buf, count);
1046 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
1048 usb_fill_bulk_urb (urb, serial->dev,
1049 usb_sndbulkpipe (serial->dev,
1050 port->bulk_out_endpointAddress),
1052 garmin_write_bulk_callback, port);
1053 urb->transfer_flags |= URB_ZERO_PACKET;
1055 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1056 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1057 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1058 pkt_clear(garmin_data_p);
1059 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1063 /* send it down the pipe */
1064 status = usb_submit_urb(urb, GFP_ATOMIC);
1067 "%s - usb_submit_urb(write bulk) "
1068 "failed with status = %d\n",
1069 __FUNCTION__, status);
1073 if (GARMIN_LAYERID_APPL == getLayerId(buffer)
1074 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1076 gsp_send_ack(garmin_data_p, buffer[4]);
1080 /* we are done with this urb, so let the host driver
1081 * really free it when it is finished with it */
1089 static int garmin_write (struct usb_serial_port *port,
1090 const unsigned char *buf, int count)
1092 int pktid, pktsiz, len;
1093 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1094 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1096 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buf);
1098 /* check for our private packets */
1099 if (count >= GARMIN_PKTHDR_LENGTH) {
1105 memcpy(garmin_data_p->privpkt, buf, len);
1107 pktsiz = getDataLength(garmin_data_p->privpkt);
1108 pktid = getPacketId(garmin_data_p->privpkt);
1110 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1111 && GARMIN_LAYERID_PRIVATE == getLayerId(garmin_data_p->privpkt)) {
1113 dbg("%s - processing private request %d",
1114 __FUNCTION__, pktid);
1116 // drop all unfinished transfers
1117 garmin_clear(garmin_data_p);
1121 case PRIV_PKTID_SET_DEBUG:
1124 debug = __le32_to_cpu(privpkt[3]);
1125 dbg("%s - debug level set to 0x%X",
1126 __FUNCTION__, debug);
1129 case PRIV_PKTID_SET_MODE:
1132 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1133 dbg("%s - mode set to %d",
1134 __FUNCTION__, garmin_data_p->mode);
1137 case PRIV_PKTID_INFO_REQ:
1138 priv_status_resp(port);
1141 case PRIV_PKTID_RESET_REQ:
1142 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1145 case PRIV_PKTID_SET_DEF_MODE:
1148 initial_mode = __le32_to_cpu(privpkt[3]);
1149 dbg("%s - initial_mode set to %d",
1151 garmin_data_p->mode);
1158 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1159 return gsp_receive(garmin_data_p, buf, count);
1160 } else { /* MODE_NATIVE */
1161 return nat_receive(garmin_data_p, buf, count);
1166 static int garmin_write_room (struct usb_serial_port *port)
1169 * Report back the bytes currently available in the output buffer.
1171 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1172 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1176 static int garmin_chars_in_buffer (struct usb_serial_port *port)
1179 * Report back the number of bytes currently in our input buffer.
1180 * Will this lock up the driver - the buffer contains an incomplete
1181 * package which will not be written to the device until it
1182 * has been completed ?
1184 //struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1185 //return garmin_data_p->insize;
1190 static void garmin_read_process(struct garmin_data * garmin_data_p,
1191 unsigned char *data, unsigned data_length)
1193 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1194 /* abort-transfer cmd is actice */
1195 dbg("%s - pkt dropped", __FUNCTION__);
1196 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1197 garmin_data_p->state != STATE_RESET ) {
1199 /* remember any appl.layer packets, so we know
1200 if a reset is required or not when closing
1202 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1203 sizeof(GARMIN_APP_LAYER_REPLY)))
1204 garmin_data_p->flags |= FLAGS_APP_RESP_SEEN;
1206 /* if throttling is active or postprecessing is required
1207 put the received data in th input queue, otherwise
1208 send it directly to the tty port */
1209 if (garmin_data_p->flags & FLAGS_QUEUING) {
1210 pkt_add(garmin_data_p, data, data_length);
1211 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1212 if (getLayerId(data) == GARMIN_LAYERID_APPL) {
1213 pkt_add(garmin_data_p, data, data_length);
1216 send_to_tty(garmin_data_p->port, data, data_length);
1222 static void garmin_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
1224 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1225 struct usb_serial *serial = port->serial;
1226 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1227 unsigned char *data = urb->transfer_buffer;
1230 dbg("%s - port %d", __FUNCTION__, port->number);
1233 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1238 dbg("%s - nonzero read bulk status received: %d",
1239 __FUNCTION__, urb->status);
1243 usb_serial_debug_data(debug, &port->dev,
1244 __FUNCTION__, urb->actual_length, data);
1246 garmin_read_process(garmin_data_p, data, urb->actual_length);
1248 /* Continue trying to read until nothing more is received */
1249 if (urb->actual_length > 0) {
1250 usb_fill_bulk_urb (port->read_urb, serial->dev,
1251 usb_rcvbulkpipe (serial->dev,
1252 port->bulk_in_endpointAddress),
1253 port->read_urb->transfer_buffer,
1254 port->read_urb->transfer_buffer_length,
1255 garmin_read_bulk_callback, port);
1256 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1259 "%s - failed resubmitting read urb, error %d\n",
1260 __FUNCTION__, status);
1266 static void garmin_read_int_callback (struct urb *urb, struct pt_regs *regs)
1269 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1270 struct usb_serial *serial = port->serial;
1271 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1272 unsigned char *data = urb->transfer_buffer;
1274 switch (urb->status) {
1281 /* this urb is terminated, clean up */
1282 dbg("%s - urb shutting down with status: %d",
1283 __FUNCTION__, urb->status);
1286 dbg("%s - nonzero urb status received: %d",
1287 __FUNCTION__, urb->status);
1291 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1292 urb->actual_length, urb->transfer_buffer);
1294 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1295 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1296 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1298 dbg("%s - bulk data available.", __FUNCTION__);
1300 /* bulk data available */
1301 usb_fill_bulk_urb (port->read_urb, serial->dev,
1302 usb_rcvbulkpipe (serial->dev,
1303 port->bulk_in_endpointAddress),
1304 port->read_urb->transfer_buffer,
1305 port->read_urb->transfer_buffer_length,
1306 garmin_read_bulk_callback, port);
1307 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1310 "%s - failed submitting read urb, error %d\n",
1311 __FUNCTION__, status);
1314 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1315 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1316 sizeof(GARMIN_START_SESSION_REPLY))) {
1318 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1320 /* save the serial number */
1321 garmin_data_p->serial_num
1322 = __le32_to_cpup((__le32*)(data+GARMIN_PKTHDR_LENGTH));
1324 dbg("%s - start-of-session reply seen - serial %u.",
1325 __FUNCTION__, garmin_data_p->serial_num);
1328 if (garmin_data_p->ignorePkts) {
1329 /* this reply belongs to a request generated by the driver,
1331 dbg("%s - pkt ignored (%d)",
1332 __FUNCTION__, garmin_data_p->ignorePkts);
1333 garmin_data_p->ignorePkts--;
1335 garmin_read_process(garmin_data_p, data, urb->actual_length);
1338 port->interrupt_in_urb->dev = port->serial->dev;
1339 status = usb_submit_urb (urb, GFP_ATOMIC);
1341 dev_err(&urb->dev->dev,
1342 "%s - Error %d submitting interrupt urb\n",
1343 __FUNCTION__, status);
1348 * Sends the next queued packt to the tty port (garmin native mode only)
1349 * and then sets a timer to call itself again until all queued data
1352 static int garmin_flush_queue(struct garmin_data * garmin_data_p)
1354 struct garmin_packet *pkt;
1356 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1357 pkt = pkt_pop(garmin_data_p);
1360 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1362 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1365 garmin_data_p->flags &= ~FLAGS_QUEUING;
1372 static void garmin_throttle (struct usb_serial_port *port)
1374 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1376 dbg("%s - port %d", __FUNCTION__, port->number);
1377 /* set flag, data received will be put into a queue
1378 for later processing */
1379 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1383 static void garmin_unthrottle (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 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1390 /* in native mode send queued data to tty, in
1391 serial mode nothing needs to be done here */
1392 if (garmin_data_p->mode == MODE_NATIVE)
1393 garmin_flush_queue(garmin_data_p);
1399 * The timer is currently only used to send queued packets to
1400 * the tty in cases where the protocol provides no own handshaking
1401 * to initiate the transfer.
1403 static void timeout_handler(unsigned long data)
1405 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1407 /* send the next queued packet to the tty port */
1408 if (garmin_data_p->mode == MODE_NATIVE)
1409 if (garmin_data_p->flags & FLAGS_QUEUING)
1410 garmin_flush_queue(garmin_data_p);
1415 static int garmin_attach (struct usb_serial *serial)
1418 struct usb_serial_port *port = serial->port[0];
1419 struct garmin_data * garmin_data_p = NULL;
1421 dbg("%s", __FUNCTION__);
1423 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1424 if (garmin_data_p == NULL) {
1425 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
1428 init_timer(&garmin_data_p->timer);
1429 spin_lock_init(&garmin_data_p->lock);
1430 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1431 //garmin_data_p->timer.expires = jiffies + session_timeout;
1432 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1433 garmin_data_p->timer.function = timeout_handler;
1434 garmin_data_p->port = port;
1435 garmin_data_p->state = 0;
1436 garmin_data_p->count = 0;
1437 usb_set_serial_port_data(port, garmin_data_p);
1439 status = garmin_init_session(port);
1445 static void garmin_shutdown (struct usb_serial *serial)
1447 struct usb_serial_port *port = serial->port[0];
1448 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1450 dbg("%s", __FUNCTION__);
1452 usb_kill_urb (port->interrupt_in_urb);
1453 del_timer_sync(&garmin_data_p->timer);
1454 kfree (garmin_data_p);
1455 usb_set_serial_port_data(port, NULL);
1459 /* All of the device info needed */
1460 static struct usb_serial_driver garmin_device = {
1462 .owner = THIS_MODULE,
1463 .name = "garmin_gps",
1465 .description = "Garmin GPS usb/tty",
1466 .id_table = id_table,
1467 .num_interrupt_in = 1,
1471 .open = garmin_open,
1472 .close = garmin_close,
1473 .throttle = garmin_throttle,
1474 .unthrottle = garmin_unthrottle,
1475 .attach = garmin_attach,
1476 .shutdown = garmin_shutdown,
1477 .write = garmin_write,
1478 .write_room = garmin_write_room,
1479 .chars_in_buffer = garmin_chars_in_buffer,
1480 .write_bulk_callback = garmin_write_bulk_callback,
1481 .read_bulk_callback = garmin_read_bulk_callback,
1482 .read_int_callback = garmin_read_int_callback,
1486 static int __init garmin_init (void)
1490 retval = usb_serial_register(&garmin_device);
1492 goto failed_garmin_register;
1493 retval = usb_register(&garmin_driver);
1495 goto failed_usb_register;
1496 info(DRIVER_DESC " " DRIVER_VERSION);
1499 failed_usb_register:
1500 usb_serial_deregister(&garmin_device);
1501 failed_garmin_register:
1506 static void __exit garmin_exit (void)
1508 usb_deregister (&garmin_driver);
1509 usb_serial_deregister (&garmin_device);
1515 module_init(garmin_init);
1516 module_exit(garmin_exit);
1518 MODULE_AUTHOR( DRIVER_AUTHOR );
1519 MODULE_DESCRIPTION( DRIVER_DESC );
1520 MODULE_LICENSE("GPL");
1522 module_param(debug, bool, S_IWUSR | S_IRUGO);
1523 MODULE_PARM_DESC(debug, "Debug enabled or not");
1524 module_param(initial_mode, int, S_IRUGO);
1525 MODULE_PARM_DESC(initial_mode, "Initial mode");