Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Garmin GPS driver | |
3 | * | |
468d1362 | 4 | * Copyright (C) 2006,2007 Hermann Kneissel herkne@users.sourceforge.net |
1da177e4 LT |
5 | * |
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 | ||
1da177e4 LT |
26 | #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> | |
af6d780b | 36 | #include <linux/uaccess.h> |
468d1362 | 37 | #include <asm/atomic.h> |
1da177e4 | 38 | #include <linux/usb.h> |
a969888c | 39 | #include <linux/usb/serial.h> |
1da177e4 | 40 | |
eb6d8c2d HK |
41 | #include <linux/version.h> |
42 | ||
1da177e4 LT |
43 | /* the mode to be set when the port ist opened */ |
44 | static int initial_mode = 1; | |
45 | ||
46 | /* debug flag */ | |
af6d780b | 47 | static int debug; |
1da177e4 | 48 | |
1da177e4 LT |
49 | #define GARMIN_VENDOR_ID 0x091E |
50 | ||
51 | /* | |
52 | * Version Information | |
53 | */ | |
54 | ||
55 | #define VERSION_MAJOR 0 | |
468d1362 | 56 | #define VERSION_MINOR 31 |
1da177e4 LT |
57 | |
58 | #define _STR(s) #s | |
af6d780b | 59 | #define _DRIVER_VERSION(a, b) "v" _STR(a) "." _STR(b) |
1da177e4 LT |
60 | #define DRIVER_VERSION _DRIVER_VERSION(VERSION_MAJOR, VERSION_MINOR) |
61 | #define DRIVER_AUTHOR "hermann kneissel" | |
62 | #define DRIVER_DESC "garmin gps driver" | |
63 | ||
64 | /* error codes returned by the driver */ | |
65 | #define EINVPKT 1000 /* invalid packet structure */ | |
66 | ||
67 | ||
af6d780b | 68 | /* size of the header of a packet using the usb protocol */ |
1da177e4 LT |
69 | #define GARMIN_PKTHDR_LENGTH 12 |
70 | ||
af6d780b AC |
71 | /* max. possible size of a packet using the serial protocol */ |
72 | #define MAX_SERIAL_PKT_SIZ (3 + 255 + 3) | |
1da177e4 | 73 | |
af6d780b AC |
74 | /* max. possible size of a packet with worst case stuffing */ |
75 | #define MAX_SERIAL_PKT_SIZ_STUFFED (MAX_SERIAL_PKT_SIZ + 256) | |
1da177e4 | 76 | |
af6d780b AC |
77 | /* size of a buffer able to hold a complete (no stuffing) packet |
78 | * (the document protocol does not contain packets with a larger | |
79 | * size, but in theory a packet may be 64k+12 bytes - if in | |
80 | * later protocol versions larger packet sizes occur, this value | |
81 | * should be increased accordingly, so the input buffer is always | |
82 | * large enough the store a complete packet inclusive header) */ | |
83 | #define GPS_IN_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ) | |
1da177e4 | 84 | |
af6d780b AC |
85 | /* size of a buffer able to hold a complete (incl. stuffing) packet */ |
86 | #define GPS_OUT_BUFSIZ (GARMIN_PKTHDR_LENGTH+MAX_SERIAL_PKT_SIZ_STUFFED) | |
1da177e4 | 87 | |
af6d780b AC |
88 | /* where to place the packet id of a serial packet, so we can |
89 | * prepend the usb-packet header without the need to move the | |
90 | * packets data */ | |
1da177e4 LT |
91 | #define GSP_INITIAL_OFFSET (GARMIN_PKTHDR_LENGTH-2) |
92 | ||
af6d780b | 93 | /* max. size of incoming private packets (header+1 param) */ |
1da177e4 LT |
94 | #define PRIVPKTSIZ (GARMIN_PKTHDR_LENGTH+4) |
95 | ||
96 | #define GARMIN_LAYERID_TRANSPORT 0 | |
97 | #define GARMIN_LAYERID_APPL 20 | |
af6d780b | 98 | /* our own layer-id to use for some control mechanisms */ |
1da177e4 LT |
99 | #define GARMIN_LAYERID_PRIVATE 0x01106E4B |
100 | ||
101 | #define GARMIN_PKTID_PVT_DATA 51 | |
102 | #define GARMIN_PKTID_L001_COMMAND_DATA 10 | |
103 | ||
104 | #define CMND_ABORT_TRANSFER 0 | |
105 | ||
af6d780b | 106 | /* packet ids used in private layer */ |
1da177e4 LT |
107 | #define PRIV_PKTID_SET_DEBUG 1 |
108 | #define PRIV_PKTID_SET_MODE 2 | |
109 | #define PRIV_PKTID_INFO_REQ 3 | |
110 | #define PRIV_PKTID_INFO_RESP 4 | |
111 | #define PRIV_PKTID_RESET_REQ 5 | |
112 | #define PRIV_PKTID_SET_DEF_MODE 6 | |
113 | ||
114 | ||
115 | #define ETX 0x03 | |
116 | #define DLE 0x10 | |
117 | #define ACK 0x06 | |
118 | #define NAK 0x15 | |
119 | ||
120 | /* structure used to queue incoming packets */ | |
121 | struct garmin_packet { | |
122 | struct list_head list; | |
123 | int seq; | |
af6d780b AC |
124 | /* the real size of the data array, always > 0 */ |
125 | int size; | |
1da177e4 LT |
126 | __u8 data[1]; |
127 | }; | |
128 | ||
129 | /* structure used to keep the current state of the driver */ | |
130 | struct garmin_data { | |
131 | __u8 state; | |
132 | __u16 flags; | |
133 | __u8 mode; | |
134 | __u8 ignorePkts; | |
135 | __u8 count; | |
136 | __u8 pkt_id; | |
137 | __u32 serial_num; | |
138 | struct timer_list timer; | |
139 | struct usb_serial_port *port; | |
140 | int seq_counter; | |
141 | int insize; | |
142 | int outsize; | |
143 | __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */ | |
144 | __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */ | |
145 | __u8 privpkt[4*6]; | |
468d1362 HK |
146 | atomic_t req_count; |
147 | atomic_t resp_count; | |
1da177e4 LT |
148 | spinlock_t lock; |
149 | struct list_head pktlist; | |
150 | }; | |
151 | ||
152 | ||
153 | #define STATE_NEW 0 | |
154 | #define STATE_INITIAL_DELAY 1 | |
155 | #define STATE_TIMEOUT 2 | |
156 | #define STATE_SESSION_REQ1 3 | |
157 | #define STATE_SESSION_REQ2 4 | |
158 | #define STATE_ACTIVE 5 | |
159 | ||
160 | #define STATE_RESET 8 | |
161 | #define STATE_DISCONNECTED 9 | |
162 | #define STATE_WAIT_TTY_ACK 10 | |
163 | #define STATE_GSP_WAIT_DATA 11 | |
164 | ||
165 | #define MODE_NATIVE 0 | |
166 | #define MODE_GARMIN_SERIAL 1 | |
167 | ||
af6d780b | 168 | /* Flags used in garmin_data.flags: */ |
1da177e4 LT |
169 | #define FLAGS_SESSION_REPLY_MASK 0x00C0 |
170 | #define FLAGS_SESSION_REPLY1_SEEN 0x0080 | |
171 | #define FLAGS_SESSION_REPLY2_SEEN 0x0040 | |
172 | #define FLAGS_BULK_IN_ACTIVE 0x0020 | |
eb6d8c2d HK |
173 | #define FLAGS_BULK_IN_RESTART 0x0010 |
174 | #define FLAGS_THROTTLED 0x0008 | |
1da177e4 LT |
175 | #define CLEAR_HALT_REQUIRED 0x0001 |
176 | ||
177 | #define FLAGS_QUEUING 0x0100 | |
1da177e4 LT |
178 | #define FLAGS_DROP_DATA 0x0800 |
179 | ||
180 | #define FLAGS_GSP_SKIP 0x1000 | |
181 | #define FLAGS_GSP_DLESEEN 0x2000 | |
182 | ||
183 | ||
184 | ||
185 | ||
186 | ||
187 | ||
188 | /* function prototypes */ | |
af6d780b | 189 | static void gsp_next_packet(struct garmin_data *garmin_data_p); |
1da177e4 | 190 | static int garmin_write_bulk(struct usb_serial_port *port, |
468d1362 HK |
191 | const unsigned char *buf, int count, |
192 | int dismiss_ack); | |
1da177e4 LT |
193 | |
194 | /* some special packets to be send or received */ | |
195 | static unsigned char const GARMIN_START_SESSION_REQ[] | |
196 | = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 }; | |
197 | static unsigned char const GARMIN_START_SESSION_REQ2[] | |
198 | = { 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0 }; | |
199 | static unsigned char const GARMIN_START_SESSION_REPLY[] | |
200 | = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 }; | |
201 | static unsigned char const GARMIN_SESSION_ACTIVE_REPLY[] | |
202 | = { 0, 0, 0, 0, 17, 0, 0, 0, 4, 0, 0, 0, 0, 16, 0, 0 }; | |
203 | static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[] | |
204 | = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }; | |
205 | static unsigned char const GARMIN_APP_LAYER_REPLY[] | |
206 | = { 0x14, 0, 0, 0 }; | |
207 | static unsigned char const GARMIN_START_PVT_REQ[] | |
208 | = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 }; | |
209 | static unsigned char const GARMIN_STOP_PVT_REQ[] | |
210 | = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 }; | |
211 | static unsigned char const GARMIN_STOP_TRANSFER_REQ[] | |
212 | = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 }; | |
213 | static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[] | |
214 | = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 }; | |
215 | static unsigned char const PRIVATE_REQ[] | |
216 | = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 }; | |
217 | ||
218 | ||
219 | ||
220 | static struct usb_device_id id_table [] = { | |
af6d780b AC |
221 | /* the same device id seems to be used by all |
222 | usb enabled GPS devices */ | |
223 | { USB_DEVICE(GARMIN_VENDOR_ID, 3) }, | |
1da177e4 LT |
224 | { } /* Terminating entry */ |
225 | }; | |
226 | ||
af6d780b | 227 | MODULE_DEVICE_TABLE(usb, id_table); |
1da177e4 LT |
228 | |
229 | static struct usb_driver garmin_driver = { | |
1da177e4 LT |
230 | .name = "garmin_gps", |
231 | .probe = usb_serial_probe, | |
232 | .disconnect = usb_serial_disconnect, | |
233 | .id_table = id_table, | |
eb6d8c2d | 234 | .no_dynamic_id = 1, |
1da177e4 LT |
235 | }; |
236 | ||
237 | ||
af6d780b | 238 | static inline int noResponseFromAppLayer(struct garmin_data *garmin_data_p) |
1da177e4 | 239 | { |
af6d780b AC |
240 | return atomic_read(&garmin_data_p->req_count) == |
241 | atomic_read(&garmin_data_p->resp_count); | |
1da177e4 LT |
242 | } |
243 | ||
244 | ||
245 | static inline int getLayerId(const __u8 *usbPacket) | |
246 | { | |
247 | return __le32_to_cpup((__le32 *)(usbPacket)); | |
248 | } | |
249 | ||
250 | static inline int getPacketId(const __u8 *usbPacket) | |
251 | { | |
252 | return __le32_to_cpup((__le32 *)(usbPacket+4)); | |
253 | } | |
254 | ||
255 | static inline int getDataLength(const __u8 *usbPacket) | |
256 | { | |
257 | return __le32_to_cpup((__le32 *)(usbPacket+8)); | |
258 | } | |
259 | ||
260 | ||
261 | /* | |
262 | * check if the usb-packet in buf contains an abort-transfer command. | |
263 | * (if yes, all queued data will be dropped) | |
264 | */ | |
265 | static inline int isAbortTrfCmnd(const unsigned char *buf) | |
266 | { | |
af6d780b AC |
267 | if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ, |
268 | sizeof(GARMIN_STOP_TRANSFER_REQ)) || | |
269 | 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2, | |
270 | sizeof(GARMIN_STOP_TRANSFER_REQ_V2))) | |
1da177e4 LT |
271 | return 1; |
272 | else | |
273 | return 0; | |
274 | } | |
275 | ||
276 | ||
277 | ||
278 | static void send_to_tty(struct usb_serial_port *port, | |
eb6d8c2d | 279 | char *data, unsigned int actual_length) |
1da177e4 | 280 | { |
95da310e | 281 | struct tty_struct *tty = port->port.tty; |
1da177e4 LT |
282 | |
283 | if (tty && actual_length) { | |
284 | ||
af6d780b | 285 | usb_serial_debug_data(debug, &port->dev, |
441b62c1 | 286 | __func__, actual_length, data); |
1da177e4 | 287 | |
33f0f88f AC |
288 | tty_buffer_request_room(tty, actual_length); |
289 | tty_insert_flip_string(tty, data, actual_length); | |
1da177e4 LT |
290 | tty_flip_buffer_push(tty); |
291 | } | |
292 | } | |
293 | ||
294 | ||
295 | /****************************************************************************** | |
296 | * packet queue handling | |
297 | ******************************************************************************/ | |
298 | ||
299 | /* | |
300 | * queue a received (usb-)packet for later processing | |
301 | */ | |
af6d780b | 302 | static int pkt_add(struct garmin_data *garmin_data_p, |
eb6d8c2d | 303 | unsigned char *data, unsigned int data_length) |
1da177e4 | 304 | { |
eb6d8c2d | 305 | int state = 0; |
1da177e4 LT |
306 | int result = 0; |
307 | unsigned long flags; | |
308 | struct garmin_packet *pkt; | |
309 | ||
310 | /* process only packets containg data ... */ | |
311 | if (data_length) { | |
1da177e4 | 312 | pkt = kmalloc(sizeof(struct garmin_packet)+data_length, |
af6d780b | 313 | GFP_ATOMIC); |
1da177e4 LT |
314 | if (pkt == NULL) { |
315 | dev_err(&garmin_data_p->port->dev, "out of memory\n"); | |
316 | return 0; | |
317 | } | |
318 | pkt->size = data_length; | |
319 | memcpy(pkt->data, data, data_length); | |
320 | ||
321 | spin_lock_irqsave(&garmin_data_p->lock, flags); | |
eb6d8c2d | 322 | garmin_data_p->flags |= FLAGS_QUEUING; |
1da177e4 LT |
323 | result = list_empty(&garmin_data_p->pktlist); |
324 | pkt->seq = garmin_data_p->seq_counter++; | |
325 | list_add_tail(&pkt->list, &garmin_data_p->pktlist); | |
eb6d8c2d | 326 | state = garmin_data_p->state; |
1da177e4 LT |
327 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
328 | ||
329 | /* in serial mode, if someone is waiting for data from | |
330 | the device, iconvert and send the next packet to tty. */ | |
af6d780b | 331 | if (result && (state == STATE_GSP_WAIT_DATA)) |
1da177e4 | 332 | gsp_next_packet(garmin_data_p); |
1da177e4 LT |
333 | } |
334 | return result; | |
335 | } | |
336 | ||
337 | ||
338 | /* get the next pending packet */ | |
af6d780b | 339 | static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p) |
1da177e4 LT |
340 | { |
341 | unsigned long flags; | |
342 | struct garmin_packet *result = NULL; | |
343 | ||
344 | spin_lock_irqsave(&garmin_data_p->lock, flags); | |
345 | if (!list_empty(&garmin_data_p->pktlist)) { | |
346 | result = (struct garmin_packet *)garmin_data_p->pktlist.next; | |
347 | list_del(&result->list); | |
348 | } | |
349 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
350 | return result; | |
351 | } | |
352 | ||
353 | ||
354 | /* free up all queued data */ | |
af6d780b | 355 | static void pkt_clear(struct garmin_data *garmin_data_p) |
1da177e4 LT |
356 | { |
357 | unsigned long flags; | |
358 | struct garmin_packet *result = NULL; | |
359 | ||
441b62c1 | 360 | dbg("%s", __func__); |
1da177e4 LT |
361 | |
362 | spin_lock_irqsave(&garmin_data_p->lock, flags); | |
363 | while (!list_empty(&garmin_data_p->pktlist)) { | |
364 | result = (struct garmin_packet *)garmin_data_p->pktlist.next; | |
365 | list_del(&result->list); | |
366 | kfree(result); | |
367 | } | |
368 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
369 | } | |
370 | ||
371 | ||
372 | /****************************************************************************** | |
373 | * garmin serial protocol handling handling | |
374 | ******************************************************************************/ | |
375 | ||
376 | /* send an ack packet back to the tty */ | |
af6d780b | 377 | static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id) |
1da177e4 LT |
378 | { |
379 | __u8 pkt[10]; | |
eb6d8c2d HK |
380 | __u8 cksum = 0; |
381 | __u8 *ptr = pkt; | |
382 | unsigned l = 0; | |
1da177e4 | 383 | |
441b62c1 | 384 | dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id); |
1da177e4 LT |
385 | |
386 | *ptr++ = DLE; | |
387 | *ptr++ = ACK; | |
388 | cksum += ACK; | |
389 | ||
390 | *ptr++ = 2; | |
391 | cksum += 2; | |
392 | ||
393 | *ptr++ = pkt_id; | |
394 | cksum += pkt_id; | |
395 | ||
af6d780b | 396 | if (pkt_id == DLE) |
1da177e4 | 397 | *ptr++ = DLE; |
1da177e4 LT |
398 | |
399 | *ptr++ = 0; | |
400 | *ptr++ = 0xFF & (-cksum); | |
401 | *ptr++ = DLE; | |
402 | *ptr++ = ETX; | |
403 | ||
404 | l = ptr-pkt; | |
405 | ||
406 | send_to_tty(garmin_data_p->port, pkt, l); | |
407 | return 0; | |
408 | } | |
409 | ||
410 | ||
411 | ||
412 | /* | |
413 | * called for a complete packet received from tty layer | |
414 | * | |
415 | * the complete packet (pkzid ... cksum) is in garmin_data_p->inbuf starting | |
416 | * at GSP_INITIAL_OFFSET. | |
417 | * | |
418 | * count - number of bytes in the input buffer including space reserved for | |
af6d780b | 419 | * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet |
1da177e4 LT |
420 | * (including pkt-id, data-length a. cksum) |
421 | */ | |
af6d780b | 422 | static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count) |
1da177e4 | 423 | { |
af6d780b | 424 | const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET; |
eb6d8c2d | 425 | __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer; |
1da177e4 LT |
426 | |
427 | int cksum = 0; | |
428 | int n = 0; | |
429 | int pktid = recpkt[0]; | |
430 | int size = recpkt[1]; | |
431 | ||
432 | usb_serial_debug_data(debug, &garmin_data_p->port->dev, | |
441b62c1 | 433 | __func__, count-GSP_INITIAL_OFFSET, recpkt); |
1da177e4 LT |
434 | |
435 | if (size != (count-GSP_INITIAL_OFFSET-3)) { | |
436 | dbg("%s - invalid size, expected %d bytes, got %d", | |
441b62c1 | 437 | __func__, size, (count-GSP_INITIAL_OFFSET-3)); |
1da177e4 LT |
438 | return -EINVPKT; |
439 | } | |
440 | ||
441 | cksum += *recpkt++; | |
442 | cksum += *recpkt++; | |
443 | ||
af6d780b AC |
444 | /* sanity check, remove after test ... */ |
445 | if ((__u8 *)&(usbdata[3]) != recpkt) { | |
1da177e4 | 446 | dbg("%s - ptr mismatch %p - %p", |
441b62c1 | 447 | __func__, &(usbdata[4]), recpkt); |
1da177e4 LT |
448 | return -EINVPKT; |
449 | } | |
450 | ||
451 | while (n < size) { | |
452 | cksum += *recpkt++; | |
453 | n++; | |
454 | } | |
455 | ||
eb6d8c2d HK |
456 | if ((0xff & (cksum + *recpkt)) != 0) { |
457 | dbg("%s - invalid checksum, expected %02x, got %02x", | |
441b62c1 | 458 | __func__, 0xff & -cksum, 0xff & *recpkt); |
eb6d8c2d HK |
459 | return -EINVPKT; |
460 | } | |
1da177e4 LT |
461 | |
462 | usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL); | |
463 | usbdata[1] = __cpu_to_le32(pktid); | |
464 | usbdata[2] = __cpu_to_le32(size); | |
465 | ||
af6d780b | 466 | garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer, |
468d1362 | 467 | GARMIN_PKTHDR_LENGTH+size, 0); |
1da177e4 LT |
468 | |
469 | /* if this was an abort-transfer command, flush all | |
470 | queued data. */ | |
471 | if (isAbortTrfCmnd(garmin_data_p->inbuffer)) { | |
472 | garmin_data_p->flags |= FLAGS_DROP_DATA; | |
473 | pkt_clear(garmin_data_p); | |
474 | } | |
475 | ||
476 | return count; | |
477 | } | |
478 | ||
479 | ||
480 | ||
481 | /* | |
482 | * Called for data received from tty | |
483 | * | |
484 | * buf contains the data read, it may span more than one packet or even | |
485 | * incomplete packets | |
486 | * | |
487 | * input record should be a serial-record, but it may not be complete. | |
488 | * Copy it into our local buffer, until an etx is seen (or an error | |
489 | * occurs). | |
490 | * Once the record is complete, convert into a usb packet and send it | |
491 | * to the bulk pipe, send an ack back to the tty. | |
492 | * | |
493 | * If the input is an ack, just send the last queued packet to the | |
494 | * tty layer. | |
495 | * | |
496 | * if the input is an abort command, drop all queued data. | |
497 | */ | |
498 | ||
af6d780b | 499 | static int gsp_receive(struct garmin_data *garmin_data_p, |
eb6d8c2d | 500 | const unsigned char *buf, int count) |
1da177e4 | 501 | { |
eb6d8c2d | 502 | unsigned long flags; |
1da177e4 LT |
503 | int offs = 0; |
504 | int ack_or_nak_seen = 0; | |
505 | int i = 0; | |
eb6d8c2d HK |
506 | __u8 *dest; |
507 | int size; | |
af6d780b | 508 | /* dleSeen: set if last byte read was a DLE */ |
eb6d8c2d | 509 | int dleSeen; |
af6d780b AC |
510 | /* skip: if set, skip incoming data until possible start of |
511 | * new packet | |
512 | */ | |
eb6d8c2d | 513 | int skip; |
1da177e4 LT |
514 | __u8 data; |
515 | ||
eb6d8c2d HK |
516 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
517 | dest = garmin_data_p->inbuffer; | |
518 | size = garmin_data_p->insize; | |
519 | dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN; | |
520 | skip = garmin_data_p->flags & FLAGS_GSP_SKIP; | |
521 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
522 | ||
1da177e4 | 523 | dbg("%s - dle=%d skip=%d size=%d count=%d", |
441b62c1 | 524 | __func__, dleSeen, skip, size, count); |
1da177e4 | 525 | |
af6d780b | 526 | if (size == 0) |
1da177e4 | 527 | size = GSP_INITIAL_OFFSET; |
1da177e4 LT |
528 | |
529 | while (offs < count) { | |
530 | ||
531 | data = *(buf+offs); | |
af6d780b | 532 | offs++; |
1da177e4 LT |
533 | |
534 | if (data == DLE) { | |
535 | if (skip) { /* start of a new pkt */ | |
536 | skip = 0; | |
537 | size = GSP_INITIAL_OFFSET; | |
538 | dleSeen = 1; | |
539 | } else if (dleSeen) { | |
540 | dest[size++] = data; | |
541 | dleSeen = 0; | |
542 | } else { | |
543 | dleSeen = 1; | |
544 | } | |
545 | } else if (data == ETX) { | |
546 | if (dleSeen) { | |
547 | /* packet complete */ | |
548 | ||
549 | data = dest[GSP_INITIAL_OFFSET]; | |
550 | ||
551 | if (data == ACK) { | |
552 | ack_or_nak_seen = ACK; | |
553 | dbg("ACK packet complete."); | |
554 | } else if (data == NAK) { | |
555 | ack_or_nak_seen = NAK; | |
556 | dbg("NAK packet complete."); | |
557 | } else { | |
af6d780b AC |
558 | dbg("packet complete - id=0x%X.", |
559 | 0xFF & data); | |
1da177e4 LT |
560 | gsp_rec_packet(garmin_data_p, size); |
561 | } | |
562 | ||
563 | skip = 1; | |
564 | size = GSP_INITIAL_OFFSET; | |
565 | dleSeen = 0; | |
566 | } else { | |
567 | dest[size++] = data; | |
568 | } | |
569 | } else if (!skip) { | |
570 | ||
571 | if (dleSeen) { | |
572 | dbg("non-masked DLE at %d - restarting", i); | |
573 | size = GSP_INITIAL_OFFSET; | |
574 | dleSeen = 0; | |
575 | } | |
576 | ||
577 | dest[size++] = data; | |
578 | } | |
579 | ||
580 | if (size >= GPS_IN_BUFSIZ) { | |
441b62c1 | 581 | dbg("%s - packet too large.", __func__); |
1da177e4 LT |
582 | skip = 1; |
583 | size = GSP_INITIAL_OFFSET; | |
584 | dleSeen = 0; | |
585 | } | |
586 | } | |
587 | ||
eb6d8c2d HK |
588 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
589 | ||
1da177e4 LT |
590 | garmin_data_p->insize = size; |
591 | ||
af6d780b | 592 | /* copy flags back to structure */ |
1da177e4 LT |
593 | if (skip) |
594 | garmin_data_p->flags |= FLAGS_GSP_SKIP; | |
595 | else | |
596 | garmin_data_p->flags &= ~FLAGS_GSP_SKIP; | |
597 | ||
598 | if (dleSeen) | |
599 | garmin_data_p->flags |= FLAGS_GSP_DLESEEN; | |
600 | else | |
601 | garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN; | |
602 | ||
af6d780b | 603 | if (ack_or_nak_seen) |
1da177e4 | 604 | garmin_data_p->state = STATE_GSP_WAIT_DATA; |
eb6d8c2d HK |
605 | |
606 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
607 | ||
af6d780b | 608 | if (ack_or_nak_seen) |
1da177e4 | 609 | gsp_next_packet(garmin_data_p); |
1da177e4 LT |
610 | return count; |
611 | } | |
612 | ||
613 | ||
614 | ||
615 | ||
616 | /* | |
617 | * Sends a usb packet to the tty | |
618 | * | |
619 | * Assumes, that all packages and at an usb-packet boundary. | |
620 | * | |
621 | * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent | |
622 | */ | |
af6d780b | 623 | static int gsp_send(struct garmin_data *garmin_data_p, |
1da177e4 LT |
624 | const unsigned char *buf, int count) |
625 | { | |
626 | const unsigned char *src; | |
627 | unsigned char *dst; | |
628 | int pktid = 0; | |
629 | int datalen = 0; | |
630 | int cksum = 0; | |
af6d780b | 631 | int i = 0; |
1da177e4 LT |
632 | int k; |
633 | ||
441b62c1 | 634 | dbg("%s - state %d - %d bytes.", __func__, |
af6d780b | 635 | garmin_data_p->state, count); |
1da177e4 LT |
636 | |
637 | k = garmin_data_p->outsize; | |
638 | if ((k+count) > GPS_OUT_BUFSIZ) { | |
639 | dbg("packet too large"); | |
640 | garmin_data_p->outsize = 0; | |
641 | return -4; | |
642 | } | |
643 | ||
644 | memcpy(garmin_data_p->outbuffer+k, buf, count); | |
645 | k += count; | |
646 | garmin_data_p->outsize = k; | |
647 | ||
648 | if (k >= GARMIN_PKTHDR_LENGTH) { | |
649 | pktid = getPacketId(garmin_data_p->outbuffer); | |
af6d780b | 650 | datalen = getDataLength(garmin_data_p->outbuffer); |
1da177e4 LT |
651 | i = GARMIN_PKTHDR_LENGTH + datalen; |
652 | if (k < i) | |
653 | return 0; | |
654 | } else { | |
655 | return 0; | |
656 | } | |
657 | ||
af6d780b | 658 | dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, k, i); |
1da177e4 LT |
659 | |
660 | /* garmin_data_p->outbuffer now contains a complete packet */ | |
661 | ||
662 | usb_serial_debug_data(debug, &garmin_data_p->port->dev, | |
af6d780b | 663 | __func__, k, garmin_data_p->outbuffer); |
1da177e4 LT |
664 | |
665 | garmin_data_p->outsize = 0; | |
666 | ||
667 | if (GARMIN_LAYERID_APPL != getLayerId(garmin_data_p->outbuffer)) { | |
af6d780b AC |
668 | dbg("not an application packet (%d)", |
669 | getLayerId(garmin_data_p->outbuffer)); | |
1da177e4 LT |
670 | return -1; |
671 | } | |
672 | ||
673 | if (pktid > 255) { | |
674 | dbg("packet-id %d too large", pktid); | |
675 | return -2; | |
676 | } | |
677 | ||
678 | if (datalen > 255) { | |
679 | dbg("packet-size %d too large", datalen); | |
680 | return -3; | |
681 | } | |
682 | ||
683 | /* the serial protocol should be able to handle this packet */ | |
684 | ||
685 | k = 0; | |
686 | src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH; | |
af6d780b | 687 | for (i = 0; i < datalen; i++) { |
1da177e4 LT |
688 | if (*src++ == DLE) |
689 | k++; | |
690 | } | |
691 | ||
692 | src = garmin_data_p->outbuffer+GARMIN_PKTHDR_LENGTH; | |
693 | if (k > (GARMIN_PKTHDR_LENGTH-2)) { | |
af6d780b | 694 | /* can't add stuffing DLEs in place, move data to end |
eb6d8c2d | 695 | of buffer ... */ |
1da177e4 LT |
696 | dst = garmin_data_p->outbuffer+GPS_OUT_BUFSIZ-datalen; |
697 | memcpy(dst, src, datalen); | |
698 | src = dst; | |
699 | } | |
700 | ||
701 | dst = garmin_data_p->outbuffer; | |
702 | ||
703 | *dst++ = DLE; | |
704 | *dst++ = pktid; | |
705 | cksum += pktid; | |
706 | *dst++ = datalen; | |
707 | cksum += datalen; | |
708 | if (datalen == DLE) | |
709 | *dst++ = DLE; | |
710 | ||
af6d780b | 711 | for (i = 0; i < datalen; i++) { |
1da177e4 LT |
712 | __u8 c = *src++; |
713 | *dst++ = c; | |
714 | cksum += c; | |
715 | if (c == DLE) | |
716 | *dst++ = DLE; | |
717 | } | |
af6d780b | 718 | |
1da177e4 LT |
719 | cksum = 0xFF & -cksum; |
720 | *dst++ = cksum; | |
721 | if (cksum == DLE) | |
722 | *dst++ = DLE; | |
723 | *dst++ = DLE; | |
724 | *dst++ = ETX; | |
725 | ||
726 | i = dst-garmin_data_p->outbuffer; | |
727 | ||
728 | send_to_tty(garmin_data_p->port, garmin_data_p->outbuffer, i); | |
729 | ||
730 | garmin_data_p->pkt_id = pktid; | |
731 | garmin_data_p->state = STATE_WAIT_TTY_ACK; | |
732 | ||
733 | return i; | |
734 | } | |
735 | ||
736 | ||
737 | ||
738 | ||
739 | ||
740 | /* | |
741 | * Process the next pending data packet - if there is one | |
742 | */ | |
af6d780b | 743 | static void gsp_next_packet(struct garmin_data *garmin_data_p) |
1da177e4 LT |
744 | { |
745 | struct garmin_packet *pkt = NULL; | |
746 | ||
747 | while ((pkt = pkt_pop(garmin_data_p)) != NULL) { | |
441b62c1 | 748 | dbg("%s - next pkt: %d", __func__, pkt->seq); |
1da177e4 LT |
749 | if (gsp_send(garmin_data_p, pkt->data, pkt->size) > 0) { |
750 | kfree(pkt); | |
751 | return; | |
752 | } | |
753 | kfree(pkt); | |
754 | } | |
755 | } | |
756 | ||
757 | ||
758 | ||
759 | ||
760 | /****************************************************************************** | |
761 | * garmin native mode | |
762 | ******************************************************************************/ | |
763 | ||
764 | ||
765 | /* | |
766 | * Called for data received from tty | |
767 | * | |
768 | * The input data is expected to be in garmin usb-packet format. | |
769 | * | |
770 | * buf contains the data read, it may span more than one packet | |
771 | * or even incomplete packets | |
772 | */ | |
af6d780b | 773 | static int nat_receive(struct garmin_data *garmin_data_p, |
eb6d8c2d | 774 | const unsigned char *buf, int count) |
1da177e4 | 775 | { |
eb6d8c2d | 776 | unsigned long flags; |
af6d780b | 777 | __u8 *dest; |
1da177e4 LT |
778 | int offs = 0; |
779 | int result = count; | |
780 | int len; | |
781 | ||
782 | while (offs < count) { | |
af6d780b | 783 | /* if buffer contains header, copy rest of data */ |
1da177e4 LT |
784 | if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) |
785 | len = GARMIN_PKTHDR_LENGTH | |
786 | +getDataLength(garmin_data_p->inbuffer); | |
787 | else | |
788 | len = GARMIN_PKTHDR_LENGTH; | |
789 | ||
790 | if (len >= GPS_IN_BUFSIZ) { | |
af6d780b AC |
791 | /* seems to be an invalid packet, ignore rest |
792 | of input */ | |
793 | dbg("%s - packet size too large: %d", __func__, len); | |
1da177e4 LT |
794 | garmin_data_p->insize = 0; |
795 | count = 0; | |
796 | result = -EINVPKT; | |
797 | } else { | |
798 | len -= garmin_data_p->insize; | |
799 | if (len > (count-offs)) | |
800 | len = (count-offs); | |
801 | if (len > 0) { | |
802 | dest = garmin_data_p->inbuffer | |
af6d780b | 803 | + garmin_data_p->insize; |
1da177e4 LT |
804 | memcpy(dest, buf+offs, len); |
805 | garmin_data_p->insize += len; | |
806 | offs += len; | |
807 | } | |
808 | } | |
809 | ||
810 | /* do we have a complete packet ? */ | |
811 | if (garmin_data_p->insize >= GARMIN_PKTHDR_LENGTH) { | |
812 | len = GARMIN_PKTHDR_LENGTH+ | |
813 | getDataLength(garmin_data_p->inbuffer); | |
814 | if (garmin_data_p->insize >= len) { | |
af6d780b AC |
815 | garmin_write_bulk(garmin_data_p->port, |
816 | garmin_data_p->inbuffer, | |
817 | len, 0); | |
1da177e4 LT |
818 | garmin_data_p->insize = 0; |
819 | ||
820 | /* if this was an abort-transfer command, | |
821 | flush all queued data. */ | |
822 | if (isAbortTrfCmnd(garmin_data_p->inbuffer)) { | |
af6d780b AC |
823 | spin_lock_irqsave(&garmin_data_p->lock, |
824 | flags); | |
1da177e4 | 825 | garmin_data_p->flags |= FLAGS_DROP_DATA; |
af6d780b AC |
826 | spin_unlock_irqrestore( |
827 | &garmin_data_p->lock, flags); | |
1da177e4 LT |
828 | pkt_clear(garmin_data_p); |
829 | } | |
830 | } | |
831 | } | |
832 | } | |
833 | return result; | |
834 | } | |
835 | ||
836 | ||
837 | /****************************************************************************** | |
838 | * private packets | |
839 | ******************************************************************************/ | |
840 | ||
841 | static void priv_status_resp(struct usb_serial_port *port) | |
842 | { | |
af6d780b | 843 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 LT |
844 | __le32 *pkt = (__le32 *)garmin_data_p->privpkt; |
845 | ||
846 | pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE); | |
847 | pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP); | |
848 | pkt[2] = __cpu_to_le32(12); | |
849 | pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR); | |
850 | pkt[4] = __cpu_to_le32(garmin_data_p->mode); | |
851 | pkt[5] = __cpu_to_le32(garmin_data_p->serial_num); | |
852 | ||
af6d780b | 853 | send_to_tty(port, (__u8 *)pkt, 6 * 4); |
1da177e4 LT |
854 | } |
855 | ||
856 | ||
857 | /****************************************************************************** | |
858 | * Garmin specific driver functions | |
859 | ******************************************************************************/ | |
860 | ||
861 | static int process_resetdev_request(struct usb_serial_port *port) | |
862 | { | |
eb6d8c2d | 863 | unsigned long flags; |
1da177e4 | 864 | int status; |
af6d780b | 865 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 | 866 | |
eb6d8c2d | 867 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 LT |
868 | garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED); |
869 | garmin_data_p->state = STATE_RESET; | |
870 | garmin_data_p->serial_num = 0; | |
eb6d8c2d | 871 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 | 872 | |
af6d780b AC |
873 | usb_kill_urb(port->interrupt_in_urb); |
874 | dbg("%s - usb_reset_device", __func__); | |
1da177e4 LT |
875 | status = usb_reset_device(port->serial->dev); |
876 | if (status) | |
877 | dbg("%s - usb_reset_device failed: %d", | |
441b62c1 | 878 | __func__, status); |
1da177e4 LT |
879 | return status; |
880 | } | |
881 | ||
882 | ||
883 | ||
884 | /* | |
885 | * clear all cached data | |
886 | */ | |
af6d780b | 887 | static int garmin_clear(struct garmin_data *garmin_data_p) |
1da177e4 | 888 | { |
eb6d8c2d | 889 | unsigned long flags; |
1da177e4 LT |
890 | int status = 0; |
891 | ||
892 | struct usb_serial_port *port = garmin_data_p->port; | |
893 | ||
468d1362 | 894 | if (port != NULL && atomic_read(&garmin_data_p->resp_count)) { |
1da177e4 LT |
895 | /* send a terminate command */ |
896 | status = garmin_write_bulk(port, GARMIN_STOP_TRANSFER_REQ, | |
af6d780b | 897 | sizeof(GARMIN_STOP_TRANSFER_REQ), 1); |
1da177e4 LT |
898 | } |
899 | ||
900 | /* flush all queued data */ | |
901 | pkt_clear(garmin_data_p); | |
902 | ||
eb6d8c2d | 903 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 LT |
904 | garmin_data_p->insize = 0; |
905 | garmin_data_p->outsize = 0; | |
eb6d8c2d | 906 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
907 | |
908 | return status; | |
909 | } | |
910 | ||
911 | ||
912 | ||
913 | ||
914 | ||
915 | ||
916 | static int garmin_init_session(struct usb_serial_port *port) | |
917 | { | |
eb6d8c2d | 918 | unsigned long flags; |
1da177e4 | 919 | struct usb_serial *serial = port->serial; |
af6d780b | 920 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 LT |
921 | int status = 0; |
922 | ||
923 | if (status == 0) { | |
af6d780b | 924 | usb_kill_urb(port->interrupt_in_urb); |
1da177e4 | 925 | |
441b62c1 | 926 | dbg("%s - adding interrupt input", __func__); |
1da177e4 LT |
927 | port->interrupt_in_urb->dev = serial->dev; |
928 | status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); | |
929 | if (status) | |
930 | dev_err(&serial->dev->dev, | |
af6d780b AC |
931 | "%s - failed submitting interrupt urb, error %d\n", |
932 | __func__, status); | |
1da177e4 LT |
933 | } |
934 | ||
935 | if (status == 0) { | |
441b62c1 | 936 | dbg("%s - starting session ...", __func__); |
1da177e4 LT |
937 | garmin_data_p->state = STATE_ACTIVE; |
938 | status = garmin_write_bulk(port, GARMIN_START_SESSION_REQ, | |
af6d780b | 939 | sizeof(GARMIN_START_SESSION_REQ), 0); |
1da177e4 LT |
940 | |
941 | if (status >= 0) { | |
942 | ||
eb6d8c2d | 943 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 944 | garmin_data_p->ignorePkts++; |
eb6d8c2d | 945 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
946 | |
947 | /* not needed, but the win32 driver does it too ... */ | |
948 | status = garmin_write_bulk(port, | |
af6d780b AC |
949 | GARMIN_START_SESSION_REQ2, |
950 | sizeof(GARMIN_START_SESSION_REQ2), 0); | |
1da177e4 LT |
951 | if (status >= 0) { |
952 | status = 0; | |
eb6d8c2d | 953 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 954 | garmin_data_p->ignorePkts++; |
af6d780b AC |
955 | spin_unlock_irqrestore(&garmin_data_p->lock, |
956 | flags); | |
1da177e4 LT |
957 | } |
958 | } | |
959 | } | |
960 | ||
961 | return status; | |
962 | } | |
963 | ||
964 | ||
965 | ||
966 | ||
967 | ||
af6d780b | 968 | static int garmin_open(struct tty_struct *tty, |
95da310e | 969 | struct usb_serial_port *port, struct file *filp) |
1da177e4 | 970 | { |
eb6d8c2d | 971 | unsigned long flags; |
1da177e4 | 972 | int status = 0; |
af6d780b | 973 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 | 974 | |
441b62c1 | 975 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
976 | |
977 | /* | |
978 | * Force low_latency on so that our tty_push actually forces the data | |
979 | * through, otherwise it is scheduled, and with high data rates (like | |
980 | * with OHCI) data can get lost. | |
981 | */ | |
95da310e AC |
982 | if (tty) |
983 | tty->low_latency = 1; | |
1da177e4 | 984 | |
eb6d8c2d | 985 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 LT |
986 | garmin_data_p->mode = initial_mode; |
987 | garmin_data_p->count = 0; | |
988 | garmin_data_p->flags = 0; | |
468d1362 HK |
989 | atomic_set(&garmin_data_p->req_count, 0); |
990 | atomic_set(&garmin_data_p->resp_count, 0); | |
eb6d8c2d | 991 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
992 | |
993 | /* shutdown any bulk reads that might be going on */ | |
af6d780b AC |
994 | usb_kill_urb(port->write_urb); |
995 | usb_kill_urb(port->read_urb); | |
1da177e4 | 996 | |
95da310e | 997 | if (garmin_data_p->state == STATE_RESET) |
1da177e4 | 998 | status = garmin_init_session(port); |
1da177e4 LT |
999 | |
1000 | garmin_data_p->state = STATE_ACTIVE; | |
1da177e4 LT |
1001 | return status; |
1002 | } | |
1003 | ||
1004 | ||
95da310e | 1005 | static void garmin_close(struct tty_struct *tty, |
af6d780b | 1006 | struct usb_serial_port *port, struct file *filp) |
1da177e4 LT |
1007 | { |
1008 | struct usb_serial *serial = port->serial; | |
af6d780b | 1009 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 | 1010 | |
441b62c1 | 1011 | dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__, |
1da177e4 LT |
1012 | port->number, garmin_data_p->mode, |
1013 | garmin_data_p->state, garmin_data_p->flags); | |
1014 | ||
1015 | if (!serial) | |
1016 | return; | |
1017 | ||
95bef012 ON |
1018 | mutex_lock(&port->serial->disc_mutex); |
1019 | if (!port->serial->disconnected) | |
1020 | garmin_clear(garmin_data_p); | |
1da177e4 LT |
1021 | |
1022 | /* shutdown our urbs */ | |
af6d780b AC |
1023 | usb_kill_urb(port->read_urb); |
1024 | usb_kill_urb(port->write_urb); | |
1da177e4 | 1025 | |
95bef012 ON |
1026 | if (!port->serial->disconnected) { |
1027 | if (noResponseFromAppLayer(garmin_data_p) || | |
1028 | ((garmin_data_p->flags & CLEAR_HALT_REQUIRED) != 0)) { | |
1029 | process_resetdev_request(port); | |
1030 | garmin_data_p->state = STATE_RESET; | |
1031 | } else { | |
1032 | garmin_data_p->state = STATE_DISCONNECTED; | |
1033 | } | |
1da177e4 LT |
1034 | } else { |
1035 | garmin_data_p->state = STATE_DISCONNECTED; | |
1036 | } | |
95bef012 | 1037 | mutex_unlock(&port->serial->disc_mutex); |
1da177e4 LT |
1038 | } |
1039 | ||
af6d780b | 1040 | static void garmin_write_bulk_callback(struct urb *urb) |
1da177e4 | 1041 | { |
eb6d8c2d | 1042 | unsigned long flags; |
cdc97792 | 1043 | struct usb_serial_port *port = urb->context; |
f9feb517 | 1044 | int status = urb->status; |
1da177e4 | 1045 | |
468d1362 | 1046 | if (port) { |
af6d780b AC |
1047 | struct garmin_data *garmin_data_p = |
1048 | usb_get_serial_port_data(port); | |
1da177e4 | 1049 | |
441b62c1 | 1050 | dbg("%s - port %d", __func__, port->number); |
1da177e4 | 1051 | |
468d1362 HK |
1052 | if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer) |
1053 | && (garmin_data_p->mode == MODE_GARMIN_SERIAL)) { | |
af6d780b AC |
1054 | gsp_send_ack(garmin_data_p, |
1055 | ((__u8 *)urb->transfer_buffer)[4]); | |
468d1362 HK |
1056 | } |
1057 | ||
1058 | if (status) { | |
1059 | dbg("%s - nonzero write bulk status received: %d", | |
441b62c1 | 1060 | __func__, urb->status); |
468d1362 HK |
1061 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1062 | garmin_data_p->flags |= CLEAR_HALT_REQUIRED; | |
1063 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
1064 | } | |
1065 | ||
1066 | usb_serial_port_softint(port); | |
1da177e4 LT |
1067 | } |
1068 | ||
af6d780b AC |
1069 | /* Ignore errors that resulted from garmin_write_bulk with |
1070 | dismiss_ack = 1 */ | |
468d1362 HK |
1071 | |
1072 | /* free up the transfer buffer, as usb_free_urb() does not do this */ | |
af6d780b | 1073 | kfree(urb->transfer_buffer); |
1da177e4 LT |
1074 | } |
1075 | ||
1076 | ||
af6d780b | 1077 | static int garmin_write_bulk(struct usb_serial_port *port, |
468d1362 HK |
1078 | const unsigned char *buf, int count, |
1079 | int dismiss_ack) | |
1da177e4 | 1080 | { |
eb6d8c2d | 1081 | unsigned long flags; |
1da177e4 | 1082 | struct usb_serial *serial = port->serial; |
af6d780b | 1083 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 LT |
1084 | struct urb *urb; |
1085 | unsigned char *buffer; | |
1086 | int status; | |
1087 | ||
441b62c1 | 1088 | dbg("%s - port %d, state %d", __func__, port->number, |
1da177e4 LT |
1089 | garmin_data_p->state); |
1090 | ||
eb6d8c2d | 1091 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 1092 | garmin_data_p->flags &= ~FLAGS_DROP_DATA; |
eb6d8c2d | 1093 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 | 1094 | |
af6d780b | 1095 | buffer = kmalloc(count, GFP_ATOMIC); |
1da177e4 LT |
1096 | if (!buffer) { |
1097 | dev_err(&port->dev, "out of memory\n"); | |
1098 | return -ENOMEM; | |
1099 | } | |
1100 | ||
1101 | urb = usb_alloc_urb(0, GFP_ATOMIC); | |
1102 | if (!urb) { | |
1103 | dev_err(&port->dev, "no more free urbs\n"); | |
af6d780b | 1104 | kfree(buffer); |
1da177e4 LT |
1105 | return -ENOMEM; |
1106 | } | |
1107 | ||
af6d780b | 1108 | memcpy(buffer, buf, count); |
1da177e4 | 1109 | |
441b62c1 | 1110 | usb_serial_debug_data(debug, &port->dev, __func__, count, buffer); |
1da177e4 | 1111 | |
af6d780b AC |
1112 | usb_fill_bulk_urb(urb, serial->dev, |
1113 | usb_sndbulkpipe(serial->dev, | |
1114 | port->bulk_out_endpointAddress), | |
1da177e4 | 1115 | buffer, count, |
468d1362 HK |
1116 | garmin_write_bulk_callback, |
1117 | dismiss_ack ? NULL : port); | |
1da177e4 LT |
1118 | urb->transfer_flags |= URB_ZERO_PACKET; |
1119 | ||
1120 | if (GARMIN_LAYERID_APPL == getLayerId(buffer)) { | |
468d1362 | 1121 | atomic_inc(&garmin_data_p->req_count); |
1da177e4 LT |
1122 | if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { |
1123 | pkt_clear(garmin_data_p); | |
1124 | garmin_data_p->state = STATE_GSP_WAIT_DATA; | |
1125 | } | |
1126 | } | |
1127 | ||
1128 | /* send it down the pipe */ | |
1129 | status = usb_submit_urb(urb, GFP_ATOMIC); | |
1130 | if (status) { | |
1131 | dev_err(&port->dev, | |
af6d780b | 1132 | "%s - usb_submit_urb(write bulk) failed with status = %d\n", |
441b62c1 | 1133 | __func__, status); |
1da177e4 | 1134 | count = status; |
1da177e4 LT |
1135 | } |
1136 | ||
1137 | /* we are done with this urb, so let the host driver | |
1138 | * really free it when it is finished with it */ | |
af6d780b | 1139 | usb_free_urb(urb); |
1da177e4 LT |
1140 | |
1141 | return count; | |
1142 | } | |
1143 | ||
af6d780b | 1144 | static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port, |
95da310e | 1145 | const unsigned char *buf, int count) |
1da177e4 LT |
1146 | { |
1147 | int pktid, pktsiz, len; | |
af6d780b | 1148 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 LT |
1149 | __le32 *privpkt = (__le32 *)garmin_data_p->privpkt; |
1150 | ||
441b62c1 | 1151 | usb_serial_debug_data(debug, &port->dev, __func__, count, buf); |
1da177e4 LT |
1152 | |
1153 | /* check for our private packets */ | |
1154 | if (count >= GARMIN_PKTHDR_LENGTH) { | |
1da177e4 LT |
1155 | len = PRIVPKTSIZ; |
1156 | if (count < len) | |
1157 | len = count; | |
1158 | ||
1159 | memcpy(garmin_data_p->privpkt, buf, len); | |
1160 | ||
1161 | pktsiz = getDataLength(garmin_data_p->privpkt); | |
1162 | pktid = getPacketId(garmin_data_p->privpkt); | |
1163 | ||
1164 | if (count == (GARMIN_PKTHDR_LENGTH+pktsiz) | |
af6d780b AC |
1165 | && GARMIN_LAYERID_PRIVATE == |
1166 | getLayerId(garmin_data_p->privpkt)) { | |
1da177e4 LT |
1167 | |
1168 | dbg("%s - processing private request %d", | |
441b62c1 | 1169 | __func__, pktid); |
1da177e4 | 1170 | |
af6d780b | 1171 | /* drop all unfinished transfers */ |
1da177e4 LT |
1172 | garmin_clear(garmin_data_p); |
1173 | ||
af6d780b | 1174 | switch (pktid) { |
1da177e4 LT |
1175 | |
1176 | case PRIV_PKTID_SET_DEBUG: | |
1177 | if (pktsiz != 4) | |
1178 | return -EINVPKT; | |
1179 | debug = __le32_to_cpu(privpkt[3]); | |
1180 | dbg("%s - debug level set to 0x%X", | |
441b62c1 | 1181 | __func__, debug); |
1da177e4 LT |
1182 | break; |
1183 | ||
1184 | case PRIV_PKTID_SET_MODE: | |
1185 | if (pktsiz != 4) | |
1186 | return -EINVPKT; | |
1187 | garmin_data_p->mode = __le32_to_cpu(privpkt[3]); | |
1188 | dbg("%s - mode set to %d", | |
441b62c1 | 1189 | __func__, garmin_data_p->mode); |
1da177e4 LT |
1190 | break; |
1191 | ||
1192 | case PRIV_PKTID_INFO_REQ: | |
1193 | priv_status_resp(port); | |
1194 | break; | |
1195 | ||
1196 | case PRIV_PKTID_RESET_REQ: | |
468d1362 | 1197 | atomic_inc(&garmin_data_p->req_count); |
1da177e4 LT |
1198 | break; |
1199 | ||
1200 | case PRIV_PKTID_SET_DEF_MODE: | |
1201 | if (pktsiz != 4) | |
1202 | return -EINVPKT; | |
1203 | initial_mode = __le32_to_cpu(privpkt[3]); | |
1204 | dbg("%s - initial_mode set to %d", | |
441b62c1 | 1205 | __func__, |
1da177e4 LT |
1206 | garmin_data_p->mode); |
1207 | break; | |
1208 | } | |
1209 | return count; | |
1210 | } | |
1211 | } | |
1212 | ||
eb6d8c2d HK |
1213 | garmin_data_p->ignorePkts = 0; |
1214 | ||
1da177e4 LT |
1215 | if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { |
1216 | return gsp_receive(garmin_data_p, buf, count); | |
1217 | } else { /* MODE_NATIVE */ | |
1218 | return nat_receive(garmin_data_p, buf, count); | |
1219 | } | |
1220 | } | |
1221 | ||
1222 | ||
95da310e | 1223 | static int garmin_write_room(struct tty_struct *tty) |
1da177e4 | 1224 | { |
95da310e | 1225 | struct usb_serial_port *port = tty->driver_data; |
1da177e4 LT |
1226 | /* |
1227 | * Report back the bytes currently available in the output buffer. | |
1228 | */ | |
af6d780b | 1229 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 LT |
1230 | return GPS_OUT_BUFSIZ-garmin_data_p->outsize; |
1231 | } | |
1232 | ||
1233 | ||
af6d780b | 1234 | static void garmin_read_process(struct garmin_data *garmin_data_p, |
1da177e4 LT |
1235 | unsigned char *data, unsigned data_length) |
1236 | { | |
1237 | if (garmin_data_p->flags & FLAGS_DROP_DATA) { | |
1238 | /* abort-transfer cmd is actice */ | |
441b62c1 | 1239 | dbg("%s - pkt dropped", __func__); |
1da177e4 | 1240 | } else if (garmin_data_p->state != STATE_DISCONNECTED && |
af6d780b | 1241 | garmin_data_p->state != STATE_RESET) { |
1da177e4 LT |
1242 | |
1243 | /* remember any appl.layer packets, so we know | |
1244 | if a reset is required or not when closing | |
1245 | the device */ | |
1246 | if (0 == memcmp(data, GARMIN_APP_LAYER_REPLY, | |
af6d780b | 1247 | sizeof(GARMIN_APP_LAYER_REPLY))) { |
468d1362 | 1248 | atomic_inc(&garmin_data_p->resp_count); |
eb6d8c2d | 1249 | } |
1da177e4 LT |
1250 | |
1251 | /* if throttling is active or postprecessing is required | |
eb6d8c2d | 1252 | put the received data in the input queue, otherwise |
1da177e4 LT |
1253 | send it directly to the tty port */ |
1254 | if (garmin_data_p->flags & FLAGS_QUEUING) { | |
1255 | pkt_add(garmin_data_p, data, data_length); | |
1256 | } else if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { | |
af6d780b | 1257 | if (getLayerId(data) == GARMIN_LAYERID_APPL) |
1da177e4 | 1258 | pkt_add(garmin_data_p, data, data_length); |
1da177e4 LT |
1259 | } else { |
1260 | send_to_tty(garmin_data_p->port, data, data_length); | |
1261 | } | |
1262 | } | |
1263 | } | |
1264 | ||
1265 | ||
af6d780b | 1266 | static void garmin_read_bulk_callback(struct urb *urb) |
1da177e4 | 1267 | { |
eb6d8c2d | 1268 | unsigned long flags; |
cdc97792 | 1269 | struct usb_serial_port *port = urb->context; |
1da177e4 | 1270 | struct usb_serial *serial = port->serial; |
af6d780b | 1271 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 | 1272 | unsigned char *data = urb->transfer_buffer; |
f9feb517 GKH |
1273 | int status = urb->status; |
1274 | int retval; | |
1da177e4 | 1275 | |
441b62c1 | 1276 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
1277 | |
1278 | if (!serial) { | |
441b62c1 | 1279 | dbg("%s - bad serial pointer, exiting", __func__); |
1da177e4 LT |
1280 | return; |
1281 | } | |
1282 | ||
f9feb517 | 1283 | if (status) { |
1da177e4 | 1284 | dbg("%s - nonzero read bulk status received: %d", |
441b62c1 | 1285 | __func__, status); |
1da177e4 LT |
1286 | return; |
1287 | } | |
1288 | ||
af6d780b | 1289 | usb_serial_debug_data(debug, &port->dev, |
441b62c1 | 1290 | __func__, urb->actual_length, data); |
1da177e4 LT |
1291 | |
1292 | garmin_read_process(garmin_data_p, data, urb->actual_length); | |
1293 | ||
eb6d8c2d HK |
1294 | if (urb->actual_length == 0 && |
1295 | 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) { | |
1296 | spin_lock_irqsave(&garmin_data_p->lock, flags); | |
1297 | garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART; | |
1298 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
f9feb517 GKH |
1299 | retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
1300 | if (retval) | |
1da177e4 LT |
1301 | dev_err(&port->dev, |
1302 | "%s - failed resubmitting read urb, error %d\n", | |
441b62c1 | 1303 | __func__, retval); |
eb6d8c2d HK |
1304 | } else if (urb->actual_length > 0) { |
1305 | /* Continue trying to read until nothing more is received */ | |
1306 | if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) { | |
f9feb517 GKH |
1307 | retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
1308 | if (retval) | |
eb6d8c2d | 1309 | dev_err(&port->dev, |
f9feb517 | 1310 | "%s - failed resubmitting read urb, " |
441b62c1 | 1311 | "error %d\n", __func__, retval); |
eb6d8c2d HK |
1312 | } |
1313 | } else { | |
441b62c1 | 1314 | dbg("%s - end of bulk data", __func__); |
eb6d8c2d HK |
1315 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1316 | garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE; | |
1317 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
1da177e4 LT |
1318 | } |
1319 | return; | |
1320 | } | |
1321 | ||
1322 | ||
af6d780b | 1323 | static void garmin_read_int_callback(struct urb *urb) |
1da177e4 | 1324 | { |
eb6d8c2d | 1325 | unsigned long flags; |
f9feb517 | 1326 | int retval; |
cdc97792 | 1327 | struct usb_serial_port *port = urb->context; |
1da177e4 | 1328 | struct usb_serial *serial = port->serial; |
af6d780b | 1329 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 | 1330 | unsigned char *data = urb->transfer_buffer; |
f9feb517 | 1331 | int status = urb->status; |
1da177e4 | 1332 | |
f9feb517 | 1333 | switch (status) { |
1da177e4 LT |
1334 | case 0: |
1335 | /* success */ | |
1336 | break; | |
1337 | case -ECONNRESET: | |
1338 | case -ENOENT: | |
1339 | case -ESHUTDOWN: | |
1340 | /* this urb is terminated, clean up */ | |
1341 | dbg("%s - urb shutting down with status: %d", | |
441b62c1 | 1342 | __func__, status); |
1da177e4 LT |
1343 | return; |
1344 | default: | |
1345 | dbg("%s - nonzero urb status received: %d", | |
441b62c1 | 1346 | __func__, status); |
1da177e4 LT |
1347 | return; |
1348 | } | |
1349 | ||
441b62c1 | 1350 | usb_serial_debug_data(debug, &port->dev, __func__, |
1da177e4 LT |
1351 | urb->actual_length, urb->transfer_buffer); |
1352 | ||
1353 | if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) && | |
1354 | 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY, | |
af6d780b | 1355 | sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) { |
1da177e4 | 1356 | |
441b62c1 | 1357 | dbg("%s - bulk data available.", __func__); |
1da177e4 | 1358 | |
eb6d8c2d HK |
1359 | if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) { |
1360 | ||
1361 | /* bulk data available */ | |
af6d780b AC |
1362 | usb_fill_bulk_urb(port->read_urb, serial->dev, |
1363 | usb_rcvbulkpipe(serial->dev, | |
1364 | port->bulk_in_endpointAddress), | |
eb6d8c2d HK |
1365 | port->read_urb->transfer_buffer, |
1366 | port->read_urb->transfer_buffer_length, | |
1367 | garmin_read_bulk_callback, port); | |
f9feb517 GKH |
1368 | retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
1369 | if (retval) { | |
eb6d8c2d | 1370 | dev_err(&port->dev, |
af6d780b AC |
1371 | "%s - failed submitting read urb, error %d\n", |
1372 | __func__, retval); | |
eb6d8c2d HK |
1373 | } else { |
1374 | spin_lock_irqsave(&garmin_data_p->lock, flags); | |
1375 | garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE; | |
1376 | /* do not send this packet to the user */ | |
1377 | garmin_data_p->ignorePkts = 1; | |
af6d780b AC |
1378 | spin_unlock_irqrestore(&garmin_data_p->lock, |
1379 | flags); | |
eb6d8c2d HK |
1380 | } |
1381 | } else { | |
1382 | /* bulk-in transfer still active */ | |
1383 | spin_lock_irqsave(&garmin_data_p->lock, flags); | |
1384 | garmin_data_p->flags |= FLAGS_BULK_IN_RESTART; | |
1385 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); | |
1da177e4 LT |
1386 | } |
1387 | ||
1388 | } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY)) | |
1389 | && 0 == memcmp(data, GARMIN_START_SESSION_REPLY, | |
af6d780b | 1390 | sizeof(GARMIN_START_SESSION_REPLY))) { |
1da177e4 | 1391 | |
eb6d8c2d | 1392 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 1393 | garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN; |
eb6d8c2d | 1394 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
1395 | |
1396 | /* save the serial number */ | |
af6d780b AC |
1397 | garmin_data_p->serial_num = __le32_to_cpup( |
1398 | (__le32 *)(data+GARMIN_PKTHDR_LENGTH)); | |
1da177e4 LT |
1399 | |
1400 | dbg("%s - start-of-session reply seen - serial %u.", | |
441b62c1 | 1401 | __func__, garmin_data_p->serial_num); |
1da177e4 LT |
1402 | } |
1403 | ||
1404 | if (garmin_data_p->ignorePkts) { | |
1405 | /* this reply belongs to a request generated by the driver, | |
1406 | ignore it. */ | |
1407 | dbg("%s - pkt ignored (%d)", | |
441b62c1 | 1408 | __func__, garmin_data_p->ignorePkts); |
eb6d8c2d | 1409 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 1410 | garmin_data_p->ignorePkts--; |
eb6d8c2d | 1411 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
1412 | } else { |
1413 | garmin_read_process(garmin_data_p, data, urb->actual_length); | |
1414 | } | |
1415 | ||
1416 | port->interrupt_in_urb->dev = port->serial->dev; | |
af6d780b | 1417 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
f9feb517 | 1418 | if (retval) |
1da177e4 LT |
1419 | dev_err(&urb->dev->dev, |
1420 | "%s - Error %d submitting interrupt urb\n", | |
441b62c1 | 1421 | __func__, retval); |
1da177e4 LT |
1422 | } |
1423 | ||
1424 | ||
1425 | /* | |
1426 | * Sends the next queued packt to the tty port (garmin native mode only) | |
1427 | * and then sets a timer to call itself again until all queued data | |
1428 | * is sent. | |
1429 | */ | |
af6d780b | 1430 | static int garmin_flush_queue(struct garmin_data *garmin_data_p) |
1da177e4 | 1431 | { |
eb6d8c2d | 1432 | unsigned long flags; |
1da177e4 LT |
1433 | struct garmin_packet *pkt; |
1434 | ||
1435 | if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) { | |
1436 | pkt = pkt_pop(garmin_data_p); | |
1437 | if (pkt != NULL) { | |
1da177e4 LT |
1438 | send_to_tty(garmin_data_p->port, pkt->data, pkt->size); |
1439 | kfree(pkt); | |
1440 | mod_timer(&garmin_data_p->timer, (1)+jiffies); | |
1441 | ||
1442 | } else { | |
eb6d8c2d | 1443 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 1444 | garmin_data_p->flags &= ~FLAGS_QUEUING; |
eb6d8c2d | 1445 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
1446 | } |
1447 | } | |
1448 | return 0; | |
1449 | } | |
1450 | ||
1451 | ||
95da310e | 1452 | static void garmin_throttle(struct tty_struct *tty) |
1da177e4 | 1453 | { |
95da310e | 1454 | struct usb_serial_port *port = tty->driver_data; |
af6d780b | 1455 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
95da310e | 1456 | unsigned long flags; |
1da177e4 | 1457 | |
441b62c1 | 1458 | dbg("%s - port %d", __func__, port->number); |
1da177e4 LT |
1459 | /* set flag, data received will be put into a queue |
1460 | for later processing */ | |
eb6d8c2d | 1461 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 1462 | garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED; |
eb6d8c2d | 1463 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
1464 | } |
1465 | ||
1466 | ||
af6d780b | 1467 | static void garmin_unthrottle(struct tty_struct *tty) |
1da177e4 | 1468 | { |
95da310e | 1469 | struct usb_serial_port *port = tty->driver_data; |
af6d780b | 1470 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
95da310e | 1471 | unsigned long flags; |
eb6d8c2d | 1472 | int status; |
1da177e4 | 1473 | |
441b62c1 | 1474 | dbg("%s - port %d", __func__, port->number); |
eb6d8c2d | 1475 | spin_lock_irqsave(&garmin_data_p->lock, flags); |
1da177e4 | 1476 | garmin_data_p->flags &= ~FLAGS_THROTTLED; |
eb6d8c2d | 1477 | spin_unlock_irqrestore(&garmin_data_p->lock, flags); |
1da177e4 LT |
1478 | |
1479 | /* in native mode send queued data to tty, in | |
1480 | serial mode nothing needs to be done here */ | |
1481 | if (garmin_data_p->mode == MODE_NATIVE) | |
1482 | garmin_flush_queue(garmin_data_p); | |
eb6d8c2d HK |
1483 | |
1484 | if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) { | |
1485 | status = usb_submit_urb(port->read_urb, GFP_ATOMIC); | |
1486 | if (status) | |
1487 | dev_err(&port->dev, | |
1488 | "%s - failed resubmitting read urb, error %d\n", | |
441b62c1 | 1489 | __func__, status); |
eb6d8c2d | 1490 | } |
1da177e4 LT |
1491 | } |
1492 | ||
1da177e4 LT |
1493 | /* |
1494 | * The timer is currently only used to send queued packets to | |
1495 | * the tty in cases where the protocol provides no own handshaking | |
1496 | * to initiate the transfer. | |
1497 | */ | |
1498 | static void timeout_handler(unsigned long data) | |
1499 | { | |
1500 | struct garmin_data *garmin_data_p = (struct garmin_data *) data; | |
1501 | ||
1502 | /* send the next queued packet to the tty port */ | |
1503 | if (garmin_data_p->mode == MODE_NATIVE) | |
1504 | if (garmin_data_p->flags & FLAGS_QUEUING) | |
1505 | garmin_flush_queue(garmin_data_p); | |
1506 | } | |
1507 | ||
1508 | ||
1509 | ||
95da310e | 1510 | static int garmin_attach(struct usb_serial *serial) |
1da177e4 LT |
1511 | { |
1512 | int status = 0; | |
1513 | struct usb_serial_port *port = serial->port[0]; | |
af6d780b | 1514 | struct garmin_data *garmin_data_p = NULL; |
1da177e4 | 1515 | |
441b62c1 | 1516 | dbg("%s", __func__); |
1da177e4 | 1517 | |
7ac9da10 | 1518 | garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL); |
1da177e4 | 1519 | if (garmin_data_p == NULL) { |
441b62c1 | 1520 | dev_err(&port->dev, "%s - Out of memory\n", __func__); |
1da177e4 LT |
1521 | return -ENOMEM; |
1522 | } | |
1da177e4 LT |
1523 | init_timer(&garmin_data_p->timer); |
1524 | spin_lock_init(&garmin_data_p->lock); | |
1525 | INIT_LIST_HEAD(&garmin_data_p->pktlist); | |
af6d780b | 1526 | /* garmin_data_p->timer.expires = jiffies + session_timeout; */ |
1da177e4 LT |
1527 | garmin_data_p->timer.data = (unsigned long)garmin_data_p; |
1528 | garmin_data_p->timer.function = timeout_handler; | |
1529 | garmin_data_p->port = port; | |
1530 | garmin_data_p->state = 0; | |
1531 | garmin_data_p->count = 0; | |
1532 | usb_set_serial_port_data(port, garmin_data_p); | |
1533 | ||
1534 | status = garmin_init_session(port); | |
1535 | ||
1536 | return status; | |
1537 | } | |
1538 | ||
1539 | ||
95da310e | 1540 | static void garmin_shutdown(struct usb_serial *serial) |
1da177e4 LT |
1541 | { |
1542 | struct usb_serial_port *port = serial->port[0]; | |
af6d780b | 1543 | struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); |
1da177e4 | 1544 | |
441b62c1 | 1545 | dbg("%s", __func__); |
1da177e4 | 1546 | |
af6d780b | 1547 | usb_kill_urb(port->interrupt_in_urb); |
1da177e4 | 1548 | del_timer_sync(&garmin_data_p->timer); |
af6d780b | 1549 | kfree(garmin_data_p); |
1da177e4 LT |
1550 | usb_set_serial_port_data(port, NULL); |
1551 | } | |
1552 | ||
1553 | ||
1da177e4 | 1554 | /* All of the device info needed */ |
ea65370d | 1555 | static struct usb_serial_driver garmin_device = { |
18fcac35 | 1556 | .driver = { |
eb6d8c2d HK |
1557 | .owner = THIS_MODULE, |
1558 | .name = "garmin_gps", | |
18fcac35 | 1559 | }, |
eb6d8c2d | 1560 | .description = "Garmin GPS usb/tty", |
d9b1b787 | 1561 | .usb_driver = &garmin_driver, |
1da177e4 | 1562 | .id_table = id_table, |
1da177e4 LT |
1563 | .num_ports = 1, |
1564 | .open = garmin_open, | |
1565 | .close = garmin_close, | |
1566 | .throttle = garmin_throttle, | |
1567 | .unthrottle = garmin_unthrottle, | |
1568 | .attach = garmin_attach, | |
1569 | .shutdown = garmin_shutdown, | |
1570 | .write = garmin_write, | |
1571 | .write_room = garmin_write_room, | |
1da177e4 LT |
1572 | .write_bulk_callback = garmin_write_bulk_callback, |
1573 | .read_bulk_callback = garmin_read_bulk_callback, | |
1574 | .read_int_callback = garmin_read_int_callback, | |
1575 | }; | |
1576 | ||
1577 | ||
eb6d8c2d | 1578 | |
af6d780b | 1579 | static int __init garmin_init(void) |
1da177e4 LT |
1580 | { |
1581 | int retval; | |
1582 | ||
1583 | retval = usb_serial_register(&garmin_device); | |
1584 | if (retval) | |
1585 | goto failed_garmin_register; | |
1586 | retval = usb_register(&garmin_driver); | |
1587 | if (retval) | |
1588 | goto failed_usb_register; | |
1589 | info(DRIVER_DESC " " DRIVER_VERSION); | |
1590 | ||
1591 | return 0; | |
1592 | failed_usb_register: | |
1593 | usb_serial_deregister(&garmin_device); | |
1594 | failed_garmin_register: | |
1595 | return retval; | |
1596 | } | |
1597 | ||
1598 | ||
af6d780b | 1599 | static void __exit garmin_exit(void) |
1da177e4 | 1600 | { |
af6d780b AC |
1601 | usb_deregister(&garmin_driver); |
1602 | usb_serial_deregister(&garmin_device); | |
1da177e4 LT |
1603 | } |
1604 | ||
1605 | ||
1606 | ||
1607 | ||
1608 | module_init(garmin_init); | |
1609 | module_exit(garmin_exit); | |
1610 | ||
af6d780b AC |
1611 | MODULE_AUTHOR(DRIVER_AUTHOR); |
1612 | MODULE_DESCRIPTION(DRIVER_DESC); | |
1da177e4 LT |
1613 | MODULE_LICENSE("GPL"); |
1614 | ||
1615 | module_param(debug, bool, S_IWUSR | S_IRUGO); | |
1616 | MODULE_PARM_DESC(debug, "Debug enabled or not"); | |
1617 | module_param(initial_mode, int, S_IRUGO); | |
1618 | MODULE_PARM_DESC(initial_mode, "Initial mode"); | |
1619 |