ati_remote.c 28 KB

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