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