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