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