hid-wiimote.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #ifndef __HID_WIIMOTE_H
  2. #define __HID_WIIMOTE_H
  3. /*
  4. * HID driver for Nintendo Wii / Wii U peripherals
  5. * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. */
  13. #include <linux/completion.h>
  14. #include <linux/device.h>
  15. #include <linux/hid.h>
  16. #include <linux/input.h>
  17. #include <linux/leds.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/spinlock.h>
  22. #define WIIMOTE_NAME "Nintendo Wii Remote"
  23. #define WIIMOTE_BUFSIZE 32
  24. #define WIIPROTO_FLAG_LED1 0x01
  25. #define WIIPROTO_FLAG_LED2 0x02
  26. #define WIIPROTO_FLAG_LED3 0x04
  27. #define WIIPROTO_FLAG_LED4 0x08
  28. #define WIIPROTO_FLAG_RUMBLE 0x10
  29. #define WIIPROTO_FLAG_ACCEL 0x20
  30. #define WIIPROTO_FLAG_IR_BASIC 0x40
  31. #define WIIPROTO_FLAG_IR_EXT 0x80
  32. #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */
  33. #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
  34. WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
  35. #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
  36. WIIPROTO_FLAG_IR_FULL)
  37. /* return flag for led \num */
  38. #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
  39. struct wiimote_buf {
  40. __u8 data[HID_MAX_BUFFER_SIZE];
  41. size_t size;
  42. };
  43. struct wiimote_queue {
  44. spinlock_t lock;
  45. struct work_struct worker;
  46. __u8 head;
  47. __u8 tail;
  48. struct wiimote_buf outq[WIIMOTE_BUFSIZE];
  49. };
  50. struct wiimote_state {
  51. spinlock_t lock;
  52. __u8 flags;
  53. __u8 accel_split[2];
  54. __u8 drm;
  55. /* synchronous cmd requests */
  56. struct mutex sync;
  57. struct completion ready;
  58. int cmd;
  59. __u32 opt;
  60. /* results of synchronous requests */
  61. __u8 cmd_battery;
  62. __u8 cmd_err;
  63. __u8 *cmd_read_buf;
  64. __u8 cmd_read_size;
  65. };
  66. struct wiimote_data {
  67. struct hid_device *hdev;
  68. struct input_dev *input;
  69. struct led_classdev *leds[4];
  70. struct input_dev *accel;
  71. struct input_dev *ir;
  72. struct power_supply battery;
  73. struct wiimote_ext *ext;
  74. struct wiimote_debug *debug;
  75. struct wiimote_queue queue;
  76. struct wiimote_state state;
  77. };
  78. enum wiiproto_reqs {
  79. WIIPROTO_REQ_NULL = 0x0,
  80. WIIPROTO_REQ_RUMBLE = 0x10,
  81. WIIPROTO_REQ_LED = 0x11,
  82. WIIPROTO_REQ_DRM = 0x12,
  83. WIIPROTO_REQ_IR1 = 0x13,
  84. WIIPROTO_REQ_SREQ = 0x15,
  85. WIIPROTO_REQ_WMEM = 0x16,
  86. WIIPROTO_REQ_RMEM = 0x17,
  87. WIIPROTO_REQ_IR2 = 0x1a,
  88. WIIPROTO_REQ_STATUS = 0x20,
  89. WIIPROTO_REQ_DATA = 0x21,
  90. WIIPROTO_REQ_RETURN = 0x22,
  91. WIIPROTO_REQ_DRM_K = 0x30,
  92. WIIPROTO_REQ_DRM_KA = 0x31,
  93. WIIPROTO_REQ_DRM_KE = 0x32,
  94. WIIPROTO_REQ_DRM_KAI = 0x33,
  95. WIIPROTO_REQ_DRM_KEE = 0x34,
  96. WIIPROTO_REQ_DRM_KAE = 0x35,
  97. WIIPROTO_REQ_DRM_KIE = 0x36,
  98. WIIPROTO_REQ_DRM_KAIE = 0x37,
  99. WIIPROTO_REQ_DRM_E = 0x3d,
  100. WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
  101. WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
  102. WIIPROTO_REQ_MAX
  103. };
  104. #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
  105. dev))
  106. extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
  107. extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  108. const __u8 *wmem, __u8 size);
  109. extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
  110. __u8 *rmem, __u8 size);
  111. #define wiiproto_req_rreg(wdata, os, sz) \
  112. wiiproto_req_rmem((wdata), false, (os), (sz))
  113. #define wiiproto_req_reeprom(wdata, os, sz) \
  114. wiiproto_req_rmem((wdata), true, (os), (sz))
  115. extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
  116. __u32 offset, __u16 size);
  117. #ifdef CONFIG_HID_WIIMOTE_EXT
  118. extern int wiiext_init(struct wiimote_data *wdata);
  119. extern void wiiext_deinit(struct wiimote_data *wdata);
  120. extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
  121. extern bool wiiext_active(struct wiimote_data *wdata);
  122. extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
  123. #else
  124. static inline int wiiext_init(void *u) { return 0; }
  125. static inline void wiiext_deinit(void *u) { }
  126. static inline void wiiext_event(void *u, bool p) { }
  127. static inline bool wiiext_active(void *u) { return false; }
  128. static inline void wiiext_handle(void *u, const __u8 *p) { }
  129. #endif
  130. #ifdef CONFIG_DEBUG_FS
  131. extern int wiidebug_init(struct wiimote_data *wdata);
  132. extern void wiidebug_deinit(struct wiimote_data *wdata);
  133. #else
  134. static inline int wiidebug_init(void *u) { return 0; }
  135. static inline void wiidebug_deinit(void *u) { }
  136. #endif
  137. /* requires the state.lock spinlock to be held */
  138. static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
  139. __u32 opt)
  140. {
  141. return wdata->state.cmd == cmd && wdata->state.opt == opt;
  142. }
  143. /* requires the state.lock spinlock to be held */
  144. static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
  145. {
  146. wdata->state.cmd = WIIPROTO_REQ_NULL;
  147. complete(&wdata->state.ready);
  148. }
  149. static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
  150. {
  151. return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
  152. }
  153. /* requires the state.lock spinlock to be held */
  154. static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
  155. __u32 opt)
  156. {
  157. INIT_COMPLETION(wdata->state.ready);
  158. wdata->state.cmd = cmd;
  159. wdata->state.opt = opt;
  160. }
  161. static inline void wiimote_cmd_release(struct wiimote_data *wdata)
  162. {
  163. mutex_unlock(&wdata->state.sync);
  164. }
  165. static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
  166. {
  167. int ret;
  168. ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
  169. if (ret < 0)
  170. return -ERESTARTSYS;
  171. else if (ret == 0)
  172. return -EIO;
  173. else
  174. return 0;
  175. }
  176. #endif