93c48fbef7a589900b8ed19c46990b839061801b
1 #ifndef __HID_WIIMOTE_H
2 #define __HID_WIIMOTE_H
5 * HID driver for Nintendo Wii / Wii U peripherals
6 * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
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)
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>
26 #define WIIMOTE_NAME "Nintendo Wii Remote"
27 #define WIIMOTE_BUFSIZE 32
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
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)
45 /* return flag for led \num */
46 #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
63 enum wiimote_devtype
{
72 enum wiimote_exttype
{
79 __u8 data
[HID_MAX_BUFFER_SIZE
];
83 struct wiimote_queue
{
85 struct work_struct worker
;
88 struct wiimote_buf outq
[WIIMOTE_BUFSIZE
];
91 struct wiimote_state
{
98 /* synchronous cmd requests */
100 struct completion ready
;
104 /* results of synchronous requests */
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
;
118 struct wiimote_ext
*ext
;
119 struct wiimote_debug
*debug
;
121 struct wiimote_queue queue
;
122 struct wiimote_state state
;
123 struct work_struct init_worker
;
126 /* wiimote modules */
132 WIIMOD_NULL
= WIIMOD_NUM
,
135 #define WIIMOD_FLAG_INPUT 0x0001
140 int (*probe
) (const struct wiimod_ops
*ops
,
141 struct wiimote_data
*wdata
);
142 void (*remove
) (const struct wiimod_ops
*ops
,
143 struct wiimote_data
*wdata
);
145 void (*in_keys
) (struct wiimote_data
*wdata
, const __u8
*keys
);
146 void (*in_accel
) (struct wiimote_data
*wdata
, const __u8
*accel
);
147 void (*in_ir
) (struct wiimote_data
*wdata
, const __u8
*ir
, bool packed
,
151 extern const struct wiimod_ops
*wiimod_table
[WIIMOD_NUM
];
153 /* wiimote requests */
156 WIIPROTO_REQ_NULL
= 0x0,
157 WIIPROTO_REQ_RUMBLE
= 0x10,
158 WIIPROTO_REQ_LED
= 0x11,
159 WIIPROTO_REQ_DRM
= 0x12,
160 WIIPROTO_REQ_IR1
= 0x13,
161 WIIPROTO_REQ_SREQ
= 0x15,
162 WIIPROTO_REQ_WMEM
= 0x16,
163 WIIPROTO_REQ_RMEM
= 0x17,
164 WIIPROTO_REQ_IR2
= 0x1a,
165 WIIPROTO_REQ_STATUS
= 0x20,
166 WIIPROTO_REQ_DATA
= 0x21,
167 WIIPROTO_REQ_RETURN
= 0x22,
168 WIIPROTO_REQ_DRM_K
= 0x30,
169 WIIPROTO_REQ_DRM_KA
= 0x31,
170 WIIPROTO_REQ_DRM_KE
= 0x32,
171 WIIPROTO_REQ_DRM_KAI
= 0x33,
172 WIIPROTO_REQ_DRM_KEE
= 0x34,
173 WIIPROTO_REQ_DRM_KAE
= 0x35,
174 WIIPROTO_REQ_DRM_KIE
= 0x36,
175 WIIPROTO_REQ_DRM_KAIE
= 0x37,
176 WIIPROTO_REQ_DRM_E
= 0x3d,
177 WIIPROTO_REQ_DRM_SKAI1
= 0x3e,
178 WIIPROTO_REQ_DRM_SKAI2
= 0x3f,
182 #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
185 extern void wiiproto_req_drm(struct wiimote_data
*wdata
, __u8 drm
);
186 extern void wiiproto_req_rumble(struct wiimote_data
*wdata
, __u8 rumble
);
187 extern int wiimote_cmd_write(struct wiimote_data
*wdata
, __u32 offset
,
188 const __u8
*wmem
, __u8 size
);
189 extern ssize_t
wiimote_cmd_read(struct wiimote_data
*wdata
, __u32 offset
,
190 __u8
*rmem
, __u8 size
);
192 #define wiiproto_req_rreg(wdata, os, sz) \
193 wiiproto_req_rmem((wdata), false, (os), (sz))
194 #define wiiproto_req_reeprom(wdata, os, sz) \
195 wiiproto_req_rmem((wdata), true, (os), (sz))
196 extern void wiiproto_req_rmem(struct wiimote_data
*wdata
, bool eeprom
,
197 __u32 offset
, __u16 size
);
199 #ifdef CONFIG_HID_WIIMOTE_EXT
201 extern int wiiext_init(struct wiimote_data
*wdata
);
202 extern void wiiext_deinit(struct wiimote_data
*wdata
);
203 extern void wiiext_event(struct wiimote_data
*wdata
, bool plugged
);
204 extern bool wiiext_active(struct wiimote_data
*wdata
);
205 extern void wiiext_handle(struct wiimote_data
*wdata
, const __u8
*payload
);
209 static inline int wiiext_init(void *u
) { return 0; }
210 static inline void wiiext_deinit(void *u
) { }
211 static inline void wiiext_event(void *u
, bool p
) { }
212 static inline bool wiiext_active(void *u
) { return false; }
213 static inline void wiiext_handle(void *u
, const __u8
*p
) { }
217 #ifdef CONFIG_DEBUG_FS
219 extern int wiidebug_init(struct wiimote_data
*wdata
);
220 extern void wiidebug_deinit(struct wiimote_data
*wdata
);
224 static inline int wiidebug_init(void *u
) { return 0; }
225 static inline void wiidebug_deinit(void *u
) { }
229 /* requires the state.lock spinlock to be held */
230 static inline bool wiimote_cmd_pending(struct wiimote_data
*wdata
, int cmd
,
233 return wdata
->state
.cmd
== cmd
&& wdata
->state
.opt
== opt
;
236 /* requires the state.lock spinlock to be held */
237 static inline void wiimote_cmd_complete(struct wiimote_data
*wdata
)
239 wdata
->state
.cmd
= WIIPROTO_REQ_NULL
;
240 complete(&wdata
->state
.ready
);
243 /* requires the state.lock spinlock to be held */
244 static inline void wiimote_cmd_abort(struct wiimote_data
*wdata
)
246 /* Abort synchronous request by waking up the sleeping caller. But
247 * reset the state.cmd field to an invalid value so no further event
248 * handlers will work with it. */
249 wdata
->state
.cmd
= WIIPROTO_REQ_MAX
;
250 complete(&wdata
->state
.ready
);
253 static inline int wiimote_cmd_acquire(struct wiimote_data
*wdata
)
255 return mutex_lock_interruptible(&wdata
->state
.sync
) ? -ERESTARTSYS
: 0;
258 static inline void wiimote_cmd_acquire_noint(struct wiimote_data
*wdata
)
260 mutex_lock(&wdata
->state
.sync
);
263 /* requires the state.lock spinlock to be held */
264 static inline void wiimote_cmd_set(struct wiimote_data
*wdata
, int cmd
,
267 INIT_COMPLETION(wdata
->state
.ready
);
268 wdata
->state
.cmd
= cmd
;
269 wdata
->state
.opt
= opt
;
272 static inline void wiimote_cmd_release(struct wiimote_data
*wdata
)
274 mutex_unlock(&wdata
->state
.sync
);
277 static inline int wiimote_cmd_wait(struct wiimote_data
*wdata
)
281 /* The completion acts as implicit memory barrier so we can safely
282 * assume that state.cmd is set on success/failure and isn't accessed
283 * by any other thread, anymore. */
285 ret
= wait_for_completion_interruptible_timeout(&wdata
->state
.ready
, HZ
);
290 else if (wdata
->state
.cmd
!= WIIPROTO_REQ_NULL
)
296 static inline int wiimote_cmd_wait_noint(struct wiimote_data
*wdata
)
300 /* no locking needed; see wiimote_cmd_wait() */
301 ret
= wait_for_completion_timeout(&wdata
->state
.ready
, HZ
);
304 else if (wdata
->state
.cmd
!= WIIPROTO_REQ_NULL
)
This page took 0.036484 seconds and 4 git commands to generate.