2 * ati_remote2 - ATI/Philips USB RF remote driver
4 * Copyright (C) 2005 Ville Syrjala <syrjala@sci.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
11 #include <linux/usb/input.h>
13 #define DRIVER_DESC "ATI/Philips USB RF remote driver"
14 #define DRIVER_VERSION "0.1"
16 MODULE_DESCRIPTION(DRIVER_DESC);
17 MODULE_VERSION(DRIVER_VERSION);
18 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
19 MODULE_LICENSE("GPL");
21 static unsigned int mode_mask = 0x1F;
22 module_param(mode_mask, uint, 0644);
23 MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
25 static struct usb_device_id ati_remote2_id_table[] = {
26 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
29 MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
34 } ati_remote2_key_table[] = {
47 { 0x10, KEY_VOLUMEUP },
48 { 0x11, KEY_VOLUMEDOWN },
49 { 0x20, KEY_CHANNELUP },
50 { 0x21, KEY_CHANNELDOWN },
51 { 0x28, KEY_FORWARD },
76 { 0xbe, KEY_QUESTION },
80 { (0x00 << 8) | 0x3f, KEY_PROG1 },
81 { (0x01 << 8) | 0x3f, KEY_PROG2 },
82 { (0x02 << 8) | 0x3f, KEY_PROG3 },
83 { (0x03 << 8) | 0x3f, KEY_PROG4 },
84 { (0x04 << 8) | 0x3f, KEY_PC },
89 struct input_dev *idev;
90 struct usb_device *udev;
92 struct usb_interface *intf[2];
93 struct usb_endpoint_descriptor *ep[2];
96 dma_addr_t buf_dma[2];
98 unsigned long jiffies;
105 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
106 static void ati_remote2_disconnect(struct usb_interface *interface);
108 static struct usb_driver ati_remote2_driver = {
109 .name = "ati_remote2",
110 .probe = ati_remote2_probe,
111 .disconnect = ati_remote2_disconnect,
112 .id_table = ati_remote2_id_table,
115 static int ati_remote2_open(struct input_dev *idev)
117 struct ati_remote2 *ar2 = idev->private;
120 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
122 dev_err(&ar2->intf[0]->dev,
123 "%s: usb_submit_urb() = %d\n", __FUNCTION__, r);
126 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
128 usb_kill_urb(ar2->urb[0]);
129 dev_err(&ar2->intf[1]->dev,
130 "%s: usb_submit_urb() = %d\n", __FUNCTION__, r);
137 static void ati_remote2_close(struct input_dev *idev)
139 struct ati_remote2 *ar2 = idev->private;
141 usb_kill_urb(ar2->urb[0]);
142 usb_kill_urb(ar2->urb[1]);
145 static void ati_remote2_input_mouse(struct ati_remote2 *ar2, struct pt_regs *regs)
147 struct input_dev *idev = ar2->idev;
148 u8 *data = ar2->buf[0];
151 dev_err(&ar2->intf[0]->dev,
152 "Unknown mode byte (%02x %02x %02x %02x)\n",
153 data[3], data[2], data[1], data[0]);
157 if (!((1 << data[0]) & mode_mask))
160 input_regs(idev, regs);
161 input_event(idev, EV_REL, REL_X, (s8) data[1]);
162 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
166 static int ati_remote2_lookup(unsigned int hw_code)
170 for (i = 0; ati_remote2_key_table[i].key_code != KEY_RESERVED; i++)
171 if (ati_remote2_key_table[i].hw_code == hw_code)
177 static void ati_remote2_input_key(struct ati_remote2 *ar2, struct pt_regs *regs)
179 struct input_dev *idev = ar2->idev;
180 u8 *data = ar2->buf[1];
184 dev_err(&ar2->intf[1]->dev,
185 "Unknown mode byte (%02x %02x %02x %02x)\n",
186 data[3], data[2], data[1], data[0]);
192 * Mode keys (AUX1-AUX4, PC) all generate the same code byte.
193 * Use the mode byte to figure out which one was pressed.
195 if (hw_code == 0x3f) {
197 * For some incomprehensible reason the mouse pad generates
198 * events which look identical to the events from the last
199 * pressed mode key. Naturally we don't want to generate key
200 * events for the mouse pad so we filter out any subsequent
201 * events from the same mode key.
203 if (ar2->mode == data[0])
209 hw_code |= data[0] << 8;
212 if (!((1 << data[0]) & mode_mask))
215 index = ati_remote2_lookup(hw_code);
217 dev_err(&ar2->intf[1]->dev,
218 "Unknown code byte (%02x %02x %02x %02x)\n",
219 data[3], data[2], data[1], data[0]);
224 case 0: /* release */
227 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
231 /* No repeat for mouse buttons. */
232 if (ati_remote2_key_table[index].key_code == BTN_LEFT ||
233 ati_remote2_key_table[index].key_code == BTN_RIGHT)
236 if (!time_after_eq(jiffies, ar2->jiffies))
239 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
242 dev_err(&ar2->intf[1]->dev,
243 "Unknown state byte (%02x %02x %02x %02x)\n",
244 data[3], data[2], data[1], data[0]);
248 input_regs(idev, regs);
249 input_event(idev, EV_KEY, ati_remote2_key_table[index].key_code, data[1]);
253 static void ati_remote2_complete_mouse(struct urb *urb, struct pt_regs *regs)
255 struct ati_remote2 *ar2 = urb->context;
258 switch (urb->status) {
260 ati_remote2_input_mouse(ar2, regs);
266 dev_dbg(&ar2->intf[0]->dev,
267 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
270 dev_err(&ar2->intf[0]->dev,
271 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
274 r = usb_submit_urb(urb, GFP_ATOMIC);
276 dev_err(&ar2->intf[0]->dev,
277 "%s(): usb_submit_urb() = %d\n", __FUNCTION__, r);
280 static void ati_remote2_complete_key(struct urb *urb, struct pt_regs *regs)
282 struct ati_remote2 *ar2 = urb->context;
285 switch (urb->status) {
287 ati_remote2_input_key(ar2, regs);
293 dev_dbg(&ar2->intf[1]->dev,
294 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
297 dev_err(&ar2->intf[1]->dev,
298 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
301 r = usb_submit_urb(urb, GFP_ATOMIC);
303 dev_err(&ar2->intf[1]->dev,
304 "%s(): usb_submit_urb() = %d\n", __FUNCTION__, r);
307 static int ati_remote2_input_init(struct ati_remote2 *ar2)
309 struct input_dev *idev;
312 idev = input_allocate_device();
319 idev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_REL);
320 idev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
321 idev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
322 for (i = 0; ati_remote2_key_table[i].key_code != KEY_RESERVED; i++)
323 set_bit(ati_remote2_key_table[i].key_code, idev->keybit);
325 idev->rep[REP_DELAY] = 250;
326 idev->rep[REP_PERIOD] = 33;
328 idev->open = ati_remote2_open;
329 idev->close = ati_remote2_close;
331 idev->name = ar2->name;
332 idev->phys = ar2->phys;
334 usb_to_input_id(ar2->udev, &idev->id);
335 idev->cdev.dev = &ar2->udev->dev;
337 i = input_register_device(idev);
339 input_free_device(idev);
344 static int ati_remote2_urb_init(struct ati_remote2 *ar2)
346 struct usb_device *udev = ar2->udev;
349 for (i = 0; i < 2; i++) {
350 ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
354 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
358 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
359 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
360 maxp = maxp > 4 ? 4 : maxp;
362 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
363 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
364 ar2, ar2->ep[i]->bInterval);
365 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
366 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
372 static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
376 for (i = 0; i < 2; i++) {
378 usb_free_urb(ar2->urb[i]);
381 usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
385 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
387 struct usb_device *udev = interface_to_usbdev(interface);
388 struct usb_host_interface *alt = interface->cur_altsetting;
389 struct ati_remote2 *ar2;
392 if (alt->desc.bInterfaceNumber)
395 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
401 ar2->intf[0] = interface;
402 ar2->ep[0] = &alt->endpoint[0].desc;
404 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
405 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
408 alt = ar2->intf[1]->cur_altsetting;
409 ar2->ep[1] = &alt->endpoint[0].desc;
411 r = ati_remote2_urb_init(ar2);
415 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
416 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
418 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
420 r = ati_remote2_input_init(ar2);
424 usb_set_intfdata(interface, ar2);
429 ati_remote2_urb_cleanup(ar2);
431 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
438 static void ati_remote2_disconnect(struct usb_interface *interface)
440 struct ati_remote2 *ar2;
441 struct usb_host_interface *alt = interface->cur_altsetting;
443 if (alt->desc.bInterfaceNumber)
446 ar2 = usb_get_intfdata(interface);
447 usb_set_intfdata(interface, NULL);
449 input_unregister_device(ar2->idev);
451 ati_remote2_urb_cleanup(ar2);
453 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
458 static int __init ati_remote2_init(void)
462 r = usb_register(&ati_remote2_driver);
464 printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
466 printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
471 static void __exit ati_remote2_exit(void)
473 usb_deregister(&ati_remote2_driver);
476 module_init(ati_remote2_init);
477 module_exit(ati_remote2_exit);