hid-wiimote.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "hid-ids.h"
  17. #define WIIMOTE_VERSION "0.1"
  18. #define WIIMOTE_NAME "Nintendo Wii Remote"
  19. struct wiimote_data {
  20. atomic_t ready;
  21. struct hid_device *hdev;
  22. struct input_dev *input;
  23. };
  24. static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
  25. size_t count)
  26. {
  27. __u8 *buf;
  28. ssize_t ret;
  29. if (!hdev->hid_output_raw_report)
  30. return -ENODEV;
  31. buf = kmemdup(buffer, count, GFP_KERNEL);
  32. if (!buf)
  33. return -ENOMEM;
  34. ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
  35. kfree(buf);
  36. return ret;
  37. }
  38. static int wiimote_input_event(struct input_dev *dev, unsigned int type,
  39. unsigned int code, int value)
  40. {
  41. struct wiimote_data *wdata = input_get_drvdata(dev);
  42. if (!atomic_read(&wdata->ready))
  43. return -EBUSY;
  44. /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
  45. smp_rmb();
  46. return 0;
  47. }
  48. static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
  49. u8 *raw_data, int size)
  50. {
  51. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  52. if (!atomic_read(&wdata->ready))
  53. return -EBUSY;
  54. /* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */
  55. smp_rmb();
  56. if (size < 1)
  57. return -EINVAL;
  58. return 0;
  59. }
  60. static struct wiimote_data *wiimote_create(struct hid_device *hdev)
  61. {
  62. struct wiimote_data *wdata;
  63. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  64. if (!wdata)
  65. return NULL;
  66. wdata->input = input_allocate_device();
  67. if (!wdata->input) {
  68. kfree(wdata);
  69. return NULL;
  70. }
  71. wdata->hdev = hdev;
  72. hid_set_drvdata(hdev, wdata);
  73. input_set_drvdata(wdata->input, wdata);
  74. wdata->input->event = wiimote_input_event;
  75. wdata->input->dev.parent = &wdata->hdev->dev;
  76. wdata->input->id.bustype = wdata->hdev->bus;
  77. wdata->input->id.vendor = wdata->hdev->vendor;
  78. wdata->input->id.product = wdata->hdev->product;
  79. wdata->input->id.version = wdata->hdev->version;
  80. wdata->input->name = WIIMOTE_NAME;
  81. return wdata;
  82. }
  83. static void wiimote_destroy(struct wiimote_data *wdata)
  84. {
  85. kfree(wdata);
  86. }
  87. static int wiimote_hid_probe(struct hid_device *hdev,
  88. const struct hid_device_id *id)
  89. {
  90. struct wiimote_data *wdata;
  91. int ret;
  92. wdata = wiimote_create(hdev);
  93. if (!wdata) {
  94. hid_err(hdev, "Can't alloc device\n");
  95. return -ENOMEM;
  96. }
  97. ret = hid_parse(hdev);
  98. if (ret) {
  99. hid_err(hdev, "HID parse failed\n");
  100. goto err;
  101. }
  102. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  103. if (ret) {
  104. hid_err(hdev, "HW start failed\n");
  105. goto err;
  106. }
  107. ret = input_register_device(wdata->input);
  108. if (ret) {
  109. hid_err(hdev, "Cannot register input device\n");
  110. goto err_stop;
  111. }
  112. /* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */
  113. smp_wmb();
  114. atomic_set(&wdata->ready, 1);
  115. hid_info(hdev, "New device registered\n");
  116. return 0;
  117. err_stop:
  118. hid_hw_stop(hdev);
  119. err:
  120. input_free_device(wdata->input);
  121. wiimote_destroy(wdata);
  122. return ret;
  123. }
  124. static void wiimote_hid_remove(struct hid_device *hdev)
  125. {
  126. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  127. hid_info(hdev, "Device removed\n");
  128. hid_hw_stop(hdev);
  129. input_unregister_device(wdata->input);
  130. wiimote_destroy(wdata);
  131. }
  132. static const struct hid_device_id wiimote_hid_devices[] = {
  133. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  134. USB_DEVICE_ID_NINTENDO_WIIMOTE) },
  135. { }
  136. };
  137. MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
  138. static struct hid_driver wiimote_hid_driver = {
  139. .name = "wiimote",
  140. .id_table = wiimote_hid_devices,
  141. .probe = wiimote_hid_probe,
  142. .remove = wiimote_hid_remove,
  143. .raw_event = wiimote_hid_event,
  144. };
  145. static int __init wiimote_init(void)
  146. {
  147. int ret;
  148. ret = hid_register_driver(&wiimote_hid_driver);
  149. if (ret)
  150. pr_err("Can't register wiimote hid driver\n");
  151. return ret;
  152. }
  153. static void __exit wiimote_exit(void)
  154. {
  155. hid_unregister_driver(&wiimote_hid_driver);
  156. }
  157. module_init(wiimote_init);
  158. module_exit(wiimote_exit);
  159. MODULE_LICENSE("GPL");
  160. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  161. MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
  162. MODULE_VERSION(WIIMOTE_VERSION);