ati_remote.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. /*
  2. * USB ATI Remote support
  3. *
  4. * Copyright (c) 2011 Anssi Hannula <anssi.hannula@iki.fi>
  5. * 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
  9. * porting to the 2.6 kernel interfaces, along with other modification
  10. * 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.
  12. *
  13. * The 2.1.1 driver was derived from the usbati_remote and usbkbd drivers by
  14. * 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. *
  27. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  28. *
  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
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * 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.
  38. *
  39. * 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
  42. *
  43. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  44. *
  45. * Hardware & software notes
  46. *
  47. * These remote controls are distributed by ATI as part of their
  48. * "All-In-Wonder" video card packages. The receiver self-identifies as a
  49. * "USB Receiver" with manufacturer "X10 Wireless Technology Inc".
  50. *
  51. * The "Lola" remote is available from X10. See:
  52. * 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. *
  56. * It is possible to use multiple receivers and remotes on multiple computers
  57. * simultaneously by configuring them to use specific channels.
  58. *
  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
  61. * hardware.
  62. *
  63. * Each remote can be configured to transmit on one channel as follows:
  64. * - 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
  67. * to 16, and press the hand icon again.
  68. *
  69. * 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
  74. * channels will be ignored by the module. To mask out channels, just add
  75. * all the 2^channel_number values together.
  76. *
  77. * For instance, set channel_mask = 2^4 = 16 (binary 10000) to make ati_remote
  78. * ignore signals coming from remote controls transmitting on channel 4, but
  79. * accept all other channels.
  80. *
  81. * Or, set channel_mask = 65533, (0xFFFD), and all channels except 1 will be
  82. * ignored.
  83. *
  84. * The default is 0 (respond to all channels). Bit 0 and bits 17-32 of this
  85. * parameter are unused.
  86. *
  87. */
  88. #include <linux/kernel.h>
  89. #include <linux/errno.h>
  90. #include <linux/init.h>
  91. #include <linux/slab.h>
  92. #include <linux/module.h>
  93. #include <linux/mutex.h>
  94. #include <linux/usb/input.h>
  95. #include <linux/wait.h>
  96. #include <linux/jiffies.h>
  97. #include <media/rc-core.h>
  98. /*
  99. * Module and Version Information, Module Parameters
  100. */
  101. #define ATI_REMOTE_VENDOR_ID 0x0bc7
  102. #define LOLA_REMOTE_PRODUCT_ID 0x0002
  103. #define LOLA2_REMOTE_PRODUCT_ID 0x0003
  104. #define ATI_REMOTE_PRODUCT_ID 0x0004
  105. #define NVIDIA_REMOTE_PRODUCT_ID 0x0005
  106. #define MEDION_REMOTE_PRODUCT_ID 0x0006
  107. #define DRIVER_VERSION "2.2.1"
  108. #define DRIVER_AUTHOR "Torrey Hoffman <thoffman@arnor.net>"
  109. #define DRIVER_DESC "ATI/X10 RF USB Remote Control"
  110. #define NAME_BUFSIZE 80 /* size of product name, path buffers */
  111. #define DATA_BUFSIZE 63 /* size of URB data buffers */
  112. /*
  113. * Duplicate event filtering time.
  114. * Sequential, identical KIND_FILTERED inputs with less than
  115. * FILTER_TIME milliseconds between them are considered as repeat
  116. * events. The hardware generates 5 events for the first keypress
  117. * and we have to take this into account for an accurate repeat
  118. * behaviour.
  119. */
  120. #define FILTER_TIME 60 /* msec */
  121. #define REPEAT_DELAY 500 /* msec */
  122. static unsigned long channel_mask;
  123. module_param(channel_mask, ulong, 0644);
  124. MODULE_PARM_DESC(channel_mask, "Bitmask of remote control channels to ignore");
  125. static int debug;
  126. module_param(debug, int, 0644);
  127. MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
  128. static int repeat_filter = FILTER_TIME;
  129. module_param(repeat_filter, int, 0644);
  130. MODULE_PARM_DESC(repeat_filter, "Repeat filter time, default = 60 msec");
  131. static int repeat_delay = REPEAT_DELAY;
  132. module_param(repeat_delay, int, 0644);
  133. MODULE_PARM_DESC(repeat_delay, "Delay before sending repeats, default = 500 msec");
  134. static bool mouse = true;
  135. module_param(mouse, bool, 0444);
  136. MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes");
  137. #define dbginfo(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0)
  138. #undef err
  139. #define err(format, arg...) printk(KERN_ERR format , ## arg)
  140. static struct usb_device_id ati_remote_table[] = {
  141. { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
  142. { USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
  143. { USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
  144. { USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_ATI_X10 },
  145. { USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID), .driver_info = (unsigned long)RC_MAP_MEDION_X10 },
  146. {} /* Terminating entry */
  147. };
  148. MODULE_DEVICE_TABLE(usb, ati_remote_table);
  149. /* Get hi and low bytes of a 16-bits int */
  150. #define HI(a) ((unsigned char)((a) >> 8))
  151. #define LO(a) ((unsigned char)((a) & 0xff))
  152. #define SEND_FLAG_IN_PROGRESS 1
  153. #define SEND_FLAG_COMPLETE 2
  154. /* Device initialization strings */
  155. static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
  156. static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
  157. struct ati_remote {
  158. struct input_dev *idev;
  159. struct rc_dev *rdev;
  160. struct usb_device *udev;
  161. struct usb_interface *interface;
  162. struct urb *irq_urb;
  163. struct urb *out_urb;
  164. struct usb_endpoint_descriptor *endpoint_in;
  165. struct usb_endpoint_descriptor *endpoint_out;
  166. unsigned char *inbuf;
  167. unsigned char *outbuf;
  168. dma_addr_t inbuf_dma;
  169. dma_addr_t outbuf_dma;
  170. unsigned char old_data[2]; /* Detect duplicate events */
  171. unsigned long old_jiffies;
  172. unsigned long acc_jiffies; /* handle acceleration */
  173. unsigned long first_jiffies;
  174. unsigned int repeat_count;
  175. char rc_name[NAME_BUFSIZE];
  176. char rc_phys[NAME_BUFSIZE];
  177. char mouse_name[NAME_BUFSIZE];
  178. char mouse_phys[NAME_BUFSIZE];
  179. wait_queue_head_t wait;
  180. int send_flags;
  181. int users; /* 0-2, users are rc and input */
  182. struct mutex open_mutex;
  183. };
  184. /* "Kinds" of messages sent from the hardware to the driver. */
  185. #define KIND_END 0
  186. #define KIND_LITERAL 1 /* Simply pass to input system */
  187. #define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */
  188. #define KIND_LU 3 /* Directional keypad diagonals - left up, */
  189. #define KIND_RU 4 /* right up, */
  190. #define KIND_LD 5 /* left down, */
  191. #define KIND_RD 6 /* right down */
  192. #define KIND_ACCEL 7 /* Directional keypad - left, right, up, down.*/
  193. /* Translation table from hardware messages to input events. */
  194. static const struct {
  195. short kind;
  196. unsigned char data1, data2;
  197. int type;
  198. unsigned int code;
  199. int value;
  200. } ati_remote_tbl[] = {
  201. /* Directional control pad axes */
  202. {KIND_ACCEL, 0x35, 0x70, EV_REL, REL_X, -1}, /* left */
  203. {KIND_ACCEL, 0x36, 0x71, EV_REL, REL_X, 1}, /* right */
  204. {KIND_ACCEL, 0x37, 0x72, EV_REL, REL_Y, -1}, /* up */
  205. {KIND_ACCEL, 0x38, 0x73, EV_REL, REL_Y, 1}, /* down */
  206. /* Directional control pad diagonals */
  207. {KIND_LU, 0x39, 0x74, EV_REL, 0, 0}, /* left up */
  208. {KIND_RU, 0x3a, 0x75, EV_REL, 0, 0}, /* right up */
  209. {KIND_LD, 0x3c, 0x77, EV_REL, 0, 0}, /* left down */
  210. {KIND_RD, 0x3b, 0x76, EV_REL, 0, 0}, /* right down */
  211. /* "Mouse button" buttons */
  212. {KIND_LITERAL, 0x3d, 0x78, EV_KEY, BTN_LEFT, 1}, /* left btn down */
  213. {KIND_LITERAL, 0x3e, 0x79, EV_KEY, BTN_LEFT, 0}, /* left btn up */
  214. {KIND_LITERAL, 0x41, 0x7c, EV_KEY, BTN_RIGHT, 1},/* right btn down */
  215. {KIND_LITERAL, 0x42, 0x7d, EV_KEY, BTN_RIGHT, 0},/* right btn up */
  216. /* Artificial "doubleclick" events are generated by the hardware.
  217. * They are mapped to the "side" and "extra" mouse buttons here. */
  218. {KIND_FILTERED, 0x3f, 0x7a, EV_KEY, BTN_SIDE, 1}, /* left dblclick */
  219. {KIND_FILTERED, 0x43, 0x7e, EV_KEY, BTN_EXTRA, 1},/* right dblclick */
  220. /* Non-mouse events are handled by rc-core */
  221. {KIND_END, 0x00, 0x00, EV_MAX + 1, 0, 0}
  222. };
  223. /* Local function prototypes */
  224. static int ati_remote_sendpacket (struct ati_remote *ati_remote, u16 cmd, unsigned char *data);
  225. static void ati_remote_irq_out (struct urb *urb);
  226. static void ati_remote_irq_in (struct urb *urb);
  227. static void ati_remote_input_report (struct urb *urb);
  228. static int ati_remote_initialize (struct ati_remote *ati_remote);
  229. static int ati_remote_probe (struct usb_interface *interface, const struct usb_device_id *id);
  230. static void ati_remote_disconnect (struct usb_interface *interface);
  231. /* usb specific object to register with the usb subsystem */
  232. static struct usb_driver ati_remote_driver = {
  233. .name = "ati_remote",
  234. .probe = ati_remote_probe,
  235. .disconnect = ati_remote_disconnect,
  236. .id_table = ati_remote_table,
  237. };
  238. /*
  239. * ati_remote_dump_input
  240. */
  241. static void ati_remote_dump(struct device *dev, unsigned char *data,
  242. unsigned int len)
  243. {
  244. if (len == 1) {
  245. if (data[0] != (unsigned char)0xff && data[0] != 0x00)
  246. dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
  247. } else if (len == 4)
  248. dev_warn(dev, "Weird key %02x %02x %02x %02x\n",
  249. data[0], data[1], data[2], data[3]);
  250. else
  251. dev_warn(dev, "Weird data, len=%d %02x %02x %02x %02x %02x %02x ...\n",
  252. len, data[0], data[1], data[2], data[3], data[4], data[5]);
  253. }
  254. /*
  255. * ati_remote_open
  256. */
  257. static int ati_remote_open(struct ati_remote *ati_remote)
  258. {
  259. int err = 0;
  260. mutex_lock(&ati_remote->open_mutex);
  261. if (ati_remote->users++ != 0)
  262. goto out; /* one was already active */
  263. /* On first open, submit the read urb which was set up previously. */
  264. ati_remote->irq_urb->dev = ati_remote->udev;
  265. if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
  266. dev_err(&ati_remote->interface->dev,
  267. "%s: usb_submit_urb failed!\n", __func__);
  268. err = -EIO;
  269. }
  270. out: mutex_unlock(&ati_remote->open_mutex);
  271. return err;
  272. }
  273. /*
  274. * ati_remote_close
  275. */
  276. static void ati_remote_close(struct ati_remote *ati_remote)
  277. {
  278. mutex_lock(&ati_remote->open_mutex);
  279. if (--ati_remote->users == 0)
  280. usb_kill_urb(ati_remote->irq_urb);
  281. mutex_unlock(&ati_remote->open_mutex);
  282. }
  283. static int ati_remote_input_open(struct input_dev *inputdev)
  284. {
  285. struct ati_remote *ati_remote = input_get_drvdata(inputdev);
  286. return ati_remote_open(ati_remote);
  287. }
  288. static void ati_remote_input_close(struct input_dev *inputdev)
  289. {
  290. struct ati_remote *ati_remote = input_get_drvdata(inputdev);
  291. ati_remote_close(ati_remote);
  292. }
  293. static int ati_remote_rc_open(struct rc_dev *rdev)
  294. {
  295. struct ati_remote *ati_remote = rdev->priv;
  296. return ati_remote_open(ati_remote);
  297. }
  298. static void ati_remote_rc_close(struct rc_dev *rdev)
  299. {
  300. struct ati_remote *ati_remote = rdev->priv;
  301. ati_remote_close(ati_remote);
  302. }
  303. /*
  304. * ati_remote_irq_out
  305. */
  306. static void ati_remote_irq_out(struct urb *urb)
  307. {
  308. struct ati_remote *ati_remote = urb->context;
  309. if (urb->status) {
  310. dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
  311. __func__, urb->status);
  312. return;
  313. }
  314. ati_remote->send_flags |= SEND_FLAG_COMPLETE;
  315. wmb();
  316. wake_up(&ati_remote->wait);
  317. }
  318. /*
  319. * ati_remote_sendpacket
  320. *
  321. * Used to send device initialization strings
  322. */
  323. static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, unsigned char *data)
  324. {
  325. int retval = 0;
  326. /* Set up out_urb */
  327. memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
  328. ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
  329. ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
  330. ati_remote->out_urb->dev = ati_remote->udev;
  331. ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
  332. retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
  333. if (retval) {
  334. dev_dbg(&ati_remote->interface->dev,
  335. "sendpacket: usb_submit_urb failed: %d\n", retval);
  336. return retval;
  337. }
  338. wait_event_timeout(ati_remote->wait,
  339. ((ati_remote->out_urb->status != -EINPROGRESS) ||
  340. (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
  341. HZ);
  342. usb_kill_urb(ati_remote->out_urb);
  343. return retval;
  344. }
  345. /*
  346. * ati_remote_event_lookup
  347. */
  348. static int ati_remote_event_lookup(int rem, unsigned char d1, unsigned char d2)
  349. {
  350. int i;
  351. for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
  352. /*
  353. * Decide if the table entry matches the remote input.
  354. */
  355. if (ati_remote_tbl[i].data1 == d1 &&
  356. ati_remote_tbl[i].data2 == d2)
  357. return i;
  358. }
  359. return -1;
  360. }
  361. /*
  362. * ati_remote_compute_accel
  363. *
  364. * Implements acceleration curve for directional control pad
  365. * If elapsed time since last event is > 1/4 second, user "stopped",
  366. * so reset acceleration. Otherwise, user is probably holding the control
  367. * pad down, so we increase acceleration, ramping up over two seconds to
  368. * a maximum speed.
  369. */
  370. static int ati_remote_compute_accel(struct ati_remote *ati_remote)
  371. {
  372. static const char accel[] = { 1, 2, 4, 6, 9, 13, 20 };
  373. unsigned long now = jiffies;
  374. int acc;
  375. if (time_after(now, ati_remote->old_jiffies + msecs_to_jiffies(250))) {
  376. acc = 1;
  377. ati_remote->acc_jiffies = now;
  378. }
  379. else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(125)))
  380. acc = accel[0];
  381. else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(250)))
  382. acc = accel[1];
  383. else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(500)))
  384. acc = accel[2];
  385. else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1000)))
  386. acc = accel[3];
  387. else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(1500)))
  388. acc = accel[4];
  389. else if (time_before(now, ati_remote->acc_jiffies + msecs_to_jiffies(2000)))
  390. acc = accel[5];
  391. else
  392. acc = accel[6];
  393. return acc;
  394. }
  395. /*
  396. * ati_remote_report_input
  397. */
  398. static void ati_remote_input_report(struct urb *urb)
  399. {
  400. struct ati_remote *ati_remote = urb->context;
  401. unsigned char *data= ati_remote->inbuf;
  402. struct input_dev *dev = ati_remote->idev;
  403. int index = -1;
  404. int acc;
  405. int remote_num;
  406. unsigned char scancode[2];
  407. /* Deal with strange looking inputs */
  408. if ( (urb->actual_length != 4) || (data[0] != 0x14) ||
  409. ((data[3] & 0x0f) != 0x00) ) {
  410. ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
  411. return;
  412. }
  413. /* Mask unwanted remote channels. */
  414. /* note: remote_num is 0-based, channel 1 on remote == 0 here */
  415. remote_num = (data[3] >> 4) & 0x0f;
  416. if (channel_mask & (1 << (remote_num + 1))) {
  417. dbginfo(&ati_remote->interface->dev,
  418. "Masked input from channel 0x%02x: data %02x,%02x, mask= 0x%02lx\n",
  419. remote_num, data[1], data[2], channel_mask);
  420. return;
  421. }
  422. scancode[0] = (((data[1] - ((remote_num + 1) << 4)) & 0xf0) | (data[1] & 0x0f));
  423. scancode[1] = data[2];
  424. /* Look up event code index in mouse translation table. */
  425. index = ati_remote_event_lookup(remote_num, scancode[0], scancode[1]);
  426. if (index >= 0) {
  427. dbginfo(&ati_remote->interface->dev,
  428. "channel 0x%02x; mouse data %02x,%02x; index %d; keycode %d\n",
  429. remote_num, data[1], data[2], index, ati_remote_tbl[index].code);
  430. if (!dev)
  431. return; /* no mouse device */
  432. } else
  433. dbginfo(&ati_remote->interface->dev,
  434. "channel 0x%02x; key data %02x,%02x, scancode %02x,%02x\n",
  435. remote_num, data[1], data[2], scancode[0], scancode[1]);
  436. if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
  437. input_event(dev, ati_remote_tbl[index].type,
  438. ati_remote_tbl[index].code,
  439. ati_remote_tbl[index].value);
  440. input_sync(dev);
  441. ati_remote->old_jiffies = jiffies;
  442. return;
  443. }
  444. if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
  445. unsigned long now = jiffies;
  446. /* Filter duplicate events which happen "too close" together. */
  447. if (ati_remote->old_data[0] == data[1] &&
  448. ati_remote->old_data[1] == data[2] &&
  449. time_before(now, ati_remote->old_jiffies +
  450. msecs_to_jiffies(repeat_filter))) {
  451. ati_remote->repeat_count++;
  452. } else {
  453. ati_remote->repeat_count = 0;
  454. ati_remote->first_jiffies = now;
  455. }
  456. ati_remote->old_data[0] = data[1];
  457. ati_remote->old_data[1] = data[2];
  458. ati_remote->old_jiffies = now;
  459. /* Ensure we skip at least the 4 first duplicate events (generated
  460. * by a single keypress), and continue skipping until repeat_delay
  461. * msecs have passed
  462. */
  463. if (ati_remote->repeat_count > 0 &&
  464. (ati_remote->repeat_count < 5 ||
  465. time_before(now, ati_remote->first_jiffies +
  466. msecs_to_jiffies(repeat_delay))))
  467. return;
  468. if (index < 0) {
  469. /* Not a mouse event, hand it to rc-core. */
  470. u32 rc_code = (scancode[0] << 8) | scancode[1];
  471. /*
  472. * We don't use the rc-core repeat handling yet as
  473. * it would cause ghost repeats which would be a
  474. * regression for this driver.
  475. */
  476. rc_keydown_notimeout(ati_remote->rdev, rc_code, 0);
  477. rc_keyup(ati_remote->rdev);
  478. return;
  479. }
  480. input_event(dev, ati_remote_tbl[index].type,
  481. ati_remote_tbl[index].code, 1);
  482. input_sync(dev);
  483. input_event(dev, ati_remote_tbl[index].type,
  484. ati_remote_tbl[index].code, 0);
  485. input_sync(dev);
  486. } else {
  487. /*
  488. * Other event kinds are from the directional control pad, and have an
  489. * acceleration factor applied to them. Without this acceleration, the
  490. * control pad is mostly unusable.
  491. */
  492. acc = ati_remote_compute_accel(ati_remote);
  493. switch (ati_remote_tbl[index].kind) {
  494. case KIND_ACCEL:
  495. input_event(dev, ati_remote_tbl[index].type,
  496. ati_remote_tbl[index].code,
  497. ati_remote_tbl[index].value * acc);
  498. break;
  499. case KIND_LU:
  500. input_report_rel(dev, REL_X, -acc);
  501. input_report_rel(dev, REL_Y, -acc);
  502. break;
  503. case KIND_RU:
  504. input_report_rel(dev, REL_X, acc);
  505. input_report_rel(dev, REL_Y, -acc);
  506. break;
  507. case KIND_LD:
  508. input_report_rel(dev, REL_X, -acc);
  509. input_report_rel(dev, REL_Y, acc);
  510. break;
  511. case KIND_RD:
  512. input_report_rel(dev, REL_X, acc);
  513. input_report_rel(dev, REL_Y, acc);
  514. break;
  515. default:
  516. dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
  517. ati_remote_tbl[index].kind);
  518. }
  519. input_sync(dev);
  520. ati_remote->old_jiffies = jiffies;
  521. ati_remote->old_data[0] = data[1];
  522. ati_remote->old_data[1] = data[2];
  523. }
  524. }
  525. /*
  526. * ati_remote_irq_in
  527. */
  528. static void ati_remote_irq_in(struct urb *urb)
  529. {
  530. struct ati_remote *ati_remote = urb->context;
  531. int retval;
  532. switch (urb->status) {
  533. case 0: /* success */
  534. ati_remote_input_report(urb);
  535. break;
  536. case -ECONNRESET: /* unlink */
  537. case -ENOENT:
  538. case -ESHUTDOWN:
  539. dev_dbg(&ati_remote->interface->dev, "%s: urb error status, unlink? \n",
  540. __func__);
  541. return;
  542. default: /* error */
  543. dev_dbg(&ati_remote->interface->dev, "%s: Nonzero urb status %d\n",
  544. __func__, urb->status);
  545. }
  546. retval = usb_submit_urb(urb, GFP_ATOMIC);
  547. if (retval)
  548. dev_err(&ati_remote->interface->dev, "%s: usb_submit_urb()=%d\n",
  549. __func__, retval);
  550. }
  551. /*
  552. * ati_remote_alloc_buffers
  553. */
  554. static int ati_remote_alloc_buffers(struct usb_device *udev,
  555. struct ati_remote *ati_remote)
  556. {
  557. ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
  558. &ati_remote->inbuf_dma);
  559. if (!ati_remote->inbuf)
  560. return -1;
  561. ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
  562. &ati_remote->outbuf_dma);
  563. if (!ati_remote->outbuf)
  564. return -1;
  565. ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  566. if (!ati_remote->irq_urb)
  567. return -1;
  568. ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
  569. if (!ati_remote->out_urb)
  570. return -1;
  571. return 0;
  572. }
  573. /*
  574. * ati_remote_free_buffers
  575. */
  576. static void ati_remote_free_buffers(struct ati_remote *ati_remote)
  577. {
  578. usb_free_urb(ati_remote->irq_urb);
  579. usb_free_urb(ati_remote->out_urb);
  580. usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
  581. ati_remote->inbuf, ati_remote->inbuf_dma);
  582. usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
  583. ati_remote->outbuf, ati_remote->outbuf_dma);
  584. }
  585. static void ati_remote_input_init(struct ati_remote *ati_remote)
  586. {
  587. struct input_dev *idev = ati_remote->idev;
  588. int i;
  589. idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
  590. idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
  591. BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
  592. idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
  593. for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
  594. if (ati_remote_tbl[i].type == EV_KEY)
  595. set_bit(ati_remote_tbl[i].code, idev->keybit);
  596. input_set_drvdata(idev, ati_remote);
  597. idev->open = ati_remote_input_open;
  598. idev->close = ati_remote_input_close;
  599. idev->name = ati_remote->mouse_name;
  600. idev->phys = ati_remote->mouse_phys;
  601. usb_to_input_id(ati_remote->udev, &idev->id);
  602. idev->dev.parent = &ati_remote->interface->dev;
  603. }
  604. static void ati_remote_rc_init(struct ati_remote *ati_remote)
  605. {
  606. struct rc_dev *rdev = ati_remote->rdev;
  607. rdev->priv = ati_remote;
  608. rdev->driver_type = RC_DRIVER_SCANCODE;
  609. rdev->allowed_protos = RC_TYPE_OTHER;
  610. rdev->driver_name = "ati_remote";
  611. rdev->open = ati_remote_rc_open;
  612. rdev->close = ati_remote_rc_close;
  613. rdev->input_name = ati_remote->rc_name;
  614. rdev->input_phys = ati_remote->rc_phys;
  615. usb_to_input_id(ati_remote->udev, &rdev->input_id);
  616. rdev->dev.parent = &ati_remote->interface->dev;
  617. }
  618. static int ati_remote_initialize(struct ati_remote *ati_remote)
  619. {
  620. struct usb_device *udev = ati_remote->udev;
  621. int pipe, maxp;
  622. init_waitqueue_head(&ati_remote->wait);
  623. /* Set up irq_urb */
  624. pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
  625. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  626. maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
  627. usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
  628. maxp, ati_remote_irq_in, ati_remote,
  629. ati_remote->endpoint_in->bInterval);
  630. ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
  631. ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  632. /* Set up out_urb */
  633. pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
  634. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  635. maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
  636. usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
  637. maxp, ati_remote_irq_out, ati_remote,
  638. ati_remote->endpoint_out->bInterval);
  639. ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
  640. ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  641. /* send initialization strings */
  642. if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
  643. (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
  644. dev_err(&ati_remote->interface->dev,
  645. "Initializing ati_remote hardware failed.\n");
  646. return -EIO;
  647. }
  648. return 0;
  649. }
  650. /*
  651. * ati_remote_probe
  652. */
  653. static int ati_remote_probe(struct usb_interface *interface, const struct usb_device_id *id)
  654. {
  655. struct usb_device *udev = interface_to_usbdev(interface);
  656. struct usb_host_interface *iface_host = interface->cur_altsetting;
  657. struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
  658. struct ati_remote *ati_remote;
  659. struct input_dev *input_dev;
  660. struct rc_dev *rc_dev;
  661. int err = -ENOMEM;
  662. if (iface_host->desc.bNumEndpoints != 2) {
  663. err("%s: Unexpected desc.bNumEndpoints\n", __func__);
  664. return -ENODEV;
  665. }
  666. endpoint_in = &iface_host->endpoint[0].desc;
  667. endpoint_out = &iface_host->endpoint[1].desc;
  668. if (!usb_endpoint_is_int_in(endpoint_in)) {
  669. err("%s: Unexpected endpoint_in\n", __func__);
  670. return -ENODEV;
  671. }
  672. if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
  673. err("%s: endpoint_in message size==0? \n", __func__);
  674. return -ENODEV;
  675. }
  676. ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
  677. rc_dev = rc_allocate_device();
  678. if (!ati_remote || !rc_dev)
  679. goto fail1;
  680. /* Allocate URB buffers, URBs */
  681. if (ati_remote_alloc_buffers(udev, ati_remote))
  682. goto fail2;
  683. ati_remote->endpoint_in = endpoint_in;
  684. ati_remote->endpoint_out = endpoint_out;
  685. ati_remote->udev = udev;
  686. ati_remote->rdev = rc_dev;
  687. ati_remote->interface = interface;
  688. usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
  689. strlcpy(ati_remote->mouse_phys, ati_remote->rc_phys,
  690. sizeof(ati_remote->mouse_phys));
  691. strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
  692. strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
  693. if (udev->manufacturer)
  694. strlcpy(ati_remote->rc_name, udev->manufacturer,
  695. sizeof(ati_remote->rc_name));
  696. if (udev->product)
  697. snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
  698. "%s %s", ati_remote->rc_name, udev->product);
  699. if (!strlen(ati_remote->rc_name))
  700. snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
  701. DRIVER_DESC "(%04x,%04x)",
  702. le16_to_cpu(ati_remote->udev->descriptor.idVendor),
  703. le16_to_cpu(ati_remote->udev->descriptor.idProduct));
  704. snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
  705. "%s mouse", ati_remote->rc_name);
  706. if (id->driver_info)
  707. rc_dev->map_name = (const char *)id->driver_info;
  708. else
  709. rc_dev->map_name = RC_MAP_ATI_X10;
  710. ati_remote_rc_init(ati_remote);
  711. mutex_init(&ati_remote->open_mutex);
  712. /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
  713. err = ati_remote_initialize(ati_remote);
  714. if (err)
  715. goto fail3;
  716. /* Set up and register rc device */
  717. err = rc_register_device(ati_remote->rdev);
  718. if (err)
  719. goto fail3;
  720. /* use our delay for rc_dev */
  721. ati_remote->rdev->input_dev->rep[REP_DELAY] = repeat_delay;
  722. /* Set up and register mouse input device */
  723. if (mouse) {
  724. input_dev = input_allocate_device();
  725. if (!input_dev)
  726. goto fail4;
  727. ati_remote->idev = input_dev;
  728. ati_remote_input_init(ati_remote);
  729. err = input_register_device(input_dev);
  730. if (err)
  731. goto fail5;
  732. }
  733. usb_set_intfdata(interface, ati_remote);
  734. return 0;
  735. fail5: input_free_device(input_dev);
  736. fail4: rc_unregister_device(rc_dev);
  737. rc_dev = NULL;
  738. fail3: usb_kill_urb(ati_remote->irq_urb);
  739. usb_kill_urb(ati_remote->out_urb);
  740. fail2: ati_remote_free_buffers(ati_remote);
  741. fail1: rc_free_device(rc_dev);
  742. kfree(ati_remote);
  743. return err;
  744. }
  745. /*
  746. * ati_remote_disconnect
  747. */
  748. static void ati_remote_disconnect(struct usb_interface *interface)
  749. {
  750. struct ati_remote *ati_remote;
  751. ati_remote = usb_get_intfdata(interface);
  752. usb_set_intfdata(interface, NULL);
  753. if (!ati_remote) {
  754. dev_warn(&interface->dev, "%s - null device?\n", __func__);
  755. return;
  756. }
  757. usb_kill_urb(ati_remote->irq_urb);
  758. usb_kill_urb(ati_remote->out_urb);
  759. if (ati_remote->idev)
  760. input_unregister_device(ati_remote->idev);
  761. rc_unregister_device(ati_remote->rdev);
  762. ati_remote_free_buffers(ati_remote);
  763. kfree(ati_remote);
  764. }
  765. /*
  766. * ati_remote_init
  767. */
  768. static int __init ati_remote_init(void)
  769. {
  770. int result;
  771. result = usb_register(&ati_remote_driver);
  772. if (result)
  773. printk(KERN_ERR KBUILD_MODNAME
  774. ": usb_register error #%d\n", result);
  775. else
  776. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  777. DRIVER_DESC "\n");
  778. return result;
  779. }
  780. /*
  781. * ati_remote_exit
  782. */
  783. static void __exit ati_remote_exit(void)
  784. {
  785. usb_deregister(&ati_remote_driver);
  786. }
  787. /*
  788. * module specification
  789. */
  790. module_init(ati_remote_init);
  791. module_exit(ati_remote_exit);
  792. MODULE_AUTHOR(DRIVER_AUTHOR);
  793. MODULE_DESCRIPTION(DRIVER_DESC);
  794. MODULE_LICENSE("GPL");