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