14 #include <linux/kernel.h>
15 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
21 #define DRIVER_VERSION "v0.1"
23 #define DRIVER_DESC "Driver for the USB Keyspan remote control."
24 #define DRIVER_LICENSE "GPL"
32 #define USB_KEYSPAN_VENDOR_ID 0x06CD
33 #define USB_KEYSPAN_PRODUCT_UIA11 0x0202
37 #define ZERO_MASK 0x1F
41 #define SYNC_MASK 0x3FFF
43 #define STOP_MASK 0x1F
53 static const unsigned short keyspan_key_table[] = {
130 static struct usb_driver keyspan_driver;
150 static int keyspan_load_tester(
struct usb_keyspan* dev,
int bits_needed)
152 if (dev->
data.bits_left >= bits_needed)
159 if (dev->
data.pos >= dev->
data.len) {
161 "%s - Error ran out of data. pos: %d, len: %d\n",
162 __func__, dev->
data.pos, dev->
data.len);
167 while ((dev->
data.bits_left + 7 < (
sizeof(dev->
data.tester) * 8)) &&
169 dev->
data.tester += (dev->
data.buffer[dev->
data.pos++] << dev->
data.bits_left);
170 dev->
data.bits_left += 8;
176 static void keyspan_report_button(
struct usb_keyspan *remote,
int button,
int press)
181 input_report_key(input, remote->
keymap[button], press);
188 static void keyspan_check_data(
struct usb_keyspan *remote)
194 switch(remote->
stage) {
205 remote->
data.pos = 0;
206 remote->
data.tester = 0;
207 remote->
data.bits_left = 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) {
229 remote->
data.tester = remote->
data.tester >> 14;
230 remote->
data.bits_left -= 14;
234 remote->
data.tester = remote->
data.tester >> 1;
235 --remote->
data.bits_left;
242 remote->
data.len = 0;
258 for (i = 0; i < 9; i++) {
259 keyspan_load_tester(remote, 6);
263 remote->
data.tester = remote->
data.tester >> 5;
264 remote->
data.bits_left -= 5;
267 remote->
data.tester = remote->
data.tester >> 6;
268 remote->
data.bits_left -= 6;
271 "%s - Unknown sequence found in system data.\n",
279 for (i = 0; i < 5; i++) {
280 keyspan_load_tester(remote, 6);
284 remote->
data.tester = remote->
data.tester >> 5;
285 remote->
data.bits_left -= 5;
288 remote->
data.tester = remote->
data.tester >> 6;
289 remote->
data.bits_left -= 6;
292 "%s - Unknown sequence found in button data.\n",
299 keyspan_load_tester(remote, 6);
302 remote->
data.tester = remote->
data.tester >> 5;
303 remote->
data.bits_left -= 5;
306 remote->
data.tester = remote->
data.tester >> 6;
307 remote->
data.bits_left -= 6;
310 "%s - Error in message, invalid toggle.\n",
316 keyspan_load_tester(remote, 5);
318 remote->
data.tester = remote->
data.tester >> 5;
319 remote->
data.bits_left -= 5;
322 "Bad message received, no stop bit found.\n");
326 "%s found valid message: system: %d, button: %d, toggle: %d\n",
330 keyspan_report_button(remote,
message.button, 1);
331 keyspan_report_button(remote,
message.button, 0);
343 static int keyspan_setup(
struct usb_device* dev)
348 0x11, 0x40, 0x5601, 0x0,
NULL, 0, 0);
350 dev_dbg(&dev->dev,
"%s - failed to set bit rate due to error: %d\n",
356 0x44, 0x40, 0x0, 0x0,
NULL, 0, 0);
358 dev_dbg(&dev->dev,
"%s - failed to set resume sensitivity due to error: %d\n",
364 0x22, 0x40, 0x0, 0x0,
NULL, 0, 0);
366 dev_dbg(&dev->dev,
"%s - failed to turn receive on due to error: %d\n",
371 dev_dbg(&dev->dev,
"%s - Setup complete.\n", __func__);
378 static void keyspan_irq_recv(
struct urb *
urb)
384 switch (urb->status) {
402 keyspan_check_data(dev);
408 "%s - usb_submit_urb failed with result: %d\n",
412 static int keyspan_open(
struct input_dev *dev)
414 struct usb_keyspan *remote = input_get_drvdata(dev);
423 static void keyspan_close(
struct input_dev *dev)
425 struct usb_keyspan *remote = input_get_drvdata(dev);
436 for (i = 0; i < iface->desc.bNumEndpoints; ++
i) {
437 endpoint = &iface->endpoint[
i].desc;
439 if (usb_endpoint_is_int_in(endpoint)) {
453 struct usb_device *
udev = interface_to_usbdev(interface);
456 struct input_dev *input_dev;
459 endpoint = keyspan_get_in_endpoint(interface->cur_altsetting);
463 remote = kzalloc(
sizeof(*remote),
GFP_KERNEL);
464 input_dev = input_allocate_device();
465 if (!remote || !input_dev) {
471 remote->
input = input_dev;
488 error = keyspan_setup(udev);
494 if (udev->manufacturer)
498 if (udev->manufacturer)
505 "USB Keyspan Remote %04x:%04x",
509 usb_make_path(udev, remote->
phys,
sizeof(remote->
phys));
513 input_dev->name = remote->
name;
514 input_dev->phys = remote->
phys;
515 usb_to_input_id(udev, &input_dev->id);
516 input_dev->dev.parent = &interface->dev;
517 input_dev->keycode = remote->
keymap;
518 input_dev->keycodesize =
sizeof(
unsigned short);
523 for (i = 0; i <
ARRAY_SIZE(keyspan_key_table); i++)
524 __set_bit(keyspan_key_table[i], input_dev->keybit);
527 input_set_drvdata(input_dev, remote);
529 input_dev->open = keyspan_open;
530 input_dev->close = keyspan_close;
536 usb_fill_int_urb(remote->
irq_urb,
539 remote->
in_buffer, RECV_SIZE, keyspan_irq_recv, remote,
542 remote->
irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
545 error = input_register_device(remote->
input);
550 usb_set_intfdata(interface, remote);
556 fail1:
kfree(remote);
557 input_free_device(input_dev);
565 static void keyspan_disconnect(
struct usb_interface *interface)
569 remote = usb_get_intfdata(interface);
570 usb_set_intfdata(interface,
NULL);
573 input_unregister_device(remote->
input);
584 static struct usb_driver keyspan_driver =
586 .name =
"keyspan_remote",
587 .probe = keyspan_probe,
588 .disconnect = keyspan_disconnect,
589 .id_table = keyspan_table