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