ati_remote2.c 11 KB

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