1/*
2 *  USB ATI Remote support
3 *
4 *  Version 2.2.0 Copyright (c) 2004 Torrey Hoffman <thoffman@arnor.net>
5 *  Version 2.1.1 Copyright (c) 2002 Vladimir Dergachev
6 *
7 *  This 2.2.0 version is a rewrite / cleanup of the 2.1.1 driver, including
8 *  porting to the 2.6 kernel interfaces, along with other modification
9 *  to better match the style of the existing usb/input drivers.  However, the
10 *  protocol and hardware handling is essentially unchanged from 2.1.1.
11 *
12 *  The 2.1.1 driver was derived from the usbati_remote and usbkbd drivers by
13 *  Vojtech Pavlik.
14 *
15 *  Changes:
16 *
17 *  Feb 2004: Torrey Hoffman <thoffman@arnor.net>
18 *            Version 2.2.0
19 *  Jun 2004: Torrey Hoffman <thoffman@arnor.net>
20 *            Version 2.2.1
21 *            Added key repeat support contributed by:
22 *                Vincent Vanackere <vanackere@lif.univ-mrs.fr>
23 *            Added support for the "Lola" remote contributed by:
24 *                Seth Cohn <sethcohn@yahoo.com>
25 *
26 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
27 *
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * (at your option) any later version.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36 * GNU General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 *
42 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
43 *
44 * Hardware & software notes
45 *
46 * These remote controls are distributed by ATI as part of their
47 * "All-In-Wonder" video card packages.  The receiver self-identifies as a
48 * "USB Receiver" with manufacturer "X10 Wireless Technology Inc".
49 *
50 * The "Lola" remote is available from X10.  See:
51 *    http://www.x10.com/products/lola_sg1.htm
52 * The Lola is similar to the ATI remote but has no mouse support, and slightly
53 * different keys.
54 *
55 * It is possible to use multiple receivers and remotes on multiple computers
56 * simultaneously by configuring them to use specific channels.
57 *
58 * The RF protocol used by the remote supports 16 distinct channels, 1 to 16.
59 * Actually, it may even support more, at least in some revisions of the
60 * hardware.
61 *
62 * Each remote can be configured to transmit on one channel as follows:
63 *   - Press and hold the "hand icon" button.
64 *   - When the red LED starts to blink, let go of the "hand icon" button.
65 *   - When it stops blinking, input the channel code as two digits, from 01
66 *     to 16, and press the hand icon again.
67 *
68 * The timing can be a little tricky.  Try loading the module with debug=1
69 * to have the kernel print out messages about the remote control number
70 * and mask.  Note: debugging prints remote numbers as zero-based hexadecimal.
71 *
72 * The driver has a "channel_mask" parameter. This bitmask specifies which
73 * channels will be ignored by the module.  To mask out channels, just add
74 * all the 2^channel_number values together.
75 *
76 * For instance, set channel_mask = 2^4 = 16 (binary 10000) to make ati_remote
77 * ignore signals coming from remote controls transmitting on channel 4, but
78 * accept all other channels.
79 *
80 * Or, set channel_mask = 65533, (0xFFFD), and all channels except 1 will be
81 * ignored.
82 *
83 * The default is 0 (respond to all channels). Bit 0 and bits 17-32 of this
84 * parameter are unused.
85 *
86 */
87
88#include <linux/kernel.h>
89#include <linux/errno.h>
90#include <linux/init.h>
91#include <linux/slab.h>
92#include <linux/module.h>
93#include <linux/moduleparam.h>
94#include <linux/usb/input.h>
95#include <linux/wait.h>
96#include <linux/jiffies.h>
97
98/*
99 * Module and Version Information, Module Parameters
100 */
101
102#define ATI_REMOTE_VENDOR_ID	0x0bc7
103#define ATI_REMOTE_PRODUCT_ID	0x004
104#define LOLA_REMOTE_PRODUCT_ID	0x002
105#define MEDION_REMOTE_PRODUCT_ID 0x006
106
107#define DRIVER_VERSION	        "2.2.1"
108#define DRIVER_AUTHOR           "Torrey Hoffman <thoffman@arnor.net>"
109#define DRIVER_DESC             "ATI/X10 RF USB Remote Control"
110
111#define NAME_BUFSIZE      80    /* size of product name, path buffers */
112#define DATA_BUFSIZE      63    /* size of URB data buffers */
113
114/*
115 * Duplicate event filtering time.
116 * Sequential, identical KIND_FILTERED inputs with less than
117 * FILTER_TIME milliseconds between them are considered as repeat
118 * events. The hardware generates 5 events for the first keypress
119 * and we have to take this into account for an accurate repeat
120 * behaviour.
121 */
122#define FILTER_TIME	60 /* msec */
123#define REPEAT_DELAY	500 /* msec */
124
125static unsigned long channel_mask;
126module_param(channel_mask, ulong, 0644);
127MODULE_PARM_DESC(channel_mask, "Bitmask of remote control channels to ignore");
128
129static int debug;
130module_param(debug, int, 0644);
131MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
132
133static int repeat_filter = FILTER_TIME;
134module_param(repeat_filter, int, 0644);
135MODULE_PARM_DESC(repeat_filter, "Repeat filter time, default = 60 msec");
136
137static int repeat_delay = REPEAT_DELAY;
138module_param(repeat_delay, int, 0644);
139MODULE_PARM_DESC(repeat_delay, "Delay before sending repeats, default = 500 msec");
140
141#define dbginfo(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
142#undef err
143#define err(format, arg...) printk(KERN_ERR format , ## arg)
144
145static struct usb_device_id ati_remote_table[] = {
146	{ USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID) },
147	{ USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID) },
148	{ USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID) },
149	{}	/* Terminating entry */
150};
151
152MODULE_DEVICE_TABLE(usb, ati_remote_table);
153
154/* Get hi and low bytes of a 16-bits int */
155#define HI(a)	((unsigned char)((a) >> 8))
156#define LO(a)	((unsigned char)((a) & 0xff))
157
158#define SEND_FLAG_IN_PROGRESS	1
159#define SEND_FLAG_COMPLETE	2
160
161/* Device initialization strings */
162static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
163static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
164
165struct ati_remote {
166	struct input_dev *idev;
167	struct usb_device *udev;
168	struct usb_interface *interface;
169
170	struct urb *irq_urb;
171	struct urb *out_urb;
172	struct usb_endpoint_descriptor *endpoint_in;
173	struct usb_endpoint_descriptor *endpoint_out;
174	unsigned char *inbuf;
175	unsigned char *outbuf;
176	dma_addr_t inbuf_dma;
177	dma_addr_t outbuf_dma;
178
179	unsigned char old_data[2];  /* Detect duplicate events */
180	unsigned long old_jiffies;
181	unsigned long acc_jiffies;  /* handle acceleration */
182	unsigned long first_jiffies;
183
184	unsigned int repeat_count;
185
186	char name[NAME_BUFSIZE];
187	char phys[NAME_BUFSIZE];
188
189	wait_queue_head_t wait;
190	int send_flags;
191};
192
193/* "Kinds" of messages sent from the hardware to the driver. */
194#define KIND_END        0
195#define KIND_LITERAL    1   /* Simply pass to input system */
196#define KIND_FILTERED   2   /* Add artificial key-up events, drop keyrepeats */
197#define KIND_LU         3   /* Directional keypad diagonals - left up, */
198#define KIND_RU         4   /*   right up,  */
199#define KIND_LD         5   /*   left down, */
200#define KIND_RD         6   /*   right down */
201#define KIND_ACCEL      7   /* Directional keypad - left, right, up, down.*/
202
203/* Translation table from hardware messages to input events. */
204static const struct {
205	short kind;
206	unsigned char data1, data2;
207	int type;
208	unsigned int code;
209	int value;
210}  ati_remote_tbl[] = {
211	/* Directional control pad axes */
212	{KIND_ACCEL,   0x35, 0x70, EV_REL, REL_X, -1},	 /* left */
213	{KIND_ACCEL,   0x36, 0x71, EV_REL, REL_X, 1},    /* right */
214	{KIND_ACCEL,   0x37, 0x72, EV_REL, REL_Y, -1},	 /* up */
215	{KIND_ACCEL,   0x38, 0x73, EV_REL, REL_Y, 1},    /* down */
216	/* Directional control pad diagonals */
217	{KIND_LU,      0x39, 0x74, EV_REL, 0, 0},        /* left up */
218	{KIND_RU,      0x3a, 0x75, EV_REL, 0, 0},        /* right up */
219	{KIND_LD,      0x3c, 0x77, EV_REL, 0, 0},        /* left down */
220	{KIND_RD,      0x3b, 0x76, EV_REL, 0, 0},        /* right down */
221
222	/* "Mouse button" buttons */
223	{KIND_LITERAL, 0x3d, 0x78, EV_KEY, BTN_LEFT, 1}, /* left btn down */
224	{KIND_LITERAL, 0x3e, 0x79, EV_KEY, BTN_LEFT, 0}, /* left btn up */
225	{KIND_LITERAL, 0x41, 0x7c, EV_KEY, BTN_RIGHT, 1},/* right btn down */
226	{KIND_LITERAL, 0x42, 0x7d, EV_KEY, BTN_RIGHT, 0},/* right btn up */
227
228	/* Artificial "doubleclick" events are generated by the hardware.
229	 * They are mapped to the "side" and "extra" mouse buttons here. */
230	{KIND_FILTERED, 0x3f, 0x7a, EV_KEY, BTN_SIDE, 1}, /* left dblclick */
231	{KIND_FILTERED, 0x43, 0x7e, EV_KEY, BTN_EXTRA, 1},/* right dblclick */
232
233	/* keyboard. */
234	{KIND_FILTERED, 0xd2, 0x0d, EV_KEY, KEY_1, 1},
235	{KIND_FILTERED, 0xd3, 0x0e, EV_KEY, KEY_2, 1},
236	{KIND_FILTERED, 0xd4, 0x0f, EV_KEY, KEY_3, 1},
237	{KIND_FILTERED, 0xd5, 0x10, EV_KEY, KEY_4, 1},
238	{KIND_FILTERED, 0xd6, 0x11, EV_KEY, KEY_5, 1},
239	{KIND_FILTERED, 0xd7, 0x12, EV_KEY, KEY_6, 1},
240	{KIND_FILTERED, 0xd8, 0x13, EV_KEY, KEY_7, 1},
241	{KIND_FILTERED, 0xd9, 0x14, EV_KEY, KEY_8, 1},
242	{KIND_FILTERED, 0xda, 0x15, EV_KEY, KEY_9, 1},
243	{KIND_FILTERED, 0xdc, 0x17, EV_KEY, KEY_0, 1},
244	{KIND_FILTERED, 0xc5, 0x00, EV_KEY, KEY_A, 1},
245	{KIND_FILTERED, 0xc6, 0x01, EV_KEY, KEY_B, 1},
246	{KIND_FILTERED, 0xde, 0x19, EV_KEY, KEY_C, 1},
247	{KIND_FILTERED, 0xe0, 0x1b, EV_KEY, KEY_D, 1},
248	{KIND_FILTERED, 0xe6, 0x21, EV_KEY, KEY_E, 1},
249	{KIND_FILTERED, 0xe8, 0x23, EV_KEY, KEY_F, 1},
250
251	/* "special" keys */
252	{KIND_FILTERED, 0xdd, 0x18, EV_KEY, KEY_KPENTER, 1},    /* "check" */
253	{KIND_FILTERED, 0xdb, 0x16, EV_KEY, KEY_MENU, 1},       /* "menu" */
254	{KIND_FILTERED, 0xc7, 0x02, EV_KEY, KEY_POWER, 1},      /* Power */
255	{KIND_FILTERED, 0xc8, 0x03, EV_KEY, KEY_TV, 1},         /* TV */
256	{KIND_FILTERED, 0xc9, 0x04, EV_KEY, KEY_DVD, 1},        /* DVD */
257	{KIND_FILTERED, 0xca, 0x05, EV_KEY, KEY_WWW, 1},        /* WEB */
258	{KIND_FILTERED, 0xcb, 0x06, EV_KEY, KEY_BOOKMARKS, 1},  /* "book" */
259	{KIND_FILTERED, 0xcc, 0x07, EV_KEY, KEY_EDIT, 1},       /* "hand" */
260	{KIND_FILTERED, 0xe1, 0x1c, EV_KEY, KEY_COFFEE, 1},     /* "timer" */
261	{KIND_FILTERED, 0xe5, 0x20, EV_KEY, KEY_FRONT, 1},      /* "max" */
262	{KIND_FILTERED, 0xe2, 0x1d, EV_KEY, KEY_LEFT, 1},       /* left */
263	{KIND_FILTERED, 0xe4, 0x1f, EV_KEY, KEY_RIGHT, 1},      /* right */
264	{KIND_FILTERED, 0xe7, 0x22, EV_KEY, KEY_DOWN, 1},       /* down */
265	{KIND_FILTERED, 0xdf, 0x1a, EV_KEY, KEY_UP, 1},         /* up */
266	{KIND_FILTERED, 0xe3, 0x1e, EV_KEY, KEY_OK, 1},         /* "OK" */
267	{KIND_FILTERED, 0xce, 0x09, EV_KEY, KEY_VOLUMEDOWN, 1}, /* VOL + */
268	{KIND_FILTERED, 0xcd, 0x08, EV_KEY, KEY_VOLUMEUP, 1},   /* VOL - */
269	{KIND_FILTERED, 0xcf, 0x0a, EV_KEY, KEY_MUTE, 1},       /* MUTE  */
270	{KIND_FILTERED, 0xd0, 0x0b, EV_KEY, KEY_CHANNELUP, 1},  /* CH + */
271	{KIND_FILTERED, 0xd1, 0x0c, EV_KEY, KEY_CHANNELDOWN, 1},/* CH - */
272	{KIND_FILTERED, 0xec, 0x27, EV_KEY, KEY_RECORD, 1},     /* ( o) red */
273	{KIND_FILTERED, 0xea, 0x25, EV_KEY, KEY_PLAY, 1},       /* ( >) */
274	{KIND_FILTERED, 0xe9, 0x24, EV_KEY, KEY_REWIND, 1},     /* (<<) */
275	{KIND_FILTERED, 0xeb, 0x26, EV_KEY, KEY_FORWARD, 1},    /* (>>) */
276	{KIND_FILTERED, 0xed, 0x28, EV_KEY, KEY_STOP, 1},       /* ([]) */
277	{KIND_FILTERED, 0xee, 0x29, EV_KEY, KEY_PAUSE, 1},      /* ('') */
278	{KIND_FILTERED, 0xf0, 0x2b, EV_KEY, KEY_PREVIOUS, 1},   /* (<-) */
279	{KIND_FILTERED, 0xef, 0x2a, EV_KEY, KEY_NEXT, 1},       /* (>+) */
280	{KIND_FILTERED, 0xf2, 0x2D, EV_KEY, KEY_INFO, 1},       /* PLAYING */
281	{KIND_FILTERED, 0xf3, 0x2E, EV_KEY, KEY_HOME, 1},       /* TOP */
282	{KIND_FILTERED, 0xf4, 0x2F, EV_KEY, KEY_END, 1},        /* END */
283	{KIND_FILTERED, 0xf5, 0x30, EV_KEY, KEY_SELECT, 1},     /* SELECT */
284
285	{KIND_END, 0x00, 0x00, EV_MAX + 1, 0, 0}
286};
287
288/* Local function prototypes */
289static void ati_remote_dump		(unsigned char *data, unsigned int actual_length);
290static int ati_remote_open		(struct input_dev *inputdev);
291static void ati_remote_close		(struct input_dev *inputdev);
292static int ati_remote_sendpacket	(struct ati_remote *ati_remote, u16 cmd, unsigned char *data);
293static void ati_remote_irq_out		(struct urb *urb);
294static void ati_remote_irq_in		(struct urb *urb);
295static void ati_remote_input_report	(struct urb *urb);
296static int ati_remote_initialize	(struct ati_remote *ati_remote);
297static int ati_remote_probe		(struct usb_interface *interface, const struct usb_device_id *id);
298static void ati_remote_disconnect	(struct usb_interface *interface);
299
300/* usb specific object to register with the usb subsystem */
301static struct usb_driver ati_remote_driver = {
302	.name         = "ati_remote",
303	.probe        = ati_remote_probe,
304	.disconnect   = ati_remote_disconnect,
305	.id_table     = ati_remote_table,
306};
307
308/*
309 *	ati_remote_dump_input
310 */
311static void ati_remote_dump(unsigned char *data, unsigned int len)
312{
313	if ((len == 1) && (data[0] != (unsigned char)0xff) && (data[0] != 0x00))
314		warn("Weird byte 0x%02x", data[0]);
315	else if (len == 4)
316		warn("Weird key %02x %02x %02x %02x",
317		     data[0], data[1], data[2], data[3]);
318	else
319		warn("Weird data, len=%d %02x %02x %02x %02x %02x %02x ...",
320		     len, data[0], data[1], data[2], data[3], data[4], data[5]);
321}
322
323/*
324 *	ati_remote_open
325 */
326static int ati_remote_open(struct input_dev *inputdev)
327{
328	struct ati_remote *ati_remote = input_get_drvdata(inputdev);
329
330	/* On first open, submit the read urb which was set up previously. */
331	ati_remote->irq_urb->dev = ati_remote->udev;
332	if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
333		dev_err(&ati_remote->interface->dev,
334			"%s: usb_submit_urb failed!\n", __FUNCTION__);
335		return -EIO;
336	}
337
338	return 0;
339}
340
341/*
342 *	ati_remote_close
343 */
344static void ati_remote_close(struct input_dev *inputdev)
345{
346	struct ati_remote *ati_remote = input_get_drvdata(inputdev);
347
348	usb_kill_urb(ati_remote->irq_urb);
349}
350
351/*
352 *		ati_remote_irq_out
353 */
354static void ati_remote_irq_out(struct urb *urb)
355{
356	struct ati_remote *ati_remote = urb->context;
357
358	if (urb->status) {
359		dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
360			__FUNCTION__, urb->status);
361		return;
362	}
363
364	ati_remote->send_flags |= SEND_FLAG_COMPLETE;
365	wmb();
366	wake_up(&ati_remote->wait);
367}
368
369/*
370 *	ati_remote_sendpacket
371 *
372 *	Used to send device initialization strings
373 */
374static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, unsigned char *data)
375{
376	int retval = 0;
377
378	/* Set up out_urb */
379	memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
380	((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
381
382	ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
383	ati_remote->out_urb->dev = ati_remote->udev;
384	ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
385
386	retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
387	if (retval) {
388		dev_dbg(&ati_remote->interface->dev,
389			 "sendpacket: usb_submit_urb failed: %d\n", retval);
390		return retval;
391	}
392
393	wait_event_timeout(ati_remote->wait,
394		((ati_remote->out_urb->status != -EINPROGRESS) ||
395			(ati_remote->send_flags & SEND_FLAG_COMPLETE)),
396		HZ);
397	usb_kill_urb(ati_remote->out_urb);
398
399	return retval;
400}
401
402/*
403 *	ati_remote_event_lookup
404 */
405static int ati_remote_event_lookup(int rem, unsigned char d1, unsigned char d2)
406{
407	int i;
408
409	for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
410		/*
411		 * Decide if the table entry matches the remote input.
412		 */
413		if ((((ati_remote_tbl[i].data1 & 0x0f) == (d1 & 0x0f))) &&
414		    ((((ati_remote_tbl[i].data1 >> 4) -
415		       (d1 >> 4) + rem) & 0x0f) == 0x0f) &&
416		    (ati_remote_tbl[i].data2 == d2))
417			return i;
418
419	}
420	return -1;
421}
422
423/*
424 *	ati_remote_compute_accel
425 *
426 * Implements acceleration curve for directional control pad
427 * If elapsed time since last event is > 1/4 second, user "stopped",
428 * so reset acceleration. Otherwise, user is probably holding the control
429 * pad down, so we increase acceleration, ramping up over two seconds to
430 * a maximum speed.
431 */
432static int ati_remote_compute_accel(struct ati_remote *ati_remote)
433{
434	static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
435	unsigned long now = jiffies;
436	int acc;
437
438	if (time_after(now, ati_remote->old_jiffies + msecs_to_jiffies(250))) {
439		acc = 1;
440		ati_remote->acc_jiffies = now;
441	}
442	else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(125)))
443		acc = accel[0];
444	else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(250)))
445		acc = accel[1];
446	else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(500)))
447		acc = accel[2];
448	else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1000)))
449		acc = accel[3];
450	else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1500)))
451		acc = accel[4];
452	else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(2000)))
453		acc = accel[5];
454	else
455		acc = accel[6];
456
457	return acc;
458}
459
460/*
461 *	ati_remote_report_input
462 */
463static void ati_remote_input_report(struct urb *urb)
464{
465	struct ati_remote *ati_remote = urb->context;
466	unsigned char *data= ati_remote->inbuf;
467	struct input_dev *dev = ati_remote->idev;
468	int index, acc;
469	int remote_num;
470
471	/* Deal with strange looking inputs */
472	if ( (urb->actual_length != 4) || (data[0] != 0x14) ||
473		((data[3] & 0x0f) != 0x00) ) {
474		ati_remote_dump(data, urb->actual_length);
475		return;
476	}
477
478	/* Mask unwanted remote channels.  */
479	/* note: remote_num is 0-based, channel 1 on remote == 0 here */
480	remote_num = (data[3] >> 4) & 0x0f;
481        if (channel_mask & (1 << (remote_num + 1))) {
482		dbginfo(&ati_remote->interface->dev,
483			"Masked input from channel 0x%02x: data %02x,%02x, mask= 0x%02lx\n",
484			remote_num, data[1], data[2], channel_mask);
485		return;
486	}
487
488	/* Look up event code index in translation table */
489	index = ati_remote_event_lookup(remote_num, data[1], data[2]);
490	if (index < 0) {
491		dev_warn(&ati_remote->interface->dev,
492			 "Unknown input from channel 0x%02x: data %02x,%02x\n",
493			 remote_num, data[1], data[2]);
494		return;
495	}
496	dbginfo(&ati_remote->interface->dev,
497		"channel 0x%02x; data %02x,%02x; index %d; keycode %d\n",
498		remote_num, data[1], data[2], index, ati_remote_tbl[index].code);
499
500	if (ati_remote_tbl[index].kind == KIND_LITERAL) {
501		input_event(dev, ati_remote_tbl[index].type,
502			ati_remote_tbl[index].code,
503			ati_remote_tbl[index].value);
504		input_sync(dev);
505
506		ati_remote->old_jiffies = jiffies;
507		return;
508	}
509
510	if (ati_remote_tbl[index].kind == KIND_FILTERED) {
511		unsigned long now = jiffies;
512
513		/* Filter duplicate events which happen "too close" together. */
514		if (ati_remote->old_data[0] == data[1] &&
515		    ati_remote->old_data[1] == data[2] &&
516		    time_before(now, ati_remote->old_jiffies +
517				     msecs_to_jiffies(repeat_filter))) {
518			ati_remote->repeat_count++;
519		} else {
520			ati_remote->repeat_count = 0;
521			ati_remote->first_jiffies = now;
522		}
523
524		ati_remote->old_data[0] = data[1];
525		ati_remote->old_data[1] = data[2];
526		ati_remote->old_jiffies = now;
527
528		/* Ensure we skip at least the 4 first duplicate events (generated
529		 * by a single keypress), and continue skipping until repeat_delay
530		 * msecs have passed
531		 */
532		if (ati_remote->repeat_count > 0 &&
533		    (ati_remote->repeat_count < 5 ||
534		     time_before(now, ati_remote->first_jiffies +
535				      msecs_to_jiffies(repeat_delay))))
536			return;
537
538
539		input_event(dev, ati_remote_tbl[index].type,
540			ati_remote_tbl[index].code, 1);
541		input_sync(dev);
542		input_event(dev, ati_remote_tbl[index].type,
543			ati_remote_tbl[index].code, 0);
544		input_sync(dev);
545
546	} else {
547
548		/*
549		 * Other event kinds are from the directional control pad, and have an
550		 * acceleration factor applied to them.  Without this acceleration, the
551		 * control pad is mostly unusable.
552		 */
553		acc = ati_remote_compute_accel(ati_remote);
554
555		switch (ati_remote_tbl[index].kind) {
556		case KIND_ACCEL:
557			input_event(dev, ati_remote_tbl[index].type,
558				ati_remote_tbl[index].code,
559				ati_remote_tbl[index].value * acc);
560			break;
561		case KIND_LU:
562			input_report_rel(dev, REL_X, -acc);
563			input_report_rel(dev, REL_Y, -acc);
564			break;
565		case KIND_RU:
566			input_report_rel(dev, REL_X, acc);
567			input_report_rel(dev, REL_Y, -acc);
568			break;
569		case KIND_LD:
570			input_report_rel(dev, REL_X, -acc);
571			input_report_rel(dev, REL_Y, acc);
572			break;
573		case KIND_RD:
574			input_report_rel(dev, REL_X, acc);
575			input_report_rel(dev, REL_Y, acc);
576			break;
577		default:
578			dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
579				ati_remote_tbl[index].kind);
580		}
581		input_sync(dev);
582
583		ati_remote->old_jiffies = jiffies;
584		ati_remote->old_data[0] = data[1];
585		ati_remote->old_data[1] = data[2];
586	}
587}
588
589/*
590 *	ati_remote_irq_in
591 */
592static void ati_remote_irq_in(struct urb *urb)
593{
594	struct ati_remote *ati_remote = urb->context;
595	int retval;
596
597	switch (urb->status) {
598	case 0:			/* success */
599		ati_remote_input_report(urb);
600		break;
601	case -ECONNRESET:	/* unlink */
602	case -ENOENT:
603	case -ESHUTDOWN:
604		dev_dbg(&ati_remote->interface->dev, "%s: urb error status, unlink? \n",
605			__FUNCTION__);
606		return;
607	default:		/* error */
608		dev_dbg(&ati_remote->interface->dev, "%s: Nonzero urb status %d\n",
609			__FUNCTION__, urb->status);
610	}
611
612	retval = usb_submit_urb(urb, GFP_ATOMIC);
613	if (retval)
614		dev_err(&ati_remote->interface->dev, "%s: usb_submit_urb()=%d\n",
615			__FUNCTION__, retval);
616}
617
618/*
619 *	ati_remote_alloc_buffers
620 */
621static int ati_remote_alloc_buffers(struct usb_device *udev,
622				    struct ati_remote *ati_remote)
623{
624	ati_remote->inbuf = usb_buffer_alloc(udev, DATA_BUFSIZE, GFP_ATOMIC,
625					     &ati_remote->inbuf_dma);
626	if (!ati_remote->inbuf)
627		return -1;
628
629	ati_remote->outbuf = usb_buffer_alloc(udev, DATA_BUFSIZE, GFP_ATOMIC,
630					      &ati_remote->outbuf_dma);
631	if (!ati_remote->outbuf)
632		return -1;
633
634	ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
635	if (!ati_remote->irq_urb)
636		return -1;
637
638	ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
639	if (!ati_remote->out_urb)
640		return -1;
641
642	return 0;
643}
644
645/*
646 *	ati_remote_free_buffers
647 */
648static void ati_remote_free_buffers(struct ati_remote *ati_remote)
649{
650	usb_free_urb(ati_remote->irq_urb);
651	usb_free_urb(ati_remote->out_urb);
652
653	usb_buffer_free(ati_remote->udev, DATA_BUFSIZE,
654		ati_remote->inbuf, ati_remote->inbuf_dma);
655
656	usb_buffer_free(ati_remote->udev, DATA_BUFSIZE,
657		ati_remote->outbuf, ati_remote->outbuf_dma);
658}
659
660static void ati_remote_input_init(struct ati_remote *ati_remote)
661{
662	struct input_dev *idev = ati_remote->idev;
663	int i;
664
665	idev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
666	idev->keybit[LONG(BTN_MOUSE)] = ( BIT(BTN_LEFT) | BIT(BTN_RIGHT) |
667					  BIT(BTN_SIDE) | BIT(BTN_EXTRA) );
668	idev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
669	for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
670		if (ati_remote_tbl[i].type == EV_KEY)
671			set_bit(ati_remote_tbl[i].code, idev->keybit);
672
673	input_set_drvdata(idev, ati_remote);
674
675	idev->open = ati_remote_open;
676	idev->close = ati_remote_close;
677
678	idev->name = ati_remote->name;
679	idev->phys = ati_remote->phys;
680
681	usb_to_input_id(ati_remote->udev, &idev->id);
682	idev->dev.parent = &ati_remote->udev->dev;
683}
684
685static int ati_remote_initialize(struct ati_remote *ati_remote)
686{
687	struct usb_device *udev = ati_remote->udev;
688	int pipe, maxp;
689
690	init_waitqueue_head(&ati_remote->wait);
691
692	/* Set up irq_urb */
693	pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
694	maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
695	maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
696
697	usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
698			 maxp, ati_remote_irq_in, ati_remote,
699			 ati_remote->endpoint_in->bInterval);
700	ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
701	ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
702
703	/* Set up out_urb */
704	pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
705	maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
706	maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
707
708	usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
709			 maxp, ati_remote_irq_out, ati_remote,
710			 ati_remote->endpoint_out->bInterval);
711	ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
712	ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
713
714	/* send initialization strings */
715	if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
716	    (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
717		dev_err(&ati_remote->interface->dev,
718			 "Initializing ati_remote hardware failed.\n");
719		return -EIO;
720	}
721
722	return 0;
723}
724
725/*
726 *	ati_remote_probe
727 */
728static int ati_remote_probe(struct usb_interface *interface, const struct usb_device_id *id)
729{
730	struct usb_device *udev = interface_to_usbdev(interface);
731	struct usb_host_interface *iface_host = interface->cur_altsetting;
732	struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
733	struct ati_remote *ati_remote;
734	struct input_dev *input_dev;
735	int err = -ENOMEM;
736
737	if (iface_host->desc.bNumEndpoints != 2) {
738		err("%s: Unexpected desc.bNumEndpoints\n", __FUNCTION__);
739		return -ENODEV;
740	}
741
742	endpoint_in = &iface_host->endpoint[0].desc;
743	endpoint_out = &iface_host->endpoint[1].desc;
744
745	if (!usb_endpoint_is_int_in(endpoint_in)) {
746		err("%s: Unexpected endpoint_in\n", __FUNCTION__);
747		return -ENODEV;
748	}
749	if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
750		err("%s: endpoint_in message size==0? \n", __FUNCTION__);
751		return -ENODEV;
752	}
753
754	ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
755	input_dev = input_allocate_device();
756	if (!ati_remote || !input_dev)
757		goto fail1;
758
759	/* Allocate URB buffers, URBs */
760	if (ati_remote_alloc_buffers(udev, ati_remote))
761		goto fail2;
762
763	ati_remote->endpoint_in = endpoint_in;
764	ati_remote->endpoint_out = endpoint_out;
765	ati_remote->udev = udev;
766	ati_remote->idev = input_dev;
767	ati_remote->interface = interface;
768
769	usb_make_path(udev, ati_remote->phys, sizeof(ati_remote->phys));
770	strlcpy(ati_remote->phys, "/input0", sizeof(ati_remote->phys));
771
772	if (udev->manufacturer)
773		strlcpy(ati_remote->name, udev->manufacturer, sizeof(ati_remote->name));
774
775	if (udev->product)
776		snprintf(ati_remote->name, sizeof(ati_remote->name),
777			 "%s %s", ati_remote->name, udev->product);
778
779	if (!strlen(ati_remote->name))
780		snprintf(ati_remote->name, sizeof(ati_remote->name),
781			DRIVER_DESC "(%04x,%04x)",
782			le16_to_cpu(ati_remote->udev->descriptor.idVendor),
783			le16_to_cpu(ati_remote->udev->descriptor.idProduct));
784
785	ati_remote_input_init(ati_remote);
786
787	/* Device Hardware Initialization - fills in ati_remote->idev from udev. */
788	err = ati_remote_initialize(ati_remote);
789	if (err)
790		goto fail3;
791
792	/* Set up and register input device */
793	err = input_register_device(ati_remote->idev);
794	if (err)
795		goto fail3;
796
797	usb_set_intfdata(interface, ati_remote);
798	return 0;
799
800 fail3:	usb_kill_urb(ati_remote->irq_urb);
801	usb_kill_urb(ati_remote->out_urb);
802 fail2:	ati_remote_free_buffers(ati_remote);
803 fail1:	input_free_device(input_dev);
804	kfree(ati_remote);
805	return err;
806}
807
808/*
809 *	ati_remote_disconnect
810 */
811static void ati_remote_disconnect(struct usb_interface *interface)
812{
813	struct ati_remote *ati_remote;
814
815	ati_remote = usb_get_intfdata(interface);
816	usb_set_intfdata(interface, NULL);
817	if (!ati_remote) {
818		warn("%s - null device?\n", __FUNCTION__);
819		return;
820	}
821
822	usb_kill_urb(ati_remote->irq_urb);
823	usb_kill_urb(ati_remote->out_urb);
824	input_unregister_device(ati_remote->idev);
825	ati_remote_free_buffers(ati_remote);
826	kfree(ati_remote);
827}
828
829/*
830 *	ati_remote_init
831 */
832static int __init ati_remote_init(void)
833{
834	int result;
835
836	result = usb_register(&ati_remote_driver);
837	if (result)
838		err("usb_register error #%d\n", result);
839	else
840		info("Registered USB driver " DRIVER_DESC " v. " DRIVER_VERSION);
841
842	return result;
843}
844
845/*
846 *	ati_remote_exit
847 */
848static void __exit ati_remote_exit(void)
849{
850	usb_deregister(&ati_remote_driver);
851}
852
853/*
854 *	module specification
855 */
856
857module_init(ati_remote_init);
858module_exit(ati_remote_exit);
859
860MODULE_AUTHOR(DRIVER_AUTHOR);
861MODULE_DESCRIPTION(DRIVER_DESC);
862MODULE_LICENSE("GPL");
863