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