]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/rc/ati_remote.c
[media] drivers/media/rc/redrat3.c: fix error return code
[mirror_ubuntu-artful-kernel.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
282#define KIND_LITERAL 1 /* Simply pass to input system */
283#define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
284#define KIND_LU 3 /* Directional keypad diagonals - left up, */
285#define KIND_RU 4 /* right up, */
286#define KIND_LD 5 /* left down, */
287#define KIND_RD 6 /* right down */
288#define KIND_ACCEL 7 /* Directional keypad - left, right, up, down.*/
289
290/* Translation table from hardware messages to input events. */
4c4c9432 291static const struct {
1da177e4 292 short kind;
5eefb4f0 293 unsigned char data;
1da177e4
LT
294 int type;
295 unsigned int code;
296 int value;
c5b7c7c3 297} ati_remote_tbl[] = {
1da177e4 298 /* Directional control pad axes */
5eefb4f0
AH
299 {KIND_ACCEL, 0x70, EV_REL, REL_X, -1}, /* left */
300 {KIND_ACCEL, 0x71, EV_REL, REL_X, 1}, /* right */
301 {KIND_ACCEL, 0x72, EV_REL, REL_Y, -1}, /* up */
302 {KIND_ACCEL, 0x73, EV_REL, REL_Y, 1}, /* down */
05f091ab 303 /* Directional control pad diagonals */
5eefb4f0
AH
304 {KIND_LU, 0x74, EV_REL, 0, 0}, /* left up */
305 {KIND_RU, 0x75, EV_REL, 0, 0}, /* right up */
306 {KIND_LD, 0x77, EV_REL, 0, 0}, /* left down */
307 {KIND_RD, 0x76, EV_REL, 0, 0}, /* right down */
1da177e4
LT
308
309 /* "Mouse button" buttons */
5eefb4f0
AH
310 {KIND_LITERAL, 0x78, EV_KEY, BTN_LEFT, 1}, /* left btn down */
311 {KIND_LITERAL, 0x79, EV_KEY, BTN_LEFT, 0}, /* left btn up */
312 {KIND_LITERAL, 0x7c, EV_KEY, BTN_RIGHT, 1},/* right btn down */
313 {KIND_LITERAL, 0x7d, EV_KEY, BTN_RIGHT, 0},/* right btn up */
1da177e4 314
05f091ab 315 /* Artificial "doubleclick" events are generated by the hardware.
1da177e4 316 * They are mapped to the "side" and "extra" mouse buttons here. */
5eefb4f0
AH
317 {KIND_FILTERED, 0x7a, EV_KEY, BTN_SIDE, 1}, /* left dblclick */
318 {KIND_FILTERED, 0x7e, EV_KEY, BTN_EXTRA, 1},/* right dblclick */
1da177e4 319
c34516e5 320 /* Non-mouse events are handled by rc-core */
5eefb4f0 321 {KIND_END, 0x00, EV_MAX + 1, 0, 0}
1da177e4
LT
322};
323
1da177e4 324/*
a342daea 325 * ati_remote_dump_input
1da177e4 326 */
1817b169
GKH
327static void ati_remote_dump(struct device *dev, unsigned char *data,
328 unsigned int len)
1da177e4 329{
0224e040
AH
330 if (len == 1) {
331 if (data[0] != (unsigned char)0xff && data[0] != 0x00)
332 dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
333 } else if (len == 4)
7c94c69d 334 dev_warn(dev, "Weird key %*ph\n", 4, data);
1da177e4 335 else
7c94c69d 336 dev_warn(dev, "Weird data, len=%d %*ph ...\n", len, 6, data);
1da177e4
LT
337}
338
339/*
a342daea 340 * ati_remote_open
1da177e4 341 */
c34516e5 342static int ati_remote_open(struct ati_remote *ati_remote)
1da177e4 343{
c34516e5
AH
344 int err = 0;
345
346 mutex_lock(&ati_remote->open_mutex);
347
348 if (ati_remote->users++ != 0)
349 goto out; /* one was already active */
1da177e4
LT
350
351 /* On first open, submit the read urb which was set up previously. */
352 ati_remote->irq_urb->dev = ati_remote->udev;
353 if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
05f091ab 354 dev_err(&ati_remote->interface->dev,
ea3e6c59 355 "%s: usb_submit_urb failed!\n", __func__);
c34516e5 356 err = -EIO;
1da177e4
LT
357 }
358
c34516e5
AH
359out: mutex_unlock(&ati_remote->open_mutex);
360 return err;
1da177e4
LT
361}
362
363/*
a342daea 364 * ati_remote_close
1da177e4 365 */
c34516e5
AH
366static void ati_remote_close(struct ati_remote *ati_remote)
367{
368 mutex_lock(&ati_remote->open_mutex);
369 if (--ati_remote->users == 0)
370 usb_kill_urb(ati_remote->irq_urb);
371 mutex_unlock(&ati_remote->open_mutex);
372}
373
374static int ati_remote_input_open(struct input_dev *inputdev)
1da177e4 375{
7791bdae 376 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
c34516e5
AH
377 return ati_remote_open(ati_remote);
378}
05f091ab 379
c34516e5
AH
380static void ati_remote_input_close(struct input_dev *inputdev)
381{
382 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
383 ati_remote_close(ati_remote);
384}
385
386static int ati_remote_rc_open(struct rc_dev *rdev)
387{
388 struct ati_remote *ati_remote = rdev->priv;
389 return ati_remote_open(ati_remote);
390}
391
392static void ati_remote_rc_close(struct rc_dev *rdev)
393{
394 struct ati_remote *ati_remote = rdev->priv;
395 ati_remote_close(ati_remote);
1da177e4
LT
396}
397
398/*
a342daea 399 * ati_remote_irq_out
1da177e4 400 */
7d12e780 401static void ati_remote_irq_out(struct urb *urb)
1da177e4
LT
402{
403 struct ati_remote *ati_remote = urb->context;
05f091ab 404
1da177e4
LT
405 if (urb->status) {
406 dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
ea3e6c59 407 __func__, urb->status);
1da177e4
LT
408 return;
409 }
05f091ab 410
1da177e4
LT
411 ati_remote->send_flags |= SEND_FLAG_COMPLETE;
412 wmb();
413 wake_up(&ati_remote->wait);
414}
415
416/*
a342daea 417 * ati_remote_sendpacket
05f091ab 418 *
a342daea 419 * Used to send device initialization strings
1da177e4 420 */
a342daea
DC
421static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd,
422 unsigned char *data)
1da177e4
LT
423{
424 int retval = 0;
05f091ab 425
1da177e4
LT
426 /* Set up out_urb */
427 memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
05f091ab 428 ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
1da177e4
LT
429
430 ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
431 ati_remote->out_urb->dev = ati_remote->udev;
432 ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
433
434 retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
435 if (retval) {
05f091ab 436 dev_dbg(&ati_remote->interface->dev,
1da177e4
LT
437 "sendpacket: usb_submit_urb failed: %d\n", retval);
438 return retval;
439 }
440
441 wait_event_timeout(ati_remote->wait,
442 ((ati_remote->out_urb->status != -EINPROGRESS) ||
05f091ab 443 (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
1da177e4
LT
444 HZ);
445 usb_kill_urb(ati_remote->out_urb);
05f091ab 446
1da177e4
LT
447 return retval;
448}
449
2ffc1cca 450/*
a342daea 451 * ati_remote_compute_accel
2ffc1cca
DT
452 *
453 * Implements acceleration curve for directional control pad
454 * If elapsed time since last event is > 1/4 second, user "stopped",
455 * so reset acceleration. Otherwise, user is probably holding the control
456 * pad down, so we increase acceleration, ramping up over two seconds to
457 * a maximum speed.
458 */
459static int ati_remote_compute_accel(struct ati_remote *ati_remote)
460{
461 static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
462 unsigned long now = jiffies;
463 int acc;
464
465 if (time_after(now, ati_remote->old_jiffies + msecs_to_jiffies(250))) {
466 acc = 1;
467 ati_remote->acc_jiffies = now;
468 }
469 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(125)))
470 acc = accel[0];
471 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(250)))
472 acc = accel[1];
473 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(500)))
474 acc = accel[2];
475 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1000)))
476 acc = accel[3];
477 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1500)))
478 acc = accel[4];
479 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(2000)))
480 acc = accel[5];
481 else
482 acc = accel[6];
483
484 return acc;
485}
486
1da177e4 487/*
a342daea 488 * ati_remote_report_input
1da177e4 489 */
7d12e780 490static void ati_remote_input_report(struct urb *urb)
1da177e4
LT
491{
492 struct ati_remote *ati_remote = urb->context;
493 unsigned char *data= ati_remote->inbuf;
c5b7c7c3 494 struct input_dev *dev = ati_remote->idev;
c34516e5
AH
495 int index = -1;
496 int acc;
1da177e4 497 int remote_num;
5eefb4f0 498 unsigned char scancode;
9d454d48 499 u32 wheel_keycode = KEY_RESERVED;
5eefb4f0
AH
500 int i;
501
502 /*
503 * data[0] = 0x14
504 * data[1] = data[2] + data[3] + 0xd5 (a checksum byte)
505 * data[2] = the key code (with toggle bit in MSB with some models)
506 * data[3] = channel << 4 (the low 4 bits must be zero)
507 */
05f091ab 508
1da177e4 509 /* Deal with strange looking inputs */
05f091ab 510 if ( (urb->actual_length != 4) || (data[0] != 0x14) ||
1da177e4 511 ((data[3] & 0x0f) != 0x00) ) {
1817b169 512 ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
1da177e4
LT
513 return;
514 }
515
5eefb4f0
AH
516 if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) {
517 dbginfo(&ati_remote->interface->dev,
7c94c69d 518 "wrong checksum in input: %*ph\n", 4, data);
5eefb4f0
AH
519 return;
520 }
521
1da177e4
LT
522 /* Mask unwanted remote channels. */
523 /* note: remote_num is 0-based, channel 1 on remote == 0 here */
524 remote_num = (data[3] >> 4) & 0x0f;
9688efda 525 if (channel_mask & (1 << (remote_num + 1))) {
1da177e4 526 dbginfo(&ati_remote->interface->dev,
a342daea
DC
527 "Masked input from channel 0x%02x: data %02x,%02x, "
528 "mask= 0x%02lx\n",
1da177e4
LT
529 remote_num, data[1], data[2], channel_mask);
530 return;
531 }
532
999d6bc9 533 /*
5eefb4f0
AH
534 * MSB is a toggle code, though only used by some devices
535 * (e.g. SnapStream Firefly)
999d6bc9 536 */
5eefb4f0 537 scancode = data[2] & 0x7f;
c34516e5 538
9d454d48
AH
539 dbginfo(&ati_remote->interface->dev,
540 "channel 0x%02x; key data %02x, scancode %02x\n",
541 remote_num, data[2], scancode);
542
543 if (scancode >= 0x70) {
544 /*
545 * This is either a mouse or scrollwheel event, depending on
546 * the remote/keymap.
547 * Get the keycode assigned to scancode 0x78/0x70. If it is
548 * set, assume this is a scrollwheel up/down event.
549 */
550 wheel_keycode = rc_g_keycode_from_table(ati_remote->rdev,
551 scancode & 0x78);
552
553 if (wheel_keycode == KEY_RESERVED) {
554 /* scrollwheel was not mapped, assume mouse */
555
a342daea
DC
556 /* Look up event code index in the mouse translation
557 * table.
558 */
9d454d48
AH
559 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
560 if (scancode == ati_remote_tbl[i].data) {
561 index = i;
562 break;
563 }
564 }
5eefb4f0
AH
565 }
566 }
c34516e5 567
c34516e5 568 if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
1da177e4
LT
569 input_event(dev, ati_remote_tbl[index].type,
570 ati_remote_tbl[index].code,
571 ati_remote_tbl[index].value);
572 input_sync(dev);
05f091ab 573
1da177e4
LT
574 ati_remote->old_jiffies = jiffies;
575 return;
576 }
05f091ab 577
c34516e5 578 if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
0de95509
KP
579 unsigned long now = jiffies;
580
1da177e4 581 /* Filter duplicate events which happen "too close" together. */
5eefb4f0 582 if (ati_remote->old_data == data[2] &&
0de95509
KP
583 time_before(now, ati_remote->old_jiffies +
584 msecs_to_jiffies(repeat_filter))) {
1da177e4 585 ati_remote->repeat_count++;
05f091ab 586 } else {
1da177e4 587 ati_remote->repeat_count = 0;
0de95509 588 ati_remote->first_jiffies = now;
1da177e4 589 }
05f091ab 590
5eefb4f0 591 ati_remote->old_data = data[2];
0de95509 592 ati_remote->old_jiffies = now;
1da177e4 593
0de95509
KP
594 /* Ensure we skip at least the 4 first duplicate events (generated
595 * by a single keypress), and continue skipping until repeat_delay
596 * msecs have passed
597 */
c6698697 598 if (ati_remote->repeat_count > 0 &&
0de95509
KP
599 (ati_remote->repeat_count < 5 ||
600 time_before(now, ati_remote->first_jiffies +
601 msecs_to_jiffies(repeat_delay))))
1da177e4 602 return;
05f091ab 603
c34516e5
AH
604 if (index < 0) {
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 */
625 rc_keydown_notimeout(ati_remote->rdev, scancode,
626 data[2]);
627 rc_keyup(ati_remote->rdev);
628 }
c34516e5
AH
629 return;
630 }
1da177e4 631
1da177e4
LT
632 input_event(dev, ati_remote_tbl[index].type,
633 ati_remote_tbl[index].code, 1);
c3c38fbd 634 input_sync(dev);
1da177e4
LT
635 input_event(dev, ati_remote_tbl[index].type,
636 ati_remote_tbl[index].code, 0);
637 input_sync(dev);
638
2ffc1cca 639 } else {
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
DT
645 */
646 acc = ati_remote_compute_accel(ati_remote);
1da177e4 647
2ffc1cca
DT
648 switch (ati_remote_tbl[index].kind) {
649 case KIND_ACCEL:
650 input_event(dev, ati_remote_tbl[index].type,
651 ati_remote_tbl[index].code,
652 ati_remote_tbl[index].value * acc);
653 break;
654 case KIND_LU:
655 input_report_rel(dev, REL_X, -acc);
656 input_report_rel(dev, REL_Y, -acc);
657 break;
658 case KIND_RU:
659 input_report_rel(dev, REL_X, acc);
660 input_report_rel(dev, REL_Y, -acc);
661 break;
662 case KIND_LD:
663 input_report_rel(dev, REL_X, -acc);
664 input_report_rel(dev, REL_Y, acc);
665 break;
666 case KIND_RD:
667 input_report_rel(dev, REL_X, acc);
668 input_report_rel(dev, REL_Y, acc);
669 break;
670 default:
a342daea
DC
671 dev_dbg(&ati_remote->interface->dev,
672 "ati_remote kind=%d\n",
2ffc1cca
DT
673 ati_remote_tbl[index].kind);
674 }
675 input_sync(dev);
676
677 ati_remote->old_jiffies = jiffies;
5eefb4f0 678 ati_remote->old_data = data[2];
2ffc1cca 679 }
1da177e4
LT
680}
681
682/*
a342daea 683 * ati_remote_irq_in
1da177e4 684 */
7d12e780 685static void ati_remote_irq_in(struct urb *urb)
1da177e4
LT
686{
687 struct ati_remote *ati_remote = urb->context;
688 int retval;
689
690 switch (urb->status) {
691 case 0: /* success */
7d12e780 692 ati_remote_input_report(urb);
1da177e4
LT
693 break;
694 case -ECONNRESET: /* unlink */
695 case -ENOENT:
696 case -ESHUTDOWN:
a342daea
DC
697 dev_dbg(&ati_remote->interface->dev,
698 "%s: urb error status, unlink?\n",
ea3e6c59 699 __func__);
05f091ab 700 return;
1da177e4 701 default: /* error */
a342daea
DC
702 dev_dbg(&ati_remote->interface->dev,
703 "%s: Nonzero urb status %d\n",
ea3e6c59 704 __func__, urb->status);
1da177e4 705 }
05f091ab 706
54e6ecb2 707 retval = usb_submit_urb(urb, GFP_ATOMIC);
1da177e4 708 if (retval)
a342daea
DC
709 dev_err(&ati_remote->interface->dev,
710 "%s: usb_submit_urb()=%d\n",
ea3e6c59 711 __func__, retval);
1da177e4
LT
712}
713
714/*
a342daea 715 * ati_remote_alloc_buffers
1da177e4 716 */
c5b7c7c3
DT
717static int ati_remote_alloc_buffers(struct usb_device *udev,
718 struct ati_remote *ati_remote)
1da177e4 719{
997ea58e
DM
720 ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
721 &ati_remote->inbuf_dma);
c5b7c7c3
DT
722 if (!ati_remote->inbuf)
723 return -1;
1da177e4 724
997ea58e 725 ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
9688efda 726 &ati_remote->outbuf_dma);
c5b7c7c3
DT
727 if (!ati_remote->outbuf)
728 return -1;
1da177e4 729
c5b7c7c3
DT
730 ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
731 if (!ati_remote->irq_urb)
732 return -1;
1da177e4 733
c5b7c7c3
DT
734 ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
735 if (!ati_remote->out_urb)
736 return -1;
05f091ab 737
c5b7c7c3
DT
738 return 0;
739}
05f091ab 740
c5b7c7c3 741/*
a342daea 742 * ati_remote_free_buffers
c5b7c7c3
DT
743 */
744static void ati_remote_free_buffers(struct ati_remote *ati_remote)
745{
459f836a
MK
746 usb_free_urb(ati_remote->irq_urb);
747 usb_free_urb(ati_remote->out_urb);
1da177e4 748
997ea58e 749 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
9dce447a 750 ati_remote->inbuf, ati_remote->inbuf_dma);
c5b7c7c3 751
997ea58e 752 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
9dce447a 753 ati_remote->outbuf, ati_remote->outbuf_dma);
1da177e4
LT
754}
755
756static void ati_remote_input_init(struct ati_remote *ati_remote)
757{
c5b7c7c3 758 struct input_dev *idev = ati_remote->idev;
1da177e4
LT
759 int i;
760
7b19ada2
JS
761 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
762 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
763 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
764 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
1da177e4
LT
765 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
766 if (ati_remote_tbl[i].type == EV_KEY)
767 set_bit(ati_remote_tbl[i].code, idev->keybit);
05f091ab 768
7791bdae
DT
769 input_set_drvdata(idev, ati_remote);
770
c34516e5
AH
771 idev->open = ati_remote_input_open;
772 idev->close = ati_remote_input_close;
05f091ab 773
c34516e5
AH
774 idev->name = ati_remote->mouse_name;
775 idev->phys = ati_remote->mouse_phys;
05f091ab 776
16a334c0 777 usb_to_input_id(ati_remote->udev, &idev->id);
44fd0b60 778 idev->dev.parent = &ati_remote->interface->dev;
1da177e4
LT
779}
780
c34516e5
AH
781static void ati_remote_rc_init(struct ati_remote *ati_remote)
782{
783 struct rc_dev *rdev = ati_remote->rdev;
784
785 rdev->priv = ati_remote;
786 rdev->driver_type = RC_DRIVER_SCANCODE;
787 rdev->allowed_protos = RC_TYPE_OTHER;
788 rdev->driver_name = "ati_remote";
789
790 rdev->open = ati_remote_rc_open;
791 rdev->close = ati_remote_rc_close;
792
793 rdev->input_name = ati_remote->rc_name;
794 rdev->input_phys = ati_remote->rc_phys;
795
796 usb_to_input_id(ati_remote->udev, &rdev->input_id);
44fd0b60 797 rdev->dev.parent = &ati_remote->interface->dev;
c34516e5
AH
798}
799
1da177e4
LT
800static int ati_remote_initialize(struct ati_remote *ati_remote)
801{
802 struct usb_device *udev = ati_remote->udev;
803 int pipe, maxp;
05f091ab 804
1da177e4
LT
805 init_waitqueue_head(&ati_remote->wait);
806
807 /* Set up irq_urb */
808 pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
809 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
810 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
05f091ab
DT
811
812 usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
813 maxp, ati_remote_irq_in, ati_remote,
1da177e4
LT
814 ati_remote->endpoint_in->bInterval);
815 ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
816 ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
05f091ab 817
1da177e4
LT
818 /* Set up out_urb */
819 pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
820 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
821 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
822
05f091ab
DT
823 usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
824 maxp, ati_remote_irq_out, ati_remote,
1da177e4
LT
825 ati_remote->endpoint_out->bInterval);
826 ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
827 ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
828
829 /* send initialization strings */
830 if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
831 (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
05f091ab 832 dev_err(&ati_remote->interface->dev,
1da177e4 833 "Initializing ati_remote hardware failed.\n");
c5b7c7c3 834 return -EIO;
1da177e4 835 }
05f091ab 836
1da177e4
LT
837 return 0;
838}
839
840/*
a342daea 841 * ati_remote_probe
1da177e4 842 */
a342daea
DC
843static int ati_remote_probe(struct usb_interface *interface,
844 const struct usb_device_id *id)
1da177e4
LT
845{
846 struct usb_device *udev = interface_to_usbdev(interface);
c5b7c7c3
DT
847 struct usb_host_interface *iface_host = interface->cur_altsetting;
848 struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
0d74679c 849 struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info;
c5b7c7c3
DT
850 struct ati_remote *ati_remote;
851 struct input_dev *input_dev;
c34516e5 852 struct rc_dev *rc_dev;
c5b7c7c3 853 int err = -ENOMEM;
1da177e4 854
1da177e4 855 if (iface_host->desc.bNumEndpoints != 2) {
ea3e6c59 856 err("%s: Unexpected desc.bNumEndpoints\n", __func__);
c5b7c7c3 857 return -ENODEV;
1da177e4
LT
858 }
859
c5b7c7c3
DT
860 endpoint_in = &iface_host->endpoint[0].desc;
861 endpoint_out = &iface_host->endpoint[1].desc;
1da177e4 862
96642a2c 863 if (!usb_endpoint_is_int_in(endpoint_in)) {
ea3e6c59 864 err("%s: Unexpected endpoint_in\n", __func__);
c5b7c7c3 865 return -ENODEV;
1da177e4 866 }
c5b7c7c3 867 if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
ea3e6c59 868 err("%s: endpoint_in message size==0? \n", __func__);
c5b7c7c3 869 return -ENODEV;
1da177e4
LT
870 }
871
c5b7c7c3 872 ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
c34516e5
AH
873 rc_dev = rc_allocate_device();
874 if (!ati_remote || !rc_dev)
c5b7c7c3 875 goto fail1;
1da177e4 876
c5b7c7c3
DT
877 /* Allocate URB buffers, URBs */
878 if (ati_remote_alloc_buffers(udev, ati_remote))
879 goto fail2;
1da177e4 880
c5b7c7c3
DT
881 ati_remote->endpoint_in = endpoint_in;
882 ati_remote->endpoint_out = endpoint_out;
883 ati_remote->udev = udev;
c34516e5 884 ati_remote->rdev = rc_dev;
c5b7c7c3 885 ati_remote->interface = interface;
1da177e4 886
c34516e5
AH
887 usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
888 strlcpy(ati_remote->mouse_phys, ati_remote->rc_phys,
889 sizeof(ati_remote->mouse_phys));
890
891 strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
892 strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
1da177e4 893
1da177e4 894 if (udev->manufacturer)
c34516e5
AH
895 strlcpy(ati_remote->rc_name, udev->manufacturer,
896 sizeof(ati_remote->rc_name));
1da177e4
LT
897
898 if (udev->product)
c34516e5
AH
899 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
900 "%s %s", ati_remote->rc_name, udev->product);
1da177e4 901
c34516e5
AH
902 if (!strlen(ati_remote->rc_name))
903 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
c5b7c7c3 904 DRIVER_DESC "(%04x,%04x)",
05f091ab 905 le16_to_cpu(ati_remote->udev->descriptor.idVendor),
1da177e4
LT
906 le16_to_cpu(ati_remote->udev->descriptor.idProduct));
907
c34516e5
AH
908 snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
909 "%s mouse", ati_remote->rc_name);
910
0d74679c
AH
911 rc_dev->map_name = RC_MAP_ATI_X10; /* default map */
912
913 /* set default keymap according to receiver model */
914 if (type) {
915 if (type->default_keymap)
916 rc_dev->map_name = type->default_keymap;
917 else if (type->get_default_keymap)
918 rc_dev->map_name = type->get_default_keymap(interface);
919 }
175fcecf 920
c34516e5
AH
921 ati_remote_rc_init(ati_remote);
922 mutex_init(&ati_remote->open_mutex);
c5b7c7c3 923
1da177e4 924 /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
c5b7c7c3
DT
925 err = ati_remote_initialize(ati_remote);
926 if (err)
927 goto fail3;
1da177e4 928
c34516e5
AH
929 /* Set up and register rc device */
930 err = rc_register_device(ati_remote->rdev);
5014186d
DT
931 if (err)
932 goto fail3;
1da177e4 933
c34516e5
AH
934 /* use our delay for rc_dev */
935 ati_remote->rdev->input_dev->rep[REP_DELAY] = repeat_delay;
936
937 /* Set up and register mouse input device */
938 if (mouse) {
939 input_dev = input_allocate_device();
940 if (!input_dev)
941 goto fail4;
942
943 ati_remote->idev = input_dev;
944 ati_remote_input_init(ati_remote);
945 err = input_register_device(input_dev);
946
947 if (err)
948 goto fail5;
949 }
950
1da177e4 951 usb_set_intfdata(interface, ati_remote);
c5b7c7c3 952 return 0;
05f091ab 953
c34516e5
AH
954 fail5: input_free_device(input_dev);
955 fail4: rc_unregister_device(rc_dev);
956 rc_dev = NULL;
5014186d 957 fail3: usb_kill_urb(ati_remote->irq_urb);
c5b7c7c3 958 usb_kill_urb(ati_remote->out_urb);
5014186d 959 fail2: ati_remote_free_buffers(ati_remote);
c34516e5 960 fail1: rc_free_device(rc_dev);
c5b7c7c3
DT
961 kfree(ati_remote);
962 return err;
1da177e4
LT
963}
964
965/*
a342daea 966 * ati_remote_disconnect
1da177e4
LT
967 */
968static void ati_remote_disconnect(struct usb_interface *interface)
969{
970 struct ati_remote *ati_remote;
971
1da177e4
LT
972 ati_remote = usb_get_intfdata(interface);
973 usb_set_intfdata(interface, NULL);
974 if (!ati_remote) {
1817b169 975 dev_warn(&interface->dev, "%s - null device?\n", __func__);
1da177e4
LT
976 return;
977 }
05f091ab 978
c5b7c7c3
DT
979 usb_kill_urb(ati_remote->irq_urb);
980 usb_kill_urb(ati_remote->out_urb);
c34516e5
AH
981 if (ati_remote->idev)
982 input_unregister_device(ati_remote->idev);
983 rc_unregister_device(ati_remote->rdev);
c5b7c7c3
DT
984 ati_remote_free_buffers(ati_remote);
985 kfree(ati_remote);
1da177e4
LT
986}
987
a342daea
DC
988/* usb specific object to register with the usb subsystem */
989static struct usb_driver ati_remote_driver = {
990 .name = "ati_remote",
991 .probe = ati_remote_probe,
992 .disconnect = ati_remote_disconnect,
993 .id_table = ati_remote_table,
994};
995
ecb3b2b3 996module_usb_driver(ati_remote_driver);
1da177e4
LT
997
998MODULE_AUTHOR(DRIVER_AUTHOR);
999MODULE_DESCRIPTION(DRIVER_DESC);
1000MODULE_LICENSE("GPL");