blob: b0a7a9e909a4fe8d7568b5f162a1c2cb24404bd0 [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>
Hermann Kneissel468d1362007-08-03 20:20:33 +020037#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070039#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* the mode to be set when the port ist opened */
42static int initial_mode = 1;
43
44/* debug flag */
Alan Coxaf6d7802008-07-22 11:11:44 +010045static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define GARMIN_VENDOR_ID 0x091E
48
49/*
50 * Version Information
51 */
52
53#define VERSION_MAJOR 0
Hermann Kneisselb4026c42011-04-29 08:58:43 +020054#define VERSION_MINOR 36
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define _STR(s) #s
Alan Coxaf6d7802008-07-22 11:11:44 +010057#define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR)
59#define DRIVER_AUTHOR "hermann kneissel"
60#define DRIVER_DESC "garmin gps driver"
61
62/* error codes returned by the driver */
63#define EINVPKT 1000 /* invalid packet structure */
64
65
Alan Coxaf6d7802008-07-22 11:11:44 +010066/* size of the header of a packet using the usb protocol */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define GARMIN_PKTHDR_LENGTH 12
68
Alan Coxaf6d7802008-07-22 11:11:44 +010069/* max. possible size of a packet using the serial protocol */
70#define MAX_SERIAL_PKT_SIZ (3 + 255 + 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Alan Coxaf6d7802008-07-22 11:11:44 +010072/* max. possible size of a packet with worst case stuffing */
73#define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Alan Coxaf6d7802008-07-22 11:11:44 +010075/* size of a buffer able to hold a complete (no stuffing) packet
76 * (the document protocol does not contain packets with a larger
77 * size, but in theory a packet may be 64k+12 bytes - if in
78 * later protocol versions larger packet sizes occur, this value
79 * should be increased accordingly, so the input buffer is always
80 * large enough the store a complete packet inclusive header) */
81#define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Alan Coxaf6d7802008-07-22 11:11:44 +010083/* size of a buffer able to hold a complete (incl. stuffing) packet */
84#define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Alan Coxaf6d7802008-07-22 11:11:44 +010086/* where to place the packet id of a serial packet, so we can
87 * prepend the usb-packet header without the need to move the
88 * packets data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2)
90
Alan Coxaf6d7802008-07-22 11:11:44 +010091/* max. size of incoming private packets (header+1 param) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4)
93
94#define GARMIN_LAYERID_TRANSPORT 0
95#define GARMIN_LAYERID_APPL 20
Alan Coxaf6d7802008-07-22 11:11:44 +010096/* our own layer-id to use for some control mechanisms */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define GARMIN_LAYERID_PRIVATE 0x01106E4B
98
99#define GARMIN_PKTID_PVT_DATA 51
100#define GARMIN_PKTID_L001_COMMAND_DATA 10
101
102#define CMND_ABORT_TRANSFER 0
103
Alan Coxaf6d7802008-07-22 11:11:44 +0100104/* packet ids used in private layer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define PRIV_PKTID_SET_DEBUG 1
106#define PRIV_PKTID_SET_MODE 2
107#define PRIV_PKTID_INFO_REQ 3
108#define PRIV_PKTID_INFO_RESP 4
109#define PRIV_PKTID_RESET_REQ 5
110#define PRIV_PKTID_SET_DEF_MODE 6
111
112
113#define ETX 0x03
114#define DLE 0x10
115#define ACK 0x06
116#define NAK 0x15
117
118/* structure used to queue incoming packets */
119struct garmin_packet {
120 struct list_head list;
121 int seq;
Alan Coxaf6d7802008-07-22 11:11:44 +0100122 /* the real size of the data array, always > 0 */
123 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 __u8 data[1];
125};
126
127/* structure used to keep the current state of the driver */
128struct garmin_data {
129 __u8 state;
130 __u16 flags;
131 __u8 mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 __u8 count;
133 __u8 pkt_id;
134 __u32 serial_num;
135 struct timer_list timer;
136 struct usb_serial_port *port;
137 int seq_counter;
138 int insize;
139 int outsize;
140 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */
141 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */
142 __u8 privpkt[4*6];
143 spinlock_t lock;
144 struct list_head pktlist;
145};
146
147
148#define STATE_NEW 0
149#define STATE_INITIAL_DELAY 1
150#define STATE_TIMEOUT 2
151#define STATE_SESSION_REQ1 3
152#define STATE_SESSION_REQ2 4
153#define STATE_ACTIVE 5
154
155#define STATE_RESET 8
156#define STATE_DISCONNECTED 9
157#define STATE_WAIT_TTY_ACK 10
158#define STATE_GSP_WAIT_DATA 11
159
160#define MODE_NATIVE 0
161#define MODE_GARMIN_SERIAL 1
162
Alan Coxaf6d7802008-07-22 11:11:44 +0100163/* Flags used in garmin_data.flags: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define FLAGS_SESSION_REPLY_MASK 0x00C0
165#define FLAGS_SESSION_REPLY1_SEEN 0x0080
166#define FLAGS_SESSION_REPLY2_SEEN 0x0040
167#define FLAGS_BULK_IN_ACTIVE 0x0020
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200168#define FLAGS_BULK_IN_RESTART 0x0010
169#define FLAGS_THROTTLED 0x0008
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200170#define APP_REQ_SEEN 0x0004
171#define APP_RESP_SEEN 0x0002
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#define CLEAR_HALT_REQUIRED 0x0001
173
174#define FLAGS_QUEUING 0x0100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define FLAGS_DROP_DATA 0x0800
176
177#define FLAGS_GSP_SKIP 0x1000
178#define FLAGS_GSP_DLESEEN 0x2000
179
180
181
182
183
184
185/* function prototypes */
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200186static int gsp_next_packet(struct garmin_data *garmin_data_p);
187static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200188 const unsigned char *buf, int count,
189 int dismiss_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/* some special packets to be send or received */
192static unsigned char const GARMIN_START_SESSION_REQ[]
193 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static unsigned char const GARMIN_START_SESSION_REPLY[]
195 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[]
197 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 };
198static unsigned char const GARMIN_APP_LAYER_REPLY[]
199 = { 0x14, 0, 0, 0 };
200static unsigned char const GARMIN_START_PVT_REQ[]
201 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 };
202static unsigned char const GARMIN_STOP_PVT_REQ[]
203 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 };
204static unsigned char const GARMIN_STOP_TRANSFER_REQ[]
205 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 };
206static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[]
207 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 };
208static unsigned char const PRIVATE_REQ[]
209 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 };
210
211
212
Németh Márton7d40d7e2010-01-10 15:34:24 +0100213static const struct usb_device_id id_table[] = {
Alan Coxaf6d7802008-07-22 11:11:44 +0100214 /* the same device id seems to be used by all
215 usb enabled GPS devices */
216 { USB_DEVICE(GARMIN_VENDOR_ID, 3) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 { } /* Terminating entry */
218};
219
Alan Coxaf6d7802008-07-22 11:11:44 +0100220MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222static struct usb_driver garmin_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .name = "garmin_gps",
224 .probe = usb_serial_probe,
225 .disconnect = usb_serial_disconnect,
226 .id_table = id_table,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200227 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228};
229
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static inline int getLayerId(const __u8 *usbPacket)
232{
233 return __le32_to_cpup((__le32 *)(usbPacket));
234}
235
236static inline int getPacketId(const __u8 *usbPacket)
237{
238 return __le32_to_cpup((__le32 *)(usbPacket+4));
239}
240
241static inline int getDataLength(const __u8 *usbPacket)
242{
243 return __le32_to_cpup((__le32 *)(usbPacket+8));
244}
245
246
247/*
248 * check if the usb-packet in buf contains an abort-transfer command.
249 * (if yes, all queued data will be dropped)
250 */
251static inline int isAbortTrfCmnd(const unsigned char *buf)
252{
Alan Coxaf6d7802008-07-22 11:11:44 +0100253 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ,
254 sizeof(GARMIN_STOP_TRANSFER_REQ)) ||
255 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2,
256 sizeof(GARMIN_STOP_TRANSFER_REQ_V2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 1;
258 else
259 return 0;
260}
261
262
263
264static void send_to_tty(struct usb_serial_port *port,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200265 char *data, unsigned int actual_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Alan Cox4a90f092008-10-13 10:39:46 +0100267 struct tty_struct *tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (tty && actual_length) {
270
Alan Coxaf6d7802008-07-22 11:11:44 +0100271 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800272 __func__, actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Alan Cox33f0f882006-01-09 20:54:13 -0800274 tty_insert_flip_string(tty, data, actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 tty_flip_buffer_push(tty);
276 }
Alan Cox4a90f092008-10-13 10:39:46 +0100277 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280
281/******************************************************************************
282 * packet queue handling
283 ******************************************************************************/
284
285/*
286 * queue a received (usb-)packet for later processing
287 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100288static int pkt_add(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200289 unsigned char *data, unsigned int data_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200291 int state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int result = 0;
293 unsigned long flags;
294 struct garmin_packet *pkt;
295
296 /* process only packets containg data ... */
297 if (data_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 pkt = kmalloc(sizeof(struct garmin_packet)+data_length,
Alan Coxaf6d7802008-07-22 11:11:44 +0100299 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (pkt == NULL) {
301 dev_err(&garmin_data_p->port->dev, "out of memory\n");
302 return 0;
303 }
304 pkt->size = data_length;
305 memcpy(pkt->data, data, data_length);
306
307 spin_lock_irqsave(&garmin_data_p->lock, flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200308 garmin_data_p->flags |= FLAGS_QUEUING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 result = list_empty(&garmin_data_p->pktlist);
310 pkt->seq = garmin_data_p->seq_counter++;
311 list_add_tail(&pkt->list, &garmin_data_p->pktlist);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200312 state = garmin_data_p->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
314
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200315 dbg("%s - added: pkt: %d - %d bytes",
316 __func__, pkt->seq, data_length);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /* in serial mode, if someone is waiting for data from
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200319 the device, convert and send the next packet to tty. */
Alan Coxaf6d7802008-07-22 11:11:44 +0100320 if (result && (state == STATE_GSP_WAIT_DATA))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 gsp_next_packet(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323 return result;
324}
325
326
327/* get the next pending packet */
Alan Coxaf6d7802008-07-22 11:11:44 +0100328static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 unsigned long flags;
331 struct garmin_packet *result = NULL;
332
333 spin_lock_irqsave(&garmin_data_p->lock, flags);
334 if (!list_empty(&garmin_data_p->pktlist)) {
335 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
336 list_del(&result->list);
337 }
338 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
339 return result;
340}
341
342
343/* free up all queued data */
Alan Coxaf6d7802008-07-22 11:11:44 +0100344static void pkt_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned long flags;
347 struct garmin_packet *result = NULL;
348
Harvey Harrison441b62c2008-03-03 16:08:34 -0800349 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 spin_lock_irqsave(&garmin_data_p->lock, flags);
352 while (!list_empty(&garmin_data_p->pktlist)) {
353 result = (struct garmin_packet *)garmin_data_p->pktlist.next;
354 list_del(&result->list);
355 kfree(result);
356 }
357 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
358}
359
360
361/******************************************************************************
362 * garmin serial protocol handling handling
363 ******************************************************************************/
364
365/* send an ack packet back to the tty */
Alan Coxaf6d7802008-07-22 11:11:44 +0100366static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 __u8 pkt[10];
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200369 __u8 cksum = 0;
370 __u8 *ptr = pkt;
371 unsigned l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Harvey Harrison441b62c2008-03-03 16:08:34 -0800373 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 *ptr++ = DLE;
376 *ptr++ = ACK;
377 cksum += ACK;
378
379 *ptr++ = 2;
380 cksum += 2;
381
382 *ptr++ = pkt_id;
383 cksum += pkt_id;
384
Alan Coxaf6d7802008-07-22 11:11:44 +0100385 if (pkt_id == DLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *ptr++ = DLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 *ptr++ = 0;
389 *ptr++ = 0xFF & (-cksum);
390 *ptr++ = DLE;
391 *ptr++ = ETX;
392
393 l = ptr-pkt;
394
395 send_to_tty(garmin_data_p->port, pkt, l);
396 return 0;
397}
398
399
400
401/*
402 * called for a complete packet received from tty layer
403 *
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200404 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 * at GSP_INITIAL_OFFSET.
406 *
407 * count - number of bytes in the input buffer including space reserved for
Alan Coxaf6d7802008-07-22 11:11:44 +0100408 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 * (including pkt-id, data-length a. cksum)
410 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100411static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200413 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100414 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200415 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 int cksum = 0;
418 int n = 0;
419 int pktid = recpkt[0];
420 int size = recpkt[1];
421
422 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800423 __func__, count-GSP_INITIAL_OFFSET, recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 if (size != (count-GSP_INITIAL_OFFSET-3)) {
426 dbg("%s - invalid size, expected %d bytes, got %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800427 __func__, size, (count-GSP_INITIAL_OFFSET-3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -EINVPKT;
429 }
430
431 cksum += *recpkt++;
432 cksum += *recpkt++;
433
Alan Coxaf6d7802008-07-22 11:11:44 +0100434 /* sanity check, remove after test ... */
435 if ((__u8 *)&(usbdata[3]) != recpkt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 dbg("%s - ptr mismatch %p - %p",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800437 __func__, &(usbdata[4]), recpkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -EINVPKT;
439 }
440
441 while (n < size) {
442 cksum += *recpkt++;
443 n++;
444 }
445
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200446 if ((0xff & (cksum + *recpkt)) != 0) {
447 dbg("%s - invalid checksum, expected %02x, got %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800448 __func__, 0xff & -cksum, 0xff & *recpkt);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200449 return -EINVPKT;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL);
453 usbdata[1] = __cpu_to_le32(pktid);
454 usbdata[2] = __cpu_to_le32(size);
455
Alan Coxaf6d7802008-07-22 11:11:44 +0100456 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer,
Hermann Kneissel468d1362007-08-03 20:20:33 +0200457 GARMIN_PKTHDR_LENGTH+size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* if this was an abort-transfer command, flush all
460 queued data. */
461 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200462 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 garmin_data_p->flags |= FLAGS_DROP_DATA;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200464 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 pkt_clear(garmin_data_p);
466 }
467
468 return count;
469}
470
471
472
473/*
474 * Called for data received from tty
475 *
476 * buf contains the data read, it may span more than one packet or even
477 * incomplete packets
478 *
479 * input record should be a serial-record, but it may not be complete.
480 * Copy it into our local buffer, until an etx is seen (or an error
481 * occurs).
482 * Once the record is complete, convert into a usb packet and send it
483 * to the bulk pipe, send an ack back to the tty.
484 *
485 * If the input is an ack, just send the last queued packet to the
486 * tty layer.
487 *
488 * if the input is an abort command, drop all queued data.
489 */
490
Alan Coxaf6d7802008-07-22 11:11:44 +0100491static int gsp_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200492 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200494 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int offs = 0;
496 int ack_or_nak_seen = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200497 __u8 *dest;
498 int size;
Alan Coxaf6d7802008-07-22 11:11:44 +0100499 /* dleSeen: set if last byte read was a DLE */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200500 int dleSeen;
Alan Coxaf6d7802008-07-22 11:11:44 +0100501 /* skip: if set, skip incoming data until possible start of
502 * new packet
503 */
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200504 int skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 __u8 data;
506
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200507 spin_lock_irqsave(&garmin_data_p->lock, flags);
508 dest = garmin_data_p->inbuffer;
509 size = garmin_data_p->insize;
510 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN;
511 skip = garmin_data_p->flags & FLAGS_GSP_SKIP;
512 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
513
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200514 /* dbg("%s - dle=%d skip=%d size=%d count=%d",
515 __func__, dleSeen, skip, size, count); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Alan Coxaf6d7802008-07-22 11:11:44 +0100517 if (size == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 size = GSP_INITIAL_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 while (offs < count) {
521
522 data = *(buf+offs);
Alan Coxaf6d7802008-07-22 11:11:44 +0100523 offs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if (data == DLE) {
526 if (skip) { /* start of a new pkt */
527 skip = 0;
528 size = GSP_INITIAL_OFFSET;
529 dleSeen = 1;
530 } else if (dleSeen) {
531 dest[size++] = data;
532 dleSeen = 0;
533 } else {
534 dleSeen = 1;
535 }
536 } else if (data == ETX) {
537 if (dleSeen) {
538 /* packet complete */
539
540 data = dest[GSP_INITIAL_OFFSET];
541
542 if (data == ACK) {
543 ack_or_nak_seen = ACK;
544 dbg("ACK packet complete.");
545 } else if (data == NAK) {
546 ack_or_nak_seen = NAK;
547 dbg("NAK packet complete.");
548 } else {
Alan Coxaf6d7802008-07-22 11:11:44 +0100549 dbg("packet complete - id=0x%X.",
550 0xFF & data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 gsp_rec_packet(garmin_data_p, size);
552 }
553
554 skip = 1;
555 size = GSP_INITIAL_OFFSET;
556 dleSeen = 0;
557 } else {
558 dest[size++] = data;
559 }
560 } else if (!skip) {
561
562 if (dleSeen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 size = GSP_INITIAL_OFFSET;
564 dleSeen = 0;
565 }
566
567 dest[size++] = data;
568 }
569
570 if (size >= GPS_IN_BUFSIZ) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800571 dbg("%s - packet too large.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 skip = 1;
573 size = GSP_INITIAL_OFFSET;
574 dleSeen = 0;
575 }
576 }
577
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200578 spin_lock_irqsave(&garmin_data_p->lock, flags);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 garmin_data_p->insize = size;
581
Alan Coxaf6d7802008-07-22 11:11:44 +0100582 /* copy flags back to structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (skip)
584 garmin_data_p->flags |= FLAGS_GSP_SKIP;
585 else
586 garmin_data_p->flags &= ~FLAGS_GSP_SKIP;
587
588 if (dleSeen)
589 garmin_data_p->flags |= FLAGS_GSP_DLESEEN;
590 else
591 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN;
592
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200593 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
594
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200595 if (ack_or_nak_seen) {
596 if (gsp_next_packet(garmin_data_p) > 0)
597 garmin_data_p->state = STATE_ACTIVE;
598 else
599 garmin_data_p->state = STATE_GSP_WAIT_DATA;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return count;
602}
603
604
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/*
607 * Sends a usb packet to the tty
608 *
609 * Assumes, that all packages and at an usb-packet boundary.
610 *
611 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent
612 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100613static int gsp_send(struct garmin_data *garmin_data_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 const unsigned char *buf, int count)
615{
616 const unsigned char *src;
617 unsigned char *dst;
618 int pktid = 0;
619 int datalen = 0;
620 int cksum = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100621 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int k;
623
Harvey Harrison441b62c2008-03-03 16:08:34 -0800624 dbg("%s - state %d - %d bytes.", __func__,
Alan Coxaf6d7802008-07-22 11:11:44 +0100625 garmin_data_p->state, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 k = garmin_data_p->outsize;
628 if ((k+count) > GPS_OUT_BUFSIZ) {
629 dbg("packet too large");
630 garmin_data_p->outsize = 0;
631 return -4;
632 }
633
634 memcpy(garmin_data_p->outbuffer+k, buf, count);
635 k += count;
636 garmin_data_p->outsize = k;
637
638 if (k >= GARMIN_PKTHDR_LENGTH) {
639 pktid = getPacketId(garmin_data_p->outbuffer);
Alan Coxaf6d7802008-07-22 11:11:44 +0100640 datalen = getDataLength(garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 i = GARMIN_PKTHDR_LENGTH + datalen;
642 if (k < i)
643 return 0;
644 } else {
645 return 0;
646 }
647
Alan Coxaf6d7802008-07-22 11:11:44 +0100648 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* garmin_data_p->outbuffer now contains a complete packet */
651
652 usb_serial_debug_data(debug, &garmin_data_p->port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100653 __func__, k, garmin_data_p->outbuffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 garmin_data_p->outsize = 0;
656
657 if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100658 dbg("not an application packet (%d)",
659 getLayerId(garmin_data_p->outbuffer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -1;
661 }
662
663 if (pktid > 255) {
664 dbg("packet-id %d too large", pktid);
665 return -2;
666 }
667
668 if (datalen > 255) {
669 dbg("packet-size %d too large", datalen);
670 return -3;
671 }
672
673 /* the serial protocol should be able to handle this packet */
674
675 k = 0;
676 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
Alan Coxaf6d7802008-07-22 11:11:44 +0100677 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (*src++ == DLE)
679 k++;
680 }
681
682 src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH;
683 if (k > (GARMIN_PKTHDR_LENGTH-2)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100684 /* can't add stuffing DLEs in place, move data to end
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200685 of buffer ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen;
687 memcpy(dst, src, datalen);
688 src = dst;
689 }
690
691 dst = garmin_data_p->outbuffer;
692
693 *dst++ = DLE;
694 *dst++ = pktid;
695 cksum += pktid;
696 *dst++ = datalen;
697 cksum += datalen;
698 if (datalen == DLE)
699 *dst++ = DLE;
700
Alan Coxaf6d7802008-07-22 11:11:44 +0100701 for (i = 0; i < datalen; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 __u8 c = *src++;
703 *dst++ = c;
704 cksum += c;
705 if (c == DLE)
706 *dst++ = DLE;
707 }
Alan Coxaf6d7802008-07-22 11:11:44 +0100708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 cksum = 0xFF & -cksum;
710 *dst++ = cksum;
711 if (cksum == DLE)
712 *dst++ = DLE;
713 *dst++ = DLE;
714 *dst++ = ETX;
715
716 i = dst-garmin_data_p->outbuffer;
717
718 send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i);
719
720 garmin_data_p->pkt_id = pktid;
721 garmin_data_p->state = STATE_WAIT_TTY_ACK;
722
723 return i;
724}
725
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/*
728 * Process the next pending data packet - if there is one
729 */
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200730static int gsp_next_packet(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200732 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct garmin_packet *pkt = NULL;
734
735 while ((pkt = pkt_pop(garmin_data_p)) != NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800736 dbg("%s - next pkt: %d", __func__, pkt->seq);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200737 result = gsp_send(garmin_data_p, pkt->data, pkt->size);
738 if (result > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 kfree(pkt);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200740 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742 kfree(pkt);
743 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200744 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/******************************************************************************
750 * garmin native mode
751 ******************************************************************************/
752
753
754/*
755 * Called for data received from tty
756 *
757 * The input data is expected to be in garmin usb-packet format.
758 *
759 * buf contains the data read, it may span more than one packet
760 * or even incomplete packets
761 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100762static int nat_receive(struct garmin_data *garmin_data_p,
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200763 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200765 unsigned long flags;
Alan Coxaf6d7802008-07-22 11:11:44 +0100766 __u8 *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int offs = 0;
768 int result = count;
769 int len;
770
771 while (offs < count) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100772 /* if buffer contains header, copy rest of data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH)
774 len = GARMIN_PKTHDR_LENGTH
775 +getDataLength(garmin_data_p->inbuffer);
776 else
777 len = GARMIN_PKTHDR_LENGTH;
778
779 if (len >= GPS_IN_BUFSIZ) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100780 /* seems to be an invalid packet, ignore rest
781 of input */
782 dbg("%s - packet size too large: %d", __func__, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 garmin_data_p->insize = 0;
784 count = 0;
785 result = -EINVPKT;
786 } else {
787 len -= garmin_data_p->insize;
788 if (len > (count-offs))
789 len = (count-offs);
790 if (len > 0) {
791 dest = garmin_data_p->inbuffer
Alan Coxaf6d7802008-07-22 11:11:44 +0100792 + garmin_data_p->insize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 memcpy(dest, buf+offs, len);
794 garmin_data_p->insize += len;
795 offs += len;
796 }
797 }
798
799 /* do we have a complete packet ? */
800 if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) {
801 len = GARMIN_PKTHDR_LENGTH+
802 getDataLength(garmin_data_p->inbuffer);
803 if (garmin_data_p->insize >= len) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100804 garmin_write_bulk(garmin_data_p->port,
805 garmin_data_p->inbuffer,
806 len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 garmin_data_p->insize = 0;
808
809 /* if this was an abort-transfer command,
810 flush all queued data. */
811 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100812 spin_lock_irqsave(&garmin_data_p->lock,
813 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 garmin_data_p->flags |= FLAGS_DROP_DATA;
Alan Coxaf6d7802008-07-22 11:11:44 +0100815 spin_unlock_irqrestore(
816 &garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 pkt_clear(garmin_data_p);
818 }
819 }
820 }
821 }
822 return result;
823}
824
825
826/******************************************************************************
827 * private packets
828 ******************************************************************************/
829
830static void priv_status_resp(struct usb_serial_port *port)
831{
Alan Coxaf6d7802008-07-22 11:11:44 +0100832 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 __le32 *pkt = (__le32 *)garmin_data_p->privpkt;
834
835 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE);
836 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP);
837 pkt[2] = __cpu_to_le32(12);
838 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR);
839 pkt[4] = __cpu_to_le32(garmin_data_p->mode);
840 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num);
841
Alan Coxaf6d7802008-07-22 11:11:44 +0100842 send_to_tty(port, (__u8 *)pkt, 6 * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
845
846/******************************************************************************
847 * Garmin specific driver functions
848 ******************************************************************************/
849
850static int process_resetdev_request(struct usb_serial_port *port)
851{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200852 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int status;
Alan Coxaf6d7802008-07-22 11:11:44 +0100854 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200856 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED);
858 garmin_data_p->state = STATE_RESET;
859 garmin_data_p->serial_num = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200860 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Alan Coxaf6d7802008-07-22 11:11:44 +0100862 usb_kill_urb(port->interrupt_in_urb);
863 dbg("%s - usb_reset_device", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 status = usb_reset_device(port->serial->dev);
865 if (status)
866 dbg("%s - usb_reset_device failed: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800867 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return status;
869}
870
871
872
873/*
874 * clear all cached data
875 */
Alan Coxaf6d7802008-07-22 11:11:44 +0100876static int garmin_clear(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200878 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int status = 0;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 /* flush all queued data */
882 pkt_clear(garmin_data_p);
883
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200884 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 garmin_data_p->insize = 0;
886 garmin_data_p->outsize = 0;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200887 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 return status;
890}
891
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893static int garmin_init_session(struct usb_serial_port *port)
894{
895 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100896 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 int status = 0;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200898 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (status == 0) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100901 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Harvey Harrison441b62c2008-03-03 16:08:34 -0800903 dbg("%s - adding interrupt input", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 port->interrupt_in_urb->dev = serial->dev;
905 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
906 if (status)
907 dev_err(&serial->dev->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +0100908 "%s - failed submitting interrupt urb, error %d\n",
909 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200912 /*
913 * using the initialization method from gpsbabel. See comments in
914 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles()
915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (status == 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800917 dbg("%s - starting session ...", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 garmin_data_p->state = STATE_ACTIVE;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200919
920 for (i = 0; i < 3; i++) {
921 status = garmin_write_bulk(port,
922 GARMIN_START_SESSION_REQ,
Alan Coxaf6d7802008-07-22 11:11:44 +0100923 sizeof(GARMIN_START_SESSION_REQ), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200925 if (status < 0)
926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200928
929 if (status > 0)
930 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933 return status;
934}
935
936
937
Alan Coxa509a7e2009-09-19 13:13:26 -0700938static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200940 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int status = 0;
Alan Coxaf6d7802008-07-22 11:11:44 +0100942 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Harvey Harrison441b62c2008-03-03 16:08:34 -0800944 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200946 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 garmin_data_p->mode = initial_mode;
948 garmin_data_p->count = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +0200949 garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +0200950 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* shutdown any bulk reads that might be going on */
Alan Coxaf6d7802008-07-22 11:11:44 +0100953 usb_kill_urb(port->write_urb);
954 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Alan Cox95da3102008-07-22 11:09:07 +0100956 if (garmin_data_p->state == STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 status = garmin_init_session(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 garmin_data_p->state = STATE_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return status;
961}
962
963
Alan Cox335f8512009-06-11 12:26:29 +0100964static void garmin_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +0100967 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Harvey Harrison441b62c2008-03-03 16:08:34 -0800969 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 port->number, garmin_data_p->mode,
971 garmin_data_p->state, garmin_data_p->flags);
972
973 if (!serial)
974 return;
975
Oliver Neukum95bef012008-01-22 13:56:18 +0100976 mutex_lock(&port->serial->disc_mutex);
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200977
Oliver Neukum95bef012008-01-22 13:56:18 +0100978 if (!port->serial->disconnected)
979 garmin_clear(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 /* shutdown our urbs */
Alan Coxaf6d7802008-07-22 11:11:44 +0100982 usb_kill_urb(port->read_urb);
983 usb_kill_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200985 /* keep reset state so we know that we must start a new session */
986 if (garmin_data_p->state != STATE_RESET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 garmin_data_p->state = STATE_DISCONNECTED;
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200988
Oliver Neukum95bef012008-01-22 13:56:18 +0100989 mutex_unlock(&port->serial->disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Hermann Kneisselb4072f42009-04-26 22:42:04 +0200992
Alan Coxaf6d7802008-07-22 11:11:44 +0100993static void garmin_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Ming Leicdc97792008-02-24 18:41:47 +0800995 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Hermann Kneissel468d1362007-08-03 20:20:33 +0200997 if (port) {
Alan Coxaf6d7802008-07-22 11:11:44 +0100998 struct garmin_data *garmin_data_p =
999 usb_get_serial_port_data(port);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001000
Harvey Harrison441b62c2008-03-03 16:08:34 -08001001 dbg("%s - port %d", __func__, port->number);
Hermann Kneissel468d1362007-08-03 20:20:33 +02001002
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001003 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) {
1004
1005 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1006 gsp_send_ack(garmin_data_p,
Alan Coxaf6d7802008-07-22 11:11:44 +01001007 ((__u8 *)urb->transfer_buffer)[4]);
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001008 }
Hermann Kneissel468d1362007-08-03 20:20:33 +02001009 }
Hermann Kneissel468d1362007-08-03 20:20:33 +02001010 usb_serial_port_softint(port);
1011 }
1012
Alan Coxaf6d7802008-07-22 11:11:44 +01001013 /* Ignore errors that resulted from garmin_write_bulk with
1014 dismiss_ack = 1 */
Hermann Kneissel468d1362007-08-03 20:20:33 +02001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 /* free up the transfer buffer, as usb_free_urb() does not do this */
Alan Coxaf6d7802008-07-22 11:11:44 +01001017 kfree(urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020
Alan Coxaf6d7802008-07-22 11:11:44 +01001021static int garmin_write_bulk(struct usb_serial_port *port,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001022 const unsigned char *buf, int count,
1023 int dismiss_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001025 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001027 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 struct urb *urb;
1029 unsigned char *buffer;
1030 int status;
1031
Harvey Harrison441b62c2008-03-03 16:08:34 -08001032 dbg("%s - port %d, state %d", __func__, port->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 garmin_data_p->state);
1034
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001035 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 garmin_data_p->flags &= ~FLAGS_DROP_DATA;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001037 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Alan Coxaf6d7802008-07-22 11:11:44 +01001039 buffer = kmalloc(count, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!buffer) {
1041 dev_err(&port->dev, "out of memory\n");
1042 return -ENOMEM;
1043 }
1044
1045 urb = usb_alloc_urb(0, GFP_ATOMIC);
1046 if (!urb) {
1047 dev_err(&port->dev, "no more free urbs\n");
Alan Coxaf6d7802008-07-22 11:11:44 +01001048 kfree(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return -ENOMEM;
1050 }
1051
Alan Coxaf6d7802008-07-22 11:11:44 +01001052 memcpy(buffer, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Harvey Harrison441b62c2008-03-03 16:08:34 -08001054 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Alan Coxaf6d7802008-07-22 11:11:44 +01001056 usb_fill_bulk_urb(urb, serial->dev,
1057 usb_sndbulkpipe(serial->dev,
1058 port->bulk_out_endpointAddress),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 buffer, count,
Hermann Kneissel468d1362007-08-03 20:20:33 +02001060 garmin_write_bulk_callback,
1061 dismiss_ack ? NULL : port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 urb->transfer_flags |= URB_ZERO_PACKET;
1063
1064 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001065
1066 spin_lock_irqsave(&garmin_data_p->lock, flags);
1067 garmin_data_p->flags |= APP_REQ_SEEN;
1068 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1071 pkt_clear(garmin_data_p);
1072 garmin_data_p->state = STATE_GSP_WAIT_DATA;
1073 }
1074 }
1075
1076 /* send it down the pipe */
1077 status = usb_submit_urb(urb, GFP_ATOMIC);
1078 if (status) {
1079 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001080 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001081 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 count = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
1085 /* we are done with this urb, so let the host driver
1086 * really free it when it is finished with it */
Alan Coxaf6d7802008-07-22 11:11:44 +01001087 usb_free_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 return count;
1090}
1091
Alan Coxaf6d7802008-07-22 11:11:44 +01001092static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port,
Alan Cox95da3102008-07-22 11:09:07 +01001093 const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 int pktid, pktsiz, len;
Alan Coxaf6d7802008-07-22 11:11:44 +01001096 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt;
1098
Harvey Harrison441b62c2008-03-03 16:08:34 -08001099 usb_serial_debug_data(debug, &port->dev, __func__, count, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001101 if (garmin_data_p->state == STATE_RESET)
1102 return -EIO;
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* check for our private packets */
1105 if (count >= GARMIN_PKTHDR_LENGTH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 len = PRIVPKTSIZ;
1107 if (count < len)
1108 len = count;
1109
1110 memcpy(garmin_data_p->privpkt, buf, len);
1111
1112 pktsiz = getDataLength(garmin_data_p->privpkt);
1113 pktid = getPacketId(garmin_data_p->privpkt);
1114
1115 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz)
Alan Coxaf6d7802008-07-22 11:11:44 +01001116 && GARMIN_LAYERID_PRIVATE ==
1117 getLayerId(garmin_data_p->privpkt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 dbg("%s - processing private request %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001120 __func__, pktid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Alan Coxaf6d7802008-07-22 11:11:44 +01001122 /* drop all unfinished transfers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 garmin_clear(garmin_data_p);
1124
Alan Coxaf6d7802008-07-22 11:11:44 +01001125 switch (pktid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 case PRIV_PKTID_SET_DEBUG:
1128 if (pktsiz != 4)
1129 return -EINVPKT;
1130 debug = __le32_to_cpu(privpkt[3]);
1131 dbg("%s - debug level set to 0x%X",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001132 __func__, debug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
1134
1135 case PRIV_PKTID_SET_MODE:
1136 if (pktsiz != 4)
1137 return -EINVPKT;
1138 garmin_data_p->mode = __le32_to_cpu(privpkt[3]);
1139 dbg("%s - mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001140 __func__, garmin_data_p->mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
1142
1143 case PRIV_PKTID_INFO_REQ:
1144 priv_status_resp(port);
1145 break;
1146
1147 case PRIV_PKTID_RESET_REQ:
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001148 process_resetdev_request(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
1150
1151 case PRIV_PKTID_SET_DEF_MODE:
1152 if (pktsiz != 4)
1153 return -EINVPKT;
1154 initial_mode = __le32_to_cpu(privpkt[3]);
1155 dbg("%s - initial_mode set to %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001156 __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 garmin_data_p->mode);
1158 break;
1159 }
1160 return count;
1161 }
1162 }
1163
1164 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
1165 return gsp_receive(garmin_data_p, buf, count);
1166 } else { /* MODE_NATIVE */
1167 return nat_receive(garmin_data_p, buf, count);
1168 }
1169}
1170
1171
Alan Cox95da3102008-07-22 11:09:07 +01001172static int garmin_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Alan Cox95da3102008-07-22 11:09:07 +01001174 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * Report back the bytes currently available in the output buffer.
1177 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001178 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return GPS_OUT_BUFSIZ-garmin_data_p->outsize;
1180}
1181
1182
Alan Coxaf6d7802008-07-22 11:11:44 +01001183static void garmin_read_process(struct garmin_data *garmin_data_p,
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001184 unsigned char *data, unsigned data_length,
1185 int bulk_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001187 unsigned long flags;
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (garmin_data_p->flags & FLAGS_DROP_DATA) {
1190 /* abort-transfer cmd is actice */
Harvey Harrison441b62c2008-03-03 16:08:34 -08001191 dbg("%s - pkt dropped", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 } else if (garmin_data_p->state != STATE_DISCONNECTED &&
Alan Coxaf6d7802008-07-22 11:11:44 +01001193 garmin_data_p->state != STATE_RESET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* if throttling is active or postprecessing is required
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001196 put the received data in the input queue, otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 send it directly to the tty port */
1198 if (garmin_data_p->flags & FLAGS_QUEUING) {
1199 pkt_add(garmin_data_p, data, data_length);
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001200 } else if (bulk_data ||
1201 getLayerId(data) == GARMIN_LAYERID_APPL) {
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001202
1203 spin_lock_irqsave(&garmin_data_p->lock, flags);
1204 garmin_data_p->flags |= APP_RESP_SEEN;
1205 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
1206
1207 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 pkt_add(garmin_data_p, data, data_length);
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001209 } else {
1210 send_to_tty(garmin_data_p->port, data,
1211 data_length);
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Hermann Kneisselb4072f42009-04-26 22:42:04 +02001214 /* ignore system layer packets ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216}
1217
1218
Alan Coxaf6d7802008-07-22 11:11:44 +01001219static void garmin_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001221 unsigned long flags;
Ming Leicdc97792008-02-24 18:41:47 +08001222 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001224 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001226 int status = urb->status;
1227 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Harvey Harrison441b62c2008-03-03 16:08:34 -08001229 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 if (!serial) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001232 dbg("%s - bad serial pointer, exiting", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return;
1234 }
1235
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001236 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 dbg("%s - nonzero read bulk status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001238 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return;
1240 }
1241
Alan Coxaf6d7802008-07-22 11:11:44 +01001242 usb_serial_debug_data(debug, &port->dev,
Harvey Harrison441b62c2008-03-03 16:08:34 -08001243 __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001245 garmin_read_process(garmin_data_p, data, urb->actual_length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001247 if (urb->actual_length == 0 &&
1248 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) {
1249 spin_lock_irqsave(&garmin_data_p->lock, flags);
1250 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART;
1251 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001252 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1253 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 dev_err(&port->dev,
1255 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001256 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001257 } else if (urb->actual_length > 0) {
1258 /* Continue trying to read until nothing more is received */
1259 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) {
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001260 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1261 if (retval)
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001262 dev_err(&port->dev,
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001263 "%s - failed resubmitting read urb, "
Harvey Harrison441b62c2008-03-03 16:08:34 -08001264 "error %d\n", __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001265 }
1266 } else {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001267 dbg("%s - end of bulk data", __func__);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001268 spin_lock_irqsave(&garmin_data_p->lock, flags);
1269 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE;
1270 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
1274
Alan Coxaf6d7802008-07-22 11:11:44 +01001275static void garmin_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001277 unsigned long flags;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001278 int retval;
Ming Leicdc97792008-02-24 18:41:47 +08001279 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 struct usb_serial *serial = port->serial;
Alan Coxaf6d7802008-07-22 11:11:44 +01001281 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001283 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001285 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 case 0:
1287 /* success */
1288 break;
1289 case -ECONNRESET:
1290 case -ENOENT:
1291 case -ESHUTDOWN:
1292 /* this urb is terminated, clean up */
1293 dbg("%s - urb shutting down with status: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001294 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return;
1296 default:
1297 dbg("%s - nonzero urb status received: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001298 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return;
1300 }
1301
Harvey Harrison441b62c2008-03-03 16:08:34 -08001302 usb_serial_debug_data(debug, &port->dev, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 urb->actual_length, urb->transfer_buffer);
1304
1305 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) &&
1306 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001307 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Harvey Harrison441b62c2008-03-03 16:08:34 -08001309 dbg("%s - bulk data available.", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001311 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
1312
1313 /* bulk data available */
Alan Coxaf6d7802008-07-22 11:11:44 +01001314 usb_fill_bulk_urb(port->read_urb, serial->dev,
1315 usb_rcvbulkpipe(serial->dev,
1316 port->bulk_in_endpointAddress),
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001317 port->read_urb->transfer_buffer,
1318 port->read_urb->transfer_buffer_length,
1319 garmin_read_bulk_callback, port);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001320 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
1321 if (retval) {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001322 dev_err(&port->dev,
Alan Coxaf6d7802008-07-22 11:11:44 +01001323 "%s - failed submitting read urb, error %d\n",
1324 __func__, retval);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001325 } else {
1326 spin_lock_irqsave(&garmin_data_p->lock, flags);
1327 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE;
Alan Coxaf6d7802008-07-22 11:11:44 +01001328 spin_unlock_irqrestore(&garmin_data_p->lock,
1329 flags);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001330 }
1331 } else {
1332 /* bulk-in transfer still active */
1333 spin_lock_irqsave(&garmin_data_p->lock, flags);
1334 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART;
1335 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337
1338 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY))
1339 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY,
Alan Coxaf6d7802008-07-22 11:11:44 +01001340 sizeof(GARMIN_START_SESSION_REPLY))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001342 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001344 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* save the serial number */
Alan Coxaf6d7802008-07-22 11:11:44 +01001347 garmin_data_p->serial_num = __le32_to_cpup(
1348 (__le32 *)(data+GARMIN_PKTHDR_LENGTH));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 dbg("%s - start-of-session reply seen - serial %u.",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001351 __func__, garmin_data_p->serial_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001354 garmin_read_process(garmin_data_p, data, urb->actual_length, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 port->interrupt_in_urb->dev = port->serial->dev;
Alan Coxaf6d7802008-07-22 11:11:44 +01001357 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmanf9feb512007-06-15 15:44:13 -07001358 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 dev_err(&urb->dev->dev,
1360 "%s - Error %d submitting interrupt urb\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001361 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364
1365/*
1366 * Sends the next queued packt to the tty port (garmin native mode only)
1367 * and then sets a timer to call itself again until all queued data
1368 * is sent.
1369 */
Alan Coxaf6d7802008-07-22 11:11:44 +01001370static int garmin_flush_queue(struct garmin_data *garmin_data_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001372 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct garmin_packet *pkt;
1374
1375 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) {
1376 pkt = pkt_pop(garmin_data_p);
1377 if (pkt != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 send_to_tty(garmin_data_p->port, pkt->data, pkt->size);
1379 kfree(pkt);
1380 mod_timer(&garmin_data_p->timer, (1)+jiffies);
1381
1382 } else {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001383 spin_lock_irqsave(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 garmin_data_p->flags &= ~FLAGS_QUEUING;
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001385 spin_unlock_irqrestore(&garmin_data_p->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387 }
1388 return 0;
1389}
1390
1391
Alan Cox95da3102008-07-22 11:09:07 +01001392static void garmin_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Alan Cox95da3102008-07-22 11:09:07 +01001394 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001395 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Harvey Harrison441b62c2008-03-03 16:08:34 -08001397 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* set flag, data received will be put into a queue
1399 for later processing */
Oliver Neukum63832512009-10-07 10:50:23 +02001400 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001402 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405
Alan Coxaf6d7802008-07-22 11:11:44 +01001406static void garmin_unthrottle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Alan Cox95da3102008-07-22 11:09:07 +01001408 struct usb_serial_port *port = tty->driver_data;
Alan Coxaf6d7802008-07-22 11:11:44 +01001409 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001410 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Harvey Harrison441b62c2008-03-03 16:08:34 -08001412 dbg("%s - port %d", __func__, port->number);
Oliver Neukum63832512009-10-07 10:50:23 +02001413 spin_lock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 garmin_data_p->flags &= ~FLAGS_THROTTLED;
Oliver Neukum63832512009-10-07 10:50:23 +02001415 spin_unlock_irq(&garmin_data_p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* in native mode send queued data to tty, in
1418 serial mode nothing needs to be done here */
1419 if (garmin_data_p->mode == MODE_NATIVE)
1420 garmin_flush_queue(garmin_data_p);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001421
1422 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) {
Oliver Neukum63832512009-10-07 10:50:23 +02001423 status = usb_submit_urb(port->read_urb, GFP_KERNEL);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001424 if (status)
1425 dev_err(&port->dev,
1426 "%s - failed resubmitting read urb, error %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001427 __func__, status);
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431/*
1432 * The timer is currently only used to send queued packets to
1433 * the tty in cases where the protocol provides no own handshaking
1434 * to initiate the transfer.
1435 */
1436static void timeout_handler(unsigned long data)
1437{
1438 struct garmin_data *garmin_data_p = (struct garmin_data *) data;
1439
1440 /* send the next queued packet to the tty port */
1441 if (garmin_data_p->mode == MODE_NATIVE)
1442 if (garmin_data_p->flags & FLAGS_QUEUING)
1443 garmin_flush_queue(garmin_data_p);
1444}
1445
1446
1447
Alan Cox95da3102008-07-22 11:09:07 +01001448static int garmin_attach(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 int status = 0;
1451 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001452 struct garmin_data *garmin_data_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Harvey Harrison441b62c2008-03-03 16:08:34 -08001454 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Burman Yan7ac9da12006-11-22 20:54:38 +02001456 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (garmin_data_p == NULL) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001458 dev_err(&port->dev, "%s - Out of memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return -ENOMEM;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 init_timer(&garmin_data_p->timer);
1462 spin_lock_init(&garmin_data_p->lock);
1463 INIT_LIST_HEAD(&garmin_data_p->pktlist);
Alan Coxaf6d7802008-07-22 11:11:44 +01001464 /* garmin_data_p->timer.expires = jiffies + session_timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 garmin_data_p->timer.data = (unsigned long)garmin_data_p;
1466 garmin_data_p->timer.function = timeout_handler;
1467 garmin_data_p->port = port;
1468 garmin_data_p->state = 0;
Hermann Kneisselb4026c42011-04-29 08:58:43 +02001469 garmin_data_p->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 garmin_data_p->count = 0;
1471 usb_set_serial_port_data(port, garmin_data_p);
1472
1473 status = garmin_init_session(port);
1474
1475 return status;
1476}
1477
1478
Alan Sternf9c99bb2009-06-02 11:53:55 -04001479static void garmin_disconnect(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct usb_serial_port *port = serial->port[0];
Alan Coxaf6d7802008-07-22 11:11:44 +01001482 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Harvey Harrison441b62c2008-03-03 16:08:34 -08001484 dbg("%s", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Alan Coxaf6d7802008-07-22 11:11:44 +01001486 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 del_timer_sync(&garmin_data_p->timer);
Alan Sternf9c99bb2009-06-02 11:53:55 -04001488}
1489
1490
1491static void garmin_release(struct usb_serial *serial)
1492{
1493 struct usb_serial_port *port = serial->port[0];
1494 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port);
1495
1496 dbg("%s", __func__);
1497
Alan Coxaf6d7802008-07-22 11:11:44 +01001498 kfree(garmin_data_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/* All of the device info needed */
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -07001503static struct usb_serial_driver garmin_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001504 .driver = {
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001505 .owner = THIS_MODULE,
1506 .name = "garmin_gps",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -07001507 },
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001508 .description = "Garmin GPS usb/tty",
Johannes Hölzld9b1b782006-12-17 21:50:24 +01001509 .usb_driver = &garmin_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 .num_ports = 1,
1512 .open = garmin_open,
1513 .close = garmin_close,
1514 .throttle = garmin_throttle,
1515 .unthrottle = garmin_unthrottle,
1516 .attach = garmin_attach,
Alan Sternf9c99bb2009-06-02 11:53:55 -04001517 .disconnect = garmin_disconnect,
1518 .release = garmin_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 .write = garmin_write,
1520 .write_room = garmin_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 .write_bulk_callback = garmin_write_bulk_callback,
1522 .read_bulk_callback = garmin_read_bulk_callback,
1523 .read_int_callback = garmin_read_int_callback,
1524};
1525
1526
Hermann Kneisseleb6d8c22006-07-11 19:41:33 +02001527
Alan Coxaf6d7802008-07-22 11:11:44 +01001528static int __init garmin_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 int retval;
1531
1532 retval = usb_serial_register(&garmin_device);
1533 if (retval)
1534 goto failed_garmin_register;
1535 retval = usb_register(&garmin_driver);
1536 if (retval)
1537 goto failed_usb_register;
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -07001538 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1539 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 return 0;
1542failed_usb_register:
1543 usb_serial_deregister(&garmin_device);
1544failed_garmin_register:
1545 return retval;
1546}
1547
1548
Alan Coxaf6d7802008-07-22 11:11:44 +01001549static void __exit garmin_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Alan Coxaf6d7802008-07-22 11:11:44 +01001551 usb_deregister(&garmin_driver);
1552 usb_serial_deregister(&garmin_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
1555
1556
1557
1558module_init(garmin_init);
1559module_exit(garmin_exit);
1560
Alan Coxaf6d7802008-07-22 11:11:44 +01001561MODULE_AUTHOR(DRIVER_AUTHOR);
1562MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563MODULE_LICENSE("GPL");
1564
1565module_param(debug, bool, S_IWUSR | S_IRUGO);
1566MODULE_PARM_DESC(debug, "Debug enabled or not");
1567module_param(initial_mode, int, S_IRUGO);
1568MODULE_PARM_DESC(initial_mode, "Initial mode");
1569