ASoC: Remove duplicate ADC/DAC widgets from wm_hubs.c
[linux/fpc-iii.git] / drivers / input / misc / ati_remote2.c
blob0501f0e65157bdfb1e1d90fd7ff1846d10c97eb0
1 /*
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 enum {
35 ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,
36 ATI_REMOTE2_MAX_MODE_MASK = 0x1F,
39 static int ati_remote2_set_mask(const char *val,
40 struct kernel_param *kp, unsigned int max)
42 unsigned long mask;
43 int ret;
45 if (!val)
46 return -EINVAL;
48 ret = strict_strtoul(val, 0, &mask);
49 if (ret)
50 return ret;
52 if (mask & ~max)
53 return -EINVAL;
55 *(unsigned int *)kp->arg = mask;
57 return 0;
60 static int ati_remote2_set_channel_mask(const char *val,
61 struct kernel_param *kp)
63 pr_debug("%s()\n", __func__);
65 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK);
68 static int ati_remote2_get_channel_mask(char *buffer, struct kernel_param *kp)
70 pr_debug("%s()\n", __func__);
72 return sprintf(buffer, "0x%04x", *(unsigned int *)kp->arg);
75 static int ati_remote2_set_mode_mask(const char *val, struct kernel_param *kp)
77 pr_debug("%s()\n", __func__);
79 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK);
82 static int ati_remote2_get_mode_mask(char *buffer, struct kernel_param *kp)
84 pr_debug("%s()\n", __func__);
86 return sprintf(buffer, "0x%02x", *(unsigned int *)kp->arg);
89 static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK;
90 #define param_check_channel_mask(name, p) __param_check(name, p, unsigned int)
91 #define param_set_channel_mask ati_remote2_set_channel_mask
92 #define param_get_channel_mask ati_remote2_get_channel_mask
93 module_param(channel_mask, channel_mask, 0644);
94 MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
96 static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK;
97 #define param_check_mode_mask(name, p) __param_check(name, p, unsigned int)
98 #define param_set_mode_mask ati_remote2_set_mode_mask
99 #define param_get_mode_mask ati_remote2_get_mode_mask
100 module_param(mode_mask, mode_mask, 0644);
101 MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
103 static struct usb_device_id ati_remote2_id_table[] = {
104 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
107 MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
109 static DEFINE_MUTEX(ati_remote2_mutex);
111 enum {
112 ATI_REMOTE2_OPENED = 0x1,
113 ATI_REMOTE2_SUSPENDED = 0x2,
116 enum {
117 ATI_REMOTE2_AUX1,
118 ATI_REMOTE2_AUX2,
119 ATI_REMOTE2_AUX3,
120 ATI_REMOTE2_AUX4,
121 ATI_REMOTE2_PC,
122 ATI_REMOTE2_MODES,
125 static const struct {
126 u8 hw_code;
127 u16 keycode;
128 } ati_remote2_key_table[] = {
129 { 0x00, KEY_0 },
130 { 0x01, KEY_1 },
131 { 0x02, KEY_2 },
132 { 0x03, KEY_3 },
133 { 0x04, KEY_4 },
134 { 0x05, KEY_5 },
135 { 0x06, KEY_6 },
136 { 0x07, KEY_7 },
137 { 0x08, KEY_8 },
138 { 0x09, KEY_9 },
139 { 0x0c, KEY_POWER },
140 { 0x0d, KEY_MUTE },
141 { 0x10, KEY_VOLUMEUP },
142 { 0x11, KEY_VOLUMEDOWN },
143 { 0x20, KEY_CHANNELUP },
144 { 0x21, KEY_CHANNELDOWN },
145 { 0x28, KEY_FORWARD },
146 { 0x29, KEY_REWIND },
147 { 0x2c, KEY_PLAY },
148 { 0x30, KEY_PAUSE },
149 { 0x31, KEY_STOP },
150 { 0x37, KEY_RECORD },
151 { 0x38, KEY_DVD },
152 { 0x39, KEY_TV },
153 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */
154 { 0x54, KEY_MENU },
155 { 0x58, KEY_UP },
156 { 0x59, KEY_DOWN },
157 { 0x5a, KEY_LEFT },
158 { 0x5b, KEY_RIGHT },
159 { 0x5c, KEY_OK },
160 { 0x78, KEY_A },
161 { 0x79, KEY_B },
162 { 0x7a, KEY_C },
163 { 0x7b, KEY_D },
164 { 0x7c, KEY_E },
165 { 0x7d, KEY_F },
166 { 0x82, KEY_ENTER },
167 { 0x8e, KEY_VENDOR },
168 { 0x96, KEY_COFFEE },
169 { 0xa9, BTN_LEFT },
170 { 0xaa, BTN_RIGHT },
171 { 0xbe, KEY_QUESTION },
172 { 0xd0, KEY_EDIT },
173 { 0xd5, KEY_FRONT },
174 { 0xf9, KEY_INFO },
177 struct ati_remote2 {
178 struct input_dev *idev;
179 struct usb_device *udev;
181 struct usb_interface *intf[2];
182 struct usb_endpoint_descriptor *ep[2];
183 struct urb *urb[2];
184 void *buf[2];
185 dma_addr_t buf_dma[2];
187 unsigned long jiffies;
188 int mode;
190 char name[64];
191 char phys[64];
193 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
194 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
196 unsigned int flags;
198 unsigned int channel_mask;
199 unsigned int mode_mask;
202 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
203 static void ati_remote2_disconnect(struct usb_interface *interface);
204 static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message);
205 static int ati_remote2_resume(struct usb_interface *interface);
206 static int ati_remote2_reset_resume(struct usb_interface *interface);
207 static int ati_remote2_pre_reset(struct usb_interface *interface);
208 static int ati_remote2_post_reset(struct usb_interface *interface);
210 static struct usb_driver ati_remote2_driver = {
211 .name = "ati_remote2",
212 .probe = ati_remote2_probe,
213 .disconnect = ati_remote2_disconnect,
214 .id_table = ati_remote2_id_table,
215 .suspend = ati_remote2_suspend,
216 .resume = ati_remote2_resume,
217 .reset_resume = ati_remote2_reset_resume,
218 .pre_reset = ati_remote2_pre_reset,
219 .post_reset = ati_remote2_post_reset,
220 .supports_autosuspend = 1,
223 static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
225 int r;
227 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
228 if (r) {
229 dev_err(&ar2->intf[0]->dev,
230 "%s(): usb_submit_urb() = %d\n", __func__, r);
231 return r;
233 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
234 if (r) {
235 usb_kill_urb(ar2->urb[0]);
236 dev_err(&ar2->intf[1]->dev,
237 "%s(): usb_submit_urb() = %d\n", __func__, r);
238 return r;
241 return 0;
244 static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)
246 usb_kill_urb(ar2->urb[1]);
247 usb_kill_urb(ar2->urb[0]);
250 static int ati_remote2_open(struct input_dev *idev)
252 struct ati_remote2 *ar2 = input_get_drvdata(idev);
253 int r;
255 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
257 r = usb_autopm_get_interface(ar2->intf[0]);
258 if (r) {
259 dev_err(&ar2->intf[0]->dev,
260 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
261 goto fail1;
264 mutex_lock(&ati_remote2_mutex);
266 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
267 r = ati_remote2_submit_urbs(ar2);
268 if (r)
269 goto fail2;
272 ar2->flags |= ATI_REMOTE2_OPENED;
274 mutex_unlock(&ati_remote2_mutex);
276 usb_autopm_put_interface(ar2->intf[0]);
278 return 0;
280 fail2:
281 mutex_unlock(&ati_remote2_mutex);
282 usb_autopm_put_interface(ar2->intf[0]);
283 fail1:
284 return r;
287 static void ati_remote2_close(struct input_dev *idev)
289 struct ati_remote2 *ar2 = input_get_drvdata(idev);
291 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
293 mutex_lock(&ati_remote2_mutex);
295 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
296 ati_remote2_kill_urbs(ar2);
298 ar2->flags &= ~ATI_REMOTE2_OPENED;
300 mutex_unlock(&ati_remote2_mutex);
303 static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
305 struct input_dev *idev = ar2->idev;
306 u8 *data = ar2->buf[0];
307 int channel, mode;
309 channel = data[0] >> 4;
311 if (!((1 << channel) & ar2->channel_mask))
312 return;
314 mode = data[0] & 0x0F;
316 if (mode > ATI_REMOTE2_PC) {
317 dev_err(&ar2->intf[0]->dev,
318 "Unknown mode byte (%02x %02x %02x %02x)\n",
319 data[3], data[2], data[1], data[0]);
320 return;
323 if (!((1 << mode) & ar2->mode_mask))
324 return;
326 input_event(idev, EV_REL, REL_X, (s8) data[1]);
327 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
328 input_sync(idev);
331 static int ati_remote2_lookup(unsigned int hw_code)
333 int i;
335 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
336 if (ati_remote2_key_table[i].hw_code == hw_code)
337 return i;
339 return -1;
342 static void ati_remote2_input_key(struct ati_remote2 *ar2)
344 struct input_dev *idev = ar2->idev;
345 u8 *data = ar2->buf[1];
346 int channel, mode, hw_code, index;
348 channel = data[0] >> 4;
350 if (!((1 << channel) & ar2->channel_mask))
351 return;
353 mode = data[0] & 0x0F;
355 if (mode > ATI_REMOTE2_PC) {
356 dev_err(&ar2->intf[1]->dev,
357 "Unknown mode byte (%02x %02x %02x %02x)\n",
358 data[3], data[2], data[1], data[0]);
359 return;
362 hw_code = data[2];
363 if (hw_code == 0x3f) {
365 * For some incomprehensible reason the mouse pad generates
366 * events which look identical to the events from the last
367 * pressed mode key. Naturally we don't want to generate key
368 * events for the mouse pad so we filter out any subsequent
369 * events from the same mode key.
371 if (ar2->mode == mode)
372 return;
374 if (data[1] == 0)
375 ar2->mode = mode;
378 if (!((1 << mode) & ar2->mode_mask))
379 return;
381 index = ati_remote2_lookup(hw_code);
382 if (index < 0) {
383 dev_err(&ar2->intf[1]->dev,
384 "Unknown code byte (%02x %02x %02x %02x)\n",
385 data[3], data[2], data[1], data[0]);
386 return;
389 switch (data[1]) {
390 case 0: /* release */
391 break;
392 case 1: /* press */
393 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
394 break;
395 case 2: /* repeat */
397 /* No repeat for mouse buttons. */
398 if (ar2->keycode[mode][index] == BTN_LEFT ||
399 ar2->keycode[mode][index] == BTN_RIGHT)
400 return;
402 if (!time_after_eq(jiffies, ar2->jiffies))
403 return;
405 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
406 break;
407 default:
408 dev_err(&ar2->intf[1]->dev,
409 "Unknown state byte (%02x %02x %02x %02x)\n",
410 data[3], data[2], data[1], data[0]);
411 return;
414 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
415 input_sync(idev);
418 static void ati_remote2_complete_mouse(struct urb *urb)
420 struct ati_remote2 *ar2 = urb->context;
421 int r;
423 switch (urb->status) {
424 case 0:
425 usb_mark_last_busy(ar2->udev);
426 ati_remote2_input_mouse(ar2);
427 break;
428 case -ENOENT:
429 case -EILSEQ:
430 case -ECONNRESET:
431 case -ESHUTDOWN:
432 dev_dbg(&ar2->intf[0]->dev,
433 "%s(): urb status = %d\n", __func__, urb->status);
434 return;
435 default:
436 usb_mark_last_busy(ar2->udev);
437 dev_err(&ar2->intf[0]->dev,
438 "%s(): urb status = %d\n", __func__, urb->status);
441 r = usb_submit_urb(urb, GFP_ATOMIC);
442 if (r)
443 dev_err(&ar2->intf[0]->dev,
444 "%s(): usb_submit_urb() = %d\n", __func__, r);
447 static void ati_remote2_complete_key(struct urb *urb)
449 struct ati_remote2 *ar2 = urb->context;
450 int r;
452 switch (urb->status) {
453 case 0:
454 usb_mark_last_busy(ar2->udev);
455 ati_remote2_input_key(ar2);
456 break;
457 case -ENOENT:
458 case -EILSEQ:
459 case -ECONNRESET:
460 case -ESHUTDOWN:
461 dev_dbg(&ar2->intf[1]->dev,
462 "%s(): urb status = %d\n", __func__, urb->status);
463 return;
464 default:
465 usb_mark_last_busy(ar2->udev);
466 dev_err(&ar2->intf[1]->dev,
467 "%s(): urb status = %d\n", __func__, urb->status);
470 r = usb_submit_urb(urb, GFP_ATOMIC);
471 if (r)
472 dev_err(&ar2->intf[1]->dev,
473 "%s(): usb_submit_urb() = %d\n", __func__, r);
476 static int ati_remote2_getkeycode(struct input_dev *idev,
477 int scancode, int *keycode)
479 struct ati_remote2 *ar2 = input_get_drvdata(idev);
480 int index, mode;
482 mode = scancode >> 8;
483 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
484 return -EINVAL;
486 index = ati_remote2_lookup(scancode & 0xFF);
487 if (index < 0)
488 return -EINVAL;
490 *keycode = ar2->keycode[mode][index];
491 return 0;
494 static int ati_remote2_setkeycode(struct input_dev *idev, int scancode, int keycode)
496 struct ati_remote2 *ar2 = input_get_drvdata(idev);
497 int index, mode, old_keycode;
499 mode = scancode >> 8;
500 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
501 return -EINVAL;
503 index = ati_remote2_lookup(scancode & 0xFF);
504 if (index < 0)
505 return -EINVAL;
507 if (keycode < KEY_RESERVED || keycode > KEY_MAX)
508 return -EINVAL;
510 old_keycode = ar2->keycode[mode][index];
511 ar2->keycode[mode][index] = keycode;
512 __set_bit(keycode, idev->keybit);
514 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
515 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
516 if (ar2->keycode[mode][index] == old_keycode)
517 return 0;
521 __clear_bit(old_keycode, idev->keybit);
523 return 0;
526 static int ati_remote2_input_init(struct ati_remote2 *ar2)
528 struct input_dev *idev;
529 int index, mode, retval;
531 idev = input_allocate_device();
532 if (!idev)
533 return -ENOMEM;
535 ar2->idev = idev;
536 input_set_drvdata(idev, ar2);
538 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
539 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
540 BIT_MASK(BTN_RIGHT);
541 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
543 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
544 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
545 ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
546 __set_bit(ar2->keycode[mode][index], idev->keybit);
550 /* AUX1-AUX4 and PC generate the same scancode. */
551 index = ati_remote2_lookup(0x3f);
552 ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
553 ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
554 ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
555 ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
556 ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
557 __set_bit(KEY_PROG1, idev->keybit);
558 __set_bit(KEY_PROG2, idev->keybit);
559 __set_bit(KEY_PROG3, idev->keybit);
560 __set_bit(KEY_PROG4, idev->keybit);
561 __set_bit(KEY_PC, idev->keybit);
563 idev->rep[REP_DELAY] = 250;
564 idev->rep[REP_PERIOD] = 33;
566 idev->open = ati_remote2_open;
567 idev->close = ati_remote2_close;
569 idev->getkeycode = ati_remote2_getkeycode;
570 idev->setkeycode = ati_remote2_setkeycode;
572 idev->name = ar2->name;
573 idev->phys = ar2->phys;
575 usb_to_input_id(ar2->udev, &idev->id);
576 idev->dev.parent = &ar2->udev->dev;
578 retval = input_register_device(idev);
579 if (retval)
580 input_free_device(idev);
582 return retval;
585 static int ati_remote2_urb_init(struct ati_remote2 *ar2)
587 struct usb_device *udev = ar2->udev;
588 int i, pipe, maxp;
590 for (i = 0; i < 2; i++) {
591 ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
592 if (!ar2->buf[i])
593 return -ENOMEM;
595 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
596 if (!ar2->urb[i])
597 return -ENOMEM;
599 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
600 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
601 maxp = maxp > 4 ? 4 : maxp;
603 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
604 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
605 ar2, ar2->ep[i]->bInterval);
606 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
607 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
610 return 0;
613 static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
615 int i;
617 for (i = 0; i < 2; i++) {
618 usb_free_urb(ar2->urb[i]);
619 usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
623 static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
625 int r, i, channel;
628 * Configure receiver to only accept input from remote "channel"
629 * channel == 0 -> Accept input from any remote channel
630 * channel == 1 -> Only accept input from remote channel 1
631 * channel == 2 -> Only accept input from remote channel 2
632 * ...
633 * channel == 16 -> Only accept input from remote channel 16
636 channel = 0;
637 for (i = 0; i < 16; i++) {
638 if ((1 << i) & ch_mask) {
639 if (!(~(1 << i) & ch_mask))
640 channel = i + 1;
641 break;
645 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
646 0x20,
647 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
648 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
649 if (r) {
650 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
651 __func__, r);
652 return r;
655 return 0;
658 static ssize_t ati_remote2_show_channel_mask(struct device *dev,
659 struct device_attribute *attr,
660 char *buf)
662 struct usb_device *udev = to_usb_device(dev);
663 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
664 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
666 return sprintf(buf, "0x%04x\n", ar2->channel_mask);
669 static ssize_t ati_remote2_store_channel_mask(struct device *dev,
670 struct device_attribute *attr,
671 const char *buf, size_t count)
673 struct usb_device *udev = to_usb_device(dev);
674 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
675 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
676 unsigned long mask;
677 int r;
679 if (strict_strtoul(buf, 0, &mask))
680 return -EINVAL;
682 if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
683 return -EINVAL;
685 r = usb_autopm_get_interface(ar2->intf[0]);
686 if (r) {
687 dev_err(&ar2->intf[0]->dev,
688 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
689 return r;
692 mutex_lock(&ati_remote2_mutex);
694 if (mask != ar2->channel_mask && !ati_remote2_setup(ar2, mask))
695 ar2->channel_mask = mask;
697 mutex_unlock(&ati_remote2_mutex);
699 usb_autopm_put_interface(ar2->intf[0]);
701 return count;
704 static ssize_t ati_remote2_show_mode_mask(struct device *dev,
705 struct device_attribute *attr,
706 char *buf)
708 struct usb_device *udev = to_usb_device(dev);
709 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
710 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
712 return sprintf(buf, "0x%02x\n", ar2->mode_mask);
715 static ssize_t ati_remote2_store_mode_mask(struct device *dev,
716 struct device_attribute *attr,
717 const char *buf, size_t count)
719 struct usb_device *udev = to_usb_device(dev);
720 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
721 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
722 unsigned long mask;
724 if (strict_strtoul(buf, 0, &mask))
725 return -EINVAL;
727 if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
728 return -EINVAL;
730 ar2->mode_mask = mask;
732 return count;
735 static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
736 ati_remote2_store_channel_mask);
738 static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
739 ati_remote2_store_mode_mask);
741 static struct attribute *ati_remote2_attrs[] = {
742 &dev_attr_channel_mask.attr,
743 &dev_attr_mode_mask.attr,
744 NULL,
747 static struct attribute_group ati_remote2_attr_group = {
748 .attrs = ati_remote2_attrs,
751 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
753 struct usb_device *udev = interface_to_usbdev(interface);
754 struct usb_host_interface *alt = interface->cur_altsetting;
755 struct ati_remote2 *ar2;
756 int r;
758 if (alt->desc.bInterfaceNumber)
759 return -ENODEV;
761 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
762 if (!ar2)
763 return -ENOMEM;
765 ar2->udev = udev;
767 ar2->intf[0] = interface;
768 ar2->ep[0] = &alt->endpoint[0].desc;
770 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
771 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
772 if (r)
773 goto fail1;
774 alt = ar2->intf[1]->cur_altsetting;
775 ar2->ep[1] = &alt->endpoint[0].desc;
777 r = ati_remote2_urb_init(ar2);
778 if (r)
779 goto fail2;
781 ar2->channel_mask = channel_mask;
782 ar2->mode_mask = mode_mask;
784 r = ati_remote2_setup(ar2, ar2->channel_mask);
785 if (r)
786 goto fail2;
788 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
789 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
791 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
793 r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);
794 if (r)
795 goto fail2;
797 r = ati_remote2_input_init(ar2);
798 if (r)
799 goto fail3;
801 usb_set_intfdata(interface, ar2);
803 interface->needs_remote_wakeup = 1;
805 return 0;
807 fail3:
808 sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group);
809 fail2:
810 ati_remote2_urb_cleanup(ar2);
811 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
812 fail1:
813 kfree(ar2);
815 return r;
818 static void ati_remote2_disconnect(struct usb_interface *interface)
820 struct ati_remote2 *ar2;
821 struct usb_host_interface *alt = interface->cur_altsetting;
823 if (alt->desc.bInterfaceNumber)
824 return;
826 ar2 = usb_get_intfdata(interface);
827 usb_set_intfdata(interface, NULL);
829 input_unregister_device(ar2->idev);
831 sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);
833 ati_remote2_urb_cleanup(ar2);
835 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
837 kfree(ar2);
840 static int ati_remote2_suspend(struct usb_interface *interface,
841 pm_message_t message)
843 struct ati_remote2 *ar2;
844 struct usb_host_interface *alt = interface->cur_altsetting;
846 if (alt->desc.bInterfaceNumber)
847 return 0;
849 ar2 = usb_get_intfdata(interface);
851 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
853 mutex_lock(&ati_remote2_mutex);
855 if (ar2->flags & ATI_REMOTE2_OPENED)
856 ati_remote2_kill_urbs(ar2);
858 ar2->flags |= ATI_REMOTE2_SUSPENDED;
860 mutex_unlock(&ati_remote2_mutex);
862 return 0;
865 static int ati_remote2_resume(struct usb_interface *interface)
867 struct ati_remote2 *ar2;
868 struct usb_host_interface *alt = interface->cur_altsetting;
869 int r = 0;
871 if (alt->desc.bInterfaceNumber)
872 return 0;
874 ar2 = usb_get_intfdata(interface);
876 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
878 mutex_lock(&ati_remote2_mutex);
880 if (ar2->flags & ATI_REMOTE2_OPENED)
881 r = ati_remote2_submit_urbs(ar2);
883 if (!r)
884 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
886 mutex_unlock(&ati_remote2_mutex);
888 return r;
891 static int ati_remote2_reset_resume(struct usb_interface *interface)
893 struct ati_remote2 *ar2;
894 struct usb_host_interface *alt = interface->cur_altsetting;
895 int r = 0;
897 if (alt->desc.bInterfaceNumber)
898 return 0;
900 ar2 = usb_get_intfdata(interface);
902 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
904 mutex_lock(&ati_remote2_mutex);
906 r = ati_remote2_setup(ar2, ar2->channel_mask);
907 if (r)
908 goto out;
910 if (ar2->flags & ATI_REMOTE2_OPENED)
911 r = ati_remote2_submit_urbs(ar2);
913 if (!r)
914 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
916 out:
917 mutex_unlock(&ati_remote2_mutex);
919 return r;
922 static int ati_remote2_pre_reset(struct usb_interface *interface)
924 struct ati_remote2 *ar2;
925 struct usb_host_interface *alt = interface->cur_altsetting;
927 if (alt->desc.bInterfaceNumber)
928 return 0;
930 ar2 = usb_get_intfdata(interface);
932 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
934 mutex_lock(&ati_remote2_mutex);
936 if (ar2->flags == ATI_REMOTE2_OPENED)
937 ati_remote2_kill_urbs(ar2);
939 return 0;
942 static int ati_remote2_post_reset(struct usb_interface *interface)
944 struct ati_remote2 *ar2;
945 struct usb_host_interface *alt = interface->cur_altsetting;
946 int r = 0;
948 if (alt->desc.bInterfaceNumber)
949 return 0;
951 ar2 = usb_get_intfdata(interface);
953 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
955 if (ar2->flags == ATI_REMOTE2_OPENED)
956 r = ati_remote2_submit_urbs(ar2);
958 mutex_unlock(&ati_remote2_mutex);
960 return r;
963 static int __init ati_remote2_init(void)
965 int r;
967 r = usb_register(&ati_remote2_driver);
968 if (r)
969 printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
970 else
971 printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
973 return r;
976 static void __exit ati_remote2_exit(void)
978 usb_deregister(&ati_remote2_driver);
981 module_init(ati_remote2_init);
982 module_exit(ati_remote2_exit);