Merge branch 'hid-suspend' into picolcd
[pandora-kernel.git] / drivers / input / misc / ati_remote2.c
1 /*
2  * ati_remote2 - ATI/Philips USB RF remote driver
3  *
4  * Copyright (C) 2005-2008 Ville Syrjala <syrjala@sci.fi>
5  * Copyright (C) 2007-2008 Peter Stokes <linux@dadeos.co.uk>
6  *
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.
10  */
11
12 #include <linux/usb/input.h>
13 #include <linux/slab.h>
14
15 #define DRIVER_DESC    "ATI/Philips USB RF remote driver"
16 #define DRIVER_VERSION "0.3"
17
18 MODULE_DESCRIPTION(DRIVER_DESC);
19 MODULE_VERSION(DRIVER_VERSION);
20 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
21 MODULE_LICENSE("GPL");
22
23 /*
24  * ATI Remote Wonder II Channel Configuration
25  *
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".
33  */
34
35 enum {
36         ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,
37         ATI_REMOTE2_MAX_MODE_MASK = 0x1F,
38 };
39
40 static int ati_remote2_set_mask(const char *val,
41                                 struct kernel_param *kp, unsigned int max)
42 {
43         unsigned long mask;
44         int ret;
45
46         if (!val)
47                 return -EINVAL;
48
49         ret = strict_strtoul(val, 0, &mask);
50         if (ret)
51                 return ret;
52
53         if (mask & ~max)
54                 return -EINVAL;
55
56         *(unsigned int *)kp->arg = mask;
57
58         return 0;
59 }
60
61 static int ati_remote2_set_channel_mask(const char *val,
62                                         struct kernel_param *kp)
63 {
64         pr_debug("%s()\n", __func__);
65
66         return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK);
67 }
68
69 static int ati_remote2_get_channel_mask(char *buffer, struct kernel_param *kp)
70 {
71         pr_debug("%s()\n", __func__);
72
73         return sprintf(buffer, "0x%04x", *(unsigned int *)kp->arg);
74 }
75
76 static int ati_remote2_set_mode_mask(const char *val, struct kernel_param *kp)
77 {
78         pr_debug("%s()\n", __func__);
79
80         return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK);
81 }
82
83 static int ati_remote2_get_mode_mask(char *buffer, struct kernel_param *kp)
84 {
85         pr_debug("%s()\n", __func__);
86
87         return sprintf(buffer, "0x%02x", *(unsigned int *)kp->arg);
88 }
89
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>");
96
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>");
103
104 static struct usb_device_id ati_remote2_id_table[] = {
105         { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
106         { }
107 };
108 MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
109
110 static DEFINE_MUTEX(ati_remote2_mutex);
111
112 enum {
113         ATI_REMOTE2_OPENED = 0x1,
114         ATI_REMOTE2_SUSPENDED = 0x2,
115 };
116
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,
124 };
125
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 },
176 };
177
178 struct ati_remote2 {
179         struct input_dev *idev;
180         struct usb_device *udev;
181
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];
187
188         unsigned long jiffies;
189         int mode;
190
191         char name[64];
192         char phys[64];
193
194         /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
195         u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
196
197         unsigned int flags;
198
199         unsigned int channel_mask;
200         unsigned int mode_mask;
201 };
202
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);
210
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,
222 };
223
224 static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
225 {
226         int r;
227
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;
233         }
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;
240         }
241
242         return 0;
243 }
244
245 static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)
246 {
247         usb_kill_urb(ar2->urb[1]);
248         usb_kill_urb(ar2->urb[0]);
249 }
250
251 static int ati_remote2_open(struct input_dev *idev)
252 {
253         struct ati_remote2 *ar2 = input_get_drvdata(idev);
254         int r;
255
256         dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
257
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;
263         }
264
265         mutex_lock(&ati_remote2_mutex);
266
267         if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
268                 r = ati_remote2_submit_urbs(ar2);
269                 if (r)
270                         goto fail2;
271         }
272
273         ar2->flags |= ATI_REMOTE2_OPENED;
274
275         mutex_unlock(&ati_remote2_mutex);
276
277         usb_autopm_put_interface(ar2->intf[0]);
278
279         return 0;
280
281  fail2:
282         mutex_unlock(&ati_remote2_mutex);
283         usb_autopm_put_interface(ar2->intf[0]);
284  fail1:
285         return r;
286 }
287
288 static void ati_remote2_close(struct input_dev *idev)
289 {
290         struct ati_remote2 *ar2 = input_get_drvdata(idev);
291
292         dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
293
294         mutex_lock(&ati_remote2_mutex);
295
296         if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
297                 ati_remote2_kill_urbs(ar2);
298
299         ar2->flags &= ~ATI_REMOTE2_OPENED;
300
301         mutex_unlock(&ati_remote2_mutex);
302 }
303
304 static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
305 {
306         struct input_dev *idev = ar2->idev;
307         u8 *data = ar2->buf[0];
308         int channel, mode;
309
310         channel = data[0] >> 4;
311
312         if (!((1 << channel) & ar2->channel_mask))
313                 return;
314
315         mode = data[0] & 0x0F;
316
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;
322         }
323
324         if (!((1 << mode) & ar2->mode_mask))
325                 return;
326
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);
330 }
331
332 static int ati_remote2_lookup(unsigned int hw_code)
333 {
334         int i;
335
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;
339
340         return -1;
341 }
342
343 static void ati_remote2_input_key(struct ati_remote2 *ar2)
344 {
345         struct input_dev *idev = ar2->idev;
346         u8 *data = ar2->buf[1];
347         int channel, mode, hw_code, index;
348
349         channel = data[0] >> 4;
350
351         if (!((1 << channel) & ar2->channel_mask))
352                 return;
353
354         mode = data[0] & 0x0F;
355
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;
361         }
362
363         hw_code = data[2];
364         if (hw_code == 0x3f) {
365                 /*
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.
371                  */
372                 if (ar2->mode == mode)
373                         return;
374
375                 if (data[1] == 0)
376                         ar2->mode = mode;
377         }
378
379         if (!((1 << mode) & ar2->mode_mask))
380                 return;
381
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;
388         }
389
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 */
397
398                 /* No repeat for mouse buttons. */
399                 if (ar2->keycode[mode][index] == BTN_LEFT ||
400                     ar2->keycode[mode][index] == BTN_RIGHT)
401                         return;
402
403                 if (!time_after_eq(jiffies, ar2->jiffies))
404                         return;
405
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;
413         }
414
415         input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
416         input_sync(idev);
417 }
418
419 static void ati_remote2_complete_mouse(struct urb *urb)
420 {
421         struct ati_remote2 *ar2 = urb->context;
422         int r;
423
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);
440         }
441
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);
446 }
447
448 static void ati_remote2_complete_key(struct urb *urb)
449 {
450         struct ati_remote2 *ar2 = urb->context;
451         int r;
452
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);
469         }
470
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);
475 }
476
477 static int ati_remote2_getkeycode(struct input_dev *idev,
478                                   unsigned int scancode, unsigned int *keycode)
479 {
480         struct ati_remote2 *ar2 = input_get_drvdata(idev);
481         unsigned int mode;
482         int index;
483
484         mode = scancode >> 8;
485         if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
486                 return -EINVAL;
487
488         index = ati_remote2_lookup(scancode & 0xFF);
489         if (index < 0)
490                 return -EINVAL;
491
492         *keycode = ar2->keycode[mode][index];
493         return 0;
494 }
495
496 static int ati_remote2_setkeycode(struct input_dev *idev,
497                                   unsigned int scancode, unsigned int keycode)
498 {
499         struct ati_remote2 *ar2 = input_get_drvdata(idev);
500         unsigned int mode, old_keycode;
501         int index;
502
503         mode = scancode >> 8;
504         if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
505                 return -EINVAL;
506
507         index = ati_remote2_lookup(scancode & 0xFF);
508         if (index < 0)
509                 return -EINVAL;
510
511         old_keycode = ar2->keycode[mode][index];
512         ar2->keycode[mode][index] = keycode;
513         __set_bit(keycode, idev->keybit);
514
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;
519                 }
520         }
521
522         __clear_bit(old_keycode, idev->keybit);
523
524         return 0;
525 }
526
527 static int ati_remote2_input_init(struct ati_remote2 *ar2)
528 {
529         struct input_dev *idev;
530         int index, mode, retval;
531
532         idev = input_allocate_device();
533         if (!idev)
534                 return -ENOMEM;
535
536         ar2->idev = idev;
537         input_set_drvdata(idev, ar2);
538
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);
543
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);
548                 }
549         }
550
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);
563
564         idev->rep[REP_DELAY]  = 250;
565         idev->rep[REP_PERIOD] = 33;
566
567         idev->open = ati_remote2_open;
568         idev->close = ati_remote2_close;
569
570         idev->getkeycode = ati_remote2_getkeycode;
571         idev->setkeycode = ati_remote2_setkeycode;
572
573         idev->name = ar2->name;
574         idev->phys = ar2->phys;
575
576         usb_to_input_id(ar2->udev, &idev->id);
577         idev->dev.parent = &ar2->udev->dev;
578
579         retval = input_register_device(idev);
580         if (retval)
581                 input_free_device(idev);
582
583         return retval;
584 }
585
586 static int ati_remote2_urb_init(struct ati_remote2 *ar2)
587 {
588         struct usb_device *udev = ar2->udev;
589         int i, pipe, maxp;
590
591         for (i = 0; i < 2; i++) {
592                 ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
593                 if (!ar2->buf[i])
594                         return -ENOMEM;
595
596                 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
597                 if (!ar2->urb[i])
598                         return -ENOMEM;
599
600                 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
601                 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
602                 maxp = maxp > 4 ? 4 : maxp;
603
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;
609         }
610
611         return 0;
612 }
613
614 static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
615 {
616         int i;
617
618         for (i = 0; i < 2; i++) {
619                 usb_free_urb(ar2->urb[i]);
620                 usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
621         }
622 }
623
624 static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
625 {
626         int r, i, channel;
627
628         /*
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
635          */
636
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;
643                 }
644         }
645
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;
654         }
655
656         return 0;
657 }
658
659 static ssize_t ati_remote2_show_channel_mask(struct device *dev,
660                                              struct device_attribute *attr,
661                                              char *buf)
662 {
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);
666
667         return sprintf(buf, "0x%04x\n", ar2->channel_mask);
668 }
669
670 static ssize_t ati_remote2_store_channel_mask(struct device *dev,
671                                               struct device_attribute *attr,
672                                               const char *buf, size_t count)
673 {
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;
679
680         if (strict_strtoul(buf, 0, &mask))
681                 return -EINVAL;
682
683         if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
684                 return -EINVAL;
685
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;
691         }
692
693         mutex_lock(&ati_remote2_mutex);
694
695         if (mask != ar2->channel_mask && !ati_remote2_setup(ar2, mask))
696                 ar2->channel_mask = mask;
697
698         mutex_unlock(&ati_remote2_mutex);
699
700         usb_autopm_put_interface(ar2->intf[0]);
701
702         return count;
703 }
704
705 static ssize_t ati_remote2_show_mode_mask(struct device *dev,
706                                           struct device_attribute *attr,
707                                           char *buf)
708 {
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);
712
713         return sprintf(buf, "0x%02x\n", ar2->mode_mask);
714 }
715
716 static ssize_t ati_remote2_store_mode_mask(struct device *dev,
717                                            struct device_attribute *attr,
718                                            const char *buf, size_t count)
719 {
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;
724
725         if (strict_strtoul(buf, 0, &mask))
726                 return -EINVAL;
727
728         if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
729                 return -EINVAL;
730
731         ar2->mode_mask = mask;
732
733         return count;
734 }
735
736 static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
737                    ati_remote2_store_channel_mask);
738
739 static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
740                    ati_remote2_store_mode_mask);
741
742 static struct attribute *ati_remote2_attrs[] = {
743         &dev_attr_channel_mask.attr,
744         &dev_attr_mode_mask.attr,
745         NULL,
746 };
747
748 static struct attribute_group ati_remote2_attr_group = {
749         .attrs = ati_remote2_attrs,
750 };
751
752 static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
753 {
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;
758
759         if (alt->desc.bInterfaceNumber)
760                 return -ENODEV;
761
762         ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
763         if (!ar2)
764                 return -ENOMEM;
765
766         ar2->udev = udev;
767
768         ar2->intf[0] = interface;
769         ar2->ep[0] = &alt->endpoint[0].desc;
770
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;
777
778         r = ati_remote2_urb_init(ar2);
779         if (r)
780                 goto fail2;
781
782         ar2->channel_mask = channel_mask;
783         ar2->mode_mask = mode_mask;
784
785         r = ati_remote2_setup(ar2, ar2->channel_mask);
786         if (r)
787                 goto fail2;
788
789         usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
790         strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
791
792         strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
793
794         r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);
795         if (r)
796                 goto fail2;
797
798         r = ati_remote2_input_init(ar2);
799         if (r)
800                 goto fail3;
801
802         usb_set_intfdata(interface, ar2);
803
804         interface->needs_remote_wakeup = 1;
805
806         return 0;
807
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);
815
816         return r;
817 }
818
819 static void ati_remote2_disconnect(struct usb_interface *interface)
820 {
821         struct ati_remote2 *ar2;
822         struct usb_host_interface *alt = interface->cur_altsetting;
823
824         if (alt->desc.bInterfaceNumber)
825                 return;
826
827         ar2 = usb_get_intfdata(interface);
828         usb_set_intfdata(interface, NULL);
829
830         input_unregister_device(ar2->idev);
831
832         sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);
833
834         ati_remote2_urb_cleanup(ar2);
835
836         usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
837
838         kfree(ar2);
839 }
840
841 static int ati_remote2_suspend(struct usb_interface *interface,
842                                pm_message_t message)
843 {
844         struct ati_remote2 *ar2;
845         struct usb_host_interface *alt = interface->cur_altsetting;
846
847         if (alt->desc.bInterfaceNumber)
848                 return 0;
849
850         ar2 = usb_get_intfdata(interface);
851
852         dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
853
854         mutex_lock(&ati_remote2_mutex);
855
856         if (ar2->flags & ATI_REMOTE2_OPENED)
857                 ati_remote2_kill_urbs(ar2);
858
859         ar2->flags |= ATI_REMOTE2_SUSPENDED;
860
861         mutex_unlock(&ati_remote2_mutex);
862
863         return 0;
864 }
865
866 static int ati_remote2_resume(struct usb_interface *interface)
867 {
868         struct ati_remote2 *ar2;
869         struct usb_host_interface *alt = interface->cur_altsetting;
870         int r = 0;
871
872         if (alt->desc.bInterfaceNumber)
873                 return 0;
874
875         ar2 = usb_get_intfdata(interface);
876
877         dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
878
879         mutex_lock(&ati_remote2_mutex);
880
881         if (ar2->flags & ATI_REMOTE2_OPENED)
882                 r = ati_remote2_submit_urbs(ar2);
883
884         if (!r)
885                 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
886
887         mutex_unlock(&ati_remote2_mutex);
888
889         return r;
890 }
891
892 static int ati_remote2_reset_resume(struct usb_interface *interface)
893 {
894         struct ati_remote2 *ar2;
895         struct usb_host_interface *alt = interface->cur_altsetting;
896         int r = 0;
897
898         if (alt->desc.bInterfaceNumber)
899                 return 0;
900
901         ar2 = usb_get_intfdata(interface);
902
903         dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
904
905         mutex_lock(&ati_remote2_mutex);
906
907         r = ati_remote2_setup(ar2, ar2->channel_mask);
908         if (r)
909                 goto out;
910
911         if (ar2->flags & ATI_REMOTE2_OPENED)
912                 r = ati_remote2_submit_urbs(ar2);
913
914         if (!r)
915                 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
916
917  out:
918         mutex_unlock(&ati_remote2_mutex);
919
920         return r;
921 }
922
923 static int ati_remote2_pre_reset(struct usb_interface *interface)
924 {
925         struct ati_remote2 *ar2;
926         struct usb_host_interface *alt = interface->cur_altsetting;
927
928         if (alt->desc.bInterfaceNumber)
929                 return 0;
930
931         ar2 = usb_get_intfdata(interface);
932
933         dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
934
935         mutex_lock(&ati_remote2_mutex);
936
937         if (ar2->flags == ATI_REMOTE2_OPENED)
938                 ati_remote2_kill_urbs(ar2);
939
940         return 0;
941 }
942
943 static int ati_remote2_post_reset(struct usb_interface *interface)
944 {
945         struct ati_remote2 *ar2;
946         struct usb_host_interface *alt = interface->cur_altsetting;
947         int r = 0;
948
949         if (alt->desc.bInterfaceNumber)
950                 return 0;
951
952         ar2 = usb_get_intfdata(interface);
953
954         dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
955
956         if (ar2->flags == ATI_REMOTE2_OPENED)
957                 r = ati_remote2_submit_urbs(ar2);
958
959         mutex_unlock(&ati_remote2_mutex);
960
961         return r;
962 }
963
964 static int __init ati_remote2_init(void)
965 {
966         int r;
967
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");
973
974         return r;
975 }
976
977 static void __exit ati_remote2_exit(void)
978 {
979         usb_deregister(&ati_remote2_driver);
980 }
981
982 module_init(ati_remote2_init);
983 module_exit(ati_remote2_exit);