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/slab.h>
29 #include <linux/timer.h>
30 #include <linux/tty.h>
31 #include <linux/tty_driver.h>
32 #include <linux/tty_flip.h>
33 #include <linux/module.h>
34 #include <linux/spinlock.h>
35 #include <linux/uaccess.h>
36 #include <linux/atomic.h>
37 #include <linux/usb.h>
38 #include <linux/usb/serial.h>
40 /* the mode to be set when the port ist opened */
41 static int initial_mode
= 1;
43 #define GARMIN_VENDOR_ID 0x091E
49 #define VERSION_MAJOR 0
50 #define VERSION_MINOR 36
53 #define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
54 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
55 #define DRIVER_AUTHOR "hermann kneissel"
56 #define DRIVER_DESC "garmin gps driver"
58 /* error codes returned by the driver */
59 #define EINVPKT 1000 /* invalid packet structure */
62 /* size of the header of a packet using the usb protocol */
63 #define GARMIN_PKTHDR_LENGTH 12
65 /* max. possible size of a packet using the serial protocol */
66 #define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
68 /* max. possible size of a packet with worst case stuffing */
69 #define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
71 /* size of a buffer able to hold a complete (no stuffing) packet
72 * (the document protocol does not contain packets with a larger
73 * size, but in theory a packet may be 64k+12 bytes - if in
74 * later protocol versions larger packet sizes occur, this value
75 * should be increased accordingly, so the input buffer is always
76 * large enough the store a complete packet inclusive header) */
77 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
79 /* size of a buffer able to hold a complete (incl. stuffing) packet */
80 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
82 /* where to place the packet id of a serial packet, so we can
83 * prepend the usb-packet header without the need to move the
85 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
87 /* max. size of incoming private packets (header+1 param) */
88 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
90 #define GARMIN_LAYERID_TRANSPORT 0
91 #define GARMIN_LAYERID_APPL 20
92 /* our own layer-id to use for some control mechanisms */
93 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
95 #define GARMIN_PKTID_PVT_DATA 51
96 #define GARMIN_PKTID_L001_COMMAND_DATA 10
98 #define CMND_ABORT_TRANSFER 0
100 /* packet ids used in private layer */
101 #define PRIV_PKTID_SET_DEBUG 1
102 #define PRIV_PKTID_SET_MODE 2
103 #define PRIV_PKTID_INFO_REQ 3
104 #define PRIV_PKTID_INFO_RESP 4
105 #define PRIV_PKTID_RESET_REQ 5
106 #define PRIV_PKTID_SET_DEF_MODE 6
114 /* structure used to queue incoming packets */
115 struct garmin_packet
{
116 struct list_head list
;
118 /* the real size of the data array, always > 0 */
123 /* structure used to keep the current state of the driver */
131 struct timer_list timer
;
132 struct usb_serial_port
*port
;
136 __u8 inbuffer
[GPS_IN_BUFSIZ
]; /* tty -> usb */
137 __u8 outbuffer
[GPS_OUT_BUFSIZ
]; /* usb -> tty */
140 struct list_head pktlist
;
145 #define STATE_INITIAL_DELAY 1
146 #define STATE_TIMEOUT 2
147 #define STATE_SESSION_REQ1 3
148 #define STATE_SESSION_REQ2 4
149 #define STATE_ACTIVE 5
151 #define STATE_RESET 8
152 #define STATE_DISCONNECTED 9
153 #define STATE_WAIT_TTY_ACK 10
154 #define STATE_GSP_WAIT_DATA 11
156 #define MODE_NATIVE 0
157 #define MODE_GARMIN_SERIAL 1
159 /* Flags used in garmin_data.flags: */
160 #define FLAGS_SESSION_REPLY_MASK 0x00C0
161 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
162 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
163 #define FLAGS_BULK_IN_ACTIVE 0x0020
164 #define FLAGS_BULK_IN_RESTART 0x0010
165 #define FLAGS_THROTTLED 0x0008
166 #define APP_REQ_SEEN 0x0004
167 #define APP_RESP_SEEN 0x0002
168 #define CLEAR_HALT_REQUIRED 0x0001
170 #define FLAGS_QUEUING 0x0100
171 #define FLAGS_DROP_DATA 0x0800
173 #define FLAGS_GSP_SKIP 0x1000
174 #define FLAGS_GSP_DLESEEN 0x2000
181 /* function prototypes */
182 static int gsp_next_packet(struct garmin_data
*garmin_data_p
);
183 static int garmin_write_bulk(struct usb_serial_port
*port
,
184 const unsigned char *buf
, int count
,
187 /* some special packets to be send or received */
188 static unsigned char const GARMIN_START_SESSION_REQ
[]
189 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
190 static unsigned char const GARMIN_START_SESSION_REPLY
[]
191 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
192 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY
[]
193 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
194 static unsigned char const GARMIN_APP_LAYER_REPLY
[]
196 static unsigned char const GARMIN_START_PVT_REQ
[]
197 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
198 static unsigned char const GARMIN_STOP_PVT_REQ
[]
199 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
200 static unsigned char const GARMIN_STOP_TRANSFER_REQ
[]
201 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
202 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2
[]
203 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
204 static unsigned char const PRIVATE_REQ
[]
205 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
209 static const struct usb_device_id id_table
[] = {
210 /* the same device id seems to be used by all
211 usb enabled GPS devices */
212 { USB_DEVICE(GARMIN_VENDOR_ID
, 3) },
213 { } /* Terminating entry */
215 MODULE_DEVICE_TABLE(usb
, id_table
);
218 static inline int getLayerId(const __u8
*usbPacket
)
220 return __le32_to_cpup((__le32
*)(usbPacket
));
223 static inline int getPacketId(const __u8
*usbPacket
)
225 return __le32_to_cpup((__le32
*)(usbPacket
+4));
228 static inline int getDataLength(const __u8
*usbPacket
)
230 return __le32_to_cpup((__le32
*)(usbPacket
+8));
235 * check if the usb-packet in buf contains an abort-transfer command.
236 * (if yes, all queued data will be dropped)
238 static inline int isAbortTrfCmnd(const unsigned char *buf
)
240 if (0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ
,
241 sizeof(GARMIN_STOP_TRANSFER_REQ
)) ||
242 0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ_V2
,
243 sizeof(GARMIN_STOP_TRANSFER_REQ_V2
)))
251 static void send_to_tty(struct usb_serial_port
*port
,
252 char *data
, unsigned int actual_length
)
255 usb_serial_debug_data(&port
->dev
, __func__
, actual_length
, data
);
256 tty_insert_flip_string(&port
->port
, data
, actual_length
);
257 tty_flip_buffer_push(&port
->port
);
262 /******************************************************************************
263 * packet queue handling
264 ******************************************************************************/
267 * queue a received (usb-)packet for later processing
269 static int pkt_add(struct garmin_data
*garmin_data_p
,
270 unsigned char *data
, unsigned int data_length
)
275 struct garmin_packet
*pkt
;
277 /* process only packets containing data ... */
279 pkt
= kmalloc(sizeof(struct garmin_packet
)+data_length
,
284 pkt
->size
= data_length
;
285 memcpy(pkt
->data
, data
, data_length
);
287 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
288 garmin_data_p
->flags
|= FLAGS_QUEUING
;
289 result
= list_empty(&garmin_data_p
->pktlist
);
290 pkt
->seq
= garmin_data_p
->seq_counter
++;
291 list_add_tail(&pkt
->list
, &garmin_data_p
->pktlist
);
292 state
= garmin_data_p
->state
;
293 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
295 dev_dbg(&garmin_data_p
->port
->dev
,
296 "%s - added: pkt: %d - %d bytes\n", __func__
,
297 pkt
->seq
, data_length
);
299 /* in serial mode, if someone is waiting for data from
300 the device, convert and send the next packet to tty. */
301 if (result
&& (state
== STATE_GSP_WAIT_DATA
))
302 gsp_next_packet(garmin_data_p
);
308 /* get the next pending packet */
309 static struct garmin_packet
*pkt_pop(struct garmin_data
*garmin_data_p
)
312 struct garmin_packet
*result
= NULL
;
314 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
315 if (!list_empty(&garmin_data_p
->pktlist
)) {
316 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
317 list_del(&result
->list
);
319 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
324 /* free up all queued data */
325 static void pkt_clear(struct garmin_data
*garmin_data_p
)
328 struct garmin_packet
*result
= NULL
;
330 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
331 while (!list_empty(&garmin_data_p
->pktlist
)) {
332 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
333 list_del(&result
->list
);
336 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
340 /******************************************************************************
341 * garmin serial protocol handling handling
342 ******************************************************************************/
344 /* send an ack packet back to the tty */
345 static int gsp_send_ack(struct garmin_data
*garmin_data_p
, __u8 pkt_id
)
352 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt-id: 0x%X.\n", __func__
,
369 *ptr
++ = 0xFF & (-cksum
);
375 send_to_tty(garmin_data_p
->port
, pkt
, l
);
382 * called for a complete packet received from tty layer
384 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
385 * at GSP_INITIAL_OFFSET.
387 * count - number of bytes in the input buffer including space reserved for
388 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
389 * (including pkt-id, data-length a. cksum)
391 static int gsp_rec_packet(struct garmin_data
*garmin_data_p
, int count
)
393 struct device
*dev
= &garmin_data_p
->port
->dev
;
395 const __u8
*recpkt
= garmin_data_p
->inbuffer
+GSP_INITIAL_OFFSET
;
396 __le32
*usbdata
= (__le32
*) garmin_data_p
->inbuffer
;
399 int pktid
= recpkt
[0];
400 int size
= recpkt
[1];
402 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
,
403 count
-GSP_INITIAL_OFFSET
, recpkt
);
405 if (size
!= (count
-GSP_INITIAL_OFFSET
-3)) {
406 dev_dbg(dev
, "%s - invalid size, expected %d bytes, got %d\n",
407 __func__
, size
, (count
-GSP_INITIAL_OFFSET
-3));
414 /* sanity check, remove after test ... */
415 if ((__u8
*)&(usbdata
[3]) != recpkt
) {
416 dev_dbg(dev
, "%s - ptr mismatch %p - %p\n", __func__
,
417 &(usbdata
[4]), recpkt
);
426 if ((0xff & (cksum
+ *recpkt
)) != 0) {
427 dev_dbg(dev
, "%s - invalid checksum, expected %02x, got %02x\n",
428 __func__
, 0xff & -cksum
, 0xff & *recpkt
);
432 usbdata
[0] = __cpu_to_le32(GARMIN_LAYERID_APPL
);
433 usbdata
[1] = __cpu_to_le32(pktid
);
434 usbdata
[2] = __cpu_to_le32(size
);
436 garmin_write_bulk(garmin_data_p
->port
, garmin_data_p
->inbuffer
,
437 GARMIN_PKTHDR_LENGTH
+size
, 0);
439 /* if this was an abort-transfer command, flush all
441 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
442 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
443 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
444 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
445 pkt_clear(garmin_data_p
);
454 * Called for data received from tty
456 * buf contains the data read, it may span more than one packet or even
459 * input record should be a serial-record, but it may not be complete.
460 * Copy it into our local buffer, until an etx is seen (or an error
462 * Once the record is complete, convert into a usb packet and send it
463 * to the bulk pipe, send an ack back to the tty.
465 * If the input is an ack, just send the last queued packet to the
468 * if the input is an abort command, drop all queued data.
471 static int gsp_receive(struct garmin_data
*garmin_data_p
,
472 const unsigned char *buf
, int count
)
474 struct device
*dev
= &garmin_data_p
->port
->dev
;
477 int ack_or_nak_seen
= 0;
480 /* dleSeen: set if last byte read was a DLE */
482 /* skip: if set, skip incoming data until possible start of
488 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
489 dest
= garmin_data_p
->inbuffer
;
490 size
= garmin_data_p
->insize
;
491 dleSeen
= garmin_data_p
->flags
& FLAGS_GSP_DLESEEN
;
492 skip
= garmin_data_p
->flags
& FLAGS_GSP_SKIP
;
493 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
495 /* dev_dbg(dev, "%s - dle=%d skip=%d size=%d count=%d\n",
496 __func__, dleSeen, skip, size, count); */
499 size
= GSP_INITIAL_OFFSET
;
501 while (offs
< count
) {
507 if (skip
) { /* start of a new pkt */
509 size
= GSP_INITIAL_OFFSET
;
511 } else if (dleSeen
) {
517 } else if (data
== ETX
) {
519 /* packet complete */
521 data
= dest
[GSP_INITIAL_OFFSET
];
524 ack_or_nak_seen
= ACK
;
525 dev_dbg(dev
, "ACK packet complete.\n");
526 } else if (data
== NAK
) {
527 ack_or_nak_seen
= NAK
;
528 dev_dbg(dev
, "NAK packet complete.\n");
530 dev_dbg(dev
, "packet complete - id=0x%X.\n",
532 gsp_rec_packet(garmin_data_p
, size
);
536 size
= GSP_INITIAL_OFFSET
;
544 size
= GSP_INITIAL_OFFSET
;
551 if (size
>= GPS_IN_BUFSIZ
) {
552 dev_dbg(dev
, "%s - packet too large.\n", __func__
);
554 size
= GSP_INITIAL_OFFSET
;
559 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
561 garmin_data_p
->insize
= size
;
563 /* copy flags back to structure */
565 garmin_data_p
->flags
|= FLAGS_GSP_SKIP
;
567 garmin_data_p
->flags
&= ~FLAGS_GSP_SKIP
;
570 garmin_data_p
->flags
|= FLAGS_GSP_DLESEEN
;
572 garmin_data_p
->flags
&= ~FLAGS_GSP_DLESEEN
;
574 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
576 if (ack_or_nak_seen
) {
577 if (gsp_next_packet(garmin_data_p
) > 0)
578 garmin_data_p
->state
= STATE_ACTIVE
;
580 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
588 * Sends a usb packet to the tty
590 * Assumes, that all packages and at an usb-packet boundary.
592 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
594 static int gsp_send(struct garmin_data
*garmin_data_p
,
595 const unsigned char *buf
, int count
)
597 struct device
*dev
= &garmin_data_p
->port
->dev
;
598 const unsigned char *src
;
606 dev_dbg(dev
, "%s - state %d - %d bytes.\n", __func__
,
607 garmin_data_p
->state
, count
);
609 k
= garmin_data_p
->outsize
;
610 if ((k
+count
) > GPS_OUT_BUFSIZ
) {
611 dev_dbg(dev
, "packet too large\n");
612 garmin_data_p
->outsize
= 0;
616 memcpy(garmin_data_p
->outbuffer
+k
, buf
, count
);
618 garmin_data_p
->outsize
= k
;
620 if (k
>= GARMIN_PKTHDR_LENGTH
) {
621 pktid
= getPacketId(garmin_data_p
->outbuffer
);
622 datalen
= getDataLength(garmin_data_p
->outbuffer
);
623 i
= GARMIN_PKTHDR_LENGTH
+ datalen
;
630 dev_dbg(dev
, "%s - %d bytes in buffer, %d bytes in pkt.\n", __func__
, k
, i
);
632 /* garmin_data_p->outbuffer now contains a complete packet */
634 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
, k
,
635 garmin_data_p
->outbuffer
);
637 garmin_data_p
->outsize
= 0;
639 if (GARMIN_LAYERID_APPL
!= getLayerId(garmin_data_p
->outbuffer
)) {
640 dev_dbg(dev
, "not an application packet (%d)\n",
641 getLayerId(garmin_data_p
->outbuffer
));
646 dev_dbg(dev
, "packet-id %d too large\n", pktid
);
651 dev_dbg(dev
, "packet-size %d too large\n", datalen
);
655 /* the serial protocol should be able to handle this packet */
658 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
659 for (i
= 0; i
< datalen
; i
++) {
664 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
665 if (k
> (GARMIN_PKTHDR_LENGTH
-2)) {
666 /* can't add stuffing DLEs in place, move data to end
668 dst
= garmin_data_p
->outbuffer
+GPS_OUT_BUFSIZ
-datalen
;
669 memcpy(dst
, src
, datalen
);
673 dst
= garmin_data_p
->outbuffer
;
683 for (i
= 0; i
< datalen
; i
++) {
691 cksum
= 0xFF & -cksum
;
698 i
= dst
-garmin_data_p
->outbuffer
;
700 send_to_tty(garmin_data_p
->port
, garmin_data_p
->outbuffer
, i
);
702 garmin_data_p
->pkt_id
= pktid
;
703 garmin_data_p
->state
= STATE_WAIT_TTY_ACK
;
710 * Process the next pending data packet - if there is one
712 static int gsp_next_packet(struct garmin_data
*garmin_data_p
)
715 struct garmin_packet
*pkt
= NULL
;
717 while ((pkt
= pkt_pop(garmin_data_p
)) != NULL
) {
718 dev_dbg(&garmin_data_p
->port
->dev
, "%s - next pkt: %d\n", __func__
, pkt
->seq
);
719 result
= gsp_send(garmin_data_p
, pkt
->data
, pkt
->size
);
731 /******************************************************************************
733 ******************************************************************************/
737 * Called for data received from tty
739 * The input data is expected to be in garmin usb-packet format.
741 * buf contains the data read, it may span more than one packet
742 * or even incomplete packets
744 static int nat_receive(struct garmin_data
*garmin_data_p
,
745 const unsigned char *buf
, int count
)
753 while (offs
< count
) {
754 /* if buffer contains header, copy rest of data */
755 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
)
756 len
= GARMIN_PKTHDR_LENGTH
757 +getDataLength(garmin_data_p
->inbuffer
);
759 len
= GARMIN_PKTHDR_LENGTH
;
761 if (len
>= GPS_IN_BUFSIZ
) {
762 /* seems to be an invalid packet, ignore rest
764 dev_dbg(&garmin_data_p
->port
->dev
,
765 "%s - packet size too large: %d\n",
767 garmin_data_p
->insize
= 0;
771 len
-= garmin_data_p
->insize
;
772 if (len
> (count
-offs
))
775 dest
= garmin_data_p
->inbuffer
776 + garmin_data_p
->insize
;
777 memcpy(dest
, buf
+offs
, len
);
778 garmin_data_p
->insize
+= len
;
783 /* do we have a complete packet ? */
784 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
) {
785 len
= GARMIN_PKTHDR_LENGTH
+
786 getDataLength(garmin_data_p
->inbuffer
);
787 if (garmin_data_p
->insize
>= len
) {
788 garmin_write_bulk(garmin_data_p
->port
,
789 garmin_data_p
->inbuffer
,
791 garmin_data_p
->insize
= 0;
793 /* if this was an abort-transfer command,
794 flush all queued data. */
795 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
796 spin_lock_irqsave(&garmin_data_p
->lock
,
798 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
799 spin_unlock_irqrestore(
800 &garmin_data_p
->lock
, flags
);
801 pkt_clear(garmin_data_p
);
810 /******************************************************************************
812 ******************************************************************************/
814 static void priv_status_resp(struct usb_serial_port
*port
)
816 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
817 __le32
*pkt
= (__le32
*)garmin_data_p
->privpkt
;
819 pkt
[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE
);
820 pkt
[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP
);
821 pkt
[2] = __cpu_to_le32(12);
822 pkt
[3] = __cpu_to_le32(VERSION_MAJOR
<< 16 | VERSION_MINOR
);
823 pkt
[4] = __cpu_to_le32(garmin_data_p
->mode
);
824 pkt
[5] = __cpu_to_le32(garmin_data_p
->serial_num
);
826 send_to_tty(port
, (__u8
*)pkt
, 6 * 4);
830 /******************************************************************************
831 * Garmin specific driver functions
832 ******************************************************************************/
834 static int process_resetdev_request(struct usb_serial_port
*port
)
838 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
840 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
841 garmin_data_p
->flags
&= ~(CLEAR_HALT_REQUIRED
);
842 garmin_data_p
->state
= STATE_RESET
;
843 garmin_data_p
->serial_num
= 0;
844 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
846 usb_kill_urb(port
->interrupt_in_urb
);
847 dev_dbg(&port
->dev
, "%s - usb_reset_device\n", __func__
);
848 status
= usb_reset_device(port
->serial
->dev
);
850 dev_dbg(&port
->dev
, "%s - usb_reset_device failed: %d\n",
858 * clear all cached data
860 static int garmin_clear(struct garmin_data
*garmin_data_p
)
865 /* flush all queued data */
866 pkt_clear(garmin_data_p
);
868 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
869 garmin_data_p
->insize
= 0;
870 garmin_data_p
->outsize
= 0;
871 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
877 static int garmin_init_session(struct usb_serial_port
*port
)
879 struct usb_serial
*serial
= port
->serial
;
880 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
885 usb_kill_urb(port
->interrupt_in_urb
);
887 dev_dbg(&serial
->dev
->dev
, "%s - adding interrupt input\n", __func__
);
888 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
890 dev_err(&serial
->dev
->dev
,
891 "%s - failed submitting interrupt urb, error %d\n",
896 * using the initialization method from gpsbabel. See comments in
897 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
900 dev_dbg(&serial
->dev
->dev
, "%s - starting session ...\n", __func__
);
901 garmin_data_p
->state
= STATE_ACTIVE
;
903 for (i
= 0; i
< 3; i
++) {
904 status
= garmin_write_bulk(port
,
905 GARMIN_START_SESSION_REQ
,
906 sizeof(GARMIN_START_SESSION_REQ
), 0);
921 static int garmin_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
925 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
927 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
928 garmin_data_p
->mode
= initial_mode
;
929 garmin_data_p
->count
= 0;
930 garmin_data_p
->flags
&= FLAGS_SESSION_REPLY1_SEEN
;
931 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
933 /* shutdown any bulk reads that might be going on */
934 usb_kill_urb(port
->write_urb
);
935 usb_kill_urb(port
->read_urb
);
937 if (garmin_data_p
->state
== STATE_RESET
)
938 status
= garmin_init_session(port
);
940 garmin_data_p
->state
= STATE_ACTIVE
;
945 static void garmin_close(struct usb_serial_port
*port
)
947 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
949 dev_dbg(&port
->dev
, "%s - mode=%d state=%d flags=0x%X\n",
950 __func__
, garmin_data_p
->mode
, garmin_data_p
->state
,
951 garmin_data_p
->flags
);
953 garmin_clear(garmin_data_p
);
955 /* shutdown our urbs */
956 usb_kill_urb(port
->read_urb
);
957 usb_kill_urb(port
->write_urb
);
959 /* keep reset state so we know that we must start a new session */
960 if (garmin_data_p
->state
!= STATE_RESET
)
961 garmin_data_p
->state
= STATE_DISCONNECTED
;
965 static void garmin_write_bulk_callback(struct urb
*urb
)
967 struct usb_serial_port
*port
= urb
->context
;
970 struct garmin_data
*garmin_data_p
=
971 usb_get_serial_port_data(port
);
973 if (GARMIN_LAYERID_APPL
== getLayerId(urb
->transfer_buffer
)) {
975 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
976 gsp_send_ack(garmin_data_p
,
977 ((__u8
*)urb
->transfer_buffer
)[4]);
980 usb_serial_port_softint(port
);
983 /* Ignore errors that resulted from garmin_write_bulk with
986 /* free up the transfer buffer, as usb_free_urb() does not do this */
987 kfree(urb
->transfer_buffer
);
991 static int garmin_write_bulk(struct usb_serial_port
*port
,
992 const unsigned char *buf
, int count
,
996 struct usb_serial
*serial
= port
->serial
;
997 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
999 unsigned char *buffer
;
1002 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1003 garmin_data_p
->flags
&= ~FLAGS_DROP_DATA
;
1004 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1006 buffer
= kmalloc(count
, GFP_ATOMIC
);
1010 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1016 memcpy(buffer
, buf
, count
);
1018 usb_serial_debug_data(&port
->dev
, __func__
, count
, buffer
);
1020 usb_fill_bulk_urb(urb
, serial
->dev
,
1021 usb_sndbulkpipe(serial
->dev
,
1022 port
->bulk_out_endpointAddress
),
1024 garmin_write_bulk_callback
,
1025 dismiss_ack
? NULL
: port
);
1026 urb
->transfer_flags
|= URB_ZERO_PACKET
;
1028 if (GARMIN_LAYERID_APPL
== getLayerId(buffer
)) {
1030 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1031 garmin_data_p
->flags
|= APP_REQ_SEEN
;
1032 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1034 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1035 pkt_clear(garmin_data_p
);
1036 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
1040 /* send it down the pipe */
1041 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1044 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1049 /* we are done with this urb, so let the host driver
1050 * really free it when it is finished with it */
1056 static int garmin_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1057 const unsigned char *buf
, int count
)
1059 struct device
*dev
= &port
->dev
;
1060 int pktid
, pktsiz
, len
;
1061 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1062 __le32
*privpkt
= (__le32
*)garmin_data_p
->privpkt
;
1064 usb_serial_debug_data(dev
, __func__
, count
, buf
);
1066 if (garmin_data_p
->state
== STATE_RESET
)
1069 /* check for our private packets */
1070 if (count
>= GARMIN_PKTHDR_LENGTH
) {
1075 memcpy(garmin_data_p
->privpkt
, buf
, len
);
1077 pktsiz
= getDataLength(garmin_data_p
->privpkt
);
1078 pktid
= getPacketId(garmin_data_p
->privpkt
);
1080 if (count
== (GARMIN_PKTHDR_LENGTH
+pktsiz
)
1081 && GARMIN_LAYERID_PRIVATE
==
1082 getLayerId(garmin_data_p
->privpkt
)) {
1084 dev_dbg(dev
, "%s - processing private request %d\n",
1087 /* drop all unfinished transfers */
1088 garmin_clear(garmin_data_p
);
1091 case PRIV_PKTID_SET_MODE
:
1094 garmin_data_p
->mode
= __le32_to_cpu(privpkt
[3]);
1095 dev_dbg(dev
, "%s - mode set to %d\n",
1096 __func__
, garmin_data_p
->mode
);
1099 case PRIV_PKTID_INFO_REQ
:
1100 priv_status_resp(port
);
1103 case PRIV_PKTID_RESET_REQ
:
1104 process_resetdev_request(port
);
1107 case PRIV_PKTID_SET_DEF_MODE
:
1110 initial_mode
= __le32_to_cpu(privpkt
[3]);
1111 dev_dbg(dev
, "%s - initial_mode set to %d\n",
1113 garmin_data_p
->mode
);
1120 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1121 return gsp_receive(garmin_data_p
, buf
, count
);
1122 } else { /* MODE_NATIVE */
1123 return nat_receive(garmin_data_p
, buf
, count
);
1128 static int garmin_write_room(struct tty_struct
*tty
)
1130 struct usb_serial_port
*port
= tty
->driver_data
;
1132 * Report back the bytes currently available in the output buffer.
1134 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1135 return GPS_OUT_BUFSIZ
-garmin_data_p
->outsize
;
1139 static void garmin_read_process(struct garmin_data
*garmin_data_p
,
1140 unsigned char *data
, unsigned data_length
,
1143 unsigned long flags
;
1145 if (garmin_data_p
->flags
& FLAGS_DROP_DATA
) {
1146 /* abort-transfer cmd is active */
1147 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt dropped\n", __func__
);
1148 } else if (garmin_data_p
->state
!= STATE_DISCONNECTED
&&
1149 garmin_data_p
->state
!= STATE_RESET
) {
1151 /* if throttling is active or postprecessing is required
1152 put the received data in the input queue, otherwise
1153 send it directly to the tty port */
1154 if (garmin_data_p
->flags
& FLAGS_QUEUING
) {
1155 pkt_add(garmin_data_p
, data
, data_length
);
1156 } else if (bulk_data
||
1157 getLayerId(data
) == GARMIN_LAYERID_APPL
) {
1159 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1160 garmin_data_p
->flags
|= APP_RESP_SEEN
;
1161 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1163 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1164 pkt_add(garmin_data_p
, data
, data_length
);
1166 send_to_tty(garmin_data_p
->port
, data
,
1170 /* ignore system layer packets ... */
1175 static void garmin_read_bulk_callback(struct urb
*urb
)
1177 unsigned long flags
;
1178 struct usb_serial_port
*port
= urb
->context
;
1179 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1180 unsigned char *data
= urb
->transfer_buffer
;
1181 int status
= urb
->status
;
1185 dev_dbg(&urb
->dev
->dev
, "%s - nonzero read bulk status received: %d\n",
1190 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
1192 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 1);
1194 if (urb
->actual_length
== 0 &&
1195 0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_RESTART
)) {
1196 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1197 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_RESTART
;
1198 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1199 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1202 "%s - failed resubmitting read urb, error %d\n",
1204 } else if (urb
->actual_length
> 0) {
1205 /* Continue trying to read until nothing more is received */
1206 if (0 == (garmin_data_p
->flags
& FLAGS_THROTTLED
)) {
1207 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1210 "%s - failed resubmitting read urb, error %d\n",
1214 dev_dbg(&port
->dev
, "%s - end of bulk data\n", __func__
);
1215 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1216 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_ACTIVE
;
1217 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1222 static void garmin_read_int_callback(struct urb
*urb
)
1224 unsigned long flags
;
1226 struct usb_serial_port
*port
= urb
->context
;
1227 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1228 unsigned char *data
= urb
->transfer_buffer
;
1229 int status
= urb
->status
;
1238 /* this urb is terminated, clean up */
1239 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n",
1243 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n",
1248 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
,
1249 urb
->transfer_buffer
);
1251 if (urb
->actual_length
== sizeof(GARMIN_BULK_IN_AVAIL_REPLY
) &&
1252 0 == memcmp(data
, GARMIN_BULK_IN_AVAIL_REPLY
,
1253 sizeof(GARMIN_BULK_IN_AVAIL_REPLY
))) {
1255 dev_dbg(&port
->dev
, "%s - bulk data available.\n", __func__
);
1257 if (0 == (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1259 /* bulk data available */
1260 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1263 "%s - failed submitting read urb, error %d\n",
1266 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1267 garmin_data_p
->flags
|= FLAGS_BULK_IN_ACTIVE
;
1268 spin_unlock_irqrestore(&garmin_data_p
->lock
,
1272 /* bulk-in transfer still active */
1273 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1274 garmin_data_p
->flags
|= FLAGS_BULK_IN_RESTART
;
1275 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1278 } else if (urb
->actual_length
== (4+sizeof(GARMIN_START_SESSION_REPLY
))
1279 && 0 == memcmp(data
, GARMIN_START_SESSION_REPLY
,
1280 sizeof(GARMIN_START_SESSION_REPLY
))) {
1282 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1283 garmin_data_p
->flags
|= FLAGS_SESSION_REPLY1_SEEN
;
1284 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1286 /* save the serial number */
1287 garmin_data_p
->serial_num
= __le32_to_cpup(
1288 (__le32
*)(data
+GARMIN_PKTHDR_LENGTH
));
1290 dev_dbg(&port
->dev
, "%s - start-of-session reply seen - serial %u.\n",
1291 __func__
, garmin_data_p
->serial_num
);
1294 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 0);
1296 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1298 dev_err(&urb
->dev
->dev
,
1299 "%s - Error %d submitting interrupt urb\n",
1305 * Sends the next queued packt to the tty port (garmin native mode only)
1306 * and then sets a timer to call itself again until all queued data
1309 static int garmin_flush_queue(struct garmin_data
*garmin_data_p
)
1311 unsigned long flags
;
1312 struct garmin_packet
*pkt
;
1314 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1315 pkt
= pkt_pop(garmin_data_p
);
1317 send_to_tty(garmin_data_p
->port
, pkt
->data
, pkt
->size
);
1319 mod_timer(&garmin_data_p
->timer
, (1)+jiffies
);
1322 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1323 garmin_data_p
->flags
&= ~FLAGS_QUEUING
;
1324 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1331 static void garmin_throttle(struct tty_struct
*tty
)
1333 struct usb_serial_port
*port
= tty
->driver_data
;
1334 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1336 /* set flag, data received will be put into a queue
1337 for later processing */
1338 spin_lock_irq(&garmin_data_p
->lock
);
1339 garmin_data_p
->flags
|= FLAGS_QUEUING
|FLAGS_THROTTLED
;
1340 spin_unlock_irq(&garmin_data_p
->lock
);
1344 static void garmin_unthrottle(struct tty_struct
*tty
)
1346 struct usb_serial_port
*port
= tty
->driver_data
;
1347 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1350 spin_lock_irq(&garmin_data_p
->lock
);
1351 garmin_data_p
->flags
&= ~FLAGS_THROTTLED
;
1352 spin_unlock_irq(&garmin_data_p
->lock
);
1354 /* in native mode send queued data to tty, in
1355 serial mode nothing needs to be done here */
1356 if (garmin_data_p
->mode
== MODE_NATIVE
)
1357 garmin_flush_queue(garmin_data_p
);
1359 if (0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1360 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1363 "%s - failed resubmitting read urb, error %d\n",
1369 * The timer is currently only used to send queued packets to
1370 * the tty in cases where the protocol provides no own handshaking
1371 * to initiate the transfer.
1373 static void timeout_handler(unsigned long data
)
1375 struct garmin_data
*garmin_data_p
= (struct garmin_data
*) data
;
1377 /* send the next queued packet to the tty port */
1378 if (garmin_data_p
->mode
== MODE_NATIVE
)
1379 if (garmin_data_p
->flags
& FLAGS_QUEUING
)
1380 garmin_flush_queue(garmin_data_p
);
1385 static int garmin_port_probe(struct usb_serial_port
*port
)
1388 struct garmin_data
*garmin_data_p
;
1390 garmin_data_p
= kzalloc(sizeof(struct garmin_data
), GFP_KERNEL
);
1394 init_timer(&garmin_data_p
->timer
);
1395 spin_lock_init(&garmin_data_p
->lock
);
1396 INIT_LIST_HEAD(&garmin_data_p
->pktlist
);
1397 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1398 garmin_data_p
->timer
.data
= (unsigned long)garmin_data_p
;
1399 garmin_data_p
->timer
.function
= timeout_handler
;
1400 garmin_data_p
->port
= port
;
1401 garmin_data_p
->state
= 0;
1402 garmin_data_p
->flags
= 0;
1403 garmin_data_p
->count
= 0;
1404 usb_set_serial_port_data(port
, garmin_data_p
);
1406 status
= garmin_init_session(port
);
1412 static int garmin_port_remove(struct usb_serial_port
*port
)
1414 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1416 usb_kill_urb(port
->interrupt_in_urb
);
1417 del_timer_sync(&garmin_data_p
->timer
);
1418 kfree(garmin_data_p
);
1423 /* All of the device info needed */
1424 static struct usb_serial_driver garmin_device
= {
1426 .owner
= THIS_MODULE
,
1427 .name
= "garmin_gps",
1429 .description
= "Garmin GPS usb/tty",
1430 .id_table
= id_table
,
1432 .open
= garmin_open
,
1433 .close
= garmin_close
,
1434 .throttle
= garmin_throttle
,
1435 .unthrottle
= garmin_unthrottle
,
1436 .port_probe
= garmin_port_probe
,
1437 .port_remove
= garmin_port_remove
,
1438 .write
= garmin_write
,
1439 .write_room
= garmin_write_room
,
1440 .write_bulk_callback
= garmin_write_bulk_callback
,
1441 .read_bulk_callback
= garmin_read_bulk_callback
,
1442 .read_int_callback
= garmin_read_int_callback
,
1445 static struct usb_serial_driver
* const serial_drivers
[] = {
1446 &garmin_device
, NULL
1449 module_usb_serial_driver(serial_drivers
, id_table
);
1451 MODULE_AUTHOR(DRIVER_AUTHOR
);
1452 MODULE_DESCRIPTION(DRIVER_DESC
);
1453 MODULE_LICENSE("GPL");
1455 module_param(initial_mode
, int, S_IRUGO
);
1456 MODULE_PARM_DESC(initial_mode
, "Initial mode");