blob: e148749b5851a6578a58848034896ddcaa27bec8 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020014
15#define DRIVER_DESC "ATI/Philips USB RF remote driver"
Ville Syrjala1971b9d2008-07-03 10:45:37 -040016#define DRIVER_VERSION "0.3"
Ville Syrjälä735b0cb2005-12-10 20:30:54 +020017
18MODULE_DESCRIPTION(DRIVER_DESC);
19MODULE_VERSION(DRIVER_VERSION);
20MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
21MODULE_LICENSE("GPL");
22
Peter Stokesa1421d32007-04-12 01:33:10 -040023/*
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
Ville Syrjala1971b9d2008-07-03 10:45:37 -040031 * 16 and then press the "PC" button again, the button will slowly flash the count of the
Peter Stokesa1421d32007-04-12 01:33:10 -040032 * newly configured "channel".
33 */
34
Ville Syrjalad329e332009-01-29 23:42:16 -080035enum {
36 ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF,
37 ATI_REMOTE2_MAX_MODE_MASK = 0x1F,
38};
39
Ville Syrjala8a49cfa2009-01-29 23:42:16 -080040static 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
61static 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
69static 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
76static 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
83static 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
Ville Syrjalad329e332009-01-29 23:42:16 -080090static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK;
Ville Syrjala8a49cfa2009-01-29 23:42:16 -080091#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
94module_param(channel_mask, channel_mask, 0644);
Peter Stokesa1421d32007-04-12 01:33:10 -040095MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
96
Ville Syrjalad329e332009-01-29 23:42:16 -080097static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK;
Ville Syrjala8a49cfa2009-01-29 23:42:16 -080098#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
101module_param(mode_mask, mode_mask, 0644);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200102MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
103
104static struct usb_device_id ati_remote2_id_table[] = {
105 { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
106 { }
107};
108MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
109
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400110static DEFINE_MUTEX(ati_remote2_mutex);
111
112enum {
113 ATI_REMOTE2_OPENED = 0x1,
114 ATI_REMOTE2_SUSPENDED = 0x2,
115};
116
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400117enum {
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
126static const struct {
127 u8 hw_code;
128 u16 keycode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200129} 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 },
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400154 { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200155 { 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 },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200173 { 0xd0, KEY_EDIT },
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400174 { 0xd5, KEY_FRONT },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200175 { 0xf9, KEY_INFO },
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200176};
177
178struct 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];
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400193
194 /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */
195 u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)];
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400196
197 unsigned int flags;
Ville Syrjalad329e332009-01-29 23:42:16 -0800198
199 unsigned int channel_mask;
200 unsigned int mode_mask;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200201};
202
203static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
204static void ati_remote2_disconnect(struct usb_interface *interface);
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400205static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message);
206static int ati_remote2_resume(struct usb_interface *interface);
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800207static int ati_remote2_reset_resume(struct usb_interface *interface);
208static int ati_remote2_pre_reset(struct usb_interface *interface);
209static int ati_remote2_post_reset(struct usb_interface *interface);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200210
211static 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,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400216 .suspend = ati_remote2_suspend,
217 .resume = ati_remote2_resume,
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800218 .reset_resume = ati_remote2_reset_resume,
219 .pre_reset = ati_remote2_pre_reset,
220 .post_reset = ati_remote2_post_reset,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400221 .supports_autosuspend = 1,
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200222};
223
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400224static int ati_remote2_submit_urbs(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200225{
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200226 int r;
227
228 r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
229 if (r) {
230 dev_err(&ar2->intf[0]->dev,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400231 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200232 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,
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400238 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200239 return r;
240 }
241
242 return 0;
243}
244
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400245static 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
251static 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
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200288static void ati_remote2_close(struct input_dev *idev)
289{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400290 struct ati_remote2 *ar2 = input_get_drvdata(idev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200291
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400292 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);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200302}
303
David Howells7d12e782006-10-05 14:55:46 +0100304static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200305{
306 struct input_dev *idev = ar2->idev;
307 u8 *data = ar2->buf[0];
Peter Stokesa1421d32007-04-12 01:33:10 -0400308 int channel, mode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200309
Peter Stokesa1421d32007-04-12 01:33:10 -0400310 channel = data[0] >> 4;
311
Ville Syrjalad329e332009-01-29 23:42:16 -0800312 if (!((1 << channel) & ar2->channel_mask))
Peter Stokesa1421d32007-04-12 01:33:10 -0400313 return;
314
315 mode = data[0] & 0x0F;
316
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400317 if (mode > ATI_REMOTE2_PC) {
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200318 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
Ville Syrjalad329e332009-01-29 23:42:16 -0800324 if (!((1 << mode) & ar2->mode_mask))
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200325 return;
326
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200327 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
332static int ati_remote2_lookup(unsigned int hw_code)
333{
334 int i;
335
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400336 for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200337 if (ati_remote2_key_table[i].hw_code == hw_code)
338 return i;
339
340 return -1;
341}
342
David Howells7d12e782006-10-05 14:55:46 +0100343static void ati_remote2_input_key(struct ati_remote2 *ar2)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200344{
345 struct input_dev *idev = ar2->idev;
346 u8 *data = ar2->buf[1];
Peter Stokesa1421d32007-04-12 01:33:10 -0400347 int channel, mode, hw_code, index;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200348
Peter Stokesa1421d32007-04-12 01:33:10 -0400349 channel = data[0] >> 4;
350
Ville Syrjalad329e332009-01-29 23:42:16 -0800351 if (!((1 << channel) & ar2->channel_mask))
Peter Stokesa1421d32007-04-12 01:33:10 -0400352 return;
353
354 mode = data[0] & 0x0F;
355
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400356 if (mode > ATI_REMOTE2_PC) {
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200357 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];
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200364 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 */
Peter Stokesa1421d32007-04-12 01:33:10 -0400372 if (ar2->mode == mode)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200373 return;
374
375 if (data[1] == 0)
Peter Stokesa1421d32007-04-12 01:33:10 -0400376 ar2->mode = mode;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200377 }
378
Ville Syrjalad329e332009-01-29 23:42:16 -0800379 if (!((1 << mode) & ar2->mode_mask))
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200380 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. */
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400399 if (ar2->keycode[mode][index] == BTN_LEFT ||
400 ar2->keycode[mode][index] == BTN_RIGHT)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200401 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
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400415 input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200416 input_sync(idev);
417}
418
David Howells7d12e782006-10-05 14:55:46 +0100419static void ati_remote2_complete_mouse(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200420{
421 struct ati_remote2 *ar2 = urb->context;
422 int r;
423
424 switch (urb->status) {
425 case 0:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400426 usb_mark_last_busy(ar2->udev);
David Howells7d12e782006-10-05 14:55:46 +0100427 ati_remote2_input_mouse(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200428 break;
429 case -ENOENT:
430 case -EILSEQ:
431 case -ECONNRESET:
432 case -ESHUTDOWN:
433 dev_dbg(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400434 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200435 return;
436 default:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400437 usb_mark_last_busy(ar2->udev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200438 dev_err(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400439 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200440 }
441
442 r = usb_submit_urb(urb, GFP_ATOMIC);
443 if (r)
444 dev_err(&ar2->intf[0]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400445 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200446}
447
David Howells7d12e782006-10-05 14:55:46 +0100448static void ati_remote2_complete_key(struct urb *urb)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200449{
450 struct ati_remote2 *ar2 = urb->context;
451 int r;
452
453 switch (urb->status) {
454 case 0:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400455 usb_mark_last_busy(ar2->udev);
David Howells7d12e782006-10-05 14:55:46 +0100456 ati_remote2_input_key(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200457 break;
458 case -ENOENT:
459 case -EILSEQ:
460 case -ECONNRESET:
461 case -ESHUTDOWN:
462 dev_dbg(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400463 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200464 return;
465 default:
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400466 usb_mark_last_busy(ar2->udev);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200467 dev_err(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400468 "%s(): urb status = %d\n", __func__, urb->status);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200469 }
470
471 r = usb_submit_urb(urb, GFP_ATOMIC);
472 if (r)
473 dev_err(&ar2->intf[1]->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400474 "%s(): usb_submit_urb() = %d\n", __func__, r);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200475}
476
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400477static int ati_remote2_getkeycode(struct input_dev *idev,
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800478 unsigned int scancode, unsigned int *keycode)
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400479{
480 struct ati_remote2 *ar2 = input_get_drvdata(idev);
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800481 unsigned int mode;
482 int index;
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400483
484 mode = scancode >> 8;
Ville Syrjalad329e332009-01-29 23:42:16 -0800485 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400486 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
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800496static int ati_remote2_setkeycode(struct input_dev *idev,
497 unsigned int scancode, unsigned int keycode)
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400498{
499 struct ati_remote2 *ar2 = input_get_drvdata(idev);
Dmitry Torokhov58b93992010-03-08 22:37:10 -0800500 unsigned int mode, old_keycode;
501 int index;
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400502
503 mode = scancode >> 8;
Ville Syrjalad329e332009-01-29 23:42:16 -0800504 if (mode > ATI_REMOTE2_PC || !((1 << mode) & ar2->mode_mask))
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400505 return -EINVAL;
506
507 index = ati_remote2_lookup(scancode & 0xFF);
508 if (index < 0)
509 return -EINVAL;
510
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400511 old_keycode = ar2->keycode[mode][index];
512 ar2->keycode[mode][index] = keycode;
Ville Syrjala225c9882009-05-18 16:01:25 -0700513 __set_bit(keycode, idev->keybit);
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400514
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
Ville Syrjala225c9882009-05-18 16:01:25 -0700522 __clear_bit(old_keycode, idev->keybit);
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400523
524 return 0;
525}
526
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200527static int ati_remote2_input_init(struct ati_remote2 *ar2)
528{
529 struct input_dev *idev;
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400530 int index, mode, retval;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200531
532 idev = input_allocate_device();
533 if (!idev)
534 return -ENOMEM;
535
536 ar2->idev = idev;
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400537 input_set_drvdata(idev, ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200538
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700539 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);
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400543
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;
Ville Syrjala225c9882009-05-18 16:01:25 -0700547 __set_bit(ar2->keycode[mode][index], idev->keybit);
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400548 }
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;
Ville Syrjala225c9882009-05-18 16:01:25 -0700558 __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);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200563
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
Ville Syrjala1971b9d2008-07-03 10:45:37 -0400570 idev->getkeycode = ati_remote2_getkeycode;
571 idev->setkeycode = ati_remote2_setkeycode;
572
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200573 idev->name = ar2->name;
574 idev->phys = ar2->phys;
575
576 usb_to_input_id(ar2->udev, &idev->id);
Dmitry Torokhovc0f82d52007-04-12 01:35:03 -0400577 idev->dev.parent = &ar2->udev->dev;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200578
Dmitry Torokhov50141862007-04-12 01:33:39 -0400579 retval = input_register_device(idev);
580 if (retval)
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200581 input_free_device(idev);
582
Dmitry Torokhov50141862007-04-12 01:33:39 -0400583 return retval;
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200584}
585
586static 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++) {
Daniel Mack997ea582010-04-12 13:17:25 +0200592 ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200593 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
614static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
615{
616 int i;
617
618 for (i = 0; i < 2; i++) {
Mariusz Kozlowski23815262006-11-08 15:35:50 +0100619 usb_free_urb(ar2->urb[i]);
Daniel Mack997ea582010-04-12 13:17:25 +0200620 usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200621 }
622}
623
Ville Syrjalad329e332009-01-29 23:42:16 -0800624static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask)
Peter Stokesa1421d32007-04-12 01:33:10 -0400625{
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++) {
Ville Syrjalad329e332009-01-29 23:42:16 -0800639 if ((1 << i) & ch_mask) {
640 if (!(~(1 << i) & ch_mask))
Peter Stokesa1421d32007-04-12 01:33:10 -0400641 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",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400652 __func__, r);
Peter Stokesa1421d32007-04-12 01:33:10 -0400653 return r;
654 }
655
656 return 0;
657}
658
Ville Syrjalad329e332009-01-29 23:42:16 -0800659static 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
670static 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
705static 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
716static 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
736static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask,
737 ati_remote2_store_channel_mask);
738
739static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask,
740 ati_remote2_store_mode_mask);
741
742static struct attribute *ati_remote2_attrs[] = {
743 &dev_attr_channel_mask.attr,
744 &dev_attr_mode_mask.attr,
745 NULL,
746};
747
748static struct attribute_group ati_remote2_attr_group = {
749 .attrs = ati_remote2_attrs,
750};
751
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200752static 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
Ville Syrjala8a49cfa2009-01-29 23:42:16 -0800782 ar2->channel_mask = channel_mask;
783 ar2->mode_mask = mode_mask;
Ville Syrjalad329e332009-01-29 23:42:16 -0800784
785 r = ati_remote2_setup(ar2, ar2->channel_mask);
Peter Stokesa1421d32007-04-12 01:33:10 -0400786 if (r)
787 goto fail2;
788
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200789 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
Ville Syrjalad329e332009-01-29 23:42:16 -0800794 r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200795 if (r)
796 goto fail2;
797
Ville Syrjalad329e332009-01-29 23:42:16 -0800798 r = ati_remote2_input_init(ar2);
799 if (r)
800 goto fail3;
801
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200802 usb_set_intfdata(interface, ar2);
803
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400804 interface->needs_remote_wakeup = 1;
805
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200806 return 0;
807
Ville Syrjalad329e332009-01-29 23:42:16 -0800808 fail3:
809 sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200810 fail2:
811 ati_remote2_urb_cleanup(ar2);
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200812 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
813 fail1:
814 kfree(ar2);
815
816 return r;
817}
818
819static 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
Ville Syrjalad329e332009-01-29 23:42:16 -0800832 sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group);
833
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200834 ati_remote2_urb_cleanup(ar2);
835
836 usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
837
838 kfree(ar2);
839}
840
Ville Syrjalad6505ab2008-07-03 10:45:37 -0400841static 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
866static 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
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800892static 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
Ville Syrjalad329e332009-01-29 23:42:16 -0800907 r = ati_remote2_setup(ar2, ar2->channel_mask);
Ville Syrjala169bc1e2009-01-29 23:42:16 -0800908 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
923static 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
943static 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
Ville Syrjälä735b0cb2005-12-10 20:30:54 +0200964static 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
977static void __exit ati_remote2_exit(void)
978{
979 usb_deregister(&ati_remote2_driver);
980}
981
982module_init(ati_remote2_init);
983module_exit(ati_remote2_exit);