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