ati_remote.c 28 KB

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