hid-wiimote.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/hid.h>
  12. #include <linux/module.h>
  13. #include "hid-ids.h"
  14. #define WIIMOTE_VERSION "0.1"
  15. #define WIIMOTE_NAME "Nintendo Wii Remote"
  16. struct wiimote_data {
  17. struct hid_device *hdev;
  18. };
  19. static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
  20. u8 *raw_data, int size)
  21. {
  22. if (size < 1)
  23. return -EINVAL;
  24. return 0;
  25. }
  26. static struct wiimote_data *wiimote_create(struct hid_device *hdev)
  27. {
  28. struct wiimote_data *wdata;
  29. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  30. if (!wdata)
  31. return NULL;
  32. wdata->hdev = hdev;
  33. hid_set_drvdata(hdev, wdata);
  34. return wdata;
  35. }
  36. static void wiimote_destroy(struct wiimote_data *wdata)
  37. {
  38. kfree(wdata);
  39. }
  40. static int wiimote_hid_probe(struct hid_device *hdev,
  41. const struct hid_device_id *id)
  42. {
  43. struct wiimote_data *wdata;
  44. int ret;
  45. wdata = wiimote_create(hdev);
  46. if (!wdata) {
  47. hid_err(hdev, "Can't alloc device\n");
  48. return -ENOMEM;
  49. }
  50. ret = hid_parse(hdev);
  51. if (ret) {
  52. hid_err(hdev, "HID parse failed\n");
  53. goto err;
  54. }
  55. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  56. if (ret) {
  57. hid_err(hdev, "HW start failed\n");
  58. goto err;
  59. }
  60. hid_info(hdev, "New device registered\n");
  61. return 0;
  62. err:
  63. wiimote_destroy(wdata);
  64. return ret;
  65. }
  66. static void wiimote_hid_remove(struct hid_device *hdev)
  67. {
  68. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  69. hid_info(hdev, "Device removed\n");
  70. hid_hw_stop(hdev);
  71. wiimote_destroy(wdata);
  72. }
  73. static const struct hid_device_id wiimote_hid_devices[] = {
  74. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  75. USB_DEVICE_ID_NINTENDO_WIIMOTE) },
  76. { }
  77. };
  78. MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
  79. static struct hid_driver wiimote_hid_driver = {
  80. .name = "wiimote",
  81. .id_table = wiimote_hid_devices,
  82. .probe = wiimote_hid_probe,
  83. .remove = wiimote_hid_remove,
  84. .raw_event = wiimote_hid_event,
  85. };
  86. static int __init wiimote_init(void)
  87. {
  88. int ret;
  89. ret = hid_register_driver(&wiimote_hid_driver);
  90. if (ret)
  91. pr_err("Can't register wiimote hid driver\n");
  92. return ret;
  93. }
  94. static void __exit wiimote_exit(void)
  95. {
  96. hid_unregister_driver(&wiimote_hid_driver);
  97. }
  98. module_init(wiimote_init);
  99. module_exit(wiimote_exit);
  100. MODULE_LICENSE("GPL");
  101. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  102. MODULE_DESCRIPTION(WIIMOTE_NAME " Device Driver");
  103. MODULE_VERSION(WIIMOTE_VERSION);