blob: 69da3abcb3637542a1c1424e665006b2caa6817b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Garmin GPS driver
3 *
Hermann Kneisselb4026c42011-04-29 08:58:43 +02004 * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de
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>
Arun Sharma600634972011-07-26 16:09:06 -070037#include <linux/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 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103045static bool 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 Kneisselb4026c42011-04-29 08:58:43 +020054#define VERSION_MINOR 36
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 __u8 count;
133 __u8 pkt_id;
134 __u32 serial_num;
135 struct timer_list timer;
136 struct usb_serial_port *port;
137 int seq_counter;
138 int insize;
139 int outsize;
140 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
141 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
142 __u8 privpkt[4*6];
143 spinlock_t lock;
144 struct list_head pktlist;
145};
146
147
148#define STATE_NEW 0
149#define STATE_INITIAL_DELAY 1
150#define STATE_TIMEOUT 2
151#define STATE_SESSION_REQ1 3
152#define STATE_SESSION_REQ2 4
153#define STATE_ACTIVE 5
154
155#define STATE_RESET 8
156#define STATE_DISCONNECTED 9
157#define STATE_WAIT_TTY_ACK 10
158#define STATE_GSP_WAIT_DATA 11
159
160#define MODE_NATIVE 0
161#define MODE_GARMIN_SERIAL 1
162
Alan Coxaf6d7802008-07-22 11:11:44 +0100163/* Flags used in garmin_data.flags: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define FLAGS_SESSION_REPLY_MASK 0x00C0
165#define FLAGS_SESSION_REPLY1_SEEN 0x0080
166#define FLAGS_SESSION_REPLY2_SEEN 0x0040
167#define FLAGS_BULK_IN_ACTIVE 0x0020
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200168#define FLAGS_BULK_IN_RESTART 0x0010
169#define FLAGS_THROTTLED 0x0008
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200170#define APP_REQ_SEEN 0x0004
171#define APP_RESP_SEEN 0x0002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#define CLEAR_HALT_REQUIRED 0x0001
173
174#define FLAGS_QUEUING 0x0100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define FLAGS_DROP_DATA 0x0800
176
177#define FLAGS_GSP_SKIP 0x1000
178#define FLAGS_GSP_DLESEEN 0x2000
179
180
181
182
183
184
185/* function prototypes */
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200186static int gsp_next_packet(struct garmin_data *garmin_data_p);
187static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200188 const unsigned char *buf, int count,
189 int dismiss_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/* some special packets to be send or received */
192static unsigned char const GARMIN_START_SESSION_REQ[]
193 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static unsigned char const GARMIN_START_SESSION_REPLY[]
195 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
197 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
198static unsigned char const GARMIN_APP_LAYER_REPLY[]
199 = { 0x14, 0, 0, 0 };
200static unsigned char const GARMIN_START_PVT_REQ[]
201 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
202static unsigned char const GARMIN_STOP_PVT_REQ[]
203 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
204static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
205 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
206static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
207 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
208static unsigned char const PRIVATE_REQ[]
209 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
210
211
212
Németh Márton7d40d7e2010-01-10 15:34:24 +0100213static const struct usb_device_id id_table[] = {
Alan Coxaf6d7802008-07-22 11:11:44 +0100214 /* the same device id seems to be used by all
215 usb enabled GPS devices */
216 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 { } /* Terminating entry */
218};
219
Alan Coxaf6d7802008-07-22 11:11:44 +0100220MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222static struct usb_driver garmin_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .name = "garmin_gps",
224 .probe = usb_serial_probe,
225 .disconnect = usb_serial_disconnect,
226 .id_table = id_table,
227};
228
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static inline int getLayerId(const __u8 *usbPacket)
231{
232 return __le32_to_cpup((__le32 *)(usbPacket));
233}
234
235static inline int getPacketId(const __u8 *usbPacket)
236{
237 return __le32_to_cpup((__le32 *)(usbPacket+4));
238}
239
240static inline int getDataLength(const __u8 *usbPacket)
241{
242 return __le32_to_cpup((__le32 *)(usbPacket+8));
243}
244
245
246/*
247 * check if the usb-packet in buf contains an abort-transfer command.
248 * (if yes, all queued data will be dropped)
249 */
250static inline int isAbortTrfCmnd(const unsigned char *buf)
251{
Alan Coxaf6d7802008-07-22 11:11:44 +0100252 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
253 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
254 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
255 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 1;
257 else
258 return 0;
259}
260
261
262
263static void send_to_tty(struct usb_serial_port *port,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200264 char *data, unsigned int actual_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Alan Cox4a90f092008-10-13 10:39:46 +0100266 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (tty && actual_length) {
269
Alan Coxaf6d7802008-07-22 11:11:44 +0100270 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800271 __func__, actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Alan Cox33f0f882006-01-09 20:54:13 -0800273 tty_insert_flip_string(tty, data, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 tty_flip_buffer_push(tty);
275 }
Alan Cox4a90f092008-10-13 10:39:46 +0100276 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279
280/******************************************************************************
281 * packet queue handling
282 ******************************************************************************/
283
284/*
285 * queue a received (usb-)packet for later processing
286 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100287static int pkt_add(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200288 unsigned char *data, unsigned int data_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200290 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int result = 0;
292 unsigned long flags;
293 struct garmin_packet *pkt;
294
295 /* process only packets containg data ... */
296 if (data_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
Alan Coxaf6d7802008-07-22 11:11:44 +0100298 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (pkt == NULL) {
300 dev_err(&garmin_data_p->port->dev, "out of memory\n");
301 return 0;
302 }
303 pkt->size = data_length;
304 memcpy(pkt->data, data, data_length);
305
306 spin_lock_irqsave(&garmin_data_p->lock, flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200307 garmin_data_p->flags |= FLAGS_QUEUING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 result = list_empty(&garmin_data_p->pktlist);
309 pkt->seq = garmin_data_p->seq_counter++;
310 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200311 state = garmin_data_p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
313
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200314 dbg("%s - added: pkt: %d - %d bytes",
315 __func__, pkt->seq, data_length);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 /* in serial mode, if someone is waiting for data from
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200318 the device, convert and send the next packet to tty. */
Alan Coxaf6d7802008-07-22 11:11:44 +0100319 if (result && (state == STATE_GSP_WAIT_DATA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 return result;
323}
324
325
326/* get the next pending packet */
Alan Coxaf6d7802008-07-22 11:11:44 +0100327static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 unsigned long flags;
330 struct garmin_packet *result = NULL;
331
332 spin_lock_irqsave(&garmin_data_p->lock, flags);
333 if (!list_empty(&garmin_data_p->pktlist)) {
334 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
335 list_del(&result->list);
336 }
337 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
338 return result;
339}
340
341
342/* free up all queued data */
Alan Coxaf6d7802008-07-22 11:11:44 +0100343static void pkt_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 unsigned long flags;
346 struct garmin_packet *result = NULL;
347
Harvey Harrison441b62c2008-03-03 16:08:34 -0800348 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 spin_lock_irqsave(&garmin_data_p->lock, flags);
351 while (!list_empty(&garmin_data_p->pktlist)) {
352 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
353 list_del(&result->list);
354 kfree(result);
355 }
356 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
357}
358
359
360/******************************************************************************
361 * garmin serial protocol handling handling
362 ******************************************************************************/
363
364/* send an ack packet back to the tty */
Alan Coxaf6d7802008-07-22 11:11:44 +0100365static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 __u8 pkt[10];
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200368 __u8 cksum = 0;
369 __u8 *ptr = pkt;
370 unsigned l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Harvey Harrison441b62c2008-03-03 16:08:34 -0800372 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 *ptr++ = DLE;
375 *ptr++ = ACK;
376 cksum += ACK;
377
378 *ptr++ = 2;
379 cksum += 2;
380
381 *ptr++ = pkt_id;
382 cksum += pkt_id;
383
Alan Coxaf6d7802008-07-22 11:11:44 +0100384 if (pkt_id == DLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *ptr++ = DLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 *ptr++ = 0;
388 *ptr++ = 0xFF & (-cksum);
389 *ptr++ = DLE;
390 *ptr++ = ETX;
391
392 l = ptr-pkt;
393
394 send_to_tty(garmin_data_p->port, pkt, l);
395 return 0;
396}
397
398
399
400/*
401 * called for a complete packet received from tty layer
402 *
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200403 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * at GSP_INITIAL_OFFSET.
405 *
406 * count - number of bytes in the input buffer including space reserved for
Alan Coxaf6d7802008-07-22 11:11:44 +0100407 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 * (including pkt-id, data-length a. cksum)
409 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100410static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200412 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100413 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200414 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 int cksum = 0;
417 int n = 0;
418 int pktid = recpkt[0];
419 int size = recpkt[1];
420
421 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800422 __func__, count-GSP_INITIAL_OFFSET, recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (size != (count-GSP_INITIAL_OFFSET-3)) {
425 dbg("%s - invalid size, expected %d bytes, got %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800426 __func__, size, (count-GSP_INITIAL_OFFSET-3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EINVPKT;
428 }
429
430 cksum += *recpkt++;
431 cksum += *recpkt++;
432
Alan Coxaf6d7802008-07-22 11:11:44 +0100433 /* sanity check, remove after test ... */
434 if ((__u8 *)&(usbdata[3]) != recpkt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 dbg("%s - ptr mismatch %p - %p",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800436 __func__, &(usbdata[4]), recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EINVPKT;
438 }
439
440 while (n < size) {
441 cksum += *recpkt++;
442 n++;
443 }
444
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200445 if ((0xff & (cksum + *recpkt)) != 0) {
446 dbg("%s - invalid checksum, expected %02x, got %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800447 __func__, 0xff & -cksum, 0xff & *recpkt);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200448 return -EINVPKT;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
452 usbdata[1] = __cpu_to_le32(pktid);
453 usbdata[2] = __cpu_to_le32(size);
454
Alan Coxaf6d7802008-07-22 11:11:44 +0100455 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200456 GARMIN_PKTHDR_LENGTH+size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* if this was an abort-transfer command, flush all
459 queued data. */
460 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200461 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 garmin_data_p->flags |= FLAGS_DROP_DATA;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200463 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 pkt_clear(garmin_data_p);
465 }
466
467 return count;
468}
469
470
471
472/*
473 * Called for data received from tty
474 *
475 * buf contains the data read, it may span more than one packet or even
476 * incomplete packets
477 *
478 * input record should be a serial-record, but it may not be complete.
479 * Copy it into our local buffer, until an etx is seen (or an error
480 * occurs).
481 * Once the record is complete, convert into a usb packet and send it
482 * to the bulk pipe, send an ack back to the tty.
483 *
484 * If the input is an ack, just send the last queued packet to the
485 * tty layer.
486 *
487 * if the input is an abort command, drop all queued data.
488 */
489
Alan Coxaf6d7802008-07-22 11:11:44 +0100490static int gsp_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200491 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200493 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int offs = 0;
495 int ack_or_nak_seen = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200496 __u8 *dest;
497 int size;
Alan Coxaf6d7802008-07-22 11:11:44 +0100498 /* dleSeen: set if last byte read was a DLE */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200499 int dleSeen;
Alan Coxaf6d7802008-07-22 11:11:44 +0100500 /* skip: if set, skip incoming data until possible start of
501 * new packet
502 */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200503 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 __u8 data;
505
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200506 spin_lock_irqsave(&garmin_data_p->lock, flags);
507 dest = garmin_data_p->inbuffer;
508 size = garmin_data_p->insize;
509 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
510 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
511 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
512
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200513 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
514 __func__, dleSeen, skip, size, count); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Alan Coxaf6d7802008-07-22 11:11:44 +0100516 if (size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 size = GSP_INITIAL_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 while (offs < count) {
520
521 data = *(buf+offs);
Alan Coxaf6d7802008-07-22 11:11:44 +0100522 offs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (data == DLE) {
525 if (skip) { /* start of a new pkt */
526 skip = 0;
527 size = GSP_INITIAL_OFFSET;
528 dleSeen = 1;
529 } else if (dleSeen) {
530 dest[size++] = data;
531 dleSeen = 0;
532 } else {
533 dleSeen = 1;
534 }
535 } else if (data == ETX) {
536 if (dleSeen) {
537 /* packet complete */
538
539 data = dest[GSP_INITIAL_OFFSET];
540
541 if (data == ACK) {
542 ack_or_nak_seen = ACK;
543 dbg("ACK packet complete.");
544 } else if (data == NAK) {
545 ack_or_nak_seen = NAK;
546 dbg("NAK packet complete.");
547 } else {
Alan Coxaf6d7802008-07-22 11:11:44 +0100548 dbg("packet complete - id=0x%X.",
549 0xFF & data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 gsp_rec_packet(garmin_data_p, size);
551 }
552
553 skip = 1;
554 size = GSP_INITIAL_OFFSET;
555 dleSeen = 0;
556 } else {
557 dest[size++] = data;
558 }
559 } else if (!skip) {
560
561 if (dleSeen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 size = GSP_INITIAL_OFFSET;
563 dleSeen = 0;
564 }
565
566 dest[size++] = data;
567 }
568
569 if (size >= GPS_IN_BUFSIZ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800570 dbg("%s - packet too large.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 skip = 1;
572 size = GSP_INITIAL_OFFSET;
573 dleSeen = 0;
574 }
575 }
576
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200577 spin_lock_irqsave(&garmin_data_p->lock, flags);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 garmin_data_p->insize = size;
580
Alan Coxaf6d7802008-07-22 11:11:44 +0100581 /* copy flags back to structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (skip)
583 garmin_data_p->flags |= FLAGS_GSP_SKIP;
584 else
585 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
586
587 if (dleSeen)
588 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
589 else
590 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
591
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200592 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
593
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200594 if (ack_or_nak_seen) {
595 if (gsp_next_packet(garmin_data_p) > 0)
596 garmin_data_p->state = STATE_ACTIVE;
597 else
598 garmin_data_p->state = STATE_GSP_WAIT_DATA;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return count;
601}
602
603
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
606 * Sends a usb packet to the tty
607 *
608 * Assumes, that all packages and at an usb-packet boundary.
609 *
610 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
611 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100612static int gsp_send(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 const unsigned char *buf, int count)
614{
615 const unsigned char *src;
616 unsigned char *dst;
617 int pktid = 0;
618 int datalen = 0;
619 int cksum = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100620 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int k;
622
Harvey Harrison441b62c2008-03-03 16:08:34 -0800623 dbg("%s - state %d - %d bytes.", __func__,
Alan Coxaf6d7802008-07-22 11:11:44 +0100624 garmin_data_p->state, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 k = garmin_data_p->outsize;
627 if ((k+count) > GPS_OUT_BUFSIZ) {
628 dbg("packet too large");
629 garmin_data_p->outsize = 0;
630 return -4;
631 }
632
633 memcpy(garmin_data_p->outbuffer+k, buf, count);
634 k += count;
635 garmin_data_p->outsize = k;
636
637 if (k >= GARMIN_PKTHDR_LENGTH) {
638 pktid = getPacketId(garmin_data_p->outbuffer);
Alan Coxaf6d7802008-07-22 11:11:44 +0100639 datalen = getDataLength(garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 i = GARMIN_PKTHDR_LENGTH + datalen;
641 if (k < i)
642 return 0;
643 } else {
644 return 0;
645 }
646
Alan Coxaf6d7802008-07-22 11:11:44 +0100647 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /* garmin_data_p->outbuffer now contains a complete packet */
650
651 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100652 __func__, k, garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 garmin_data_p->outsize = 0;
655
656 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100657 dbg("not an application packet (%d)",
658 getLayerId(garmin_data_p->outbuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -1;
660 }
661
662 if (pktid > 255) {
663 dbg("packet-id %d too large", pktid);
664 return -2;
665 }
666
667 if (datalen > 255) {
668 dbg("packet-size %d too large", datalen);
669 return -3;
670 }
671
672 /* the serial protocol should be able to handle this packet */
673
674 k = 0;
675 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
Alan Coxaf6d7802008-07-22 11:11:44 +0100676 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (*src++ == DLE)
678 k++;
679 }
680
681 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
682 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100683 /* can't add stuffing DLEs in place, move data to end
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200684 of buffer ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
686 memcpy(dst, src, datalen);
687 src = dst;
688 }
689
690 dst = garmin_data_p->outbuffer;
691
692 *dst++ = DLE;
693 *dst++ = pktid;
694 cksum += pktid;
695 *dst++ = datalen;
696 cksum += datalen;
697 if (datalen == DLE)
698 *dst++ = DLE;
699
Alan Coxaf6d7802008-07-22 11:11:44 +0100700 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 __u8 c = *src++;
702 *dst++ = c;
703 cksum += c;
704 if (c == DLE)
705 *dst++ = DLE;
706 }
Alan Coxaf6d7802008-07-22 11:11:44 +0100707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 cksum = 0xFF & -cksum;
709 *dst++ = cksum;
710 if (cksum == DLE)
711 *dst++ = DLE;
712 *dst++ = DLE;
713 *dst++ = ETX;
714
715 i = dst-garmin_data_p->outbuffer;
716
717 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
718
719 garmin_data_p->pkt_id = pktid;
720 garmin_data_p->state = STATE_WAIT_TTY_ACK;
721
722 return i;
723}
724
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/*
727 * Process the next pending data packet - if there is one
728 */
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200729static int gsp_next_packet(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200731 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct garmin_packet *pkt = NULL;
733
734 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800735 dbg("%s - next pkt: %d", __func__, pkt->seq);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200736 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
737 if (result > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 kfree(pkt);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200739 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741 kfree(pkt);
742 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200743 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
746
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748/******************************************************************************
749 * garmin native mode
750 ******************************************************************************/
751
752
753/*
754 * Called for data received from tty
755 *
756 * The input data is expected to be in garmin usb-packet format.
757 *
758 * buf contains the data read, it may span more than one packet
759 * or even incomplete packets
760 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100761static int nat_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200762 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200764 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100765 __u8 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int offs = 0;
767 int result = count;
768 int len;
769
770 while (offs < count) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100771 /* if buffer contains header, copy rest of data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
773 len = GARMIN_PKTHDR_LENGTH
774 +getDataLength(garmin_data_p->inbuffer);
775 else
776 len = GARMIN_PKTHDR_LENGTH;
777
778 if (len >= GPS_IN_BUFSIZ) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100779 /* seems to be an invalid packet, ignore rest
780 of input */
781 dbg("%s - packet size too large: %d", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 garmin_data_p->insize = 0;
783 count = 0;
784 result = -EINVPKT;
785 } else {
786 len -= garmin_data_p->insize;
787 if (len > (count-offs))
788 len = (count-offs);
789 if (len > 0) {
790 dest = garmin_data_p->inbuffer
Alan Coxaf6d7802008-07-22 11:11:44 +0100791 + garmin_data_p->insize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 memcpy(dest, buf+offs, len);
793 garmin_data_p->insize += len;
794 offs += len;
795 }
796 }
797
798 /* do we have a complete packet ? */
799 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
800 len = GARMIN_PKTHDR_LENGTH+
801 getDataLength(garmin_data_p->inbuffer);
802 if (garmin_data_p->insize >= len) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100803 garmin_write_bulk(garmin_data_p->port,
804 garmin_data_p->inbuffer,
805 len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 garmin_data_p->insize = 0;
807
808 /* if this was an abort-transfer command,
809 flush all queued data. */
810 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100811 spin_lock_irqsave(&garmin_data_p->lock,
812 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 garmin_data_p->flags |= FLAGS_DROP_DATA;
Alan Coxaf6d7802008-07-22 11:11:44 +0100814 spin_unlock_irqrestore(
815 &garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 pkt_clear(garmin_data_p);
817 }
818 }
819 }
820 }
821 return result;
822}
823
824
825/******************************************************************************
826 * private packets
827 ******************************************************************************/
828
829static void priv_status_resp(struct usb_serial_port *port)
830{
Alan Coxaf6d7802008-07-22 11:11:44 +0100831 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
833
834 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
835 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
836 pkt[2] = __cpu_to_le32(12);
837 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
838 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
839 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
840
Alan Coxaf6d7802008-07-22 11:11:44 +0100841 send_to_tty(port, (__u8 *)pkt, 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844
845/******************************************************************************
846 * Garmin specific driver functions
847 ******************************************************************************/
848
849static int process_resetdev_request(struct usb_serial_port *port)
850{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200851 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int status;
Alan Coxaf6d7802008-07-22 11:11:44 +0100853 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200855 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
857 garmin_data_p->state = STATE_RESET;
858 garmin_data_p->serial_num = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200859 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Alan Coxaf6d7802008-07-22 11:11:44 +0100861 usb_kill_urb(port->interrupt_in_urb);
862 dbg("%s - usb_reset_device", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 status = usb_reset_device(port->serial->dev);
864 if (status)
865 dbg("%s - usb_reset_device failed: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800866 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return status;
868}
869
870
871
872/*
873 * clear all cached data
874 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100875static int garmin_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200877 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int status = 0;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* flush all queued data */
881 pkt_clear(garmin_data_p);
882
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200883 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 garmin_data_p->insize = 0;
885 garmin_data_p->outsize = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200886 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 return status;
889}
890
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892static int garmin_init_session(struct usb_serial_port *port)
893{
894 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100895 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 int status = 0;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200897 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 if (status == 0) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100900 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Harvey Harrison441b62c2008-03-03 16:08:34 -0800902 dbg("%s - adding interrupt input", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
904 if (status)
905 dev_err(&serial->dev->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100906 "%s - failed submitting interrupt urb, error %d\n",
907 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200910 /*
911 * using the initialization method from gpsbabel. See comments in
912 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (status == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800915 dbg("%s - starting session ...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 garmin_data_p->state = STATE_ACTIVE;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200917
918 for (i = 0; i < 3; i++) {
919 status = garmin_write_bulk(port,
920 GARMIN_START_SESSION_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100921 sizeof(GARMIN_START_SESSION_REQ), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200923 if (status < 0)
924 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200926
927 if (status > 0)
928 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
931 return status;
932}
933
934
935
Alan Coxa509a7e2009-09-19 13:13:26 -0700936static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200938 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int status = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100940 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Harvey Harrison441b62c2008-03-03 16:08:34 -0800942 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200944 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 garmin_data_p->mode = initial_mode;
946 garmin_data_p->count = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200947 garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200948 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /* shutdown any bulk reads that might be going on */
Alan Coxaf6d7802008-07-22 11:11:44 +0100951 usb_kill_urb(port->write_urb);
952 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Alan Cox95da3102008-07-22 11:09:07 +0100954 if (garmin_data_p->state == STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 status = garmin_init_session(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 garmin_data_p->state = STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return status;
959}
960
961
Alan Cox335f8512009-06-11 12:26:29 +0100962static void garmin_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
964 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100965 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Harvey Harrison441b62c2008-03-03 16:08:34 -0800967 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 port->number, garmin_data_p->mode,
969 garmin_data_p->state, garmin_data_p->flags);
970
971 if (!serial)
972 return;
973
Oliver Neukum95bef012008-01-22 13:56:18 +0100974 mutex_lock(&port->serial->disc_mutex);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200975
Oliver Neukum95bef012008-01-22 13:56:18 +0100976 if (!port->serial->disconnected)
977 garmin_clear(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /* shutdown our urbs */
Alan Coxaf6d7802008-07-22 11:11:44 +0100980 usb_kill_urb(port->read_urb);
981 usb_kill_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200983 /* keep reset state so we know that we must start a new session */
984 if (garmin_data_p->state != STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 garmin_data_p->state = STATE_DISCONNECTED;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200986
Oliver Neukum95bef012008-01-22 13:56:18 +0100987 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200990
Alan Coxaf6d7802008-07-22 11:11:44 +0100991static void garmin_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Ming Leicdc97792008-02-24 18:41:47 +0800993 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Hermann Kneissel468d1362007-08-03 20:20:33 +0200995 if (port) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100996 struct garmin_data *garmin_data_p =
997 usb_get_serial_port_data(port);
Hermann Kneissel468d1362007-08-03 20:20:33 +0200998
Harvey Harrison441b62c2008-03-03 16:08:34 -0800999 dbg("%s - port %d", __func__, port->number);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001000
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001001 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
1002
1003 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1004 gsp_send_ack(garmin_data_p,
Alan Coxaf6d7802008-07-22 11:11:44 +01001005 ((__u8 *)urb->transfer_buffer)[4]);
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001006 }
Hermann Kneissel468d1362007-08-03 20:20:33 +02001007 }
Hermann Kneissel468d1362007-08-03 20:20:33 +02001008 usb_serial_port_softint(port);
1009 }
1010
Alan Coxaf6d7802008-07-22 11:11:44 +01001011 /* Ignore errors that resulted from garmin_write_bulk with
1012 dismiss_ack = 1 */
Hermann Kneissel468d1362007-08-03 20:20:33 +02001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxaf6d7802008-07-22 11:11:44 +01001015 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
1018
Alan Coxaf6d7802008-07-22 11:11:44 +01001019static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001020 const unsigned char *buf, int count,
1021 int dismiss_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001023 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001025 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 struct urb *urb;
1027 unsigned char *buffer;
1028 int status;
1029
Harvey Harrison441b62c2008-03-03 16:08:34 -08001030 dbg("%s - port %d, state %d", __func__, port->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 garmin_data_p->state);
1032
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001033 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001035 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Alan Coxaf6d7802008-07-22 11:11:44 +01001037 buffer = kmalloc(count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!buffer) {
1039 dev_err(&port->dev, "out of memory\n");
1040 return -ENOMEM;
1041 }
1042
1043 urb = usb_alloc_urb(0, GFP_ATOMIC);
1044 if (!urb) {
1045 dev_err(&port->dev, "no more free urbs\n");
Alan Coxaf6d7802008-07-22 11:11:44 +01001046 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -ENOMEM;
1048 }
1049
Alan Coxaf6d7802008-07-22 11:11:44 +01001050 memcpy(buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Harvey Harrison441b62c2008-03-03 16:08:34 -08001052 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Alan Coxaf6d7802008-07-22 11:11:44 +01001054 usb_fill_bulk_urb(urb, serial->dev,
1055 usb_sndbulkpipe(serial->dev,
1056 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 buffer, count,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001058 garmin_write_bulk_callback,
1059 dismiss_ack ? NULL : port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 urb->transfer_flags |= URB_ZERO_PACKET;
1061
1062 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001063
1064 spin_lock_irqsave(&garmin_data_p->lock, flags);
1065 garmin_data_p->flags |= APP_REQ_SEEN;
1066 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1069 pkt_clear(garmin_data_p);
1070 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1071 }
1072 }
1073
1074 /* send it down the pipe */
1075 status = usb_submit_urb(urb, GFP_ATOMIC);
1076 if (status) {
1077 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001078 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001079 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 count = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
1083 /* we are done with this urb, so let the host driver
1084 * really free it when it is finished with it */
Alan Coxaf6d7802008-07-22 11:11:44 +01001085 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 return count;
1088}
1089
Alan Coxaf6d7802008-07-22 11:11:44 +01001090static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +01001091 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 int pktid, pktsiz, len;
Alan Coxaf6d7802008-07-22 11:11:44 +01001094 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1096
Harvey Harrison441b62c2008-03-03 16:08:34 -08001097 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001099 if (garmin_data_p->state == STATE_RESET)
1100 return -EIO;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* check for our private packets */
1103 if (count >= GARMIN_PKTHDR_LENGTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 len = PRIVPKTSIZ;
1105 if (count < len)
1106 len = count;
1107
1108 memcpy(garmin_data_p->privpkt, buf, len);
1109
1110 pktsiz = getDataLength(garmin_data_p->privpkt);
1111 pktid = getPacketId(garmin_data_p->privpkt);
1112
1113 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
Alan Coxaf6d7802008-07-22 11:11:44 +01001114 && GARMIN_LAYERID_PRIVATE ==
1115 getLayerId(garmin_data_p->privpkt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 dbg("%s - processing private request %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001118 __func__, pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Alan Coxaf6d7802008-07-22 11:11:44 +01001120 /* drop all unfinished transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 garmin_clear(garmin_data_p);
1122
Alan Coxaf6d7802008-07-22 11:11:44 +01001123 switch (pktid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 case PRIV_PKTID_SET_DEBUG:
1126 if (pktsiz != 4)
1127 return -EINVPKT;
1128 debug = __le32_to_cpu(privpkt[3]);
1129 dbg("%s - debug level set to 0x%X",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001130 __func__, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
1132
1133 case PRIV_PKTID_SET_MODE:
1134 if (pktsiz != 4)
1135 return -EINVPKT;
1136 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1137 dbg("%s - mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001138 __func__, garmin_data_p->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140
1141 case PRIV_PKTID_INFO_REQ:
1142 priv_status_resp(port);
1143 break;
1144
1145 case PRIV_PKTID_RESET_REQ:
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001146 process_resetdev_request(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
1148
1149 case PRIV_PKTID_SET_DEF_MODE:
1150 if (pktsiz != 4)
1151 return -EINVPKT;
1152 initial_mode = __le32_to_cpu(privpkt[3]);
1153 dbg("%s - initial_mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001154 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 garmin_data_p->mode);
1156 break;
1157 }
1158 return count;
1159 }
1160 }
1161
1162 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1163 return gsp_receive(garmin_data_p, buf, count);
1164 } else { /* MODE_NATIVE */
1165 return nat_receive(garmin_data_p, buf, count);
1166 }
1167}
1168
1169
Alan Cox95da3102008-07-22 11:09:07 +01001170static int garmin_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Alan Cox95da3102008-07-22 11:09:07 +01001172 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * Report back the bytes currently available in the output buffer.
1175 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001176 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1178}
1179
1180
Alan Coxaf6d7802008-07-22 11:11:44 +01001181static void garmin_read_process(struct garmin_data *garmin_data_p,
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001182 unsigned char *data, unsigned data_length,
1183 int bulk_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001185 unsigned long flags;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1188 /* abort-transfer cmd is actice */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001189 dbg("%s - pkt dropped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
Alan Coxaf6d7802008-07-22 11:11:44 +01001191 garmin_data_p->state != STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /* if throttling is active or postprecessing is required
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001194 put the received data in the input queue, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 send it directly to the tty port */
1196 if (garmin_data_p->flags & FLAGS_QUEUING) {
1197 pkt_add(garmin_data_p, data, data_length);
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001198 } else if (bulk_data ||
1199 getLayerId(data) == GARMIN_LAYERID_APPL) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001200
1201 spin_lock_irqsave(&garmin_data_p->lock, flags);
1202 garmin_data_p->flags |= APP_RESP_SEEN;
1203 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1204
1205 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 pkt_add(garmin_data_p, data, data_length);
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001207 } else {
1208 send_to_tty(garmin_data_p->port, data,
1209 data_length);
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001212 /* ignore system layer packets ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214}
1215
1216
Alan Coxaf6d7802008-07-22 11:11:44 +01001217static void garmin_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001219 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001220 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001222 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001224 int status = urb->status;
1225 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Harvey Harrison441b62c2008-03-03 16:08:34 -08001227 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (!serial) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001230 dbg("%s - bad serial pointer, exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
1232 }
1233
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001234 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001236 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return;
1238 }
1239
Alan Coxaf6d7802008-07-22 11:11:44 +01001240 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -08001241 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001243 garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001245 if (urb->actual_length == 0 &&
1246 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1247 spin_lock_irqsave(&garmin_data_p->lock, flags);
1248 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1249 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001250 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1251 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 dev_err(&port->dev,
1253 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001254 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001255 } else if (urb->actual_length > 0) {
1256 /* Continue trying to read until nothing more is received */
1257 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001258 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1259 if (retval)
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001260 dev_err(&port->dev,
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001261 "%s - failed resubmitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001262 "error %d\n", __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001263 }
1264 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001265 dbg("%s - end of bulk data", __func__);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001266 spin_lock_irqsave(&garmin_data_p->lock, flags);
1267 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1268 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272
Alan Coxaf6d7802008-07-22 11:11:44 +01001273static void garmin_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001275 unsigned long flags;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001276 int retval;
Ming Leicdc97792008-02-24 18:41:47 +08001277 struct usb_serial_port *port = urb->context;
Alan Coxaf6d7802008-07-22 11:11:44 +01001278 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001280 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001282 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case 0:
1284 /* success */
1285 break;
1286 case -ECONNRESET:
1287 case -ENOENT:
1288 case -ESHUTDOWN:
1289 /* this urb is terminated, clean up */
1290 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001291 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return;
1293 default:
1294 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001295 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return;
1297 }
1298
Harvey Harrison441b62c2008-03-03 16:08:34 -08001299 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 urb->actual_length, urb->transfer_buffer);
1301
1302 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1303 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001304 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Harvey Harrison441b62c2008-03-03 16:08:34 -08001306 dbg("%s - bulk data available.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001308 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1309
1310 /* bulk data available */
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001311 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1312 if (retval) {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001313 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001314 "%s - failed submitting read urb, error %d\n",
1315 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001316 } else {
1317 spin_lock_irqsave(&garmin_data_p->lock, flags);
1318 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
Alan Coxaf6d7802008-07-22 11:11:44 +01001319 spin_unlock_irqrestore(&garmin_data_p->lock,
1320 flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001321 }
1322 } else {
1323 /* bulk-in transfer still active */
1324 spin_lock_irqsave(&garmin_data_p->lock, flags);
1325 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1326 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
1329 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1330 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001331 sizeof(GARMIN_START_SESSION_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001333 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001335 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* save the serial number */
Alan Coxaf6d7802008-07-22 11:11:44 +01001338 garmin_data_p->serial_num = __le32_to_cpup(
1339 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 dbg("%s - start-of-session reply seen - serial %u.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001342 __func__, garmin_data_p->serial_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001345 garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Alan Coxaf6d7802008-07-22 11:11:44 +01001347 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001348 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 dev_err(&urb->dev->dev,
1350 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001351 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354
1355/*
1356 * Sends the next queued packt to the tty port (garmin native mode only)
1357 * and then sets a timer to call itself again until all queued data
1358 * is sent.
1359 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001360static int garmin_flush_queue(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001362 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 struct garmin_packet *pkt;
1364
1365 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1366 pkt = pkt_pop(garmin_data_p);
1367 if (pkt != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1369 kfree(pkt);
1370 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1371
1372 } else {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001373 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 garmin_data_p->flags &= ~FLAGS_QUEUING;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001375 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 }
1378 return 0;
1379}
1380
1381
Alan Cox95da3102008-07-22 11:09:07 +01001382static void garmin_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Alan Cox95da3102008-07-22 11:09:07 +01001384 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001385 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Harvey Harrison441b62c2008-03-03 16:08:34 -08001387 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* set flag, data received will be put into a queue
1389 for later processing */
Oliver Neukum63832512009-10-07 10:50:23 +02001390 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001392 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
1395
Alan Coxaf6d7802008-07-22 11:11:44 +01001396static void garmin_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Alan Cox95da3102008-07-22 11:09:07 +01001398 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001399 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001400 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Harvey Harrison441b62c2008-03-03 16:08:34 -08001402 dbg("%s - port %d", __func__, port->number);
Oliver Neukum63832512009-10-07 10:50:23 +02001403 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 garmin_data_p->flags &= ~FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001405 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /* in native mode send queued data to tty, in
1408 serial mode nothing needs to be done here */
1409 if (garmin_data_p->mode == MODE_NATIVE)
1410 garmin_flush_queue(garmin_data_p);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001411
1412 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
Oliver Neukum63832512009-10-07 10:50:23 +02001413 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001414 if (status)
1415 dev_err(&port->dev,
1416 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001417 __func__, status);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421/*
1422 * The timer is currently only used to send queued packets to
1423 * the tty in cases where the protocol provides no own handshaking
1424 * to initiate the transfer.
1425 */
1426static void timeout_handler(unsigned long data)
1427{
1428 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1429
1430 /* send the next queued packet to the tty port */
1431 if (garmin_data_p->mode == MODE_NATIVE)
1432 if (garmin_data_p->flags & FLAGS_QUEUING)
1433 garmin_flush_queue(garmin_data_p);
1434}
1435
1436
1437
Alan Cox95da3102008-07-22 11:09:07 +01001438static int garmin_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 int status = 0;
1441 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001442 struct garmin_data *garmin_data_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Harvey Harrison441b62c2008-03-03 16:08:34 -08001444 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Burman Yan7ac9da12006-11-22 20:54:38 +02001446 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (garmin_data_p == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001448 dev_err(&port->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -ENOMEM;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 init_timer(&garmin_data_p->timer);
1452 spin_lock_init(&garmin_data_p->lock);
1453 INIT_LIST_HEAD(&garmin_data_p->pktlist);
Alan Coxaf6d7802008-07-22 11:11:44 +01001454 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1456 garmin_data_p->timer.function = timeout_handler;
1457 garmin_data_p->port = port;
1458 garmin_data_p->state = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001459 garmin_data_p->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 garmin_data_p->count = 0;
1461 usb_set_serial_port_data(port, garmin_data_p);
1462
1463 status = garmin_init_session(port);
1464
1465 return status;
1466}
1467
1468
Alan Sternf9c99bb2009-06-02 11:53:55 -04001469static void garmin_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001472 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Harvey Harrison441b62c2008-03-03 16:08:34 -08001474 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Alan Coxaf6d7802008-07-22 11:11:44 +01001476 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 del_timer_sync(&garmin_data_p->timer);
Alan Sternf9c99bb2009-06-02 11:53:55 -04001478}
1479
1480
1481static void garmin_release(struct usb_serial *serial)
1482{
1483 struct usb_serial_port *port = serial->port[0];
1484 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1485
1486 dbg("%s", __func__);
1487
Alan Coxaf6d7802008-07-22 11:11:44 +01001488 kfree(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001493static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001494 .driver = {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001495 .owner = THIS_MODULE,
1496 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001497 },
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001498 .description = "Garmin GPS usb/tty",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 .num_ports = 1,
1501 .open = garmin_open,
1502 .close = garmin_close,
1503 .throttle = garmin_throttle,
1504 .unthrottle = garmin_unthrottle,
1505 .attach = garmin_attach,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001506 .disconnect = garmin_disconnect,
1507 .release = garmin_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 .write = garmin_write,
1509 .write_room = garmin_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 .write_bulk_callback = garmin_write_bulk_callback,
1511 .read_bulk_callback = garmin_read_bulk_callback,
1512 .read_int_callback = garmin_read_int_callback,
1513};
1514
Alan Stern97b6b6d2012-02-23 14:56:32 -05001515static struct usb_serial_driver * const serial_drivers[] = {
1516 &garmin_device, NULL
1517};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001519
Alan Coxaf6d7802008-07-22 11:11:44 +01001520static int __init garmin_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 int retval;
1523
Alan Stern97b6b6d2012-02-23 14:56:32 -05001524 retval = usb_serial_register_drivers(&garmin_driver, serial_drivers);
1525 if (retval == 0)
1526 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1527 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return retval;
1529}
1530
1531
Alan Coxaf6d7802008-07-22 11:11:44 +01001532static void __exit garmin_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Alan Stern97b6b6d2012-02-23 14:56:32 -05001534 usb_serial_deregister_drivers(&garmin_driver, serial_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537
1538
1539
1540module_init(garmin_init);
1541module_exit(garmin_exit);
1542
Alan Coxaf6d7802008-07-22 11:11:44 +01001543MODULE_AUTHOR(DRIVER_AUTHOR);
1544MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545MODULE_LICENSE("GPL");
1546
1547module_param(debug, bool, S_IWUSR | S_IRUGO);
1548MODULE_PARM_DESC(debug, "Debug enabled or not");
1549module_param(initial_mode, int, S_IRUGO);
1550MODULE_PARM_DESC(initial_mode, "Initial mode");
1551