USB: rename usb_buffer_alloc() and usb_buffer_free() users
[firewire-audio.git] / drivers / input / misc / ati_remote2.c
blobe148749b5851a6578a58848034896ddcaa27bec8
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>
13 #include <linux/slab.h>
15 #define DRIVER_DESC "ATI/Philips USB RF remote driver"
16 #define DRIVER_VERSION "0.3"
18 MODULE_DESCRIPTION(DRIVER_DESC);
19 MODULE_VERSION(DRIVER_VERSION);
20 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
21 MODULE_LICENSE("GPL");
24 * ATI Remote Wonder II Channel Configuration
26 * The remote control can by assigned one of sixteen "channels" in order to facilitate
27 * the use of multiple remote controls within range of each other.
28 * A remote's "channel" may be altered by pressing and holding the "PC" button for
29 * approximately 3 seconds, after which the button will slowly flash the count of the
30 * currently configured "channel", using the numeric keypad enter a number between 1 and
31 * 16 and then press the "PC" button again, the button will slowly flash the count of the
32 * newly configured "channel".
35 enum {
36 ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,
37 ATI_REMOTE2_MAX_MODE_MASK = 0x1F,
40 static int ati_remote2_set_mask(const char *val,
41 struct kernel_param *kp, unsigned int max)
43 unsigned long mask;
44 int ret;
46 if (!val)
47 return -EINVAL;
49 ret = strict_strtoul(val, 0, &mask);
50 if (ret)
51 return ret;
53 if (mask & ~max)
54 return -EINVAL;
56 *(unsigned int *)kp->arg = mask;
58 return 0;
61 static int ati_remote2_set_channel_mask(const char *val,
62 struct kernel_param *kp)
64 pr_debug("%s()\n", __func__);
66 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK);
69 static int ati_remote2_get_channel_mask(char *buffer, struct kernel_param *kp)
71 pr_debug("%s()\n", __func__);
73 return sprintf(buffer, "0x%04x", *(unsigned int *)kp->arg);
76 static int ati_remote2_set_mode_mask(const char *val, struct kernel_param *kp)
78 pr_debug("%s()\n", __func__);
80 return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK);
83 static int ati_remote2_get_mode_mask(char *buffer, struct kernel_param *kp)
85 pr_debug("%s()\n", __func__);
87 return sprintf(buffer, "0x%02x", *(unsigned int *)kp->arg);
90 static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK;
91 #define param_check_channel_mask(name, p) __param_check(name, p, unsigned int)
92 #define param_set_channel_mask ati_remote2_set_channel_mask
93 #define param_get_channel_mask ati_remote2_get_channel_mask
94 module_param(channel_mask, channel_mask, 0644);
95 MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
97 static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK;
98 #define param_check_mode_mask(name, p) __param_check(name, p, unsigned int)
99 #define param_set_mode_mask ati_remote2_set_mode_mask
100 #define param_get_mode_mask ati_remote2_get_mode_mask
101 module_param(mode_mask, mode_mask, 0644);
102 MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
104 static struct usb_device_id ati_remote2_id_table[] = {
105 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
108 MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
110 static DEFINE_MUTEX(ati_remote2_mutex);
112 enum {
113 ATI_REMOTE2_OPENED = 0x1,
114 ATI_REMOTE2_SUSPENDED = 0x2,
117 enum {
118 ATI_REMOTE2_AUX1,
119 ATI_REMOTE2_AUX2,
120 ATI_REMOTE2_AUX3,
121 ATI_REMOTE2_AUX4,
122 ATI_REMOTE2_PC,
123 ATI_REMOTE2_MODES,
126 static const struct {
127 u8 hw_code;
128 u16 keycode;
129 } ati_remote2_key_table[] = {
130 { 0x00, KEY_0 },
131 { 0x01, KEY_1 },
132 { 0x02, KEY_2 },
133 { 0x03, KEY_3 },
134 { 0x04, KEY_4 },
135 { 0x05, KEY_5 },
136 { 0x06, KEY_6 },
137 { 0x07, KEY_7 },
138 { 0x08, KEY_8 },
139 { 0x09, KEY_9 },
140 { 0x0c, KEY_POWER },
141 { 0x0d, KEY_MUTE },
142 { 0x10, KEY_VOLUMEUP },
143 { 0x11, KEY_VOLUMEDOWN },
144 { 0x20, KEY_CHANNELUP },
145 { 0x21, KEY_CHANNELDOWN },
146 { 0x28, KEY_FORWARD },
147 { 0x29, KEY_REWIND },
148 { 0x2c, KEY_PLAY },
149 { 0x30, KEY_PAUSE },
150 { 0x31, KEY_STOP },
151 { 0x37, KEY_RECORD },
152 { 0x38, KEY_DVD },
153 { 0x39, KEY_TV },
154 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */
155 { 0x54, KEY_MENU },
156 { 0x58, KEY_UP },
157 { 0x59, KEY_DOWN },
158 { 0x5a, KEY_LEFT },
159 { 0x5b, KEY_RIGHT },
160 { 0x5c, KEY_OK },
161 { 0x78, KEY_A },
162 { 0x79, KEY_B },
163 { 0x7a, KEY_C },
164 { 0x7b, KEY_D },
165 { 0x7c, KEY_E },
166 { 0x7d, KEY_F },
167 { 0x82, KEY_ENTER },
168 { 0x8e, KEY_VENDOR },
169 { 0x96, KEY_COFFEE },
170 { 0xa9, BTN_LEFT },
171 { 0xaa, BTN_RIGHT },
172 { 0xbe, KEY_QUESTION },
173 { 0xd0, KEY_EDIT },
174 { 0xd5, KEY_FRONT },
175 { 0xf9, KEY_INFO },
178 struct ati_remote2 {
179 struct input_dev *idev;
180 struct usb_device *udev;
182 struct usb_interface *intf[2];
183 struct usb_endpoint_descriptor *ep[2];
184 struct urb *urb[2];
185 void *buf[2];
186 dma_addr_t buf_dma[2];
188 unsigned long jiffies;
189 int mode;
191 char name[64];
192 char phys[64];
194 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
195 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
197 unsigned int flags;
199 unsigned int channel_mask;
200 unsigned int mode_mask;
203 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
204 static void ati_remote2_disconnect(struct usb_interface *interface);
205 static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message);
206 static int ati_remote2_resume(struct usb_interface *interface);
207 static int ati_remote2_reset_resume(struct usb_interface *interface);
208 static int ati_remote2_pre_reset(struct usb_interface *interface);
209 static int ati_remote2_post_reset(struct usb_interface *interface);
211 static struct usb_driver ati_remote2_driver = {
212 .name = "ati_remote2",
213 .probe = ati_remote2_probe,
214 .disconnect = ati_remote2_disconnect,
215 .id_table = ati_remote2_id_table,
216 .suspend = ati_remote2_suspend,
217 .resume = ati_remote2_resume,
218 .reset_resume = ati_remote2_reset_resume,
219 .pre_reset = ati_remote2_pre_reset,
220 .post_reset = ati_remote2_post_reset,
221 .supports_autosuspend = 1,
224 static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
226 int r;
228 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
229 if (r) {
230 dev_err(&ar2->intf[0]->dev,
231 "%s(): usb_submit_urb() = %d\n", __func__, r);
232 return r;
234 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
235 if (r) {
236 usb_kill_urb(ar2->urb[0]);
237 dev_err(&ar2->intf[1]->dev,
238 "%s(): usb_submit_urb() = %d\n", __func__, r);
239 return r;
242 return 0;
245 static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)
247 usb_kill_urb(ar2->urb[1]);
248 usb_kill_urb(ar2->urb[0]);
251 static int ati_remote2_open(struct input_dev *idev)
253 struct ati_remote2 *ar2 = input_get_drvdata(idev);
254 int r;
256 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
258 r = usb_autopm_get_interface(ar2->intf[0]);
259 if (r) {
260 dev_err(&ar2->intf[0]->dev,
261 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
262 goto fail1;
265 mutex_lock(&ati_remote2_mutex);
267 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
268 r = ati_remote2_submit_urbs(ar2);
269 if (r)
270 goto fail2;
273 ar2->flags |= ATI_REMOTE2_OPENED;
275 mutex_unlock(&ati_remote2_mutex);
277 usb_autopm_put_interface(ar2->intf[0]);
279 return 0;
281 fail2:
282 mutex_unlock(&ati_remote2_mutex);
283 usb_autopm_put_interface(ar2->intf[0]);
284 fail1:
285 return r;
288 static void ati_remote2_close(struct input_dev *idev)
290 struct ati_remote2 *ar2 = input_get_drvdata(idev);
292 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
294 mutex_lock(&ati_remote2_mutex);
296 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
297 ati_remote2_kill_urbs(ar2);
299 ar2->flags &= ~ATI_REMOTE2_OPENED;
301 mutex_unlock(&ati_remote2_mutex);
304 static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
306 struct input_dev *idev = ar2->idev;
307 u8 *data = ar2->buf[0];
308 int channel, mode;
310 channel = data[0] >> 4;
312 if (!((1 << channel) & ar2->channel_mask))
313 return;
315 mode = data[0] & 0x0F;
317 if (mode > ATI_REMOTE2_PC) {
318 dev_err(&ar2->intf[0]->dev,
319 "Unknown mode byte (%02x %02x %02x %02x)\n",
320 data[3], data[2], data[1], data[0]);
321 return;
324 if (!((1 << mode) & ar2->mode_mask))
325 return;
327 input_event(idev, EV_REL, REL_X, (s8) data[1]);
328 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
329 input_sync(idev);
332 static int ati_remote2_lookup(unsigned int hw_code)
334 int i;
336 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
337 if (ati_remote2_key_table[i].hw_code == hw_code)
338 return i;
340 return -1;
343 static void ati_remote2_input_key(struct ati_remote2 *ar2)
345 struct input_dev *idev = ar2->idev;
346 u8 *data = ar2->buf[1];
347 int channel, mode, hw_code, index;
349 channel = data[0] >> 4;
351 if (!((1 << channel) & ar2->channel_mask))
352 return;
354 mode = data[0] & 0x0F;
356 if (mode > ATI_REMOTE2_PC) {
357 dev_err(&ar2->intf[1]->dev,
358 "Unknown mode byte (%02x %02x %02x %02x)\n",
359 data[3], data[2], data[1], data[0]);
360 return;
363 hw_code = data[2];
364 if (hw_code == 0x3f) {
366 * For some incomprehensible reason the mouse pad generates
367 * events which look identical to the events from the last
368 * pressed mode key. Naturally we don't want to generate key
369 * events for the mouse pad so we filter out any subsequent
370 * events from the same mode key.
372 if (ar2->mode == mode)
373 return;
375 if (data[1] == 0)
376 ar2->mode = mode;
379 if (!((1 << mode) & ar2->mode_mask))
380 return;
382 index = ati_remote2_lookup(hw_code);
383 if (index < 0) {
384 dev_err(&ar2->intf[1]->dev,
385 "Unknown code byte (%02x %02x %02x %02x)\n",
386 data[3], data[2], data[1], data[0]);
387 return;
390 switch (data[1]) {
391 case 0: /* release */
392 break;
393 case 1: /* press */
394 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
395 break;
396 case 2: /* repeat */
398 /* No repeat for mouse buttons. */
399 if (ar2->keycode[mode][index] == BTN_LEFT ||
400 ar2->keycode[mode][index] == BTN_RIGHT)
401 return;
403 if (!time_after_eq(jiffies, ar2->jiffies))
404 return;
406 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
407 break;
408 default:
409 dev_err(&ar2->intf[1]->dev,
410 "Unknown state byte (%02x %02x %02x %02x)\n",
411 data[3], data[2], data[1], data[0]);
412 return;
415 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
416 input_sync(idev);
419 static void ati_remote2_complete_mouse(struct urb *urb)
421 struct ati_remote2 *ar2 = urb->context;
422 int r;
424 switch (urb->status) {
425 case 0:
426 usb_mark_last_busy(ar2->udev);
427 ati_remote2_input_mouse(ar2);
428 break;
429 case -ENOENT:
430 case -EILSEQ:
431 case -ECONNRESET:
432 case -ESHUTDOWN:
433 dev_dbg(&ar2->intf[0]->dev,
434 "%s(): urb status = %d\n", __func__, urb->status);
435 return;
436 default:
437 usb_mark_last_busy(ar2->udev);
438 dev_err(&ar2->intf[0]->dev,
439 "%s(): urb status = %d\n", __func__, urb->status);
442 r = usb_submit_urb(urb, GFP_ATOMIC);
443 if (r)
444 dev_err(&ar2->intf[0]->dev,
445 "%s(): usb_submit_urb() = %d\n", __func__, r);
448 static void ati_remote2_complete_key(struct urb *urb)
450 struct ati_remote2 *ar2 = urb->context;
451 int r;
453 switch (urb->status) {
454 case 0:
455 usb_mark_last_busy(ar2->udev);
456 ati_remote2_input_key(ar2);
457 break;
458 case -ENOENT:
459 case -EILSEQ:
460 case -ECONNRESET:
461 case -ESHUTDOWN:
462 dev_dbg(&ar2->intf[1]->dev,
463 "%s(): urb status = %d\n", __func__, urb->status);
464 return;
465 default:
466 usb_mark_last_busy(ar2->udev);
467 dev_err(&ar2->intf[1]->dev,
468 "%s(): urb status = %d\n", __func__, urb->status);
471 r = usb_submit_urb(urb, GFP_ATOMIC);
472 if (r)
473 dev_err(&ar2->intf[1]->dev,
474 "%s(): usb_submit_urb() = %d\n", __func__, r);
477 static int ati_remote2_getkeycode(struct input_dev *idev,
478 unsigned int scancode, unsigned int *keycode)
480 struct ati_remote2 *ar2 = input_get_drvdata(idev);
481 unsigned int mode;
482 int index;
484 mode = scancode >> 8;
485 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
486 return -EINVAL;
488 index = ati_remote2_lookup(scancode & 0xFF);
489 if (index < 0)
490 return -EINVAL;
492 *keycode = ar2->keycode[mode][index];
493 return 0;
496 static int ati_remote2_setkeycode(struct input_dev *idev,
497 unsigned int scancode, unsigned int keycode)
499 struct ati_remote2 *ar2 = input_get_drvdata(idev);
500 unsigned int mode, old_keycode;
501 int index;
503 mode = scancode >> 8;
504 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
505 return -EINVAL;
507 index = ati_remote2_lookup(scancode & 0xFF);
508 if (index < 0)
509 return -EINVAL;
511 old_keycode = ar2->keycode[mode][index];
512 ar2->keycode[mode][index] = keycode;
513 __set_bit(keycode, idev->keybit);
515 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
516 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
517 if (ar2->keycode[mode][index] == old_keycode)
518 return 0;
522 __clear_bit(old_keycode, idev->keybit);
524 return 0;
527 static int ati_remote2_input_init(struct ati_remote2 *ar2)
529 struct input_dev *idev;
530 int index, mode, retval;
532 idev = input_allocate_device();
533 if (!idev)
534 return -ENOMEM;
536 ar2->idev = idev;
537 input_set_drvdata(idev, ar2);
539 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
540 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
541 BIT_MASK(BTN_RIGHT);
542 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
544 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
545 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
546 ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
547 __set_bit(ar2->keycode[mode][index], idev->keybit);
551 /* AUX1-AUX4 and PC generate the same scancode. */
552 index = ati_remote2_lookup(0x3f);
553 ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
554 ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
555 ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
556 ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
557 ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
558 __set_bit(KEY_PROG1, idev->keybit);
559 __set_bit(KEY_PROG2, idev->keybit);
560 __set_bit(KEY_PROG3, idev->keybit);
561 __set_bit(KEY_PROG4, idev->keybit);
562 __set_bit(KEY_PC, idev->keybit);
564 idev->rep[REP_DELAY] = 250;
565 idev->rep[REP_PERIOD] = 33;
567 idev->open = ati_remote2_open;
568 idev->close = ati_remote2_close;
570 idev->getkeycode = ati_remote2_getkeycode;
571 idev->setkeycode = ati_remote2_setkeycode;
573 idev->name = ar2->name;
574 idev->phys = ar2->phys;
576 usb_to_input_id(ar2->udev, &idev->id);
577 idev->dev.parent = &ar2->udev->dev;
579 retval = input_register_device(idev);
580 if (retval)
581 input_free_device(idev);
583 return retval;
586 static int ati_remote2_urb_init(struct ati_remote2 *ar2)
588 struct usb_device *udev = ar2->udev;
589 int i, pipe, maxp;
591 for (i = 0; i < 2; i++) {
592 ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
593 if (!ar2->buf[i])
594 return -ENOMEM;
596 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
597 if (!ar2->urb[i])
598 return -ENOMEM;
600 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
601 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
602 maxp = maxp > 4 ? 4 : maxp;
604 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
605 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
606 ar2, ar2->ep[i]->bInterval);
607 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
608 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
611 return 0;
614 static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
616 int i;
618 for (i = 0; i < 2; i++) {
619 usb_free_urb(ar2->urb[i]);
620 usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
624 static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
626 int r, i, channel;
629 * Configure receiver to only accept input from remote "channel"
630 * channel == 0 -> Accept input from any remote channel
631 * channel == 1 -> Only accept input from remote channel 1
632 * channel == 2 -> Only accept input from remote channel 2
633 * ...
634 * channel == 16 -> Only accept input from remote channel 16
637 channel = 0;
638 for (i = 0; i < 16; i++) {
639 if ((1 << i) & ch_mask) {
640 if (!(~(1 << i) & ch_mask))
641 channel = i + 1;
642 break;
646 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
647 0x20,
648 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
649 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
650 if (r) {
651 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
652 __func__, r);
653 return r;
656 return 0;
659 static ssize_t ati_remote2_show_channel_mask(struct device *dev,
660 struct device_attribute *attr,
661 char *buf)
663 struct usb_device *udev = to_usb_device(dev);
664 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
665 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
667 return sprintf(buf, "0x%04x\n", ar2->channel_mask);
670 static ssize_t ati_remote2_store_channel_mask(struct device *dev,
671 struct device_attribute *attr,
672 const char *buf, size_t count)
674 struct usb_device *udev = to_usb_device(dev);
675 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
676 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
677 unsigned long mask;
678 int r;
680 if (strict_strtoul(buf, 0, &mask))
681 return -EINVAL;
683 if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
684 return -EINVAL;
686 r = usb_autopm_get_interface(ar2->intf[0]);
687 if (r) {
688 dev_err(&ar2->intf[0]->dev,
689 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
690 return r;
693 mutex_lock(&ati_remote2_mutex);
695 if (mask != ar2->channel_mask && !ati_remote2_setup(ar2, mask))
696 ar2->channel_mask = mask;
698 mutex_unlock(&ati_remote2_mutex);
700 usb_autopm_put_interface(ar2->intf[0]);
702 return count;
705 static ssize_t ati_remote2_show_mode_mask(struct device *dev,
706 struct device_attribute *attr,
707 char *buf)
709 struct usb_device *udev = to_usb_device(dev);
710 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
711 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
713 return sprintf(buf, "0x%02x\n", ar2->mode_mask);
716 static ssize_t ati_remote2_store_mode_mask(struct device *dev,
717 struct device_attribute *attr,
718 const char *buf, size_t count)
720 struct usb_device *udev = to_usb_device(dev);
721 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
722 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
723 unsigned long mask;
725 if (strict_strtoul(buf, 0, &mask))
726 return -EINVAL;
728 if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
729 return -EINVAL;
731 ar2->mode_mask = mask;
733 return count;
736 static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
737 ati_remote2_store_channel_mask);
739 static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
740 ati_remote2_store_mode_mask);
742 static struct attribute *ati_remote2_attrs[] = {
743 &dev_attr_channel_mask.attr,
744 &dev_attr_mode_mask.attr,
745 NULL,
748 static struct attribute_group ati_remote2_attr_group = {
749 .attrs = ati_remote2_attrs,
752 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
754 struct usb_device *udev = interface_to_usbdev(interface);
755 struct usb_host_interface *alt = interface->cur_altsetting;
756 struct ati_remote2 *ar2;
757 int r;
759 if (alt->desc.bInterfaceNumber)
760 return -ENODEV;
762 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
763 if (!ar2)
764 return -ENOMEM;
766 ar2->udev = udev;
768 ar2->intf[0] = interface;
769 ar2->ep[0] = &alt->endpoint[0].desc;
771 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
772 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
773 if (r)
774 goto fail1;
775 alt = ar2->intf[1]->cur_altsetting;
776 ar2->ep[1] = &alt->endpoint[0].desc;
778 r = ati_remote2_urb_init(ar2);
779 if (r)
780 goto fail2;
782 ar2->channel_mask = channel_mask;
783 ar2->mode_mask = mode_mask;
785 r = ati_remote2_setup(ar2, ar2->channel_mask);
786 if (r)
787 goto fail2;
789 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
790 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
792 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
794 r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);
795 if (r)
796 goto fail2;
798 r = ati_remote2_input_init(ar2);
799 if (r)
800 goto fail3;
802 usb_set_intfdata(interface, ar2);
804 interface->needs_remote_wakeup = 1;
806 return 0;
808 fail3:
809 sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group);
810 fail2:
811 ati_remote2_urb_cleanup(ar2);
812 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
813 fail1:
814 kfree(ar2);
816 return r;
819 static void ati_remote2_disconnect(struct usb_interface *interface)
821 struct ati_remote2 *ar2;
822 struct usb_host_interface *alt = interface->cur_altsetting;
824 if (alt->desc.bInterfaceNumber)
825 return;
827 ar2 = usb_get_intfdata(interface);
828 usb_set_intfdata(interface, NULL);
830 input_unregister_device(ar2->idev);
832 sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);
834 ati_remote2_urb_cleanup(ar2);
836 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
838 kfree(ar2);
841 static int ati_remote2_suspend(struct usb_interface *interface,
842 pm_message_t message)
844 struct ati_remote2 *ar2;
845 struct usb_host_interface *alt = interface->cur_altsetting;
847 if (alt->desc.bInterfaceNumber)
848 return 0;
850 ar2 = usb_get_intfdata(interface);
852 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
854 mutex_lock(&ati_remote2_mutex);
856 if (ar2->flags & ATI_REMOTE2_OPENED)
857 ati_remote2_kill_urbs(ar2);
859 ar2->flags |= ATI_REMOTE2_SUSPENDED;
861 mutex_unlock(&ati_remote2_mutex);
863 return 0;
866 static int ati_remote2_resume(struct usb_interface *interface)
868 struct ati_remote2 *ar2;
869 struct usb_host_interface *alt = interface->cur_altsetting;
870 int r = 0;
872 if (alt->desc.bInterfaceNumber)
873 return 0;
875 ar2 = usb_get_intfdata(interface);
877 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
879 mutex_lock(&ati_remote2_mutex);
881 if (ar2->flags & ATI_REMOTE2_OPENED)
882 r = ati_remote2_submit_urbs(ar2);
884 if (!r)
885 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
887 mutex_unlock(&ati_remote2_mutex);
889 return r;
892 static int ati_remote2_reset_resume(struct usb_interface *interface)
894 struct ati_remote2 *ar2;
895 struct usb_host_interface *alt = interface->cur_altsetting;
896 int r = 0;
898 if (alt->desc.bInterfaceNumber)
899 return 0;
901 ar2 = usb_get_intfdata(interface);
903 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
905 mutex_lock(&ati_remote2_mutex);
907 r = ati_remote2_setup(ar2, ar2->channel_mask);
908 if (r)
909 goto out;
911 if (ar2->flags & ATI_REMOTE2_OPENED)
912 r = ati_remote2_submit_urbs(ar2);
914 if (!r)
915 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
917 out:
918 mutex_unlock(&ati_remote2_mutex);
920 return r;
923 static int ati_remote2_pre_reset(struct usb_interface *interface)
925 struct ati_remote2 *ar2;
926 struct usb_host_interface *alt = interface->cur_altsetting;
928 if (alt->desc.bInterfaceNumber)
929 return 0;
931 ar2 = usb_get_intfdata(interface);
933 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
935 mutex_lock(&ati_remote2_mutex);
937 if (ar2->flags == ATI_REMOTE2_OPENED)
938 ati_remote2_kill_urbs(ar2);
940 return 0;
943 static int ati_remote2_post_reset(struct usb_interface *interface)
945 struct ati_remote2 *ar2;
946 struct usb_host_interface *alt = interface->cur_altsetting;
947 int r = 0;
949 if (alt->desc.bInterfaceNumber)
950 return 0;
952 ar2 = usb_get_intfdata(interface);
954 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
956 if (ar2->flags == ATI_REMOTE2_OPENED)
957 r = ati_remote2_submit_urbs(ar2);
959 mutex_unlock(&ati_remote2_mutex);
961 return r;
964 static int __init ati_remote2_init(void)
966 int r;
968 r = usb_register(&ati_remote2_driver);
969 if (r)
970 printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
971 else
972 printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
974 return r;
977 static void __exit ati_remote2_exit(void)
979 usb_deregister(&ati_remote2_driver);
982 module_init(ati_remote2_init);
983 module_exit(ati_remote2_exit);