ati_remote.c 29 KB

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