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
;
141 struct usb_anchor write_urbs
;
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 (memcmp(buf
, GARMIN_STOP_TRANSFER_REQ
,
242 sizeof(GARMIN_STOP_TRANSFER_REQ
)) == 0 ||
243 memcmp(buf
, GARMIN_STOP_TRANSFER_REQ_V2
,
244 sizeof(GARMIN_STOP_TRANSFER_REQ_V2
)) == 0)
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 containing data ... */
280 pkt
= kmalloc(sizeof(struct garmin_packet
)+data_length
,
285 pkt
->size
= data_length
;
286 memcpy(pkt
->data
, data
, data_length
);
288 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
289 garmin_data_p
->flags
|= FLAGS_QUEUING
;
290 result
= list_empty(&garmin_data_p
->pktlist
);
291 pkt
->seq
= garmin_data_p
->seq_counter
++;
292 list_add_tail(&pkt
->list
, &garmin_data_p
->pktlist
);
293 state
= garmin_data_p
->state
;
294 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
296 dev_dbg(&garmin_data_p
->port
->dev
,
297 "%s - added: pkt: %d - %d bytes\n", __func__
,
298 pkt
->seq
, data_length
);
300 /* in serial mode, if someone is waiting for data from
301 the device, convert and send the next packet to tty. */
302 if (result
&& (state
== STATE_GSP_WAIT_DATA
))
303 gsp_next_packet(garmin_data_p
);
309 /* get the next pending packet */
310 static struct garmin_packet
*pkt_pop(struct garmin_data
*garmin_data_p
)
313 struct garmin_packet
*result
= NULL
;
315 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
316 if (!list_empty(&garmin_data_p
->pktlist
)) {
317 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
318 list_del(&result
->list
);
320 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
325 /* free up all queued data */
326 static void pkt_clear(struct garmin_data
*garmin_data_p
)
329 struct garmin_packet
*result
= NULL
;
331 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
332 while (!list_empty(&garmin_data_p
->pktlist
)) {
333 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
334 list_del(&result
->list
);
337 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
341 /******************************************************************************
342 * garmin serial protocol handling handling
343 ******************************************************************************/
345 /* send an ack packet back to the tty */
346 static int gsp_send_ack(struct garmin_data
*garmin_data_p
, __u8 pkt_id
)
353 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt-id: 0x%X.\n", __func__
,
370 *ptr
++ = (-cksum
) & 0xFF;
376 send_to_tty(garmin_data_p
->port
, pkt
, l
);
383 * called for a complete packet received from tty layer
385 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
386 * at GSP_INITIAL_OFFSET.
388 * count - number of bytes in the input buffer including space reserved for
389 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
390 * (including pkt-id, data-length a. cksum)
392 static int gsp_rec_packet(struct garmin_data
*garmin_data_p
, int count
)
394 struct device
*dev
= &garmin_data_p
->port
->dev
;
396 const __u8
*recpkt
= garmin_data_p
->inbuffer
+GSP_INITIAL_OFFSET
;
397 __le32
*usbdata
= (__le32
*) garmin_data_p
->inbuffer
;
400 int pktid
= recpkt
[0];
401 int size
= recpkt
[1];
403 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
,
404 count
-GSP_INITIAL_OFFSET
, recpkt
);
406 if (size
!= (count
-GSP_INITIAL_OFFSET
-3)) {
407 dev_dbg(dev
, "%s - invalid size, expected %d bytes, got %d\n",
408 __func__
, size
, (count
-GSP_INITIAL_OFFSET
-3));
415 /* sanity check, remove after test ... */
416 if ((__u8
*)&(usbdata
[3]) != recpkt
) {
417 dev_dbg(dev
, "%s - ptr mismatch %p - %p\n", __func__
,
418 &(usbdata
[4]), recpkt
);
427 if (((cksum
+ *recpkt
) & 0xff) != 0) {
428 dev_dbg(dev
, "%s - invalid checksum, expected %02x, got %02x\n",
429 __func__
, -cksum
& 0xff, *recpkt
);
433 usbdata
[0] = __cpu_to_le32(GARMIN_LAYERID_APPL
);
434 usbdata
[1] = __cpu_to_le32(pktid
);
435 usbdata
[2] = __cpu_to_le32(size
);
437 garmin_write_bulk(garmin_data_p
->port
, garmin_data_p
->inbuffer
,
438 GARMIN_PKTHDR_LENGTH
+size
, 0);
440 /* if this was an abort-transfer command, flush all
442 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
443 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
444 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
445 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
446 pkt_clear(garmin_data_p
);
455 * Called for data received from tty
457 * buf contains the data read, it may span more than one packet or even
460 * input record should be a serial-record, but it may not be complete.
461 * Copy it into our local buffer, until an etx is seen (or an error
463 * Once the record is complete, convert into a usb packet and send it
464 * to the bulk pipe, send an ack back to the tty.
466 * If the input is an ack, just send the last queued packet to the
469 * if the input is an abort command, drop all queued data.
472 static int gsp_receive(struct garmin_data
*garmin_data_p
,
473 const unsigned char *buf
, int count
)
475 struct device
*dev
= &garmin_data_p
->port
->dev
;
478 int ack_or_nak_seen
= 0;
481 /* dleSeen: set if last byte read was a DLE */
483 /* skip: if set, skip incoming data until possible start of
489 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
490 dest
= garmin_data_p
->inbuffer
;
491 size
= garmin_data_p
->insize
;
492 dleSeen
= garmin_data_p
->flags
& FLAGS_GSP_DLESEEN
;
493 skip
= garmin_data_p
->flags
& FLAGS_GSP_SKIP
;
494 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
496 /* dev_dbg(dev, "%s - dle=%d skip=%d size=%d count=%d\n",
497 __func__, dleSeen, skip, size, count); */
500 size
= GSP_INITIAL_OFFSET
;
502 while (offs
< count
) {
508 if (skip
) { /* start of a new pkt */
510 size
= GSP_INITIAL_OFFSET
;
512 } else if (dleSeen
) {
518 } else if (data
== ETX
) {
520 /* packet complete */
522 data
= dest
[GSP_INITIAL_OFFSET
];
525 ack_or_nak_seen
= ACK
;
526 dev_dbg(dev
, "ACK packet complete.\n");
527 } else if (data
== NAK
) {
528 ack_or_nak_seen
= NAK
;
529 dev_dbg(dev
, "NAK packet complete.\n");
531 dev_dbg(dev
, "packet complete - id=0x%X.\n",
533 gsp_rec_packet(garmin_data_p
, size
);
537 size
= GSP_INITIAL_OFFSET
;
545 size
= GSP_INITIAL_OFFSET
;
552 if (size
>= GPS_IN_BUFSIZ
) {
553 dev_dbg(dev
, "%s - packet too large.\n", __func__
);
555 size
= GSP_INITIAL_OFFSET
;
560 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
562 garmin_data_p
->insize
= size
;
564 /* copy flags back to structure */
566 garmin_data_p
->flags
|= FLAGS_GSP_SKIP
;
568 garmin_data_p
->flags
&= ~FLAGS_GSP_SKIP
;
571 garmin_data_p
->flags
|= FLAGS_GSP_DLESEEN
;
573 garmin_data_p
->flags
&= ~FLAGS_GSP_DLESEEN
;
575 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
577 if (ack_or_nak_seen
) {
578 if (gsp_next_packet(garmin_data_p
) > 0)
579 garmin_data_p
->state
= STATE_ACTIVE
;
581 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
589 * Sends a usb packet to the tty
591 * Assumes, that all packages and at an usb-packet boundary.
593 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
595 static int gsp_send(struct garmin_data
*garmin_data_p
,
596 const unsigned char *buf
, int count
)
598 struct device
*dev
= &garmin_data_p
->port
->dev
;
599 const unsigned char *src
;
607 dev_dbg(dev
, "%s - state %d - %d bytes.\n", __func__
,
608 garmin_data_p
->state
, count
);
610 k
= garmin_data_p
->outsize
;
611 if ((k
+count
) > GPS_OUT_BUFSIZ
) {
612 dev_dbg(dev
, "packet too large\n");
613 garmin_data_p
->outsize
= 0;
617 memcpy(garmin_data_p
->outbuffer
+k
, buf
, count
);
619 garmin_data_p
->outsize
= k
;
621 if (k
>= GARMIN_PKTHDR_LENGTH
) {
622 pktid
= getPacketId(garmin_data_p
->outbuffer
);
623 datalen
= getDataLength(garmin_data_p
->outbuffer
);
624 i
= GARMIN_PKTHDR_LENGTH
+ datalen
;
631 dev_dbg(dev
, "%s - %d bytes in buffer, %d bytes in pkt.\n", __func__
, k
, i
);
633 /* garmin_data_p->outbuffer now contains a complete packet */
635 usb_serial_debug_data(&garmin_data_p
->port
->dev
, __func__
, k
,
636 garmin_data_p
->outbuffer
);
638 garmin_data_p
->outsize
= 0;
640 if (getLayerId(garmin_data_p
->outbuffer
) != GARMIN_LAYERID_APPL
) {
641 dev_dbg(dev
, "not an application packet (%d)\n",
642 getLayerId(garmin_data_p
->outbuffer
));
647 dev_dbg(dev
, "packet-id %d too large\n", pktid
);
652 dev_dbg(dev
, "packet-size %d too large\n", datalen
);
656 /* the serial protocol should be able to handle this packet */
659 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
660 for (i
= 0; i
< datalen
; i
++) {
665 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
666 if (k
> (GARMIN_PKTHDR_LENGTH
-2)) {
667 /* can't add stuffing DLEs in place, move data to end
669 dst
= garmin_data_p
->outbuffer
+GPS_OUT_BUFSIZ
-datalen
;
670 memcpy(dst
, src
, datalen
);
674 dst
= garmin_data_p
->outbuffer
;
684 for (i
= 0; i
< datalen
; i
++) {
692 cksum
= -cksum
& 0xFF;
699 i
= dst
-garmin_data_p
->outbuffer
;
701 send_to_tty(garmin_data_p
->port
, garmin_data_p
->outbuffer
, i
);
703 garmin_data_p
->pkt_id
= pktid
;
704 garmin_data_p
->state
= STATE_WAIT_TTY_ACK
;
711 * Process the next pending data packet - if there is one
713 static int gsp_next_packet(struct garmin_data
*garmin_data_p
)
716 struct garmin_packet
*pkt
= NULL
;
718 while ((pkt
= pkt_pop(garmin_data_p
)) != NULL
) {
719 dev_dbg(&garmin_data_p
->port
->dev
, "%s - next pkt: %d\n", __func__
, pkt
->seq
);
720 result
= gsp_send(garmin_data_p
, pkt
->data
, pkt
->size
);
732 /******************************************************************************
734 ******************************************************************************/
738 * Called for data received from tty
740 * The input data is expected to be in garmin usb-packet format.
742 * buf contains the data read, it may span more than one packet
743 * or even incomplete packets
745 static int nat_receive(struct garmin_data
*garmin_data_p
,
746 const unsigned char *buf
, int count
)
754 while (offs
< count
) {
755 /* if buffer contains header, copy rest of data */
756 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
)
757 len
= GARMIN_PKTHDR_LENGTH
758 +getDataLength(garmin_data_p
->inbuffer
);
760 len
= GARMIN_PKTHDR_LENGTH
;
762 if (len
>= GPS_IN_BUFSIZ
) {
763 /* seems to be an invalid packet, ignore rest
765 dev_dbg(&garmin_data_p
->port
->dev
,
766 "%s - packet size too large: %d\n",
768 garmin_data_p
->insize
= 0;
772 len
-= garmin_data_p
->insize
;
773 if (len
> (count
-offs
))
776 dest
= garmin_data_p
->inbuffer
777 + garmin_data_p
->insize
;
778 memcpy(dest
, buf
+offs
, len
);
779 garmin_data_p
->insize
+= len
;
784 /* do we have a complete packet ? */
785 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
) {
786 len
= GARMIN_PKTHDR_LENGTH
+
787 getDataLength(garmin_data_p
->inbuffer
);
788 if (garmin_data_p
->insize
>= len
) {
789 garmin_write_bulk(garmin_data_p
->port
,
790 garmin_data_p
->inbuffer
,
792 garmin_data_p
->insize
= 0;
794 /* if this was an abort-transfer command,
795 flush all queued data. */
796 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
797 spin_lock_irqsave(&garmin_data_p
->lock
,
799 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
800 spin_unlock_irqrestore(
801 &garmin_data_p
->lock
, flags
);
802 pkt_clear(garmin_data_p
);
811 /******************************************************************************
813 ******************************************************************************/
815 static void priv_status_resp(struct usb_serial_port
*port
)
817 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
818 __le32
*pkt
= (__le32
*)garmin_data_p
->privpkt
;
820 pkt
[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE
);
821 pkt
[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP
);
822 pkt
[2] = __cpu_to_le32(12);
823 pkt
[3] = __cpu_to_le32(VERSION_MAJOR
<< 16 | VERSION_MINOR
);
824 pkt
[4] = __cpu_to_le32(garmin_data_p
->mode
);
825 pkt
[5] = __cpu_to_le32(garmin_data_p
->serial_num
);
827 send_to_tty(port
, (__u8
*)pkt
, 6 * 4);
831 /******************************************************************************
832 * Garmin specific driver functions
833 ******************************************************************************/
835 static int process_resetdev_request(struct usb_serial_port
*port
)
839 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
841 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
842 garmin_data_p
->flags
&= ~(CLEAR_HALT_REQUIRED
);
843 garmin_data_p
->state
= STATE_RESET
;
844 garmin_data_p
->serial_num
= 0;
845 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
847 usb_kill_urb(port
->interrupt_in_urb
);
848 dev_dbg(&port
->dev
, "%s - usb_reset_device\n", __func__
);
849 status
= usb_reset_device(port
->serial
->dev
);
851 dev_dbg(&port
->dev
, "%s - usb_reset_device failed: %d\n",
859 * clear all cached data
861 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);
919 usb_kill_anchored_urbs(&garmin_data_p
->write_urbs
);
920 usb_kill_urb(port
->interrupt_in_urb
);
927 static int garmin_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
931 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
933 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
934 garmin_data_p
->mode
= initial_mode
;
935 garmin_data_p
->count
= 0;
936 garmin_data_p
->flags
&= FLAGS_SESSION_REPLY1_SEEN
;
937 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
939 /* shutdown any bulk reads that might be going on */
940 usb_kill_urb(port
->read_urb
);
942 if (garmin_data_p
->state
== STATE_RESET
)
943 status
= garmin_init_session(port
);
945 garmin_data_p
->state
= STATE_ACTIVE
;
950 static void garmin_close(struct usb_serial_port
*port
)
952 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
954 dev_dbg(&port
->dev
, "%s - mode=%d state=%d flags=0x%X\n",
955 __func__
, garmin_data_p
->mode
, garmin_data_p
->state
,
956 garmin_data_p
->flags
);
958 garmin_clear(garmin_data_p
);
960 /* shutdown our urbs */
961 usb_kill_urb(port
->read_urb
);
962 usb_kill_anchored_urbs(&garmin_data_p
->write_urbs
);
964 /* keep reset state so we know that we must start a new session */
965 if (garmin_data_p
->state
!= STATE_RESET
)
966 garmin_data_p
->state
= STATE_DISCONNECTED
;
970 static void garmin_write_bulk_callback(struct urb
*urb
)
972 struct usb_serial_port
*port
= urb
->context
;
975 struct garmin_data
*garmin_data_p
=
976 usb_get_serial_port_data(port
);
978 if (getLayerId(urb
->transfer_buffer
) == GARMIN_LAYERID_APPL
) {
980 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
981 gsp_send_ack(garmin_data_p
,
982 ((__u8
*)urb
->transfer_buffer
)[4]);
985 usb_serial_port_softint(port
);
988 /* Ignore errors that resulted from garmin_write_bulk with
991 /* free up the transfer buffer, as usb_free_urb() does not do this */
992 kfree(urb
->transfer_buffer
);
996 static int garmin_write_bulk(struct usb_serial_port
*port
,
997 const unsigned char *buf
, int count
,
1000 unsigned long flags
;
1001 struct usb_serial
*serial
= port
->serial
;
1002 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1004 unsigned char *buffer
;
1007 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1008 garmin_data_p
->flags
&= ~FLAGS_DROP_DATA
;
1009 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1011 buffer
= kmalloc(count
, GFP_ATOMIC
);
1015 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
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 (getLayerId(buffer
) == GARMIN_LAYERID_APPL
) {
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 usb_anchor_urb(urb
, &garmin_data_p
->write_urbs
);
1047 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1050 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1053 usb_unanchor_urb(urb
);
1057 /* we are done with this urb, so let the host driver
1058 * really free it when it is finished with it */
1064 static int garmin_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1065 const unsigned char *buf
, int count
)
1067 struct device
*dev
= &port
->dev
;
1068 int pktid
, pktsiz
, len
;
1069 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1070 __le32
*privpkt
= (__le32
*)garmin_data_p
->privpkt
;
1072 usb_serial_debug_data(dev
, __func__
, count
, buf
);
1074 if (garmin_data_p
->state
== STATE_RESET
)
1077 /* check for our private packets */
1078 if (count
>= GARMIN_PKTHDR_LENGTH
) {
1083 memcpy(garmin_data_p
->privpkt
, buf
, len
);
1085 pktsiz
= getDataLength(garmin_data_p
->privpkt
);
1086 pktid
= getPacketId(garmin_data_p
->privpkt
);
1088 if (count
== (GARMIN_PKTHDR_LENGTH
+ pktsiz
) &&
1089 getLayerId(garmin_data_p
->privpkt
) ==
1090 GARMIN_LAYERID_PRIVATE
) {
1092 dev_dbg(dev
, "%s - processing private request %d\n",
1095 /* drop all unfinished transfers */
1096 garmin_clear(garmin_data_p
);
1099 case PRIV_PKTID_SET_MODE
:
1102 garmin_data_p
->mode
= __le32_to_cpu(privpkt
[3]);
1103 dev_dbg(dev
, "%s - mode set to %d\n",
1104 __func__
, garmin_data_p
->mode
);
1107 case PRIV_PKTID_INFO_REQ
:
1108 priv_status_resp(port
);
1111 case PRIV_PKTID_RESET_REQ
:
1112 process_resetdev_request(port
);
1115 case PRIV_PKTID_SET_DEF_MODE
:
1118 initial_mode
= __le32_to_cpu(privpkt
[3]);
1119 dev_dbg(dev
, "%s - initial_mode set to %d\n",
1121 garmin_data_p
->mode
);
1128 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1129 return gsp_receive(garmin_data_p
, buf
, count
);
1130 } else { /* MODE_NATIVE */
1131 return nat_receive(garmin_data_p
, buf
, count
);
1136 static int garmin_write_room(struct tty_struct
*tty
)
1138 struct usb_serial_port
*port
= tty
->driver_data
;
1140 * Report back the bytes currently available in the output buffer.
1142 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1143 return GPS_OUT_BUFSIZ
-garmin_data_p
->outsize
;
1147 static void garmin_read_process(struct garmin_data
*garmin_data_p
,
1148 unsigned char *data
, unsigned data_length
,
1151 unsigned long flags
;
1153 if (garmin_data_p
->flags
& FLAGS_DROP_DATA
) {
1154 /* abort-transfer cmd is active */
1155 dev_dbg(&garmin_data_p
->port
->dev
, "%s - pkt dropped\n", __func__
);
1156 } else if (garmin_data_p
->state
!= STATE_DISCONNECTED
&&
1157 garmin_data_p
->state
!= STATE_RESET
) {
1159 /* if throttling is active or postprecessing is required
1160 put the received data in the input queue, otherwise
1161 send it directly to the tty port */
1162 if (garmin_data_p
->flags
& FLAGS_QUEUING
) {
1163 pkt_add(garmin_data_p
, data
, data_length
);
1164 } else if (bulk_data
||
1165 getLayerId(data
) == GARMIN_LAYERID_APPL
) {
1167 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1168 garmin_data_p
->flags
|= APP_RESP_SEEN
;
1169 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1171 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1172 pkt_add(garmin_data_p
, data
, data_length
);
1174 send_to_tty(garmin_data_p
->port
, data
,
1178 /* ignore system layer packets ... */
1183 static void garmin_read_bulk_callback(struct urb
*urb
)
1185 unsigned long flags
;
1186 struct usb_serial_port
*port
= urb
->context
;
1187 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1188 unsigned char *data
= urb
->transfer_buffer
;
1189 int status
= urb
->status
;
1193 dev_dbg(&urb
->dev
->dev
, "%s - nonzero read bulk status received: %d\n",
1198 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
, data
);
1200 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 1);
1202 if (urb
->actual_length
== 0 &&
1203 (garmin_data_p
->flags
& FLAGS_BULK_IN_RESTART
) != 0) {
1204 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1205 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_RESTART
;
1206 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1207 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1210 "%s - failed resubmitting read urb, error %d\n",
1212 } else if (urb
->actual_length
> 0) {
1213 /* Continue trying to read until nothing more is received */
1214 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1215 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1218 "%s - failed resubmitting read urb, error %d\n",
1222 dev_dbg(&port
->dev
, "%s - end of bulk data\n", __func__
);
1223 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1224 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_ACTIVE
;
1225 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1230 static void garmin_read_int_callback(struct urb
*urb
)
1232 unsigned long flags
;
1234 struct usb_serial_port
*port
= urb
->context
;
1235 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1236 unsigned char *data
= urb
->transfer_buffer
;
1237 int status
= urb
->status
;
1246 /* this urb is terminated, clean up */
1247 dev_dbg(&urb
->dev
->dev
, "%s - urb shutting down with status: %d\n",
1251 dev_dbg(&urb
->dev
->dev
, "%s - nonzero urb status received: %d\n",
1256 usb_serial_debug_data(&port
->dev
, __func__
, urb
->actual_length
,
1257 urb
->transfer_buffer
);
1259 if (urb
->actual_length
== sizeof(GARMIN_BULK_IN_AVAIL_REPLY
) &&
1260 memcmp(data
, GARMIN_BULK_IN_AVAIL_REPLY
,
1261 sizeof(GARMIN_BULK_IN_AVAIL_REPLY
)) == 0) {
1263 dev_dbg(&port
->dev
, "%s - bulk data available.\n", __func__
);
1265 if ((garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
) == 0) {
1267 /* bulk data available */
1268 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1271 "%s - failed submitting read urb, error %d\n",
1274 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1275 garmin_data_p
->flags
|= FLAGS_BULK_IN_ACTIVE
;
1276 spin_unlock_irqrestore(&garmin_data_p
->lock
,
1280 /* bulk-in transfer still active */
1281 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1282 garmin_data_p
->flags
|= FLAGS_BULK_IN_RESTART
;
1283 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1286 } else if (urb
->actual_length
== (4+sizeof(GARMIN_START_SESSION_REPLY
))
1287 && memcmp(data
, GARMIN_START_SESSION_REPLY
,
1288 sizeof(GARMIN_START_SESSION_REPLY
)) == 0) {
1290 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1291 garmin_data_p
->flags
|= FLAGS_SESSION_REPLY1_SEEN
;
1292 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1294 /* save the serial number */
1295 garmin_data_p
->serial_num
= __le32_to_cpup(
1296 (__le32
*)(data
+GARMIN_PKTHDR_LENGTH
));
1298 dev_dbg(&port
->dev
, "%s - start-of-session reply seen - serial %u.\n",
1299 __func__
, garmin_data_p
->serial_num
);
1302 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 0);
1304 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1306 dev_err(&urb
->dev
->dev
,
1307 "%s - Error %d submitting interrupt urb\n",
1313 * Sends the next queued packt to the tty port (garmin native mode only)
1314 * and then sets a timer to call itself again until all queued data
1317 static int garmin_flush_queue(struct garmin_data
*garmin_data_p
)
1319 unsigned long flags
;
1320 struct garmin_packet
*pkt
;
1322 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1323 pkt
= pkt_pop(garmin_data_p
);
1325 send_to_tty(garmin_data_p
->port
, pkt
->data
, pkt
->size
);
1327 mod_timer(&garmin_data_p
->timer
, (1)+jiffies
);
1330 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1331 garmin_data_p
->flags
&= ~FLAGS_QUEUING
;
1332 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1339 static void garmin_throttle(struct tty_struct
*tty
)
1341 struct usb_serial_port
*port
= tty
->driver_data
;
1342 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1344 /* set flag, data received will be put into a queue
1345 for later processing */
1346 spin_lock_irq(&garmin_data_p
->lock
);
1347 garmin_data_p
->flags
|= FLAGS_QUEUING
|FLAGS_THROTTLED
;
1348 spin_unlock_irq(&garmin_data_p
->lock
);
1352 static void garmin_unthrottle(struct tty_struct
*tty
)
1354 struct usb_serial_port
*port
= tty
->driver_data
;
1355 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1358 spin_lock_irq(&garmin_data_p
->lock
);
1359 garmin_data_p
->flags
&= ~FLAGS_THROTTLED
;
1360 spin_unlock_irq(&garmin_data_p
->lock
);
1362 /* in native mode send queued data to tty, in
1363 serial mode nothing needs to be done here */
1364 if (garmin_data_p
->mode
== MODE_NATIVE
)
1365 garmin_flush_queue(garmin_data_p
);
1367 if ((garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
) != 0) {
1368 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1371 "%s - failed resubmitting read urb, error %d\n",
1377 * The timer is currently only used to send queued packets to
1378 * the tty in cases where the protocol provides no own handshaking
1379 * to initiate the transfer.
1381 static void timeout_handler(unsigned long data
)
1383 struct garmin_data
*garmin_data_p
= (struct garmin_data
*) data
;
1385 /* send the next queued packet to the tty port */
1386 if (garmin_data_p
->mode
== MODE_NATIVE
)
1387 if (garmin_data_p
->flags
& FLAGS_QUEUING
)
1388 garmin_flush_queue(garmin_data_p
);
1393 static int garmin_port_probe(struct usb_serial_port
*port
)
1396 struct garmin_data
*garmin_data_p
;
1398 garmin_data_p
= kzalloc(sizeof(struct garmin_data
), GFP_KERNEL
);
1402 init_timer(&garmin_data_p
->timer
);
1403 spin_lock_init(&garmin_data_p
->lock
);
1404 INIT_LIST_HEAD(&garmin_data_p
->pktlist
);
1405 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1406 garmin_data_p
->timer
.data
= (unsigned long)garmin_data_p
;
1407 garmin_data_p
->timer
.function
= timeout_handler
;
1408 garmin_data_p
->port
= port
;
1409 garmin_data_p
->state
= 0;
1410 garmin_data_p
->flags
= 0;
1411 garmin_data_p
->count
= 0;
1412 init_usb_anchor(&garmin_data_p
->write_urbs
);
1413 usb_set_serial_port_data(port
, garmin_data_p
);
1415 status
= garmin_init_session(port
);
1421 kfree(garmin_data_p
);
1427 static int garmin_port_remove(struct usb_serial_port
*port
)
1429 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1431 usb_kill_anchored_urbs(&garmin_data_p
->write_urbs
);
1432 usb_kill_urb(port
->interrupt_in_urb
);
1433 del_timer_sync(&garmin_data_p
->timer
);
1434 kfree(garmin_data_p
);
1439 /* All of the device info needed */
1440 static struct usb_serial_driver garmin_device
= {
1442 .owner
= THIS_MODULE
,
1443 .name
= "garmin_gps",
1445 .description
= "Garmin GPS usb/tty",
1446 .id_table
= id_table
,
1448 .open
= garmin_open
,
1449 .close
= garmin_close
,
1450 .throttle
= garmin_throttle
,
1451 .unthrottle
= garmin_unthrottle
,
1452 .port_probe
= garmin_port_probe
,
1453 .port_remove
= garmin_port_remove
,
1454 .write
= garmin_write
,
1455 .write_room
= garmin_write_room
,
1456 .write_bulk_callback
= garmin_write_bulk_callback
,
1457 .read_bulk_callback
= garmin_read_bulk_callback
,
1458 .read_int_callback
= garmin_read_int_callback
,
1461 static struct usb_serial_driver
* const serial_drivers
[] = {
1462 &garmin_device
, NULL
1465 module_usb_serial_driver(serial_drivers
, id_table
);
1467 MODULE_AUTHOR(DRIVER_AUTHOR
);
1468 MODULE_DESCRIPTION(DRIVER_DESC
);
1469 MODULE_LICENSE("GPL");
1471 module_param(initial_mode
, int, S_IRUGO
);
1472 MODULE_PARM_DESC(initial_mode
, "Initial mode");