ati_remote.c 28 KB

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