blob: c12dda73cdd5334bb8c64ea4bb04dd53d3a1f9f3 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * USB ATI Remote support
4 *
Anssi Hannula9d454d42012-04-01 16:41:46 -03005 * Copyright (c) 2011, 2012 Anssi Hannula <anssi.hannula@iki.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Version 2.2.0 Copyright (c) 2004 Torrey Hoffman <thoffman@arnor.net>
7 * Version 2.1.1 Copyright (c) 2002 Vladimir Dergachev
8 *
9 * This 2.2.0 version is a rewrite / cleanup of the 2.1.1 driver, including
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050010 * porting to the 2.6 kernel interfaces, along with other modification
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * to better match the style of the existing usb/input drivers. However, the
12 * protocol and hardware handling is essentially unchanged from 2.1.1.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050013 *
14 * The 2.1.1 driver was derived from the usbati_remote and usbkbd drivers by
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Vojtech Pavlik.
16 *
17 * Changes:
18 *
19 * Feb 2004: Torrey Hoffman <thoffman@arnor.net>
20 * Version 2.2.0
21 * Jun 2004: Torrey Hoffman <thoffman@arnor.net>
22 * Version 2.2.1
23 * Added key repeat support contributed by:
24 * Vincent Vanackere <vanackere@lif.univ-mrs.fr>
25 * Added support for the "Lola" remote contributed by:
26 * Seth Cohn <sethcohn@yahoo.com>
27 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050028 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050030 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
32 * Hardware & software notes
33 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050034 * These remote controls are distributed by ATI as part of their
35 * "All-In-Wonder" video card packages. The receiver self-identifies as a
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * "USB Receiver" with manufacturer "X10 Wireless Technology Inc".
37 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050038 * The "Lola" remote is available from X10. See:
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * http://www.x10.com/products/lola_sg1.htm
40 * The Lola is similar to the ATI remote but has no mouse support, and slightly
41 * different keys.
42 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050043 * It is possible to use multiple receivers and remotes on multiple computers
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * simultaneously by configuring them to use specific channels.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050045 *
46 * The RF protocol used by the remote supports 16 distinct channels, 1 to 16.
47 * Actually, it may even support more, at least in some revisions of the
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * hardware.
49 *
50 * Each remote can be configured to transmit on one channel as follows:
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050051 * - Press and hold the "hand icon" button.
52 * - When the red LED starts to blink, let go of the "hand icon" button.
53 * - When it stops blinking, input the channel code as two digits, from 01
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * to 16, and press the hand icon again.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050055 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * The timing can be a little tricky. Try loading the module with debug=1
57 * to have the kernel print out messages about the remote control number
58 * and mask. Note: debugging prints remote numbers as zero-based hexadecimal.
59 *
60 * The driver has a "channel_mask" parameter. This bitmask specifies which
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050061 * channels will be ignored by the module. To mask out channels, just add
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * all the 2^channel_number values together.
63 *
64 * For instance, set channel_mask = 2^4 = 16 (binary 10000) to make ati_remote
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050065 * ignore signals coming from remote controls transmitting on channel 4, but
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * accept all other channels.
67 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050068 * Or, set channel_mask = 65533, (0xFFFD), and all channels except 1 will be
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * ignored.
70 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050071 * The default is 0 (respond to all channels). Bit 0 and bits 17-32 of this
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * parameter are unused.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/kernel.h>
76#include <linux/errno.h>
77#include <linux/init.h>
78#include <linux/slab.h>
79#include <linux/module.h>
Anssi Hannulac34516e2011-08-06 18:18:08 -030080#include <linux/mutex.h>
David Brownellae0dadc2006-06-13 10:04:34 -070081#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/wait.h>
Marcelo Feitoza Parisif0b80fb2005-12-01 00:50:39 +030083#include <linux/jiffies.h>
Anssi Hannulac34516e2011-08-06 18:18:08 -030084#include <media/rc-core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Module and Version Information, Module Parameters
88 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050089
Jarod Wilson51320882010-05-03 23:30:32 -070090#define ATI_REMOTE_VENDOR_ID 0x0bc7
91#define LOLA_REMOTE_PRODUCT_ID 0x0002
92#define LOLA2_REMOTE_PRODUCT_ID 0x0003
93#define ATI_REMOTE_PRODUCT_ID 0x0004
94#define NVIDIA_REMOTE_PRODUCT_ID 0x0005
95#define MEDION_REMOTE_PRODUCT_ID 0x0006
Anssi Hannula999d6bc2011-08-06 18:18:12 -030096#define FIREFLY_REMOTE_PRODUCT_ID 0x0008
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Anssi Hannula9688efd2011-08-06 18:18:07 -030098#define DRIVER_VERSION "2.2.1"
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#define DRIVER_AUTHOR "Torrey Hoffman <thoffman@arnor.net>"
100#define DRIVER_DESC "ATI/X10 RF USB Remote Control"
101
102#define NAME_BUFSIZE 80 /* size of product name, path buffers */
103#define DATA_BUFSIZE 63 /* size of URB data buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400105/*
106 * Duplicate event filtering time.
107 * Sequential, identical KIND_FILTERED inputs with less than
108 * FILTER_TIME milliseconds between them are considered as repeat
109 * events. The hardware generates 5 events for the first keypress
110 * and we have to take this into account for an accurate repeat
111 * behaviour.
112 */
113#define FILTER_TIME 60 /* msec */
Karl Pickett0de95502007-04-12 01:35:59 -0400114#define REPEAT_DELAY 500 /* msec */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400115
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500116static unsigned long channel_mask;
Dmitry Torokhovc605b672006-08-04 22:53:15 -0400117module_param(channel_mask, ulong, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118MODULE_PARM_DESC(channel_mask, "Bitmask of remote control channels to ignore");
119
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500120static int debug;
Dmitry Torokhovc605b672006-08-04 22:53:15 -0400121module_param(debug, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
123
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400124static int repeat_filter = FILTER_TIME;
125module_param(repeat_filter, int, 0644);
126MODULE_PARM_DESC(repeat_filter, "Repeat filter time, default = 60 msec");
127
Karl Pickett0de95502007-04-12 01:35:59 -0400128static int repeat_delay = REPEAT_DELAY;
129module_param(repeat_delay, int, 0644);
130MODULE_PARM_DESC(repeat_delay, "Delay before sending repeats, default = 500 msec");
131
Anssi Hannulac34516e2011-08-06 18:18:08 -0300132static bool mouse = true;
133module_param(mouse, bool, 0444);
134MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes");
135
Du, Changbina342daea2012-07-07 03:53:28 -0300136#define dbginfo(dev, format, arg...) \
137 do { if (debug) dev_info(dev , format , ## arg); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#undef err
139#define err(format, arg...) printk(KERN_ERR format , ## arg)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500140
Anssi Hannula0d746792012-04-01 16:41:45 -0300141struct ati_receiver_type {
142 /* either default_keymap or get_default_keymap should be set */
143 const char *default_keymap;
144 const char *(*get_default_keymap)(struct usb_interface *interface);
145};
146
Anssi Hannula9d454d42012-04-01 16:41:46 -0300147static const char *get_medion_keymap(struct usb_interface *interface)
148{
149 struct usb_device *udev = interface_to_usbdev(interface);
150
Anssi Hannula5085c992012-05-14 09:52:37 -0300151 /*
152 * There are many different Medion remotes shipped with a receiver
153 * with the same usb id, but the receivers have subtle differences
154 * in the USB descriptors allowing us to detect them.
155 */
156
157 if (udev->manufacturer && udev->product) {
158 if (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP) {
159
160 if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc")
161 && !strcmp(udev->product, "USB Receiver"))
162 return RC_MAP_MEDION_X10_DIGITAINER;
163
164 if (!strcmp(udev->manufacturer, "X10 WTI")
165 && !strcmp(udev->product, "RF receiver"))
166 return RC_MAP_MEDION_X10_OR2X;
167 } else {
168
169 if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc")
170 && !strcmp(udev->product, "USB Receiver"))
171 return RC_MAP_MEDION_X10;
172 }
173 }
174
175 dev_info(&interface->dev,
176 "Unknown Medion X10 receiver, using default ati_remote Medion keymap\n");
Anssi Hannula9d454d42012-04-01 16:41:46 -0300177
178 return RC_MAP_MEDION_X10;
179}
180
Du, Changbina342daea2012-07-07 03:53:28 -0300181static const struct ati_receiver_type type_ati = {
182 .default_keymap = RC_MAP_ATI_X10
183};
184static const struct ati_receiver_type type_medion = {
185 .get_default_keymap = get_medion_keymap
186};
187static const struct ati_receiver_type type_firefly = {
188 .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY
189};
Anssi Hannula0d746792012-04-01 16:41:45 -0300190
Arvind Yadav5fad16b2017-08-13 05:54:44 -0300191static const struct usb_device_id ati_remote_table[] = {
Du, Changbina342daea2012-07-07 03:53:28 -0300192 {
193 USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID),
194 .driver_info = (unsigned long)&type_ati
195 },
196 {
197 USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID),
198 .driver_info = (unsigned long)&type_ati
199 },
200 {
201 USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID),
202 .driver_info = (unsigned long)&type_ati
203 },
204 {
205 USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID),
206 .driver_info = (unsigned long)&type_ati
207 },
208 {
209 USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID),
210 .driver_info = (unsigned long)&type_medion
211 },
212 {
213 USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID),
214 .driver_info = (unsigned long)&type_firefly
215 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 {} /* Terminating entry */
217};
218
219MODULE_DEVICE_TABLE(usb, ati_remote_table);
220
221/* Get hi and low bytes of a 16-bits int */
222#define HI(a) ((unsigned char)((a) >> 8))
223#define LO(a) ((unsigned char)((a) & 0xff))
224
225#define SEND_FLAG_IN_PROGRESS 1
226#define SEND_FLAG_COMPLETE 2
227
228/* Device initialization strings */
229static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
230static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232struct ati_remote {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500233 struct input_dev *idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300234 struct rc_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct usb_device *udev;
236 struct usb_interface *interface;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct urb *irq_urb;
239 struct urb *out_urb;
240 struct usb_endpoint_descriptor *endpoint_in;
241 struct usb_endpoint_descriptor *endpoint_out;
242 unsigned char *inbuf;
243 unsigned char *outbuf;
244 dma_addr_t inbuf_dma;
245 dma_addr_t outbuf_dma;
246
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300247 unsigned char old_data; /* Detect duplicate events */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 unsigned long old_jiffies;
249 unsigned long acc_jiffies; /* handle acceleration */
Karl Pickett0de95502007-04-12 01:35:59 -0400250 unsigned long first_jiffies;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 unsigned int repeat_count;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500253
Anssi Hannulac34516e2011-08-06 18:18:08 -0300254 char rc_name[NAME_BUFSIZE];
255 char rc_phys[NAME_BUFSIZE];
256 char mouse_name[NAME_BUFSIZE];
257 char mouse_phys[NAME_BUFSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 wait_queue_head_t wait;
260 int send_flags;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300261
262 int users; /* 0-2, users are rc and input */
263 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
266/* "Kinds" of messages sent from the hardware to the driver. */
267#define KIND_END 0
George Spelvin3f245b92014-05-11 08:14:54 -0300268#define KIND_LITERAL 1 /* Simply pass to input system as EV_KEY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
George Spelvin3f245b92014-05-11 08:14:54 -0300270#define KIND_ACCEL 3 /* Translate to EV_REL mouse-move events */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272/* Translation table from hardware messages to input events. */
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100273static const struct {
George Spelvin528abb92014-05-11 08:12:55 -0300274 unsigned char kind;
George Spelvin3f245b92014-05-11 08:14:54 -0300275 unsigned char data; /* Raw key code from remote */
276 unsigned short code; /* Input layer translation */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500277} ati_remote_tbl[] = {
George Spelvin8ecd5e32014-05-11 08:14:18 -0300278 /* Directional control pad axes. Code is xxyy */
George Spelvin3f245b92014-05-11 08:14:54 -0300279 {KIND_ACCEL, 0x70, 0xff00}, /* left */
280 {KIND_ACCEL, 0x71, 0x0100}, /* right */
281 {KIND_ACCEL, 0x72, 0x00ff}, /* up */
282 {KIND_ACCEL, 0x73, 0x0001}, /* down */
George Spelvin8ecd5e32014-05-11 08:14:18 -0300283
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500284 /* Directional control pad diagonals */
George Spelvin3f245b92014-05-11 08:14:54 -0300285 {KIND_ACCEL, 0x74, 0xffff}, /* left up */
286 {KIND_ACCEL, 0x75, 0x01ff}, /* right up */
287 {KIND_ACCEL, 0x77, 0xff01}, /* left down */
288 {KIND_ACCEL, 0x76, 0x0101}, /* right down */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
George Spelvin3f245b92014-05-11 08:14:54 -0300290 /* "Mouse button" buttons. The code below uses the fact that the
291 * lsbit of the raw code is a down/up indicator. */
292 {KIND_LITERAL, 0x78, BTN_LEFT}, /* left btn down */
293 {KIND_LITERAL, 0x79, BTN_LEFT}, /* left btn up */
294 {KIND_LITERAL, 0x7c, BTN_RIGHT},/* right btn down */
295 {KIND_LITERAL, 0x7d, BTN_RIGHT},/* right btn up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Mauro Carvalho Chehab04ad3012019-02-18 14:29:01 -0500297 /* Artificial "double-click" events are generated by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * They are mapped to the "side" and "extra" mouse buttons here. */
George Spelvin3f245b92014-05-11 08:14:54 -0300299 {KIND_FILTERED, 0x7a, BTN_SIDE}, /* left dblclick */
300 {KIND_FILTERED, 0x7e, BTN_EXTRA},/* right dblclick */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Anssi Hannulac34516e2011-08-06 18:18:08 -0300302 /* Non-mouse events are handled by rc-core */
George Spelvin3f245b92014-05-11 08:14:54 -0300303 {KIND_END, 0x00, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
Du, Changbina342daea2012-07-07 03:53:28 -0300307 * ati_remote_dump_input
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 */
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700309static void ati_remote_dump(struct device *dev, unsigned char *data,
310 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Anssi Hannula0224e042011-08-06 18:18:10 -0300312 if (len == 1) {
313 if (data[0] != (unsigned char)0xff && data[0] != 0x00)
314 dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
315 } else if (len == 4)
Andy Shevchenko7c94c692012-08-07 12:43:09 -0300316 dev_warn(dev, "Weird key %*ph\n", 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 else
Andy Shevchenko7c94c692012-08-07 12:43:09 -0300318 dev_warn(dev, "Weird data, len=%d %*ph ...\n", len, 6, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321/*
Du, Changbina342daea2012-07-07 03:53:28 -0300322 * ati_remote_open
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300324static int ati_remote_open(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Anssi Hannulac34516e2011-08-06 18:18:08 -0300326 int err = 0;
327
328 mutex_lock(&ati_remote->open_mutex);
329
330 if (ati_remote->users++ != 0)
331 goto out; /* one was already active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* On first open, submit the read urb which was set up previously. */
334 ati_remote->irq_urb->dev = ati_remote->udev;
335 if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500336 dev_err(&ati_remote->interface->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400337 "%s: usb_submit_urb failed!\n", __func__);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300338 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Anssi Hannulac34516e2011-08-06 18:18:08 -0300341out: mutex_unlock(&ati_remote->open_mutex);
342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/*
Du, Changbina342daea2012-07-07 03:53:28 -0300346 * ati_remote_close
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300348static void ati_remote_close(struct ati_remote *ati_remote)
349{
350 mutex_lock(&ati_remote->open_mutex);
351 if (--ati_remote->users == 0)
352 usb_kill_urb(ati_remote->irq_urb);
353 mutex_unlock(&ati_remote->open_mutex);
354}
355
356static int ati_remote_input_open(struct input_dev *inputdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400358 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300359 return ati_remote_open(ati_remote);
360}
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500361
Anssi Hannulac34516e2011-08-06 18:18:08 -0300362static void ati_remote_input_close(struct input_dev *inputdev)
363{
364 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
365 ati_remote_close(ati_remote);
366}
367
368static int ati_remote_rc_open(struct rc_dev *rdev)
369{
370 struct ati_remote *ati_remote = rdev->priv;
371 return ati_remote_open(ati_remote);
372}
373
374static void ati_remote_rc_close(struct rc_dev *rdev)
375{
376 struct ati_remote *ati_remote = rdev->priv;
377 ati_remote_close(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
Du, Changbina342daea2012-07-07 03:53:28 -0300381 * ati_remote_irq_out
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
David Howells7d12e782006-10-05 14:55:46 +0100383static void ati_remote_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct ati_remote *ati_remote = urb->context;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (urb->status) {
388 dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400389 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return;
391 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 ati_remote->send_flags |= SEND_FLAG_COMPLETE;
394 wmb();
395 wake_up(&ati_remote->wait);
396}
397
398/*
Du, Changbina342daea2012-07-07 03:53:28 -0300399 * ati_remote_sendpacket
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500400 *
Du, Changbina342daea2012-07-07 03:53:28 -0300401 * Used to send device initialization strings
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
Du, Changbina342daea2012-07-07 03:53:28 -0300403static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd,
404 unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 int retval = 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* Set up out_urb */
409 memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500410 ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
413 ati_remote->out_urb->dev = ati_remote->udev;
414 ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
415
416 retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
417 if (retval) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500418 dev_dbg(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 "sendpacket: usb_submit_urb failed: %d\n", retval);
420 return retval;
421 }
422
423 wait_event_timeout(ati_remote->wait,
424 ((ati_remote->out_urb->status != -EINPROGRESS) ||
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500425 (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 HZ);
427 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return retval;
430}
431
Mauro Carvalho Chehabfe28f5d2016-02-27 07:51:12 -0300432struct accel_times {
433 const char value;
434 unsigned int msecs;
435};
436
437static const struct accel_times accel[] = {
438 { 1, 125 },
439 { 2, 250 },
440 { 4, 500 },
441 { 6, 1000 },
442 { 9, 1500 },
443 { 13, 2000 },
444 { 20, 0 },
445};
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
Du, Changbina342daea2012-07-07 03:53:28 -0300448 * ati_remote_compute_accel
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400449 *
450 * Implements acceleration curve for directional control pad
451 * If elapsed time since last event is > 1/4 second, user "stopped",
452 * so reset acceleration. Otherwise, user is probably holding the control
453 * pad down, so we increase acceleration, ramping up over two seconds to
454 * a maximum speed.
455 */
456static int ati_remote_compute_accel(struct ati_remote *ati_remote)
457{
Mauro Carvalho Chehabfe28f5d2016-02-27 07:51:12 -0300458 unsigned long now = jiffies, reset_time;
459 int i;
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400460
Mauro Carvalho Chehabfe28f5d2016-02-27 07:51:12 -0300461 reset_time = msecs_to_jiffies(250);
462
463 if (time_after(now, ati_remote->old_jiffies + reset_time)) {
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400464 ati_remote->acc_jiffies = now;
Mauro Carvalho Chehabfe28f5d2016-02-27 07:51:12 -0300465 return 1;
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400466 }
Mauro Carvalho Chehabfe28f5d2016-02-27 07:51:12 -0300467 for (i = 0; i < ARRAY_SIZE(accel) - 1; i++) {
468 unsigned long timeout = msecs_to_jiffies(accel[i].msecs);
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400469
Mauro Carvalho Chehabfe28f5d2016-02-27 07:51:12 -0300470 if (time_before(now, ati_remote->acc_jiffies + timeout))
471 return accel[i].value;
472 }
473 return accel[i].value;
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400474}
475
476/*
Du, Changbina342daea2012-07-07 03:53:28 -0300477 * ati_remote_report_input
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
David Howells7d12e782006-10-05 14:55:46 +0100479static void ati_remote_input_report(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct ati_remote *ati_remote = urb->context;
482 unsigned char *data= ati_remote->inbuf;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500483 struct input_dev *dev = ati_remote->idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300484 int index = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int remote_num;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300486 unsigned char scancode;
Anssi Hannula9d454d42012-04-01 16:41:46 -0300487 u32 wheel_keycode = KEY_RESERVED;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300488 int i;
489
490 /*
491 * data[0] = 0x14
492 * data[1] = data[2] + data[3] + 0xd5 (a checksum byte)
493 * data[2] = the key code (with toggle bit in MSB with some models)
494 * data[3] = channel << 4 (the low 4 bits must be zero)
495 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* Deal with strange looking inputs */
George Spelvin20d7e3c2014-05-11 08:12:09 -0300498 if ( urb->actual_length != 4 || data[0] != 0x14 ||
499 data[1] != (unsigned char)(data[2] + data[3] + 0xD5) ||
500 (data[3] & 0x0f) != 0x00) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700501 ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return;
503 }
504
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300505 if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) {
506 dbginfo(&ati_remote->interface->dev,
Andy Shevchenko7c94c692012-08-07 12:43:09 -0300507 "wrong checksum in input: %*ph\n", 4, data);
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300508 return;
509 }
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Mask unwanted remote channels. */
512 /* note: remote_num is 0-based, channel 1 on remote == 0 here */
513 remote_num = (data[3] >> 4) & 0x0f;
Anssi Hannula9688efd2011-08-06 18:18:07 -0300514 if (channel_mask & (1 << (remote_num + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 dbginfo(&ati_remote->interface->dev,
Mauro Carvalho Chehab25ec5872016-10-18 17:44:25 -0200516 "Masked input from channel 0x%02x: data %02x, mask= 0x%02lx\n",
George Spelvin20d7e3c2014-05-11 08:12:09 -0300517 remote_num, data[2], channel_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return;
519 }
520
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300521 /*
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300522 * MSB is a toggle code, though only used by some devices
523 * (e.g. SnapStream Firefly)
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300524 */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300525 scancode = data[2] & 0x7f;
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300526
Anssi Hannula9d454d42012-04-01 16:41:46 -0300527 dbginfo(&ati_remote->interface->dev,
528 "channel 0x%02x; key data %02x, scancode %02x\n",
529 remote_num, data[2], scancode);
530
531 if (scancode >= 0x70) {
532 /*
533 * This is either a mouse or scrollwheel event, depending on
534 * the remote/keymap.
535 * Get the keycode assigned to scancode 0x78/0x70. If it is
536 * set, assume this is a scrollwheel up/down event.
537 */
538 wheel_keycode = rc_g_keycode_from_table(ati_remote->rdev,
539 scancode & 0x78);
540
541 if (wheel_keycode == KEY_RESERVED) {
542 /* scrollwheel was not mapped, assume mouse */
543
Du, Changbina342daea2012-07-07 03:53:28 -0300544 /* Look up event code index in the mouse translation
545 * table.
546 */
Anssi Hannula9d454d42012-04-01 16:41:46 -0300547 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
548 if (scancode == ati_remote_tbl[i].data) {
549 index = i;
550 break;
551 }
552 }
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300553 }
554 }
Anssi Hannulac34516e2011-08-06 18:18:08 -0300555
Anssi Hannulac34516e2011-08-06 18:18:08 -0300556 if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
George Spelvin3f245b92014-05-11 08:14:54 -0300557 /*
558 * The lsbit of the raw key code is a down/up flag.
559 * Invert it to match the input layer's conventions.
560 */
561 input_event(dev, EV_KEY, ati_remote_tbl[index].code,
562 !(data[2] & 1));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 ati_remote->old_jiffies = jiffies;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500565
George Spelvin8ecd5e32014-05-11 08:14:18 -0300566 } else if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
Karl Pickett0de95502007-04-12 01:35:59 -0400567 unsigned long now = jiffies;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /* Filter duplicate events which happen "too close" together. */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300570 if (ati_remote->old_data == data[2] &&
Karl Pickett0de95502007-04-12 01:35:59 -0400571 time_before(now, ati_remote->old_jiffies +
572 msecs_to_jiffies(repeat_filter))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 ati_remote->repeat_count++;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500574 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ati_remote->repeat_count = 0;
Karl Pickett0de95502007-04-12 01:35:59 -0400576 ati_remote->first_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500578
Karl Pickett0de95502007-04-12 01:35:59 -0400579 ati_remote->old_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
George Spelvin3f245b92014-05-11 08:14:54 -0300581 /* Ensure we skip at least the 4 first duplicate events
582 * (generated by a single keypress), and continue skipping
583 * until repeat_delay msecs have passed.
Karl Pickett0de95502007-04-12 01:35:59 -0400584 */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400585 if (ati_remote->repeat_count > 0 &&
Karl Pickett0de95502007-04-12 01:35:59 -0400586 (ati_remote->repeat_count < 5 ||
587 time_before(now, ati_remote->first_jiffies +
588 msecs_to_jiffies(repeat_delay))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500590
George Spelvin89b0c0d2014-05-11 08:15:44 -0300591 if (index >= 0) {
592 input_event(dev, EV_KEY, ati_remote_tbl[index].code, 1);
593 input_event(dev, EV_KEY, ati_remote_tbl[index].code, 0);
594 } else {
Anssi Hannulac34516e2011-08-06 18:18:08 -0300595 /* Not a mouse event, hand it to rc-core. */
Anssi Hannula9d454d42012-04-01 16:41:46 -0300596 int count = 1;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300597
Anssi Hannula9d454d42012-04-01 16:41:46 -0300598 if (wheel_keycode != KEY_RESERVED) {
599 /*
600 * This is a scrollwheel event, send the
601 * scroll up (0x78) / down (0x70) scancode
602 * repeatedly as many times as indicated by
603 * rest of the scancode.
604 */
605 count = (scancode & 0x07) + 1;
606 scancode &= 0x78;
607 }
608
609 while (count--) {
610 /*
611 * We don't use the rc-core repeat handling yet as
612 * it would cause ghost repeats which would be a
613 * regression for this driver.
614 */
Sean Young6d741bf2017-08-07 16:20:58 -0400615 rc_keydown_notimeout(ati_remote->rdev,
616 RC_PROTO_OTHER,
David Härdeman120703f2014-04-03 20:31:30 -0300617 scancode, data[2]);
Anssi Hannula9d454d42012-04-01 16:41:46 -0300618 rc_keyup(ati_remote->rdev);
619 }
George Spelvin89b0c0d2014-05-11 08:15:44 -0300620 goto nosync;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
George Spelvin8ecd5e32014-05-11 08:14:18 -0300623 } else if (ati_remote_tbl[index].kind == KIND_ACCEL) {
624 signed char dx = ati_remote_tbl[index].code >> 8;
625 signed char dy = ati_remote_tbl[index].code & 255;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500626
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400627 /*
Du, Changbina342daea2012-07-07 03:53:28 -0300628 * Other event kinds are from the directional control pad, and
629 * have an acceleration factor applied to them. Without this
630 * acceleration, the control pad is mostly unusable.
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400631 */
George Spelvin8ecd5e32014-05-11 08:14:18 -0300632 int acc = ati_remote_compute_accel(ati_remote);
633 if (dx)
634 input_report_rel(dev, REL_X, dx * acc);
635 if (dy)
636 input_report_rel(dev, REL_Y, dy * acc);
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400637 ati_remote->old_jiffies = jiffies;
George Spelvin89b0c0d2014-05-11 08:15:44 -0300638
George Spelvin8ecd5e32014-05-11 08:14:18 -0300639 } else {
640 dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
641 ati_remote_tbl[index].kind);
George Spelvin89b0c0d2014-05-11 08:15:44 -0300642 return;
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400643 }
George Spelvin89b0c0d2014-05-11 08:15:44 -0300644 input_sync(dev);
645nosync:
646 ati_remote->old_data = data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649/*
Du, Changbina342daea2012-07-07 03:53:28 -0300650 * ati_remote_irq_in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 */
David Howells7d12e782006-10-05 14:55:46 +0100652static void ati_remote_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct ati_remote *ati_remote = urb->context;
655 int retval;
656
657 switch (urb->status) {
658 case 0: /* success */
David Howells7d12e782006-10-05 14:55:46 +0100659 ati_remote_input_report(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
661 case -ECONNRESET: /* unlink */
662 case -ENOENT:
663 case -ESHUTDOWN:
Du, Changbina342daea2012-07-07 03:53:28 -0300664 dev_dbg(&ati_remote->interface->dev,
665 "%s: urb error status, unlink?\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400666 __func__);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500667 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 default: /* error */
Du, Changbina342daea2012-07-07 03:53:28 -0300669 dev_dbg(&ati_remote->interface->dev,
670 "%s: Nonzero urb status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400671 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500673
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800674 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (retval)
Du, Changbina342daea2012-07-07 03:53:28 -0300676 dev_err(&ati_remote->interface->dev,
677 "%s: usb_submit_urb()=%d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400678 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681/*
Du, Changbina342daea2012-07-07 03:53:28 -0300682 * ati_remote_alloc_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500684static int ati_remote_alloc_buffers(struct usb_device *udev,
685 struct ati_remote *ati_remote)
686{
Daniel Mack997ea582010-04-12 13:17:25 +0200687 ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
688 &ati_remote->inbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500689 if (!ati_remote->inbuf)
690 return -1;
691
Daniel Mack997ea582010-04-12 13:17:25 +0200692 ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
Anssi Hannula9688efd2011-08-06 18:18:07 -0300693 &ati_remote->outbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500694 if (!ati_remote->outbuf)
695 return -1;
696
697 ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
698 if (!ati_remote->irq_urb)
699 return -1;
700
701 ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
702 if (!ati_remote->out_urb)
703 return -1;
704
705 return 0;
706}
707
708/*
Du, Changbina342daea2012-07-07 03:53:28 -0300709 * ati_remote_free_buffers
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500710 */
711static void ati_remote_free_buffers(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Mariusz Kozlowski459f8362006-11-08 15:35:46 +0100713 usb_free_urb(ati_remote->irq_urb);
714 usb_free_urb(ati_remote->out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Daniel Mack997ea582010-04-12 13:17:25 +0200716 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800717 ati_remote->inbuf, ati_remote->inbuf_dma);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500718
Daniel Mack997ea582010-04-12 13:17:25 +0200719 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800720 ati_remote->outbuf, ati_remote->outbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723static void ati_remote_input_init(struct ati_remote *ati_remote)
724{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500725 struct input_dev *idev = ati_remote->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 int i;
727
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700728 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
729 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
730 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
731 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
George Spelvin3f245b92014-05-11 08:14:54 -0300733 if (ati_remote_tbl[i].kind == KIND_LITERAL ||
734 ati_remote_tbl[i].kind == KIND_FILTERED)
George Spelvin1e182702014-05-11 08:16:15 -0300735 __set_bit(ati_remote_tbl[i].code, idev->keybit);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500736
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400737 input_set_drvdata(idev, ati_remote);
738
Anssi Hannulac34516e2011-08-06 18:18:08 -0300739 idev->open = ati_remote_input_open;
740 idev->close = ati_remote_input_close;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500741
Anssi Hannulac34516e2011-08-06 18:18:08 -0300742 idev->name = ati_remote->mouse_name;
743 idev->phys = ati_remote->mouse_phys;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500744
Dmitry Torokhov16a334c2005-06-30 00:49:08 -0500745 usb_to_input_id(ati_remote->udev, &idev->id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300746 idev->dev.parent = &ati_remote->interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Anssi Hannulac34516e2011-08-06 18:18:08 -0300749static void ati_remote_rc_init(struct ati_remote *ati_remote)
750{
751 struct rc_dev *rdev = ati_remote->rdev;
752
753 rdev->priv = ati_remote;
Sean Young6d741bf2017-08-07 16:20:58 -0400754 rdev->allowed_protocols = RC_PROTO_BIT_OTHER;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300755 rdev->driver_name = "ati_remote";
756
757 rdev->open = ati_remote_rc_open;
758 rdev->close = ati_remote_rc_close;
759
Sean Young518f4b22017-07-01 12:13:19 -0400760 rdev->device_name = ati_remote->rc_name;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300761 rdev->input_phys = ati_remote->rc_phys;
762
763 usb_to_input_id(ati_remote->udev, &rdev->input_id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300764 rdev->dev.parent = &ati_remote->interface->dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static int ati_remote_initialize(struct ati_remote *ati_remote)
768{
769 struct usb_device *udev = ati_remote->udev;
770 int pipe, maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 init_waitqueue_head(&ati_remote->wait);
773
774 /* Set up irq_urb */
775 pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
776 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
777 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500778
779 usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
780 maxp, ati_remote_irq_in, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 ati_remote->endpoint_in->bInterval);
782 ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
783 ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* Set up out_urb */
786 pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
787 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
788 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
789
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500790 usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
791 maxp, ati_remote_irq_out, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ati_remote->endpoint_out->bInterval);
793 ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
794 ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
795
796 /* send initialization strings */
797 if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
798 (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500799 dev_err(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 "Initializing ati_remote hardware failed.\n");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500801 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805}
806
807/*
Du, Changbina342daea2012-07-07 03:53:28 -0300808 * ati_remote_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Du, Changbina342daea2012-07-07 03:53:28 -0300810static int ati_remote_probe(struct usb_interface *interface,
811 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct usb_device *udev = interface_to_usbdev(interface);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500814 struct usb_host_interface *iface_host = interface->cur_altsetting;
815 struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
Anssi Hannula0d746792012-04-01 16:41:45 -0300816 struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500817 struct ati_remote *ati_remote;
818 struct input_dev *input_dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300819 struct rc_dev *rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500820 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (iface_host->desc.bNumEndpoints != 2) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400823 err("%s: Unexpected desc.bNumEndpoints\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500824 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500827 endpoint_in = &iface_host->endpoint[0].desc;
828 endpoint_out = &iface_host->endpoint[1].desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Luiz Fernando N. Capitulino96642a22006-09-27 11:58:53 -0700830 if (!usb_endpoint_is_int_in(endpoint_in)) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400831 err("%s: Unexpected endpoint_in\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500832 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500834 if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400835 err("%s: endpoint_in message size==0? \n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500836 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Oliver Neukuma8be8002020-09-16 15:50:51 +0200838 if (!usb_endpoint_is_int_out(endpoint_out)) {
839 err("%s: Unexpected endpoint_out\n", __func__);
840 return -ENODEV;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500843 ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
Andi Shyti0f7499f2016-12-16 06:50:58 -0200844 rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300845 if (!ati_remote || !rc_dev)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300846 goto exit_free_dev_rdev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /* Allocate URB buffers, URBs */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500849 if (ati_remote_alloc_buffers(udev, ati_remote))
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300850 goto exit_free_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500852 ati_remote->endpoint_in = endpoint_in;
853 ati_remote->endpoint_out = endpoint_out;
854 ati_remote->udev = udev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300855 ati_remote->rdev = rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500856 ati_remote->interface = interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Anssi Hannulac34516e2011-08-06 18:18:08 -0300858 usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -0400859 strscpy(ati_remote->mouse_phys, ati_remote->rc_phys,
Anssi Hannulac34516e2011-08-06 18:18:08 -0300860 sizeof(ati_remote->mouse_phys));
861
862 strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
863 strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Rasmus Villemoes869f0762016-03-08 17:40:53 -0300865 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name), "%s%s%s",
866 udev->manufacturer ?: "",
867 udev->manufacturer && udev->product ? " " : "",
868 udev->product ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Anssi Hannulac34516e2011-08-06 18:18:08 -0300870 if (!strlen(ati_remote->rc_name))
871 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500872 DRIVER_DESC "(%04x,%04x)",
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500873 le16_to_cpu(ati_remote->udev->descriptor.idVendor),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 le16_to_cpu(ati_remote->udev->descriptor.idProduct));
875
Anssi Hannulac34516e2011-08-06 18:18:08 -0300876 snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
877 "%s mouse", ati_remote->rc_name);
878
Anssi Hannula0d746792012-04-01 16:41:45 -0300879 rc_dev->map_name = RC_MAP_ATI_X10; /* default map */
880
881 /* set default keymap according to receiver model */
882 if (type) {
883 if (type->default_keymap)
884 rc_dev->map_name = type->default_keymap;
885 else if (type->get_default_keymap)
886 rc_dev->map_name = type->get_default_keymap(interface);
887 }
Anssi Hannula175fcec2011-08-06 18:18:11 -0300888
Anssi Hannulac34516e2011-08-06 18:18:08 -0300889 ati_remote_rc_init(ati_remote);
890 mutex_init(&ati_remote->open_mutex);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500893 err = ati_remote_initialize(ati_remote);
894 if (err)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300895 goto exit_kill_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Anssi Hannulac34516e2011-08-06 18:18:08 -0300897 /* Set up and register rc device */
898 err = rc_register_device(ati_remote->rdev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400899 if (err)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300900 goto exit_kill_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Anssi Hannulac34516e2011-08-06 18:18:08 -0300902 /* Set up and register mouse input device */
903 if (mouse) {
904 input_dev = input_allocate_device();
Peter Senna Tschudina84b17e2012-09-04 08:05:04 -0300905 if (!input_dev) {
906 err = -ENOMEM;
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300907 goto exit_unregister_device;
Peter Senna Tschudina84b17e2012-09-04 08:05:04 -0300908 }
Anssi Hannulac34516e2011-08-06 18:18:08 -0300909
910 ati_remote->idev = input_dev;
911 ati_remote_input_init(ati_remote);
912 err = input_register_device(input_dev);
913
914 if (err)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300915 goto exit_free_input_device;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300916 }
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 usb_set_intfdata(interface, ati_remote);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500919 return 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500920
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300921 exit_free_input_device:
922 input_free_device(input_dev);
923 exit_unregister_device:
924 rc_unregister_device(rc_dev);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300925 rc_dev = NULL;
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300926 exit_kill_urbs:
927 usb_kill_urb(ati_remote->irq_urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500928 usb_kill_urb(ati_remote->out_urb);
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300929 exit_free_buffers:
930 ati_remote_free_buffers(ati_remote);
931 exit_free_dev_rdev:
932 rc_free_device(rc_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500933 kfree(ati_remote);
934 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937/*
Du, Changbina342daea2012-07-07 03:53:28 -0300938 * ati_remote_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
940static void ati_remote_disconnect(struct usb_interface *interface)
941{
942 struct ati_remote *ati_remote;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ati_remote = usb_get_intfdata(interface);
945 usb_set_intfdata(interface, NULL);
946 if (!ati_remote) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700947 dev_warn(&interface->dev, "%s - null device?\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return;
949 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500950
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500951 usb_kill_urb(ati_remote->irq_urb);
952 usb_kill_urb(ati_remote->out_urb);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300953 if (ati_remote->idev)
954 input_unregister_device(ati_remote->idev);
955 rc_unregister_device(ati_remote->rdev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500956 ati_remote_free_buffers(ati_remote);
957 kfree(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Du, Changbina342daea2012-07-07 03:53:28 -0300960/* usb specific object to register with the usb subsystem */
961static struct usb_driver ati_remote_driver = {
962 .name = "ati_remote",
963 .probe = ati_remote_probe,
964 .disconnect = ati_remote_disconnect,
965 .id_table = ati_remote_table,
966};
967
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800968module_usb_driver(ati_remote_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970MODULE_AUTHOR(DRIVER_AUTHOR);
971MODULE_DESCRIPTION(DRIVER_DESC);
972MODULE_LICENSE("GPL");