1 /* 2 * Garmin GPS driver 3 * 4 * Copyright (C) 2006-2011 Hermann Kneissel herkne@gmx.de 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 <linux/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 bool debug; 46 47 #define GARMIN_VENDOR_ID 0x091E 48 49 /* 50 * Version Information 51 */ 52 53 #define VERSION_MAJOR 0 54 #define VERSION_MINOR 36 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 count; 133 __u8 pkt_id; 134 __u32 serial_num; 135 struct timer_list timer; 136 struct usb_serial_port *port; 137 int seq_counter; 138 int insize; 139 int outsize; 140 __u8 inbuffer [GPS_IN_BUFSIZ]; /* tty -> usb */ 141 __u8 outbuffer[GPS_OUT_BUFSIZ]; /* usb -> tty */ 142 __u8 privpkt[4*6]; 143 spinlock_t lock; 144 struct list_head pktlist; 145 }; 146 147 148 #define STATE_NEW 0 149 #define STATE_INITIAL_DELAY 1 150 #define STATE_TIMEOUT 2 151 #define STATE_SESSION_REQ1 3 152 #define STATE_SESSION_REQ2 4 153 #define STATE_ACTIVE 5 154 155 #define STATE_RESET 8 156 #define STATE_DISCONNECTED 9 157 #define STATE_WAIT_TTY_ACK 10 158 #define STATE_GSP_WAIT_DATA 11 159 160 #define MODE_NATIVE 0 161 #define MODE_GARMIN_SERIAL 1 162 163 /* Flags used in garmin_data.flags: */ 164 #define FLAGS_SESSION_REPLY_MASK 0x00C0 165 #define FLAGS_SESSION_REPLY1_SEEN 0x0080 166 #define FLAGS_SESSION_REPLY2_SEEN 0x0040 167 #define FLAGS_BULK_IN_ACTIVE 0x0020 168 #define FLAGS_BULK_IN_RESTART 0x0010 169 #define FLAGS_THROTTLED 0x0008 170 #define APP_REQ_SEEN 0x0004 171 #define APP_RESP_SEEN 0x0002 172 #define CLEAR_HALT_REQUIRED 0x0001 173 174 #define FLAGS_QUEUING 0x0100 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 int 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 int dismiss_ack); 190 191 /* some special packets to be send or received */ 192 static unsigned char const GARMIN_START_SESSION_REQ[] 193 = { 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 }; 194 static unsigned char const GARMIN_START_SESSION_REPLY[] 195 = { 0, 0, 0, 0, 6, 0, 0, 0, 4, 0, 0, 0 }; 196 static unsigned char const GARMIN_BULK_IN_AVAIL_REPLY[] 197 = { 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }; 198 static unsigned char const GARMIN_APP_LAYER_REPLY[] 199 = { 0x14, 0, 0, 0 }; 200 static unsigned char const GARMIN_START_PVT_REQ[] 201 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 49, 0 }; 202 static unsigned char const GARMIN_STOP_PVT_REQ[] 203 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 50, 0 }; 204 static unsigned char const GARMIN_STOP_TRANSFER_REQ[] 205 = { 20, 0, 0, 0, 10, 0, 0, 0, 2, 0, 0, 0, 0, 0 }; 206 static unsigned char const GARMIN_STOP_TRANSFER_REQ_V2[] 207 = { 20, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 0 }; 208 static unsigned char const PRIVATE_REQ[] 209 = { 0x4B, 0x6E, 0x10, 0x01, 0xFF, 0, 0, 0, 0xFF, 0, 0, 0 }; 210 211 212 213 static const struct usb_device_id id_table[] = { 214 /* the same device id seems to be used by all 215 usb enabled GPS devices */ 216 { USB_DEVICE(GARMIN_VENDOR_ID, 3) }, 217 { } /* Terminating entry */ 218 }; 219 220 MODULE_DEVICE_TABLE(usb, id_table); 221 222 static struct usb_driver garmin_driver = { 223 .name = "garmin_gps", 224 .disconnect = usb_serial_disconnect, 225 .id_table = id_table, 226 }; 227 228 229 static inline int getLayerId(const __u8 *usbPacket) 230 { 231 return __le32_to_cpup((__le32 *)(usbPacket)); 232 } 233 234 static inline int getPacketId(const __u8 *usbPacket) 235 { 236 return __le32_to_cpup((__le32 *)(usbPacket+4)); 237 } 238 239 static inline int getDataLength(const __u8 *usbPacket) 240 { 241 return __le32_to_cpup((__le32 *)(usbPacket+8)); 242 } 243 244 245 /* 246 * check if the usb-packet in buf contains an abort-transfer command. 247 * (if yes, all queued data will be dropped) 248 */ 249 static inline int isAbortTrfCmnd(const unsigned char *buf) 250 { 251 if (0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ, 252 sizeof(GARMIN_STOP_TRANSFER_REQ)) || 253 0 == memcmp(buf, GARMIN_STOP_TRANSFER_REQ_V2, 254 sizeof(GARMIN_STOP_TRANSFER_REQ_V2))) 255 return 1; 256 else 257 return 0; 258 } 259 260 261 262 static void send_to_tty(struct usb_serial_port *port, 263 char *data, unsigned int actual_length) 264 { 265 struct tty_struct *tty = tty_port_tty_get(&port->port); 266 267 if (tty && actual_length) { 268 269 usb_serial_debug_data(debug, &port->dev, 270 __func__, actual_length, data); 271 272 tty_insert_flip_string(tty, data, actual_length); 273 tty_flip_buffer_push(tty); 274 } 275 tty_kref_put(tty); 276 } 277 278 279 /****************************************************************************** 280 * packet queue handling 281 ******************************************************************************/ 282 283 /* 284 * queue a received (usb-)packet for later processing 285 */ 286 static int pkt_add(struct garmin_data *garmin_data_p, 287 unsigned char *data, unsigned int data_length) 288 { 289 int state = 0; 290 int result = 0; 291 unsigned long flags; 292 struct garmin_packet *pkt; 293 294 /* process only packets containg data ... */ 295 if (data_length) { 296 pkt = kmalloc(sizeof(struct garmin_packet)+data_length, 297 GFP_ATOMIC); 298 if (pkt == NULL) { 299 dev_err(&garmin_data_p->port->dev, "out of memory\n"); 300 return 0; 301 } 302 pkt->size = data_length; 303 memcpy(pkt->data, data, data_length); 304 305 spin_lock_irqsave(&garmin_data_p->lock, flags); 306 garmin_data_p->flags |= FLAGS_QUEUING; 307 result = list_empty(&garmin_data_p->pktlist); 308 pkt->seq = garmin_data_p->seq_counter++; 309 list_add_tail(&pkt->list, &garmin_data_p->pktlist); 310 state = garmin_data_p->state; 311 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 312 313 dbg("%s - added: pkt: %d - %d bytes", 314 __func__, pkt->seq, data_length); 315 316 /* in serial mode, if someone is waiting for data from 317 the device, convert and send the next packet to tty. */ 318 if (result && (state == STATE_GSP_WAIT_DATA)) 319 gsp_next_packet(garmin_data_p); 320 } 321 return result; 322 } 323 324 325 /* get the next pending packet */ 326 static struct garmin_packet *pkt_pop(struct garmin_data *garmin_data_p) 327 { 328 unsigned long flags; 329 struct garmin_packet *result = NULL; 330 331 spin_lock_irqsave(&garmin_data_p->lock, flags); 332 if (!list_empty(&garmin_data_p->pktlist)) { 333 result = (struct garmin_packet *)garmin_data_p->pktlist.next; 334 list_del(&result->list); 335 } 336 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 337 return result; 338 } 339 340 341 /* free up all queued data */ 342 static void pkt_clear(struct garmin_data *garmin_data_p) 343 { 344 unsigned long flags; 345 struct garmin_packet *result = NULL; 346 347 spin_lock_irqsave(&garmin_data_p->lock, flags); 348 while (!list_empty(&garmin_data_p->pktlist)) { 349 result = (struct garmin_packet *)garmin_data_p->pktlist.next; 350 list_del(&result->list); 351 kfree(result); 352 } 353 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 354 } 355 356 357 /****************************************************************************** 358 * garmin serial protocol handling handling 359 ******************************************************************************/ 360 361 /* send an ack packet back to the tty */ 362 static int gsp_send_ack(struct garmin_data *garmin_data_p, __u8 pkt_id) 363 { 364 __u8 pkt[10]; 365 __u8 cksum = 0; 366 __u8 *ptr = pkt; 367 unsigned l = 0; 368 369 dbg("%s - pkt-id: 0x%X.", __func__, 0xFF & pkt_id); 370 371 *ptr++ = DLE; 372 *ptr++ = ACK; 373 cksum += ACK; 374 375 *ptr++ = 2; 376 cksum += 2; 377 378 *ptr++ = pkt_id; 379 cksum += pkt_id; 380 381 if (pkt_id == DLE) 382 *ptr++ = DLE; 383 384 *ptr++ = 0; 385 *ptr++ = 0xFF & (-cksum); 386 *ptr++ = DLE; 387 *ptr++ = ETX; 388 389 l = ptr-pkt; 390 391 send_to_tty(garmin_data_p->port, pkt, l); 392 return 0; 393 } 394 395 396 397 /* 398 * called for a complete packet received from tty layer 399 * 400 * the complete packet (pktid ... cksum) is in garmin_data_p->inbuf starting 401 * at GSP_INITIAL_OFFSET. 402 * 403 * count - number of bytes in the input buffer including space reserved for 404 * the usb header: GSP_INITIAL_OFFSET + number of bytes in packet 405 * (including pkt-id, data-length a. cksum) 406 */ 407 static int gsp_rec_packet(struct garmin_data *garmin_data_p, int count) 408 { 409 unsigned long flags; 410 const __u8 *recpkt = garmin_data_p->inbuffer+GSP_INITIAL_OFFSET; 411 __le32 *usbdata = (__le32 *) garmin_data_p->inbuffer; 412 413 int cksum = 0; 414 int n = 0; 415 int pktid = recpkt[0]; 416 int size = recpkt[1]; 417 418 usb_serial_debug_data(debug, &garmin_data_p->port->dev, 419 __func__, count-GSP_INITIAL_OFFSET, recpkt); 420 421 if (size != (count-GSP_INITIAL_OFFSET-3)) { 422 dbg("%s - invalid size, expected %d bytes, got %d", 423 __func__, size, (count-GSP_INITIAL_OFFSET-3)); 424 return -EINVPKT; 425 } 426 427 cksum += *recpkt++; 428 cksum += *recpkt++; 429 430 /* sanity check, remove after test ... */ 431 if ((__u8 *)&(usbdata[3]) != recpkt) { 432 dbg("%s - ptr mismatch %p - %p", 433 __func__, &(usbdata[4]), recpkt); 434 return -EINVPKT; 435 } 436 437 while (n < size) { 438 cksum += *recpkt++; 439 n++; 440 } 441 442 if ((0xff & (cksum + *recpkt)) != 0) { 443 dbg("%s - invalid checksum, expected %02x, got %02x", 444 __func__, 0xff & -cksum, 0xff & *recpkt); 445 return -EINVPKT; 446 } 447 448 usbdata[0] = __cpu_to_le32(GARMIN_LAYERID_APPL); 449 usbdata[1] = __cpu_to_le32(pktid); 450 usbdata[2] = __cpu_to_le32(size); 451 452 garmin_write_bulk(garmin_data_p->port, garmin_data_p->inbuffer, 453 GARMIN_PKTHDR_LENGTH+size, 0); 454 455 /* if this was an abort-transfer command, flush all 456 queued data. */ 457 if (isAbortTrfCmnd(garmin_data_p->inbuffer)) { 458 spin_lock_irqsave(&garmin_data_p->lock, flags); 459 garmin_data_p->flags |= FLAGS_DROP_DATA; 460 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 461 pkt_clear(garmin_data_p); 462 } 463 464 return count; 465 } 466 467 468 469 /* 470 * Called for data received from tty 471 * 472 * buf contains the data read, it may span more than one packet or even 473 * incomplete packets 474 * 475 * input record should be a serial-record, but it may not be complete. 476 * Copy it into our local buffer, until an etx is seen (or an error 477 * occurs). 478 * Once the record is complete, convert into a usb packet and send it 479 * to the bulk pipe, send an ack back to the tty. 480 * 481 * If the input is an ack, just send the last queued packet to the 482 * tty layer. 483 * 484 * if the input is an abort command, drop all queued data. 485 */ 486 487 static int gsp_receive(struct garmin_data *garmin_data_p, 488 const unsigned char *buf, int count) 489 { 490 unsigned long flags; 491 int offs = 0; 492 int ack_or_nak_seen = 0; 493 __u8 *dest; 494 int size; 495 /* dleSeen: set if last byte read was a DLE */ 496 int dleSeen; 497 /* skip: if set, skip incoming data until possible start of 498 * new packet 499 */ 500 int skip; 501 __u8 data; 502 503 spin_lock_irqsave(&garmin_data_p->lock, flags); 504 dest = garmin_data_p->inbuffer; 505 size = garmin_data_p->insize; 506 dleSeen = garmin_data_p->flags & FLAGS_GSP_DLESEEN; 507 skip = garmin_data_p->flags & FLAGS_GSP_SKIP; 508 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 509 510 /* dbg("%s - dle=%d skip=%d size=%d count=%d", 511 __func__, dleSeen, skip, size, count); */ 512 513 if (size == 0) 514 size = GSP_INITIAL_OFFSET; 515 516 while (offs < count) { 517 518 data = *(buf+offs); 519 offs++; 520 521 if (data == DLE) { 522 if (skip) { /* start of a new pkt */ 523 skip = 0; 524 size = GSP_INITIAL_OFFSET; 525 dleSeen = 1; 526 } else if (dleSeen) { 527 dest[size++] = data; 528 dleSeen = 0; 529 } else { 530 dleSeen = 1; 531 } 532 } else if (data == ETX) { 533 if (dleSeen) { 534 /* packet complete */ 535 536 data = dest[GSP_INITIAL_OFFSET]; 537 538 if (data == ACK) { 539 ack_or_nak_seen = ACK; 540 dbg("ACK packet complete."); 541 } else if (data == NAK) { 542 ack_or_nak_seen = NAK; 543 dbg("NAK packet complete."); 544 } else { 545 dbg("packet complete - id=0x%X.", 546 0xFF & data); 547 gsp_rec_packet(garmin_data_p, size); 548 } 549 550 skip = 1; 551 size = GSP_INITIAL_OFFSET; 552 dleSeen = 0; 553 } else { 554 dest[size++] = data; 555 } 556 } else if (!skip) { 557 558 if (dleSeen) { 559 size = GSP_INITIAL_OFFSET; 560 dleSeen = 0; 561 } 562 563 dest[size++] = data; 564 } 565 566 if (size >= GPS_IN_BUFSIZ) { 567 dbg("%s - packet too large.", __func__); 568 skip = 1; 569 size = GSP_INITIAL_OFFSET; 570 dleSeen = 0; 571 } 572 } 573 574 spin_lock_irqsave(&garmin_data_p->lock, flags); 575 576 garmin_data_p->insize = size; 577 578 /* copy flags back to structure */ 579 if (skip) 580 garmin_data_p->flags |= FLAGS_GSP_SKIP; 581 else 582 garmin_data_p->flags &= ~FLAGS_GSP_SKIP; 583 584 if (dleSeen) 585 garmin_data_p->flags |= FLAGS_GSP_DLESEEN; 586 else 587 garmin_data_p->flags &= ~FLAGS_GSP_DLESEEN; 588 589 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 590 591 if (ack_or_nak_seen) { 592 if (gsp_next_packet(garmin_data_p) > 0) 593 garmin_data_p->state = STATE_ACTIVE; 594 else 595 garmin_data_p->state = STATE_GSP_WAIT_DATA; 596 } 597 return count; 598 } 599 600 601 602 /* 603 * Sends a usb packet to the tty 604 * 605 * Assumes, that all packages and at an usb-packet boundary. 606 * 607 * return <0 on error, 0 if packet is incomplete or > 0 if packet was sent 608 */ 609 static int gsp_send(struct garmin_data *garmin_data_p, 610 const unsigned char *buf, int count) 611 { 612 const unsigned char *src; 613 unsigned char *dst; 614 int pktid = 0; 615 int datalen = 0; 616 int cksum = 0; 617 int i = 0; 618 int k; 619 620 dbg("%s - state %d - %d bytes.", __func__, 621 garmin_data_p->state, count); 622 623 k = garmin_data_p->outsize; 624 if ((k+count) > GPS_OUT_BUFSIZ) { 625 dbg("packet too large"); 626 garmin_data_p->outsize = 0; 627 return -4; 628 } 629 630 memcpy(garmin_data_p->outbuffer+k, buf, count); 631 k += count; 632 garmin_data_p->outsize = k; 633 634 if (k >= GARMIN_PKTHDR_LENGTH) { 635 pktid = getPacketId(garmin_data_p->outbuffer); 636 datalen = getDataLength(garmin_data_p->outbuffer); 637 i = GARMIN_PKTHDR_LENGTH + datalen; 638 if (k < i) 639 return 0; 640 } else { 641 return 0; 642 } 643 644 dbg("%s - %d bytes in buffer, %d bytes in pkt.", __func__, 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 __func__, 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 * Process the next pending data packet - if there is one 725 */ 726 static int gsp_next_packet(struct garmin_data *garmin_data_p) 727 { 728 int result = 0; 729 struct garmin_packet *pkt = NULL; 730 731 while ((pkt = pkt_pop(garmin_data_p)) != NULL) { 732 dbg("%s - next pkt: %d", __func__, pkt->seq); 733 result = gsp_send(garmin_data_p, pkt->data, pkt->size); 734 if (result > 0) { 735 kfree(pkt); 736 return result; 737 } 738 kfree(pkt); 739 } 740 return result; 741 } 742 743 744 745 /****************************************************************************** 746 * garmin native mode 747 ******************************************************************************/ 748 749 750 /* 751 * Called for data received from tty 752 * 753 * The input data is expected to be in garmin usb-packet format. 754 * 755 * buf contains the data read, it may span more than one packet 756 * or even incomplete packets 757 */ 758 static int nat_receive(struct garmin_data *garmin_data_p, 759 const unsigned char *buf, int count) 760 { 761 unsigned long flags; 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 /* seems to be an invalid packet, ignore rest 777 of input */ 778 dbg("%s - packet size too large: %d", __func__, 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, 0); 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 spin_lock_irqsave(&garmin_data_p->lock, 809 flags); 810 garmin_data_p->flags |= FLAGS_DROP_DATA; 811 spin_unlock_irqrestore( 812 &garmin_data_p->lock, flags); 813 pkt_clear(garmin_data_p); 814 } 815 } 816 } 817 } 818 return result; 819 } 820 821 822 /****************************************************************************** 823 * private packets 824 ******************************************************************************/ 825 826 static void priv_status_resp(struct usb_serial_port *port) 827 { 828 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 829 __le32 *pkt = (__le32 *)garmin_data_p->privpkt; 830 831 pkt[0] = __cpu_to_le32(GARMIN_LAYERID_PRIVATE); 832 pkt[1] = __cpu_to_le32(PRIV_PKTID_INFO_RESP); 833 pkt[2] = __cpu_to_le32(12); 834 pkt[3] = __cpu_to_le32(VERSION_MAJOR << 16 | VERSION_MINOR); 835 pkt[4] = __cpu_to_le32(garmin_data_p->mode); 836 pkt[5] = __cpu_to_le32(garmin_data_p->serial_num); 837 838 send_to_tty(port, (__u8 *)pkt, 6 * 4); 839 } 840 841 842 /****************************************************************************** 843 * Garmin specific driver functions 844 ******************************************************************************/ 845 846 static int process_resetdev_request(struct usb_serial_port *port) 847 { 848 unsigned long flags; 849 int status; 850 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 851 852 spin_lock_irqsave(&garmin_data_p->lock, flags); 853 garmin_data_p->flags &= ~(CLEAR_HALT_REQUIRED); 854 garmin_data_p->state = STATE_RESET; 855 garmin_data_p->serial_num = 0; 856 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 857 858 usb_kill_urb(port->interrupt_in_urb); 859 dbg("%s - usb_reset_device", __func__); 860 status = usb_reset_device(port->serial->dev); 861 if (status) 862 dbg("%s - usb_reset_device failed: %d", 863 __func__, status); 864 return status; 865 } 866 867 868 869 /* 870 * clear all cached data 871 */ 872 static int garmin_clear(struct garmin_data *garmin_data_p) 873 { 874 unsigned long flags; 875 int status = 0; 876 877 /* flush all queued data */ 878 pkt_clear(garmin_data_p); 879 880 spin_lock_irqsave(&garmin_data_p->lock, flags); 881 garmin_data_p->insize = 0; 882 garmin_data_p->outsize = 0; 883 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 884 885 return status; 886 } 887 888 889 static int garmin_init_session(struct usb_serial_port *port) 890 { 891 struct usb_serial *serial = port->serial; 892 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 893 int status = 0; 894 int i = 0; 895 896 if (status == 0) { 897 usb_kill_urb(port->interrupt_in_urb); 898 899 dbg("%s - adding interrupt input", __func__); 900 status = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 901 if (status) 902 dev_err(&serial->dev->dev, 903 "%s - failed submitting interrupt urb, error %d\n", 904 __func__, status); 905 } 906 907 /* 908 * using the initialization method from gpsbabel. See comments in 909 * gpsbabel/jeeps/gpslibusb.c gusb_reset_toggles() 910 */ 911 if (status == 0) { 912 dbg("%s - starting session ...", __func__); 913 garmin_data_p->state = STATE_ACTIVE; 914 915 for (i = 0; i < 3; i++) { 916 status = garmin_write_bulk(port, 917 GARMIN_START_SESSION_REQ, 918 sizeof(GARMIN_START_SESSION_REQ), 0); 919 920 if (status < 0) 921 break; 922 } 923 924 if (status > 0) 925 status = 0; 926 } 927 928 return status; 929 } 930 931 932 933 static int garmin_open(struct tty_struct *tty, struct usb_serial_port *port) 934 { 935 unsigned long flags; 936 int status = 0; 937 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 938 939 spin_lock_irqsave(&garmin_data_p->lock, flags); 940 garmin_data_p->mode = initial_mode; 941 garmin_data_p->count = 0; 942 garmin_data_p->flags &= FLAGS_SESSION_REPLY1_SEEN; 943 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 944 945 /* shutdown any bulk reads that might be going on */ 946 usb_kill_urb(port->write_urb); 947 usb_kill_urb(port->read_urb); 948 949 if (garmin_data_p->state == STATE_RESET) 950 status = garmin_init_session(port); 951 952 garmin_data_p->state = STATE_ACTIVE; 953 return status; 954 } 955 956 957 static void garmin_close(struct usb_serial_port *port) 958 { 959 struct usb_serial *serial = port->serial; 960 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 961 962 dbg("%s - port %d - mode=%d state=%d flags=0x%X", __func__, 963 port->number, garmin_data_p->mode, 964 garmin_data_p->state, garmin_data_p->flags); 965 966 if (!serial) 967 return; 968 969 mutex_lock(&port->serial->disc_mutex); 970 971 if (!port->serial->disconnected) 972 garmin_clear(garmin_data_p); 973 974 /* shutdown our urbs */ 975 usb_kill_urb(port->read_urb); 976 usb_kill_urb(port->write_urb); 977 978 /* keep reset state so we know that we must start a new session */ 979 if (garmin_data_p->state != STATE_RESET) 980 garmin_data_p->state = STATE_DISCONNECTED; 981 982 mutex_unlock(&port->serial->disc_mutex); 983 } 984 985 986 static void garmin_write_bulk_callback(struct urb *urb) 987 { 988 struct usb_serial_port *port = urb->context; 989 990 if (port) { 991 struct garmin_data *garmin_data_p = 992 usb_get_serial_port_data(port); 993 994 if (GARMIN_LAYERID_APPL == getLayerId(urb->transfer_buffer)) { 995 996 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { 997 gsp_send_ack(garmin_data_p, 998 ((__u8 *)urb->transfer_buffer)[4]); 999 } 1000 } 1001 usb_serial_port_softint(port); 1002 } 1003 1004 /* Ignore errors that resulted from garmin_write_bulk with 1005 dismiss_ack = 1 */ 1006 1007 /* free up the transfer buffer, as usb_free_urb() does not do this */ 1008 kfree(urb->transfer_buffer); 1009 } 1010 1011 1012 static int garmin_write_bulk(struct usb_serial_port *port, 1013 const unsigned char *buf, int count, 1014 int dismiss_ack) 1015 { 1016 unsigned long flags; 1017 struct usb_serial *serial = port->serial; 1018 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1019 struct urb *urb; 1020 unsigned char *buffer; 1021 int status; 1022 1023 spin_lock_irqsave(&garmin_data_p->lock, flags); 1024 garmin_data_p->flags &= ~FLAGS_DROP_DATA; 1025 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1026 1027 buffer = kmalloc(count, GFP_ATOMIC); 1028 if (!buffer) { 1029 dev_err(&port->dev, "out of memory\n"); 1030 return -ENOMEM; 1031 } 1032 1033 urb = usb_alloc_urb(0, GFP_ATOMIC); 1034 if (!urb) { 1035 dev_err(&port->dev, "no more free urbs\n"); 1036 kfree(buffer); 1037 return -ENOMEM; 1038 } 1039 1040 memcpy(buffer, buf, count); 1041 1042 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer); 1043 1044 usb_fill_bulk_urb(urb, serial->dev, 1045 usb_sndbulkpipe(serial->dev, 1046 port->bulk_out_endpointAddress), 1047 buffer, count, 1048 garmin_write_bulk_callback, 1049 dismiss_ack ? NULL : port); 1050 urb->transfer_flags |= URB_ZERO_PACKET; 1051 1052 if (GARMIN_LAYERID_APPL == getLayerId(buffer)) { 1053 1054 spin_lock_irqsave(&garmin_data_p->lock, flags); 1055 garmin_data_p->flags |= APP_REQ_SEEN; 1056 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1057 1058 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { 1059 pkt_clear(garmin_data_p); 1060 garmin_data_p->state = STATE_GSP_WAIT_DATA; 1061 } 1062 } 1063 1064 /* send it down the pipe */ 1065 status = usb_submit_urb(urb, GFP_ATOMIC); 1066 if (status) { 1067 dev_err(&port->dev, 1068 "%s - usb_submit_urb(write bulk) failed with status = %d\n", 1069 __func__, status); 1070 count = status; 1071 } 1072 1073 /* we are done with this urb, so let the host driver 1074 * really free it when it is finished with it */ 1075 usb_free_urb(urb); 1076 1077 return count; 1078 } 1079 1080 static int garmin_write(struct tty_struct *tty, struct usb_serial_port *port, 1081 const unsigned char *buf, int count) 1082 { 1083 int pktid, pktsiz, len; 1084 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1085 __le32 *privpkt = (__le32 *)garmin_data_p->privpkt; 1086 1087 usb_serial_debug_data(debug, &port->dev, __func__, count, buf); 1088 1089 if (garmin_data_p->state == STATE_RESET) 1090 return -EIO; 1091 1092 /* check for our private packets */ 1093 if (count >= GARMIN_PKTHDR_LENGTH) { 1094 len = PRIVPKTSIZ; 1095 if (count < len) 1096 len = count; 1097 1098 memcpy(garmin_data_p->privpkt, buf, len); 1099 1100 pktsiz = getDataLength(garmin_data_p->privpkt); 1101 pktid = getPacketId(garmin_data_p->privpkt); 1102 1103 if (count == (GARMIN_PKTHDR_LENGTH+pktsiz) 1104 && GARMIN_LAYERID_PRIVATE == 1105 getLayerId(garmin_data_p->privpkt)) { 1106 1107 dbg("%s - processing private request %d", 1108 __func__, pktid); 1109 1110 /* drop all unfinished transfers */ 1111 garmin_clear(garmin_data_p); 1112 1113 switch (pktid) { 1114 1115 case PRIV_PKTID_SET_DEBUG: 1116 if (pktsiz != 4) 1117 return -EINVPKT; 1118 debug = __le32_to_cpu(privpkt[3]); 1119 dbg("%s - debug level set to 0x%X", 1120 __func__, debug); 1121 break; 1122 1123 case PRIV_PKTID_SET_MODE: 1124 if (pktsiz != 4) 1125 return -EINVPKT; 1126 garmin_data_p->mode = __le32_to_cpu(privpkt[3]); 1127 dbg("%s - mode set to %d", 1128 __func__, garmin_data_p->mode); 1129 break; 1130 1131 case PRIV_PKTID_INFO_REQ: 1132 priv_status_resp(port); 1133 break; 1134 1135 case PRIV_PKTID_RESET_REQ: 1136 process_resetdev_request(port); 1137 break; 1138 1139 case PRIV_PKTID_SET_DEF_MODE: 1140 if (pktsiz != 4) 1141 return -EINVPKT; 1142 initial_mode = __le32_to_cpu(privpkt[3]); 1143 dbg("%s - initial_mode set to %d", 1144 __func__, 1145 garmin_data_p->mode); 1146 break; 1147 } 1148 return count; 1149 } 1150 } 1151 1152 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { 1153 return gsp_receive(garmin_data_p, buf, count); 1154 } else { /* MODE_NATIVE */ 1155 return nat_receive(garmin_data_p, buf, count); 1156 } 1157 } 1158 1159 1160 static int garmin_write_room(struct tty_struct *tty) 1161 { 1162 struct usb_serial_port *port = tty->driver_data; 1163 /* 1164 * Report back the bytes currently available in the output buffer. 1165 */ 1166 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1167 return GPS_OUT_BUFSIZ-garmin_data_p->outsize; 1168 } 1169 1170 1171 static void garmin_read_process(struct garmin_data *garmin_data_p, 1172 unsigned char *data, unsigned data_length, 1173 int bulk_data) 1174 { 1175 unsigned long flags; 1176 1177 if (garmin_data_p->flags & FLAGS_DROP_DATA) { 1178 /* abort-transfer cmd is actice */ 1179 dbg("%s - pkt dropped", __func__); 1180 } else if (garmin_data_p->state != STATE_DISCONNECTED && 1181 garmin_data_p->state != STATE_RESET) { 1182 1183 /* if throttling is active or postprecessing is required 1184 put the received data in the input queue, otherwise 1185 send it directly to the tty port */ 1186 if (garmin_data_p->flags & FLAGS_QUEUING) { 1187 pkt_add(garmin_data_p, data, data_length); 1188 } else if (bulk_data || 1189 getLayerId(data) == GARMIN_LAYERID_APPL) { 1190 1191 spin_lock_irqsave(&garmin_data_p->lock, flags); 1192 garmin_data_p->flags |= APP_RESP_SEEN; 1193 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1194 1195 if (garmin_data_p->mode == MODE_GARMIN_SERIAL) { 1196 pkt_add(garmin_data_p, data, data_length); 1197 } else { 1198 send_to_tty(garmin_data_p->port, data, 1199 data_length); 1200 } 1201 } 1202 /* ignore system layer packets ... */ 1203 } 1204 } 1205 1206 1207 static void garmin_read_bulk_callback(struct urb *urb) 1208 { 1209 unsigned long flags; 1210 struct usb_serial_port *port = urb->context; 1211 struct usb_serial *serial = port->serial; 1212 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1213 unsigned char *data = urb->transfer_buffer; 1214 int status = urb->status; 1215 int retval; 1216 1217 if (!serial) { 1218 dbg("%s - bad serial pointer, exiting", __func__); 1219 return; 1220 } 1221 1222 if (status) { 1223 dbg("%s - nonzero read bulk status received: %d", 1224 __func__, status); 1225 return; 1226 } 1227 1228 usb_serial_debug_data(debug, &port->dev, 1229 __func__, urb->actual_length, data); 1230 1231 garmin_read_process(garmin_data_p, data, urb->actual_length, 1); 1232 1233 if (urb->actual_length == 0 && 1234 0 != (garmin_data_p->flags & FLAGS_BULK_IN_RESTART)) { 1235 spin_lock_irqsave(&garmin_data_p->lock, flags); 1236 garmin_data_p->flags &= ~FLAGS_BULK_IN_RESTART; 1237 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1238 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); 1239 if (retval) 1240 dev_err(&port->dev, 1241 "%s - failed resubmitting read urb, error %d\n", 1242 __func__, retval); 1243 } else if (urb->actual_length > 0) { 1244 /* Continue trying to read until nothing more is received */ 1245 if (0 == (garmin_data_p->flags & FLAGS_THROTTLED)) { 1246 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); 1247 if (retval) 1248 dev_err(&port->dev, 1249 "%s - failed resubmitting read urb, " 1250 "error %d\n", __func__, retval); 1251 } 1252 } else { 1253 dbg("%s - end of bulk data", __func__); 1254 spin_lock_irqsave(&garmin_data_p->lock, flags); 1255 garmin_data_p->flags &= ~FLAGS_BULK_IN_ACTIVE; 1256 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1257 } 1258 } 1259 1260 1261 static void garmin_read_int_callback(struct urb *urb) 1262 { 1263 unsigned long flags; 1264 int retval; 1265 struct usb_serial_port *port = urb->context; 1266 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1267 unsigned char *data = urb->transfer_buffer; 1268 int status = urb->status; 1269 1270 switch (status) { 1271 case 0: 1272 /* success */ 1273 break; 1274 case -ECONNRESET: 1275 case -ENOENT: 1276 case -ESHUTDOWN: 1277 /* this urb is terminated, clean up */ 1278 dbg("%s - urb shutting down with status: %d", 1279 __func__, status); 1280 return; 1281 default: 1282 dbg("%s - nonzero urb status received: %d", 1283 __func__, status); 1284 return; 1285 } 1286 1287 usb_serial_debug_data(debug, &port->dev, __func__, 1288 urb->actual_length, urb->transfer_buffer); 1289 1290 if (urb->actual_length == sizeof(GARMIN_BULK_IN_AVAIL_REPLY) && 1291 0 == memcmp(data, GARMIN_BULK_IN_AVAIL_REPLY, 1292 sizeof(GARMIN_BULK_IN_AVAIL_REPLY))) { 1293 1294 dbg("%s - bulk data available.", __func__); 1295 1296 if (0 == (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) { 1297 1298 /* bulk data available */ 1299 retval = usb_submit_urb(port->read_urb, GFP_ATOMIC); 1300 if (retval) { 1301 dev_err(&port->dev, 1302 "%s - failed submitting read urb, error %d\n", 1303 __func__, retval); 1304 } else { 1305 spin_lock_irqsave(&garmin_data_p->lock, flags); 1306 garmin_data_p->flags |= FLAGS_BULK_IN_ACTIVE; 1307 spin_unlock_irqrestore(&garmin_data_p->lock, 1308 flags); 1309 } 1310 } else { 1311 /* bulk-in transfer still active */ 1312 spin_lock_irqsave(&garmin_data_p->lock, flags); 1313 garmin_data_p->flags |= FLAGS_BULK_IN_RESTART; 1314 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1315 } 1316 1317 } else if (urb->actual_length == (4+sizeof(GARMIN_START_SESSION_REPLY)) 1318 && 0 == memcmp(data, GARMIN_START_SESSION_REPLY, 1319 sizeof(GARMIN_START_SESSION_REPLY))) { 1320 1321 spin_lock_irqsave(&garmin_data_p->lock, flags); 1322 garmin_data_p->flags |= FLAGS_SESSION_REPLY1_SEEN; 1323 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1324 1325 /* save the serial number */ 1326 garmin_data_p->serial_num = __le32_to_cpup( 1327 (__le32 *)(data+GARMIN_PKTHDR_LENGTH)); 1328 1329 dbg("%s - start-of-session reply seen - serial %u.", 1330 __func__, garmin_data_p->serial_num); 1331 } 1332 1333 garmin_read_process(garmin_data_p, data, urb->actual_length, 0); 1334 1335 retval = usb_submit_urb(urb, GFP_ATOMIC); 1336 if (retval) 1337 dev_err(&urb->dev->dev, 1338 "%s - Error %d submitting interrupt urb\n", 1339 __func__, retval); 1340 } 1341 1342 1343 /* 1344 * Sends the next queued packt to the tty port (garmin native mode only) 1345 * and then sets a timer to call itself again until all queued data 1346 * is sent. 1347 */ 1348 static int garmin_flush_queue(struct garmin_data *garmin_data_p) 1349 { 1350 unsigned long flags; 1351 struct garmin_packet *pkt; 1352 1353 if ((garmin_data_p->flags & FLAGS_THROTTLED) == 0) { 1354 pkt = pkt_pop(garmin_data_p); 1355 if (pkt != NULL) { 1356 send_to_tty(garmin_data_p->port, pkt->data, pkt->size); 1357 kfree(pkt); 1358 mod_timer(&garmin_data_p->timer, (1)+jiffies); 1359 1360 } else { 1361 spin_lock_irqsave(&garmin_data_p->lock, flags); 1362 garmin_data_p->flags &= ~FLAGS_QUEUING; 1363 spin_unlock_irqrestore(&garmin_data_p->lock, flags); 1364 } 1365 } 1366 return 0; 1367 } 1368 1369 1370 static void garmin_throttle(struct tty_struct *tty) 1371 { 1372 struct usb_serial_port *port = tty->driver_data; 1373 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1374 1375 /* set flag, data received will be put into a queue 1376 for later processing */ 1377 spin_lock_irq(&garmin_data_p->lock); 1378 garmin_data_p->flags |= FLAGS_QUEUING|FLAGS_THROTTLED; 1379 spin_unlock_irq(&garmin_data_p->lock); 1380 } 1381 1382 1383 static void garmin_unthrottle(struct tty_struct *tty) 1384 { 1385 struct usb_serial_port *port = tty->driver_data; 1386 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1387 int status; 1388 1389 spin_lock_irq(&garmin_data_p->lock); 1390 garmin_data_p->flags &= ~FLAGS_THROTTLED; 1391 spin_unlock_irq(&garmin_data_p->lock); 1392 1393 /* in native mode send queued data to tty, in 1394 serial mode nothing needs to be done here */ 1395 if (garmin_data_p->mode == MODE_NATIVE) 1396 garmin_flush_queue(garmin_data_p); 1397 1398 if (0 != (garmin_data_p->flags & FLAGS_BULK_IN_ACTIVE)) { 1399 status = usb_submit_urb(port->read_urb, GFP_KERNEL); 1400 if (status) 1401 dev_err(&port->dev, 1402 "%s - failed resubmitting read urb, error %d\n", 1403 __func__, status); 1404 } 1405 } 1406 1407 /* 1408 * The timer is currently only used to send queued packets to 1409 * the tty in cases where the protocol provides no own handshaking 1410 * to initiate the transfer. 1411 */ 1412 static void timeout_handler(unsigned long data) 1413 { 1414 struct garmin_data *garmin_data_p = (struct garmin_data *) data; 1415 1416 /* send the next queued packet to the tty port */ 1417 if (garmin_data_p->mode == MODE_NATIVE) 1418 if (garmin_data_p->flags & FLAGS_QUEUING) 1419 garmin_flush_queue(garmin_data_p); 1420 } 1421 1422 1423 1424 static int garmin_attach(struct usb_serial *serial) 1425 { 1426 int status = 0; 1427 struct usb_serial_port *port = serial->port[0]; 1428 struct garmin_data *garmin_data_p = NULL; 1429 1430 garmin_data_p = kzalloc(sizeof(struct garmin_data), GFP_KERNEL); 1431 if (garmin_data_p == NULL) { 1432 dev_err(&port->dev, "%s - Out of memory\n", __func__); 1433 return -ENOMEM; 1434 } 1435 init_timer(&garmin_data_p->timer); 1436 spin_lock_init(&garmin_data_p->lock); 1437 INIT_LIST_HEAD(&garmin_data_p->pktlist); 1438 /* garmin_data_p->timer.expires = jiffies + session_timeout; */ 1439 garmin_data_p->timer.data = (unsigned long)garmin_data_p; 1440 garmin_data_p->timer.function = timeout_handler; 1441 garmin_data_p->port = port; 1442 garmin_data_p->state = 0; 1443 garmin_data_p->flags = 0; 1444 garmin_data_p->count = 0; 1445 usb_set_serial_port_data(port, garmin_data_p); 1446 1447 status = garmin_init_session(port); 1448 1449 return status; 1450 } 1451 1452 1453 static void garmin_disconnect(struct usb_serial *serial) 1454 { 1455 struct usb_serial_port *port = serial->port[0]; 1456 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1457 1458 usb_kill_urb(port->interrupt_in_urb); 1459 del_timer_sync(&garmin_data_p->timer); 1460 } 1461 1462 1463 static void garmin_release(struct usb_serial *serial) 1464 { 1465 struct usb_serial_port *port = serial->port[0]; 1466 struct garmin_data *garmin_data_p = usb_get_serial_port_data(port); 1467 1468 kfree(garmin_data_p); 1469 } 1470 1471 1472 /* All of the device info needed */ 1473 static struct usb_serial_driver garmin_device = { 1474 .driver = { 1475 .owner = THIS_MODULE, 1476 .name = "garmin_gps", 1477 }, 1478 .description = "Garmin GPS usb/tty", 1479 .id_table = id_table, 1480 .num_ports = 1, 1481 .open = garmin_open, 1482 .close = garmin_close, 1483 .throttle = garmin_throttle, 1484 .unthrottle = garmin_unthrottle, 1485 .attach = garmin_attach, 1486 .disconnect = garmin_disconnect, 1487 .release = garmin_release, 1488 .write = garmin_write, 1489 .write_room = garmin_write_room, 1490 .write_bulk_callback = garmin_write_bulk_callback, 1491 .read_bulk_callback = garmin_read_bulk_callback, 1492 .read_int_callback = garmin_read_int_callback, 1493 }; 1494 1495 static struct usb_serial_driver * const serial_drivers[] = { 1496 &garmin_device, NULL 1497 }; 1498 1499 module_usb_serial_driver(garmin_driver, serial_drivers); 1500 1501 MODULE_AUTHOR(DRIVER_AUTHOR); 1502 MODULE_DESCRIPTION(DRIVER_DESC); 1503 MODULE_LICENSE("GPL"); 1504 1505 module_param(debug, bool, S_IWUSR | S_IRUGO); 1506 MODULE_PARM_DESC(debug, "Debug enabled or not"); 1507 module_param(initial_mode, int, S_IRUGO); 1508 MODULE_PARM_DESC(initial_mode, "Initial mode"); 1509