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