blob: 0871d7b2df438172a3b6154eab786e88335beb3a [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
Ville Syrjalad329e332009-01-29 23:42:16 -080034enum {
35 ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,
36 ATI_REMOTE2_MAX_MODE_MASK = 0x1F,
37};
38
39static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK;
Peter Stokesa1421d32007-04-12 01:33:10 -040040module_param(channel_mask, uint, 0644);
41MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
42
Ville Syrjalad329e332009-01-29 23:42:16 -080043static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020044module_param(mode_mask, uint, 0644);
45MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
46
47static struct usb_device_id ati_remote2_id_table[] = {
48 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
49 { }
50};
51MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
52
Ville Syrjalad6505ab2008-07-03 10:45:37 -040053static DEFINE_MUTEX(ati_remote2_mutex);
54
55enum {
56 ATI_REMOTE2_OPENED = 0x1,
57 ATI_REMOTE2_SUSPENDED = 0x2,
58};
59
Ville Syrjala1971b9d2008-07-03 10:45:37 -040060enum {
61 ATI_REMOTE2_AUX1,
62 ATI_REMOTE2_AUX2,
63 ATI_REMOTE2_AUX3,
64 ATI_REMOTE2_AUX4,
65 ATI_REMOTE2_PC,
66 ATI_REMOTE2_MODES,
67};
68
69static const struct {
70 u8 hw_code;
71 u16 keycode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020072} ati_remote2_key_table[] = {
73 { 0x00, KEY_0 },
74 { 0x01, KEY_1 },
75 { 0x02, KEY_2 },
76 { 0x03, KEY_3 },
77 { 0x04, KEY_4 },
78 { 0x05, KEY_5 },
79 { 0x06, KEY_6 },
80 { 0x07, KEY_7 },
81 { 0x08, KEY_8 },
82 { 0x09, KEY_9 },
83 { 0x0c, KEY_POWER },
84 { 0x0d, KEY_MUTE },
85 { 0x10, KEY_VOLUMEUP },
86 { 0x11, KEY_VOLUMEDOWN },
87 { 0x20, KEY_CHANNELUP },
88 { 0x21, KEY_CHANNELDOWN },
89 { 0x28, KEY_FORWARD },
90 { 0x29, KEY_REWIND },
91 { 0x2c, KEY_PLAY },
92 { 0x30, KEY_PAUSE },
93 { 0x31, KEY_STOP },
94 { 0x37, KEY_RECORD },
95 { 0x38, KEY_DVD },
96 { 0x39, KEY_TV },
Ville Syrjala1971b9d2008-07-03 10:45:37 -040097 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020098 { 0x54, KEY_MENU },
99 { 0x58, KEY_UP },
100 { 0x59, KEY_DOWN },
101 { 0x5a, KEY_LEFT },
102 { 0x5b, KEY_RIGHT },
103 { 0x5c, KEY_OK },
104 { 0x78, KEY_A },
105 { 0x79, KEY_B },
106 { 0x7a, KEY_C },
107 { 0x7b, KEY_D },
108 { 0x7c, KEY_E },
109 { 0x7d, KEY_F },
110 { 0x82, KEY_ENTER },
111 { 0x8e, KEY_VENDOR },
112 { 0x96, KEY_COFFEE },
113 { 0xa9, BTN_LEFT },
114 { 0xaa, BTN_RIGHT },
115 { 0xbe, KEY_QUESTION },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200116 { 0xd0, KEY_EDIT },
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400117 { 0xd5, KEY_FRONT },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200118 { 0xf9, KEY_INFO },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200119};
120
121struct ati_remote2 {
122 struct input_dev *idev;
123 struct usb_device *udev;
124
125 struct usb_interface *intf[2];
126 struct usb_endpoint_descriptor *ep[2];
127 struct urb *urb[2];
128 void *buf[2];
129 dma_addr_t buf_dma[2];
130
131 unsigned long jiffies;
132 int mode;
133
134 char name[64];
135 char phys[64];
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400136
137 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
138 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400139
140 unsigned int flags;
Ville Syrjalad329e332009-01-29 23:42:16 -0800141
142 unsigned int channel_mask;
143 unsigned int mode_mask;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200144};
145
146static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
147static void ati_remote2_disconnect(struct usb_interface *interface);
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400148static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message);
149static int ati_remote2_resume(struct usb_interface *interface);
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800150static int ati_remote2_reset_resume(struct usb_interface *interface);
151static int ati_remote2_pre_reset(struct usb_interface *interface);
152static int ati_remote2_post_reset(struct usb_interface *interface);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200153
154static struct usb_driver ati_remote2_driver = {
155 .name = "ati_remote2",
156 .probe = ati_remote2_probe,
157 .disconnect = ati_remote2_disconnect,
158 .id_table = ati_remote2_id_table,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400159 .suspend = ati_remote2_suspend,
160 .resume = ati_remote2_resume,
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800161 .reset_resume = ati_remote2_reset_resume,
162 .pre_reset = ati_remote2_pre_reset,
163 .post_reset = ati_remote2_post_reset,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400164 .supports_autosuspend = 1,
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200165};
166
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400167static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200168{
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200169 int r;
170
171 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
172 if (r) {
173 dev_err(&ar2->intf[0]->dev,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400174 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200175 return r;
176 }
177 r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
178 if (r) {
179 usb_kill_urb(ar2->urb[0]);
180 dev_err(&ar2->intf[1]->dev,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400181 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200182 return r;
183 }
184
185 return 0;
186}
187
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400188static void ati_remote2_kill_urbs(struct ati_remote2 *ar2)
189{
190 usb_kill_urb(ar2->urb[1]);
191 usb_kill_urb(ar2->urb[0]);
192}
193
194static int ati_remote2_open(struct input_dev *idev)
195{
196 struct ati_remote2 *ar2 = input_get_drvdata(idev);
197 int r;
198
199 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
200
201 r = usb_autopm_get_interface(ar2->intf[0]);
202 if (r) {
203 dev_err(&ar2->intf[0]->dev,
204 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
205 goto fail1;
206 }
207
208 mutex_lock(&ati_remote2_mutex);
209
210 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) {
211 r = ati_remote2_submit_urbs(ar2);
212 if (r)
213 goto fail2;
214 }
215
216 ar2->flags |= ATI_REMOTE2_OPENED;
217
218 mutex_unlock(&ati_remote2_mutex);
219
220 usb_autopm_put_interface(ar2->intf[0]);
221
222 return 0;
223
224 fail2:
225 mutex_unlock(&ati_remote2_mutex);
226 usb_autopm_put_interface(ar2->intf[0]);
227 fail1:
228 return r;
229}
230
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200231static void ati_remote2_close(struct input_dev *idev)
232{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400233 struct ati_remote2 *ar2 = input_get_drvdata(idev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200234
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400235 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
236
237 mutex_lock(&ati_remote2_mutex);
238
239 if (!(ar2->flags & ATI_REMOTE2_SUSPENDED))
240 ati_remote2_kill_urbs(ar2);
241
242 ar2->flags &= ~ATI_REMOTE2_OPENED;
243
244 mutex_unlock(&ati_remote2_mutex);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200245}
246
David Howells7d12e782006-10-05 14:55:46 +0100247static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200248{
249 struct input_dev *idev = ar2->idev;
250 u8 *data = ar2->buf[0];
Peter Stokesa1421d32007-04-12 01:33:10 -0400251 int channel, mode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200252
Peter Stokesa1421d32007-04-12 01:33:10 -0400253 channel = data[0] >> 4;
254
Ville Syrjalad329e332009-01-29 23:42:16 -0800255 if (!((1 << channel) & ar2->channel_mask))
Peter Stokesa1421d32007-04-12 01:33:10 -0400256 return;
257
258 mode = data[0] & 0x0F;
259
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400260 if (mode > ATI_REMOTE2_PC) {
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200261 dev_err(&ar2->intf[0]->dev,
262 "Unknown mode byte (%02x %02x %02x %02x)\n",
263 data[3], data[2], data[1], data[0]);
264 return;
265 }
266
Ville Syrjalad329e332009-01-29 23:42:16 -0800267 if (!((1 << mode) & ar2->mode_mask))
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200268 return;
269
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200270 input_event(idev, EV_REL, REL_X, (s8) data[1]);
271 input_event(idev, EV_REL, REL_Y, (s8) data[2]);
272 input_sync(idev);
273}
274
275static int ati_remote2_lookup(unsigned int hw_code)
276{
277 int i;
278
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400279 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200280 if (ati_remote2_key_table[i].hw_code == hw_code)
281 return i;
282
283 return -1;
284}
285
David Howells7d12e782006-10-05 14:55:46 +0100286static void ati_remote2_input_key(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200287{
288 struct input_dev *idev = ar2->idev;
289 u8 *data = ar2->buf[1];
Peter Stokesa1421d32007-04-12 01:33:10 -0400290 int channel, mode, hw_code, index;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200291
Peter Stokesa1421d32007-04-12 01:33:10 -0400292 channel = data[0] >> 4;
293
Ville Syrjalad329e332009-01-29 23:42:16 -0800294 if (!((1 << channel) & ar2->channel_mask))
Peter Stokesa1421d32007-04-12 01:33:10 -0400295 return;
296
297 mode = data[0] & 0x0F;
298
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400299 if (mode > ATI_REMOTE2_PC) {
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200300 dev_err(&ar2->intf[1]->dev,
301 "Unknown mode byte (%02x %02x %02x %02x)\n",
302 data[3], data[2], data[1], data[0]);
303 return;
304 }
305
306 hw_code = data[2];
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200307 if (hw_code == 0x3f) {
308 /*
309 * For some incomprehensible reason the mouse pad generates
310 * events which look identical to the events from the last
311 * pressed mode key. Naturally we don't want to generate key
312 * events for the mouse pad so we filter out any subsequent
313 * events from the same mode key.
314 */
Peter Stokesa1421d32007-04-12 01:33:10 -0400315 if (ar2->mode == mode)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200316 return;
317
318 if (data[1] == 0)
Peter Stokesa1421d32007-04-12 01:33:10 -0400319 ar2->mode = mode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200320 }
321
Ville Syrjalad329e332009-01-29 23:42:16 -0800322 if (!((1 << mode) & ar2->mode_mask))
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200323 return;
324
325 index = ati_remote2_lookup(hw_code);
326 if (index < 0) {
327 dev_err(&ar2->intf[1]->dev,
328 "Unknown code byte (%02x %02x %02x %02x)\n",
329 data[3], data[2], data[1], data[0]);
330 return;
331 }
332
333 switch (data[1]) {
334 case 0: /* release */
335 break;
336 case 1: /* press */
337 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
338 break;
339 case 2: /* repeat */
340
341 /* No repeat for mouse buttons. */
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400342 if (ar2->keycode[mode][index] == BTN_LEFT ||
343 ar2->keycode[mode][index] == BTN_RIGHT)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200344 return;
345
346 if (!time_after_eq(jiffies, ar2->jiffies))
347 return;
348
349 ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
350 break;
351 default:
352 dev_err(&ar2->intf[1]->dev,
353 "Unknown state byte (%02x %02x %02x %02x)\n",
354 data[3], data[2], data[1], data[0]);
355 return;
356 }
357
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400358 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200359 input_sync(idev);
360}
361
David Howells7d12e782006-10-05 14:55:46 +0100362static void ati_remote2_complete_mouse(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200363{
364 struct ati_remote2 *ar2 = urb->context;
365 int r;
366
367 switch (urb->status) {
368 case 0:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400369 usb_mark_last_busy(ar2->udev);
David Howells7d12e782006-10-05 14:55:46 +0100370 ati_remote2_input_mouse(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200371 break;
372 case -ENOENT:
373 case -EILSEQ:
374 case -ECONNRESET:
375 case -ESHUTDOWN:
376 dev_dbg(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400377 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200378 return;
379 default:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400380 usb_mark_last_busy(ar2->udev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200381 dev_err(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400382 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200383 }
384
385 r = usb_submit_urb(urb, GFP_ATOMIC);
386 if (r)
387 dev_err(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400388 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200389}
390
David Howells7d12e782006-10-05 14:55:46 +0100391static void ati_remote2_complete_key(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200392{
393 struct ati_remote2 *ar2 = urb->context;
394 int r;
395
396 switch (urb->status) {
397 case 0:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400398 usb_mark_last_busy(ar2->udev);
David Howells7d12e782006-10-05 14:55:46 +0100399 ati_remote2_input_key(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200400 break;
401 case -ENOENT:
402 case -EILSEQ:
403 case -ECONNRESET:
404 case -ESHUTDOWN:
405 dev_dbg(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400406 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200407 return;
408 default:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400409 usb_mark_last_busy(ar2->udev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200410 dev_err(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400411 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200412 }
413
414 r = usb_submit_urb(urb, GFP_ATOMIC);
415 if (r)
416 dev_err(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400417 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200418}
419
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400420static int ati_remote2_getkeycode(struct input_dev *idev,
421 int scancode, int *keycode)
422{
423 struct ati_remote2 *ar2 = input_get_drvdata(idev);
424 int index, mode;
425
426 mode = scancode >> 8;
Ville Syrjalad329e332009-01-29 23:42:16 -0800427 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400428 return -EINVAL;
429
430 index = ati_remote2_lookup(scancode & 0xFF);
431 if (index < 0)
432 return -EINVAL;
433
434 *keycode = ar2->keycode[mode][index];
435 return 0;
436}
437
438static int ati_remote2_setkeycode(struct input_dev *idev, int scancode, int keycode)
439{
440 struct ati_remote2 *ar2 = input_get_drvdata(idev);
441 int index, mode, old_keycode;
442
443 mode = scancode >> 8;
Ville Syrjalad329e332009-01-29 23:42:16 -0800444 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400445 return -EINVAL;
446
447 index = ati_remote2_lookup(scancode & 0xFF);
448 if (index < 0)
449 return -EINVAL;
450
451 if (keycode < KEY_RESERVED || keycode > KEY_MAX)
452 return -EINVAL;
453
454 old_keycode = ar2->keycode[mode][index];
455 ar2->keycode[mode][index] = keycode;
456 set_bit(keycode, idev->keybit);
457
458 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
459 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
460 if (ar2->keycode[mode][index] == old_keycode)
461 return 0;
462 }
463 }
464
465 clear_bit(old_keycode, idev->keybit);
466
467 return 0;
468}
469
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200470static int ati_remote2_input_init(struct ati_remote2 *ar2)
471{
472 struct input_dev *idev;
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400473 int index, mode, retval;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200474
475 idev = input_allocate_device();
476 if (!idev)
477 return -ENOMEM;
478
479 ar2->idev = idev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400480 input_set_drvdata(idev, ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200481
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700482 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL);
483 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
484 BIT_MASK(BTN_RIGHT);
485 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400486
487 for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) {
488 for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) {
489 ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode;
490 set_bit(ar2->keycode[mode][index], idev->keybit);
491 }
492 }
493
494 /* AUX1-AUX4 and PC generate the same scancode. */
495 index = ati_remote2_lookup(0x3f);
496 ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1;
497 ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2;
498 ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3;
499 ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4;
500 ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC;
501 set_bit(KEY_PROG1, idev->keybit);
502 set_bit(KEY_PROG2, idev->keybit);
503 set_bit(KEY_PROG3, idev->keybit);
504 set_bit(KEY_PROG4, idev->keybit);
505 set_bit(KEY_PC, idev->keybit);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200506
507 idev->rep[REP_DELAY] = 250;
508 idev->rep[REP_PERIOD] = 33;
509
510 idev->open = ati_remote2_open;
511 idev->close = ati_remote2_close;
512
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400513 idev->getkeycode = ati_remote2_getkeycode;
514 idev->setkeycode = ati_remote2_setkeycode;
515
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200516 idev->name = ar2->name;
517 idev->phys = ar2->phys;
518
519 usb_to_input_id(ar2->udev, &idev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400520 idev->dev.parent = &ar2->udev->dev;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200521
Dmitry Torokhov50141862007-04-12 01:33:39 -0400522 retval = input_register_device(idev);
523 if (retval)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200524 input_free_device(idev);
525
Dmitry Torokhov50141862007-04-12 01:33:39 -0400526 return retval;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200527}
528
529static int ati_remote2_urb_init(struct ati_remote2 *ar2)
530{
531 struct usb_device *udev = ar2->udev;
532 int i, pipe, maxp;
533
534 for (i = 0; i < 2; i++) {
535 ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
536 if (!ar2->buf[i])
537 return -ENOMEM;
538
539 ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
540 if (!ar2->urb[i])
541 return -ENOMEM;
542
543 pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
544 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
545 maxp = maxp > 4 ? 4 : maxp;
546
547 usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
548 i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
549 ar2, ar2->ep[i]->bInterval);
550 ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
551 ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
552 }
553
554 return 0;
555}
556
557static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
558{
559 int i;
560
561 for (i = 0; i < 2; i++) {
Mariusz Kozlowski23815262006-11-08 15:35:50 +0100562 usb_free_urb(ar2->urb[i]);
Dmitry Torokhove37a97d2007-05-03 00:57:29 -0400563 usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200564 }
565}
566
Ville Syrjalad329e332009-01-29 23:42:16 -0800567static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
Peter Stokesa1421d32007-04-12 01:33:10 -0400568{
569 int r, i, channel;
570
571 /*
572 * Configure receiver to only accept input from remote "channel"
573 * channel == 0 -> Accept input from any remote channel
574 * channel == 1 -> Only accept input from remote channel 1
575 * channel == 2 -> Only accept input from remote channel 2
576 * ...
577 * channel == 16 -> Only accept input from remote channel 16
578 */
579
580 channel = 0;
581 for (i = 0; i < 16; i++) {
Ville Syrjalad329e332009-01-29 23:42:16 -0800582 if ((1 << i) & ch_mask) {
583 if (!(~(1 << i) & ch_mask))
Peter Stokesa1421d32007-04-12 01:33:10 -0400584 channel = i + 1;
585 break;
586 }
587 }
588
589 r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
590 0x20,
591 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
592 channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
593 if (r) {
594 dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400595 __func__, r);
Peter Stokesa1421d32007-04-12 01:33:10 -0400596 return r;
597 }
598
599 return 0;
600}
601
Ville Syrjalad329e332009-01-29 23:42:16 -0800602static ssize_t ati_remote2_show_channel_mask(struct device *dev,
603 struct device_attribute *attr,
604 char *buf)
605{
606 struct usb_device *udev = to_usb_device(dev);
607 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
608 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
609
610 return sprintf(buf, "0x%04x\n", ar2->channel_mask);
611}
612
613static ssize_t ati_remote2_store_channel_mask(struct device *dev,
614 struct device_attribute *attr,
615 const char *buf, size_t count)
616{
617 struct usb_device *udev = to_usb_device(dev);
618 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
619 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
620 unsigned long mask;
621 int r;
622
623 if (strict_strtoul(buf, 0, &mask))
624 return -EINVAL;
625
626 if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK)
627 return -EINVAL;
628
629 r = usb_autopm_get_interface(ar2->intf[0]);
630 if (r) {
631 dev_err(&ar2->intf[0]->dev,
632 "%s(): usb_autopm_get_interface() = %d\n", __func__, r);
633 return r;
634 }
635
636 mutex_lock(&ati_remote2_mutex);
637
638 if (mask != ar2->channel_mask && !ati_remote2_setup(ar2, mask))
639 ar2->channel_mask = mask;
640
641 mutex_unlock(&ati_remote2_mutex);
642
643 usb_autopm_put_interface(ar2->intf[0]);
644
645 return count;
646}
647
648static ssize_t ati_remote2_show_mode_mask(struct device *dev,
649 struct device_attribute *attr,
650 char *buf)
651{
652 struct usb_device *udev = to_usb_device(dev);
653 struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
654 struct ati_remote2 *ar2 = usb_get_intfdata(intf);
655
656 return sprintf(buf, "0x%02x\n", ar2->mode_mask);
657}
658
659static ssize_t ati_remote2_store_mode_mask(struct device *dev,
660 struct device_attribute *attr,
661 const char *buf, size_t count)
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 unsigned long mask;
667
668 if (strict_strtoul(buf, 0, &mask))
669 return -EINVAL;
670
671 if (mask & ~ATI_REMOTE2_MAX_MODE_MASK)
672 return -EINVAL;
673
674 ar2->mode_mask = mask;
675
676 return count;
677}
678
679static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
680 ati_remote2_store_channel_mask);
681
682static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
683 ati_remote2_store_mode_mask);
684
685static struct attribute *ati_remote2_attrs[] = {
686 &dev_attr_channel_mask.attr,
687 &dev_attr_mode_mask.attr,
688 NULL,
689};
690
691static struct attribute_group ati_remote2_attr_group = {
692 .attrs = ati_remote2_attrs,
693};
694
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200695static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
696{
697 struct usb_device *udev = interface_to_usbdev(interface);
698 struct usb_host_interface *alt = interface->cur_altsetting;
699 struct ati_remote2 *ar2;
700 int r;
701
702 if (alt->desc.bInterfaceNumber)
703 return -ENODEV;
704
705 ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
706 if (!ar2)
707 return -ENOMEM;
708
709 ar2->udev = udev;
710
711 ar2->intf[0] = interface;
712 ar2->ep[0] = &alt->endpoint[0].desc;
713
714 ar2->intf[1] = usb_ifnum_to_if(udev, 1);
715 r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
716 if (r)
717 goto fail1;
718 alt = ar2->intf[1]->cur_altsetting;
719 ar2->ep[1] = &alt->endpoint[0].desc;
720
721 r = ati_remote2_urb_init(ar2);
722 if (r)
723 goto fail2;
724
Ville Syrjalad329e332009-01-29 23:42:16 -0800725 ar2->channel_mask = channel_mask & ATI_REMOTE2_MAX_CHANNEL_MASK;
726 ar2->mode_mask = mode_mask & ATI_REMOTE2_MAX_MODE_MASK;
727
728 r = ati_remote2_setup(ar2, ar2->channel_mask);
Peter Stokesa1421d32007-04-12 01:33:10 -0400729 if (r)
730 goto fail2;
731
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200732 usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
733 strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
734
735 strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
736
Ville Syrjalad329e332009-01-29 23:42:16 -0800737 r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200738 if (r)
739 goto fail2;
740
Ville Syrjalad329e332009-01-29 23:42:16 -0800741 r = ati_remote2_input_init(ar2);
742 if (r)
743 goto fail3;
744
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200745 usb_set_intfdata(interface, ar2);
746
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400747 interface->needs_remote_wakeup = 1;
748
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200749 return 0;
750
Ville Syrjalad329e332009-01-29 23:42:16 -0800751 fail3:
752 sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200753 fail2:
754 ati_remote2_urb_cleanup(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200755 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
756 fail1:
757 kfree(ar2);
758
759 return r;
760}
761
762static void ati_remote2_disconnect(struct usb_interface *interface)
763{
764 struct ati_remote2 *ar2;
765 struct usb_host_interface *alt = interface->cur_altsetting;
766
767 if (alt->desc.bInterfaceNumber)
768 return;
769
770 ar2 = usb_get_intfdata(interface);
771 usb_set_intfdata(interface, NULL);
772
773 input_unregister_device(ar2->idev);
774
Ville Syrjalad329e332009-01-29 23:42:16 -0800775 sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);
776
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200777 ati_remote2_urb_cleanup(ar2);
778
779 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
780
781 kfree(ar2);
782}
783
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400784static int ati_remote2_suspend(struct usb_interface *interface,
785 pm_message_t message)
786{
787 struct ati_remote2 *ar2;
788 struct usb_host_interface *alt = interface->cur_altsetting;
789
790 if (alt->desc.bInterfaceNumber)
791 return 0;
792
793 ar2 = usb_get_intfdata(interface);
794
795 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
796
797 mutex_lock(&ati_remote2_mutex);
798
799 if (ar2->flags & ATI_REMOTE2_OPENED)
800 ati_remote2_kill_urbs(ar2);
801
802 ar2->flags |= ATI_REMOTE2_SUSPENDED;
803
804 mutex_unlock(&ati_remote2_mutex);
805
806 return 0;
807}
808
809static int ati_remote2_resume(struct usb_interface *interface)
810{
811 struct ati_remote2 *ar2;
812 struct usb_host_interface *alt = interface->cur_altsetting;
813 int r = 0;
814
815 if (alt->desc.bInterfaceNumber)
816 return 0;
817
818 ar2 = usb_get_intfdata(interface);
819
820 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
821
822 mutex_lock(&ati_remote2_mutex);
823
824 if (ar2->flags & ATI_REMOTE2_OPENED)
825 r = ati_remote2_submit_urbs(ar2);
826
827 if (!r)
828 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
829
830 mutex_unlock(&ati_remote2_mutex);
831
832 return r;
833}
834
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800835static int ati_remote2_reset_resume(struct usb_interface *interface)
836{
837 struct ati_remote2 *ar2;
838 struct usb_host_interface *alt = interface->cur_altsetting;
839 int r = 0;
840
841 if (alt->desc.bInterfaceNumber)
842 return 0;
843
844 ar2 = usb_get_intfdata(interface);
845
846 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
847
848 mutex_lock(&ati_remote2_mutex);
849
Ville Syrjalad329e332009-01-29 23:42:16 -0800850 r = ati_remote2_setup(ar2, ar2->channel_mask);
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800851 if (r)
852 goto out;
853
854 if (ar2->flags & ATI_REMOTE2_OPENED)
855 r = ati_remote2_submit_urbs(ar2);
856
857 if (!r)
858 ar2->flags &= ~ATI_REMOTE2_SUSPENDED;
859
860 out:
861 mutex_unlock(&ati_remote2_mutex);
862
863 return r;
864}
865
866static int ati_remote2_pre_reset(struct usb_interface *interface)
867{
868 struct ati_remote2 *ar2;
869 struct usb_host_interface *alt = interface->cur_altsetting;
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 ati_remote2_kill_urbs(ar2);
882
883 return 0;
884}
885
886static int ati_remote2_post_reset(struct usb_interface *interface)
887{
888 struct ati_remote2 *ar2;
889 struct usb_host_interface *alt = interface->cur_altsetting;
890 int r = 0;
891
892 if (alt->desc.bInterfaceNumber)
893 return 0;
894
895 ar2 = usb_get_intfdata(interface);
896
897 dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__);
898
899 if (ar2->flags == ATI_REMOTE2_OPENED)
900 r = ati_remote2_submit_urbs(ar2);
901
902 mutex_unlock(&ati_remote2_mutex);
903
904 return r;
905}
906
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200907static int __init ati_remote2_init(void)
908{
909 int r;
910
911 r = usb_register(&ati_remote2_driver);
912 if (r)
913 printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
914 else
915 printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
916
917 return r;
918}
919
920static void __exit ati_remote2_exit(void)
921{
922 usb_deregister(&ati_remote2_driver);
923}
924
925module_init(ati_remote2_init);
926module_exit(ati_remote2_exit);