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