David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 1 | #ifndef __HID_WIIMOTE_H |
| 2 | #define __HID_WIIMOTE_H |
| 3 | |
| 4 | /* |
David Herrmann | 92eda7e | 2013-05-05 23:12:45 +0200 | [diff] [blame] | 5 | * HID driver for Nintendo Wii / Wii U peripherals |
| 6 | * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com> |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation; either version 2 of the License, or (at your option) |
| 13 | * any later version. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/completion.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/hid.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <linux/leds.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/power_supply.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | |
| 26 | #define WIIMOTE_NAME "Nintendo Wii Remote" |
| 27 | #define WIIMOTE_BUFSIZE 32 |
| 28 | |
| 29 | #define WIIPROTO_FLAG_LED1 0x01 |
| 30 | #define WIIPROTO_FLAG_LED2 0x02 |
| 31 | #define WIIPROTO_FLAG_LED3 0x04 |
| 32 | #define WIIPROTO_FLAG_LED4 0x08 |
| 33 | #define WIIPROTO_FLAG_RUMBLE 0x10 |
| 34 | #define WIIPROTO_FLAG_ACCEL 0x20 |
| 35 | #define WIIPROTO_FLAG_IR_BASIC 0x40 |
| 36 | #define WIIPROTO_FLAG_IR_EXT 0x80 |
| 37 | #define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */ |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 38 | #define WIIPROTO_FLAG_EXT_PLUGGED 0x0100 |
| 39 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 40 | #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ |
| 41 | WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) |
| 42 | #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \ |
| 43 | WIIPROTO_FLAG_IR_FULL) |
| 44 | |
| 45 | /* return flag for led \num */ |
| 46 | #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1)) |
| 47 | |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 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 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 63 | enum wiimote_devtype { |
| 64 | WIIMOTE_DEV_PENDING, |
| 65 | WIIMOTE_DEV_UNKNOWN, |
| 66 | WIIMOTE_DEV_GENERIC, |
| 67 | WIIMOTE_DEV_GEN10, |
| 68 | WIIMOTE_DEV_GEN20, |
| 69 | WIIMOTE_DEV_NUM, |
| 70 | }; |
| 71 | |
| 72 | enum wiimote_exttype { |
| 73 | WIIMOTE_EXT_NONE, |
| 74 | WIIMOTE_EXT_UNKNOWN, |
| 75 | WIIMOTE_EXT_NUM, |
| 76 | }; |
| 77 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 78 | struct wiimote_buf { |
| 79 | __u8 data[HID_MAX_BUFFER_SIZE]; |
| 80 | size_t size; |
| 81 | }; |
| 82 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 83 | struct wiimote_queue { |
| 84 | spinlock_t lock; |
| 85 | struct work_struct worker; |
| 86 | __u8 head; |
| 87 | __u8 tail; |
| 88 | struct wiimote_buf outq[WIIMOTE_BUFSIZE]; |
| 89 | }; |
| 90 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 91 | struct wiimote_state { |
| 92 | spinlock_t lock; |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 93 | __u32 flags; |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 94 | __u8 accel_split[2]; |
David Herrmann | 43d782a | 2011-11-17 14:12:12 +0100 | [diff] [blame] | 95 | __u8 drm; |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 96 | __u8 devtype; |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 97 | |
| 98 | /* synchronous cmd requests */ |
| 99 | struct mutex sync; |
| 100 | struct completion ready; |
| 101 | int cmd; |
| 102 | __u32 opt; |
| 103 | |
| 104 | /* results of synchronous requests */ |
| 105 | __u8 cmd_battery; |
| 106 | __u8 cmd_err; |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 107 | __u8 *cmd_read_buf; |
| 108 | __u8 cmd_read_size; |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | struct wiimote_data { |
| 112 | struct hid_device *hdev; |
| 113 | struct input_dev *input; |
| 114 | struct led_classdev *leds[4]; |
| 115 | struct input_dev *accel; |
| 116 | struct input_dev *ir; |
| 117 | struct power_supply battery; |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 118 | struct wiimote_ext *ext; |
David Herrmann | 43e5e7c | 2011-11-17 14:12:10 +0100 | [diff] [blame] | 119 | struct wiimote_debug *debug; |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 120 | |
David Herrmann | 1393853 | 2013-05-05 23:12:46 +0200 | [diff] [blame] | 121 | struct wiimote_queue queue; |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 122 | struct wiimote_state state; |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 123 | struct work_struct init_worker; |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 126 | /* wiimote modules */ |
| 127 | |
| 128 | enum wiimod_module { |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 129 | WIIMOD_KEYS, |
| 130 | WIIMOD_RUMBLE, |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame^] | 131 | WIIMOD_BATTERY, |
David Herrmann | 27f0694 | 2013-05-05 23:12:51 +0200 | [diff] [blame] | 132 | WIIMOD_NUM, |
| 133 | WIIMOD_NULL = WIIMOD_NUM, |
| 134 | }; |
| 135 | |
| 136 | #define WIIMOD_FLAG_INPUT 0x0001 |
| 137 | |
| 138 | struct wiimod_ops { |
| 139 | __u16 flags; |
| 140 | unsigned long arg; |
| 141 | int (*probe) (const struct wiimod_ops *ops, |
| 142 | struct wiimote_data *wdata); |
| 143 | void (*remove) (const struct wiimod_ops *ops, |
| 144 | struct wiimote_data *wdata); |
| 145 | |
| 146 | void (*in_keys) (struct wiimote_data *wdata, const __u8 *keys); |
| 147 | void (*in_accel) (struct wiimote_data *wdata, const __u8 *accel); |
| 148 | void (*in_ir) (struct wiimote_data *wdata, const __u8 *ir, bool packed, |
| 149 | unsigned int id); |
| 150 | }; |
| 151 | |
| 152 | extern const struct wiimod_ops *wiimod_table[WIIMOD_NUM]; |
| 153 | |
| 154 | /* wiimote requests */ |
| 155 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 156 | enum wiiproto_reqs { |
| 157 | WIIPROTO_REQ_NULL = 0x0, |
| 158 | WIIPROTO_REQ_RUMBLE = 0x10, |
| 159 | WIIPROTO_REQ_LED = 0x11, |
| 160 | WIIPROTO_REQ_DRM = 0x12, |
| 161 | WIIPROTO_REQ_IR1 = 0x13, |
| 162 | WIIPROTO_REQ_SREQ = 0x15, |
| 163 | WIIPROTO_REQ_WMEM = 0x16, |
| 164 | WIIPROTO_REQ_RMEM = 0x17, |
| 165 | WIIPROTO_REQ_IR2 = 0x1a, |
| 166 | WIIPROTO_REQ_STATUS = 0x20, |
| 167 | WIIPROTO_REQ_DATA = 0x21, |
| 168 | WIIPROTO_REQ_RETURN = 0x22, |
| 169 | WIIPROTO_REQ_DRM_K = 0x30, |
| 170 | WIIPROTO_REQ_DRM_KA = 0x31, |
| 171 | WIIPROTO_REQ_DRM_KE = 0x32, |
| 172 | WIIPROTO_REQ_DRM_KAI = 0x33, |
| 173 | WIIPROTO_REQ_DRM_KEE = 0x34, |
| 174 | WIIPROTO_REQ_DRM_KAE = 0x35, |
| 175 | WIIPROTO_REQ_DRM_KIE = 0x36, |
| 176 | WIIPROTO_REQ_DRM_KAIE = 0x37, |
| 177 | WIIPROTO_REQ_DRM_E = 0x3d, |
| 178 | WIIPROTO_REQ_DRM_SKAI1 = 0x3e, |
| 179 | WIIPROTO_REQ_DRM_SKAI2 = 0x3f, |
David Herrmann | 43d782a | 2011-11-17 14:12:12 +0100 | [diff] [blame] | 180 | WIIPROTO_REQ_MAX |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \ |
| 184 | dev)) |
| 185 | |
| 186 | extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm); |
David Herrmann | 20cef81 | 2013-05-05 23:12:52 +0200 | [diff] [blame] | 187 | extern void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble); |
David Herrmann | dcf3923 | 2013-05-05 23:12:53 +0200 | [diff] [blame^] | 188 | extern void wiiproto_req_status(struct wiimote_data *wdata); |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 189 | extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, |
| 190 | const __u8 *wmem, __u8 size); |
David Herrmann | fad8c0e | 2011-11-17 14:12:00 +0100 | [diff] [blame] | 191 | extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, |
| 192 | __u8 *rmem, __u8 size); |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 193 | |
David Herrmann | 1d3452c | 2011-11-17 14:12:11 +0100 | [diff] [blame] | 194 | #define wiiproto_req_rreg(wdata, os, sz) \ |
| 195 | wiiproto_req_rmem((wdata), false, (os), (sz)) |
| 196 | #define wiiproto_req_reeprom(wdata, os, sz) \ |
| 197 | wiiproto_req_rmem((wdata), true, (os), (sz)) |
| 198 | extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, |
| 199 | __u32 offset, __u16 size); |
| 200 | |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 201 | #ifdef CONFIG_HID_WIIMOTE_EXT |
| 202 | |
| 203 | extern int wiiext_init(struct wiimote_data *wdata); |
| 204 | extern void wiiext_deinit(struct wiimote_data *wdata); |
| 205 | extern void wiiext_event(struct wiimote_data *wdata, bool plugged); |
| 206 | extern bool wiiext_active(struct wiimote_data *wdata); |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 207 | extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload); |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 208 | |
| 209 | #else |
| 210 | |
| 211 | static inline int wiiext_init(void *u) { return 0; } |
| 212 | static inline void wiiext_deinit(void *u) { } |
| 213 | static inline void wiiext_event(void *u, bool p) { } |
| 214 | static inline bool wiiext_active(void *u) { return false; } |
David Herrmann | 0b6815d | 2011-11-17 14:12:06 +0100 | [diff] [blame] | 215 | static inline void wiiext_handle(void *u, const __u8 *p) { } |
David Herrmann | cb99221 | 2011-11-17 14:12:01 +0100 | [diff] [blame] | 216 | |
| 217 | #endif |
| 218 | |
David Herrmann | 43e5e7c | 2011-11-17 14:12:10 +0100 | [diff] [blame] | 219 | #ifdef CONFIG_DEBUG_FS |
| 220 | |
| 221 | extern int wiidebug_init(struct wiimote_data *wdata); |
| 222 | extern void wiidebug_deinit(struct wiimote_data *wdata); |
| 223 | |
| 224 | #else |
| 225 | |
| 226 | static inline int wiidebug_init(void *u) { return 0; } |
| 227 | static inline void wiidebug_deinit(void *u) { } |
| 228 | |
| 229 | #endif |
| 230 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 231 | /* requires the state.lock spinlock to be held */ |
| 232 | static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd, |
| 233 | __u32 opt) |
| 234 | { |
| 235 | return wdata->state.cmd == cmd && wdata->state.opt == opt; |
| 236 | } |
| 237 | |
| 238 | /* requires the state.lock spinlock to be held */ |
| 239 | static inline void wiimote_cmd_complete(struct wiimote_data *wdata) |
| 240 | { |
| 241 | wdata->state.cmd = WIIPROTO_REQ_NULL; |
| 242 | complete(&wdata->state.ready); |
| 243 | } |
| 244 | |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 245 | /* requires the state.lock spinlock to be held */ |
| 246 | static inline void wiimote_cmd_abort(struct wiimote_data *wdata) |
| 247 | { |
| 248 | /* Abort synchronous request by waking up the sleeping caller. But |
| 249 | * reset the state.cmd field to an invalid value so no further event |
| 250 | * handlers will work with it. */ |
| 251 | wdata->state.cmd = WIIPROTO_REQ_MAX; |
| 252 | complete(&wdata->state.ready); |
| 253 | } |
| 254 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 255 | static inline int wiimote_cmd_acquire(struct wiimote_data *wdata) |
| 256 | { |
| 257 | return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0; |
| 258 | } |
| 259 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 260 | static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata) |
| 261 | { |
| 262 | mutex_lock(&wdata->state.sync); |
| 263 | } |
| 264 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 265 | /* requires the state.lock spinlock to be held */ |
| 266 | static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd, |
| 267 | __u32 opt) |
| 268 | { |
| 269 | INIT_COMPLETION(wdata->state.ready); |
| 270 | wdata->state.cmd = cmd; |
| 271 | wdata->state.opt = opt; |
| 272 | } |
| 273 | |
| 274 | static inline void wiimote_cmd_release(struct wiimote_data *wdata) |
| 275 | { |
| 276 | mutex_unlock(&wdata->state.sync); |
| 277 | } |
| 278 | |
| 279 | static inline int wiimote_cmd_wait(struct wiimote_data *wdata) |
| 280 | { |
| 281 | int ret; |
| 282 | |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 283 | /* The completion acts as implicit memory barrier so we can safely |
| 284 | * assume that state.cmd is set on success/failure and isn't accessed |
| 285 | * by any other thread, anymore. */ |
| 286 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 287 | ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ); |
| 288 | if (ret < 0) |
| 289 | return -ERESTARTSYS; |
| 290 | else if (ret == 0) |
| 291 | return -EIO; |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 292 | else if (wdata->state.cmd != WIIPROTO_REQ_NULL) |
| 293 | return -EIO; |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 294 | else |
| 295 | return 0; |
| 296 | } |
| 297 | |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 298 | static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata) |
| 299 | { |
| 300 | unsigned long ret; |
| 301 | |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 302 | /* no locking needed; see wiimote_cmd_wait() */ |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 303 | ret = wait_for_completion_timeout(&wdata->state.ready, HZ); |
| 304 | if (!ret) |
| 305 | return -EIO; |
David Herrmann | d758b1f0 | 2013-05-05 23:12:50 +0200 | [diff] [blame] | 306 | else if (wdata->state.cmd != WIIPROTO_REQ_NULL) |
| 307 | return -EIO; |
David Herrmann | c57ff76 | 2013-05-05 23:12:48 +0200 | [diff] [blame] | 308 | else |
| 309 | return 0; |
| 310 | } |
| 311 | |
David Herrmann | 7e27440 | 2011-11-17 14:11:59 +0100 | [diff] [blame] | 312 | #endif |