Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 1 | /* |
| 2 | * keyspan_remote: USB driver for the Keyspan DMR |
| 3 | * |
| 4 | * Copyright (C) 2005 Zymeta Corporation - Michael Downey (downey@zymeta.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2. |
| 9 | * |
| 10 | * This driver has been put together with the support of Innosys, Inc. |
| 11 | * and Keyspan, Inc the manufacturers of the Keyspan USB DMR product. |
| 12 | */ |
| 13 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/errno.h> |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | #include <linux/module.h> |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 18 | #include <linux/usb/input.h> |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 19 | |
| 20 | #define DRIVER_VERSION "v0.1" |
| 21 | #define DRIVER_AUTHOR "Michael Downey <downey@zymeta.com>" |
| 22 | #define DRIVER_DESC "Driver for the USB Keyspan remote control." |
| 23 | #define DRIVER_LICENSE "GPL" |
| 24 | |
| 25 | /* Parameters that can be passed to the driver. */ |
| 26 | static int debug; |
| 27 | module_param(debug, int, 0444); |
| 28 | MODULE_PARM_DESC(debug, "Enable extra debug messages and information"); |
| 29 | |
| 30 | /* Vendor and product ids */ |
| 31 | #define USB_KEYSPAN_VENDOR_ID 0x06CD |
| 32 | #define USB_KEYSPAN_PRODUCT_UIA11 0x0202 |
| 33 | |
| 34 | /* Defines for converting the data from the remote. */ |
| 35 | #define ZERO 0x18 |
| 36 | #define ZERO_MASK 0x1F /* 5 bits for a 0 */ |
| 37 | #define ONE 0x3C |
| 38 | #define ONE_MASK 0x3F /* 6 bits for a 1 */ |
| 39 | #define SYNC 0x3F80 |
| 40 | #define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */ |
| 41 | #define STOP 0x00 |
| 42 | #define STOP_MASK 0x1F /* 5 bits for the STOP sequence */ |
| 43 | #define GAP 0xFF |
| 44 | |
| 45 | #define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */ |
| 46 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 47 | /* |
| 48 | * Table that maps the 31 possible keycodes to input keys. |
| 49 | * Currently there are 15 and 17 button models so RESERVED codes |
| 50 | * are blank areas in the mapping. |
| 51 | */ |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 52 | static const unsigned short keyspan_key_table[] = { |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 53 | KEY_RESERVED, /* 0 is just a place holder. */ |
| 54 | KEY_RESERVED, |
| 55 | KEY_STOP, |
| 56 | KEY_PLAYCD, |
| 57 | KEY_RESERVED, |
| 58 | KEY_PREVIOUSSONG, |
| 59 | KEY_REWIND, |
| 60 | KEY_FORWARD, |
| 61 | KEY_NEXTSONG, |
| 62 | KEY_RESERVED, |
| 63 | KEY_RESERVED, |
| 64 | KEY_RESERVED, |
| 65 | KEY_PAUSE, |
| 66 | KEY_VOLUMEUP, |
| 67 | KEY_RESERVED, |
| 68 | KEY_RESERVED, |
| 69 | KEY_RESERVED, |
| 70 | KEY_VOLUMEDOWN, |
| 71 | KEY_RESERVED, |
| 72 | KEY_UP, |
| 73 | KEY_RESERVED, |
| 74 | KEY_MUTE, |
| 75 | KEY_LEFT, |
| 76 | KEY_ENTER, |
| 77 | KEY_RIGHT, |
| 78 | KEY_RESERVED, |
| 79 | KEY_RESERVED, |
| 80 | KEY_DOWN, |
| 81 | KEY_RESERVED, |
| 82 | KEY_KPASTERISK, |
| 83 | KEY_RESERVED, |
| 84 | KEY_MENU |
| 85 | }; |
| 86 | |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 87 | /* table of devices that work with this driver */ |
| 88 | static struct usb_device_id keyspan_table[] = { |
| 89 | { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) }, |
| 90 | { } /* Terminating entry */ |
| 91 | }; |
| 92 | |
| 93 | /* Structure to store all the real stuff that a remote sends to us. */ |
| 94 | struct keyspan_message { |
| 95 | u16 system; |
| 96 | u8 button; |
| 97 | u8 toggle; |
| 98 | }; |
| 99 | |
| 100 | /* Structure used for all the bit testing magic needed to be done. */ |
| 101 | struct bit_tester { |
| 102 | u32 tester; |
| 103 | int len; |
| 104 | int pos; |
| 105 | int bits_left; |
| 106 | u8 buffer[32]; |
| 107 | }; |
| 108 | |
| 109 | /* Structure to hold all of our driver specific stuff */ |
| 110 | struct usb_keyspan { |
| 111 | char name[128]; |
| 112 | char phys[64]; |
| 113 | unsigned short keymap[ARRAY_SIZE(keyspan_key_table)]; |
| 114 | struct usb_device *udev; |
| 115 | struct input_dev *input; |
| 116 | struct usb_interface *interface; |
| 117 | struct usb_endpoint_descriptor *in_endpoint; |
| 118 | struct urb* irq_urb; |
| 119 | int open; |
| 120 | dma_addr_t in_dma; |
| 121 | unsigned char *in_buffer; |
| 122 | |
| 123 | /* variables used to parse messages from remote. */ |
| 124 | struct bit_tester data; |
| 125 | int stage; |
| 126 | int toggle; |
| 127 | }; |
| 128 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 129 | static struct usb_driver keyspan_driver; |
| 130 | |
| 131 | /* |
| 132 | * Debug routine that prints out what we've received from the remote. |
| 133 | */ |
| 134 | static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/ |
| 135 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 136 | char codes[4 * RECV_SIZE]; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 137 | int i; |
| 138 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 139 | for (i = 0; i < RECV_SIZE; i++) |
| 140 | snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 141 | |
| 142 | dev_info(&dev->udev->dev, "%s\n", codes); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Routine that manages the bit_tester structure. It makes sure that there are |
| 147 | * at least bits_needed bits loaded into the tester. |
| 148 | */ |
| 149 | static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed) |
| 150 | { |
| 151 | if (dev->data.bits_left >= bits_needed) |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 152 | return 0; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Somehow we've missed the last message. The message will be repeated |
| 156 | * though so it's not too big a deal |
| 157 | */ |
| 158 | if (dev->data.pos >= dev->data.len) { |
Greg Kroah-Hartman | 419b1a1 | 2012-05-04 15:32:58 -0700 | [diff] [blame] | 159 | dev_dbg(&dev->interface->dev, |
Greg Kroah-Hartman | 654f311 | 2005-11-17 09:48:09 -0800 | [diff] [blame] | 160 | "%s - Error ran out of data. pos: %d, len: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 161 | __func__, dev->data.pos, dev->data.len); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 162 | return -1; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Load as much as we can into the tester. */ |
| 166 | while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) && |
| 167 | (dev->data.pos < dev->data.len)) { |
| 168 | dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left); |
| 169 | dev->data.bits_left += 8; |
| 170 | } |
| 171 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 172 | return 0; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 173 | } |
| 174 | |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 175 | static void keyspan_report_button(struct usb_keyspan *remote, int button, int press) |
| 176 | { |
| 177 | struct input_dev *input = remote->input; |
| 178 | |
| 179 | input_event(input, EV_MSC, MSC_SCAN, button); |
| 180 | input_report_key(input, remote->keymap[button], press); |
| 181 | input_sync(input); |
| 182 | } |
| 183 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 184 | /* |
| 185 | * Routine that handles all the logic needed to parse out the message from the remote. |
| 186 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 187 | static void keyspan_check_data(struct usb_keyspan *remote) |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 188 | { |
| 189 | int i; |
| 190 | int found = 0; |
| 191 | struct keyspan_message message; |
| 192 | |
| 193 | switch(remote->stage) { |
| 194 | case 0: |
| 195 | /* |
| 196 | * In stage 0 we want to find the start of a message. The remote sends a 0xFF as filler. |
| 197 | * So the first byte that isn't a FF should be the start of a new message. |
| 198 | */ |
| 199 | for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i); |
| 200 | |
| 201 | if (i < RECV_SIZE) { |
| 202 | memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE); |
| 203 | remote->data.len = RECV_SIZE; |
| 204 | remote->data.pos = 0; |
| 205 | remote->data.tester = 0; |
| 206 | remote->data.bits_left = 0; |
| 207 | remote->stage = 1; |
| 208 | } |
| 209 | break; |
| 210 | |
| 211 | case 1: |
| 212 | /* |
| 213 | * Stage 1 we should have 16 bytes and should be able to detect a |
| 214 | * SYNC. The SYNC is 14 bits, 7 0's and then 7 1's. |
| 215 | */ |
| 216 | memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE); |
| 217 | remote->data.len += RECV_SIZE; |
| 218 | |
| 219 | found = 0; |
| 220 | while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) { |
| 221 | for (i = 0; i < 8; ++i) { |
| 222 | if (keyspan_load_tester(remote, 14) != 0) { |
| 223 | remote->stage = 0; |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | if ((remote->data.tester & SYNC_MASK) == SYNC) { |
| 228 | remote->data.tester = remote->data.tester >> 14; |
| 229 | remote->data.bits_left -= 14; |
| 230 | found = 1; |
| 231 | break; |
| 232 | } else { |
| 233 | remote->data.tester = remote->data.tester >> 1; |
| 234 | --remote->data.bits_left; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (!found) { |
| 240 | remote->stage = 0; |
| 241 | remote->data.len = 0; |
| 242 | } else { |
| 243 | remote->stage = 2; |
| 244 | } |
| 245 | break; |
| 246 | |
| 247 | case 2: |
| 248 | /* |
| 249 | * Stage 2 we should have 24 bytes which will be enough for a full |
| 250 | * message. We need to parse out the system code, button code, |
| 251 | * toggle code, and stop. |
| 252 | */ |
| 253 | memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE); |
| 254 | remote->data.len += RECV_SIZE; |
| 255 | |
| 256 | message.system = 0; |
| 257 | for (i = 0; i < 9; i++) { |
| 258 | keyspan_load_tester(remote, 6); |
| 259 | |
| 260 | if ((remote->data.tester & ZERO_MASK) == ZERO) { |
| 261 | message.system = message.system << 1; |
| 262 | remote->data.tester = remote->data.tester >> 5; |
| 263 | remote->data.bits_left -= 5; |
| 264 | } else if ((remote->data.tester & ONE_MASK) == ONE) { |
| 265 | message.system = (message.system << 1) + 1; |
| 266 | remote->data.tester = remote->data.tester >> 6; |
| 267 | remote->data.bits_left -= 6; |
| 268 | } else { |
Greg Kroah-Hartman | 419b1a1 | 2012-05-04 15:32:58 -0700 | [diff] [blame] | 269 | dev_err(&remote->interface->dev, |
Greg Kroah-Hartman | 4efeca5 | 2012-04-25 14:48:28 -0700 | [diff] [blame] | 270 | "%s - Unknown sequence found in system data.\n", |
| 271 | __func__); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 272 | remote->stage = 0; |
| 273 | return; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | message.button = 0; |
| 278 | for (i = 0; i < 5; i++) { |
| 279 | keyspan_load_tester(remote, 6); |
| 280 | |
| 281 | if ((remote->data.tester & ZERO_MASK) == ZERO) { |
| 282 | message.button = message.button << 1; |
| 283 | remote->data.tester = remote->data.tester >> 5; |
| 284 | remote->data.bits_left -= 5; |
| 285 | } else if ((remote->data.tester & ONE_MASK) == ONE) { |
| 286 | message.button = (message.button << 1) + 1; |
| 287 | remote->data.tester = remote->data.tester >> 6; |
| 288 | remote->data.bits_left -= 6; |
| 289 | } else { |
Greg Kroah-Hartman | 419b1a1 | 2012-05-04 15:32:58 -0700 | [diff] [blame] | 290 | dev_err(&remote->interface->dev, |
Greg Kroah-Hartman | 4efeca5 | 2012-04-25 14:48:28 -0700 | [diff] [blame] | 291 | "%s - Unknown sequence found in button data.\n", |
| 292 | __func__); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 293 | remote->stage = 0; |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | keyspan_load_tester(remote, 6); |
| 299 | if ((remote->data.tester & ZERO_MASK) == ZERO) { |
| 300 | message.toggle = 0; |
| 301 | remote->data.tester = remote->data.tester >> 5; |
| 302 | remote->data.bits_left -= 5; |
| 303 | } else if ((remote->data.tester & ONE_MASK) == ONE) { |
| 304 | message.toggle = 1; |
| 305 | remote->data.tester = remote->data.tester >> 6; |
| 306 | remote->data.bits_left -= 6; |
| 307 | } else { |
Greg Kroah-Hartman | 419b1a1 | 2012-05-04 15:32:58 -0700 | [diff] [blame] | 308 | dev_err(&remote->interface->dev, |
Greg Kroah-Hartman | 4efeca5 | 2012-04-25 14:48:28 -0700 | [diff] [blame] | 309 | "%s - Error in message, invalid toggle.\n", |
| 310 | __func__); |
Michael Downey | 01e8950 | 2006-04-03 08:58:07 -0600 | [diff] [blame] | 311 | remote->stage = 0; |
| 312 | return; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | keyspan_load_tester(remote, 5); |
| 316 | if ((remote->data.tester & STOP_MASK) == STOP) { |
| 317 | remote->data.tester = remote->data.tester >> 5; |
| 318 | remote->data.bits_left -= 5; |
| 319 | } else { |
Greg Kroah-Hartman | 419b1a1 | 2012-05-04 15:32:58 -0700 | [diff] [blame] | 320 | dev_err(&remote->interface->dev, |
Greg Kroah-Hartman | 4efeca5 | 2012-04-25 14:48:28 -0700 | [diff] [blame] | 321 | "Bad message received, no stop bit found.\n"); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 322 | } |
| 323 | |
Greg Kroah-Hartman | 419b1a1 | 2012-05-04 15:32:58 -0700 | [diff] [blame] | 324 | dev_dbg(&remote->interface->dev, |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 325 | "%s found valid message: system: %d, button: %d, toggle: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 326 | __func__, message.system, message.button, message.toggle); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 327 | |
| 328 | if (message.toggle != remote->toggle) { |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 329 | keyspan_report_button(remote, message.button, 1); |
| 330 | keyspan_report_button(remote, message.button, 0); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 331 | remote->toggle = message.toggle; |
| 332 | } |
| 333 | |
| 334 | remote->stage = 0; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Routine for sending all the initialization messages to the remote. |
| 341 | */ |
| 342 | static int keyspan_setup(struct usb_device* dev) |
| 343 | { |
| 344 | int retval = 0; |
| 345 | |
| 346 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 347 | 0x11, 0x40, 0x5601, 0x0, NULL, 0, 0); |
| 348 | if (retval) { |
| 349 | dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 350 | __func__, retval); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 351 | return(retval); |
| 352 | } |
| 353 | |
| 354 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 355 | 0x44, 0x40, 0x0, 0x0, NULL, 0, 0); |
| 356 | if (retval) { |
| 357 | dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 358 | __func__, retval); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 359 | return(retval); |
| 360 | } |
| 361 | |
| 362 | retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), |
| 363 | 0x22, 0x40, 0x0, 0x0, NULL, 0, 0); |
| 364 | if (retval) { |
| 365 | dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 366 | __func__, retval); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 367 | return(retval); |
| 368 | } |
| 369 | |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 370 | dev_dbg(&dev->dev, "%s - Setup complete.\n", __func__); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 371 | return(retval); |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * Routine used to handle a new message that has come in. |
| 376 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 377 | static void keyspan_irq_recv(struct urb *urb) |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 378 | { |
| 379 | struct usb_keyspan *dev = urb->context; |
| 380 | int retval; |
| 381 | |
| 382 | /* Check our status in case we need to bail out early. */ |
| 383 | switch (urb->status) { |
| 384 | case 0: |
| 385 | break; |
| 386 | |
| 387 | /* Device went away so don't keep trying to read from it. */ |
| 388 | case -ECONNRESET: |
| 389 | case -ENOENT: |
| 390 | case -ESHUTDOWN: |
| 391 | return; |
| 392 | |
| 393 | default: |
| 394 | goto resubmit; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (debug) |
| 398 | keyspan_print(dev); |
| 399 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 400 | keyspan_check_data(dev); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 401 | |
| 402 | resubmit: |
| 403 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 404 | if (retval) |
Greg Kroah-Hartman | 419b1a1 | 2012-05-04 15:32:58 -0700 | [diff] [blame] | 405 | dev_err(&dev->interface->dev, |
Greg Kroah-Hartman | 4efeca5 | 2012-04-25 14:48:28 -0700 | [diff] [blame] | 406 | "%s - usb_submit_urb failed with result: %d\n", |
| 407 | __func__, retval); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static int keyspan_open(struct input_dev *dev) |
| 411 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 412 | struct usb_keyspan *remote = input_get_drvdata(dev); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 413 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 414 | remote->irq_urb->dev = remote->udev; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 415 | if (usb_submit_urb(remote->irq_urb, GFP_KERNEL)) |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 416 | return -EIO; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 417 | |
| 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static void keyspan_close(struct input_dev *dev) |
| 422 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 423 | struct usb_keyspan *remote = input_get_drvdata(dev); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 424 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 425 | usb_kill_urb(remote->irq_urb); |
| 426 | } |
| 427 | |
| 428 | static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface) |
| 429 | { |
| 430 | |
| 431 | struct usb_endpoint_descriptor *endpoint; |
| 432 | int i; |
| 433 | |
| 434 | for (i = 0; i < iface->desc.bNumEndpoints; ++i) { |
| 435 | endpoint = &iface->endpoint[i].desc; |
| 436 | |
Luiz Fernando N. Capitulino | 9672319 | 2006-09-27 11:58:53 -0700 | [diff] [blame] | 437 | if (usb_endpoint_is_int_in(endpoint)) { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 438 | /* we found our interrupt in endpoint */ |
| 439 | return endpoint; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | return NULL; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Routine that sets up the driver to handle a specific USB device detected on the bus. |
| 448 | */ |
| 449 | static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id) |
| 450 | { |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 451 | struct usb_device *udev = interface_to_usbdev(interface); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 452 | struct usb_endpoint_descriptor *endpoint; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 453 | struct usb_keyspan *remote; |
| 454 | struct input_dev *input_dev; |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 455 | int i, error; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 456 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 457 | endpoint = keyspan_get_in_endpoint(interface->cur_altsetting); |
| 458 | if (!endpoint) |
| 459 | return -ENODEV; |
| 460 | |
| 461 | remote = kzalloc(sizeof(*remote), GFP_KERNEL); |
| 462 | input_dev = input_allocate_device(); |
| 463 | if (!remote || !input_dev) { |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 464 | error = -ENOMEM; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 465 | goto fail1; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 466 | } |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 467 | |
| 468 | remote->udev = udev; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 469 | remote->input = input_dev; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 470 | remote->interface = interface; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 471 | remote->in_endpoint = endpoint; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 472 | remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */ |
| 473 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 474 | remote->in_buffer = usb_alloc_coherent(udev, RECV_SIZE, GFP_ATOMIC, &remote->in_dma); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 475 | if (!remote->in_buffer) { |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 476 | error = -ENOMEM; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 477 | goto fail1; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 481 | if (!remote->irq_urb) { |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 482 | error = -ENOMEM; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 483 | goto fail2; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 484 | } |
| 485 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 486 | error = keyspan_setup(udev); |
| 487 | if (error) { |
| 488 | error = -ENODEV; |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 489 | goto fail3; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 490 | } |
| 491 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 492 | if (udev->manufacturer) |
| 493 | strlcpy(remote->name, udev->manufacturer, sizeof(remote->name)); |
| 494 | |
| 495 | if (udev->product) { |
| 496 | if (udev->manufacturer) |
| 497 | strlcat(remote->name, " ", sizeof(remote->name)); |
| 498 | strlcat(remote->name, udev->product, sizeof(remote->name)); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 499 | } |
| 500 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 501 | if (!strlen(remote->name)) |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 502 | snprintf(remote->name, sizeof(remote->name), |
| 503 | "USB Keyspan Remote %04x:%04x", |
| 504 | le16_to_cpu(udev->descriptor.idVendor), |
| 505 | le16_to_cpu(udev->descriptor.idProduct)); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 506 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 507 | usb_make_path(udev, remote->phys, sizeof(remote->phys)); |
| 508 | strlcat(remote->phys, "/input0", sizeof(remote->phys)); |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 509 | memcpy(remote->keymap, keyspan_key_table, sizeof(remote->keymap)); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 510 | |
| 511 | input_dev->name = remote->name; |
| 512 | input_dev->phys = remote->phys; |
| 513 | usb_to_input_id(udev, &input_dev->id); |
Dmitry Torokhov | c0f82d5 | 2007-04-12 01:35:03 -0400 | [diff] [blame] | 514 | input_dev->dev.parent = &interface->dev; |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 515 | input_dev->keycode = remote->keymap; |
| 516 | input_dev->keycodesize = sizeof(unsigned short); |
| 517 | input_dev->keycodemax = ARRAY_SIZE(remote->keymap); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 518 | |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 519 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
| 520 | __set_bit(EV_KEY, input_dev->evbit); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 521 | for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++) |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 522 | __set_bit(keyspan_key_table[i], input_dev->keybit); |
| 523 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 524 | |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 525 | input_set_drvdata(input_dev, remote); |
| 526 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 527 | input_dev->open = keyspan_open; |
| 528 | input_dev->close = keyspan_close; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 529 | |
| 530 | /* |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 531 | * Initialize the URB to access the device. |
| 532 | * The urb gets sent to the device in keyspan_open() |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 533 | */ |
| 534 | usb_fill_int_urb(remote->irq_urb, |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 535 | remote->udev, |
| 536 | usb_rcvintpipe(remote->udev, endpoint->bEndpointAddress), |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 537 | remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote, |
Dmitry Torokhov | 1953ea2 | 2007-11-04 00:41:24 -0400 | [diff] [blame] | 538 | endpoint->bInterval); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 539 | remote->irq_urb->transfer_dma = remote->in_dma; |
| 540 | remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 541 | |
| 542 | /* we can register the device now, as it is ready */ |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 543 | error = input_register_device(remote->input); |
| 544 | if (error) |
| 545 | goto fail3; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 546 | |
| 547 | /* save our data pointer in this interface device */ |
| 548 | usb_set_intfdata(interface, remote); |
| 549 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 550 | return 0; |
| 551 | |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 552 | fail3: usb_free_urb(remote->irq_urb); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 553 | fail2: usb_free_coherent(udev, RECV_SIZE, remote->in_buffer, remote->in_dma); |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 554 | fail1: kfree(remote); |
| 555 | input_free_device(input_dev); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 556 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 557 | return error; |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Routine called when a device is disconnected from the USB. |
| 562 | */ |
| 563 | static void keyspan_disconnect(struct usb_interface *interface) |
| 564 | { |
| 565 | struct usb_keyspan *remote; |
| 566 | |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 567 | remote = usb_get_intfdata(interface); |
| 568 | usb_set_intfdata(interface, NULL); |
| 569 | |
| 570 | if (remote) { /* We have a valid driver structure so clean up everything we allocated. */ |
Dmitry Torokhov | c5b7c7c | 2005-09-15 02:01:47 -0500 | [diff] [blame] | 571 | input_unregister_device(remote->input); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 572 | usb_kill_urb(remote->irq_urb); |
| 573 | usb_free_urb(remote->irq_urb); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 574 | usb_free_coherent(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 575 | kfree(remote); |
| 576 | } |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* |
| 580 | * Standard driver set up sections |
| 581 | */ |
| 582 | static struct usb_driver keyspan_driver = |
| 583 | { |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 584 | .name = "keyspan_remote", |
| 585 | .probe = keyspan_probe, |
| 586 | .disconnect = keyspan_disconnect, |
| 587 | .id_table = keyspan_table |
| 588 | }; |
| 589 | |
Greg Kroah-Hartman | 08642e7 | 2011-11-18 09:48:31 -0800 | [diff] [blame] | 590 | module_usb_driver(keyspan_driver); |
Michael Downey | 99f83c9 | 2005-06-27 11:48:26 -0600 | [diff] [blame] | 591 | |
| 592 | MODULE_DEVICE_TABLE(usb, keyspan_table); |
| 593 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 594 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 595 | MODULE_LICENSE(DRIVER_LICENSE); |