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