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