aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Herrmann <dh.herrmann@googlemail.com>2011-11-17 08:11:59 -0500
committerJiri Kosina <jkosina@suse.cz>2011-11-22 17:07:53 -0500
commit7e274400629fbf8eab294fef39c3efde463966a9 (patch)
treefdbf3e1e3eece94032e809054fd258c1363b47f3
parent192a1acfbd600fea8a596b7d92572b70131b7738 (diff)
HID: wiimote: Move common symbols into header
Wiimote extension and sound support need access to several symbols so move them into a new header. Signed-off-by: David Herrmann <dh.herrmann@googlemail.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
-rw-r--r--drivers/hid/hid-wiimote-core.c134
-rw-r--r--drivers/hid/hid-wiimote.h163
2 files changed, 166 insertions, 131 deletions
diff --git a/drivers/hid/hid-wiimote-core.c b/drivers/hid/hid-wiimote-core.c
index 76739c07fa3c..c955d500c81f 100644
--- a/drivers/hid/hid-wiimote-core.c
+++ b/drivers/hid/hid-wiimote-core.c
@@ -20,91 +20,9 @@
20#include <linux/power_supply.h> 20#include <linux/power_supply.h>
21#include <linux/spinlock.h> 21#include <linux/spinlock.h>
22#include "hid-ids.h" 22#include "hid-ids.h"
23#include "hid-wiimote.h"
23 24
24#define WIIMOTE_VERSION "0.2" 25#define WIIMOTE_VERSION "0.2"
25#define WIIMOTE_NAME "Nintendo Wii Remote"
26#define WIIMOTE_BUFSIZE 32
27
28struct wiimote_buf {
29 __u8 data[HID_MAX_BUFFER_SIZE];
30 size_t size;
31};
32
33struct wiimote_state {
34 spinlock_t lock;
35 __u8 flags;
36 __u8 accel_split[2];
37
38 /* synchronous cmd requests */
39 struct mutex sync;
40 struct completion ready;
41 int cmd;
42 __u32 opt;
43
44 /* results of synchronous requests */
45 __u8 cmd_battery;
46 __u8 cmd_err;
47};
48
49struct wiimote_data {
50 struct hid_device *hdev;
51 struct input_dev *input;
52 struct led_classdev *leds[4];
53 struct input_dev *accel;
54 struct input_dev *ir;
55 struct power_supply battery;
56
57 spinlock_t qlock;
58 __u8 head;
59 __u8 tail;
60 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
61 struct work_struct worker;
62
63 struct wiimote_state state;
64};
65
66#define WIIPROTO_FLAG_LED1 0x01
67#define WIIPROTO_FLAG_LED2 0x02
68#define WIIPROTO_FLAG_LED3 0x04
69#define WIIPROTO_FLAG_LED4 0x08
70#define WIIPROTO_FLAG_RUMBLE 0x10
71#define WIIPROTO_FLAG_ACCEL 0x20
72#define WIIPROTO_FLAG_IR_BASIC 0x40
73#define WIIPROTO_FLAG_IR_EXT 0x80
74#define WIIPROTO_FLAG_IR_FULL 0xc0 /* IR_BASIC | IR_EXT */
75#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
76 WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
77#define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
78 WIIPROTO_FLAG_IR_FULL)
79
80/* return flag for led \num */
81#define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
82
83enum wiiproto_reqs {
84 WIIPROTO_REQ_NULL = 0x0,
85 WIIPROTO_REQ_RUMBLE = 0x10,
86 WIIPROTO_REQ_LED = 0x11,
87 WIIPROTO_REQ_DRM = 0x12,
88 WIIPROTO_REQ_IR1 = 0x13,
89 WIIPROTO_REQ_SREQ = 0x15,
90 WIIPROTO_REQ_WMEM = 0x16,
91 WIIPROTO_REQ_RMEM = 0x17,
92 WIIPROTO_REQ_IR2 = 0x1a,
93 WIIPROTO_REQ_STATUS = 0x20,
94 WIIPROTO_REQ_DATA = 0x21,
95 WIIPROTO_REQ_RETURN = 0x22,
96 WIIPROTO_REQ_DRM_K = 0x30,
97 WIIPROTO_REQ_DRM_KA = 0x31,
98 WIIPROTO_REQ_DRM_KE = 0x32,
99 WIIPROTO_REQ_DRM_KAI = 0x33,
100 WIIPROTO_REQ_DRM_KEE = 0x34,
101 WIIPROTO_REQ_DRM_KAE = 0x35,
102 WIIPROTO_REQ_DRM_KIE = 0x36,
103 WIIPROTO_REQ_DRM_KAIE = 0x37,
104 WIIPROTO_REQ_DRM_E = 0x3d,
105 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
106 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
107};
108 26
109enum wiiproto_keys { 27enum wiiproto_keys {
110 WIIPROTO_KEY_LEFT, 28 WIIPROTO_KEY_LEFT,
@@ -139,52 +57,6 @@ static enum power_supply_property wiimote_battery_props[] = {
139 POWER_SUPPLY_PROP_CAPACITY 57 POWER_SUPPLY_PROP_CAPACITY
140}; 58};
141 59
142/* requires the state.lock spinlock to be held */
143static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
144 __u32 opt)
145{
146 return wdata->state.cmd == cmd && wdata->state.opt == opt;
147}
148
149/* requires the state.lock spinlock to be held */
150static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
151{
152 wdata->state.cmd = WIIPROTO_REQ_NULL;
153 complete(&wdata->state.ready);
154}
155
156static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
157{
158 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
159}
160
161/* requires the state.lock spinlock to be held */
162static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
163 __u32 opt)
164{
165 INIT_COMPLETION(wdata->state.ready);
166 wdata->state.cmd = cmd;
167 wdata->state.opt = opt;
168}
169
170static inline void wiimote_cmd_release(struct wiimote_data *wdata)
171{
172 mutex_unlock(&wdata->state.sync);
173}
174
175static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
176{
177 int ret;
178
179 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
180 if (ret < 0)
181 return -ERESTARTSYS;
182 else if (ret == 0)
183 return -EIO;
184 else
185 return 0;
186}
187
188static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, 60static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
189 size_t count) 61 size_t count)
190{ 62{
@@ -347,7 +219,7 @@ static __u8 select_drm(struct wiimote_data *wdata)
347 } 219 }
348} 220}
349 221
350static void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) 222void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
351{ 223{
352 __u8 cmd[3]; 224 __u8 cmd[3];
353 225
@@ -441,7 +313,7 @@ static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
441} 313}
442 314
443/* requries the cmd-mutex to be held */ 315/* requries the cmd-mutex to be held */
444static int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset, 316int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
445 const __u8 *wmem, __u8 size) 317 const __u8 *wmem, __u8 size)
446{ 318{
447 unsigned long flags; 319 unsigned long flags;
diff --git a/drivers/hid/hid-wiimote.h b/drivers/hid/hid-wiimote.h
new file mode 100644
index 000000000000..d78f79c08901
--- /dev/null
+++ b/drivers/hid/hid-wiimote.h
@@ -0,0 +1,163 @@
1#ifndef __HID_WIIMOTE_H
2#define __HID_WIIMOTE_H
3
4/*
5 * HID driver for Nintendo Wiimote devices
6 * Copyright (c) 2011 David Herrmann
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_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
51struct wiimote_state {
52 spinlock_t lock;
53 __u8 flags;
54 __u8 accel_split[2];
55
56 /* synchronous cmd requests */
57 struct mutex sync;
58 struct completion ready;
59 int cmd;
60 __u32 opt;
61
62 /* results of synchronous requests */
63 __u8 cmd_battery;
64 __u8 cmd_err;
65};
66
67struct wiimote_data {
68 struct hid_device *hdev;
69 struct input_dev *input;
70 struct led_classdev *leds[4];
71 struct input_dev *accel;
72 struct input_dev *ir;
73 struct power_supply battery;
74
75 spinlock_t qlock;
76 __u8 head;
77 __u8 tail;
78 struct wiimote_buf outq[WIIMOTE_BUFSIZE];
79 struct work_struct worker;
80
81 struct wiimote_state state;
82};
83
84enum wiiproto_reqs {
85 WIIPROTO_REQ_NULL = 0x0,
86 WIIPROTO_REQ_RUMBLE = 0x10,
87 WIIPROTO_REQ_LED = 0x11,
88 WIIPROTO_REQ_DRM = 0x12,
89 WIIPROTO_REQ_IR1 = 0x13,
90 WIIPROTO_REQ_SREQ = 0x15,
91 WIIPROTO_REQ_WMEM = 0x16,
92 WIIPROTO_REQ_RMEM = 0x17,
93 WIIPROTO_REQ_IR2 = 0x1a,
94 WIIPROTO_REQ_STATUS = 0x20,
95 WIIPROTO_REQ_DATA = 0x21,
96 WIIPROTO_REQ_RETURN = 0x22,
97 WIIPROTO_REQ_DRM_K = 0x30,
98 WIIPROTO_REQ_DRM_KA = 0x31,
99 WIIPROTO_REQ_DRM_KE = 0x32,
100 WIIPROTO_REQ_DRM_KAI = 0x33,
101 WIIPROTO_REQ_DRM_KEE = 0x34,
102 WIIPROTO_REQ_DRM_KAE = 0x35,
103 WIIPROTO_REQ_DRM_KIE = 0x36,
104 WIIPROTO_REQ_DRM_KAIE = 0x37,
105 WIIPROTO_REQ_DRM_E = 0x3d,
106 WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
107 WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
108};
109
110#define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
111 dev))
112
113extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
114extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
115 const __u8 *wmem, __u8 size);
116
117/* requires the state.lock spinlock to be held */
118static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
119 __u32 opt)
120{
121 return wdata->state.cmd == cmd && wdata->state.opt == opt;
122}
123
124/* requires the state.lock spinlock to be held */
125static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
126{
127 wdata->state.cmd = WIIPROTO_REQ_NULL;
128 complete(&wdata->state.ready);
129}
130
131static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
132{
133 return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
134}
135
136/* requires the state.lock spinlock to be held */
137static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
138 __u32 opt)
139{
140 INIT_COMPLETION(wdata->state.ready);
141 wdata->state.cmd = cmd;
142 wdata->state.opt = opt;
143}
144
145static inline void wiimote_cmd_release(struct wiimote_data *wdata)
146{
147 mutex_unlock(&wdata->state.sync);
148}
149
150static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
151{
152 int ret;
153
154 ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
155 if (ret < 0)
156 return -ERESTARTSYS;
157 else if (ret == 0)
158 return -EIO;
159 else
160 return 0;
161}
162
163#endif