hid-wiimote.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * HID driver for Nintendo Wiimote devices
  3. * Copyright (c) 2011 David Herrmann
  4. */
  5. /*
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/atomic.h>
  12. #include <linux/device.h>
  13. #include <linux/hid.h>
  14. #include <linux/input.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include "hid-ids.h"
  18. #define WIIMOTE_VERSION "0.1"
  19. #define WIIMOTE_NAME "Nintendo Wii Remote"
  20. #define WIIMOTE_BUFSIZE 32
  21. struct wiimote_buf {
  22. __u8 data[HID_MAX_BUFFER_SIZE];
  23. size_t size;
  24. };
  25. struct wiimote_data {
  26. atomic_t ready;
  27. struct hid_device *hdev;
  28. struct input_dev *input;
  29. spinlock_t qlock;
  30. __u8 head;
  31. __u8 tail;
  32. struct wiimote_buf outq[WIIMOTE_BUFSIZE];
  33. struct work_struct worker;
  34. };
  35. static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
  36. size_t count)
  37. {
  38. __u8 *buf;
  39. ssize_t ret;
  40. if (!hdev->hid_output_raw_report)
  41. return -ENODEV;
  42. buf = kmemdup(buffer, count, GFP_KERNEL);
  43. if (!buf)
  44. return -ENOMEM;
  45. ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
  46. kfree(buf);
  47. return ret;
  48. }
  49. static void wiimote_worker(struct work_struct *work)
  50. {
  51. struct wiimote_data *wdata = container_of(work, struct wiimote_data,
  52. worker);
  53. unsigned long flags;
  54. spin_lock_irqsave(&wdata->qlock, flags);
  55. while (wdata->head != wdata->tail) {
  56. spin_unlock_irqrestore(&wdata->qlock, flags);
  57. wiimote_hid_send(wdata->hdev, wdata->outq[wdata->tail].data,
  58. wdata->outq[wdata->tail].size);
  59. spin_lock_irqsave(&wdata->qlock, flags);
  60. wdata->tail = (wdata->tail + 1) % WIIMOTE_BUFSIZE;
  61. }
  62. spin_unlock_irqrestore(&wdata->qlock, flags);
  63. }
  64. static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
  65. size_t count)
  66. {
  67. unsigned long flags;
  68. __u8 newhead;
  69. if (count > HID_MAX_BUFFER_SIZE) {
  70. hid_warn(wdata->hdev, "Sending too large output report\n");
  71. return;
  72. }
  73. /*
  74. * Copy new request into our output queue and check whether the
  75. * queue is full. If it is full, discard this request.
  76. * If it is empty we need to start a new worker that will
  77. * send out the buffer to the hid device.
  78. * If the queue is not empty, then there must be a worker
  79. * that is currently sending out our buffer and this worker
  80. * will reschedule itself until the queue is empty.
  81. */
  82. spin_lock_irqsave(&wdata->qlock, flags);
  83. memcpy(wdata->outq[wdata->head].data, buffer, count);
  84. wdata->outq[wdata->head].size = count;
  85. newhead = (wdata->head + 1) % WIIMOTE_BUFSIZE;
  86. if (wdata->head == wdata->tail) {
  87. wdata->head = newhead;
  88. schedule_work(&wdata->worker);
  89. } else if (newhead != wdata->tail) {
  90. wdata->head = newhead;
  91. } else {
  92. hid_warn(wdata->hdev, "Output queue is full");
  93. }
  94. spin_unlock_irqrestore(&wdata->qlock, flags);
  95. }
  96. static int wiimote_input_event(struct input_dev *dev, unsigned int type,
  97. unsigned int code, int value)
  98. {
  99. struct wiimote_data *wdata = input_get_drvdata(dev);
  100. if (!atomic_read(&wdata->ready))
  101. return -EBUSY;
  102. /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
  103. smp_rmb();
  104. return 0;
  105. }
  106. static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
  107. u8 *raw_data, int size)
  108. {
  109. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  110. if (!atomic_read(&wdata->ready))
  111. return -EBUSY;
  112. /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
  113. smp_rmb();
  114. if (size < 1)
  115. return -EINVAL;
  116. return 0;
  117. }
  118. static struct wiimote_data *wiimote_create(struct hid_device *hdev)
  119. {
  120. struct wiimote_data *wdata;
  121. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  122. if (!wdata)
  123. return NULL;
  124. wdata->input = input_allocate_device();
  125. if (!wdata->input) {
  126. kfree(wdata);
  127. return NULL;
  128. }
  129. wdata->hdev = hdev;
  130. hid_set_drvdata(hdev, wdata);
  131. input_set_drvdata(wdata->input, wdata);
  132. wdata->input->event = wiimote_input_event;
  133. wdata->input->dev.parent = &wdata->hdev->dev;
  134. wdata->input->id.bustype = wdata->hdev->bus;
  135. wdata->input->id.vendor = wdata->hdev->vendor;
  136. wdata->input->id.product = wdata->hdev->product;
  137. wdata->input->id.version = wdata->hdev->version;
  138. wdata->input->name = WIIMOTE_NAME;
  139. spin_lock_init(&wdata->qlock);
  140. INIT_WORK(&wdata->worker, wiimote_worker);
  141. return wdata;
  142. }
  143. static void wiimote_destroy(struct wiimote_data *wdata)
  144. {
  145. kfree(wdata);
  146. }
  147. static int wiimote_hid_probe(struct hid_device *hdev,
  148. const struct hid_device_id *id)
  149. {
  150. struct wiimote_data *wdata;
  151. int ret;
  152. wdata = wiimote_create(hdev);
  153. if (!wdata) {
  154. hid_err(hdev, "Can't alloc device\n");
  155. return -ENOMEM;
  156. }
  157. ret = hid_parse(hdev);
  158. if (ret) {
  159. hid_err(hdev, "HID parse failed\n");
  160. goto err;
  161. }
  162. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  163. if (ret) {
  164. hid_err(hdev, "HW start failed\n");
  165. goto err;
  166. }
  167. ret = input_register_device(wdata->input);
  168. if (ret) {
  169. hid_err(hdev, "Cannot register input device\n");
  170. goto err_stop;
  171. }
  172. /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
  173. smp_wmb();
  174. atomic_set(&wdata->ready, 1);
  175. hid_info(hdev, "New device registered\n");
  176. return 0;
  177. err_stop:
  178. hid_hw_stop(hdev);
  179. err:
  180. input_free_device(wdata->input);
  181. wiimote_destroy(wdata);
  182. return ret;
  183. }
  184. static void wiimote_hid_remove(struct hid_device *hdev)
  185. {
  186. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  187. hid_info(hdev, "Device removed\n");
  188. hid_hw_stop(hdev);
  189. input_unregister_device(wdata->input);
  190. cancel_work_sync(&wdata->worker);
  191. wiimote_destroy(wdata);
  192. }
  193. static const struct hid_device_id wiimote_hid_devices[] = {
  194. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  195. USB_DEVICE_ID_NINTENDO_WIIMOTE) },
  196. { }
  197. };
  198. MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
  199. static struct hid_driver wiimote_hid_driver = {
  200. .name = "wiimote",
  201. .id_table = wiimote_hid_devices,
  202. .probe = wiimote_hid_probe,
  203. .remove = wiimote_hid_remove,
  204. .raw_event = wiimote_hid_event,
  205. };
  206. static int __init wiimote_init(void)
  207. {
  208. int ret;
  209. ret = hid_register_driver(&wiimote_hid_driver);
  210. if (ret)
  211. pr_err("Can't register wiimote hid driver\n");
  212. return ret;
  213. }
  214. static void __exit wiimote_exit(void)
  215. {
  216. hid_unregister_driver(&wiimote_hid_driver);
  217. }
  218. module_init(wiimote_init);
  219. module_exit(wiimote_exit);
  220. MODULE_LICENSE("GPL");
  221. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  222. MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
  223. MODULE_VERSION(WIIMOTE_VERSION);