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