blob: a35631891cc00589cbaa1ef8d6ef90026cbe19d3 [file] [log] [blame]
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * USB ATI Remote support
3 *
Anssi Hannula9d454d42012-04-01 16:41:46 -03004 * Copyright (c) 2011, 2012 Anssi Hannula <anssi.hannula@iki.fi>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -05009 * porting to the 2.6 kernel interfaces, along with other modification
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050012 *
13 * The 2.1.1 driver was derived from the usbati_remote and usbkbd drivers by
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * 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 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050027 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050031 * the Free Software Foundation; either version 2 of the License, or
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * (at your option) any later version.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050033 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * 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.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050038 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * 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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050042 *
43 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 * Hardware & software notes
46 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050047 * These remote controls are distributed by ATI as part of their
48 * "All-In-Wonder" video card packages. The receiver self-identifies as a
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * "USB Receiver" with manufacturer "X10 Wireless Technology Inc".
50 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050051 * The "Lola" remote is available from X10. See:
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * 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 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050056 * It is possible to use multiple receivers and remotes on multiple computers
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * simultaneously by configuring them to use specific channels.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050058 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * hardware.
62 *
63 * Each remote can be configured to transmit on one channel as follows:
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050064 * - 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * to 16, and press the hand icon again.
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 * 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
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050074 * channels will be ignored by the module. To mask out channels, just add
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * all the 2^channel_number values together.
76 *
77 * For instance, set channel_mask = 2^4 = 16 (binary 10000) to make ati_remote
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050078 * ignore signals coming from remote controls transmitting on channel 4, but
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * accept all other channels.
80 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050081 * Or, set channel_mask = 65533, (0xFFFD), and all channels except 1 will be
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * ignored.
83 *
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -050084 * The default is 0 (respond to all channels). Bit 0 and bits 17-32 of this
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * parameter are unused.
86 *
87 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/kernel.h>
90#include <linux/errno.h>
91#include <linux/init.h>
92#include <linux/slab.h>
93#include <linux/module.h>
Anssi Hannulac34516e2011-08-06 18:18:08 -030094#include <linux/mutex.h>
David Brownellae0dadc2006-06-13 10:04:34 -070095#include <linux/usb/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/wait.h>
Marcelo Feitoza Parisif0b80fb2005-12-01 00:50:39 +030097#include <linux/jiffies.h>
Anssi Hannulac34516e2011-08-06 18:18:08 -030098#include <media/rc-core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
101 * Module and Version Information, Module Parameters
102 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500103
Jarod Wilson51320882010-05-03 23:30:32 -0700104#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
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300110#define FIREFLY_REMOTE_PRODUCT_ID 0x0008
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Anssi Hannula9688efd2011-08-06 18:18:07 -0300112#define DRIVER_VERSION "2.2.1"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400119/*
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 */
Karl Pickett0de95502007-04-12 01:35:59 -0400128#define REPEAT_DELAY 500 /* msec */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400129
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500130static unsigned long channel_mask;
Dmitry Torokhovc605b672006-08-04 22:53:15 -0400131module_param(channel_mask, ulong, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132MODULE_PARM_DESC(channel_mask, "Bitmask of remote control channels to ignore");
133
Dmitry Torokhov65cde542005-05-29 02:29:38 -0500134static int debug;
Dmitry Torokhovc605b672006-08-04 22:53:15 -0400135module_param(debug, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
137
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400138static int repeat_filter = FILTER_TIME;
139module_param(repeat_filter, int, 0644);
140MODULE_PARM_DESC(repeat_filter, "Repeat filter time, default = 60 msec");
141
Karl Pickett0de95502007-04-12 01:35:59 -0400142static int repeat_delay = REPEAT_DELAY;
143module_param(repeat_delay, int, 0644);
144MODULE_PARM_DESC(repeat_delay, "Delay before sending repeats, default = 500 msec");
145
Anssi Hannulac34516e2011-08-06 18:18:08 -0300146static bool mouse = true;
147module_param(mouse, bool, 0444);
148MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes");
149
Du, Changbina342dae2012-07-07 03:53:28 -0300150#define dbginfo(dev, format, arg...) \
151 do { if (debug) dev_info(dev , format , ## arg); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#undef err
153#define err(format, arg...) printk(KERN_ERR format , ## arg)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500154
Anssi Hannula0d746792012-04-01 16:41:45 -0300155struct 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
Anssi Hannula9d454d42012-04-01 16:41:46 -0300161static const char *get_medion_keymap(struct usb_interface *interface)
162{
163 struct usb_device *udev = interface_to_usbdev(interface);
164
Anssi Hannula5085c992012-05-14 09:52:37 -0300165 /*
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");
Anssi Hannula9d454d42012-04-01 16:41:46 -0300191
192 return RC_MAP_MEDION_X10;
193}
194
Du, Changbina342dae2012-07-07 03:53:28 -0300195static const struct ati_receiver_type type_ati = {
196 .default_keymap = RC_MAP_ATI_X10
197};
198static const struct ati_receiver_type type_medion = {
199 .get_default_keymap = get_medion_keymap
200};
201static const struct ati_receiver_type type_firefly = {
202 .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY
203};
Anssi Hannula0d746792012-04-01 16:41:45 -0300204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static struct usb_device_id ati_remote_table[] = {
Du, Changbina342dae2012-07-07 03:53:28 -0300206 {
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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 {} /* Terminating entry */
231};
232
233MODULE_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 */
243static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
244static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246struct ati_remote {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500247 struct input_dev *idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300248 struct rc_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct usb_device *udev;
250 struct usb_interface *interface;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 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
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300261 unsigned char old_data; /* Detect duplicate events */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned long old_jiffies;
263 unsigned long acc_jiffies; /* handle acceleration */
Karl Pickett0de95502007-04-12 01:35:59 -0400264 unsigned long first_jiffies;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 unsigned int repeat_count;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500267
Anssi Hannulac34516e2011-08-06 18:18:08 -0300268 char rc_name[NAME_BUFSIZE];
269 char rc_phys[NAME_BUFSIZE];
270 char mouse_name[NAME_BUFSIZE];
271 char mouse_phys[NAME_BUFSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 wait_queue_head_t wait;
274 int send_flags;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300275
276 int users; /* 0-2, users are rc and input */
277 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
280/* "Kinds" of messages sent from the hardware to the driver. */
281#define KIND_END 0
George Spelvin3f245b92014-05-11 08:14:54 -0300282#define KIND_LITERAL 1 /* Simply pass to input system as EV_KEY */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
George Spelvin3f245b92014-05-11 08:14:54 -0300284#define KIND_ACCEL 3 /* Translate to EV_REL mouse-move events */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286/* Translation table from hardware messages to input events. */
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100287static const struct {
George Spelvin528abb92014-05-11 08:12:55 -0300288 unsigned char kind;
George Spelvin3f245b92014-05-11 08:14:54 -0300289 unsigned char data; /* Raw key code from remote */
290 unsigned short code; /* Input layer translation */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500291} ati_remote_tbl[] = {
George Spelvin8ecd5e32014-05-11 08:14:18 -0300292 /* Directional control pad axes. Code is xxyy */
George Spelvin3f245b92014-05-11 08:14:54 -0300293 {KIND_ACCEL, 0x70, 0xff00}, /* left */
294 {KIND_ACCEL, 0x71, 0x0100}, /* right */
295 {KIND_ACCEL, 0x72, 0x00ff}, /* up */
296 {KIND_ACCEL, 0x73, 0x0001}, /* down */
George Spelvin8ecd5e32014-05-11 08:14:18 -0300297
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500298 /* Directional control pad diagonals */
George Spelvin3f245b92014-05-11 08:14:54 -0300299 {KIND_ACCEL, 0x74, 0xffff}, /* left up */
300 {KIND_ACCEL, 0x75, 0x01ff}, /* right up */
301 {KIND_ACCEL, 0x77, 0xff01}, /* left down */
302 {KIND_ACCEL, 0x76, 0x0101}, /* right down */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
George Spelvin3f245b92014-05-11 08:14:54 -0300304 /* "Mouse button" buttons. The code below uses the fact that the
305 * lsbit of the raw code is a down/up indicator. */
306 {KIND_LITERAL, 0x78, BTN_LEFT}, /* left btn down */
307 {KIND_LITERAL, 0x79, BTN_LEFT}, /* left btn up */
308 {KIND_LITERAL, 0x7c, BTN_RIGHT},/* right btn down */
309 {KIND_LITERAL, 0x7d, BTN_RIGHT},/* right btn up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500311 /* Artificial "doubleclick" events are generated by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * They are mapped to the "side" and "extra" mouse buttons here. */
George Spelvin3f245b92014-05-11 08:14:54 -0300313 {KIND_FILTERED, 0x7a, BTN_SIDE}, /* left dblclick */
314 {KIND_FILTERED, 0x7e, BTN_EXTRA},/* right dblclick */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Anssi Hannulac34516e2011-08-06 18:18:08 -0300316 /* Non-mouse events are handled by rc-core */
George Spelvin3f245b92014-05-11 08:14:54 -0300317 {KIND_END, 0x00, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/*
Du, Changbina342dae2012-07-07 03:53:28 -0300321 * ati_remote_dump_input
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700323static void ati_remote_dump(struct device *dev, unsigned char *data,
324 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Anssi Hannula0224e042011-08-06 18:18:10 -0300326 if (len == 1) {
327 if (data[0] != (unsigned char)0xff && data[0] != 0x00)
328 dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
329 } else if (len == 4)
Andy Shevchenko7c94c692012-08-07 12:43:09 -0300330 dev_warn(dev, "Weird key %*ph\n", 4, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 else
Andy Shevchenko7c94c692012-08-07 12:43:09 -0300332 dev_warn(dev, "Weird data, len=%d %*ph ...\n", len, 6, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335/*
Du, Changbina342dae2012-07-07 03:53:28 -0300336 * ati_remote_open
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300338static int ati_remote_open(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Anssi Hannulac34516e2011-08-06 18:18:08 -0300340 int err = 0;
341
342 mutex_lock(&ati_remote->open_mutex);
343
344 if (ati_remote->users++ != 0)
345 goto out; /* one was already active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* On first open, submit the read urb which was set up previously. */
348 ati_remote->irq_urb->dev = ati_remote->udev;
349 if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500350 dev_err(&ati_remote->interface->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400351 "%s: usb_submit_urb failed!\n", __func__);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300352 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Anssi Hannulac34516e2011-08-06 18:18:08 -0300355out: mutex_unlock(&ati_remote->open_mutex);
356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*
Du, Changbina342dae2012-07-07 03:53:28 -0300360 * ati_remote_close
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300362static void ati_remote_close(struct ati_remote *ati_remote)
363{
364 mutex_lock(&ati_remote->open_mutex);
365 if (--ati_remote->users == 0)
366 usb_kill_urb(ati_remote->irq_urb);
367 mutex_unlock(&ati_remote->open_mutex);
368}
369
370static int ati_remote_input_open(struct input_dev *inputdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400372 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300373 return ati_remote_open(ati_remote);
374}
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500375
Anssi Hannulac34516e2011-08-06 18:18:08 -0300376static void ati_remote_input_close(struct input_dev *inputdev)
377{
378 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
379 ati_remote_close(ati_remote);
380}
381
382static int ati_remote_rc_open(struct rc_dev *rdev)
383{
384 struct ati_remote *ati_remote = rdev->priv;
385 return ati_remote_open(ati_remote);
386}
387
388static void ati_remote_rc_close(struct rc_dev *rdev)
389{
390 struct ati_remote *ati_remote = rdev->priv;
391 ati_remote_close(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
Du, Changbina342dae2012-07-07 03:53:28 -0300395 * ati_remote_irq_out
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
David Howells7d12e782006-10-05 14:55:46 +0100397static void ati_remote_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 struct ati_remote *ati_remote = urb->context;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (urb->status) {
402 dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400403 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return;
405 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ati_remote->send_flags |= SEND_FLAG_COMPLETE;
408 wmb();
409 wake_up(&ati_remote->wait);
410}
411
412/*
Du, Changbina342dae2012-07-07 03:53:28 -0300413 * ati_remote_sendpacket
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500414 *
Du, Changbina342dae2012-07-07 03:53:28 -0300415 * Used to send device initialization strings
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Du, Changbina342dae2012-07-07 03:53:28 -0300417static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd,
418 unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 int retval = 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* Set up out_urb */
423 memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500424 ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
427 ati_remote->out_urb->dev = ati_remote->udev;
428 ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
429
430 retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
431 if (retval) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500432 dev_dbg(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 "sendpacket: usb_submit_urb failed: %d\n", retval);
434 return retval;
435 }
436
437 wait_event_timeout(ati_remote->wait,
438 ((ati_remote->out_urb->status != -EINPROGRESS) ||
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500439 (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 HZ);
441 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return retval;
444}
445
446/*
Du, Changbina342dae2012-07-07 03:53:28 -0300447 * ati_remote_compute_accel
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400448 *
449 * Implements acceleration curve for directional control pad
450 * If elapsed time since last event is > 1/4 second, user "stopped",
451 * so reset acceleration. Otherwise, user is probably holding the control
452 * pad down, so we increase acceleration, ramping up over two seconds to
453 * a maximum speed.
454 */
455static int ati_remote_compute_accel(struct ati_remote *ati_remote)
456{
457 static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
458 unsigned long now = jiffies;
459 int acc;
460
461 if (time_after(now, ati_remote->old_jiffies + msecs_to_jiffies(250))) {
462 acc = 1;
463 ati_remote->acc_jiffies = now;
464 }
465 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(125)))
466 acc = accel[0];
467 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(250)))
468 acc = accel[1];
469 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(500)))
470 acc = accel[2];
471 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1000)))
472 acc = accel[3];
473 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1500)))
474 acc = accel[4];
475 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(2000)))
476 acc = accel[5];
477 else
478 acc = accel[6];
479
480 return acc;
481}
482
483/*
Du, Changbina342dae2012-07-07 03:53:28 -0300484 * ati_remote_report_input
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
David Howells7d12e782006-10-05 14:55:46 +0100486static void ati_remote_input_report(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct ati_remote *ati_remote = urb->context;
489 unsigned char *data= ati_remote->inbuf;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500490 struct input_dev *dev = ati_remote->idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300491 int index = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int remote_num;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300493 unsigned char scancode;
Anssi Hannula9d454d42012-04-01 16:41:46 -0300494 u32 wheel_keycode = KEY_RESERVED;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300495 int i;
496
497 /*
498 * data[0] = 0x14
499 * data[1] = data[2] + data[3] + 0xd5 (a checksum byte)
500 * data[2] = the key code (with toggle bit in MSB with some models)
501 * data[3] = channel << 4 (the low 4 bits must be zero)
502 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Deal with strange looking inputs */
George Spelvin20d7e3c2014-05-11 08:12:09 -0300505 if ( urb->actual_length != 4 || data[0] != 0x14 ||
506 data[1] != (unsigned char)(data[2] + data[3] + 0xD5) ||
507 (data[3] & 0x0f) != 0x00) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700508 ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return;
510 }
511
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300512 if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) {
513 dbginfo(&ati_remote->interface->dev,
Andy Shevchenko7c94c692012-08-07 12:43:09 -0300514 "wrong checksum in input: %*ph\n", 4, data);
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300515 return;
516 }
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* Mask unwanted remote channels. */
519 /* note: remote_num is 0-based, channel 1 on remote == 0 here */
520 remote_num = (data[3] >> 4) & 0x0f;
Anssi Hannula9688efd2011-08-06 18:18:07 -0300521 if (channel_mask & (1 << (remote_num + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 dbginfo(&ati_remote->interface->dev,
George Spelvin20d7e3c2014-05-11 08:12:09 -0300523 "Masked input from channel 0x%02x: data %02x, "
Du, Changbina342dae2012-07-07 03:53:28 -0300524 "mask= 0x%02lx\n",
George Spelvin20d7e3c2014-05-11 08:12:09 -0300525 remote_num, data[2], channel_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return;
527 }
528
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300529 /*
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300530 * MSB is a toggle code, though only used by some devices
531 * (e.g. SnapStream Firefly)
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300532 */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300533 scancode = data[2] & 0x7f;
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300534
Anssi Hannula9d454d42012-04-01 16:41:46 -0300535 dbginfo(&ati_remote->interface->dev,
536 "channel 0x%02x; key data %02x, scancode %02x\n",
537 remote_num, data[2], scancode);
538
539 if (scancode >= 0x70) {
540 /*
541 * This is either a mouse or scrollwheel event, depending on
542 * the remote/keymap.
543 * Get the keycode assigned to scancode 0x78/0x70. If it is
544 * set, assume this is a scrollwheel up/down event.
545 */
546 wheel_keycode = rc_g_keycode_from_table(ati_remote->rdev,
547 scancode & 0x78);
548
549 if (wheel_keycode == KEY_RESERVED) {
550 /* scrollwheel was not mapped, assume mouse */
551
Du, Changbina342dae2012-07-07 03:53:28 -0300552 /* Look up event code index in the mouse translation
553 * table.
554 */
Anssi Hannula9d454d42012-04-01 16:41:46 -0300555 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
556 if (scancode == ati_remote_tbl[i].data) {
557 index = i;
558 break;
559 }
560 }
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300561 }
562 }
Anssi Hannulac34516e2011-08-06 18:18:08 -0300563
Anssi Hannulac34516e2011-08-06 18:18:08 -0300564 if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
George Spelvin3f245b92014-05-11 08:14:54 -0300565 /*
566 * The lsbit of the raw key code is a down/up flag.
567 * Invert it to match the input layer's conventions.
568 */
569 input_event(dev, EV_KEY, ati_remote_tbl[index].code,
570 !(data[2] & 1));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ati_remote->old_jiffies = jiffies;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500573
George Spelvin8ecd5e32014-05-11 08:14:18 -0300574 } else if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
Karl Pickett0de95502007-04-12 01:35:59 -0400575 unsigned long now = jiffies;
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* Filter duplicate events which happen "too close" together. */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300578 if (ati_remote->old_data == data[2] &&
Karl Pickett0de95502007-04-12 01:35:59 -0400579 time_before(now, ati_remote->old_jiffies +
580 msecs_to_jiffies(repeat_filter))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ati_remote->repeat_count++;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500582 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 ati_remote->repeat_count = 0;
Karl Pickett0de95502007-04-12 01:35:59 -0400584 ati_remote->first_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500586
Karl Pickett0de95502007-04-12 01:35:59 -0400587 ati_remote->old_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
George Spelvin3f245b92014-05-11 08:14:54 -0300589 /* Ensure we skip at least the 4 first duplicate events
590 * (generated by a single keypress), and continue skipping
591 * until repeat_delay msecs have passed.
Karl Pickett0de95502007-04-12 01:35:59 -0400592 */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400593 if (ati_remote->repeat_count > 0 &&
Karl Pickett0de95502007-04-12 01:35:59 -0400594 (ati_remote->repeat_count < 5 ||
595 time_before(now, ati_remote->first_jiffies +
596 msecs_to_jiffies(repeat_delay))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500598
George Spelvin89b0c0d2014-05-11 08:15:44 -0300599 if (index >= 0) {
600 input_event(dev, EV_KEY, ati_remote_tbl[index].code, 1);
601 input_event(dev, EV_KEY, ati_remote_tbl[index].code, 0);
602 } else {
Anssi Hannulac34516e2011-08-06 18:18:08 -0300603 /* Not a mouse event, hand it to rc-core. */
Anssi Hannula9d454d42012-04-01 16:41:46 -0300604 int count = 1;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300605
Anssi Hannula9d454d42012-04-01 16:41:46 -0300606 if (wheel_keycode != KEY_RESERVED) {
607 /*
608 * This is a scrollwheel event, send the
609 * scroll up (0x78) / down (0x70) scancode
610 * repeatedly as many times as indicated by
611 * rest of the scancode.
612 */
613 count = (scancode & 0x07) + 1;
614 scancode &= 0x78;
615 }
616
617 while (count--) {
618 /*
619 * We don't use the rc-core repeat handling yet as
620 * it would cause ghost repeats which would be a
621 * regression for this driver.
622 */
David Härdeman120703f2014-04-03 20:31:30 -0300623 rc_keydown_notimeout(ati_remote->rdev, RC_TYPE_OTHER,
624 scancode, data[2]);
Anssi Hannula9d454d42012-04-01 16:41:46 -0300625 rc_keyup(ati_remote->rdev);
626 }
George Spelvin89b0c0d2014-05-11 08:15:44 -0300627 goto nosync;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
George Spelvin8ecd5e32014-05-11 08:14:18 -0300630 } else if (ati_remote_tbl[index].kind == KIND_ACCEL) {
631 signed char dx = ati_remote_tbl[index].code >> 8;
632 signed char dy = ati_remote_tbl[index].code & 255;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500633
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400634 /*
Du, Changbina342dae2012-07-07 03:53:28 -0300635 * Other event kinds are from the directional control pad, and
636 * have an acceleration factor applied to them. Without this
637 * acceleration, the control pad is mostly unusable.
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400638 */
George Spelvin8ecd5e32014-05-11 08:14:18 -0300639 int acc = ati_remote_compute_accel(ati_remote);
640 if (dx)
641 input_report_rel(dev, REL_X, dx * acc);
642 if (dy)
643 input_report_rel(dev, REL_Y, dy * acc);
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400644 ati_remote->old_jiffies = jiffies;
George Spelvin89b0c0d2014-05-11 08:15:44 -0300645
George Spelvin8ecd5e32014-05-11 08:14:18 -0300646 } else {
647 dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
648 ati_remote_tbl[index].kind);
George Spelvin89b0c0d2014-05-11 08:15:44 -0300649 return;
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400650 }
George Spelvin89b0c0d2014-05-11 08:15:44 -0300651 input_sync(dev);
652nosync:
653 ati_remote->old_data = data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
Du, Changbina342dae2012-07-07 03:53:28 -0300657 * ati_remote_irq_in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
David Howells7d12e782006-10-05 14:55:46 +0100659static void ati_remote_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
661 struct ati_remote *ati_remote = urb->context;
662 int retval;
663
664 switch (urb->status) {
665 case 0: /* success */
David Howells7d12e782006-10-05 14:55:46 +0100666 ati_remote_input_report(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 break;
668 case -ECONNRESET: /* unlink */
669 case -ENOENT:
670 case -ESHUTDOWN:
Du, Changbina342dae2012-07-07 03:53:28 -0300671 dev_dbg(&ati_remote->interface->dev,
672 "%s: urb error status, unlink?\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400673 __func__);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500674 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 default: /* error */
Du, Changbina342dae2012-07-07 03:53:28 -0300676 dev_dbg(&ati_remote->interface->dev,
677 "%s: Nonzero urb status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400678 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500680
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800681 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (retval)
Du, Changbina342dae2012-07-07 03:53:28 -0300683 dev_err(&ati_remote->interface->dev,
684 "%s: usb_submit_urb()=%d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400685 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688/*
Du, Changbina342dae2012-07-07 03:53:28 -0300689 * ati_remote_alloc_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500691static int ati_remote_alloc_buffers(struct usb_device *udev,
692 struct ati_remote *ati_remote)
693{
Daniel Mack997ea582010-04-12 13:17:25 +0200694 ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
695 &ati_remote->inbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500696 if (!ati_remote->inbuf)
697 return -1;
698
Daniel Mack997ea582010-04-12 13:17:25 +0200699 ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
Anssi Hannula9688efd2011-08-06 18:18:07 -0300700 &ati_remote->outbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500701 if (!ati_remote->outbuf)
702 return -1;
703
704 ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
705 if (!ati_remote->irq_urb)
706 return -1;
707
708 ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
709 if (!ati_remote->out_urb)
710 return -1;
711
712 return 0;
713}
714
715/*
Du, Changbina342dae2012-07-07 03:53:28 -0300716 * ati_remote_free_buffers
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500717 */
718static void ati_remote_free_buffers(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Mariusz Kozlowski459f8362006-11-08 15:35:46 +0100720 usb_free_urb(ati_remote->irq_urb);
721 usb_free_urb(ati_remote->out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Daniel Mack997ea582010-04-12 13:17:25 +0200723 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800724 ati_remote->inbuf, ati_remote->inbuf_dma);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500725
Daniel Mack997ea582010-04-12 13:17:25 +0200726 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800727 ati_remote->outbuf, ati_remote->outbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
730static void ati_remote_input_init(struct ati_remote *ati_remote)
731{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500732 struct input_dev *idev = ati_remote->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int i;
734
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700735 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
736 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
737 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
738 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
George Spelvin3f245b92014-05-11 08:14:54 -0300740 if (ati_remote_tbl[i].kind == KIND_LITERAL ||
741 ati_remote_tbl[i].kind == KIND_FILTERED)
George Spelvin1e182702014-05-11 08:16:15 -0300742 __set_bit(ati_remote_tbl[i].code, idev->keybit);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500743
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400744 input_set_drvdata(idev, ati_remote);
745
Anssi Hannulac34516e2011-08-06 18:18:08 -0300746 idev->open = ati_remote_input_open;
747 idev->close = ati_remote_input_close;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500748
Anssi Hannulac34516e2011-08-06 18:18:08 -0300749 idev->name = ati_remote->mouse_name;
750 idev->phys = ati_remote->mouse_phys;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500751
Dmitry Torokhov16a334c2005-06-30 00:49:08 -0500752 usb_to_input_id(ati_remote->udev, &idev->id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300753 idev->dev.parent = &ati_remote->interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Anssi Hannulac34516e2011-08-06 18:18:08 -0300756static void ati_remote_rc_init(struct ati_remote *ati_remote)
757{
758 struct rc_dev *rdev = ati_remote->rdev;
759
760 rdev->priv = ati_remote;
761 rdev->driver_type = RC_DRIVER_SCANCODE;
David Härdemanc5540fb2014-04-03 20:32:21 -0300762 rdev->allowed_protocols = RC_BIT_OTHER;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300763 rdev->driver_name = "ati_remote";
764
765 rdev->open = ati_remote_rc_open;
766 rdev->close = ati_remote_rc_close;
767
768 rdev->input_name = ati_remote->rc_name;
769 rdev->input_phys = ati_remote->rc_phys;
770
771 usb_to_input_id(ati_remote->udev, &rdev->input_id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300772 rdev->dev.parent = &ati_remote->interface->dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static int ati_remote_initialize(struct ati_remote *ati_remote)
776{
777 struct usb_device *udev = ati_remote->udev;
778 int pipe, maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 init_waitqueue_head(&ati_remote->wait);
781
782 /* Set up irq_urb */
783 pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
784 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
785 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500786
787 usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
788 maxp, ati_remote_irq_in, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 ati_remote->endpoint_in->bInterval);
790 ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
791 ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* Set up out_urb */
794 pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
795 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
796 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
797
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500798 usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
799 maxp, ati_remote_irq_out, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 ati_remote->endpoint_out->bInterval);
801 ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
802 ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
803
804 /* send initialization strings */
805 if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
806 (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500807 dev_err(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 "Initializing ati_remote hardware failed.\n");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500809 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 0;
813}
814
815/*
Du, Changbina342dae2012-07-07 03:53:28 -0300816 * ati_remote_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Du, Changbina342dae2012-07-07 03:53:28 -0300818static int ati_remote_probe(struct usb_interface *interface,
819 const struct usb_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct usb_device *udev = interface_to_usbdev(interface);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500822 struct usb_host_interface *iface_host = interface->cur_altsetting;
823 struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
Anssi Hannula0d746792012-04-01 16:41:45 -0300824 struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500825 struct ati_remote *ati_remote;
826 struct input_dev *input_dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300827 struct rc_dev *rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500828 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (iface_host->desc.bNumEndpoints != 2) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400831 err("%s: Unexpected desc.bNumEndpoints\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500832 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500835 endpoint_in = &iface_host->endpoint[0].desc;
836 endpoint_out = &iface_host->endpoint[1].desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Luiz Fernando N. Capitulino96642a22006-09-27 11:58:53 -0700838 if (!usb_endpoint_is_int_in(endpoint_in)) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400839 err("%s: Unexpected endpoint_in\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500840 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500842 if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400843 err("%s: endpoint_in message size==0? \n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500844 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500847 ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300848 rc_dev = rc_allocate_device();
849 if (!ati_remote || !rc_dev)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300850 goto exit_free_dev_rdev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* Allocate URB buffers, URBs */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500853 if (ati_remote_alloc_buffers(udev, ati_remote))
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300854 goto exit_free_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500856 ati_remote->endpoint_in = endpoint_in;
857 ati_remote->endpoint_out = endpoint_out;
858 ati_remote->udev = udev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300859 ati_remote->rdev = rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500860 ati_remote->interface = interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Anssi Hannulac34516e2011-08-06 18:18:08 -0300862 usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
863 strlcpy(ati_remote->mouse_phys, ati_remote->rc_phys,
864 sizeof(ati_remote->mouse_phys));
865
866 strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
867 strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (udev->manufacturer)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300870 strlcpy(ati_remote->rc_name, udev->manufacturer,
871 sizeof(ati_remote->rc_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (udev->product)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300874 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
875 "%s %s", ati_remote->rc_name, udev->product);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Anssi Hannulac34516e2011-08-06 18:18:08 -0300877 if (!strlen(ati_remote->rc_name))
878 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500879 DRIVER_DESC "(%04x,%04x)",
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500880 le16_to_cpu(ati_remote->udev->descriptor.idVendor),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 le16_to_cpu(ati_remote->udev->descriptor.idProduct));
882
Anssi Hannulac34516e2011-08-06 18:18:08 -0300883 snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
884 "%s mouse", ati_remote->rc_name);
885
Anssi Hannula0d746792012-04-01 16:41:45 -0300886 rc_dev->map_name = RC_MAP_ATI_X10; /* default map */
887
888 /* set default keymap according to receiver model */
889 if (type) {
890 if (type->default_keymap)
891 rc_dev->map_name = type->default_keymap;
892 else if (type->get_default_keymap)
893 rc_dev->map_name = type->get_default_keymap(interface);
894 }
Anssi Hannula175fcec2011-08-06 18:18:11 -0300895
Anssi Hannulac34516e2011-08-06 18:18:08 -0300896 ati_remote_rc_init(ati_remote);
897 mutex_init(&ati_remote->open_mutex);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500900 err = ati_remote_initialize(ati_remote);
901 if (err)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300902 goto exit_kill_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Anssi Hannulac34516e2011-08-06 18:18:08 -0300904 /* Set up and register rc device */
905 err = rc_register_device(ati_remote->rdev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400906 if (err)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300907 goto exit_kill_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Anssi Hannulac34516e2011-08-06 18:18:08 -0300909 /* use our delay for rc_dev */
910 ati_remote->rdev->input_dev->rep[REP_DELAY] = repeat_delay;
911
912 /* Set up and register mouse input device */
913 if (mouse) {
914 input_dev = input_allocate_device();
Peter Senna Tschudina84b17e2012-09-04 08:05:04 -0300915 if (!input_dev) {
916 err = -ENOMEM;
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300917 goto exit_unregister_device;
Peter Senna Tschudina84b17e2012-09-04 08:05:04 -0300918 }
Anssi Hannulac34516e2011-08-06 18:18:08 -0300919
920 ati_remote->idev = input_dev;
921 ati_remote_input_init(ati_remote);
922 err = input_register_device(input_dev);
923
924 if (err)
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300925 goto exit_free_input_device;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300926 }
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 usb_set_intfdata(interface, ati_remote);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500929 return 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500930
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300931 exit_free_input_device:
932 input_free_device(input_dev);
933 exit_unregister_device:
934 rc_unregister_device(rc_dev);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300935 rc_dev = NULL;
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300936 exit_kill_urbs:
937 usb_kill_urb(ati_remote->irq_urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500938 usb_kill_urb(ati_remote->out_urb);
Matthijs Kooijman70ef6992012-11-02 09:13:54 -0300939 exit_free_buffers:
940 ati_remote_free_buffers(ati_remote);
941 exit_free_dev_rdev:
942 rc_free_device(rc_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500943 kfree(ati_remote);
944 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/*
Du, Changbina342dae2012-07-07 03:53:28 -0300948 * ati_remote_disconnect
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
950static void ati_remote_disconnect(struct usb_interface *interface)
951{
952 struct ati_remote *ati_remote;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 ati_remote = usb_get_intfdata(interface);
955 usb_set_intfdata(interface, NULL);
956 if (!ati_remote) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700957 dev_warn(&interface->dev, "%s - null device?\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return;
959 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500960
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500961 usb_kill_urb(ati_remote->irq_urb);
962 usb_kill_urb(ati_remote->out_urb);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300963 if (ati_remote->idev)
964 input_unregister_device(ati_remote->idev);
965 rc_unregister_device(ati_remote->rdev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500966 ati_remote_free_buffers(ati_remote);
967 kfree(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Du, Changbina342dae2012-07-07 03:53:28 -0300970/* usb specific object to register with the usb subsystem */
971static struct usb_driver ati_remote_driver = {
972 .name = "ati_remote",
973 .probe = ati_remote_probe,
974 .disconnect = ati_remote_disconnect,
975 .id_table = ati_remote_table,
976};
977
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800978module_usb_driver(ati_remote_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980MODULE_AUTHOR(DRIVER_AUTHOR);
981MODULE_DESCRIPTION(DRIVER_DESC);
982MODULE_LICENSE("GPL");