4 * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de
6 * The latest version of the driver can be found at
7 * http://sourceforge.net/projects/garmin-gps/
9 * This driver has been derived from v2.1 of the visor driver.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/timer.h>
31 #include <linux/tty.h>
32 #include <linux/tty_driver.h>
33 #include <linux/tty_flip.h>
34 #include <linux/module.h>
35 #include <linux/spinlock.h>
36 #include <linux/uaccess.h>
37 #include <linux/atomic.h>
38 #include <linux/usb.h>
39 #include <linux/usb/serial.h>
41 /* the mode to be set when the port ist opened */
42 static int initial_mode
= 1;
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 */
219 MODULE_DEVICE_TABLE(usb
, id_table
);
222 static inline int getLayerId(const __u8
*usbPacket
)
224 return __le32_to_cpup((__le32
*)(usbPacket
));
227 static inline int getPacketId(const __u8
*usbPacket
)
229 return __le32_to_cpup((__le32
*)(usbPacket
+4));
232 static inline int getDataLength(const __u8
*usbPacket
)
234 return __le32_to_cpup((__le32
*)(usbPacket
+8));
239 * check if the usb-packet in buf contains an abort-transfer command.
240 * (if yes, all queued data will be dropped)
242 static inline int isAbortTrfCmnd(const unsigned char *buf
)
244 if (0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ
,
245 sizeof(GARMIN_STOP_TRANSFER_REQ
)) ||
246 0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ_V2
,
247 sizeof(GARMIN_STOP_TRANSFER_REQ_V2
)))
255 static void send_to_tty(struct usb_serial_port
*port
,
256 char *data
, unsigned int actual_length
)
258 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
260 if (tty
&& actual_length
) {
262 usb_serial_debug_data(debug
, &port
->dev
,
263 __func__
, actual_length
, data
);
265 tty_insert_flip_string(tty
, data
, actual_length
);
266 tty_flip_buffer_push(tty
);
272 /******************************************************************************
273 * packet queue handling
274 ******************************************************************************/
277 * queue a received (usb-)packet for later processing
279 static int pkt_add(struct garmin_data
*garmin_data_p
,
280 unsigned char *data
, unsigned int data_length
)
285 struct garmin_packet
*pkt
;
287 /* process only packets containg data ... */
289 pkt
= kmalloc(sizeof(struct garmin_packet
)+data_length
,
292 dev_err(&garmin_data_p
->port
->dev
, "out of memory\n");
295 pkt
->size
= data_length
;
296 memcpy(pkt
->data
, data
, data_length
);
298 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
299 garmin_data_p
->flags
|= FLAGS_QUEUING
;
300 result
= list_empty(&garmin_data_p
->pktlist
);
301 pkt
->seq
= garmin_data_p
->seq_counter
++;
302 list_add_tail(&pkt
->list
, &garmin_data_p
->pktlist
);
303 state
= garmin_data_p
->state
;
304 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
306 dbg("%s - added: pkt: %d - %d bytes",
307 __func__
, pkt
->seq
, data_length
);
309 /* in serial mode, if someone is waiting for data from
310 the device, convert and send the next packet to tty. */
311 if (result
&& (state
== STATE_GSP_WAIT_DATA
))
312 gsp_next_packet(garmin_data_p
);
318 /* get the next pending packet */
319 static struct garmin_packet
*pkt_pop(struct garmin_data
*garmin_data_p
)
322 struct garmin_packet
*result
= NULL
;
324 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
325 if (!list_empty(&garmin_data_p
->pktlist
)) {
326 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
327 list_del(&result
->list
);
329 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
334 /* free up all queued data */
335 static void pkt_clear(struct garmin_data
*garmin_data_p
)
338 struct garmin_packet
*result
= NULL
;
340 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
341 while (!list_empty(&garmin_data_p
->pktlist
)) {
342 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
343 list_del(&result
->list
);
346 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
350 /******************************************************************************
351 * garmin serial protocol handling handling
352 ******************************************************************************/
354 /* send an ack packet back to the tty */
355 static int gsp_send_ack(struct garmin_data
*garmin_data_p
, __u8 pkt_id
)
362 dbg("%s - pkt-id: 0x%X.", __func__
, 0xFF & pkt_id
);
378 *ptr
++ = 0xFF & (-cksum
);
384 send_to_tty(garmin_data_p
->port
, pkt
, l
);
391 * called for a complete packet received from tty layer
393 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
394 * at GSP_INITIAL_OFFSET.
396 * count - number of bytes in the input buffer including space reserved for
397 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
398 * (including pkt-id, data-length a. cksum)
400 static int gsp_rec_packet(struct garmin_data
*garmin_data_p
, int count
)
403 const __u8
*recpkt
= garmin_data_p
->inbuffer
+GSP_INITIAL_OFFSET
;
404 __le32
*usbdata
= (__le32
*) garmin_data_p
->inbuffer
;
408 int pktid
= recpkt
[0];
409 int size
= recpkt
[1];
411 usb_serial_debug_data(debug
, &garmin_data_p
->port
->dev
,
412 __func__
, count
-GSP_INITIAL_OFFSET
, recpkt
);
414 if (size
!= (count
-GSP_INITIAL_OFFSET
-3)) {
415 dbg("%s - invalid size, expected %d bytes, got %d",
416 __func__
, size
, (count
-GSP_INITIAL_OFFSET
-3));
423 /* sanity check, remove after test ... */
424 if ((__u8
*)&(usbdata
[3]) != recpkt
) {
425 dbg("%s - ptr mismatch %p - %p",
426 __func__
, &(usbdata
[4]), recpkt
);
435 if ((0xff & (cksum
+ *recpkt
)) != 0) {
436 dbg("%s - invalid checksum, expected %02x, got %02x",
437 __func__
, 0xff & -cksum
, 0xff & *recpkt
);
441 usbdata
[0] = __cpu_to_le32(GARMIN_LAYERID_APPL
);
442 usbdata
[1] = __cpu_to_le32(pktid
);
443 usbdata
[2] = __cpu_to_le32(size
);
445 garmin_write_bulk(garmin_data_p
->port
, garmin_data_p
->inbuffer
,
446 GARMIN_PKTHDR_LENGTH
+size
, 0);
448 /* if this was an abort-transfer command, flush all
450 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
451 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
452 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
453 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
454 pkt_clear(garmin_data_p
);
463 * Called for data received from tty
465 * buf contains the data read, it may span more than one packet or even
468 * input record should be a serial-record, but it may not be complete.
469 * Copy it into our local buffer, until an etx is seen (or an error
471 * Once the record is complete, convert into a usb packet and send it
472 * to the bulk pipe, send an ack back to the tty.
474 * If the input is an ack, just send the last queued packet to the
477 * if the input is an abort command, drop all queued data.
480 static int gsp_receive(struct garmin_data
*garmin_data_p
,
481 const unsigned char *buf
, int count
)
485 int ack_or_nak_seen
= 0;
488 /* dleSeen: set if last byte read was a DLE */
490 /* skip: if set, skip incoming data until possible start of
496 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
497 dest
= garmin_data_p
->inbuffer
;
498 size
= garmin_data_p
->insize
;
499 dleSeen
= garmin_data_p
->flags
& FLAGS_GSP_DLESEEN
;
500 skip
= garmin_data_p
->flags
& FLAGS_GSP_SKIP
;
501 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
503 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
504 __func__, dleSeen, skip, size, count); */
507 size
= GSP_INITIAL_OFFSET
;
509 while (offs
< count
) {
515 if (skip
) { /* start of a new pkt */
517 size
= GSP_INITIAL_OFFSET
;
519 } else if (dleSeen
) {
525 } else if (data
== ETX
) {
527 /* packet complete */
529 data
= dest
[GSP_INITIAL_OFFSET
];
532 ack_or_nak_seen
= ACK
;
533 dbg("ACK packet complete.");
534 } else if (data
== NAK
) {
535 ack_or_nak_seen
= NAK
;
536 dbg("NAK packet complete.");
538 dbg("packet complete - id=0x%X.",
540 gsp_rec_packet(garmin_data_p
, size
);
544 size
= GSP_INITIAL_OFFSET
;
552 size
= GSP_INITIAL_OFFSET
;
559 if (size
>= GPS_IN_BUFSIZ
) {
560 dbg("%s - packet too large.", __func__
);
562 size
= GSP_INITIAL_OFFSET
;
567 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
569 garmin_data_p
->insize
= size
;
571 /* copy flags back to structure */
573 garmin_data_p
->flags
|= FLAGS_GSP_SKIP
;
575 garmin_data_p
->flags
&= ~FLAGS_GSP_SKIP
;
578 garmin_data_p
->flags
|= FLAGS_GSP_DLESEEN
;
580 garmin_data_p
->flags
&= ~FLAGS_GSP_DLESEEN
;
582 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
584 if (ack_or_nak_seen
) {
585 if (gsp_next_packet(garmin_data_p
) > 0)
586 garmin_data_p
->state
= STATE_ACTIVE
;
588 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
596 * Sends a usb packet to the tty
598 * Assumes, that all packages and at an usb-packet boundary.
600 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
602 static int gsp_send(struct garmin_data
*garmin_data_p
,
603 const unsigned char *buf
, int count
)
605 const unsigned char *src
;
613 dbg("%s - state %d - %d bytes.", __func__
,
614 garmin_data_p
->state
, count
);
616 k
= garmin_data_p
->outsize
;
617 if ((k
+count
) > GPS_OUT_BUFSIZ
) {
618 dbg("packet too large");
619 garmin_data_p
->outsize
= 0;
623 memcpy(garmin_data_p
->outbuffer
+k
, buf
, count
);
625 garmin_data_p
->outsize
= k
;
627 if (k
>= GARMIN_PKTHDR_LENGTH
) {
628 pktid
= getPacketId(garmin_data_p
->outbuffer
);
629 datalen
= getDataLength(garmin_data_p
->outbuffer
);
630 i
= GARMIN_PKTHDR_LENGTH
+ datalen
;
637 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__
, k
, i
);
639 /* garmin_data_p->outbuffer now contains a complete packet */
641 usb_serial_debug_data(debug
, &garmin_data_p
->port
->dev
,
642 __func__
, k
, garmin_data_p
->outbuffer
);
644 garmin_data_p
->outsize
= 0;
646 if (GARMIN_LAYERID_APPL
!= getLayerId(garmin_data_p
->outbuffer
)) {
647 dbg("not an application packet (%d)",
648 getLayerId(garmin_data_p
->outbuffer
));
653 dbg("packet-id %d too large", pktid
);
658 dbg("packet-size %d too large", datalen
);
662 /* the serial protocol should be able to handle this packet */
665 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
666 for (i
= 0; i
< datalen
; i
++) {
671 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
672 if (k
> (GARMIN_PKTHDR_LENGTH
-2)) {
673 /* can't add stuffing DLEs in place, move data to end
675 dst
= garmin_data_p
->outbuffer
+GPS_OUT_BUFSIZ
-datalen
;
676 memcpy(dst
, src
, datalen
);
680 dst
= garmin_data_p
->outbuffer
;
690 for (i
= 0; i
< datalen
; i
++) {
698 cksum
= 0xFF & -cksum
;
705 i
= dst
-garmin_data_p
->outbuffer
;
707 send_to_tty(garmin_data_p
->port
, garmin_data_p
->outbuffer
, i
);
709 garmin_data_p
->pkt_id
= pktid
;
710 garmin_data_p
->state
= STATE_WAIT_TTY_ACK
;
717 * Process the next pending data packet - if there is one
719 static int gsp_next_packet(struct garmin_data
*garmin_data_p
)
722 struct garmin_packet
*pkt
= NULL
;
724 while ((pkt
= pkt_pop(garmin_data_p
)) != NULL
) {
725 dbg("%s - next pkt: %d", __func__
, pkt
->seq
);
726 result
= gsp_send(garmin_data_p
, pkt
->data
, pkt
->size
);
738 /******************************************************************************
740 ******************************************************************************/
744 * Called for data received from tty
746 * The input data is expected to be in garmin usb-packet format.
748 * buf contains the data read, it may span more than one packet
749 * or even incomplete packets
751 static int nat_receive(struct garmin_data
*garmin_data_p
,
752 const unsigned char *buf
, int count
)
760 while (offs
< count
) {
761 /* if buffer contains header, copy rest of data */
762 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
)
763 len
= GARMIN_PKTHDR_LENGTH
764 +getDataLength(garmin_data_p
->inbuffer
);
766 len
= GARMIN_PKTHDR_LENGTH
;
768 if (len
>= GPS_IN_BUFSIZ
) {
769 /* seems to be an invalid packet, ignore rest
771 dbg("%s - packet size too large: %d", __func__
, len
);
772 garmin_data_p
->insize
= 0;
776 len
-= garmin_data_p
->insize
;
777 if (len
> (count
-offs
))
780 dest
= garmin_data_p
->inbuffer
781 + garmin_data_p
->insize
;
782 memcpy(dest
, buf
+offs
, len
);
783 garmin_data_p
->insize
+= len
;
788 /* do we have a complete packet ? */
789 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
) {
790 len
= GARMIN_PKTHDR_LENGTH
+
791 getDataLength(garmin_data_p
->inbuffer
);
792 if (garmin_data_p
->insize
>= len
) {
793 garmin_write_bulk(garmin_data_p
->port
,
794 garmin_data_p
->inbuffer
,
796 garmin_data_p
->insize
= 0;
798 /* if this was an abort-transfer command,
799 flush all queued data. */
800 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
801 spin_lock_irqsave(&garmin_data_p
->lock
,
803 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
804 spin_unlock_irqrestore(
805 &garmin_data_p
->lock
, flags
);
806 pkt_clear(garmin_data_p
);
815 /******************************************************************************
817 ******************************************************************************/
819 static void priv_status_resp(struct usb_serial_port
*port
)
821 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
822 __le32
*pkt
= (__le32
*)garmin_data_p
->privpkt
;
824 pkt
[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE
);
825 pkt
[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP
);
826 pkt
[2] = __cpu_to_le32(12);
827 pkt
[3] = __cpu_to_le32(VERSION_MAJOR
<< 16 | VERSION_MINOR
);
828 pkt
[4] = __cpu_to_le32(garmin_data_p
->mode
);
829 pkt
[5] = __cpu_to_le32(garmin_data_p
->serial_num
);
831 send_to_tty(port
, (__u8
*)pkt
, 6 * 4);
835 /******************************************************************************
836 * Garmin specific driver functions
837 ******************************************************************************/
839 static int process_resetdev_request(struct usb_serial_port
*port
)
843 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
845 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
846 garmin_data_p
->flags
&= ~(CLEAR_HALT_REQUIRED
);
847 garmin_data_p
->state
= STATE_RESET
;
848 garmin_data_p
->serial_num
= 0;
849 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
851 usb_kill_urb(port
->interrupt_in_urb
);
852 dbg("%s - usb_reset_device", __func__
);
853 status
= usb_reset_device(port
->serial
->dev
);
855 dbg("%s - usb_reset_device failed: %d",
863 * clear all cached data
865 static int garmin_clear(struct garmin_data
*garmin_data_p
)
870 /* flush all queued data */
871 pkt_clear(garmin_data_p
);
873 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
874 garmin_data_p
->insize
= 0;
875 garmin_data_p
->outsize
= 0;
876 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
882 static int garmin_init_session(struct usb_serial_port
*port
)
884 struct usb_serial
*serial
= port
->serial
;
885 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
890 usb_kill_urb(port
->interrupt_in_urb
);
892 dbg("%s - adding interrupt input", __func__
);
893 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
895 dev_err(&serial
->dev
->dev
,
896 "%s - failed submitting interrupt urb, error %d\n",
901 * using the initialization method from gpsbabel. See comments in
902 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
905 dbg("%s - starting session ...", __func__
);
906 garmin_data_p
->state
= STATE_ACTIVE
;
908 for (i
= 0; i
< 3; i
++) {
909 status
= garmin_write_bulk(port
,
910 GARMIN_START_SESSION_REQ
,
911 sizeof(GARMIN_START_SESSION_REQ
), 0);
926 static int garmin_open(struct tty_struct
*tty
, struct usb_serial_port
*port
)
930 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
932 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
933 garmin_data_p
->mode
= initial_mode
;
934 garmin_data_p
->count
= 0;
935 garmin_data_p
->flags
&= FLAGS_SESSION_REPLY1_SEEN
;
936 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
938 /* shutdown any bulk reads that might be going on */
939 usb_kill_urb(port
->write_urb
);
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 usb_serial
*serial
= port
->serial
;
953 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
955 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__
,
956 port
->number
, garmin_data_p
->mode
,
957 garmin_data_p
->state
, garmin_data_p
->flags
);
962 mutex_lock(&port
->serial
->disc_mutex
);
964 if (!port
->serial
->disconnected
)
965 garmin_clear(garmin_data_p
);
967 /* shutdown our urbs */
968 usb_kill_urb(port
->read_urb
);
969 usb_kill_urb(port
->write_urb
);
971 /* keep reset state so we know that we must start a new session */
972 if (garmin_data_p
->state
!= STATE_RESET
)
973 garmin_data_p
->state
= STATE_DISCONNECTED
;
975 mutex_unlock(&port
->serial
->disc_mutex
);
979 static void garmin_write_bulk_callback(struct urb
*urb
)
981 struct usb_serial_port
*port
= urb
->context
;
984 struct garmin_data
*garmin_data_p
=
985 usb_get_serial_port_data(port
);
987 if (GARMIN_LAYERID_APPL
== getLayerId(urb
->transfer_buffer
)) {
989 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
990 gsp_send_ack(garmin_data_p
,
991 ((__u8
*)urb
->transfer_buffer
)[4]);
994 usb_serial_port_softint(port
);
997 /* Ignore errors that resulted from garmin_write_bulk with
1000 /* free up the transfer buffer, as usb_free_urb() does not do this */
1001 kfree(urb
->transfer_buffer
);
1005 static int garmin_write_bulk(struct usb_serial_port
*port
,
1006 const unsigned char *buf
, int count
,
1009 unsigned long flags
;
1010 struct usb_serial
*serial
= port
->serial
;
1011 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1013 unsigned char *buffer
;
1016 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1017 garmin_data_p
->flags
&= ~FLAGS_DROP_DATA
;
1018 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1020 buffer
= kmalloc(count
, GFP_ATOMIC
);
1022 dev_err(&port
->dev
, "out of memory\n");
1026 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1028 dev_err(&port
->dev
, "no more free urbs\n");
1033 memcpy(buffer
, buf
, count
);
1035 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, buffer
);
1037 usb_fill_bulk_urb(urb
, serial
->dev
,
1038 usb_sndbulkpipe(serial
->dev
,
1039 port
->bulk_out_endpointAddress
),
1041 garmin_write_bulk_callback
,
1042 dismiss_ack
? NULL
: port
);
1043 urb
->transfer_flags
|= URB_ZERO_PACKET
;
1045 if (GARMIN_LAYERID_APPL
== getLayerId(buffer
)) {
1047 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1048 garmin_data_p
->flags
|= APP_REQ_SEEN
;
1049 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1051 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1052 pkt_clear(garmin_data_p
);
1053 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
1057 /* send it down the pipe */
1058 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1061 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
1066 /* we are done with this urb, so let the host driver
1067 * really free it when it is finished with it */
1073 static int garmin_write(struct tty_struct
*tty
, struct usb_serial_port
*port
,
1074 const unsigned char *buf
, int count
)
1076 int pktid
, pktsiz
, len
;
1077 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1078 __le32
*privpkt
= (__le32
*)garmin_data_p
->privpkt
;
1080 usb_serial_debug_data(debug
, &port
->dev
, __func__
, count
, buf
);
1082 if (garmin_data_p
->state
== STATE_RESET
)
1085 /* check for our private packets */
1086 if (count
>= GARMIN_PKTHDR_LENGTH
) {
1091 memcpy(garmin_data_p
->privpkt
, buf
, len
);
1093 pktsiz
= getDataLength(garmin_data_p
->privpkt
);
1094 pktid
= getPacketId(garmin_data_p
->privpkt
);
1096 if (count
== (GARMIN_PKTHDR_LENGTH
+pktsiz
)
1097 && GARMIN_LAYERID_PRIVATE
==
1098 getLayerId(garmin_data_p
->privpkt
)) {
1100 dbg("%s - processing private request %d",
1103 /* drop all unfinished transfers */
1104 garmin_clear(garmin_data_p
);
1108 case PRIV_PKTID_SET_DEBUG
:
1111 debug
= __le32_to_cpu(privpkt
[3]);
1112 dbg("%s - debug level set to 0x%X",
1116 case PRIV_PKTID_SET_MODE
:
1119 garmin_data_p
->mode
= __le32_to_cpu(privpkt
[3]);
1120 dbg("%s - mode set to %d",
1121 __func__
, garmin_data_p
->mode
);
1124 case PRIV_PKTID_INFO_REQ
:
1125 priv_status_resp(port
);
1128 case PRIV_PKTID_RESET_REQ
:
1129 process_resetdev_request(port
);
1132 case PRIV_PKTID_SET_DEF_MODE
:
1135 initial_mode
= __le32_to_cpu(privpkt
[3]);
1136 dbg("%s - initial_mode set to %d",
1138 garmin_data_p
->mode
);
1145 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1146 return gsp_receive(garmin_data_p
, buf
, count
);
1147 } else { /* MODE_NATIVE */
1148 return nat_receive(garmin_data_p
, buf
, count
);
1153 static int garmin_write_room(struct tty_struct
*tty
)
1155 struct usb_serial_port
*port
= tty
->driver_data
;
1157 * Report back the bytes currently available in the output buffer.
1159 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1160 return GPS_OUT_BUFSIZ
-garmin_data_p
->outsize
;
1164 static void garmin_read_process(struct garmin_data
*garmin_data_p
,
1165 unsigned char *data
, unsigned data_length
,
1168 unsigned long flags
;
1170 if (garmin_data_p
->flags
& FLAGS_DROP_DATA
) {
1171 /* abort-transfer cmd is actice */
1172 dbg("%s - pkt dropped", __func__
);
1173 } else if (garmin_data_p
->state
!= STATE_DISCONNECTED
&&
1174 garmin_data_p
->state
!= STATE_RESET
) {
1176 /* if throttling is active or postprecessing is required
1177 put the received data in the input queue, otherwise
1178 send it directly to the tty port */
1179 if (garmin_data_p
->flags
& FLAGS_QUEUING
) {
1180 pkt_add(garmin_data_p
, data
, data_length
);
1181 } else if (bulk_data
||
1182 getLayerId(data
) == GARMIN_LAYERID_APPL
) {
1184 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1185 garmin_data_p
->flags
|= APP_RESP_SEEN
;
1186 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1188 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1189 pkt_add(garmin_data_p
, data
, data_length
);
1191 send_to_tty(garmin_data_p
->port
, data
,
1195 /* ignore system layer packets ... */
1200 static void garmin_read_bulk_callback(struct urb
*urb
)
1202 unsigned long flags
;
1203 struct usb_serial_port
*port
= urb
->context
;
1204 struct usb_serial
*serial
= port
->serial
;
1205 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1206 unsigned char *data
= urb
->transfer_buffer
;
1207 int status
= urb
->status
;
1211 dbg("%s - bad serial pointer, exiting", __func__
);
1216 dbg("%s - nonzero read bulk status received: %d",
1221 usb_serial_debug_data(debug
, &port
->dev
,
1222 __func__
, urb
->actual_length
, data
);
1224 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 1);
1226 if (urb
->actual_length
== 0 &&
1227 0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_RESTART
)) {
1228 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1229 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_RESTART
;
1230 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1231 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1234 "%s - failed resubmitting read urb, error %d\n",
1236 } else if (urb
->actual_length
> 0) {
1237 /* Continue trying to read until nothing more is received */
1238 if (0 == (garmin_data_p
->flags
& FLAGS_THROTTLED
)) {
1239 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1242 "%s - failed resubmitting read urb, "
1243 "error %d\n", __func__
, retval
);
1246 dbg("%s - end of bulk data", __func__
);
1247 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1248 garmin_data_p
->flags
&= ~FLAGS_BULK_IN_ACTIVE
;
1249 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1254 static void garmin_read_int_callback(struct urb
*urb
)
1256 unsigned long flags
;
1258 struct usb_serial_port
*port
= urb
->context
;
1259 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1260 unsigned char *data
= urb
->transfer_buffer
;
1261 int status
= urb
->status
;
1270 /* this urb is terminated, clean up */
1271 dbg("%s - urb shutting down with status: %d",
1275 dbg("%s - nonzero urb status received: %d",
1280 usb_serial_debug_data(debug
, &port
->dev
, __func__
,
1281 urb
->actual_length
, urb
->transfer_buffer
);
1283 if (urb
->actual_length
== sizeof(GARMIN_BULK_IN_AVAIL_REPLY
) &&
1284 0 == memcmp(data
, GARMIN_BULK_IN_AVAIL_REPLY
,
1285 sizeof(GARMIN_BULK_IN_AVAIL_REPLY
))) {
1287 dbg("%s - bulk data available.", __func__
);
1289 if (0 == (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1291 /* bulk data available */
1292 retval
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1295 "%s - failed submitting read urb, error %d\n",
1298 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1299 garmin_data_p
->flags
|= FLAGS_BULK_IN_ACTIVE
;
1300 spin_unlock_irqrestore(&garmin_data_p
->lock
,
1304 /* bulk-in transfer still active */
1305 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1306 garmin_data_p
->flags
|= FLAGS_BULK_IN_RESTART
;
1307 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1310 } else if (urb
->actual_length
== (4+sizeof(GARMIN_START_SESSION_REPLY
))
1311 && 0 == memcmp(data
, GARMIN_START_SESSION_REPLY
,
1312 sizeof(GARMIN_START_SESSION_REPLY
))) {
1314 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1315 garmin_data_p
->flags
|= FLAGS_SESSION_REPLY1_SEEN
;
1316 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1318 /* save the serial number */
1319 garmin_data_p
->serial_num
= __le32_to_cpup(
1320 (__le32
*)(data
+GARMIN_PKTHDR_LENGTH
));
1322 dbg("%s - start-of-session reply seen - serial %u.",
1323 __func__
, garmin_data_p
->serial_num
);
1326 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
, 0);
1328 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
1330 dev_err(&urb
->dev
->dev
,
1331 "%s - Error %d submitting interrupt urb\n",
1337 * Sends the next queued packt to the tty port (garmin native mode only)
1338 * and then sets a timer to call itself again until all queued data
1341 static int garmin_flush_queue(struct garmin_data
*garmin_data_p
)
1343 unsigned long flags
;
1344 struct garmin_packet
*pkt
;
1346 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1347 pkt
= pkt_pop(garmin_data_p
);
1349 send_to_tty(garmin_data_p
->port
, pkt
->data
, pkt
->size
);
1351 mod_timer(&garmin_data_p
->timer
, (1)+jiffies
);
1354 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
1355 garmin_data_p
->flags
&= ~FLAGS_QUEUING
;
1356 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
1363 static void garmin_throttle(struct tty_struct
*tty
)
1365 struct usb_serial_port
*port
= tty
->driver_data
;
1366 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1368 /* set flag, data received will be put into a queue
1369 for later processing */
1370 spin_lock_irq(&garmin_data_p
->lock
);
1371 garmin_data_p
->flags
|= FLAGS_QUEUING
|FLAGS_THROTTLED
;
1372 spin_unlock_irq(&garmin_data_p
->lock
);
1376 static void garmin_unthrottle(struct tty_struct
*tty
)
1378 struct usb_serial_port
*port
= tty
->driver_data
;
1379 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1382 spin_lock_irq(&garmin_data_p
->lock
);
1383 garmin_data_p
->flags
&= ~FLAGS_THROTTLED
;
1384 spin_unlock_irq(&garmin_data_p
->lock
);
1386 /* in native mode send queued data to tty, in
1387 serial mode nothing needs to be done here */
1388 if (garmin_data_p
->mode
== MODE_NATIVE
)
1389 garmin_flush_queue(garmin_data_p
);
1391 if (0 != (garmin_data_p
->flags
& FLAGS_BULK_IN_ACTIVE
)) {
1392 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1395 "%s - failed resubmitting read urb, error %d\n",
1401 * The timer is currently only used to send queued packets to
1402 * the tty in cases where the protocol provides no own handshaking
1403 * to initiate the transfer.
1405 static void timeout_handler(unsigned long data
)
1407 struct garmin_data
*garmin_data_p
= (struct garmin_data
*) data
;
1409 /* send the next queued packet to the tty port */
1410 if (garmin_data_p
->mode
== MODE_NATIVE
)
1411 if (garmin_data_p
->flags
& FLAGS_QUEUING
)
1412 garmin_flush_queue(garmin_data_p
);
1417 static int garmin_port_probe(struct usb_serial_port
*port
)
1420 struct garmin_data
*garmin_data_p
;
1422 garmin_data_p
= kzalloc(sizeof(struct garmin_data
), GFP_KERNEL
);
1423 if (garmin_data_p
== NULL
) {
1424 dev_err(&port
->dev
, "%s - Out of memory\n", __func__
);
1427 init_timer(&garmin_data_p
->timer
);
1428 spin_lock_init(&garmin_data_p
->lock
);
1429 INIT_LIST_HEAD(&garmin_data_p
->pktlist
);
1430 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1431 garmin_data_p
->timer
.data
= (unsigned long)garmin_data_p
;
1432 garmin_data_p
->timer
.function
= timeout_handler
;
1433 garmin_data_p
->port
= port
;
1434 garmin_data_p
->state
= 0;
1435 garmin_data_p
->flags
= 0;
1436 garmin_data_p
->count
= 0;
1437 usb_set_serial_port_data(port
, garmin_data_p
);
1439 status
= garmin_init_session(port
);
1445 static int garmin_port_remove(struct usb_serial_port
*port
)
1447 struct garmin_data
*garmin_data_p
= usb_get_serial_port_data(port
);
1449 usb_kill_urb(port
->interrupt_in_urb
);
1450 del_timer_sync(&garmin_data_p
->timer
);
1451 kfree(garmin_data_p
);
1456 /* All of the device info needed */
1457 static struct usb_serial_driver garmin_device
= {
1459 .owner
= THIS_MODULE
,
1460 .name
= "garmin_gps",
1462 .description
= "Garmin GPS usb/tty",
1463 .id_table
= id_table
,
1465 .open
= garmin_open
,
1466 .close
= garmin_close
,
1467 .throttle
= garmin_throttle
,
1468 .unthrottle
= garmin_unthrottle
,
1469 .port_probe
= garmin_port_probe
,
1470 .port_remove
= garmin_port_remove
,
1471 .write
= garmin_write
,
1472 .write_room
= garmin_write_room
,
1473 .write_bulk_callback
= garmin_write_bulk_callback
,
1474 .read_bulk_callback
= garmin_read_bulk_callback
,
1475 .read_int_callback
= garmin_read_int_callback
,
1478 static struct usb_serial_driver
* const serial_drivers
[] = {
1479 &garmin_device
, NULL
1482 module_usb_serial_driver(serial_drivers
, id_table
);
1484 MODULE_AUTHOR(DRIVER_AUTHOR
);
1485 MODULE_DESCRIPTION(DRIVER_DESC
);
1486 MODULE_LICENSE("GPL");
1488 module_param(debug
, bool, S_IWUSR
| S_IRUGO
);
1489 MODULE_PARM_DESC(debug
, "Debug enabled or not");
1490 module_param(initial_mode
, int, S_IRUGO
);
1491 MODULE_PARM_DESC(initial_mode
, "Initial mode");