hid-wiimote.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_FLAG_EXT_PLUGGED 0x0100
  34. #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
  35. WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
  36. #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
  37. WIIPROTO_FLAG_IR_FULL)
  38. /* return flag for led \num */
  39. #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
  40. enum wiimote_devtype {
  41. WIIMOTE_DEV_PENDING,
  42. WIIMOTE_DEV_UNKNOWN,
  43. WIIMOTE_DEV_GENERIC,
  44. WIIMOTE_DEV_GEN10,
  45. WIIMOTE_DEV_GEN20,
  46. WIIMOTE_DEV_NUM,
  47. };
  48. enum wiimote_exttype {
  49. WIIMOTE_EXT_NONE,
  50. WIIMOTE_EXT_UNKNOWN,
  51. WIIMOTE_EXT_NUM,
  52. };
  53. struct wiimote_buf {
  54. __u8 data[HID_MAX_BUFFER_SIZE];
  55. size_t size;
  56. };
  57. struct wiimote_queue {
  58. spinlock_t lock;
  59. struct work_struct worker;
  60. __u8 head;
  61. __u8 tail;
  62. struct wiimote_buf outq[WIIMOTE_BUFSIZE];
  63. };
  64. struct wiimote_state {
  65. spinlock_t lock;
  66. __u32 flags;
  67. __u8 accel_split[2];
  68. __u8 drm;
  69. __u8 devtype;
  70. /* synchronous cmd requests */
  71. struct mutex sync;
  72. struct completion ready;
  73. int cmd;
  74. __u32 opt;
  75. /* results of synchronous requests */
  76. __u8 cmd_battery;
  77. __u8 cmd_err;
  78. __u8 *cmd_read_buf;
  79. __u8 cmd_read_size;
  80. };
  81. struct wiimote_data {
  82. struct hid_device *hdev;
  83. struct input_dev *input;
  84. struct led_classdev *leds[4];
  85. struct input_dev *accel;
  86. struct input_dev *ir;
  87. struct power_supply battery;
  88. struct wiimote_ext *ext;
  89. struct wiimote_debug *debug;
  90. struct wiimote_queue queue;
  91. struct wiimote_state state;
  92. struct work_struct init_worker;
  93. };
  94. enum wiiproto_reqs {
  95. WIIPROTO_REQ_NULL = 0x0,
  96. WIIPROTO_REQ_RUMBLE = 0x10,
  97. WIIPROTO_REQ_LED = 0x11,
  98. WIIPROTO_REQ_DRM = 0x12,
  99. WIIPROTO_REQ_IR1 = 0x13,
  100. WIIPROTO_REQ_SREQ = 0x15,
  101. WIIPROTO_REQ_WMEM = 0x16,
  102. WIIPROTO_REQ_RMEM = 0x17,
  103. WIIPROTO_REQ_IR2 = 0x1a,
  104. WIIPROTO_REQ_STATUS = 0x20,
  105. WIIPROTO_REQ_DATA = 0x21,
  106. WIIPROTO_REQ_RETURN = 0x22,
  107. WIIPROTO_REQ_DRM_K = 0x30,
  108. WIIPROTO_REQ_DRM_KA = 0x31,
  109. WIIPROTO_REQ_DRM_KE = 0x32,
  110. WIIPROTO_REQ_DRM_KAI = 0x33,
  111. WIIPROTO_REQ_DRM_KEE = 0x34,
  112. WIIPROTO_REQ_DRM_KAE = 0x35,
  113. WIIPROTO_REQ_DRM_KIE = 0x36,
  114. WIIPROTO_REQ_DRM_KAIE = 0x37,
  115. WIIPROTO_REQ_DRM_E = 0x3d,
  116. WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
  117. WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
  118. WIIPROTO_REQ_MAX
  119. };
  120. #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
  121. dev))
  122. extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
  123. extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  124. const __u8 *wmem, __u8 size);
  125. extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
  126. __u8 *rmem, __u8 size);
  127. #define wiiproto_req_rreg(wdata, os, sz) \
  128. wiiproto_req_rmem((wdata), false, (os), (sz))
  129. #define wiiproto_req_reeprom(wdata, os, sz) \
  130. wiiproto_req_rmem((wdata), true, (os), (sz))
  131. extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
  132. __u32 offset, __u16 size);
  133. #ifdef CONFIG_HID_WIIMOTE_EXT
  134. extern int wiiext_init(struct wiimote_data *wdata);
  135. extern void wiiext_deinit(struct wiimote_data *wdata);
  136. extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
  137. extern bool wiiext_active(struct wiimote_data *wdata);
  138. extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
  139. #else
  140. static inline int wiiext_init(void *u) { return 0; }
  141. static inline void wiiext_deinit(void *u) { }
  142. static inline void wiiext_event(void *u, bool p) { }
  143. static inline bool wiiext_active(void *u) { return false; }
  144. static inline void wiiext_handle(void *u, const __u8 *p) { }
  145. #endif
  146. #ifdef CONFIG_DEBUG_FS
  147. extern int wiidebug_init(struct wiimote_data *wdata);
  148. extern void wiidebug_deinit(struct wiimote_data *wdata);
  149. #else
  150. static inline int wiidebug_init(void *u) { return 0; }
  151. static inline void wiidebug_deinit(void *u) { }
  152. #endif
  153. /* requires the state.lock spinlock to be held */
  154. static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
  155. __u32 opt)
  156. {
  157. return wdata->state.cmd == cmd && wdata->state.opt == opt;
  158. }
  159. /* requires the state.lock spinlock to be held */
  160. static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
  161. {
  162. wdata->state.cmd = WIIPROTO_REQ_NULL;
  163. complete(&wdata->state.ready);
  164. }
  165. /* requires the state.lock spinlock to be held */
  166. static inline void wiimote_cmd_abort(struct wiimote_data *wdata)
  167. {
  168. /* Abort synchronous request by waking up the sleeping caller. But
  169. * reset the state.cmd field to an invalid value so no further event
  170. * handlers will work with it. */
  171. wdata->state.cmd = WIIPROTO_REQ_MAX;
  172. complete(&wdata->state.ready);
  173. }
  174. static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
  175. {
  176. return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
  177. }
  178. static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata)
  179. {
  180. mutex_lock(&wdata->state.sync);
  181. }
  182. /* requires the state.lock spinlock to be held */
  183. static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
  184. __u32 opt)
  185. {
  186. INIT_COMPLETION(wdata->state.ready);
  187. wdata->state.cmd = cmd;
  188. wdata->state.opt = opt;
  189. }
  190. static inline void wiimote_cmd_release(struct wiimote_data *wdata)
  191. {
  192. mutex_unlock(&wdata->state.sync);
  193. }
  194. static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
  195. {
  196. int ret;
  197. /* The completion acts as implicit memory barrier so we can safely
  198. * assume that state.cmd is set on success/failure and isn't accessed
  199. * by any other thread, anymore. */
  200. ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
  201. if (ret < 0)
  202. return -ERESTARTSYS;
  203. else if (ret == 0)
  204. return -EIO;
  205. else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
  206. return -EIO;
  207. else
  208. return 0;
  209. }
  210. static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata)
  211. {
  212. unsigned long ret;
  213. /* no locking needed; see wiimote_cmd_wait() */
  214. ret = wait_for_completion_timeout(&wdata->state.ready, HZ);
  215. if (!ret)
  216. return -EIO;
  217. else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
  218. return -EIO;
  219. else
  220. return 0;
  221. }
  222. #endif