hid-wiimote.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include <linux/timer.h>
  23. #define WIIMOTE_NAME "Nintendo Wii Remote"
  24. #define WIIMOTE_BUFSIZE 32
  25. #define WIIPROTO_FLAG_LED1 0x01
  26. #define WIIPROTO_FLAG_LED2 0x02
  27. #define WIIPROTO_FLAG_LED3 0x04
  28. #define WIIPROTO_FLAG_LED4 0x08
  29. #define WIIPROTO_FLAG_RUMBLE 0x10
  30. #define WIIPROTO_FLAG_ACCEL 0x20
  31. #define WIIPROTO_FLAG_IR_BASIC 0x40
  32. #define WIIPROTO_FLAG_IR_EXT 0x80
  33. #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */
  34. #define WIIPROTO_FLAG_EXT_PLUGGED 0x0100
  35. #define WIIPROTO_FLAG_EXT_USED 0x0200
  36. #define WIIPROTO_FLAG_EXT_ACTIVE 0x0400
  37. #define WIIPROTO_FLAG_MP_PLUGGED 0x0800
  38. #define WIIPROTO_FLAG_MP_USED 0x1000
  39. #define WIIPROTO_FLAG_MP_ACTIVE 0x2000
  40. #define WIIPROTO_FLAG_EXITING 0x4000
  41. #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
  42. WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
  43. #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
  44. WIIPROTO_FLAG_IR_FULL)
  45. /* return flag for led \num */
  46. #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
  47. enum wiiproto_keys {
  48. WIIPROTO_KEY_LEFT,
  49. WIIPROTO_KEY_RIGHT,
  50. WIIPROTO_KEY_UP,
  51. WIIPROTO_KEY_DOWN,
  52. WIIPROTO_KEY_PLUS,
  53. WIIPROTO_KEY_MINUS,
  54. WIIPROTO_KEY_ONE,
  55. WIIPROTO_KEY_TWO,
  56. WIIPROTO_KEY_A,
  57. WIIPROTO_KEY_B,
  58. WIIPROTO_KEY_HOME,
  59. WIIPROTO_KEY_COUNT
  60. };
  61. enum wiimote_devtype {
  62. WIIMOTE_DEV_PENDING,
  63. WIIMOTE_DEV_UNKNOWN,
  64. WIIMOTE_DEV_GENERIC,
  65. WIIMOTE_DEV_GEN10,
  66. WIIMOTE_DEV_GEN20,
  67. WIIMOTE_DEV_BALANCE_BOARD,
  68. WIIMOTE_DEV_NUM,
  69. };
  70. enum wiimote_exttype {
  71. WIIMOTE_EXT_NONE,
  72. WIIMOTE_EXT_UNKNOWN,
  73. WIIMOTE_EXT_BALANCE_BOARD,
  74. WIIMOTE_EXT_NUM,
  75. };
  76. enum wiimote_mptype {
  77. WIIMOTE_MP_NONE,
  78. WIIMOTE_MP_UNKNOWN,
  79. WIIMOTE_MP_SINGLE,
  80. WIIMOTE_MP_PASSTHROUGH_NUNCHUK,
  81. WIIMOTE_MP_PASSTHROUGH_CLASSIC,
  82. };
  83. struct wiimote_buf {
  84. __u8 data[HID_MAX_BUFFER_SIZE];
  85. size_t size;
  86. };
  87. struct wiimote_queue {
  88. spinlock_t lock;
  89. struct work_struct worker;
  90. __u8 head;
  91. __u8 tail;
  92. struct wiimote_buf outq[WIIMOTE_BUFSIZE];
  93. };
  94. struct wiimote_state {
  95. spinlock_t lock;
  96. __u32 flags;
  97. __u8 accel_split[2];
  98. __u8 drm;
  99. __u8 devtype;
  100. __u8 exttype;
  101. __u8 mp;
  102. /* synchronous cmd requests */
  103. struct mutex sync;
  104. struct completion ready;
  105. int cmd;
  106. __u32 opt;
  107. /* results of synchronous requests */
  108. __u8 cmd_battery;
  109. __u8 cmd_err;
  110. __u8 *cmd_read_buf;
  111. __u8 cmd_read_size;
  112. /* calibration data */
  113. __u16 calib_bboard[4][3];
  114. };
  115. struct wiimote_data {
  116. struct hid_device *hdev;
  117. struct input_dev *input;
  118. struct led_classdev *leds[4];
  119. struct input_dev *accel;
  120. struct input_dev *ir;
  121. struct power_supply battery;
  122. struct timer_list timer;
  123. struct wiimote_ext *ext;
  124. struct wiimote_debug *debug;
  125. union {
  126. struct input_dev *input;
  127. } extension;
  128. struct wiimote_queue queue;
  129. struct wiimote_state state;
  130. struct work_struct init_worker;
  131. };
  132. /* wiimote modules */
  133. enum wiimod_module {
  134. WIIMOD_KEYS,
  135. WIIMOD_RUMBLE,
  136. WIIMOD_BATTERY,
  137. WIIMOD_LED1,
  138. WIIMOD_LED2,
  139. WIIMOD_LED3,
  140. WIIMOD_LED4,
  141. WIIMOD_ACCEL,
  142. WIIMOD_IR,
  143. WIIMOD_NUM,
  144. WIIMOD_NULL = WIIMOD_NUM,
  145. };
  146. #define WIIMOD_FLAG_INPUT 0x0001
  147. #define WIIMOD_FLAG_EXT8 0x0002
  148. #define WIIMOD_FLAG_EXT16 0x0004
  149. struct wiimod_ops {
  150. __u16 flags;
  151. unsigned long arg;
  152. int (*probe) (const struct wiimod_ops *ops,
  153. struct wiimote_data *wdata);
  154. void (*remove) (const struct wiimod_ops *ops,
  155. struct wiimote_data *wdata);
  156. void (*in_keys) (struct wiimote_data *wdata, const __u8 *keys);
  157. void (*in_accel) (struct wiimote_data *wdata, const __u8 *accel);
  158. void (*in_ir) (struct wiimote_data *wdata, const __u8 *ir, bool packed,
  159. unsigned int id);
  160. void (*in_mp) (struct wiimote_data *wdata, const __u8 *mp);
  161. void (*in_ext) (struct wiimote_data *wdata, const __u8 *ext);
  162. };
  163. extern const struct wiimod_ops *wiimod_table[WIIMOD_NUM];
  164. extern const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM];
  165. extern const struct wiimod_ops wiimod_mp;
  166. /* wiimote requests */
  167. enum wiiproto_reqs {
  168. WIIPROTO_REQ_NULL = 0x0,
  169. WIIPROTO_REQ_RUMBLE = 0x10,
  170. WIIPROTO_REQ_LED = 0x11,
  171. WIIPROTO_REQ_DRM = 0x12,
  172. WIIPROTO_REQ_IR1 = 0x13,
  173. WIIPROTO_REQ_SREQ = 0x15,
  174. WIIPROTO_REQ_WMEM = 0x16,
  175. WIIPROTO_REQ_RMEM = 0x17,
  176. WIIPROTO_REQ_IR2 = 0x1a,
  177. WIIPROTO_REQ_STATUS = 0x20,
  178. WIIPROTO_REQ_DATA = 0x21,
  179. WIIPROTO_REQ_RETURN = 0x22,
  180. /* DRM_K: BB*2 */
  181. WIIPROTO_REQ_DRM_K = 0x30,
  182. /* DRM_KA: BB*2 AA*3 */
  183. WIIPROTO_REQ_DRM_KA = 0x31,
  184. /* DRM_KE: BB*2 EE*8 */
  185. WIIPROTO_REQ_DRM_KE = 0x32,
  186. /* DRM_KAI: BB*2 AA*3 II*12 */
  187. WIIPROTO_REQ_DRM_KAI = 0x33,
  188. /* DRM_KEE: BB*2 EE*19 */
  189. WIIPROTO_REQ_DRM_KEE = 0x34,
  190. /* DRM_KAE: BB*2 AA*3 EE*16 */
  191. WIIPROTO_REQ_DRM_KAE = 0x35,
  192. /* DRM_KIE: BB*2 II*10 EE*9 */
  193. WIIPROTO_REQ_DRM_KIE = 0x36,
  194. /* DRM_KAIE: BB*2 AA*3 II*10 EE*6 */
  195. WIIPROTO_REQ_DRM_KAIE = 0x37,
  196. /* DRM_E: EE*21 */
  197. WIIPROTO_REQ_DRM_E = 0x3d,
  198. /* DRM_SKAI1: BB*2 AA*1 II*18 */
  199. WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
  200. /* DRM_SKAI2: BB*2 AA*1 II*18 */
  201. WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
  202. WIIPROTO_REQ_MAX
  203. };
  204. #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
  205. dev))
  206. void __wiimote_schedule(struct wiimote_data *wdata);
  207. extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
  208. extern void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble);
  209. extern void wiiproto_req_leds(struct wiimote_data *wdata, int leds);
  210. extern void wiiproto_req_status(struct wiimote_data *wdata);
  211. extern void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel);
  212. extern void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags);
  213. extern void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags);
  214. extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  215. const __u8 *wmem, __u8 size);
  216. extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
  217. __u8 *rmem, __u8 size);
  218. #define wiiproto_req_rreg(wdata, os, sz) \
  219. wiiproto_req_rmem((wdata), false, (os), (sz))
  220. #define wiiproto_req_reeprom(wdata, os, sz) \
  221. wiiproto_req_rmem((wdata), true, (os), (sz))
  222. extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
  223. __u32 offset, __u16 size);
  224. #ifdef CONFIG_HID_WIIMOTE_EXT
  225. extern int wiiext_init(struct wiimote_data *wdata);
  226. extern void wiiext_deinit(struct wiimote_data *wdata);
  227. extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
  228. extern bool wiiext_active(struct wiimote_data *wdata);
  229. extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
  230. #else
  231. static inline int wiiext_init(void *u) { return 0; }
  232. static inline void wiiext_deinit(void *u) { }
  233. static inline void wiiext_event(void *u, bool p) { }
  234. static inline bool wiiext_active(void *u) { return false; }
  235. static inline void wiiext_handle(void *u, const __u8 *p) { }
  236. #endif
  237. #ifdef CONFIG_DEBUG_FS
  238. extern int wiidebug_init(struct wiimote_data *wdata);
  239. extern void wiidebug_deinit(struct wiimote_data *wdata);
  240. #else
  241. static inline int wiidebug_init(void *u) { return 0; }
  242. static inline void wiidebug_deinit(void *u) { }
  243. #endif
  244. /* requires the state.lock spinlock to be held */
  245. static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
  246. __u32 opt)
  247. {
  248. return wdata->state.cmd == cmd && wdata->state.opt == opt;
  249. }
  250. /* requires the state.lock spinlock to be held */
  251. static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
  252. {
  253. wdata->state.cmd = WIIPROTO_REQ_NULL;
  254. complete(&wdata->state.ready);
  255. }
  256. /* requires the state.lock spinlock to be held */
  257. static inline void wiimote_cmd_abort(struct wiimote_data *wdata)
  258. {
  259. /* Abort synchronous request by waking up the sleeping caller. But
  260. * reset the state.cmd field to an invalid value so no further event
  261. * handlers will work with it. */
  262. wdata->state.cmd = WIIPROTO_REQ_MAX;
  263. complete(&wdata->state.ready);
  264. }
  265. static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
  266. {
  267. return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
  268. }
  269. static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata)
  270. {
  271. mutex_lock(&wdata->state.sync);
  272. }
  273. /* requires the state.lock spinlock to be held */
  274. static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
  275. __u32 opt)
  276. {
  277. INIT_COMPLETION(wdata->state.ready);
  278. wdata->state.cmd = cmd;
  279. wdata->state.opt = opt;
  280. }
  281. static inline void wiimote_cmd_release(struct wiimote_data *wdata)
  282. {
  283. mutex_unlock(&wdata->state.sync);
  284. }
  285. static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
  286. {
  287. int ret;
  288. /* The completion acts as implicit memory barrier so we can safely
  289. * assume that state.cmd is set on success/failure and isn't accessed
  290. * by any other thread, anymore. */
  291. ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
  292. if (ret < 0)
  293. return -ERESTARTSYS;
  294. else if (ret == 0)
  295. return -EIO;
  296. else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
  297. return -EIO;
  298. else
  299. return 0;
  300. }
  301. static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata)
  302. {
  303. unsigned long ret;
  304. /* no locking needed; see wiimote_cmd_wait() */
  305. ret = wait_for_completion_timeout(&wdata->state.ready, HZ);
  306. if (!ret)
  307. return -EIO;
  308. else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
  309. return -EIO;
  310. else
  311. return 0;
  312. }
  313. #endif