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
)
256 usb_serial_debug_data(&port
->dev
, __func__
, actual_length
, data
);
257 tty_insert_flip_string(&port
->port
, data
, actual_length
);
258 tty_flip_buffer_push(&port
->port
);
263 /******************************************************************************
264 * packet queue handling
265 ******************************************************************************/
268 * queue a received (usb-)packet for later processing
270 static int pkt_add(struct garmin_data
*garmin_data_p
,
271 unsigned char *data
, unsigned int data_length
)
276 struct garmin_packet
*pkt
;
278 /* process only packets containg data ... */
280 pkt
= kmalloc(sizeof(struct garmin_packet
)+data_length
,
283 dev_err(&garmin_data_p
->port
->dev
, "out of memory\n");
286 pkt
->size
= data_length
;
287 memcpy(pkt
->data
, data
, data_length
);
289 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
290 garmin_data_p
->flags
|= FLAGS_QUEUING
;
291 result
= list_empty(&garmin_data_p
->pktlist
);
292 pkt
->seq
= garmin_data_p
->seq_counter
++;
293 list_add_tail(&pkt
->list
, &garmin_data_p
->pktlist
);
294 state
= garmin_data_p
->state
;
295 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
297 dev_dbg(&garmin_data_p
->port
->dev
,
298 "%s - added: pkt: %d - %d bytes\n", __func__
,
299 pkt
->seq
, data_length
);
301 /* in serial mode, if someone is waiting for data from
302 the device, convert and send the next packet to tty. */
303 if (result
&& (state
== STATE_GSP_WAIT_DATA
))
304 gsp_next_packet(garmin_data_p
);
310 /* get the next pending packet */
311 static struct garmin_packet
*pkt_pop(struct garmin_data
*garmin_data_p
)
314 struct garmin_packet
*result
= NULL
;
316 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
317 if (!list_empty(&garmin_data_p
->pktlist
)) {
318 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
319 list_del(&result
->list
);
321 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
326 /* free up all queued data */
327 static void pkt_clear(struct garmin_data
*garmin_data_p
)
330 struct garmin_packet
*result
= NULL
;
332 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
333 while (!list_empty(&garmin_data_p
->pktlist
)) {
334 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
335 list_del(&result
->list
);
338 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
342 /******************************************************************************
343 * garmin serial protocol handling handling
344 ******************************************************************************/
346 /* send an ack packet back to the tty */
347 static int gsp_send_ack(struct garmin_data
*garmin_data_p
, __u8 pkt_id
)
354 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt-id: 0x%X.\n", __func__
,
371 *ptr
++ = 0xFF & (-cksum
);
377 send_to_tty(garmin_data_p
->port
, pkt
, l
);
384 * called for a complete packet received from tty layer
386 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
387 * at GSP_INITIAL_OFFSET.
389 * count - number of bytes in the input buffer including space reserved for
390 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
391 * (including pkt-id, data-length a. cksum)
393 static int gsp_rec_packet(struct garmin_data
*garmin_data_p
, int count
)
395 struct device
*dev
= &garmin_data_p
->port
->dev
;
397 const __u8
*recpkt
= garmin_data_p
->inbuffer
+GSP_INITIAL_OFFSET
;
398 __le32
*usbdata
= (__le32
*) garmin_data_p
->inbuffer
;
401 int pktid
= recpkt
[0];
402 int size
= recpkt
[1];
404 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
,
405 count
-GSP_INITIAL_OFFSET
, recpkt
);
407 if (size
!= (count
-GSP_INITIAL_OFFSET
-3)) {
408 dev_dbg(dev
, "%s - invalid size, expected %d bytes, got %d\n",
409 __func__
, size
, (count
-GSP_INITIAL_OFFSET
-3));
416 /* sanity check, remove after test ... */
417 if ((__u8
*)&(usbdata
[3]) != recpkt
) {
418 dev_dbg(dev
, "%s - ptr mismatch %p - %p\n", __func__
,
419 &(usbdata
[4]), recpkt
);
428 if ((0xff & (cksum
+ *recpkt
)) != 0) {
429 dev_dbg(dev
, "%s - invalid checksum, expected %02x, got %02x\n",
430 __func__
, 0xff & -cksum
, 0xff & *recpkt
);
434 usbdata
[0] = __cpu_to_le32(GARMIN_LAYERID_APPL
);
435 usbdata
[1] = __cpu_to_le32(pktid
);
436 usbdata
[2] = __cpu_to_le32(size
);
438 garmin_write_bulk(garmin_data_p
->port
, garmin_data_p
->inbuffer
,
439 GARMIN_PKTHDR_LENGTH
+size
, 0);
441 /* if this was an abort-transfer command, flush all
443 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
444 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
445 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
446 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
447 pkt_clear(garmin_data_p
);
456 * Called for data received from tty
458 * buf contains the data read, it may span more than one packet or even
461 * input record should be a serial-record, but it may not be complete.
462 * Copy it into our local buffer, until an etx is seen (or an error
464 * Once the record is complete, convert into a usb packet and send it
465 * to the bulk pipe, send an ack back to the tty.
467 * If the input is an ack, just send the last queued packet to the
470 * if the input is an abort command, drop all queued data.
473 static int gsp_receive(struct garmin_data
*garmin_data_p
,
474 const unsigned char *buf
, int count
)
476 struct device
*dev
= &garmin_data_p
->port
->dev
;
479 int ack_or_nak_seen
= 0;
482 /* dleSeen: set if last byte read was a DLE */
484 /* skip: if set, skip incoming data until possible start of
490 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
491 dest
= garmin_data_p
->inbuffer
;
492 size
= garmin_data_p
->insize
;
493 dleSeen
= garmin_data_p
->flags
& FLAGS_GSP_DLESEEN
;
494 skip
= garmin_data_p
->flags
& FLAGS_GSP_SKIP
;
495 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
497 /* dev_dbg(dev, "%s - dle=%d skip=%d size=%d count=%d\n",
498 __func__, dleSeen, skip, size, count); */
501 size
= GSP_INITIAL_OFFSET
;
503 while (offs
< count
) {
509 if (skip
) { /* start of a new pkt */
511 size
= GSP_INITIAL_OFFSET
;
513 } else if (dleSeen
) {
519 } else if (data
== ETX
) {
521 /* packet complete */
523 data
= dest
[GSP_INITIAL_OFFSET
];
526 ack_or_nak_seen
= ACK
;
527 dev_dbg(dev
, "ACK packet complete.\n");
528 } else if (data
== NAK
) {
529 ack_or_nak_seen
= NAK
;
530 dev_dbg(dev
, "NAK packet complete.\n");
532 dev_dbg(dev
, "packet complete - id=0x%X.\n",
534 gsp_rec_packet(garmin_data_p
, size
);
538 size
= GSP_INITIAL_OFFSET
;
546 size
= GSP_INITIAL_OFFSET
;
553 if (size
>= GPS_IN_BUFSIZ
) {
554 dev_dbg(dev
, "%s - packet too large.\n", __func__
);
556 size
= GSP_INITIAL_OFFSET
;
561 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
563 garmin_data_p
->insize
= size
;
565 /* copy flags back to structure */
567 garmin_data_p
->flags
|= FLAGS_GSP_SKIP
;
569 garmin_data_p
->flags
&= ~FLAGS_GSP_SKIP
;
572 garmin_data_p
->flags
|= FLAGS_GSP_DLESEEN
;
574 garmin_data_p
->flags
&= ~FLAGS_GSP_DLESEEN
;
576 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
578 if (ack_or_nak_seen
) {
579 if (gsp_next_packet(garmin_data_p
) > 0)
580 garmin_data_p
->state
= STATE_ACTIVE
;
582 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
590 * Sends a usb packet to the tty
592 * Assumes, that all packages and at an usb-packet boundary.
594 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
596 static int gsp_send(struct garmin_data
*garmin_data_p
,
597 const unsigned char *buf
, int count
)
599 struct device
*dev
= &garmin_data_p
->port
->dev
;
600 const unsigned char *src
;
608 dev_dbg(dev
, "%s - state %d - %d bytes.\n", __func__
,
609 garmin_data_p
->state
, count
);
611 k
= garmin_data_p
->outsize
;
612 if ((k
+count
) > GPS_OUT_BUFSIZ
) {
613 dev_dbg(dev
, "packet too large\n");
614 garmin_data_p
->outsize
= 0;
618 memcpy(garmin_data_p
->outbuffer
+k
, buf
, count
);
620 garmin_data_p
->outsize
= k
;
622 if (k
>= GARMIN_PKTHDR_LENGTH
) {
623 pktid
= getPacketId(garmin_data_p
->outbuffer
);
624 datalen
= getDataLength(garmin_data_p
->outbuffer
);
625 i
= GARMIN_PKTHDR_LENGTH
+ datalen
;
632 dev_dbg(dev
, "%s - %d bytes in buffer, %d bytes in pkt.\n", __func__
, k
, i
);
634 /* garmin_data_p->outbuffer now contains a complete packet */
636 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
, k
,
637 garmin_data_p
->outbuffer
);
639 garmin_data_p
->outsize
= 0;
641 if (GARMIN_LAYERID_APPL
!= getLayerId(garmin_data_p
->outbuffer
)) {
642 dev_dbg(dev
, "not an application packet (%d)\n",
643 getLayerId(garmin_data_p
->outbuffer
));
648 dev_dbg(dev
, "packet-id %d too large\n", pktid
);
653 dev_dbg(dev
, "packet-size %d too large\n", datalen
);
657 /* the serial protocol should be able to handle this packet */
660 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
661 for (i
= 0; i
< datalen
; i
++) {
666 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
667 if (k
> (GARMIN_PKTHDR_LENGTH
-2)) {
668 /* can't add stuffing DLEs in place, move data to end
670 dst
= garmin_data_p
->outbuffer
+GPS_OUT_BUFSIZ
-datalen
;
671 memcpy(dst
, src
, datalen
);
675 dst
= garmin_data_p
->outbuffer
;
685 for (i
= 0; i
< datalen
; i
++) {
693 cksum
= 0xFF & -cksum
;
700 i
= dst
-garmin_data_p
->outbuffer
;
702 send_to_tty(garmin_data_p
->port
, garmin_data_p
->outbuffer
, i
);
704 garmin_data_p
->pkt_id
= pktid
;
705 garmin_data_p
->state
= STATE_WAIT_TTY_ACK
;
712 * Process the next pending data packet - if there is one
714 static int gsp_next_packet(struct garmin_data
*garmin_data_p
)
717 struct garmin_packet
*pkt
= NULL
;
719 while ((pkt
= pkt_pop(garmin_data_p
)) != NULL
) {
720 dev_dbg(&garmin_data_p
->port
->dev
, "%s - next pkt: %d\n", __func__
, pkt
->seq
);
721 result
= gsp_send(garmin_data_p
, pkt
->data
, pkt
->size
);
733 /******************************************************************************
735 ******************************************************************************/
739 * Called for data received from tty
741 * The input data is expected to be in garmin usb-packet format.
743 * buf contains the data read, it may span more than one packet
744 * or even incomplete packets
746 static int nat_receive(struct garmin_data
*garmin_data_p
,
747 const unsigned char *buf
, int count
)
755 while (offs
< count
) {
756 /* if buffer contains header, copy rest of data */
757 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
)
758 len
= GARMIN_PKTHDR_LENGTH
759 +getDataLength(garmin_data_p
->inbuffer
);
761 len
= GARMIN_PKTHDR_LENGTH
;
763 if (len
>= GPS_IN_BUFSIZ
) {
764 /* seems to be an invalid packet, ignore rest
766 dev_dbg(&garmin_data_p
->port
->dev
,
767 "%s - packet size too large: %d\n",
769 garmin_data_p
->insize
= 0;
773 len
-= garmin_data_p
->insize
;
774 if (len
> (count
-offs
))
777 dest
= garmin_data_p
->inbuffer
778 + garmin_data_p
->insize
;
779 memcpy(dest
, buf
+offs
, len
);
780 garmin_data_p
->insize
+= len
;
785 /* do we have a complete packet ? */
786 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
) {
787 len
= GARMIN_PKTHDR_LENGTH
+
788 getDataLength(garmin_data_p
->inbuffer
);
789 if (garmin_data_p
->insize
>= len
) {
790 garmin_write_bulk(garmin_data_p
->port
,
791 garmin_data_p
->inbuffer
,
793 garmin_data_p
->insize
= 0;
795 /* if this was an abort-transfer command,
796 flush all queued data. */
797 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
798 spin_lock_irqsave(&garmin_data_p
->lock
,
800 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
801 spin_unlock_irqrestore(
802 &garmin_data_p
->lock
, flags
);
803 pkt_clear(garmin_data_p
);
812 /******************************************************************************
814 ******************************************************************************/
816 static void priv_status_resp(struct usb_serial_port
*port
)
818 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
819 __le32
*pkt
= (__le32
*)garmin_data_p
->privpkt
;
821 pkt
[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE
);
822 pkt
[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP
);
823 pkt
[2] = __cpu_to_le32(12);
824 pkt
[3] = __cpu_to_le32(VERSION_MAJOR
<< 16 | VERSION_MINOR
);
825 pkt
[4] = __cpu_to_le32(garmin_data_p
->mode
);
826 pkt
[5] = __cpu_to_le32(garmin_data_p
->serial_num
);
828 send_to_tty(port
, (__u8
*)pkt
, 6 * 4);
832 /******************************************************************************
833 * Garmin specific driver functions
834 ******************************************************************************/
836 static int process_resetdev_request(struct usb_serial_port
*port
)
840 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
842 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
843 garmin_data_p
->flags
&= ~(CLEAR_HALT_REQUIRED
);
844 garmin_data_p
->state
= STATE_RESET
;
845 garmin_data_p
->serial_num
= 0;
846 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
848 usb_kill_urb(port
->interrupt_in_urb
);
849 dev_dbg(&port
->dev
, "%s - usb_reset_device\n", __func__
);
850 status
= usb_reset_device(port
->serial
->dev
);
852 dev_dbg(&port
->dev
, "%s - usb_reset_device failed: %d\n",
860 * clear all cached data
862 static int garmin_clear(struct garmin_data
*garmin_data_p
)
867 /* flush all queued data */
868 pkt_clear(garmin_data_p
);
870 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
871 garmin_data_p
->insize
= 0;
872 garmin_data_p
->outsize
= 0;
873 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
879 static int garmin_init_session(struct usb_serial_port
*port
)
881 struct usb_serial
*serial
= port
->serial
;
882 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
887 usb_kill_urb(port
->interrupt_in_urb
);
889 dev_dbg(&serial
->dev
->dev
, "%s - adding interrupt input\n", __func__
);
890 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
892 dev_err(&serial
->dev
->dev
,
893 "%s - failed submitting interrupt urb, error %d\n",
898 * using the initialization method from gpsbabel. See comments in
899 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
902 dev_dbg(&serial
->dev
->dev
, "%s - starting session ...\n", __func__
);
903 garmin_data_p
->state
= STATE_ACTIVE
;
905 for (i
= 0; i
< 3; i
++) {
906 status
= garmin_write_bulk(port
,
907 GARMIN_START_SESSION_REQ
,
908 sizeof(GARMIN_START_SESSION_REQ
), 0);
923 static int garmin_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
927 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
929 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
930 garmin_data_p
->mode
= initial_mode
;
931 garmin_data_p
->count
= 0;
932 garmin_data_p
->flags
&= FLAGS_SESSION_REPLY1_SEEN
;
933 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
935 /* shutdown any bulk reads that might be going on */
936 usb_kill_urb(port
->write_urb
);
937 usb_kill_urb(port
->read_urb
);
939 if (garmin_data_p
->state
== STATE_RESET
)
940 status
= garmin_init_session(port
);
942 garmin_data_p
->state
= STATE_ACTIVE
;
947 static void garmin_close(struct usb_serial_port
*port
)
949 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
951 dev_dbg(&port
->dev
, "%s - mode=%d state=%d flags=0x%X\n",
952 __func__
, garmin_data_p
->mode
, garmin_data_p
->state
,
953 garmin_data_p
->flags
);
955 garmin_clear(garmin_data_p
);
957 /* shutdown our urbs */
958 usb_kill_urb(port
->read_urb
);
959 usb_kill_urb(port
->write_urb
);
961 /* keep reset state so we know that we must start a new session */
962 if (garmin_data_p
->state
!= STATE_RESET
)
963 garmin_data_p
->state
= STATE_DISCONNECTED
;
967 static void garmin_write_bulk_callback(struct urb
*urb
)
969 struct usb_serial_port
*port
= urb
->context
;
972 struct garmin_data
*garmin_data_p
=
973 usb_get_serial_port_data(port
);
975 if (GARMIN_LAYERID_APPL
== getLayerId(urb
->transfer_buffer
)) {
977 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
978 gsp_send_ack(garmin_data_p
,
979 ((__u8
*)urb
->transfer_buffer
)[4]);
982 usb_serial_port_softint(port
);
985 /* Ignore errors that resulted from garmin_write_bulk with
988 /* free up the transfer buffer, as usb_free_urb() does not do this */
989 kfree(urb
->transfer_buffer
);
993 static int garmin_write_bulk(struct usb_serial_port
*port
,
994 const unsigned char *buf
, int count
,
998 struct usb_serial
*serial
= port
->serial
;
999 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1001 unsigned char *buffer
;
1004 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1005 garmin_data_p
->flags
&= ~FLAGS_DROP_DATA
;
1006 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1008 buffer
= kmalloc(count
, GFP_ATOMIC
);
1010 dev_err(&port
->dev
, "out of memory\n");
1014 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1016 dev_err(&port
->dev
, "no more free urbs\n");
1021 memcpy(buffer
, buf
, count
);
1023 usb_serial_debug_data(&port
->dev
, __func__
, count
, buffer
);
1025 usb_fill_bulk_urb(urb
, serial
->dev
,
1026 usb_sndbulkpipe(serial
->dev
,
1027 port
->bulk_out_endpointAddress
),
1029 garmin_write_bulk_callback
,
1030 dismiss_ack
? NULL
: port
);
1031 urb
->transfer_flags
|= URB_ZERO_PACKET
;
1033 if (GARMIN_LAYERID_APPL
== getLayerId(buffer
)) {
1035 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1036 garmin_data_p
->flags
|= APP_REQ_SEEN
;
1037 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1039 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1040 pkt_clear(garmin_data_p
);
1041 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
1045 /* send it down the pipe */
1046 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1049 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1054 /* we are done with this urb, so let the host driver
1055 * really free it when it is finished with it */
1061 static int garmin_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1062 const unsigned char *buf
, int count
)
1064 struct device
*dev
= &port
->dev
;
1065 int pktid
, pktsiz
, len
;
1066 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1067 __le32
*privpkt
= (__le32
*)garmin_data_p
->privpkt
;
1069 usb_serial_debug_data(dev
, __func__
, count
, buf
);
1071 if (garmin_data_p
->state
== STATE_RESET
)
1074 /* check for our private packets */
1075 if (count
>= GARMIN_PKTHDR_LENGTH
) {
1080 memcpy(garmin_data_p
->privpkt
, buf
, len
);
1082 pktsiz
= getDataLength(garmin_data_p
->privpkt
);
1083 pktid
= getPacketId(garmin_data_p
->privpkt
);
1085 if (count
== (GARMIN_PKTHDR_LENGTH
+pktsiz
)
1086 && GARMIN_LAYERID_PRIVATE
==
1087 getLayerId(garmin_data_p
->privpkt
)) {
1089 dev_dbg(dev
, "%s - processing private request %d\n",
1092 /* drop all unfinished transfers */
1093 garmin_clear(garmin_data_p
);
1096 case PRIV_PKTID_SET_MODE
:
1099 garmin_data_p
->mode
= __le32_to_cpu(privpkt
[3]);
1100 dev_dbg(dev
, "%s - mode set to %d\n",
1101 __func__
, garmin_data_p
->mode
);
1104 case PRIV_PKTID_INFO_REQ
:
1105 priv_status_resp(port
);
1108 case PRIV_PKTID_RESET_REQ
:
1109 process_resetdev_request(port
);
1112 case PRIV_PKTID_SET_DEF_MODE
:
1115 initial_mode
= __le32_to_cpu(privpkt
[3]);
1116 dev_dbg(dev
, "%s - initial_mode set to %d\n",
1118 garmin_data_p
->mode
);
1125 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1126 return gsp_receive(garmin_data_p
, buf
, count
);
1127 } else { /* MODE_NATIVE */
1128 return nat_receive(garmin_data_p
, buf
, count
);
1133 static int garmin_write_room(struct tty_struct
*tty
)
1135 struct usb_serial_port
*port
= tty
->driver_data
;
1137 * Report back the bytes currently available in the output buffer.
1139 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1140 return GPS_OUT_BUFSIZ
-garmin_data_p
->outsize
;
1144 static void garmin_read_process(struct garmin_data
*garmin_data_p
,
1145 unsigned char *data
, unsigned data_length
,
1148 unsigned long flags
;
1150 if (garmin_data_p
->flags
& FLAGS_DROP_DATA
) {
1151 /* abort-transfer cmd is actice */
1152 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt dropped\n", __func__
);
1153 } else if (garmin_data_p
->state
!= STATE_DISCONNECTED
&&
1154 garmin_data_p
->state
!= STATE_RESET
) {
1156 /* if throttling is active or postprecessing is required
1157 put the received data in the input queue, otherwise
1158 send it directly to the tty port */
1159 if (garmin_data_p
->flags
& FLAGS_QUEUING
) {
1160 pkt_add(garmin_data_p
, data
, data_length
);
1161 } else if (bulk_data
||
1162 getLayerId(data
) == GARMIN_LAYERID_APPL
) {
1164 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1165 garmin_data_p
->flags
|= APP_RESP_SEEN
;
1166 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1168 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1169 pkt_add(garmin_data_p
, data
, data_length
);
1171 send_to_tty(garmin_data_p
->port
, data
,
1175 /* ignore system layer packets ... */
1180 static void garmin_read_bulk_callback(struct urb
*urb
)
1182 unsigned long flags
;
1183 struct usb_serial_port
*port
= urb
->context
;
1184 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1185 unsigned char *data
= urb
->transfer_buffer
;
1186 int status
= urb
->status
;
1190 dev_dbg(&urb
->dev
->dev
, "%s - nonzero read bulk status received: %d\n",
1195 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
1197 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 1);
1199 if (urb
->actual_length
== 0 &&
1200 0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_RESTART
)) {
1201 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1202 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_RESTART
;
1203 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1204 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1207 "%s - failed resubmitting read urb, error %d\n",
1209 } else if (urb
->actual_length
> 0) {
1210 /* Continue trying to read until nothing more is received */
1211 if (0 == (garmin_data_p
->flags
& FLAGS_THROTTLED
)) {
1212 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1215 "%s - failed resubmitting read urb, error %d\n",
1219 dev_dbg(&port
->dev
, "%s - end of bulk data\n", __func__
);
1220 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1221 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_ACTIVE
;
1222 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1227 static void garmin_read_int_callback(struct urb
*urb
)
1229 unsigned long flags
;
1231 struct usb_serial_port
*port
= urb
->context
;
1232 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1233 unsigned char *data
= urb
->transfer_buffer
;
1234 int status
= urb
->status
;
1243 /* this urb is terminated, clean up */
1244 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n",
1248 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n",
1253 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
,
1254 urb
->transfer_buffer
);
1256 if (urb
->actual_length
== sizeof(GARMIN_BULK_IN_AVAIL_REPLY
) &&
1257 0 == memcmp(data
, GARMIN_BULK_IN_AVAIL_REPLY
,
1258 sizeof(GARMIN_BULK_IN_AVAIL_REPLY
))) {
1260 dev_dbg(&port
->dev
, "%s - bulk data available.\n", __func__
);
1262 if (0 == (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1264 /* bulk data available */
1265 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1268 "%s - failed submitting read urb, error %d\n",
1271 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1272 garmin_data_p
->flags
|= FLAGS_BULK_IN_ACTIVE
;
1273 spin_unlock_irqrestore(&garmin_data_p
->lock
,
1277 /* bulk-in transfer still active */
1278 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1279 garmin_data_p
->flags
|= FLAGS_BULK_IN_RESTART
;
1280 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1283 } else if (urb
->actual_length
== (4+sizeof(GARMIN_START_SESSION_REPLY
))
1284 && 0 == memcmp(data
, GARMIN_START_SESSION_REPLY
,
1285 sizeof(GARMIN_START_SESSION_REPLY
))) {
1287 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1288 garmin_data_p
->flags
|= FLAGS_SESSION_REPLY1_SEEN
;
1289 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1291 /* save the serial number */
1292 garmin_data_p
->serial_num
= __le32_to_cpup(
1293 (__le32
*)(data
+GARMIN_PKTHDR_LENGTH
));
1295 dev_dbg(&port
->dev
, "%s - start-of-session reply seen - serial %u.\n",
1296 __func__
, garmin_data_p
->serial_num
);
1299 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 0);
1301 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1303 dev_err(&urb
->dev
->dev
,
1304 "%s - Error %d submitting interrupt urb\n",
1310 * Sends the next queued packt to the tty port (garmin native mode only)
1311 * and then sets a timer to call itself again until all queued data
1314 static int garmin_flush_queue(struct garmin_data
*garmin_data_p
)
1316 unsigned long flags
;
1317 struct garmin_packet
*pkt
;
1319 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1320 pkt
= pkt_pop(garmin_data_p
);
1322 send_to_tty(garmin_data_p
->port
, pkt
->data
, pkt
->size
);
1324 mod_timer(&garmin_data_p
->timer
, (1)+jiffies
);
1327 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1328 garmin_data_p
->flags
&= ~FLAGS_QUEUING
;
1329 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1336 static void garmin_throttle(struct tty_struct
*tty
)
1338 struct usb_serial_port
*port
= tty
->driver_data
;
1339 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1341 /* set flag, data received will be put into a queue
1342 for later processing */
1343 spin_lock_irq(&garmin_data_p
->lock
);
1344 garmin_data_p
->flags
|= FLAGS_QUEUING
|FLAGS_THROTTLED
;
1345 spin_unlock_irq(&garmin_data_p
->lock
);
1349 static void garmin_unthrottle(struct tty_struct
*tty
)
1351 struct usb_serial_port
*port
= tty
->driver_data
;
1352 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1355 spin_lock_irq(&garmin_data_p
->lock
);
1356 garmin_data_p
->flags
&= ~FLAGS_THROTTLED
;
1357 spin_unlock_irq(&garmin_data_p
->lock
);
1359 /* in native mode send queued data to tty, in
1360 serial mode nothing needs to be done here */
1361 if (garmin_data_p
->mode
== MODE_NATIVE
)
1362 garmin_flush_queue(garmin_data_p
);
1364 if (0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1365 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1368 "%s - failed resubmitting read urb, error %d\n",
1374 * The timer is currently only used to send queued packets to
1375 * the tty in cases where the protocol provides no own handshaking
1376 * to initiate the transfer.
1378 static void timeout_handler(unsigned long data
)
1380 struct garmin_data
*garmin_data_p
= (struct garmin_data
*) data
;
1382 /* send the next queued packet to the tty port */
1383 if (garmin_data_p
->mode
== MODE_NATIVE
)
1384 if (garmin_data_p
->flags
& FLAGS_QUEUING
)
1385 garmin_flush_queue(garmin_data_p
);
1390 static int garmin_port_probe(struct usb_serial_port
*port
)
1393 struct garmin_data
*garmin_data_p
;
1395 garmin_data_p
= kzalloc(sizeof(struct garmin_data
), GFP_KERNEL
);
1396 if (garmin_data_p
== NULL
) {
1397 dev_err(&port
->dev
, "%s - Out of memory\n", __func__
);
1400 init_timer(&garmin_data_p
->timer
);
1401 spin_lock_init(&garmin_data_p
->lock
);
1402 INIT_LIST_HEAD(&garmin_data_p
->pktlist
);
1403 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1404 garmin_data_p
->timer
.data
= (unsigned long)garmin_data_p
;
1405 garmin_data_p
->timer
.function
= timeout_handler
;
1406 garmin_data_p
->port
= port
;
1407 garmin_data_p
->state
= 0;
1408 garmin_data_p
->flags
= 0;
1409 garmin_data_p
->count
= 0;
1410 usb_set_serial_port_data(port
, garmin_data_p
);
1412 status
= garmin_init_session(port
);
1418 static int garmin_port_remove(struct usb_serial_port
*port
)
1420 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1422 usb_kill_urb(port
->interrupt_in_urb
);
1423 del_timer_sync(&garmin_data_p
->timer
);
1424 kfree(garmin_data_p
);
1429 /* All of the device info needed */
1430 static struct usb_serial_driver garmin_device
= {
1432 .owner
= THIS_MODULE
,
1433 .name
= "garmin_gps",
1435 .description
= "Garmin GPS usb/tty",
1436 .id_table
= id_table
,
1438 .open
= garmin_open
,
1439 .close
= garmin_close
,
1440 .throttle
= garmin_throttle
,
1441 .unthrottle
= garmin_unthrottle
,
1442 .port_probe
= garmin_port_probe
,
1443 .port_remove
= garmin_port_remove
,
1444 .write
= garmin_write
,
1445 .write_room
= garmin_write_room
,
1446 .write_bulk_callback
= garmin_write_bulk_callback
,
1447 .read_bulk_callback
= garmin_read_bulk_callback
,
1448 .read_int_callback
= garmin_read_int_callback
,
1451 static struct usb_serial_driver
* const serial_drivers
[] = {
1452 &garmin_device
, NULL
1455 module_usb_serial_driver(serial_drivers
, id_table
);
1457 MODULE_AUTHOR(DRIVER_AUTHOR
);
1458 MODULE_DESCRIPTION(DRIVER_DESC
);
1459 MODULE_LICENSE("GPL");
1461 module_param(initial_mode
, int, S_IRUGO
);
1462 MODULE_PARM_DESC(initial_mode
, "Initial mode");