4 * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de
6 * The latest version of the driver can be found at
7 * http://sourceforge.net/projects/garmin-gps/
9 * This driver has been derived from v2.1 of the visor driver.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/timer.h>
31 #include <linux/tty.h>
32 #include <linux/tty_driver.h>
33 #include <linux/tty_flip.h>
34 #include <linux/module.h>
35 #include <linux/spinlock.h>
36 #include <linux/uaccess.h>
37 #include <asm/atomic.h>
38 #include <linux/usb.h>
39 #include <linux/usb/serial.h>
41 /* the mode to be set when the port ist opened */
42 static int initial_mode
= 1;
47 #define GARMIN_VENDOR_ID 0x091E
53 #define VERSION_MAJOR 0
54 #define VERSION_MINOR 36
57 #define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
58 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
59 #define DRIVER_AUTHOR "hermann kneissel"
60 #define DRIVER_DESC "garmin gps driver"
62 /* error codes returned by the driver */
63 #define EINVPKT 1000 /* invalid packet structure */
66 /* size of the header of a packet using the usb protocol */
67 #define GARMIN_PKTHDR_LENGTH 12
69 /* max. possible size of a packet using the serial protocol */
70 #define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
72 /* max. possible size of a packet with worst case stuffing */
73 #define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
75 /* size of a buffer able to hold a complete (no stuffing) packet
76 * (the document protocol does not contain packets with a larger
77 * size, but in theory a packet may be 64k+12 bytes - if in
78 * later protocol versions larger packet sizes occur, this value
79 * should be increased accordingly, so the input buffer is always
80 * large enough the store a complete packet inclusive header) */
81 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
83 /* size of a buffer able to hold a complete (incl. stuffing) packet */
84 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
86 /* where to place the packet id of a serial packet, so we can
87 * prepend the usb-packet header without the need to move the
89 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
91 /* max. size of incoming private packets (header+1 param) */
92 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
94 #define GARMIN_LAYERID_TRANSPORT 0
95 #define GARMIN_LAYERID_APPL 20
96 /* our own layer-id to use for some control mechanisms */
97 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
99 #define GARMIN_PKTID_PVT_DATA 51
100 #define GARMIN_PKTID_L001_COMMAND_DATA 10
102 #define CMND_ABORT_TRANSFER 0
104 /* packet ids used in private layer */
105 #define PRIV_PKTID_SET_DEBUG 1
106 #define PRIV_PKTID_SET_MODE 2
107 #define PRIV_PKTID_INFO_REQ 3
108 #define PRIV_PKTID_INFO_RESP 4
109 #define PRIV_PKTID_RESET_REQ 5
110 #define PRIV_PKTID_SET_DEF_MODE 6
118 /* structure used to queue incoming packets */
119 struct garmin_packet
{
120 struct list_head list
;
122 /* the real size of the data array, always > 0 */
127 /* structure used to keep the current state of the driver */
135 struct timer_list timer
;
136 struct usb_serial_port
*port
;
140 __u8 inbuffer
[GPS_IN_BUFSIZ
]; /* tty -> usb */
141 __u8 outbuffer
[GPS_OUT_BUFSIZ
]; /* usb -> tty */
144 struct list_head pktlist
;
149 #define STATE_INITIAL_DELAY 1
150 #define STATE_TIMEOUT 2
151 #define STATE_SESSION_REQ1 3
152 #define STATE_SESSION_REQ2 4
153 #define STATE_ACTIVE 5
155 #define STATE_RESET 8
156 #define STATE_DISCONNECTED 9
157 #define STATE_WAIT_TTY_ACK 10
158 #define STATE_GSP_WAIT_DATA 11
160 #define MODE_NATIVE 0
161 #define MODE_GARMIN_SERIAL 1
163 /* Flags used in garmin_data.flags: */
164 #define FLAGS_SESSION_REPLY_MASK 0x00C0
165 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
166 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
167 #define FLAGS_BULK_IN_ACTIVE 0x0020
168 #define FLAGS_BULK_IN_RESTART 0x0010
169 #define FLAGS_THROTTLED 0x0008
170 #define APP_REQ_SEEN 0x0004
171 #define APP_RESP_SEEN 0x0002
172 #define CLEAR_HALT_REQUIRED 0x0001
174 #define FLAGS_QUEUING 0x0100
175 #define FLAGS_DROP_DATA 0x0800
177 #define FLAGS_GSP_SKIP 0x1000
178 #define FLAGS_GSP_DLESEEN 0x2000
185 /* function prototypes */
186 static int gsp_next_packet(struct garmin_data
*garmin_data_p
);
187 static int garmin_write_bulk(struct usb_serial_port
*port
,
188 const unsigned char *buf
, int count
,
191 /* some special packets to be send or received */
192 static unsigned char const GARMIN_START_SESSION_REQ
[]
193 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
194 static unsigned char const GARMIN_START_SESSION_REPLY
[]
195 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
196 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY
[]
197 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
198 static unsigned char const GARMIN_APP_LAYER_REPLY
[]
200 static unsigned char const GARMIN_START_PVT_REQ
[]
201 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
202 static unsigned char const GARMIN_STOP_PVT_REQ
[]
203 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
204 static unsigned char const GARMIN_STOP_TRANSFER_REQ
[]
205 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
206 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2
[]
207 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
208 static unsigned char const PRIVATE_REQ
[]
209 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
213 static const struct usb_device_id id_table
[] = {
214 /* the same device id seems to be used by all
215 usb enabled GPS devices */
216 { USB_DEVICE(GARMIN_VENDOR_ID
, 3) },
217 { } /* Terminating entry */
220 MODULE_DEVICE_TABLE(usb
, id_table
);
222 static struct usb_driver garmin_driver
= {
223 .name
= "garmin_gps",
224 .probe
= usb_serial_probe
,
225 .disconnect
= usb_serial_disconnect
,
226 .id_table
= id_table
,
231 static inline int getLayerId(const __u8
*usbPacket
)
233 return __le32_to_cpup((__le32
*)(usbPacket
));
236 static inline int getPacketId(const __u8
*usbPacket
)
238 return __le32_to_cpup((__le32
*)(usbPacket
+4));
241 static inline int getDataLength(const __u8
*usbPacket
)
243 return __le32_to_cpup((__le32
*)(usbPacket
+8));
248 * check if the usb-packet in buf contains an abort-transfer command.
249 * (if yes, all queued data will be dropped)
251 static inline int isAbortTrfCmnd(const unsigned char *buf
)
253 if (0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ
,
254 sizeof(GARMIN_STOP_TRANSFER_REQ
)) ||
255 0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ_V2
,
256 sizeof(GARMIN_STOP_TRANSFER_REQ_V2
)))
264 static void send_to_tty(struct usb_serial_port
*port
,
265 char *data
, unsigned int actual_length
)
267 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
269 if (tty
&& actual_length
) {
271 usb_serial_debug_data(debug
, &port
->dev
,
272 __func__
, actual_length
, data
);
274 tty_insert_flip_string(tty
, data
, actual_length
);
275 tty_flip_buffer_push(tty
);
281 /******************************************************************************
282 * packet queue handling
283 ******************************************************************************/
286 * queue a received (usb-)packet for later processing
288 static int pkt_add(struct garmin_data
*garmin_data_p
,
289 unsigned char *data
, unsigned int data_length
)
294 struct garmin_packet
*pkt
;
296 /* process only packets containg data ... */
298 pkt
= kmalloc(sizeof(struct garmin_packet
)+data_length
,
301 dev_err(&garmin_data_p
->port
->dev
, "out of memory\n");
304 pkt
->size
= data_length
;
305 memcpy(pkt
->data
, data
, data_length
);
307 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
308 garmin_data_p
->flags
|= FLAGS_QUEUING
;
309 result
= list_empty(&garmin_data_p
->pktlist
);
310 pkt
->seq
= garmin_data_p
->seq_counter
++;
311 list_add_tail(&pkt
->list
, &garmin_data_p
->pktlist
);
312 state
= garmin_data_p
->state
;
313 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
315 dbg("%s - added: pkt: %d - %d bytes",
316 __func__
, pkt
->seq
, data_length
);
318 /* in serial mode, if someone is waiting for data from
319 the device, convert and send the next packet to tty. */
320 if (result
&& (state
== STATE_GSP_WAIT_DATA
))
321 gsp_next_packet(garmin_data_p
);
327 /* get the next pending packet */
328 static struct garmin_packet
*pkt_pop(struct garmin_data
*garmin_data_p
)
331 struct garmin_packet
*result
= NULL
;
333 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
334 if (!list_empty(&garmin_data_p
->pktlist
)) {
335 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
336 list_del(&result
->list
);
338 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
343 /* free up all queued data */
344 static void pkt_clear(struct garmin_data
*garmin_data_p
)
347 struct garmin_packet
*result
= NULL
;
351 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
352 while (!list_empty(&garmin_data_p
->pktlist
)) {
353 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
354 list_del(&result
->list
);
357 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
361 /******************************************************************************
362 * garmin serial protocol handling handling
363 ******************************************************************************/
365 /* send an ack packet back to the tty */
366 static int gsp_send_ack(struct garmin_data
*garmin_data_p
, __u8 pkt_id
)
373 dbg("%s - pkt-id: 0x%X.", __func__
, 0xFF & pkt_id
);
389 *ptr
++ = 0xFF & (-cksum
);
395 send_to_tty(garmin_data_p
->port
, pkt
, l
);
402 * called for a complete packet received from tty layer
404 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
405 * at GSP_INITIAL_OFFSET.
407 * count - number of bytes in the input buffer including space reserved for
408 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
409 * (including pkt-id, data-length a. cksum)
411 static int gsp_rec_packet(struct garmin_data
*garmin_data_p
, int count
)
414 const __u8
*recpkt
= garmin_data_p
->inbuffer
+GSP_INITIAL_OFFSET
;
415 __le32
*usbdata
= (__le32
*) garmin_data_p
->inbuffer
;
419 int pktid
= recpkt
[0];
420 int size
= recpkt
[1];
422 usb_serial_debug_data(debug
, &garmin_data_p
->port
->dev
,
423 __func__
, count
-GSP_INITIAL_OFFSET
, recpkt
);
425 if (size
!= (count
-GSP_INITIAL_OFFSET
-3)) {
426 dbg("%s - invalid size, expected %d bytes, got %d",
427 __func__
, size
, (count
-GSP_INITIAL_OFFSET
-3));
434 /* sanity check, remove after test ... */
435 if ((__u8
*)&(usbdata
[3]) != recpkt
) {
436 dbg("%s - ptr mismatch %p - %p",
437 __func__
, &(usbdata
[4]), recpkt
);
446 if ((0xff & (cksum
+ *recpkt
)) != 0) {
447 dbg("%s - invalid checksum, expected %02x, got %02x",
448 __func__
, 0xff & -cksum
, 0xff & *recpkt
);
452 usbdata
[0] = __cpu_to_le32(GARMIN_LAYERID_APPL
);
453 usbdata
[1] = __cpu_to_le32(pktid
);
454 usbdata
[2] = __cpu_to_le32(size
);
456 garmin_write_bulk(garmin_data_p
->port
, garmin_data_p
->inbuffer
,
457 GARMIN_PKTHDR_LENGTH
+size
, 0);
459 /* if this was an abort-transfer command, flush all
461 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
462 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
463 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
464 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
465 pkt_clear(garmin_data_p
);
474 * Called for data received from tty
476 * buf contains the data read, it may span more than one packet or even
479 * input record should be a serial-record, but it may not be complete.
480 * Copy it into our local buffer, until an etx is seen (or an error
482 * Once the record is complete, convert into a usb packet and send it
483 * to the bulk pipe, send an ack back to the tty.
485 * If the input is an ack, just send the last queued packet to the
488 * if the input is an abort command, drop all queued data.
491 static int gsp_receive(struct garmin_data
*garmin_data_p
,
492 const unsigned char *buf
, int count
)
496 int ack_or_nak_seen
= 0;
499 /* dleSeen: set if last byte read was a DLE */
501 /* skip: if set, skip incoming data until possible start of
507 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
508 dest
= garmin_data_p
->inbuffer
;
509 size
= garmin_data_p
->insize
;
510 dleSeen
= garmin_data_p
->flags
& FLAGS_GSP_DLESEEN
;
511 skip
= garmin_data_p
->flags
& FLAGS_GSP_SKIP
;
512 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
514 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
515 __func__, dleSeen, skip, size, count); */
518 size
= GSP_INITIAL_OFFSET
;
520 while (offs
< count
) {
526 if (skip
) { /* start of a new pkt */
528 size
= GSP_INITIAL_OFFSET
;
530 } else if (dleSeen
) {
536 } else if (data
== ETX
) {
538 /* packet complete */
540 data
= dest
[GSP_INITIAL_OFFSET
];
543 ack_or_nak_seen
= ACK
;
544 dbg("ACK packet complete.");
545 } else if (data
== NAK
) {
546 ack_or_nak_seen
= NAK
;
547 dbg("NAK packet complete.");
549 dbg("packet complete - id=0x%X.",
551 gsp_rec_packet(garmin_data_p
, size
);
555 size
= GSP_INITIAL_OFFSET
;
563 size
= GSP_INITIAL_OFFSET
;
570 if (size
>= GPS_IN_BUFSIZ
) {
571 dbg("%s - packet too large.", __func__
);
573 size
= GSP_INITIAL_OFFSET
;
578 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
580 garmin_data_p
->insize
= size
;
582 /* copy flags back to structure */
584 garmin_data_p
->flags
|= FLAGS_GSP_SKIP
;
586 garmin_data_p
->flags
&= ~FLAGS_GSP_SKIP
;
589 garmin_data_p
->flags
|= FLAGS_GSP_DLESEEN
;
591 garmin_data_p
->flags
&= ~FLAGS_GSP_DLESEEN
;
593 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
595 if (ack_or_nak_seen
) {
596 if (gsp_next_packet(garmin_data_p
) > 0)
597 garmin_data_p
->state
= STATE_ACTIVE
;
599 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
607 * Sends a usb packet to the tty
609 * Assumes, that all packages and at an usb-packet boundary.
611 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
613 static int gsp_send(struct garmin_data
*garmin_data_p
,
614 const unsigned char *buf
, int count
)
616 const unsigned char *src
;
624 dbg("%s - state %d - %d bytes.", __func__
,
625 garmin_data_p
->state
, count
);
627 k
= garmin_data_p
->outsize
;
628 if ((k
+count
) > GPS_OUT_BUFSIZ
) {
629 dbg("packet too large");
630 garmin_data_p
->outsize
= 0;
634 memcpy(garmin_data_p
->outbuffer
+k
, buf
, count
);
636 garmin_data_p
->outsize
= k
;
638 if (k
>= GARMIN_PKTHDR_LENGTH
) {
639 pktid
= getPacketId(garmin_data_p
->outbuffer
);
640 datalen
= getDataLength(garmin_data_p
->outbuffer
);
641 i
= GARMIN_PKTHDR_LENGTH
+ datalen
;
648 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__
, k
, i
);
650 /* garmin_data_p->outbuffer now contains a complete packet */
652 usb_serial_debug_data(debug
, &garmin_data_p
->port
->dev
,
653 __func__
, k
, garmin_data_p
->outbuffer
);
655 garmin_data_p
->outsize
= 0;
657 if (GARMIN_LAYERID_APPL
!= getLayerId(garmin_data_p
->outbuffer
)) {
658 dbg("not an application packet (%d)",
659 getLayerId(garmin_data_p
->outbuffer
));
664 dbg("packet-id %d too large", pktid
);
669 dbg("packet-size %d too large", datalen
);
673 /* the serial protocol should be able to handle this packet */
676 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
677 for (i
= 0; i
< datalen
; i
++) {
682 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
683 if (k
> (GARMIN_PKTHDR_LENGTH
-2)) {
684 /* can't add stuffing DLEs in place, move data to end
686 dst
= garmin_data_p
->outbuffer
+GPS_OUT_BUFSIZ
-datalen
;
687 memcpy(dst
, src
, datalen
);
691 dst
= garmin_data_p
->outbuffer
;
701 for (i
= 0; i
< datalen
; i
++) {
709 cksum
= 0xFF & -cksum
;
716 i
= dst
-garmin_data_p
->outbuffer
;
718 send_to_tty(garmin_data_p
->port
, garmin_data_p
->outbuffer
, i
);
720 garmin_data_p
->pkt_id
= pktid
;
721 garmin_data_p
->state
= STATE_WAIT_TTY_ACK
;
728 * Process the next pending data packet - if there is one
730 static int gsp_next_packet(struct garmin_data
*garmin_data_p
)
733 struct garmin_packet
*pkt
= NULL
;
735 while ((pkt
= pkt_pop(garmin_data_p
)) != NULL
) {
736 dbg("%s - next pkt: %d", __func__
, pkt
->seq
);
737 result
= gsp_send(garmin_data_p
, pkt
->data
, pkt
->size
);
749 /******************************************************************************
751 ******************************************************************************/
755 * Called for data received from tty
757 * The input data is expected to be in garmin usb-packet format.
759 * buf contains the data read, it may span more than one packet
760 * or even incomplete packets
762 static int nat_receive(struct garmin_data
*garmin_data_p
,
763 const unsigned char *buf
, int count
)
771 while (offs
< count
) {
772 /* if buffer contains header, copy rest of data */
773 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
)
774 len
= GARMIN_PKTHDR_LENGTH
775 +getDataLength(garmin_data_p
->inbuffer
);
777 len
= GARMIN_PKTHDR_LENGTH
;
779 if (len
>= GPS_IN_BUFSIZ
) {
780 /* seems to be an invalid packet, ignore rest
782 dbg("%s - packet size too large: %d", __func__
, len
);
783 garmin_data_p
->insize
= 0;
787 len
-= garmin_data_p
->insize
;
788 if (len
> (count
-offs
))
791 dest
= garmin_data_p
->inbuffer
792 + garmin_data_p
->insize
;
793 memcpy(dest
, buf
+offs
, len
);
794 garmin_data_p
->insize
+= len
;
799 /* do we have a complete packet ? */
800 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
) {
801 len
= GARMIN_PKTHDR_LENGTH
+
802 getDataLength(garmin_data_p
->inbuffer
);
803 if (garmin_data_p
->insize
>= len
) {
804 garmin_write_bulk(garmin_data_p
->port
,
805 garmin_data_p
->inbuffer
,
807 garmin_data_p
->insize
= 0;
809 /* if this was an abort-transfer command,
810 flush all queued data. */
811 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
812 spin_lock_irqsave(&garmin_data_p
->lock
,
814 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
815 spin_unlock_irqrestore(
816 &garmin_data_p
->lock
, flags
);
817 pkt_clear(garmin_data_p
);
826 /******************************************************************************
828 ******************************************************************************/
830 static void priv_status_resp(struct usb_serial_port
*port
)
832 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
833 __le32
*pkt
= (__le32
*)garmin_data_p
->privpkt
;
835 pkt
[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE
);
836 pkt
[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP
);
837 pkt
[2] = __cpu_to_le32(12);
838 pkt
[3] = __cpu_to_le32(VERSION_MAJOR
<< 16 | VERSION_MINOR
);
839 pkt
[4] = __cpu_to_le32(garmin_data_p
->mode
);
840 pkt
[5] = __cpu_to_le32(garmin_data_p
->serial_num
);
842 send_to_tty(port
, (__u8
*)pkt
, 6 * 4);
846 /******************************************************************************
847 * Garmin specific driver functions
848 ******************************************************************************/
850 static int process_resetdev_request(struct usb_serial_port
*port
)
854 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
856 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
857 garmin_data_p
->flags
&= ~(CLEAR_HALT_REQUIRED
);
858 garmin_data_p
->state
= STATE_RESET
;
859 garmin_data_p
->serial_num
= 0;
860 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
862 usb_kill_urb(port
->interrupt_in_urb
);
863 dbg("%s - usb_reset_device", __func__
);
864 status
= usb_reset_device(port
->serial
->dev
);
866 dbg("%s - usb_reset_device failed: %d",
874 * clear all cached data
876 static int garmin_clear(struct garmin_data
*garmin_data_p
)
881 /* flush all queued data */
882 pkt_clear(garmin_data_p
);
884 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
885 garmin_data_p
->insize
= 0;
886 garmin_data_p
->outsize
= 0;
887 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
893 static int garmin_init_session(struct usb_serial_port
*port
)
895 struct usb_serial
*serial
= port
->serial
;
896 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
901 usb_kill_urb(port
->interrupt_in_urb
);
903 dbg("%s - adding interrupt input", __func__
);
904 port
->interrupt_in_urb
->dev
= serial
->dev
;
905 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
907 dev_err(&serial
->dev
->dev
,
908 "%s - failed submitting interrupt urb, error %d\n",
913 * using the initialization method from gpsbabel. See comments in
914 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
917 dbg("%s - starting session ...", __func__
);
918 garmin_data_p
->state
= STATE_ACTIVE
;
920 for (i
= 0; i
< 3; i
++) {
921 status
= garmin_write_bulk(port
,
922 GARMIN_START_SESSION_REQ
,
923 sizeof(GARMIN_START_SESSION_REQ
), 0);
938 static int garmin_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
942 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
944 dbg("%s - port %d", __func__
, port
->number
);
946 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
947 garmin_data_p
->mode
= initial_mode
;
948 garmin_data_p
->count
= 0;
949 garmin_data_p
->flags
&= FLAGS_SESSION_REPLY1_SEEN
;
950 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
952 /* shutdown any bulk reads that might be going on */
953 usb_kill_urb(port
->write_urb
);
954 usb_kill_urb(port
->read_urb
);
956 if (garmin_data_p
->state
== STATE_RESET
)
957 status
= garmin_init_session(port
);
959 garmin_data_p
->state
= STATE_ACTIVE
;
964 static void garmin_close(struct usb_serial_port
*port
)
966 struct usb_serial
*serial
= port
->serial
;
967 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
969 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__
,
970 port
->number
, garmin_data_p
->mode
,
971 garmin_data_p
->state
, garmin_data_p
->flags
);
976 mutex_lock(&port
->serial
->disc_mutex
);
978 if (!port
->serial
->disconnected
)
979 garmin_clear(garmin_data_p
);
981 /* shutdown our urbs */
982 usb_kill_urb(port
->read_urb
);
983 usb_kill_urb(port
->write_urb
);
985 /* keep reset state so we know that we must start a new session */
986 if (garmin_data_p
->state
!= STATE_RESET
)
987 garmin_data_p
->state
= STATE_DISCONNECTED
;
989 mutex_unlock(&port
->serial
->disc_mutex
);
993 static void garmin_write_bulk_callback(struct urb
*urb
)
995 struct usb_serial_port
*port
= urb
->context
;
998 struct garmin_data
*garmin_data_p
=
999 usb_get_serial_port_data(port
);
1001 dbg("%s - port %d", __func__
, port
->number
);
1003 if (GARMIN_LAYERID_APPL
== getLayerId(urb
->transfer_buffer
)) {
1005 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1006 gsp_send_ack(garmin_data_p
,
1007 ((__u8
*)urb
->transfer_buffer
)[4]);
1010 usb_serial_port_softint(port
);
1013 /* Ignore errors that resulted from garmin_write_bulk with
1016 /* free up the transfer buffer, as usb_free_urb() does not do this */
1017 kfree(urb
->transfer_buffer
);
1021 static int garmin_write_bulk(struct usb_serial_port
*port
,
1022 const unsigned char *buf
, int count
,
1025 unsigned long flags
;
1026 struct usb_serial
*serial
= port
->serial
;
1027 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1029 unsigned char *buffer
;
1032 dbg("%s - port %d, state %d", __func__
, port
->number
,
1033 garmin_data_p
->state
);
1035 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1036 garmin_data_p
->flags
&= ~FLAGS_DROP_DATA
;
1037 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1039 buffer
= kmalloc(count
, GFP_ATOMIC
);
1041 dev_err(&port
->dev
, "out of memory\n");
1045 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1047 dev_err(&port
->dev
, "no more free urbs\n");
1052 memcpy(buffer
, buf
, count
);
1054 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, buffer
);
1056 usb_fill_bulk_urb(urb
, serial
->dev
,
1057 usb_sndbulkpipe(serial
->dev
,
1058 port
->bulk_out_endpointAddress
),
1060 garmin_write_bulk_callback
,
1061 dismiss_ack
? NULL
: port
);
1062 urb
->transfer_flags
|= URB_ZERO_PACKET
;
1064 if (GARMIN_LAYERID_APPL
== getLayerId(buffer
)) {
1066 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1067 garmin_data_p
->flags
|= APP_REQ_SEEN
;
1068 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1070 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1071 pkt_clear(garmin_data_p
);
1072 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
1076 /* send it down the pipe */
1077 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1080 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1085 /* we are done with this urb, so let the host driver
1086 * really free it when it is finished with it */
1092 static int garmin_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1093 const unsigned char *buf
, int count
)
1095 int pktid
, pktsiz
, len
;
1096 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1097 __le32
*privpkt
= (__le32
*)garmin_data_p
->privpkt
;
1099 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, buf
);
1101 if (garmin_data_p
->state
== STATE_RESET
)
1104 /* check for our private packets */
1105 if (count
>= GARMIN_PKTHDR_LENGTH
) {
1110 memcpy(garmin_data_p
->privpkt
, buf
, len
);
1112 pktsiz
= getDataLength(garmin_data_p
->privpkt
);
1113 pktid
= getPacketId(garmin_data_p
->privpkt
);
1115 if (count
== (GARMIN_PKTHDR_LENGTH
+pktsiz
)
1116 && GARMIN_LAYERID_PRIVATE
==
1117 getLayerId(garmin_data_p
->privpkt
)) {
1119 dbg("%s - processing private request %d",
1122 /* drop all unfinished transfers */
1123 garmin_clear(garmin_data_p
);
1127 case PRIV_PKTID_SET_DEBUG
:
1130 debug
= __le32_to_cpu(privpkt
[3]);
1131 dbg("%s - debug level set to 0x%X",
1135 case PRIV_PKTID_SET_MODE
:
1138 garmin_data_p
->mode
= __le32_to_cpu(privpkt
[3]);
1139 dbg("%s - mode set to %d",
1140 __func__
, garmin_data_p
->mode
);
1143 case PRIV_PKTID_INFO_REQ
:
1144 priv_status_resp(port
);
1147 case PRIV_PKTID_RESET_REQ
:
1148 process_resetdev_request(port
);
1151 case PRIV_PKTID_SET_DEF_MODE
:
1154 initial_mode
= __le32_to_cpu(privpkt
[3]);
1155 dbg("%s - initial_mode set to %d",
1157 garmin_data_p
->mode
);
1164 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1165 return gsp_receive(garmin_data_p
, buf
, count
);
1166 } else { /* MODE_NATIVE */
1167 return nat_receive(garmin_data_p
, buf
, count
);
1172 static int garmin_write_room(struct tty_struct
*tty
)
1174 struct usb_serial_port
*port
= tty
->driver_data
;
1176 * Report back the bytes currently available in the output buffer.
1178 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1179 return GPS_OUT_BUFSIZ
-garmin_data_p
->outsize
;
1183 static void garmin_read_process(struct garmin_data
*garmin_data_p
,
1184 unsigned char *data
, unsigned data_length
,
1187 unsigned long flags
;
1189 if (garmin_data_p
->flags
& FLAGS_DROP_DATA
) {
1190 /* abort-transfer cmd is actice */
1191 dbg("%s - pkt dropped", __func__
);
1192 } else if (garmin_data_p
->state
!= STATE_DISCONNECTED
&&
1193 garmin_data_p
->state
!= STATE_RESET
) {
1195 /* if throttling is active or postprecessing is required
1196 put the received data in the input queue, otherwise
1197 send it directly to the tty port */
1198 if (garmin_data_p
->flags
& FLAGS_QUEUING
) {
1199 pkt_add(garmin_data_p
, data
, data_length
);
1200 } else if (bulk_data
||
1201 getLayerId(data
) == GARMIN_LAYERID_APPL
) {
1203 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1204 garmin_data_p
->flags
|= APP_RESP_SEEN
;
1205 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1207 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1208 pkt_add(garmin_data_p
, data
, data_length
);
1210 send_to_tty(garmin_data_p
->port
, data
,
1214 /* ignore system layer packets ... */
1219 static void garmin_read_bulk_callback(struct urb
*urb
)
1221 unsigned long flags
;
1222 struct usb_serial_port
*port
= urb
->context
;
1223 struct usb_serial
*serial
= port
->serial
;
1224 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1225 unsigned char *data
= urb
->transfer_buffer
;
1226 int status
= urb
->status
;
1229 dbg("%s - port %d", __func__
, port
->number
);
1232 dbg("%s - bad serial pointer, exiting", __func__
);
1237 dbg("%s - nonzero read bulk status received: %d",
1242 usb_serial_debug_data(debug
, &port
->dev
,
1243 __func__
, urb
->actual_length
, data
);
1245 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 1);
1247 if (urb
->actual_length
== 0 &&
1248 0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_RESTART
)) {
1249 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1250 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_RESTART
;
1251 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1252 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1255 "%s - failed resubmitting read urb, error %d\n",
1257 } else if (urb
->actual_length
> 0) {
1258 /* Continue trying to read until nothing more is received */
1259 if (0 == (garmin_data_p
->flags
& FLAGS_THROTTLED
)) {
1260 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1263 "%s - failed resubmitting read urb, "
1264 "error %d\n", __func__
, retval
);
1267 dbg("%s - end of bulk data", __func__
);
1268 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1269 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_ACTIVE
;
1270 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1275 static void garmin_read_int_callback(struct urb
*urb
)
1277 unsigned long flags
;
1279 struct usb_serial_port
*port
= urb
->context
;
1280 struct usb_serial
*serial
= port
->serial
;
1281 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1282 unsigned char *data
= urb
->transfer_buffer
;
1283 int status
= urb
->status
;
1292 /* this urb is terminated, clean up */
1293 dbg("%s - urb shutting down with status: %d",
1297 dbg("%s - nonzero urb status received: %d",
1302 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
1303 urb
->actual_length
, urb
->transfer_buffer
);
1305 if (urb
->actual_length
== sizeof(GARMIN_BULK_IN_AVAIL_REPLY
) &&
1306 0 == memcmp(data
, GARMIN_BULK_IN_AVAIL_REPLY
,
1307 sizeof(GARMIN_BULK_IN_AVAIL_REPLY
))) {
1309 dbg("%s - bulk data available.", __func__
);
1311 if (0 == (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1313 /* bulk data available */
1314 usb_fill_bulk_urb(port
->read_urb
, serial
->dev
,
1315 usb_rcvbulkpipe(serial
->dev
,
1316 port
->bulk_in_endpointAddress
),
1317 port
->read_urb
->transfer_buffer
,
1318 port
->read_urb
->transfer_buffer_length
,
1319 garmin_read_bulk_callback
, port
);
1320 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1323 "%s - failed submitting read urb, error %d\n",
1326 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1327 garmin_data_p
->flags
|= FLAGS_BULK_IN_ACTIVE
;
1328 spin_unlock_irqrestore(&garmin_data_p
->lock
,
1332 /* bulk-in transfer still active */
1333 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1334 garmin_data_p
->flags
|= FLAGS_BULK_IN_RESTART
;
1335 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1338 } else if (urb
->actual_length
== (4+sizeof(GARMIN_START_SESSION_REPLY
))
1339 && 0 == memcmp(data
, GARMIN_START_SESSION_REPLY
,
1340 sizeof(GARMIN_START_SESSION_REPLY
))) {
1342 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1343 garmin_data_p
->flags
|= FLAGS_SESSION_REPLY1_SEEN
;
1344 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1346 /* save the serial number */
1347 garmin_data_p
->serial_num
= __le32_to_cpup(
1348 (__le32
*)(data
+GARMIN_PKTHDR_LENGTH
));
1350 dbg("%s - start-of-session reply seen - serial %u.",
1351 __func__
, garmin_data_p
->serial_num
);
1354 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 0);
1356 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
1357 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1359 dev_err(&urb
->dev
->dev
,
1360 "%s - Error %d submitting interrupt urb\n",
1366 * Sends the next queued packt to the tty port (garmin native mode only)
1367 * and then sets a timer to call itself again until all queued data
1370 static int garmin_flush_queue(struct garmin_data
*garmin_data_p
)
1372 unsigned long flags
;
1373 struct garmin_packet
*pkt
;
1375 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1376 pkt
= pkt_pop(garmin_data_p
);
1378 send_to_tty(garmin_data_p
->port
, pkt
->data
, pkt
->size
);
1380 mod_timer(&garmin_data_p
->timer
, (1)+jiffies
);
1383 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1384 garmin_data_p
->flags
&= ~FLAGS_QUEUING
;
1385 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1392 static void garmin_throttle(struct tty_struct
*tty
)
1394 struct usb_serial_port
*port
= tty
->driver_data
;
1395 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1397 dbg("%s - port %d", __func__
, port
->number
);
1398 /* set flag, data received will be put into a queue
1399 for later processing */
1400 spin_lock_irq(&garmin_data_p
->lock
);
1401 garmin_data_p
->flags
|= FLAGS_QUEUING
|FLAGS_THROTTLED
;
1402 spin_unlock_irq(&garmin_data_p
->lock
);
1406 static void garmin_unthrottle(struct tty_struct
*tty
)
1408 struct usb_serial_port
*port
= tty
->driver_data
;
1409 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1412 dbg("%s - port %d", __func__
, port
->number
);
1413 spin_lock_irq(&garmin_data_p
->lock
);
1414 garmin_data_p
->flags
&= ~FLAGS_THROTTLED
;
1415 spin_unlock_irq(&garmin_data_p
->lock
);
1417 /* in native mode send queued data to tty, in
1418 serial mode nothing needs to be done here */
1419 if (garmin_data_p
->mode
== MODE_NATIVE
)
1420 garmin_flush_queue(garmin_data_p
);
1422 if (0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1423 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1426 "%s - failed resubmitting read urb, error %d\n",
1432 * The timer is currently only used to send queued packets to
1433 * the tty in cases where the protocol provides no own handshaking
1434 * to initiate the transfer.
1436 static void timeout_handler(unsigned long data
)
1438 struct garmin_data
*garmin_data_p
= (struct garmin_data
*) data
;
1440 /* send the next queued packet to the tty port */
1441 if (garmin_data_p
->mode
== MODE_NATIVE
)
1442 if (garmin_data_p
->flags
& FLAGS_QUEUING
)
1443 garmin_flush_queue(garmin_data_p
);
1448 static int garmin_attach(struct usb_serial
*serial
)
1451 struct usb_serial_port
*port
= serial
->port
[0];
1452 struct garmin_data
*garmin_data_p
= NULL
;
1454 dbg("%s", __func__
);
1456 garmin_data_p
= kzalloc(sizeof(struct garmin_data
), GFP_KERNEL
);
1457 if (garmin_data_p
== NULL
) {
1458 dev_err(&port
->dev
, "%s - Out of memory\n", __func__
);
1461 init_timer(&garmin_data_p
->timer
);
1462 spin_lock_init(&garmin_data_p
->lock
);
1463 INIT_LIST_HEAD(&garmin_data_p
->pktlist
);
1464 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1465 garmin_data_p
->timer
.data
= (unsigned long)garmin_data_p
;
1466 garmin_data_p
->timer
.function
= timeout_handler
;
1467 garmin_data_p
->port
= port
;
1468 garmin_data_p
->state
= 0;
1469 garmin_data_p
->flags
= 0;
1470 garmin_data_p
->count
= 0;
1471 usb_set_serial_port_data(port
, garmin_data_p
);
1473 status
= garmin_init_session(port
);
1479 static void garmin_disconnect(struct usb_serial
*serial
)
1481 struct usb_serial_port
*port
= serial
->port
[0];
1482 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1484 dbg("%s", __func__
);
1486 usb_kill_urb(port
->interrupt_in_urb
);
1487 del_timer_sync(&garmin_data_p
->timer
);
1491 static void garmin_release(struct usb_serial
*serial
)
1493 struct usb_serial_port
*port
= serial
->port
[0];
1494 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1496 dbg("%s", __func__
);
1498 kfree(garmin_data_p
);
1502 /* All of the device info needed */
1503 static struct usb_serial_driver garmin_device
= {
1505 .owner
= THIS_MODULE
,
1506 .name
= "garmin_gps",
1508 .description
= "Garmin GPS usb/tty",
1509 .usb_driver
= &garmin_driver
,
1510 .id_table
= id_table
,
1512 .open
= garmin_open
,
1513 .close
= garmin_close
,
1514 .throttle
= garmin_throttle
,
1515 .unthrottle
= garmin_unthrottle
,
1516 .attach
= garmin_attach
,
1517 .disconnect
= garmin_disconnect
,
1518 .release
= garmin_release
,
1519 .write
= garmin_write
,
1520 .write_room
= garmin_write_room
,
1521 .write_bulk_callback
= garmin_write_bulk_callback
,
1522 .read_bulk_callback
= garmin_read_bulk_callback
,
1523 .read_int_callback
= garmin_read_int_callback
,
1528 static int __init
garmin_init(void)
1532 retval
= usb_serial_register(&garmin_device
);
1534 goto failed_garmin_register
;
1535 retval
= usb_register(&garmin_driver
);
1537 goto failed_usb_register
;
1538 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_VERSION
":"
1542 failed_usb_register
:
1543 usb_serial_deregister(&garmin_device
);
1544 failed_garmin_register
:
1549 static void __exit
garmin_exit(void)
1551 usb_deregister(&garmin_driver
);
1552 usb_serial_deregister(&garmin_device
);
1558 module_init(garmin_init
);
1559 module_exit(garmin_exit
);
1561 MODULE_AUTHOR(DRIVER_AUTHOR
);
1562 MODULE_DESCRIPTION(DRIVER_DESC
);
1563 MODULE_LICENSE("GPL");
1565 module_param(debug
, bool, S_IWUSR
| S_IRUGO
);
1566 MODULE_PARM_DESC(debug
, "Debug enabled or not");
1567 module_param(initial_mode
, int, S_IRUGO
);
1568 MODULE_PARM_DESC(initial_mode
, "Initial mode");