ati_remote.c 28 KB

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