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