blob: 3a2d3a1d3d636782a2b308e6250333d35af26558 [file] [log] [blame]
David Herrmann7e274402011-11-17 14:11:59 +01001#ifndef __HID_WIIMOTE_H
2#define __HID_WIIMOTE_H
3
4/*
David Herrmann92eda7e2013-05-05 23:12:45 +02005 * HID driver for Nintendo Wii / Wii U peripherals
6 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
David Herrmann7e274402011-11-17 14:11:59 +01007 */
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 Herrmannc57ff762013-05-05 23:12:48 +020038#define WIIPROTO_FLAG_EXT_PLUGGED 0x0100
39
David Herrmann7e274402011-11-17 14:11:59 +010040#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 Herrmann20cef812013-05-05 23:12:52 +020048enum 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 Herrmannc57ff762013-05-05 23:12:48 +020063enum 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
72enum wiimote_exttype {
73 WIIMOTE_EXT_NONE,
74 WIIMOTE_EXT_UNKNOWN,
75 WIIMOTE_EXT_NUM,
76};
77
David Herrmann7e274402011-11-17 14:11:59 +010078struct wiimote_buf {
79 __u8 data[HID_MAX_BUFFER_SIZE];
80 size_t size;
81};
82
David Herrmann13938532013-05-05 23:12:46 +020083struct 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 Herrmann7e274402011-11-17 14:11:59 +010091struct wiimote_state {
92 spinlock_t lock;
David Herrmannc57ff762013-05-05 23:12:48 +020093 __u32 flags;
David Herrmann7e274402011-11-17 14:11:59 +010094 __u8 accel_split[2];
David Herrmann43d782a2011-11-17 14:12:12 +010095 __u8 drm;
David Herrmannc57ff762013-05-05 23:12:48 +020096 __u8 devtype;
David Herrmann7e274402011-11-17 14:11:59 +010097
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 Herrmannfad8c0e2011-11-17 14:12:00 +0100107 __u8 *cmd_read_buf;
108 __u8 cmd_read_size;
David Herrmann7e274402011-11-17 14:11:59 +0100109};
110
111struct 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 Herrmanncb992212011-11-17 14:12:01 +0100118 struct wiimote_ext *ext;
David Herrmann43e5e7c2011-11-17 14:12:10 +0100119 struct wiimote_debug *debug;
David Herrmann7e274402011-11-17 14:11:59 +0100120
David Herrmann13938532013-05-05 23:12:46 +0200121 struct wiimote_queue queue;
David Herrmann7e274402011-11-17 14:11:59 +0100122 struct wiimote_state state;
David Herrmannc57ff762013-05-05 23:12:48 +0200123 struct work_struct init_worker;
David Herrmann7e274402011-11-17 14:11:59 +0100124};
125
David Herrmann27f06942013-05-05 23:12:51 +0200126/* wiimote modules */
127
128enum wiimod_module {
David Herrmann20cef812013-05-05 23:12:52 +0200129 WIIMOD_KEYS,
130 WIIMOD_RUMBLE,
David Herrmanndcf39232013-05-05 23:12:53 +0200131 WIIMOD_BATTERY,
David Herrmann6c5ae012013-05-05 23:12:54 +0200132 WIIMOD_LED1,
133 WIIMOD_LED2,
134 WIIMOD_LED3,
135 WIIMOD_LED4,
David Herrmann0ea16752013-05-05 23:12:55 +0200136 WIIMOD_ACCEL,
David Herrmann3b5f03c2013-05-05 23:12:56 +0200137 WIIMOD_IR,
David Herrmann27f06942013-05-05 23:12:51 +0200138 WIIMOD_NUM,
139 WIIMOD_NULL = WIIMOD_NUM,
140};
141
142#define WIIMOD_FLAG_INPUT 0x0001
143
144struct wiimod_ops {
145 __u16 flags;
146 unsigned long arg;
147 int (*probe) (const struct wiimod_ops *ops,
148 struct wiimote_data *wdata);
149 void (*remove) (const struct wiimod_ops *ops,
150 struct wiimote_data *wdata);
151
152 void (*in_keys) (struct wiimote_data *wdata, const __u8 *keys);
153 void (*in_accel) (struct wiimote_data *wdata, const __u8 *accel);
154 void (*in_ir) (struct wiimote_data *wdata, const __u8 *ir, bool packed,
155 unsigned int id);
156};
157
158extern const struct wiimod_ops *wiimod_table[WIIMOD_NUM];
159
160/* wiimote requests */
161
David Herrmann7e274402011-11-17 14:11:59 +0100162enum wiiproto_reqs {
163 WIIPROTO_REQ_NULL = 0x0,
164 WIIPROTO_REQ_RUMBLE = 0x10,
165 WIIPROTO_REQ_LED = 0x11,
166 WIIPROTO_REQ_DRM = 0x12,
167 WIIPROTO_REQ_IR1 = 0x13,
168 WIIPROTO_REQ_SREQ = 0x15,
169 WIIPROTO_REQ_WMEM = 0x16,
170 WIIPROTO_REQ_RMEM = 0x17,
171 WIIPROTO_REQ_IR2 = 0x1a,
172 WIIPROTO_REQ_STATUS = 0x20,
173 WIIPROTO_REQ_DATA = 0x21,
174 WIIPROTO_REQ_RETURN = 0x22,
175 WIIPROTO_REQ_DRM_K = 0x30,
176 WIIPROTO_REQ_DRM_KA = 0x31,
177 WIIPROTO_REQ_DRM_KE = 0x32,
178 WIIPROTO_REQ_DRM_KAI = 0x33,
179 WIIPROTO_REQ_DRM_KEE = 0x34,
180 WIIPROTO_REQ_DRM_KAE = 0x35,
181 WIIPROTO_REQ_DRM_KIE = 0x36,
182 WIIPROTO_REQ_DRM_KAIE = 0x37,
183 WIIPROTO_REQ_DRM_E = 0x3d,
184 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
185 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
David Herrmann43d782a2011-11-17 14:12:12 +0100186 WIIPROTO_REQ_MAX
David Herrmann7e274402011-11-17 14:11:59 +0100187};
188
189#define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
190 dev))
191
192extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
David Herrmann20cef812013-05-05 23:12:52 +0200193extern void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble);
David Herrmann6c5ae012013-05-05 23:12:54 +0200194extern void wiiproto_req_leds(struct wiimote_data *wdata, int leds);
David Herrmanndcf39232013-05-05 23:12:53 +0200195extern void wiiproto_req_status(struct wiimote_data *wdata);
David Herrmann0ea16752013-05-05 23:12:55 +0200196extern void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel);
David Herrmann3b5f03c2013-05-05 23:12:56 +0200197extern void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags);
198extern void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags);
David Herrmann7e274402011-11-17 14:11:59 +0100199extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
200 const __u8 *wmem, __u8 size);
David Herrmannfad8c0e2011-11-17 14:12:00 +0100201extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
202 __u8 *rmem, __u8 size);
David Herrmann7e274402011-11-17 14:11:59 +0100203
David Herrmann1d3452c2011-11-17 14:12:11 +0100204#define wiiproto_req_rreg(wdata, os, sz) \
205 wiiproto_req_rmem((wdata), false, (os), (sz))
206#define wiiproto_req_reeprom(wdata, os, sz) \
207 wiiproto_req_rmem((wdata), true, (os), (sz))
208extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
209 __u32 offset, __u16 size);
210
David Herrmanncb992212011-11-17 14:12:01 +0100211#ifdef CONFIG_HID_WIIMOTE_EXT
212
213extern int wiiext_init(struct wiimote_data *wdata);
214extern void wiiext_deinit(struct wiimote_data *wdata);
215extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
216extern bool wiiext_active(struct wiimote_data *wdata);
David Herrmann0b6815d2011-11-17 14:12:06 +0100217extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
David Herrmanncb992212011-11-17 14:12:01 +0100218
219#else
220
221static inline int wiiext_init(void *u) { return 0; }
222static inline void wiiext_deinit(void *u) { }
223static inline void wiiext_event(void *u, bool p) { }
224static inline bool wiiext_active(void *u) { return false; }
David Herrmann0b6815d2011-11-17 14:12:06 +0100225static inline void wiiext_handle(void *u, const __u8 *p) { }
David Herrmanncb992212011-11-17 14:12:01 +0100226
227#endif
228
David Herrmann43e5e7c2011-11-17 14:12:10 +0100229#ifdef CONFIG_DEBUG_FS
230
231extern int wiidebug_init(struct wiimote_data *wdata);
232extern void wiidebug_deinit(struct wiimote_data *wdata);
233
234#else
235
236static inline int wiidebug_init(void *u) { return 0; }
237static inline void wiidebug_deinit(void *u) { }
238
239#endif
240
David Herrmann7e274402011-11-17 14:11:59 +0100241/* requires the state.lock spinlock to be held */
242static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
243 __u32 opt)
244{
245 return wdata->state.cmd == cmd && wdata->state.opt == opt;
246}
247
248/* requires the state.lock spinlock to be held */
249static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
250{
251 wdata->state.cmd = WIIPROTO_REQ_NULL;
252 complete(&wdata->state.ready);
253}
254
David Herrmannd758b1f02013-05-05 23:12:50 +0200255/* requires the state.lock spinlock to be held */
256static inline void wiimote_cmd_abort(struct wiimote_data *wdata)
257{
258 /* Abort synchronous request by waking up the sleeping caller. But
259 * reset the state.cmd field to an invalid value so no further event
260 * handlers will work with it. */
261 wdata->state.cmd = WIIPROTO_REQ_MAX;
262 complete(&wdata->state.ready);
263}
264
David Herrmann7e274402011-11-17 14:11:59 +0100265static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
266{
267 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
268}
269
David Herrmannc57ff762013-05-05 23:12:48 +0200270static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata)
271{
272 mutex_lock(&wdata->state.sync);
273}
274
David Herrmann7e274402011-11-17 14:11:59 +0100275/* requires the state.lock spinlock to be held */
276static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
277 __u32 opt)
278{
279 INIT_COMPLETION(wdata->state.ready);
280 wdata->state.cmd = cmd;
281 wdata->state.opt = opt;
282}
283
284static inline void wiimote_cmd_release(struct wiimote_data *wdata)
285{
286 mutex_unlock(&wdata->state.sync);
287}
288
289static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
290{
291 int ret;
292
David Herrmannd758b1f02013-05-05 23:12:50 +0200293 /* The completion acts as implicit memory barrier so we can safely
294 * assume that state.cmd is set on success/failure and isn't accessed
295 * by any other thread, anymore. */
296
David Herrmann7e274402011-11-17 14:11:59 +0100297 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
298 if (ret < 0)
299 return -ERESTARTSYS;
300 else if (ret == 0)
301 return -EIO;
David Herrmannd758b1f02013-05-05 23:12:50 +0200302 else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
303 return -EIO;
David Herrmann7e274402011-11-17 14:11:59 +0100304 else
305 return 0;
306}
307
David Herrmannc57ff762013-05-05 23:12:48 +0200308static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata)
309{
310 unsigned long ret;
311
David Herrmannd758b1f02013-05-05 23:12:50 +0200312 /* no locking needed; see wiimote_cmd_wait() */
David Herrmannc57ff762013-05-05 23:12:48 +0200313 ret = wait_for_completion_timeout(&wdata->state.ready, HZ);
314 if (!ret)
315 return -EIO;
David Herrmannd758b1f02013-05-05 23:12:50 +0200316 else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
317 return -EIO;
David Herrmannc57ff762013-05-05 23:12:48 +0200318 else
319 return 0;
320}
321
David Herrmann7e274402011-11-17 14:11:59 +0100322#endif