ati_remote.c 29 KB

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