Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ati_remote2 - ATI/Philips USB RF remote driver
4 *
5 * Copyright (C) 2005-2008 Ville Syrjala <syrjala@sci.fi>
6 * Copyright (C) 2007-2008 Peter Stokes <linux@dadeos.co.uk>
7 */
8
9#include <linux/usb/input.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12
13#define DRIVER_DESC "ATI/Philips USB RF remote driver"
14
15MODULE_DESCRIPTION(DRIVER_DESC);
16MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
17MODULE_LICENSE("GPL");
18
19/*
20 * ATI Remote Wonder II Channel Configuration
21 *
22 * The remote control can be assigned one of sixteen "channels" in order to facilitate
23 * the use of multiple remote controls within range of each other.
24 * A remote's "channel" may be altered by pressing and holding the "PC" button for
25 * approximately 3 seconds, after which the button will slowly flash the count of the
26 * currently configured "channel", using the numeric keypad enter a number between 1 and
27 * 16 and then press the "PC" button again, the button will slowly flash the count of the
28 * newly configured "channel".
29 */
30
31enum {
32 ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,
33 ATI_REMOTE2_MAX_MODE_MASK = 0x1F,
34};
35
36static int ati_remote2_set_mask(const char *val,
37 const struct kernel_param *kp,
38 unsigned int max)
39{
40 unsigned int mask;
41 int ret;
42
43 if (!val)
44 return -EINVAL;
45
46 ret = kstrtouint(val, 0, &mask);
47 if (ret)
48 return ret;
49
50 if (mask & ~max)
51 return -EINVAL;
52
53 *(unsigned int *)kp->arg = mask;
54
55 return 0;
56}
57
58static int ati_remote2_set_channel_mask(const char *val,
59 const struct kernel_param *kp)
60{
61 pr_debug("%s()\n", __func__);
62
63 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK);
64}
65
66static int ati_remote2_get_channel_mask(char *buffer,
67 const struct kernel_param *kp)
68{
69 pr_debug("%s()\n", __func__);
70
71 return sprintf(buffer, "0x%04x\n", *(unsigned int *)kp->arg);
72}
73
74static int ati_remote2_set_mode_mask(const char *val,
75 const struct kernel_param *kp)
76{
77 pr_debug("%s()\n", __func__);
78
79 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK);
80}
81
82static int ati_remote2_get_mode_mask(char *buffer,
83 const struct kernel_param *kp)
84{
85 pr_debug("%s()\n", __func__);
86
87 return sprintf(buffer, "0x%02x\n", *(unsigned int *)kp->arg);
88}
89
90static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK;
91#define param_check_channel_mask(name, p) __param_check(name, p, unsigned int)
92static const struct kernel_param_ops param_ops_channel_mask = {
93 .set = ati_remote2_set_channel_mask,
94 .get = ati_remote2_get_channel_mask,
95};
96module_param(channel_mask, channel_mask, 0644);
97MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
98
99static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK;
100#define param_check_mode_mask(name, p) __param_check(name, p, unsigned int)
101static const struct kernel_param_ops param_ops_mode_mask = {
102 .set = ati_remote2_set_mode_mask,
103 .get = ati_remote2_get_mode_mask,
104};
105module_param(mode_mask, mode_mask, 0644);
106MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
107
108static const struct usb_device_id ati_remote2_id_table[] = {
109 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
110 { }
111};
112MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
113
114static DEFINE_MUTEX(ati_remote2_mutex);
115
116enum {
117 ATI_REMOTE2_OPENED = 0x1,
118 ATI_REMOTE2_SUSPENDED = 0x2,
119};
120
121enum {
122 ATI_REMOTE2_AUX1,
123 ATI_REMOTE2_AUX2,
124 ATI_REMOTE2_AUX3,
125 ATI_REMOTE2_AUX4,
126 ATI_REMOTE2_PC,
127 ATI_REMOTE2_MODES,
128};
129
130static const struct {
131 u8 hw_code;
132 u16 keycode;
133} ati_remote2_key_table[] = {
134 { 0x00, KEY_0 },
135 { 0x01, KEY_1 },
136 { 0x02, KEY_2 },
137 { 0x03, KEY_3 },
138 { 0x04, KEY_4 },
139 { 0x05, KEY_5 },
140 { 0x06, KEY_6 },
141 { 0x07, KEY_7 },
142 { 0x08, KEY_8 },
143 { 0x09, KEY_9 },
144 { 0x0c, KEY_POWER },
145 { 0x0d, KEY_MUTE },
146 { 0x10, KEY_VOLUMEUP },
147 { 0x11, KEY_VOLUMEDOWN },
148 { 0x20, KEY_CHANNELUP },
149 { 0x21, KEY_CHANNELDOWN },
150 { 0x28, KEY_FORWARD },
151 { 0x29, KEY_REWIND },
152 { 0x2c, KEY_PLAY },
153 { 0x30, KEY_PAUSE },
154 { 0x31, KEY_STOP },
155 { 0x37, KEY_RECORD },
156 { 0x38, KEY_DVD },
157 { 0x39, KEY_TV },
158 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */
159 { 0x54, KEY_MENU },
160 { 0x58, KEY_UP },
161 { 0x59, KEY_DOWN },
162 { 0x5a, KEY_LEFT },
163 { 0x5b, KEY_RIGHT },
164 { 0x5c, KEY_OK },
165 { 0x78, KEY_A },
166 { 0x79, KEY_B },
167 { 0x7a, KEY_C },
168 { 0x7b, KEY_D },
169 { 0x7c, KEY_E },
170 { 0x7d, KEY_F },
171 { 0x82, KEY_ENTER },
172 { 0x8e, KEY_VENDOR },
173 { 0x96, KEY_COFFEE },
174 { 0xa9, BTN_LEFT },
175 { 0xaa, BTN_RIGHT },
176 { 0xbe, KEY_QUESTION },
177 { 0xd0, KEY_EDIT },
178 { 0xd5, KEY_FRONT },
179 { 0xf9, KEY_INFO },
180};
181
182struct ati_remote2 {
183 struct input_dev *idev;
184 struct usb_device *udev;
185
186 struct usb_interface *intf[2];
187 struct usb_endpoint_descriptor *ep[2];
188 struct urb *urb[2];
189 void *buf[2];
190 dma_addr_t buf_dma[2];
191
192 unsigned long jiffies;
193 int mode;
194
195 char name[64];
196 char phys[64];
197
198 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
199 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
200
201 unsigned int flags;
202
203 unsigned int channel_mask;
204 unsigned int mode_mask;
205};
206
207static struct usb_driver ati_remote2_driver;
208
209static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
210{
211 int r;
212
213 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
214 if (r) {
215 dev_err(&ar2->intf[0]->dev,
216 "%s(): usb_submit_urb() = %d\n", __func__, r);
217 return r;
218 }
219 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
220 if (r) {
221 usb_kill_urb(ar2->urb[0]);
222 dev_err(&ar2->intf[1]->dev,
223 "%s(): usb_submit_urb() = %d\n", __func__, r);
224 return r;
225 }
226
227 return 0;
228}
229
230static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)
231{
232 usb_kill_urb(ar2->urb[1]);
233 usb_kill_urb(ar2->urb[0]);
234}
235
236static int ati_remote2_open(struct input_dev *idev)
237{
238 struct ati_remote2 *ar2 = input_get_drvdata(idev);
239 int r;
240
241 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
242
243 r = usb_autopm_get_interface(ar2->intf[0]);
244 if (r) {
245 dev_err(&ar2->intf[0]->dev,
246 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
247 return r;
248 }
249
250 scoped_guard(mutex, &ati_remote2_mutex) {
251 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
252 r = ati_remote2_submit_urbs(ar2);
253 if (r)
254 break;
255 }
256
257 ar2->flags |= ATI_REMOTE2_OPENED;
258 }
259
260 usb_autopm_put_interface(ar2->intf[0]);
261
262 return r;
263}
264
265static void ati_remote2_close(struct input_dev *idev)
266{
267 struct ati_remote2 *ar2 = input_get_drvdata(idev);
268
269 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
270
271 guard(mutex)(&ati_remote2_mutex);
272
273 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
274 ati_remote2_kill_urbs(ar2);
275
276 ar2->flags &= ~ATI_REMOTE2_OPENED;
277}
278
279static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
280{
281 struct input_dev *idev = ar2->idev;
282 u8 *data = ar2->buf[0];
283 int channel, mode;
284
285 channel = data[0] >> 4;
286
287 if (!((1 << channel) & ar2->channel_mask))
288 return;
289
290 mode = data[0] & 0x0F;
291
292 if (mode > ATI_REMOTE2_PC) {
293 dev_err(&ar2->intf[0]->dev,
294 "Unknown mode byte (%02x %02x %02x %02x)\n",
295 data[3], data[2], data[1], data[0]);
296 return;
297 }
298
299 if (!((1 << mode) & ar2->mode_mask))
300 return;
301
302 input_event(idev, EV_REL, REL_X, (s8) data[1]);
303 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
304 input_sync(idev);
305}
306
307static int ati_remote2_lookup(unsigned int hw_code)
308{
309 int i;
310
311 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
312 if (ati_remote2_key_table[i].hw_code == hw_code)
313 return i;
314
315 return -1;
316}
317
318static void ati_remote2_input_key(struct ati_remote2 *ar2)
319{
320 struct input_dev *idev = ar2->idev;
321 u8 *data = ar2->buf[1];
322 int channel, mode, hw_code, index;
323
324 channel = data[0] >> 4;
325
326 if (!((1 << channel) & ar2->channel_mask))
327 return;
328
329 mode = data[0] & 0x0F;
330
331 if (mode > ATI_REMOTE2_PC) {
332 dev_err(&ar2->intf[1]->dev,
333 "Unknown mode byte (%02x %02x %02x %02x)\n",
334 data[3], data[2], data[1], data[0]);
335 return;
336 }
337
338 hw_code = data[2];
339 if (hw_code == 0x3f) {
340 /*
341 * For some incomprehensible reason the mouse pad generates
342 * events which look identical to the events from the last
343 * pressed mode key. Naturally we don't want to generate key
344 * events for the mouse pad so we filter out any subsequent
345 * events from the same mode key.
346 */
347 if (ar2->mode == mode)
348 return;
349
350 if (data[1] == 0)
351 ar2->mode = mode;
352 }
353
354 if (!((1 << mode) & ar2->mode_mask))
355 return;
356
357 index = ati_remote2_lookup(hw_code);
358 if (index < 0) {
359 dev_err(&ar2->intf[1]->dev,
360 "Unknown code byte (%02x %02x %02x %02x)\n",
361 data[3], data[2], data[1], data[0]);
362 return;
363 }
364
365 switch (data[1]) {
366 case 0: /* release */
367 break;
368 case 1: /* press */
369 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
370 break;
371 case 2: /* repeat */
372
373 /* No repeat for mouse buttons. */
374 if (ar2->keycode[mode][index] == BTN_LEFT ||
375 ar2->keycode[mode][index] == BTN_RIGHT)
376 return;
377
378 if (!time_after_eq(jiffies, ar2->jiffies))
379 return;
380
381 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
382 break;
383 default:
384 dev_err(&ar2->intf[1]->dev,
385 "Unknown state byte (%02x %02x %02x %02x)\n",
386 data[3], data[2], data[1], data[0]);
387 return;
388 }
389
390 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
391 input_sync(idev);
392}
393
394static void ati_remote2_complete_mouse(struct urb *urb)
395{
396 struct ati_remote2 *ar2 = urb->context;
397 int r;
398
399 switch (urb->status) {
400 case 0:
401 usb_mark_last_busy(ar2->udev);
402 ati_remote2_input_mouse(ar2);
403 break;
404 case -ENOENT:
405 case -EILSEQ:
406 case -ECONNRESET:
407 case -ESHUTDOWN:
408 dev_dbg(&ar2->intf[0]->dev,
409 "%s(): urb status = %d\n", __func__, urb->status);
410 return;
411 default:
412 usb_mark_last_busy(ar2->udev);
413 dev_err(&ar2->intf[0]->dev,
414 "%s(): urb status = %d\n", __func__, urb->status);
415 }
416
417 r = usb_submit_urb(urb, GFP_ATOMIC);
418 if (r)
419 dev_err(&ar2->intf[0]->dev,
420 "%s(): usb_submit_urb() = %d\n", __func__, r);
421}
422
423static void ati_remote2_complete_key(struct urb *urb)
424{
425 struct ati_remote2 *ar2 = urb->context;
426 int r;
427
428 switch (urb->status) {
429 case 0:
430 usb_mark_last_busy(ar2->udev);
431 ati_remote2_input_key(ar2);
432 break;
433 case -ENOENT:
434 case -EILSEQ:
435 case -ECONNRESET:
436 case -ESHUTDOWN:
437 dev_dbg(&ar2->intf[1]->dev,
438 "%s(): urb status = %d\n", __func__, urb->status);
439 return;
440 default:
441 usb_mark_last_busy(ar2->udev);
442 dev_err(&ar2->intf[1]->dev,
443 "%s(): urb status = %d\n", __func__, urb->status);
444 }
445
446 r = usb_submit_urb(urb, GFP_ATOMIC);
447 if (r)
448 dev_err(&ar2->intf[1]->dev,
449 "%s(): usb_submit_urb() = %d\n", __func__, r);
450}
451
452static int ati_remote2_getkeycode(struct input_dev *idev,
453 struct input_keymap_entry *ke)
454{
455 struct ati_remote2 *ar2 = input_get_drvdata(idev);
456 unsigned int mode;
457 int offset;
458 unsigned int index;
459 unsigned int scancode;
460
461 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
462 index = ke->index;
463 if (index >= ATI_REMOTE2_MODES *
464 ARRAY_SIZE(ati_remote2_key_table))
465 return -EINVAL;
466
467 mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);
468 offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);
469 scancode = (mode << 8) + ati_remote2_key_table[offset].hw_code;
470 } else {
471 if (input_scancode_to_scalar(ke, &scancode))
472 return -EINVAL;
473
474 mode = scancode >> 8;
475 if (mode > ATI_REMOTE2_PC)
476 return -EINVAL;
477
478 offset = ati_remote2_lookup(scancode & 0xff);
479 if (offset < 0)
480 return -EINVAL;
481
482 index = mode * ARRAY_SIZE(ati_remote2_key_table) + offset;
483 }
484
485 ke->keycode = ar2->keycode[mode][offset];
486 ke->len = sizeof(scancode);
487 memcpy(&ke->scancode, &scancode, sizeof(scancode));
488 ke->index = index;
489
490 return 0;
491}
492
493static int ati_remote2_setkeycode(struct input_dev *idev,
494 const struct input_keymap_entry *ke,
495 unsigned int *old_keycode)
496{
497 struct ati_remote2 *ar2 = input_get_drvdata(idev);
498 unsigned int mode;
499 int offset;
500 unsigned int index;
501 unsigned int scancode;
502
503 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
504 if (ke->index >= ATI_REMOTE2_MODES *
505 ARRAY_SIZE(ati_remote2_key_table))
506 return -EINVAL;
507
508 mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);
509 offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);
510 } else {
511 if (input_scancode_to_scalar(ke, &scancode))
512 return -EINVAL;
513
514 mode = scancode >> 8;
515 if (mode > ATI_REMOTE2_PC)
516 return -EINVAL;
517
518 offset = ati_remote2_lookup(scancode & 0xff);
519 if (offset < 0)
520 return -EINVAL;
521 }
522
523 *old_keycode = ar2->keycode[mode][offset];
524 ar2->keycode[mode][offset] = ke->keycode;
525 __set_bit(ke->keycode, idev->keybit);
526
527 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
528 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
529 if (ar2->keycode[mode][index] == *old_keycode)
530 return 0;
531 }
532 }
533
534 __clear_bit(*old_keycode, idev->keybit);
535
536 return 0;
537}
538
539static int ati_remote2_input_init(struct ati_remote2 *ar2)
540{
541 struct input_dev *idev;
542 int index, mode, retval;
543
544 idev = input_allocate_device();
545 if (!idev)
546 return -ENOMEM;
547
548 ar2->idev = idev;
549 input_set_drvdata(idev, ar2);
550
551 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
552 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
553 BIT_MASK(BTN_RIGHT);
554 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
555
556 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
557 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
558 ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
559 __set_bit(ar2->keycode[mode][index], idev->keybit);
560 }
561 }
562
563 /* AUX1-AUX4 and PC generate the same scancode. */
564 index = ati_remote2_lookup(0x3f);
565 ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
566 ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
567 ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
568 ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
569 ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
570 __set_bit(KEY_PROG1, idev->keybit);
571 __set_bit(KEY_PROG2, idev->keybit);
572 __set_bit(KEY_PROG3, idev->keybit);
573 __set_bit(KEY_PROG4, idev->keybit);
574 __set_bit(KEY_PC, idev->keybit);
575
576 idev->rep[REP_DELAY] = 250;
577 idev->rep[REP_PERIOD] = 33;
578
579 idev->open = ati_remote2_open;
580 idev->close = ati_remote2_close;
581
582 idev->getkeycode = ati_remote2_getkeycode;
583 idev->setkeycode = ati_remote2_setkeycode;
584
585 idev->name = ar2->name;
586 idev->phys = ar2->phys;
587
588 usb_to_input_id(ar2->udev, &idev->id);
589 idev->dev.parent = &ar2->udev->dev;
590
591 retval = input_register_device(idev);
592 if (retval)
593 input_free_device(idev);
594
595 return retval;
596}
597
598static int ati_remote2_urb_init(struct ati_remote2 *ar2)
599{
600 struct usb_device *udev = ar2->udev;
601 int i, pipe, maxp;
602
603 for (i = 0; i < 2; i++) {
604 ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
605 if (!ar2->buf[i])
606 return -ENOMEM;
607
608 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
609 if (!ar2->urb[i])
610 return -ENOMEM;
611
612 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
613 maxp = usb_maxpacket(udev, pipe);
614 maxp = maxp > 4 ? 4 : maxp;
615
616 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
617 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
618 ar2, ar2->ep[i]->bInterval);
619 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
620 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
621 }
622
623 return 0;
624}
625
626static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
627{
628 int i;
629
630 for (i = 0; i < 2; i++) {
631 usb_free_urb(ar2->urb[i]);
632 usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
633 }
634}
635
636static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
637{
638 int r, i, channel;
639
640 /*
641 * Configure receiver to only accept input from remote "channel"
642 * channel == 0 -> Accept input from any remote channel
643 * channel == 1 -> Only accept input from remote channel 1
644 * channel == 2 -> Only accept input from remote channel 2
645 * ...
646 * channel == 16 -> Only accept input from remote channel 16
647 */
648
649 channel = 0;
650 for (i = 0; i < 16; i++) {
651 if ((1 << i) & ch_mask) {
652 if (!(~(1 << i) & ch_mask))
653 channel = i + 1;
654 break;
655 }
656 }
657
658 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
659 0x20,
660 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
661 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
662 if (r) {
663 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
664 __func__, r);
665 return r;
666 }
667
668 return 0;
669}
670
671static ssize_t ati_remote2_show_channel_mask(struct device *dev,
672 struct device_attribute *attr,
673 char *buf)
674{
675 struct usb_device *udev = to_usb_device(dev);
676 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
677 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
678
679 return sprintf(buf, "0x%04x\n", ar2->channel_mask);
680}
681
682static ssize_t ati_remote2_store_channel_mask(struct device *dev,
683 struct device_attribute *attr,
684 const char *buf, size_t count)
685{
686 struct usb_device *udev = to_usb_device(dev);
687 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
688 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
689 unsigned int mask;
690 int r;
691
692 r = kstrtouint(buf, 0, &mask);
693 if (r)
694 return r;
695
696 if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
697 return -EINVAL;
698
699 r = usb_autopm_get_interface(ar2->intf[0]);
700 if (r) {
701 dev_err(&ar2->intf[0]->dev,
702 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
703 return r;
704 }
705
706 scoped_guard(mutex, &ati_remote2_mutex) {
707 if (mask != ar2->channel_mask) {
708 r = ati_remote2_setup(ar2, mask);
709 if (!r)
710 ar2->channel_mask = mask;
711 }
712 }
713
714 usb_autopm_put_interface(ar2->intf[0]);
715
716 return r ? r : count;
717}
718
719static ssize_t ati_remote2_show_mode_mask(struct device *dev,
720 struct device_attribute *attr,
721 char *buf)
722{
723 struct usb_device *udev = to_usb_device(dev);
724 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
725 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
726
727 return sprintf(buf, "0x%02x\n", ar2->mode_mask);
728}
729
730static ssize_t ati_remote2_store_mode_mask(struct device *dev,
731 struct device_attribute *attr,
732 const char *buf, size_t count)
733{
734 struct usb_device *udev = to_usb_device(dev);
735 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
736 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
737 unsigned int mask;
738 int err;
739
740 err = kstrtouint(buf, 0, &mask);
741 if (err)
742 return err;
743
744 if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
745 return -EINVAL;
746
747 ar2->mode_mask = mask;
748
749 return count;
750}
751
752static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
753 ati_remote2_store_channel_mask);
754
755static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
756 ati_remote2_store_mode_mask);
757
758static struct attribute *ati_remote2_attrs[] = {
759 &dev_attr_channel_mask.attr,
760 &dev_attr_mode_mask.attr,
761 NULL,
762};
763ATTRIBUTE_GROUPS(ati_remote2);
764
765static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
766{
767 struct usb_device *udev = interface_to_usbdev(interface);
768 struct usb_host_interface *alt = interface->cur_altsetting;
769 struct ati_remote2 *ar2;
770 int r;
771
772 if (alt->desc.bInterfaceNumber)
773 return -ENODEV;
774
775 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
776 if (!ar2)
777 return -ENOMEM;
778
779 ar2->udev = udev;
780
781 /* Sanity check, first interface must have an endpoint */
782 if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) {
783 dev_err(&interface->dev,
784 "%s(): interface 0 must have an endpoint\n", __func__);
785 r = -ENODEV;
786 goto fail1;
787 }
788 ar2->intf[0] = interface;
789 ar2->ep[0] = &alt->endpoint[0].desc;
790
791 /* Sanity check, the device must have two interfaces */
792 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
793 if ((udev->actconfig->desc.bNumInterfaces < 2) || !ar2->intf[1]) {
794 dev_err(&interface->dev, "%s(): need 2 interfaces, found %d\n",
795 __func__, udev->actconfig->desc.bNumInterfaces);
796 r = -ENODEV;
797 goto fail1;
798 }
799
800 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
801 if (r)
802 goto fail1;
803
804 /* Sanity check, second interface must have an endpoint */
805 alt = ar2->intf[1]->cur_altsetting;
806 if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) {
807 dev_err(&interface->dev,
808 "%s(): interface 1 must have an endpoint\n", __func__);
809 r = -ENODEV;
810 goto fail2;
811 }
812 ar2->ep[1] = &alt->endpoint[0].desc;
813
814 r = ati_remote2_urb_init(ar2);
815 if (r)
816 goto fail3;
817
818 ar2->channel_mask = channel_mask;
819 ar2->mode_mask = mode_mask;
820
821 r = ati_remote2_setup(ar2, ar2->channel_mask);
822 if (r)
823 goto fail3;
824
825 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
826 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
827
828 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
829
830 r = ati_remote2_input_init(ar2);
831 if (r)
832 goto fail3;
833
834 usb_set_intfdata(interface, ar2);
835
836 interface->needs_remote_wakeup = 1;
837
838 return 0;
839
840 fail3:
841 ati_remote2_urb_cleanup(ar2);
842 fail2:
843 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
844 fail1:
845 kfree(ar2);
846
847 return r;
848}
849
850static void ati_remote2_disconnect(struct usb_interface *interface)
851{
852 struct ati_remote2 *ar2;
853 struct usb_host_interface *alt = interface->cur_altsetting;
854
855 if (alt->desc.bInterfaceNumber)
856 return;
857
858 ar2 = usb_get_intfdata(interface);
859 usb_set_intfdata(interface, NULL);
860
861 input_unregister_device(ar2->idev);
862
863 ati_remote2_urb_cleanup(ar2);
864
865 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
866
867 kfree(ar2);
868}
869
870static int ati_remote2_suspend(struct usb_interface *interface,
871 pm_message_t message)
872{
873 struct ati_remote2 *ar2;
874 struct usb_host_interface *alt = interface->cur_altsetting;
875
876 if (alt->desc.bInterfaceNumber)
877 return 0;
878
879 ar2 = usb_get_intfdata(interface);
880
881 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
882
883 guard(mutex)(&ati_remote2_mutex);
884
885 if (ar2->flags & ATI_REMOTE2_OPENED)
886 ati_remote2_kill_urbs(ar2);
887
888 ar2->flags |= ATI_REMOTE2_SUSPENDED;
889
890 return 0;
891}
892
893static int ati_remote2_resume(struct usb_interface *interface)
894{
895 struct ati_remote2 *ar2;
896 struct usb_host_interface *alt = interface->cur_altsetting;
897 int r = 0;
898
899 if (alt->desc.bInterfaceNumber)
900 return 0;
901
902 ar2 = usb_get_intfdata(interface);
903
904 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
905
906 guard(mutex)(&ati_remote2_mutex);
907
908 if (ar2->flags & ATI_REMOTE2_OPENED)
909 r = ati_remote2_submit_urbs(ar2);
910
911 if (!r)
912 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
913
914 return r;
915}
916
917static int ati_remote2_reset_resume(struct usb_interface *interface)
918{
919 struct ati_remote2 *ar2;
920 struct usb_host_interface *alt = interface->cur_altsetting;
921 int r = 0;
922
923 if (alt->desc.bInterfaceNumber)
924 return 0;
925
926 ar2 = usb_get_intfdata(interface);
927
928 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
929
930 guard(mutex)(&ati_remote2_mutex);
931
932 r = ati_remote2_setup(ar2, ar2->channel_mask);
933 if (r)
934 return r;
935
936 if (ar2->flags & ATI_REMOTE2_OPENED)
937 r = ati_remote2_submit_urbs(ar2);
938
939 if (!r)
940 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
941
942 return r;
943}
944
945static int ati_remote2_pre_reset(struct usb_interface *interface)
946{
947 struct ati_remote2 *ar2;
948 struct usb_host_interface *alt = interface->cur_altsetting;
949
950 if (alt->desc.bInterfaceNumber)
951 return 0;
952
953 ar2 = usb_get_intfdata(interface);
954
955 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
956
957 mutex_lock(&ati_remote2_mutex);
958
959 if (ar2->flags == ATI_REMOTE2_OPENED)
960 ati_remote2_kill_urbs(ar2);
961
962 return 0;
963}
964
965static int ati_remote2_post_reset(struct usb_interface *interface)
966{
967 struct ati_remote2 *ar2;
968 struct usb_host_interface *alt = interface->cur_altsetting;
969 int r = 0;
970
971 if (alt->desc.bInterfaceNumber)
972 return 0;
973
974 ar2 = usb_get_intfdata(interface);
975
976 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
977
978 if (ar2->flags == ATI_REMOTE2_OPENED)
979 r = ati_remote2_submit_urbs(ar2);
980
981 mutex_unlock(&ati_remote2_mutex);
982
983 return r;
984}
985
986static struct usb_driver ati_remote2_driver = {
987 .name = "ati_remote2",
988 .probe = ati_remote2_probe,
989 .disconnect = ati_remote2_disconnect,
990 .dev_groups = ati_remote2_groups,
991 .id_table = ati_remote2_id_table,
992 .suspend = ati_remote2_suspend,
993 .resume = ati_remote2_resume,
994 .reset_resume = ati_remote2_reset_resume,
995 .pre_reset = ati_remote2_pre_reset,
996 .post_reset = ati_remote2_post_reset,
997 .supports_autosuspend = 1,
998};
999
1000module_usb_driver(ati_remote2_driver);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ati_remote2 - ATI/Philips USB RF remote driver
4 *
5 * Copyright (C) 2005-2008 Ville Syrjala <syrjala@sci.fi>
6 * Copyright (C) 2007-2008 Peter Stokes <linux@dadeos.co.uk>
7 */
8
9#include <linux/usb/input.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12
13#define DRIVER_DESC "ATI/Philips USB RF remote driver"
14
15MODULE_DESCRIPTION(DRIVER_DESC);
16MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
17MODULE_LICENSE("GPL");
18
19/*
20 * ATI Remote Wonder II Channel Configuration
21 *
22 * The remote control can be assigned one of sixteen "channels" in order to facilitate
23 * the use of multiple remote controls within range of each other.
24 * A remote's "channel" may be altered by pressing and holding the "PC" button for
25 * approximately 3 seconds, after which the button will slowly flash the count of the
26 * currently configured "channel", using the numeric keypad enter a number between 1 and
27 * 16 and then press the "PC" button again, the button will slowly flash the count of the
28 * newly configured "channel".
29 */
30
31enum {
32 ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,
33 ATI_REMOTE2_MAX_MODE_MASK = 0x1F,
34};
35
36static int ati_remote2_set_mask(const char *val,
37 const struct kernel_param *kp,
38 unsigned int max)
39{
40 unsigned int mask;
41 int ret;
42
43 if (!val)
44 return -EINVAL;
45
46 ret = kstrtouint(val, 0, &mask);
47 if (ret)
48 return ret;
49
50 if (mask & ~max)
51 return -EINVAL;
52
53 *(unsigned int *)kp->arg = mask;
54
55 return 0;
56}
57
58static int ati_remote2_set_channel_mask(const char *val,
59 const struct kernel_param *kp)
60{
61 pr_debug("%s()\n", __func__);
62
63 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK);
64}
65
66static int ati_remote2_get_channel_mask(char *buffer,
67 const struct kernel_param *kp)
68{
69 pr_debug("%s()\n", __func__);
70
71 return sprintf(buffer, "0x%04x\n", *(unsigned int *)kp->arg);
72}
73
74static int ati_remote2_set_mode_mask(const char *val,
75 const struct kernel_param *kp)
76{
77 pr_debug("%s()\n", __func__);
78
79 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK);
80}
81
82static int ati_remote2_get_mode_mask(char *buffer,
83 const struct kernel_param *kp)
84{
85 pr_debug("%s()\n", __func__);
86
87 return sprintf(buffer, "0x%02x\n", *(unsigned int *)kp->arg);
88}
89
90static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK;
91#define param_check_channel_mask(name, p) __param_check(name, p, unsigned int)
92static const struct kernel_param_ops param_ops_channel_mask = {
93 .set = ati_remote2_set_channel_mask,
94 .get = ati_remote2_get_channel_mask,
95};
96module_param(channel_mask, channel_mask, 0644);
97MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
98
99static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK;
100#define param_check_mode_mask(name, p) __param_check(name, p, unsigned int)
101static const struct kernel_param_ops param_ops_mode_mask = {
102 .set = ati_remote2_set_mode_mask,
103 .get = ati_remote2_get_mode_mask,
104};
105module_param(mode_mask, mode_mask, 0644);
106MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
107
108static const struct usb_device_id ati_remote2_id_table[] = {
109 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
110 { }
111};
112MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
113
114static DEFINE_MUTEX(ati_remote2_mutex);
115
116enum {
117 ATI_REMOTE2_OPENED = 0x1,
118 ATI_REMOTE2_SUSPENDED = 0x2,
119};
120
121enum {
122 ATI_REMOTE2_AUX1,
123 ATI_REMOTE2_AUX2,
124 ATI_REMOTE2_AUX3,
125 ATI_REMOTE2_AUX4,
126 ATI_REMOTE2_PC,
127 ATI_REMOTE2_MODES,
128};
129
130static const struct {
131 u8 hw_code;
132 u16 keycode;
133} ati_remote2_key_table[] = {
134 { 0x00, KEY_0 },
135 { 0x01, KEY_1 },
136 { 0x02, KEY_2 },
137 { 0x03, KEY_3 },
138 { 0x04, KEY_4 },
139 { 0x05, KEY_5 },
140 { 0x06, KEY_6 },
141 { 0x07, KEY_7 },
142 { 0x08, KEY_8 },
143 { 0x09, KEY_9 },
144 { 0x0c, KEY_POWER },
145 { 0x0d, KEY_MUTE },
146 { 0x10, KEY_VOLUMEUP },
147 { 0x11, KEY_VOLUMEDOWN },
148 { 0x20, KEY_CHANNELUP },
149 { 0x21, KEY_CHANNELDOWN },
150 { 0x28, KEY_FORWARD },
151 { 0x29, KEY_REWIND },
152 { 0x2c, KEY_PLAY },
153 { 0x30, KEY_PAUSE },
154 { 0x31, KEY_STOP },
155 { 0x37, KEY_RECORD },
156 { 0x38, KEY_DVD },
157 { 0x39, KEY_TV },
158 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */
159 { 0x54, KEY_MENU },
160 { 0x58, KEY_UP },
161 { 0x59, KEY_DOWN },
162 { 0x5a, KEY_LEFT },
163 { 0x5b, KEY_RIGHT },
164 { 0x5c, KEY_OK },
165 { 0x78, KEY_A },
166 { 0x79, KEY_B },
167 { 0x7a, KEY_C },
168 { 0x7b, KEY_D },
169 { 0x7c, KEY_E },
170 { 0x7d, KEY_F },
171 { 0x82, KEY_ENTER },
172 { 0x8e, KEY_VENDOR },
173 { 0x96, KEY_COFFEE },
174 { 0xa9, BTN_LEFT },
175 { 0xaa, BTN_RIGHT },
176 { 0xbe, KEY_QUESTION },
177 { 0xd0, KEY_EDIT },
178 { 0xd5, KEY_FRONT },
179 { 0xf9, KEY_INFO },
180};
181
182struct ati_remote2 {
183 struct input_dev *idev;
184 struct usb_device *udev;
185
186 struct usb_interface *intf[2];
187 struct usb_endpoint_descriptor *ep[2];
188 struct urb *urb[2];
189 void *buf[2];
190 dma_addr_t buf_dma[2];
191
192 unsigned long jiffies;
193 int mode;
194
195 char name[64];
196 char phys[64];
197
198 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
199 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
200
201 unsigned int flags;
202
203 unsigned int channel_mask;
204 unsigned int mode_mask;
205};
206
207static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
208static void ati_remote2_disconnect(struct usb_interface *interface);
209static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message);
210static int ati_remote2_resume(struct usb_interface *interface);
211static int ati_remote2_reset_resume(struct usb_interface *interface);
212static int ati_remote2_pre_reset(struct usb_interface *interface);
213static int ati_remote2_post_reset(struct usb_interface *interface);
214
215static struct usb_driver ati_remote2_driver = {
216 .name = "ati_remote2",
217 .probe = ati_remote2_probe,
218 .disconnect = ati_remote2_disconnect,
219 .id_table = ati_remote2_id_table,
220 .suspend = ati_remote2_suspend,
221 .resume = ati_remote2_resume,
222 .reset_resume = ati_remote2_reset_resume,
223 .pre_reset = ati_remote2_pre_reset,
224 .post_reset = ati_remote2_post_reset,
225 .supports_autosuspend = 1,
226};
227
228static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
229{
230 int r;
231
232 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
233 if (r) {
234 dev_err(&ar2->intf[0]->dev,
235 "%s(): usb_submit_urb() = %d\n", __func__, r);
236 return r;
237 }
238 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
239 if (r) {
240 usb_kill_urb(ar2->urb[0]);
241 dev_err(&ar2->intf[1]->dev,
242 "%s(): usb_submit_urb() = %d\n", __func__, r);
243 return r;
244 }
245
246 return 0;
247}
248
249static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)
250{
251 usb_kill_urb(ar2->urb[1]);
252 usb_kill_urb(ar2->urb[0]);
253}
254
255static int ati_remote2_open(struct input_dev *idev)
256{
257 struct ati_remote2 *ar2 = input_get_drvdata(idev);
258 int r;
259
260 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
261
262 r = usb_autopm_get_interface(ar2->intf[0]);
263 if (r) {
264 dev_err(&ar2->intf[0]->dev,
265 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
266 goto fail1;
267 }
268
269 mutex_lock(&ati_remote2_mutex);
270
271 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
272 r = ati_remote2_submit_urbs(ar2);
273 if (r)
274 goto fail2;
275 }
276
277 ar2->flags |= ATI_REMOTE2_OPENED;
278
279 mutex_unlock(&ati_remote2_mutex);
280
281 usb_autopm_put_interface(ar2->intf[0]);
282
283 return 0;
284
285 fail2:
286 mutex_unlock(&ati_remote2_mutex);
287 usb_autopm_put_interface(ar2->intf[0]);
288 fail1:
289 return r;
290}
291
292static void ati_remote2_close(struct input_dev *idev)
293{
294 struct ati_remote2 *ar2 = input_get_drvdata(idev);
295
296 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
297
298 mutex_lock(&ati_remote2_mutex);
299
300 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
301 ati_remote2_kill_urbs(ar2);
302
303 ar2->flags &= ~ATI_REMOTE2_OPENED;
304
305 mutex_unlock(&ati_remote2_mutex);
306}
307
308static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
309{
310 struct input_dev *idev = ar2->idev;
311 u8 *data = ar2->buf[0];
312 int channel, mode;
313
314 channel = data[0] >> 4;
315
316 if (!((1 << channel) & ar2->channel_mask))
317 return;
318
319 mode = data[0] & 0x0F;
320
321 if (mode > ATI_REMOTE2_PC) {
322 dev_err(&ar2->intf[0]->dev,
323 "Unknown mode byte (%02x %02x %02x %02x)\n",
324 data[3], data[2], data[1], data[0]);
325 return;
326 }
327
328 if (!((1 << mode) & ar2->mode_mask))
329 return;
330
331 input_event(idev, EV_REL, REL_X, (s8) data[1]);
332 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
333 input_sync(idev);
334}
335
336static int ati_remote2_lookup(unsigned int hw_code)
337{
338 int i;
339
340 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
341 if (ati_remote2_key_table[i].hw_code == hw_code)
342 return i;
343
344 return -1;
345}
346
347static void ati_remote2_input_key(struct ati_remote2 *ar2)
348{
349 struct input_dev *idev = ar2->idev;
350 u8 *data = ar2->buf[1];
351 int channel, mode, hw_code, index;
352
353 channel = data[0] >> 4;
354
355 if (!((1 << channel) & ar2->channel_mask))
356 return;
357
358 mode = data[0] & 0x0F;
359
360 if (mode > ATI_REMOTE2_PC) {
361 dev_err(&ar2->intf[1]->dev,
362 "Unknown mode byte (%02x %02x %02x %02x)\n",
363 data[3], data[2], data[1], data[0]);
364 return;
365 }
366
367 hw_code = data[2];
368 if (hw_code == 0x3f) {
369 /*
370 * For some incomprehensible reason the mouse pad generates
371 * events which look identical to the events from the last
372 * pressed mode key. Naturally we don't want to generate key
373 * events for the mouse pad so we filter out any subsequent
374 * events from the same mode key.
375 */
376 if (ar2->mode == mode)
377 return;
378
379 if (data[1] == 0)
380 ar2->mode = mode;
381 }
382
383 if (!((1 << mode) & ar2->mode_mask))
384 return;
385
386 index = ati_remote2_lookup(hw_code);
387 if (index < 0) {
388 dev_err(&ar2->intf[1]->dev,
389 "Unknown code byte (%02x %02x %02x %02x)\n",
390 data[3], data[2], data[1], data[0]);
391 return;
392 }
393
394 switch (data[1]) {
395 case 0: /* release */
396 break;
397 case 1: /* press */
398 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
399 break;
400 case 2: /* repeat */
401
402 /* No repeat for mouse buttons. */
403 if (ar2->keycode[mode][index] == BTN_LEFT ||
404 ar2->keycode[mode][index] == BTN_RIGHT)
405 return;
406
407 if (!time_after_eq(jiffies, ar2->jiffies))
408 return;
409
410 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
411 break;
412 default:
413 dev_err(&ar2->intf[1]->dev,
414 "Unknown state byte (%02x %02x %02x %02x)\n",
415 data[3], data[2], data[1], data[0]);
416 return;
417 }
418
419 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
420 input_sync(idev);
421}
422
423static void ati_remote2_complete_mouse(struct urb *urb)
424{
425 struct ati_remote2 *ar2 = urb->context;
426 int r;
427
428 switch (urb->status) {
429 case 0:
430 usb_mark_last_busy(ar2->udev);
431 ati_remote2_input_mouse(ar2);
432 break;
433 case -ENOENT:
434 case -EILSEQ:
435 case -ECONNRESET:
436 case -ESHUTDOWN:
437 dev_dbg(&ar2->intf[0]->dev,
438 "%s(): urb status = %d\n", __func__, urb->status);
439 return;
440 default:
441 usb_mark_last_busy(ar2->udev);
442 dev_err(&ar2->intf[0]->dev,
443 "%s(): urb status = %d\n", __func__, urb->status);
444 }
445
446 r = usb_submit_urb(urb, GFP_ATOMIC);
447 if (r)
448 dev_err(&ar2->intf[0]->dev,
449 "%s(): usb_submit_urb() = %d\n", __func__, r);
450}
451
452static void ati_remote2_complete_key(struct urb *urb)
453{
454 struct ati_remote2 *ar2 = urb->context;
455 int r;
456
457 switch (urb->status) {
458 case 0:
459 usb_mark_last_busy(ar2->udev);
460 ati_remote2_input_key(ar2);
461 break;
462 case -ENOENT:
463 case -EILSEQ:
464 case -ECONNRESET:
465 case -ESHUTDOWN:
466 dev_dbg(&ar2->intf[1]->dev,
467 "%s(): urb status = %d\n", __func__, urb->status);
468 return;
469 default:
470 usb_mark_last_busy(ar2->udev);
471 dev_err(&ar2->intf[1]->dev,
472 "%s(): urb status = %d\n", __func__, urb->status);
473 }
474
475 r = usb_submit_urb(urb, GFP_ATOMIC);
476 if (r)
477 dev_err(&ar2->intf[1]->dev,
478 "%s(): usb_submit_urb() = %d\n", __func__, r);
479}
480
481static int ati_remote2_getkeycode(struct input_dev *idev,
482 struct input_keymap_entry *ke)
483{
484 struct ati_remote2 *ar2 = input_get_drvdata(idev);
485 unsigned int mode;
486 int offset;
487 unsigned int index;
488 unsigned int scancode;
489
490 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
491 index = ke->index;
492 if (index >= ATI_REMOTE2_MODES *
493 ARRAY_SIZE(ati_remote2_key_table))
494 return -EINVAL;
495
496 mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);
497 offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);
498 scancode = (mode << 8) + ati_remote2_key_table[offset].hw_code;
499 } else {
500 if (input_scancode_to_scalar(ke, &scancode))
501 return -EINVAL;
502
503 mode = scancode >> 8;
504 if (mode > ATI_REMOTE2_PC)
505 return -EINVAL;
506
507 offset = ati_remote2_lookup(scancode & 0xff);
508 if (offset < 0)
509 return -EINVAL;
510
511 index = mode * ARRAY_SIZE(ati_remote2_key_table) + offset;
512 }
513
514 ke->keycode = ar2->keycode[mode][offset];
515 ke->len = sizeof(scancode);
516 memcpy(&ke->scancode, &scancode, sizeof(scancode));
517 ke->index = index;
518
519 return 0;
520}
521
522static int ati_remote2_setkeycode(struct input_dev *idev,
523 const struct input_keymap_entry *ke,
524 unsigned int *old_keycode)
525{
526 struct ati_remote2 *ar2 = input_get_drvdata(idev);
527 unsigned int mode;
528 int offset;
529 unsigned int index;
530 unsigned int scancode;
531
532 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
533 if (ke->index >= ATI_REMOTE2_MODES *
534 ARRAY_SIZE(ati_remote2_key_table))
535 return -EINVAL;
536
537 mode = ke->index / ARRAY_SIZE(ati_remote2_key_table);
538 offset = ke->index % ARRAY_SIZE(ati_remote2_key_table);
539 } else {
540 if (input_scancode_to_scalar(ke, &scancode))
541 return -EINVAL;
542
543 mode = scancode >> 8;
544 if (mode > ATI_REMOTE2_PC)
545 return -EINVAL;
546
547 offset = ati_remote2_lookup(scancode & 0xff);
548 if (offset < 0)
549 return -EINVAL;
550 }
551
552 *old_keycode = ar2->keycode[mode][offset];
553 ar2->keycode[mode][offset] = ke->keycode;
554 __set_bit(ke->keycode, idev->keybit);
555
556 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
557 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
558 if (ar2->keycode[mode][index] == *old_keycode)
559 return 0;
560 }
561 }
562
563 __clear_bit(*old_keycode, idev->keybit);
564
565 return 0;
566}
567
568static int ati_remote2_input_init(struct ati_remote2 *ar2)
569{
570 struct input_dev *idev;
571 int index, mode, retval;
572
573 idev = input_allocate_device();
574 if (!idev)
575 return -ENOMEM;
576
577 ar2->idev = idev;
578 input_set_drvdata(idev, ar2);
579
580 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
581 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
582 BIT_MASK(BTN_RIGHT);
583 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
584
585 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
586 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
587 ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
588 __set_bit(ar2->keycode[mode][index], idev->keybit);
589 }
590 }
591
592 /* AUX1-AUX4 and PC generate the same scancode. */
593 index = ati_remote2_lookup(0x3f);
594 ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
595 ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
596 ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
597 ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
598 ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
599 __set_bit(KEY_PROG1, idev->keybit);
600 __set_bit(KEY_PROG2, idev->keybit);
601 __set_bit(KEY_PROG3, idev->keybit);
602 __set_bit(KEY_PROG4, idev->keybit);
603 __set_bit(KEY_PC, idev->keybit);
604
605 idev->rep[REP_DELAY] = 250;
606 idev->rep[REP_PERIOD] = 33;
607
608 idev->open = ati_remote2_open;
609 idev->close = ati_remote2_close;
610
611 idev->getkeycode = ati_remote2_getkeycode;
612 idev->setkeycode = ati_remote2_setkeycode;
613
614 idev->name = ar2->name;
615 idev->phys = ar2->phys;
616
617 usb_to_input_id(ar2->udev, &idev->id);
618 idev->dev.parent = &ar2->udev->dev;
619
620 retval = input_register_device(idev);
621 if (retval)
622 input_free_device(idev);
623
624 return retval;
625}
626
627static int ati_remote2_urb_init(struct ati_remote2 *ar2)
628{
629 struct usb_device *udev = ar2->udev;
630 int i, pipe, maxp;
631
632 for (i = 0; i < 2; i++) {
633 ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
634 if (!ar2->buf[i])
635 return -ENOMEM;
636
637 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
638 if (!ar2->urb[i])
639 return -ENOMEM;
640
641 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
642 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
643 maxp = maxp > 4 ? 4 : maxp;
644
645 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
646 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
647 ar2, ar2->ep[i]->bInterval);
648 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
649 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
650 }
651
652 return 0;
653}
654
655static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
656{
657 int i;
658
659 for (i = 0; i < 2; i++) {
660 usb_free_urb(ar2->urb[i]);
661 usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
662 }
663}
664
665static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
666{
667 int r, i, channel;
668
669 /*
670 * Configure receiver to only accept input from remote "channel"
671 * channel == 0 -> Accept input from any remote channel
672 * channel == 1 -> Only accept input from remote channel 1
673 * channel == 2 -> Only accept input from remote channel 2
674 * ...
675 * channel == 16 -> Only accept input from remote channel 16
676 */
677
678 channel = 0;
679 for (i = 0; i < 16; i++) {
680 if ((1 << i) & ch_mask) {
681 if (!(~(1 << i) & ch_mask))
682 channel = i + 1;
683 break;
684 }
685 }
686
687 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
688 0x20,
689 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
690 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
691 if (r) {
692 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
693 __func__, r);
694 return r;
695 }
696
697 return 0;
698}
699
700static ssize_t ati_remote2_show_channel_mask(struct device *dev,
701 struct device_attribute *attr,
702 char *buf)
703{
704 struct usb_device *udev = to_usb_device(dev);
705 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
706 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
707
708 return sprintf(buf, "0x%04x\n", ar2->channel_mask);
709}
710
711static ssize_t ati_remote2_store_channel_mask(struct device *dev,
712 struct device_attribute *attr,
713 const char *buf, size_t count)
714{
715 struct usb_device *udev = to_usb_device(dev);
716 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
717 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
718 unsigned int mask;
719 int r;
720
721 r = kstrtouint(buf, 0, &mask);
722 if (r)
723 return r;
724
725 if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
726 return -EINVAL;
727
728 r = usb_autopm_get_interface(ar2->intf[0]);
729 if (r) {
730 dev_err(&ar2->intf[0]->dev,
731 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
732 return r;
733 }
734
735 mutex_lock(&ati_remote2_mutex);
736
737 if (mask != ar2->channel_mask) {
738 r = ati_remote2_setup(ar2, mask);
739 if (!r)
740 ar2->channel_mask = mask;
741 }
742
743 mutex_unlock(&ati_remote2_mutex);
744
745 usb_autopm_put_interface(ar2->intf[0]);
746
747 return r ? r : count;
748}
749
750static ssize_t ati_remote2_show_mode_mask(struct device *dev,
751 struct device_attribute *attr,
752 char *buf)
753{
754 struct usb_device *udev = to_usb_device(dev);
755 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
756 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
757
758 return sprintf(buf, "0x%02x\n", ar2->mode_mask);
759}
760
761static ssize_t ati_remote2_store_mode_mask(struct device *dev,
762 struct device_attribute *attr,
763 const char *buf, size_t count)
764{
765 struct usb_device *udev = to_usb_device(dev);
766 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
767 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
768 unsigned int mask;
769 int err;
770
771 err = kstrtouint(buf, 0, &mask);
772 if (err)
773 return err;
774
775 if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
776 return -EINVAL;
777
778 ar2->mode_mask = mask;
779
780 return count;
781}
782
783static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
784 ati_remote2_store_channel_mask);
785
786static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
787 ati_remote2_store_mode_mask);
788
789static struct attribute *ati_remote2_attrs[] = {
790 &dev_attr_channel_mask.attr,
791 &dev_attr_mode_mask.attr,
792 NULL,
793};
794
795static struct attribute_group ati_remote2_attr_group = {
796 .attrs = ati_remote2_attrs,
797};
798
799static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
800{
801 struct usb_device *udev = interface_to_usbdev(interface);
802 struct usb_host_interface *alt = interface->cur_altsetting;
803 struct ati_remote2 *ar2;
804 int r;
805
806 if (alt->desc.bInterfaceNumber)
807 return -ENODEV;
808
809 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
810 if (!ar2)
811 return -ENOMEM;
812
813 ar2->udev = udev;
814
815 /* Sanity check, first interface must have an endpoint */
816 if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) {
817 dev_err(&interface->dev,
818 "%s(): interface 0 must have an endpoint\n", __func__);
819 r = -ENODEV;
820 goto fail1;
821 }
822 ar2->intf[0] = interface;
823 ar2->ep[0] = &alt->endpoint[0].desc;
824
825 /* Sanity check, the device must have two interfaces */
826 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
827 if ((udev->actconfig->desc.bNumInterfaces < 2) || !ar2->intf[1]) {
828 dev_err(&interface->dev, "%s(): need 2 interfaces, found %d\n",
829 __func__, udev->actconfig->desc.bNumInterfaces);
830 r = -ENODEV;
831 goto fail1;
832 }
833
834 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
835 if (r)
836 goto fail1;
837
838 /* Sanity check, second interface must have an endpoint */
839 alt = ar2->intf[1]->cur_altsetting;
840 if (alt->desc.bNumEndpoints < 1 || !alt->endpoint) {
841 dev_err(&interface->dev,
842 "%s(): interface 1 must have an endpoint\n", __func__);
843 r = -ENODEV;
844 goto fail2;
845 }
846 ar2->ep[1] = &alt->endpoint[0].desc;
847
848 r = ati_remote2_urb_init(ar2);
849 if (r)
850 goto fail3;
851
852 ar2->channel_mask = channel_mask;
853 ar2->mode_mask = mode_mask;
854
855 r = ati_remote2_setup(ar2, ar2->channel_mask);
856 if (r)
857 goto fail3;
858
859 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
860 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
861
862 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
863
864 r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);
865 if (r)
866 goto fail3;
867
868 r = ati_remote2_input_init(ar2);
869 if (r)
870 goto fail4;
871
872 usb_set_intfdata(interface, ar2);
873
874 interface->needs_remote_wakeup = 1;
875
876 return 0;
877
878 fail4:
879 sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group);
880 fail3:
881 ati_remote2_urb_cleanup(ar2);
882 fail2:
883 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
884 fail1:
885 kfree(ar2);
886
887 return r;
888}
889
890static void ati_remote2_disconnect(struct usb_interface *interface)
891{
892 struct ati_remote2 *ar2;
893 struct usb_host_interface *alt = interface->cur_altsetting;
894
895 if (alt->desc.bInterfaceNumber)
896 return;
897
898 ar2 = usb_get_intfdata(interface);
899 usb_set_intfdata(interface, NULL);
900
901 input_unregister_device(ar2->idev);
902
903 sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);
904
905 ati_remote2_urb_cleanup(ar2);
906
907 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
908
909 kfree(ar2);
910}
911
912static int ati_remote2_suspend(struct usb_interface *interface,
913 pm_message_t message)
914{
915 struct ati_remote2 *ar2;
916 struct usb_host_interface *alt = interface->cur_altsetting;
917
918 if (alt->desc.bInterfaceNumber)
919 return 0;
920
921 ar2 = usb_get_intfdata(interface);
922
923 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
924
925 mutex_lock(&ati_remote2_mutex);
926
927 if (ar2->flags & ATI_REMOTE2_OPENED)
928 ati_remote2_kill_urbs(ar2);
929
930 ar2->flags |= ATI_REMOTE2_SUSPENDED;
931
932 mutex_unlock(&ati_remote2_mutex);
933
934 return 0;
935}
936
937static int ati_remote2_resume(struct usb_interface *interface)
938{
939 struct ati_remote2 *ar2;
940 struct usb_host_interface *alt = interface->cur_altsetting;
941 int r = 0;
942
943 if (alt->desc.bInterfaceNumber)
944 return 0;
945
946 ar2 = usb_get_intfdata(interface);
947
948 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
949
950 mutex_lock(&ati_remote2_mutex);
951
952 if (ar2->flags & ATI_REMOTE2_OPENED)
953 r = ati_remote2_submit_urbs(ar2);
954
955 if (!r)
956 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
957
958 mutex_unlock(&ati_remote2_mutex);
959
960 return r;
961}
962
963static int ati_remote2_reset_resume(struct usb_interface *interface)
964{
965 struct ati_remote2 *ar2;
966 struct usb_host_interface *alt = interface->cur_altsetting;
967 int r = 0;
968
969 if (alt->desc.bInterfaceNumber)
970 return 0;
971
972 ar2 = usb_get_intfdata(interface);
973
974 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
975
976 mutex_lock(&ati_remote2_mutex);
977
978 r = ati_remote2_setup(ar2, ar2->channel_mask);
979 if (r)
980 goto out;
981
982 if (ar2->flags & ATI_REMOTE2_OPENED)
983 r = ati_remote2_submit_urbs(ar2);
984
985 if (!r)
986 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
987
988 out:
989 mutex_unlock(&ati_remote2_mutex);
990
991 return r;
992}
993
994static int ati_remote2_pre_reset(struct usb_interface *interface)
995{
996 struct ati_remote2 *ar2;
997 struct usb_host_interface *alt = interface->cur_altsetting;
998
999 if (alt->desc.bInterfaceNumber)
1000 return 0;
1001
1002 ar2 = usb_get_intfdata(interface);
1003
1004 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
1005
1006 mutex_lock(&ati_remote2_mutex);
1007
1008 if (ar2->flags == ATI_REMOTE2_OPENED)
1009 ati_remote2_kill_urbs(ar2);
1010
1011 return 0;
1012}
1013
1014static int ati_remote2_post_reset(struct usb_interface *interface)
1015{
1016 struct ati_remote2 *ar2;
1017 struct usb_host_interface *alt = interface->cur_altsetting;
1018 int r = 0;
1019
1020 if (alt->desc.bInterfaceNumber)
1021 return 0;
1022
1023 ar2 = usb_get_intfdata(interface);
1024
1025 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
1026
1027 if (ar2->flags == ATI_REMOTE2_OPENED)
1028 r = ati_remote2_submit_urbs(ar2);
1029
1030 mutex_unlock(&ati_remote2_mutex);
1031
1032 return r;
1033}
1034
1035module_usb_driver(ati_remote2_driver);