blob: 346c15a510663cbda3d4db9f6508b6717cee69fd [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
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200306 dbg("%s - added: pkt: %d - %d bytes",
307 __func__, pkt->seq, data_length);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* in serial mode, if someone is waiting for data from
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200310 the device, convert and send the next packet to tty. */
Alan Coxaf6d7802008-07-22 11:11:44 +0100311 if (result && (state == STATE_GSP_WAIT_DATA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 return result;
315}
316
317
318/* get the next pending packet */
Alan Coxaf6d7802008-07-22 11:11:44 +0100319static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 unsigned long flags;
322 struct garmin_packet *result = NULL;
323
324 spin_lock_irqsave(&garmin_data_p->lock, flags);
325 if (!list_empty(&garmin_data_p->pktlist)) {
326 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
327 list_del(&result->list);
328 }
329 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
330 return result;
331}
332
333
334/* free up all queued data */
Alan Coxaf6d7802008-07-22 11:11:44 +0100335static void pkt_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 unsigned long flags;
338 struct garmin_packet *result = NULL;
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 spin_lock_irqsave(&garmin_data_p->lock, flags);
341 while (!list_empty(&garmin_data_p->pktlist)) {
342 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
343 list_del(&result->list);
344 kfree(result);
345 }
346 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
347}
348
349
350/******************************************************************************
351 * garmin serial protocol handling handling
352 ******************************************************************************/
353
354/* send an ack packet back to the tty */
Alan Coxaf6d7802008-07-22 11:11:44 +0100355static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 __u8 pkt[10];
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200358 __u8 cksum = 0;
359 __u8 *ptr = pkt;
360 unsigned l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Harvey Harrison441b62c2008-03-03 16:08:34 -0800362 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 *ptr++ = DLE;
365 *ptr++ = ACK;
366 cksum += ACK;
367
368 *ptr++ = 2;
369 cksum += 2;
370
371 *ptr++ = pkt_id;
372 cksum += pkt_id;
373
Alan Coxaf6d7802008-07-22 11:11:44 +0100374 if (pkt_id == DLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *ptr++ = DLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 *ptr++ = 0;
378 *ptr++ = 0xFF & (-cksum);
379 *ptr++ = DLE;
380 *ptr++ = ETX;
381
382 l = ptr-pkt;
383
384 send_to_tty(garmin_data_p->port, pkt, l);
385 return 0;
386}
387
388
389
390/*
391 * called for a complete packet received from tty layer
392 *
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200393 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * at GSP_INITIAL_OFFSET.
395 *
396 * count - number of bytes in the input buffer including space reserved for
Alan Coxaf6d7802008-07-22 11:11:44 +0100397 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * (including pkt-id, data-length a. cksum)
399 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100400static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200402 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100403 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200404 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 int cksum = 0;
407 int n = 0;
408 int pktid = recpkt[0];
409 int size = recpkt[1];
410
411 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800412 __func__, count-GSP_INITIAL_OFFSET, recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (size != (count-GSP_INITIAL_OFFSET-3)) {
415 dbg("%s - invalid size, expected %d bytes, got %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800416 __func__, size, (count-GSP_INITIAL_OFFSET-3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return -EINVPKT;
418 }
419
420 cksum += *recpkt++;
421 cksum += *recpkt++;
422
Alan Coxaf6d7802008-07-22 11:11:44 +0100423 /* sanity check, remove after test ... */
424 if ((__u8 *)&(usbdata[3]) != recpkt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 dbg("%s - ptr mismatch %p - %p",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800426 __func__, &(usbdata[4]), recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EINVPKT;
428 }
429
430 while (n < size) {
431 cksum += *recpkt++;
432 n++;
433 }
434
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200435 if ((0xff & (cksum + *recpkt)) != 0) {
436 dbg("%s - invalid checksum, expected %02x, got %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 __func__, 0xff & -cksum, 0xff & *recpkt);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200438 return -EINVPKT;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
442 usbdata[1] = __cpu_to_le32(pktid);
443 usbdata[2] = __cpu_to_le32(size);
444
Alan Coxaf6d7802008-07-22 11:11:44 +0100445 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200446 GARMIN_PKTHDR_LENGTH+size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /* if this was an abort-transfer command, flush all
449 queued data. */
450 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200451 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 garmin_data_p->flags |= FLAGS_DROP_DATA;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200453 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 pkt_clear(garmin_data_p);
455 }
456
457 return count;
458}
459
460
461
462/*
463 * Called for data received from tty
464 *
465 * buf contains the data read, it may span more than one packet or even
466 * incomplete packets
467 *
468 * input record should be a serial-record, but it may not be complete.
469 * Copy it into our local buffer, until an etx is seen (or an error
470 * occurs).
471 * Once the record is complete, convert into a usb packet and send it
472 * to the bulk pipe, send an ack back to the tty.
473 *
474 * If the input is an ack, just send the last queued packet to the
475 * tty layer.
476 *
477 * if the input is an abort command, drop all queued data.
478 */
479
Alan Coxaf6d7802008-07-22 11:11:44 +0100480static int gsp_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200481 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200483 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int offs = 0;
485 int ack_or_nak_seen = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200486 __u8 *dest;
487 int size;
Alan Coxaf6d7802008-07-22 11:11:44 +0100488 /* dleSeen: set if last byte read was a DLE */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200489 int dleSeen;
Alan Coxaf6d7802008-07-22 11:11:44 +0100490 /* skip: if set, skip incoming data until possible start of
491 * new packet
492 */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200493 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 __u8 data;
495
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200496 spin_lock_irqsave(&garmin_data_p->lock, flags);
497 dest = garmin_data_p->inbuffer;
498 size = garmin_data_p->insize;
499 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
500 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
501 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
502
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200503 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
504 __func__, dleSeen, skip, size, count); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Alan Coxaf6d7802008-07-22 11:11:44 +0100506 if (size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 size = GSP_INITIAL_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 while (offs < count) {
510
511 data = *(buf+offs);
Alan Coxaf6d7802008-07-22 11:11:44 +0100512 offs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 if (data == DLE) {
515 if (skip) { /* start of a new pkt */
516 skip = 0;
517 size = GSP_INITIAL_OFFSET;
518 dleSeen = 1;
519 } else if (dleSeen) {
520 dest[size++] = data;
521 dleSeen = 0;
522 } else {
523 dleSeen = 1;
524 }
525 } else if (data == ETX) {
526 if (dleSeen) {
527 /* packet complete */
528
529 data = dest[GSP_INITIAL_OFFSET];
530
531 if (data == ACK) {
532 ack_or_nak_seen = ACK;
533 dbg("ACK packet complete.");
534 } else if (data == NAK) {
535 ack_or_nak_seen = NAK;
536 dbg("NAK packet complete.");
537 } else {
Alan Coxaf6d7802008-07-22 11:11:44 +0100538 dbg("packet complete - id=0x%X.",
539 0xFF & data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 gsp_rec_packet(garmin_data_p, size);
541 }
542
543 skip = 1;
544 size = GSP_INITIAL_OFFSET;
545 dleSeen = 0;
546 } else {
547 dest[size++] = data;
548 }
549 } else if (!skip) {
550
551 if (dleSeen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 size = GSP_INITIAL_OFFSET;
553 dleSeen = 0;
554 }
555
556 dest[size++] = data;
557 }
558
559 if (size >= GPS_IN_BUFSIZ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800560 dbg("%s - packet too large.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 skip = 1;
562 size = GSP_INITIAL_OFFSET;
563 dleSeen = 0;
564 }
565 }
566
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200567 spin_lock_irqsave(&garmin_data_p->lock, flags);
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 garmin_data_p->insize = size;
570
Alan Coxaf6d7802008-07-22 11:11:44 +0100571 /* copy flags back to structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (skip)
573 garmin_data_p->flags |= FLAGS_GSP_SKIP;
574 else
575 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
576
577 if (dleSeen)
578 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
579 else
580 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
581
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200582 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
583
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200584 if (ack_or_nak_seen) {
585 if (gsp_next_packet(garmin_data_p) > 0)
586 garmin_data_p->state = STATE_ACTIVE;
587 else
588 garmin_data_p->state = STATE_GSP_WAIT_DATA;
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return count;
591}
592
593
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * Sends a usb packet to the tty
597 *
598 * Assumes, that all packages and at an usb-packet boundary.
599 *
600 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
601 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100602static int gsp_send(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 const unsigned char *buf, int count)
604{
605 const unsigned char *src;
606 unsigned char *dst;
607 int pktid = 0;
608 int datalen = 0;
609 int cksum = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100610 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int k;
612
Harvey Harrison441b62c2008-03-03 16:08:34 -0800613 dbg("%s - state %d - %d bytes.", __func__,
Alan Coxaf6d7802008-07-22 11:11:44 +0100614 garmin_data_p->state, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 k = garmin_data_p->outsize;
617 if ((k+count) > GPS_OUT_BUFSIZ) {
618 dbg("packet too large");
619 garmin_data_p->outsize = 0;
620 return -4;
621 }
622
623 memcpy(garmin_data_p->outbuffer+k, buf, count);
624 k += count;
625 garmin_data_p->outsize = k;
626
627 if (k >= GARMIN_PKTHDR_LENGTH) {
628 pktid = getPacketId(garmin_data_p->outbuffer);
Alan Coxaf6d7802008-07-22 11:11:44 +0100629 datalen = getDataLength(garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 i = GARMIN_PKTHDR_LENGTH + datalen;
631 if (k < i)
632 return 0;
633 } else {
634 return 0;
635 }
636
Alan Coxaf6d7802008-07-22 11:11:44 +0100637 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* garmin_data_p->outbuffer now contains a complete packet */
640
641 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100642 __func__, k, garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 garmin_data_p->outsize = 0;
645
646 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100647 dbg("not an application packet (%d)",
648 getLayerId(garmin_data_p->outbuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -1;
650 }
651
652 if (pktid > 255) {
653 dbg("packet-id %d too large", pktid);
654 return -2;
655 }
656
657 if (datalen > 255) {
658 dbg("packet-size %d too large", datalen);
659 return -3;
660 }
661
662 /* the serial protocol should be able to handle this packet */
663
664 k = 0;
665 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
Alan Coxaf6d7802008-07-22 11:11:44 +0100666 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (*src++ == DLE)
668 k++;
669 }
670
671 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
672 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100673 /* can't add stuffing DLEs in place, move data to end
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200674 of buffer ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
676 memcpy(dst, src, datalen);
677 src = dst;
678 }
679
680 dst = garmin_data_p->outbuffer;
681
682 *dst++ = DLE;
683 *dst++ = pktid;
684 cksum += pktid;
685 *dst++ = datalen;
686 cksum += datalen;
687 if (datalen == DLE)
688 *dst++ = DLE;
689
Alan Coxaf6d7802008-07-22 11:11:44 +0100690 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 __u8 c = *src++;
692 *dst++ = c;
693 cksum += c;
694 if (c == DLE)
695 *dst++ = DLE;
696 }
Alan Coxaf6d7802008-07-22 11:11:44 +0100697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 cksum = 0xFF & -cksum;
699 *dst++ = cksum;
700 if (cksum == DLE)
701 *dst++ = DLE;
702 *dst++ = DLE;
703 *dst++ = ETX;
704
705 i = dst-garmin_data_p->outbuffer;
706
707 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
708
709 garmin_data_p->pkt_id = pktid;
710 garmin_data_p->state = STATE_WAIT_TTY_ACK;
711
712 return i;
713}
714
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/*
717 * Process the next pending data packet - if there is one
718 */
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200719static int gsp_next_packet(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200721 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct garmin_packet *pkt = NULL;
723
724 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800725 dbg("%s - next pkt: %d", __func__, pkt->seq);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200726 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
727 if (result > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 kfree(pkt);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200729 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731 kfree(pkt);
732 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200733 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
736
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/******************************************************************************
739 * garmin native mode
740 ******************************************************************************/
741
742
743/*
744 * Called for data received from tty
745 *
746 * The input data is expected to be in garmin usb-packet format.
747 *
748 * buf contains the data read, it may span more than one packet
749 * or even incomplete packets
750 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100751static int nat_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200752 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200754 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100755 __u8 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 int offs = 0;
757 int result = count;
758 int len;
759
760 while (offs < count) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100761 /* if buffer contains header, copy rest of data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
763 len = GARMIN_PKTHDR_LENGTH
764 +getDataLength(garmin_data_p->inbuffer);
765 else
766 len = GARMIN_PKTHDR_LENGTH;
767
768 if (len >= GPS_IN_BUFSIZ) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100769 /* seems to be an invalid packet, ignore rest
770 of input */
771 dbg("%s - packet size too large: %d", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 garmin_data_p->insize = 0;
773 count = 0;
774 result = -EINVPKT;
775 } else {
776 len -= garmin_data_p->insize;
777 if (len > (count-offs))
778 len = (count-offs);
779 if (len > 0) {
780 dest = garmin_data_p->inbuffer
Alan Coxaf6d7802008-07-22 11:11:44 +0100781 + garmin_data_p->insize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 memcpy(dest, buf+offs, len);
783 garmin_data_p->insize += len;
784 offs += len;
785 }
786 }
787
788 /* do we have a complete packet ? */
789 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
790 len = GARMIN_PKTHDR_LENGTH+
791 getDataLength(garmin_data_p->inbuffer);
792 if (garmin_data_p->insize >= len) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100793 garmin_write_bulk(garmin_data_p->port,
794 garmin_data_p->inbuffer,
795 len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 garmin_data_p->insize = 0;
797
798 /* if this was an abort-transfer command,
799 flush all queued data. */
800 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100801 spin_lock_irqsave(&garmin_data_p->lock,
802 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 garmin_data_p->flags |= FLAGS_DROP_DATA;
Alan Coxaf6d7802008-07-22 11:11:44 +0100804 spin_unlock_irqrestore(
805 &garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 pkt_clear(garmin_data_p);
807 }
808 }
809 }
810 }
811 return result;
812}
813
814
815/******************************************************************************
816 * private packets
817 ******************************************************************************/
818
819static void priv_status_resp(struct usb_serial_port *port)
820{
Alan Coxaf6d7802008-07-22 11:11:44 +0100821 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
823
824 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
825 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
826 pkt[2] = __cpu_to_le32(12);
827 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
828 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
829 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
830
Alan Coxaf6d7802008-07-22 11:11:44 +0100831 send_to_tty(port, (__u8 *)pkt, 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834
835/******************************************************************************
836 * Garmin specific driver functions
837 ******************************************************************************/
838
839static int process_resetdev_request(struct usb_serial_port *port)
840{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200841 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int status;
Alan Coxaf6d7802008-07-22 11:11:44 +0100843 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200845 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
847 garmin_data_p->state = STATE_RESET;
848 garmin_data_p->serial_num = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200849 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Alan Coxaf6d7802008-07-22 11:11:44 +0100851 usb_kill_urb(port->interrupt_in_urb);
852 dbg("%s - usb_reset_device", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 status = usb_reset_device(port->serial->dev);
854 if (status)
855 dbg("%s - usb_reset_device failed: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800856 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return status;
858}
859
860
861
862/*
863 * clear all cached data
864 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100865static int garmin_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200867 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int status = 0;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* flush all queued data */
871 pkt_clear(garmin_data_p);
872
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200873 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 garmin_data_p->insize = 0;
875 garmin_data_p->outsize = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200876 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return status;
879}
880
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882static int garmin_init_session(struct usb_serial_port *port)
883{
884 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100885 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 int status = 0;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200887 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 if (status == 0) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100890 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Harvey Harrison441b62c2008-03-03 16:08:34 -0800892 dbg("%s - adding interrupt input", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
894 if (status)
895 dev_err(&serial->dev->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100896 "%s - failed submitting interrupt urb, error %d\n",
897 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200900 /*
901 * using the initialization method from gpsbabel. See comments in
902 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
903 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (status == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800905 dbg("%s - starting session ...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 garmin_data_p->state = STATE_ACTIVE;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200907
908 for (i = 0; i < 3; i++) {
909 status = garmin_write_bulk(port,
910 GARMIN_START_SESSION_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100911 sizeof(GARMIN_START_SESSION_REQ), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200913 if (status < 0)
914 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200916
917 if (status > 0)
918 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921 return status;
922}
923
924
925
Alan Coxa509a7e2009-09-19 13:13:26 -0700926static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200928 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int status = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100930 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200932 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 garmin_data_p->mode = initial_mode;
934 garmin_data_p->count = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200935 garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200936 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /* shutdown any bulk reads that might be going on */
Alan Coxaf6d7802008-07-22 11:11:44 +0100939 usb_kill_urb(port->write_urb);
940 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Alan Cox95da3102008-07-22 11:09:07 +0100942 if (garmin_data_p->state == STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 status = garmin_init_session(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 garmin_data_p->state = STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return status;
947}
948
949
Alan Cox335f8512009-06-11 12:26:29 +0100950static void garmin_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100953 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Harvey Harrison441b62c2008-03-03 16:08:34 -0800955 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 port->number, garmin_data_p->mode,
957 garmin_data_p->state, garmin_data_p->flags);
958
959 if (!serial)
960 return;
961
Oliver Neukum95bef012008-01-22 13:56:18 +0100962 mutex_lock(&port->serial->disc_mutex);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200963
Oliver Neukum95bef012008-01-22 13:56:18 +0100964 if (!port->serial->disconnected)
965 garmin_clear(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 /* shutdown our urbs */
Alan Coxaf6d7802008-07-22 11:11:44 +0100968 usb_kill_urb(port->read_urb);
969 usb_kill_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200971 /* keep reset state so we know that we must start a new session */
972 if (garmin_data_p->state != STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 garmin_data_p->state = STATE_DISCONNECTED;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200974
Oliver Neukum95bef012008-01-22 13:56:18 +0100975 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200978
Alan Coxaf6d7802008-07-22 11:11:44 +0100979static void garmin_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Ming Leicdc97792008-02-24 18:41:47 +0800981 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Hermann Kneissel468d1362007-08-03 20:20:33 +0200983 if (port) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100984 struct garmin_data *garmin_data_p =
985 usb_get_serial_port_data(port);
Hermann Kneissel468d1362007-08-03 20:20:33 +0200986
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200987 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
988
989 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
990 gsp_send_ack(garmin_data_p,
Alan Coxaf6d7802008-07-22 11:11:44 +0100991 ((__u8 *)urb->transfer_buffer)[4]);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200992 }
Hermann Kneissel468d1362007-08-03 20:20:33 +0200993 }
Hermann Kneissel468d1362007-08-03 20:20:33 +0200994 usb_serial_port_softint(port);
995 }
996
Alan Coxaf6d7802008-07-22 11:11:44 +0100997 /* Ignore errors that resulted from garmin_write_bulk with
998 dismiss_ack = 1 */
Hermann Kneissel468d1362007-08-03 20:20:33 +0200999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxaf6d7802008-07-22 11:11:44 +01001001 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004
Alan Coxaf6d7802008-07-22 11:11:44 +01001005static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001006 const unsigned char *buf, int count,
1007 int dismiss_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001009 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001011 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 struct urb *urb;
1013 unsigned char *buffer;
1014 int status;
1015
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001016 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001018 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Alan Coxaf6d7802008-07-22 11:11:44 +01001020 buffer = kmalloc(count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (!buffer) {
1022 dev_err(&port->dev, "out of memory\n");
1023 return -ENOMEM;
1024 }
1025
1026 urb = usb_alloc_urb(0, GFP_ATOMIC);
1027 if (!urb) {
1028 dev_err(&port->dev, "no more free urbs\n");
Alan Coxaf6d7802008-07-22 11:11:44 +01001029 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -ENOMEM;
1031 }
1032
Alan Coxaf6d7802008-07-22 11:11:44 +01001033 memcpy(buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Harvey Harrison441b62c2008-03-03 16:08:34 -08001035 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Alan Coxaf6d7802008-07-22 11:11:44 +01001037 usb_fill_bulk_urb(urb, serial->dev,
1038 usb_sndbulkpipe(serial->dev,
1039 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 buffer, count,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001041 garmin_write_bulk_callback,
1042 dismiss_ack ? NULL : port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 urb->transfer_flags |= URB_ZERO_PACKET;
1044
1045 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001046
1047 spin_lock_irqsave(&garmin_data_p->lock, flags);
1048 garmin_data_p->flags |= APP_REQ_SEEN;
1049 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1052 pkt_clear(garmin_data_p);
1053 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1054 }
1055 }
1056
1057 /* send it down the pipe */
1058 status = usb_submit_urb(urb, GFP_ATOMIC);
1059 if (status) {
1060 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001061 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001062 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 count = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 /* we are done with this urb, so let the host driver
1067 * really free it when it is finished with it */
Alan Coxaf6d7802008-07-22 11:11:44 +01001068 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 return count;
1071}
1072
Alan Coxaf6d7802008-07-22 11:11:44 +01001073static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +01001074 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 int pktid, pktsiz, len;
Alan Coxaf6d7802008-07-22 11:11:44 +01001077 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1079
Harvey Harrison441b62c2008-03-03 16:08:34 -08001080 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001082 if (garmin_data_p->state == STATE_RESET)
1083 return -EIO;
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* check for our private packets */
1086 if (count >= GARMIN_PKTHDR_LENGTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 len = PRIVPKTSIZ;
1088 if (count < len)
1089 len = count;
1090
1091 memcpy(garmin_data_p->privpkt, buf, len);
1092
1093 pktsiz = getDataLength(garmin_data_p->privpkt);
1094 pktid = getPacketId(garmin_data_p->privpkt);
1095
1096 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
Alan Coxaf6d7802008-07-22 11:11:44 +01001097 && GARMIN_LAYERID_PRIVATE ==
1098 getLayerId(garmin_data_p->privpkt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 dbg("%s - processing private request %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001101 __func__, pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Alan Coxaf6d7802008-07-22 11:11:44 +01001103 /* drop all unfinished transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 garmin_clear(garmin_data_p);
1105
Alan Coxaf6d7802008-07-22 11:11:44 +01001106 switch (pktid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 case PRIV_PKTID_SET_DEBUG:
1109 if (pktsiz != 4)
1110 return -EINVPKT;
1111 debug = __le32_to_cpu(privpkt[3]);
1112 dbg("%s - debug level set to 0x%X",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001113 __func__, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 break;
1115
1116 case PRIV_PKTID_SET_MODE:
1117 if (pktsiz != 4)
1118 return -EINVPKT;
1119 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1120 dbg("%s - mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001121 __func__, garmin_data_p->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 break;
1123
1124 case PRIV_PKTID_INFO_REQ:
1125 priv_status_resp(port);
1126 break;
1127
1128 case PRIV_PKTID_RESET_REQ:
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001129 process_resetdev_request(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 break;
1131
1132 case PRIV_PKTID_SET_DEF_MODE:
1133 if (pktsiz != 4)
1134 return -EINVPKT;
1135 initial_mode = __le32_to_cpu(privpkt[3]);
1136 dbg("%s - initial_mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001137 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 garmin_data_p->mode);
1139 break;
1140 }
1141 return count;
1142 }
1143 }
1144
1145 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1146 return gsp_receive(garmin_data_p, buf, count);
1147 } else { /* MODE_NATIVE */
1148 return nat_receive(garmin_data_p, buf, count);
1149 }
1150}
1151
1152
Alan Cox95da3102008-07-22 11:09:07 +01001153static int garmin_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Alan Cox95da3102008-07-22 11:09:07 +01001155 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /*
1157 * Report back the bytes currently available in the output buffer.
1158 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001159 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1161}
1162
1163
Alan Coxaf6d7802008-07-22 11:11:44 +01001164static void garmin_read_process(struct garmin_data *garmin_data_p,
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001165 unsigned char *data, unsigned data_length,
1166 int bulk_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001168 unsigned long flags;
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1171 /* abort-transfer cmd is actice */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001172 dbg("%s - pkt dropped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
Alan Coxaf6d7802008-07-22 11:11:44 +01001174 garmin_data_p->state != STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* if throttling is active or postprecessing is required
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001177 put the received data in the input queue, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 send it directly to the tty port */
1179 if (garmin_data_p->flags & FLAGS_QUEUING) {
1180 pkt_add(garmin_data_p, data, data_length);
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001181 } else if (bulk_data ||
1182 getLayerId(data) == GARMIN_LAYERID_APPL) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001183
1184 spin_lock_irqsave(&garmin_data_p->lock, flags);
1185 garmin_data_p->flags |= APP_RESP_SEEN;
1186 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1187
1188 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 pkt_add(garmin_data_p, data, data_length);
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001190 } else {
1191 send_to_tty(garmin_data_p->port, data,
1192 data_length);
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001195 /* ignore system layer packets ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197}
1198
1199
Alan Coxaf6d7802008-07-22 11:11:44 +01001200static void garmin_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001202 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001203 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001205 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001207 int status = urb->status;
1208 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (!serial) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001211 dbg("%s - bad serial pointer, exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return;
1213 }
1214
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001215 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001217 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return;
1219 }
1220
Alan Coxaf6d7802008-07-22 11:11:44 +01001221 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -08001222 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001224 garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001226 if (urb->actual_length == 0 &&
1227 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1228 spin_lock_irqsave(&garmin_data_p->lock, flags);
1229 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1230 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001231 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1232 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 dev_err(&port->dev,
1234 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001235 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001236 } else if (urb->actual_length > 0) {
1237 /* Continue trying to read until nothing more is received */
1238 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001239 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1240 if (retval)
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001241 dev_err(&port->dev,
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001242 "%s - failed resubmitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001243 "error %d\n", __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001244 }
1245 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001246 dbg("%s - end of bulk data", __func__);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001247 spin_lock_irqsave(&garmin_data_p->lock, flags);
1248 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1249 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253
Alan Coxaf6d7802008-07-22 11:11:44 +01001254static void garmin_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001256 unsigned long flags;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001257 int retval;
Ming Leicdc97792008-02-24 18:41:47 +08001258 struct usb_serial_port *port = urb->context;
Alan Coxaf6d7802008-07-22 11:11:44 +01001259 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001261 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001263 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 case 0:
1265 /* success */
1266 break;
1267 case -ECONNRESET:
1268 case -ENOENT:
1269 case -ESHUTDOWN:
1270 /* this urb is terminated, clean up */
1271 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001272 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return;
1274 default:
1275 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001276 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return;
1278 }
1279
Harvey Harrison441b62c2008-03-03 16:08:34 -08001280 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 urb->actual_length, urb->transfer_buffer);
1282
1283 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1284 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001285 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Harvey Harrison441b62c2008-03-03 16:08:34 -08001287 dbg("%s - bulk data available.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001289 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1290
1291 /* bulk data available */
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001292 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1293 if (retval) {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001294 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001295 "%s - failed submitting read urb, error %d\n",
1296 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001297 } else {
1298 spin_lock_irqsave(&garmin_data_p->lock, flags);
1299 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
Alan Coxaf6d7802008-07-22 11:11:44 +01001300 spin_unlock_irqrestore(&garmin_data_p->lock,
1301 flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001302 }
1303 } else {
1304 /* bulk-in transfer still active */
1305 spin_lock_irqsave(&garmin_data_p->lock, flags);
1306 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1307 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
1310 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1311 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001312 sizeof(GARMIN_START_SESSION_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001314 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001316 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /* save the serial number */
Alan Coxaf6d7802008-07-22 11:11:44 +01001319 garmin_data_p->serial_num = __le32_to_cpup(
1320 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 dbg("%s - start-of-session reply seen - serial %u.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001323 __func__, garmin_data_p->serial_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001326 garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Alan Coxaf6d7802008-07-22 11:11:44 +01001328 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001329 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 dev_err(&urb->dev->dev,
1331 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001332 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
1335
1336/*
1337 * Sends the next queued packt to the tty port (garmin native mode only)
1338 * and then sets a timer to call itself again until all queued data
1339 * is sent.
1340 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001341static int garmin_flush_queue(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001343 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 struct garmin_packet *pkt;
1345
1346 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1347 pkt = pkt_pop(garmin_data_p);
1348 if (pkt != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1350 kfree(pkt);
1351 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1352
1353 } else {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001354 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 garmin_data_p->flags &= ~FLAGS_QUEUING;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001356 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358 }
1359 return 0;
1360}
1361
1362
Alan Cox95da3102008-07-22 11:09:07 +01001363static void garmin_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Alan Cox95da3102008-07-22 11:09:07 +01001365 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001366 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /* set flag, data received will be put into a queue
1369 for later processing */
Oliver Neukum63832512009-10-07 10:50:23 +02001370 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001372 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
1375
Alan Coxaf6d7802008-07-22 11:11:44 +01001376static void garmin_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Alan Cox95da3102008-07-22 11:09:07 +01001378 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001379 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001380 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Oliver Neukum63832512009-10-07 10:50:23 +02001382 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 garmin_data_p->flags &= ~FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001384 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* in native mode send queued data to tty, in
1387 serial mode nothing needs to be done here */
1388 if (garmin_data_p->mode == MODE_NATIVE)
1389 garmin_flush_queue(garmin_data_p);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001390
1391 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
Oliver Neukum63832512009-10-07 10:50:23 +02001392 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001393 if (status)
1394 dev_err(&port->dev,
1395 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001396 __func__, status);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/*
1401 * The timer is currently only used to send queued packets to
1402 * the tty in cases where the protocol provides no own handshaking
1403 * to initiate the transfer.
1404 */
1405static void timeout_handler(unsigned long data)
1406{
1407 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1408
1409 /* send the next queued packet to the tty port */
1410 if (garmin_data_p->mode == MODE_NATIVE)
1411 if (garmin_data_p->flags & FLAGS_QUEUING)
1412 garmin_flush_queue(garmin_data_p);
1413}
1414
1415
1416
Alan Cox95da3102008-07-22 11:09:07 +01001417static int garmin_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 int status = 0;
1420 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001421 struct garmin_data *garmin_data_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Burman Yan7ac9da12006-11-22 20:54:38 +02001423 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (garmin_data_p == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001425 dev_err(&port->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return -ENOMEM;
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 init_timer(&garmin_data_p->timer);
1429 spin_lock_init(&garmin_data_p->lock);
1430 INIT_LIST_HEAD(&garmin_data_p->pktlist);
Alan Coxaf6d7802008-07-22 11:11:44 +01001431 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1433 garmin_data_p->timer.function = timeout_handler;
1434 garmin_data_p->port = port;
1435 garmin_data_p->state = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001436 garmin_data_p->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 garmin_data_p->count = 0;
1438 usb_set_serial_port_data(port, garmin_data_p);
1439
1440 status = garmin_init_session(port);
1441
1442 return status;
1443}
1444
1445
Alan Sternf9c99bb2009-06-02 11:53:55 -04001446static void garmin_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001449 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Alan Coxaf6d7802008-07-22 11:11:44 +01001451 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 del_timer_sync(&garmin_data_p->timer);
Alan Sternf9c99bb2009-06-02 11:53:55 -04001453}
1454
1455
1456static void garmin_release(struct usb_serial *serial)
1457{
1458 struct usb_serial_port *port = serial->port[0];
1459 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1460
Alan Coxaf6d7802008-07-22 11:11:44 +01001461 kfree(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001466static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001467 .driver = {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001468 .owner = THIS_MODULE,
1469 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001470 },
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001471 .description = "Garmin GPS usb/tty",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 .num_ports = 1,
1474 .open = garmin_open,
1475 .close = garmin_close,
1476 .throttle = garmin_throttle,
1477 .unthrottle = garmin_unthrottle,
1478 .attach = garmin_attach,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001479 .disconnect = garmin_disconnect,
1480 .release = garmin_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 .write = garmin_write,
1482 .write_room = garmin_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 .write_bulk_callback = garmin_write_bulk_callback,
1484 .read_bulk_callback = garmin_read_bulk_callback,
1485 .read_int_callback = garmin_read_int_callback,
1486};
1487
Alan Stern97b6b6d2012-02-23 14:56:32 -05001488static struct usb_serial_driver * const serial_drivers[] = {
1489 &garmin_device, NULL
1490};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -07001492module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Alan Coxaf6d7802008-07-22 11:11:44 +01001494MODULE_AUTHOR(DRIVER_AUTHOR);
1495MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496MODULE_LICENSE("GPL");
1497
1498module_param(debug, bool, S_IWUSR | S_IRUGO);
1499MODULE_PARM_DESC(debug, "Debug enabled or not");
1500module_param(initial_mode, int, S_IRUGO);
1501MODULE_PARM_DESC(initial_mode, "Initial mode");