hid-wiimote.h 9.2 KB

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