Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 #ifndef __HID_WIIMOTE_H
0003 #define __HID_WIIMOTE_H
0004 
0005 /*
0006  * HID driver for Nintendo Wii / Wii U peripherals
0007  * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
0008  */
0009 
0010 /*
0011  */
0012 
0013 #include <linux/completion.h>
0014 #include <linux/device.h>
0015 #include <linux/hid.h>
0016 #include <linux/input.h>
0017 #include <linux/leds.h>
0018 #include <linux/module.h>
0019 #include <linux/mutex.h>
0020 #include <linux/power_supply.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/timer.h>
0023 
0024 #define WIIMOTE_NAME "Nintendo Wii Remote"
0025 #define WIIMOTE_BUFSIZE 32
0026 
0027 #define WIIPROTO_FLAG_LED1      0x01
0028 #define WIIPROTO_FLAG_LED2      0x02
0029 #define WIIPROTO_FLAG_LED3      0x04
0030 #define WIIPROTO_FLAG_LED4      0x08
0031 #define WIIPROTO_FLAG_RUMBLE        0x10
0032 #define WIIPROTO_FLAG_ACCEL     0x20
0033 #define WIIPROTO_FLAG_IR_BASIC      0x40
0034 #define WIIPROTO_FLAG_IR_EXT        0x80
0035 #define WIIPROTO_FLAG_IR_FULL       0xc0 /* IR_BASIC | IR_EXT */
0036 #define WIIPROTO_FLAG_EXT_PLUGGED   0x0100
0037 #define WIIPROTO_FLAG_EXT_USED      0x0200
0038 #define WIIPROTO_FLAG_EXT_ACTIVE    0x0400
0039 #define WIIPROTO_FLAG_MP_PLUGGED    0x0800
0040 #define WIIPROTO_FLAG_MP_USED       0x1000
0041 #define WIIPROTO_FLAG_MP_ACTIVE     0x2000
0042 #define WIIPROTO_FLAG_EXITING       0x4000
0043 #define WIIPROTO_FLAG_DRM_LOCKED    0x8000
0044 #define WIIPROTO_FLAG_BUILTIN_MP    0x010000
0045 #define WIIPROTO_FLAG_NO_MP     0x020000
0046 #define WIIPROTO_FLAG_PRO_CALIB_DONE    0x040000
0047 
0048 #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
0049                     WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
0050 #define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \
0051                             WIIPROTO_FLAG_IR_FULL)
0052 
0053 /* return flag for led \num */
0054 #define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
0055 
0056 enum wiiproto_keys {
0057     WIIPROTO_KEY_LEFT,
0058     WIIPROTO_KEY_RIGHT,
0059     WIIPROTO_KEY_UP,
0060     WIIPROTO_KEY_DOWN,
0061     WIIPROTO_KEY_PLUS,
0062     WIIPROTO_KEY_MINUS,
0063     WIIPROTO_KEY_ONE,
0064     WIIPROTO_KEY_TWO,
0065     WIIPROTO_KEY_A,
0066     WIIPROTO_KEY_B,
0067     WIIPROTO_KEY_HOME,
0068     WIIPROTO_KEY_COUNT
0069 };
0070 
0071 enum wiimote_devtype {
0072     WIIMOTE_DEV_PENDING,
0073     WIIMOTE_DEV_UNKNOWN,
0074     WIIMOTE_DEV_GENERIC,
0075     WIIMOTE_DEV_GEN10,
0076     WIIMOTE_DEV_GEN20,
0077     WIIMOTE_DEV_BALANCE_BOARD,
0078     WIIMOTE_DEV_PRO_CONTROLLER,
0079     WIIMOTE_DEV_NUM,
0080 };
0081 
0082 enum wiimote_exttype {
0083     WIIMOTE_EXT_NONE,
0084     WIIMOTE_EXT_UNKNOWN,
0085     WIIMOTE_EXT_NUNCHUK,
0086     WIIMOTE_EXT_CLASSIC_CONTROLLER,
0087     WIIMOTE_EXT_BALANCE_BOARD,
0088     WIIMOTE_EXT_PRO_CONTROLLER,
0089     WIIMOTE_EXT_DRUMS,
0090     WIIMOTE_EXT_GUITAR,
0091     WIIMOTE_EXT_NUM,
0092 };
0093 
0094 enum wiimote_mptype {
0095     WIIMOTE_MP_NONE,
0096     WIIMOTE_MP_UNKNOWN,
0097     WIIMOTE_MP_SINGLE,
0098     WIIMOTE_MP_PASSTHROUGH_NUNCHUK,
0099     WIIMOTE_MP_PASSTHROUGH_CLASSIC,
0100 };
0101 
0102 struct wiimote_buf {
0103     __u8 data[HID_MAX_BUFFER_SIZE];
0104     size_t size;
0105 };
0106 
0107 struct wiimote_queue {
0108     spinlock_t lock;
0109     struct work_struct worker;
0110     __u8 head;
0111     __u8 tail;
0112     struct wiimote_buf outq[WIIMOTE_BUFSIZE];
0113 };
0114 
0115 struct wiimote_state {
0116     spinlock_t lock;
0117     __u32 flags;
0118     __u8 accel_split[2];
0119     __u8 drm;
0120     __u8 devtype;
0121     __u8 exttype;
0122     __u8 mp;
0123 
0124     /* synchronous cmd requests */
0125     struct mutex sync;
0126     struct completion ready;
0127     int cmd;
0128     __u32 opt;
0129 
0130     /* results of synchronous requests */
0131     __u8 cmd_battery;
0132     __u8 cmd_err;
0133     __u8 *cmd_read_buf;
0134     __u8 cmd_read_size;
0135 
0136     /* calibration/cache data */
0137     __u16 calib_bboard[4][3];
0138     __s16 calib_pro_sticks[4];
0139     __u8 pressure_drums[7];
0140     __u8 cache_rumble;
0141 };
0142 
0143 struct wiimote_data {
0144     struct hid_device *hdev;
0145     struct input_dev *input;
0146     struct work_struct rumble_worker;
0147     struct led_classdev *leds[4];
0148     struct input_dev *accel;
0149     struct input_dev *ir;
0150     struct power_supply *battery;
0151     struct power_supply_desc battery_desc;
0152     struct input_dev *mp;
0153     struct timer_list timer;
0154     struct wiimote_debug *debug;
0155 
0156     union {
0157         struct input_dev *input;
0158     } extension;
0159 
0160     struct wiimote_queue queue;
0161     struct wiimote_state state;
0162     struct work_struct init_worker;
0163 };
0164 
0165 extern bool wiimote_dpad_as_analog;
0166 
0167 /* wiimote modules */
0168 
0169 enum wiimod_module {
0170     WIIMOD_KEYS,
0171     WIIMOD_RUMBLE,
0172     WIIMOD_BATTERY,
0173     WIIMOD_LED1,
0174     WIIMOD_LED2,
0175     WIIMOD_LED3,
0176     WIIMOD_LED4,
0177     WIIMOD_ACCEL,
0178     WIIMOD_IR,
0179     WIIMOD_BUILTIN_MP,
0180     WIIMOD_NO_MP,
0181     WIIMOD_NUM,
0182     WIIMOD_NULL = WIIMOD_NUM,
0183 };
0184 
0185 #define WIIMOD_FLAG_INPUT       0x0001
0186 #define WIIMOD_FLAG_EXT8        0x0002
0187 #define WIIMOD_FLAG_EXT16       0x0004
0188 
0189 struct wiimod_ops {
0190     __u16 flags;
0191     unsigned long arg;
0192     int (*probe) (const struct wiimod_ops *ops,
0193               struct wiimote_data *wdata);
0194     void (*remove) (const struct wiimod_ops *ops,
0195             struct wiimote_data *wdata);
0196 
0197     void (*in_keys) (struct wiimote_data *wdata, const __u8 *keys);
0198     void (*in_accel) (struct wiimote_data *wdata, const __u8 *accel);
0199     void (*in_ir) (struct wiimote_data *wdata, const __u8 *ir, bool packed,
0200                unsigned int id);
0201     void (*in_mp) (struct wiimote_data *wdata, const __u8 *mp);
0202     void (*in_ext) (struct wiimote_data *wdata, const __u8 *ext);
0203 };
0204 
0205 extern const struct wiimod_ops *wiimod_table[WIIMOD_NUM];
0206 extern const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM];
0207 extern const struct wiimod_ops wiimod_mp;
0208 
0209 /* wiimote requests */
0210 
0211 enum wiiproto_reqs {
0212     WIIPROTO_REQ_NULL = 0x0,
0213     WIIPROTO_REQ_RUMBLE = 0x10,
0214     WIIPROTO_REQ_LED = 0x11,
0215     WIIPROTO_REQ_DRM = 0x12,
0216     WIIPROTO_REQ_IR1 = 0x13,
0217     WIIPROTO_REQ_SREQ = 0x15,
0218     WIIPROTO_REQ_WMEM = 0x16,
0219     WIIPROTO_REQ_RMEM = 0x17,
0220     WIIPROTO_REQ_IR2 = 0x1a,
0221     WIIPROTO_REQ_STATUS = 0x20,
0222     WIIPROTO_REQ_DATA = 0x21,
0223     WIIPROTO_REQ_RETURN = 0x22,
0224 
0225     /* DRM_K: BB*2 */
0226     WIIPROTO_REQ_DRM_K = 0x30,
0227 
0228     /* DRM_KA: BB*2 AA*3 */
0229     WIIPROTO_REQ_DRM_KA = 0x31,
0230 
0231     /* DRM_KE: BB*2 EE*8 */
0232     WIIPROTO_REQ_DRM_KE = 0x32,
0233 
0234     /* DRM_KAI: BB*2 AA*3 II*12 */
0235     WIIPROTO_REQ_DRM_KAI = 0x33,
0236 
0237     /* DRM_KEE: BB*2 EE*19 */
0238     WIIPROTO_REQ_DRM_KEE = 0x34,
0239 
0240     /* DRM_KAE: BB*2 AA*3 EE*16 */
0241     WIIPROTO_REQ_DRM_KAE = 0x35,
0242 
0243     /* DRM_KIE: BB*2 II*10 EE*9 */
0244     WIIPROTO_REQ_DRM_KIE = 0x36,
0245 
0246     /* DRM_KAIE: BB*2 AA*3 II*10 EE*6 */
0247     WIIPROTO_REQ_DRM_KAIE = 0x37,
0248 
0249     /* DRM_E: EE*21 */
0250     WIIPROTO_REQ_DRM_E = 0x3d,
0251 
0252     /* DRM_SKAI1: BB*2 AA*1 II*18 */
0253     WIIPROTO_REQ_DRM_SKAI1 = 0x3e,
0254 
0255     /* DRM_SKAI2: BB*2 AA*1 II*18 */
0256     WIIPROTO_REQ_DRM_SKAI2 = 0x3f,
0257 
0258     WIIPROTO_REQ_MAX
0259 };
0260 
0261 #define dev_to_wii(pdev) hid_get_drvdata(to_hid_device(pdev))
0262 
0263 void __wiimote_schedule(struct wiimote_data *wdata);
0264 
0265 extern void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm);
0266 extern void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble);
0267 extern void wiiproto_req_leds(struct wiimote_data *wdata, int leds);
0268 extern void wiiproto_req_status(struct wiimote_data *wdata);
0269 extern void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel);
0270 extern void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags);
0271 extern void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags);
0272 extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
0273                         const __u8 *wmem, __u8 size);
0274 extern ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset,
0275                             __u8 *rmem, __u8 size);
0276 
0277 #define wiiproto_req_rreg(wdata, os, sz) \
0278                 wiiproto_req_rmem((wdata), false, (os), (sz))
0279 #define wiiproto_req_reeprom(wdata, os, sz) \
0280                 wiiproto_req_rmem((wdata), true, (os), (sz))
0281 extern void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom,
0282                         __u32 offset, __u16 size);
0283 
0284 #ifdef CONFIG_DEBUG_FS
0285 
0286 extern int wiidebug_init(struct wiimote_data *wdata);
0287 extern void wiidebug_deinit(struct wiimote_data *wdata);
0288 
0289 #else
0290 
0291 static inline int wiidebug_init(void *u) { return 0; }
0292 static inline void wiidebug_deinit(void *u) { }
0293 
0294 #endif
0295 
0296 /* requires the state.lock spinlock to be held */
0297 static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
0298                                 __u32 opt)
0299 {
0300     return wdata->state.cmd == cmd && wdata->state.opt == opt;
0301 }
0302 
0303 /* requires the state.lock spinlock to be held */
0304 static inline void wiimote_cmd_complete(struct wiimote_data *wdata)
0305 {
0306     wdata->state.cmd = WIIPROTO_REQ_NULL;
0307     complete(&wdata->state.ready);
0308 }
0309 
0310 /* requires the state.lock spinlock to be held */
0311 static inline void wiimote_cmd_abort(struct wiimote_data *wdata)
0312 {
0313     /* Abort synchronous request by waking up the sleeping caller. But
0314      * reset the state.cmd field to an invalid value so no further event
0315      * handlers will work with it. */
0316     wdata->state.cmd = WIIPROTO_REQ_MAX;
0317     complete(&wdata->state.ready);
0318 }
0319 
0320 static inline int wiimote_cmd_acquire(struct wiimote_data *wdata)
0321 {
0322     return mutex_lock_interruptible(&wdata->state.sync) ? -ERESTARTSYS : 0;
0323 }
0324 
0325 static inline void wiimote_cmd_acquire_noint(struct wiimote_data *wdata)
0326 {
0327     mutex_lock(&wdata->state.sync);
0328 }
0329 
0330 /* requires the state.lock spinlock to be held */
0331 static inline void wiimote_cmd_set(struct wiimote_data *wdata, int cmd,
0332                                 __u32 opt)
0333 {
0334     reinit_completion(&wdata->state.ready);
0335     wdata->state.cmd = cmd;
0336     wdata->state.opt = opt;
0337 }
0338 
0339 static inline void wiimote_cmd_release(struct wiimote_data *wdata)
0340 {
0341     mutex_unlock(&wdata->state.sync);
0342 }
0343 
0344 static inline int wiimote_cmd_wait(struct wiimote_data *wdata)
0345 {
0346     int ret;
0347 
0348     /* The completion acts as implicit memory barrier so we can safely
0349      * assume that state.cmd is set on success/failure and isn't accessed
0350      * by any other thread, anymore. */
0351 
0352     ret = wait_for_completion_interruptible_timeout(&wdata->state.ready, HZ);
0353     if (ret < 0)
0354         return -ERESTARTSYS;
0355     else if (ret == 0)
0356         return -EIO;
0357     else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
0358         return -EIO;
0359     else
0360         return 0;
0361 }
0362 
0363 static inline int wiimote_cmd_wait_noint(struct wiimote_data *wdata)
0364 {
0365     unsigned long ret;
0366 
0367     /* no locking needed; see wiimote_cmd_wait() */
0368     ret = wait_for_completion_timeout(&wdata->state.ready, HZ);
0369     if (!ret)
0370         return -EIO;
0371     else if (wdata->state.cmd != WIIPROTO_REQ_NULL)
0372         return -EIO;
0373     else
0374         return 0;
0375 }
0376 
0377 #endif