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