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