blob: 351eb9000def330852623a1bffd2dd727525fd0d [file] [log] [blame]
Ville Syrjälä735b0cb2005-12-10 20:30:54 +02001/*
2 * ati_remote2 - ATI/Philips USB RF remote driver
3 *
Ville Syrjala1971b9d2008-07-03 10:45:37 -04004 * Copyright (C) 2005-2008 Ville Syrjala <syrjala@sci.fi>
5 * Copyright (C) 2007-2008 Peter Stokes <linux@dadeos.co.uk>
Ville Syrjälä735b0cb2005-12-10 20:30:54 +02006 *
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
David Brownellae0dadc2006-06-13 10:04:34 -070012#include <linux/usb/input.h>
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020013
14#define DRIVER_DESC "ATI/Philips USB RF remote driver"
Ville Syrjala1971b9d2008-07-03 10:45:37 -040015#define DRIVER_VERSION "0.3"
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020016
17MODULE_DESCRIPTION(DRIVER_DESC);
18MODULE_VERSION(DRIVER_VERSION);
19MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
20MODULE_LICENSE("GPL");
21
Peter Stokesa1421d32007-04-12 01:33:10 -040022/*
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
Ville Syrjala1971b9d2008-07-03 10:45:37 -040030 * 16 and then press the "PC" button again, the button will slowly flash the count of the
Peter Stokesa1421d32007-04-12 01:33:10 -040031 * newly configured "channel".
32 */
33
34static unsigned int channel_mask = 0xFFFF;
35module_param(channel_mask, uint, 0644);
36MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
37
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020038static unsigned int mode_mask = 0x1F;
39module_param(mode_mask, uint, 0644);
40MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
41
42static struct usb_device_id ati_remote2_id_table[] = {
43 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
44 { }
45};
46MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
47
Ville Syrjalad6505ab2008-07-03 10:45:37 -040048static DEFINE_MUTEX(ati_remote2_mutex);
49
50enum {
51 ATI_REMOTE2_OPENED = 0x1,
52 ATI_REMOTE2_SUSPENDED = 0x2,
53};
54
Ville Syrjala1971b9d2008-07-03 10:45:37 -040055enum {
56 ATI_REMOTE2_AUX1,
57 ATI_REMOTE2_AUX2,
58 ATI_REMOTE2_AUX3,
59 ATI_REMOTE2_AUX4,
60 ATI_REMOTE2_PC,
61 ATI_REMOTE2_MODES,
62};
63
64static const struct {
65 u8 hw_code;
66 u16 keycode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020067} ati_remote2_key_table[] = {
68 { 0x00, KEY_0 },
69 { 0x01, KEY_1 },
70 { 0x02, KEY_2 },
71 { 0x03, KEY_3 },
72 { 0x04, KEY_4 },
73 { 0x05, KEY_5 },
74 { 0x06, KEY_6 },
75 { 0x07, KEY_7 },
76 { 0x08, KEY_8 },
77 { 0x09, KEY_9 },
78 { 0x0c, KEY_POWER },
79 { 0x0d, KEY_MUTE },
80 { 0x10, KEY_VOLUMEUP },
81 { 0x11, KEY_VOLUMEDOWN },
82 { 0x20, KEY_CHANNELUP },
83 { 0x21, KEY_CHANNELDOWN },
84 { 0x28, KEY_FORWARD },
85 { 0x29, KEY_REWIND },
86 { 0x2c, KEY_PLAY },
87 { 0x30, KEY_PAUSE },
88 { 0x31, KEY_STOP },
89 { 0x37, KEY_RECORD },
90 { 0x38, KEY_DVD },
91 { 0x39, KEY_TV },
Ville Syrjala1971b9d2008-07-03 10:45:37 -040092 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020093 { 0x54, KEY_MENU },
94 { 0x58, KEY_UP },
95 { 0x59, KEY_DOWN },
96 { 0x5a, KEY_LEFT },
97 { 0x5b, KEY_RIGHT },
98 { 0x5c, KEY_OK },
99 { 0x78, KEY_A },
100 { 0x79, KEY_B },
101 { 0x7a, KEY_C },
102 { 0x7b, KEY_D },
103 { 0x7c, KEY_E },
104 { 0x7d, KEY_F },
105 { 0x82, KEY_ENTER },
106 { 0x8e, KEY_VENDOR },
107 { 0x96, KEY_COFFEE },
108 { 0xa9, BTN_LEFT },
109 { 0xaa, BTN_RIGHT },
110 { 0xbe, KEY_QUESTION },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200111 { 0xd0, KEY_EDIT },
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400112 { 0xd5, KEY_FRONT },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200113 { 0xf9, KEY_INFO },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200114};
115
116struct ati_remote2 {
117 struct input_dev *idev;
118 struct usb_device *udev;
119
120 struct usb_interface *intf[2];
121 struct usb_endpoint_descriptor *ep[2];
122 struct urb *urb[2];
123 void *buf[2];
124 dma_addr_t buf_dma[2];
125
126 unsigned long jiffies;
127 int mode;
128
129 char name[64];
130 char phys[64];
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400131
132 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
133 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400134
135 unsigned int flags;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200136};
137
138static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
139static void ati_remote2_disconnect(struct usb_interface *interface);
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400140static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message);
141static int ati_remote2_resume(struct usb_interface *interface);
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800142static int ati_remote2_reset_resume(struct usb_interface *interface);
143static int ati_remote2_pre_reset(struct usb_interface *interface);
144static int ati_remote2_post_reset(struct usb_interface *interface);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200145
146static struct usb_driver ati_remote2_driver = {
147 .name = "ati_remote2",
148 .probe = ati_remote2_probe,
149 .disconnect = ati_remote2_disconnect,
150 .id_table = ati_remote2_id_table,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400151 .suspend = ati_remote2_suspend,
152 .resume = ati_remote2_resume,
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800153 .reset_resume = ati_remote2_reset_resume,
154 .pre_reset = ati_remote2_pre_reset,
155 .post_reset = ati_remote2_post_reset,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400156 .supports_autosuspend = 1,
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200157};
158
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400159static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200160{
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200161 int r;
162
163 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
164 if (r) {
165 dev_err(&ar2->intf[0]->dev,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400166 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200167 return r;
168 }
169 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
170 if (r) {
171 usb_kill_urb(ar2->urb[0]);
172 dev_err(&ar2->intf[1]->dev,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400173 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200174 return r;
175 }
176
177 return 0;
178}
179
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400180static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)
181{
182 usb_kill_urb(ar2->urb[1]);
183 usb_kill_urb(ar2->urb[0]);
184}
185
186static int ati_remote2_open(struct input_dev *idev)
187{
188 struct ati_remote2 *ar2 = input_get_drvdata(idev);
189 int r;
190
191 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
192
193 r = usb_autopm_get_interface(ar2->intf[0]);
194 if (r) {
195 dev_err(&ar2->intf[0]->dev,
196 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
197 goto fail1;
198 }
199
200 mutex_lock(&ati_remote2_mutex);
201
202 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
203 r = ati_remote2_submit_urbs(ar2);
204 if (r)
205 goto fail2;
206 }
207
208 ar2->flags |= ATI_REMOTE2_OPENED;
209
210 mutex_unlock(&ati_remote2_mutex);
211
212 usb_autopm_put_interface(ar2->intf[0]);
213
214 return 0;
215
216 fail2:
217 mutex_unlock(&ati_remote2_mutex);
218 usb_autopm_put_interface(ar2->intf[0]);
219 fail1:
220 return r;
221}
222
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200223static void ati_remote2_close(struct input_dev *idev)
224{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400225 struct ati_remote2 *ar2 = input_get_drvdata(idev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200226
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400227 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
228
229 mutex_lock(&ati_remote2_mutex);
230
231 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
232 ati_remote2_kill_urbs(ar2);
233
234 ar2->flags &= ~ATI_REMOTE2_OPENED;
235
236 mutex_unlock(&ati_remote2_mutex);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200237}
238
David Howells7d12e782006-10-05 14:55:46 +0100239static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200240{
241 struct input_dev *idev = ar2->idev;
242 u8 *data = ar2->buf[0];
Peter Stokesa1421d32007-04-12 01:33:10 -0400243 int channel, mode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200244
Peter Stokesa1421d32007-04-12 01:33:10 -0400245 channel = data[0] >> 4;
246
247 if (!((1 << channel) & channel_mask))
248 return;
249
250 mode = data[0] & 0x0F;
251
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400252 if (mode > ATI_REMOTE2_PC) {
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200253 dev_err(&ar2->intf[0]->dev,
254 "Unknown mode byte (%02x %02x %02x %02x)\n",
255 data[3], data[2], data[1], data[0]);
256 return;
257 }
258
Peter Stokesa1421d32007-04-12 01:33:10 -0400259 if (!((1 << mode) & mode_mask))
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200260 return;
261
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200262 input_event(idev, EV_REL, REL_X, (s8) data[1]);
263 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
264 input_sync(idev);
265}
266
267static int ati_remote2_lookup(unsigned int hw_code)
268{
269 int i;
270
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400271 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200272 if (ati_remote2_key_table[i].hw_code == hw_code)
273 return i;
274
275 return -1;
276}
277
David Howells7d12e782006-10-05 14:55:46 +0100278static void ati_remote2_input_key(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200279{
280 struct input_dev *idev = ar2->idev;
281 u8 *data = ar2->buf[1];
Peter Stokesa1421d32007-04-12 01:33:10 -0400282 int channel, mode, hw_code, index;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200283
Peter Stokesa1421d32007-04-12 01:33:10 -0400284 channel = data[0] >> 4;
285
286 if (!((1 << channel) & channel_mask))
287 return;
288
289 mode = data[0] & 0x0F;
290
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400291 if (mode > ATI_REMOTE2_PC) {
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200292 dev_err(&ar2->intf[1]->dev,
293 "Unknown mode byte (%02x %02x %02x %02x)\n",
294 data[3], data[2], data[1], data[0]);
295 return;
296 }
297
298 hw_code = data[2];
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200299 if (hw_code == 0x3f) {
300 /*
301 * For some incomprehensible reason the mouse pad generates
302 * events which look identical to the events from the last
303 * pressed mode key. Naturally we don't want to generate key
304 * events for the mouse pad so we filter out any subsequent
305 * events from the same mode key.
306 */
Peter Stokesa1421d32007-04-12 01:33:10 -0400307 if (ar2->mode == mode)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200308 return;
309
310 if (data[1] == 0)
Peter Stokesa1421d32007-04-12 01:33:10 -0400311 ar2->mode = mode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200312 }
313
Peter Stokesa1421d32007-04-12 01:33:10 -0400314 if (!((1 << mode) & mode_mask))
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200315 return;
316
317 index = ati_remote2_lookup(hw_code);
318 if (index < 0) {
319 dev_err(&ar2->intf[1]->dev,
320 "Unknown code byte (%02x %02x %02x %02x)\n",
321 data[3], data[2], data[1], data[0]);
322 return;
323 }
324
325 switch (data[1]) {
326 case 0: /* release */
327 break;
328 case 1: /* press */
329 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
330 break;
331 case 2: /* repeat */
332
333 /* No repeat for mouse buttons. */
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400334 if (ar2->keycode[mode][index] == BTN_LEFT ||
335 ar2->keycode[mode][index] == BTN_RIGHT)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200336 return;
337
338 if (!time_after_eq(jiffies, ar2->jiffies))
339 return;
340
341 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
342 break;
343 default:
344 dev_err(&ar2->intf[1]->dev,
345 "Unknown state byte (%02x %02x %02x %02x)\n",
346 data[3], data[2], data[1], data[0]);
347 return;
348 }
349
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400350 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200351 input_sync(idev);
352}
353
David Howells7d12e782006-10-05 14:55:46 +0100354static void ati_remote2_complete_mouse(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200355{
356 struct ati_remote2 *ar2 = urb->context;
357 int r;
358
359 switch (urb->status) {
360 case 0:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400361 usb_mark_last_busy(ar2->udev);
David Howells7d12e782006-10-05 14:55:46 +0100362 ati_remote2_input_mouse(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200363 break;
364 case -ENOENT:
365 case -EILSEQ:
366 case -ECONNRESET:
367 case -ESHUTDOWN:
368 dev_dbg(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400369 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200370 return;
371 default:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400372 usb_mark_last_busy(ar2->udev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200373 dev_err(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400374 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200375 }
376
377 r = usb_submit_urb(urb, GFP_ATOMIC);
378 if (r)
379 dev_err(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400380 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200381}
382
David Howells7d12e782006-10-05 14:55:46 +0100383static void ati_remote2_complete_key(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200384{
385 struct ati_remote2 *ar2 = urb->context;
386 int r;
387
388 switch (urb->status) {
389 case 0:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400390 usb_mark_last_busy(ar2->udev);
David Howells7d12e782006-10-05 14:55:46 +0100391 ati_remote2_input_key(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200392 break;
393 case -ENOENT:
394 case -EILSEQ:
395 case -ECONNRESET:
396 case -ESHUTDOWN:
397 dev_dbg(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400398 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200399 return;
400 default:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400401 usb_mark_last_busy(ar2->udev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200402 dev_err(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400403 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200404 }
405
406 r = usb_submit_urb(urb, GFP_ATOMIC);
407 if (r)
408 dev_err(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400409 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200410}
411
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400412static int ati_remote2_getkeycode(struct input_dev *idev,
413 int scancode, int *keycode)
414{
415 struct ati_remote2 *ar2 = input_get_drvdata(idev);
416 int index, mode;
417
418 mode = scancode >> 8;
419 if (mode > ATI_REMOTE2_PC || !((1 << mode) & mode_mask))
420 return -EINVAL;
421
422 index = ati_remote2_lookup(scancode & 0xFF);
423 if (index < 0)
424 return -EINVAL;
425
426 *keycode = ar2->keycode[mode][index];
427 return 0;
428}
429
430static int ati_remote2_setkeycode(struct input_dev *idev, int scancode, int keycode)
431{
432 struct ati_remote2 *ar2 = input_get_drvdata(idev);
433 int index, mode, old_keycode;
434
435 mode = scancode >> 8;
436 if (mode > ATI_REMOTE2_PC || !((1 << mode) & mode_mask))
437 return -EINVAL;
438
439 index = ati_remote2_lookup(scancode & 0xFF);
440 if (index < 0)
441 return -EINVAL;
442
443 if (keycode < KEY_RESERVED || keycode > KEY_MAX)
444 return -EINVAL;
445
446 old_keycode = ar2->keycode[mode][index];
447 ar2->keycode[mode][index] = keycode;
448 set_bit(keycode, idev->keybit);
449
450 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
451 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
452 if (ar2->keycode[mode][index] == old_keycode)
453 return 0;
454 }
455 }
456
457 clear_bit(old_keycode, idev->keybit);
458
459 return 0;
460}
461
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200462static int ati_remote2_input_init(struct ati_remote2 *ar2)
463{
464 struct input_dev *idev;
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400465 int index, mode, retval;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200466
467 idev = input_allocate_device();
468 if (!idev)
469 return -ENOMEM;
470
471 ar2->idev = idev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400472 input_set_drvdata(idev, ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200473
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700474 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
475 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
476 BIT_MASK(BTN_RIGHT);
477 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400478
479 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
480 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
481 ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
482 set_bit(ar2->keycode[mode][index], idev->keybit);
483 }
484 }
485
486 /* AUX1-AUX4 and PC generate the same scancode. */
487 index = ati_remote2_lookup(0x3f);
488 ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
489 ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
490 ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
491 ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
492 ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
493 set_bit(KEY_PROG1, idev->keybit);
494 set_bit(KEY_PROG2, idev->keybit);
495 set_bit(KEY_PROG3, idev->keybit);
496 set_bit(KEY_PROG4, idev->keybit);
497 set_bit(KEY_PC, idev->keybit);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200498
499 idev->rep[REP_DELAY] = 250;
500 idev->rep[REP_PERIOD] = 33;
501
502 idev->open = ati_remote2_open;
503 idev->close = ati_remote2_close;
504
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400505 idev->getkeycode = ati_remote2_getkeycode;
506 idev->setkeycode = ati_remote2_setkeycode;
507
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200508 idev->name = ar2->name;
509 idev->phys = ar2->phys;
510
511 usb_to_input_id(ar2->udev, &idev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400512 idev->dev.parent = &ar2->udev->dev;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200513
Dmitry Torokhov50141862007-04-12 01:33:39 -0400514 retval = input_register_device(idev);
515 if (retval)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200516 input_free_device(idev);
517
Dmitry Torokhov50141862007-04-12 01:33:39 -0400518 return retval;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200519}
520
521static int ati_remote2_urb_init(struct ati_remote2 *ar2)
522{
523 struct usb_device *udev = ar2->udev;
524 int i, pipe, maxp;
525
526 for (i = 0; i < 2; i++) {
527 ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
528 if (!ar2->buf[i])
529 return -ENOMEM;
530
531 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
532 if (!ar2->urb[i])
533 return -ENOMEM;
534
535 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
536 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
537 maxp = maxp > 4 ? 4 : maxp;
538
539 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
540 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
541 ar2, ar2->ep[i]->bInterval);
542 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
543 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
544 }
545
546 return 0;
547}
548
549static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
550{
551 int i;
552
553 for (i = 0; i < 2; i++) {
Mariusz Kozlowski23815262006-11-08 15:35:50 +0100554 usb_free_urb(ar2->urb[i]);
Dmitry Torokhove37a97d2007-05-03 00:57:29 -0400555 usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200556 }
557}
558
Peter Stokesa1421d32007-04-12 01:33:10 -0400559static int ati_remote2_setup(struct ati_remote2 *ar2)
560{
561 int r, i, channel;
562
563 /*
564 * Configure receiver to only accept input from remote "channel"
565 * channel == 0 -> Accept input from any remote channel
566 * channel == 1 -> Only accept input from remote channel 1
567 * channel == 2 -> Only accept input from remote channel 2
568 * ...
569 * channel == 16 -> Only accept input from remote channel 16
570 */
571
572 channel = 0;
573 for (i = 0; i < 16; i++) {
574 if ((1 << i) & channel_mask) {
575 if (!(~(1 << i) & 0xFFFF & channel_mask))
576 channel = i + 1;
577 break;
578 }
579 }
580
581 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
582 0x20,
583 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
584 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
585 if (r) {
586 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400587 __func__, r);
Peter Stokesa1421d32007-04-12 01:33:10 -0400588 return r;
589 }
590
591 return 0;
592}
593
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200594static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
595{
596 struct usb_device *udev = interface_to_usbdev(interface);
597 struct usb_host_interface *alt = interface->cur_altsetting;
598 struct ati_remote2 *ar2;
599 int r;
600
601 if (alt->desc.bInterfaceNumber)
602 return -ENODEV;
603
604 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
605 if (!ar2)
606 return -ENOMEM;
607
608 ar2->udev = udev;
609
610 ar2->intf[0] = interface;
611 ar2->ep[0] = &alt->endpoint[0].desc;
612
613 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
614 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
615 if (r)
616 goto fail1;
617 alt = ar2->intf[1]->cur_altsetting;
618 ar2->ep[1] = &alt->endpoint[0].desc;
619
620 r = ati_remote2_urb_init(ar2);
621 if (r)
622 goto fail2;
623
Peter Stokesa1421d32007-04-12 01:33:10 -0400624 r = ati_remote2_setup(ar2);
625 if (r)
626 goto fail2;
627
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200628 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
629 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
630
631 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
632
633 r = ati_remote2_input_init(ar2);
634 if (r)
635 goto fail2;
636
637 usb_set_intfdata(interface, ar2);
638
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400639 interface->needs_remote_wakeup = 1;
640
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200641 return 0;
642
643 fail2:
644 ati_remote2_urb_cleanup(ar2);
645
646 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
647 fail1:
648 kfree(ar2);
649
650 return r;
651}
652
653static void ati_remote2_disconnect(struct usb_interface *interface)
654{
655 struct ati_remote2 *ar2;
656 struct usb_host_interface *alt = interface->cur_altsetting;
657
658 if (alt->desc.bInterfaceNumber)
659 return;
660
661 ar2 = usb_get_intfdata(interface);
662 usb_set_intfdata(interface, NULL);
663
664 input_unregister_device(ar2->idev);
665
666 ati_remote2_urb_cleanup(ar2);
667
668 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
669
670 kfree(ar2);
671}
672
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400673static int ati_remote2_suspend(struct usb_interface *interface,
674 pm_message_t message)
675{
676 struct ati_remote2 *ar2;
677 struct usb_host_interface *alt = interface->cur_altsetting;
678
679 if (alt->desc.bInterfaceNumber)
680 return 0;
681
682 ar2 = usb_get_intfdata(interface);
683
684 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
685
686 mutex_lock(&ati_remote2_mutex);
687
688 if (ar2->flags & ATI_REMOTE2_OPENED)
689 ati_remote2_kill_urbs(ar2);
690
691 ar2->flags |= ATI_REMOTE2_SUSPENDED;
692
693 mutex_unlock(&ati_remote2_mutex);
694
695 return 0;
696}
697
698static int ati_remote2_resume(struct usb_interface *interface)
699{
700 struct ati_remote2 *ar2;
701 struct usb_host_interface *alt = interface->cur_altsetting;
702 int r = 0;
703
704 if (alt->desc.bInterfaceNumber)
705 return 0;
706
707 ar2 = usb_get_intfdata(interface);
708
709 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
710
711 mutex_lock(&ati_remote2_mutex);
712
713 if (ar2->flags & ATI_REMOTE2_OPENED)
714 r = ati_remote2_submit_urbs(ar2);
715
716 if (!r)
717 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
718
719 mutex_unlock(&ati_remote2_mutex);
720
721 return r;
722}
723
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800724static int ati_remote2_reset_resume(struct usb_interface *interface)
725{
726 struct ati_remote2 *ar2;
727 struct usb_host_interface *alt = interface->cur_altsetting;
728 int r = 0;
729
730 if (alt->desc.bInterfaceNumber)
731 return 0;
732
733 ar2 = usb_get_intfdata(interface);
734
735 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
736
737 mutex_lock(&ati_remote2_mutex);
738
739 r = ati_remote2_setup(ar2);
740 if (r)
741 goto out;
742
743 if (ar2->flags & ATI_REMOTE2_OPENED)
744 r = ati_remote2_submit_urbs(ar2);
745
746 if (!r)
747 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
748
749 out:
750 mutex_unlock(&ati_remote2_mutex);
751
752 return r;
753}
754
755static int ati_remote2_pre_reset(struct usb_interface *interface)
756{
757 struct ati_remote2 *ar2;
758 struct usb_host_interface *alt = interface->cur_altsetting;
759
760 if (alt->desc.bInterfaceNumber)
761 return 0;
762
763 ar2 = usb_get_intfdata(interface);
764
765 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
766
767 mutex_lock(&ati_remote2_mutex);
768
769 if (ar2->flags == ATI_REMOTE2_OPENED)
770 ati_remote2_kill_urbs(ar2);
771
772 return 0;
773}
774
775static int ati_remote2_post_reset(struct usb_interface *interface)
776{
777 struct ati_remote2 *ar2;
778 struct usb_host_interface *alt = interface->cur_altsetting;
779 int r = 0;
780
781 if (alt->desc.bInterfaceNumber)
782 return 0;
783
784 ar2 = usb_get_intfdata(interface);
785
786 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
787
788 if (ar2->flags == ATI_REMOTE2_OPENED)
789 r = ati_remote2_submit_urbs(ar2);
790
791 mutex_unlock(&ati_remote2_mutex);
792
793 return r;
794}
795
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200796static int __init ati_remote2_init(void)
797{
798 int r;
799
800 r = usb_register(&ati_remote2_driver);
801 if (r)
802 printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
803 else
804 printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
805
806 return r;
807}
808
809static void __exit ati_remote2_exit(void)
810{
811 usb_deregister(&ati_remote2_driver);
812}
813
814module_init(ati_remote2_init);
815module_exit(ati_remote2_exit);