tty: riscom8 kref and tty_port_close
[linux-2.6/mini2440.git] / drivers / usb / serial / garmin_gps.c
blob8839f1c70b7fc186ebe5c2b4f169467765086428
1 /*
2 * Garmin GPS driver
4 * Copyright (C) 2006-2009 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/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/timer.h>
31 #include <linux/tty.h>
32 #include <linux/tty_driver.h>
33 #include <linux/tty_flip.h>
34 #include <linux/module.h>
35 #include <linux/spinlock.h>
36 #include <linux/uaccess.h>
37 #include <asm/atomic.h>
38 #include <linux/usb.h>
39 #include <linux/usb/serial.h>
41 /* the mode to be set when the port ist opened */
42 static int initial_mode = 1;
44 /* debug flag */
45 static int debug;
47 #define GARMIN_VENDOR_ID 0x091E
50 * Version Information
53 #define VERSION_MAJOR 0
54 #define VERSION_MINOR 33
56 #define _STR(s) #s
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
88 * packets data */
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
113 #define ETX 0x03
114 #define DLE 0x10
115 #define ACK 0x06
116 #define NAK 0x15
118 /* structure used to queue incoming packets */
119 struct garmin_packet {
120 struct list_head list;
121 int seq;
122 /* the real size of the data array, always > 0 */
123 int size;
124 __u8 data[1];
127 /* structure used to keep the current state of the driver */
128 struct garmin_data {
129 __u8 state;
130 __u16 flags;
131 __u8 mode;
132 __u8 count;
133 __u8 pkt_id;
134 __u32 serial_num;
135 struct timer_list timer;
136 struct usb_serial_port *port;
137 int seq_counter;
138 int insize;
139 int outsize;
140 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
141 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
142 __u8 privpkt[4*6];
143 spinlock_t lock;
144 struct list_head pktlist;
148 #define STATE_NEW 0
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,
189 int dismiss_ack);
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[]
199 = { 0x14, 0, 0, 0 };
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 struct usb_device_id id_table [] = {
214 /* the same device id seems to be used by all
215 usb enabled GPS devices */
216 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
217 { } /* Terminating entry */
220 MODULE_DEVICE_TABLE(usb, id_table);
222 static struct usb_driver garmin_driver = {
223 .name = "garmin_gps",
224 .probe = usb_serial_probe,
225 .disconnect = usb_serial_disconnect,
226 .id_table = id_table,
227 .no_dynamic_id = 1,
231 static inline int getLayerId(const __u8 *usbPacket)
233 return __le32_to_cpup((__le32 *)(usbPacket));
236 static inline int getPacketId(const __u8 *usbPacket)
238 return __le32_to_cpup((__le32 *)(usbPacket+4));
241 static inline int getDataLength(const __u8 *usbPacket)
243 return __le32_to_cpup((__le32 *)(usbPacket+8));
248 * check if the usb-packet in buf contains an abort-transfer command.
249 * (if yes, all queued data will be dropped)
251 static inline int isAbortTrfCmnd(const unsigned char *buf)
253 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
254 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
255 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
256 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
257 return 1;
258 else
259 return 0;
264 static void send_to_tty(struct usb_serial_port *port,
265 char *data, unsigned int actual_length)
267 struct tty_struct *tty = tty_port_tty_get(&port->port);
269 if (tty && actual_length) {
271 usb_serial_debug_data(debug, &port->dev,
272 __func__, actual_length, data);
274 tty_buffer_request_room(tty, actual_length);
275 tty_insert_flip_string(tty, data, actual_length);
276 tty_flip_buffer_push(tty);
278 tty_kref_put(tty);
282 /******************************************************************************
283 * packet queue handling
284 ******************************************************************************/
287 * queue a received (usb-)packet for later processing
289 static int pkt_add(struct garmin_data *garmin_data_p,
290 unsigned char *data, unsigned int data_length)
292 int state = 0;
293 int result = 0;
294 unsigned long flags;
295 struct garmin_packet *pkt;
297 /* process only packets containg data ... */
298 if (data_length) {
299 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
300 GFP_ATOMIC);
301 if (pkt == NULL) {
302 dev_err(&garmin_data_p->port->dev, "out of memory\n");
303 return 0;
305 pkt->size = data_length;
306 memcpy(pkt->data, data, data_length);
308 spin_lock_irqsave(&garmin_data_p->lock, flags);
309 garmin_data_p->flags |= FLAGS_QUEUING;
310 result = list_empty(&garmin_data_p->pktlist);
311 pkt->seq = garmin_data_p->seq_counter++;
312 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
313 state = garmin_data_p->state;
314 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
316 dbg("%s - added: pkt: %d - %d bytes",
317 __func__, pkt->seq, data_length);
319 /* in serial mode, if someone is waiting for data from
320 the device, convert and send the next packet to tty. */
321 if (result && (state == STATE_GSP_WAIT_DATA))
322 gsp_next_packet(garmin_data_p);
324 return result;
328 /* get the next pending packet */
329 static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
331 unsigned long flags;
332 struct garmin_packet *result = NULL;
334 spin_lock_irqsave(&garmin_data_p->lock, flags);
335 if (!list_empty(&garmin_data_p->pktlist)) {
336 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
337 list_del(&result->list);
339 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
340 return result;
344 /* free up all queued data */
345 static void pkt_clear(struct garmin_data *garmin_data_p)
347 unsigned long flags;
348 struct garmin_packet *result = NULL;
350 dbg("%s", __func__);
352 spin_lock_irqsave(&garmin_data_p->lock, flags);
353 while (!list_empty(&garmin_data_p->pktlist)) {
354 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
355 list_del(&result->list);
356 kfree(result);
358 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
362 /******************************************************************************
363 * garmin serial protocol handling handling
364 ******************************************************************************/
366 /* send an ack packet back to the tty */
367 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
369 __u8 pkt[10];
370 __u8 cksum = 0;
371 __u8 *ptr = pkt;
372 unsigned l = 0;
374 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
376 *ptr++ = DLE;
377 *ptr++ = ACK;
378 cksum += ACK;
380 *ptr++ = 2;
381 cksum += 2;
383 *ptr++ = pkt_id;
384 cksum += pkt_id;
386 if (pkt_id == DLE)
387 *ptr++ = DLE;
389 *ptr++ = 0;
390 *ptr++ = 0xFF & (-cksum);
391 *ptr++ = DLE;
392 *ptr++ = ETX;
394 l = ptr-pkt;
396 send_to_tty(garmin_data_p->port, pkt, l);
397 return 0;
403 * called for a complete packet received from tty layer
405 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
406 * at GSP_INITIAL_OFFSET.
408 * count - number of bytes in the input buffer including space reserved for
409 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
410 * (including pkt-id, data-length a. cksum)
412 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
414 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
415 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
417 int cksum = 0;
418 int n = 0;
419 int pktid = recpkt[0];
420 int size = recpkt[1];
422 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
423 __func__, count-GSP_INITIAL_OFFSET, recpkt);
425 if (size != (count-GSP_INITIAL_OFFSET-3)) {
426 dbg("%s - invalid size, expected %d bytes, got %d",
427 __func__, size, (count-GSP_INITIAL_OFFSET-3));
428 return -EINVPKT;
431 cksum += *recpkt++;
432 cksum += *recpkt++;
434 /* sanity check, remove after test ... */
435 if ((__u8 *)&(usbdata[3]) != recpkt) {
436 dbg("%s - ptr mismatch %p - %p",
437 __func__, &(usbdata[4]), recpkt);
438 return -EINVPKT;
441 while (n < size) {
442 cksum += *recpkt++;
443 n++;
446 if ((0xff & (cksum + *recpkt)) != 0) {
447 dbg("%s - invalid checksum, expected %02x, got %02x",
448 __func__, 0xff & -cksum, 0xff & *recpkt);
449 return -EINVPKT;
452 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
453 usbdata[1] = __cpu_to_le32(pktid);
454 usbdata[2] = __cpu_to_le32(size);
456 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
457 GARMIN_PKTHDR_LENGTH+size, 0);
459 /* if this was an abort-transfer command, flush all
460 queued data. */
461 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
462 garmin_data_p->flags |= FLAGS_DROP_DATA;
463 pkt_clear(garmin_data_p);
466 return count;
472 * Called for data received from tty
474 * buf contains the data read, it may span more than one packet or even
475 * incomplete packets
477 * input record should be a serial-record, but it may not be complete.
478 * Copy it into our local buffer, until an etx is seen (or an error
479 * occurs).
480 * Once the record is complete, convert into a usb packet and send it
481 * to the bulk pipe, send an ack back to the tty.
483 * If the input is an ack, just send the last queued packet to the
484 * tty layer.
486 * if the input is an abort command, drop all queued data.
489 static int gsp_receive(struct garmin_data *garmin_data_p,
490 const unsigned char *buf, int count)
492 unsigned long flags;
493 int offs = 0;
494 int ack_or_nak_seen = 0;
495 __u8 *dest;
496 int size;
497 /* dleSeen: set if last byte read was a DLE */
498 int dleSeen;
499 /* skip: if set, skip incoming data until possible start of
500 * new packet
502 int skip;
503 __u8 data;
505 spin_lock_irqsave(&garmin_data_p->lock, flags);
506 dest = garmin_data_p->inbuffer;
507 size = garmin_data_p->insize;
508 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
509 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
510 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
512 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
513 __func__, dleSeen, skip, size, count); */
515 if (size == 0)
516 size = GSP_INITIAL_OFFSET;
518 while (offs < count) {
520 data = *(buf+offs);
521 offs++;
523 if (data == DLE) {
524 if (skip) { /* start of a new pkt */
525 skip = 0;
526 size = GSP_INITIAL_OFFSET;
527 dleSeen = 1;
528 } else if (dleSeen) {
529 dest[size++] = data;
530 dleSeen = 0;
531 } else {
532 dleSeen = 1;
534 } else if (data == ETX) {
535 if (dleSeen) {
536 /* packet complete */
538 data = dest[GSP_INITIAL_OFFSET];
540 if (data == ACK) {
541 ack_or_nak_seen = ACK;
542 dbg("ACK packet complete.");
543 } else if (data == NAK) {
544 ack_or_nak_seen = NAK;
545 dbg("NAK packet complete.");
546 } else {
547 dbg("packet complete - id=0x%X.",
548 0xFF & data);
549 gsp_rec_packet(garmin_data_p, size);
552 skip = 1;
553 size = GSP_INITIAL_OFFSET;
554 dleSeen = 0;
555 } else {
556 dest[size++] = data;
558 } else if (!skip) {
560 if (dleSeen) {
561 size = GSP_INITIAL_OFFSET;
562 dleSeen = 0;
565 dest[size++] = data;
568 if (size >= GPS_IN_BUFSIZ) {
569 dbg("%s - packet too large.", __func__);
570 skip = 1;
571 size = GSP_INITIAL_OFFSET;
572 dleSeen = 0;
576 spin_lock_irqsave(&garmin_data_p->lock, flags);
578 garmin_data_p->insize = size;
580 /* copy flags back to structure */
581 if (skip)
582 garmin_data_p->flags |= FLAGS_GSP_SKIP;
583 else
584 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
586 if (dleSeen)
587 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
588 else
589 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
591 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
593 if (ack_or_nak_seen) {
594 if (gsp_next_packet(garmin_data_p) > 0)
595 garmin_data_p->state = STATE_ACTIVE;
596 else
597 garmin_data_p->state = STATE_GSP_WAIT_DATA;
599 return count;
605 * Sends a usb packet to the tty
607 * Assumes, that all packages and at an usb-packet boundary.
609 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
611 static int gsp_send(struct garmin_data *garmin_data_p,
612 const unsigned char *buf, int count)
614 const unsigned char *src;
615 unsigned char *dst;
616 int pktid = 0;
617 int datalen = 0;
618 int cksum = 0;
619 int i = 0;
620 int k;
622 dbg("%s - state %d - %d bytes.", __func__,
623 garmin_data_p->state, count);
625 k = garmin_data_p->outsize;
626 if ((k+count) > GPS_OUT_BUFSIZ) {
627 dbg("packet too large");
628 garmin_data_p->outsize = 0;
629 return -4;
632 memcpy(garmin_data_p->outbuffer+k, buf, count);
633 k += count;
634 garmin_data_p->outsize = k;
636 if (k >= GARMIN_PKTHDR_LENGTH) {
637 pktid = getPacketId(garmin_data_p->outbuffer);
638 datalen = getDataLength(garmin_data_p->outbuffer);
639 i = GARMIN_PKTHDR_LENGTH + datalen;
640 if (k < i)
641 return 0;
642 } else {
643 return 0;
646 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
648 /* garmin_data_p->outbuffer now contains a complete packet */
650 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
651 __func__, k, garmin_data_p->outbuffer);
653 garmin_data_p->outsize = 0;
655 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
656 dbg("not an application packet (%d)",
657 getLayerId(garmin_data_p->outbuffer));
658 return -1;
661 if (pktid > 255) {
662 dbg("packet-id %d too large", pktid);
663 return -2;
666 if (datalen > 255) {
667 dbg("packet-size %d too large", datalen);
668 return -3;
671 /* the serial protocol should be able to handle this packet */
673 k = 0;
674 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
675 for (i = 0; i < datalen; i++) {
676 if (*src++ == DLE)
677 k++;
680 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
681 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
682 /* can't add stuffing DLEs in place, move data to end
683 of buffer ... */
684 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
685 memcpy(dst, src, datalen);
686 src = dst;
689 dst = garmin_data_p->outbuffer;
691 *dst++ = DLE;
692 *dst++ = pktid;
693 cksum += pktid;
694 *dst++ = datalen;
695 cksum += datalen;
696 if (datalen == DLE)
697 *dst++ = DLE;
699 for (i = 0; i < datalen; i++) {
700 __u8 c = *src++;
701 *dst++ = c;
702 cksum += c;
703 if (c == DLE)
704 *dst++ = DLE;
707 cksum = 0xFF & -cksum;
708 *dst++ = cksum;
709 if (cksum == DLE)
710 *dst++ = DLE;
711 *dst++ = DLE;
712 *dst++ = ETX;
714 i = dst-garmin_data_p->outbuffer;
716 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
718 garmin_data_p->pkt_id = pktid;
719 garmin_data_p->state = STATE_WAIT_TTY_ACK;
721 return i;
726 * Process the next pending data packet - if there is one
728 static int gsp_next_packet(struct garmin_data *garmin_data_p)
730 int result = 0;
731 struct garmin_packet *pkt = NULL;
733 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
734 dbg("%s - next pkt: %d", __func__, pkt->seq);
735 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
736 if (result > 0) {
737 kfree(pkt);
738 return result;
740 kfree(pkt);
742 return result;
747 /******************************************************************************
748 * garmin native mode
749 ******************************************************************************/
753 * Called for data received from tty
755 * The input data is expected to be in garmin usb-packet format.
757 * buf contains the data read, it may span more than one packet
758 * or even incomplete packets
760 static int nat_receive(struct garmin_data *garmin_data_p,
761 const unsigned char *buf, int count)
763 unsigned long flags;
764 __u8 *dest;
765 int offs = 0;
766 int result = count;
767 int len;
769 while (offs < count) {
770 /* if buffer contains header, copy rest of data */
771 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
772 len = GARMIN_PKTHDR_LENGTH
773 +getDataLength(garmin_data_p->inbuffer);
774 else
775 len = GARMIN_PKTHDR_LENGTH;
777 if (len >= GPS_IN_BUFSIZ) {
778 /* seems to be an invalid packet, ignore rest
779 of input */
780 dbg("%s - packet size too large: %d", __func__, len);
781 garmin_data_p->insize = 0;
782 count = 0;
783 result = -EINVPKT;
784 } else {
785 len -= garmin_data_p->insize;
786 if (len > (count-offs))
787 len = (count-offs);
788 if (len > 0) {
789 dest = garmin_data_p->inbuffer
790 + garmin_data_p->insize;
791 memcpy(dest, buf+offs, len);
792 garmin_data_p->insize += len;
793 offs += len;
797 /* do we have a complete packet ? */
798 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
799 len = GARMIN_PKTHDR_LENGTH+
800 getDataLength(garmin_data_p->inbuffer);
801 if (garmin_data_p->insize >= len) {
802 garmin_write_bulk(garmin_data_p->port,
803 garmin_data_p->inbuffer,
804 len, 0);
805 garmin_data_p->insize = 0;
807 /* if this was an abort-transfer command,
808 flush all queued data. */
809 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
810 spin_lock_irqsave(&garmin_data_p->lock,
811 flags);
812 garmin_data_p->flags |= FLAGS_DROP_DATA;
813 spin_unlock_irqrestore(
814 &garmin_data_p->lock, flags);
815 pkt_clear(garmin_data_p);
820 return result;
824 /******************************************************************************
825 * private packets
826 ******************************************************************************/
828 static void priv_status_resp(struct usb_serial_port *port)
830 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
831 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
833 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
834 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
835 pkt[2] = __cpu_to_le32(12);
836 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
837 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
838 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
840 send_to_tty(port, (__u8 *)pkt, 6 * 4);
844 /******************************************************************************
845 * Garmin specific driver functions
846 ******************************************************************************/
848 static int process_resetdev_request(struct usb_serial_port *port)
850 unsigned long flags;
851 int status;
852 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
854 spin_lock_irqsave(&garmin_data_p->lock, flags);
855 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
856 garmin_data_p->state = STATE_RESET;
857 garmin_data_p->serial_num = 0;
858 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
860 usb_kill_urb(port->interrupt_in_urb);
861 dbg("%s - usb_reset_device", __func__);
862 status = usb_reset_device(port->serial->dev);
863 if (status)
864 dbg("%s - usb_reset_device failed: %d",
865 __func__, status);
866 return status;
872 * clear all cached data
874 static int garmin_clear(struct garmin_data *garmin_data_p)
876 unsigned long flags;
877 int status = 0;
879 /* flush all queued data */
880 pkt_clear(garmin_data_p);
882 spin_lock_irqsave(&garmin_data_p->lock, flags);
883 garmin_data_p->insize = 0;
884 garmin_data_p->outsize = 0;
885 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
887 return status;
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);
895 int status = 0;
896 int i = 0;
898 if (status == 0) {
899 usb_kill_urb(port->interrupt_in_urb);
901 dbg("%s - adding interrupt input", __func__);
902 port->interrupt_in_urb->dev = serial->dev;
903 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
904 if (status)
905 dev_err(&serial->dev->dev,
906 "%s - failed submitting interrupt urb, error %d\n",
907 __func__, status);
911 * using the initialization method from gpsbabel. See comments in
912 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
914 if (status == 0) {
915 dbg("%s - starting session ...", __func__);
916 garmin_data_p->state = STATE_ACTIVE;
918 for (i = 0; i < 3; i++) {
919 status = garmin_write_bulk(port,
920 GARMIN_START_SESSION_REQ,
921 sizeof(GARMIN_START_SESSION_REQ), 0);
923 if (status < 0)
924 break;
927 if (status > 0)
928 status = 0;
931 return status;
936 static int garmin_open(struct tty_struct *tty,
937 struct usb_serial_port *port, struct file *filp)
939 unsigned long flags;
940 int status = 0;
941 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
943 dbg("%s - port %d", __func__, port->number);
945 spin_lock_irqsave(&garmin_data_p->lock, flags);
946 garmin_data_p->mode = initial_mode;
947 garmin_data_p->count = 0;
948 garmin_data_p->flags = 0;
949 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
951 /* shutdown any bulk reads that might be going on */
952 usb_kill_urb(port->write_urb);
953 usb_kill_urb(port->read_urb);
955 if (garmin_data_p->state == STATE_RESET)
956 status = garmin_init_session(port);
958 garmin_data_p->state = STATE_ACTIVE;
959 return status;
963 static void garmin_close(struct usb_serial_port *port)
965 struct usb_serial *serial = port->serial;
966 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
968 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
969 port->number, garmin_data_p->mode,
970 garmin_data_p->state, garmin_data_p->flags);
972 if (!serial)
973 return;
975 mutex_lock(&port->serial->disc_mutex);
977 if (!port->serial->disconnected)
978 garmin_clear(garmin_data_p);
980 /* shutdown our urbs */
981 usb_kill_urb(port->read_urb);
982 usb_kill_urb(port->write_urb);
984 /* keep reset state so we know that we must start a new session */
985 if (garmin_data_p->state != STATE_RESET)
986 garmin_data_p->state = STATE_DISCONNECTED;
988 mutex_unlock(&port->serial->disc_mutex);
992 static void garmin_write_bulk_callback(struct urb *urb)
994 struct usb_serial_port *port = urb->context;
996 if (port) {
997 struct garmin_data *garmin_data_p =
998 usb_get_serial_port_data(port);
1000 dbg("%s - port %d", __func__, port->number);
1002 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
1004 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1005 gsp_send_ack(garmin_data_p,
1006 ((__u8 *)urb->transfer_buffer)[4]);
1009 usb_serial_port_softint(port);
1012 /* Ignore errors that resulted from garmin_write_bulk with
1013 dismiss_ack = 1 */
1015 /* free up the transfer buffer, as usb_free_urb() does not do this */
1016 kfree(urb->transfer_buffer);
1020 static int garmin_write_bulk(struct usb_serial_port *port,
1021 const unsigned char *buf, int count,
1022 int dismiss_ack)
1024 unsigned long flags;
1025 struct usb_serial *serial = port->serial;
1026 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1027 struct urb *urb;
1028 unsigned char *buffer;
1029 int status;
1031 dbg("%s - port %d, state %d", __func__, port->number,
1032 garmin_data_p->state);
1034 spin_lock_irqsave(&garmin_data_p->lock, flags);
1035 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
1036 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1038 buffer = kmalloc(count, GFP_ATOMIC);
1039 if (!buffer) {
1040 dev_err(&port->dev, "out of memory\n");
1041 return -ENOMEM;
1044 urb = usb_alloc_urb(0, GFP_ATOMIC);
1045 if (!urb) {
1046 dev_err(&port->dev, "no more free urbs\n");
1047 kfree(buffer);
1048 return -ENOMEM;
1051 memcpy(buffer, buf, count);
1053 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
1055 usb_fill_bulk_urb(urb, serial->dev,
1056 usb_sndbulkpipe(serial->dev,
1057 port->bulk_out_endpointAddress),
1058 buffer, count,
1059 garmin_write_bulk_callback,
1060 dismiss_ack ? NULL : port);
1061 urb->transfer_flags |= URB_ZERO_PACKET;
1063 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
1065 spin_lock_irqsave(&garmin_data_p->lock, flags);
1066 garmin_data_p->flags |= APP_REQ_SEEN;
1067 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1069 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1070 pkt_clear(garmin_data_p);
1071 garmin_data_p->state = STATE_GSP_WAIT_DATA;
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) failed with status = %d\n",
1080 __func__, status);
1081 count = status;
1084 /* we are done with this urb, so let the host driver
1085 * really free it when it is finished with it */
1086 usb_free_urb(urb);
1088 return count;
1091 static int garmin_write(struct tty_struct *tty, 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, __func__, count, buf);
1100 if (garmin_data_p->state == STATE_RESET)
1101 return -EIO;
1103 /* check for our private packets */
1104 if (count >= GARMIN_PKTHDR_LENGTH) {
1105 len = PRIVPKTSIZ;
1106 if (count < len)
1107 len = count;
1109 memcpy(garmin_data_p->privpkt, buf, len);
1111 pktsiz = getDataLength(garmin_data_p->privpkt);
1112 pktid = getPacketId(garmin_data_p->privpkt);
1114 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
1115 && GARMIN_LAYERID_PRIVATE ==
1116 getLayerId(garmin_data_p->privpkt)) {
1118 dbg("%s - processing private request %d",
1119 __func__, pktid);
1121 /* drop all unfinished transfers */
1122 garmin_clear(garmin_data_p);
1124 switch (pktid) {
1126 case PRIV_PKTID_SET_DEBUG:
1127 if (pktsiz != 4)
1128 return -EINVPKT;
1129 debug = __le32_to_cpu(privpkt[3]);
1130 dbg("%s - debug level set to 0x%X",
1131 __func__, debug);
1132 break;
1134 case PRIV_PKTID_SET_MODE:
1135 if (pktsiz != 4)
1136 return -EINVPKT;
1137 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1138 dbg("%s - mode set to %d",
1139 __func__, garmin_data_p->mode);
1140 break;
1142 case PRIV_PKTID_INFO_REQ:
1143 priv_status_resp(port);
1144 break;
1146 case PRIV_PKTID_RESET_REQ:
1147 process_resetdev_request(port);
1148 break;
1150 case PRIV_PKTID_SET_DEF_MODE:
1151 if (pktsiz != 4)
1152 return -EINVPKT;
1153 initial_mode = __le32_to_cpu(privpkt[3]);
1154 dbg("%s - initial_mode set to %d",
1155 __func__,
1156 garmin_data_p->mode);
1157 break;
1159 return count;
1163 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1164 return gsp_receive(garmin_data_p, buf, count);
1165 } else { /* MODE_NATIVE */
1166 return nat_receive(garmin_data_p, buf, count);
1171 static int garmin_write_room(struct tty_struct *tty)
1173 struct usb_serial_port *port = tty->driver_data;
1175 * Report back the bytes currently available in the output buffer.
1177 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1178 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1182 static void garmin_read_process(struct garmin_data *garmin_data_p,
1183 unsigned char *data, unsigned data_length)
1185 unsigned long flags;
1187 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1188 /* abort-transfer cmd is actice */
1189 dbg("%s - pkt dropped", __func__);
1190 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
1191 garmin_data_p->state != STATE_RESET) {
1193 /* if throttling is active or postprecessing is required
1194 put the received data in the input queue, otherwise
1195 send it directly to the tty port */
1196 if (garmin_data_p->flags & FLAGS_QUEUING) {
1197 pkt_add(garmin_data_p, data, data_length);
1198 } else if (getLayerId(data) == GARMIN_LAYERID_APPL) {
1200 spin_lock_irqsave(&garmin_data_p->lock, flags);
1201 garmin_data_p->flags |= APP_RESP_SEEN;
1202 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1204 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1205 pkt_add(garmin_data_p, data, data_length);
1206 } else {
1207 send_to_tty(garmin_data_p->port, data,
1208 data_length);
1211 /* ignore system layer packets ... */
1216 static void garmin_read_bulk_callback(struct urb *urb)
1218 unsigned long flags;
1219 struct usb_serial_port *port = urb->context;
1220 struct usb_serial *serial = port->serial;
1221 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1222 unsigned char *data = urb->transfer_buffer;
1223 int status = urb->status;
1224 int retval;
1226 dbg("%s - port %d", __func__, port->number);
1228 if (!serial) {
1229 dbg("%s - bad serial pointer, exiting", __func__);
1230 return;
1233 if (status) {
1234 dbg("%s - nonzero read bulk status received: %d",
1235 __func__, status);
1236 return;
1239 usb_serial_debug_data(debug, &port->dev,
1240 __func__, urb->actual_length, data);
1242 garmin_read_process(garmin_data_p, data, urb->actual_length);
1244 if (urb->actual_length == 0 &&
1245 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1246 spin_lock_irqsave(&garmin_data_p->lock, flags);
1247 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1248 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1249 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1250 if (retval)
1251 dev_err(&port->dev,
1252 "%s - failed resubmitting read urb, error %d\n",
1253 __func__, retval);
1254 } else if (urb->actual_length > 0) {
1255 /* Continue trying to read until nothing more is received */
1256 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
1257 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1258 if (retval)
1259 dev_err(&port->dev,
1260 "%s - failed resubmitting read urb, "
1261 "error %d\n", __func__, retval);
1263 } else {
1264 dbg("%s - end of bulk data", __func__);
1265 spin_lock_irqsave(&garmin_data_p->lock, flags);
1266 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1267 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1269 return;
1273 static void garmin_read_int_callback(struct urb *urb)
1275 unsigned long flags;
1276 int retval;
1277 struct usb_serial_port *port = urb->context;
1278 struct usb_serial *serial = port->serial;
1279 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1280 unsigned char *data = urb->transfer_buffer;
1281 int status = urb->status;
1283 switch (status) {
1284 case 0:
1285 /* success */
1286 break;
1287 case -ECONNRESET:
1288 case -ENOENT:
1289 case -ESHUTDOWN:
1290 /* this urb is terminated, clean up */
1291 dbg("%s - urb shutting down with status: %d",
1292 __func__, status);
1293 return;
1294 default:
1295 dbg("%s - nonzero urb status received: %d",
1296 __func__, status);
1297 return;
1300 usb_serial_debug_data(debug, &port->dev, __func__,
1301 urb->actual_length, urb->transfer_buffer);
1303 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1304 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
1305 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
1307 dbg("%s - bulk data available.", __func__);
1309 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1311 /* bulk data available */
1312 usb_fill_bulk_urb(port->read_urb, serial->dev,
1313 usb_rcvbulkpipe(serial->dev,
1314 port->bulk_in_endpointAddress),
1315 port->read_urb->transfer_buffer,
1316 port->read_urb->transfer_buffer_length,
1317 garmin_read_bulk_callback, port);
1318 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1319 if (retval) {
1320 dev_err(&port->dev,
1321 "%s - failed submitting read urb, error %d\n",
1322 __func__, retval);
1323 } else {
1324 spin_lock_irqsave(&garmin_data_p->lock, flags);
1325 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1326 spin_unlock_irqrestore(&garmin_data_p->lock,
1327 flags);
1329 } else {
1330 /* bulk-in transfer still active */
1331 spin_lock_irqsave(&garmin_data_p->lock, flags);
1332 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1333 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1336 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1337 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
1338 sizeof(GARMIN_START_SESSION_REPLY))) {
1340 spin_lock_irqsave(&garmin_data_p->lock, flags);
1341 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
1342 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1344 /* save the serial number */
1345 garmin_data_p->serial_num = __le32_to_cpup(
1346 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
1348 dbg("%s - start-of-session reply seen - serial %u.",
1349 __func__, garmin_data_p->serial_num);
1352 garmin_read_process(garmin_data_p, data, urb->actual_length);
1354 port->interrupt_in_urb->dev = port->serial->dev;
1355 retval = usb_submit_urb(urb, GFP_ATOMIC);
1356 if (retval)
1357 dev_err(&urb->dev->dev,
1358 "%s - Error %d submitting interrupt urb\n",
1359 __func__, retval);
1364 * Sends the next queued packt to the tty port (garmin native mode only)
1365 * and then sets a timer to call itself again until all queued data
1366 * is sent.
1368 static int garmin_flush_queue(struct garmin_data *garmin_data_p)
1370 unsigned long flags;
1371 struct garmin_packet *pkt;
1373 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1374 pkt = pkt_pop(garmin_data_p);
1375 if (pkt != NULL) {
1376 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1377 kfree(pkt);
1378 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1380 } else {
1381 spin_lock_irqsave(&garmin_data_p->lock, flags);
1382 garmin_data_p->flags &= ~FLAGS_QUEUING;
1383 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1386 return 0;
1390 static void garmin_throttle(struct tty_struct *tty)
1392 struct usb_serial_port *port = tty->driver_data;
1393 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1394 unsigned long flags;
1396 dbg("%s - port %d", __func__, port->number);
1397 /* set flag, data received will be put into a queue
1398 for later processing */
1399 spin_lock_irqsave(&garmin_data_p->lock, flags);
1400 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
1401 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1405 static void garmin_unthrottle(struct tty_struct *tty)
1407 struct usb_serial_port *port = tty->driver_data;
1408 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1409 unsigned long flags;
1410 int status;
1412 dbg("%s - port %d", __func__, port->number);
1413 spin_lock_irqsave(&garmin_data_p->lock, flags);
1414 garmin_data_p->flags &= ~FLAGS_THROTTLED;
1415 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1417 /* in native mode send queued data to tty, in
1418 serial mode nothing needs to be done here */
1419 if (garmin_data_p->mode == MODE_NATIVE)
1420 garmin_flush_queue(garmin_data_p);
1422 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1423 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1424 if (status)
1425 dev_err(&port->dev,
1426 "%s - failed resubmitting read urb, error %d\n",
1427 __func__, status);
1432 * The timer is currently only used to send queued packets to
1433 * the tty in cases where the protocol provides no own handshaking
1434 * to initiate the transfer.
1436 static void timeout_handler(unsigned long data)
1438 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1440 /* send the next queued packet to the tty port */
1441 if (garmin_data_p->mode == MODE_NATIVE)
1442 if (garmin_data_p->flags & FLAGS_QUEUING)
1443 garmin_flush_queue(garmin_data_p);
1448 static int garmin_attach(struct usb_serial *serial)
1450 int status = 0;
1451 struct usb_serial_port *port = serial->port[0];
1452 struct garmin_data *garmin_data_p = NULL;
1454 dbg("%s", __func__);
1456 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
1457 if (garmin_data_p == NULL) {
1458 dev_err(&port->dev, "%s - Out of memory\n", __func__);
1459 return -ENOMEM;
1461 init_timer(&garmin_data_p->timer);
1462 spin_lock_init(&garmin_data_p->lock);
1463 INIT_LIST_HEAD(&garmin_data_p->pktlist);
1464 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
1465 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1466 garmin_data_p->timer.function = timeout_handler;
1467 garmin_data_p->port = port;
1468 garmin_data_p->state = 0;
1469 garmin_data_p->count = 0;
1470 usb_set_serial_port_data(port, garmin_data_p);
1472 status = garmin_init_session(port);
1474 return status;
1478 static void garmin_disconnect(struct usb_serial *serial)
1480 struct usb_serial_port *port = serial->port[0];
1481 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1483 dbg("%s", __func__);
1485 usb_kill_urb(port->interrupt_in_urb);
1486 del_timer_sync(&garmin_data_p->timer);
1490 static void garmin_release(struct usb_serial *serial)
1492 struct usb_serial_port *port = serial->port[0];
1493 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1495 dbg("%s", __func__);
1497 kfree(garmin_data_p);
1501 /* All of the device info needed */
1502 static struct usb_serial_driver garmin_device = {
1503 .driver = {
1504 .owner = THIS_MODULE,
1505 .name = "garmin_gps",
1507 .description = "Garmin GPS usb/tty",
1508 .usb_driver = &garmin_driver,
1509 .id_table = id_table,
1510 .num_ports = 1,
1511 .open = garmin_open,
1512 .close = garmin_close,
1513 .throttle = garmin_throttle,
1514 .unthrottle = garmin_unthrottle,
1515 .attach = garmin_attach,
1516 .disconnect = garmin_disconnect,
1517 .release = garmin_release,
1518 .write = garmin_write,
1519 .write_room = garmin_write_room,
1520 .write_bulk_callback = garmin_write_bulk_callback,
1521 .read_bulk_callback = garmin_read_bulk_callback,
1522 .read_int_callback = garmin_read_int_callback,
1527 static int __init garmin_init(void)
1529 int retval;
1531 retval = usb_serial_register(&garmin_device);
1532 if (retval)
1533 goto failed_garmin_register;
1534 retval = usb_register(&garmin_driver);
1535 if (retval)
1536 goto failed_usb_register;
1537 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1538 DRIVER_DESC "\n");
1540 return 0;
1541 failed_usb_register:
1542 usb_serial_deregister(&garmin_device);
1543 failed_garmin_register:
1544 return retval;
1548 static void __exit garmin_exit(void)
1550 usb_deregister(&garmin_driver);
1551 usb_serial_deregister(&garmin_device);
1557 module_init(garmin_init);
1558 module_exit(garmin_exit);
1560 MODULE_AUTHOR(DRIVER_AUTHOR);
1561 MODULE_DESCRIPTION(DRIVER_DESC);
1562 MODULE_LICENSE("GPL");
1564 module_param(debug, bool, S_IWUSR | S_IRUGO);
1565 MODULE_PARM_DESC(debug, "Debug enabled or not");
1566 module_param(initial_mode, int, S_IRUGO);
1567 MODULE_PARM_DESC(initial_mode, "Initial mode");