4 * Copyright (C) 2004 Hermann Kneissel herkne@users.sourceforge.net
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/config.h>
27 #include <linux/kernel.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/timer.h>
32 #include <linux/tty.h>
33 #include <linux/tty_driver.h>
34 #include <linux/tty_flip.h>
35 #include <linux/module.h>
36 #include <linux/spinlock.h>
37 #include <asm/uaccess.h>
38 #include <linux/usb.h>
40 /* the mode to be set when the port ist opened */
41 static int initial_mode
= 1;
46 #include "usb-serial.h"
48 #define GARMIN_VENDOR_ID 0x091E
54 #define VERSION_MAJOR 0
55 #define VERSION_MINOR 23
58 #define _DRIVER_VERSION(a,b) "v" _STR(a) "." _STR(b)
59 #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
60 #define DRIVER_AUTHOR "hermann kneissel"
61 #define DRIVER_DESC "garmin gps driver"
63 /* error codes returned by the driver */
64 #define EINVPKT 1000 /* invalid packet structure */
67 // size of the header of a packet using the usb protocol
68 #define GARMIN_PKTHDR_LENGTH 12
70 // max. possible size of a packet using the serial protocol
71 #define MAX_SERIAL_PKT_SIZ (3+255+3)
73 // max. possible size of a packet with worst case stuffing
74 #define MAX_SERIAL_PKT_SIZ_STUFFED MAX_SERIAL_PKT_SIZ+256
76 // size of a buffer able to hold a complete (no stuffing) packet
77 // (the document protocol does not contain packets with a larger
78 // size, but in theory a packet may be 64k+12 bytes - if in
79 // later protocol versions larger packet sizes occur, this value
80 // should be increased accordingly, so the input buffer is always
81 // large enough the store a complete packet inclusive header)
82 #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
84 // size of a buffer able to hold a complete (incl. stuffing) packet
85 #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
87 // where to place the packet id of a serial packet, so we can
88 // prepend the usb-packet header without the need to move the
90 #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
92 // max. size of incoming private packets (header+1 param)
93 #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
95 #define GARMIN_LAYERID_TRANSPORT 0
96 #define GARMIN_LAYERID_APPL 20
97 // our own layer-id to use for some control mechanisms
98 #define GARMIN_LAYERID_PRIVATE 0x01106E4B
100 #define GARMIN_PKTID_PVT_DATA 51
101 #define GARMIN_PKTID_L001_COMMAND_DATA 10
103 #define CMND_ABORT_TRANSFER 0
105 // packet ids used in private layer
106 #define PRIV_PKTID_SET_DEBUG 1
107 #define PRIV_PKTID_SET_MODE 2
108 #define PRIV_PKTID_INFO_REQ 3
109 #define PRIV_PKTID_INFO_RESP 4
110 #define PRIV_PKTID_RESET_REQ 5
111 #define PRIV_PKTID_SET_DEF_MODE 6
119 /* structure used to queue incoming packets */
120 struct garmin_packet
{
121 struct list_head list
;
123 int size
; // the real size of the data array, always > 0
127 /* structure used to keep the current state of the driver */
136 struct timer_list timer
;
137 struct usb_serial_port
*port
;
141 __u8 inbuffer
[GPS_IN_BUFSIZ
]; /* tty -> usb */
142 __u8 outbuffer
[GPS_OUT_BUFSIZ
]; /* usb -> tty */
145 struct list_head pktlist
;
150 #define STATE_INITIAL_DELAY 1
151 #define STATE_TIMEOUT 2
152 #define STATE_SESSION_REQ1 3
153 #define STATE_SESSION_REQ2 4
154 #define STATE_ACTIVE 5
156 #define STATE_RESET 8
157 #define STATE_DISCONNECTED 9
158 #define STATE_WAIT_TTY_ACK 10
159 #define STATE_GSP_WAIT_DATA 11
161 #define MODE_NATIVE 0
162 #define MODE_GARMIN_SERIAL 1
164 // Flags used in garmin_data.flags:
165 #define FLAGS_SESSION_REPLY_MASK 0x00C0
166 #define FLAGS_SESSION_REPLY1_SEEN 0x0080
167 #define FLAGS_SESSION_REPLY2_SEEN 0x0040
168 #define FLAGS_BULK_IN_ACTIVE 0x0020
169 #define FLAGS_THROTTLED 0x0010
170 #define CLEAR_HALT_REQUIRED 0x0001
172 #define FLAGS_QUEUING 0x0100
173 #define FLAGS_APP_RESP_SEEN 0x0200
174 #define FLAGS_APP_REQ_SEEN 0x0400
175 #define FLAGS_DROP_DATA 0x0800
177 #define FLAGS_GSP_SKIP 0x1000
178 #define FLAGS_GSP_DLESEEN 0x2000
185 /* function prototypes */
186 static void 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
);
190 /* some special packets to be send or received */
191 static unsigned char const GARMIN_START_SESSION_REQ
[]
192 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
193 static unsigned char const GARMIN_START_SESSION_REQ2
[]
194 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
195 static unsigned char const GARMIN_START_SESSION_REPLY
[]
196 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
197 static unsigned char const GARMIN_SESSION_ACTIVE_REPLY
[]
198 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
199 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY
[]
200 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
201 static unsigned char const GARMIN_APP_LAYER_REPLY
[]
203 static unsigned char const GARMIN_START_PVT_REQ
[]
204 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
205 static unsigned char const GARMIN_STOP_PVT_REQ
[]
206 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
207 static unsigned char const GARMIN_STOP_TRANSFER_REQ
[]
208 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
209 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2
[]
210 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
211 static unsigned char const PRIVATE_REQ
[]
212 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
216 static struct usb_device_id id_table
[] = {
217 /* the same device id seems to be used by all usb enabled gps devices */
218 { USB_DEVICE(GARMIN_VENDOR_ID
, 3 ) },
219 { } /* Terminating entry */
222 MODULE_DEVICE_TABLE (usb
, id_table
);
224 static struct usb_driver garmin_driver
= {
225 .name
= "garmin_gps",
226 .probe
= usb_serial_probe
,
227 .disconnect
= usb_serial_disconnect
,
228 .id_table
= id_table
,
233 static inline int noResponseFromAppLayer(struct garmin_data
* garmin_data_p
)
235 return ((garmin_data_p
->flags
236 & (FLAGS_APP_REQ_SEEN
|FLAGS_APP_RESP_SEEN
))
237 == FLAGS_APP_REQ_SEEN
);
241 static inline int getLayerId(const __u8
*usbPacket
)
243 return __le32_to_cpup((__le32
*)(usbPacket
));
246 static inline int getPacketId(const __u8
*usbPacket
)
248 return __le32_to_cpup((__le32
*)(usbPacket
+4));
251 static inline int getDataLength(const __u8
*usbPacket
)
253 return __le32_to_cpup((__le32
*)(usbPacket
+8));
258 * check if the usb-packet in buf contains an abort-transfer command.
259 * (if yes, all queued data will be dropped)
261 static inline int isAbortTrfCmnd(const unsigned char *buf
)
263 if (0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ
,
264 sizeof(GARMIN_STOP_TRANSFER_REQ
)) ||
265 0 == memcmp(buf
, GARMIN_STOP_TRANSFER_REQ_V2
,
266 sizeof(GARMIN_STOP_TRANSFER_REQ_V2
)))
274 static void send_to_tty(struct usb_serial_port
*port
,
275 char *data
, unsigned int actual_length
)
277 struct tty_struct
*tty
= port
->tty
;
279 if (tty
&& actual_length
) {
281 usb_serial_debug_data(debug
, &port
->dev
,
282 __FUNCTION__
, actual_length
, data
);
284 tty_buffer_request_room(tty
, actual_length
);
285 tty_insert_flip_string(tty
, data
, actual_length
);
286 tty_flip_buffer_push(tty
);
291 /******************************************************************************
292 * packet queue handling
293 ******************************************************************************/
296 * queue a received (usb-)packet for later processing
298 static int pkt_add(struct garmin_data
* garmin_data_p
,
299 unsigned char *data
, unsigned int data_length
)
303 struct garmin_packet
*pkt
;
305 /* process only packets containg data ... */
307 garmin_data_p
->flags
|= FLAGS_QUEUING
;
308 pkt
= kmalloc(sizeof(struct garmin_packet
)+data_length
,
311 dev_err(&garmin_data_p
->port
->dev
, "out of memory\n");
314 pkt
->size
= data_length
;
315 memcpy(pkt
->data
, data
, data_length
);
317 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
318 result
= list_empty(&garmin_data_p
->pktlist
);
319 pkt
->seq
= garmin_data_p
->seq_counter
++;
320 list_add_tail(&pkt
->list
, &garmin_data_p
->pktlist
);
321 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
323 /* in serial mode, if someone is waiting for data from
324 the device, iconvert and send the next packet to tty. */
325 if (result
&& (garmin_data_p
->state
== STATE_GSP_WAIT_DATA
)) {
326 gsp_next_packet(garmin_data_p
);
333 /* get the next pending packet */
334 static struct garmin_packet
*pkt_pop(struct garmin_data
* garmin_data_p
)
337 struct garmin_packet
*result
= NULL
;
339 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
340 if (!list_empty(&garmin_data_p
->pktlist
)) {
341 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
342 list_del(&result
->list
);
344 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
349 /* free up all queued data */
350 static void pkt_clear(struct garmin_data
* garmin_data_p
)
353 struct garmin_packet
*result
= NULL
;
355 dbg("%s", __FUNCTION__
);
357 spin_lock_irqsave(&garmin_data_p
->lock
, flags
);
358 while (!list_empty(&garmin_data_p
->pktlist
)) {
359 result
= (struct garmin_packet
*)garmin_data_p
->pktlist
.next
;
360 list_del(&result
->list
);
363 spin_unlock_irqrestore(&garmin_data_p
->lock
, flags
);
367 /******************************************************************************
368 * garmin serial protocol handling handling
369 ******************************************************************************/
371 /* send an ack packet back to the tty */
372 static int gsp_send_ack(struct garmin_data
* garmin_data_p
, __u8 pkt_id
)
379 dbg("%s - pkt-id: 0x%X.", __FUNCTION__
, 0xFF & pkt_id
);
396 *ptr
++ = 0xFF & (-cksum
);
402 send_to_tty(garmin_data_p
->port
, pkt
, l
);
409 * called for a complete packet received from tty layer
411 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
412 * at GSP_INITIAL_OFFSET.
414 * count - number of bytes in the input buffer including space reserved for
415 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
416 * (including pkt-id, data-length a. cksum)
418 static int gsp_rec_packet(struct garmin_data
* garmin_data_p
, int count
)
420 const __u8
* recpkt
= garmin_data_p
->inbuffer
+GSP_INITIAL_OFFSET
;
421 __le32
*usbdata
= (__le32
*) garmin_data_p
->inbuffer
;
425 int pktid
= recpkt
[0];
426 int size
= recpkt
[1];
428 usb_serial_debug_data(debug
, &garmin_data_p
->port
->dev
,
429 __FUNCTION__
, count
-GSP_INITIAL_OFFSET
, recpkt
);
431 if (size
!= (count
-GSP_INITIAL_OFFSET
-3)) {
432 dbg("%s - invalid size, expected %d bytes, got %d",
433 __FUNCTION__
, size
, (count
-GSP_INITIAL_OFFSET
-3));
440 // sanity check, remove after test ...
441 if ((__u8
*)&(usbdata
[3]) != recpkt
) {
442 dbg("%s - ptr mismatch %p - %p",
443 __FUNCTION__
, &(usbdata
[4]), recpkt
);
452 if ((0xff & (cksum
+ *recpkt
)) != 0) {
453 dbg("%s - invalid checksum, expected %02x, got %02x",
454 __FUNCTION__
, 0xff & -cksum
, 0xff & *recpkt
);
458 usbdata
[0] = __cpu_to_le32(GARMIN_LAYERID_APPL
);
459 usbdata
[1] = __cpu_to_le32(pktid
);
460 usbdata
[2] = __cpu_to_le32(size
);
462 garmin_write_bulk (garmin_data_p
->port
, garmin_data_p
->inbuffer
,
463 GARMIN_PKTHDR_LENGTH
+size
);
465 /* if this was an abort-transfer command, flush all
467 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
468 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
469 pkt_clear(garmin_data_p
);
478 * Called for data received from tty
480 * buf contains the data read, it may span more than one packet or even
483 * input record should be a serial-record, but it may not be complete.
484 * Copy it into our local buffer, until an etx is seen (or an error
486 * Once the record is complete, convert into a usb packet and send it
487 * to the bulk pipe, send an ack back to the tty.
489 * If the input is an ack, just send the last queued packet to the
492 * if the input is an abort command, drop all queued data.
495 static int gsp_receive(struct garmin_data
* garmin_data_p
,
496 const unsigned char *buf
, int count
)
499 int ack_or_nak_seen
= 0;
501 __u8
*dest
= garmin_data_p
->inbuffer
;
502 int size
= garmin_data_p
->insize
;
503 // dleSeen: set if last byte read was a DLE
504 int dleSeen
= garmin_data_p
->flags
& FLAGS_GSP_DLESEEN
;
505 // skip: if set, skip incoming data until possible start of
507 int skip
= garmin_data_p
->flags
& FLAGS_GSP_SKIP
;
510 dbg("%s - dle=%d skip=%d size=%d count=%d",
511 __FUNCTION__
, dleSeen
, skip
, size
, count
);
514 size
= GSP_INITIAL_OFFSET
;
517 while (offs
< count
) {
523 if (skip
) { /* start of a new pkt */
525 size
= GSP_INITIAL_OFFSET
;
527 } else if (dleSeen
) {
533 } else if (data
== ETX
) {
535 /* packet complete */
537 data
= dest
[GSP_INITIAL_OFFSET
];
540 ack_or_nak_seen
= ACK
;
541 dbg("ACK packet complete.");
542 } else if (data
== NAK
) {
543 ack_or_nak_seen
= NAK
;
544 dbg("NAK packet complete.");
546 dbg("packet complete "
549 gsp_rec_packet(garmin_data_p
, size
);
553 size
= GSP_INITIAL_OFFSET
;
561 dbg("non-masked DLE at %d - restarting", i
);
562 size
= GSP_INITIAL_OFFSET
;
569 if (size
>= GPS_IN_BUFSIZ
) {
570 dbg("%s - packet too large.", __FUNCTION__
);
572 size
= GSP_INITIAL_OFFSET
;
577 garmin_data_p
->insize
= size
;
579 // copy flags back to structure
581 garmin_data_p
->flags
|= FLAGS_GSP_SKIP
;
583 garmin_data_p
->flags
&= ~FLAGS_GSP_SKIP
;
586 garmin_data_p
->flags
|= FLAGS_GSP_DLESEEN
;
588 garmin_data_p
->flags
&= ~FLAGS_GSP_DLESEEN
;
590 if (ack_or_nak_seen
) {
591 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
592 gsp_next_packet(garmin_data_p
);
602 * Sends a usb packet to the tty
604 * Assumes, that all packages and at an usb-packet boundary.
606 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
608 static int gsp_send(struct garmin_data
* garmin_data_p
,
609 const unsigned char *buf
, int count
)
611 const unsigned char *src
;
619 dbg("%s - state %d - %d bytes.", __FUNCTION__
,
620 garmin_data_p
->state
, count
);
622 k
= garmin_data_p
->outsize
;
623 if ((k
+count
) > GPS_OUT_BUFSIZ
) {
624 dbg("packet too large");
625 garmin_data_p
->outsize
= 0;
629 memcpy(garmin_data_p
->outbuffer
+k
, buf
, count
);
631 garmin_data_p
->outsize
= k
;
633 if (k
>= GARMIN_PKTHDR_LENGTH
) {
634 pktid
= getPacketId(garmin_data_p
->outbuffer
);
635 datalen
= getDataLength(garmin_data_p
->outbuffer
);
636 i
= GARMIN_PKTHDR_LENGTH
+ datalen
;
643 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __FUNCTION__
,
646 /* garmin_data_p->outbuffer now contains a complete packet */
648 usb_serial_debug_data(debug
, &garmin_data_p
->port
->dev
,
649 __FUNCTION__
, k
, garmin_data_p
->outbuffer
);
651 garmin_data_p
->outsize
= 0;
653 if (GARMIN_LAYERID_APPL
!= getLayerId(garmin_data_p
->outbuffer
)) {
654 dbg("not an application packet (%d)",
655 getLayerId(garmin_data_p
->outbuffer
));
660 dbg("packet-id %d too large", pktid
);
665 dbg("packet-size %d too large", datalen
);
669 /* the serial protocol should be able to handle this packet */
672 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
673 for (i
=0; i
<datalen
; i
++) {
678 src
= garmin_data_p
->outbuffer
+GARMIN_PKTHDR_LENGTH
;
679 if (k
> (GARMIN_PKTHDR_LENGTH
-2)) {
680 /* can't add stuffing DLEs in place, move data to end
682 dst
= garmin_data_p
->outbuffer
+GPS_OUT_BUFSIZ
-datalen
;
683 memcpy(dst
, src
, datalen
);
687 dst
= garmin_data_p
->outbuffer
;
697 for (i
=0; i
<datalen
; i
++) {
705 cksum
= 0xFF & -cksum
;
712 i
= dst
-garmin_data_p
->outbuffer
;
714 send_to_tty(garmin_data_p
->port
, garmin_data_p
->outbuffer
, i
);
716 garmin_data_p
->pkt_id
= pktid
;
717 garmin_data_p
->state
= STATE_WAIT_TTY_ACK
;
727 * Process the next pending data packet - if there is one
729 static void gsp_next_packet(struct garmin_data
* garmin_data_p
)
731 struct garmin_packet
*pkt
= NULL
;
733 while ((pkt
= pkt_pop(garmin_data_p
)) != NULL
) {
734 dbg("%s - next pkt: %d", __FUNCTION__
, pkt
->seq
);
735 if (gsp_send(garmin_data_p
, pkt
->data
, pkt
->size
) > 0) {
746 /******************************************************************************
748 ******************************************************************************/
752 * Called for data received from tty
754 * The input data is expected to be in garmin usb-packet format.
756 * buf contains the data read, it may span more than one packet
757 * or even incomplete packets
759 static int nat_receive(struct garmin_data
* garmin_data_p
,
760 const unsigned char *buf
, int count
)
767 while (offs
< count
) {
768 // if buffer contains header, copy rest of data
769 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
)
770 len
= GARMIN_PKTHDR_LENGTH
771 +getDataLength(garmin_data_p
->inbuffer
);
773 len
= GARMIN_PKTHDR_LENGTH
;
775 if (len
>= GPS_IN_BUFSIZ
) {
776 /* seem to be an invalid packet, ignore rest of input */
777 dbg("%s - packet size too large: %d",
779 garmin_data_p
->insize
= 0;
783 len
-= garmin_data_p
->insize
;
784 if (len
> (count
-offs
))
787 dest
= garmin_data_p
->inbuffer
788 +garmin_data_p
->insize
;
789 memcpy(dest
, buf
+offs
, len
);
790 garmin_data_p
->insize
+= len
;
795 /* do we have a complete packet ? */
796 if (garmin_data_p
->insize
>= GARMIN_PKTHDR_LENGTH
) {
797 len
= GARMIN_PKTHDR_LENGTH
+
798 getDataLength(garmin_data_p
->inbuffer
);
799 if (garmin_data_p
->insize
>= len
) {
800 garmin_write_bulk (garmin_data_p
->port
,
801 garmin_data_p
->inbuffer
,
803 garmin_data_p
->insize
= 0;
805 /* if this was an abort-transfer command,
806 flush all queued data. */
807 if (isAbortTrfCmnd(garmin_data_p
->inbuffer
)) {
808 garmin_data_p
->flags
|= FLAGS_DROP_DATA
;
809 pkt_clear(garmin_data_p
);
818 /******************************************************************************
820 ******************************************************************************/
822 static void priv_status_resp(struct usb_serial_port
*port
)
824 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
825 __le32
*pkt
= (__le32
*)garmin_data_p
->privpkt
;
827 pkt
[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE
);
828 pkt
[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP
);
829 pkt
[2] = __cpu_to_le32(12);
830 pkt
[3] = __cpu_to_le32(VERSION_MAJOR
<< 16 | VERSION_MINOR
);
831 pkt
[4] = __cpu_to_le32(garmin_data_p
->mode
);
832 pkt
[5] = __cpu_to_le32(garmin_data_p
->serial_num
);
834 send_to_tty(port
, (__u8
*)pkt
, 6*4);
838 /******************************************************************************
839 * Garmin specific driver functions
840 ******************************************************************************/
842 static int process_resetdev_request(struct usb_serial_port
*port
)
845 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
847 garmin_data_p
->flags
&= ~(CLEAR_HALT_REQUIRED
);
848 garmin_data_p
->state
= STATE_RESET
;
849 garmin_data_p
->serial_num
= 0;
851 usb_kill_urb (port
->interrupt_in_urb
);
852 dbg("%s - usb_reset_device", __FUNCTION__
);
853 status
= usb_reset_device(port
->serial
->dev
);
855 dbg("%s - usb_reset_device failed: %d",
856 __FUNCTION__
, status
);
863 * clear all cached data
865 static int garmin_clear(struct garmin_data
* garmin_data_p
)
869 struct usb_serial_port
*port
= garmin_data_p
->port
;
871 if (port
!= NULL
&& garmin_data_p
->flags
& FLAGS_APP_RESP_SEEN
) {
872 /* send a terminate command */
873 status
= garmin_write_bulk(port
, GARMIN_STOP_TRANSFER_REQ
,
874 sizeof(GARMIN_STOP_TRANSFER_REQ
));
877 /* flush all queued data */
878 pkt_clear(garmin_data_p
);
880 garmin_data_p
->insize
= 0;
881 garmin_data_p
->outsize
= 0;
891 static int garmin_init_session(struct usb_serial_port
*port
)
893 struct usb_serial
*serial
= port
->serial
;
894 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
898 usb_kill_urb (port
->interrupt_in_urb
);
900 dbg("%s - adding interrupt input", __FUNCTION__
);
901 port
->interrupt_in_urb
->dev
= serial
->dev
;
902 status
= usb_submit_urb(port
->interrupt_in_urb
, GFP_KERNEL
);
904 dev_err(&serial
->dev
->dev
,
905 "%s - failed submitting interrupt urb,"
907 __FUNCTION__
, status
);
911 dbg("%s - starting session ...", __FUNCTION__
);
912 garmin_data_p
->state
= STATE_ACTIVE
;
913 status
= garmin_write_bulk(port
, GARMIN_START_SESSION_REQ
,
914 sizeof(GARMIN_START_SESSION_REQ
));
918 garmin_data_p
->ignorePkts
++;
920 /* not needed, but the win32 driver does it too ... */
921 status
= garmin_write_bulk(port
,
922 GARMIN_START_SESSION_REQ2
,
923 sizeof(GARMIN_START_SESSION_REQ2
));
926 garmin_data_p
->ignorePkts
++;
938 static int garmin_open (struct usb_serial_port
*port
, struct file
*filp
)
941 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
943 dbg("%s - port %d", __FUNCTION__
, port
->number
);
946 * Force low_latency on so that our tty_push actually forces the data
947 * through, otherwise it is scheduled, and with high data rates (like
948 * with OHCI) data can get lost.
951 port
->tty
->low_latency
= 1;
953 garmin_data_p
->mode
= initial_mode
;
954 garmin_data_p
->count
= 0;
955 garmin_data_p
->flags
= 0;
957 /* shutdown any bulk reads that might be going on */
958 usb_kill_urb (port
->write_urb
);
959 usb_kill_urb (port
->read_urb
);
961 if (garmin_data_p
->state
== STATE_RESET
) {
962 status
= garmin_init_session(port
);
965 garmin_data_p
->state
= STATE_ACTIVE
;
971 static void garmin_close (struct usb_serial_port
*port
, struct file
* filp
)
973 struct usb_serial
*serial
= port
->serial
;
974 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
976 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __FUNCTION__
,
977 port
->number
, garmin_data_p
->mode
,
978 garmin_data_p
->state
, garmin_data_p
->flags
);
983 garmin_clear(garmin_data_p
);
985 /* shutdown our urbs */
986 usb_kill_urb (port
->read_urb
);
987 usb_kill_urb (port
->write_urb
);
989 if (noResponseFromAppLayer(garmin_data_p
) ||
990 ((garmin_data_p
->flags
& CLEAR_HALT_REQUIRED
) != 0)) {
991 process_resetdev_request(port
);
992 garmin_data_p
->state
= STATE_RESET
;
994 garmin_data_p
->state
= STATE_DISCONNECTED
;
999 static void garmin_write_bulk_callback (struct urb
*urb
, struct pt_regs
*regs
)
1001 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1002 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1004 /* free up the transfer buffer, as usb_free_urb() does not do this */
1005 kfree (urb
->transfer_buffer
);
1007 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1010 dbg("%s - nonzero write bulk status received: %d",
1011 __FUNCTION__
, urb
->status
);
1012 garmin_data_p
->flags
|= CLEAR_HALT_REQUIRED
;
1015 usb_serial_port_softint(port
);
1019 static int garmin_write_bulk (struct usb_serial_port
*port
,
1020 const unsigned char *buf
, int count
)
1022 struct usb_serial
*serial
= port
->serial
;
1023 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1025 unsigned char *buffer
;
1028 dbg("%s - port %d, state %d", __FUNCTION__
, port
->number
,
1029 garmin_data_p
->state
);
1031 garmin_data_p
->flags
&= ~FLAGS_DROP_DATA
;
1033 buffer
= kmalloc (count
, GFP_ATOMIC
);
1035 dev_err(&port
->dev
, "out of memory\n");
1039 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
1041 dev_err(&port
->dev
, "no more free urbs\n");
1046 memcpy (buffer
, buf
, count
);
1048 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
, count
, buffer
);
1050 usb_fill_bulk_urb (urb
, serial
->dev
,
1051 usb_sndbulkpipe (serial
->dev
,
1052 port
->bulk_out_endpointAddress
),
1054 garmin_write_bulk_callback
, port
);
1055 urb
->transfer_flags
|= URB_ZERO_PACKET
;
1057 if (GARMIN_LAYERID_APPL
== getLayerId(buffer
)) {
1058 garmin_data_p
->flags
|= FLAGS_APP_REQ_SEEN
;
1059 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1060 pkt_clear(garmin_data_p
);
1061 garmin_data_p
->state
= STATE_GSP_WAIT_DATA
;
1065 /* send it down the pipe */
1066 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
1069 "%s - usb_submit_urb(write bulk) "
1070 "failed with status = %d\n",
1071 __FUNCTION__
, status
);
1075 if (GARMIN_LAYERID_APPL
== getLayerId(buffer
)
1076 && (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
)) {
1078 gsp_send_ack(garmin_data_p
, buffer
[4]);
1082 /* we are done with this urb, so let the host driver
1083 * really free it when it is finished with it */
1091 static int garmin_write (struct usb_serial_port
*port
,
1092 const unsigned char *buf
, int count
)
1094 int pktid
, pktsiz
, len
;
1095 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1096 __le32
*privpkt
= (__le32
*)garmin_data_p
->privpkt
;
1098 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
, count
, buf
);
1100 /* check for our private packets */
1101 if (count
>= GARMIN_PKTHDR_LENGTH
) {
1107 memcpy(garmin_data_p
->privpkt
, buf
, len
);
1109 pktsiz
= getDataLength(garmin_data_p
->privpkt
);
1110 pktid
= getPacketId(garmin_data_p
->privpkt
);
1112 if (count
== (GARMIN_PKTHDR_LENGTH
+pktsiz
)
1113 && GARMIN_LAYERID_PRIVATE
== getLayerId(garmin_data_p
->privpkt
)) {
1115 dbg("%s - processing private request %d",
1116 __FUNCTION__
, pktid
);
1118 // drop all unfinished transfers
1119 garmin_clear(garmin_data_p
);
1123 case PRIV_PKTID_SET_DEBUG
:
1126 debug
= __le32_to_cpu(privpkt
[3]);
1127 dbg("%s - debug level set to 0x%X",
1128 __FUNCTION__
, debug
);
1131 case PRIV_PKTID_SET_MODE
:
1134 garmin_data_p
->mode
= __le32_to_cpu(privpkt
[3]);
1135 dbg("%s - mode set to %d",
1136 __FUNCTION__
, garmin_data_p
->mode
);
1139 case PRIV_PKTID_INFO_REQ
:
1140 priv_status_resp(port
);
1143 case PRIV_PKTID_RESET_REQ
:
1144 garmin_data_p
->flags
|= FLAGS_APP_REQ_SEEN
;
1147 case PRIV_PKTID_SET_DEF_MODE
:
1150 initial_mode
= __le32_to_cpu(privpkt
[3]);
1151 dbg("%s - initial_mode set to %d",
1153 garmin_data_p
->mode
);
1160 if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1161 return gsp_receive(garmin_data_p
, buf
, count
);
1162 } else { /* MODE_NATIVE */
1163 return nat_receive(garmin_data_p
, buf
, count
);
1168 static int garmin_write_room (struct usb_serial_port
*port
)
1171 * Report back the bytes currently available in the output buffer.
1173 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1174 return GPS_OUT_BUFSIZ
-garmin_data_p
->outsize
;
1178 static int garmin_chars_in_buffer (struct usb_serial_port
*port
)
1181 * Report back the number of bytes currently in our input buffer.
1182 * Will this lock up the driver - the buffer contains an incomplete
1183 * package which will not be written to the device until it
1184 * has been completed ?
1186 //struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1187 //return garmin_data_p->insize;
1192 static void garmin_read_process(struct garmin_data
* garmin_data_p
,
1193 unsigned char *data
, unsigned data_length
)
1195 if (garmin_data_p
->flags
& FLAGS_DROP_DATA
) {
1196 /* abort-transfer cmd is actice */
1197 dbg("%s - pkt dropped", __FUNCTION__
);
1198 } else if (garmin_data_p
->state
!= STATE_DISCONNECTED
&&
1199 garmin_data_p
->state
!= STATE_RESET
) {
1201 /* remember any appl.layer packets, so we know
1202 if a reset is required or not when closing
1204 if (0 == memcmp(data
, GARMIN_APP_LAYER_REPLY
,
1205 sizeof(GARMIN_APP_LAYER_REPLY
)))
1206 garmin_data_p
->flags
|= FLAGS_APP_RESP_SEEN
;
1208 /* if throttling is active or postprecessing is required
1209 put the received data in th input queue, otherwise
1210 send it directly to the tty port */
1211 if (garmin_data_p
->flags
& FLAGS_QUEUING
) {
1212 pkt_add(garmin_data_p
, data
, data_length
);
1213 } else if (garmin_data_p
->mode
== MODE_GARMIN_SERIAL
) {
1214 if (getLayerId(data
) == GARMIN_LAYERID_APPL
) {
1215 pkt_add(garmin_data_p
, data
, data_length
);
1218 send_to_tty(garmin_data_p
->port
, data
, data_length
);
1224 static void garmin_read_bulk_callback (struct urb
*urb
, struct pt_regs
*regs
)
1226 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1227 struct usb_serial
*serial
= port
->serial
;
1228 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1229 unsigned char *data
= urb
->transfer_buffer
;
1232 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1235 dbg("%s - bad serial pointer, exiting", __FUNCTION__
);
1240 dbg("%s - nonzero read bulk status received: %d",
1241 __FUNCTION__
, urb
->status
);
1245 usb_serial_debug_data(debug
, &port
->dev
,
1246 __FUNCTION__
, urb
->actual_length
, data
);
1248 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
);
1250 /* Continue trying to read until nothing more is received */
1251 if (urb
->actual_length
> 0) {
1252 usb_fill_bulk_urb (port
->read_urb
, serial
->dev
,
1253 usb_rcvbulkpipe (serial
->dev
,
1254 port
->bulk_in_endpointAddress
),
1255 port
->read_urb
->transfer_buffer
,
1256 port
->read_urb
->transfer_buffer_length
,
1257 garmin_read_bulk_callback
, port
);
1258 status
= usb_submit_urb(port
->read_urb
, GFP_ATOMIC
);
1261 "%s - failed resubmitting read urb, error %d\n",
1262 __FUNCTION__
, status
);
1268 static void garmin_read_int_callback (struct urb
*urb
, struct pt_regs
*regs
)
1271 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
1272 struct usb_serial
*serial
= port
->serial
;
1273 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1274 unsigned char *data
= urb
->transfer_buffer
;
1276 switch (urb
->status
) {
1283 /* this urb is terminated, clean up */
1284 dbg("%s - urb shutting down with status: %d",
1285 __FUNCTION__
, urb
->status
);
1288 dbg("%s - nonzero urb status received: %d",
1289 __FUNCTION__
, urb
->status
);
1293 usb_serial_debug_data(debug
, &port
->dev
, __FUNCTION__
,
1294 urb
->actual_length
, urb
->transfer_buffer
);
1296 if (urb
->actual_length
== sizeof(GARMIN_BULK_IN_AVAIL_REPLY
) &&
1297 0 == memcmp(data
, GARMIN_BULK_IN_AVAIL_REPLY
,
1298 sizeof(GARMIN_BULK_IN_AVAIL_REPLY
))) {
1300 dbg("%s - bulk data available.", __FUNCTION__
);
1302 /* bulk data available */
1303 usb_fill_bulk_urb (port
->read_urb
, serial
->dev
,
1304 usb_rcvbulkpipe (serial
->dev
,
1305 port
->bulk_in_endpointAddress
),
1306 port
->read_urb
->transfer_buffer
,
1307 port
->read_urb
->transfer_buffer_length
,
1308 garmin_read_bulk_callback
, port
);
1309 status
= usb_submit_urb(port
->read_urb
, GFP_KERNEL
);
1312 "%s - failed submitting read urb, error %d\n",
1313 __FUNCTION__
, status
);
1316 } else if (urb
->actual_length
== (4+sizeof(GARMIN_START_SESSION_REPLY
))
1317 && 0 == memcmp(data
, GARMIN_START_SESSION_REPLY
,
1318 sizeof(GARMIN_START_SESSION_REPLY
))) {
1320 garmin_data_p
->flags
|= FLAGS_SESSION_REPLY1_SEEN
;
1322 /* save the serial number */
1323 garmin_data_p
->serial_num
1324 = __le32_to_cpup((__le32
*)(data
+GARMIN_PKTHDR_LENGTH
));
1326 dbg("%s - start-of-session reply seen - serial %u.",
1327 __FUNCTION__
, garmin_data_p
->serial_num
);
1330 if (garmin_data_p
->ignorePkts
) {
1331 /* this reply belongs to a request generated by the driver,
1333 dbg("%s - pkt ignored (%d)",
1334 __FUNCTION__
, garmin_data_p
->ignorePkts
);
1335 garmin_data_p
->ignorePkts
--;
1337 garmin_read_process(garmin_data_p
, data
, urb
->actual_length
);
1340 port
->interrupt_in_urb
->dev
= port
->serial
->dev
;
1341 status
= usb_submit_urb (urb
, GFP_ATOMIC
);
1343 dev_err(&urb
->dev
->dev
,
1344 "%s - Error %d submitting interrupt urb\n",
1345 __FUNCTION__
, status
);
1350 * Sends the next queued packt to the tty port (garmin native mode only)
1351 * and then sets a timer to call itself again until all queued data
1354 static int garmin_flush_queue(struct garmin_data
* garmin_data_p
)
1356 struct garmin_packet
*pkt
;
1358 if ((garmin_data_p
->flags
& FLAGS_THROTTLED
) == 0) {
1359 pkt
= pkt_pop(garmin_data_p
);
1362 send_to_tty(garmin_data_p
->port
, pkt
->data
, pkt
->size
);
1364 mod_timer(&garmin_data_p
->timer
, (1)+jiffies
);
1367 garmin_data_p
->flags
&= ~FLAGS_QUEUING
;
1374 static void garmin_throttle (struct usb_serial_port
*port
)
1376 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1378 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1379 /* set flag, data received will be put into a queue
1380 for later processing */
1381 garmin_data_p
->flags
|= FLAGS_QUEUING
|FLAGS_THROTTLED
;
1385 static void garmin_unthrottle (struct usb_serial_port
*port
)
1387 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1389 dbg("%s - port %d", __FUNCTION__
, port
->number
);
1390 garmin_data_p
->flags
&= ~FLAGS_THROTTLED
;
1392 /* in native mode send queued data to tty, in
1393 serial mode nothing needs to be done here */
1394 if (garmin_data_p
->mode
== MODE_NATIVE
)
1395 garmin_flush_queue(garmin_data_p
);
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_attach (struct usb_serial
*serial
)
1420 struct usb_serial_port
*port
= serial
->port
[0];
1421 struct garmin_data
* garmin_data_p
= NULL
;
1423 dbg("%s", __FUNCTION__
);
1425 garmin_data_p
= kzalloc(sizeof(struct garmin_data
), GFP_KERNEL
);
1426 if (garmin_data_p
== NULL
) {
1427 dev_err(&port
->dev
, "%s - Out of memory\n", __FUNCTION__
);
1430 init_timer(&garmin_data_p
->timer
);
1431 spin_lock_init(&garmin_data_p
->lock
);
1432 INIT_LIST_HEAD(&garmin_data_p
->pktlist
);
1433 //garmin_data_p->timer.expires = jiffies + session_timeout;
1434 garmin_data_p
->timer
.data
= (unsigned long)garmin_data_p
;
1435 garmin_data_p
->timer
.function
= timeout_handler
;
1436 garmin_data_p
->port
= port
;
1437 garmin_data_p
->state
= 0;
1438 garmin_data_p
->count
= 0;
1439 usb_set_serial_port_data(port
, garmin_data_p
);
1441 status
= garmin_init_session(port
);
1447 static void garmin_shutdown (struct usb_serial
*serial
)
1449 struct usb_serial_port
*port
= serial
->port
[0];
1450 struct garmin_data
* garmin_data_p
= usb_get_serial_port_data(port
);
1452 dbg("%s", __FUNCTION__
);
1454 usb_kill_urb (port
->interrupt_in_urb
);
1455 del_timer_sync(&garmin_data_p
->timer
);
1456 kfree (garmin_data_p
);
1457 usb_set_serial_port_data(port
, NULL
);
1461 /* All of the device info needed */
1462 static struct usb_serial_driver garmin_device
= {
1464 .owner
= THIS_MODULE
,
1465 .name
= "garmin_gps",
1467 .description
= "Garmin GPS usb/tty",
1468 .id_table
= id_table
,
1469 .num_interrupt_in
= 1,
1473 .open
= garmin_open
,
1474 .close
= garmin_close
,
1475 .throttle
= garmin_throttle
,
1476 .unthrottle
= garmin_unthrottle
,
1477 .attach
= garmin_attach
,
1478 .shutdown
= garmin_shutdown
,
1479 .write
= garmin_write
,
1480 .write_room
= garmin_write_room
,
1481 .chars_in_buffer
= garmin_chars_in_buffer
,
1482 .write_bulk_callback
= garmin_write_bulk_callback
,
1483 .read_bulk_callback
= garmin_read_bulk_callback
,
1484 .read_int_callback
= garmin_read_int_callback
,
1488 static int __init
garmin_init (void)
1492 retval
= usb_serial_register(&garmin_device
);
1494 goto failed_garmin_register
;
1495 retval
= usb_register(&garmin_driver
);
1497 goto failed_usb_register
;
1498 info(DRIVER_DESC
" " DRIVER_VERSION
);
1501 failed_usb_register
:
1502 usb_serial_deregister(&garmin_device
);
1503 failed_garmin_register
:
1508 static void __exit
garmin_exit (void)
1510 usb_deregister (&garmin_driver
);
1511 usb_serial_deregister (&garmin_device
);
1517 module_init(garmin_init
);
1518 module_exit(garmin_exit
);
1520 MODULE_AUTHOR( DRIVER_AUTHOR
);
1521 MODULE_DESCRIPTION( DRIVER_DESC
);
1522 MODULE_LICENSE("GPL");
1524 module_param(debug
, bool, S_IWUSR
| S_IRUGO
);
1525 MODULE_PARM_DESC(debug
, "Debug enabled or not");
1526 module_param(initial_mode
, int, S_IRUGO
);
1527 MODULE_PARM_DESC(initial_mode
, "Initial mode");