]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/hid/hid-wiimote.h
HID: wiimote: add sub-device module infrastructure
[mirror_ubuntu-zesty-kernel.git] / drivers / hid / hid-wiimote.h
1 #ifndef __HID_WIIMOTE_H
2 #define __HID_WIIMOTE_H
3
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 * 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 */
38 #define WIIPROTO_FLAG_EXT_PLUGGED 0x0100
39
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
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
63 struct wiimote_buf {
64 __u8 data[HID_MAX_BUFFER_SIZE];
65 size_t size;
66 };
67
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
76 struct wiimote_state {
77 spinlock_t lock;
78 __u32 flags;
79 __u8 accel_split[2];
80 __u8 drm;
81 __u8 devtype;
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;
92 __u8 *cmd_read_buf;
93 __u8 cmd_read_size;
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;
103 struct wiimote_ext *ext;
104 struct wiimote_debug *debug;
105
106 struct wiimote_queue queue;
107 struct wiimote_state state;
108 struct work_struct init_worker;
109 };
110
111 /* wiimote modules */
112
113 enum wiimod_module {
114 WIIMOD_NUM,
115 WIIMOD_NULL = WIIMOD_NUM,
116 };
117
118 #define WIIMOD_FLAG_INPUT 0x0001
119
120 struct wiimod_ops {
121 __u16 flags;
122 unsigned long arg;
123 int (*probe) (const struct wiimod_ops *ops,
124 struct wiimote_data *wdata);
125 void (*remove) (const struct wiimod_ops *ops,
126 struct wiimote_data *wdata);
127
128 void (*in_keys) (struct wiimote_data *wdata, const __u8 *keys);
129 void (*in_accel) (struct wiimote_data *wdata, const __u8 *accel);
130 void (*in_ir) (struct wiimote_data *wdata, const __u8 *ir, bool packed,
131 unsigned int id);
132 };
133
134 extern const struct wiimod_ops *wiimod_table[WIIMOD_NUM];
135
136 /* wiimote requests */
137
138 enum wiiproto_reqs {
139 WIIPROTO_REQ_NULL = 0x0,
140 WIIPROTO_REQ_RUMBLE = 0x10,
141 WIIPROTO_REQ_LED = 0x11,
142 WIIPROTO_REQ_DRM = 0x12,
143 WIIPROTO_REQ_IR1 = 0x13,
144 WIIPROTO_REQ_SREQ = 0x15,
145 WIIPROTO_REQ_WMEM = 0x16,
146 WIIPROTO_REQ_RMEM = 0x17,
147 WIIPROTO_REQ_IR2 = 0x1a,
148 WIIPROTO_REQ_STATUS = 0x20,
149 WIIPROTO_REQ_DATA = 0x21,
150 WIIPROTO_REQ_RETURN = 0x22,
151 WIIPROTO_REQ_DRM_K = 0x30,
152 WIIPROTO_REQ_DRM_KA = 0x31,
153 WIIPROTO_REQ_DRM_KE = 0x32,
154 WIIPROTO_REQ_DRM_KAI = 0x33,
155 WIIPROTO_REQ_DRM_KEE = 0x34,
156 WIIPROTO_REQ_DRM_KAE = 0x35,
157 WIIPROTO_REQ_DRM_KIE = 0x36,
158 WIIPROTO_REQ_DRM_KAIE = 0x37,
159 WIIPROTO_REQ_DRM_E = 0x3d,
160 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
161 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
162 WIIPROTO_REQ_MAX
163 };
164
165 #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
166 dev))
167
168 extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
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
174 #define wiiproto_req_rreg(wdata, os, sz) \
175 wiiproto_req_rmem((wdata), false, (os), (sz))
176 #define wiiproto_req_reeprom(wdata, os, sz) \
177 wiiproto_req_rmem((wdata), true, (os), (sz))
178 extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
179 __u32 offset, __u16 size);
180
181 #ifdef CONFIG_HID_WIIMOTE_EXT
182
183 extern int wiiext_init(struct wiimote_data *wdata);
184 extern void wiiext_deinit(struct wiimote_data *wdata);
185 extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
186 extern bool wiiext_active(struct wiimote_data *wdata);
187 extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
188
189 #else
190
191 static inline int wiiext_init(void *u) { return 0; }
192 static inline void wiiext_deinit(void *u) { }
193 static inline void wiiext_event(void *u, bool p) { }
194 static inline bool wiiext_active(void *u) { return false; }
195 static inline void wiiext_handle(void *u, const __u8 *p) { }
196
197 #endif
198
199 #ifdef CONFIG_DEBUG_FS
200
201 extern int wiidebug_init(struct wiimote_data *wdata);
202 extern void wiidebug_deinit(struct wiimote_data *wdata);
203
204 #else
205
206 static inline int wiidebug_init(void *u) { return 0; }
207 static inline void wiidebug_deinit(void *u) { }
208
209 #endif
210
211 /* requires the state.lock spinlock to be held */
212 static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
213 __u32 opt)
214 {
215 return wdata->state.cmd == cmd && wdata->state.opt == opt;
216 }
217
218 /* requires the state.lock spinlock to be held */
219 static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
220 {
221 wdata->state.cmd = WIIPROTO_REQ_NULL;
222 complete(&wdata->state.ready);
223 }
224
225 /* requires the state.lock spinlock to be held */
226 static inline void wiimote_cmd_abort(struct wiimote_data *wdata)
227 {
228 /* Abort synchronous request by waking up the sleeping caller. But
229 * reset the state.cmd field to an invalid value so no further event
230 * handlers will work with it. */
231 wdata->state.cmd = WIIPROTO_REQ_MAX;
232 complete(&wdata->state.ready);
233 }
234
235 static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
236 {
237 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
238 }
239
240 static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata)
241 {
242 mutex_lock(&wdata->state.sync);
243 }
244
245 /* requires the state.lock spinlock to be held */
246 static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
247 __u32 opt)
248 {
249 INIT_COMPLETION(wdata->state.ready);
250 wdata->state.cmd = cmd;
251 wdata->state.opt = opt;
252 }
253
254 static inline void wiimote_cmd_release(struct wiimote_data *wdata)
255 {
256 mutex_unlock(&wdata->state.sync);
257 }
258
259 static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
260 {
261 int ret;
262
263 /* The completion acts as implicit memory barrier so we can safely
264 * assume that state.cmd is set on success/failure and isn't accessed
265 * by any other thread, anymore. */
266
267 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
268 if (ret < 0)
269 return -ERESTARTSYS;
270 else if (ret == 0)
271 return -EIO;
272 else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
273 return -EIO;
274 else
275 return 0;
276 }
277
278 static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata)
279 {
280 unsigned long ret;
281
282 /* no locking needed; see wiimote_cmd_wait() */
283 ret = wait_for_completion_timeout(&wdata->state.ready, HZ);
284 if (!ret)
285 return -EIO;
286 else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
287 return -EIO;
288 else
289 return 0;
290 }
291
292 #endif