Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dledford/rdma
[linux-2.6-block.git] / drivers / media / rc / ati_remote.c
CommitLineData
05f091ab 1/*
1da177e4
LT
2 * USB ATI Remote support
3 *
9d454d48 4 * Copyright (c) 2011, 2012 Anssi Hannula <anssi.hannula@iki.fi>
1da177e4
LT
5 * Version 2.2.0 Copyright (c) 2004 Torrey Hoffman <thoffman@arnor.net>
6 * Version 2.1.1 Copyright (c) 2002 Vladimir Dergachev
7 *
8 * This 2.2.0 version is a rewrite / cleanup of the 2.1.1 driver, including
05f091ab 9 * porting to the 2.6 kernel interfaces, along with other modification
1da177e4
LT
10 * to better match the style of the existing usb/input drivers. However, the
11 * protocol and hardware handling is essentially unchanged from 2.1.1.
05f091ab
DT
12 *
13 * The 2.1.1 driver was derived from the usbati_remote and usbkbd drivers by
1da177e4
LT
14 * Vojtech Pavlik.
15 *
16 * Changes:
17 *
18 * Feb 2004: Torrey Hoffman <thoffman@arnor.net>
19 * Version 2.2.0
20 * Jun 2004: Torrey Hoffman <thoffman@arnor.net>
21 * Version 2.2.1
22 * Added key repeat support contributed by:
23 * Vincent Vanackere <vanackere@lif.univ-mrs.fr>
24 * Added support for the "Lola" remote contributed by:
25 * Seth Cohn <sethcohn@yahoo.com>
26 *
05f091ab 27 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1da177e4
LT
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
05f091ab 31 * the Free Software Foundation; either version 2 of the License, or
1da177e4 32 * (at your option) any later version.
05f091ab 33 *
1da177e4
LT
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
05f091ab 38 *
1da177e4
LT
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
05f091ab
DT
42 *
43 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1da177e4
LT
44 *
45 * Hardware & software notes
46 *
05f091ab
DT
47 * These remote controls are distributed by ATI as part of their
48 * "All-In-Wonder" video card packages. The receiver self-identifies as a
1da177e4
LT
49 * "USB Receiver" with manufacturer "X10 Wireless Technology Inc".
50 *
05f091ab 51 * The "Lola" remote is available from X10. See:
1da177e4
LT
52 * http://www.x10.com/products/lola_sg1.htm
53 * The Lola is similar to the ATI remote but has no mouse support, and slightly
54 * different keys.
55 *
05f091ab 56 * It is possible to use multiple receivers and remotes on multiple computers
1da177e4 57 * simultaneously by configuring them to use specific channels.
05f091ab
DT
58 *
59 * The RF protocol used by the remote supports 16 distinct channels, 1 to 16.
60 * Actually, it may even support more, at least in some revisions of the
1da177e4
LT
61 * hardware.
62 *
63 * Each remote can be configured to transmit on one channel as follows:
05f091ab
DT
64 * - Press and hold the "hand icon" button.
65 * - When the red LED starts to blink, let go of the "hand icon" button.
66 * - When it stops blinking, input the channel code as two digits, from 01
1da177e4 67 * to 16, and press the hand icon again.
05f091ab 68 *
1da177e4
LT
69 * The timing can be a little tricky. Try loading the module with debug=1
70 * to have the kernel print out messages about the remote control number
71 * and mask. Note: debugging prints remote numbers as zero-based hexadecimal.
72 *
73 * The driver has a "channel_mask" parameter. This bitmask specifies which
05f091ab 74 * channels will be ignored by the module. To mask out channels, just add
1da177e4
LT
75 * all the 2^channel_number values together.
76 *
77 * For instance, set channel_mask = 2^4 = 16 (binary 10000) to make ati_remote
05f091ab 78 * ignore signals coming from remote controls transmitting on channel 4, but
1da177e4
LT
79 * accept all other channels.
80 *
05f091ab 81 * Or, set channel_mask = 65533, (0xFFFD), and all channels except 1 will be
1da177e4
LT
82 * ignored.
83 *
05f091ab 84 * The default is 0 (respond to all channels). Bit 0 and bits 17-32 of this
1da177e4
LT
85 * parameter are unused.
86 *
87 */
88
1da177e4
LT
89#include <linux/kernel.h>
90#include <linux/errno.h>
91#include <linux/init.h>
92#include <linux/slab.h>
93#include <linux/module.h>
c34516e5 94#include <linux/mutex.h>
ae0dadcf 95#include <linux/usb/input.h>
1da177e4 96#include <linux/wait.h>
f0b80fbf 97#include <linux/jiffies.h>
c34516e5 98#include <media/rc-core.h>
1da177e4
LT
99
100/*
101 * Module and Version Information, Module Parameters
102 */
05f091ab 103
51320886
JW
104#define ATI_REMOTE_VENDOR_ID 0x0bc7
105#define LOLA_REMOTE_PRODUCT_ID 0x0002
106#define LOLA2_REMOTE_PRODUCT_ID 0x0003
107#define ATI_REMOTE_PRODUCT_ID 0x0004
108#define NVIDIA_REMOTE_PRODUCT_ID 0x0005
109#define MEDION_REMOTE_PRODUCT_ID 0x0006
999d6bc9 110#define FIREFLY_REMOTE_PRODUCT_ID 0x0008
1da177e4 111
9688efda 112#define DRIVER_VERSION "2.2.1"
1da177e4
LT
113#define DRIVER_AUTHOR "Torrey Hoffman <thoffman@arnor.net>"
114#define DRIVER_DESC "ATI/X10 RF USB Remote Control"
115
116#define NAME_BUFSIZE 80 /* size of product name, path buffers */
117#define DATA_BUFSIZE 63 /* size of URB data buffers */
1da177e4 118
c6698697
EH
119/*
120 * Duplicate event filtering time.
121 * Sequential, identical KIND_FILTERED inputs with less than
122 * FILTER_TIME milliseconds between them are considered as repeat
123 * events. The hardware generates 5 events for the first keypress
124 * and we have to take this into account for an accurate repeat
125 * behaviour.
126 */
127#define FILTER_TIME 60 /* msec */
0de95509 128#define REPEAT_DELAY 500 /* msec */
c6698697 129
65cde54b 130static unsigned long channel_mask;
c605b679 131module_param(channel_mask, ulong, 0644);
1da177e4
LT
132MODULE_PARM_DESC(channel_mask, "Bitmask of remote control channels to ignore");
133
65cde54b 134static int debug;
c605b679 135module_param(debug, int, 0644);
1da177e4
LT
136MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
137
c6698697
EH
138static int repeat_filter = FILTER_TIME;
139module_param(repeat_filter, int, 0644);
140MODULE_PARM_DESC(repeat_filter, "Repeat filter time, default = 60 msec");
141
0de95509
KP
142static int repeat_delay = REPEAT_DELAY;
143module_param(repeat_delay, int, 0644);
144MODULE_PARM_DESC(repeat_delay, "Delay before sending repeats, default = 500 msec");
145
c34516e5
AH
146static bool mouse = true;
147module_param(mouse, bool, 0444);
148MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes");
149
a342daea
DC
150#define dbginfo(dev, format, arg...) \
151 do { if (debug) dev_info(dev , format , ## arg); } while (0)
1da177e4
LT
152#undef err
153#define err(format, arg...) printk(KERN_ERR format , ## arg)
05f091ab 154
0d74679c
AH
155struct ati_receiver_type {
156 /* either default_keymap or get_default_keymap should be set */
157 const char *default_keymap;
158 const char *(*get_default_keymap)(struct usb_interface *interface);
159};
160
9d454d48
AH
161static const char *get_medion_keymap(struct usb_interface *interface)
162{
163 struct usb_device *udev = interface_to_usbdev(interface);
164
5085c99e
AH
165 /*
166 * There are many different Medion remotes shipped with a receiver
167 * with the same usb id, but the receivers have subtle differences
168 * in the USB descriptors allowing us to detect them.
169 */
170
171 if (udev->manufacturer && udev->product) {
172 if (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP) {
173
174 if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc")
175 && !strcmp(udev->product, "USB Receiver"))
176 return RC_MAP_MEDION_X10_DIGITAINER;
177
178 if (!strcmp(udev->manufacturer, "X10 WTI")
179 && !strcmp(udev->product, "RF receiver"))
180 return RC_MAP_MEDION_X10_OR2X;
181 } else {
182
183 if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc")
184 && !strcmp(udev->product, "USB Receiver"))
185 return RC_MAP_MEDION_X10;
186 }
187 }
188
189 dev_info(&interface->dev,
190 "Unknown Medion X10 receiver, using default ati_remote Medion keymap\n");
9d454d48
AH
191
192 return RC_MAP_MEDION_X10;
193}
194
a342daea
DC
195static const struct ati_receiver_type type_ati = {
196 .default_keymap = RC_MAP_ATI_X10
197};
198static const struct ati_receiver_type type_medion = {
199 .get_default_keymap = get_medion_keymap
200};
201static const struct ati_receiver_type type_firefly = {
202 .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY
203};
0d74679c 204
1da177e4 205static struct usb_device_id ati_remote_table[] = {
a342daea
DC
206 {
207 USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID),
208 .driver_info = (unsigned long)&type_ati
209 },
210 {
211 USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID),
212 .driver_info = (unsigned long)&type_ati
213 },
214 {
215 USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID),
216 .driver_info = (unsigned long)&type_ati
217 },
218 {
219 USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID),
220 .driver_info = (unsigned long)&type_ati
221 },
222 {
223 USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID),
224 .driver_info = (unsigned long)&type_medion
225 },
226 {
227 USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID),
228 .driver_info = (unsigned long)&type_firefly
229 },
1da177e4
LT
230 {} /* Terminating entry */
231};
232
233MODULE_DEVICE_TABLE(usb, ati_remote_table);
234
235/* Get hi and low bytes of a 16-bits int */
236#define HI(a) ((unsigned char)((a) >> 8))
237#define LO(a) ((unsigned char)((a) & 0xff))
238
239#define SEND_FLAG_IN_PROGRESS 1
240#define SEND_FLAG_COMPLETE 2
241
242/* Device initialization strings */
243static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
244static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
245
1da177e4 246struct ati_remote {
c5b7c7c3 247 struct input_dev *idev;
c34516e5 248 struct rc_dev *rdev;
1da177e4
LT
249 struct usb_device *udev;
250 struct usb_interface *interface;
05f091ab 251
1da177e4
LT
252 struct urb *irq_urb;
253 struct urb *out_urb;
254 struct usb_endpoint_descriptor *endpoint_in;
255 struct usb_endpoint_descriptor *endpoint_out;
256 unsigned char *inbuf;
257 unsigned char *outbuf;
258 dma_addr_t inbuf_dma;
259 dma_addr_t outbuf_dma;
260
5eefb4f0 261 unsigned char old_data; /* Detect duplicate events */
1da177e4
LT
262 unsigned long old_jiffies;
263 unsigned long acc_jiffies; /* handle acceleration */
0de95509
KP
264 unsigned long first_jiffies;
265
1da177e4 266 unsigned int repeat_count;
05f091ab 267
c34516e5
AH
268 char rc_name[NAME_BUFSIZE];
269 char rc_phys[NAME_BUFSIZE];
270 char mouse_name[NAME_BUFSIZE];
271 char mouse_phys[NAME_BUFSIZE];
1da177e4
LT
272
273 wait_queue_head_t wait;
274 int send_flags;
c34516e5
AH
275
276 int users; /* 0-2, users are rc and input */
277 struct mutex open_mutex;
1da177e4
LT
278};
279
280/* "Kinds" of messages sent from the hardware to the driver. */
281#define KIND_END 0
3f245b9c 282#define KIND_LITERAL 1 /* Simply pass to input system as EV_KEY */
1da177e4 283#define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
3f245b9c 284#define KIND_ACCEL 3 /* Translate to EV_REL mouse-move events */
1da177e4
LT
285
286/* Translation table from hardware messages to input events. */
4c4c9432 287static const struct {
528abb98 288 unsigned char kind;
3f245b9c
GS
289 unsigned char data; /* Raw key code from remote */
290 unsigned short code; /* Input layer translation */
c5b7c7c3 291} ati_remote_tbl[] = {
8ecd5e32 292 /* Directional control pad axes. Code is xxyy */
3f245b9c
GS
293 {KIND_ACCEL, 0x70, 0xff00}, /* left */
294 {KIND_ACCEL, 0x71, 0x0100}, /* right */
295 {KIND_ACCEL, 0x72, 0x00ff}, /* up */
296 {KIND_ACCEL, 0x73, 0x0001}, /* down */
8ecd5e32 297
05f091ab 298 /* Directional control pad diagonals */
3f245b9c
GS
299 {KIND_ACCEL, 0x74, 0xffff}, /* left up */
300 {KIND_ACCEL, 0x75, 0x01ff}, /* right up */
301 {KIND_ACCEL, 0x77, 0xff01}, /* left down */
302 {KIND_ACCEL, 0x76, 0x0101}, /* right down */
303
304 /* "Mouse button" buttons. The code below uses the fact that the
305 * lsbit of the raw code is a down/up indicator. */
306 {KIND_LITERAL, 0x78, BTN_LEFT}, /* left btn down */
307 {KIND_LITERAL, 0x79, BTN_LEFT}, /* left btn up */
308 {KIND_LITERAL, 0x7c, BTN_RIGHT},/* right btn down */
309 {KIND_LITERAL, 0x7d, BTN_RIGHT},/* right btn up */
1da177e4 310
05f091ab 311 /* Artificial "doubleclick" events are generated by the hardware.
1da177e4 312 * They are mapped to the "side" and "extra" mouse buttons here. */
3f245b9c
GS
313 {KIND_FILTERED, 0x7a, BTN_SIDE}, /* left dblclick */
314 {KIND_FILTERED, 0x7e, BTN_EXTRA},/* right dblclick */
1da177e4 315
c34516e5 316 /* Non-mouse events are handled by rc-core */
3f245b9c 317 {KIND_END, 0x00, 0}
1da177e4
LT
318};
319
1da177e4 320/*
a342daea 321 * ati_remote_dump_input
1da177e4 322 */
1817b169
GKH
323static void ati_remote_dump(struct device *dev, unsigned char *data,
324 unsigned int len)
1da177e4 325{
0224e040
AH
326 if (len == 1) {
327 if (data[0] != (unsigned char)0xff && data[0] != 0x00)
328 dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
329 } else if (len == 4)
7c94c69d 330 dev_warn(dev, "Weird key %*ph\n", 4, data);
1da177e4 331 else
7c94c69d 332 dev_warn(dev, "Weird data, len=%d %*ph ...\n", len, 6, data);
1da177e4
LT
333}
334
335/*
a342daea 336 * ati_remote_open
1da177e4 337 */
c34516e5 338static int ati_remote_open(struct ati_remote *ati_remote)
1da177e4 339{
c34516e5
AH
340 int err = 0;
341
342 mutex_lock(&ati_remote->open_mutex);
343
344 if (ati_remote->users++ != 0)
345 goto out; /* one was already active */
1da177e4
LT
346
347 /* On first open, submit the read urb which was set up previously. */
348 ati_remote->irq_urb->dev = ati_remote->udev;
349 if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
05f091ab 350 dev_err(&ati_remote->interface->dev,
ea3e6c59 351 "%s: usb_submit_urb failed!\n", __func__);
c34516e5 352 err = -EIO;
1da177e4
LT
353 }
354
c34516e5
AH
355out: mutex_unlock(&ati_remote->open_mutex);
356 return err;
1da177e4
LT
357}
358
359/*
a342daea 360 * ati_remote_close
1da177e4 361 */
c34516e5
AH
362static void ati_remote_close(struct ati_remote *ati_remote)
363{
364 mutex_lock(&ati_remote->open_mutex);
365 if (--ati_remote->users == 0)
366 usb_kill_urb(ati_remote->irq_urb);
367 mutex_unlock(&ati_remote->open_mutex);
368}
369
370static int ati_remote_input_open(struct input_dev *inputdev)
1da177e4 371{
7791bdae 372 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
c34516e5
AH
373 return ati_remote_open(ati_remote);
374}
05f091ab 375
c34516e5
AH
376static void ati_remote_input_close(struct input_dev *inputdev)
377{
378 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
379 ati_remote_close(ati_remote);
380}
381
382static int ati_remote_rc_open(struct rc_dev *rdev)
383{
384 struct ati_remote *ati_remote = rdev->priv;
385 return ati_remote_open(ati_remote);
386}
387
388static void ati_remote_rc_close(struct rc_dev *rdev)
389{
390 struct ati_remote *ati_remote = rdev->priv;
391 ati_remote_close(ati_remote);
1da177e4
LT
392}
393
394/*
a342daea 395 * ati_remote_irq_out
1da177e4 396 */
7d12e780 397static void ati_remote_irq_out(struct urb *urb)
1da177e4
LT
398{
399 struct ati_remote *ati_remote = urb->context;
05f091ab 400
1da177e4
LT
401 if (urb->status) {
402 dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
ea3e6c59 403 __func__, urb->status);
1da177e4
LT
404 return;
405 }
05f091ab 406
1da177e4
LT
407 ati_remote->send_flags |= SEND_FLAG_COMPLETE;
408 wmb();
409 wake_up(&ati_remote->wait);
410}
411
412/*
a342daea 413 * ati_remote_sendpacket
05f091ab 414 *
a342daea 415 * Used to send device initialization strings
1da177e4 416 */
a342daea
DC
417static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd,
418 unsigned char *data)
1da177e4
LT
419{
420 int retval = 0;
05f091ab 421
1da177e4
LT
422 /* Set up out_urb */
423 memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
05f091ab 424 ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
1da177e4
LT
425
426 ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
427 ati_remote->out_urb->dev = ati_remote->udev;
428 ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
429
430 retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
431 if (retval) {
05f091ab 432 dev_dbg(&ati_remote->interface->dev,
1da177e4
LT
433 "sendpacket: usb_submit_urb failed: %d\n", retval);
434 return retval;
435 }
436
437 wait_event_timeout(ati_remote->wait,
438 ((ati_remote->out_urb->status != -EINPROGRESS) ||
05f091ab 439 (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
1da177e4
LT
440 HZ);
441 usb_kill_urb(ati_remote->out_urb);
05f091ab 442
1da177e4
LT
443 return retval;
444}
445
fe28f5de
MCC
446struct accel_times {
447 const char value;
448 unsigned int msecs;
449};
450
451static const struct accel_times accel[] = {
452 { 1, 125 },
453 { 2, 250 },
454 { 4, 500 },
455 { 6, 1000 },
456 { 9, 1500 },
457 { 13, 2000 },
458 { 20, 0 },
459};
460
2ffc1cca 461/*
a342daea 462 * ati_remote_compute_accel
2ffc1cca
DT
463 *
464 * Implements acceleration curve for directional control pad
465 * If elapsed time since last event is > 1/4 second, user "stopped",
466 * so reset acceleration. Otherwise, user is probably holding the control
467 * pad down, so we increase acceleration, ramping up over two seconds to
468 * a maximum speed.
469 */
470static int ati_remote_compute_accel(struct ati_remote *ati_remote)
471{
fe28f5de
MCC
472 unsigned long now = jiffies, reset_time;
473 int i;
474
475 reset_time = msecs_to_jiffies(250);
2ffc1cca 476
fe28f5de 477 if (time_after(now, ati_remote->old_jiffies + reset_time)) {
2ffc1cca 478 ati_remote->acc_jiffies = now;
fe28f5de 479 return 1;
2ffc1cca 480 }
fe28f5de
MCC
481 for (i = 0; i < ARRAY_SIZE(accel) - 1; i++) {
482 unsigned long timeout = msecs_to_jiffies(accel[i].msecs);
2ffc1cca 483
fe28f5de
MCC
484 if (time_before(now, ati_remote->acc_jiffies + timeout))
485 return accel[i].value;
486 }
487 return accel[i].value;
2ffc1cca
DT
488}
489
1da177e4 490/*
a342daea 491 * ati_remote_report_input
1da177e4 492 */
7d12e780 493static void ati_remote_input_report(struct urb *urb)
1da177e4
LT
494{
495 struct ati_remote *ati_remote = urb->context;
496 unsigned char *data= ati_remote->inbuf;
c5b7c7c3 497 struct input_dev *dev = ati_remote->idev;
c34516e5 498 int index = -1;
1da177e4 499 int remote_num;
5eefb4f0 500 unsigned char scancode;
9d454d48 501 u32 wheel_keycode = KEY_RESERVED;
5eefb4f0
AH
502 int i;
503
504 /*
505 * data[0] = 0x14
506 * data[1] = data[2] + data[3] + 0xd5 (a checksum byte)
507 * data[2] = the key code (with toggle bit in MSB with some models)
508 * data[3] = channel << 4 (the low 4 bits must be zero)
509 */
05f091ab 510
1da177e4 511 /* Deal with strange looking inputs */
20d7e3ca
GS
512 if ( urb->actual_length != 4 || data[0] != 0x14 ||
513 data[1] != (unsigned char)(data[2] + data[3] + 0xD5) ||
514 (data[3] & 0x0f) != 0x00) {
1817b169 515 ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
1da177e4
LT
516 return;
517 }
518
5eefb4f0
AH
519 if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) {
520 dbginfo(&ati_remote->interface->dev,
7c94c69d 521 "wrong checksum in input: %*ph\n", 4, data);
5eefb4f0
AH
522 return;
523 }
524
1da177e4
LT
525 /* Mask unwanted remote channels. */
526 /* note: remote_num is 0-based, channel 1 on remote == 0 here */
527 remote_num = (data[3] >> 4) & 0x0f;
9688efda 528 if (channel_mask & (1 << (remote_num + 1))) {
1da177e4 529 dbginfo(&ati_remote->interface->dev,
20d7e3ca 530 "Masked input from channel 0x%02x: data %02x, "
a342daea 531 "mask= 0x%02lx\n",
20d7e3ca 532 remote_num, data[2], channel_mask);
1da177e4
LT
533 return;
534 }
535
999d6bc9 536 /*
5eefb4f0
AH
537 * MSB is a toggle code, though only used by some devices
538 * (e.g. SnapStream Firefly)
999d6bc9 539 */
5eefb4f0 540 scancode = data[2] & 0x7f;
c34516e5 541
9d454d48
AH
542 dbginfo(&ati_remote->interface->dev,
543 "channel 0x%02x; key data %02x, scancode %02x\n",
544 remote_num, data[2], scancode);
545
546 if (scancode >= 0x70) {
547 /*
548 * This is either a mouse or scrollwheel event, depending on
549 * the remote/keymap.
550 * Get the keycode assigned to scancode 0x78/0x70. If it is
551 * set, assume this is a scrollwheel up/down event.
552 */
553 wheel_keycode = rc_g_keycode_from_table(ati_remote->rdev,
554 scancode & 0x78);
555
556 if (wheel_keycode == KEY_RESERVED) {
557 /* scrollwheel was not mapped, assume mouse */
558
a342daea
DC
559 /* Look up event code index in the mouse translation
560 * table.
561 */
9d454d48
AH
562 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
563 if (scancode == ati_remote_tbl[i].data) {
564 index = i;
565 break;
566 }
567 }
5eefb4f0
AH
568 }
569 }
c34516e5 570
c34516e5 571 if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
3f245b9c
GS
572 /*
573 * The lsbit of the raw key code is a down/up flag.
574 * Invert it to match the input layer's conventions.
575 */
576 input_event(dev, EV_KEY, ati_remote_tbl[index].code,
577 !(data[2] & 1));
05f091ab 578
1da177e4 579 ati_remote->old_jiffies = jiffies;
05f091ab 580
8ecd5e32 581 } else if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
0de95509
KP
582 unsigned long now = jiffies;
583
1da177e4 584 /* Filter duplicate events which happen "too close" together. */
5eefb4f0 585 if (ati_remote->old_data == data[2] &&
0de95509
KP
586 time_before(now, ati_remote->old_jiffies +
587 msecs_to_jiffies(repeat_filter))) {
1da177e4 588 ati_remote->repeat_count++;
05f091ab 589 } else {
1da177e4 590 ati_remote->repeat_count = 0;
0de95509 591 ati_remote->first_jiffies = now;
1da177e4 592 }
05f091ab 593
0de95509 594 ati_remote->old_jiffies = now;
1da177e4 595
3f245b9c
GS
596 /* Ensure we skip at least the 4 first duplicate events
597 * (generated by a single keypress), and continue skipping
598 * until repeat_delay msecs have passed.
0de95509 599 */
c6698697 600 if (ati_remote->repeat_count > 0 &&
0de95509
KP
601 (ati_remote->repeat_count < 5 ||
602 time_before(now, ati_remote->first_jiffies +
603 msecs_to_jiffies(repeat_delay))))
1da177e4 604 return;
05f091ab 605
89b0c0d2
GS
606 if (index >= 0) {
607 input_event(dev, EV_KEY, ati_remote_tbl[index].code, 1);
608 input_event(dev, EV_KEY, ati_remote_tbl[index].code, 0);
609 } else {
c34516e5 610 /* Not a mouse event, hand it to rc-core. */
9d454d48
AH
611 int count = 1;
612
613 if (wheel_keycode != KEY_RESERVED) {
614 /*
615 * This is a scrollwheel event, send the
616 * scroll up (0x78) / down (0x70) scancode
617 * repeatedly as many times as indicated by
618 * rest of the scancode.
619 */
620 count = (scancode & 0x07) + 1;
621 scancode &= 0x78;
622 }
623
624 while (count--) {
625 /*
626 * We don't use the rc-core repeat handling yet as
627 * it would cause ghost repeats which would be a
628 * regression for this driver.
629 */
120703f9
DH
630 rc_keydown_notimeout(ati_remote->rdev, RC_TYPE_OTHER,
631 scancode, data[2]);
9d454d48
AH
632 rc_keyup(ati_remote->rdev);
633 }
89b0c0d2 634 goto nosync;
c34516e5 635 }
1da177e4 636
8ecd5e32
GS
637 } else if (ati_remote_tbl[index].kind == KIND_ACCEL) {
638 signed char dx = ati_remote_tbl[index].code >> 8;
639 signed char dy = ati_remote_tbl[index].code & 255;
05f091ab 640
2ffc1cca 641 /*
a342daea
DC
642 * Other event kinds are from the directional control pad, and
643 * have an acceleration factor applied to them. Without this
644 * acceleration, the control pad is mostly unusable.
2ffc1cca 645 */
8ecd5e32
GS
646 int acc = ati_remote_compute_accel(ati_remote);
647 if (dx)
648 input_report_rel(dev, REL_X, dx * acc);
649 if (dy)
650 input_report_rel(dev, REL_Y, dy * acc);
2ffc1cca 651 ati_remote->old_jiffies = jiffies;
89b0c0d2 652
8ecd5e32
GS
653 } else {
654 dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
655 ati_remote_tbl[index].kind);
89b0c0d2 656 return;
2ffc1cca 657 }
89b0c0d2
GS
658 input_sync(dev);
659nosync:
660 ati_remote->old_data = data[2];
1da177e4
LT
661}
662
663/*
a342daea 664 * ati_remote_irq_in
1da177e4 665 */
7d12e780 666static void ati_remote_irq_in(struct urb *urb)
1da177e4
LT
667{
668 struct ati_remote *ati_remote = urb->context;
669 int retval;
670
671 switch (urb->status) {
672 case 0: /* success */
7d12e780 673 ati_remote_input_report(urb);
1da177e4
LT
674 break;
675 case -ECONNRESET: /* unlink */
676 case -ENOENT:
677 case -ESHUTDOWN:
a342daea
DC
678 dev_dbg(&ati_remote->interface->dev,
679 "%s: urb error status, unlink?\n",
ea3e6c59 680 __func__);
05f091ab 681 return;
1da177e4 682 default: /* error */
a342daea
DC
683 dev_dbg(&ati_remote->interface->dev,
684 "%s: Nonzero urb status %d\n",
ea3e6c59 685 __func__, urb->status);
1da177e4 686 }
05f091ab 687
54e6ecb2 688 retval = usb_submit_urb(urb, GFP_ATOMIC);
1da177e4 689 if (retval)
a342daea
DC
690 dev_err(&ati_remote->interface->dev,
691 "%s: usb_submit_urb()=%d\n",
ea3e6c59 692 __func__, retval);
1da177e4
LT
693}
694
695/*
a342daea 696 * ati_remote_alloc_buffers
1da177e4 697 */
c5b7c7c3
DT
698static int ati_remote_alloc_buffers(struct usb_device *udev,
699 struct ati_remote *ati_remote)
1da177e4 700{
997ea58e
DM
701 ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
702 &ati_remote->inbuf_dma);
c5b7c7c3
DT
703 if (!ati_remote->inbuf)
704 return -1;
1da177e4 705
997ea58e 706 ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
9688efda 707 &ati_remote->outbuf_dma);
c5b7c7c3
DT
708 if (!ati_remote->outbuf)
709 return -1;
1da177e4 710
c5b7c7c3
DT
711 ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
712 if (!ati_remote->irq_urb)
713 return -1;
1da177e4 714
c5b7c7c3
DT
715 ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
716 if (!ati_remote->out_urb)
717 return -1;
05f091ab 718
c5b7c7c3
DT
719 return 0;
720}
05f091ab 721
c5b7c7c3 722/*
a342daea 723 * ati_remote_free_buffers
c5b7c7c3
DT
724 */
725static void ati_remote_free_buffers(struct ati_remote *ati_remote)
726{
459f836a
MK
727 usb_free_urb(ati_remote->irq_urb);
728 usb_free_urb(ati_remote->out_urb);
1da177e4 729
997ea58e 730 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
9dce447a 731 ati_remote->inbuf, ati_remote->inbuf_dma);
c5b7c7c3 732
997ea58e 733 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
9dce447a 734 ati_remote->outbuf, ati_remote->outbuf_dma);
1da177e4
LT
735}
736
737static void ati_remote_input_init(struct ati_remote *ati_remote)
738{
c5b7c7c3 739 struct input_dev *idev = ati_remote->idev;
1da177e4
LT
740 int i;
741
7b19ada2
JS
742 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
743 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
744 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
745 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1da177e4 746 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
3f245b9c
GS
747 if (ati_remote_tbl[i].kind == KIND_LITERAL ||
748 ati_remote_tbl[i].kind == KIND_FILTERED)
1e182708 749 __set_bit(ati_remote_tbl[i].code, idev->keybit);
05f091ab 750
7791bdae
DT
751 input_set_drvdata(idev, ati_remote);
752
c34516e5
AH
753 idev->open = ati_remote_input_open;
754 idev->close = ati_remote_input_close;
05f091ab 755
c34516e5
AH
756 idev->name = ati_remote->mouse_name;
757 idev->phys = ati_remote->mouse_phys;
05f091ab 758
16a334c0 759 usb_to_input_id(ati_remote->udev, &idev->id);
44fd0b60 760 idev->dev.parent = &ati_remote->interface->dev;
1da177e4
LT
761}
762
c34516e5
AH
763static void ati_remote_rc_init(struct ati_remote *ati_remote)
764{
765 struct rc_dev *rdev = ati_remote->rdev;
766
767 rdev->priv = ati_remote;
768 rdev->driver_type = RC_DRIVER_SCANCODE;
c5540fbb 769 rdev->allowed_protocols = RC_BIT_OTHER;
c34516e5
AH
770 rdev->driver_name = "ati_remote";
771
772 rdev->open = ati_remote_rc_open;
773 rdev->close = ati_remote_rc_close;
774
775 rdev->input_name = ati_remote->rc_name;
776 rdev->input_phys = ati_remote->rc_phys;
777
778 usb_to_input_id(ati_remote->udev, &rdev->input_id);
44fd0b60 779 rdev->dev.parent = &ati_remote->interface->dev;
c34516e5
AH
780}
781
1da177e4
LT
782static int ati_remote_initialize(struct ati_remote *ati_remote)
783{
784 struct usb_device *udev = ati_remote->udev;
785 int pipe, maxp;
05f091ab 786
1da177e4
LT
787 init_waitqueue_head(&ati_remote->wait);
788
789 /* Set up irq_urb */
790 pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
791 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
792 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
05f091ab
DT
793
794 usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
795 maxp, ati_remote_irq_in, ati_remote,
1da177e4
LT
796 ati_remote->endpoint_in->bInterval);
797 ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
798 ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
05f091ab 799
1da177e4
LT
800 /* Set up out_urb */
801 pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
802 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
803 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
804
05f091ab
DT
805 usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
806 maxp, ati_remote_irq_out, ati_remote,
1da177e4
LT
807 ati_remote->endpoint_out->bInterval);
808 ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
809 ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
810
811 /* send initialization strings */
812 if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
813 (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
05f091ab 814 dev_err(&ati_remote->interface->dev,
1da177e4 815 "Initializing ati_remote hardware failed.\n");
c5b7c7c3 816 return -EIO;
1da177e4 817 }
05f091ab 818
1da177e4
LT
819 return 0;
820}
821
822/*
a342daea 823 * ati_remote_probe
1da177e4 824 */
a342daea
DC
825static int ati_remote_probe(struct usb_interface *interface,
826 const struct usb_device_id *id)
1da177e4
LT
827{
828 struct usb_device *udev = interface_to_usbdev(interface);
c5b7c7c3
DT
829 struct usb_host_interface *iface_host = interface->cur_altsetting;
830 struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
0d74679c 831 struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info;
c5b7c7c3
DT
832 struct ati_remote *ati_remote;
833 struct input_dev *input_dev;
c34516e5 834 struct rc_dev *rc_dev;
c5b7c7c3 835 int err = -ENOMEM;
1da177e4 836
1da177e4 837 if (iface_host->desc.bNumEndpoints != 2) {
ea3e6c59 838 err("%s: Unexpected desc.bNumEndpoints\n", __func__);
c5b7c7c3 839 return -ENODEV;
1da177e4
LT
840 }
841
c5b7c7c3
DT
842 endpoint_in = &iface_host->endpoint[0].desc;
843 endpoint_out = &iface_host->endpoint[1].desc;
1da177e4 844
96642a2c 845 if (!usb_endpoint_is_int_in(endpoint_in)) {
ea3e6c59 846 err("%s: Unexpected endpoint_in\n", __func__);
c5b7c7c3 847 return -ENODEV;
1da177e4 848 }
c5b7c7c3 849 if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
ea3e6c59 850 err("%s: endpoint_in message size==0? \n", __func__);
c5b7c7c3 851 return -ENODEV;
1da177e4
LT
852 }
853
c5b7c7c3 854 ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
c34516e5
AH
855 rc_dev = rc_allocate_device();
856 if (!ati_remote || !rc_dev)
70ef6991 857 goto exit_free_dev_rdev;
1da177e4 858
c5b7c7c3
DT
859 /* Allocate URB buffers, URBs */
860 if (ati_remote_alloc_buffers(udev, ati_remote))
70ef6991 861 goto exit_free_buffers;
1da177e4 862
c5b7c7c3
DT
863 ati_remote->endpoint_in = endpoint_in;
864 ati_remote->endpoint_out = endpoint_out;
865 ati_remote->udev = udev;
c34516e5 866 ati_remote->rdev = rc_dev;
c5b7c7c3 867 ati_remote->interface = interface;
1da177e4 868
c34516e5
AH
869 usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
870 strlcpy(ati_remote->mouse_phys, ati_remote->rc_phys,
871 sizeof(ati_remote->mouse_phys));
872
873 strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
874 strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
1da177e4 875
1da177e4 876 if (udev->manufacturer)
c34516e5
AH
877 strlcpy(ati_remote->rc_name, udev->manufacturer,
878 sizeof(ati_remote->rc_name));
1da177e4
LT
879
880 if (udev->product)
c34516e5
AH
881 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
882 "%s %s", ati_remote->rc_name, udev->product);
1da177e4 883
c34516e5
AH
884 if (!strlen(ati_remote->rc_name))
885 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
c5b7c7c3 886 DRIVER_DESC "(%04x,%04x)",
05f091ab 887 le16_to_cpu(ati_remote->udev->descriptor.idVendor),
1da177e4
LT
888 le16_to_cpu(ati_remote->udev->descriptor.idProduct));
889
c34516e5
AH
890 snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
891 "%s mouse", ati_remote->rc_name);
892
0d74679c
AH
893 rc_dev->map_name = RC_MAP_ATI_X10; /* default map */
894
895 /* set default keymap according to receiver model */
896 if (type) {
897 if (type->default_keymap)
898 rc_dev->map_name = type->default_keymap;
899 else if (type->get_default_keymap)
900 rc_dev->map_name = type->get_default_keymap(interface);
901 }
175fcecf 902
c34516e5
AH
903 ati_remote_rc_init(ati_remote);
904 mutex_init(&ati_remote->open_mutex);
c5b7c7c3 905
1da177e4 906 /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
c5b7c7c3
DT
907 err = ati_remote_initialize(ati_remote);
908 if (err)
70ef6991 909 goto exit_kill_urbs;
1da177e4 910
c34516e5
AH
911 /* Set up and register rc device */
912 err = rc_register_device(ati_remote->rdev);
5014186d 913 if (err)
70ef6991 914 goto exit_kill_urbs;
1da177e4 915
c34516e5
AH
916 /* use our delay for rc_dev */
917 ati_remote->rdev->input_dev->rep[REP_DELAY] = repeat_delay;
918
919 /* Set up and register mouse input device */
920 if (mouse) {
921 input_dev = input_allocate_device();
a84b17e2
PST
922 if (!input_dev) {
923 err = -ENOMEM;
70ef6991 924 goto exit_unregister_device;
a84b17e2 925 }
c34516e5
AH
926
927 ati_remote->idev = input_dev;
928 ati_remote_input_init(ati_remote);
929 err = input_register_device(input_dev);
930
931 if (err)
70ef6991 932 goto exit_free_input_device;
c34516e5
AH
933 }
934
1da177e4 935 usb_set_intfdata(interface, ati_remote);
c5b7c7c3 936 return 0;
05f091ab 937
70ef6991
MK
938 exit_free_input_device:
939 input_free_device(input_dev);
940 exit_unregister_device:
941 rc_unregister_device(rc_dev);
c34516e5 942 rc_dev = NULL;
70ef6991
MK
943 exit_kill_urbs:
944 usb_kill_urb(ati_remote->irq_urb);
c5b7c7c3 945 usb_kill_urb(ati_remote->out_urb);
70ef6991
MK
946 exit_free_buffers:
947 ati_remote_free_buffers(ati_remote);
948 exit_free_dev_rdev:
949 rc_free_device(rc_dev);
c5b7c7c3
DT
950 kfree(ati_remote);
951 return err;
1da177e4
LT
952}
953
954/*
a342daea 955 * ati_remote_disconnect
1da177e4
LT
956 */
957static void ati_remote_disconnect(struct usb_interface *interface)
958{
959 struct ati_remote *ati_remote;
960
1da177e4
LT
961 ati_remote = usb_get_intfdata(interface);
962 usb_set_intfdata(interface, NULL);
963 if (!ati_remote) {
1817b169 964 dev_warn(&interface->dev, "%s - null device?\n", __func__);
1da177e4
LT
965 return;
966 }
05f091ab 967
c5b7c7c3
DT
968 usb_kill_urb(ati_remote->irq_urb);
969 usb_kill_urb(ati_remote->out_urb);
c34516e5
AH
970 if (ati_remote->idev)
971 input_unregister_device(ati_remote->idev);
972 rc_unregister_device(ati_remote->rdev);
c5b7c7c3
DT
973 ati_remote_free_buffers(ati_remote);
974 kfree(ati_remote);
1da177e4
LT
975}
976
a342daea
DC
977/* usb specific object to register with the usb subsystem */
978static struct usb_driver ati_remote_driver = {
979 .name = "ati_remote",
980 .probe = ati_remote_probe,
981 .disconnect = ati_remote_disconnect,
982 .id_table = ati_remote_table,
983};
984
ecb3b2b3 985module_usb_driver(ati_remote_driver);
1da177e4
LT
986
987MODULE_AUTHOR(DRIVER_AUTHOR);
988MODULE_DESCRIPTION(DRIVER_DESC);
989MODULE_LICENSE("GPL");