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