ati_remote.c 28 KB

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