blob: 2e663f1afd5e4ea85fa170f2d387f06d817bac5a [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
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +020041#include <linux/version.h>
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* the mode to be set when the port ist opened */
44static int initial_mode = 1;
45
46/* debug flag */
Alan Coxaf6d7802008-07-22 11:11:44 +010047static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define GARMIN_VENDOR_ID 0x091E
50
51/*
52 * Version Information
53 */
54
55#define VERSION_MAJOR 0
Hermann Kneissel468d1362007-08-03 20:20:33 +020056#define VERSION_MINOR 31
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define _STR(s) #s
Alan Coxaf6d7802008-07-22 11:11:44 +010059#define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
61#define DRIVER_AUTHOR "hermann kneissel"
62#define DRIVER_DESC "garmin gps driver"
63
64/* error codes returned by the driver */
65#define EINVPKT 1000 /* invalid packet structure */
66
67
Alan Coxaf6d7802008-07-22 11:11:44 +010068/* size of the header of a packet using the usb protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define GARMIN_PKTHDR_LENGTH 12
70
Alan Coxaf6d7802008-07-22 11:11:44 +010071/* max. possible size of a packet using the serial protocol */
72#define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Alan Coxaf6d7802008-07-22 11:11:44 +010074/* max. possible size of a packet with worst case stuffing */
75#define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Alan Coxaf6d7802008-07-22 11:11:44 +010077/* size of a buffer able to hold a complete (no stuffing) packet
78 * (the document protocol does not contain packets with a larger
79 * size, but in theory a packet may be 64k+12 bytes - if in
80 * later protocol versions larger packet sizes occur, this value
81 * should be increased accordingly, so the input buffer is always
82 * large enough the store a complete packet inclusive header) */
83#define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Alan Coxaf6d7802008-07-22 11:11:44 +010085/* size of a buffer able to hold a complete (incl. stuffing) packet */
86#define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Alan Coxaf6d7802008-07-22 11:11:44 +010088/* where to place the packet id of a serial packet, so we can
89 * prepend the usb-packet header without the need to move the
90 * packets data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
92
Alan Coxaf6d7802008-07-22 11:11:44 +010093/* max. size of incoming private packets (header+1 param) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
95
96#define GARMIN_LAYERID_TRANSPORT 0
97#define GARMIN_LAYERID_APPL 20
Alan Coxaf6d7802008-07-22 11:11:44 +010098/* our own layer-id to use for some control mechanisms */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define GARMIN_LAYERID_PRIVATE 0x01106E4B
100
101#define GARMIN_PKTID_PVT_DATA 51
102#define GARMIN_PKTID_L001_COMMAND_DATA 10
103
104#define CMND_ABORT_TRANSFER 0
105
Alan Coxaf6d7802008-07-22 11:11:44 +0100106/* packet ids used in private layer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#define PRIV_PKTID_SET_DEBUG 1
108#define PRIV_PKTID_SET_MODE 2
109#define PRIV_PKTID_INFO_REQ 3
110#define PRIV_PKTID_INFO_RESP 4
111#define PRIV_PKTID_RESET_REQ 5
112#define PRIV_PKTID_SET_DEF_MODE 6
113
114
115#define ETX 0x03
116#define DLE 0x10
117#define ACK 0x06
118#define NAK 0x15
119
120/* structure used to queue incoming packets */
121struct garmin_packet {
122 struct list_head list;
123 int seq;
Alan Coxaf6d7802008-07-22 11:11:44 +0100124 /* the real size of the data array, always > 0 */
125 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 __u8 data[1];
127};
128
129/* structure used to keep the current state of the driver */
130struct garmin_data {
131 __u8 state;
132 __u16 flags;
133 __u8 mode;
134 __u8 ignorePkts;
135 __u8 count;
136 __u8 pkt_id;
137 __u32 serial_num;
138 struct timer_list timer;
139 struct usb_serial_port *port;
140 int seq_counter;
141 int insize;
142 int outsize;
143 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
144 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
145 __u8 privpkt[4*6];
Hermann Kneissel468d1362007-08-03 20:20:33 +0200146 atomic_t req_count;
147 atomic_t resp_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 spinlock_t lock;
149 struct list_head pktlist;
150};
151
152
153#define STATE_NEW 0
154#define STATE_INITIAL_DELAY 1
155#define STATE_TIMEOUT 2
156#define STATE_SESSION_REQ1 3
157#define STATE_SESSION_REQ2 4
158#define STATE_ACTIVE 5
159
160#define STATE_RESET 8
161#define STATE_DISCONNECTED 9
162#define STATE_WAIT_TTY_ACK 10
163#define STATE_GSP_WAIT_DATA 11
164
165#define MODE_NATIVE 0
166#define MODE_GARMIN_SERIAL 1
167
Alan Coxaf6d7802008-07-22 11:11:44 +0100168/* Flags used in garmin_data.flags: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#define FLAGS_SESSION_REPLY_MASK 0x00C0
170#define FLAGS_SESSION_REPLY1_SEEN 0x0080
171#define FLAGS_SESSION_REPLY2_SEEN 0x0040
172#define FLAGS_BULK_IN_ACTIVE 0x0020
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200173#define FLAGS_BULK_IN_RESTART 0x0010
174#define FLAGS_THROTTLED 0x0008
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define CLEAR_HALT_REQUIRED 0x0001
176
177#define FLAGS_QUEUING 0x0100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#define FLAGS_DROP_DATA 0x0800
179
180#define FLAGS_GSP_SKIP 0x1000
181#define FLAGS_GSP_DLESEEN 0x2000
182
183
184
185
186
187
188/* function prototypes */
Alan Coxaf6d7802008-07-22 11:11:44 +0100189static void gsp_next_packet(struct garmin_data *garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200191 const unsigned char *buf, int count,
192 int dismiss_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/* some special packets to be send or received */
195static unsigned char const GARMIN_START_SESSION_REQ[]
196 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
197static unsigned char const GARMIN_START_SESSION_REQ2[]
198 = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 };
199static unsigned char const GARMIN_START_SESSION_REPLY[]
200 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
201static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[]
202 = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 };
203static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
204 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
205static unsigned char const GARMIN_APP_LAYER_REPLY[]
206 = { 0x14, 0, 0, 0 };
207static unsigned char const GARMIN_START_PVT_REQ[]
208 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
209static unsigned char const GARMIN_STOP_PVT_REQ[]
210 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
211static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
212 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
213static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
214 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
215static unsigned char const PRIVATE_REQ[]
216 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
217
218
219
220static struct usb_device_id id_table [] = {
Alan Coxaf6d7802008-07-22 11:11:44 +0100221 /* the same device id seems to be used by all
222 usb enabled GPS devices */
223 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 { } /* Terminating entry */
225};
226
Alan Coxaf6d7802008-07-22 11:11:44 +0100227MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229static struct usb_driver garmin_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .name = "garmin_gps",
231 .probe = usb_serial_probe,
232 .disconnect = usb_serial_disconnect,
233 .id_table = id_table,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200234 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237
Alan Coxaf6d7802008-07-22 11:11:44 +0100238static inline int noResponseFromAppLayer(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Alan Coxaf6d7802008-07-22 11:11:44 +0100240 return atomic_read(&garmin_data_p->req_count) ==
241 atomic_read(&garmin_data_p->resp_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244
245static inline int getLayerId(const __u8 *usbPacket)
246{
247 return __le32_to_cpup((__le32 *)(usbPacket));
248}
249
250static inline int getPacketId(const __u8 *usbPacket)
251{
252 return __le32_to_cpup((__le32 *)(usbPacket+4));
253}
254
255static inline int getDataLength(const __u8 *usbPacket)
256{
257 return __le32_to_cpup((__le32 *)(usbPacket+8));
258}
259
260
261/*
262 * check if the usb-packet in buf contains an abort-transfer command.
263 * (if yes, all queued data will be dropped)
264 */
265static inline int isAbortTrfCmnd(const unsigned char *buf)
266{
Alan Coxaf6d7802008-07-22 11:11:44 +0100267 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
268 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
269 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
270 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 1;
272 else
273 return 0;
274}
275
276
277
278static void send_to_tty(struct usb_serial_port *port,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200279 char *data, unsigned int actual_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Alan Cox95da3102008-07-22 11:09:07 +0100281 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (tty && actual_length) {
284
Alan Coxaf6d7802008-07-22 11:11:44 +0100285 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800286 __func__, actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Alan Cox33f0f882006-01-09 20:54:13 -0800288 tty_buffer_request_room(tty, actual_length);
289 tty_insert_flip_string(tty, data, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 tty_flip_buffer_push(tty);
291 }
292}
293
294
295/******************************************************************************
296 * packet queue handling
297 ******************************************************************************/
298
299/*
300 * queue a received (usb-)packet for later processing
301 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100302static int pkt_add(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200303 unsigned char *data, unsigned int data_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200305 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int result = 0;
307 unsigned long flags;
308 struct garmin_packet *pkt;
309
310 /* process only packets containg data ... */
311 if (data_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
Alan Coxaf6d7802008-07-22 11:11:44 +0100313 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (pkt == NULL) {
315 dev_err(&garmin_data_p->port->dev, "out of memory\n");
316 return 0;
317 }
318 pkt->size = data_length;
319 memcpy(pkt->data, data, data_length);
320
321 spin_lock_irqsave(&garmin_data_p->lock, flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200322 garmin_data_p->flags |= FLAGS_QUEUING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 result = list_empty(&garmin_data_p->pktlist);
324 pkt->seq = garmin_data_p->seq_counter++;
325 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200326 state = garmin_data_p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
328
329 /* in serial mode, if someone is waiting for data from
330 the device, iconvert and send the next packet to tty. */
Alan Coxaf6d7802008-07-22 11:11:44 +0100331 if (result && (state == STATE_GSP_WAIT_DATA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 return result;
335}
336
337
338/* get the next pending packet */
Alan Coxaf6d7802008-07-22 11:11:44 +0100339static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 unsigned long flags;
342 struct garmin_packet *result = NULL;
343
344 spin_lock_irqsave(&garmin_data_p->lock, flags);
345 if (!list_empty(&garmin_data_p->pktlist)) {
346 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
347 list_del(&result->list);
348 }
349 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
350 return result;
351}
352
353
354/* free up all queued data */
Alan Coxaf6d7802008-07-22 11:11:44 +0100355static void pkt_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 unsigned long flags;
358 struct garmin_packet *result = NULL;
359
Harvey Harrison441b62c2008-03-03 16:08:34 -0800360 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 spin_lock_irqsave(&garmin_data_p->lock, flags);
363 while (!list_empty(&garmin_data_p->pktlist)) {
364 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
365 list_del(&result->list);
366 kfree(result);
367 }
368 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
369}
370
371
372/******************************************************************************
373 * garmin serial protocol handling handling
374 ******************************************************************************/
375
376/* send an ack packet back to the tty */
Alan Coxaf6d7802008-07-22 11:11:44 +0100377static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 __u8 pkt[10];
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200380 __u8 cksum = 0;
381 __u8 *ptr = pkt;
382 unsigned l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Harvey Harrison441b62c2008-03-03 16:08:34 -0800384 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 *ptr++ = DLE;
387 *ptr++ = ACK;
388 cksum += ACK;
389
390 *ptr++ = 2;
391 cksum += 2;
392
393 *ptr++ = pkt_id;
394 cksum += pkt_id;
395
Alan Coxaf6d7802008-07-22 11:11:44 +0100396 if (pkt_id == DLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *ptr++ = DLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 *ptr++ = 0;
400 *ptr++ = 0xFF & (-cksum);
401 *ptr++ = DLE;
402 *ptr++ = ETX;
403
404 l = ptr-pkt;
405
406 send_to_tty(garmin_data_p->port, pkt, l);
407 return 0;
408}
409
410
411
412/*
413 * called for a complete packet received from tty layer
414 *
415 * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting
416 * at GSP_INITIAL_OFFSET.
417 *
418 * count - number of bytes in the input buffer including space reserved for
Alan Coxaf6d7802008-07-22 11:11:44 +0100419 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * (including pkt-id, data-length a. cksum)
421 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100422static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Alan Coxaf6d7802008-07-22 11:11:44 +0100424 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200425 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 int cksum = 0;
428 int n = 0;
429 int pktid = recpkt[0];
430 int size = recpkt[1];
431
432 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800433 __func__, count-GSP_INITIAL_OFFSET, recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (size != (count-GSP_INITIAL_OFFSET-3)) {
436 dbg("%s - invalid size, expected %d bytes, got %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 __func__, size, (count-GSP_INITIAL_OFFSET-3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -EINVPKT;
439 }
440
441 cksum += *recpkt++;
442 cksum += *recpkt++;
443
Alan Coxaf6d7802008-07-22 11:11:44 +0100444 /* sanity check, remove after test ... */
445 if ((__u8 *)&(usbdata[3]) != recpkt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 dbg("%s - ptr mismatch %p - %p",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800447 __func__, &(usbdata[4]), recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -EINVPKT;
449 }
450
451 while (n < size) {
452 cksum += *recpkt++;
453 n++;
454 }
455
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200456 if ((0xff & (cksum + *recpkt)) != 0) {
457 dbg("%s - invalid checksum, expected %02x, got %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800458 __func__, 0xff & -cksum, 0xff & *recpkt);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200459 return -EINVPKT;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
463 usbdata[1] = __cpu_to_le32(pktid);
464 usbdata[2] = __cpu_to_le32(size);
465
Alan Coxaf6d7802008-07-22 11:11:44 +0100466 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200467 GARMIN_PKTHDR_LENGTH+size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /* if this was an abort-transfer command, flush all
470 queued data. */
471 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
472 garmin_data_p->flags |= FLAGS_DROP_DATA;
473 pkt_clear(garmin_data_p);
474 }
475
476 return count;
477}
478
479
480
481/*
482 * Called for data received from tty
483 *
484 * buf contains the data read, it may span more than one packet or even
485 * incomplete packets
486 *
487 * input record should be a serial-record, but it may not be complete.
488 * Copy it into our local buffer, until an etx is seen (or an error
489 * occurs).
490 * Once the record is complete, convert into a usb packet and send it
491 * to the bulk pipe, send an ack back to the tty.
492 *
493 * If the input is an ack, just send the last queued packet to the
494 * tty layer.
495 *
496 * if the input is an abort command, drop all queued data.
497 */
498
Alan Coxaf6d7802008-07-22 11:11:44 +0100499static int gsp_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200500 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200502 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int offs = 0;
504 int ack_or_nak_seen = 0;
505 int i = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200506 __u8 *dest;
507 int size;
Alan Coxaf6d7802008-07-22 11:11:44 +0100508 /* dleSeen: set if last byte read was a DLE */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200509 int dleSeen;
Alan Coxaf6d7802008-07-22 11:11:44 +0100510 /* skip: if set, skip incoming data until possible start of
511 * new packet
512 */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200513 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 __u8 data;
515
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200516 spin_lock_irqsave(&garmin_data_p->lock, flags);
517 dest = garmin_data_p->inbuffer;
518 size = garmin_data_p->insize;
519 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
520 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
521 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 dbg("%s - dle=%d skip=%d size=%d count=%d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800524 __func__, dleSeen, skip, size, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Alan Coxaf6d7802008-07-22 11:11:44 +0100526 if (size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 size = GSP_INITIAL_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 while (offs < count) {
530
531 data = *(buf+offs);
Alan Coxaf6d7802008-07-22 11:11:44 +0100532 offs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 if (data == DLE) {
535 if (skip) { /* start of a new pkt */
536 skip = 0;
537 size = GSP_INITIAL_OFFSET;
538 dleSeen = 1;
539 } else if (dleSeen) {
540 dest[size++] = data;
541 dleSeen = 0;
542 } else {
543 dleSeen = 1;
544 }
545 } else if (data == ETX) {
546 if (dleSeen) {
547 /* packet complete */
548
549 data = dest[GSP_INITIAL_OFFSET];
550
551 if (data == ACK) {
552 ack_or_nak_seen = ACK;
553 dbg("ACK packet complete.");
554 } else if (data == NAK) {
555 ack_or_nak_seen = NAK;
556 dbg("NAK packet complete.");
557 } else {
Alan Coxaf6d7802008-07-22 11:11:44 +0100558 dbg("packet complete - id=0x%X.",
559 0xFF & data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 gsp_rec_packet(garmin_data_p, size);
561 }
562
563 skip = 1;
564 size = GSP_INITIAL_OFFSET;
565 dleSeen = 0;
566 } else {
567 dest[size++] = data;
568 }
569 } else if (!skip) {
570
571 if (dleSeen) {
572 dbg("non-masked DLE at %d - restarting", i);
573 size = GSP_INITIAL_OFFSET;
574 dleSeen = 0;
575 }
576
577 dest[size++] = data;
578 }
579
580 if (size >= GPS_IN_BUFSIZ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800581 dbg("%s - packet too large.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 skip = 1;
583 size = GSP_INITIAL_OFFSET;
584 dleSeen = 0;
585 }
586 }
587
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200588 spin_lock_irqsave(&garmin_data_p->lock, flags);
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 garmin_data_p->insize = size;
591
Alan Coxaf6d7802008-07-22 11:11:44 +0100592 /* copy flags back to structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (skip)
594 garmin_data_p->flags |= FLAGS_GSP_SKIP;
595 else
596 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
597
598 if (dleSeen)
599 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
600 else
601 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
602
Alan Coxaf6d7802008-07-22 11:11:44 +0100603 if (ack_or_nak_seen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 garmin_data_p->state = STATE_GSP_WAIT_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200605
606 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
607
Alan Coxaf6d7802008-07-22 11:11:44 +0100608 if (ack_or_nak_seen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return count;
611}
612
613
614
615
616/*
617 * Sends a usb packet to the tty
618 *
619 * Assumes, that all packages and at an usb-packet boundary.
620 *
621 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
622 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100623static int gsp_send(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 const unsigned char *buf, int count)
625{
626 const unsigned char *src;
627 unsigned char *dst;
628 int pktid = 0;
629 int datalen = 0;
630 int cksum = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100631 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int k;
633
Harvey Harrison441b62c2008-03-03 16:08:34 -0800634 dbg("%s - state %d - %d bytes.", __func__,
Alan Coxaf6d7802008-07-22 11:11:44 +0100635 garmin_data_p->state, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 k = garmin_data_p->outsize;
638 if ((k+count) > GPS_OUT_BUFSIZ) {
639 dbg("packet too large");
640 garmin_data_p->outsize = 0;
641 return -4;
642 }
643
644 memcpy(garmin_data_p->outbuffer+k, buf, count);
645 k += count;
646 garmin_data_p->outsize = k;
647
648 if (k >= GARMIN_PKTHDR_LENGTH) {
649 pktid = getPacketId(garmin_data_p->outbuffer);
Alan Coxaf6d7802008-07-22 11:11:44 +0100650 datalen = getDataLength(garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 i = GARMIN_PKTHDR_LENGTH + datalen;
652 if (k < i)
653 return 0;
654 } else {
655 return 0;
656 }
657
Alan Coxaf6d7802008-07-22 11:11:44 +0100658 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* garmin_data_p->outbuffer now contains a complete packet */
661
662 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100663 __func__, k, garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 garmin_data_p->outsize = 0;
666
667 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100668 dbg("not an application packet (%d)",
669 getLayerId(garmin_data_p->outbuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -1;
671 }
672
673 if (pktid > 255) {
674 dbg("packet-id %d too large", pktid);
675 return -2;
676 }
677
678 if (datalen > 255) {
679 dbg("packet-size %d too large", datalen);
680 return -3;
681 }
682
683 /* the serial protocol should be able to handle this packet */
684
685 k = 0;
686 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
Alan Coxaf6d7802008-07-22 11:11:44 +0100687 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (*src++ == DLE)
689 k++;
690 }
691
692 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
693 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100694 /* can't add stuffing DLEs in place, move data to end
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200695 of buffer ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
697 memcpy(dst, src, datalen);
698 src = dst;
699 }
700
701 dst = garmin_data_p->outbuffer;
702
703 *dst++ = DLE;
704 *dst++ = pktid;
705 cksum += pktid;
706 *dst++ = datalen;
707 cksum += datalen;
708 if (datalen == DLE)
709 *dst++ = DLE;
710
Alan Coxaf6d7802008-07-22 11:11:44 +0100711 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 __u8 c = *src++;
713 *dst++ = c;
714 cksum += c;
715 if (c == DLE)
716 *dst++ = DLE;
717 }
Alan Coxaf6d7802008-07-22 11:11:44 +0100718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 cksum = 0xFF & -cksum;
720 *dst++ = cksum;
721 if (cksum == DLE)
722 *dst++ = DLE;
723 *dst++ = DLE;
724 *dst++ = ETX;
725
726 i = dst-garmin_data_p->outbuffer;
727
728 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
729
730 garmin_data_p->pkt_id = pktid;
731 garmin_data_p->state = STATE_WAIT_TTY_ACK;
732
733 return i;
734}
735
736
737
738
739
740/*
741 * Process the next pending data packet - if there is one
742 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100743static void gsp_next_packet(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
745 struct garmin_packet *pkt = NULL;
746
747 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800748 dbg("%s - next pkt: %d", __func__, pkt->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) {
750 kfree(pkt);
751 return;
752 }
753 kfree(pkt);
754 }
755}
756
757
758
759
760/******************************************************************************
761 * garmin native mode
762 ******************************************************************************/
763
764
765/*
766 * Called for data received from tty
767 *
768 * The input data is expected to be in garmin usb-packet format.
769 *
770 * buf contains the data read, it may span more than one packet
771 * or even incomplete packets
772 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100773static int nat_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200774 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200776 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100777 __u8 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int offs = 0;
779 int result = count;
780 int len;
781
782 while (offs < count) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100783 /* if buffer contains header, copy rest of data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
785 len = GARMIN_PKTHDR_LENGTH
786 +getDataLength(garmin_data_p->inbuffer);
787 else
788 len = GARMIN_PKTHDR_LENGTH;
789
790 if (len >= GPS_IN_BUFSIZ) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100791 /* seems to be an invalid packet, ignore rest
792 of input */
793 dbg("%s - packet size too large: %d", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 garmin_data_p->insize = 0;
795 count = 0;
796 result = -EINVPKT;
797 } else {
798 len -= garmin_data_p->insize;
799 if (len > (count-offs))
800 len = (count-offs);
801 if (len > 0) {
802 dest = garmin_data_p->inbuffer
Alan Coxaf6d7802008-07-22 11:11:44 +0100803 + garmin_data_p->insize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 memcpy(dest, buf+offs, len);
805 garmin_data_p->insize += len;
806 offs += len;
807 }
808 }
809
810 /* do we have a complete packet ? */
811 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
812 len = GARMIN_PKTHDR_LENGTH+
813 getDataLength(garmin_data_p->inbuffer);
814 if (garmin_data_p->insize >= len) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100815 garmin_write_bulk(garmin_data_p->port,
816 garmin_data_p->inbuffer,
817 len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 garmin_data_p->insize = 0;
819
820 /* if this was an abort-transfer command,
821 flush all queued data. */
822 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100823 spin_lock_irqsave(&garmin_data_p->lock,
824 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 garmin_data_p->flags |= FLAGS_DROP_DATA;
Alan Coxaf6d7802008-07-22 11:11:44 +0100826 spin_unlock_irqrestore(
827 &garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 pkt_clear(garmin_data_p);
829 }
830 }
831 }
832 }
833 return result;
834}
835
836
837/******************************************************************************
838 * private packets
839 ******************************************************************************/
840
841static void priv_status_resp(struct usb_serial_port *port)
842{
Alan Coxaf6d7802008-07-22 11:11:44 +0100843 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
845
846 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
847 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
848 pkt[2] = __cpu_to_le32(12);
849 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
850 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
851 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
852
Alan Coxaf6d7802008-07-22 11:11:44 +0100853 send_to_tty(port, (__u8 *)pkt, 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856
857/******************************************************************************
858 * Garmin specific driver functions
859 ******************************************************************************/
860
861static int process_resetdev_request(struct usb_serial_port *port)
862{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200863 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 int status;
Alan Coxaf6d7802008-07-22 11:11:44 +0100865 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200867 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
869 garmin_data_p->state = STATE_RESET;
870 garmin_data_p->serial_num = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200871 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Alan Coxaf6d7802008-07-22 11:11:44 +0100873 usb_kill_urb(port->interrupt_in_urb);
874 dbg("%s - usb_reset_device", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 status = usb_reset_device(port->serial->dev);
876 if (status)
877 dbg("%s - usb_reset_device failed: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800878 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return status;
880}
881
882
883
884/*
885 * clear all cached data
886 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100887static int garmin_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200889 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 int status = 0;
891
892 struct usb_serial_port *port = garmin_data_p->port;
893
Hermann Kneissel468d1362007-08-03 20:20:33 +0200894 if (port != NULL && atomic_read(&garmin_data_p->resp_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* send a terminate command */
896 status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100897 sizeof(GARMIN_STOP_TRANSFER_REQ), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
900 /* flush all queued data */
901 pkt_clear(garmin_data_p);
902
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200903 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 garmin_data_p->insize = 0;
905 garmin_data_p->outsize = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200906 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 return status;
909}
910
911
912
913
914
915
916static int garmin_init_session(struct usb_serial_port *port)
917{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200918 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100920 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int status = 0;
922
923 if (status == 0) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100924 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Harvey Harrison441b62c2008-03-03 16:08:34 -0800926 dbg("%s - adding interrupt input", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 port->interrupt_in_urb->dev = serial->dev;
928 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
929 if (status)
930 dev_err(&serial->dev->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100931 "%s - failed submitting interrupt urb, error %d\n",
932 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
935 if (status == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800936 dbg("%s - starting session ...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 garmin_data_p->state = STATE_ACTIVE;
938 status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100939 sizeof(GARMIN_START_SESSION_REQ), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (status >= 0) {
942
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200943 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 garmin_data_p->ignorePkts++;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200945 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* not needed, but the win32 driver does it too ... */
948 status = garmin_write_bulk(port,
Alan Coxaf6d7802008-07-22 11:11:44 +0100949 GARMIN_START_SESSION_REQ2,
950 sizeof(GARMIN_START_SESSION_REQ2), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (status >= 0) {
952 status = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200953 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 garmin_data_p->ignorePkts++;
Alan Coxaf6d7802008-07-22 11:11:44 +0100955 spin_unlock_irqrestore(&garmin_data_p->lock,
956 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 }
959 }
960
961 return status;
962}
963
964
965
966
967
Alan Coxaf6d7802008-07-22 11:11:44 +0100968static int garmin_open(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +0100969 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200971 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int status = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100973 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Harvey Harrison441b62c2008-03-03 16:08:34 -0800975 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /*
978 * Force low_latency on so that our tty_push actually forces the data
979 * through, otherwise it is scheduled, and with high data rates (like
980 * with OHCI) data can get lost.
981 */
Alan Cox95da3102008-07-22 11:09:07 +0100982 if (tty)
983 tty->low_latency = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200985 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 garmin_data_p->mode = initial_mode;
987 garmin_data_p->count = 0;
988 garmin_data_p->flags = 0;
Hermann Kneissel468d1362007-08-03 20:20:33 +0200989 atomic_set(&garmin_data_p->req_count, 0);
990 atomic_set(&garmin_data_p->resp_count, 0);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200991 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /* shutdown any bulk reads that might be going on */
Alan Coxaf6d7802008-07-22 11:11:44 +0100994 usb_kill_urb(port->write_urb);
995 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Alan Cox95da3102008-07-22 11:09:07 +0100997 if (garmin_data_p->state == STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 status = garmin_init_session(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 garmin_data_p->state = STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return status;
1002}
1003
1004
Alan Cox95da3102008-07-22 11:09:07 +01001005static void garmin_close(struct tty_struct *tty,
Alan Coxaf6d7802008-07-22 11:11:44 +01001006 struct usb_serial_port *port, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001009 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Harvey Harrison441b62c2008-03-03 16:08:34 -08001011 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 port->number, garmin_data_p->mode,
1013 garmin_data_p->state, garmin_data_p->flags);
1014
1015 if (!serial)
1016 return;
1017
Oliver Neukum95bef012008-01-22 13:56:18 +01001018 mutex_lock(&port->serial->disc_mutex);
1019 if (!port->serial->disconnected)
1020 garmin_clear(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* shutdown our urbs */
Alan Coxaf6d7802008-07-22 11:11:44 +01001023 usb_kill_urb(port->read_urb);
1024 usb_kill_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Oliver Neukum95bef012008-01-22 13:56:18 +01001026 if (!port->serial->disconnected) {
1027 if (noResponseFromAppLayer(garmin_data_p) ||
1028 ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) {
1029 process_resetdev_request(port);
1030 garmin_data_p->state = STATE_RESET;
1031 } else {
1032 garmin_data_p->state = STATE_DISCONNECTED;
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 } else {
1035 garmin_data_p->state = STATE_DISCONNECTED;
1036 }
Oliver Neukum95bef012008-01-22 13:56:18 +01001037 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
Alan Coxaf6d7802008-07-22 11:11:44 +01001040static void garmin_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001042 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001043 struct usb_serial_port *port = urb->context;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001044 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Hermann Kneissel468d1362007-08-03 20:20:33 +02001046 if (port) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001047 struct garmin_data *garmin_data_p =
1048 usb_get_serial_port_data(port);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001049
Harvey Harrison441b62c2008-03-03 16:08:34 -08001050 dbg("%s - port %d", __func__, port->number);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001051
1052 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)
1053 && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001054 gsp_send_ack(garmin_data_p,
1055 ((__u8 *)urb->transfer_buffer)[4]);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001056 }
1057
1058 if (status) {
1059 dbg("%s - nonzero write bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001060 __func__, urb->status);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001061 spin_lock_irqsave(&garmin_data_p->lock, flags);
1062 garmin_data_p->flags |= CLEAR_HALT_REQUIRED;
1063 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1064 }
1065
1066 usb_serial_port_softint(port);
1067 }
1068
Alan Coxaf6d7802008-07-22 11:11:44 +01001069 /* Ignore errors that resulted from garmin_write_bulk with
1070 dismiss_ack = 1 */
Hermann Kneissel468d1362007-08-03 20:20:33 +02001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxaf6d7802008-07-22 11:11:44 +01001073 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076
Alan Coxaf6d7802008-07-22 11:11:44 +01001077static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001078 const unsigned char *buf, int count,
1079 int dismiss_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001081 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001083 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct urb *urb;
1085 unsigned char *buffer;
1086 int status;
1087
Harvey Harrison441b62c2008-03-03 16:08:34 -08001088 dbg("%s - port %d, state %d", __func__, port->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 garmin_data_p->state);
1090
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001091 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001093 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Alan Coxaf6d7802008-07-22 11:11:44 +01001095 buffer = kmalloc(count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!buffer) {
1097 dev_err(&port->dev, "out of memory\n");
1098 return -ENOMEM;
1099 }
1100
1101 urb = usb_alloc_urb(0, GFP_ATOMIC);
1102 if (!urb) {
1103 dev_err(&port->dev, "no more free urbs\n");
Alan Coxaf6d7802008-07-22 11:11:44 +01001104 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return -ENOMEM;
1106 }
1107
Alan Coxaf6d7802008-07-22 11:11:44 +01001108 memcpy(buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Harvey Harrison441b62c2008-03-03 16:08:34 -08001110 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Alan Coxaf6d7802008-07-22 11:11:44 +01001112 usb_fill_bulk_urb(urb, serial->dev,
1113 usb_sndbulkpipe(serial->dev,
1114 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 buffer, count,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001116 garmin_write_bulk_callback,
1117 dismiss_ack ? NULL : port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 urb->transfer_flags |= URB_ZERO_PACKET;
1119
1120 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
Hermann Kneissel468d1362007-08-03 20:20:33 +02001121 atomic_inc(&garmin_data_p->req_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1123 pkt_clear(garmin_data_p);
1124 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1125 }
1126 }
1127
1128 /* send it down the pipe */
1129 status = usb_submit_urb(urb, GFP_ATOMIC);
1130 if (status) {
1131 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001132 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001133 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 count = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
1137 /* we are done with this urb, so let the host driver
1138 * really free it when it is finished with it */
Alan Coxaf6d7802008-07-22 11:11:44 +01001139 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 return count;
1142}
1143
Alan Coxaf6d7802008-07-22 11:11:44 +01001144static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +01001145 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int pktid, pktsiz, len;
Alan Coxaf6d7802008-07-22 11:11:44 +01001148 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1150
Harvey Harrison441b62c2008-03-03 16:08:34 -08001151 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 /* check for our private packets */
1154 if (count >= GARMIN_PKTHDR_LENGTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 len = PRIVPKTSIZ;
1156 if (count < len)
1157 len = count;
1158
1159 memcpy(garmin_data_p->privpkt, buf, len);
1160
1161 pktsiz = getDataLength(garmin_data_p->privpkt);
1162 pktid = getPacketId(garmin_data_p->privpkt);
1163
1164 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
Alan Coxaf6d7802008-07-22 11:11:44 +01001165 && GARMIN_LAYERID_PRIVATE ==
1166 getLayerId(garmin_data_p->privpkt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 dbg("%s - processing private request %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001169 __func__, pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Alan Coxaf6d7802008-07-22 11:11:44 +01001171 /* drop all unfinished transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 garmin_clear(garmin_data_p);
1173
Alan Coxaf6d7802008-07-22 11:11:44 +01001174 switch (pktid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 case PRIV_PKTID_SET_DEBUG:
1177 if (pktsiz != 4)
1178 return -EINVPKT;
1179 debug = __le32_to_cpu(privpkt[3]);
1180 dbg("%s - debug level set to 0x%X",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001181 __func__, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
1183
1184 case PRIV_PKTID_SET_MODE:
1185 if (pktsiz != 4)
1186 return -EINVPKT;
1187 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1188 dbg("%s - mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001189 __func__, garmin_data_p->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
1191
1192 case PRIV_PKTID_INFO_REQ:
1193 priv_status_resp(port);
1194 break;
1195
1196 case PRIV_PKTID_RESET_REQ:
Hermann Kneissel468d1362007-08-03 20:20:33 +02001197 atomic_inc(&garmin_data_p->req_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
1199
1200 case PRIV_PKTID_SET_DEF_MODE:
1201 if (pktsiz != 4)
1202 return -EINVPKT;
1203 initial_mode = __le32_to_cpu(privpkt[3]);
1204 dbg("%s - initial_mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001205 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 garmin_data_p->mode);
1207 break;
1208 }
1209 return count;
1210 }
1211 }
1212
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001213 garmin_data_p->ignorePkts = 0;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1216 return gsp_receive(garmin_data_p, buf, count);
1217 } else { /* MODE_NATIVE */
1218 return nat_receive(garmin_data_p, buf, count);
1219 }
1220}
1221
1222
Alan Cox95da3102008-07-22 11:09:07 +01001223static int garmin_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Alan Cox95da3102008-07-22 11:09:07 +01001225 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /*
1227 * Report back the bytes currently available in the output buffer.
1228 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001229 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1231}
1232
1233
Alan Coxaf6d7802008-07-22 11:11:44 +01001234static void garmin_read_process(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 unsigned char *data, unsigned data_length)
1236{
1237 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1238 /* abort-transfer cmd is actice */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001239 dbg("%s - pkt dropped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
Alan Coxaf6d7802008-07-22 11:11:44 +01001241 garmin_data_p->state != STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* remember any appl.layer packets, so we know
1244 if a reset is required or not when closing
1245 the device */
1246 if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001247 sizeof(GARMIN_APP_LAYER_REPLY))) {
Hermann Kneissel468d1362007-08-03 20:20:33 +02001248 atomic_inc(&garmin_data_p->resp_count);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* if throttling is active or postprecessing is required
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001252 put the received data in the input queue, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 send it directly to the tty port */
1254 if (garmin_data_p->flags & FLAGS_QUEUING) {
1255 pkt_add(garmin_data_p, data, data_length);
1256 } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
Alan Coxaf6d7802008-07-22 11:11:44 +01001257 if (getLayerId(data) == GARMIN_LAYERID_APPL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 pkt_add(garmin_data_p, data, data_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 } else {
1260 send_to_tty(garmin_data_p->port, data, data_length);
1261 }
1262 }
1263}
1264
1265
Alan Coxaf6d7802008-07-22 11:11:44 +01001266static void garmin_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001268 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001269 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001271 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001273 int status = urb->status;
1274 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Harvey Harrison441b62c2008-03-03 16:08:34 -08001276 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (!serial) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001279 dbg("%s - bad serial pointer, exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return;
1281 }
1282
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001283 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001285 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return;
1287 }
1288
Alan Coxaf6d7802008-07-22 11:11:44 +01001289 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -08001290 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 garmin_read_process(garmin_data_p, data, urb->actual_length);
1293
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001294 if (urb->actual_length == 0 &&
1295 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1296 spin_lock_irqsave(&garmin_data_p->lock, flags);
1297 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1298 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001299 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1300 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 dev_err(&port->dev,
1302 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001303 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001304 } else if (urb->actual_length > 0) {
1305 /* Continue trying to read until nothing more is received */
1306 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001307 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1308 if (retval)
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001309 dev_err(&port->dev,
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001310 "%s - failed resubmitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001311 "error %d\n", __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001312 }
1313 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001314 dbg("%s - end of bulk data", __func__);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001315 spin_lock_irqsave(&garmin_data_p->lock, flags);
1316 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1317 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 return;
1320}
1321
1322
Alan Coxaf6d7802008-07-22 11:11:44 +01001323static void garmin_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001325 unsigned long flags;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001326 int retval;
Ming Leicdc97792008-02-24 18:41:47 +08001327 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001329 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001331 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001333 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 case 0:
1335 /* success */
1336 break;
1337 case -ECONNRESET:
1338 case -ENOENT:
1339 case -ESHUTDOWN:
1340 /* this urb is terminated, clean up */
1341 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001342 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return;
1344 default:
1345 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001346 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return;
1348 }
1349
Harvey Harrison441b62c2008-03-03 16:08:34 -08001350 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 urb->actual_length, urb->transfer_buffer);
1352
1353 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1354 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001355 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Harvey Harrison441b62c2008-03-03 16:08:34 -08001357 dbg("%s - bulk data available.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001359 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1360
1361 /* bulk data available */
Alan Coxaf6d7802008-07-22 11:11:44 +01001362 usb_fill_bulk_urb(port->read_urb, serial->dev,
1363 usb_rcvbulkpipe(serial->dev,
1364 port->bulk_in_endpointAddress),
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001365 port->read_urb->transfer_buffer,
1366 port->read_urb->transfer_buffer_length,
1367 garmin_read_bulk_callback, port);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001368 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1369 if (retval) {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001370 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001371 "%s - failed submitting read urb, error %d\n",
1372 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001373 } else {
1374 spin_lock_irqsave(&garmin_data_p->lock, flags);
1375 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
1376 /* do not send this packet to the user */
1377 garmin_data_p->ignorePkts = 1;
Alan Coxaf6d7802008-07-22 11:11:44 +01001378 spin_unlock_irqrestore(&garmin_data_p->lock,
1379 flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001380 }
1381 } else {
1382 /* bulk-in transfer still active */
1383 spin_lock_irqsave(&garmin_data_p->lock, flags);
1384 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1385 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
1388 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1389 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001390 sizeof(GARMIN_START_SESSION_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001392 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001394 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /* save the serial number */
Alan Coxaf6d7802008-07-22 11:11:44 +01001397 garmin_data_p->serial_num = __le32_to_cpup(
1398 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 dbg("%s - start-of-session reply seen - serial %u.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001401 __func__, garmin_data_p->serial_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
1404 if (garmin_data_p->ignorePkts) {
1405 /* this reply belongs to a request generated by the driver,
1406 ignore it. */
1407 dbg("%s - pkt ignored (%d)",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001408 __func__, garmin_data_p->ignorePkts);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001409 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 garmin_data_p->ignorePkts--;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001411 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 } else {
1413 garmin_read_process(garmin_data_p, data, urb->actual_length);
1414 }
1415
1416 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxaf6d7802008-07-22 11:11:44 +01001417 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001418 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 dev_err(&urb->dev->dev,
1420 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001421 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424
1425/*
1426 * Sends the next queued packt to the tty port (garmin native mode only)
1427 * and then sets a timer to call itself again until all queued data
1428 * is sent.
1429 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001430static int garmin_flush_queue(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001432 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct garmin_packet *pkt;
1434
1435 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1436 pkt = pkt_pop(garmin_data_p);
1437 if (pkt != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1439 kfree(pkt);
1440 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1441
1442 } else {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001443 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 garmin_data_p->flags &= ~FLAGS_QUEUING;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001445 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447 }
1448 return 0;
1449}
1450
1451
Alan Cox95da3102008-07-22 11:09:07 +01001452static void garmin_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Alan Cox95da3102008-07-22 11:09:07 +01001454 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001455 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001456 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Harvey Harrison441b62c2008-03-03 16:08:34 -08001458 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* set flag, data received will be put into a queue
1460 for later processing */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001461 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001463 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466
Alan Coxaf6d7802008-07-22 11:11:44 +01001467static void garmin_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Alan Cox95da3102008-07-22 11:09:07 +01001469 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001470 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Alan Cox95da3102008-07-22 11:09:07 +01001471 unsigned long flags;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001472 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Harvey Harrison441b62c2008-03-03 16:08:34 -08001474 dbg("%s - port %d", __func__, port->number);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001475 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 garmin_data_p->flags &= ~FLAGS_THROTTLED;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001477 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 /* in native mode send queued data to tty, in
1480 serial mode nothing needs to be done here */
1481 if (garmin_data_p->mode == MODE_NATIVE)
1482 garmin_flush_queue(garmin_data_p);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001483
1484 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1485 status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1486 if (status)
1487 dev_err(&port->dev,
1488 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001489 __func__, status);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493/*
1494 * The timer is currently only used to send queued packets to
1495 * the tty in cases where the protocol provides no own handshaking
1496 * to initiate the transfer.
1497 */
1498static void timeout_handler(unsigned long data)
1499{
1500 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1501
1502 /* send the next queued packet to the tty port */
1503 if (garmin_data_p->mode == MODE_NATIVE)
1504 if (garmin_data_p->flags & FLAGS_QUEUING)
1505 garmin_flush_queue(garmin_data_p);
1506}
1507
1508
1509
Alan Cox95da3102008-07-22 11:09:07 +01001510static int garmin_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 int status = 0;
1513 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001514 struct garmin_data *garmin_data_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Harvey Harrison441b62c2008-03-03 16:08:34 -08001516 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Burman Yan7ac9da12006-11-22 20:54:38 +02001518 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (garmin_data_p == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001520 dev_err(&port->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return -ENOMEM;
1522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 init_timer(&garmin_data_p->timer);
1524 spin_lock_init(&garmin_data_p->lock);
1525 INIT_LIST_HEAD(&garmin_data_p->pktlist);
Alan Coxaf6d7802008-07-22 11:11:44 +01001526 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1528 garmin_data_p->timer.function = timeout_handler;
1529 garmin_data_p->port = port;
1530 garmin_data_p->state = 0;
1531 garmin_data_p->count = 0;
1532 usb_set_serial_port_data(port, garmin_data_p);
1533
1534 status = garmin_init_session(port);
1535
1536 return status;
1537}
1538
1539
Alan Cox95da3102008-07-22 11:09:07 +01001540static void garmin_shutdown(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001543 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Harvey Harrison441b62c2008-03-03 16:08:34 -08001545 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Alan Coxaf6d7802008-07-22 11:11:44 +01001547 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 del_timer_sync(&garmin_data_p->timer);
Alan Coxaf6d7802008-07-22 11:11:44 +01001549 kfree(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 usb_set_serial_port_data(port, NULL);
1551}
1552
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001555static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001556 .driver = {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001557 .owner = THIS_MODULE,
1558 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001559 },
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001560 .description = "Garmin GPS usb/tty",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001561 .usb_driver = &garmin_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 .num_ports = 1,
1564 .open = garmin_open,
1565 .close = garmin_close,
1566 .throttle = garmin_throttle,
1567 .unthrottle = garmin_unthrottle,
1568 .attach = garmin_attach,
1569 .shutdown = garmin_shutdown,
1570 .write = garmin_write,
1571 .write_room = garmin_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 .write_bulk_callback = garmin_write_bulk_callback,
1573 .read_bulk_callback = garmin_read_bulk_callback,
1574 .read_int_callback = garmin_read_int_callback,
1575};
1576
1577
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001578
Alan Coxaf6d7802008-07-22 11:11:44 +01001579static int __init garmin_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 int retval;
1582
1583 retval = usb_serial_register(&garmin_device);
1584 if (retval)
1585 goto failed_garmin_register;
1586 retval = usb_register(&garmin_driver);
1587 if (retval)
1588 goto failed_usb_register;
1589 info(DRIVER_DESC " " DRIVER_VERSION);
1590
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