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