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