ati_remote.c 30 KB

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