xbox_remote.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Driver for Xbox DVD Movie Playback Kit
  3. // Copyright (c) 2018 by Benjamin Valentin <[email protected]>
  4. /*
  5. * Xbox DVD Movie Playback Kit USB IR dongle support
  6. *
  7. * The driver was derived from the ati_remote driver 2.2.1
  8. * and used information from lirc_xbox.c
  9. *
  10. * Copyright (c) 2011, 2012 Anssi Hannula <[email protected]>
  11. * Copyright (c) 2004 Torrey Hoffman <[email protected]>
  12. * Copyright (c) 2002 Vladimir Dergachev
  13. * Copyright (c) 2003-2004 Paul Miller <[email protected]>
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/usb/input.h>
  18. #include <media/rc-core.h>
  19. /*
  20. * Module and Version Information
  21. */
  22. #define DRIVER_VERSION "1.0.0"
  23. #define DRIVER_AUTHOR "Benjamin Valentin <[email protected]>"
  24. #define DRIVER_DESC "Xbox DVD USB Remote Control"
  25. #define NAME_BUFSIZE 80 /* size of product name, path buffers */
  26. #define DATA_BUFSIZE 8 /* size of URB data buffers */
  27. /*
  28. * USB vendor ids for XBOX DVD Dongles
  29. */
  30. #define VENDOR_GAMESTER 0x040b
  31. #define VENDOR_MICROSOFT 0x045e
  32. static const struct usb_device_id xbox_remote_table[] = {
  33. /* Gamester Xbox DVD Movie Playback Kit IR */
  34. {
  35. USB_DEVICE(VENDOR_GAMESTER, 0x6521),
  36. },
  37. /* Microsoft Xbox DVD Movie Playback Kit IR */
  38. {
  39. USB_DEVICE(VENDOR_MICROSOFT, 0x0284),
  40. },
  41. {} /* Terminating entry */
  42. };
  43. MODULE_DEVICE_TABLE(usb, xbox_remote_table);
  44. struct xbox_remote {
  45. struct rc_dev *rdev;
  46. struct usb_device *udev;
  47. struct usb_interface *interface;
  48. struct urb *irq_urb;
  49. unsigned char inbuf[DATA_BUFSIZE] __aligned(sizeof(u16));
  50. char rc_name[NAME_BUFSIZE];
  51. char rc_phys[NAME_BUFSIZE];
  52. };
  53. static int xbox_remote_rc_open(struct rc_dev *rdev)
  54. {
  55. struct xbox_remote *xbox_remote = rdev->priv;
  56. /* On first open, submit the read urb which was set up previously. */
  57. xbox_remote->irq_urb->dev = xbox_remote->udev;
  58. if (usb_submit_urb(xbox_remote->irq_urb, GFP_KERNEL)) {
  59. dev_err(&xbox_remote->interface->dev,
  60. "%s: usb_submit_urb failed!\n", __func__);
  61. return -EIO;
  62. }
  63. return 0;
  64. }
  65. static void xbox_remote_rc_close(struct rc_dev *rdev)
  66. {
  67. struct xbox_remote *xbox_remote = rdev->priv;
  68. usb_kill_urb(xbox_remote->irq_urb);
  69. }
  70. /*
  71. * xbox_remote_report_input
  72. */
  73. static void xbox_remote_input_report(struct urb *urb)
  74. {
  75. struct xbox_remote *xbox_remote = urb->context;
  76. unsigned char *data = xbox_remote->inbuf;
  77. /*
  78. * data[0] = 0x00
  79. * data[1] = length - always 0x06
  80. * data[2] = the key code
  81. * data[3] = high part of key code
  82. * data[4] = last_press_ms (low)
  83. * data[5] = last_press_ms (high)
  84. */
  85. /* Deal with strange looking inputs */
  86. if (urb->actual_length != 6 || urb->actual_length != data[1]) {
  87. dev_warn(&urb->dev->dev, "Weird data, len=%d: %*ph\n",
  88. urb->actual_length, urb->actual_length, data);
  89. return;
  90. }
  91. rc_keydown(xbox_remote->rdev, RC_PROTO_XBOX_DVD,
  92. le16_to_cpup((__le16 *)(data + 2)), 0);
  93. }
  94. /*
  95. * xbox_remote_irq_in
  96. */
  97. static void xbox_remote_irq_in(struct urb *urb)
  98. {
  99. struct xbox_remote *xbox_remote = urb->context;
  100. int retval;
  101. switch (urb->status) {
  102. case 0: /* success */
  103. xbox_remote_input_report(urb);
  104. break;
  105. case -ECONNRESET: /* unlink */
  106. case -ENOENT:
  107. case -ESHUTDOWN:
  108. dev_dbg(&xbox_remote->interface->dev,
  109. "%s: urb error status, unlink?\n",
  110. __func__);
  111. return;
  112. default: /* error */
  113. dev_dbg(&xbox_remote->interface->dev,
  114. "%s: Nonzero urb status %d\n",
  115. __func__, urb->status);
  116. }
  117. retval = usb_submit_urb(urb, GFP_ATOMIC);
  118. if (retval)
  119. dev_err(&xbox_remote->interface->dev,
  120. "%s: usb_submit_urb()=%d\n",
  121. __func__, retval);
  122. }
  123. static void xbox_remote_rc_init(struct xbox_remote *xbox_remote)
  124. {
  125. struct rc_dev *rdev = xbox_remote->rdev;
  126. rdev->priv = xbox_remote;
  127. rdev->allowed_protocols = RC_PROTO_BIT_XBOX_DVD;
  128. rdev->driver_name = "xbox_remote";
  129. rdev->open = xbox_remote_rc_open;
  130. rdev->close = xbox_remote_rc_close;
  131. rdev->device_name = xbox_remote->rc_name;
  132. rdev->input_phys = xbox_remote->rc_phys;
  133. rdev->timeout = MS_TO_US(10);
  134. usb_to_input_id(xbox_remote->udev, &rdev->input_id);
  135. rdev->dev.parent = &xbox_remote->interface->dev;
  136. }
  137. static void xbox_remote_initialize(struct xbox_remote *xbox_remote,
  138. struct usb_endpoint_descriptor *endpoint_in)
  139. {
  140. struct usb_device *udev = xbox_remote->udev;
  141. int pipe, maxp;
  142. /* Set up irq_urb */
  143. pipe = usb_rcvintpipe(udev, endpoint_in->bEndpointAddress);
  144. maxp = usb_maxpacket(udev, pipe);
  145. maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
  146. usb_fill_int_urb(xbox_remote->irq_urb, udev, pipe, xbox_remote->inbuf,
  147. maxp, xbox_remote_irq_in, xbox_remote,
  148. endpoint_in->bInterval);
  149. }
  150. /*
  151. * xbox_remote_probe
  152. */
  153. static int xbox_remote_probe(struct usb_interface *interface,
  154. const struct usb_device_id *id)
  155. {
  156. struct usb_device *udev = interface_to_usbdev(interface);
  157. struct usb_host_interface *iface_host = interface->cur_altsetting;
  158. struct usb_endpoint_descriptor *endpoint_in;
  159. struct xbox_remote *xbox_remote;
  160. struct rc_dev *rc_dev;
  161. int err = -ENOMEM;
  162. // why is there also a device with no endpoints?
  163. if (iface_host->desc.bNumEndpoints == 0)
  164. return -ENODEV;
  165. if (iface_host->desc.bNumEndpoints != 1) {
  166. pr_err("%s: Unexpected desc.bNumEndpoints: %d\n",
  167. __func__, iface_host->desc.bNumEndpoints);
  168. return -ENODEV;
  169. }
  170. endpoint_in = &iface_host->endpoint[0].desc;
  171. if (!usb_endpoint_is_int_in(endpoint_in)) {
  172. pr_err("%s: Unexpected endpoint_in\n", __func__);
  173. return -ENODEV;
  174. }
  175. if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
  176. pr_err("%s: endpoint_in message size==0?\n", __func__);
  177. return -ENODEV;
  178. }
  179. xbox_remote = kzalloc(sizeof(*xbox_remote), GFP_KERNEL);
  180. rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
  181. if (!xbox_remote || !rc_dev)
  182. goto exit_free_dev_rdev;
  183. /* Allocate URB buffer */
  184. xbox_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
  185. if (!xbox_remote->irq_urb)
  186. goto exit_free_buffers;
  187. xbox_remote->udev = udev;
  188. xbox_remote->rdev = rc_dev;
  189. xbox_remote->interface = interface;
  190. usb_make_path(udev, xbox_remote->rc_phys, sizeof(xbox_remote->rc_phys));
  191. strlcat(xbox_remote->rc_phys, "/input0", sizeof(xbox_remote->rc_phys));
  192. snprintf(xbox_remote->rc_name, sizeof(xbox_remote->rc_name), "%s%s%s",
  193. udev->manufacturer ?: "",
  194. udev->manufacturer && udev->product ? " " : "",
  195. udev->product ?: "");
  196. if (!strlen(xbox_remote->rc_name))
  197. snprintf(xbox_remote->rc_name, sizeof(xbox_remote->rc_name),
  198. DRIVER_DESC "(%04x,%04x)",
  199. le16_to_cpu(xbox_remote->udev->descriptor.idVendor),
  200. le16_to_cpu(xbox_remote->udev->descriptor.idProduct));
  201. rc_dev->map_name = RC_MAP_XBOX_DVD; /* default map */
  202. xbox_remote_rc_init(xbox_remote);
  203. /* Device Hardware Initialization */
  204. xbox_remote_initialize(xbox_remote, endpoint_in);
  205. /* Set up and register rc device */
  206. err = rc_register_device(xbox_remote->rdev);
  207. if (err)
  208. goto exit_kill_urbs;
  209. usb_set_intfdata(interface, xbox_remote);
  210. return 0;
  211. exit_kill_urbs:
  212. usb_kill_urb(xbox_remote->irq_urb);
  213. exit_free_buffers:
  214. usb_free_urb(xbox_remote->irq_urb);
  215. exit_free_dev_rdev:
  216. rc_free_device(rc_dev);
  217. kfree(xbox_remote);
  218. return err;
  219. }
  220. /*
  221. * xbox_remote_disconnect
  222. */
  223. static void xbox_remote_disconnect(struct usb_interface *interface)
  224. {
  225. struct xbox_remote *xbox_remote;
  226. xbox_remote = usb_get_intfdata(interface);
  227. usb_set_intfdata(interface, NULL);
  228. if (!xbox_remote) {
  229. dev_warn(&interface->dev, "%s - null device?\n", __func__);
  230. return;
  231. }
  232. usb_kill_urb(xbox_remote->irq_urb);
  233. rc_unregister_device(xbox_remote->rdev);
  234. usb_free_urb(xbox_remote->irq_urb);
  235. kfree(xbox_remote);
  236. }
  237. /* usb specific object to register with the usb subsystem */
  238. static struct usb_driver xbox_remote_driver = {
  239. .name = "xbox_remote",
  240. .probe = xbox_remote_probe,
  241. .disconnect = xbox_remote_disconnect,
  242. .id_table = xbox_remote_table,
  243. };
  244. module_usb_driver(xbox_remote_driver);
  245. MODULE_AUTHOR(DRIVER_AUTHOR);
  246. MODULE_DESCRIPTION(DRIVER_DESC);
  247. MODULE_LICENSE("GPL");