blob: 86ddd72dbe3d1a19121c67a04fd0e57bcd7040a2 [file] [log] [blame]
Michael Downey99f83c92005-06-27 11:48:26 -06001/*
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 Downey99f83c92005-06-27 11:48:26 -060014#include <linux/kernel.h>
15#include <linux/errno.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
David Brownellae0dadc2006-06-13 10:04:34 -070020#include <linux/usb/input.h>
Michael Downey99f83c92005-06-27 11:48:26 -060021
22#define DRIVER_VERSION "v0.1"
23#define DRIVER_AUTHOR "Michael Downey <downey@zymeta.com>"
24#define DRIVER_DESC "Driver for the USB Keyspan remote control."
25#define DRIVER_LICENSE "GPL"
26
27/* Parameters that can be passed to the driver. */
28static int debug;
29module_param(debug, int, 0444);
30MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
31
32/* Vendor and product ids */
33#define USB_KEYSPAN_VENDOR_ID 0x06CD
34#define USB_KEYSPAN_PRODUCT_UIA11 0x0202
35
36/* Defines for converting the data from the remote. */
37#define ZERO 0x18
38#define ZERO_MASK 0x1F /* 5 bits for a 0 */
39#define ONE 0x3C
40#define ONE_MASK 0x3F /* 6 bits for a 1 */
41#define SYNC 0x3F80
42#define SYNC_MASK 0x3FFF /* 14 bits for a SYNC sequence */
43#define STOP 0x00
44#define STOP_MASK 0x1F /* 5 bits for the STOP sequence */
45#define GAP 0xFF
46
47#define RECV_SIZE 8 /* The UIA-11 type have a 8 byte limit. */
48
Michael Downey99f83c92005-06-27 11:48:26 -060049/*
50 * Table that maps the 31 possible keycodes to input keys.
51 * Currently there are 15 and 17 button models so RESERVED codes
52 * are blank areas in the mapping.
53 */
Dmitry Torokhov1953ea22007-11-04 00:41:24 -040054static const unsigned short keyspan_key_table[] = {
Michael Downey99f83c92005-06-27 11:48:26 -060055 KEY_RESERVED, /* 0 is just a place holder. */
56 KEY_RESERVED,
57 KEY_STOP,
58 KEY_PLAYCD,
59 KEY_RESERVED,
60 KEY_PREVIOUSSONG,
61 KEY_REWIND,
62 KEY_FORWARD,
63 KEY_NEXTSONG,
64 KEY_RESERVED,
65 KEY_RESERVED,
66 KEY_RESERVED,
67 KEY_PAUSE,
68 KEY_VOLUMEUP,
69 KEY_RESERVED,
70 KEY_RESERVED,
71 KEY_RESERVED,
72 KEY_VOLUMEDOWN,
73 KEY_RESERVED,
74 KEY_UP,
75 KEY_RESERVED,
76 KEY_MUTE,
77 KEY_LEFT,
78 KEY_ENTER,
79 KEY_RIGHT,
80 KEY_RESERVED,
81 KEY_RESERVED,
82 KEY_DOWN,
83 KEY_RESERVED,
84 KEY_KPASTERISK,
85 KEY_RESERVED,
86 KEY_MENU
87};
88
Dmitry Torokhov1953ea22007-11-04 00:41:24 -040089/* table of devices that work with this driver */
90static struct usb_device_id keyspan_table[] = {
91 { USB_DEVICE(USB_KEYSPAN_VENDOR_ID, USB_KEYSPAN_PRODUCT_UIA11) },
92 { } /* Terminating entry */
93};
94
95/* Structure to store all the real stuff that a remote sends to us. */
96struct keyspan_message {
97 u16 system;
98 u8 button;
99 u8 toggle;
100};
101
102/* Structure used for all the bit testing magic needed to be done. */
103struct bit_tester {
104 u32 tester;
105 int len;
106 int pos;
107 int bits_left;
108 u8 buffer[32];
109};
110
111/* Structure to hold all of our driver specific stuff */
112struct usb_keyspan {
113 char name[128];
114 char phys[64];
115 unsigned short keymap[ARRAY_SIZE(keyspan_key_table)];
116 struct usb_device *udev;
117 struct input_dev *input;
118 struct usb_interface *interface;
119 struct usb_endpoint_descriptor *in_endpoint;
120 struct urb* irq_urb;
121 int open;
122 dma_addr_t in_dma;
123 unsigned char *in_buffer;
124
125 /* variables used to parse messages from remote. */
126 struct bit_tester data;
127 int stage;
128 int toggle;
129};
130
Michael Downey99f83c92005-06-27 11:48:26 -0600131static struct usb_driver keyspan_driver;
132
133/*
134 * Debug routine that prints out what we've received from the remote.
135 */
136static void keyspan_print(struct usb_keyspan* dev) /*unsigned char* data)*/
137{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500138 char codes[4 * RECV_SIZE];
Michael Downey99f83c92005-06-27 11:48:26 -0600139 int i;
140
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500141 for (i = 0; i < RECV_SIZE; i++)
142 snprintf(codes + i * 3, 4, "%02x ", dev->in_buffer[i]);
Michael Downey99f83c92005-06-27 11:48:26 -0600143
144 dev_info(&dev->udev->dev, "%s\n", codes);
145}
146
147/*
148 * Routine that manages the bit_tester structure. It makes sure that there are
149 * at least bits_needed bits loaded into the tester.
150 */
151static int keyspan_load_tester(struct usb_keyspan* dev, int bits_needed)
152{
153 if (dev->data.bits_left >= bits_needed)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500154 return 0;
Michael Downey99f83c92005-06-27 11:48:26 -0600155
156 /*
157 * Somehow we've missed the last message. The message will be repeated
158 * though so it's not too big a deal
159 */
160 if (dev->data.pos >= dev->data.len) {
Greg Kroah-Hartman654f3112005-11-17 09:48:09 -0800161 dev_dbg(&dev->udev->dev,
162 "%s - Error ran out of data. pos: %d, len: %d\n",
Michael Downey99f83c92005-06-27 11:48:26 -0600163 __FUNCTION__, dev->data.pos, dev->data.len);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500164 return -1;
Michael Downey99f83c92005-06-27 11:48:26 -0600165 }
166
167 /* Load as much as we can into the tester. */
168 while ((dev->data.bits_left + 7 < (sizeof(dev->data.tester) * 8)) &&
169 (dev->data.pos < dev->data.len)) {
170 dev->data.tester += (dev->data.buffer[dev->data.pos++] << dev->data.bits_left);
171 dev->data.bits_left += 8;
172 }
173
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500174 return 0;
Michael Downey99f83c92005-06-27 11:48:26 -0600175}
176
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400177static void keyspan_report_button(struct usb_keyspan *remote, int button, int press)
178{
179 struct input_dev *input = remote->input;
180
181 input_event(input, EV_MSC, MSC_SCAN, button);
182 input_report_key(input, remote->keymap[button], press);
183 input_sync(input);
184}
185
Michael Downey99f83c92005-06-27 11:48:26 -0600186/*
187 * Routine that handles all the logic needed to parse out the message from the remote.
188 */
David Howells7d12e782006-10-05 14:55:46 +0100189static void keyspan_check_data(struct usb_keyspan *remote)
Michael Downey99f83c92005-06-27 11:48:26 -0600190{
191 int i;
192 int found = 0;
193 struct keyspan_message message;
194
195 switch(remote->stage) {
196 case 0:
197 /*
198 * In stage 0 we want to find the start of a message. The remote sends a 0xFF as filler.
199 * So the first byte that isn't a FF should be the start of a new message.
200 */
201 for (i = 0; i < RECV_SIZE && remote->in_buffer[i] == GAP; ++i);
202
203 if (i < RECV_SIZE) {
204 memcpy(remote->data.buffer, remote->in_buffer, RECV_SIZE);
205 remote->data.len = RECV_SIZE;
206 remote->data.pos = 0;
207 remote->data.tester = 0;
208 remote->data.bits_left = 0;
209 remote->stage = 1;
210 }
211 break;
212
213 case 1:
214 /*
215 * Stage 1 we should have 16 bytes and should be able to detect a
216 * SYNC. The SYNC is 14 bits, 7 0's and then 7 1's.
217 */
218 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
219 remote->data.len += RECV_SIZE;
220
221 found = 0;
222 while ((remote->data.bits_left >= 14 || remote->data.pos < remote->data.len) && !found) {
223 for (i = 0; i < 8; ++i) {
224 if (keyspan_load_tester(remote, 14) != 0) {
225 remote->stage = 0;
226 return;
227 }
228
229 if ((remote->data.tester & SYNC_MASK) == SYNC) {
230 remote->data.tester = remote->data.tester >> 14;
231 remote->data.bits_left -= 14;
232 found = 1;
233 break;
234 } else {
235 remote->data.tester = remote->data.tester >> 1;
236 --remote->data.bits_left;
237 }
238 }
239 }
240
241 if (!found) {
242 remote->stage = 0;
243 remote->data.len = 0;
244 } else {
245 remote->stage = 2;
246 }
247 break;
248
249 case 2:
250 /*
251 * Stage 2 we should have 24 bytes which will be enough for a full
252 * message. We need to parse out the system code, button code,
253 * toggle code, and stop.
254 */
255 memcpy(remote->data.buffer + remote->data.len, remote->in_buffer, RECV_SIZE);
256 remote->data.len += RECV_SIZE;
257
258 message.system = 0;
259 for (i = 0; i < 9; i++) {
260 keyspan_load_tester(remote, 6);
261
262 if ((remote->data.tester & ZERO_MASK) == ZERO) {
263 message.system = message.system << 1;
264 remote->data.tester = remote->data.tester >> 5;
265 remote->data.bits_left -= 5;
266 } else if ((remote->data.tester & ONE_MASK) == ONE) {
267 message.system = (message.system << 1) + 1;
268 remote->data.tester = remote->data.tester >> 6;
269 remote->data.bits_left -= 6;
270 } else {
271 err("%s - Unknown sequence found in system data.\n", __FUNCTION__);
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 {
290 err("%s - Unknown sequence found in button data.\n", __FUNCTION__);
291 remote->stage = 0;
292 return;
293 }
294 }
295
296 keyspan_load_tester(remote, 6);
297 if ((remote->data.tester & ZERO_MASK) == ZERO) {
298 message.toggle = 0;
299 remote->data.tester = remote->data.tester >> 5;
300 remote->data.bits_left -= 5;
301 } else if ((remote->data.tester & ONE_MASK) == ONE) {
302 message.toggle = 1;
303 remote->data.tester = remote->data.tester >> 6;
304 remote->data.bits_left -= 6;
305 } else {
306 err("%s - Error in message, invalid toggle.\n", __FUNCTION__);
Michael Downey01e89502006-04-03 08:58:07 -0600307 remote->stage = 0;
308 return;
Michael Downey99f83c92005-06-27 11:48:26 -0600309 }
310
311 keyspan_load_tester(remote, 5);
312 if ((remote->data.tester & STOP_MASK) == STOP) {
313 remote->data.tester = remote->data.tester >> 5;
314 remote->data.bits_left -= 5;
315 } else {
316 err("Bad message recieved, no stop bit found.\n");
317 }
318
Greg Kroah-Hartman654f3112005-11-17 09:48:09 -0800319 dev_dbg(&remote->udev->dev,
Michael Downey99f83c92005-06-27 11:48:26 -0600320 "%s found valid message: system: %d, button: %d, toggle: %d\n",
321 __FUNCTION__, message.system, message.button, message.toggle);
322
323 if (message.toggle != remote->toggle) {
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400324 keyspan_report_button(remote, message.button, 1);
325 keyspan_report_button(remote, message.button, 0);
Michael Downey99f83c92005-06-27 11:48:26 -0600326 remote->toggle = message.toggle;
327 }
328
329 remote->stage = 0;
330 break;
331 }
332}
333
334/*
335 * Routine for sending all the initialization messages to the remote.
336 */
337static int keyspan_setup(struct usb_device* dev)
338{
339 int retval = 0;
340
341 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
342 0x11, 0x40, 0x5601, 0x0, NULL, 0, 0);
343 if (retval) {
344 dev_dbg(&dev->dev, "%s - failed to set bit rate due to error: %d\n",
345 __FUNCTION__, retval);
346 return(retval);
347 }
348
349 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
350 0x44, 0x40, 0x0, 0x0, NULL, 0, 0);
351 if (retval) {
352 dev_dbg(&dev->dev, "%s - failed to set resume sensitivity due to error: %d\n",
353 __FUNCTION__, retval);
354 return(retval);
355 }
356
357 retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
358 0x22, 0x40, 0x0, 0x0, NULL, 0, 0);
359 if (retval) {
360 dev_dbg(&dev->dev, "%s - failed to turn receive on due to error: %d\n",
361 __FUNCTION__, retval);
362 return(retval);
363 }
364
365 dev_dbg(&dev->dev, "%s - Setup complete.\n", __FUNCTION__);
366 return(retval);
367}
368
369/*
370 * Routine used to handle a new message that has come in.
371 */
David Howells7d12e782006-10-05 14:55:46 +0100372static void keyspan_irq_recv(struct urb *urb)
Michael Downey99f83c92005-06-27 11:48:26 -0600373{
374 struct usb_keyspan *dev = urb->context;
375 int retval;
376
377 /* Check our status in case we need to bail out early. */
378 switch (urb->status) {
379 case 0:
380 break;
381
382 /* Device went away so don't keep trying to read from it. */
383 case -ECONNRESET:
384 case -ENOENT:
385 case -ESHUTDOWN:
386 return;
387
388 default:
389 goto resubmit;
390 break;
391 }
392
393 if (debug)
394 keyspan_print(dev);
395
David Howells7d12e782006-10-05 14:55:46 +0100396 keyspan_check_data(dev);
Michael Downey99f83c92005-06-27 11:48:26 -0600397
398resubmit:
399 retval = usb_submit_urb(urb, GFP_ATOMIC);
400 if (retval)
401 err ("%s - usb_submit_urb failed with result: %d", __FUNCTION__, retval);
402}
403
404static int keyspan_open(struct input_dev *dev)
405{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400406 struct usb_keyspan *remote = input_get_drvdata(dev);
Michael Downey99f83c92005-06-27 11:48:26 -0600407
Michael Downey99f83c92005-06-27 11:48:26 -0600408 remote->irq_urb->dev = remote->udev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500409 if (usb_submit_urb(remote->irq_urb, GFP_KERNEL))
Michael Downey99f83c92005-06-27 11:48:26 -0600410 return -EIO;
Michael Downey99f83c92005-06-27 11:48:26 -0600411
412 return 0;
413}
414
415static void keyspan_close(struct input_dev *dev)
416{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400417 struct usb_keyspan *remote = input_get_drvdata(dev);
Michael Downey99f83c92005-06-27 11:48:26 -0600418
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500419 usb_kill_urb(remote->irq_urb);
420}
421
422static struct usb_endpoint_descriptor *keyspan_get_in_endpoint(struct usb_host_interface *iface)
423{
424
425 struct usb_endpoint_descriptor *endpoint;
426 int i;
427
428 for (i = 0; i < iface->desc.bNumEndpoints; ++i) {
429 endpoint = &iface->endpoint[i].desc;
430
Luiz Fernando N. Capitulino96723192006-09-27 11:58:53 -0700431 if (usb_endpoint_is_int_in(endpoint)) {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500432 /* we found our interrupt in endpoint */
433 return endpoint;
434 }
435 }
436
437 return NULL;
Michael Downey99f83c92005-06-27 11:48:26 -0600438}
439
440/*
441 * Routine that sets up the driver to handle a specific USB device detected on the bus.
442 */
443static int keyspan_probe(struct usb_interface *interface, const struct usb_device_id *id)
444{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500445 struct usb_device *udev = interface_to_usbdev(interface);
Michael Downey99f83c92005-06-27 11:48:26 -0600446 struct usb_endpoint_descriptor *endpoint;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500447 struct usb_keyspan *remote;
448 struct input_dev *input_dev;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400449 int i, error;
Michael Downey99f83c92005-06-27 11:48:26 -0600450
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500451 endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
452 if (!endpoint)
453 return -ENODEV;
454
455 remote = kzalloc(sizeof(*remote), GFP_KERNEL);
456 input_dev = input_allocate_device();
457 if (!remote || !input_dev) {
Dmitry Torokhov50141862007-04-12 01:33:39 -0400458 error = -ENOMEM;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500459 goto fail1;
Michael Downey99f83c92005-06-27 11:48:26 -0600460 }
Michael Downey99f83c92005-06-27 11:48:26 -0600461
462 remote->udev = udev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500463 remote->input = input_dev;
Michael Downey99f83c92005-06-27 11:48:26 -0600464 remote->interface = interface;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500465 remote->in_endpoint = endpoint;
Michael Downey99f83c92005-06-27 11:48:26 -0600466 remote->toggle = -1; /* Set to -1 so we will always not match the toggle from the first remote message. */
467
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800468 remote->in_buffer = usb_buffer_alloc(udev, RECV_SIZE, GFP_ATOMIC, &remote->in_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500469 if (!remote->in_buffer) {
Dmitry Torokhov50141862007-04-12 01:33:39 -0400470 error = -ENOMEM;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500471 goto fail1;
Michael Downey99f83c92005-06-27 11:48:26 -0600472 }
473
474 remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
475 if (!remote->irq_urb) {
Dmitry Torokhov50141862007-04-12 01:33:39 -0400476 error = -ENOMEM;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500477 goto fail2;
Michael Downey99f83c92005-06-27 11:48:26 -0600478 }
479
Dmitry Torokhov50141862007-04-12 01:33:39 -0400480 error = keyspan_setup(udev);
481 if (error) {
482 error = -ENODEV;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500483 goto fail3;
Michael Downey99f83c92005-06-27 11:48:26 -0600484 }
485
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500486 if (udev->manufacturer)
487 strlcpy(remote->name, udev->manufacturer, sizeof(remote->name));
488
489 if (udev->product) {
490 if (udev->manufacturer)
491 strlcat(remote->name, " ", sizeof(remote->name));
492 strlcat(remote->name, udev->product, sizeof(remote->name));
Michael Downey99f83c92005-06-27 11:48:26 -0600493 }
494
Michael Downey99f83c92005-06-27 11:48:26 -0600495 if (!strlen(remote->name))
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500496 snprintf(remote->name, sizeof(remote->name),
497 "USB Keyspan Remote %04x:%04x",
498 le16_to_cpu(udev->descriptor.idVendor),
499 le16_to_cpu(udev->descriptor.idProduct));
Michael Downey99f83c92005-06-27 11:48:26 -0600500
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500501 usb_make_path(udev, remote->phys, sizeof(remote->phys));
502 strlcat(remote->phys, "/input0", sizeof(remote->phys));
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400503 memcpy(remote->keymap, keyspan_key_table, sizeof(remote->keymap));
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500504
505 input_dev->name = remote->name;
506 input_dev->phys = remote->phys;
507 usb_to_input_id(udev, &input_dev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400508 input_dev->dev.parent = &interface->dev;
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400509 input_dev->keycode = remote->keymap;
510 input_dev->keycodesize = sizeof(unsigned short);
511 input_dev->keycodemax = ARRAY_SIZE(remote->keymap);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500512
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400513 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
514 __set_bit(EV_KEY, input_dev->evbit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500515 for (i = 0; i < ARRAY_SIZE(keyspan_key_table); i++)
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400516 __set_bit(keyspan_key_table[i], input_dev->keybit);
517 __clear_bit(KEY_RESERVED, input_dev->keybit);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500518
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400519 input_set_drvdata(input_dev, remote);
520
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500521 input_dev->open = keyspan_open;
522 input_dev->close = keyspan_close;
Michael Downey99f83c92005-06-27 11:48:26 -0600523
524 /*
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400525 * Initialize the URB to access the device.
526 * The urb gets sent to the device in keyspan_open()
Michael Downey99f83c92005-06-27 11:48:26 -0600527 */
528 usb_fill_int_urb(remote->irq_urb,
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400529 remote->udev,
530 usb_rcvintpipe(remote->udev, endpoint->bEndpointAddress),
Michael Downey99f83c92005-06-27 11:48:26 -0600531 remote->in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
Dmitry Torokhov1953ea22007-11-04 00:41:24 -0400532 endpoint->bInterval);
Michael Downey99f83c92005-06-27 11:48:26 -0600533 remote->irq_urb->transfer_dma = remote->in_dma;
534 remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
535
536 /* we can register the device now, as it is ready */
Dmitry Torokhov50141862007-04-12 01:33:39 -0400537 error = input_register_device(remote->input);
538 if (error)
539 goto fail3;
Michael Downey99f83c92005-06-27 11:48:26 -0600540
541 /* save our data pointer in this interface device */
542 usb_set_intfdata(interface, remote);
543
Michael Downey99f83c92005-06-27 11:48:26 -0600544 return 0;
545
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500546 fail3: usb_free_urb(remote->irq_urb);
547 fail2: usb_buffer_free(udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
548 fail1: kfree(remote);
549 input_free_device(input_dev);
Michael Downey99f83c92005-06-27 11:48:26 -0600550
Dmitry Torokhov50141862007-04-12 01:33:39 -0400551 return error;
Michael Downey99f83c92005-06-27 11:48:26 -0600552}
553
554/*
555 * Routine called when a device is disconnected from the USB.
556 */
557static void keyspan_disconnect(struct usb_interface *interface)
558{
559 struct usb_keyspan *remote;
560
Michael Downey99f83c92005-06-27 11:48:26 -0600561 remote = usb_get_intfdata(interface);
562 usb_set_intfdata(interface, NULL);
563
564 if (remote) { /* We have a valid driver structure so clean up everything we allocated. */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500565 input_unregister_device(remote->input);
Michael Downey99f83c92005-06-27 11:48:26 -0600566 usb_kill_urb(remote->irq_urb);
567 usb_free_urb(remote->irq_urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500568 usb_buffer_free(remote->udev, RECV_SIZE, remote->in_buffer, remote->in_dma);
Michael Downey99f83c92005-06-27 11:48:26 -0600569 kfree(remote);
570 }
Michael Downey99f83c92005-06-27 11:48:26 -0600571}
572
573/*
574 * Standard driver set up sections
575 */
576static struct usb_driver keyspan_driver =
577{
Michael Downey99f83c92005-06-27 11:48:26 -0600578 .name = "keyspan_remote",
579 .probe = keyspan_probe,
580 .disconnect = keyspan_disconnect,
581 .id_table = keyspan_table
582};
583
584static int __init usb_keyspan_init(void)
585{
586 int result;
587
588 /* register this driver with the USB subsystem */
589 result = usb_register(&keyspan_driver);
590 if (result)
591 err("usb_register failed. Error number %d\n", result);
592
593 return result;
594}
595
596static void __exit usb_keyspan_exit(void)
597{
598 /* deregister this driver with the USB subsystem */
599 usb_deregister(&keyspan_driver);
600}
601
602module_init(usb_keyspan_init);
603module_exit(usb_keyspan_exit);
604
605MODULE_DEVICE_TABLE(usb, keyspan_table);
606MODULE_AUTHOR(DRIVER_AUTHOR);
607MODULE_DESCRIPTION(DRIVER_DESC);
608MODULE_LICENSE(DRIVER_LICENSE);