4 * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de
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 <linux/uaccess.h>
37 #include <linux/atomic.h>
38 #include <linux/usb.h>
39 #include <linux/usb/serial.h>
41 /* the mode to be set when the port ist opened */
42 static int initial_mode = 1;
47 #define GARMIN_VENDOR_ID 0x091E
53 #define VERSION_MAJOR 0
54 #define VERSION_MINOR 36
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 /* the real size of the data array, always > 0 */
127 /* 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_BULK_IN_RESTART 0x0010
169 #define FLAGS_THROTTLED 0x0008
170 #define APP_REQ_SEEN 0x0004
171 #define APP_RESP_SEEN 0x0002
172 #define CLEAR_HALT_REQUIRED 0x0001
174 #define FLAGS_QUEUING 0x0100
175 #define FLAGS_DROP_DATA 0x0800
177 #define FLAGS_GSP_SKIP 0x1000
178 #define FLAGS_GSP_DLESEEN 0x2000
185 /* function prototypes */
186 static int 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,
191 /* some special packets to be send or received */
192 static unsigned char const GARMIN_START_SESSION_REQ[]
193 = { 0, 0, 0, 0, 5, 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_BULK_IN_AVAIL_REPLY[]
197 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
198 static unsigned char const GARMIN_APP_LAYER_REPLY[]
200 static unsigned char const GARMIN_START_PVT_REQ[]
201 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
202 static unsigned char const GARMIN_STOP_PVT_REQ[]
203 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
204 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
205 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
206 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
207 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
208 static unsigned char const PRIVATE_REQ[]
209 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
213 static const struct usb_device_id id_table[] = {
214 /* the same device id seems to be used by all
215 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 .disconnect = usb_serial_disconnect,
225 .id_table = id_table,
229 static inline int getLayerId(const __u8 *usbPacket)
231 return __le32_to_cpup((__le32 *)(usbPacket));
234 static inline int getPacketId(const __u8 *usbPacket)
236 return __le32_to_cpup((__le32 *)(usbPacket+4));
239 static inline int getDataLength(const __u8 *usbPacket)
241 return __le32_to_cpup((__le32 *)(usbPacket+8));
246 * check if the usb-packet in buf contains an abort-transfer command.
247 * (if yes, all queued data will be dropped)
249 static inline int isAbortTrfCmnd(const unsigned char *buf)
251 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
252 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
253 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
254 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
262 static void send_to_tty(struct usb_serial_port *port,
263 char *data, unsigned int actual_length)
265 struct tty_struct *tty = tty_port_tty_get(&port->port);
267 if (tty && actual_length) {
269 usb_serial_debug_data(debug, &port->dev,
270 __func__, actual_length, data);
272 tty_insert_flip_string(tty, data, actual_length);
273 tty_flip_buffer_push(tty);
279 /******************************************************************************
280 * packet queue handling
281 ******************************************************************************/
284 * queue a received (usb-)packet for later processing
286 static int pkt_add(struct garmin_data *garmin_data_p,
287 unsigned char *data, unsigned int data_length)
292 struct garmin_packet *pkt;
294 /* process only packets containg data ... */
296 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
299 dev_err(&garmin_data_p->port->dev, "out of memory\n");
302 pkt->size = data_length;
303 memcpy(pkt->data, data, data_length);
305 spin_lock_irqsave(&garmin_data_p->lock, flags);
306 garmin_data_p->flags |= FLAGS_QUEUING;
307 result = list_empty(&garmin_data_p->pktlist);
308 pkt->seq = garmin_data_p->seq_counter++;
309 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
310 state = garmin_data_p->state;
311 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
313 dbg("%s - added: pkt: %d - %d bytes",
314 __func__, pkt->seq, data_length);
316 /* in serial mode, if someone is waiting for data from
317 the device, convert and send the next packet to tty. */
318 if (result && (state == STATE_GSP_WAIT_DATA))
319 gsp_next_packet(garmin_data_p);
325 /* get the next pending packet */
326 static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
329 struct garmin_packet *result = NULL;
331 spin_lock_irqsave(&garmin_data_p->lock, flags);
332 if (!list_empty(&garmin_data_p->pktlist)) {
333 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
334 list_del(&result->list);
336 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
341 /* free up all queued data */
342 static void pkt_clear(struct garmin_data *garmin_data_p)
345 struct garmin_packet *result = NULL;
347 spin_lock_irqsave(&garmin_data_p->lock, flags);
348 while (!list_empty(&garmin_data_p->pktlist)) {
349 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
350 list_del(&result->list);
353 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
357 /******************************************************************************
358 * garmin serial protocol handling handling
359 ******************************************************************************/
361 /* send an ack packet back to the tty */
362 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
369 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
385 *ptr++ = 0xFF & (-cksum);
391 send_to_tty(garmin_data_p->port, pkt, l);
398 * called for a complete packet received from tty layer
400 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
401 * at GSP_INITIAL_OFFSET.
403 * count - number of bytes in the input buffer including space reserved for
404 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
405 * (including pkt-id, data-length a. cksum)
407 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
410 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
411 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
415 int pktid = recpkt[0];
416 int size = recpkt[1];
418 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
419 __func__, count-GSP_INITIAL_OFFSET, recpkt);
421 if (size != (count-GSP_INITIAL_OFFSET-3)) {
422 dbg("%s - invalid size, expected %d bytes, got %d",
423 __func__, size, (count-GSP_INITIAL_OFFSET-3));
430 /* sanity check, remove after test ... */
431 if ((__u8 *)&(usbdata[3]) != recpkt) {
432 dbg("%s - ptr mismatch %p - %p",
433 __func__, &(usbdata[4]), recpkt);
442 if ((0xff & (cksum + *recpkt)) != 0) {
443 dbg("%s - invalid checksum, expected %02x, got %02x",
444 __func__, 0xff & -cksum, 0xff & *recpkt);
448 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
449 usbdata[1] = __cpu_to_le32(pktid);
450 usbdata[2] = __cpu_to_le32(size);
452 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
453 GARMIN_PKTHDR_LENGTH+size, 0);
455 /* if this was an abort-transfer command, flush all
457 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
458 spin_lock_irqsave(&garmin_data_p->lock, flags);
459 garmin_data_p->flags |= FLAGS_DROP_DATA;
460 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
461 pkt_clear(garmin_data_p);
470 * Called for data received from tty
472 * buf contains the data read, it may span more than one packet or even
475 * input record should be a serial-record, but it may not be complete.
476 * Copy it into our local buffer, until an etx is seen (or an error
478 * Once the record is complete, convert into a usb packet and send it
479 * to the bulk pipe, send an ack back to the tty.
481 * If the input is an ack, just send the last queued packet to the
484 * if the input is an abort command, drop all queued data.
487 static int gsp_receive(struct garmin_data *garmin_data_p,
488 const unsigned char *buf, int count)
492 int ack_or_nak_seen = 0;
495 /* dleSeen: set if last byte read was a DLE */
497 /* skip: if set, skip incoming data until possible start of
503 spin_lock_irqsave(&garmin_data_p->lock, flags);
504 dest = garmin_data_p->inbuffer;
505 size = garmin_data_p->insize;
506 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
507 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
508 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
510 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
511 __func__, dleSeen, skip, size, count); */
514 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 - id=0x%X.",
547 gsp_rec_packet(garmin_data_p, size);
551 size = GSP_INITIAL_OFFSET;
559 size = GSP_INITIAL_OFFSET;
566 if (size >= GPS_IN_BUFSIZ) {
567 dbg("%s - packet too large.", __func__);
569 size = GSP_INITIAL_OFFSET;
574 spin_lock_irqsave(&garmin_data_p->lock, flags);
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 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
591 if (ack_or_nak_seen) {
592 if (gsp_next_packet(garmin_data_p) > 0)
593 garmin_data_p->state = STATE_ACTIVE;
595 garmin_data_p->state = STATE_GSP_WAIT_DATA;
603 * Sends a usb packet to the tty
605 * Assumes, that all packages and at an usb-packet boundary.
607 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
609 static int gsp_send(struct garmin_data *garmin_data_p,
610 const unsigned char *buf, int count)
612 const unsigned char *src;
620 dbg("%s - state %d - %d bytes.", __func__,
621 garmin_data_p->state, count);
623 k = garmin_data_p->outsize;
624 if ((k+count) > GPS_OUT_BUFSIZ) {
625 dbg("packet too large");
626 garmin_data_p->outsize = 0;
630 memcpy(garmin_data_p->outbuffer+k, buf, count);
632 garmin_data_p->outsize = k;
634 if (k >= GARMIN_PKTHDR_LENGTH) {
635 pktid = getPacketId(garmin_data_p->outbuffer);
636 datalen = getDataLength(garmin_data_p->outbuffer);
637 i = GARMIN_PKTHDR_LENGTH + datalen;
644 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
646 /* garmin_data_p->outbuffer now contains a complete packet */
648 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
649 __func__, k, garmin_data_p->outbuffer);
651 garmin_data_p->outsize = 0;
653 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
654 dbg("not an application packet (%d)",
655 getLayerId(garmin_data_p->outbuffer));
660 dbg("packet-id %d too large", pktid);
665 dbg("packet-size %d too large", datalen);
669 /* the serial protocol should be able to handle this packet */
672 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
673 for (i = 0; i < datalen; i++) {
678 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
679 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
680 /* can't add stuffing DLEs in place, move data to end
682 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
683 memcpy(dst, src, datalen);
687 dst = garmin_data_p->outbuffer;
697 for (i = 0; i < datalen; i++) {
705 cksum = 0xFF & -cksum;
712 i = dst-garmin_data_p->outbuffer;
714 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
716 garmin_data_p->pkt_id = pktid;
717 garmin_data_p->state = STATE_WAIT_TTY_ACK;
724 * Process the next pending data packet - if there is one
726 static int 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", __func__, pkt->seq);
733 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
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)
767 while (offs < count) {
768 /* if buffer contains header, copy rest of data */
769 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
770 len = GARMIN_PKTHDR_LENGTH
771 +getDataLength(garmin_data_p->inbuffer);
773 len = GARMIN_PKTHDR_LENGTH;
775 if (len >= GPS_IN_BUFSIZ) {
776 /* seems to be an invalid packet, ignore rest
778 dbg("%s - packet size too large: %d", __func__, len);
779 garmin_data_p->insize = 0;
783 len -= garmin_data_p->insize;
784 if (len > (count-offs))
787 dest = garmin_data_p->inbuffer
788 + garmin_data_p->insize;
789 memcpy(dest, buf+offs, len);
790 garmin_data_p->insize += len;
795 /* do we have a complete packet ? */
796 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
797 len = GARMIN_PKTHDR_LENGTH+
798 getDataLength(garmin_data_p->inbuffer);
799 if (garmin_data_p->insize >= len) {
800 garmin_write_bulk(garmin_data_p->port,
801 garmin_data_p->inbuffer,
803 garmin_data_p->insize = 0;
805 /* if this was an abort-transfer command,
806 flush all queued data. */
807 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
808 spin_lock_irqsave(&garmin_data_p->lock,
810 garmin_data_p->flags |= FLAGS_DROP_DATA;
811 spin_unlock_irqrestore(
812 &garmin_data_p->lock, flags);
813 pkt_clear(garmin_data_p);
822 /******************************************************************************
824 ******************************************************************************/
826 static void priv_status_resp(struct usb_serial_port *port)
828 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
829 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
831 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
832 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
833 pkt[2] = __cpu_to_le32(12);
834 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
835 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
836 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
838 send_to_tty(port, (__u8 *)pkt, 6 * 4);
842 /******************************************************************************
843 * Garmin specific driver functions
844 ******************************************************************************/
846 static int process_resetdev_request(struct usb_serial_port *port)
850 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
852 spin_lock_irqsave(&garmin_data_p->lock, flags);
853 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
854 garmin_data_p->state = STATE_RESET;
855 garmin_data_p->serial_num = 0;
856 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
858 usb_kill_urb(port->interrupt_in_urb);
859 dbg("%s - usb_reset_device", __func__);
860 status = usb_reset_device(port->serial->dev);
862 dbg("%s - usb_reset_device failed: %d",
870 * clear all cached data
872 static int garmin_clear(struct garmin_data *garmin_data_p)
877 /* flush all queued data */
878 pkt_clear(garmin_data_p);
880 spin_lock_irqsave(&garmin_data_p->lock, flags);
881 garmin_data_p->insize = 0;
882 garmin_data_p->outsize = 0;
883 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
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);
897 usb_kill_urb(port->interrupt_in_urb);
899 dbg("%s - adding interrupt input", __func__);
900 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
902 dev_err(&serial->dev->dev,
903 "%s - failed submitting interrupt urb, error %d\n",
908 * using the initialization method from gpsbabel. See comments in
909 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
912 dbg("%s - starting session ...", __func__);
913 garmin_data_p->state = STATE_ACTIVE;
915 for (i = 0; i < 3; i++) {
916 status = garmin_write_bulk(port,
917 GARMIN_START_SESSION_REQ,
918 sizeof(GARMIN_START_SESSION_REQ), 0);
933 static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
937 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
939 spin_lock_irqsave(&garmin_data_p->lock, flags);
940 garmin_data_p->mode = initial_mode;
941 garmin_data_p->count = 0;
942 garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
943 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
945 /* shutdown any bulk reads that might be going on */
946 usb_kill_urb(port->write_urb);
947 usb_kill_urb(port->read_urb);
949 if (garmin_data_p->state == STATE_RESET)
950 status = garmin_init_session(port);
952 garmin_data_p->state = STATE_ACTIVE;
957 static void garmin_close(struct usb_serial_port *port)
959 struct usb_serial *serial = port->serial;
960 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
962 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
963 port->number, garmin_data_p->mode,
964 garmin_data_p->state, garmin_data_p->flags);
969 mutex_lock(&port->serial->disc_mutex);
971 if (!port->serial->disconnected)
972 garmin_clear(garmin_data_p);
974 /* shutdown our urbs */
975 usb_kill_urb(port->read_urb);
976 usb_kill_urb(port->write_urb);
978 /* keep reset state so we know that we must start a new session */
979 if (garmin_data_p->state != STATE_RESET)
980 garmin_data_p->state = STATE_DISCONNECTED;
982 mutex_unlock(&port->serial->disc_mutex);
986 static void garmin_write_bulk_callback(struct urb *urb)
988 struct usb_serial_port *port = urb->context;
991 struct garmin_data *garmin_data_p =
992 usb_get_serial_port_data(port);
994 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
996 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
997 gsp_send_ack(garmin_data_p,
998 ((__u8 *)urb->transfer_buffer)[4]);
1001 usb_serial_port_softint(port);
1004 /* Ignore errors that resulted from garmin_write_bulk with
1007 /* free up the transfer buffer, as usb_free_urb() does not do this */
1008 kfree(urb->transfer_buffer);
1012 static int garmin_write_bulk(struct usb_serial_port *port,
1013 const unsigned char *buf, int count,
1016 unsigned long flags;
1017 struct usb_serial *serial = port->serial;
1018 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1020 unsigned char *buffer;
1023 spin_lock_irqsave(&garmin_data_p->lock, flags);
1024 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1025 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1027 buffer = kmalloc(count, GFP_ATOMIC);
1029 dev_err(&port->dev, "out of memory\n");
1033 urb = usb_alloc_urb(0, GFP_ATOMIC);
1035 dev_err(&port->dev, "no more free urbs\n");
1040 memcpy(buffer, buf, count);
1042 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
1044 usb_fill_bulk_urb(urb, serial->dev,
1045 usb_sndbulkpipe(serial->dev,
1046 port->bulk_out_endpointAddress),
1048 garmin_write_bulk_callback,
1049 dismiss_ack ? NULL : port);
1050 urb->transfer_flags |= URB_ZERO_PACKET;
1052 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1054 spin_lock_irqsave(&garmin_data_p->lock, flags);
1055 garmin_data_p->flags |= APP_REQ_SEEN;
1056 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
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) failed with status = %d\n",
1073 /* we are done with this urb, so let the host driver
1074 * really free it when it is finished with it */
1080 static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1081 const unsigned char *buf, int count)
1083 int pktid, pktsiz, len;
1084 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1085 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1087 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
1089 if (garmin_data_p->state == STATE_RESET)
1092 /* check for our private packets */
1093 if (count >= GARMIN_PKTHDR_LENGTH) {
1098 memcpy(garmin_data_p->privpkt, buf, len);
1100 pktsiz = getDataLength(garmin_data_p->privpkt);
1101 pktid = getPacketId(garmin_data_p->privpkt);
1103 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1104 && GARMIN_LAYERID_PRIVATE ==
1105 getLayerId(garmin_data_p->privpkt)) {
1107 dbg("%s - processing private request %d",
1110 /* drop all unfinished transfers */
1111 garmin_clear(garmin_data_p);
1115 case PRIV_PKTID_SET_DEBUG:
1118 debug = __le32_to_cpu(privpkt[3]);
1119 dbg("%s - debug level set to 0x%X",
1123 case PRIV_PKTID_SET_MODE:
1126 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1127 dbg("%s - mode set to %d",
1128 __func__, garmin_data_p->mode);
1131 case PRIV_PKTID_INFO_REQ:
1132 priv_status_resp(port);
1135 case PRIV_PKTID_RESET_REQ:
1136 process_resetdev_request(port);
1139 case PRIV_PKTID_SET_DEF_MODE:
1142 initial_mode = __le32_to_cpu(privpkt[3]);
1143 dbg("%s - initial_mode set to %d",
1145 garmin_data_p->mode);
1152 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1153 return gsp_receive(garmin_data_p, buf, count);
1154 } else { /* MODE_NATIVE */
1155 return nat_receive(garmin_data_p, buf, count);
1160 static int garmin_write_room(struct tty_struct *tty)
1162 struct usb_serial_port *port = tty->driver_data;
1164 * Report back the bytes currently available in the output buffer.
1166 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1167 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1171 static void garmin_read_process(struct garmin_data *garmin_data_p,
1172 unsigned char *data, unsigned data_length,
1175 unsigned long flags;
1177 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1178 /* abort-transfer cmd is actice */
1179 dbg("%s - pkt dropped", __func__);
1180 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1181 garmin_data_p->state != STATE_RESET) {
1183 /* if throttling is active or postprecessing is required
1184 put the received data in the input queue, otherwise
1185 send it directly to the tty port */
1186 if (garmin_data_p->flags & FLAGS_QUEUING) {
1187 pkt_add(garmin_data_p, data, data_length);
1188 } else if (bulk_data ||
1189 getLayerId(data) == GARMIN_LAYERID_APPL) {
1191 spin_lock_irqsave(&garmin_data_p->lock, flags);
1192 garmin_data_p->flags |= APP_RESP_SEEN;
1193 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1195 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1196 pkt_add(garmin_data_p, data, data_length);
1198 send_to_tty(garmin_data_p->port, data,
1202 /* ignore system layer packets ... */
1207 static void garmin_read_bulk_callback(struct urb *urb)
1209 unsigned long flags;
1210 struct usb_serial_port *port = urb->context;
1211 struct usb_serial *serial = port->serial;
1212 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1213 unsigned char *data = urb->transfer_buffer;
1214 int status = urb->status;
1218 dbg("%s - bad serial pointer, exiting", __func__);
1223 dbg("%s - nonzero read bulk status received: %d",
1228 usb_serial_debug_data(debug, &port->dev,
1229 __func__, urb->actual_length, data);
1231 garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
1233 if (urb->actual_length == 0 &&
1234 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1235 spin_lock_irqsave(&garmin_data_p->lock, flags);
1236 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1237 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1238 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1241 "%s - failed resubmitting read urb, error %d\n",
1243 } else if (urb->actual_length > 0) {
1244 /* Continue trying to read until nothing more is received */
1245 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1246 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1249 "%s - failed resubmitting read urb, "
1250 "error %d\n", __func__, retval);
1253 dbg("%s - end of bulk data", __func__);
1254 spin_lock_irqsave(&garmin_data_p->lock, flags);
1255 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1256 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1261 static void garmin_read_int_callback(struct urb *urb)
1263 unsigned long flags;
1265 struct usb_serial_port *port = urb->context;
1266 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1267 unsigned char *data = urb->transfer_buffer;
1268 int status = urb->status;
1277 /* this urb is terminated, clean up */
1278 dbg("%s - urb shutting down with status: %d",
1282 dbg("%s - nonzero urb status received: %d",
1287 usb_serial_debug_data(debug, &port->dev, __func__,
1288 urb->actual_length, urb->transfer_buffer);
1290 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1291 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1292 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1294 dbg("%s - bulk data available.", __func__);
1296 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1298 /* bulk data available */
1299 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1302 "%s - failed submitting read urb, error %d\n",
1305 spin_lock_irqsave(&garmin_data_p->lock, flags);
1306 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1307 spin_unlock_irqrestore(&garmin_data_p->lock,
1311 /* bulk-in transfer still active */
1312 spin_lock_irqsave(&garmin_data_p->lock, flags);
1313 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1314 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1317 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1318 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1319 sizeof(GARMIN_START_SESSION_REPLY))) {
1321 spin_lock_irqsave(&garmin_data_p->lock, flags);
1322 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1323 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1325 /* save the serial number */
1326 garmin_data_p->serial_num = __le32_to_cpup(
1327 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
1329 dbg("%s - start-of-session reply seen - serial %u.",
1330 __func__, garmin_data_p->serial_num);
1333 garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
1335 retval = usb_submit_urb(urb, GFP_ATOMIC);
1337 dev_err(&urb->dev->dev,
1338 "%s - Error %d submitting interrupt urb\n",
1344 * Sends the next queued packt to the tty port (garmin native mode only)
1345 * and then sets a timer to call itself again until all queued data
1348 static int garmin_flush_queue(struct garmin_data *garmin_data_p)
1350 unsigned long flags;
1351 struct garmin_packet *pkt;
1353 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1354 pkt = pkt_pop(garmin_data_p);
1356 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1358 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1361 spin_lock_irqsave(&garmin_data_p->lock, flags);
1362 garmin_data_p->flags &= ~FLAGS_QUEUING;
1363 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1370 static void garmin_throttle(struct tty_struct *tty)
1372 struct usb_serial_port *port = tty->driver_data;
1373 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1375 /* set flag, data received will be put into a queue
1376 for later processing */
1377 spin_lock_irq(&garmin_data_p->lock);
1378 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1379 spin_unlock_irq(&garmin_data_p->lock);
1383 static void garmin_unthrottle(struct tty_struct *tty)
1385 struct usb_serial_port *port = tty->driver_data;
1386 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1389 spin_lock_irq(&garmin_data_p->lock);
1390 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1391 spin_unlock_irq(&garmin_data_p->lock);
1393 /* in native mode send queued data to tty, in
1394 serial mode nothing needs to be done here */
1395 if (garmin_data_p->mode == MODE_NATIVE)
1396 garmin_flush_queue(garmin_data_p);
1398 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1399 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1402 "%s - failed resubmitting read urb, error %d\n",
1408 * The timer is currently only used to send queued packets to
1409 * the tty in cases where the protocol provides no own handshaking
1410 * to initiate the transfer.
1412 static void timeout_handler(unsigned long data)
1414 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1416 /* send the next queued packet to the tty port */
1417 if (garmin_data_p->mode == MODE_NATIVE)
1418 if (garmin_data_p->flags & FLAGS_QUEUING)
1419 garmin_flush_queue(garmin_data_p);
1424 static int garmin_attach(struct usb_serial *serial)
1427 struct usb_serial_port *port = serial->port[0];
1428 struct garmin_data *garmin_data_p = NULL;
1430 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1431 if (garmin_data_p == NULL) {
1432 dev_err(&port->dev, "%s - Out of memory\n", __func__);
1435 init_timer(&garmin_data_p->timer);
1436 spin_lock_init(&garmin_data_p->lock);
1437 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1438 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1439 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1440 garmin_data_p->timer.function = timeout_handler;
1441 garmin_data_p->port = port;
1442 garmin_data_p->state = 0;
1443 garmin_data_p->flags = 0;
1444 garmin_data_p->count = 0;
1445 usb_set_serial_port_data(port, garmin_data_p);
1447 status = garmin_init_session(port);
1453 static void garmin_disconnect(struct usb_serial *serial)
1455 struct usb_serial_port *port = serial->port[0];
1456 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1458 usb_kill_urb(port->interrupt_in_urb);
1459 del_timer_sync(&garmin_data_p->timer);
1463 static void garmin_release(struct usb_serial *serial)
1465 struct usb_serial_port *port = serial->port[0];
1466 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1468 kfree(garmin_data_p);
1472 /* All of the device info needed */
1473 static struct usb_serial_driver garmin_device = {
1475 .owner = THIS_MODULE,
1476 .name = "garmin_gps",
1478 .description = "Garmin GPS usb/tty",
1479 .id_table = id_table,
1481 .open = garmin_open,
1482 .close = garmin_close,
1483 .throttle = garmin_throttle,
1484 .unthrottle = garmin_unthrottle,
1485 .attach = garmin_attach,
1486 .disconnect = garmin_disconnect,
1487 .release = garmin_release,
1488 .write = garmin_write,
1489 .write_room = garmin_write_room,
1490 .write_bulk_callback = garmin_write_bulk_callback,
1491 .read_bulk_callback = garmin_read_bulk_callback,
1492 .read_int_callback = garmin_read_int_callback,
1495 static struct usb_serial_driver * const serial_drivers[] = {
1496 &garmin_device, NULL
1499 module_usb_serial_driver(garmin_driver, serial_drivers);
1501 MODULE_AUTHOR(DRIVER_AUTHOR);
1502 MODULE_DESCRIPTION(DRIVER_DESC);
1503 MODULE_LICENSE("GPL");
1505 module_param(debug, bool, S_IWUSR | S_IRUGO);
1506 MODULE_PARM_DESC(debug, "Debug enabled or not");
1507 module_param(initial_mode, int, S_IRUGO);
1508 MODULE_PARM_DESC(initial_mode, "Initial mode");