blob: a26a0e2cdb4acd8debe7f26506d1e5c542595ddd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Garmin GPS driver
3 *
Hermann Kneissel468d1362007-08-03 20:20:33 +02004 * Copyright (C) 2006,2007 Hermann Kneissel herkne@users.sourceforge.net
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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>
Alan Coxaf6d7802008-07-22 11:11:44 +010036#include <linux/uaccess.h>
Hermann Kneissel468d1362007-08-03 20:20:33 +020037#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070039#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* the mode to be set when the port ist opened */
42static int initial_mode = 1;
43
44/* debug flag */
Alan Coxaf6d7802008-07-22 11:11:44 +010045static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define GARMIN_VENDOR_ID 0x091E
48
49/*
50 * Version Information
51 */
52
53#define VERSION_MAJOR 0
Hermann Kneissel468d1362007-08-03 20:20:33 +020054#define VERSION_MINOR 31
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define _STR(s) #s
Alan Coxaf6d7802008-07-22 11:11:44 +010057#define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#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
Alan Coxaf6d7802008-07-22 11:11:44 +010066/* size of the header of a packet using the usb protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define GARMIN_PKTHDR_LENGTH 12
68
Alan Coxaf6d7802008-07-22 11:11:44 +010069/* max. possible size of a packet using the serial protocol */
70#define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Alan Coxaf6d7802008-07-22 11:11:44 +010072/* max. possible size of a packet with worst case stuffing */
73#define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Alan Coxaf6d7802008-07-22 11:11:44 +010075/* 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Alan Coxaf6d7802008-07-22 11:11:44 +010083/* 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Alan Coxaf6d7802008-07-22 11:11:44 +010086/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
90
Alan Coxaf6d7802008-07-22 11:11:44 +010091/* max. size of incoming private packets (header+1 param) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
93
94#define GARMIN_LAYERID_TRANSPORT 0
95#define GARMIN_LAYERID_APPL 20
Alan Coxaf6d7802008-07-22 11:11:44 +010096/* our own layer-id to use for some control mechanisms */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#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
Alan Coxaf6d7802008-07-22 11:11:44 +0100104/* packet ids used in private layer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#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;
Alan Coxaf6d7802008-07-22 11:11:44 +0100122 /* the real size of the data array, always > 0 */
123 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 __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];
Hermann Kneissel468d1362007-08-03 20:20:33 +0200144 atomic_t req_count;
145 atomic_t resp_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 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
Alan Coxaf6d7802008-07-22 11:11:44 +0100166/* Flags used in garmin_data.flags: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#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
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200171#define FLAGS_BULK_IN_RESTART 0x0010
172#define FLAGS_THROTTLED 0x0008
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#define CLEAR_HALT_REQUIRED 0x0001
174
175#define FLAGS_QUEUING 0x0100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#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 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100187static void gsp_next_packet(struct garmin_data *garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200189 const unsigned char *buf, int count,
190 int dismiss_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
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 [] = {
Alan Coxaf6d7802008-07-22 11:11:44 +0100219 /* the same device id seems to be used by all
220 usb enabled GPS devices */
221 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 { } /* Terminating entry */
223};
224
Alan Coxaf6d7802008-07-22 11:11:44 +0100225MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227static struct usb_driver garmin_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .name = "garmin_gps",
229 .probe = usb_serial_probe,
230 .disconnect = usb_serial_disconnect,
231 .id_table = id_table,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200232 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
235
Alan Coxaf6d7802008-07-22 11:11:44 +0100236static inline int noResponseFromAppLayer(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Alan Coxaf6d7802008-07-22 11:11:44 +0100238 return atomic_read(&garmin_data_p->req_count) ==
239 atomic_read(&garmin_data_p->resp_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
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{
Alan Coxaf6d7802008-07-22 11:11:44 +0100265 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)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 1;
270 else
271 return 0;
272}
273
274
275
276static void send_to_tty(struct usb_serial_port *port,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200277 char *data, unsigned int actual_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Alan Cox4a90f092008-10-13 10:39:46 +0100279 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 if (tty && actual_length) {
282
Alan Coxaf6d7802008-07-22 11:11:44 +0100283 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800284 __func__, actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Alan Cox33f0f882006-01-09 20:54:13 -0800286 tty_buffer_request_room(tty, actual_length);
287 tty_insert_flip_string(tty, data, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 tty_flip_buffer_push(tty);
289 }
Alan Cox4a90f092008-10-13 10:39:46 +0100290 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293
294/******************************************************************************
295 * packet queue handling
296 ******************************************************************************/
297
298/*
299 * queue a received (usb-)packet for later processing
300 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100301static int pkt_add(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200302 unsigned char *data, unsigned int data_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200304 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int result = 0;
306 unsigned long flags;
307 struct garmin_packet *pkt;
308
309 /* process only packets containg data ... */
310 if (data_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
Alan Coxaf6d7802008-07-22 11:11:44 +0100312 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 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);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200321 garmin_data_p->flags |= FLAGS_QUEUING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 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);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200325 state = garmin_data_p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 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. */
Alan Coxaf6d7802008-07-22 11:11:44 +0100330 if (result && (state == STATE_GSP_WAIT_DATA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 return result;
334}
335
336
337/* get the next pending packet */
Alan Coxaf6d7802008-07-22 11:11:44 +0100338static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
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 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100354static void pkt_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 unsigned long flags;
357 struct garmin_packet *result = NULL;
358
Harvey Harrison441b62c2008-03-03 16:08:34 -0800359 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
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 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100376static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 __u8 pkt[10];
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200379 __u8 cksum = 0;
380 __u8 *ptr = pkt;
381 unsigned l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Harvey Harrison441b62c2008-03-03 16:08:34 -0800383 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
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
Alan Coxaf6d7802008-07-22 11:11:44 +0100395 if (pkt_id == DLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *ptr++ = DLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
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
Alan Coxaf6d7802008-07-22 11:11:44 +0100418 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * (including pkt-id, data-length a. cksum)
420 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100421static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Alan Coxaf6d7802008-07-22 11:11:44 +0100423 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200424 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
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,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800432 __func__, count-GSP_INITIAL_OFFSET, recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (size != (count-GSP_INITIAL_OFFSET-3)) {
435 dbg("%s - invalid size, expected %d bytes, got %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800436 __func__, size, (count-GSP_INITIAL_OFFSET-3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EINVPKT;
438 }
439
440 cksum += *recpkt++;
441 cksum += *recpkt++;
442
Alan Coxaf6d7802008-07-22 11:11:44 +0100443 /* sanity check, remove after test ... */
444 if ((__u8 *)&(usbdata[3]) != recpkt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 dbg("%s - ptr mismatch %p - %p",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800446 __func__, &(usbdata[4]), recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -EINVPKT;
448 }
449
450 while (n < size) {
451 cksum += *recpkt++;
452 n++;
453 }
454
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200455 if ((0xff & (cksum + *recpkt)) != 0) {
456 dbg("%s - invalid checksum, expected %02x, got %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800457 __func__, 0xff & -cksum, 0xff & *recpkt);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200458 return -EINVPKT;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
462 usbdata[1] = __cpu_to_le32(pktid);
463 usbdata[2] = __cpu_to_le32(size);
464
Alan Coxaf6d7802008-07-22 11:11:44 +0100465 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200466 GARMIN_PKTHDR_LENGTH+size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
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
Alan Coxaf6d7802008-07-22 11:11:44 +0100498static int gsp_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200499 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200501 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int offs = 0;
503 int ack_or_nak_seen = 0;
504 int i = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200505 __u8 *dest;
506 int size;
Alan Coxaf6d7802008-07-22 11:11:44 +0100507 /* dleSeen: set if last byte read was a DLE */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200508 int dleSeen;
Alan Coxaf6d7802008-07-22 11:11:44 +0100509 /* skip: if set, skip incoming data until possible start of
510 * new packet
511 */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200512 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 __u8 data;
514
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200515 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 dbg("%s - dle=%d skip=%d size=%d count=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800523 __func__, dleSeen, skip, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Alan Coxaf6d7802008-07-22 11:11:44 +0100525 if (size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 size = GSP_INITIAL_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 while (offs < count) {
529
530 data = *(buf+offs);
Alan Coxaf6d7802008-07-22 11:11:44 +0100531 offs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
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 {
Alan Coxaf6d7802008-07-22 11:11:44 +0100557 dbg("packet complete - id=0x%X.",
558 0xFF & data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 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) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800580 dbg("%s - packet too large.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 skip = 1;
582 size = GSP_INITIAL_OFFSET;
583 dleSeen = 0;
584 }
585 }
586
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200587 spin_lock_irqsave(&garmin_data_p->lock, flags);
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 garmin_data_p->insize = size;
590
Alan Coxaf6d7802008-07-22 11:11:44 +0100591 /* copy flags back to structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 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
Alan Coxaf6d7802008-07-22 11:11:44 +0100602 if (ack_or_nak_seen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 garmin_data_p->state = STATE_GSP_WAIT_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200604
605 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
606
Alan Coxaf6d7802008-07-22 11:11:44 +0100607 if (ack_or_nak_seen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 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 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100622static int gsp_send(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 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;
Alan Coxaf6d7802008-07-22 11:11:44 +0100630 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int k;
632
Harvey Harrison441b62c2008-03-03 16:08:34 -0800633 dbg("%s - state %d - %d bytes.", __func__,
Alan Coxaf6d7802008-07-22 11:11:44 +0100634 garmin_data_p->state, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
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);
Alan Coxaf6d7802008-07-22 11:11:44 +0100649 datalen = getDataLength(garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 i = GARMIN_PKTHDR_LENGTH + datalen;
651 if (k < i)
652 return 0;
653 } else {
654 return 0;
655 }
656
Alan Coxaf6d7802008-07-22 11:11:44 +0100657 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* garmin_data_p->outbuffer now contains a complete packet */
660
661 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100662 __func__, k, garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 garmin_data_p->outsize = 0;
665
666 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100667 dbg("not an application packet (%d)",
668 getLayerId(garmin_data_p->outbuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 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;
Alan Coxaf6d7802008-07-22 11:11:44 +0100686 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (*src++ == DLE)
688 k++;
689 }
690
691 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
692 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100693 /* can't add stuffing DLEs in place, move data to end
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200694 of buffer ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 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
Alan Coxaf6d7802008-07-22 11:11:44 +0100710 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 __u8 c = *src++;
712 *dst++ = c;
713 cksum += c;
714 if (c == DLE)
715 *dst++ = DLE;
716 }
Alan Coxaf6d7802008-07-22 11:11:44 +0100717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 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 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100742static void gsp_next_packet(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct garmin_packet *pkt = NULL;
745
746 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800747 dbg("%s - next pkt: %d", __func__, pkt->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 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 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100772static int nat_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200773 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200775 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100776 __u8 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int offs = 0;
778 int result = count;
779 int len;
780
781 while (offs < count) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100782 /* if buffer contains header, copy rest of data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 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) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100790 /* seems to be an invalid packet, ignore rest
791 of input */
792 dbg("%s - packet size too large: %d", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 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
Alan Coxaf6d7802008-07-22 11:11:44 +0100802 + garmin_data_p->insize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 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) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100814 garmin_write_bulk(garmin_data_p->port,
815 garmin_data_p->inbuffer,
816 len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 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)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100822 spin_lock_irqsave(&garmin_data_p->lock,
823 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 garmin_data_p->flags |= FLAGS_DROP_DATA;
Alan Coxaf6d7802008-07-22 11:11:44 +0100825 spin_unlock_irqrestore(
826 &garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 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{
Alan Coxaf6d7802008-07-22 11:11:44 +0100842 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 __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
Alan Coxaf6d7802008-07-22 11:11:44 +0100852 send_to_tty(port, (__u8 *)pkt, 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855
856/******************************************************************************
857 * Garmin specific driver functions
858 ******************************************************************************/
859
860static int process_resetdev_request(struct usb_serial_port *port)
861{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200862 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int status;
Alan Coxaf6d7802008-07-22 11:11:44 +0100864 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200866 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
868 garmin_data_p->state = STATE_RESET;
869 garmin_data_p->serial_num = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200870 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Alan Coxaf6d7802008-07-22 11:11:44 +0100872 usb_kill_urb(port->interrupt_in_urb);
873 dbg("%s - usb_reset_device", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 status = usb_reset_device(port->serial->dev);
875 if (status)
876 dbg("%s - usb_reset_device failed: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800877 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return status;
879}
880
881
882
883/*
884 * clear all cached data
885 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100886static int garmin_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200888 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int status = 0;
890
891 struct usb_serial_port *port = garmin_data_p->port;
892
Hermann Kneissel468d1362007-08-03 20:20:33 +0200893 if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /* send a terminate command */
895 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100896 sizeof(GARMIN_STOP_TRANSFER_REQ), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
899 /* flush all queued data */
900 pkt_clear(garmin_data_p);
901
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200902 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 garmin_data_p->insize = 0;
904 garmin_data_p->outsize = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200905 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 return status;
908}
909
910
911
912
913
914
915static int garmin_init_session(struct usb_serial_port *port)
916{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200917 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100919 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int status = 0;
921
922 if (status == 0) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100923 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Harvey Harrison441b62c2008-03-03 16:08:34 -0800925 dbg("%s - adding interrupt input", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 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,
Alan Coxaf6d7802008-07-22 11:11:44 +0100930 "%s - failed submitting interrupt urb, error %d\n",
931 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934 if (status == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800935 dbg("%s - starting session ...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 garmin_data_p->state = STATE_ACTIVE;
937 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100938 sizeof(GARMIN_START_SESSION_REQ), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (status >= 0) {
941
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200942 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 garmin_data_p->ignorePkts++;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200944 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 /* not needed, but the win32 driver does it too ... */
947 status = garmin_write_bulk(port,
Alan Coxaf6d7802008-07-22 11:11:44 +0100948 GARMIN_START_SESSION_REQ2,
949 sizeof(GARMIN_START_SESSION_REQ2), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (status >= 0) {
951 status = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200952 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 garmin_data_p->ignorePkts++;
Alan Coxaf6d7802008-07-22 11:11:44 +0100954 spin_unlock_irqrestore(&garmin_data_p->lock,
955 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 }
958 }
959
960 return status;
961}
962
963
964
965
966
Alan Coxaf6d7802008-07-22 11:11:44 +0100967static int garmin_open(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100968 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200970 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 int status = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100972 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Harvey Harrison441b62c2008-03-03 16:08:34 -0800974 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /*
977 * Force low_latency on so that our tty_push actually forces the data
978 * through, otherwise it is scheduled, and with high data rates (like
979 * with OHCI) data can get lost.
980 */
Alan Cox95da3102008-07-22 11:09:07 +0100981 if (tty)
982 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200984 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 garmin_data_p->mode = initial_mode;
986 garmin_data_p->count = 0;
987 garmin_data_p->flags = 0;
Hermann Kneissel468d1362007-08-03 20:20:33 +0200988 atomic_set(&garmin_data_p->req_count, 0);
989 atomic_set(&garmin_data_p->resp_count, 0);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200990 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /* shutdown any bulk reads that might be going on */
Alan Coxaf6d7802008-07-22 11:11:44 +0100993 usb_kill_urb(port->write_urb);
994 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Alan Cox95da3102008-07-22 11:09:07 +0100996 if (garmin_data_p->state == STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 status = garmin_init_session(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 garmin_data_p->state = STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return status;
1001}
1002
1003
Alan Cox95da3102008-07-22 11:09:07 +01001004static void garmin_close(struct tty_struct *tty,
Alan Coxaf6d7802008-07-22 11:11:44 +01001005 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
1007 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001008 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Harvey Harrison441b62c2008-03-03 16:08:34 -08001010 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 port->number, garmin_data_p->mode,
1012 garmin_data_p->state, garmin_data_p->flags);
1013
1014 if (!serial)
1015 return;
1016
Oliver Neukum95bef012008-01-22 13:56:18 +01001017 mutex_lock(&port->serial->disc_mutex);
1018 if (!port->serial->disconnected)
1019 garmin_clear(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* shutdown our urbs */
Alan Coxaf6d7802008-07-22 11:11:44 +01001022 usb_kill_urb(port->read_urb);
1023 usb_kill_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Oliver Neukum95bef012008-01-22 13:56:18 +01001025 if (!port->serial->disconnected) {
1026 if (noResponseFromAppLayer(garmin_data_p) ||
1027 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1028 process_resetdev_request(port);
1029 garmin_data_p->state = STATE_RESET;
1030 } else {
1031 garmin_data_p->state = STATE_DISCONNECTED;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 } else {
1034 garmin_data_p->state = STATE_DISCONNECTED;
1035 }
Oliver Neukum95bef012008-01-22 13:56:18 +01001036 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Alan Coxaf6d7802008-07-22 11:11:44 +01001039static void garmin_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001041 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001042 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001043 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Hermann Kneissel468d1362007-08-03 20:20:33 +02001045 if (port) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001046 struct garmin_data *garmin_data_p =
1047 usb_get_serial_port_data(port);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001048
Harvey Harrison441b62c2008-03-03 16:08:34 -08001049 dbg("%s - port %d", __func__, port->number);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001050
1051 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)
1052 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001053 gsp_send_ack(garmin_data_p,
1054 ((__u8 *)urb->transfer_buffer)[4]);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001055 }
1056
1057 if (status) {
1058 dbg("%s - nonzero write bulk status received: %d",
Greg Kroah-Hartman50de36f2008-12-10 16:00:30 -08001059 __func__, status);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001060 spin_lock_irqsave(&garmin_data_p->lock, flags);
1061 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1062 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1063 }
1064
1065 usb_serial_port_softint(port);
1066 }
1067
Alan Coxaf6d7802008-07-22 11:11:44 +01001068 /* Ignore errors that resulted from garmin_write_bulk with
1069 dismiss_ack = 1 */
Hermann Kneissel468d1362007-08-03 20:20:33 +02001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxaf6d7802008-07-22 11:11:44 +01001072 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075
Alan Coxaf6d7802008-07-22 11:11:44 +01001076static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001077 const unsigned char *buf, int count,
1078 int dismiss_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001080 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001082 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct urb *urb;
1084 unsigned char *buffer;
1085 int status;
1086
Harvey Harrison441b62c2008-03-03 16:08:34 -08001087 dbg("%s - port %d, state %d", __func__, port->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 garmin_data_p->state);
1089
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001090 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001092 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Alan Coxaf6d7802008-07-22 11:11:44 +01001094 buffer = kmalloc(count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!buffer) {
1096 dev_err(&port->dev, "out of memory\n");
1097 return -ENOMEM;
1098 }
1099
1100 urb = usb_alloc_urb(0, GFP_ATOMIC);
1101 if (!urb) {
1102 dev_err(&port->dev, "no more free urbs\n");
Alan Coxaf6d7802008-07-22 11:11:44 +01001103 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return -ENOMEM;
1105 }
1106
Alan Coxaf6d7802008-07-22 11:11:44 +01001107 memcpy(buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Harvey Harrison441b62c2008-03-03 16:08:34 -08001109 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Alan Coxaf6d7802008-07-22 11:11:44 +01001111 usb_fill_bulk_urb(urb, serial->dev,
1112 usb_sndbulkpipe(serial->dev,
1113 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 buffer, count,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001115 garmin_write_bulk_callback,
1116 dismiss_ack ? NULL : port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 urb->transfer_flags |= URB_ZERO_PACKET;
1118
1119 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
Hermann Kneissel468d1362007-08-03 20:20:33 +02001120 atomic_inc(&garmin_data_p->req_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1122 pkt_clear(garmin_data_p);
1123 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1124 }
1125 }
1126
1127 /* send it down the pipe */
1128 status = usb_submit_urb(urb, GFP_ATOMIC);
1129 if (status) {
1130 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001131 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001132 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 count = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 /* we are done with this urb, so let the host driver
1137 * really free it when it is finished with it */
Alan Coxaf6d7802008-07-22 11:11:44 +01001138 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 return count;
1141}
1142
Alan Coxaf6d7802008-07-22 11:11:44 +01001143static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +01001144 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
1146 int pktid, pktsiz, len;
Alan Coxaf6d7802008-07-22 11:11:44 +01001147 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1149
Harvey Harrison441b62c2008-03-03 16:08:34 -08001150 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* check for our private packets */
1153 if (count >= GARMIN_PKTHDR_LENGTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 len = PRIVPKTSIZ;
1155 if (count < len)
1156 len = count;
1157
1158 memcpy(garmin_data_p->privpkt, buf, len);
1159
1160 pktsiz = getDataLength(garmin_data_p->privpkt);
1161 pktid = getPacketId(garmin_data_p->privpkt);
1162
1163 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
Alan Coxaf6d7802008-07-22 11:11:44 +01001164 && GARMIN_LAYERID_PRIVATE ==
1165 getLayerId(garmin_data_p->privpkt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 dbg("%s - processing private request %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001168 __func__, pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Alan Coxaf6d7802008-07-22 11:11:44 +01001170 /* drop all unfinished transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 garmin_clear(garmin_data_p);
1172
Alan Coxaf6d7802008-07-22 11:11:44 +01001173 switch (pktid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 case PRIV_PKTID_SET_DEBUG:
1176 if (pktsiz != 4)
1177 return -EINVPKT;
1178 debug = __le32_to_cpu(privpkt[3]);
1179 dbg("%s - debug level set to 0x%X",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001180 __func__, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182
1183 case PRIV_PKTID_SET_MODE:
1184 if (pktsiz != 4)
1185 return -EINVPKT;
1186 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1187 dbg("%s - mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001188 __func__, garmin_data_p->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 break;
1190
1191 case PRIV_PKTID_INFO_REQ:
1192 priv_status_resp(port);
1193 break;
1194
1195 case PRIV_PKTID_RESET_REQ:
Hermann Kneissel468d1362007-08-03 20:20:33 +02001196 atomic_inc(&garmin_data_p->req_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198
1199 case PRIV_PKTID_SET_DEF_MODE:
1200 if (pktsiz != 4)
1201 return -EINVPKT;
1202 initial_mode = __le32_to_cpu(privpkt[3]);
1203 dbg("%s - initial_mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001204 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 garmin_data_p->mode);
1206 break;
1207 }
1208 return count;
1209 }
1210 }
1211
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001212 garmin_data_p->ignorePkts = 0;
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1215 return gsp_receive(garmin_data_p, buf, count);
1216 } else { /* MODE_NATIVE */
1217 return nat_receive(garmin_data_p, buf, count);
1218 }
1219}
1220
1221
Alan Cox95da3102008-07-22 11:09:07 +01001222static int garmin_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Alan Cox95da3102008-07-22 11:09:07 +01001224 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Report back the bytes currently available in the output buffer.
1227 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001228 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1230}
1231
1232
Alan Coxaf6d7802008-07-22 11:11:44 +01001233static void garmin_read_process(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 unsigned char *data, unsigned data_length)
1235{
1236 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1237 /* abort-transfer cmd is actice */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001238 dbg("%s - pkt dropped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
Alan Coxaf6d7802008-07-22 11:11:44 +01001240 garmin_data_p->state != STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* remember any appl.layer packets, so we know
1243 if a reset is required or not when closing
1244 the device */
1245 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001246 sizeof(GARMIN_APP_LAYER_REPLY))) {
Hermann Kneissel468d1362007-08-03 20:20:33 +02001247 atomic_inc(&garmin_data_p->resp_count);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* if throttling is active or postprecessing is required
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001251 put the received data in the input queue, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 send it directly to the tty port */
1253 if (garmin_data_p->flags & FLAGS_QUEUING) {
1254 pkt_add(garmin_data_p, data, data_length);
1255 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001256 if (getLayerId(data) == GARMIN_LAYERID_APPL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 pkt_add(garmin_data_p, data, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 } else {
1259 send_to_tty(garmin_data_p->port, data, data_length);
1260 }
1261 }
1262}
1263
1264
Alan Coxaf6d7802008-07-22 11:11:44 +01001265static void garmin_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001267 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001268 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001270 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001272 int status = urb->status;
1273 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Harvey Harrison441b62c2008-03-03 16:08:34 -08001275 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 if (!serial) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001278 dbg("%s - bad serial pointer, exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return;
1280 }
1281
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001282 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001284 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return;
1286 }
1287
Alan Coxaf6d7802008-07-22 11:11:44 +01001288 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -08001289 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 garmin_read_process(garmin_data_p, data, urb->actual_length);
1292
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001293 if (urb->actual_length == 0 &&
1294 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1295 spin_lock_irqsave(&garmin_data_p->lock, flags);
1296 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1297 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001298 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1299 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 dev_err(&port->dev,
1301 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001302 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001303 } else if (urb->actual_length > 0) {
1304 /* Continue trying to read until nothing more is received */
1305 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001306 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1307 if (retval)
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001308 dev_err(&port->dev,
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001309 "%s - failed resubmitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001310 "error %d\n", __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001311 }
1312 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001313 dbg("%s - end of bulk data", __func__);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001314 spin_lock_irqsave(&garmin_data_p->lock, flags);
1315 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1316 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 return;
1319}
1320
1321
Alan Coxaf6d7802008-07-22 11:11:44 +01001322static void garmin_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001324 unsigned long flags;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001325 int retval;
Ming Leicdc97792008-02-24 18:41:47 +08001326 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001328 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001330 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001332 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 case 0:
1334 /* success */
1335 break;
1336 case -ECONNRESET:
1337 case -ENOENT:
1338 case -ESHUTDOWN:
1339 /* this urb is terminated, clean up */
1340 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001341 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return;
1343 default:
1344 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001345 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return;
1347 }
1348
Harvey Harrison441b62c2008-03-03 16:08:34 -08001349 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 urb->actual_length, urb->transfer_buffer);
1351
1352 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1353 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001354 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Harvey Harrison441b62c2008-03-03 16:08:34 -08001356 dbg("%s - bulk data available.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001358 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1359
1360 /* bulk data available */
Alan Coxaf6d7802008-07-22 11:11:44 +01001361 usb_fill_bulk_urb(port->read_urb, serial->dev,
1362 usb_rcvbulkpipe(serial->dev,
1363 port->bulk_in_endpointAddress),
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001364 port->read_urb->transfer_buffer,
1365 port->read_urb->transfer_buffer_length,
1366 garmin_read_bulk_callback, port);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001367 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1368 if (retval) {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001369 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001370 "%s - failed submitting read urb, error %d\n",
1371 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001372 } else {
1373 spin_lock_irqsave(&garmin_data_p->lock, flags);
1374 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1375 /* do not send this packet to the user */
1376 garmin_data_p->ignorePkts = 1;
Alan Coxaf6d7802008-07-22 11:11:44 +01001377 spin_unlock_irqrestore(&garmin_data_p->lock,
1378 flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001379 }
1380 } else {
1381 /* bulk-in transfer still active */
1382 spin_lock_irqsave(&garmin_data_p->lock, flags);
1383 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1384 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
1387 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1388 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001389 sizeof(GARMIN_START_SESSION_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001391 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001393 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* save the serial number */
Alan Coxaf6d7802008-07-22 11:11:44 +01001396 garmin_data_p->serial_num = __le32_to_cpup(
1397 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 dbg("%s - start-of-session reply seen - serial %u.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001400 __func__, garmin_data_p->serial_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 if (garmin_data_p->ignorePkts) {
1404 /* this reply belongs to a request generated by the driver,
1405 ignore it. */
1406 dbg("%s - pkt ignored (%d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001407 __func__, garmin_data_p->ignorePkts);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001408 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 garmin_data_p->ignorePkts--;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001410 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 } else {
1412 garmin_read_process(garmin_data_p, data, urb->actual_length);
1413 }
1414
1415 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxaf6d7802008-07-22 11:11:44 +01001416 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001417 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 dev_err(&urb->dev->dev,
1419 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001420 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423
1424/*
1425 * Sends the next queued packt to the tty port (garmin native mode only)
1426 * and then sets a timer to call itself again until all queued data
1427 * is sent.
1428 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001429static int garmin_flush_queue(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001431 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct garmin_packet *pkt;
1433
1434 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1435 pkt = pkt_pop(garmin_data_p);
1436 if (pkt != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1438 kfree(pkt);
1439 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1440
1441 } else {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001442 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 garmin_data_p->flags &= ~FLAGS_QUEUING;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001444 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 }
1447 return 0;
1448}
1449
1450
Alan Cox95da3102008-07-22 11:09:07 +01001451static void garmin_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Alan Cox95da3102008-07-22 11:09:07 +01001453 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001454 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001455 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Harvey Harrison441b62c2008-03-03 16:08:34 -08001457 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /* set flag, data received will be put into a queue
1459 for later processing */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001460 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001462 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
1465
Alan Coxaf6d7802008-07-22 11:11:44 +01001466static void garmin_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Alan Cox95da3102008-07-22 11:09:07 +01001468 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001469 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001470 unsigned long flags;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001471 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Harvey Harrison441b62c2008-03-03 16:08:34 -08001473 dbg("%s - port %d", __func__, port->number);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001474 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 garmin_data_p->flags &= ~FLAGS_THROTTLED;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001476 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 /* in native mode send queued data to tty, in
1479 serial mode nothing needs to be done here */
1480 if (garmin_data_p->mode == MODE_NATIVE)
1481 garmin_flush_queue(garmin_data_p);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001482
1483 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1484 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1485 if (status)
1486 dev_err(&port->dev,
1487 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001488 __func__, status);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492/*
1493 * The timer is currently only used to send queued packets to
1494 * the tty in cases where the protocol provides no own handshaking
1495 * to initiate the transfer.
1496 */
1497static void timeout_handler(unsigned long data)
1498{
1499 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1500
1501 /* send the next queued packet to the tty port */
1502 if (garmin_data_p->mode == MODE_NATIVE)
1503 if (garmin_data_p->flags & FLAGS_QUEUING)
1504 garmin_flush_queue(garmin_data_p);
1505}
1506
1507
1508
Alan Cox95da3102008-07-22 11:09:07 +01001509static int garmin_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int status = 0;
1512 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001513 struct garmin_data *garmin_data_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Harvey Harrison441b62c2008-03-03 16:08:34 -08001515 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Burman Yan7ac9da12006-11-22 20:54:38 +02001517 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (garmin_data_p == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001519 dev_err(&port->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -ENOMEM;
1521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 init_timer(&garmin_data_p->timer);
1523 spin_lock_init(&garmin_data_p->lock);
1524 INIT_LIST_HEAD(&garmin_data_p->pktlist);
Alan Coxaf6d7802008-07-22 11:11:44 +01001525 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1527 garmin_data_p->timer.function = timeout_handler;
1528 garmin_data_p->port = port;
1529 garmin_data_p->state = 0;
1530 garmin_data_p->count = 0;
1531 usb_set_serial_port_data(port, garmin_data_p);
1532
1533 status = garmin_init_session(port);
1534
1535 return status;
1536}
1537
1538
Alan Cox95da3102008-07-22 11:09:07 +01001539static void garmin_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001542 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Harvey Harrison441b62c2008-03-03 16:08:34 -08001544 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Alan Coxaf6d7802008-07-22 11:11:44 +01001546 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 del_timer_sync(&garmin_data_p->timer);
Alan Coxaf6d7802008-07-22 11:11:44 +01001548 kfree(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 usb_set_serial_port_data(port, NULL);
1550}
1551
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001554static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001555 .driver = {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001556 .owner = THIS_MODULE,
1557 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001558 },
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001559 .description = "Garmin GPS usb/tty",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001560 .usb_driver = &garmin_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 .num_ports = 1,
1563 .open = garmin_open,
1564 .close = garmin_close,
1565 .throttle = garmin_throttle,
1566 .unthrottle = garmin_unthrottle,
1567 .attach = garmin_attach,
1568 .shutdown = garmin_shutdown,
1569 .write = garmin_write,
1570 .write_room = garmin_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 .write_bulk_callback = garmin_write_bulk_callback,
1572 .read_bulk_callback = garmin_read_bulk_callback,
1573 .read_int_callback = garmin_read_int_callback,
1574};
1575
1576
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001577
Alan Coxaf6d7802008-07-22 11:11:44 +01001578static int __init garmin_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 int retval;
1581
1582 retval = usb_serial_register(&garmin_device);
1583 if (retval)
1584 goto failed_garmin_register;
1585 retval = usb_register(&garmin_driver);
1586 if (retval)
1587 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001588 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1589 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 return 0;
1592failed_usb_register:
1593 usb_serial_deregister(&garmin_device);
1594failed_garmin_register:
1595 return retval;
1596}
1597
1598
Alan Coxaf6d7802008-07-22 11:11:44 +01001599static void __exit garmin_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Alan Coxaf6d7802008-07-22 11:11:44 +01001601 usb_deregister(&garmin_driver);
1602 usb_serial_deregister(&garmin_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605
1606
1607
1608module_init(garmin_init);
1609module_exit(garmin_exit);
1610
Alan Coxaf6d7802008-07-22 11:11:44 +01001611MODULE_AUTHOR(DRIVER_AUTHOR);
1612MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613MODULE_LICENSE("GPL");
1614
1615module_param(debug, bool, S_IWUSR | S_IRUGO);
1616MODULE_PARM_DESC(debug, "Debug enabled or not");
1617module_param(initial_mode, int, S_IRUGO);
1618MODULE_PARM_DESC(initial_mode, "Initial mode");
1619