blob: 83f1f79db7c72b404f43115725f2c7a04b03f7c2 [file] [log] [blame]
Ville Syrjälä735b0cb2005-12-10 20:30:54 +02001/*
2 * ati_remote2 - ATI/Philips USB RF remote driver
3 *
4 * Copyright (C) 2005 Ville Syrjala <syrjala@sci.fi>
5 *
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.
9 */
10
David Brownellae0dadc2006-06-13 10:04:34 -070011#include <linux/usb/input.h>
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020012
13#define DRIVER_DESC "ATI/Philips USB RF remote driver"
14#define DRIVER_VERSION "0.1"
15
16MODULE_DESCRIPTION(DRIVER_DESC);
17MODULE_VERSION(DRIVER_VERSION);
18MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
19MODULE_LICENSE("GPL");
20
21static unsigned int mode_mask = 0x1F;
22module_param(mode_mask, uint, 0644);
23MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
24
25static struct usb_device_id ati_remote2_id_table[] = {
26 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
27 { }
28};
29MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
30
31static struct {
32 int hw_code;
33 int key_code;
34} ati_remote2_key_table[] = {
35 { 0x00, KEY_0 },
36 { 0x01, KEY_1 },
37 { 0x02, KEY_2 },
38 { 0x03, KEY_3 },
39 { 0x04, KEY_4 },
40 { 0x05, KEY_5 },
41 { 0x06, KEY_6 },
42 { 0x07, KEY_7 },
43 { 0x08, KEY_8 },
44 { 0x09, KEY_9 },
45 { 0x0c, KEY_POWER },
46 { 0x0d, KEY_MUTE },
47 { 0x10, KEY_VOLUMEUP },
48 { 0x11, KEY_VOLUMEDOWN },
49 { 0x20, KEY_CHANNELUP },
50 { 0x21, KEY_CHANNELDOWN },
51 { 0x28, KEY_FORWARD },
52 { 0x29, KEY_REWIND },
53 { 0x2c, KEY_PLAY },
54 { 0x30, KEY_PAUSE },
55 { 0x31, KEY_STOP },
56 { 0x37, KEY_RECORD },
57 { 0x38, KEY_DVD },
58 { 0x39, KEY_TV },
59 { 0x54, KEY_MENU },
60 { 0x58, KEY_UP },
61 { 0x59, KEY_DOWN },
62 { 0x5a, KEY_LEFT },
63 { 0x5b, KEY_RIGHT },
64 { 0x5c, KEY_OK },
65 { 0x78, KEY_A },
66 { 0x79, KEY_B },
67 { 0x7a, KEY_C },
68 { 0x7b, KEY_D },
69 { 0x7c, KEY_E },
70 { 0x7d, KEY_F },
71 { 0x82, KEY_ENTER },
72 { 0x8e, KEY_VENDOR },
73 { 0x96, KEY_COFFEE },
74 { 0xa9, BTN_LEFT },
75 { 0xaa, BTN_RIGHT },
76 { 0xbe, KEY_QUESTION },
77 { 0xd5, KEY_FRONT },
78 { 0xd0, KEY_EDIT },
79 { 0xf9, KEY_INFO },
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 },
85 { 0, KEY_RESERVED }
86};
87
88struct ati_remote2 {
89 struct input_dev *idev;
90 struct usb_device *udev;
91
92 struct usb_interface *intf[2];
93 struct usb_endpoint_descriptor *ep[2];
94 struct urb *urb[2];
95 void *buf[2];
96 dma_addr_t buf_dma[2];
97
98 unsigned long jiffies;
99 int mode;
100
101 char name[64];
102 char phys[64];
103};
104
105static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
106static void ati_remote2_disconnect(struct usb_interface *interface);
107
108static 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,
113};
114
115static int ati_remote2_open(struct input_dev *idev)
116{
117 struct ati_remote2 *ar2 = idev->private;
118 int r;
119
120 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
121 if (r) {
122 dev_err(&ar2->intf[0]->dev,
123 "%s: usb_submit_urb() = %d\n", __FUNCTION__, r);
124 return r;
125 }
126 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
127 if (r) {
128 usb_kill_urb(ar2->urb[0]);
129 dev_err(&ar2->intf[1]->dev,
130 "%s: usb_submit_urb() = %d\n", __FUNCTION__, r);
131 return r;
132 }
133
134 return 0;
135}
136
137static void ati_remote2_close(struct input_dev *idev)
138{
139 struct ati_remote2 *ar2 = idev->private;
140
141 usb_kill_urb(ar2->urb[0]);
142 usb_kill_urb(ar2->urb[1]);
143}
144
David Howells7d12e782006-10-05 14:55:46 +0100145static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200146{
147 struct input_dev *idev = ar2->idev;
148 u8 *data = ar2->buf[0];
149
150 if (data[0] > 4) {
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]);
154 return;
155 }
156
157 if (!((1 << data[0]) & mode_mask))
158 return;
159
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200160 input_event(idev, EV_REL, REL_X, (s8) data[1]);
161 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
162 input_sync(idev);
163}
164
165static int ati_remote2_lookup(unsigned int hw_code)
166{
167 int i;
168
169 for (i = 0; ati_remote2_key_table[i].key_code != KEY_RESERVED; i++)
170 if (ati_remote2_key_table[i].hw_code == hw_code)
171 return i;
172
173 return -1;
174}
175
David Howells7d12e782006-10-05 14:55:46 +0100176static void ati_remote2_input_key(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200177{
178 struct input_dev *idev = ar2->idev;
179 u8 *data = ar2->buf[1];
180 int hw_code, index;
181
182 if (data[0] > 4) {
183 dev_err(&ar2->intf[1]->dev,
184 "Unknown mode byte (%02x %02x %02x %02x)\n",
185 data[3], data[2], data[1], data[0]);
186 return;
187 }
188
189 hw_code = data[2];
190 /*
191 * Mode keys (AUX1-AUX4, PC) all generate the same code byte.
192 * Use the mode byte to figure out which one was pressed.
193 */
194 if (hw_code == 0x3f) {
195 /*
196 * For some incomprehensible reason the mouse pad generates
197 * events which look identical to the events from the last
198 * pressed mode key. Naturally we don't want to generate key
199 * events for the mouse pad so we filter out any subsequent
200 * events from the same mode key.
201 */
202 if (ar2->mode == data[0])
203 return;
204
205 if (data[1] == 0)
206 ar2->mode = data[0];
207
208 hw_code |= data[0] << 8;
209 }
210
211 if (!((1 << data[0]) & mode_mask))
212 return;
213
214 index = ati_remote2_lookup(hw_code);
215 if (index < 0) {
216 dev_err(&ar2->intf[1]->dev,
217 "Unknown code byte (%02x %02x %02x %02x)\n",
218 data[3], data[2], data[1], data[0]);
219 return;
220 }
221
222 switch (data[1]) {
223 case 0: /* release */
224 break;
225 case 1: /* press */
226 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
227 break;
228 case 2: /* repeat */
229
230 /* No repeat for mouse buttons. */
231 if (ati_remote2_key_table[index].key_code == BTN_LEFT ||
232 ati_remote2_key_table[index].key_code == BTN_RIGHT)
233 return;
234
235 if (!time_after_eq(jiffies, ar2->jiffies))
236 return;
237
238 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
239 break;
240 default:
241 dev_err(&ar2->intf[1]->dev,
242 "Unknown state byte (%02x %02x %02x %02x)\n",
243 data[3], data[2], data[1], data[0]);
244 return;
245 }
246
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200247 input_event(idev, EV_KEY, ati_remote2_key_table[index].key_code, data[1]);
248 input_sync(idev);
249}
250
David Howells7d12e782006-10-05 14:55:46 +0100251static void ati_remote2_complete_mouse(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200252{
253 struct ati_remote2 *ar2 = urb->context;
254 int r;
255
256 switch (urb->status) {
257 case 0:
David Howells7d12e782006-10-05 14:55:46 +0100258 ati_remote2_input_mouse(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200259 break;
260 case -ENOENT:
261 case -EILSEQ:
262 case -ECONNRESET:
263 case -ESHUTDOWN:
264 dev_dbg(&ar2->intf[0]->dev,
265 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
266 return;
267 default:
268 dev_err(&ar2->intf[0]->dev,
269 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
270 }
271
272 r = usb_submit_urb(urb, GFP_ATOMIC);
273 if (r)
274 dev_err(&ar2->intf[0]->dev,
275 "%s(): usb_submit_urb() = %d\n", __FUNCTION__, r);
276}
277
David Howells7d12e782006-10-05 14:55:46 +0100278static void ati_remote2_complete_key(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200279{
280 struct ati_remote2 *ar2 = urb->context;
281 int r;
282
283 switch (urb->status) {
284 case 0:
David Howells7d12e782006-10-05 14:55:46 +0100285 ati_remote2_input_key(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200286 break;
287 case -ENOENT:
288 case -EILSEQ:
289 case -ECONNRESET:
290 case -ESHUTDOWN:
291 dev_dbg(&ar2->intf[1]->dev,
292 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
293 return;
294 default:
295 dev_err(&ar2->intf[1]->dev,
296 "%s(): urb status = %d\n", __FUNCTION__, urb->status);
297 }
298
299 r = usb_submit_urb(urb, GFP_ATOMIC);
300 if (r)
301 dev_err(&ar2->intf[1]->dev,
302 "%s(): usb_submit_urb() = %d\n", __FUNCTION__, r);
303}
304
305static int ati_remote2_input_init(struct ati_remote2 *ar2)
306{
307 struct input_dev *idev;
308 int i;
309
310 idev = input_allocate_device();
311 if (!idev)
312 return -ENOMEM;
313
314 ar2->idev = idev;
315 idev->private = ar2;
316
317 idev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_REL);
318 idev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
319 idev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
320 for (i = 0; ati_remote2_key_table[i].key_code != KEY_RESERVED; i++)
321 set_bit(ati_remote2_key_table[i].key_code, idev->keybit);
322
323 idev->rep[REP_DELAY] = 250;
324 idev->rep[REP_PERIOD] = 33;
325
326 idev->open = ati_remote2_open;
327 idev->close = ati_remote2_close;
328
329 idev->name = ar2->name;
330 idev->phys = ar2->phys;
331
332 usb_to_input_id(ar2->udev, &idev->id);
333 idev->cdev.dev = &ar2->udev->dev;
334
335 i = input_register_device(idev);
336 if (i)
337 input_free_device(idev);
338
339 return i;
340}
341
342static int ati_remote2_urb_init(struct ati_remote2 *ar2)
343{
344 struct usb_device *udev = ar2->udev;
345 int i, pipe, maxp;
346
347 for (i = 0; i < 2; i++) {
348 ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
349 if (!ar2->buf[i])
350 return -ENOMEM;
351
352 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
353 if (!ar2->urb[i])
354 return -ENOMEM;
355
356 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
357 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
358 maxp = maxp > 4 ? 4 : maxp;
359
360 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
361 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
362 ar2, ar2->ep[i]->bInterval);
363 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
364 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
365 }
366
367 return 0;
368}
369
370static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
371{
372 int i;
373
374 for (i = 0; i < 2; i++) {
Mariusz Kozlowski23815262006-11-08 15:35:50 +0100375 usb_free_urb(ar2->urb[i]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200376
377 if (ar2->buf[i])
378 usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
379 }
380}
381
382static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
383{
384 struct usb_device *udev = interface_to_usbdev(interface);
385 struct usb_host_interface *alt = interface->cur_altsetting;
386 struct ati_remote2 *ar2;
387 int r;
388
389 if (alt->desc.bInterfaceNumber)
390 return -ENODEV;
391
392 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
393 if (!ar2)
394 return -ENOMEM;
395
396 ar2->udev = udev;
397
398 ar2->intf[0] = interface;
399 ar2->ep[0] = &alt->endpoint[0].desc;
400
401 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
402 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
403 if (r)
404 goto fail1;
405 alt = ar2->intf[1]->cur_altsetting;
406 ar2->ep[1] = &alt->endpoint[0].desc;
407
408 r = ati_remote2_urb_init(ar2);
409 if (r)
410 goto fail2;
411
412 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
413 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
414
415 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
416
417 r = ati_remote2_input_init(ar2);
418 if (r)
419 goto fail2;
420
421 usb_set_intfdata(interface, ar2);
422
423 return 0;
424
425 fail2:
426 ati_remote2_urb_cleanup(ar2);
427
428 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
429 fail1:
430 kfree(ar2);
431
432 return r;
433}
434
435static void ati_remote2_disconnect(struct usb_interface *interface)
436{
437 struct ati_remote2 *ar2;
438 struct usb_host_interface *alt = interface->cur_altsetting;
439
440 if (alt->desc.bInterfaceNumber)
441 return;
442
443 ar2 = usb_get_intfdata(interface);
444 usb_set_intfdata(interface, NULL);
445
446 input_unregister_device(ar2->idev);
447
448 ati_remote2_urb_cleanup(ar2);
449
450 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
451
452 kfree(ar2);
453}
454
455static int __init ati_remote2_init(void)
456{
457 int r;
458
459 r = usb_register(&ati_remote2_driver);
460 if (r)
461 printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
462 else
463 printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
464
465 return r;
466}
467
468static void __exit ati_remote2_exit(void)
469{
470 usb_deregister(&ati_remote2_driver);
471}
472
473module_init(ati_remote2_init);
474module_exit(ati_remote2_exit);