blob: 2700d47dea3d818e7585d36bdecf2eee49267a1c [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 */
38#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
39 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
40#define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
41 WIIPROTO_FLAG_IR_FULL)
42
43/* return flag for led \num */
44#define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
45
46struct wiimote_buf {
47 __u8 data[HID_MAX_BUFFER_SIZE];
48 size_t size;
49};
50
David Herrmann13938532013-05-05 23:12:46 +020051struct wiimote_queue {
52 spinlock_t lock;
53 struct work_struct worker;
54 __u8 head;
55 __u8 tail;
56 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
57};
58
David Herrmann7e274402011-11-17 14:11:59 +010059struct wiimote_state {
60 spinlock_t lock;
61 __u8 flags;
62 __u8 accel_split[2];
David Herrmann43d782a2011-11-17 14:12:12 +010063 __u8 drm;
David Herrmann7e274402011-11-17 14:11:59 +010064
65 /* synchronous cmd requests */
66 struct mutex sync;
67 struct completion ready;
68 int cmd;
69 __u32 opt;
70
71 /* results of synchronous requests */
72 __u8 cmd_battery;
73 __u8 cmd_err;
David Herrmannfad8c0e2011-11-17 14:12:00 +010074 __u8 *cmd_read_buf;
75 __u8 cmd_read_size;
David Herrmann7e274402011-11-17 14:11:59 +010076};
77
78struct wiimote_data {
79 struct hid_device *hdev;
80 struct input_dev *input;
81 struct led_classdev *leds[4];
82 struct input_dev *accel;
83 struct input_dev *ir;
84 struct power_supply battery;
David Herrmanncb992212011-11-17 14:12:01 +010085 struct wiimote_ext *ext;
David Herrmann43e5e7c2011-11-17 14:12:10 +010086 struct wiimote_debug *debug;
David Herrmann7e274402011-11-17 14:11:59 +010087
David Herrmann13938532013-05-05 23:12:46 +020088 struct wiimote_queue queue;
David Herrmann7e274402011-11-17 14:11:59 +010089 struct wiimote_state state;
90};
91
92enum wiiproto_reqs {
93 WIIPROTO_REQ_NULL = 0x0,
94 WIIPROTO_REQ_RUMBLE = 0x10,
95 WIIPROTO_REQ_LED = 0x11,
96 WIIPROTO_REQ_DRM = 0x12,
97 WIIPROTO_REQ_IR1 = 0x13,
98 WIIPROTO_REQ_SREQ = 0x15,
99 WIIPROTO_REQ_WMEM = 0x16,
100 WIIPROTO_REQ_RMEM = 0x17,
101 WIIPROTO_REQ_IR2 = 0x1a,
102 WIIPROTO_REQ_STATUS = 0x20,
103 WIIPROTO_REQ_DATA = 0x21,
104 WIIPROTO_REQ_RETURN = 0x22,
105 WIIPROTO_REQ_DRM_K = 0x30,
106 WIIPROTO_REQ_DRM_KA = 0x31,
107 WIIPROTO_REQ_DRM_KE = 0x32,
108 WIIPROTO_REQ_DRM_KAI = 0x33,
109 WIIPROTO_REQ_DRM_KEE = 0x34,
110 WIIPROTO_REQ_DRM_KAE = 0x35,
111 WIIPROTO_REQ_DRM_KIE = 0x36,
112 WIIPROTO_REQ_DRM_KAIE = 0x37,
113 WIIPROTO_REQ_DRM_E = 0x3d,
114 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
115 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
David Herrmann43d782a2011-11-17 14:12:12 +0100116 WIIPROTO_REQ_MAX
David Herrmann7e274402011-11-17 14:11:59 +0100117};
118
119#define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
120 dev))
121
122extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
123extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
124 const __u8 *wmem, __u8 size);
David Herrmannfad8c0e2011-11-17 14:12:00 +0100125extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
126 __u8 *rmem, __u8 size);
David Herrmann7e274402011-11-17 14:11:59 +0100127
David Herrmann1d3452c2011-11-17 14:12:11 +0100128#define wiiproto_req_rreg(wdata, os, sz) \
129 wiiproto_req_rmem((wdata), false, (os), (sz))
130#define wiiproto_req_reeprom(wdata, os, sz) \
131 wiiproto_req_rmem((wdata), true, (os), (sz))
132extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
133 __u32 offset, __u16 size);
134
David Herrmanncb992212011-11-17 14:12:01 +0100135#ifdef CONFIG_HID_WIIMOTE_EXT
136
137extern int wiiext_init(struct wiimote_data *wdata);
138extern void wiiext_deinit(struct wiimote_data *wdata);
139extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
140extern bool wiiext_active(struct wiimote_data *wdata);
David Herrmann0b6815d2011-11-17 14:12:06 +0100141extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
David Herrmanncb992212011-11-17 14:12:01 +0100142
143#else
144
145static inline int wiiext_init(void *u) { return 0; }
146static inline void wiiext_deinit(void *u) { }
147static inline void wiiext_event(void *u, bool p) { }
148static inline bool wiiext_active(void *u) { return false; }
David Herrmann0b6815d2011-11-17 14:12:06 +0100149static inline void wiiext_handle(void *u, const __u8 *p) { }
David Herrmanncb992212011-11-17 14:12:01 +0100150
151#endif
152
David Herrmann43e5e7c2011-11-17 14:12:10 +0100153#ifdef CONFIG_DEBUG_FS
154
155extern int wiidebug_init(struct wiimote_data *wdata);
156extern void wiidebug_deinit(struct wiimote_data *wdata);
157
158#else
159
160static inline int wiidebug_init(void *u) { return 0; }
161static inline void wiidebug_deinit(void *u) { }
162
163#endif
164
David Herrmann7e274402011-11-17 14:11:59 +0100165/* requires the state.lock spinlock to be held */
166static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
167 __u32 opt)
168{
169 return wdata->state.cmd == cmd && wdata->state.opt == opt;
170}
171
172/* requires the state.lock spinlock to be held */
173static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
174{
175 wdata->state.cmd = WIIPROTO_REQ_NULL;
176 complete(&wdata->state.ready);
177}
178
179static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
180{
181 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
182}
183
184/* requires the state.lock spinlock to be held */
185static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
186 __u32 opt)
187{
188 INIT_COMPLETION(wdata->state.ready);
189 wdata->state.cmd = cmd;
190 wdata->state.opt = opt;
191}
192
193static inline void wiimote_cmd_release(struct wiimote_data *wdata)
194{
195 mutex_unlock(&wdata->state.sync);
196}
197
198static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
199{
200 int ret;
201
202 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
203 if (ret < 0)
204 return -ERESTARTSYS;
205 else if (ret == 0)
206 return -EIO;
207 else
208 return 0;
209}
210
211#endif