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