blob: 303f22ea04c075792cd2953628230c82d9e01160 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static struct usb_device_id ati_remote_table[] = {
Anssi Hannula175fcec2011-08-06 18:18:11 -0300155 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
156 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
157 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
158 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
159 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_MEDION_X10 },
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300160 { USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_SNAPSTREAM_FIREFLY },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 {} /* Terminating entry */
162};
163
164MODULE_DEVICE_TABLE(usb, ati_remote_table);
165
166/* Get hi and low bytes of a 16-bits int */
167#define HI(a) ((unsigned char)((a) >> 8))
168#define LO(a) ((unsigned char)((a) & 0xff))
169
170#define SEND_FLAG_IN_PROGRESS 1
171#define SEND_FLAG_COMPLETE 2
172
173/* Device initialization strings */
174static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
175static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177struct ati_remote {
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500178 struct input_dev *idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300179 struct rc_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct usb_device *udev;
181 struct usb_interface *interface;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct urb *irq_urb;
184 struct urb *out_urb;
185 struct usb_endpoint_descriptor *endpoint_in;
186 struct usb_endpoint_descriptor *endpoint_out;
187 unsigned char *inbuf;
188 unsigned char *outbuf;
189 dma_addr_t inbuf_dma;
190 dma_addr_t outbuf_dma;
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned char old_data[2]; /* Detect duplicate events */
193 unsigned long old_jiffies;
194 unsigned long acc_jiffies; /* handle acceleration */
Karl Pickett0de95502007-04-12 01:35:59 -0400195 unsigned long first_jiffies;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned int repeat_count;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500198
Anssi Hannulac34516e2011-08-06 18:18:08 -0300199 char rc_name[NAME_BUFSIZE];
200 char rc_phys[NAME_BUFSIZE];
201 char mouse_name[NAME_BUFSIZE];
202 char mouse_phys[NAME_BUFSIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 wait_queue_head_t wait;
205 int send_flags;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300206
207 int users; /* 0-2, users are rc and input */
208 struct mutex open_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
211/* "Kinds" of messages sent from the hardware to the driver. */
212#define KIND_END 0
213#define KIND_LITERAL 1 /* Simply pass to input system */
214#define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
215#define KIND_LU 3 /* Directional keypad diagonals - left up, */
216#define KIND_RU 4 /* right up, */
217#define KIND_LD 5 /* left down, */
218#define KIND_RD 6 /* right down */
219#define KIND_ACCEL 7 /* Directional keypad - left, right, up, down.*/
220
221/* Translation table from hardware messages to input events. */
Arjan van de Ven4c4c9432005-11-29 09:43:42 +0100222static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 short kind;
224 unsigned char data1, data2;
225 int type;
226 unsigned int code;
227 int value;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500228} ati_remote_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* Directional control pad axes */
230 {KIND_ACCEL, 0x35, 0x70, EV_REL, REL_X, -1}, /* left */
231 {KIND_ACCEL, 0x36, 0x71, EV_REL, REL_X, 1}, /* right */
232 {KIND_ACCEL, 0x37, 0x72, EV_REL, REL_Y, -1}, /* up */
233 {KIND_ACCEL, 0x38, 0x73, EV_REL, REL_Y, 1}, /* down */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500234 /* Directional control pad diagonals */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 {KIND_LU, 0x39, 0x74, EV_REL, 0, 0}, /* left up */
236 {KIND_RU, 0x3a, 0x75, EV_REL, 0, 0}, /* right up */
237 {KIND_LD, 0x3c, 0x77, EV_REL, 0, 0}, /* left down */
238 {KIND_RD, 0x3b, 0x76, EV_REL, 0, 0}, /* right down */
239
240 /* "Mouse button" buttons */
241 {KIND_LITERAL, 0x3d, 0x78, EV_KEY, BTN_LEFT, 1}, /* left btn down */
242 {KIND_LITERAL, 0x3e, 0x79, EV_KEY, BTN_LEFT, 0}, /* left btn up */
243 {KIND_LITERAL, 0x41, 0x7c, EV_KEY, BTN_RIGHT, 1},/* right btn down */
244 {KIND_LITERAL, 0x42, 0x7d, EV_KEY, BTN_RIGHT, 0},/* right btn up */
245
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500246 /* Artificial "doubleclick" events are generated by the hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * They are mapped to the "side" and "extra" mouse buttons here. */
248 {KIND_FILTERED, 0x3f, 0x7a, EV_KEY, BTN_SIDE, 1}, /* left dblclick */
249 {KIND_FILTERED, 0x43, 0x7e, EV_KEY, BTN_EXTRA, 1},/* right dblclick */
250
Anssi Hannulac34516e2011-08-06 18:18:08 -0300251 /* Non-mouse events are handled by rc-core */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 {KIND_END, 0x00, 0x00, EV_MAX + 1, 0, 0}
253};
254
255/* Local function prototypes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static int ati_remote_sendpacket (struct ati_remote *ati_remote, u16 cmd, unsigned char *data);
David Howells7d12e782006-10-05 14:55:46 +0100257static void ati_remote_irq_out (struct urb *urb);
258static void ati_remote_irq_in (struct urb *urb);
259static void ati_remote_input_report (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int ati_remote_initialize (struct ati_remote *ati_remote);
261static int ati_remote_probe (struct usb_interface *interface, const struct usb_device_id *id);
262static void ati_remote_disconnect (struct usb_interface *interface);
263
264/* usb specific object to register with the usb subsystem */
265static struct usb_driver ati_remote_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .name = "ati_remote",
267 .probe = ati_remote_probe,
268 .disconnect = ati_remote_disconnect,
269 .id_table = ati_remote_table,
270};
271
272/*
273 * ati_remote_dump_input
274 */
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700275static void ati_remote_dump(struct device *dev, unsigned char *data,
276 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Anssi Hannula0224e042011-08-06 18:18:10 -0300278 if (len == 1) {
279 if (data[0] != (unsigned char)0xff && data[0] != 0x00)
280 dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
281 } else if (len == 4)
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700282 dev_warn(dev, "Weird key %02x %02x %02x %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 data[0], data[1], data[2], data[3]);
284 else
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700285 dev_warn(dev, "Weird data, len=%d %02x %02x %02x %02x %02x %02x ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 len, data[0], data[1], data[2], data[3], data[4], data[5]);
287}
288
289/*
290 * ati_remote_open
291 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300292static int ati_remote_open(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Anssi Hannulac34516e2011-08-06 18:18:08 -0300294 int err = 0;
295
296 mutex_lock(&ati_remote->open_mutex);
297
298 if (ati_remote->users++ != 0)
299 goto out; /* one was already active */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* On first open, submit the read urb which was set up previously. */
302 ati_remote->irq_urb->dev = ati_remote->udev;
303 if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500304 dev_err(&ati_remote->interface->dev,
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400305 "%s: usb_submit_urb failed!\n", __func__);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300306 err = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Anssi Hannulac34516e2011-08-06 18:18:08 -0300309out: mutex_unlock(&ati_remote->open_mutex);
310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
314 * ati_remote_close
315 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300316static void ati_remote_close(struct ati_remote *ati_remote)
317{
318 mutex_lock(&ati_remote->open_mutex);
319 if (--ati_remote->users == 0)
320 usb_kill_urb(ati_remote->irq_urb);
321 mutex_unlock(&ati_remote->open_mutex);
322}
323
324static int ati_remote_input_open(struct input_dev *inputdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400326 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300327 return ati_remote_open(ati_remote);
328}
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500329
Anssi Hannulac34516e2011-08-06 18:18:08 -0300330static void ati_remote_input_close(struct input_dev *inputdev)
331{
332 struct ati_remote *ati_remote = input_get_drvdata(inputdev);
333 ati_remote_close(ati_remote);
334}
335
336static int ati_remote_rc_open(struct rc_dev *rdev)
337{
338 struct ati_remote *ati_remote = rdev->priv;
339 return ati_remote_open(ati_remote);
340}
341
342static void ati_remote_rc_close(struct rc_dev *rdev)
343{
344 struct ati_remote *ati_remote = rdev->priv;
345 ati_remote_close(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348/*
349 * ati_remote_irq_out
350 */
David Howells7d12e782006-10-05 14:55:46 +0100351static void ati_remote_irq_out(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct ati_remote *ati_remote = urb->context;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (urb->status) {
356 dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400357 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return;
359 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 ati_remote->send_flags |= SEND_FLAG_COMPLETE;
362 wmb();
363 wake_up(&ati_remote->wait);
364}
365
366/*
367 * ati_remote_sendpacket
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500368 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * Used to send device initialization strings
370 */
371static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, unsigned char *data)
372{
373 int retval = 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* Set up out_urb */
376 memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500377 ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
380 ati_remote->out_urb->dev = ati_remote->udev;
381 ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
382
383 retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
384 if (retval) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500385 dev_dbg(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 "sendpacket: usb_submit_urb failed: %d\n", retval);
387 return retval;
388 }
389
390 wait_event_timeout(ati_remote->wait,
391 ((ati_remote->out_urb->status != -EINPROGRESS) ||
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500392 (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 HZ);
394 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return retval;
397}
398
399/*
400 * ati_remote_event_lookup
401 */
402static int ati_remote_event_lookup(int rem, unsigned char d1, unsigned char d2)
403{
404 int i;
405
406 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500407 /*
408 * Decide if the table entry matches the remote input.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Anssi Hannulac34516e2011-08-06 18:18:08 -0300410 if (ati_remote_tbl[i].data1 == d1 &&
411 ati_remote_tbl[i].data2 == d2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return i;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 return -1;
416}
417
418/*
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400419 * ati_remote_compute_accel
420 *
421 * Implements acceleration curve for directional control pad
422 * If elapsed time since last event is > 1/4 second, user "stopped",
423 * so reset acceleration. Otherwise, user is probably holding the control
424 * pad down, so we increase acceleration, ramping up over two seconds to
425 * a maximum speed.
426 */
427static int ati_remote_compute_accel(struct ati_remote *ati_remote)
428{
429 static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
430 unsigned long now = jiffies;
431 int acc;
432
433 if (time_after(now, ati_remote->old_jiffies + msecs_to_jiffies(250))) {
434 acc = 1;
435 ati_remote->acc_jiffies = now;
436 }
437 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(125)))
438 acc = accel[0];
439 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(250)))
440 acc = accel[1];
441 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(500)))
442 acc = accel[2];
443 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1000)))
444 acc = accel[3];
445 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1500)))
446 acc = accel[4];
447 else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(2000)))
448 acc = accel[5];
449 else
450 acc = accel[6];
451
452 return acc;
453}
454
455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * ati_remote_report_input
457 */
David Howells7d12e782006-10-05 14:55:46 +0100458static void ati_remote_input_report(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct ati_remote *ati_remote = urb->context;
461 unsigned char *data= ati_remote->inbuf;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500462 struct input_dev *dev = ati_remote->idev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300463 int index = -1;
464 int acc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int remote_num;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300466 unsigned char scancode[2];
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Deal with strange looking inputs */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500469 if ( (urb->actual_length != 4) || (data[0] != 0x14) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ((data[3] & 0x0f) != 0x00) ) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700471 ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return;
473 }
474
475 /* Mask unwanted remote channels. */
476 /* note: remote_num is 0-based, channel 1 on remote == 0 here */
477 remote_num = (data[3] >> 4) & 0x0f;
Anssi Hannula9688efd2011-08-06 18:18:07 -0300478 if (channel_mask & (1 << (remote_num + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dbginfo(&ati_remote->interface->dev,
480 "Masked input from channel 0x%02x: data %02x,%02x, mask= 0x%02lx\n",
481 remote_num, data[1], data[2], channel_mask);
482 return;
483 }
484
Anssi Hannulac34516e2011-08-06 18:18:08 -0300485 scancode[0] = (((data[1] - ((remote_num + 1) << 4)) & 0xf0) | (data[1] & 0x0f));
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500486
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300487 /*
488 * Some devices (e.g. SnapStream Firefly) use 8080 as toggle code,
489 * so we have to clear them. The first bit is a bit tricky as the
490 * "non-toggled" state depends on remote_num, so we xor it with the
491 * second bit which is only used for toggle.
492 */
493 scancode[0] ^= (data[2] & 0x80);
494
495 scancode[1] = data[2] & ~0x80;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300496
497 /* Look up event code index in mouse translation table. */
498 index = ati_remote_event_lookup(remote_num, scancode[0], scancode[1]);
499
500 if (index >= 0) {
501 dbginfo(&ati_remote->interface->dev,
502 "channel 0x%02x; mouse data %02x,%02x; index %d; keycode %d\n",
503 remote_num, data[1], data[2], index, ati_remote_tbl[index].code);
504 if (!dev)
505 return; /* no mouse device */
506 } else
507 dbginfo(&ati_remote->interface->dev,
508 "channel 0x%02x; key data %02x,%02x, scancode %02x,%02x\n",
509 remote_num, data[1], data[2], scancode[0], scancode[1]);
510
511
512 if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 input_event(dev, ati_remote_tbl[index].type,
514 ati_remote_tbl[index].code,
515 ati_remote_tbl[index].value);
516 input_sync(dev);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ati_remote->old_jiffies = jiffies;
519 return;
520 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500521
Anssi Hannulac34516e2011-08-06 18:18:08 -0300522 if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
Karl Pickett0de95502007-04-12 01:35:59 -0400523 unsigned long now = jiffies;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Filter duplicate events which happen "too close" together. */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400526 if (ati_remote->old_data[0] == data[1] &&
527 ati_remote->old_data[1] == data[2] &&
Karl Pickett0de95502007-04-12 01:35:59 -0400528 time_before(now, ati_remote->old_jiffies +
529 msecs_to_jiffies(repeat_filter))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ati_remote->repeat_count++;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 ati_remote->repeat_count = 0;
Karl Pickett0de95502007-04-12 01:35:59 -0400533 ati_remote->first_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ati_remote->old_data[0] = data[1];
537 ati_remote->old_data[1] = data[2];
Karl Pickett0de95502007-04-12 01:35:59 -0400538 ati_remote->old_jiffies = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Karl Pickett0de95502007-04-12 01:35:59 -0400540 /* Ensure we skip at least the 4 first duplicate events (generated
541 * by a single keypress), and continue skipping until repeat_delay
542 * msecs have passed
543 */
Edwin Huffstutlerc6698692006-08-04 22:53:02 -0400544 if (ati_remote->repeat_count > 0 &&
Karl Pickett0de95502007-04-12 01:35:59 -0400545 (ati_remote->repeat_count < 5 ||
546 time_before(now, ati_remote->first_jiffies +
547 msecs_to_jiffies(repeat_delay))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500549
Anssi Hannulac34516e2011-08-06 18:18:08 -0300550 if (index < 0) {
551 /* Not a mouse event, hand it to rc-core. */
552 u32 rc_code = (scancode[0] << 8) | scancode[1];
553
554 /*
555 * We don't use the rc-core repeat handling yet as
556 * it would cause ghost repeats which would be a
557 * regression for this driver.
558 */
Anssi Hannula999d6bc2011-08-06 18:18:12 -0300559 rc_keydown_notimeout(ati_remote->rdev, rc_code,
560 data[2]);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300561 rc_keyup(ati_remote->rdev);
562 return;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 input_event(dev, ati_remote_tbl[index].type,
566 ati_remote_tbl[index].code, 1);
Dmitry Torokhovc3c38fb2006-08-04 22:53:24 -0400567 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 input_event(dev, ati_remote_tbl[index].type,
569 ati_remote_tbl[index].code, 0);
570 input_sync(dev);
571
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400572 } else {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500573
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400574 /*
575 * Other event kinds are from the directional control pad, and have an
576 * acceleration factor applied to them. Without this acceleration, the
577 * control pad is mostly unusable.
578 */
579 acc = ati_remote_compute_accel(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400581 switch (ati_remote_tbl[index].kind) {
582 case KIND_ACCEL:
583 input_event(dev, ati_remote_tbl[index].type,
584 ati_remote_tbl[index].code,
585 ati_remote_tbl[index].value * acc);
586 break;
587 case KIND_LU:
588 input_report_rel(dev, REL_X, -acc);
589 input_report_rel(dev, REL_Y, -acc);
590 break;
591 case KIND_RU:
592 input_report_rel(dev, REL_X, acc);
593 input_report_rel(dev, REL_Y, -acc);
594 break;
595 case KIND_LD:
596 input_report_rel(dev, REL_X, -acc);
597 input_report_rel(dev, REL_Y, acc);
598 break;
599 case KIND_RD:
600 input_report_rel(dev, REL_X, acc);
601 input_report_rel(dev, REL_Y, acc);
602 break;
603 default:
604 dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
605 ati_remote_tbl[index].kind);
606 }
607 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Dmitry Torokhov2ffc1cc2006-08-04 22:53:37 -0400609 ati_remote->old_jiffies = jiffies;
610 ati_remote->old_data[0] = data[1];
611 ati_remote->old_data[1] = data[2];
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/*
616 * ati_remote_irq_in
617 */
David Howells7d12e782006-10-05 14:55:46 +0100618static void ati_remote_irq_in(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
620 struct ati_remote *ati_remote = urb->context;
621 int retval;
622
623 switch (urb->status) {
624 case 0: /* success */
David Howells7d12e782006-10-05 14:55:46 +0100625 ati_remote_input_report(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
627 case -ECONNRESET: /* unlink */
628 case -ENOENT:
629 case -ESHUTDOWN:
630 dev_dbg(&ati_remote->interface->dev, "%s: urb error status, unlink? \n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400631 __func__);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 default: /* error */
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500634 dev_dbg(&ati_remote->interface->dev, "%s: Nonzero urb status %d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400635 __func__, urb->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500637
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800638 retval = usb_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (retval)
640 dev_err(&ati_remote->interface->dev, "%s: usb_submit_urb()=%d\n",
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400641 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500645 * ati_remote_alloc_buffers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500647static int ati_remote_alloc_buffers(struct usb_device *udev,
648 struct ati_remote *ati_remote)
649{
Daniel Mack997ea582010-04-12 13:17:25 +0200650 ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
651 &ati_remote->inbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500652 if (!ati_remote->inbuf)
653 return -1;
654
Daniel Mack997ea582010-04-12 13:17:25 +0200655 ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
Anssi Hannula9688efd2011-08-06 18:18:07 -0300656 &ati_remote->outbuf_dma);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500657 if (!ati_remote->outbuf)
658 return -1;
659
660 ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
661 if (!ati_remote->irq_urb)
662 return -1;
663
664 ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
665 if (!ati_remote->out_urb)
666 return -1;
667
668 return 0;
669}
670
671/*
672 * ati_remote_free_buffers
673 */
674static void ati_remote_free_buffers(struct ati_remote *ati_remote)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Mariusz Kozlowski459f8362006-11-08 15:35:46 +0100676 usb_free_urb(ati_remote->irq_urb);
677 usb_free_urb(ati_remote->out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Daniel Mack997ea582010-04-12 13:17:25 +0200679 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800680 ati_remote->inbuf, ati_remote->inbuf_dma);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500681
Daniel Mack997ea582010-04-12 13:17:25 +0200682 usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
Mariusz Kozlowski9dce4472006-11-25 11:09:38 -0800683 ati_remote->outbuf, ati_remote->outbuf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686static void ati_remote_input_init(struct ati_remote *ati_remote)
687{
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500688 struct input_dev *idev = ati_remote->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 int i;
690
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700691 idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
692 idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
693 BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
694 idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
696 if (ati_remote_tbl[i].type == EV_KEY)
697 set_bit(ati_remote_tbl[i].code, idev->keybit);
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500698
Dmitry Torokhov7791bda2007-04-12 01:34:39 -0400699 input_set_drvdata(idev, ati_remote);
700
Anssi Hannulac34516e2011-08-06 18:18:08 -0300701 idev->open = ati_remote_input_open;
702 idev->close = ati_remote_input_close;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500703
Anssi Hannulac34516e2011-08-06 18:18:08 -0300704 idev->name = ati_remote->mouse_name;
705 idev->phys = ati_remote->mouse_phys;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500706
Dmitry Torokhov16a334c2005-06-30 00:49:08 -0500707 usb_to_input_id(ati_remote->udev, &idev->id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300708 idev->dev.parent = &ati_remote->interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Anssi Hannulac34516e2011-08-06 18:18:08 -0300711static void ati_remote_rc_init(struct ati_remote *ati_remote)
712{
713 struct rc_dev *rdev = ati_remote->rdev;
714
715 rdev->priv = ati_remote;
716 rdev->driver_type = RC_DRIVER_SCANCODE;
717 rdev->allowed_protos = RC_TYPE_OTHER;
718 rdev->driver_name = "ati_remote";
719
720 rdev->open = ati_remote_rc_open;
721 rdev->close = ati_remote_rc_close;
722
723 rdev->input_name = ati_remote->rc_name;
724 rdev->input_phys = ati_remote->rc_phys;
725
726 usb_to_input_id(ati_remote->udev, &rdev->input_id);
Anssi Hannula44fd0b62011-08-06 18:18:09 -0300727 rdev->dev.parent = &ati_remote->interface->dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static int ati_remote_initialize(struct ati_remote *ati_remote)
731{
732 struct usb_device *udev = ati_remote->udev;
733 int pipe, maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 init_waitqueue_head(&ati_remote->wait);
736
737 /* Set up irq_urb */
738 pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
739 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
740 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500741
742 usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
743 maxp, ati_remote_irq_in, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 ati_remote->endpoint_in->bInterval);
745 ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
746 ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* Set up out_urb */
749 pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
750 maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
751 maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
752
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500753 usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
754 maxp, ati_remote_irq_out, ati_remote,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 ati_remote->endpoint_out->bInterval);
756 ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
757 ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
758
759 /* send initialization strings */
760 if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
761 (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500762 dev_err(&ati_remote->interface->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 "Initializing ati_remote hardware failed.\n");
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500764 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return 0;
768}
769
770/*
771 * ati_remote_probe
772 */
773static int ati_remote_probe(struct usb_interface *interface, const struct usb_device_id *id)
774{
775 struct usb_device *udev = interface_to_usbdev(interface);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500776 struct usb_host_interface *iface_host = interface->cur_altsetting;
777 struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
778 struct ati_remote *ati_remote;
779 struct input_dev *input_dev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300780 struct rc_dev *rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500781 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (iface_host->desc.bNumEndpoints != 2) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400784 err("%s: Unexpected desc.bNumEndpoints\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500785 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500788 endpoint_in = &iface_host->endpoint[0].desc;
789 endpoint_out = &iface_host->endpoint[1].desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Luiz Fernando N. Capitulino96642a22006-09-27 11:58:53 -0700791 if (!usb_endpoint_is_int_in(endpoint_in)) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400792 err("%s: Unexpected endpoint_in\n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500793 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500795 if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
Harvey Harrisonea3e6c52008-05-05 11:36:18 -0400796 err("%s: endpoint_in message size==0? \n", __func__);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500797 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500800 ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300801 rc_dev = rc_allocate_device();
802 if (!ati_remote || !rc_dev)
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500803 goto fail1;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* Allocate URB buffers, URBs */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500806 if (ati_remote_alloc_buffers(udev, ati_remote))
807 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500809 ati_remote->endpoint_in = endpoint_in;
810 ati_remote->endpoint_out = endpoint_out;
811 ati_remote->udev = udev;
Anssi Hannulac34516e2011-08-06 18:18:08 -0300812 ati_remote->rdev = rc_dev;
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500813 ati_remote->interface = interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Anssi Hannulac34516e2011-08-06 18:18:08 -0300815 usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
816 strlcpy(ati_remote->mouse_phys, ati_remote->rc_phys,
817 sizeof(ati_remote->mouse_phys));
818
819 strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
820 strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (udev->manufacturer)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300823 strlcpy(ati_remote->rc_name, udev->manufacturer,
824 sizeof(ati_remote->rc_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (udev->product)
Anssi Hannulac34516e2011-08-06 18:18:08 -0300827 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
828 "%s %s", ati_remote->rc_name, udev->product);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Anssi Hannulac34516e2011-08-06 18:18:08 -0300830 if (!strlen(ati_remote->rc_name))
831 snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500832 DRIVER_DESC "(%04x,%04x)",
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500833 le16_to_cpu(ati_remote->udev->descriptor.idVendor),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 le16_to_cpu(ati_remote->udev->descriptor.idProduct));
835
Anssi Hannulac34516e2011-08-06 18:18:08 -0300836 snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
837 "%s mouse", ati_remote->rc_name);
838
Anssi Hannula175fcec2011-08-06 18:18:11 -0300839 if (id->driver_info)
840 rc_dev->map_name = (const char *)id->driver_info;
841 else
842 rc_dev->map_name = RC_MAP_ATI_X10;
843
Anssi Hannulac34516e2011-08-06 18:18:08 -0300844 ati_remote_rc_init(ati_remote);
845 mutex_init(&ati_remote->open_mutex);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500848 err = ati_remote_initialize(ati_remote);
849 if (err)
850 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Anssi Hannulac34516e2011-08-06 18:18:08 -0300852 /* Set up and register rc device */
853 err = rc_register_device(ati_remote->rdev);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400854 if (err)
855 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Anssi Hannulac34516e2011-08-06 18:18:08 -0300857 /* use our delay for rc_dev */
858 ati_remote->rdev->input_dev->rep[REP_DELAY] = repeat_delay;
859
860 /* Set up and register mouse input device */
861 if (mouse) {
862 input_dev = input_allocate_device();
863 if (!input_dev)
864 goto fail4;
865
866 ati_remote->idev = input_dev;
867 ati_remote_input_init(ati_remote);
868 err = input_register_device(input_dev);
869
870 if (err)
871 goto fail5;
872 }
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 usb_set_intfdata(interface, ati_remote);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500875 return 0;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500876
Anssi Hannulac34516e2011-08-06 18:18:08 -0300877 fail5: input_free_device(input_dev);
878 fail4: rc_unregister_device(rc_dev);
879 rc_dev = NULL;
Dmitry Torokhov50141862007-04-12 01:33:39 -0400880 fail3: usb_kill_urb(ati_remote->irq_urb);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500881 usb_kill_urb(ati_remote->out_urb);
Dmitry Torokhov50141862007-04-12 01:33:39 -0400882 fail2: ati_remote_free_buffers(ati_remote);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300883 fail1: rc_free_device(rc_dev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500884 kfree(ati_remote);
885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888/*
889 * ati_remote_disconnect
890 */
891static void ati_remote_disconnect(struct usb_interface *interface)
892{
893 struct ati_remote *ati_remote;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 ati_remote = usb_get_intfdata(interface);
896 usb_set_intfdata(interface, NULL);
897 if (!ati_remote) {
Greg Kroah-Hartman1817b162008-08-14 09:37:34 -0700898 dev_warn(&interface->dev, "%s - null device?\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return;
900 }
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500901
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500902 usb_kill_urb(ati_remote->irq_urb);
903 usb_kill_urb(ati_remote->out_urb);
Anssi Hannulac34516e2011-08-06 18:18:08 -0300904 if (ati_remote->idev)
905 input_unregister_device(ati_remote->idev);
906 rc_unregister_device(ati_remote->rdev);
Dmitry Torokhovc5b7c7c2005-09-15 02:01:47 -0500907 ati_remote_free_buffers(ati_remote);
908 kfree(ati_remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
912 * ati_remote_init
913 */
914static int __init ati_remote_init(void)
915{
916 int result;
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 result = usb_register(&ati_remote_driver);
919 if (result)
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700920 printk(KERN_ERR KBUILD_MODNAME
921 ": usb_register error #%d\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 else
Greg Kroah-Hartman899ef6e2008-08-18 13:21:04 -0700923 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
924 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 return result;
927}
928
929/*
930 * ati_remote_exit
931 */
932static void __exit ati_remote_exit(void)
933{
934 usb_deregister(&ati_remote_driver);
935}
936
Dmitry Torokhov05f091ab2005-05-29 02:29:01 -0500937/*
938 * module specification
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
940
941module_init(ati_remote_init);
942module_exit(ati_remote_exit);
943
944MODULE_AUTHOR(DRIVER_AUTHOR);
945MODULE_DESCRIPTION(DRIVER_DESC);
946MODULE_LICENSE("GPL");