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