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 = tty_port_tty_get(&port->port);
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);
294 /******************************************************************************
295 * packet queue handling
296 ******************************************************************************/
299 * queue a received (usb-)packet for later processing
301 static int pkt_add(struct garmin_data *garmin_data_p,
302 unsigned char *data, unsigned int data_length)
307 struct garmin_packet *pkt;
309 /* process only packets containg data ... */
311 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
314 dev_err(&garmin_data_p->port->dev, "out of memory\n");
317 pkt->size = data_length;
318 memcpy(pkt->data, data, data_length);
320 spin_lock_irqsave(&garmin_data_p->lock, flags);
321 garmin_data_p->flags |= FLAGS_QUEUING;
322 result = list_empty(&garmin_data_p->pktlist);
323 pkt->seq = garmin_data_p->seq_counter++;
324 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
325 state = garmin_data_p->state;
326 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
328 /* in serial mode, if someone is waiting for data from
329 the device, iconvert and send the next packet to tty. */
330 if (result && (state == STATE_GSP_WAIT_DATA))
331 gsp_next_packet(garmin_data_p);
337 /* get the next pending packet */
338 static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
341 struct garmin_packet *result = NULL;
343 spin_lock_irqsave(&garmin_data_p->lock, flags);
344 if (!list_empty(&garmin_data_p->pktlist)) {
345 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
346 list_del(&result->list);
348 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
353 /* free up all queued data */
354 static void pkt_clear(struct garmin_data *garmin_data_p)
357 struct garmin_packet *result = NULL;
361 spin_lock_irqsave(&garmin_data_p->lock, flags);
362 while (!list_empty(&garmin_data_p->pktlist)) {
363 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
364 list_del(&result->list);
367 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
371 /******************************************************************************
372 * garmin serial protocol handling handling
373 ******************************************************************************/
375 /* send an ack packet back to the tty */
376 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
383 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
399 *ptr++ = 0xFF & (-cksum);
405 send_to_tty(garmin_data_p->port, pkt, l);
412 * called for a complete packet received from tty layer
414 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
415 * at GSP_INITIAL_OFFSET.
417 * count - number of bytes in the input buffer including space reserved for
418 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
419 * (including pkt-id, data-length a. cksum)
421 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
423 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
424 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
428 int pktid = recpkt[0];
429 int size = recpkt[1];
431 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
432 __func__, count-GSP_INITIAL_OFFSET, recpkt);
434 if (size != (count-GSP_INITIAL_OFFSET-3)) {
435 dbg("%s - invalid size, expected %d bytes, got %d",
436 __func__, size, (count-GSP_INITIAL_OFFSET-3));
443 /* sanity check, remove after test ... */
444 if ((__u8 *)&(usbdata[3]) != recpkt) {
445 dbg("%s - ptr mismatch %p - %p",
446 __func__, &(usbdata[4]), recpkt);
455 if ((0xff & (cksum + *recpkt)) != 0) {
456 dbg("%s - invalid checksum, expected %02x, got %02x",
457 __func__, 0xff & -cksum, 0xff & *recpkt);
461 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
462 usbdata[1] = __cpu_to_le32(pktid);
463 usbdata[2] = __cpu_to_le32(size);
465 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
466 GARMIN_PKTHDR_LENGTH+size, 0);
468 /* if this was an abort-transfer command, flush all
470 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
471 garmin_data_p->flags |= FLAGS_DROP_DATA;
472 pkt_clear(garmin_data_p);
481 * Called for data received from tty
483 * buf contains the data read, it may span more than one packet or even
486 * input record should be a serial-record, but it may not be complete.
487 * Copy it into our local buffer, until an etx is seen (or an error
489 * Once the record is complete, convert into a usb packet and send it
490 * to the bulk pipe, send an ack back to the tty.
492 * If the input is an ack, just send the last queued packet to the
495 * if the input is an abort command, drop all queued data.
498 static int gsp_receive(struct garmin_data *garmin_data_p,
499 const unsigned char *buf, int count)
503 int ack_or_nak_seen = 0;
507 /* dleSeen: set if last byte read was a DLE */
509 /* skip: if set, skip incoming data until possible start of
515 spin_lock_irqsave(&garmin_data_p->lock, flags);
516 dest = garmin_data_p->inbuffer;
517 size = garmin_data_p->insize;
518 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
519 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
520 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
522 dbg("%s - dle=%d skip=%d size=%d count=%d",
523 __func__, dleSeen, skip, size, count);
526 size = GSP_INITIAL_OFFSET;
528 while (offs < count) {
534 if (skip) { /* start of a new pkt */
536 size = GSP_INITIAL_OFFSET;
538 } else if (dleSeen) {
544 } else if (data == ETX) {
546 /* packet complete */
548 data = dest[GSP_INITIAL_OFFSET];
551 ack_or_nak_seen = ACK;
552 dbg("ACK packet complete.");
553 } else if (data == NAK) {
554 ack_or_nak_seen = NAK;
555 dbg("NAK packet complete.");
557 dbg("packet complete - id=0x%X.",
559 gsp_rec_packet(garmin_data_p, size);
563 size = GSP_INITIAL_OFFSET;
571 dbg("non-masked DLE at %d - restarting", i);
572 size = GSP_INITIAL_OFFSET;
579 if (size >= GPS_IN_BUFSIZ) {
580 dbg("%s - packet too large.", __func__);
582 size = GSP_INITIAL_OFFSET;
587 spin_lock_irqsave(&garmin_data_p->lock, flags);
589 garmin_data_p->insize = size;
591 /* copy flags back to structure */
593 garmin_data_p->flags |= FLAGS_GSP_SKIP;
595 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
598 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
600 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
603 garmin_data_p->state = STATE_GSP_WAIT_DATA;
605 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
608 gsp_next_packet(garmin_data_p);
616 * Sends a usb packet to the tty
618 * Assumes, that all packages and at an usb-packet boundary.
620 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
622 static int gsp_send(struct garmin_data *garmin_data_p,
623 const unsigned char *buf, int count)
625 const unsigned char *src;
633 dbg("%s - state %d - %d bytes.", __func__,
634 garmin_data_p->state, count);
636 k = garmin_data_p->outsize;
637 if ((k+count) > GPS_OUT_BUFSIZ) {
638 dbg("packet too large");
639 garmin_data_p->outsize = 0;
643 memcpy(garmin_data_p->outbuffer+k, buf, count);
645 garmin_data_p->outsize = k;
647 if (k >= GARMIN_PKTHDR_LENGTH) {
648 pktid = getPacketId(garmin_data_p->outbuffer);
649 datalen = getDataLength(garmin_data_p->outbuffer);
650 i = GARMIN_PKTHDR_LENGTH + datalen;
657 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
659 /* garmin_data_p->outbuffer now contains a complete packet */
661 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
662 __func__, k, garmin_data_p->outbuffer);
664 garmin_data_p->outsize = 0;
666 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
667 dbg("not an application packet (%d)",
668 getLayerId(garmin_data_p->outbuffer));
673 dbg("packet-id %d too large", pktid);
678 dbg("packet-size %d too large", datalen);
682 /* the serial protocol should be able to handle this packet */
685 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
686 for (i = 0; i < datalen; i++) {
691 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
692 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
693 /* can't add stuffing DLEs in place, move data to end
695 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
696 memcpy(dst, src, datalen);
700 dst = garmin_data_p->outbuffer;
710 for (i = 0; i < datalen; i++) {
718 cksum = 0xFF & -cksum;
725 i = dst-garmin_data_p->outbuffer;
727 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
729 garmin_data_p->pkt_id = pktid;
730 garmin_data_p->state = STATE_WAIT_TTY_ACK;
740 * Process the next pending data packet - if there is one
742 static void gsp_next_packet(struct garmin_data *garmin_data_p)
744 struct garmin_packet *pkt = NULL;
746 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
747 dbg("%s - next pkt: %d", __func__, pkt->seq);
748 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
759 /******************************************************************************
761 ******************************************************************************/
765 * Called for data received from tty
767 * The input data is expected to be in garmin usb-packet format.
769 * buf contains the data read, it may span more than one packet
770 * or even incomplete packets
772 static int nat_receive(struct garmin_data *garmin_data_p,
773 const unsigned char *buf, int count)
781 while (offs < count) {
782 /* if buffer contains header, copy rest of data */
783 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
784 len = GARMIN_PKTHDR_LENGTH
785 +getDataLength(garmin_data_p->inbuffer);
787 len = GARMIN_PKTHDR_LENGTH;
789 if (len >= GPS_IN_BUFSIZ) {
790 /* seems to be an invalid packet, ignore rest
792 dbg("%s - packet size too large: %d", __func__, len);
793 garmin_data_p->insize = 0;
797 len -= garmin_data_p->insize;
798 if (len > (count-offs))
801 dest = garmin_data_p->inbuffer
802 + garmin_data_p->insize;
803 memcpy(dest, buf+offs, len);
804 garmin_data_p->insize += len;
809 /* do we have a complete packet ? */
810 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
811 len = GARMIN_PKTHDR_LENGTH+
812 getDataLength(garmin_data_p->inbuffer);
813 if (garmin_data_p->insize >= len) {
814 garmin_write_bulk(garmin_data_p->port,
815 garmin_data_p->inbuffer,
817 garmin_data_p->insize = 0;
819 /* if this was an abort-transfer command,
820 flush all queued data. */
821 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
822 spin_lock_irqsave(&garmin_data_p->lock,
824 garmin_data_p->flags |= FLAGS_DROP_DATA;
825 spin_unlock_irqrestore(
826 &garmin_data_p->lock, flags);
827 pkt_clear(garmin_data_p);
836 /******************************************************************************
838 ******************************************************************************/
840 static void priv_status_resp(struct usb_serial_port *port)
842 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
843 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
845 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
846 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
847 pkt[2] = __cpu_to_le32(12);
848 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
849 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
850 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
852 send_to_tty(port, (__u8 *)pkt, 6 * 4);
856 /******************************************************************************
857 * Garmin specific driver functions
858 ******************************************************************************/
860 static int process_resetdev_request(struct usb_serial_port *port)
864 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
866 spin_lock_irqsave(&garmin_data_p->lock, flags);
867 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
868 garmin_data_p->state = STATE_RESET;
869 garmin_data_p->serial_num = 0;
870 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
872 usb_kill_urb(port->interrupt_in_urb);
873 dbg("%s - usb_reset_device", __func__);
874 status = usb_reset_device(port->serial->dev);
876 dbg("%s - usb_reset_device failed: %d",
884 * clear all cached data
886 static int garmin_clear(struct garmin_data *garmin_data_p)
891 struct usb_serial_port *port = garmin_data_p->port;
893 if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
894 /* send a terminate command */
895 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
896 sizeof(GARMIN_STOP_TRANSFER_REQ), 1);
899 /* flush all queued data */
900 pkt_clear(garmin_data_p);
902 spin_lock_irqsave(&garmin_data_p->lock, flags);
903 garmin_data_p->insize = 0;
904 garmin_data_p->outsize = 0;
905 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
915 static int garmin_init_session(struct usb_serial_port *port)
918 struct usb_serial *serial = port->serial;
919 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
923 usb_kill_urb(port->interrupt_in_urb);
925 dbg("%s - adding interrupt input", __func__);
926 port->interrupt_in_urb->dev = serial->dev;
927 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
929 dev_err(&serial->dev->dev,
930 "%s - failed submitting interrupt urb, error %d\n",
935 dbg("%s - starting session ...", __func__);
936 garmin_data_p->state = STATE_ACTIVE;
937 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
938 sizeof(GARMIN_START_SESSION_REQ), 0);
942 spin_lock_irqsave(&garmin_data_p->lock, flags);
943 garmin_data_p->ignorePkts++;
944 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
946 /* not needed, but the win32 driver does it too ... */
947 status = garmin_write_bulk(port,
948 GARMIN_START_SESSION_REQ2,
949 sizeof(GARMIN_START_SESSION_REQ2), 0);
952 spin_lock_irqsave(&garmin_data_p->lock, flags);
953 garmin_data_p->ignorePkts++;
954 spin_unlock_irqrestore(&garmin_data_p->lock,
967 static int garmin_open(struct tty_struct *tty,
968 struct usb_serial_port *port, struct file *filp)
972 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
974 dbg("%s - port %d", __func__, port->number);
977 * Force low_latency on so that our tty_push actually forces the data
978 * through, otherwise it is scheduled, and with high data rates (like
979 * with OHCI) data can get lost.
982 tty->low_latency = 1;
984 spin_lock_irqsave(&garmin_data_p->lock, flags);
985 garmin_data_p->mode = initial_mode;
986 garmin_data_p->count = 0;
987 garmin_data_p->flags = 0;
988 atomic_set(&garmin_data_p->req_count, 0);
989 atomic_set(&garmin_data_p->resp_count, 0);
990 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
992 /* shutdown any bulk reads that might be going on */
993 usb_kill_urb(port->write_urb);
994 usb_kill_urb(port->read_urb);
996 if (garmin_data_p->state == STATE_RESET)
997 status = garmin_init_session(port);
999 garmin_data_p->state = STATE_ACTIVE;
1004 static void garmin_close(struct tty_struct *tty,
1005 struct usb_serial_port *port, struct file *filp)
1007 struct usb_serial *serial = port->serial;
1008 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1010 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
1011 port->number, garmin_data_p->mode,
1012 garmin_data_p->state, garmin_data_p->flags);
1017 mutex_lock(&port->serial->disc_mutex);
1018 if (!port->serial->disconnected)
1019 garmin_clear(garmin_data_p);
1021 /* shutdown our urbs */
1022 usb_kill_urb(port->read_urb);
1023 usb_kill_urb(port->write_urb);
1025 if (!port->serial->disconnected) {
1026 if (noResponseFromAppLayer(garmin_data_p) ||
1027 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1028 process_resetdev_request(port);
1029 garmin_data_p->state = STATE_RESET;
1031 garmin_data_p->state = STATE_DISCONNECTED;
1034 garmin_data_p->state = STATE_DISCONNECTED;
1036 mutex_unlock(&port->serial->disc_mutex);
1039 static void garmin_write_bulk_callback(struct urb *urb)
1041 unsigned long flags;
1042 struct usb_serial_port *port = urb->context;
1043 int status = urb->status;
1046 struct garmin_data *garmin_data_p =
1047 usb_get_serial_port_data(port);
1049 dbg("%s - port %d", __func__, port->number);
1051 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)
1052 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1053 gsp_send_ack(garmin_data_p,
1054 ((__u8 *)urb->transfer_buffer)[4]);
1058 dbg("%s - nonzero write bulk status received: %d",
1059 __func__, urb->status);
1060 spin_lock_irqsave(&garmin_data_p->lock, flags);
1061 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1062 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1065 usb_serial_port_softint(port);
1068 /* Ignore errors that resulted from garmin_write_bulk with
1071 /* free up the transfer buffer, as usb_free_urb() does not do this */
1072 kfree(urb->transfer_buffer);
1076 static int garmin_write_bulk(struct usb_serial_port *port,
1077 const unsigned char *buf, int count,
1080 unsigned long flags;
1081 struct usb_serial *serial = port->serial;
1082 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1084 unsigned char *buffer;
1087 dbg("%s - port %d, state %d", __func__, port->number,
1088 garmin_data_p->state);
1090 spin_lock_irqsave(&garmin_data_p->lock, flags);
1091 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1092 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1094 buffer = kmalloc(count, GFP_ATOMIC);
1096 dev_err(&port->dev, "out of memory\n");
1100 urb = usb_alloc_urb(0, GFP_ATOMIC);
1102 dev_err(&port->dev, "no more free urbs\n");
1107 memcpy(buffer, buf, count);
1109 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
1111 usb_fill_bulk_urb(urb, serial->dev,
1112 usb_sndbulkpipe(serial->dev,
1113 port->bulk_out_endpointAddress),
1115 garmin_write_bulk_callback,
1116 dismiss_ack ? NULL : port);
1117 urb->transfer_flags |= URB_ZERO_PACKET;
1119 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1120 atomic_inc(&garmin_data_p->req_count);
1121 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1122 pkt_clear(garmin_data_p);
1123 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1127 /* send it down the pipe */
1128 status = usb_submit_urb(urb, GFP_ATOMIC);
1131 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1136 /* we are done with this urb, so let the host driver
1137 * really free it when it is finished with it */
1143 static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
1144 const unsigned char *buf, int count)
1146 int pktid, pktsiz, len;
1147 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1148 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1150 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
1152 /* check for our private packets */
1153 if (count >= GARMIN_PKTHDR_LENGTH) {
1158 memcpy(garmin_data_p->privpkt, buf, len);
1160 pktsiz = getDataLength(garmin_data_p->privpkt);
1161 pktid = getPacketId(garmin_data_p->privpkt);
1163 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1164 && GARMIN_LAYERID_PRIVATE ==
1165 getLayerId(garmin_data_p->privpkt)) {
1167 dbg("%s - processing private request %d",
1170 /* drop all unfinished transfers */
1171 garmin_clear(garmin_data_p);
1175 case PRIV_PKTID_SET_DEBUG:
1178 debug = __le32_to_cpu(privpkt[3]);
1179 dbg("%s - debug level set to 0x%X",
1183 case PRIV_PKTID_SET_MODE:
1186 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1187 dbg("%s - mode set to %d",
1188 __func__, garmin_data_p->mode);
1191 case PRIV_PKTID_INFO_REQ:
1192 priv_status_resp(port);
1195 case PRIV_PKTID_RESET_REQ:
1196 atomic_inc(&garmin_data_p->req_count);
1199 case PRIV_PKTID_SET_DEF_MODE:
1202 initial_mode = __le32_to_cpu(privpkt[3]);
1203 dbg("%s - initial_mode set to %d",
1205 garmin_data_p->mode);
1212 garmin_data_p->ignorePkts = 0;
1214 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1215 return gsp_receive(garmin_data_p, buf, count);
1216 } else { /* MODE_NATIVE */
1217 return nat_receive(garmin_data_p, buf, count);
1222 static int garmin_write_room(struct tty_struct *tty)
1224 struct usb_serial_port *port = tty->driver_data;
1226 * Report back the bytes currently available in the output buffer.
1228 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1229 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1233 static void garmin_read_process(struct garmin_data *garmin_data_p,
1234 unsigned char *data, unsigned data_length)
1236 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1237 /* abort-transfer cmd is actice */
1238 dbg("%s - pkt dropped", __func__);
1239 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1240 garmin_data_p->state != STATE_RESET) {
1242 /* remember any appl.layer packets, so we know
1243 if a reset is required or not when closing
1245 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1246 sizeof(GARMIN_APP_LAYER_REPLY))) {
1247 atomic_inc(&garmin_data_p->resp_count);
1250 /* if throttling is active or postprecessing is required
1251 put the received data in the input queue, otherwise
1252 send it directly to the tty port */
1253 if (garmin_data_p->flags & FLAGS_QUEUING) {
1254 pkt_add(garmin_data_p, data, data_length);
1255 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1256 if (getLayerId(data) == GARMIN_LAYERID_APPL)
1257 pkt_add(garmin_data_p, data, data_length);
1259 send_to_tty(garmin_data_p->port, data, data_length);
1265 static void garmin_read_bulk_callback(struct urb *urb)
1267 unsigned long flags;
1268 struct usb_serial_port *port = urb->context;
1269 struct usb_serial *serial = port->serial;
1270 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1271 unsigned char *data = urb->transfer_buffer;
1272 int status = urb->status;
1275 dbg("%s - port %d", __func__, port->number);
1278 dbg("%s - bad serial pointer, exiting", __func__);
1283 dbg("%s - nonzero read bulk status received: %d",
1288 usb_serial_debug_data(debug, &port->dev,
1289 __func__, urb->actual_length, data);
1291 garmin_read_process(garmin_data_p, data, urb->actual_length);
1293 if (urb->actual_length == 0 &&
1294 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1295 spin_lock_irqsave(&garmin_data_p->lock, flags);
1296 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1297 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1298 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1301 "%s - failed resubmitting read urb, error %d\n",
1303 } else if (urb->actual_length > 0) {
1304 /* Continue trying to read until nothing more is received */
1305 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1306 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1309 "%s - failed resubmitting read urb, "
1310 "error %d\n", __func__, retval);
1313 dbg("%s - end of bulk data", __func__);
1314 spin_lock_irqsave(&garmin_data_p->lock, flags);
1315 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1316 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1322 static void garmin_read_int_callback(struct urb *urb)
1324 unsigned long flags;
1326 struct usb_serial_port *port = urb->context;
1327 struct usb_serial *serial = port->serial;
1328 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1329 unsigned char *data = urb->transfer_buffer;
1330 int status = urb->status;
1339 /* this urb is terminated, clean up */
1340 dbg("%s - urb shutting down with status: %d",
1344 dbg("%s - nonzero urb status received: %d",
1349 usb_serial_debug_data(debug, &port->dev, __func__,
1350 urb->actual_length, urb->transfer_buffer);
1352 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1353 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1354 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1356 dbg("%s - bulk data available.", __func__);
1358 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1360 /* bulk data available */
1361 usb_fill_bulk_urb(port->read_urb, serial->dev,
1362 usb_rcvbulkpipe(serial->dev,
1363 port->bulk_in_endpointAddress),
1364 port->read_urb->transfer_buffer,
1365 port->read_urb->transfer_buffer_length,
1366 garmin_read_bulk_callback, port);
1367 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1370 "%s - failed submitting read urb, error %d\n",
1373 spin_lock_irqsave(&garmin_data_p->lock, flags);
1374 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1375 /* do not send this packet to the user */
1376 garmin_data_p->ignorePkts = 1;
1377 spin_unlock_irqrestore(&garmin_data_p->lock,
1381 /* bulk-in transfer still active */
1382 spin_lock_irqsave(&garmin_data_p->lock, flags);
1383 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1384 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1387 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1388 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1389 sizeof(GARMIN_START_SESSION_REPLY))) {
1391 spin_lock_irqsave(&garmin_data_p->lock, flags);
1392 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1393 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1395 /* save the serial number */
1396 garmin_data_p->serial_num = __le32_to_cpup(
1397 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
1399 dbg("%s - start-of-session reply seen - serial %u.",
1400 __func__, garmin_data_p->serial_num);
1403 if (garmin_data_p->ignorePkts) {
1404 /* this reply belongs to a request generated by the driver,
1406 dbg("%s - pkt ignored (%d)",
1407 __func__, garmin_data_p->ignorePkts);
1408 spin_lock_irqsave(&garmin_data_p->lock, flags);
1409 garmin_data_p->ignorePkts--;
1410 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1412 garmin_read_process(garmin_data_p, data, urb->actual_length);
1415 port->interrupt_in_urb->dev = port->serial->dev;
1416 retval = usb_submit_urb(urb, GFP_ATOMIC);
1418 dev_err(&urb->dev->dev,
1419 "%s - Error %d submitting interrupt urb\n",
1425 * Sends the next queued packt to the tty port (garmin native mode only)
1426 * and then sets a timer to call itself again until all queued data
1429 static int garmin_flush_queue(struct garmin_data *garmin_data_p)
1431 unsigned long flags;
1432 struct garmin_packet *pkt;
1434 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1435 pkt = pkt_pop(garmin_data_p);
1437 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1439 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1442 spin_lock_irqsave(&garmin_data_p->lock, flags);
1443 garmin_data_p->flags &= ~FLAGS_QUEUING;
1444 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1451 static void garmin_throttle(struct tty_struct *tty)
1453 struct usb_serial_port *port = tty->driver_data;
1454 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1455 unsigned long flags;
1457 dbg("%s - port %d", __func__, port->number);
1458 /* set flag, data received will be put into a queue
1459 for later processing */
1460 spin_lock_irqsave(&garmin_data_p->lock, flags);
1461 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1462 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1466 static void garmin_unthrottle(struct tty_struct *tty)
1468 struct usb_serial_port *port = tty->driver_data;
1469 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1470 unsigned long flags;
1473 dbg("%s - port %d", __func__, port->number);
1474 spin_lock_irqsave(&garmin_data_p->lock, flags);
1475 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1476 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1478 /* in native mode send queued data to tty, in
1479 serial mode nothing needs to be done here */
1480 if (garmin_data_p->mode == MODE_NATIVE)
1481 garmin_flush_queue(garmin_data_p);
1483 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1484 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1487 "%s - failed resubmitting read urb, error %d\n",
1493 * The timer is currently only used to send queued packets to
1494 * the tty in cases where the protocol provides no own handshaking
1495 * to initiate the transfer.
1497 static void timeout_handler(unsigned long data)
1499 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1501 /* send the next queued packet to the tty port */
1502 if (garmin_data_p->mode == MODE_NATIVE)
1503 if (garmin_data_p->flags & FLAGS_QUEUING)
1504 garmin_flush_queue(garmin_data_p);
1509 static int garmin_attach(struct usb_serial *serial)
1512 struct usb_serial_port *port = serial->port[0];
1513 struct garmin_data *garmin_data_p = NULL;
1515 dbg("%s", __func__);
1517 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1518 if (garmin_data_p == NULL) {
1519 dev_err(&port->dev, "%s - Out of memory\n", __func__);
1522 init_timer(&garmin_data_p->timer);
1523 spin_lock_init(&garmin_data_p->lock);
1524 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1525 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1526 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1527 garmin_data_p->timer.function = timeout_handler;
1528 garmin_data_p->port = port;
1529 garmin_data_p->state = 0;
1530 garmin_data_p->count = 0;
1531 usb_set_serial_port_data(port, garmin_data_p);
1533 status = garmin_init_session(port);
1539 static void garmin_shutdown(struct usb_serial *serial)
1541 struct usb_serial_port *port = serial->port[0];
1542 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1544 dbg("%s", __func__);
1546 usb_kill_urb(port->interrupt_in_urb);
1547 del_timer_sync(&garmin_data_p->timer);
1548 kfree(garmin_data_p);
1549 usb_set_serial_port_data(port, NULL);
1553 /* All of the device info needed */
1554 static struct usb_serial_driver garmin_device = {
1556 .owner = THIS_MODULE,
1557 .name = "garmin_gps",
1559 .description = "Garmin GPS usb/tty",
1560 .usb_driver = &garmin_driver,
1561 .id_table = id_table,
1563 .open = garmin_open,
1564 .close = garmin_close,
1565 .throttle = garmin_throttle,
1566 .unthrottle = garmin_unthrottle,
1567 .attach = garmin_attach,
1568 .shutdown = garmin_shutdown,
1569 .write = garmin_write,
1570 .write_room = garmin_write_room,
1571 .write_bulk_callback = garmin_write_bulk_callback,
1572 .read_bulk_callback = garmin_read_bulk_callback,
1573 .read_int_callback = garmin_read_int_callback,
1578 static int __init garmin_init(void)
1582 retval = usb_serial_register(&garmin_device);
1584 goto failed_garmin_register;
1585 retval = usb_register(&garmin_driver);
1587 goto failed_usb_register;
1588 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1592 failed_usb_register:
1593 usb_serial_deregister(&garmin_device);
1594 failed_garmin_register:
1599 static void __exit garmin_exit(void)
1601 usb_deregister(&garmin_driver);
1602 usb_serial_deregister(&garmin_device);
1608 module_init(garmin_init);
1609 module_exit(garmin_exit);
1611 MODULE_AUTHOR(DRIVER_AUTHOR);
1612 MODULE_DESCRIPTION(DRIVER_DESC);
1613 MODULE_LICENSE("GPL");
1615 module_param(debug, bool, S_IWUSR | S_IRUGO);
1616 MODULE_PARM_DESC(debug, "Debug enabled or not");
1617 module_param(initial_mode, int, S_IRUGO);
1618 MODULE_PARM_DESC(initial_mode, "Initial mode");