4 * Copyright (C) 2006,2007 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 <linux/uaccess.h>
37 #include <asm/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 31
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 */
136 struct timer_list timer;
137 struct usb_serial_port *port;
141 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
142 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
147 struct list_head pktlist;
152 #define STATE_INITIAL_DELAY 1
153 #define STATE_TIMEOUT 2
154 #define STATE_SESSION_REQ1 3
155 #define STATE_SESSION_REQ2 4
156 #define STATE_ACTIVE 5
158 #define STATE_RESET 8
159 #define STATE_DISCONNECTED 9
160 #define STATE_WAIT_TTY_ACK 10
161 #define STATE_GSP_WAIT_DATA 11
163 #define MODE_NATIVE 0
164 #define MODE_GARMIN_SERIAL 1
166 /* Flags used in garmin_data.flags: */
167 #define FLAGS_SESSION_REPLY_MASK 0x00C0
168 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
169 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
170 #define FLAGS_BULK_IN_ACTIVE 0x0020
171 #define FLAGS_BULK_IN_RESTART 0x0010
172 #define FLAGS_THROTTLED 0x0008
173 #define CLEAR_HALT_REQUIRED 0x0001
175 #define FLAGS_QUEUING 0x0100
176 #define FLAGS_DROP_DATA 0x0800
178 #define FLAGS_GSP_SKIP 0x1000
179 #define FLAGS_GSP_DLESEEN 0x2000
186 /* function prototypes */
187 static void gsp_next_packet(struct garmin_data *garmin_data_p);
188 static int garmin_write_bulk(struct usb_serial_port *port,
189 const unsigned char *buf, int count,
192 /* some special packets to be send or received */
193 static unsigned char const GARMIN_START_SESSION_REQ[]
194 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
195 static unsigned char const GARMIN_START_SESSION_REQ2[]
196 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
197 static unsigned char const GARMIN_START_SESSION_REPLY[]
198 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
199 static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
200 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
201 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
202 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
203 static unsigned char const GARMIN_APP_LAYER_REPLY[]
205 static unsigned char const GARMIN_START_PVT_REQ[]
206 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
207 static unsigned char const GARMIN_STOP_PVT_REQ[]
208 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
209 static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
210 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
211 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
212 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
213 static unsigned char const PRIVATE_REQ[]
214 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
218 static struct usb_device_id id_table [] = {
219 /* the same device id seems to be used by all
220 usb enabled GPS devices */
221 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
222 { } /* Terminating entry */
225 MODULE_DEVICE_TABLE(usb, id_table);
227 static struct usb_driver garmin_driver = {
228 .name = "garmin_gps",
229 .probe = usb_serial_probe,
230 .disconnect = usb_serial_disconnect,
231 .id_table = id_table,
236 static inline int noResponseFromAppLayer(struct garmin_data *garmin_data_p)
238 return atomic_read(&garmin_data_p->req_count) ==
239 atomic_read(&garmin_data_p->resp_count);
243 static inline int getLayerId(const __u8 *usbPacket)
245 return __le32_to_cpup((__le32 *)(usbPacket));
248 static inline int getPacketId(const __u8 *usbPacket)
250 return __le32_to_cpup((__le32 *)(usbPacket+4));
253 static inline int getDataLength(const __u8 *usbPacket)
255 return __le32_to_cpup((__le32 *)(usbPacket+8));
260 * check if the usb-packet in buf contains an abort-transfer command.
261 * (if yes, all queued data will be dropped)
263 static inline int isAbortTrfCmnd(const unsigned char *buf)
265 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
266 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
267 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
268 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
276 static void send_to_tty(struct usb_serial_port *port,
277 char *data, unsigned int actual_length)
279 struct tty_struct *tty = port->port.tty;
281 if (tty && actual_length) {
283 usb_serial_debug_data(debug, &port->dev,
284 __func__, actual_length, data);
286 tty_buffer_request_room(tty, actual_length);
287 tty_insert_flip_string(tty, data, actual_length);
288 tty_flip_buffer_push(tty);
293 /******************************************************************************
294 * packet queue handling
295 ******************************************************************************/
298 * queue a received (usb-)packet for later processing
300 static int pkt_add(struct garmin_data *garmin_data_p,
301 unsigned char *data, unsigned int data_length)
306 struct garmin_packet *pkt;
308 /* process only packets containg data ... */
310 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
313 dev_err(&garmin_data_p->port->dev, "out of memory\n");
316 pkt->size = data_length;
317 memcpy(pkt->data, data, data_length);
319 spin_lock_irqsave(&garmin_data_p->lock, flags);
320 garmin_data_p->flags |= FLAGS_QUEUING;
321 result = list_empty(&garmin_data_p->pktlist);
322 pkt->seq = garmin_data_p->seq_counter++;
323 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
324 state = garmin_data_p->state;
325 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
327 /* in serial mode, if someone is waiting for data from
328 the device, iconvert and send the next packet to tty. */
329 if (result && (state == STATE_GSP_WAIT_DATA))
330 gsp_next_packet(garmin_data_p);
336 /* get the next pending packet */
337 static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
340 struct garmin_packet *result = NULL;
342 spin_lock_irqsave(&garmin_data_p->lock, flags);
343 if (!list_empty(&garmin_data_p->pktlist)) {
344 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
345 list_del(&result->list);
347 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
352 /* free up all queued data */
353 static void pkt_clear(struct garmin_data *garmin_data_p)
356 struct garmin_packet *result = NULL;
360 spin_lock_irqsave(&garmin_data_p->lock, flags);
361 while (!list_empty(&garmin_data_p->pktlist)) {
362 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
363 list_del(&result->list);
366 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
370 /******************************************************************************
371 * garmin serial protocol handling handling
372 ******************************************************************************/
374 /* send an ack packet back to the tty */
375 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
382 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
398 *ptr++ = 0xFF & (-cksum);
404 send_to_tty(garmin_data_p->port, pkt, l);
411 * called for a complete packet received from tty layer
413 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
414 * at GSP_INITIAL_OFFSET.
416 * count - number of bytes in the input buffer including space reserved for
417 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
418 * (including pkt-id, data-length a. cksum)
420 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
422 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
423 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
427 int pktid = recpkt[0];
428 int size = recpkt[1];
430 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
431 __func__, count-GSP_INITIAL_OFFSET, recpkt);
433 if (size != (count-GSP_INITIAL_OFFSET-3)) {
434 dbg("%s - invalid size, expected %d bytes, got %d",
435 __func__, size, (count-GSP_INITIAL_OFFSET-3));
442 /* sanity check, remove after test ... */
443 if ((__u8 *)&(usbdata[3]) != recpkt) {
444 dbg("%s - ptr mismatch %p - %p",
445 __func__, &(usbdata[4]), recpkt);
454 if ((0xff & (cksum + *recpkt)) != 0) {
455 dbg("%s - invalid checksum, expected %02x, got %02x",
456 __func__, 0xff & -cksum, 0xff & *recpkt);
460 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
461 usbdata[1] = __cpu_to_le32(pktid);
462 usbdata[2] = __cpu_to_le32(size);
464 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
465 GARMIN_PKTHDR_LENGTH+size, 0);
467 /* if this was an abort-transfer command, flush all
469 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
470 garmin_data_p->flags |= FLAGS_DROP_DATA;
471 pkt_clear(garmin_data_p);
480 * Called for data received from tty
482 * buf contains the data read, it may span more than one packet or even
485 * input record should be a serial-record, but it may not be complete.
486 * Copy it into our local buffer, until an etx is seen (or an error
488 * Once the record is complete, convert into a usb packet and send it
489 * to the bulk pipe, send an ack back to the tty.
491 * If the input is an ack, just send the last queued packet to the
494 * if the input is an abort command, drop all queued data.
497 static int gsp_receive(struct garmin_data *garmin_data_p,
498 const unsigned char *buf, int count)
502 int ack_or_nak_seen = 0;
506 /* dleSeen: set if last byte read was a DLE */
508 /* skip: if set, skip incoming data until possible start of
514 spin_lock_irqsave(&garmin_data_p->lock, flags);
515 dest = garmin_data_p->inbuffer;
516 size = garmin_data_p->insize;
517 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
518 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
519 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
521 dbg("%s - dle=%d skip=%d size=%d count=%d",
522 __func__, dleSeen, skip, size, count);
525 size = GSP_INITIAL_OFFSET;
527 while (offs < count) {
533 if (skip) { /* start of a new pkt */
535 size = GSP_INITIAL_OFFSET;
537 } else if (dleSeen) {
543 } else if (data == ETX) {
545 /* packet complete */
547 data = dest[GSP_INITIAL_OFFSET];
550 ack_or_nak_seen = ACK;
551 dbg("ACK packet complete.");
552 } else if (data == NAK) {
553 ack_or_nak_seen = NAK;
554 dbg("NAK packet complete.");
556 dbg("packet complete - id=0x%X.",
558 gsp_rec_packet(garmin_data_p, size);
562 size = GSP_INITIAL_OFFSET;
570 dbg("non-masked DLE at %d - restarting", i);
571 size = GSP_INITIAL_OFFSET;
578 if (size >= GPS_IN_BUFSIZ) {
579 dbg("%s - packet too large.", __func__);
581 size = GSP_INITIAL_OFFSET;
586 spin_lock_irqsave(&garmin_data_p->lock, flags);
588 garmin_data_p->insize = size;
590 /* copy flags back to structure */
592 garmin_data_p->flags |= FLAGS_GSP_SKIP;
594 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
597 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
599 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
602 garmin_data_p->state = STATE_GSP_WAIT_DATA;
604 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
607 gsp_next_packet(garmin_data_p);
615 * Sends a usb packet to the tty
617 * Assumes, that all packages and at an usb-packet boundary.
619 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
621 static int gsp_send(struct garmin_data *garmin_data_p,
622 const unsigned char *buf, int count)
624 const unsigned char *src;
632 dbg("%s - state %d - %d bytes.", __func__,
633 garmin_data_p->state, count);
635 k = garmin_data_p->outsize;
636 if ((k+count) > GPS_OUT_BUFSIZ) {
637 dbg("packet too large");
638 garmin_data_p->outsize = 0;
642 memcpy(garmin_data_p->outbuffer+k, buf, count);
644 garmin_data_p->outsize = k;
646 if (k >= GARMIN_PKTHDR_LENGTH) {
647 pktid = getPacketId(garmin_data_p->outbuffer);
648 datalen = getDataLength(garmin_data_p->outbuffer);
649 i = GARMIN_PKTHDR_LENGTH + datalen;
656 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
658 /* garmin_data_p->outbuffer now contains a complete packet */
660 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
661 __func__, k, garmin_data_p->outbuffer);
663 garmin_data_p->outsize = 0;
665 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
666 dbg("not an application packet (%d)",
667 getLayerId(garmin_data_p->outbuffer));
672 dbg("packet-id %d too large", pktid);
677 dbg("packet-size %d too large", datalen);
681 /* the serial protocol should be able to handle this packet */
684 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
685 for (i = 0; i < datalen; i++) {
690 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
691 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
692 /* can't add stuffing DLEs in place, move data to end
694 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
695 memcpy(dst, src, datalen);
699 dst = garmin_data_p->outbuffer;
709 for (i = 0; i < datalen; i++) {
717 cksum = 0xFF & -cksum;
724 i = dst-garmin_data_p->outbuffer;
726 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
728 garmin_data_p->pkt_id = pktid;
729 garmin_data_p->state = STATE_WAIT_TTY_ACK;
739 * Process the next pending data packet - if there is one
741 static void gsp_next_packet(struct garmin_data *garmin_data_p)
743 struct garmin_packet *pkt = NULL;
745 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
746 dbg("%s - next pkt: %d", __func__, pkt->seq);
747 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
758 /******************************************************************************
760 ******************************************************************************/
764 * Called for data received from tty
766 * The input data is expected to be in garmin usb-packet format.
768 * buf contains the data read, it may span more than one packet
769 * or even incomplete packets
771 static int nat_receive(struct garmin_data *garmin_data_p,
772 const unsigned char *buf, int count)
780 while (offs < count) {
781 /* if buffer contains header, copy rest of data */
782 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
783 len = GARMIN_PKTHDR_LENGTH
784 +getDataLength(garmin_data_p->inbuffer);
786 len = GARMIN_PKTHDR_LENGTH;
788 if (len >= GPS_IN_BUFSIZ) {
789 /* seems to be an invalid packet, ignore rest
791 dbg("%s - packet size too large: %d", __func__, len);
792 garmin_data_p->insize = 0;
796 len -= garmin_data_p->insize;
797 if (len > (count-offs))
800 dest = garmin_data_p->inbuffer
801 + garmin_data_p->insize;
802 memcpy(dest, buf+offs, len);
803 garmin_data_p->insize += len;
808 /* do we have a complete packet ? */
809 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
810 len = GARMIN_PKTHDR_LENGTH+
811 getDataLength(garmin_data_p->inbuffer);
812 if (garmin_data_p->insize >= len) {
813 garmin_write_bulk(garmin_data_p->port,
814 garmin_data_p->inbuffer,
816 garmin_data_p->insize = 0;
818 /* if this was an abort-transfer command,
819 flush all queued data. */
820 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
821 spin_lock_irqsave(&garmin_data_p->lock,
823 garmin_data_p->flags |= FLAGS_DROP_DATA;
824 spin_unlock_irqrestore(
825 &garmin_data_p->lock, flags);
826 pkt_clear(garmin_data_p);
835 /******************************************************************************
837 ******************************************************************************/
839 static void priv_status_resp(struct usb_serial_port *port)
841 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
842 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
844 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
845 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
846 pkt[2] = __cpu_to_le32(12);
847 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
848 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
849 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
851 send_to_tty(port, (__u8 *)pkt, 6 * 4);
855 /******************************************************************************
856 * Garmin specific driver functions
857 ******************************************************************************/
859 static int process_resetdev_request(struct usb_serial_port *port)
863 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
865 spin_lock_irqsave(&garmin_data_p->lock, flags);
866 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
867 garmin_data_p->state = STATE_RESET;
868 garmin_data_p->serial_num = 0;
869 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
871 usb_kill_urb(port->interrupt_in_urb);
872 dbg("%s - usb_reset_device", __func__);
873 status = usb_reset_device(port->serial->dev);
875 dbg("%s - usb_reset_device failed: %d",
883 * clear all cached data
885 static int garmin_clear(struct garmin_data *garmin_data_p)
890 struct usb_serial_port *port = garmin_data_p->port;
892 if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
893 /* send a terminate command */
894 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
895 sizeof(GARMIN_STOP_TRANSFER_REQ), 1);
898 /* flush all queued data */
899 pkt_clear(garmin_data_p);
901 spin_lock_irqsave(&garmin_data_p->lock, flags);
902 garmin_data_p->insize = 0;
903 garmin_data_p->outsize = 0;
904 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
914 static int garmin_init_session(struct usb_serial_port *port)
917 struct usb_serial *serial = port->serial;
918 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
922 usb_kill_urb(port->interrupt_in_urb);
924 dbg("%s - adding interrupt input", __func__);
925 port->interrupt_in_urb->dev = serial->dev;
926 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
928 dev_err(&serial->dev->dev,
929 "%s - failed submitting interrupt urb, error %d\n",
934 dbg("%s - starting session ...", __func__);
935 garmin_data_p->state = STATE_ACTIVE;
936 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
937 sizeof(GARMIN_START_SESSION_REQ), 0);
941 spin_lock_irqsave(&garmin_data_p->lock, flags);
942 garmin_data_p->ignorePkts++;
943 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
945 /* not needed, but the win32 driver does it too ... */
946 status = garmin_write_bulk(port,
947 GARMIN_START_SESSION_REQ2,
948 sizeof(GARMIN_START_SESSION_REQ2), 0);
951 spin_lock_irqsave(&garmin_data_p->lock, flags);
952 garmin_data_p->ignorePkts++;
953 spin_unlock_irqrestore(&garmin_data_p->lock,
966 static int garmin_open(struct tty_struct *tty,
967 struct usb_serial_port *port, struct file *filp)
971 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
973 dbg("%s - port %d", __func__, port->number);
976 * Force low_latency on so that our tty_push actually forces the data
977 * through, otherwise it is scheduled, and with high data rates (like
978 * with OHCI) data can get lost.
981 tty->low_latency = 1;
983 spin_lock_irqsave(&garmin_data_p->lock, flags);
984 garmin_data_p->mode = initial_mode;
985 garmin_data_p->count = 0;
986 garmin_data_p->flags = 0;
987 atomic_set(&garmin_data_p->req_count, 0);
988 atomic_set(&garmin_data_p->resp_count, 0);
989 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
991 /* shutdown any bulk reads that might be going on */
992 usb_kill_urb(port->write_urb);
993 usb_kill_urb(port->read_urb);
995 if (garmin_data_p->state == STATE_RESET)
996 status = garmin_init_session(port);
998 garmin_data_p->state = STATE_ACTIVE;
1003 static void garmin_close(struct tty_struct *tty,
1004 struct usb_serial_port *port, struct file *filp)
1006 struct usb_serial *serial = port->serial;
1007 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1009 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
1010 port->number, garmin_data_p->mode,
1011 garmin_data_p->state, garmin_data_p->flags);
1016 mutex_lock(&port->serial->disc_mutex);
1017 if (!port->serial->disconnected)
1018 garmin_clear(garmin_data_p);
1020 /* shutdown our urbs */
1021 usb_kill_urb(port->read_urb);
1022 usb_kill_urb(port->write_urb);
1024 if (!port->serial->disconnected) {
1025 if (noResponseFromAppLayer(garmin_data_p) ||
1026 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1027 process_resetdev_request(port);
1028 garmin_data_p->state = STATE_RESET;
1030 garmin_data_p->state = STATE_DISCONNECTED;
1033 garmin_data_p->state = STATE_DISCONNECTED;
1035 mutex_unlock(&port->serial->disc_mutex);
1038 static void garmin_write_bulk_callback(struct urb *urb)
1040 unsigned long flags;
1041 struct usb_serial_port *port = urb->context;
1042 int status = urb->status;
1045 struct garmin_data *garmin_data_p =
1046 usb_get_serial_port_data(port);
1048 dbg("%s - port %d", __func__, port->number);
1050 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)
1051 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1052 gsp_send_ack(garmin_data_p,
1053 ((__u8 *)urb->transfer_buffer)[4]);
1057 dbg("%s - nonzero write bulk status received: %d",
1058 __func__, urb->status);
1059 spin_lock_irqsave(&garmin_data_p->lock, flags);
1060 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1061 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1064 usb_serial_port_softint(port);
1067 /* Ignore errors that resulted from garmin_write_bulk with
1070 /* free up the transfer buffer, as usb_free_urb() does not do this */
1071 kfree(urb->transfer_buffer);
1075 static int garmin_write_bulk(struct usb_serial_port *port,
1076 const unsigned char *buf, int count,
1079 unsigned long flags;
1080 struct usb_serial *serial = port->serial;
1081 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1083 unsigned char *buffer;
1086 dbg("%s - port %d, state %d", __func__, port->number,
1087 garmin_data_p->state);
1089 spin_lock_irqsave(&garmin_data_p->lock, flags);
1090 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1091 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1093 buffer = kmalloc(count, GFP_ATOMIC);
1095 dev_err(&port->dev, "out of memory\n");
1099 urb = usb_alloc_urb(0, GFP_ATOMIC);
1101 dev_err(&port->dev, "no more free urbs\n");
1106 memcpy(buffer, buf, count);
1108 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
1110 usb_fill_bulk_urb(urb, serial->dev,
1111 usb_sndbulkpipe(serial->dev,
1112 port->bulk_out_endpointAddress),
1114 garmin_write_bulk_callback,
1115 dismiss_ack ? NULL : port);
1116 urb->transfer_flags |= URB_ZERO_PACKET;
1118 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1119 atomic_inc(&garmin_data_p->req_count);
1120 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1121 pkt_clear(garmin_data_p);
1122 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1126 /* send it down the pipe */
1127 status = usb_submit_urb(urb, GFP_ATOMIC);
1130 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1135 /* we are done with this urb, so let the host driver
1136 * really free it when it is finished with it */
1142 static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1143 const unsigned char *buf, int count)
1145 int pktid, pktsiz, len;
1146 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1147 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1149 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
1151 /* check for our private packets */
1152 if (count >= GARMIN_PKTHDR_LENGTH) {
1157 memcpy(garmin_data_p->privpkt, buf, len);
1159 pktsiz = getDataLength(garmin_data_p->privpkt);
1160 pktid = getPacketId(garmin_data_p->privpkt);
1162 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1163 && GARMIN_LAYERID_PRIVATE ==
1164 getLayerId(garmin_data_p->privpkt)) {
1166 dbg("%s - processing private request %d",
1169 /* drop all unfinished transfers */
1170 garmin_clear(garmin_data_p);
1174 case PRIV_PKTID_SET_DEBUG:
1177 debug = __le32_to_cpu(privpkt[3]);
1178 dbg("%s - debug level set to 0x%X",
1182 case PRIV_PKTID_SET_MODE:
1185 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1186 dbg("%s - mode set to %d",
1187 __func__, garmin_data_p->mode);
1190 case PRIV_PKTID_INFO_REQ:
1191 priv_status_resp(port);
1194 case PRIV_PKTID_RESET_REQ:
1195 atomic_inc(&garmin_data_p->req_count);
1198 case PRIV_PKTID_SET_DEF_MODE:
1201 initial_mode = __le32_to_cpu(privpkt[3]);
1202 dbg("%s - initial_mode set to %d",
1204 garmin_data_p->mode);
1211 garmin_data_p->ignorePkts = 0;
1213 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1214 return gsp_receive(garmin_data_p, buf, count);
1215 } else { /* MODE_NATIVE */
1216 return nat_receive(garmin_data_p, buf, count);
1221 static int garmin_write_room(struct tty_struct *tty)
1223 struct usb_serial_port *port = tty->driver_data;
1225 * Report back the bytes currently available in the output buffer.
1227 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1228 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1232 static void garmin_read_process(struct garmin_data *garmin_data_p,
1233 unsigned char *data, unsigned data_length)
1235 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1236 /* abort-transfer cmd is actice */
1237 dbg("%s - pkt dropped", __func__);
1238 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1239 garmin_data_p->state != STATE_RESET) {
1241 /* remember any appl.layer packets, so we know
1242 if a reset is required or not when closing
1244 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1245 sizeof(GARMIN_APP_LAYER_REPLY))) {
1246 atomic_inc(&garmin_data_p->resp_count);
1249 /* if throttling is active or postprecessing is required
1250 put the received data in the input queue, otherwise
1251 send it directly to the tty port */
1252 if (garmin_data_p->flags & FLAGS_QUEUING) {
1253 pkt_add(garmin_data_p, data, data_length);
1254 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1255 if (getLayerId(data) == GARMIN_LAYERID_APPL)
1256 pkt_add(garmin_data_p, data, data_length);
1258 send_to_tty(garmin_data_p->port, data, data_length);
1264 static void garmin_read_bulk_callback(struct urb *urb)
1266 unsigned long flags;
1267 struct usb_serial_port *port = urb->context;
1268 struct usb_serial *serial = port->serial;
1269 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1270 unsigned char *data = urb->transfer_buffer;
1271 int status = urb->status;
1274 dbg("%s - port %d", __func__, port->number);
1277 dbg("%s - bad serial pointer, exiting", __func__);
1282 dbg("%s - nonzero read bulk status received: %d",
1287 usb_serial_debug_data(debug, &port->dev,
1288 __func__, urb->actual_length, data);
1290 garmin_read_process(garmin_data_p, data, urb->actual_length);
1292 if (urb->actual_length == 0 &&
1293 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1294 spin_lock_irqsave(&garmin_data_p->lock, flags);
1295 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1296 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1297 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1300 "%s - failed resubmitting read urb, error %d\n",
1302 } else if (urb->actual_length > 0) {
1303 /* Continue trying to read until nothing more is received */
1304 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1305 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1308 "%s - failed resubmitting read urb, "
1309 "error %d\n", __func__, retval);
1312 dbg("%s - end of bulk data", __func__);
1313 spin_lock_irqsave(&garmin_data_p->lock, flags);
1314 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1315 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1321 static void garmin_read_int_callback(struct urb *urb)
1323 unsigned long flags;
1325 struct usb_serial_port *port = urb->context;
1326 struct usb_serial *serial = port->serial;
1327 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1328 unsigned char *data = urb->transfer_buffer;
1329 int status = urb->status;
1338 /* this urb is terminated, clean up */
1339 dbg("%s - urb shutting down with status: %d",
1343 dbg("%s - nonzero urb status received: %d",
1348 usb_serial_debug_data(debug, &port->dev, __func__,
1349 urb->actual_length, urb->transfer_buffer);
1351 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1352 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1353 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1355 dbg("%s - bulk data available.", __func__);
1357 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1359 /* bulk data available */
1360 usb_fill_bulk_urb(port->read_urb, serial->dev,
1361 usb_rcvbulkpipe(serial->dev,
1362 port->bulk_in_endpointAddress),
1363 port->read_urb->transfer_buffer,
1364 port->read_urb->transfer_buffer_length,
1365 garmin_read_bulk_callback, port);
1366 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1369 "%s - failed submitting read urb, error %d\n",
1372 spin_lock_irqsave(&garmin_data_p->lock, flags);
1373 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1374 /* do not send this packet to the user */
1375 garmin_data_p->ignorePkts = 1;
1376 spin_unlock_irqrestore(&garmin_data_p->lock,
1380 /* bulk-in transfer still active */
1381 spin_lock_irqsave(&garmin_data_p->lock, flags);
1382 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1383 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1386 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1387 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1388 sizeof(GARMIN_START_SESSION_REPLY))) {
1390 spin_lock_irqsave(&garmin_data_p->lock, flags);
1391 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1392 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1394 /* save the serial number */
1395 garmin_data_p->serial_num = __le32_to_cpup(
1396 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
1398 dbg("%s - start-of-session reply seen - serial %u.",
1399 __func__, garmin_data_p->serial_num);
1402 if (garmin_data_p->ignorePkts) {
1403 /* this reply belongs to a request generated by the driver,
1405 dbg("%s - pkt ignored (%d)",
1406 __func__, garmin_data_p->ignorePkts);
1407 spin_lock_irqsave(&garmin_data_p->lock, flags);
1408 garmin_data_p->ignorePkts--;
1409 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1411 garmin_read_process(garmin_data_p, data, urb->actual_length);
1414 port->interrupt_in_urb->dev = port->serial->dev;
1415 retval = usb_submit_urb(urb, GFP_ATOMIC);
1417 dev_err(&urb->dev->dev,
1418 "%s - Error %d submitting interrupt urb\n",
1424 * Sends the next queued packt to the tty port (garmin native mode only)
1425 * and then sets a timer to call itself again until all queued data
1428 static int garmin_flush_queue(struct garmin_data *garmin_data_p)
1430 unsigned long flags;
1431 struct garmin_packet *pkt;
1433 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1434 pkt = pkt_pop(garmin_data_p);
1436 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1438 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1441 spin_lock_irqsave(&garmin_data_p->lock, flags);
1442 garmin_data_p->flags &= ~FLAGS_QUEUING;
1443 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1450 static void garmin_throttle(struct tty_struct *tty)
1452 struct usb_serial_port *port = tty->driver_data;
1453 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1454 unsigned long flags;
1456 dbg("%s - port %d", __func__, port->number);
1457 /* set flag, data received will be put into a queue
1458 for later processing */
1459 spin_lock_irqsave(&garmin_data_p->lock, flags);
1460 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1461 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1465 static void garmin_unthrottle(struct tty_struct *tty)
1467 struct usb_serial_port *port = tty->driver_data;
1468 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1469 unsigned long flags;
1472 dbg("%s - port %d", __func__, port->number);
1473 spin_lock_irqsave(&garmin_data_p->lock, flags);
1474 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1475 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1477 /* in native mode send queued data to tty, in
1478 serial mode nothing needs to be done here */
1479 if (garmin_data_p->mode == MODE_NATIVE)
1480 garmin_flush_queue(garmin_data_p);
1482 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1483 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1486 "%s - failed resubmitting read urb, error %d\n",
1492 * The timer is currently only used to send queued packets to
1493 * the tty in cases where the protocol provides no own handshaking
1494 * to initiate the transfer.
1496 static void timeout_handler(unsigned long data)
1498 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1500 /* send the next queued packet to the tty port */
1501 if (garmin_data_p->mode == MODE_NATIVE)
1502 if (garmin_data_p->flags & FLAGS_QUEUING)
1503 garmin_flush_queue(garmin_data_p);
1508 static int garmin_attach(struct usb_serial *serial)
1511 struct usb_serial_port *port = serial->port[0];
1512 struct garmin_data *garmin_data_p = NULL;
1514 dbg("%s", __func__);
1516 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1517 if (garmin_data_p == NULL) {
1518 dev_err(&port->dev, "%s - Out of memory\n", __func__);
1521 init_timer(&garmin_data_p->timer);
1522 spin_lock_init(&garmin_data_p->lock);
1523 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1524 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1525 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1526 garmin_data_p->timer.function = timeout_handler;
1527 garmin_data_p->port = port;
1528 garmin_data_p->state = 0;
1529 garmin_data_p->count = 0;
1530 usb_set_serial_port_data(port, garmin_data_p);
1532 status = garmin_init_session(port);
1538 static void garmin_shutdown(struct usb_serial *serial)
1540 struct usb_serial_port *port = serial->port[0];
1541 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1543 dbg("%s", __func__);
1545 usb_kill_urb(port->interrupt_in_urb);
1546 del_timer_sync(&garmin_data_p->timer);
1547 kfree(garmin_data_p);
1548 usb_set_serial_port_data(port, NULL);
1552 /* All of the device info needed */
1553 static struct usb_serial_driver garmin_device = {
1555 .owner = THIS_MODULE,
1556 .name = "garmin_gps",
1558 .description = "Garmin GPS usb/tty",
1559 .usb_driver = &garmin_driver,
1560 .id_table = id_table,
1562 .open = garmin_open,
1563 .close = garmin_close,
1564 .throttle = garmin_throttle,
1565 .unthrottle = garmin_unthrottle,
1566 .attach = garmin_attach,
1567 .shutdown = garmin_shutdown,
1568 .write = garmin_write,
1569 .write_room = garmin_write_room,
1570 .write_bulk_callback = garmin_write_bulk_callback,
1571 .read_bulk_callback = garmin_read_bulk_callback,
1572 .read_int_callback = garmin_read_int_callback,
1577 static int __init garmin_init(void)
1581 retval = usb_serial_register(&garmin_device);
1583 goto failed_garmin_register;
1584 retval = usb_register(&garmin_driver);
1586 goto failed_usb_register;
1587 info(DRIVER_DESC " " DRIVER_VERSION);
1590 failed_usb_register:
1591 usb_serial_deregister(&garmin_device);
1592 failed_garmin_register:
1597 static void __exit garmin_exit(void)
1599 usb_deregister(&garmin_driver);
1600 usb_serial_deregister(&garmin_device);
1606 module_init(garmin_init);
1607 module_exit(garmin_exit);
1609 MODULE_AUTHOR(DRIVER_AUTHOR);
1610 MODULE_DESCRIPTION(DRIVER_DESC);
1611 MODULE_LICENSE("GPL");
1613 module_param(debug, bool, S_IWUSR | S_IRUGO);
1614 MODULE_PARM_DESC(debug, "Debug enabled or not");
1615 module_param(initial_mode, int, S_IRUGO);
1616 MODULE_PARM_DESC(initial_mode, "Initial mode");