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