blob: 26fa043d3de71a2135411855202f83f95d30b940 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#define dbginfo(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
151#undef err
152#define err(format, arg...) printk(KERN_ERR format , ## arg)
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500153
Anssi Hannula0d746792012-04-01 16:41:45 -0300154struct ati_receiver_type {
155 /* either default_keymap or get_default_keymap should be set */
156 const char *default_keymap;
157 const char *(*get_default_keymap)(struct usb_interface *interface);
158};
159
Anssi Hannula9d454d42012-04-01 16:41:46 -0300160static const char *get_medion_keymap(struct usb_interface *interface)
161{
162 struct usb_device *udev = interface_to_usbdev(interface);
163
164 /* The receiver shipped with the "Digitainer" variant helpfully has
165 * a single additional bit set in its descriptor. */
166 if (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP)
167 return RC_MAP_MEDION_X10_DIGITAINER;
168
169 return RC_MAP_MEDION_X10;
170}
171
Anssi Hannula0d746792012-04-01 16:41:45 -0300172static const struct ati_receiver_type type_ati = { .default_keymap = RC_MAP_ATI_X10 };
Anssi Hannula9d454d42012-04-01 16:41:46 -0300173static const struct ati_receiver_type type_medion = { .get_default_keymap = get_medion_keymap };
Anssi Hannula0d746792012-04-01 16:41:45 -0300174static const struct ati_receiver_type type_firefly = { .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY };
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static struct usb_device_id ati_remote_table[] = {
Anssi Hannula0d746792012-04-01 16:41:45 -0300177 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
178 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
179 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
180 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
181 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_medion },
182 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_firefly },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 {} /* Terminating entry */
184};
185
186MODULE_DEVICE_TABLE(usb, ati_remote_table);
187
188/* Get hi and low bytes of a 16-bits int */
189#define HI(a) ((unsigned char)((a) >> 8))
190#define LO(a) ((unsigned char)((a) & 0xff))
191
192#define SEND_FLAG_IN_PROGRESS 1
193#define SEND_FLAG_COMPLETE 2
194
195/* Device initialization strings */
196static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
197static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199struct ati_remote {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500200 struct input_dev *idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300201 struct rc_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct usb_device *udev;
203 struct usb_interface *interface;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct urb *irq_urb;
206 struct urb *out_urb;
207 struct usb_endpoint_descriptor *endpoint_in;
208 struct usb_endpoint_descriptor *endpoint_out;
209 unsigned char *inbuf;
210 unsigned char *outbuf;
211 dma_addr_t inbuf_dma;
212 dma_addr_t outbuf_dma;
213
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300214 unsigned char old_data; /* Detect duplicate events */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 unsigned long old_jiffies;
216 unsigned long acc_jiffies; /* handle acceleration */
Karl Pickett0de95502007-04-12 01:35:59 -0400217 unsigned long first_jiffies;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 unsigned int repeat_count;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500220
Anssi Hannulac34516e2011-08-06 18:18:08 -0300221 char rc_name[NAME_BUFSIZE];
222 char rc_phys[NAME_BUFSIZE];
223 char mouse_name[NAME_BUFSIZE];
224 char mouse_phys[NAME_BUFSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 wait_queue_head_t wait;
227 int send_flags;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300228
229 int users; /* 0-2, users are rc and input */
230 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
233/* "Kinds" of messages sent from the hardware to the driver. */
234#define KIND_END 0
235#define KIND_LITERAL 1 /* Simply pass to input system */
236#define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
237#define KIND_LU 3 /* Directional keypad diagonals - left up, */
238#define KIND_RU 4 /* right up, */
239#define KIND_LD 5 /* left down, */
240#define KIND_RD 6 /* right down */
241#define KIND_ACCEL 7 /* Directional keypad - left, right, up, down.*/
242
243/* Translation table from hardware messages to input events. */
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100244static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 short kind;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300246 unsigned char data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int type;
248 unsigned int code;
249 int value;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500250} ati_remote_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 /* Directional control pad axes */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300252 {KIND_ACCEL, 0x70, EV_REL, REL_X, -1}, /* left */
253 {KIND_ACCEL, 0x71, EV_REL, REL_X, 1}, /* right */
254 {KIND_ACCEL, 0x72, EV_REL, REL_Y, -1}, /* up */
255 {KIND_ACCEL, 0x73, EV_REL, REL_Y, 1}, /* down */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500256 /* Directional control pad diagonals */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300257 {KIND_LU, 0x74, EV_REL, 0, 0}, /* left up */
258 {KIND_RU, 0x75, EV_REL, 0, 0}, /* right up */
259 {KIND_LD, 0x77, EV_REL, 0, 0}, /* left down */
260 {KIND_RD, 0x76, EV_REL, 0, 0}, /* right down */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* "Mouse button" buttons */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300263 {KIND_LITERAL, 0x78, EV_KEY, BTN_LEFT, 1}, /* left btn down */
264 {KIND_LITERAL, 0x79, EV_KEY, BTN_LEFT, 0}, /* left btn up */
265 {KIND_LITERAL, 0x7c, EV_KEY, BTN_RIGHT, 1},/* right btn down */
266 {KIND_LITERAL, 0x7d, EV_KEY, BTN_RIGHT, 0},/* right btn up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500268 /* Artificial "doubleclick" events are generated by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * They are mapped to the "side" and "extra" mouse buttons here. */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300270 {KIND_FILTERED, 0x7a, EV_KEY, BTN_SIDE, 1}, /* left dblclick */
271 {KIND_FILTERED, 0x7e, EV_KEY, BTN_EXTRA, 1},/* right dblclick */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Anssi Hannulac34516e2011-08-06 18:18:08 -0300273 /* Non-mouse events are handled by rc-core */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300274 {KIND_END, 0x00, EV_MAX + 1, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
277/* Local function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static int ati_remote_sendpacket (struct ati_remote *ati_remote, u16 cmd, unsigned char *data);
David Howells7d12e782006-10-05 14:55:46 +0100279static void ati_remote_irq_out (struct urb *urb);
280static void ati_remote_irq_in (struct urb *urb);
281static void ati_remote_input_report (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static int ati_remote_initialize (struct ati_remote *ati_remote);
283static int ati_remote_probe (struct usb_interface *interface, const struct usb_device_id *id);
284static void ati_remote_disconnect (struct usb_interface *interface);
285
286/* usb specific object to register with the usb subsystem */
287static struct usb_driver ati_remote_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 .name = "ati_remote",
289 .probe = ati_remote_probe,
290 .disconnect = ati_remote_disconnect,
291 .id_table = ati_remote_table,
292};
293
294/*
295 * ati_remote_dump_input
296 */
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700297static void ati_remote_dump(struct device *dev, unsigned char *data,
298 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Anssi Hannula0224e042011-08-06 18:18:10 -0300300 if (len == 1) {
301 if (data[0] != (unsigned char)0xff && data[0] != 0x00)
302 dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
303 } else if (len == 4)
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700304 dev_warn(dev, "Weird key %02x %02x %02x %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 data[0], data[1], data[2], data[3]);
306 else
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700307 dev_warn(dev, "Weird data, len=%d %02x %02x %02x %02x %02x %02x ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 len, data[0], data[1], data[2], data[3], data[4], data[5]);
309}
310
311/*
312 * ati_remote_open
313 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300314static int ati_remote_open(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Anssi Hannulac34516e2011-08-06 18:18:08 -0300316 int err = 0;
317
318 mutex_lock(&ati_remote->open_mutex);
319
320 if (ati_remote->users++ != 0)
321 goto out; /* one was already active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* On first open, submit the read urb which was set up previously. */
324 ati_remote->irq_urb->dev = ati_remote->udev;
325 if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500326 dev_err(&ati_remote->interface->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400327 "%s: usb_submit_urb failed!\n", __func__);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300328 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Anssi Hannulac34516e2011-08-06 18:18:08 -0300331out: mutex_unlock(&ati_remote->open_mutex);
332 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335/*
336 * ati_remote_close
337 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300338static void ati_remote_close(struct ati_remote *ati_remote)
339{
340 mutex_lock(&ati_remote->open_mutex);
341 if (--ati_remote->users == 0)
342 usb_kill_urb(ati_remote->irq_urb);
343 mutex_unlock(&ati_remote->open_mutex);
344}
345
346static int ati_remote_input_open(struct input_dev *inputdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400348 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300349 return ati_remote_open(ati_remote);
350}
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500351
Anssi Hannulac34516e2011-08-06 18:18:08 -0300352static void ati_remote_input_close(struct input_dev *inputdev)
353{
354 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
355 ati_remote_close(ati_remote);
356}
357
358static int ati_remote_rc_open(struct rc_dev *rdev)
359{
360 struct ati_remote *ati_remote = rdev->priv;
361 return ati_remote_open(ati_remote);
362}
363
364static void ati_remote_rc_close(struct rc_dev *rdev)
365{
366 struct ati_remote *ati_remote = rdev->priv;
367 ati_remote_close(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
370/*
371 * ati_remote_irq_out
372 */
David Howells7d12e782006-10-05 14:55:46 +0100373static void ati_remote_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct ati_remote *ati_remote = urb->context;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (urb->status) {
378 dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400379 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return;
381 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 ati_remote->send_flags |= SEND_FLAG_COMPLETE;
384 wmb();
385 wake_up(&ati_remote->wait);
386}
387
388/*
389 * ati_remote_sendpacket
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * Used to send device initialization strings
392 */
393static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, unsigned char *data)
394{
395 int retval = 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Set up out_urb */
398 memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500399 ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
402 ati_remote->out_urb->dev = ati_remote->udev;
403 ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
404
405 retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
406 if (retval) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500407 dev_dbg(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 "sendpacket: usb_submit_urb failed: %d\n", retval);
409 return retval;
410 }
411
412 wait_event_timeout(ati_remote->wait,
413 ((ati_remote->out_urb->status != -EINPROGRESS) ||
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500414 (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 HZ);
416 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return retval;
419}
420
421/*
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400422 * ati_remote_compute_accel
423 *
424 * Implements acceleration curve for directional control pad
425 * If elapsed time since last event is > 1/4 second, user "stopped",
426 * so reset acceleration. Otherwise, user is probably holding the control
427 * pad down, so we increase acceleration, ramping up over two seconds to
428 * a maximum speed.
429 */
430static int ati_remote_compute_accel(struct ati_remote *ati_remote)
431{
432 static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
433 unsigned long now = jiffies;
434 int acc;
435
436 if (time_after(now, ati_remote->old_jiffies + msecs_to_jiffies(250))) {
437 acc = 1;
438 ati_remote->acc_jiffies = now;
439 }
440 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(125)))
441 acc = accel[0];
442 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(250)))
443 acc = accel[1];
444 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(500)))
445 acc = accel[2];
446 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1000)))
447 acc = accel[3];
448 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1500)))
449 acc = accel[4];
450 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(2000)))
451 acc = accel[5];
452 else
453 acc = accel[6];
454
455 return acc;
456}
457
458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * ati_remote_report_input
460 */
David Howells7d12e782006-10-05 14:55:46 +0100461static void ati_remote_input_report(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct ati_remote *ati_remote = urb->context;
464 unsigned char *data= ati_remote->inbuf;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500465 struct input_dev *dev = ati_remote->idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300466 int index = -1;
467 int acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int remote_num;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300469 unsigned char scancode;
Anssi Hannula9d454d42012-04-01 16:41:46 -0300470 u32 wheel_keycode = KEY_RESERVED;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300471 int i;
472
473 /*
474 * data[0] = 0x14
475 * data[1] = data[2] + data[3] + 0xd5 (a checksum byte)
476 * data[2] = the key code (with toggle bit in MSB with some models)
477 * data[3] = channel << 4 (the low 4 bits must be zero)
478 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /* Deal with strange looking inputs */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500481 if ( (urb->actual_length != 4) || (data[0] != 0x14) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ((data[3] & 0x0f) != 0x00) ) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700483 ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return;
485 }
486
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300487 if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) {
488 dbginfo(&ati_remote->interface->dev,
489 "wrong checksum in input: %02x %02x %02x %02x\n",
490 data[0], data[1], data[2], data[3]);
491 return;
492 }
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Mask unwanted remote channels. */
495 /* note: remote_num is 0-based, channel 1 on remote == 0 here */
496 remote_num = (data[3] >> 4) & 0x0f;
Anssi Hannula9688efd2011-08-06 18:18:07 -0300497 if (channel_mask & (1 << (remote_num + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dbginfo(&ati_remote->interface->dev,
499 "Masked input from channel 0x%02x: data %02x,%02x, mask= 0x%02lx\n",
500 remote_num, data[1], data[2], channel_mask);
501 return;
502 }
503
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300504 /*
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300505 * MSB is a toggle code, though only used by some devices
506 * (e.g. SnapStream Firefly)
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300507 */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300508 scancode = data[2] & 0x7f;
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300509
Anssi Hannula9d454d42012-04-01 16:41:46 -0300510 dbginfo(&ati_remote->interface->dev,
511 "channel 0x%02x; key data %02x, scancode %02x\n",
512 remote_num, data[2], scancode);
513
514 if (scancode >= 0x70) {
515 /*
516 * This is either a mouse or scrollwheel event, depending on
517 * the remote/keymap.
518 * Get the keycode assigned to scancode 0x78/0x70. If it is
519 * set, assume this is a scrollwheel up/down event.
520 */
521 wheel_keycode = rc_g_keycode_from_table(ati_remote->rdev,
522 scancode & 0x78);
523
524 if (wheel_keycode == KEY_RESERVED) {
525 /* scrollwheel was not mapped, assume mouse */
526
527 /* Look up event code index in the mouse translation table. */
528 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
529 if (scancode == ati_remote_tbl[i].data) {
530 index = i;
531 break;
532 }
533 }
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300534 }
535 }
Anssi Hannulac34516e2011-08-06 18:18:08 -0300536
Anssi Hannulac34516e2011-08-06 18:18:08 -0300537 if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 input_event(dev, ati_remote_tbl[index].type,
539 ati_remote_tbl[index].code,
540 ati_remote_tbl[index].value);
541 input_sync(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 ati_remote->old_jiffies = jiffies;
544 return;
545 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500546
Anssi Hannulac34516e2011-08-06 18:18:08 -0300547 if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
Karl Pickett0de95502007-04-12 01:35:59 -0400548 unsigned long now = jiffies;
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* Filter duplicate events which happen "too close" together. */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300551 if (ati_remote->old_data == data[2] &&
Karl Pickett0de95502007-04-12 01:35:59 -0400552 time_before(now, ati_remote->old_jiffies +
553 msecs_to_jiffies(repeat_filter))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 ati_remote->repeat_count++;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500555 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 ati_remote->repeat_count = 0;
Karl Pickett0de95502007-04-12 01:35:59 -0400557 ati_remote->first_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500559
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300560 ati_remote->old_data = data[2];
Karl Pickett0de95502007-04-12 01:35:59 -0400561 ati_remote->old_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Karl Pickett0de95502007-04-12 01:35:59 -0400563 /* Ensure we skip at least the 4 first duplicate events (generated
564 * by a single keypress), and continue skipping until repeat_delay
565 * msecs have passed
566 */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400567 if (ati_remote->repeat_count > 0 &&
Karl Pickett0de95502007-04-12 01:35:59 -0400568 (ati_remote->repeat_count < 5 ||
569 time_before(now, ati_remote->first_jiffies +
570 msecs_to_jiffies(repeat_delay))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500572
Anssi Hannulac34516e2011-08-06 18:18:08 -0300573 if (index < 0) {
574 /* Not a mouse event, hand it to rc-core. */
Anssi Hannula9d454d42012-04-01 16:41:46 -0300575 int count = 1;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300576
Anssi Hannula9d454d42012-04-01 16:41:46 -0300577 if (wheel_keycode != KEY_RESERVED) {
578 /*
579 * This is a scrollwheel event, send the
580 * scroll up (0x78) / down (0x70) scancode
581 * repeatedly as many times as indicated by
582 * rest of the scancode.
583 */
584 count = (scancode & 0x07) + 1;
585 scancode &= 0x78;
586 }
587
588 while (count--) {
589 /*
590 * We don't use the rc-core repeat handling yet as
591 * it would cause ghost repeats which would be a
592 * regression for this driver.
593 */
594 rc_keydown_notimeout(ati_remote->rdev, scancode,
595 data[2]);
596 rc_keyup(ati_remote->rdev);
597 }
Anssi Hannulac34516e2011-08-06 18:18:08 -0300598 return;
599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 input_event(dev, ati_remote_tbl[index].type,
602 ati_remote_tbl[index].code, 1);
Dmitry Torokhovc3c38fb2006-08-04 22:53:24 -0400603 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 input_event(dev, ati_remote_tbl[index].type,
605 ati_remote_tbl[index].code, 0);
606 input_sync(dev);
607
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400608 } else {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500609
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400610 /*
611 * Other event kinds are from the directional control pad, and have an
612 * acceleration factor applied to them. Without this acceleration, the
613 * control pad is mostly unusable.
614 */
615 acc = ati_remote_compute_accel(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400617 switch (ati_remote_tbl[index].kind) {
618 case KIND_ACCEL:
619 input_event(dev, ati_remote_tbl[index].type,
620 ati_remote_tbl[index].code,
621 ati_remote_tbl[index].value * acc);
622 break;
623 case KIND_LU:
624 input_report_rel(dev, REL_X, -acc);
625 input_report_rel(dev, REL_Y, -acc);
626 break;
627 case KIND_RU:
628 input_report_rel(dev, REL_X, acc);
629 input_report_rel(dev, REL_Y, -acc);
630 break;
631 case KIND_LD:
632 input_report_rel(dev, REL_X, -acc);
633 input_report_rel(dev, REL_Y, acc);
634 break;
635 case KIND_RD:
636 input_report_rel(dev, REL_X, acc);
637 input_report_rel(dev, REL_Y, acc);
638 break;
639 default:
640 dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
641 ati_remote_tbl[index].kind);
642 }
643 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400645 ati_remote->old_jiffies = jiffies;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300646 ati_remote->old_data = data[2];
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
651 * ati_remote_irq_in
652 */
David Howells7d12e782006-10-05 14:55:46 +0100653static void ati_remote_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct ati_remote *ati_remote = urb->context;
656 int retval;
657
658 switch (urb->status) {
659 case 0: /* success */
David Howells7d12e782006-10-05 14:55:46 +0100660 ati_remote_input_report(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 break;
662 case -ECONNRESET: /* unlink */
663 case -ENOENT:
664 case -ESHUTDOWN:
665 dev_dbg(&ati_remote->interface->dev, "%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 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500669 dev_dbg(&ati_remote->interface->dev, "%s: Nonzero urb status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400670 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500672
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800673 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (retval)
675 dev_err(&ati_remote->interface->dev, "%s: usb_submit_urb()=%d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400676 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679/*
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500680 * ati_remote_alloc_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500682static int ati_remote_alloc_buffers(struct usb_device *udev,
683 struct ati_remote *ati_remote)
684{
Daniel Mack997ea582010-04-12 13:17:25 +0200685 ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
686 &ati_remote->inbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500687 if (!ati_remote->inbuf)
688 return -1;
689
Daniel Mack997ea582010-04-12 13:17:25 +0200690 ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
Anssi Hannula9688efd2011-08-06 18:18:07 -0300691 &ati_remote->outbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500692 if (!ati_remote->outbuf)
693 return -1;
694
695 ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
696 if (!ati_remote->irq_urb)
697 return -1;
698
699 ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
700 if (!ati_remote->out_urb)
701 return -1;
702
703 return 0;
704}
705
706/*
707 * ati_remote_free_buffers
708 */
709static void ati_remote_free_buffers(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Mariusz Kozlowski459f8362006-11-08 15:35:46 +0100711 usb_free_urb(ati_remote->irq_urb);
712 usb_free_urb(ati_remote->out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Daniel Mack997ea582010-04-12 13:17:25 +0200714 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800715 ati_remote->inbuf, ati_remote->inbuf_dma);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500716
Daniel Mack997ea582010-04-12 13:17:25 +0200717 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800718 ati_remote->outbuf, ati_remote->outbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721static void ati_remote_input_init(struct ati_remote *ati_remote)
722{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500723 struct input_dev *idev = ati_remote->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int i;
725
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700726 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
727 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
728 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
729 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
731 if (ati_remote_tbl[i].type == EV_KEY)
732 set_bit(ati_remote_tbl[i].code, idev->keybit);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500733
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400734 input_set_drvdata(idev, ati_remote);
735
Anssi Hannulac34516e2011-08-06 18:18:08 -0300736 idev->open = ati_remote_input_open;
737 idev->close = ati_remote_input_close;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500738
Anssi Hannulac34516e2011-08-06 18:18:08 -0300739 idev->name = ati_remote->mouse_name;
740 idev->phys = ati_remote->mouse_phys;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500741
Dmitry Torokhov16a334c2005-06-30 00:49:08 -0500742 usb_to_input_id(ati_remote->udev, &idev->id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300743 idev->dev.parent = &ati_remote->interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Anssi Hannulac34516e2011-08-06 18:18:08 -0300746static void ati_remote_rc_init(struct ati_remote *ati_remote)
747{
748 struct rc_dev *rdev = ati_remote->rdev;
749
750 rdev->priv = ati_remote;
751 rdev->driver_type = RC_DRIVER_SCANCODE;
752 rdev->allowed_protos = RC_TYPE_OTHER;
753 rdev->driver_name = "ati_remote";
754
755 rdev->open = ati_remote_rc_open;
756 rdev->close = ati_remote_rc_close;
757
758 rdev->input_name = ati_remote->rc_name;
759 rdev->input_phys = ati_remote->rc_phys;
760
761 usb_to_input_id(ati_remote->udev, &rdev->input_id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300762 rdev->dev.parent = &ati_remote->interface->dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300763}
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765static int ati_remote_initialize(struct ati_remote *ati_remote)
766{
767 struct usb_device *udev = ati_remote->udev;
768 int pipe, maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 init_waitqueue_head(&ati_remote->wait);
771
772 /* Set up irq_urb */
773 pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
774 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
775 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500776
777 usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
778 maxp, ati_remote_irq_in, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 ati_remote->endpoint_in->bInterval);
780 ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
781 ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* Set up out_urb */
784 pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
785 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
786 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
787
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500788 usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
789 maxp, ati_remote_irq_out, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ati_remote->endpoint_out->bInterval);
791 ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
792 ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
793
794 /* send initialization strings */
795 if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
796 (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500797 dev_err(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 "Initializing ati_remote hardware failed.\n");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500799 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
803}
804
805/*
806 * ati_remote_probe
807 */
808static int ati_remote_probe(struct usb_interface *interface, const struct usb_device_id *id)
809{
810 struct usb_device *udev = interface_to_usbdev(interface);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500811 struct usb_host_interface *iface_host = interface->cur_altsetting;
812 struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
Anssi Hannula0d746792012-04-01 16:41:45 -0300813 struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500814 struct ati_remote *ati_remote;
815 struct input_dev *input_dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300816 struct rc_dev *rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500817 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (iface_host->desc.bNumEndpoints != 2) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400820 err("%s: Unexpected desc.bNumEndpoints\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500821 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500824 endpoint_in = &iface_host->endpoint[0].desc;
825 endpoint_out = &iface_host->endpoint[1].desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Luiz Fernando N. Capitulino96642a22006-09-27 11:58:53 -0700827 if (!usb_endpoint_is_int_in(endpoint_in)) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400828 err("%s: Unexpected endpoint_in\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500829 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500831 if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400832 err("%s: endpoint_in message size==0? \n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500833 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500836 ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300837 rc_dev = rc_allocate_device();
838 if (!ati_remote || !rc_dev)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500839 goto fail1;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Allocate URB buffers, URBs */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500842 if (ati_remote_alloc_buffers(udev, ati_remote))
843 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500845 ati_remote->endpoint_in = endpoint_in;
846 ati_remote->endpoint_out = endpoint_out;
847 ati_remote->udev = udev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300848 ati_remote->rdev = rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500849 ati_remote->interface = interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Anssi Hannulac34516e2011-08-06 18:18:08 -0300851 usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
852 strlcpy(ati_remote->mouse_phys, ati_remote->rc_phys,
853 sizeof(ati_remote->mouse_phys));
854
855 strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
856 strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (udev->manufacturer)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300859 strlcpy(ati_remote->rc_name, udev->manufacturer,
860 sizeof(ati_remote->rc_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (udev->product)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300863 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
864 "%s %s", ati_remote->rc_name, udev->product);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Anssi Hannulac34516e2011-08-06 18:18:08 -0300866 if (!strlen(ati_remote->rc_name))
867 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500868 DRIVER_DESC "(%04x,%04x)",
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500869 le16_to_cpu(ati_remote->udev->descriptor.idVendor),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 le16_to_cpu(ati_remote->udev->descriptor.idProduct));
871
Anssi Hannulac34516e2011-08-06 18:18:08 -0300872 snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
873 "%s mouse", ati_remote->rc_name);
874
Anssi Hannula0d746792012-04-01 16:41:45 -0300875 rc_dev->map_name = RC_MAP_ATI_X10; /* default map */
876
877 /* set default keymap according to receiver model */
878 if (type) {
879 if (type->default_keymap)
880 rc_dev->map_name = type->default_keymap;
881 else if (type->get_default_keymap)
882 rc_dev->map_name = type->get_default_keymap(interface);
883 }
Anssi Hannula175fcec2011-08-06 18:18:11 -0300884
Anssi Hannulac34516e2011-08-06 18:18:08 -0300885 ati_remote_rc_init(ati_remote);
886 mutex_init(&ati_remote->open_mutex);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500889 err = ati_remote_initialize(ati_remote);
890 if (err)
891 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Anssi Hannulac34516e2011-08-06 18:18:08 -0300893 /* Set up and register rc device */
894 err = rc_register_device(ati_remote->rdev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400895 if (err)
896 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Anssi Hannulac34516e2011-08-06 18:18:08 -0300898 /* use our delay for rc_dev */
899 ati_remote->rdev->input_dev->rep[REP_DELAY] = repeat_delay;
900
901 /* Set up and register mouse input device */
902 if (mouse) {
903 input_dev = input_allocate_device();
904 if (!input_dev)
905 goto fail4;
906
907 ati_remote->idev = input_dev;
908 ati_remote_input_init(ati_remote);
909 err = input_register_device(input_dev);
910
911 if (err)
912 goto fail5;
913 }
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 usb_set_intfdata(interface, ati_remote);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500916 return 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500917
Anssi Hannulac34516e2011-08-06 18:18:08 -0300918 fail5: input_free_device(input_dev);
919 fail4: rc_unregister_device(rc_dev);
920 rc_dev = NULL;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400921 fail3: usb_kill_urb(ati_remote->irq_urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500922 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400923 fail2: ati_remote_free_buffers(ati_remote);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300924 fail1: rc_free_device(rc_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500925 kfree(ati_remote);
926 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929/*
930 * ati_remote_disconnect
931 */
932static void ati_remote_disconnect(struct usb_interface *interface)
933{
934 struct ati_remote *ati_remote;
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ati_remote = usb_get_intfdata(interface);
937 usb_set_intfdata(interface, NULL);
938 if (!ati_remote) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700939 dev_warn(&interface->dev, "%s - null device?\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return;
941 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500942
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500943 usb_kill_urb(ati_remote->irq_urb);
944 usb_kill_urb(ati_remote->out_urb);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300945 if (ati_remote->idev)
946 input_unregister_device(ati_remote->idev);
947 rc_unregister_device(ati_remote->rdev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500948 ati_remote_free_buffers(ati_remote);
949 kfree(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800952module_usb_driver(ati_remote_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954MODULE_AUTHOR(DRIVER_AUTHOR);
955MODULE_DESCRIPTION(DRIVER_DESC);
956MODULE_LICENSE("GPL");