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