blob: 11029dfe48ceb76ad1cb9f994406585dafb405e2 [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};
Alan Coxaf6d7802008-07-22 11:11:44 +0100219MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static inline int getLayerId(const __u8 *usbPacket)
223{
224 return __le32_to_cpup((__le32 *)(usbPacket));
225}
226
227static inline int getPacketId(const __u8 *usbPacket)
228{
229 return __le32_to_cpup((__le32 *)(usbPacket+4));
230}
231
232static inline int getDataLength(const __u8 *usbPacket)
233{
234 return __le32_to_cpup((__le32 *)(usbPacket+8));
235}
236
237
238/*
239 * check if the usb-packet in buf contains an abort-transfer command.
240 * (if yes, all queued data will be dropped)
241 */
242static inline int isAbortTrfCmnd(const unsigned char *buf)
243{
Alan Coxaf6d7802008-07-22 11:11:44 +0100244 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
245 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
246 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
247 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 1;
249 else
250 return 0;
251}
252
253
254
255static void send_to_tty(struct usb_serial_port *port,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200256 char *data, unsigned int actual_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Alan Cox4a90f092008-10-13 10:39:46 +0100258 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (tty && actual_length) {
261
Alan Coxaf6d7802008-07-22 11:11:44 +0100262 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800263 __func__, actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Alan Cox33f0f882006-01-09 20:54:13 -0800265 tty_insert_flip_string(tty, data, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 tty_flip_buffer_push(tty);
267 }
Alan Cox4a90f092008-10-13 10:39:46 +0100268 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271
272/******************************************************************************
273 * packet queue handling
274 ******************************************************************************/
275
276/*
277 * queue a received (usb-)packet for later processing
278 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100279static int pkt_add(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200280 unsigned char *data, unsigned int data_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200282 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int result = 0;
284 unsigned long flags;
285 struct garmin_packet *pkt;
286
287 /* process only packets containg data ... */
288 if (data_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
Alan Coxaf6d7802008-07-22 11:11:44 +0100290 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (pkt == NULL) {
292 dev_err(&garmin_data_p->port->dev, "out of memory\n");
293 return 0;
294 }
295 pkt->size = data_length;
296 memcpy(pkt->data, data, data_length);
297
298 spin_lock_irqsave(&garmin_data_p->lock, flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200299 garmin_data_p->flags |= FLAGS_QUEUING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 result = list_empty(&garmin_data_p->pktlist);
301 pkt->seq = garmin_data_p->seq_counter++;
302 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200303 state = garmin_data_p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
305
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700306 dev_dbg(&garmin_data_p->port->dev,
307 "%s - added: pkt: %d - %d bytes\n", __func__,
308 pkt->seq, data_length);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* in serial mode, if someone is waiting for data from
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200311 the device, convert and send the next packet to tty. */
Alan Coxaf6d7802008-07-22 11:11:44 +0100312 if (result && (state == STATE_GSP_WAIT_DATA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 return result;
316}
317
318
319/* get the next pending packet */
Alan Coxaf6d7802008-07-22 11:11:44 +0100320static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 unsigned long flags;
323 struct garmin_packet *result = NULL;
324
325 spin_lock_irqsave(&garmin_data_p->lock, flags);
326 if (!list_empty(&garmin_data_p->pktlist)) {
327 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
328 list_del(&result->list);
329 }
330 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
331 return result;
332}
333
334
335/* free up all queued data */
Alan Coxaf6d7802008-07-22 11:11:44 +0100336static void pkt_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 unsigned long flags;
339 struct garmin_packet *result = NULL;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_lock_irqsave(&garmin_data_p->lock, flags);
342 while (!list_empty(&garmin_data_p->pktlist)) {
343 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
344 list_del(&result->list);
345 kfree(result);
346 }
347 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
348}
349
350
351/******************************************************************************
352 * garmin serial protocol handling handling
353 ******************************************************************************/
354
355/* send an ack packet back to the tty */
Alan Coxaf6d7802008-07-22 11:11:44 +0100356static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 __u8 pkt[10];
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200359 __u8 cksum = 0;
360 __u8 *ptr = pkt;
361 unsigned l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700363 dev_dbg(&garmin_data_p->port->dev, "%s - pkt-id: 0x%X.\n", __func__,
364 0xFF & pkt_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 *ptr++ = DLE;
367 *ptr++ = ACK;
368 cksum += ACK;
369
370 *ptr++ = 2;
371 cksum += 2;
372
373 *ptr++ = pkt_id;
374 cksum += pkt_id;
375
Alan Coxaf6d7802008-07-22 11:11:44 +0100376 if (pkt_id == DLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *ptr++ = DLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 *ptr++ = 0;
380 *ptr++ = 0xFF & (-cksum);
381 *ptr++ = DLE;
382 *ptr++ = ETX;
383
384 l = ptr-pkt;
385
386 send_to_tty(garmin_data_p->port, pkt, l);
387 return 0;
388}
389
390
391
392/*
393 * called for a complete packet received from tty layer
394 *
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200395 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * at GSP_INITIAL_OFFSET.
397 *
398 * count - number of bytes in the input buffer including space reserved for
Alan Coxaf6d7802008-07-22 11:11:44 +0100399 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * (including pkt-id, data-length a. cksum)
401 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100402static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700404 struct device *dev = &garmin_data_p->port->dev;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200405 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100406 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200407 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int cksum = 0;
409 int n = 0;
410 int pktid = recpkt[0];
411 int size = recpkt[1];
412
413 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800414 __func__, count-GSP_INITIAL_OFFSET, recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 if (size != (count-GSP_INITIAL_OFFSET-3)) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700417 dev_dbg(dev, "%s - invalid size, expected %d bytes, got %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800418 __func__, size, (count-GSP_INITIAL_OFFSET-3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -EINVPKT;
420 }
421
422 cksum += *recpkt++;
423 cksum += *recpkt++;
424
Alan Coxaf6d7802008-07-22 11:11:44 +0100425 /* sanity check, remove after test ... */
426 if ((__u8 *)&(usbdata[3]) != recpkt) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700427 dev_dbg(dev, "%s - ptr mismatch %p - %p\n", __func__,
428 &(usbdata[4]), recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -EINVPKT;
430 }
431
432 while (n < size) {
433 cksum += *recpkt++;
434 n++;
435 }
436
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200437 if ((0xff & (cksum + *recpkt)) != 0) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700438 dev_dbg(dev, "%s - invalid checksum, expected %02x, got %02x\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800439 __func__, 0xff & -cksum, 0xff & *recpkt);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200440 return -EINVPKT;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
444 usbdata[1] = __cpu_to_le32(pktid);
445 usbdata[2] = __cpu_to_le32(size);
446
Alan Coxaf6d7802008-07-22 11:11:44 +0100447 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200448 GARMIN_PKTHDR_LENGTH+size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /* if this was an abort-transfer command, flush all
451 queued data. */
452 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200453 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 garmin_data_p->flags |= FLAGS_DROP_DATA;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200455 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 pkt_clear(garmin_data_p);
457 }
458
459 return count;
460}
461
462
463
464/*
465 * Called for data received from tty
466 *
467 * buf contains the data read, it may span more than one packet or even
468 * incomplete packets
469 *
470 * input record should be a serial-record, but it may not be complete.
471 * Copy it into our local buffer, until an etx is seen (or an error
472 * occurs).
473 * Once the record is complete, convert into a usb packet and send it
474 * to the bulk pipe, send an ack back to the tty.
475 *
476 * If the input is an ack, just send the last queued packet to the
477 * tty layer.
478 *
479 * if the input is an abort command, drop all queued data.
480 */
481
Alan Coxaf6d7802008-07-22 11:11:44 +0100482static int gsp_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200483 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700485 struct device *dev = &garmin_data_p->port->dev;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200486 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int offs = 0;
488 int ack_or_nak_seen = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200489 __u8 *dest;
490 int size;
Alan Coxaf6d7802008-07-22 11:11:44 +0100491 /* dleSeen: set if last byte read was a DLE */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200492 int dleSeen;
Alan Coxaf6d7802008-07-22 11:11:44 +0100493 /* skip: if set, skip incoming data until possible start of
494 * new packet
495 */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200496 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 __u8 data;
498
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200499 spin_lock_irqsave(&garmin_data_p->lock, flags);
500 dest = garmin_data_p->inbuffer;
501 size = garmin_data_p->insize;
502 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
503 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
504 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
505
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700506 /* dev_dbg(dev, "%s - dle=%d skip=%d size=%d count=%d\n",
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200507 __func__, dleSeen, skip, size, count); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Alan Coxaf6d7802008-07-22 11:11:44 +0100509 if (size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 size = GSP_INITIAL_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 while (offs < count) {
513
514 data = *(buf+offs);
Alan Coxaf6d7802008-07-22 11:11:44 +0100515 offs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 if (data == DLE) {
518 if (skip) { /* start of a new pkt */
519 skip = 0;
520 size = GSP_INITIAL_OFFSET;
521 dleSeen = 1;
522 } else if (dleSeen) {
523 dest[size++] = data;
524 dleSeen = 0;
525 } else {
526 dleSeen = 1;
527 }
528 } else if (data == ETX) {
529 if (dleSeen) {
530 /* packet complete */
531
532 data = dest[GSP_INITIAL_OFFSET];
533
534 if (data == ACK) {
535 ack_or_nak_seen = ACK;
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700536 dev_dbg(dev, "ACK packet complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else if (data == NAK) {
538 ack_or_nak_seen = NAK;
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700539 dev_dbg(dev, "NAK packet complete.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 } else {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700541 dev_dbg(dev, "packet complete - id=0x%X.\n",
Alan Coxaf6d7802008-07-22 11:11:44 +0100542 0xFF & data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 gsp_rec_packet(garmin_data_p, size);
544 }
545
546 skip = 1;
547 size = GSP_INITIAL_OFFSET;
548 dleSeen = 0;
549 } else {
550 dest[size++] = data;
551 }
552 } else if (!skip) {
553
554 if (dleSeen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 size = GSP_INITIAL_OFFSET;
556 dleSeen = 0;
557 }
558
559 dest[size++] = data;
560 }
561
562 if (size >= GPS_IN_BUFSIZ) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700563 dev_dbg(dev, "%s - packet too large.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 skip = 1;
565 size = GSP_INITIAL_OFFSET;
566 dleSeen = 0;
567 }
568 }
569
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200570 spin_lock_irqsave(&garmin_data_p->lock, flags);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 garmin_data_p->insize = size;
573
Alan Coxaf6d7802008-07-22 11:11:44 +0100574 /* copy flags back to structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (skip)
576 garmin_data_p->flags |= FLAGS_GSP_SKIP;
577 else
578 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
579
580 if (dleSeen)
581 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
582 else
583 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
584
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200585 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
586
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200587 if (ack_or_nak_seen) {
588 if (gsp_next_packet(garmin_data_p) > 0)
589 garmin_data_p->state = STATE_ACTIVE;
590 else
591 garmin_data_p->state = STATE_GSP_WAIT_DATA;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return count;
594}
595
596
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598/*
599 * Sends a usb packet to the tty
600 *
601 * Assumes, that all packages and at an usb-packet boundary.
602 *
603 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
604 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100605static int gsp_send(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 const unsigned char *buf, int count)
607{
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700608 struct device *dev = &garmin_data_p->port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 const unsigned char *src;
610 unsigned char *dst;
611 int pktid = 0;
612 int datalen = 0;
613 int cksum = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100614 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int k;
616
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700617 dev_dbg(dev, "%s - state %d - %d bytes.\n", __func__,
618 garmin_data_p->state, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 k = garmin_data_p->outsize;
621 if ((k+count) > GPS_OUT_BUFSIZ) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700622 dev_dbg(dev, "packet too large\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 garmin_data_p->outsize = 0;
624 return -4;
625 }
626
627 memcpy(garmin_data_p->outbuffer+k, buf, count);
628 k += count;
629 garmin_data_p->outsize = k;
630
631 if (k >= GARMIN_PKTHDR_LENGTH) {
632 pktid = getPacketId(garmin_data_p->outbuffer);
Alan Coxaf6d7802008-07-22 11:11:44 +0100633 datalen = getDataLength(garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 i = GARMIN_PKTHDR_LENGTH + datalen;
635 if (k < i)
636 return 0;
637 } else {
638 return 0;
639 }
640
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700641 dev_dbg(dev, "%s - %d bytes in buffer, %d bytes in pkt.\n", __func__, k, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* garmin_data_p->outbuffer now contains a complete packet */
644
645 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100646 __func__, k, garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 garmin_data_p->outsize = 0;
649
650 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700651 dev_dbg(dev, "not an application packet (%d)\n",
Alan Coxaf6d7802008-07-22 11:11:44 +0100652 getLayerId(garmin_data_p->outbuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -1;
654 }
655
656 if (pktid > 255) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700657 dev_dbg(dev, "packet-id %d too large\n", pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -2;
659 }
660
661 if (datalen > 255) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700662 dev_dbg(dev, "packet-size %d too large\n", datalen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return -3;
664 }
665
666 /* the serial protocol should be able to handle this packet */
667
668 k = 0;
669 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
Alan Coxaf6d7802008-07-22 11:11:44 +0100670 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (*src++ == DLE)
672 k++;
673 }
674
675 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
676 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100677 /* can't add stuffing DLEs in place, move data to end
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200678 of buffer ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
680 memcpy(dst, src, datalen);
681 src = dst;
682 }
683
684 dst = garmin_data_p->outbuffer;
685
686 *dst++ = DLE;
687 *dst++ = pktid;
688 cksum += pktid;
689 *dst++ = datalen;
690 cksum += datalen;
691 if (datalen == DLE)
692 *dst++ = DLE;
693
Alan Coxaf6d7802008-07-22 11:11:44 +0100694 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 __u8 c = *src++;
696 *dst++ = c;
697 cksum += c;
698 if (c == DLE)
699 *dst++ = DLE;
700 }
Alan Coxaf6d7802008-07-22 11:11:44 +0100701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 cksum = 0xFF & -cksum;
703 *dst++ = cksum;
704 if (cksum == DLE)
705 *dst++ = DLE;
706 *dst++ = DLE;
707 *dst++ = ETX;
708
709 i = dst-garmin_data_p->outbuffer;
710
711 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
712
713 garmin_data_p->pkt_id = pktid;
714 garmin_data_p->state = STATE_WAIT_TTY_ACK;
715
716 return i;
717}
718
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * Process the next pending data packet - if there is one
722 */
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200723static int gsp_next_packet(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200725 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct garmin_packet *pkt = NULL;
727
728 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700729 dev_dbg(&garmin_data_p->port->dev, "%s - next pkt: %d\n", __func__, pkt->seq);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200730 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
731 if (result > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 kfree(pkt);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200733 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 kfree(pkt);
736 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200737 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/******************************************************************************
743 * garmin native mode
744 ******************************************************************************/
745
746
747/*
748 * Called for data received from tty
749 *
750 * The input data is expected to be in garmin usb-packet format.
751 *
752 * buf contains the data read, it may span more than one packet
753 * or even incomplete packets
754 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100755static int nat_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200756 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200758 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100759 __u8 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int offs = 0;
761 int result = count;
762 int len;
763
764 while (offs < count) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100765 /* if buffer contains header, copy rest of data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
767 len = GARMIN_PKTHDR_LENGTH
768 +getDataLength(garmin_data_p->inbuffer);
769 else
770 len = GARMIN_PKTHDR_LENGTH;
771
772 if (len >= GPS_IN_BUFSIZ) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100773 /* seems to be an invalid packet, ignore rest
774 of input */
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700775 dev_dbg(&garmin_data_p->port->dev,
776 "%s - packet size too large: %d\n",
777 __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 garmin_data_p->insize = 0;
779 count = 0;
780 result = -EINVPKT;
781 } else {
782 len -= garmin_data_p->insize;
783 if (len > (count-offs))
784 len = (count-offs);
785 if (len > 0) {
786 dest = garmin_data_p->inbuffer
Alan Coxaf6d7802008-07-22 11:11:44 +0100787 + garmin_data_p->insize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 memcpy(dest, buf+offs, len);
789 garmin_data_p->insize += len;
790 offs += len;
791 }
792 }
793
794 /* do we have a complete packet ? */
795 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
796 len = GARMIN_PKTHDR_LENGTH+
797 getDataLength(garmin_data_p->inbuffer);
798 if (garmin_data_p->insize >= len) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100799 garmin_write_bulk(garmin_data_p->port,
800 garmin_data_p->inbuffer,
801 len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 garmin_data_p->insize = 0;
803
804 /* if this was an abort-transfer command,
805 flush all queued data. */
806 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100807 spin_lock_irqsave(&garmin_data_p->lock,
808 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 garmin_data_p->flags |= FLAGS_DROP_DATA;
Alan Coxaf6d7802008-07-22 11:11:44 +0100810 spin_unlock_irqrestore(
811 &garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 pkt_clear(garmin_data_p);
813 }
814 }
815 }
816 }
817 return result;
818}
819
820
821/******************************************************************************
822 * private packets
823 ******************************************************************************/
824
825static void priv_status_resp(struct usb_serial_port *port)
826{
Alan Coxaf6d7802008-07-22 11:11:44 +0100827 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
829
830 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
831 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
832 pkt[2] = __cpu_to_le32(12);
833 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
834 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
835 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
836
Alan Coxaf6d7802008-07-22 11:11:44 +0100837 send_to_tty(port, (__u8 *)pkt, 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
839
840
841/******************************************************************************
842 * Garmin specific driver functions
843 ******************************************************************************/
844
845static int process_resetdev_request(struct usb_serial_port *port)
846{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200847 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int status;
Alan Coxaf6d7802008-07-22 11:11:44 +0100849 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200851 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
853 garmin_data_p->state = STATE_RESET;
854 garmin_data_p->serial_num = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200855 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Alan Coxaf6d7802008-07-22 11:11:44 +0100857 usb_kill_urb(port->interrupt_in_urb);
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700858 dev_dbg(&port->dev, "%s - usb_reset_device\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 status = usb_reset_device(port->serial->dev);
860 if (status)
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700861 dev_dbg(&port->dev, "%s - usb_reset_device failed: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800862 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return status;
864}
865
866
867
868/*
869 * clear all cached data
870 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100871static int garmin_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200873 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int status = 0;
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 /* flush all queued data */
877 pkt_clear(garmin_data_p);
878
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200879 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 garmin_data_p->insize = 0;
881 garmin_data_p->outsize = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200882 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 return status;
885}
886
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888static int garmin_init_session(struct usb_serial_port *port)
889{
890 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100891 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int status = 0;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200893 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 if (status == 0) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100896 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700898 dev_dbg(&serial->dev->dev, "%s - adding interrupt input\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
900 if (status)
901 dev_err(&serial->dev->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100902 "%s - failed submitting interrupt urb, error %d\n",
903 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200906 /*
907 * using the initialization method from gpsbabel. See comments in
908 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (status == 0) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700911 dev_dbg(&serial->dev->dev, "%s - starting session ...\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 garmin_data_p->state = STATE_ACTIVE;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200913
914 for (i = 0; i < 3; i++) {
915 status = garmin_write_bulk(port,
916 GARMIN_START_SESSION_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100917 sizeof(GARMIN_START_SESSION_REQ), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200919 if (status < 0)
920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200922
923 if (status > 0)
924 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
927 return status;
928}
929
930
931
Alan Coxa509a7e2009-09-19 13:13:26 -0700932static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200934 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int status = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100936 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200938 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 garmin_data_p->mode = initial_mode;
940 garmin_data_p->count = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200941 garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200942 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /* shutdown any bulk reads that might be going on */
Alan Coxaf6d7802008-07-22 11:11:44 +0100945 usb_kill_urb(port->write_urb);
946 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Alan Cox95da3102008-07-22 11:09:07 +0100948 if (garmin_data_p->state == STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 status = garmin_init_session(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 garmin_data_p->state = STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return status;
953}
954
955
Alan Cox335f8512009-06-11 12:26:29 +0100956static void garmin_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
958 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100959 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -0700961 dev_dbg(&port->dev, "%s - port %d - mode=%d state=%d flags=0x%X\n",
962 __func__, port->number, garmin_data_p->mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 garmin_data_p->state, garmin_data_p->flags);
964
965 if (!serial)
966 return;
967
Oliver Neukum95bef012008-01-22 13:56:18 +0100968 mutex_lock(&port->serial->disc_mutex);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200969
Oliver Neukum95bef012008-01-22 13:56:18 +0100970 if (!port->serial->disconnected)
971 garmin_clear(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /* shutdown our urbs */
Alan Coxaf6d7802008-07-22 11:11:44 +0100974 usb_kill_urb(port->read_urb);
975 usb_kill_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200977 /* keep reset state so we know that we must start a new session */
978 if (garmin_data_p->state != STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 garmin_data_p->state = STATE_DISCONNECTED;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200980
Oliver Neukum95bef012008-01-22 13:56:18 +0100981 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200984
Alan Coxaf6d7802008-07-22 11:11:44 +0100985static void garmin_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Ming Leicdc97792008-02-24 18:41:47 +0800987 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Hermann Kneissel468d1362007-08-03 20:20:33 +0200989 if (port) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100990 struct garmin_data *garmin_data_p =
991 usb_get_serial_port_data(port);
Hermann Kneissel468d1362007-08-03 20:20:33 +0200992
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200993 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
994
995 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
996 gsp_send_ack(garmin_data_p,
Alan Coxaf6d7802008-07-22 11:11:44 +0100997 ((__u8 *)urb->transfer_buffer)[4]);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200998 }
Hermann Kneissel468d1362007-08-03 20:20:33 +0200999 }
Hermann Kneissel468d1362007-08-03 20:20:33 +02001000 usb_serial_port_softint(port);
1001 }
1002
Alan Coxaf6d7802008-07-22 11:11:44 +01001003 /* Ignore errors that resulted from garmin_write_bulk with
1004 dismiss_ack = 1 */
Hermann Kneissel468d1362007-08-03 20:20:33 +02001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxaf6d7802008-07-22 11:11:44 +01001007 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010
Alan Coxaf6d7802008-07-22 11:11:44 +01001011static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001012 const unsigned char *buf, int count,
1013 int dismiss_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001015 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001017 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct urb *urb;
1019 unsigned char *buffer;
1020 int status;
1021
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001022 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001024 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Alan Coxaf6d7802008-07-22 11:11:44 +01001026 buffer = kmalloc(count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (!buffer) {
1028 dev_err(&port->dev, "out of memory\n");
1029 return -ENOMEM;
1030 }
1031
1032 urb = usb_alloc_urb(0, GFP_ATOMIC);
1033 if (!urb) {
1034 dev_err(&port->dev, "no more free urbs\n");
Alan Coxaf6d7802008-07-22 11:11:44 +01001035 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return -ENOMEM;
1037 }
1038
Alan Coxaf6d7802008-07-22 11:11:44 +01001039 memcpy(buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Harvey Harrison441b62c2008-03-03 16:08:34 -08001041 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Alan Coxaf6d7802008-07-22 11:11:44 +01001043 usb_fill_bulk_urb(urb, serial->dev,
1044 usb_sndbulkpipe(serial->dev,
1045 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 buffer, count,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001047 garmin_write_bulk_callback,
1048 dismiss_ack ? NULL : port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 urb->transfer_flags |= URB_ZERO_PACKET;
1050
1051 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001052
1053 spin_lock_irqsave(&garmin_data_p->lock, flags);
1054 garmin_data_p->flags |= APP_REQ_SEEN;
1055 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1058 pkt_clear(garmin_data_p);
1059 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1060 }
1061 }
1062
1063 /* send it down the pipe */
1064 status = usb_submit_urb(urb, GFP_ATOMIC);
1065 if (status) {
1066 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001067 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001068 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 count = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
1072 /* we are done with this urb, so let the host driver
1073 * really free it when it is finished with it */
Alan Coxaf6d7802008-07-22 11:11:44 +01001074 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return count;
1077}
1078
Alan Coxaf6d7802008-07-22 11:11:44 +01001079static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +01001080 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001082 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int pktid, pktsiz, len;
Alan Coxaf6d7802008-07-22 11:11:44 +01001084 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1086
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001087 usb_serial_debug_data(debug, dev, __func__, count, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001089 if (garmin_data_p->state == STATE_RESET)
1090 return -EIO;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* check for our private packets */
1093 if (count >= GARMIN_PKTHDR_LENGTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 len = PRIVPKTSIZ;
1095 if (count < len)
1096 len = count;
1097
1098 memcpy(garmin_data_p->privpkt, buf, len);
1099
1100 pktsiz = getDataLength(garmin_data_p->privpkt);
1101 pktid = getPacketId(garmin_data_p->privpkt);
1102
1103 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
Alan Coxaf6d7802008-07-22 11:11:44 +01001104 && GARMIN_LAYERID_PRIVATE ==
1105 getLayerId(garmin_data_p->privpkt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001107 dev_dbg(dev, "%s - processing private request %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001108 __func__, pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Alan Coxaf6d7802008-07-22 11:11:44 +01001110 /* drop all unfinished transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 garmin_clear(garmin_data_p);
1112
Alan Coxaf6d7802008-07-22 11:11:44 +01001113 switch (pktid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 case PRIV_PKTID_SET_DEBUG:
1116 if (pktsiz != 4)
1117 return -EINVPKT;
1118 debug = __le32_to_cpu(privpkt[3]);
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001119 dev_dbg(dev, "%s - debug level set to 0x%X\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001120 __func__, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 break;
1122
1123 case PRIV_PKTID_SET_MODE:
1124 if (pktsiz != 4)
1125 return -EINVPKT;
1126 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001127 dev_dbg(dev, "%s - mode set to %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001128 __func__, garmin_data_p->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130
1131 case PRIV_PKTID_INFO_REQ:
1132 priv_status_resp(port);
1133 break;
1134
1135 case PRIV_PKTID_RESET_REQ:
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001136 process_resetdev_request(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 break;
1138
1139 case PRIV_PKTID_SET_DEF_MODE:
1140 if (pktsiz != 4)
1141 return -EINVPKT;
1142 initial_mode = __le32_to_cpu(privpkt[3]);
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001143 dev_dbg(dev, "%s - initial_mode set to %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001144 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 garmin_data_p->mode);
1146 break;
1147 }
1148 return count;
1149 }
1150 }
1151
1152 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1153 return gsp_receive(garmin_data_p, buf, count);
1154 } else { /* MODE_NATIVE */
1155 return nat_receive(garmin_data_p, buf, count);
1156 }
1157}
1158
1159
Alan Cox95da3102008-07-22 11:09:07 +01001160static int garmin_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
Alan Cox95da3102008-07-22 11:09:07 +01001162 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /*
1164 * Report back the bytes currently available in the output buffer.
1165 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001166 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1168}
1169
1170
Alan Coxaf6d7802008-07-22 11:11:44 +01001171static void garmin_read_process(struct garmin_data *garmin_data_p,
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001172 unsigned char *data, unsigned data_length,
1173 int bulk_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001175 unsigned long flags;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1178 /* abort-transfer cmd is actice */
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001179 dev_dbg(&garmin_data_p->port->dev, "%s - pkt dropped\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
Alan Coxaf6d7802008-07-22 11:11:44 +01001181 garmin_data_p->state != STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* if throttling is active or postprecessing is required
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001184 put the received data in the input queue, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 send it directly to the tty port */
1186 if (garmin_data_p->flags & FLAGS_QUEUING) {
1187 pkt_add(garmin_data_p, data, data_length);
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001188 } else if (bulk_data ||
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001189 getLayerId(data) == GARMIN_LAYERID_APPL) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001190
1191 spin_lock_irqsave(&garmin_data_p->lock, flags);
1192 garmin_data_p->flags |= APP_RESP_SEEN;
1193 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1194
1195 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 pkt_add(garmin_data_p, data, data_length);
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001197 } else {
1198 send_to_tty(garmin_data_p->port, data,
1199 data_length);
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001202 /* ignore system layer packets ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204}
1205
1206
Alan Coxaf6d7802008-07-22 11:11:44 +01001207static void garmin_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001209 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001210 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001212 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001214 int status = urb->status;
1215 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!serial) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001218 dev_dbg(&urb->dev->dev, "%s - bad serial pointer, exiting\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return;
1220 }
1221
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001222 if (status) {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001223 dev_dbg(&urb->dev->dev, "%s - nonzero read bulk status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001224 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return;
1226 }
1227
Alan Coxaf6d7802008-07-22 11:11:44 +01001228 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -08001229 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001231 garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001233 if (urb->actual_length == 0 &&
1234 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1235 spin_lock_irqsave(&garmin_data_p->lock, flags);
1236 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1237 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001238 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1239 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 dev_err(&port->dev,
1241 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001242 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001243 } else if (urb->actual_length > 0) {
1244 /* Continue trying to read until nothing more is received */
1245 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001246 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1247 if (retval)
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001248 dev_err(&port->dev,
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001249 "%s - failed resubmitting read urb, error %d\n",
1250 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001251 }
1252 } else {
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001253 dev_dbg(&port->dev, "%s - end of bulk data\n", __func__);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001254 spin_lock_irqsave(&garmin_data_p->lock, flags);
1255 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1256 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260
Alan Coxaf6d7802008-07-22 11:11:44 +01001261static void garmin_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001263 unsigned long flags;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001264 int retval;
Ming Leicdc97792008-02-24 18:41:47 +08001265 struct usb_serial_port *port = urb->context;
Alan Coxaf6d7802008-07-22 11:11:44 +01001266 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001268 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001270 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 case 0:
1272 /* success */
1273 break;
1274 case -ECONNRESET:
1275 case -ENOENT:
1276 case -ESHUTDOWN:
1277 /* this urb is terminated, clean up */
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001278 dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001279 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return;
1281 default:
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001282 dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001283 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return;
1285 }
1286
Harvey Harrison441b62c2008-03-03 16:08:34 -08001287 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 urb->actual_length, urb->transfer_buffer);
1289
1290 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1291 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001292 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001294 dev_dbg(&port->dev, "%s - bulk data available.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001296 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1297
1298 /* bulk data available */
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001299 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1300 if (retval) {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001301 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001302 "%s - failed submitting read urb, error %d\n",
1303 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001304 } else {
1305 spin_lock_irqsave(&garmin_data_p->lock, flags);
1306 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
Alan Coxaf6d7802008-07-22 11:11:44 +01001307 spin_unlock_irqrestore(&garmin_data_p->lock,
1308 flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001309 }
1310 } else {
1311 /* bulk-in transfer still active */
1312 spin_lock_irqsave(&garmin_data_p->lock, flags);
1313 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1314 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
1317 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1318 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001319 sizeof(GARMIN_START_SESSION_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001321 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001323 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 /* save the serial number */
Alan Coxaf6d7802008-07-22 11:11:44 +01001326 garmin_data_p->serial_num = __le32_to_cpup(
1327 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Greg Kroah-Hartman7065e822012-09-14 11:50:30 -07001329 dev_dbg(&port->dev, "%s - start-of-session reply seen - serial %u.\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001330 __func__, garmin_data_p->serial_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001333 garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Alan Coxaf6d7802008-07-22 11:11:44 +01001335 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001336 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 dev_err(&urb->dev->dev,
1338 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001339 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342
1343/*
1344 * Sends the next queued packt to the tty port (garmin native mode only)
1345 * and then sets a timer to call itself again until all queued data
1346 * is sent.
1347 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001348static int garmin_flush_queue(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001350 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct garmin_packet *pkt;
1352
1353 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1354 pkt = pkt_pop(garmin_data_p);
1355 if (pkt != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1357 kfree(pkt);
1358 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1359
1360 } else {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001361 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 garmin_data_p->flags &= ~FLAGS_QUEUING;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001363 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 }
1366 return 0;
1367}
1368
1369
Alan Cox95da3102008-07-22 11:09:07 +01001370static void garmin_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Alan Cox95da3102008-07-22 11:09:07 +01001372 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001373 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /* set flag, data received will be put into a queue
1376 for later processing */
Oliver Neukum63832512009-10-07 10:50:23 +02001377 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001379 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382
Alan Coxaf6d7802008-07-22 11:11:44 +01001383static void garmin_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Alan Cox95da3102008-07-22 11:09:07 +01001385 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001386 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001387 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Oliver Neukum63832512009-10-07 10:50:23 +02001389 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 garmin_data_p->flags &= ~FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001391 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 /* in native mode send queued data to tty, in
1394 serial mode nothing needs to be done here */
1395 if (garmin_data_p->mode == MODE_NATIVE)
1396 garmin_flush_queue(garmin_data_p);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001397
1398 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
Oliver Neukum63832512009-10-07 10:50:23 +02001399 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001400 if (status)
1401 dev_err(&port->dev,
1402 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001403 __func__, status);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407/*
1408 * The timer is currently only used to send queued packets to
1409 * the tty in cases where the protocol provides no own handshaking
1410 * to initiate the transfer.
1411 */
1412static void timeout_handler(unsigned long data)
1413{
1414 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1415
1416 /* send the next queued packet to the tty port */
1417 if (garmin_data_p->mode == MODE_NATIVE)
1418 if (garmin_data_p->flags & FLAGS_QUEUING)
1419 garmin_flush_queue(garmin_data_p);
1420}
1421
1422
1423
Alan Cox95da3102008-07-22 11:09:07 +01001424static int garmin_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 int status = 0;
1427 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001428 struct garmin_data *garmin_data_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Burman Yan7ac9da12006-11-22 20:54:38 +02001430 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (garmin_data_p == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001432 dev_err(&port->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return -ENOMEM;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 init_timer(&garmin_data_p->timer);
1436 spin_lock_init(&garmin_data_p->lock);
1437 INIT_LIST_HEAD(&garmin_data_p->pktlist);
Alan Coxaf6d7802008-07-22 11:11:44 +01001438 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1440 garmin_data_p->timer.function = timeout_handler;
1441 garmin_data_p->port = port;
1442 garmin_data_p->state = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001443 garmin_data_p->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 garmin_data_p->count = 0;
1445 usb_set_serial_port_data(port, garmin_data_p);
1446
1447 status = garmin_init_session(port);
1448
1449 return status;
1450}
1451
1452
Alan Sternf9c99bb2009-06-02 11:53:55 -04001453static void garmin_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001456 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Alan Coxaf6d7802008-07-22 11:11:44 +01001458 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 del_timer_sync(&garmin_data_p->timer);
Alan Sternf9c99bb2009-06-02 11:53:55 -04001460}
1461
1462
1463static void garmin_release(struct usb_serial *serial)
1464{
1465 struct usb_serial_port *port = serial->port[0];
1466 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1467
Alan Coxaf6d7802008-07-22 11:11:44 +01001468 kfree(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001473static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001474 .driver = {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001475 .owner = THIS_MODULE,
1476 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001477 },
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001478 .description = "Garmin GPS usb/tty",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 .num_ports = 1,
1481 .open = garmin_open,
1482 .close = garmin_close,
1483 .throttle = garmin_throttle,
1484 .unthrottle = garmin_unthrottle,
1485 .attach = garmin_attach,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001486 .disconnect = garmin_disconnect,
1487 .release = garmin_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 .write = garmin_write,
1489 .write_room = garmin_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 .write_bulk_callback = garmin_write_bulk_callback,
1491 .read_bulk_callback = garmin_read_bulk_callback,
1492 .read_int_callback = garmin_read_int_callback,
1493};
1494
Alan Stern97b6b6d2012-02-23 14:56:32 -05001495static struct usb_serial_driver * const serial_drivers[] = {
1496 &garmin_device, NULL
1497};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001499module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Alan Coxaf6d7802008-07-22 11:11:44 +01001501MODULE_AUTHOR(DRIVER_AUTHOR);
1502MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503MODULE_LICENSE("GPL");
1504
1505module_param(debug, bool, S_IWUSR | S_IRUGO);
1506MODULE_PARM_DESC(debug, "Debug enabled or not");
1507module_param(initial_mode, int, S_IRUGO);
1508MODULE_PARM_DESC(initial_mode, "Initial mode");