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