1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de
7 * The latest version of the driver can be found at
8 * http://sourceforge.net/projects/garmin-gps/
10 * This driver has been derived from v2.1 of the visor driver.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/timer.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/tty_flip.h>
20 #include <linux/module.h>
21 #include <linux/spinlock.h>
22 #include <linux/uaccess.h>
23 #include <linux/atomic.h>
24 #include <linux/usb.h>
25 #include <linux/usb/serial.h>
27 /* the mode to be set when the port ist opened */
28 static int initial_mode
= 1;
30 #define GARMIN_VENDOR_ID 0x091E
36 #define VERSION_MAJOR 0
37 #define VERSION_MINOR 36
40 #define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
41 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
42 #define DRIVER_AUTHOR "hermann kneissel"
43 #define DRIVER_DESC "garmin gps driver"
45 /* error codes returned by the driver */
46 #define EINVPKT 1000 /* invalid packet structure */
49 /* size of the header of a packet using the usb protocol */
50 #define GARMIN_PKTHDR_LENGTH 12
52 /* max. possible size of a packet using the serial protocol */
53 #define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
55 /* max. possible size of a packet with worst case stuffing */
56 #define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
58 /* size of a buffer able to hold a complete (no stuffing) packet
59 * (the document protocol does not contain packets with a larger
60 * size, but in theory a packet may be 64k+12 bytes - if in
61 * later protocol versions larger packet sizes occur, this value
62 * should be increased accordingly, so the input buffer is always
63 * large enough the store a complete packet inclusive header) */
64 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
66 /* size of a buffer able to hold a complete (incl. stuffing) packet */
67 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
69 /* where to place the packet id of a serial packet, so we can
70 * prepend the usb-packet header without the need to move the
72 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
74 /* max. size of incoming private packets (header+1 param) */
75 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
77 #define GARMIN_LAYERID_TRANSPORT 0
78 #define GARMIN_LAYERID_APPL 20
79 /* our own layer-id to use for some control mechanisms */
80 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
82 #define GARMIN_PKTID_PVT_DATA 51
83 #define GARMIN_PKTID_L001_COMMAND_DATA 10
85 #define CMND_ABORT_TRANSFER 0
87 /* packet ids used in private layer */
88 #define PRIV_PKTID_SET_DEBUG 1
89 #define PRIV_PKTID_SET_MODE 2
90 #define PRIV_PKTID_INFO_REQ 3
91 #define PRIV_PKTID_INFO_RESP 4
92 #define PRIV_PKTID_RESET_REQ 5
93 #define PRIV_PKTID_SET_DEF_MODE 6
101 /* structure used to queue incoming packets */
102 struct garmin_packet
{
103 struct list_head list
;
105 /* the real size of the data array, always > 0 */
110 /* structure used to keep the current state of the driver */
118 struct timer_list timer
;
119 struct usb_serial_port
*port
;
123 __u8 inbuffer
[GPS_IN_BUFSIZ
]; /* tty -> usb */
124 __u8 outbuffer
[GPS_OUT_BUFSIZ
]; /* usb -> tty */
127 struct list_head pktlist
;
128 struct usb_anchor write_urbs
;
133 #define STATE_INITIAL_DELAY 1
134 #define STATE_TIMEOUT 2
135 #define STATE_SESSION_REQ1 3
136 #define STATE_SESSION_REQ2 4
137 #define STATE_ACTIVE 5
139 #define STATE_RESET 8
140 #define STATE_DISCONNECTED 9
141 #define STATE_WAIT_TTY_ACK 10
142 #define STATE_GSP_WAIT_DATA 11
144 #define MODE_NATIVE 0
145 #define MODE_GARMIN_SERIAL 1
147 /* Flags used in garmin_data.flags: */
148 #define FLAGS_SESSION_REPLY_MASK 0x00C0
149 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
150 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
151 #define FLAGS_BULK_IN_ACTIVE 0x0020
152 #define FLAGS_BULK_IN_RESTART 0x0010
153 #define FLAGS_THROTTLED 0x0008
154 #define APP_REQ_SEEN 0x0004
155 #define APP_RESP_SEEN 0x0002
156 #define CLEAR_HALT_REQUIRED 0x0001
158 #define FLAGS_QUEUING 0x0100
159 #define FLAGS_DROP_DATA 0x0800
161 #define FLAGS_GSP_SKIP 0x1000
162 #define FLAGS_GSP_DLESEEN 0x2000
169 /* function prototypes */
170 static int gsp_next_packet(struct garmin_data
*garmin_data_p
);
171 static int garmin_write_bulk(struct usb_serial_port
*port
,
172 const unsigned char *buf
, int count
,
175 /* some special packets to be send or received */
176 static unsigned char const GARMIN_START_SESSION_REQ
[]
177 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
178 static unsigned char const GARMIN_START_SESSION_REPLY
[]
179 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
180 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY
[]
181 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
182 static unsigned char const GARMIN_APP_LAYER_REPLY
[]
184 static unsigned char const GARMIN_START_PVT_REQ
[]
185 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
186 static unsigned char const GARMIN_STOP_PVT_REQ
[]
187 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
188 static unsigned char const GARMIN_STOP_TRANSFER_REQ
[]
189 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
190 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2
[]
191 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
192 static unsigned char const PRIVATE_REQ
[]
193 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
197 static const struct usb_device_id id_table
[] = {
198 /* the same device id seems to be used by all
199 usb enabled GPS devices */
200 { USB_DEVICE(GARMIN_VENDOR_ID
, 3) },
201 { } /* Terminating entry */
203 MODULE_DEVICE_TABLE(usb
, id_table
);
206 static inline int getLayerId(const __u8
*usbPacket
)
208 return __le32_to_cpup((__le32
*)(usbPacket
));
211 static inline int getPacketId(const __u8
*usbPacket
)
213 return __le32_to_cpup((__le32
*)(usbPacket
+4));
216 static inline int getDataLength(const __u8
*usbPacket
)
218 return __le32_to_cpup((__le32
*)(usbPacket
+8));
223 * check if the usb-packet in buf contains an abort-transfer command.
224 * (if yes, all queued data will be dropped)
226 static inline int isAbortTrfCmnd(const unsigned char *buf
)
228 if (memcmp(buf
, GARMIN_STOP_TRANSFER_REQ
,
229 sizeof(GARMIN_STOP_TRANSFER_REQ
)) == 0 ||
230 memcmp(buf
, GARMIN_STOP_TRANSFER_REQ_V2
,
231 sizeof(GARMIN_STOP_TRANSFER_REQ_V2
)) == 0)
239 static void send_to_tty(struct usb_serial_port
*port
,
240 char *data
, unsigned int actual_length
)
243 usb_serial_debug_data(&port
->dev
, __func__
, actual_length
, data
);
244 tty_insert_flip_string(&port
->port
, data
, actual_length
);
245 tty_flip_buffer_push(&port
->port
);
250 /******************************************************************************
251 * packet queue handling
252 ******************************************************************************/
255 * queue a received (usb-)packet for later processing
257 static int pkt_add(struct garmin_data
*garmin_data_p
,
258 unsigned char *data
, unsigned int data_length
)
263 struct garmin_packet
*pkt
;
265 /* process only packets containing data ... */
267 pkt
= kmalloc(sizeof(struct garmin_packet
)+data_length
,
272 pkt
->size
= data_length
;
273 memcpy(pkt
->data
, data
, data_length
);
275 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
276 garmin_data_p
->flags
|= FLAGS_QUEUING
;
277 result
= list_empty(&garmin_data_p
->pktlist
);
278 pkt
->seq
= garmin_data_p
->seq_counter
++;
279 list_add_tail(&pkt
->list
, &garmin_data_p
->pktlist
);
280 state
= garmin_data_p
->state
;
281 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
283 dev_dbg(&garmin_data_p
->port
->dev
,
284 "%s - added: pkt: %d - %d bytes\n", __func__
,
285 pkt
->seq
, data_length
);
287 /* in serial mode, if someone is waiting for data from
288 the device, convert and send the next packet to tty. */
289 if (result
&& (state
== STATE_GSP_WAIT_DATA
))
290 gsp_next_packet(garmin_data_p
);
296 /* get the next pending packet */
297 static struct garmin_packet
*pkt_pop(struct garmin_data
*garmin_data_p
)
300 struct garmin_packet
*result
= NULL
;
302 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
303 if (!list_empty(&garmin_data_p
->pktlist
)) {
304 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
305 list_del(&result
->list
);
307 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
312 /* free up all queued data */
313 static void pkt_clear(struct garmin_data
*garmin_data_p
)
316 struct garmin_packet
*result
= NULL
;
318 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
319 while (!list_empty(&garmin_data_p
->pktlist
)) {
320 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
321 list_del(&result
->list
);
324 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
328 /******************************************************************************
329 * garmin serial protocol handling handling
330 ******************************************************************************/
332 /* send an ack packet back to the tty */
333 static int gsp_send_ack(struct garmin_data
*garmin_data_p
, __u8 pkt_id
)
340 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt-id: 0x%X.\n", __func__
,
357 *ptr
++ = (-cksum
) & 0xFF;
363 send_to_tty(garmin_data_p
->port
, pkt
, l
);
370 * called for a complete packet received from tty layer
372 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
373 * at GSP_INITIAL_OFFSET.
375 * count - number of bytes in the input buffer including space reserved for
376 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
377 * (including pkt-id, data-length a. cksum)
379 static int gsp_rec_packet(struct garmin_data
*garmin_data_p
, int count
)
381 struct device
*dev
= &garmin_data_p
->port
->dev
;
383 const __u8
*recpkt
= garmin_data_p
->inbuffer
+GSP_INITIAL_OFFSET
;
384 __le32
*usbdata
= (__le32
*) garmin_data_p
->inbuffer
;
387 int pktid
= recpkt
[0];
388 int size
= recpkt
[1];
390 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
,
391 count
-GSP_INITIAL_OFFSET
, recpkt
);
393 if (size
!= (count
-GSP_INITIAL_OFFSET
-3)) {
394 dev_dbg(dev
, "%s - invalid size, expected %d bytes, got %d\n",
395 __func__
, size
, (count
-GSP_INITIAL_OFFSET
-3));
402 /* sanity check, remove after test ... */
403 if ((__u8
*)&(usbdata
[3]) != recpkt
) {
404 dev_dbg(dev
, "%s - ptr mismatch %p - %p\n", __func__
,
405 &(usbdata
[4]), recpkt
);
414 if (((cksum
+ *recpkt
) & 0xff) != 0) {
415 dev_dbg(dev
, "%s - invalid checksum, expected %02x, got %02x\n",
416 __func__
, -cksum
& 0xff, *recpkt
);
420 usbdata
[0] = __cpu_to_le32(GARMIN_LAYERID_APPL
);
421 usbdata
[1] = __cpu_to_le32(pktid
);
422 usbdata
[2] = __cpu_to_le32(size
);
424 garmin_write_bulk(garmin_data_p
->port
, garmin_data_p
->inbuffer
,
425 GARMIN_PKTHDR_LENGTH
+size
, 0);
427 /* if this was an abort-transfer command, flush all
429 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
430 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
431 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
432 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
433 pkt_clear(garmin_data_p
);
442 * Called for data received from tty
444 * buf contains the data read, it may span more than one packet or even
447 * input record should be a serial-record, but it may not be complete.
448 * Copy it into our local buffer, until an etx is seen (or an error
450 * Once the record is complete, convert into a usb packet and send it
451 * to the bulk pipe, send an ack back to the tty.
453 * If the input is an ack, just send the last queued packet to the
456 * if the input is an abort command, drop all queued data.
459 static int gsp_receive(struct garmin_data
*garmin_data_p
,
460 const unsigned char *buf
, int count
)
462 struct device
*dev
= &garmin_data_p
->port
->dev
;
465 int ack_or_nak_seen
= 0;
468 /* dleSeen: set if last byte read was a DLE */
470 /* skip: if set, skip incoming data until possible start of
476 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
477 dest
= garmin_data_p
->inbuffer
;
478 size
= garmin_data_p
->insize
;
479 dleSeen
= garmin_data_p
->flags
& FLAGS_GSP_DLESEEN
;
480 skip
= garmin_data_p
->flags
& FLAGS_GSP_SKIP
;
481 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
483 /* dev_dbg(dev, "%s - dle=%d skip=%d size=%d count=%d\n",
484 __func__, dleSeen, skip, size, count); */
487 size
= GSP_INITIAL_OFFSET
;
489 while (offs
< count
) {
495 if (skip
) { /* start of a new pkt */
497 size
= GSP_INITIAL_OFFSET
;
499 } else if (dleSeen
) {
505 } else if (data
== ETX
) {
507 /* packet complete */
509 data
= dest
[GSP_INITIAL_OFFSET
];
512 ack_or_nak_seen
= ACK
;
513 dev_dbg(dev
, "ACK packet complete.\n");
514 } else if (data
== NAK
) {
515 ack_or_nak_seen
= NAK
;
516 dev_dbg(dev
, "NAK packet complete.\n");
518 dev_dbg(dev
, "packet complete - id=0x%X.\n",
520 gsp_rec_packet(garmin_data_p
, size
);
524 size
= GSP_INITIAL_OFFSET
;
532 size
= GSP_INITIAL_OFFSET
;
539 if (size
>= GPS_IN_BUFSIZ
) {
540 dev_dbg(dev
, "%s - packet too large.\n", __func__
);
542 size
= GSP_INITIAL_OFFSET
;
547 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
549 garmin_data_p
->insize
= size
;
551 /* copy flags back to structure */
553 garmin_data_p
->flags
|= FLAGS_GSP_SKIP
;
555 garmin_data_p
->flags
&= ~FLAGS_GSP_SKIP
;
558 garmin_data_p
->flags
|= FLAGS_GSP_DLESEEN
;
560 garmin_data_p
->flags
&= ~FLAGS_GSP_DLESEEN
;
562 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
564 if (ack_or_nak_seen
) {
565 if (gsp_next_packet(garmin_data_p
) > 0)
566 garmin_data_p
->state
= STATE_ACTIVE
;
568 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
576 * Sends a usb packet to the tty
578 * Assumes, that all packages and at an usb-packet boundary.
580 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
582 static int gsp_send(struct garmin_data
*garmin_data_p
,
583 const unsigned char *buf
, int count
)
585 struct device
*dev
= &garmin_data_p
->port
->dev
;
586 const unsigned char *src
;
594 dev_dbg(dev
, "%s - state %d - %d bytes.\n", __func__
,
595 garmin_data_p
->state
, count
);
597 k
= garmin_data_p
->outsize
;
598 if ((k
+count
) > GPS_OUT_BUFSIZ
) {
599 dev_dbg(dev
, "packet too large\n");
600 garmin_data_p
->outsize
= 0;
604 memcpy(garmin_data_p
->outbuffer
+k
, buf
, count
);
606 garmin_data_p
->outsize
= k
;
608 if (k
>= GARMIN_PKTHDR_LENGTH
) {
609 pktid
= getPacketId(garmin_data_p
->outbuffer
);
610 datalen
= getDataLength(garmin_data_p
->outbuffer
);
611 i
= GARMIN_PKTHDR_LENGTH
+ datalen
;
618 dev_dbg(dev
, "%s - %d bytes in buffer, %d bytes in pkt.\n", __func__
, k
, i
);
620 /* garmin_data_p->outbuffer now contains a complete packet */
622 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
, k
,
623 garmin_data_p
->outbuffer
);
625 garmin_data_p
->outsize
= 0;
627 if (getLayerId(garmin_data_p
->outbuffer
) != GARMIN_LAYERID_APPL
) {
628 dev_dbg(dev
, "not an application packet (%d)\n",
629 getLayerId(garmin_data_p
->outbuffer
));
634 dev_dbg(dev
, "packet-id %d too large\n", pktid
);
639 dev_dbg(dev
, "packet-size %d too large\n", datalen
);
643 /* the serial protocol should be able to handle this packet */
646 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
647 for (i
= 0; i
< datalen
; i
++) {
652 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
653 if (k
> (GARMIN_PKTHDR_LENGTH
-2)) {
654 /* can't add stuffing DLEs in place, move data to end
656 dst
= garmin_data_p
->outbuffer
+GPS_OUT_BUFSIZ
-datalen
;
657 memcpy(dst
, src
, datalen
);
661 dst
= garmin_data_p
->outbuffer
;
671 for (i
= 0; i
< datalen
; i
++) {
679 cksum
= -cksum
& 0xFF;
686 i
= dst
-garmin_data_p
->outbuffer
;
688 send_to_tty(garmin_data_p
->port
, garmin_data_p
->outbuffer
, i
);
690 garmin_data_p
->pkt_id
= pktid
;
691 garmin_data_p
->state
= STATE_WAIT_TTY_ACK
;
698 * Process the next pending data packet - if there is one
700 static int gsp_next_packet(struct garmin_data
*garmin_data_p
)
703 struct garmin_packet
*pkt
= NULL
;
705 while ((pkt
= pkt_pop(garmin_data_p
)) != NULL
) {
706 dev_dbg(&garmin_data_p
->port
->dev
, "%s - next pkt: %d\n", __func__
, pkt
->seq
);
707 result
= gsp_send(garmin_data_p
, pkt
->data
, pkt
->size
);
719 /******************************************************************************
721 ******************************************************************************/
725 * Called for data received from tty
727 * The input data is expected to be in garmin usb-packet format.
729 * buf contains the data read, it may span more than one packet
730 * or even incomplete packets
732 static int nat_receive(struct garmin_data
*garmin_data_p
,
733 const unsigned char *buf
, int count
)
741 while (offs
< count
) {
742 /* if buffer contains header, copy rest of data */
743 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
)
744 len
= GARMIN_PKTHDR_LENGTH
745 +getDataLength(garmin_data_p
->inbuffer
);
747 len
= GARMIN_PKTHDR_LENGTH
;
749 if (len
>= GPS_IN_BUFSIZ
) {
750 /* seems to be an invalid packet, ignore rest
752 dev_dbg(&garmin_data_p
->port
->dev
,
753 "%s - packet size too large: %d\n",
755 garmin_data_p
->insize
= 0;
759 len
-= garmin_data_p
->insize
;
760 if (len
> (count
-offs
))
763 dest
= garmin_data_p
->inbuffer
764 + garmin_data_p
->insize
;
765 memcpy(dest
, buf
+offs
, len
);
766 garmin_data_p
->insize
+= len
;
771 /* do we have a complete packet ? */
772 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
) {
773 len
= GARMIN_PKTHDR_LENGTH
+
774 getDataLength(garmin_data_p
->inbuffer
);
775 if (garmin_data_p
->insize
>= len
) {
776 garmin_write_bulk(garmin_data_p
->port
,
777 garmin_data_p
->inbuffer
,
779 garmin_data_p
->insize
= 0;
781 /* if this was an abort-transfer command,
782 flush all queued data. */
783 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
784 spin_lock_irqsave(&garmin_data_p
->lock
,
786 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
787 spin_unlock_irqrestore(
788 &garmin_data_p
->lock
, flags
);
789 pkt_clear(garmin_data_p
);
798 /******************************************************************************
800 ******************************************************************************/
802 static void priv_status_resp(struct usb_serial_port
*port
)
804 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
805 __le32
*pkt
= (__le32
*)garmin_data_p
->privpkt
;
807 pkt
[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE
);
808 pkt
[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP
);
809 pkt
[2] = __cpu_to_le32(12);
810 pkt
[3] = __cpu_to_le32(VERSION_MAJOR
<< 16 | VERSION_MINOR
);
811 pkt
[4] = __cpu_to_le32(garmin_data_p
->mode
);
812 pkt
[5] = __cpu_to_le32(garmin_data_p
->serial_num
);
814 send_to_tty(port
, (__u8
*)pkt
, 6 * 4);
818 /******************************************************************************
819 * Garmin specific driver functions
820 ******************************************************************************/
822 static int process_resetdev_request(struct usb_serial_port
*port
)
826 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
828 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
829 garmin_data_p
->flags
&= ~(CLEAR_HALT_REQUIRED
);
830 garmin_data_p
->state
= STATE_RESET
;
831 garmin_data_p
->serial_num
= 0;
832 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
834 usb_kill_urb(port
->interrupt_in_urb
);
835 dev_dbg(&port
->dev
, "%s - usb_reset_device\n", __func__
);
836 status
= usb_reset_device(port
->serial
->dev
);
838 dev_dbg(&port
->dev
, "%s - usb_reset_device failed: %d\n",
846 * clear all cached data
848 static int garmin_clear(struct garmin_data
*garmin_data_p
)
852 /* flush all queued data */
853 pkt_clear(garmin_data_p
);
855 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
856 garmin_data_p
->insize
= 0;
857 garmin_data_p
->outsize
= 0;
858 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
864 static int garmin_init_session(struct usb_serial_port
*port
)
866 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
870 usb_kill_urb(port
->interrupt_in_urb
);
872 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
874 dev_err(&port
->dev
, "failed to submit interrupt urb: %d\n",
880 * using the initialization method from gpsbabel. See comments in
881 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
883 dev_dbg(&port
->dev
, "%s - starting session ...\n", __func__
);
884 garmin_data_p
->state
= STATE_ACTIVE
;
886 for (i
= 0; i
< 3; i
++) {
887 status
= garmin_write_bulk(port
, GARMIN_START_SESSION_REQ
,
888 sizeof(GARMIN_START_SESSION_REQ
), 0);
896 usb_kill_anchored_urbs(&garmin_data_p
->write_urbs
);
897 usb_kill_urb(port
->interrupt_in_urb
);
904 static int garmin_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
908 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
910 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
911 garmin_data_p
->mode
= initial_mode
;
912 garmin_data_p
->count
= 0;
913 garmin_data_p
->flags
&= FLAGS_SESSION_REPLY1_SEEN
;
914 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
916 /* shutdown any bulk reads that might be going on */
917 usb_kill_urb(port
->read_urb
);
919 if (garmin_data_p
->state
== STATE_RESET
)
920 status
= garmin_init_session(port
);
922 garmin_data_p
->state
= STATE_ACTIVE
;
927 static void garmin_close(struct usb_serial_port
*port
)
929 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
931 dev_dbg(&port
->dev
, "%s - mode=%d state=%d flags=0x%X\n",
932 __func__
, garmin_data_p
->mode
, garmin_data_p
->state
,
933 garmin_data_p
->flags
);
935 garmin_clear(garmin_data_p
);
937 /* shutdown our urbs */
938 usb_kill_urb(port
->read_urb
);
939 usb_kill_anchored_urbs(&garmin_data_p
->write_urbs
);
941 /* keep reset state so we know that we must start a new session */
942 if (garmin_data_p
->state
!= STATE_RESET
)
943 garmin_data_p
->state
= STATE_DISCONNECTED
;
947 static void garmin_write_bulk_callback(struct urb
*urb
)
949 struct usb_serial_port
*port
= urb
->context
;
952 struct garmin_data
*garmin_data_p
=
953 usb_get_serial_port_data(port
);
955 if (getLayerId(urb
->transfer_buffer
) == GARMIN_LAYERID_APPL
) {
957 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
958 gsp_send_ack(garmin_data_p
,
959 ((__u8
*)urb
->transfer_buffer
)[4]);
962 usb_serial_port_softint(port
);
965 /* Ignore errors that resulted from garmin_write_bulk with
968 /* free up the transfer buffer, as usb_free_urb() does not do this */
969 kfree(urb
->transfer_buffer
);
973 static int garmin_write_bulk(struct usb_serial_port
*port
,
974 const unsigned char *buf
, int count
,
978 struct usb_serial
*serial
= port
->serial
;
979 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
981 unsigned char *buffer
;
984 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
985 garmin_data_p
->flags
&= ~FLAGS_DROP_DATA
;
986 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
988 buffer
= kmalloc(count
, GFP_ATOMIC
);
992 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
998 memcpy(buffer
, buf
, count
);
1000 usb_serial_debug_data(&port
->dev
, __func__
, count
, buffer
);
1002 usb_fill_bulk_urb(urb
, serial
->dev
,
1003 usb_sndbulkpipe(serial
->dev
,
1004 port
->bulk_out_endpointAddress
),
1006 garmin_write_bulk_callback
,
1007 dismiss_ack
? NULL
: port
);
1008 urb
->transfer_flags
|= URB_ZERO_PACKET
;
1010 if (getLayerId(buffer
) == GARMIN_LAYERID_APPL
) {
1012 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1013 garmin_data_p
->flags
|= APP_REQ_SEEN
;
1014 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1016 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1017 pkt_clear(garmin_data_p
);
1018 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
1022 /* send it down the pipe */
1023 usb_anchor_urb(urb
, &garmin_data_p
->write_urbs
);
1024 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1027 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1030 usb_unanchor_urb(urb
);
1034 /* we are done with this urb, so let the host driver
1035 * really free it when it is finished with it */
1041 static int garmin_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1042 const unsigned char *buf
, int count
)
1044 struct device
*dev
= &port
->dev
;
1045 int pktid
, pktsiz
, len
;
1046 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1047 __le32
*privpkt
= (__le32
*)garmin_data_p
->privpkt
;
1049 usb_serial_debug_data(dev
, __func__
, count
, buf
);
1051 if (garmin_data_p
->state
== STATE_RESET
)
1054 /* check for our private packets */
1055 if (count
>= GARMIN_PKTHDR_LENGTH
) {
1060 memcpy(garmin_data_p
->privpkt
, buf
, len
);
1062 pktsiz
= getDataLength(garmin_data_p
->privpkt
);
1063 pktid
= getPacketId(garmin_data_p
->privpkt
);
1065 if (count
== (GARMIN_PKTHDR_LENGTH
+ pktsiz
) &&
1066 getLayerId(garmin_data_p
->privpkt
) ==
1067 GARMIN_LAYERID_PRIVATE
) {
1069 dev_dbg(dev
, "%s - processing private request %d\n",
1072 /* drop all unfinished transfers */
1073 garmin_clear(garmin_data_p
);
1076 case PRIV_PKTID_SET_MODE
:
1079 garmin_data_p
->mode
= __le32_to_cpu(privpkt
[3]);
1080 dev_dbg(dev
, "%s - mode set to %d\n",
1081 __func__
, garmin_data_p
->mode
);
1084 case PRIV_PKTID_INFO_REQ
:
1085 priv_status_resp(port
);
1088 case PRIV_PKTID_RESET_REQ
:
1089 process_resetdev_request(port
);
1092 case PRIV_PKTID_SET_DEF_MODE
:
1095 initial_mode
= __le32_to_cpu(privpkt
[3]);
1096 dev_dbg(dev
, "%s - initial_mode set to %d\n",
1098 garmin_data_p
->mode
);
1105 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1106 return gsp_receive(garmin_data_p
, buf
, count
);
1107 } else { /* MODE_NATIVE */
1108 return nat_receive(garmin_data_p
, buf
, count
);
1113 static int garmin_write_room(struct tty_struct
*tty
)
1115 struct usb_serial_port
*port
= tty
->driver_data
;
1117 * Report back the bytes currently available in the output buffer.
1119 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1120 return GPS_OUT_BUFSIZ
-garmin_data_p
->outsize
;
1124 static void garmin_read_process(struct garmin_data
*garmin_data_p
,
1125 unsigned char *data
, unsigned data_length
,
1128 unsigned long flags
;
1130 if (garmin_data_p
->flags
& FLAGS_DROP_DATA
) {
1131 /* abort-transfer cmd is active */
1132 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt dropped\n", __func__
);
1133 } else if (garmin_data_p
->state
!= STATE_DISCONNECTED
&&
1134 garmin_data_p
->state
!= STATE_RESET
) {
1136 /* if throttling is active or postprecessing is required
1137 put the received data in the input queue, otherwise
1138 send it directly to the tty port */
1139 if (garmin_data_p
->flags
& FLAGS_QUEUING
) {
1140 pkt_add(garmin_data_p
, data
, data_length
);
1141 } else if (bulk_data
||
1142 getLayerId(data
) == GARMIN_LAYERID_APPL
) {
1144 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1145 garmin_data_p
->flags
|= APP_RESP_SEEN
;
1146 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1148 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1149 pkt_add(garmin_data_p
, data
, data_length
);
1151 send_to_tty(garmin_data_p
->port
, data
,
1155 /* ignore system layer packets ... */
1160 static void garmin_read_bulk_callback(struct urb
*urb
)
1162 unsigned long flags
;
1163 struct usb_serial_port
*port
= urb
->context
;
1164 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1165 unsigned char *data
= urb
->transfer_buffer
;
1166 int status
= urb
->status
;
1170 dev_dbg(&urb
->dev
->dev
, "%s - nonzero read bulk status received: %d\n",
1175 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
1177 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 1);
1179 if (urb
->actual_length
== 0 &&
1180 (garmin_data_p
->flags
& FLAGS_BULK_IN_RESTART
) != 0) {
1181 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1182 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_RESTART
;
1183 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1184 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1187 "%s - failed resubmitting read urb, error %d\n",
1189 } else if (urb
->actual_length
> 0) {
1190 /* Continue trying to read until nothing more is received */
1191 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1192 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1195 "%s - failed resubmitting read urb, error %d\n",
1199 dev_dbg(&port
->dev
, "%s - end of bulk data\n", __func__
);
1200 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1201 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_ACTIVE
;
1202 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1207 static void garmin_read_int_callback(struct urb
*urb
)
1209 unsigned long flags
;
1211 struct usb_serial_port
*port
= urb
->context
;
1212 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1213 unsigned char *data
= urb
->transfer_buffer
;
1214 int status
= urb
->status
;
1223 /* this urb is terminated, clean up */
1224 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n",
1228 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n",
1233 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
,
1234 urb
->transfer_buffer
);
1236 if (urb
->actual_length
== sizeof(GARMIN_BULK_IN_AVAIL_REPLY
) &&
1237 memcmp(data
, GARMIN_BULK_IN_AVAIL_REPLY
,
1238 sizeof(GARMIN_BULK_IN_AVAIL_REPLY
)) == 0) {
1240 dev_dbg(&port
->dev
, "%s - bulk data available.\n", __func__
);
1242 if ((garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
) == 0) {
1244 /* bulk data available */
1245 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1248 "%s - failed submitting read urb, error %d\n",
1251 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1252 garmin_data_p
->flags
|= FLAGS_BULK_IN_ACTIVE
;
1253 spin_unlock_irqrestore(&garmin_data_p
->lock
,
1257 /* bulk-in transfer still active */
1258 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1259 garmin_data_p
->flags
|= FLAGS_BULK_IN_RESTART
;
1260 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1263 } else if (urb
->actual_length
== (4+sizeof(GARMIN_START_SESSION_REPLY
))
1264 && memcmp(data
, GARMIN_START_SESSION_REPLY
,
1265 sizeof(GARMIN_START_SESSION_REPLY
)) == 0) {
1267 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1268 garmin_data_p
->flags
|= FLAGS_SESSION_REPLY1_SEEN
;
1269 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1271 /* save the serial number */
1272 garmin_data_p
->serial_num
= __le32_to_cpup(
1273 (__le32
*)(data
+GARMIN_PKTHDR_LENGTH
));
1275 dev_dbg(&port
->dev
, "%s - start-of-session reply seen - serial %u.\n",
1276 __func__
, garmin_data_p
->serial_num
);
1279 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 0);
1281 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1283 dev_err(&urb
->dev
->dev
,
1284 "%s - Error %d submitting interrupt urb\n",
1290 * Sends the next queued packt to the tty port (garmin native mode only)
1291 * and then sets a timer to call itself again until all queued data
1294 static int garmin_flush_queue(struct garmin_data
*garmin_data_p
)
1296 unsigned long flags
;
1297 struct garmin_packet
*pkt
;
1299 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1300 pkt
= pkt_pop(garmin_data_p
);
1302 send_to_tty(garmin_data_p
->port
, pkt
->data
, pkt
->size
);
1304 mod_timer(&garmin_data_p
->timer
, (1)+jiffies
);
1307 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1308 garmin_data_p
->flags
&= ~FLAGS_QUEUING
;
1309 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1316 static void garmin_throttle(struct tty_struct
*tty
)
1318 struct usb_serial_port
*port
= tty
->driver_data
;
1319 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1321 /* set flag, data received will be put into a queue
1322 for later processing */
1323 spin_lock_irq(&garmin_data_p
->lock
);
1324 garmin_data_p
->flags
|= FLAGS_QUEUING
|FLAGS_THROTTLED
;
1325 spin_unlock_irq(&garmin_data_p
->lock
);
1329 static void garmin_unthrottle(struct tty_struct
*tty
)
1331 struct usb_serial_port
*port
= tty
->driver_data
;
1332 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1335 spin_lock_irq(&garmin_data_p
->lock
);
1336 garmin_data_p
->flags
&= ~FLAGS_THROTTLED
;
1337 spin_unlock_irq(&garmin_data_p
->lock
);
1339 /* in native mode send queued data to tty, in
1340 serial mode nothing needs to be done here */
1341 if (garmin_data_p
->mode
== MODE_NATIVE
)
1342 garmin_flush_queue(garmin_data_p
);
1344 if ((garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
) != 0) {
1345 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1348 "%s - failed resubmitting read urb, error %d\n",
1354 * The timer is currently only used to send queued packets to
1355 * the tty in cases where the protocol provides no own handshaking
1356 * to initiate the transfer.
1358 static void timeout_handler(struct timer_list
*t
)
1360 struct garmin_data
*garmin_data_p
= from_timer(garmin_data_p
, t
, timer
);
1362 /* send the next queued packet to the tty port */
1363 if (garmin_data_p
->mode
== MODE_NATIVE
)
1364 if (garmin_data_p
->flags
& FLAGS_QUEUING
)
1365 garmin_flush_queue(garmin_data_p
);
1370 static int garmin_port_probe(struct usb_serial_port
*port
)
1373 struct garmin_data
*garmin_data_p
;
1375 garmin_data_p
= kzalloc(sizeof(struct garmin_data
), GFP_KERNEL
);
1379 timer_setup(&garmin_data_p
->timer
, timeout_handler
, 0);
1380 spin_lock_init(&garmin_data_p
->lock
);
1381 INIT_LIST_HEAD(&garmin_data_p
->pktlist
);
1382 garmin_data_p
->port
= port
;
1383 garmin_data_p
->state
= 0;
1384 garmin_data_p
->flags
= 0;
1385 garmin_data_p
->count
= 0;
1386 init_usb_anchor(&garmin_data_p
->write_urbs
);
1387 usb_set_serial_port_data(port
, garmin_data_p
);
1389 status
= garmin_init_session(port
);
1395 kfree(garmin_data_p
);
1401 static int garmin_port_remove(struct usb_serial_port
*port
)
1403 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1405 usb_kill_anchored_urbs(&garmin_data_p
->write_urbs
);
1406 usb_kill_urb(port
->interrupt_in_urb
);
1407 del_timer_sync(&garmin_data_p
->timer
);
1408 kfree(garmin_data_p
);
1413 /* All of the device info needed */
1414 static struct usb_serial_driver garmin_device
= {
1416 .owner
= THIS_MODULE
,
1417 .name
= "garmin_gps",
1419 .description
= "Garmin GPS usb/tty",
1420 .id_table
= id_table
,
1422 .open
= garmin_open
,
1423 .close
= garmin_close
,
1424 .throttle
= garmin_throttle
,
1425 .unthrottle
= garmin_unthrottle
,
1426 .port_probe
= garmin_port_probe
,
1427 .port_remove
= garmin_port_remove
,
1428 .write
= garmin_write
,
1429 .write_room
= garmin_write_room
,
1430 .write_bulk_callback
= garmin_write_bulk_callback
,
1431 .read_bulk_callback
= garmin_read_bulk_callback
,
1432 .read_int_callback
= garmin_read_int_callback
,
1435 static struct usb_serial_driver
* const serial_drivers
[] = {
1436 &garmin_device
, NULL
1439 module_usb_serial_driver(serial_drivers
, id_table
);
1441 MODULE_AUTHOR(DRIVER_AUTHOR
);
1442 MODULE_DESCRIPTION(DRIVER_DESC
);
1443 MODULE_LICENSE("GPL");
1445 module_param(initial_mode
, int, S_IRUGO
);
1446 MODULE_PARM_DESC(initial_mode
, "Initial mode");