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