2 * ati_remote2 - ATI/Philips USB RF remote driver
4 * Copyright (C) 2005-2008 Ville Syrjala <syrjala@sci.fi>
5 * Copyright (C) 2007-2008 Peter Stokes <linux@dadeos.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.3"
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 press 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
);
48 static DEFINE_MUTEX(ati_remote2_mutex
);
51 ATI_REMOTE2_OPENED
= 0x1,
52 ATI_REMOTE2_SUSPENDED
= 0x2,
67 } ati_remote2_key_table
[] = {
80 { 0x10, KEY_VOLUMEUP
},
81 { 0x11, KEY_VOLUMEDOWN
},
82 { 0x20, KEY_CHANNELUP
},
83 { 0x21, KEY_CHANNELDOWN
},
84 { 0x28, KEY_FORWARD
},
92 { 0x3f, KEY_PROG1
}, /* AUX1-AUX4 and PC */
106 { 0x8e, KEY_VENDOR
},
107 { 0x96, KEY_COFFEE
},
110 { 0xbe, KEY_QUESTION
},
117 struct input_dev
*idev
;
118 struct usb_device
*udev
;
120 struct usb_interface
*intf
[2];
121 struct usb_endpoint_descriptor
*ep
[2];
124 dma_addr_t buf_dma
[2];
126 unsigned long jiffies
;
132 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
133 u16 keycode
[ATI_REMOTE2_MODES
][ARRAY_SIZE(ati_remote2_key_table
)];
138 static int ati_remote2_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
);
139 static void ati_remote2_disconnect(struct usb_interface
*interface
);
140 static int ati_remote2_suspend(struct usb_interface
*interface
, pm_message_t message
);
141 static int ati_remote2_resume(struct usb_interface
*interface
);
142 static int ati_remote2_reset_resume(struct usb_interface
*interface
);
143 static int ati_remote2_pre_reset(struct usb_interface
*interface
);
144 static int ati_remote2_post_reset(struct usb_interface
*interface
);
146 static struct usb_driver ati_remote2_driver
= {
147 .name
= "ati_remote2",
148 .probe
= ati_remote2_probe
,
149 .disconnect
= ati_remote2_disconnect
,
150 .id_table
= ati_remote2_id_table
,
151 .suspend
= ati_remote2_suspend
,
152 .resume
= ati_remote2_resume
,
153 .reset_resume
= ati_remote2_reset_resume
,
154 .pre_reset
= ati_remote2_pre_reset
,
155 .post_reset
= ati_remote2_post_reset
,
156 .supports_autosuspend
= 1,
159 static int ati_remote2_submit_urbs(struct ati_remote2
*ar2
)
163 r
= usb_submit_urb(ar2
->urb
[0], GFP_KERNEL
);
165 dev_err(&ar2
->intf
[0]->dev
,
166 "%s(): usb_submit_urb() = %d\n", __func__
, r
);
169 r
= usb_submit_urb(ar2
->urb
[1], GFP_KERNEL
);
171 usb_kill_urb(ar2
->urb
[0]);
172 dev_err(&ar2
->intf
[1]->dev
,
173 "%s(): usb_submit_urb() = %d\n", __func__
, r
);
180 static void ati_remote2_kill_urbs(struct ati_remote2
*ar2
)
182 usb_kill_urb(ar2
->urb
[1]);
183 usb_kill_urb(ar2
->urb
[0]);
186 static int ati_remote2_open(struct input_dev
*idev
)
188 struct ati_remote2
*ar2
= input_get_drvdata(idev
);
191 dev_dbg(&ar2
->intf
[0]->dev
, "%s()\n", __func__
);
193 r
= usb_autopm_get_interface(ar2
->intf
[0]);
195 dev_err(&ar2
->intf
[0]->dev
,
196 "%s(): usb_autopm_get_interface() = %d\n", __func__
, r
);
200 mutex_lock(&ati_remote2_mutex
);
202 if (!(ar2
->flags
& ATI_REMOTE2_SUSPENDED
)) {
203 r
= ati_remote2_submit_urbs(ar2
);
208 ar2
->flags
|= ATI_REMOTE2_OPENED
;
210 mutex_unlock(&ati_remote2_mutex
);
212 usb_autopm_put_interface(ar2
->intf
[0]);
217 mutex_unlock(&ati_remote2_mutex
);
218 usb_autopm_put_interface(ar2
->intf
[0]);
223 static void ati_remote2_close(struct input_dev
*idev
)
225 struct ati_remote2
*ar2
= input_get_drvdata(idev
);
227 dev_dbg(&ar2
->intf
[0]->dev
, "%s()\n", __func__
);
229 mutex_lock(&ati_remote2_mutex
);
231 if (!(ar2
->flags
& ATI_REMOTE2_SUSPENDED
))
232 ati_remote2_kill_urbs(ar2
);
234 ar2
->flags
&= ~ATI_REMOTE2_OPENED
;
236 mutex_unlock(&ati_remote2_mutex
);
239 static void ati_remote2_input_mouse(struct ati_remote2
*ar2
)
241 struct input_dev
*idev
= ar2
->idev
;
242 u8
*data
= ar2
->buf
[0];
245 channel
= data
[0] >> 4;
247 if (!((1 << channel
) & channel_mask
))
250 mode
= data
[0] & 0x0F;
252 if (mode
> ATI_REMOTE2_PC
) {
253 dev_err(&ar2
->intf
[0]->dev
,
254 "Unknown mode byte (%02x %02x %02x %02x)\n",
255 data
[3], data
[2], data
[1], data
[0]);
259 if (!((1 << mode
) & mode_mask
))
262 input_event(idev
, EV_REL
, REL_X
, (s8
) data
[1]);
263 input_event(idev
, EV_REL
, REL_Y
, (s8
) data
[2]);
267 static int ati_remote2_lookup(unsigned int hw_code
)
271 for (i
= 0; i
< ARRAY_SIZE(ati_remote2_key_table
); i
++)
272 if (ati_remote2_key_table
[i
].hw_code
== hw_code
)
278 static void ati_remote2_input_key(struct ati_remote2
*ar2
)
280 struct input_dev
*idev
= ar2
->idev
;
281 u8
*data
= ar2
->buf
[1];
282 int channel
, mode
, hw_code
, index
;
284 channel
= data
[0] >> 4;
286 if (!((1 << channel
) & channel_mask
))
289 mode
= data
[0] & 0x0F;
291 if (mode
> ATI_REMOTE2_PC
) {
292 dev_err(&ar2
->intf
[1]->dev
,
293 "Unknown mode byte (%02x %02x %02x %02x)\n",
294 data
[3], data
[2], data
[1], data
[0]);
299 if (hw_code
== 0x3f) {
301 * For some incomprehensible reason the mouse pad generates
302 * events which look identical to the events from the last
303 * pressed mode key. Naturally we don't want to generate key
304 * events for the mouse pad so we filter out any subsequent
305 * events from the same mode key.
307 if (ar2
->mode
== mode
)
314 if (!((1 << mode
) & mode_mask
))
317 index
= ati_remote2_lookup(hw_code
);
319 dev_err(&ar2
->intf
[1]->dev
,
320 "Unknown code byte (%02x %02x %02x %02x)\n",
321 data
[3], data
[2], data
[1], data
[0]);
326 case 0: /* release */
329 ar2
->jiffies
= jiffies
+ msecs_to_jiffies(idev
->rep
[REP_DELAY
]);
333 /* No repeat for mouse buttons. */
334 if (ar2
->keycode
[mode
][index
] == BTN_LEFT
||
335 ar2
->keycode
[mode
][index
] == BTN_RIGHT
)
338 if (!time_after_eq(jiffies
, ar2
->jiffies
))
341 ar2
->jiffies
= jiffies
+ msecs_to_jiffies(idev
->rep
[REP_PERIOD
]);
344 dev_err(&ar2
->intf
[1]->dev
,
345 "Unknown state byte (%02x %02x %02x %02x)\n",
346 data
[3], data
[2], data
[1], data
[0]);
350 input_event(idev
, EV_KEY
, ar2
->keycode
[mode
][index
], data
[1]);
354 static void ati_remote2_complete_mouse(struct urb
*urb
)
356 struct ati_remote2
*ar2
= urb
->context
;
359 switch (urb
->status
) {
361 usb_mark_last_busy(ar2
->udev
);
362 ati_remote2_input_mouse(ar2
);
368 dev_dbg(&ar2
->intf
[0]->dev
,
369 "%s(): urb status = %d\n", __func__
, urb
->status
);
372 usb_mark_last_busy(ar2
->udev
);
373 dev_err(&ar2
->intf
[0]->dev
,
374 "%s(): urb status = %d\n", __func__
, urb
->status
);
377 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
379 dev_err(&ar2
->intf
[0]->dev
,
380 "%s(): usb_submit_urb() = %d\n", __func__
, r
);
383 static void ati_remote2_complete_key(struct urb
*urb
)
385 struct ati_remote2
*ar2
= urb
->context
;
388 switch (urb
->status
) {
390 usb_mark_last_busy(ar2
->udev
);
391 ati_remote2_input_key(ar2
);
397 dev_dbg(&ar2
->intf
[1]->dev
,
398 "%s(): urb status = %d\n", __func__
, urb
->status
);
401 usb_mark_last_busy(ar2
->udev
);
402 dev_err(&ar2
->intf
[1]->dev
,
403 "%s(): urb status = %d\n", __func__
, urb
->status
);
406 r
= usb_submit_urb(urb
, GFP_ATOMIC
);
408 dev_err(&ar2
->intf
[1]->dev
,
409 "%s(): usb_submit_urb() = %d\n", __func__
, r
);
412 static int ati_remote2_getkeycode(struct input_dev
*idev
,
413 int scancode
, int *keycode
)
415 struct ati_remote2
*ar2
= input_get_drvdata(idev
);
418 mode
= scancode
>> 8;
419 if (mode
> ATI_REMOTE2_PC
|| !((1 << mode
) & mode_mask
))
422 index
= ati_remote2_lookup(scancode
& 0xFF);
426 *keycode
= ar2
->keycode
[mode
][index
];
430 static int ati_remote2_setkeycode(struct input_dev
*idev
, int scancode
, int keycode
)
432 struct ati_remote2
*ar2
= input_get_drvdata(idev
);
433 int index
, mode
, old_keycode
;
435 mode
= scancode
>> 8;
436 if (mode
> ATI_REMOTE2_PC
|| !((1 << mode
) & mode_mask
))
439 index
= ati_remote2_lookup(scancode
& 0xFF);
443 if (keycode
< KEY_RESERVED
|| keycode
> KEY_MAX
)
446 old_keycode
= ar2
->keycode
[mode
][index
];
447 ar2
->keycode
[mode
][index
] = keycode
;
448 set_bit(keycode
, idev
->keybit
);
450 for (mode
= 0; mode
< ATI_REMOTE2_MODES
; mode
++) {
451 for (index
= 0; index
< ARRAY_SIZE(ati_remote2_key_table
); index
++) {
452 if (ar2
->keycode
[mode
][index
] == old_keycode
)
457 clear_bit(old_keycode
, idev
->keybit
);
462 static int ati_remote2_input_init(struct ati_remote2
*ar2
)
464 struct input_dev
*idev
;
465 int index
, mode
, retval
;
467 idev
= input_allocate_device();
472 input_set_drvdata(idev
, ar2
);
474 idev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REP
) | BIT_MASK(EV_REL
);
475 idev
->keybit
[BIT_WORD(BTN_MOUSE
)] = BIT_MASK(BTN_LEFT
) |
477 idev
->relbit
[0] = BIT_MASK(REL_X
) | BIT_MASK(REL_Y
);
479 for (mode
= 0; mode
< ATI_REMOTE2_MODES
; mode
++) {
480 for (index
= 0; index
< ARRAY_SIZE(ati_remote2_key_table
); index
++) {
481 ar2
->keycode
[mode
][index
] = ati_remote2_key_table
[index
].keycode
;
482 set_bit(ar2
->keycode
[mode
][index
], idev
->keybit
);
486 /* AUX1-AUX4 and PC generate the same scancode. */
487 index
= ati_remote2_lookup(0x3f);
488 ar2
->keycode
[ATI_REMOTE2_AUX1
][index
] = KEY_PROG1
;
489 ar2
->keycode
[ATI_REMOTE2_AUX2
][index
] = KEY_PROG2
;
490 ar2
->keycode
[ATI_REMOTE2_AUX3
][index
] = KEY_PROG3
;
491 ar2
->keycode
[ATI_REMOTE2_AUX4
][index
] = KEY_PROG4
;
492 ar2
->keycode
[ATI_REMOTE2_PC
][index
] = KEY_PC
;
493 set_bit(KEY_PROG1
, idev
->keybit
);
494 set_bit(KEY_PROG2
, idev
->keybit
);
495 set_bit(KEY_PROG3
, idev
->keybit
);
496 set_bit(KEY_PROG4
, idev
->keybit
);
497 set_bit(KEY_PC
, idev
->keybit
);
499 idev
->rep
[REP_DELAY
] = 250;
500 idev
->rep
[REP_PERIOD
] = 33;
502 idev
->open
= ati_remote2_open
;
503 idev
->close
= ati_remote2_close
;
505 idev
->getkeycode
= ati_remote2_getkeycode
;
506 idev
->setkeycode
= ati_remote2_setkeycode
;
508 idev
->name
= ar2
->name
;
509 idev
->phys
= ar2
->phys
;
511 usb_to_input_id(ar2
->udev
, &idev
->id
);
512 idev
->dev
.parent
= &ar2
->udev
->dev
;
514 retval
= input_register_device(idev
);
516 input_free_device(idev
);
521 static int ati_remote2_urb_init(struct ati_remote2
*ar2
)
523 struct usb_device
*udev
= ar2
->udev
;
526 for (i
= 0; i
< 2; i
++) {
527 ar2
->buf
[i
] = usb_buffer_alloc(udev
, 4, GFP_KERNEL
, &ar2
->buf_dma
[i
]);
531 ar2
->urb
[i
] = usb_alloc_urb(0, GFP_KERNEL
);
535 pipe
= usb_rcvintpipe(udev
, ar2
->ep
[i
]->bEndpointAddress
);
536 maxp
= usb_maxpacket(udev
, pipe
, usb_pipeout(pipe
));
537 maxp
= maxp
> 4 ? 4 : maxp
;
539 usb_fill_int_urb(ar2
->urb
[i
], udev
, pipe
, ar2
->buf
[i
], maxp
,
540 i
? ati_remote2_complete_key
: ati_remote2_complete_mouse
,
541 ar2
, ar2
->ep
[i
]->bInterval
);
542 ar2
->urb
[i
]->transfer_dma
= ar2
->buf_dma
[i
];
543 ar2
->urb
[i
]->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
549 static void ati_remote2_urb_cleanup(struct ati_remote2
*ar2
)
553 for (i
= 0; i
< 2; i
++) {
554 usb_free_urb(ar2
->urb
[i
]);
555 usb_buffer_free(ar2
->udev
, 4, ar2
->buf
[i
], ar2
->buf_dma
[i
]);
559 static int ati_remote2_setup(struct ati_remote2
*ar2
)
564 * Configure receiver to only accept input from remote "channel"
565 * channel == 0 -> Accept input from any remote channel
566 * channel == 1 -> Only accept input from remote channel 1
567 * channel == 2 -> Only accept input from remote channel 2
569 * channel == 16 -> Only accept input from remote channel 16
573 for (i
= 0; i
< 16; i
++) {
574 if ((1 << i
) & channel_mask
) {
575 if (!(~(1 << i
) & 0xFFFF & channel_mask
))
581 r
= usb_control_msg(ar2
->udev
, usb_sndctrlpipe(ar2
->udev
, 0),
583 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_INTERFACE
,
584 channel
, 0x0, NULL
, 0, USB_CTRL_SET_TIMEOUT
);
586 dev_err(&ar2
->udev
->dev
, "%s - failed to set channel due to error: %d\n",
594 static int ati_remote2_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
596 struct usb_device
*udev
= interface_to_usbdev(interface
);
597 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
598 struct ati_remote2
*ar2
;
601 if (alt
->desc
.bInterfaceNumber
)
604 ar2
= kzalloc(sizeof (struct ati_remote2
), GFP_KERNEL
);
610 ar2
->intf
[0] = interface
;
611 ar2
->ep
[0] = &alt
->endpoint
[0].desc
;
613 ar2
->intf
[1] = usb_ifnum_to_if(udev
, 1);
614 r
= usb_driver_claim_interface(&ati_remote2_driver
, ar2
->intf
[1], ar2
);
617 alt
= ar2
->intf
[1]->cur_altsetting
;
618 ar2
->ep
[1] = &alt
->endpoint
[0].desc
;
620 r
= ati_remote2_urb_init(ar2
);
624 r
= ati_remote2_setup(ar2
);
628 usb_make_path(udev
, ar2
->phys
, sizeof(ar2
->phys
));
629 strlcat(ar2
->phys
, "/input0", sizeof(ar2
->phys
));
631 strlcat(ar2
->name
, "ATI Remote Wonder II", sizeof(ar2
->name
));
633 r
= ati_remote2_input_init(ar2
);
637 usb_set_intfdata(interface
, ar2
);
639 interface
->needs_remote_wakeup
= 1;
644 ati_remote2_urb_cleanup(ar2
);
646 usb_driver_release_interface(&ati_remote2_driver
, ar2
->intf
[1]);
653 static void ati_remote2_disconnect(struct usb_interface
*interface
)
655 struct ati_remote2
*ar2
;
656 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
658 if (alt
->desc
.bInterfaceNumber
)
661 ar2
= usb_get_intfdata(interface
);
662 usb_set_intfdata(interface
, NULL
);
664 input_unregister_device(ar2
->idev
);
666 ati_remote2_urb_cleanup(ar2
);
668 usb_driver_release_interface(&ati_remote2_driver
, ar2
->intf
[1]);
673 static int ati_remote2_suspend(struct usb_interface
*interface
,
674 pm_message_t message
)
676 struct ati_remote2
*ar2
;
677 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
679 if (alt
->desc
.bInterfaceNumber
)
682 ar2
= usb_get_intfdata(interface
);
684 dev_dbg(&ar2
->intf
[0]->dev
, "%s()\n", __func__
);
686 mutex_lock(&ati_remote2_mutex
);
688 if (ar2
->flags
& ATI_REMOTE2_OPENED
)
689 ati_remote2_kill_urbs(ar2
);
691 ar2
->flags
|= ATI_REMOTE2_SUSPENDED
;
693 mutex_unlock(&ati_remote2_mutex
);
698 static int ati_remote2_resume(struct usb_interface
*interface
)
700 struct ati_remote2
*ar2
;
701 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
704 if (alt
->desc
.bInterfaceNumber
)
707 ar2
= usb_get_intfdata(interface
);
709 dev_dbg(&ar2
->intf
[0]->dev
, "%s()\n", __func__
);
711 mutex_lock(&ati_remote2_mutex
);
713 if (ar2
->flags
& ATI_REMOTE2_OPENED
)
714 r
= ati_remote2_submit_urbs(ar2
);
717 ar2
->flags
&= ~ATI_REMOTE2_SUSPENDED
;
719 mutex_unlock(&ati_remote2_mutex
);
724 static int ati_remote2_reset_resume(struct usb_interface
*interface
)
726 struct ati_remote2
*ar2
;
727 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
730 if (alt
->desc
.bInterfaceNumber
)
733 ar2
= usb_get_intfdata(interface
);
735 dev_dbg(&ar2
->intf
[0]->dev
, "%s()\n", __func__
);
737 mutex_lock(&ati_remote2_mutex
);
739 r
= ati_remote2_setup(ar2
);
743 if (ar2
->flags
& ATI_REMOTE2_OPENED
)
744 r
= ati_remote2_submit_urbs(ar2
);
747 ar2
->flags
&= ~ATI_REMOTE2_SUSPENDED
;
750 mutex_unlock(&ati_remote2_mutex
);
755 static int ati_remote2_pre_reset(struct usb_interface
*interface
)
757 struct ati_remote2
*ar2
;
758 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
760 if (alt
->desc
.bInterfaceNumber
)
763 ar2
= usb_get_intfdata(interface
);
765 dev_dbg(&ar2
->intf
[0]->dev
, "%s()\n", __func__
);
767 mutex_lock(&ati_remote2_mutex
);
769 if (ar2
->flags
== ATI_REMOTE2_OPENED
)
770 ati_remote2_kill_urbs(ar2
);
775 static int ati_remote2_post_reset(struct usb_interface
*interface
)
777 struct ati_remote2
*ar2
;
778 struct usb_host_interface
*alt
= interface
->cur_altsetting
;
781 if (alt
->desc
.bInterfaceNumber
)
784 ar2
= usb_get_intfdata(interface
);
786 dev_dbg(&ar2
->intf
[0]->dev
, "%s()\n", __func__
);
788 if (ar2
->flags
== ATI_REMOTE2_OPENED
)
789 r
= ati_remote2_submit_urbs(ar2
);
791 mutex_unlock(&ati_remote2_mutex
);
796 static int __init
ati_remote2_init(void)
800 r
= usb_register(&ati_remote2_driver
);
802 printk(KERN_ERR
"ati_remote2: usb_register() = %d\n", r
);
804 printk(KERN_INFO
"ati_remote2: " DRIVER_DESC
" " DRIVER_VERSION
"\n");
809 static void __exit
ati_remote2_exit(void)
811 usb_deregister(&ati_remote2_driver
);
814 module_init(ati_remote2_init
);
815 module_exit(ati_remote2_exit
);