Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hid-wiimote.h
Go to the documentation of this file.
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 
46 struct wiimote_buf {
48  size_t size;
49 };
50 
51 struct wiimote_state {
56 
57  /* synchronous cmd requests */
58  struct mutex sync;
59  struct completion ready;
60  int cmd;
62 
63  /* results of synchronous requests */
68 };
69 
70 struct wiimote_data {
71  struct hid_device *hdev;
72  struct input_dev *input;
73  struct led_classdev *leds[4];
74  struct input_dev *accel;
75  struct input_dev *ir;
77  struct wiimote_ext *ext;
79 
85 
87 };
88 
114 };
115 
116 #define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
117  dev))
118 
119 extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
120 extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
121  const __u8 *wmem, __u8 size);
122 extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
123  __u8 *rmem, __u8 size);
124 
125 #define wiiproto_req_rreg(wdata, os, sz) \
126  wiiproto_req_rmem((wdata), false, (os), (sz))
127 #define wiiproto_req_reeprom(wdata, os, sz) \
128  wiiproto_req_rmem((wdata), true, (os), (sz))
129 extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
131 
132 #ifdef CONFIG_HID_WIIMOTE_EXT
133 
134 extern int wiiext_init(struct wiimote_data *wdata);
135 extern void wiiext_deinit(struct wiimote_data *wdata);
136 extern void wiiext_event(struct wiimote_data *wdata, bool plugged);
137 extern bool wiiext_active(struct wiimote_data *wdata);
138 extern void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload);
139 
140 #else
141 
142 static inline int wiiext_init(void *u) { return 0; }
143 static inline void wiiext_deinit(void *u) { }
144 static inline void wiiext_event(void *u, bool p) { }
145 static inline bool wiiext_active(void *u) { return false; }
146 static inline void wiiext_handle(void *u, const __u8 *p) { }
147 
148 #endif
149 
150 #ifdef CONFIG_DEBUG_FS
151 
152 extern int wiidebug_init(struct wiimote_data *wdata);
153 extern void wiidebug_deinit(struct wiimote_data *wdata);
154 
155 #else
156 
157 static inline int wiidebug_init(void *u) { return 0; }
158 static inline void wiidebug_deinit(void *u) { }
159 
160 #endif
161 
162 /* requires the state.lock spinlock to be held */
163 static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
164  __u32 opt)
165 {
166  return wdata->state.cmd == cmd && wdata->state.opt == opt;
167 }
168 
169 /* requires the state.lock spinlock to be held */
170 static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
171 {
172  wdata->state.cmd = WIIPROTO_REQ_NULL;
173  complete(&wdata->state.ready);
174 }
175 
176 static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
177 {
178  return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
179 }
180 
181 /* requires the state.lock spinlock to be held */
182 static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
183  __u32 opt)
184 {
185  INIT_COMPLETION(wdata->state.ready);
186  wdata->state.cmd = cmd;
187  wdata->state.opt = opt;
188 }
189 
190 static inline void wiimote_cmd_release(struct wiimote_data *wdata)
191 {
192  mutex_unlock(&wdata->state.sync);
193 }
194 
195 static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
196 {
197  int ret;
198 
199  ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
200  if (ret < 0)
201  return -ERESTARTSYS;
202  else if (ret == 0)
203  return -EIO;
204  else
205  return 0;
206 }
207 
208 #endif