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