hid-wiimote.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 wiiproto_keys {
  41. WIIPROTO_KEY_LEFT,
  42. WIIPROTO_KEY_RIGHT,
  43. WIIPROTO_KEY_UP,
  44. WIIPROTO_KEY_DOWN,
  45. WIIPROTO_KEY_PLUS,
  46. WIIPROTO_KEY_MINUS,
  47. WIIPROTO_KEY_ONE,
  48. WIIPROTO_KEY_TWO,
  49. WIIPROTO_KEY_A,
  50. WIIPROTO_KEY_B,
  51. WIIPROTO_KEY_HOME,
  52. WIIPROTO_KEY_COUNT
  53. };
  54. enum wiimote_devtype {
  55. WIIMOTE_DEV_PENDING,
  56. WIIMOTE_DEV_UNKNOWN,
  57. WIIMOTE_DEV_GENERIC,
  58. WIIMOTE_DEV_GEN10,
  59. WIIMOTE_DEV_GEN20,
  60. WIIMOTE_DEV_NUM,
  61. };
  62. enum wiimote_exttype {
  63. WIIMOTE_EXT_NONE,
  64. WIIMOTE_EXT_UNKNOWN,
  65. WIIMOTE_EXT_NUM,
  66. };
  67. struct wiimote_buf {
  68. __u8 data[HID_MAX_BUFFER_SIZE];
  69. size_t size;
  70. };
  71. struct wiimote_queue {
  72. spinlock_t lock;
  73. struct work_struct worker;
  74. __u8 head;
  75. __u8 tail;
  76. struct wiimote_buf outq[WIIMOTE_BUFSIZE];
  77. };
  78. struct wiimote_state {
  79. spinlock_t lock;
  80. __u32 flags;
  81. __u8 accel_split[2];
  82. __u8 drm;
  83. __u8 devtype;
  84. /* synchronous cmd requests */
  85. struct mutex sync;
  86. struct completion ready;
  87. int cmd;
  88. __u32 opt;
  89. /* results of synchronous requests */
  90. __u8 cmd_battery;
  91. __u8 cmd_err;
  92. __u8 *cmd_read_buf;
  93. __u8 cmd_read_size;
  94. };
  95. struct wiimote_data {
  96. struct hid_device *hdev;
  97. struct input_dev *input;
  98. struct led_classdev *leds[4];
  99. struct input_dev *accel;
  100. struct input_dev *ir;
  101. struct power_supply battery;
  102. struct wiimote_ext *ext;
  103. struct wiimote_debug *debug;
  104. struct wiimote_queue queue;
  105. struct wiimote_state state;
  106. struct work_struct init_worker;
  107. };
  108. /* wiimote modules */
  109. enum wiimod_module {
  110. WIIMOD_KEYS,
  111. WIIMOD_RUMBLE,
  112. WIIMOD_BATTERY,
  113. WIIMOD_LED1,
  114. WIIMOD_LED2,
  115. WIIMOD_LED3,
  116. WIIMOD_LED4,
  117. WIIMOD_ACCEL,
  118. WIIMOD_NUM,
  119. WIIMOD_NULL = WIIMOD_NUM,
  120. };
  121. #define WIIMOD_FLAG_INPUT 0x0001
  122. struct wiimod_ops {
  123. __u16 flags;
  124. unsigned long arg;
  125. int (*probe) (const struct wiimod_ops *ops,
  126. struct wiimote_data *wdata);
  127. void (*remove) (const struct wiimod_ops *ops,
  128. struct wiimote_data *wdata);
  129. void (*in_keys) (struct wiimote_data *wdata, const __u8 *keys);
  130. void (*in_accel) (struct wiimote_data *wdata, const __u8 *accel);
  131. void (*in_ir) (struct wiimote_data *wdata, const __u8 *ir, bool packed,
  132. unsigned int id);
  133. };
  134. extern const struct wiimod_ops *wiimod_table[WIIMOD_NUM];
  135. /* wiimote requests */
  136. enum wiiproto_reqs {
  137. WIIPROTO_REQ_NULL = 0x0,
  138. WIIPROTO_REQ_RUMBLE = 0x10,
  139. WIIPROTO_REQ_LED = 0x11,
  140. WIIPROTO_REQ_DRM = 0x12,
  141. WIIPROTO_REQ_IR1 = 0x13,
  142. WIIPROTO_REQ_SREQ = 0x15,
  143. WIIPROTO_REQ_WMEM = 0x16,
  144. WIIPROTO_REQ_RMEM = 0x17,
  145. WIIPROTO_REQ_IR2 = 0x1a,
  146. WIIPROTO_REQ_STATUS = 0x20,
  147. WIIPROTO_REQ_DATA = 0x21,
  148. WIIPROTO_REQ_RETURN = 0x22,
  149. WIIPROTO_REQ_DRM_K = 0x30,
  150. WIIPROTO_REQ_DRM_KA = 0x31,
  151. WIIPROTO_REQ_DRM_KE = 0x32,
  152. WIIPROTO_REQ_DRM_KAI = 0x33,
  153. WIIPROTO_REQ_DRM_KEE = 0x34,
  154. WIIPROTO_REQ_DRM_KAE = 0x35,
  155. WIIPROTO_REQ_DRM_KIE = 0x36,
  156. WIIPROTO_REQ_DRM_KAIE = 0x37,
  157. WIIPROTO_REQ_DRM_E = 0x3d,
  158. WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
  159. WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
  160. WIIPROTO_REQ_MAX
  161. };
  162. #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
  163. dev))
  164. extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
  165. extern void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble);
  166. extern void wiiproto_req_leds(struct wiimote_data *wdata, int leds);
  167. extern void wiiproto_req_status(struct wiimote_data *wdata);
  168. extern void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel);
  169. extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  170. const __u8 *wmem, __u8 size);
  171. extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
  172. __u8 *rmem, __u8 size);
  173. #define wiiproto_req_rreg(wdata, os, sz) \
  174. wiiproto_req_rmem((wdata), false, (os), (sz))
  175. #define wiiproto_req_reeprom(wdata, os, sz) \
  176. wiiproto_req_rmem((wdata), true, (os), (sz))
  177. extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
  178. __u32 offset, __u16 size);
  179. #ifdef CONFIG_HID_WIIMOTE_EXT
  180. extern int wiiext_init(struct wiimote_data *wdata);
  181. extern void wiiext_deinit(struct wiimote_data *wdata);
  182. extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
  183. extern bool wiiext_active(struct wiimote_data *wdata);
  184. extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
  185. #else
  186. static inline int wiiext_init(void *u) { return 0; }
  187. static inline void wiiext_deinit(void *u) { }
  188. static inline void wiiext_event(void *u, bool p) { }
  189. static inline bool wiiext_active(void *u) { return false; }
  190. static inline void wiiext_handle(void *u, const __u8 *p) { }
  191. #endif
  192. #ifdef CONFIG_DEBUG_FS
  193. extern int wiidebug_init(struct wiimote_data *wdata);
  194. extern void wiidebug_deinit(struct wiimote_data *wdata);
  195. #else
  196. static inline int wiidebug_init(void *u) { return 0; }
  197. static inline void wiidebug_deinit(void *u) { }
  198. #endif
  199. /* requires the state.lock spinlock to be held */
  200. static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
  201. __u32 opt)
  202. {
  203. return wdata->state.cmd == cmd && wdata->state.opt == opt;
  204. }
  205. /* requires the state.lock spinlock to be held */
  206. static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
  207. {
  208. wdata->state.cmd = WIIPROTO_REQ_NULL;
  209. complete(&wdata->state.ready);
  210. }
  211. /* requires the state.lock spinlock to be held */
  212. static inline void wiimote_cmd_abort(struct wiimote_data *wdata)
  213. {
  214. /* Abort synchronous request by waking up the sleeping caller. But
  215. * reset the state.cmd field to an invalid value so no further event
  216. * handlers will work with it. */
  217. wdata->state.cmd = WIIPROTO_REQ_MAX;
  218. complete(&wdata->state.ready);
  219. }
  220. static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
  221. {
  222. return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
  223. }
  224. static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata)
  225. {
  226. mutex_lock(&wdata->state.sync);
  227. }
  228. /* requires the state.lock spinlock to be held */
  229. static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
  230. __u32 opt)
  231. {
  232. INIT_COMPLETION(wdata->state.ready);
  233. wdata->state.cmd = cmd;
  234. wdata->state.opt = opt;
  235. }
  236. static inline void wiimote_cmd_release(struct wiimote_data *wdata)
  237. {
  238. mutex_unlock(&wdata->state.sync);
  239. }
  240. static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
  241. {
  242. int ret;
  243. /* The completion acts as implicit memory barrier so we can safely
  244. * assume that state.cmd is set on success/failure and isn't accessed
  245. * by any other thread, anymore. */
  246. ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
  247. if (ret < 0)
  248. return -ERESTARTSYS;
  249. else if (ret == 0)
  250. return -EIO;
  251. else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
  252. return -EIO;
  253. else
  254. return 0;
  255. }
  256. static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata)
  257. {
  258. unsigned long ret;
  259. /* no locking needed; see wiimote_cmd_wait() */
  260. ret = wait_for_completion_timeout(&wdata->state.ready, HZ);
  261. if (!ret)
  262. return -EIO;
  263. else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
  264. return -EIO;
  265. else
  266. return 0;
  267. }
  268. #endif