blob: 7a35f7afad5010cad0a5ce5257dc8bc48cbc79e5 [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 Hannulac34516e2011-08-06 18:18:08 -03004 * Copyright (c) 2011 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
160static const struct ati_receiver_type type_ati = { .default_keymap = RC_MAP_ATI_X10 };
161static const struct ati_receiver_type type_medion = { .default_keymap = RC_MAP_MEDION_X10 };
162static const struct ati_receiver_type type_firefly = { .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY };
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static struct usb_device_id ati_remote_table[] = {
Anssi Hannula0d746792012-04-01 16:41:45 -0300165 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
166 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
167 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
168 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_ati },
169 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_medion },
170 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)&type_firefly },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 {} /* Terminating entry */
172};
173
174MODULE_DEVICE_TABLE(usb, ati_remote_table);
175
176/* Get hi and low bytes of a 16-bits int */
177#define HI(a) ((unsigned char)((a) >> 8))
178#define LO(a) ((unsigned char)((a) & 0xff))
179
180#define SEND_FLAG_IN_PROGRESS 1
181#define SEND_FLAG_COMPLETE 2
182
183/* Device initialization strings */
184static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
185static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187struct ati_remote {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500188 struct input_dev *idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300189 struct rc_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct usb_device *udev;
191 struct usb_interface *interface;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct urb *irq_urb;
194 struct urb *out_urb;
195 struct usb_endpoint_descriptor *endpoint_in;
196 struct usb_endpoint_descriptor *endpoint_out;
197 unsigned char *inbuf;
198 unsigned char *outbuf;
199 dma_addr_t inbuf_dma;
200 dma_addr_t outbuf_dma;
201
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300202 unsigned char old_data; /* Detect duplicate events */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned long old_jiffies;
204 unsigned long acc_jiffies; /* handle acceleration */
Karl Pickett0de95502007-04-12 01:35:59 -0400205 unsigned long first_jiffies;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned int repeat_count;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500208
Anssi Hannulac34516e2011-08-06 18:18:08 -0300209 char rc_name[NAME_BUFSIZE];
210 char rc_phys[NAME_BUFSIZE];
211 char mouse_name[NAME_BUFSIZE];
212 char mouse_phys[NAME_BUFSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 wait_queue_head_t wait;
215 int send_flags;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300216
217 int users; /* 0-2, users are rc and input */
218 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219};
220
221/* "Kinds" of messages sent from the hardware to the driver. */
222#define KIND_END 0
223#define KIND_LITERAL 1 /* Simply pass to input system */
224#define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
225#define KIND_LU 3 /* Directional keypad diagonals - left up, */
226#define KIND_RU 4 /* right up, */
227#define KIND_LD 5 /* left down, */
228#define KIND_RD 6 /* right down */
229#define KIND_ACCEL 7 /* Directional keypad - left, right, up, down.*/
230
231/* Translation table from hardware messages to input events. */
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100232static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 short kind;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300234 unsigned char data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int type;
236 unsigned int code;
237 int value;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500238} ati_remote_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* Directional control pad axes */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300240 {KIND_ACCEL, 0x70, EV_REL, REL_X, -1}, /* left */
241 {KIND_ACCEL, 0x71, EV_REL, REL_X, 1}, /* right */
242 {KIND_ACCEL, 0x72, EV_REL, REL_Y, -1}, /* up */
243 {KIND_ACCEL, 0x73, EV_REL, REL_Y, 1}, /* down */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500244 /* Directional control pad diagonals */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300245 {KIND_LU, 0x74, EV_REL, 0, 0}, /* left up */
246 {KIND_RU, 0x75, EV_REL, 0, 0}, /* right up */
247 {KIND_LD, 0x77, EV_REL, 0, 0}, /* left down */
248 {KIND_RD, 0x76, EV_REL, 0, 0}, /* right down */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* "Mouse button" buttons */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300251 {KIND_LITERAL, 0x78, EV_KEY, BTN_LEFT, 1}, /* left btn down */
252 {KIND_LITERAL, 0x79, EV_KEY, BTN_LEFT, 0}, /* left btn up */
253 {KIND_LITERAL, 0x7c, EV_KEY, BTN_RIGHT, 1},/* right btn down */
254 {KIND_LITERAL, 0x7d, EV_KEY, BTN_RIGHT, 0},/* right btn up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500256 /* Artificial "doubleclick" events are generated by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * They are mapped to the "side" and "extra" mouse buttons here. */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300258 {KIND_FILTERED, 0x7a, EV_KEY, BTN_SIDE, 1}, /* left dblclick */
259 {KIND_FILTERED, 0x7e, EV_KEY, BTN_EXTRA, 1},/* right dblclick */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Anssi Hannulac34516e2011-08-06 18:18:08 -0300261 /* Non-mouse events are handled by rc-core */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300262 {KIND_END, 0x00, EV_MAX + 1, 0, 0}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263};
264
265/* Local function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static int ati_remote_sendpacket (struct ati_remote *ati_remote, u16 cmd, unsigned char *data);
David Howells7d12e782006-10-05 14:55:46 +0100267static void ati_remote_irq_out (struct urb *urb);
268static void ati_remote_irq_in (struct urb *urb);
269static void ati_remote_input_report (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static int ati_remote_initialize (struct ati_remote *ati_remote);
271static int ati_remote_probe (struct usb_interface *interface, const struct usb_device_id *id);
272static void ati_remote_disconnect (struct usb_interface *interface);
273
274/* usb specific object to register with the usb subsystem */
275static struct usb_driver ati_remote_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .name = "ati_remote",
277 .probe = ati_remote_probe,
278 .disconnect = ati_remote_disconnect,
279 .id_table = ati_remote_table,
280};
281
282/*
283 * ati_remote_dump_input
284 */
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700285static void ati_remote_dump(struct device *dev, unsigned char *data,
286 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Anssi Hannula0224e042011-08-06 18:18:10 -0300288 if (len == 1) {
289 if (data[0] != (unsigned char)0xff && data[0] != 0x00)
290 dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
291 } else if (len == 4)
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700292 dev_warn(dev, "Weird key %02x %02x %02x %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 data[0], data[1], data[2], data[3]);
294 else
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700295 dev_warn(dev, "Weird data, len=%d %02x %02x %02x %02x %02x %02x ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 len, data[0], data[1], data[2], data[3], data[4], data[5]);
297}
298
299/*
300 * ati_remote_open
301 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300302static int ati_remote_open(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Anssi Hannulac34516e2011-08-06 18:18:08 -0300304 int err = 0;
305
306 mutex_lock(&ati_remote->open_mutex);
307
308 if (ati_remote->users++ != 0)
309 goto out; /* one was already active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* On first open, submit the read urb which was set up previously. */
312 ati_remote->irq_urb->dev = ati_remote->udev;
313 if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500314 dev_err(&ati_remote->interface->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400315 "%s: usb_submit_urb failed!\n", __func__);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300316 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
Anssi Hannulac34516e2011-08-06 18:18:08 -0300319out: mutex_unlock(&ati_remote->open_mutex);
320 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/*
324 * ati_remote_close
325 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300326static void ati_remote_close(struct ati_remote *ati_remote)
327{
328 mutex_lock(&ati_remote->open_mutex);
329 if (--ati_remote->users == 0)
330 usb_kill_urb(ati_remote->irq_urb);
331 mutex_unlock(&ati_remote->open_mutex);
332}
333
334static int ati_remote_input_open(struct input_dev *inputdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400336 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300337 return ati_remote_open(ati_remote);
338}
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500339
Anssi Hannulac34516e2011-08-06 18:18:08 -0300340static void ati_remote_input_close(struct input_dev *inputdev)
341{
342 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
343 ati_remote_close(ati_remote);
344}
345
346static int ati_remote_rc_open(struct rc_dev *rdev)
347{
348 struct ati_remote *ati_remote = rdev->priv;
349 return ati_remote_open(ati_remote);
350}
351
352static void ati_remote_rc_close(struct rc_dev *rdev)
353{
354 struct ati_remote *ati_remote = rdev->priv;
355 ati_remote_close(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358/*
359 * ati_remote_irq_out
360 */
David Howells7d12e782006-10-05 14:55:46 +0100361static void ati_remote_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct ati_remote *ati_remote = urb->context;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (urb->status) {
366 dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400367 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return;
369 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ati_remote->send_flags |= SEND_FLAG_COMPLETE;
372 wmb();
373 wake_up(&ati_remote->wait);
374}
375
376/*
377 * ati_remote_sendpacket
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500378 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 * Used to send device initialization strings
380 */
381static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, unsigned char *data)
382{
383 int retval = 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* Set up out_urb */
386 memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500387 ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
390 ati_remote->out_urb->dev = ati_remote->udev;
391 ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
392
393 retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
394 if (retval) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500395 dev_dbg(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 "sendpacket: usb_submit_urb failed: %d\n", retval);
397 return retval;
398 }
399
400 wait_event_timeout(ati_remote->wait,
401 ((ati_remote->out_urb->status != -EINPROGRESS) ||
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500402 (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 HZ);
404 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return retval;
407}
408
409/*
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400410 * ati_remote_compute_accel
411 *
412 * Implements acceleration curve for directional control pad
413 * If elapsed time since last event is > 1/4 second, user "stopped",
414 * so reset acceleration. Otherwise, user is probably holding the control
415 * pad down, so we increase acceleration, ramping up over two seconds to
416 * a maximum speed.
417 */
418static int ati_remote_compute_accel(struct ati_remote *ati_remote)
419{
420 static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
421 unsigned long now = jiffies;
422 int acc;
423
424 if (time_after(now, ati_remote->old_jiffies + msecs_to_jiffies(250))) {
425 acc = 1;
426 ati_remote->acc_jiffies = now;
427 }
428 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(125)))
429 acc = accel[0];
430 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(250)))
431 acc = accel[1];
432 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(500)))
433 acc = accel[2];
434 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1000)))
435 acc = accel[3];
436 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1500)))
437 acc = accel[4];
438 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(2000)))
439 acc = accel[5];
440 else
441 acc = accel[6];
442
443 return acc;
444}
445
446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * ati_remote_report_input
448 */
David Howells7d12e782006-10-05 14:55:46 +0100449static void ati_remote_input_report(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct ati_remote *ati_remote = urb->context;
452 unsigned char *data= ati_remote->inbuf;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500453 struct input_dev *dev = ati_remote->idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300454 int index = -1;
455 int acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int remote_num;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300457 unsigned char scancode;
458 int i;
459
460 /*
461 * data[0] = 0x14
462 * data[1] = data[2] + data[3] + 0xd5 (a checksum byte)
463 * data[2] = the key code (with toggle bit in MSB with some models)
464 * data[3] = channel << 4 (the low 4 bits must be zero)
465 */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* Deal with strange looking inputs */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500468 if ( (urb->actual_length != 4) || (data[0] != 0x14) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ((data[3] & 0x0f) != 0x00) ) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700470 ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return;
472 }
473
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300474 if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) {
475 dbginfo(&ati_remote->interface->dev,
476 "wrong checksum in input: %02x %02x %02x %02x\n",
477 data[0], data[1], data[2], data[3]);
478 return;
479 }
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* Mask unwanted remote channels. */
482 /* note: remote_num is 0-based, channel 1 on remote == 0 here */
483 remote_num = (data[3] >> 4) & 0x0f;
Anssi Hannula9688efd2011-08-06 18:18:07 -0300484 if (channel_mask & (1 << (remote_num + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dbginfo(&ati_remote->interface->dev,
486 "Masked input from channel 0x%02x: data %02x,%02x, mask= 0x%02lx\n",
487 remote_num, data[1], data[2], channel_mask);
488 return;
489 }
490
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300491 /*
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300492 * MSB is a toggle code, though only used by some devices
493 * (e.g. SnapStream Firefly)
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300494 */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300495 scancode = data[2] & 0x7f;
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300496
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300497 /* Look up event code index in the mouse translation table. */
498 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
499 if (scancode == ati_remote_tbl[i].data) {
500 index = i;
501 break;
502 }
503 }
Anssi Hannulac34516e2011-08-06 18:18:08 -0300504
505 if (index >= 0) {
506 dbginfo(&ati_remote->interface->dev,
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300507 "channel 0x%02x; mouse data %02x; index %d; keycode %d\n",
508 remote_num, data[2], index, ati_remote_tbl[index].code);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300509 if (!dev)
510 return; /* no mouse device */
511 } else
512 dbginfo(&ati_remote->interface->dev,
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300513 "channel 0x%02x; key data %02x, scancode %02x\n",
514 remote_num, data[2], scancode);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300515
516
517 if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 input_event(dev, ati_remote_tbl[index].type,
519 ati_remote_tbl[index].code,
520 ati_remote_tbl[index].value);
521 input_sync(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 ati_remote->old_jiffies = jiffies;
524 return;
525 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500526
Anssi Hannulac34516e2011-08-06 18:18:08 -0300527 if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
Karl Pickett0de95502007-04-12 01:35:59 -0400528 unsigned long now = jiffies;
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Filter duplicate events which happen "too close" together. */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300531 if (ati_remote->old_data == data[2] &&
Karl Pickett0de95502007-04-12 01:35:59 -0400532 time_before(now, ati_remote->old_jiffies +
533 msecs_to_jiffies(repeat_filter))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ati_remote->repeat_count++;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500535 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ati_remote->repeat_count = 0;
Karl Pickett0de95502007-04-12 01:35:59 -0400537 ati_remote->first_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500539
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300540 ati_remote->old_data = data[2];
Karl Pickett0de95502007-04-12 01:35:59 -0400541 ati_remote->old_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Karl Pickett0de95502007-04-12 01:35:59 -0400543 /* Ensure we skip at least the 4 first duplicate events (generated
544 * by a single keypress), and continue skipping until repeat_delay
545 * msecs have passed
546 */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400547 if (ati_remote->repeat_count > 0 &&
Karl Pickett0de95502007-04-12 01:35:59 -0400548 (ati_remote->repeat_count < 5 ||
549 time_before(now, ati_remote->first_jiffies +
550 msecs_to_jiffies(repeat_delay))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500552
Anssi Hannulac34516e2011-08-06 18:18:08 -0300553 if (index < 0) {
554 /* Not a mouse event, hand it to rc-core. */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300555
556 /*
557 * We don't use the rc-core repeat handling yet as
558 * it would cause ghost repeats which would be a
559 * regression for this driver.
560 */
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300561 rc_keydown_notimeout(ati_remote->rdev, scancode,
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300562 data[2]);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300563 rc_keyup(ati_remote->rdev);
564 return;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 input_event(dev, ati_remote_tbl[index].type,
568 ati_remote_tbl[index].code, 1);
Dmitry Torokhovc3c38fb2006-08-04 22:53:24 -0400569 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 input_event(dev, ati_remote_tbl[index].type,
571 ati_remote_tbl[index].code, 0);
572 input_sync(dev);
573
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400574 } else {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500575
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400576 /*
577 * Other event kinds are from the directional control pad, and have an
578 * acceleration factor applied to them. Without this acceleration, the
579 * control pad is mostly unusable.
580 */
581 acc = ati_remote_compute_accel(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400583 switch (ati_remote_tbl[index].kind) {
584 case KIND_ACCEL:
585 input_event(dev, ati_remote_tbl[index].type,
586 ati_remote_tbl[index].code,
587 ati_remote_tbl[index].value * acc);
588 break;
589 case KIND_LU:
590 input_report_rel(dev, REL_X, -acc);
591 input_report_rel(dev, REL_Y, -acc);
592 break;
593 case KIND_RU:
594 input_report_rel(dev, REL_X, acc);
595 input_report_rel(dev, REL_Y, -acc);
596 break;
597 case KIND_LD:
598 input_report_rel(dev, REL_X, -acc);
599 input_report_rel(dev, REL_Y, acc);
600 break;
601 case KIND_RD:
602 input_report_rel(dev, REL_X, acc);
603 input_report_rel(dev, REL_Y, acc);
604 break;
605 default:
606 dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
607 ati_remote_tbl[index].kind);
608 }
609 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400611 ati_remote->old_jiffies = jiffies;
Anssi Hannula5eefb4f2011-12-06 22:34:29 -0300612 ati_remote->old_data = data[2];
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617 * ati_remote_irq_in
618 */
David Howells7d12e782006-10-05 14:55:46 +0100619static void ati_remote_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 struct ati_remote *ati_remote = urb->context;
622 int retval;
623
624 switch (urb->status) {
625 case 0: /* success */
David Howells7d12e782006-10-05 14:55:46 +0100626 ati_remote_input_report(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628 case -ECONNRESET: /* unlink */
629 case -ENOENT:
630 case -ESHUTDOWN:
631 dev_dbg(&ati_remote->interface->dev, "%s: urb error status, unlink? \n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400632 __func__);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 default: /* error */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500635 dev_dbg(&ati_remote->interface->dev, "%s: Nonzero urb status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400636 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500638
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800639 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (retval)
641 dev_err(&ati_remote->interface->dev, "%s: usb_submit_urb()=%d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400642 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645/*
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500646 * ati_remote_alloc_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500648static int ati_remote_alloc_buffers(struct usb_device *udev,
649 struct ati_remote *ati_remote)
650{
Daniel Mack997ea582010-04-12 13:17:25 +0200651 ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
652 &ati_remote->inbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500653 if (!ati_remote->inbuf)
654 return -1;
655
Daniel Mack997ea582010-04-12 13:17:25 +0200656 ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
Anssi Hannula9688efd2011-08-06 18:18:07 -0300657 &ati_remote->outbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500658 if (!ati_remote->outbuf)
659 return -1;
660
661 ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
662 if (!ati_remote->irq_urb)
663 return -1;
664
665 ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
666 if (!ati_remote->out_urb)
667 return -1;
668
669 return 0;
670}
671
672/*
673 * ati_remote_free_buffers
674 */
675static void ati_remote_free_buffers(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Mariusz Kozlowski459f8362006-11-08 15:35:46 +0100677 usb_free_urb(ati_remote->irq_urb);
678 usb_free_urb(ati_remote->out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Daniel Mack997ea582010-04-12 13:17:25 +0200680 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800681 ati_remote->inbuf, ati_remote->inbuf_dma);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500682
Daniel Mack997ea582010-04-12 13:17:25 +0200683 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800684 ati_remote->outbuf, ati_remote->outbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687static void ati_remote_input_init(struct ati_remote *ati_remote)
688{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500689 struct input_dev *idev = ati_remote->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int i;
691
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700692 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
693 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
694 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
695 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
697 if (ati_remote_tbl[i].type == EV_KEY)
698 set_bit(ati_remote_tbl[i].code, idev->keybit);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500699
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400700 input_set_drvdata(idev, ati_remote);
701
Anssi Hannulac34516e2011-08-06 18:18:08 -0300702 idev->open = ati_remote_input_open;
703 idev->close = ati_remote_input_close;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500704
Anssi Hannulac34516e2011-08-06 18:18:08 -0300705 idev->name = ati_remote->mouse_name;
706 idev->phys = ati_remote->mouse_phys;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500707
Dmitry Torokhov16a334c2005-06-30 00:49:08 -0500708 usb_to_input_id(ati_remote->udev, &idev->id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300709 idev->dev.parent = &ati_remote->interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Anssi Hannulac34516e2011-08-06 18:18:08 -0300712static void ati_remote_rc_init(struct ati_remote *ati_remote)
713{
714 struct rc_dev *rdev = ati_remote->rdev;
715
716 rdev->priv = ati_remote;
717 rdev->driver_type = RC_DRIVER_SCANCODE;
718 rdev->allowed_protos = RC_TYPE_OTHER;
719 rdev->driver_name = "ati_remote";
720
721 rdev->open = ati_remote_rc_open;
722 rdev->close = ati_remote_rc_close;
723
724 rdev->input_name = ati_remote->rc_name;
725 rdev->input_phys = ati_remote->rc_phys;
726
727 usb_to_input_id(ati_remote->udev, &rdev->input_id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300728 rdev->dev.parent = &ati_remote->interface->dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731static int ati_remote_initialize(struct ati_remote *ati_remote)
732{
733 struct usb_device *udev = ati_remote->udev;
734 int pipe, maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 init_waitqueue_head(&ati_remote->wait);
737
738 /* Set up irq_urb */
739 pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
740 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
741 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500742
743 usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
744 maxp, ati_remote_irq_in, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ati_remote->endpoint_in->bInterval);
746 ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
747 ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /* Set up out_urb */
750 pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
751 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
752 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
753
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500754 usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
755 maxp, ati_remote_irq_out, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 ati_remote->endpoint_out->bInterval);
757 ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
758 ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
759
760 /* send initialization strings */
761 if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
762 (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500763 dev_err(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 "Initializing ati_remote hardware failed.\n");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500765 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
769}
770
771/*
772 * ati_remote_probe
773 */
774static int ati_remote_probe(struct usb_interface *interface, const struct usb_device_id *id)
775{
776 struct usb_device *udev = interface_to_usbdev(interface);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500777 struct usb_host_interface *iface_host = interface->cur_altsetting;
778 struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
Anssi Hannula0d746792012-04-01 16:41:45 -0300779 struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500780 struct ati_remote *ati_remote;
781 struct input_dev *input_dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300782 struct rc_dev *rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500783 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (iface_host->desc.bNumEndpoints != 2) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400786 err("%s: Unexpected desc.bNumEndpoints\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500787 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500790 endpoint_in = &iface_host->endpoint[0].desc;
791 endpoint_out = &iface_host->endpoint[1].desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Luiz Fernando N. Capitulino96642a22006-09-27 11:58:53 -0700793 if (!usb_endpoint_is_int_in(endpoint_in)) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400794 err("%s: Unexpected endpoint_in\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500795 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500797 if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400798 err("%s: endpoint_in message size==0? \n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500799 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500802 ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300803 rc_dev = rc_allocate_device();
804 if (!ati_remote || !rc_dev)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500805 goto fail1;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* Allocate URB buffers, URBs */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500808 if (ati_remote_alloc_buffers(udev, ati_remote))
809 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500811 ati_remote->endpoint_in = endpoint_in;
812 ati_remote->endpoint_out = endpoint_out;
813 ati_remote->udev = udev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300814 ati_remote->rdev = rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500815 ati_remote->interface = interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Anssi Hannulac34516e2011-08-06 18:18:08 -0300817 usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
818 strlcpy(ati_remote->mouse_phys, ati_remote->rc_phys,
819 sizeof(ati_remote->mouse_phys));
820
821 strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
822 strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (udev->manufacturer)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300825 strlcpy(ati_remote->rc_name, udev->manufacturer,
826 sizeof(ati_remote->rc_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 if (udev->product)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300829 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
830 "%s %s", ati_remote->rc_name, udev->product);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Anssi Hannulac34516e2011-08-06 18:18:08 -0300832 if (!strlen(ati_remote->rc_name))
833 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500834 DRIVER_DESC "(%04x,%04x)",
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500835 le16_to_cpu(ati_remote->udev->descriptor.idVendor),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 le16_to_cpu(ati_remote->udev->descriptor.idProduct));
837
Anssi Hannulac34516e2011-08-06 18:18:08 -0300838 snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
839 "%s mouse", ati_remote->rc_name);
840
Anssi Hannula0d746792012-04-01 16:41:45 -0300841 rc_dev->map_name = RC_MAP_ATI_X10; /* default map */
842
843 /* set default keymap according to receiver model */
844 if (type) {
845 if (type->default_keymap)
846 rc_dev->map_name = type->default_keymap;
847 else if (type->get_default_keymap)
848 rc_dev->map_name = type->get_default_keymap(interface);
849 }
Anssi Hannula175fcec2011-08-06 18:18:11 -0300850
Anssi Hannulac34516e2011-08-06 18:18:08 -0300851 ati_remote_rc_init(ati_remote);
852 mutex_init(&ati_remote->open_mutex);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500855 err = ati_remote_initialize(ati_remote);
856 if (err)
857 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Anssi Hannulac34516e2011-08-06 18:18:08 -0300859 /* Set up and register rc device */
860 err = rc_register_device(ati_remote->rdev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400861 if (err)
862 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Anssi Hannulac34516e2011-08-06 18:18:08 -0300864 /* use our delay for rc_dev */
865 ati_remote->rdev->input_dev->rep[REP_DELAY] = repeat_delay;
866
867 /* Set up and register mouse input device */
868 if (mouse) {
869 input_dev = input_allocate_device();
870 if (!input_dev)
871 goto fail4;
872
873 ati_remote->idev = input_dev;
874 ati_remote_input_init(ati_remote);
875 err = input_register_device(input_dev);
876
877 if (err)
878 goto fail5;
879 }
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 usb_set_intfdata(interface, ati_remote);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500882 return 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500883
Anssi Hannulac34516e2011-08-06 18:18:08 -0300884 fail5: input_free_device(input_dev);
885 fail4: rc_unregister_device(rc_dev);
886 rc_dev = NULL;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400887 fail3: usb_kill_urb(ati_remote->irq_urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500888 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400889 fail2: ati_remote_free_buffers(ati_remote);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300890 fail1: rc_free_device(rc_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500891 kfree(ati_remote);
892 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
896 * ati_remote_disconnect
897 */
898static void ati_remote_disconnect(struct usb_interface *interface)
899{
900 struct ati_remote *ati_remote;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 ati_remote = usb_get_intfdata(interface);
903 usb_set_intfdata(interface, NULL);
904 if (!ati_remote) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700905 dev_warn(&interface->dev, "%s - null device?\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return;
907 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500908
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500909 usb_kill_urb(ati_remote->irq_urb);
910 usb_kill_urb(ati_remote->out_urb);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300911 if (ati_remote->idev)
912 input_unregister_device(ati_remote->idev);
913 rc_unregister_device(ati_remote->rdev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500914 ati_remote_free_buffers(ati_remote);
915 kfree(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800918module_usb_driver(ati_remote_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920MODULE_AUTHOR(DRIVER_AUTHOR);
921MODULE_DESCRIPTION(DRIVER_DESC);
922MODULE_LICENSE("GPL");