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;
44 #define GARMIN_VENDOR_ID 0x091E
50 #define VERSION_MAJOR 0
51 #define VERSION_MINOR 36
54 #define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
55 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
56 #define DRIVER_AUTHOR "hermann kneissel"
57 #define DRIVER_DESC "garmin gps driver"
59 /* error codes returned by the driver */
60 #define EINVPKT 1000 /* invalid packet structure */
63 /* size of the header of a packet using the usb protocol */
64 #define GARMIN_PKTHDR_LENGTH 12
66 /* max. possible size of a packet using the serial protocol */
67 #define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
69 /* max. possible size of a packet with worst case stuffing */
70 #define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
72 /* size of a buffer able to hold a complete (no stuffing) packet
73 * (the document protocol does not contain packets with a larger
74 * size, but in theory a packet may be 64k+12 bytes - if in
75 * later protocol versions larger packet sizes occur, this value
76 * should be increased accordingly, so the input buffer is always
77 * large enough the store a complete packet inclusive header) */
78 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
80 /* size of a buffer able to hold a complete (incl. stuffing) packet */
81 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
83 /* where to place the packet id of a serial packet, so we can
84 * prepend the usb-packet header without the need to move the
86 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
88 /* max. size of incoming private packets (header+1 param) */
89 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
91 #define GARMIN_LAYERID_TRANSPORT 0
92 #define GARMIN_LAYERID_APPL 20
93 /* our own layer-id to use for some control mechanisms */
94 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
96 #define GARMIN_PKTID_PVT_DATA 51
97 #define GARMIN_PKTID_L001_COMMAND_DATA 10
99 #define CMND_ABORT_TRANSFER 0
101 /* packet ids used in private layer */
102 #define PRIV_PKTID_SET_DEBUG 1
103 #define PRIV_PKTID_SET_MODE 2
104 #define PRIV_PKTID_INFO_REQ 3
105 #define PRIV_PKTID_INFO_RESP 4
106 #define PRIV_PKTID_RESET_REQ 5
107 #define PRIV_PKTID_SET_DEF_MODE 6
115 /* structure used to queue incoming packets */
116 struct garmin_packet {
117 struct list_head list;
119 /* the real size of the data array, always > 0 */
124 /* structure used to keep the current state of the driver */
132 struct timer_list timer;
133 struct usb_serial_port *port;
137 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
138 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
141 struct list_head pktlist;
146 #define STATE_INITIAL_DELAY 1
147 #define STATE_TIMEOUT 2
148 #define STATE_SESSION_REQ1 3
149 #define STATE_SESSION_REQ2 4
150 #define STATE_ACTIVE 5
152 #define STATE_RESET 8
153 #define STATE_DISCONNECTED 9
154 #define STATE_WAIT_TTY_ACK 10
155 #define STATE_GSP_WAIT_DATA 11
157 #define MODE_NATIVE 0
158 #define MODE_GARMIN_SERIAL 1
160 /* Flags used in garmin_data.flags: */
161 #define FLAGS_SESSION_REPLY_MASK 0x00C0
162 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
163 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
164 #define FLAGS_BULK_IN_ACTIVE 0x0020
165 #define FLAGS_BULK_IN_RESTART 0x0010
166 #define FLAGS_THROTTLED 0x0008
167 #define APP_REQ_SEEN 0x0004
168 #define APP_RESP_SEEN 0x0002
169 #define CLEAR_HALT_REQUIRED 0x0001
171 #define FLAGS_QUEUING 0x0100
172 #define FLAGS_DROP_DATA 0x0800
174 #define FLAGS_GSP_SKIP 0x1000
175 #define FLAGS_GSP_DLESEEN 0x2000
182 /* function prototypes */
183 static int gsp_next_packet(struct garmin_data *garmin_data_p);
184 static int garmin_write_bulk(struct usb_serial_port *port,
185 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_REPLY[]
192 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
193 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
194 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
195 static unsigned char const GARMIN_APP_LAYER_REPLY[]
197 static unsigned char const GARMIN_START_PVT_REQ[]
198 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
199 static unsigned char const GARMIN_STOP_PVT_REQ[]
200 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
201 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
202 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
203 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
204 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
205 static unsigned char const PRIVATE_REQ[]
206 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
210 static const struct usb_device_id id_table[] = {
211 /* the same device id seems to be used by all
212 usb enabled GPS devices */
213 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
214 { } /* Terminating entry */
216 MODULE_DEVICE_TABLE(usb, id_table);
219 static inline int getLayerId(const __u8 *usbPacket)
221 return __le32_to_cpup((__le32 *)(usbPacket));
224 static inline int getPacketId(const __u8 *usbPacket)
226 return __le32_to_cpup((__le32 *)(usbPacket+4));
229 static inline int getDataLength(const __u8 *usbPacket)
231 return __le32_to_cpup((__le32 *)(usbPacket+8));
236 * check if the usb-packet in buf contains an abort-transfer command.
237 * (if yes, all queued data will be dropped)
239 static inline int isAbortTrfCmnd(const unsigned char *buf)
241 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
242 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
243 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
244 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
252 static void send_to_tty(struct usb_serial_port *port,
253 char *data, unsigned int actual_length)
255 struct tty_struct *tty = tty_port_tty_get(&port->port);
257 if (tty && actual_length) {
258 usb_serial_debug_data(&port->dev, __func__, actual_length, data);
259 tty_insert_flip_string(tty, data, actual_length);
260 tty_flip_buffer_push(tty);
266 /******************************************************************************
267 * packet queue handling
268 ******************************************************************************/
271 * queue a received (usb-)packet for later processing
273 static int pkt_add(struct garmin_data *garmin_data_p,
274 unsigned char *data, unsigned int data_length)
279 struct garmin_packet *pkt;
281 /* process only packets containg data ... */
283 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
286 dev_err(&garmin_data_p->port->dev, "out of memory\n");
289 pkt->size = data_length;
290 memcpy(pkt->data, data, data_length);
292 spin_lock_irqsave(&garmin_data_p->lock, flags);
293 garmin_data_p->flags |= FLAGS_QUEUING;
294 result = list_empty(&garmin_data_p->pktlist);
295 pkt->seq = garmin_data_p->seq_counter++;
296 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
297 state = garmin_data_p->state;
298 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
300 dev_dbg(&garmin_data_p->port->dev,
301 "%s - added: pkt: %d - %d bytes\n", __func__,
302 pkt->seq, data_length);
304 /* in serial mode, if someone is waiting for data from
305 the device, convert and send the next packet to tty. */
306 if (result && (state == STATE_GSP_WAIT_DATA))
307 gsp_next_packet(garmin_data_p);
313 /* get the next pending packet */
314 static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
317 struct garmin_packet *result = NULL;
319 spin_lock_irqsave(&garmin_data_p->lock, flags);
320 if (!list_empty(&garmin_data_p->pktlist)) {
321 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
322 list_del(&result->list);
324 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
329 /* free up all queued data */
330 static void pkt_clear(struct garmin_data *garmin_data_p)
333 struct garmin_packet *result = NULL;
335 spin_lock_irqsave(&garmin_data_p->lock, flags);
336 while (!list_empty(&garmin_data_p->pktlist)) {
337 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
338 list_del(&result->list);
341 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
345 /******************************************************************************
346 * garmin serial protocol handling handling
347 ******************************************************************************/
349 /* send an ack packet back to the tty */
350 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
357 dev_dbg(&garmin_data_p->port->dev, "%s - pkt-id: 0x%X.\n", __func__,
374 *ptr++ = 0xFF & (-cksum);
380 send_to_tty(garmin_data_p->port, pkt, l);
387 * called for a complete packet received from tty layer
389 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
390 * at GSP_INITIAL_OFFSET.
392 * count - number of bytes in the input buffer including space reserved for
393 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
394 * (including pkt-id, data-length a. cksum)
396 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
398 struct device *dev = &garmin_data_p->port->dev;
400 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
401 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
404 int pktid = recpkt[0];
405 int size = recpkt[1];
407 usb_serial_debug_data(&garmin_data_p->port->dev, __func__,
408 count-GSP_INITIAL_OFFSET, recpkt);
410 if (size != (count-GSP_INITIAL_OFFSET-3)) {
411 dev_dbg(dev, "%s - invalid size, expected %d bytes, got %d\n",
412 __func__, size, (count-GSP_INITIAL_OFFSET-3));
419 /* sanity check, remove after test ... */
420 if ((__u8 *)&(usbdata[3]) != recpkt) {
421 dev_dbg(dev, "%s - ptr mismatch %p - %p\n", __func__,
422 &(usbdata[4]), recpkt);
431 if ((0xff & (cksum + *recpkt)) != 0) {
432 dev_dbg(dev, "%s - invalid checksum, expected %02x, got %02x\n",
433 __func__, 0xff & -cksum, 0xff & *recpkt);
437 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
438 usbdata[1] = __cpu_to_le32(pktid);
439 usbdata[2] = __cpu_to_le32(size);
441 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
442 GARMIN_PKTHDR_LENGTH+size, 0);
444 /* if this was an abort-transfer command, flush all
446 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
447 spin_lock_irqsave(&garmin_data_p->lock, flags);
448 garmin_data_p->flags |= FLAGS_DROP_DATA;
449 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
450 pkt_clear(garmin_data_p);
459 * Called for data received from tty
461 * buf contains the data read, it may span more than one packet or even
464 * input record should be a serial-record, but it may not be complete.
465 * Copy it into our local buffer, until an etx is seen (or an error
467 * Once the record is complete, convert into a usb packet and send it
468 * to the bulk pipe, send an ack back to the tty.
470 * If the input is an ack, just send the last queued packet to the
473 * if the input is an abort command, drop all queued data.
476 static int gsp_receive(struct garmin_data *garmin_data_p,
477 const unsigned char *buf, int count)
479 struct device *dev = &garmin_data_p->port->dev;
482 int ack_or_nak_seen = 0;
485 /* dleSeen: set if last byte read was a DLE */
487 /* skip: if set, skip incoming data until possible start of
493 spin_lock_irqsave(&garmin_data_p->lock, flags);
494 dest = garmin_data_p->inbuffer;
495 size = garmin_data_p->insize;
496 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
497 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
498 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
500 /* dev_dbg(dev, "%s - dle=%d skip=%d size=%d count=%d\n",
501 __func__, dleSeen, skip, size, count); */
504 size = GSP_INITIAL_OFFSET;
506 while (offs < count) {
512 if (skip) { /* start of a new pkt */
514 size = GSP_INITIAL_OFFSET;
516 } else if (dleSeen) {
522 } else if (data == ETX) {
524 /* packet complete */
526 data = dest[GSP_INITIAL_OFFSET];
529 ack_or_nak_seen = ACK;
530 dev_dbg(dev, "ACK packet complete.\n");
531 } else if (data == NAK) {
532 ack_or_nak_seen = NAK;
533 dev_dbg(dev, "NAK packet complete.\n");
535 dev_dbg(dev, "packet complete - id=0x%X.\n",
537 gsp_rec_packet(garmin_data_p, size);
541 size = GSP_INITIAL_OFFSET;
549 size = GSP_INITIAL_OFFSET;
556 if (size >= GPS_IN_BUFSIZ) {
557 dev_dbg(dev, "%s - packet too large.\n", __func__);
559 size = GSP_INITIAL_OFFSET;
564 spin_lock_irqsave(&garmin_data_p->lock, flags);
566 garmin_data_p->insize = size;
568 /* copy flags back to structure */
570 garmin_data_p->flags |= FLAGS_GSP_SKIP;
572 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
575 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
577 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
579 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
581 if (ack_or_nak_seen) {
582 if (gsp_next_packet(garmin_data_p) > 0)
583 garmin_data_p->state = STATE_ACTIVE;
585 garmin_data_p->state = STATE_GSP_WAIT_DATA;
593 * Sends a usb packet to the tty
595 * Assumes, that all packages and at an usb-packet boundary.
597 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
599 static int gsp_send(struct garmin_data *garmin_data_p,
600 const unsigned char *buf, int count)
602 struct device *dev = &garmin_data_p->port->dev;
603 const unsigned char *src;
611 dev_dbg(dev, "%s - state %d - %d bytes.\n", __func__,
612 garmin_data_p->state, count);
614 k = garmin_data_p->outsize;
615 if ((k+count) > GPS_OUT_BUFSIZ) {
616 dev_dbg(dev, "packet too large\n");
617 garmin_data_p->outsize = 0;
621 memcpy(garmin_data_p->outbuffer+k, buf, count);
623 garmin_data_p->outsize = k;
625 if (k >= GARMIN_PKTHDR_LENGTH) {
626 pktid = getPacketId(garmin_data_p->outbuffer);
627 datalen = getDataLength(garmin_data_p->outbuffer);
628 i = GARMIN_PKTHDR_LENGTH + datalen;
635 dev_dbg(dev, "%s - %d bytes in buffer, %d bytes in pkt.\n", __func__, k, i);
637 /* garmin_data_p->outbuffer now contains a complete packet */
639 usb_serial_debug_data(&garmin_data_p->port->dev, __func__, k,
640 garmin_data_p->outbuffer);
642 garmin_data_p->outsize = 0;
644 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
645 dev_dbg(dev, "not an application packet (%d)\n",
646 getLayerId(garmin_data_p->outbuffer));
651 dev_dbg(dev, "packet-id %d too large\n", pktid);
656 dev_dbg(dev, "packet-size %d too large\n", datalen);
660 /* the serial protocol should be able to handle this packet */
663 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
664 for (i = 0; i < datalen; i++) {
669 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
670 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
671 /* can't add stuffing DLEs in place, move data to end
673 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
674 memcpy(dst, src, datalen);
678 dst = garmin_data_p->outbuffer;
688 for (i = 0; i < datalen; i++) {
696 cksum = 0xFF & -cksum;
703 i = dst-garmin_data_p->outbuffer;
705 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
707 garmin_data_p->pkt_id = pktid;
708 garmin_data_p->state = STATE_WAIT_TTY_ACK;
715 * Process the next pending data packet - if there is one
717 static int gsp_next_packet(struct garmin_data *garmin_data_p)
720 struct garmin_packet *pkt = NULL;
722 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
723 dev_dbg(&garmin_data_p->port->dev, "%s - next pkt: %d\n", __func__, pkt->seq);
724 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
736 /******************************************************************************
738 ******************************************************************************/
742 * Called for data received from tty
744 * The input data is expected to be in garmin usb-packet format.
746 * buf contains the data read, it may span more than one packet
747 * or even incomplete packets
749 static int nat_receive(struct garmin_data *garmin_data_p,
750 const unsigned char *buf, int count)
758 while (offs < count) {
759 /* if buffer contains header, copy rest of data */
760 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
761 len = GARMIN_PKTHDR_LENGTH
762 +getDataLength(garmin_data_p->inbuffer);
764 len = GARMIN_PKTHDR_LENGTH;
766 if (len >= GPS_IN_BUFSIZ) {
767 /* seems to be an invalid packet, ignore rest
769 dev_dbg(&garmin_data_p->port->dev,
770 "%s - packet size too large: %d\n",
772 garmin_data_p->insize = 0;
776 len -= garmin_data_p->insize;
777 if (len > (count-offs))
780 dest = garmin_data_p->inbuffer
781 + garmin_data_p->insize;
782 memcpy(dest, buf+offs, len);
783 garmin_data_p->insize += len;
788 /* do we have a complete packet ? */
789 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
790 len = GARMIN_PKTHDR_LENGTH+
791 getDataLength(garmin_data_p->inbuffer);
792 if (garmin_data_p->insize >= len) {
793 garmin_write_bulk(garmin_data_p->port,
794 garmin_data_p->inbuffer,
796 garmin_data_p->insize = 0;
798 /* if this was an abort-transfer command,
799 flush all queued data. */
800 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
801 spin_lock_irqsave(&garmin_data_p->lock,
803 garmin_data_p->flags |= FLAGS_DROP_DATA;
804 spin_unlock_irqrestore(
805 &garmin_data_p->lock, flags);
806 pkt_clear(garmin_data_p);
815 /******************************************************************************
817 ******************************************************************************/
819 static void priv_status_resp(struct usb_serial_port *port)
821 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
822 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
824 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
825 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
826 pkt[2] = __cpu_to_le32(12);
827 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
828 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
829 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
831 send_to_tty(port, (__u8 *)pkt, 6 * 4);
835 /******************************************************************************
836 * Garmin specific driver functions
837 ******************************************************************************/
839 static int process_resetdev_request(struct usb_serial_port *port)
843 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
845 spin_lock_irqsave(&garmin_data_p->lock, flags);
846 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
847 garmin_data_p->state = STATE_RESET;
848 garmin_data_p->serial_num = 0;
849 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
851 usb_kill_urb(port->interrupt_in_urb);
852 dev_dbg(&port->dev, "%s - usb_reset_device\n", __func__);
853 status = usb_reset_device(port->serial->dev);
855 dev_dbg(&port->dev, "%s - usb_reset_device failed: %d\n",
863 * clear all cached data
865 static int garmin_clear(struct garmin_data *garmin_data_p)
870 /* flush all queued data */
871 pkt_clear(garmin_data_p);
873 spin_lock_irqsave(&garmin_data_p->lock, flags);
874 garmin_data_p->insize = 0;
875 garmin_data_p->outsize = 0;
876 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
882 static int garmin_init_session(struct usb_serial_port *port)
884 struct usb_serial *serial = port->serial;
885 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
890 usb_kill_urb(port->interrupt_in_urb);
892 dev_dbg(&serial->dev->dev, "%s - adding interrupt input\n", __func__);
893 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
895 dev_err(&serial->dev->dev,
896 "%s - failed submitting interrupt urb, error %d\n",
901 * using the initialization method from gpsbabel. See comments in
902 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
905 dev_dbg(&serial->dev->dev, "%s - starting session ...\n", __func__);
906 garmin_data_p->state = STATE_ACTIVE;
908 for (i = 0; i < 3; i++) {
909 status = garmin_write_bulk(port,
910 GARMIN_START_SESSION_REQ,
911 sizeof(GARMIN_START_SESSION_REQ), 0);
926 static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
930 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
932 spin_lock_irqsave(&garmin_data_p->lock, flags);
933 garmin_data_p->mode = initial_mode;
934 garmin_data_p->count = 0;
935 garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
936 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
938 /* shutdown any bulk reads that might be going on */
939 usb_kill_urb(port->write_urb);
940 usb_kill_urb(port->read_urb);
942 if (garmin_data_p->state == STATE_RESET)
943 status = garmin_init_session(port);
945 garmin_data_p->state = STATE_ACTIVE;
950 static void garmin_close(struct usb_serial_port *port)
952 struct usb_serial *serial = port->serial;
953 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
955 dev_dbg(&port->dev, "%s - port %d - mode=%d state=%d flags=0x%X\n",
956 __func__, port->number, garmin_data_p->mode,
957 garmin_data_p->state, garmin_data_p->flags);
962 mutex_lock(&port->serial->disc_mutex);
964 if (!port->serial->disconnected)
965 garmin_clear(garmin_data_p);
967 /* shutdown our urbs */
968 usb_kill_urb(port->read_urb);
969 usb_kill_urb(port->write_urb);
971 /* keep reset state so we know that we must start a new session */
972 if (garmin_data_p->state != STATE_RESET)
973 garmin_data_p->state = STATE_DISCONNECTED;
975 mutex_unlock(&port->serial->disc_mutex);
979 static void garmin_write_bulk_callback(struct urb *urb)
981 struct usb_serial_port *port = urb->context;
984 struct garmin_data *garmin_data_p =
985 usb_get_serial_port_data(port);
987 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
989 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
990 gsp_send_ack(garmin_data_p,
991 ((__u8 *)urb->transfer_buffer)[4]);
994 usb_serial_port_softint(port);
997 /* Ignore errors that resulted from garmin_write_bulk with
1000 /* free up the transfer buffer, as usb_free_urb() does not do this */
1001 kfree(urb->transfer_buffer);
1005 static int garmin_write_bulk(struct usb_serial_port *port,
1006 const unsigned char *buf, int count,
1009 unsigned long flags;
1010 struct usb_serial *serial = port->serial;
1011 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1013 unsigned char *buffer;
1016 spin_lock_irqsave(&garmin_data_p->lock, flags);
1017 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1018 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1020 buffer = kmalloc(count, GFP_ATOMIC);
1022 dev_err(&port->dev, "out of memory\n");
1026 urb = usb_alloc_urb(0, GFP_ATOMIC);
1028 dev_err(&port->dev, "no more free urbs\n");
1033 memcpy(buffer, buf, count);
1035 usb_serial_debug_data(&port->dev, __func__, count, buffer);
1037 usb_fill_bulk_urb(urb, serial->dev,
1038 usb_sndbulkpipe(serial->dev,
1039 port->bulk_out_endpointAddress),
1041 garmin_write_bulk_callback,
1042 dismiss_ack ? NULL : port);
1043 urb->transfer_flags |= URB_ZERO_PACKET;
1045 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1047 spin_lock_irqsave(&garmin_data_p->lock, flags);
1048 garmin_data_p->flags |= APP_REQ_SEEN;
1049 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1051 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1052 pkt_clear(garmin_data_p);
1053 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1057 /* send it down the pipe */
1058 status = usb_submit_urb(urb, GFP_ATOMIC);
1061 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1066 /* we are done with this urb, so let the host driver
1067 * really free it when it is finished with it */
1073 static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1074 const unsigned char *buf, int count)
1076 struct device *dev = &port->dev;
1077 int pktid, pktsiz, len;
1078 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1079 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1081 usb_serial_debug_data(dev, __func__, count, buf);
1083 if (garmin_data_p->state == STATE_RESET)
1086 /* check for our private packets */
1087 if (count >= GARMIN_PKTHDR_LENGTH) {
1092 memcpy(garmin_data_p->privpkt, buf, len);
1094 pktsiz = getDataLength(garmin_data_p->privpkt);
1095 pktid = getPacketId(garmin_data_p->privpkt);
1097 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1098 && GARMIN_LAYERID_PRIVATE ==
1099 getLayerId(garmin_data_p->privpkt)) {
1101 dev_dbg(dev, "%s - processing private request %d\n",
1104 /* drop all unfinished transfers */
1105 garmin_clear(garmin_data_p);
1108 case PRIV_PKTID_SET_MODE:
1111 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1112 dev_dbg(dev, "%s - mode set to %d\n",
1113 __func__, garmin_data_p->mode);
1116 case PRIV_PKTID_INFO_REQ:
1117 priv_status_resp(port);
1120 case PRIV_PKTID_RESET_REQ:
1121 process_resetdev_request(port);
1124 case PRIV_PKTID_SET_DEF_MODE:
1127 initial_mode = __le32_to_cpu(privpkt[3]);
1128 dev_dbg(dev, "%s - initial_mode set to %d\n",
1130 garmin_data_p->mode);
1137 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1138 return gsp_receive(garmin_data_p, buf, count);
1139 } else { /* MODE_NATIVE */
1140 return nat_receive(garmin_data_p, buf, count);
1145 static int garmin_write_room(struct tty_struct *tty)
1147 struct usb_serial_port *port = tty->driver_data;
1149 * Report back the bytes currently available in the output buffer.
1151 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1152 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1156 static void garmin_read_process(struct garmin_data *garmin_data_p,
1157 unsigned char *data, unsigned data_length,
1160 unsigned long flags;
1162 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1163 /* abort-transfer cmd is actice */
1164 dev_dbg(&garmin_data_p->port->dev, "%s - pkt dropped\n", __func__);
1165 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1166 garmin_data_p->state != STATE_RESET) {
1168 /* if throttling is active or postprecessing is required
1169 put the received data in the input queue, otherwise
1170 send it directly to the tty port */
1171 if (garmin_data_p->flags & FLAGS_QUEUING) {
1172 pkt_add(garmin_data_p, data, data_length);
1173 } else if (bulk_data ||
1174 getLayerId(data) == GARMIN_LAYERID_APPL) {
1176 spin_lock_irqsave(&garmin_data_p->lock, flags);
1177 garmin_data_p->flags |= APP_RESP_SEEN;
1178 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1180 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1181 pkt_add(garmin_data_p, data, data_length);
1183 send_to_tty(garmin_data_p->port, data,
1187 /* ignore system layer packets ... */
1192 static void garmin_read_bulk_callback(struct urb *urb)
1194 unsigned long flags;
1195 struct usb_serial_port *port = urb->context;
1196 struct usb_serial *serial = port->serial;
1197 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1198 unsigned char *data = urb->transfer_buffer;
1199 int status = urb->status;
1203 dev_dbg(&urb->dev->dev, "%s - bad serial pointer, exiting\n", __func__);
1208 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
1213 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
1215 garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
1217 if (urb->actual_length == 0 &&
1218 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1219 spin_lock_irqsave(&garmin_data_p->lock, flags);
1220 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1221 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1222 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1225 "%s - failed resubmitting read urb, error %d\n",
1227 } else if (urb->actual_length > 0) {
1228 /* Continue trying to read until nothing more is received */
1229 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1230 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1233 "%s - failed resubmitting read urb, error %d\n",
1237 dev_dbg(&port->dev, "%s - end of bulk data\n", __func__);
1238 spin_lock_irqsave(&garmin_data_p->lock, flags);
1239 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1240 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1245 static void garmin_read_int_callback(struct urb *urb)
1247 unsigned long flags;
1249 struct usb_serial_port *port = urb->context;
1250 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1251 unsigned char *data = urb->transfer_buffer;
1252 int status = urb->status;
1261 /* this urb is terminated, clean up */
1262 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
1266 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
1271 usb_serial_debug_data(&port->dev, __func__, urb->actual_length,
1272 urb->transfer_buffer);
1274 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1275 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1276 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1278 dev_dbg(&port->dev, "%s - bulk data available.\n", __func__);
1280 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1282 /* bulk data available */
1283 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1286 "%s - failed submitting read urb, error %d\n",
1289 spin_lock_irqsave(&garmin_data_p->lock, flags);
1290 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1291 spin_unlock_irqrestore(&garmin_data_p->lock,
1295 /* bulk-in transfer still active */
1296 spin_lock_irqsave(&garmin_data_p->lock, flags);
1297 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1298 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1301 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1302 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1303 sizeof(GARMIN_START_SESSION_REPLY))) {
1305 spin_lock_irqsave(&garmin_data_p->lock, flags);
1306 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1307 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1309 /* save the serial number */
1310 garmin_data_p->serial_num = __le32_to_cpup(
1311 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
1313 dev_dbg(&port->dev, "%s - start-of-session reply seen - serial %u.\n",
1314 __func__, garmin_data_p->serial_num);
1317 garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
1319 retval = usb_submit_urb(urb, GFP_ATOMIC);
1321 dev_err(&urb->dev->dev,
1322 "%s - Error %d submitting interrupt urb\n",
1328 * Sends the next queued packt to the tty port (garmin native mode only)
1329 * and then sets a timer to call itself again until all queued data
1332 static int garmin_flush_queue(struct garmin_data *garmin_data_p)
1334 unsigned long flags;
1335 struct garmin_packet *pkt;
1337 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1338 pkt = pkt_pop(garmin_data_p);
1340 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1342 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1345 spin_lock_irqsave(&garmin_data_p->lock, flags);
1346 garmin_data_p->flags &= ~FLAGS_QUEUING;
1347 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1354 static void garmin_throttle(struct tty_struct *tty)
1356 struct usb_serial_port *port = tty->driver_data;
1357 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1359 /* set flag, data received will be put into a queue
1360 for later processing */
1361 spin_lock_irq(&garmin_data_p->lock);
1362 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1363 spin_unlock_irq(&garmin_data_p->lock);
1367 static void garmin_unthrottle(struct tty_struct *tty)
1369 struct usb_serial_port *port = tty->driver_data;
1370 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1373 spin_lock_irq(&garmin_data_p->lock);
1374 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1375 spin_unlock_irq(&garmin_data_p->lock);
1377 /* in native mode send queued data to tty, in
1378 serial mode nothing needs to be done here */
1379 if (garmin_data_p->mode == MODE_NATIVE)
1380 garmin_flush_queue(garmin_data_p);
1382 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1383 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1386 "%s - failed resubmitting read urb, error %d\n",
1392 * The timer is currently only used to send queued packets to
1393 * the tty in cases where the protocol provides no own handshaking
1394 * to initiate the transfer.
1396 static void timeout_handler(unsigned long data)
1398 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1400 /* send the next queued packet to the tty port */
1401 if (garmin_data_p->mode == MODE_NATIVE)
1402 if (garmin_data_p->flags & FLAGS_QUEUING)
1403 garmin_flush_queue(garmin_data_p);
1408 static int garmin_attach(struct usb_serial *serial)
1411 struct usb_serial_port *port = serial->port[0];
1412 struct garmin_data *garmin_data_p = NULL;
1414 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1415 if (garmin_data_p == NULL) {
1416 dev_err(&port->dev, "%s - Out of memory\n", __func__);
1419 init_timer(&garmin_data_p->timer);
1420 spin_lock_init(&garmin_data_p->lock);
1421 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1422 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1423 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1424 garmin_data_p->timer.function = timeout_handler;
1425 garmin_data_p->port = port;
1426 garmin_data_p->state = 0;
1427 garmin_data_p->flags = 0;
1428 garmin_data_p->count = 0;
1429 usb_set_serial_port_data(port, garmin_data_p);
1431 status = garmin_init_session(port);
1437 static void garmin_disconnect(struct usb_serial *serial)
1439 struct usb_serial_port *port = serial->port[0];
1440 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1442 usb_kill_urb(port->interrupt_in_urb);
1443 del_timer_sync(&garmin_data_p->timer);
1447 static void garmin_release(struct usb_serial *serial)
1449 struct usb_serial_port *port = serial->port[0];
1450 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1452 kfree(garmin_data_p);
1456 /* All of the device info needed */
1457 static struct usb_serial_driver garmin_device = {
1459 .owner = THIS_MODULE,
1460 .name = "garmin_gps",
1462 .description = "Garmin GPS usb/tty",
1463 .id_table = id_table,
1465 .open = garmin_open,
1466 .close = garmin_close,
1467 .throttle = garmin_throttle,
1468 .unthrottle = garmin_unthrottle,
1469 .attach = garmin_attach,
1470 .disconnect = garmin_disconnect,
1471 .release = garmin_release,
1472 .write = garmin_write,
1473 .write_room = garmin_write_room,
1474 .write_bulk_callback = garmin_write_bulk_callback,
1475 .read_bulk_callback = garmin_read_bulk_callback,
1476 .read_int_callback = garmin_read_int_callback,
1479 static struct usb_serial_driver * const serial_drivers[] = {
1480 &garmin_device, NULL
1483 module_usb_serial_driver(serial_drivers, id_table);
1485 MODULE_AUTHOR(DRIVER_AUTHOR);
1486 MODULE_DESCRIPTION(DRIVER_DESC);
1487 MODULE_LICENSE("GPL");
1489 module_param(initial_mode, int, S_IRUGO);
1490 MODULE_PARM_DESC(initial_mode, "Initial mode");