ati_remote2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * ati_remote2 - ATI/Philips USB RF remote driver
  3. *
  4. * Copyright (C) 2005 Ville Syrjala <syrjala@sci.fi>
  5. * Copyright (C) 2007 Peter Stokes <linux@dadeos.freeserve.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. */
  11. #include <linux/usb/input.h>
  12. #define DRIVER_DESC "ATI/Philips USB RF remote driver"
  13. #define DRIVER_VERSION "0.2"
  14. MODULE_DESCRIPTION(DRIVER_DESC);
  15. MODULE_VERSION(DRIVER_VERSION);
  16. MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
  17. MODULE_LICENSE("GPL");
  18. /*
  19. * ATI Remote Wonder II Channel Configuration
  20. *
  21. * The remote control can by assigned one of sixteen "channels" in order to facilitate
  22. * the use of multiple remote controls within range of each other.
  23. * A remote's "channel" may be altered by pressing and holding the "PC" button for
  24. * approximately 3 seconds, after which the button will slowly flash the count of the
  25. * currently configured "channel", using the numeric keypad enter a number between 1 and
  26. * 16 and then the "PC" button again, the button will slowly flash the count of the
  27. * newly configured "channel".
  28. */
  29. static unsigned int channel_mask = 0xFFFF;
  30. module_param(channel_mask, uint, 0644);
  31. MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>");
  32. static unsigned int mode_mask = 0x1F;
  33. module_param(mode_mask, uint, 0644);
  34. MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>");
  35. static struct usb_device_id ati_remote2_id_table[] = {
  36. { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */
  37. { }
  38. };
  39. MODULE_DEVICE_TABLE(usb, ati_remote2_id_table);
  40. static struct {
  41. int hw_code;
  42. int key_code;
  43. } ati_remote2_key_table[] = {
  44. { 0x00, KEY_0 },
  45. { 0x01, KEY_1 },
  46. { 0x02, KEY_2 },
  47. { 0x03, KEY_3 },
  48. { 0x04, KEY_4 },
  49. { 0x05, KEY_5 },
  50. { 0x06, KEY_6 },
  51. { 0x07, KEY_7 },
  52. { 0x08, KEY_8 },
  53. { 0x09, KEY_9 },
  54. { 0x0c, KEY_POWER },
  55. { 0x0d, KEY_MUTE },
  56. { 0x10, KEY_VOLUMEUP },
  57. { 0x11, KEY_VOLUMEDOWN },
  58. { 0x20, KEY_CHANNELUP },
  59. { 0x21, KEY_CHANNELDOWN },
  60. { 0x28, KEY_FORWARD },
  61. { 0x29, KEY_REWIND },
  62. { 0x2c, KEY_PLAY },
  63. { 0x30, KEY_PAUSE },
  64. { 0x31, KEY_STOP },
  65. { 0x37, KEY_RECORD },
  66. { 0x38, KEY_DVD },
  67. { 0x39, KEY_TV },
  68. { 0x54, KEY_MENU },
  69. { 0x58, KEY_UP },
  70. { 0x59, KEY_DOWN },
  71. { 0x5a, KEY_LEFT },
  72. { 0x5b, KEY_RIGHT },
  73. { 0x5c, KEY_OK },
  74. { 0x78, KEY_A },
  75. { 0x79, KEY_B },
  76. { 0x7a, KEY_C },
  77. { 0x7b, KEY_D },
  78. { 0x7c, KEY_E },
  79. { 0x7d, KEY_F },
  80. { 0x82, KEY_ENTER },
  81. { 0x8e, KEY_VENDOR },
  82. { 0x96, KEY_COFFEE },
  83. { 0xa9, BTN_LEFT },
  84. { 0xaa, BTN_RIGHT },
  85. { 0xbe, KEY_QUESTION },
  86. { 0xd5, KEY_FRONT },
  87. { 0xd0, KEY_EDIT },
  88. { 0xf9, KEY_INFO },
  89. { (0x00 << 8) | 0x3f, KEY_PROG1 },
  90. { (0x01 << 8) | 0x3f, KEY_PROG2 },
  91. { (0x02 << 8) | 0x3f, KEY_PROG3 },
  92. { (0x03 << 8) | 0x3f, KEY_PROG4 },
  93. { (0x04 << 8) | 0x3f, KEY_PC },
  94. { 0, KEY_RESERVED }
  95. };
  96. struct ati_remote2 {
  97. struct input_dev *idev;
  98. struct usb_device *udev;
  99. struct usb_interface *intf[2];
  100. struct usb_endpoint_descriptor *ep[2];
  101. struct urb *urb[2];
  102. void *buf[2];
  103. dma_addr_t buf_dma[2];
  104. unsigned long jiffies;
  105. int mode;
  106. char name[64];
  107. char phys[64];
  108. };
  109. static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id);
  110. static void ati_remote2_disconnect(struct usb_interface *interface);
  111. static struct usb_driver ati_remote2_driver = {
  112. .name = "ati_remote2",
  113. .probe = ati_remote2_probe,
  114. .disconnect = ati_remote2_disconnect,
  115. .id_table = ati_remote2_id_table,
  116. };
  117. static int ati_remote2_open(struct input_dev *idev)
  118. {
  119. struct ati_remote2 *ar2 = input_get_drvdata(idev);
  120. int r;
  121. r = usb_submit_urb(ar2->urb[0], GFP_KERNEL);
  122. if (r) {
  123. dev_err(&ar2->intf[0]->dev,
  124. "%s: usb_submit_urb() = %d\n", __FUNCTION__, r);
  125. return r;
  126. }
  127. r = usb_submit_urb(ar2->urb[1], GFP_KERNEL);
  128. if (r) {
  129. usb_kill_urb(ar2->urb[0]);
  130. dev_err(&ar2->intf[1]->dev,
  131. "%s: usb_submit_urb() = %d\n", __FUNCTION__, r);
  132. return r;
  133. }
  134. return 0;
  135. }
  136. static void ati_remote2_close(struct input_dev *idev)
  137. {
  138. struct ati_remote2 *ar2 = input_get_drvdata(idev);
  139. usb_kill_urb(ar2->urb[0]);
  140. usb_kill_urb(ar2->urb[1]);
  141. }
  142. static void ati_remote2_input_mouse(struct ati_remote2 *ar2)
  143. {
  144. struct input_dev *idev = ar2->idev;
  145. u8 *data = ar2->buf[0];
  146. int channel, mode;
  147. channel = data[0] >> 4;
  148. if (!((1 << channel) & channel_mask))
  149. return;
  150. mode = data[0] & 0x0F;
  151. if (mode > 4) {
  152. dev_err(&ar2->intf[0]->dev,
  153. "Unknown mode byte (%02x %02x %02x %02x)\n",
  154. data[3], data[2], data[1], data[0]);
  155. return;
  156. }
  157. if (!((1 << mode) & mode_mask))
  158. return;
  159. input_event(idev, EV_REL, REL_X, (s8) data[1]);
  160. input_event(idev, EV_REL, REL_Y, (s8) data[2]);
  161. input_sync(idev);
  162. }
  163. static int ati_remote2_lookup(unsigned int hw_code)
  164. {
  165. int i;
  166. for (i = 0; ati_remote2_key_table[i].key_code != KEY_RESERVED; i++)
  167. if (ati_remote2_key_table[i].hw_code == hw_code)
  168. return i;
  169. return -1;
  170. }
  171. static void ati_remote2_input_key(struct ati_remote2 *ar2)
  172. {
  173. struct input_dev *idev = ar2->idev;
  174. u8 *data = ar2->buf[1];
  175. int channel, mode, hw_code, index;
  176. channel = data[0] >> 4;
  177. if (!((1 << channel) & channel_mask))
  178. return;
  179. mode = data[0] & 0x0F;
  180. if (mode > 4) {
  181. dev_err(&ar2->intf[1]->dev,
  182. "Unknown mode byte (%02x %02x %02x %02x)\n",
  183. data[3], data[2], data[1], data[0]);
  184. return;
  185. }
  186. hw_code = data[2];
  187. /*
  188. * Mode keys (AUX1-AUX4, PC) all generate the same code byte.
  189. * Use the mode byte to figure out which one was pressed.
  190. */
  191. if (hw_code == 0x3f) {
  192. /*
  193. * For some incomprehensible reason the mouse pad generates
  194. * events which look identical to the events from the last
  195. * pressed mode key. Naturally we don't want to generate key
  196. * events for the mouse pad so we filter out any subsequent
  197. * events from the same mode key.
  198. */
  199. if (ar2->mode == mode)
  200. return;
  201. if (data[1] == 0)
  202. ar2->mode = mode;
  203. hw_code |= mode << 8;
  204. }
  205. if (!((1 << mode) & mode_mask))
  206. return;
  207. index = ati_remote2_lookup(hw_code);
  208. if (index < 0) {
  209. dev_err(&ar2->intf[1]->dev,
  210. "Unknown code byte (%02x %02x %02x %02x)\n",
  211. data[3], data[2], data[1], data[0]);
  212. return;
  213. }
  214. switch (data[1]) {
  215. case 0: /* release */
  216. break;
  217. case 1: /* press */
  218. ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]);
  219. break;
  220. case 2: /* repeat */
  221. /* No repeat for mouse buttons. */
  222. if (ati_remote2_key_table[index].key_code == BTN_LEFT ||
  223. ati_remote2_key_table[index].key_code == BTN_RIGHT)
  224. return;
  225. if (!time_after_eq(jiffies, ar2->jiffies))
  226. return;
  227. ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]);
  228. break;
  229. default:
  230. dev_err(&ar2->intf[1]->dev,
  231. "Unknown state byte (%02x %02x %02x %02x)\n",
  232. data[3], data[2], data[1], data[0]);
  233. return;
  234. }
  235. input_event(idev, EV_KEY, ati_remote2_key_table[index].key_code, data[1]);
  236. input_sync(idev);
  237. }
  238. static void ati_remote2_complete_mouse(struct urb *urb)
  239. {
  240. struct ati_remote2 *ar2 = urb->context;
  241. int r;
  242. switch (urb->status) {
  243. case 0:
  244. ati_remote2_input_mouse(ar2);
  245. break;
  246. case -ENOENT:
  247. case -EILSEQ:
  248. case -ECONNRESET:
  249. case -ESHUTDOWN:
  250. dev_dbg(&ar2->intf[0]->dev,
  251. "%s(): urb status = %d\n", __FUNCTION__, urb->status);
  252. return;
  253. default:
  254. dev_err(&ar2->intf[0]->dev,
  255. "%s(): urb status = %d\n", __FUNCTION__, urb->status);
  256. }
  257. r = usb_submit_urb(urb, GFP_ATOMIC);
  258. if (r)
  259. dev_err(&ar2->intf[0]->dev,
  260. "%s(): usb_submit_urb() = %d\n", __FUNCTION__, r);
  261. }
  262. static void ati_remote2_complete_key(struct urb *urb)
  263. {
  264. struct ati_remote2 *ar2 = urb->context;
  265. int r;
  266. switch (urb->status) {
  267. case 0:
  268. ati_remote2_input_key(ar2);
  269. break;
  270. case -ENOENT:
  271. case -EILSEQ:
  272. case -ECONNRESET:
  273. case -ESHUTDOWN:
  274. dev_dbg(&ar2->intf[1]->dev,
  275. "%s(): urb status = %d\n", __FUNCTION__, urb->status);
  276. return;
  277. default:
  278. dev_err(&ar2->intf[1]->dev,
  279. "%s(): urb status = %d\n", __FUNCTION__, urb->status);
  280. }
  281. r = usb_submit_urb(urb, GFP_ATOMIC);
  282. if (r)
  283. dev_err(&ar2->intf[1]->dev,
  284. "%s(): usb_submit_urb() = %d\n", __FUNCTION__, r);
  285. }
  286. static int ati_remote2_input_init(struct ati_remote2 *ar2)
  287. {
  288. struct input_dev *idev;
  289. int i, retval;
  290. idev = input_allocate_device();
  291. if (!idev)
  292. return -ENOMEM;
  293. ar2->idev = idev;
  294. input_set_drvdata(idev, ar2);
  295. idev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP) | BIT(EV_REL);
  296. idev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT);
  297. idev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
  298. for (i = 0; ati_remote2_key_table[i].key_code != KEY_RESERVED; i++)
  299. set_bit(ati_remote2_key_table[i].key_code, idev->keybit);
  300. idev->rep[REP_DELAY] = 250;
  301. idev->rep[REP_PERIOD] = 33;
  302. idev->open = ati_remote2_open;
  303. idev->close = ati_remote2_close;
  304. idev->name = ar2->name;
  305. idev->phys = ar2->phys;
  306. usb_to_input_id(ar2->udev, &idev->id);
  307. idev->dev.parent = &ar2->udev->dev;
  308. retval = input_register_device(idev);
  309. if (retval)
  310. input_free_device(idev);
  311. return retval;
  312. }
  313. static int ati_remote2_urb_init(struct ati_remote2 *ar2)
  314. {
  315. struct usb_device *udev = ar2->udev;
  316. int i, pipe, maxp;
  317. for (i = 0; i < 2; i++) {
  318. ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]);
  319. if (!ar2->buf[i])
  320. return -ENOMEM;
  321. ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  322. if (!ar2->urb[i])
  323. return -ENOMEM;
  324. pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress);
  325. maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
  326. maxp = maxp > 4 ? 4 : maxp;
  327. usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp,
  328. i ? ati_remote2_complete_key : ati_remote2_complete_mouse,
  329. ar2, ar2->ep[i]->bInterval);
  330. ar2->urb[i]->transfer_dma = ar2->buf_dma[i];
  331. ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  332. }
  333. return 0;
  334. }
  335. static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2)
  336. {
  337. int i;
  338. for (i = 0; i < 2; i++) {
  339. usb_free_urb(ar2->urb[i]);
  340. usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]);
  341. }
  342. }
  343. static int ati_remote2_setup(struct ati_remote2 *ar2)
  344. {
  345. int r, i, channel;
  346. /*
  347. * Configure receiver to only accept input from remote "channel"
  348. * channel == 0 -> Accept input from any remote channel
  349. * channel == 1 -> Only accept input from remote channel 1
  350. * channel == 2 -> Only accept input from remote channel 2
  351. * ...
  352. * channel == 16 -> Only accept input from remote channel 16
  353. */
  354. channel = 0;
  355. for (i = 0; i < 16; i++) {
  356. if ((1 << i) & channel_mask) {
  357. if (!(~(1 << i) & 0xFFFF & channel_mask))
  358. channel = i + 1;
  359. break;
  360. }
  361. }
  362. r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0),
  363. 0x20,
  364. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
  365. channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT);
  366. if (r) {
  367. dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n",
  368. __FUNCTION__, r);
  369. return r;
  370. }
  371. return 0;
  372. }
  373. static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id)
  374. {
  375. struct usb_device *udev = interface_to_usbdev(interface);
  376. struct usb_host_interface *alt = interface->cur_altsetting;
  377. struct ati_remote2 *ar2;
  378. int r;
  379. if (alt->desc.bInterfaceNumber)
  380. return -ENODEV;
  381. ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL);
  382. if (!ar2)
  383. return -ENOMEM;
  384. ar2->udev = udev;
  385. ar2->intf[0] = interface;
  386. ar2->ep[0] = &alt->endpoint[0].desc;
  387. ar2->intf[1] = usb_ifnum_to_if(udev, 1);
  388. r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2);
  389. if (r)
  390. goto fail1;
  391. alt = ar2->intf[1]->cur_altsetting;
  392. ar2->ep[1] = &alt->endpoint[0].desc;
  393. r = ati_remote2_urb_init(ar2);
  394. if (r)
  395. goto fail2;
  396. r = ati_remote2_setup(ar2);
  397. if (r)
  398. goto fail2;
  399. usb_make_path(udev, ar2->phys, sizeof(ar2->phys));
  400. strlcat(ar2->phys, "/input0", sizeof(ar2->phys));
  401. strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name));
  402. r = ati_remote2_input_init(ar2);
  403. if (r)
  404. goto fail2;
  405. usb_set_intfdata(interface, ar2);
  406. return 0;
  407. fail2:
  408. ati_remote2_urb_cleanup(ar2);
  409. usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
  410. fail1:
  411. kfree(ar2);
  412. return r;
  413. }
  414. static void ati_remote2_disconnect(struct usb_interface *interface)
  415. {
  416. struct ati_remote2 *ar2;
  417. struct usb_host_interface *alt = interface->cur_altsetting;
  418. if (alt->desc.bInterfaceNumber)
  419. return;
  420. ar2 = usb_get_intfdata(interface);
  421. usb_set_intfdata(interface, NULL);
  422. input_unregister_device(ar2->idev);
  423. ati_remote2_urb_cleanup(ar2);
  424. usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]);
  425. kfree(ar2);
  426. }
  427. static int __init ati_remote2_init(void)
  428. {
  429. int r;
  430. r = usb_register(&ati_remote2_driver);
  431. if (r)
  432. printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r);
  433. else
  434. printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n");
  435. return r;
  436. }
  437. static void __exit ati_remote2_exit(void)
  438. {
  439. usb_deregister(&ati_remote2_driver);
  440. }
  441. module_init(ati_remote2_init);
  442. module_exit(ati_remote2_exit);