blob: 198a322286f999cb230c3f7ae22d398723f78a93 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Garmin GPS driver
3 *
4 * Copyright (C) 2004 Hermann Kneissel herkne@users.sourceforge.net
5 *
6 * The latest version of the driver can be found at
7 * http://sourceforge.net/projects/garmin-gps/
8 *
9 * This driver has been derived from v2.1 of the visor driver.
10 *
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.
15 *
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.
20 *
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
24 */
25
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>
39
40/* the mode to be set when the port ist opened */
41static int initial_mode = 1;
42
43/* debug flag */
44static int debug = 0;
45
46#include "usb-serial.h"
47
48#define GARMIN_VENDOR_ID 0x091E
49
50/*
51 * Version Information
52 */
53
54#define VERSION_MAJOR 0
55#define VERSION_MINOR 23
56
57#define _STR(s) #s
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"
62
63/* error codes returned by the driver */
64#define EINVPKT 1000 /* invalid packet structure */
65
66
67// size of the header of a packet using the usb protocol
68#define GARMIN_PKTHDR_LENGTH 12
69
70// max. possible size of a packet using the serial protocol
71#define MAX_SERIAL_PKT_SIZ (3+255+3)
72
73// max. possible size of a packet with worst case stuffing
74#define MAX_SERIAL_PKT_SIZ_STUFFED MAX_SERIAL_PKT_SIZ+256
75
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)
83
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)
86
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
89// packets data
90#define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
91
92// max. size of incoming private packets (header+1 param)
93#define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
94
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
99
100#define GARMIN_PKTID_PVT_DATA 51
101#define GARMIN_PKTID_L001_COMMAND_DATA 10
102
103#define CMND_ABORT_TRANSFER 0
104
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
112
113
114#define ETX 0x03
115#define DLE 0x10
116#define ACK 0x06
117#define NAK 0x15
118
119/* structure used to queue incoming packets */
120struct garmin_packet {
121 struct list_head list;
122 int seq;
123 int size; // the real size of the data array, always > 0
124 __u8 data[1];
125};
126
127/* structure used to keep the current state of the driver */
128struct garmin_data {
129 __u8 state;
130 __u16 flags;
131 __u8 mode;
132 __u8 ignorePkts;
133 __u8 count;
134 __u8 pkt_id;
135 __u32 serial_num;
136 struct timer_list timer;
137 struct usb_serial_port *port;
138 int seq_counter;
139 int insize;
140 int outsize;
141 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
142 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
143 __u8 privpkt[4*6];
144 spinlock_t lock;
145 struct list_head pktlist;
146};
147
148
149#define STATE_NEW 0
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
155
156#define STATE_RESET 8
157#define STATE_DISCONNECTED 9
158#define STATE_WAIT_TTY_ACK 10
159#define STATE_GSP_WAIT_DATA 11
160
161#define MODE_NATIVE 0
162#define MODE_GARMIN_SERIAL 1
163
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
171
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
176
177#define FLAGS_GSP_SKIP 0x1000
178#define FLAGS_GSP_DLESEEN 0x2000
179
180
181
182
183
184
185/* function prototypes */
186static void gsp_next_packet(struct garmin_data * garmin_data_p);
187static int garmin_write_bulk(struct usb_serial_port *port,
188 const unsigned char *buf, int count);
189
190/* some special packets to be send or received */
191static unsigned char const GARMIN_START_SESSION_REQ[]
192 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
193static unsigned char const GARMIN_START_SESSION_REQ2[]
194 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
195static unsigned char const GARMIN_START_SESSION_REPLY[]
196 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
197static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
198 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
199static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
200 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
201static unsigned char const GARMIN_APP_LAYER_REPLY[]
202 = { 0x14, 0, 0, 0 };
203static unsigned char const GARMIN_START_PVT_REQ[]
204 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
205static unsigned char const GARMIN_STOP_PVT_REQ[]
206 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
207static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
208 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
209static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
210 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
211static unsigned char const PRIVATE_REQ[]
212 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
213
214
215
216static 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 */
220};
221
222MODULE_DEVICE_TABLE (usb, id_table);
223
224static struct usb_driver garmin_driver = {
225 .owner = THIS_MODULE,
226 .name = "garmin_gps",
227 .probe = usb_serial_probe,
228 .disconnect = usb_serial_disconnect,
229 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800230 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
233
234static inline int noResponseFromAppLayer(struct garmin_data * garmin_data_p)
235{
236 return ((garmin_data_p->flags
237 & (FLAGS_APP_REQ_SEEN|FLAGS_APP_RESP_SEEN))
238 == FLAGS_APP_REQ_SEEN);
239}
240
241
242static inline int getLayerId(const __u8 *usbPacket)
243{
244 return __le32_to_cpup((__le32 *)(usbPacket));
245}
246
247static inline int getPacketId(const __u8 *usbPacket)
248{
249 return __le32_to_cpup((__le32 *)(usbPacket+4));
250}
251
252static inline int getDataLength(const __u8 *usbPacket)
253{
254 return __le32_to_cpup((__le32 *)(usbPacket+8));
255}
256
257
258/*
259 * check if the usb-packet in buf contains an abort-transfer command.
260 * (if yes, all queued data will be dropped)
261 */
262static inline int isAbortTrfCmnd(const unsigned char *buf)
263{
264 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
265 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
266 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
267 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
268 return 1;
269 else
270 return 0;
271}
272
273
274
275static void send_to_tty(struct usb_serial_port *port,
276 char *data, unsigned int actual_length)
277{
278 struct tty_struct *tty = port->tty;
279 int i;
280
281 if (tty && actual_length) {
282
283 usb_serial_debug_data(debug, &port->dev,
284 __FUNCTION__, actual_length, data);
285
286 for (i = 0; i < actual_length ; ++i) {
287 /* if we insert more than TTY_FLIPBUF_SIZE characters,
288 we drop them. */
289 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
290 tty_flip_buffer_push(tty);
291 }
292 /* this doesn't actually push the data through unless
293 tty->low_latency is set */
294 tty_insert_flip_char(tty, data[i], 0);
295 }
296 tty_flip_buffer_push(tty);
297 }
298}
299
300
301/******************************************************************************
302 * packet queue handling
303 ******************************************************************************/
304
305/*
306 * queue a received (usb-)packet for later processing
307 */
308static int pkt_add(struct garmin_data * garmin_data_p,
309 unsigned char *data, unsigned int data_length)
310{
311 int result = 0;
312 unsigned long flags;
313 struct garmin_packet *pkt;
314
315 /* process only packets containg data ... */
316 if (data_length) {
317 garmin_data_p->flags |= FLAGS_QUEUING;
318 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
319 GFP_ATOMIC);
320 if (pkt == NULL) {
321 dev_err(&garmin_data_p->port->dev, "out of memory\n");
322 return 0;
323 }
324 pkt->size = data_length;
325 memcpy(pkt->data, data, data_length);
326
327 spin_lock_irqsave(&garmin_data_p->lock, flags);
328 result = list_empty(&garmin_data_p->pktlist);
329 pkt->seq = garmin_data_p->seq_counter++;
330 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
331 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
332
333 /* in serial mode, if someone is waiting for data from
334 the device, iconvert and send the next packet to tty. */
335 if (result && (garmin_data_p->state == STATE_GSP_WAIT_DATA)) {
336 gsp_next_packet(garmin_data_p);
337 }
338 }
339 return result;
340}
341
342
343/* get the next pending packet */
344static struct garmin_packet *pkt_pop(struct garmin_data * garmin_data_p)
345{
346 unsigned long flags;
347 struct garmin_packet *result = NULL;
348
349 spin_lock_irqsave(&garmin_data_p->lock, flags);
350 if (!list_empty(&garmin_data_p->pktlist)) {
351 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
352 list_del(&result->list);
353 }
354 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
355 return result;
356}
357
358
359/* free up all queued data */
360static void pkt_clear(struct garmin_data * garmin_data_p)
361{
362 unsigned long flags;
363 struct garmin_packet *result = NULL;
364
365 dbg("%s", __FUNCTION__);
366
367 spin_lock_irqsave(&garmin_data_p->lock, flags);
368 while (!list_empty(&garmin_data_p->pktlist)) {
369 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
370 list_del(&result->list);
371 kfree(result);
372 }
373 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
374}
375
376
377/******************************************************************************
378 * garmin serial protocol handling handling
379 ******************************************************************************/
380
381/* send an ack packet back to the tty */
382static int gsp_send_ack(struct garmin_data * garmin_data_p, __u8 pkt_id)
383{
384 __u8 pkt[10];
385 __u8 cksum = 0;
386 __u8 *ptr = pkt;
387 unsigned l = 0;
388
389 dbg("%s - pkt-id: 0x%X.", __FUNCTION__, 0xFF & pkt_id);
390
391 *ptr++ = DLE;
392 *ptr++ = ACK;
393 cksum += ACK;
394
395 *ptr++ = 2;
396 cksum += 2;
397
398 *ptr++ = pkt_id;
399 cksum += pkt_id;
400
401 if (pkt_id == DLE) {
402 *ptr++ = DLE;
403 }
404
405 *ptr++ = 0;
406 *ptr++ = 0xFF & (-cksum);
407 *ptr++ = DLE;
408 *ptr++ = ETX;
409
410 l = ptr-pkt;
411
412 send_to_tty(garmin_data_p->port, pkt, l);
413 return 0;
414}
415
416
417
418/*
419 * called for a complete packet received from tty layer
420 *
421 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
422 * at GSP_INITIAL_OFFSET.
423 *
424 * count - number of bytes in the input buffer including space reserved for
425 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
426 * (including pkt-id, data-length a. cksum)
427 */
428static int gsp_rec_packet(struct garmin_data * garmin_data_p, int count)
429{
430 const __u8* recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
431 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
432
433 int cksum = 0;
434 int n = 0;
435 int pktid = recpkt[0];
436 int size = recpkt[1];
437
438 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
439 __FUNCTION__, count-GSP_INITIAL_OFFSET, recpkt);
440
441 if (size != (count-GSP_INITIAL_OFFSET-3)) {
442 dbg("%s - invalid size, expected %d bytes, got %d",
443 __FUNCTION__, size, (count-GSP_INITIAL_OFFSET-3));
444 return -EINVPKT;
445 }
446
447 cksum += *recpkt++;
448 cksum += *recpkt++;
449
450 // sanity check, remove after test ...
451 if ((__u8*)&(usbdata[3]) != recpkt) {
452 dbg("%s - ptr mismatch %p - %p",
453 __FUNCTION__, &(usbdata[4]), recpkt);
454 return -EINVPKT;
455 }
456
457 while (n < size) {
458 cksum += *recpkt++;
459 n++;
460 }
461
462 if ((0xff & (cksum + *recpkt)) != 0) {
463 dbg("%s - invalid checksum, expected %02x, got %02x",
464 __FUNCTION__, 0xff & -cksum, 0xff & *recpkt);
465 return -EINVPKT;
466 }
467
468 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
469 usbdata[1] = __cpu_to_le32(pktid);
470 usbdata[2] = __cpu_to_le32(size);
471
472 garmin_write_bulk (garmin_data_p->port, garmin_data_p->inbuffer,
473 GARMIN_PKTHDR_LENGTH+size);
474
475 /* if this was an abort-transfer command, flush all
476 queued data. */
477 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
478 garmin_data_p->flags |= FLAGS_DROP_DATA;
479 pkt_clear(garmin_data_p);
480 }
481
482 return count;
483}
484
485
486
487/*
488 * Called for data received from tty
489 *
490 * buf contains the data read, it may span more than one packet or even
491 * incomplete packets
492 *
493 * input record should be a serial-record, but it may not be complete.
494 * Copy it into our local buffer, until an etx is seen (or an error
495 * occurs).
496 * Once the record is complete, convert into a usb packet and send it
497 * to the bulk pipe, send an ack back to the tty.
498 *
499 * If the input is an ack, just send the last queued packet to the
500 * tty layer.
501 *
502 * if the input is an abort command, drop all queued data.
503 */
504
505static int gsp_receive(struct garmin_data * garmin_data_p,
506 const unsigned char *buf, int count)
507{
508 int offs = 0;
509 int ack_or_nak_seen = 0;
510 int i = 0;
511 __u8 *dest = garmin_data_p->inbuffer;
512 int size = garmin_data_p->insize;
513 // dleSeen: set if last byte read was a DLE
514 int dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
515 // skip: if set, skip incoming data until possible start of
516 // new packet
517 int skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
518 __u8 data;
519
520 dbg("%s - dle=%d skip=%d size=%d count=%d",
521 __FUNCTION__, dleSeen, skip, size, count);
522
523 if (size == 0) {
524 size = GSP_INITIAL_OFFSET;
525 }
526
527 while (offs < count) {
528
529 data = *(buf+offs);
530 offs ++;
531
532 if (data == DLE) {
533 if (skip) { /* start of a new pkt */
534 skip = 0;
535 size = GSP_INITIAL_OFFSET;
536 dleSeen = 1;
537 } else if (dleSeen) {
538 dest[size++] = data;
539 dleSeen = 0;
540 } else {
541 dleSeen = 1;
542 }
543 } else if (data == ETX) {
544 if (dleSeen) {
545 /* packet complete */
546
547 data = dest[GSP_INITIAL_OFFSET];
548
549 if (data == ACK) {
550 ack_or_nak_seen = ACK;
551 dbg("ACK packet complete.");
552 } else if (data == NAK) {
553 ack_or_nak_seen = NAK;
554 dbg("NAK packet complete.");
555 } else {
556 dbg("packet complete "
557 "- id=0x%X.",
558 0xFF & data);
559 gsp_rec_packet(garmin_data_p, size);
560 }
561
562 skip = 1;
563 size = GSP_INITIAL_OFFSET;
564 dleSeen = 0;
565 } else {
566 dest[size++] = data;
567 }
568 } else if (!skip) {
569
570 if (dleSeen) {
571 dbg("non-masked DLE at %d - restarting", i);
572 size = GSP_INITIAL_OFFSET;
573 dleSeen = 0;
574 }
575
576 dest[size++] = data;
577 }
578
579 if (size >= GPS_IN_BUFSIZ) {
580 dbg("%s - packet too large.", __FUNCTION__);
581 skip = 1;
582 size = GSP_INITIAL_OFFSET;
583 dleSeen = 0;
584 }
585 }
586
587 garmin_data_p->insize = size;
588
589 // copy flags back to structure
590 if (skip)
591 garmin_data_p->flags |= FLAGS_GSP_SKIP;
592 else
593 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
594
595 if (dleSeen)
596 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
597 else
598 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
599
600 if (ack_or_nak_seen) {
601 garmin_data_p->state = STATE_GSP_WAIT_DATA;
602 gsp_next_packet(garmin_data_p);
603 }
604
605 return count;
606}
607
608
609
610
611/*
612 * Sends a usb packet to the tty
613 *
614 * Assumes, that all packages and at an usb-packet boundary.
615 *
616 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
617 */
618static int gsp_send(struct garmin_data * garmin_data_p,
619 const unsigned char *buf, int count)
620{
621 const unsigned char *src;
622 unsigned char *dst;
623 int pktid = 0;
624 int datalen = 0;
625 int cksum = 0;
626 int i=0;
627 int k;
628
629 dbg("%s - state %d - %d bytes.", __FUNCTION__,
630 garmin_data_p->state, count);
631
632 k = garmin_data_p->outsize;
633 if ((k+count) > GPS_OUT_BUFSIZ) {
634 dbg("packet too large");
635 garmin_data_p->outsize = 0;
636 return -4;
637 }
638
639 memcpy(garmin_data_p->outbuffer+k, buf, count);
640 k += count;
641 garmin_data_p->outsize = k;
642
643 if (k >= GARMIN_PKTHDR_LENGTH) {
644 pktid = getPacketId(garmin_data_p->outbuffer);
645 datalen= getDataLength(garmin_data_p->outbuffer);
646 i = GARMIN_PKTHDR_LENGTH + datalen;
647 if (k < i)
648 return 0;
649 } else {
650 return 0;
651 }
652
653 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __FUNCTION__,
654 k, i);
655
656 /* garmin_data_p->outbuffer now contains a complete packet */
657
658 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
659 __FUNCTION__, k, garmin_data_p->outbuffer);
660
661 garmin_data_p->outsize = 0;
662
663 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
664 dbg("not an application packet (%d)",
665 getLayerId(garmin_data_p->outbuffer));
666 return -1;
667 }
668
669 if (pktid > 255) {
670 dbg("packet-id %d too large", pktid);
671 return -2;
672 }
673
674 if (datalen > 255) {
675 dbg("packet-size %d too large", datalen);
676 return -3;
677 }
678
679 /* the serial protocol should be able to handle this packet */
680
681 k = 0;
682 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
683 for (i=0; i<datalen; i++) {
684 if (*src++ == DLE)
685 k++;
686 }
687
688 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
689 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
690 /* can't add stuffing DLEs in place, move data to end
691 of buffer ... */
692 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
693 memcpy(dst, src, datalen);
694 src = dst;
695 }
696
697 dst = garmin_data_p->outbuffer;
698
699 *dst++ = DLE;
700 *dst++ = pktid;
701 cksum += pktid;
702 *dst++ = datalen;
703 cksum += datalen;
704 if (datalen == DLE)
705 *dst++ = DLE;
706
707 for (i=0; i<datalen; i++) {
708 __u8 c = *src++;
709 *dst++ = c;
710 cksum += c;
711 if (c == DLE)
712 *dst++ = DLE;
713 }
714
715 cksum = 0xFF & -cksum;
716 *dst++ = cksum;
717 if (cksum == DLE)
718 *dst++ = DLE;
719 *dst++ = DLE;
720 *dst++ = ETX;
721
722 i = dst-garmin_data_p->outbuffer;
723
724 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
725
726 garmin_data_p->pkt_id = pktid;
727 garmin_data_p->state = STATE_WAIT_TTY_ACK;
728
729 return i;
730}
731
732
733
734
735
736/*
737 * Process the next pending data packet - if there is one
738 */
739static void gsp_next_packet(struct garmin_data * garmin_data_p)
740{
741 struct garmin_packet *pkt = NULL;
742
743 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
744 dbg("%s - next pkt: %d", __FUNCTION__, pkt->seq);
745 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
746 kfree(pkt);
747 return;
748 }
749 kfree(pkt);
750 }
751}
752
753
754
755
756/******************************************************************************
757 * garmin native mode
758 ******************************************************************************/
759
760
761/*
762 * Called for data received from tty
763 *
764 * The input data is expected to be in garmin usb-packet format.
765 *
766 * buf contains the data read, it may span more than one packet
767 * or even incomplete packets
768 */
769static int nat_receive(struct garmin_data * garmin_data_p,
770 const unsigned char *buf, int count)
771{
772 __u8 * dest;
773 int offs = 0;
774 int result = count;
775 int len;
776
777 while (offs < count) {
778 // if buffer contains header, copy rest of data
779 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
780 len = GARMIN_PKTHDR_LENGTH
781 +getDataLength(garmin_data_p->inbuffer);
782 else
783 len = GARMIN_PKTHDR_LENGTH;
784
785 if (len >= GPS_IN_BUFSIZ) {
786 /* seem to be an invalid packet, ignore rest of input */
787 dbg("%s - packet size too large: %d",
788 __FUNCTION__, len);
789 garmin_data_p->insize = 0;
790 count = 0;
791 result = -EINVPKT;
792 } else {
793 len -= garmin_data_p->insize;
794 if (len > (count-offs))
795 len = (count-offs);
796 if (len > 0) {
797 dest = garmin_data_p->inbuffer
798 +garmin_data_p->insize;
799 memcpy(dest, buf+offs, len);
800 garmin_data_p->insize += len;
801 offs += len;
802 }
803 }
804
805 /* do we have a complete packet ? */
806 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
807 len = GARMIN_PKTHDR_LENGTH+
808 getDataLength(garmin_data_p->inbuffer);
809 if (garmin_data_p->insize >= len) {
810 garmin_write_bulk (garmin_data_p->port,
811 garmin_data_p->inbuffer,
812 len);
813 garmin_data_p->insize = 0;
814
815 /* if this was an abort-transfer command,
816 flush all queued data. */
817 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
818 garmin_data_p->flags |= FLAGS_DROP_DATA;
819 pkt_clear(garmin_data_p);
820 }
821 }
822 }
823 }
824 return result;
825}
826
827
828/******************************************************************************
829 * private packets
830 ******************************************************************************/
831
832static void priv_status_resp(struct usb_serial_port *port)
833{
834 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
835 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
836
837 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
838 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
839 pkt[2] = __cpu_to_le32(12);
840 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
841 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
842 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
843
844 send_to_tty(port, (__u8*)pkt, 6*4);
845}
846
847
848/******************************************************************************
849 * Garmin specific driver functions
850 ******************************************************************************/
851
852static int process_resetdev_request(struct usb_serial_port *port)
853{
854 int status;
855 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
856
857 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
858 garmin_data_p->state = STATE_RESET;
859 garmin_data_p->serial_num = 0;
860
861 usb_kill_urb (port->interrupt_in_urb);
862 dbg("%s - usb_reset_device", __FUNCTION__ );
863 status = usb_reset_device(port->serial->dev);
864 if (status)
865 dbg("%s - usb_reset_device failed: %d",
866 __FUNCTION__, status);
867 return status;
868}
869
870
871
872/*
873 * clear all cached data
874 */
875static int garmin_clear(struct garmin_data * garmin_data_p)
876{
877 int status = 0;
878
879 struct usb_serial_port *port = garmin_data_p->port;
880
881 if (port != NULL && garmin_data_p->flags & FLAGS_APP_RESP_SEEN) {
882 /* send a terminate command */
883 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
884 sizeof(GARMIN_STOP_TRANSFER_REQ));
885 }
886
887 /* flush all queued data */
888 pkt_clear(garmin_data_p);
889
890 garmin_data_p->insize = 0;
891 garmin_data_p->outsize = 0;
892
893 return status;
894}
895
896
897
898
899
900
901static int garmin_init_session(struct usb_serial_port *port)
902{
903 struct usb_serial *serial = port->serial;
904 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
905 int status = 0;
906
907 if (status == 0) {
908 usb_kill_urb (port->interrupt_in_urb);
909
910 dbg("%s - adding interrupt input", __FUNCTION__);
911 port->interrupt_in_urb->dev = serial->dev;
912 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
913 if (status)
914 dev_err(&serial->dev->dev,
915 "%s - failed submitting interrupt urb,"
916 " error %d\n",
917 __FUNCTION__, status);
918 }
919
920 if (status == 0) {
921 dbg("%s - starting session ...", __FUNCTION__);
922 garmin_data_p->state = STATE_ACTIVE;
923 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
924 sizeof(GARMIN_START_SESSION_REQ));
925
926 if (status >= 0) {
927
928 garmin_data_p->ignorePkts++;
929
930 /* not needed, but the win32 driver does it too ... */
931 status = garmin_write_bulk(port,
932 GARMIN_START_SESSION_REQ2,
933 sizeof(GARMIN_START_SESSION_REQ2));
934 if (status >= 0) {
935 status = 0;
936 garmin_data_p->ignorePkts++;
937 }
938 }
939 }
940
941 return status;
942}
943
944
945
946
947
948static int garmin_open (struct usb_serial_port *port, struct file *filp)
949{
950 int status = 0;
951 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
952
953 dbg("%s - port %d", __FUNCTION__, port->number);
954
955 /*
956 * Force low_latency on so that our tty_push actually forces the data
957 * through, otherwise it is scheduled, and with high data rates (like
958 * with OHCI) data can get lost.
959 */
960 if (port->tty)
961 port->tty->low_latency = 1;
962
963 garmin_data_p->mode = initial_mode;
964 garmin_data_p->count = 0;
965 garmin_data_p->flags = 0;
966
967 /* shutdown any bulk reads that might be going on */
968 usb_kill_urb (port->write_urb);
969 usb_kill_urb (port->read_urb);
970
971 if (garmin_data_p->state == STATE_RESET) {
972 status = garmin_init_session(port);
973 }
974
975 garmin_data_p->state = STATE_ACTIVE;
976
977 return status;
978}
979
980
981static void garmin_close (struct usb_serial_port *port, struct file * filp)
982{
983 struct usb_serial *serial = port->serial;
984 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
985
986 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __FUNCTION__,
987 port->number, garmin_data_p->mode,
988 garmin_data_p->state, garmin_data_p->flags);
989
990 if (!serial)
991 return;
992
993 garmin_clear(garmin_data_p);
994
995 /* shutdown our urbs */
996 usb_kill_urb (port->read_urb);
997 usb_kill_urb (port->write_urb);
998
999 if (noResponseFromAppLayer(garmin_data_p) ||
1000 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1001 process_resetdev_request(port);
1002 garmin_data_p->state = STATE_RESET;
1003 } else {
1004 garmin_data_p->state = STATE_DISCONNECTED;
1005 }
1006}
1007
1008
1009static void garmin_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
1010{
1011 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1012 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1013
1014 /* free up the transfer buffer, as usb_free_urb() does not do this */
1015 kfree (urb->transfer_buffer);
1016
1017 dbg("%s - port %d", __FUNCTION__, port->number);
1018
1019 if (urb->status) {
1020 dbg("%s - nonzero write bulk status received: %d",
1021 __FUNCTION__, urb->status);
1022 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1023 }
1024
1025 schedule_work(&port->work);
1026}
1027
1028
1029static int garmin_write_bulk (struct usb_serial_port *port,
1030 const unsigned char *buf, int count)
1031{
1032 struct usb_serial *serial = port->serial;
1033 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1034 struct urb *urb;
1035 unsigned char *buffer;
1036 int status;
1037
1038 dbg("%s - port %d, state %d", __FUNCTION__, port->number,
1039 garmin_data_p->state);
1040
1041 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1042
1043 buffer = kmalloc (count, GFP_ATOMIC);
1044 if (!buffer) {
1045 dev_err(&port->dev, "out of memory\n");
1046 return -ENOMEM;
1047 }
1048
1049 urb = usb_alloc_urb(0, GFP_ATOMIC);
1050 if (!urb) {
1051 dev_err(&port->dev, "no more free urbs\n");
1052 kfree (buffer);
1053 return -ENOMEM;
1054 }
1055
1056 memcpy (buffer, buf, count);
1057
1058 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
1059
1060 usb_fill_bulk_urb (urb, serial->dev,
1061 usb_sndbulkpipe (serial->dev,
1062 port->bulk_out_endpointAddress),
1063 buffer, count,
1064 garmin_write_bulk_callback, port);
1065 urb->transfer_flags |= URB_ZERO_PACKET;
1066
1067 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1068 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1069 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1070 pkt_clear(garmin_data_p);
1071 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1072 }
1073 }
1074
1075 /* send it down the pipe */
1076 status = usb_submit_urb(urb, GFP_ATOMIC);
1077 if (status) {
1078 dev_err(&port->dev,
1079 "%s - usb_submit_urb(write bulk) "
1080 "failed with status = %d\n",
1081 __FUNCTION__, status);
1082 count = status;
1083 } else {
1084
1085 if (GARMIN_LAYERID_APPL == getLayerId(buffer)
1086 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
1087
1088 gsp_send_ack(garmin_data_p, buffer[4]);
1089 }
1090 }
1091
1092 /* we are done with this urb, so let the host driver
1093 * really free it when it is finished with it */
1094 usb_free_urb (urb);
1095
1096 return count;
1097}
1098
1099
1100
1101static int garmin_write (struct usb_serial_port *port,
1102 const unsigned char *buf, int count)
1103{
1104 int pktid, pktsiz, len;
1105 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1106 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1107
1108 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buf);
1109
1110 /* check for our private packets */
1111 if (count >= GARMIN_PKTHDR_LENGTH) {
1112
1113 len = PRIVPKTSIZ;
1114 if (count < len)
1115 len = count;
1116
1117 memcpy(garmin_data_p->privpkt, buf, len);
1118
1119 pktsiz = getDataLength(garmin_data_p->privpkt);
1120 pktid = getPacketId(garmin_data_p->privpkt);
1121
1122 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1123 && GARMIN_LAYERID_PRIVATE == getLayerId(garmin_data_p->privpkt)) {
1124
1125 dbg("%s - processing private request %d",
1126 __FUNCTION__, pktid);
1127
1128 // drop all unfinished transfers
1129 garmin_clear(garmin_data_p);
1130
1131 switch(pktid) {
1132
1133 case PRIV_PKTID_SET_DEBUG:
1134 if (pktsiz != 4)
1135 return -EINVPKT;
1136 debug = __le32_to_cpu(privpkt[3]);
1137 dbg("%s - debug level set to 0x%X",
1138 __FUNCTION__, debug);
1139 break;
1140
1141 case PRIV_PKTID_SET_MODE:
1142 if (pktsiz != 4)
1143 return -EINVPKT;
1144 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1145 dbg("%s - mode set to %d",
1146 __FUNCTION__, garmin_data_p->mode);
1147 break;
1148
1149 case PRIV_PKTID_INFO_REQ:
1150 priv_status_resp(port);
1151 break;
1152
1153 case PRIV_PKTID_RESET_REQ:
1154 garmin_data_p->flags |= FLAGS_APP_REQ_SEEN;
1155 break;
1156
1157 case PRIV_PKTID_SET_DEF_MODE:
1158 if (pktsiz != 4)
1159 return -EINVPKT;
1160 initial_mode = __le32_to_cpu(privpkt[3]);
1161 dbg("%s - initial_mode set to %d",
1162 __FUNCTION__,
1163 garmin_data_p->mode);
1164 break;
1165 }
1166 return count;
1167 }
1168 }
1169
1170 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1171 return gsp_receive(garmin_data_p, buf, count);
1172 } else { /* MODE_NATIVE */
1173 return nat_receive(garmin_data_p, buf, count);
1174 }
1175}
1176
1177
1178static int garmin_write_room (struct usb_serial_port *port)
1179{
1180 /*
1181 * Report back the bytes currently available in the output buffer.
1182 */
1183 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1184 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1185}
1186
1187
1188static int garmin_chars_in_buffer (struct usb_serial_port *port)
1189{
1190 /*
1191 * Report back the number of bytes currently in our input buffer.
1192 * Will this lock up the driver - the buffer contains an incomplete
1193 * package which will not be written to the device until it
1194 * has been completed ?
1195 */
1196 //struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1197 //return garmin_data_p->insize;
1198 return 0;
1199}
1200
1201
1202static void garmin_read_process(struct garmin_data * garmin_data_p,
1203 unsigned char *data, unsigned data_length)
1204{
1205 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1206 /* abort-transfer cmd is actice */
1207 dbg("%s - pkt dropped", __FUNCTION__);
1208 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1209 garmin_data_p->state != STATE_RESET ) {
1210
1211 /* remember any appl.layer packets, so we know
1212 if a reset is required or not when closing
1213 the device */
1214 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
1215 sizeof(GARMIN_APP_LAYER_REPLY)))
1216 garmin_data_p->flags |= FLAGS_APP_RESP_SEEN;
1217
1218 /* if throttling is active or postprecessing is required
1219 put the received data in th input queue, otherwise
1220 send it directly to the tty port */
1221 if (garmin_data_p->flags & FLAGS_QUEUING) {
1222 pkt_add(garmin_data_p, data, data_length);
1223 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1224 if (getLayerId(data) == GARMIN_LAYERID_APPL) {
1225 pkt_add(garmin_data_p, data, data_length);
1226 }
1227 } else {
1228 send_to_tty(garmin_data_p->port, data, data_length);
1229 }
1230 }
1231}
1232
1233
1234static void garmin_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
1235{
1236 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1237 struct usb_serial *serial = port->serial;
1238 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1239 unsigned char *data = urb->transfer_buffer;
1240 int status;
1241
1242 dbg("%s - port %d", __FUNCTION__, port->number);
1243
1244 if (!serial) {
1245 dbg("%s - bad serial pointer, exiting", __FUNCTION__);
1246 return;
1247 }
1248
1249 if (urb->status) {
1250 dbg("%s - nonzero read bulk status received: %d",
1251 __FUNCTION__, urb->status);
1252 return;
1253 }
1254
1255 usb_serial_debug_data(debug, &port->dev,
1256 __FUNCTION__, urb->actual_length, data);
1257
1258 garmin_read_process(garmin_data_p, data, urb->actual_length);
1259
1260 /* Continue trying to read until nothing more is received */
1261 if (urb->actual_length > 0) {
1262 usb_fill_bulk_urb (port->read_urb, serial->dev,
1263 usb_rcvbulkpipe (serial->dev,
1264 port->bulk_in_endpointAddress),
1265 port->read_urb->transfer_buffer,
1266 port->read_urb->transfer_buffer_length,
1267 garmin_read_bulk_callback, port);
1268 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1269 if (status)
1270 dev_err(&port->dev,
1271 "%s - failed resubmitting read urb, error %d\n",
1272 __FUNCTION__, status);
1273 }
1274 return;
1275}
1276
1277
1278static void garmin_read_int_callback (struct urb *urb, struct pt_regs *regs)
1279{
1280 int status;
1281 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
1282 struct usb_serial *serial = port->serial;
1283 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1284 unsigned char *data = urb->transfer_buffer;
1285
1286 switch (urb->status) {
1287 case 0:
1288 /* success */
1289 break;
1290 case -ECONNRESET:
1291 case -ENOENT:
1292 case -ESHUTDOWN:
1293 /* this urb is terminated, clean up */
1294 dbg("%s - urb shutting down with status: %d",
1295 __FUNCTION__, urb->status);
1296 return;
1297 default:
1298 dbg("%s - nonzero urb status received: %d",
1299 __FUNCTION__, urb->status);
1300 return;
1301 }
1302
1303 usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
1304 urb->actual_length, urb->transfer_buffer);
1305
1306 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1307 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1308 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1309
1310 dbg("%s - bulk data available.", __FUNCTION__);
1311
1312 /* bulk data available */
1313 usb_fill_bulk_urb (port->read_urb, serial->dev,
1314 usb_rcvbulkpipe (serial->dev,
1315 port->bulk_in_endpointAddress),
1316 port->read_urb->transfer_buffer,
1317 port->read_urb->transfer_buffer_length,
1318 garmin_read_bulk_callback, port);
1319 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
1320 if (status) {
1321 dev_err(&port->dev,
1322 "%s - failed submitting read urb, error %d\n",
1323 __FUNCTION__, status);
1324 }
1325
1326 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1327 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1328 sizeof(GARMIN_START_SESSION_REPLY))) {
1329
1330 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1331
1332 /* save the serial number */
1333 garmin_data_p->serial_num
1334 = __le32_to_cpup((__le32*)(data+GARMIN_PKTHDR_LENGTH));
1335
1336 dbg("%s - start-of-session reply seen - serial %u.",
1337 __FUNCTION__, garmin_data_p->serial_num);
1338 }
1339
1340 if (garmin_data_p->ignorePkts) {
1341 /* this reply belongs to a request generated by the driver,
1342 ignore it. */
1343 dbg("%s - pkt ignored (%d)",
1344 __FUNCTION__, garmin_data_p->ignorePkts);
1345 garmin_data_p->ignorePkts--;
1346 } else {
1347 garmin_read_process(garmin_data_p, data, urb->actual_length);
1348 }
1349
1350 port->interrupt_in_urb->dev = port->serial->dev;
1351 status = usb_submit_urb (urb, GFP_ATOMIC);
1352 if (status)
1353 dev_err(&urb->dev->dev,
1354 "%s - Error %d submitting interrupt urb\n",
1355 __FUNCTION__, status);
1356}
1357
1358
1359/*
1360 * Sends the next queued packt to the tty port (garmin native mode only)
1361 * and then sets a timer to call itself again until all queued data
1362 * is sent.
1363 */
1364static int garmin_flush_queue(struct garmin_data * garmin_data_p)
1365{
1366 struct garmin_packet *pkt;
1367
1368 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1369 pkt = pkt_pop(garmin_data_p);
1370 if (pkt != NULL) {
1371
1372 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1373 kfree(pkt);
1374 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1375
1376 } else {
1377 garmin_data_p->flags &= ~FLAGS_QUEUING;
1378 }
1379 }
1380 return 0;
1381}
1382
1383
1384static void garmin_throttle (struct usb_serial_port *port)
1385{
1386 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1387
1388 dbg("%s - port %d", __FUNCTION__, port->number);
1389 /* set flag, data received will be put into a queue
1390 for later processing */
1391 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1392}
1393
1394
1395static void garmin_unthrottle (struct usb_serial_port *port)
1396{
1397 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1398
1399 dbg("%s - port %d", __FUNCTION__, port->number);
1400 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1401
1402 /* in native mode send queued data to tty, in
1403 serial mode nothing needs to be done here */
1404 if (garmin_data_p->mode == MODE_NATIVE)
1405 garmin_flush_queue(garmin_data_p);
1406}
1407
1408
1409
1410/*
1411 * The timer is currently only used to send queued packets to
1412 * the tty in cases where the protocol provides no own handshaking
1413 * to initiate the transfer.
1414 */
1415static void timeout_handler(unsigned long data)
1416{
1417 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1418
1419 /* send the next queued packet to the tty port */
1420 if (garmin_data_p->mode == MODE_NATIVE)
1421 if (garmin_data_p->flags & FLAGS_QUEUING)
1422 garmin_flush_queue(garmin_data_p);
1423}
1424
1425
1426
1427static int garmin_attach (struct usb_serial *serial)
1428{
1429 int status = 0;
1430 struct usb_serial_port *port = serial->port[0];
1431 struct garmin_data * garmin_data_p = NULL;
1432
1433 dbg("%s", __FUNCTION__);
1434
1435 garmin_data_p = kmalloc (sizeof(struct garmin_data), GFP_KERNEL);
1436 if (garmin_data_p == NULL) {
1437 dev_err(&port->dev, "%s - Out of memory\n", __FUNCTION__);
1438 return -ENOMEM;
1439 }
1440 memset (garmin_data_p, 0, sizeof(struct garmin_data));
1441 init_timer(&garmin_data_p->timer);
1442 spin_lock_init(&garmin_data_p->lock);
1443 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1444 //garmin_data_p->timer.expires = jiffies + session_timeout;
1445 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1446 garmin_data_p->timer.function = timeout_handler;
1447 garmin_data_p->port = port;
1448 garmin_data_p->state = 0;
1449 garmin_data_p->count = 0;
1450 usb_set_serial_port_data(port, garmin_data_p);
1451
1452 status = garmin_init_session(port);
1453
1454 return status;
1455}
1456
1457
1458static void garmin_shutdown (struct usb_serial *serial)
1459{
1460 struct usb_serial_port *port = serial->port[0];
1461 struct garmin_data * garmin_data_p = usb_get_serial_port_data(port);
1462
1463 dbg("%s", __FUNCTION__);
1464
1465 usb_kill_urb (port->interrupt_in_urb);
1466 del_timer_sync(&garmin_data_p->timer);
1467 kfree (garmin_data_p);
1468 usb_set_serial_port_data(port, NULL);
1469}
1470
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001473static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001474 .driver = {
1475 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001476 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001477 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -07001478 .description = "Garmin GPS usb/tty",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 .id_table = id_table,
1480 .num_interrupt_in = 1,
1481 .num_bulk_in = 1,
1482 .num_bulk_out = 1,
1483 .num_ports = 1,
1484 .open = garmin_open,
1485 .close = garmin_close,
1486 .throttle = garmin_throttle,
1487 .unthrottle = garmin_unthrottle,
1488 .attach = garmin_attach,
1489 .shutdown = garmin_shutdown,
1490 .write = garmin_write,
1491 .write_room = garmin_write_room,
1492 .chars_in_buffer = garmin_chars_in_buffer,
1493 .write_bulk_callback = garmin_write_bulk_callback,
1494 .read_bulk_callback = garmin_read_bulk_callback,
1495 .read_int_callback = garmin_read_int_callback,
1496};
1497
1498
1499static int __init garmin_init (void)
1500{
1501 int retval;
1502
1503 retval = usb_serial_register(&garmin_device);
1504 if (retval)
1505 goto failed_garmin_register;
1506 retval = usb_register(&garmin_driver);
1507 if (retval)
1508 goto failed_usb_register;
1509 info(DRIVER_DESC " " DRIVER_VERSION);
1510
1511 return 0;
1512failed_usb_register:
1513 usb_serial_deregister(&garmin_device);
1514failed_garmin_register:
1515 return retval;
1516}
1517
1518
1519static void __exit garmin_exit (void)
1520{
1521 usb_deregister (&garmin_driver);
1522 usb_serial_deregister (&garmin_device);
1523}
1524
1525
1526
1527
1528module_init(garmin_init);
1529module_exit(garmin_exit);
1530
1531MODULE_AUTHOR( DRIVER_AUTHOR );
1532MODULE_DESCRIPTION( DRIVER_DESC );
1533MODULE_LICENSE("GPL");
1534
1535module_param(debug, bool, S_IWUSR | S_IRUGO);
1536MODULE_PARM_DESC(debug, "Debug enabled or not");
1537module_param(initial_mode, int, S_IRUGO);
1538MODULE_PARM_DESC(initial_mode, "Initial mode");
1539