2 * ati_remote2 - ATI/Philips USB RF remote driver
4 * Copyright (C) 2005 Ville Syrjala <syrjala@sci.fi>
5 * Copyright (C) 2007 Peter Stokes <linux@dadeos.freeserve.co.uk>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
12 #include <linux/usb/input.h>
14 #define DRIVER_DESC "ATI/Philips USB RF remote driver"
15 #define DRIVER_VERSION "0.2"
17 MODULE_DESCRIPTION(DRIVER_DESC
);
18 MODULE_VERSION(DRIVER_VERSION
);
19 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
20 MODULE_LICENSE("GPL");
23 * ATI Remote Wonder II Channel Configuration
25 * The remote control can by assigned one of sixteen "channels" in order to facilitate
26 * the use of multiple remote controls within range of each other.
27 * A remote's "channel" may be altered by pressing and holding the "PC" button for
28 * approximately 3 seconds, after which the button will slowly flash the count of the
29 * currently configured "channel", using the numeric keypad enter a number between 1 and
30 * 16 and then the "PC" button again, the button will slowly flash the count of the
31 * newly configured "channel".
34 static unsigned int channel_mask
= 0xFFFF;
35 module_param(channel_mask
, uint
, 0644);
36 MODULE_PARM_DESC(channel_mask
, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
38 static unsigned int mode_mask
= 0x1F;
39 module_param(mode_mask
, uint
, 0644);
40 MODULE_PARM_DESC(mode_mask
, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
42 static struct usb_device_id ati_remote2_id_table
[] = {
43 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
46 MODULE_DEVICE_TABLE(usb
, ati_remote2_id_table
);
51 } ati_remote2_key_table
[] = {
64 { 0x10, KEY_VOLUMEUP
},
65 { 0x11, KEY_VOLUMEDOWN
},
66 { 0x20, KEY_CHANNELUP
},
67 { 0x21, KEY_CHANNELDOWN
},
68 { 0x28, KEY_FORWARD
},
93 { 0xbe, KEY_QUESTION
},
97 { (0x00 << 8) | 0x3f, KEY_PROG1
},
98 { (0x01 << 8) | 0x3f, KEY_PROG2
},
99 { (0x02 << 8) | 0x3f, KEY_PROG3
},
100 { (0x03 << 8) | 0x3f, KEY_PROG4
},
101 { (0x04 << 8) | 0x3f, KEY_PC
},
106 struct input_dev
*idev
;
107 struct usb_device
*udev
;
109 struct usb_interface
*intf
[2];
110 struct usb_endpoint_descriptor
*ep
[2];
113 dma_addr_t buf_dma
[2];
115 unsigned long jiffies
;
122 static int ati_remote2_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
);
123 static void ati_remote2_disconnect(struct usb_interface
*interface
);
125 static struct usb_driver ati_remote2_driver
= {
126 .name
= "ati_remote2",
127 .probe
= ati_remote2_probe
,
128 .disconnect
= ati_remote2_disconnect
,
129 .id_table
= ati_remote2_id_table
,
132 static int ati_remote2_open(struct input_dev
*idev
)
134 struct ati_remote2
*ar2
= input_get_drvdata(idev
);
137 r
= usb_submit_urb(ar2
->urb
[0], GFP_KERNEL
);
139 dev_err(&ar2
->intf
[0]->dev
,
140 "%s: usb_submit_urb() = %d\n", __FUNCTION__
, r
);
143 r
= usb_submit_urb(ar2
->urb
[1], GFP_KERNEL
);
145 usb_kill_urb(ar2
->urb
[0]);
146 dev_err(&ar2
->intf
[1]->dev
,
147 "%s: usb_submit_urb() = %d\n", __FUNCTION__
, r
);
154 static void ati_remote2_close(struct input_dev
*idev
)
156 struct ati_remote2
*ar2
= input_get_drvdata(idev
);
158 usb_kill_urb(ar2
->urb
[0]);
159 usb_kill_urb(ar2
->urb
[1]);
162 static void ati_remote2_input_mouse(struct ati_remote2
*ar2
)
164 struct input_dev
*idev
= ar2
->idev
;
165 u8
*data
= ar2
->buf
[0];
168 channel
= data
[0] >> 4;
170 if (!((1 << channel
) & channel_mask
))
173 mode
= data
[0] & 0x0F;
176 dev_err(&ar2
->intf
[0]->dev
,
177 "Unknown mode byte (%02x %02x %02x %02x)\n",
178 data
[3], data
[2], data
[1], data
[0]);
182 if (!((1 << mode
) & mode_mask
))
185 input_event(idev
, EV_REL
, REL_X
, (s8
) data
[1]);
186 input_event(idev
, EV_REL
, REL_Y
, (s8
) data
[2]);
190 static int ati_remote2_lookup(unsigned int hw_code
)
194 for (i
= 0; ati_remote2_key_table
[i
].key_code
!= KEY_RESERVED
; i
++)
195 if (ati_remote2_key_table
[i
].hw_code
== hw_code
)
201 static void ati_remote2_input_key(struct ati_remote2
*ar2
)
203 struct input_dev
*idev
= ar2
->idev
;
204 u8
*data
= ar2
->buf
[1];
205 int channel
, mode
, hw_code
, index
;
207 channel
= data
[0] >> 4;
209 if (!((1 << channel
) & channel_mask
))
212 mode
= data
[0] & 0x0F;
215 dev_err(&ar2
->intf
[1]->dev
,
216 "Unknown mode byte (%02x %02x %02x %02x)\n",
217 data
[3], data
[2], data
[1], data
[0]);
223 * Mode keys (AUX1-AUX4, PC) all generate the same code byte.
224 * Use the mode byte to figure out which one was pressed.
226 if (hw_code
== 0x3f) {
228 * For some incomprehensible reason the mouse pad generates
229 * events which look identical to the events from the last
230 * pressed mode key. Naturally we don't want to generate key
231 * events for the mouse pad so we filter out any subsequent
232 * events from the same mode key.
234 if (ar2
->mode
== mode
)
240 hw_code
|= mode
<< 8;
243 if (!((1 << mode
) & mode_mask
))
246 index
= ati_remote2_lookup(hw_code
);
248 dev_err(&ar2
->intf
[1]->dev
,
249 "Unknown code byte (%02x %02x %02x %02x)\n",
250 data
[3], data
[2], data
[1], data
[0]);
255 case 0: /* release */
258 ar2
->jiffies
= jiffies
+ msecs_to_jiffies(idev
->rep
[REP_DELAY
]);
262 /* No repeat for mouse buttons. */
263 if (ati_remote2_key_table
[index
].key_code
== BTN_LEFT
||
264 ati_remote2_key_table
[index
].key_code
== BTN_RIGHT
)
267 if (!time_after_eq(jiffies
, ar2
->jiffies
))
270 ar2
->jiffies
= jiffies
+ msecs_to_jiffies(idev
->rep
[REP_PERIOD
]);
273 dev_err(&ar2
->intf
[1]->dev
,
274 "Unknown state byte (%02x %02x %02x %02x)\n",
275 data
[3], data
[2], data
[1], data
[0]);
279 input_event(idev
, EV_KEY
, ati_remote2_key_table
[index
].key_code
, data
[1]);
283 static void ati_remote2_complete_mouse(struct urb
*urb
)
285 struct ati_remote2
*ar2
= urb
->context
;
288 switch (urb
->status
) {
290 ati_remote2_input_mouse(ar2
);
296 dev_dbg(&ar2
->intf
[0]->dev
,
297 "%s(): urb status = %d\n", __FUNCTION__
, urb
->status
);
300 dev_err(&ar2
->intf
[0]->dev
,
301 "%s(): urb status = %d\n", __FUNCTION__
, urb
->status
);
304 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
306 dev_err(&ar2
->intf
[0]->dev
,
307 "%s(): usb_submit_urb() = %d\n", __FUNCTION__
, r
);
310 static void ati_remote2_complete_key(struct urb
*urb
)
312 struct ati_remote2
*ar2
= urb
->context
;
315 switch (urb
->status
) {
317 ati_remote2_input_key(ar2
);
323 dev_dbg(&ar2
->intf
[1]->dev
,
324 "%s(): urb status = %d\n", __FUNCTION__
, urb
->status
);
327 dev_err(&ar2
->intf
[1]->dev
,
328 "%s(): urb status = %d\n", __FUNCTION__
, urb
->status
);
331 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
333 dev_err(&ar2
->intf
[1]->dev
,
334 "%s(): usb_submit_urb() = %d\n", __FUNCTION__
, r
);
337 static int ati_remote2_input_init(struct ati_remote2
*ar2
)
339 struct input_dev
*idev
;
342 idev
= input_allocate_device();
347 input_set_drvdata(idev
, ar2
);
349 idev
->evbit
[0] = BIT(EV_KEY
) | BIT(EV_REP
) | BIT(EV_REL
);
350 idev
->keybit
[LONG(BTN_MOUSE
)] = BIT(BTN_LEFT
) | BIT(BTN_RIGHT
);
351 idev
->relbit
[0] = BIT(REL_X
) | BIT(REL_Y
);
352 for (i
= 0; ati_remote2_key_table
[i
].key_code
!= KEY_RESERVED
; i
++)
353 set_bit(ati_remote2_key_table
[i
].key_code
, idev
->keybit
);
355 idev
->rep
[REP_DELAY
] = 250;
356 idev
->rep
[REP_PERIOD
] = 33;
358 idev
->open
= ati_remote2_open
;
359 idev
->close
= ati_remote2_close
;
361 idev
->name
= ar2
->name
;
362 idev
->phys
= ar2
->phys
;
364 usb_to_input_id(ar2
->udev
, &idev
->id
);
365 idev
->dev
.parent
= &ar2
->udev
->dev
;
367 retval
= input_register_device(idev
);
369 input_free_device(idev
);
374 static int ati_remote2_urb_init(struct ati_remote2
*ar2
)
376 struct usb_device
*udev
= ar2
->udev
;
379 for (i
= 0; i
< 2; i
++) {
380 ar2
->buf
[i
] = usb_buffer_alloc(udev
, 4, GFP_KERNEL
, &ar2
->buf_dma
[i
]);
384 ar2
->urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
388 pipe
= usb_rcvintpipe(udev
, ar2
->ep
[i
]->bEndpointAddress
);
389 maxp
= usb_maxpacket(udev
, pipe
, usb_pipeout(pipe
));
390 maxp
= maxp
> 4 ? 4 : maxp
;
392 usb_fill_int_urb(ar2
->urb
[i
], udev
, pipe
, ar2
->buf
[i
], maxp
,
393 i
? ati_remote2_complete_key
: ati_remote2_complete_mouse
,
394 ar2
, ar2
->ep
[i
]->bInterval
);
395 ar2
->urb
[i
]->transfer_dma
= ar2
->buf_dma
[i
];
396 ar2
->urb
[i
]->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
402 static void ati_remote2_urb_cleanup(struct ati_remote2
*ar2
)
406 for (i
= 0; i
< 2; i
++) {
407 usb_free_urb(ar2
->urb
[i
]);
408 usb_buffer_free(ar2
->udev
, 4, ar2
->buf
[i
], ar2
->buf_dma
[i
]);
412 static int ati_remote2_setup(struct ati_remote2
*ar2
)
417 * Configure receiver to only accept input from remote "channel"
418 * channel == 0 -> Accept input from any remote channel
419 * channel == 1 -> Only accept input from remote channel 1
420 * channel == 2 -> Only accept input from remote channel 2
422 * channel == 16 -> Only accept input from remote channel 16
426 for (i
= 0; i
< 16; i
++) {
427 if ((1 << i
) & channel_mask
) {
428 if (!(~(1 << i
) & 0xFFFF & channel_mask
))
434 r
= usb_control_msg(ar2
->udev
, usb_sndctrlpipe(ar2
->udev
, 0),
436 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
437 channel
, 0x0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
439 dev_err(&ar2
->udev
->dev
, "%s - failed to set channel due to error: %d\n",
447 static int ati_remote2_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
449 struct usb_device
*udev
= interface_to_usbdev(interface
);
450 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
451 struct ati_remote2
*ar2
;
454 if (alt
->desc
.bInterfaceNumber
)
457 ar2
= kzalloc(sizeof (struct ati_remote2
), GFP_KERNEL
);
463 ar2
->intf
[0] = interface
;
464 ar2
->ep
[0] = &alt
->endpoint
[0].desc
;
466 ar2
->intf
[1] = usb_ifnum_to_if(udev
, 1);
467 r
= usb_driver_claim_interface(&ati_remote2_driver
, ar2
->intf
[1], ar2
);
470 alt
= ar2
->intf
[1]->cur_altsetting
;
471 ar2
->ep
[1] = &alt
->endpoint
[0].desc
;
473 r
= ati_remote2_urb_init(ar2
);
477 r
= ati_remote2_setup(ar2
);
481 usb_make_path(udev
, ar2
->phys
, sizeof(ar2
->phys
));
482 strlcat(ar2
->phys
, "/input0", sizeof(ar2
->phys
));
484 strlcat(ar2
->name
, "ATI Remote Wonder II", sizeof(ar2
->name
));
486 r
= ati_remote2_input_init(ar2
);
490 usb_set_intfdata(interface
, ar2
);
495 ati_remote2_urb_cleanup(ar2
);
497 usb_driver_release_interface(&ati_remote2_driver
, ar2
->intf
[1]);
504 static void ati_remote2_disconnect(struct usb_interface
*interface
)
506 struct ati_remote2
*ar2
;
507 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
509 if (alt
->desc
.bInterfaceNumber
)
512 ar2
= usb_get_intfdata(interface
);
513 usb_set_intfdata(interface
, NULL
);
515 input_unregister_device(ar2
->idev
);
517 ati_remote2_urb_cleanup(ar2
);
519 usb_driver_release_interface(&ati_remote2_driver
, ar2
->intf
[1]);
524 static int __init
ati_remote2_init(void)
528 r
= usb_register(&ati_remote2_driver
);
530 printk(KERN_ERR
"ati_remote2: usb_register() = %d\n", r
);
532 printk(KERN_INFO
"ati_remote2: " DRIVER_DESC
" " DRIVER_VERSION
"\n");
537 static void __exit
ati_remote2_exit(void)
539 usb_deregister(&ati_remote2_driver
);
542 module_init(ati_remote2_init
);
543 module_exit(ati_remote2_exit
);