HID: Wiimote Extension Support v2
Subsystems (2)
v2 cover HID: Wiimote Extension Support v2
Hi
Version 2 of the wiimote extension support removes the module-versioning and adds a comment to the deinitialization function.
Regards
David
David Herrmann (17):
HID: wiimote: Rename driver to allow multiple source files
HID: wiimote: Move common symbols into header
HID: wiimote: Add read-mem helpers
HID: wiimote: Add extension support stub
HID: wiimote: Add extension initializer stubs
HID: wiimote: Add extension initializers
HID: wiimote: Add extension sysfs attribute
HID: wiimote: Register input devices for extensions
HID: wiimote: Add extension handler stubs
HID: wiimote: Parse motion+ data
HID: wiimote: Parse nunchuck data
HID: wiimote: Parse classic controller data
HID: wiimote: Add debugfs support stubs
HID: wiimote: Allow direct eeprom access
HID: wiimote: Allow direct DRM debug access
HID: wiimote: Remove module version number
HID: wiimote: Enable NO_INIT_REPORTS quirk
Documentation/ABI/testing/sysfs-driver-hid-wiimote | 12 +
drivers/hid/Kconfig | 9 +
drivers/hid/Makefile | 8 +
drivers/hid/{hid-wiimote.c => hid-wiimote-core.c} | 246 +++----
drivers/hid/hid-wiimote-debug.c | 227 ++++++
drivers/hid/hid-wiimote-ext.c | 752 ++++++++++++++++++++
drivers/hid/hid-wiimote.h | 208 ++++++
7 files changed, 1324 insertions(+), 138 deletions(-)
rename drivers/hid/{hid-wiimote.c => hid-wiimote-core.c} (89%)
create mode 100644 drivers/hid/hid-wiimote-debug.c
create mode 100644 drivers/hid/hid-wiimote-ext.c
create mode 100644 drivers/hid/hid-wiimote.hSignature · 1 line
1.7.7.3
PATCH v2 1/17 HID: wiimote: Rename driver to allow multiple source files
Extension and sound support for the wiimote are quite complex and will be implemented in separate source files. Hence rename the current driver to "-core" suffix so multiple files can be linked into this module.
---
drivers/hid/Makefile | 2 ++
drivers/hid/{hid-wiimote.c => hid-wiimote-core.c} | 0
2 files changed, 2 insertions(+), 0 deletions(-)
rename drivers/hid/{hid-wiimote.c => hid-wiimote-core.c} (100%)drivers/hid/Makefile +2 -0
@@ -25,6 +25,8 @@ ifdef CONFIG_LOGIWII_FF hid-logitech-y += hid-lg4ff.o endif +hid-wiimote-y := hid-wiimote-core.o + obj-$(CONFIG_HID_A4TECH) += hid-a4tech.o obj-$(CONFIG_HID_ACRUX) += hid-axff.o obj-$(CONFIG_HID_APPLE) += hid-apple.o
drivers/hid/hid-wiimote-core.c renamed from drivers/hid/hid-wiimote.c +0 -0
Signature · 1 line
1.7.7.3
PATCH v2 2/17 HID: wiimote: Move common symbols into header
In reply to David HerrmannWiimote extension and sound support need access to several symbols so move them into a new header.
--- drivers/hid/hid-wiimote-core.c | 134 +-------------------------------- drivers/hid/hid-wiimote.h | 163 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 131 deletions(-) create mode 100644 drivers/hid/hid-wiimote.h
drivers/hid/hid-wiimote-core.c +3 -131
@@ -20,91 +20,9 @@#include <linux/power_supply.h> #include <linux/spinlock.h> #include "hid-ids.h" +#include "hid-wiimote.h" #define WIIMOTE_VERSION "0.2" -#define WIIMOTE_NAME "Nintendo Wii Remote" -#define WIIMOTE_BUFSIZE 32 - -struct wiimote_buf { -__u8 data[HID_MAX_BUFFER_SIZE ]; -size_t size; -}; - -struct wiimote_state { -spinlock_t lock; -__u8 flags; -__u8 accel_split[2 ]; - -/* synchronous cmd requests */ -struct mutex sync; -struct completion ready; -int cmd; -__u32 opt; - -/* results of synchronous requests */ -__u8 cmd_battery; -__u8 cmd_err; -}; - -struct wiimote_data { -struct hid_device *hdev; -struct input_dev *input; -struct led_classdev *leds[4 ]; -struct input_dev *accel; -struct input_dev *ir; -struct power_supply battery; - -spinlock_t qlock; -__u8 head; -__u8 tail; -struct wiimote_buf outq[WIIMOTE_BUFSIZE ]; -struct work_struct worker; - -struct wiimote_state state; -}; - -#define WIIPROTO_FLAG_LED1 0x01 -#define WIIPROTO_FLAG_LED2 0x02 -#define WIIPROTO_FLAG_LED3 0x04 -#define WIIPROTO_FLAG_LED4 0x08 -#define WIIPROTO_FLAG_RUMBLE 0x10 -#define WIIPROTO_FLAG_ACCEL 0x20 -#define WIIPROTO_FLAG_IR_BASIC 0x40 -#define WIIPROTO_FLAG_IR_EXT 0x80 -#define WIIPROTO_FLAG_IR_FULL 0xc0/* IR_BASIC | IR_EXT */ -#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ - WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) -#define WIIPROTO_FLAGS_IR (WIIPROTO_FLAG_IR_BASIC | WIIPROTO_FLAG_IR_EXT | \ - WIIPROTO_FLAG_IR_FULL) - -/* return flag for led \num */ -#define WIIPROTO_FLAG_LED (num) (WIIPROTO_FLAG_LED1 << (num - 1)) - -enum wiiproto_reqs { -WIIPROTO_REQ_NULL =0x0 , -WIIPROTO_REQ_RUMBLE =0x10 , -WIIPROTO_REQ_LED =0x11 , -WIIPROTO_REQ_DRM =0x12 , -WIIPROTO_REQ_IR1 =0x13 , -WIIPROTO_REQ_SREQ =0x15 , -WIIPROTO_REQ_WMEM =0x16 , -WIIPROTO_REQ_RMEM =0x17 , -WIIPROTO_REQ_IR2 =0x1a , -WIIPROTO_REQ_STATUS =0x20 , -WIIPROTO_REQ_DATA =0x21 , -WIIPROTO_REQ_RETURN =0x22 , -WIIPROTO_REQ_DRM_K =0x30 , -WIIPROTO_REQ_DRM_KA =0x31 , -WIIPROTO_REQ_DRM_KE =0x32 , -WIIPROTO_REQ_DRM_KAI =0x33 , -WIIPROTO_REQ_DRM_KEE =0x34 , -WIIPROTO_REQ_DRM_KAE =0x35 , -WIIPROTO_REQ_DRM_KIE =0x36 , -WIIPROTO_REQ_DRM_KAIE =0x37 , -WIIPROTO_REQ_DRM_E =0x3d , -WIIPROTO_REQ_DRM_SKAI1 =0x3e , -WIIPROTO_REQ_DRM_SKAI2 =0x3f , -};enum wiiproto_keys {WIIPROTO_KEY_LEFT ,
@@ -139,52 +57,6 @@ static enum power_supply_property wiimote_battery_props[] = { POWER_SUPPLY_PROP_CAPACITY }; -/* requires the state.lock spinlock to be held */ -static inline bool wiimote_cmd_pending (struct wiimote_data *wdata,int cmd, -__u32 opt) -{ -return wdata->state.cmd == cmd && wdata->state.opt == opt; -} - -/* requires the state.lock spinlock to be held */ -static inline void wiimote_cmd_complete (struct wiimote_data *wdata) -{ - wdata->state.cmd =WIIPROTO_REQ_NULL ; -complete (&wdata->state.ready); -} - -static inline int wiimote_cmd_acquire (struct wiimote_data *wdata) -{ -return mutex_lock_interruptible (&wdata->state.sync) ? -ERESTARTSYS :0 ; -} - -/* requires the state.lock spinlock to be held */ -static inline void wiimote_cmd_set (struct wiimote_data *wdata,int cmd, -__u32 opt) -{ -INIT_COMPLETION (wdata->state.ready); - wdata->state.cmd = cmd; - wdata->state.opt = opt; -} - -static inline void wiimote_cmd_release (struct wiimote_data *wdata) -{ -mutex_unlock (&wdata->state.sync); -} - -static inline int wiimote_cmd_wait (struct wiimote_data *wdata) -{ -int ret; - - ret =wait_for_completion_interruptible_timeout (&wdata->state.ready,HZ ); -if (ret <0 ) -return -ERESTARTSYS ; -else if (ret ==0 ) -return -EIO ; -else -return 0 ; -} -static ssize_t wiimote_hid_send (struct hid_device *hdev,__u8 *buffer,size_t count) {
@@ -347,7 +219,7 @@ static __u8 select_drm(struct wiimote_data *wdata) } } -static void wiiproto_req_drm (struct wiimote_data *wdata,__u8 drm) +void wiiproto_req_drm (struct wiimote_data *wdata,__u8 drm) {__u8 cmd[3 ];
@@ -441,7 +313,7 @@ static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom, }/* requries the cmd-mutex to be held */ -static int wiimote_cmd_write (struct wiimote_data *wdata,__u32 offset, +int wiimote_cmd_write (struct wiimote_data *wdata,__u32 offset,const __u8 *wmem,__u8 size) {unsigned long flags;
drivers/hid/hid-wiimote.h new file +163 -0
163 lines not on this page · raw message
Signature · 1 line
1.7.7.3
PATCH v2 3/17 HID: wiimote: Add read-mem helpers
In reply to David HerrmannAdd helper functions similar to the write-mem helpers but for reading wiimote memory and eeprom.
--- drivers/hid/hid-wiimote-core.c | 77 ++++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-wiimote.h | 4 ++ 2 files changed, 81 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote-core.c +77 -0
@@ -312,6 +312,37 @@ static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,wiimote_queue (wdata, cmd,sizeof (cmd)); } +#define wiiproto_req_rreg (wdata, os, sz) \ + wiiproto_req_rmem((wdata), false, (os), (sz)) + +#define wiiproto_req_reeprom (wdata, os, sz) \ + wiiproto_req_rmem((wdata), true, (os), (sz)) + +static void wiiproto_req_rmem (struct wiimote_data *wdata,bool eeprom, +__u32 offset,__u16 size) +{ +__u8 cmd[7 ]; + +if (size ==0 ) { +hid_warn (wdata->hdev,"Invalid length %d rmem request\n" , size); +return ; + } + + cmd[0 ] =WIIPROTO_REQ_RMEM ; + cmd[1 ] =0 ; + cmd[2 ] = (offset >>16 ) &0xff ; + cmd[3 ] = (offset >>8 ) &0xff ; + cmd[4 ] = offset &0xff ; + cmd[5 ] = (size >>8 ) &0xff ; + cmd[6 ] = size &0xff ; + +if (!eeprom) + cmd[1 ] |=0x04 ; + +wiiproto_keep_rumble (wdata, &cmd[1 ]); +wiimote_queue (wdata, cmd,sizeof (cmd)); +} +/* requries the cmd-mutex to be held */ int wiimote_cmd_write (struct wiimote_data *wdata,__u32 offset,const __u8 *wmem,__u8 size)
@@ -331,6 +362,36 @@ int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,return ret; } +/* requries the cmd-mutex to be held */ +ssize_t wiimote_cmd_read (struct wiimote_data *wdata,__u32 offset,__u8 *rmem, +__u8 size) +{ +unsigned long flags; +ssize_t ret; + +spin_lock_irqsave (&wdata->state.lock, flags); + wdata->state.cmd_read_size = size; + wdata->state.cmd_read_buf = rmem; +wiimote_cmd_set (wdata,WIIPROTO_REQ_RMEM , offset &0xffff ); +wiiproto_req_rreg (wdata, offset, size); +spin_unlock_irqrestore (&wdata->state.lock, flags); + + ret =wiimote_cmd_wait (wdata); + +spin_lock_irqsave (&wdata->state.lock, flags); + wdata->state.cmd_read_buf =NULL ; +spin_unlock_irqrestore (&wdata->state.lock, flags); + +if (!ret) { +if (wdata->state.cmd_read_size ==0 ) + ret = -EIO ; +else + ret = wdata->state.cmd_read_size; + } + +return ret; +} +static int wiimote_battery_get_property (struct power_supply *psy,enum power_supply_property psp,union power_supply_propval *val)
@@ -742,7 +803,23 @@ static void handler_status(struct wiimote_data *wdata, const __u8 *payload)static void handler_data (struct wiimote_data *wdata,const __u8 *payload) { +__u16 offset = payload[3 ] <<8 | payload[4 ]; +__u8 size = (payload[2 ] >>4 ) +1 ; +__u8 err = payload[2 ] &0x0f ; +handler_keys (wdata, payload); + +if (wiimote_cmd_pending (wdata,WIIPROTO_REQ_RMEM , offset)) { +if (err) + size =0 ; +else if (size > wdata->state.cmd_read_size) + size = wdata->state.cmd_read_size; + + wdata->state.cmd_read_size = size; +if (wdata->state.cmd_read_buf) +memcpy (wdata->state.cmd_read_buf, &payload[5 ], size); +wiimote_cmd_complete (wdata); + } }static void handler_return (struct wiimote_data *wdata,const __u8 *payload)
drivers/hid/hid-wiimote.h +4 -0
@@ -62,6 +62,8 @@ struct wiimote_state {/* results of synchronous requests */ __u8 cmd_battery;__u8 cmd_err; +__u8 *cmd_read_buf; +__u8 cmd_read_size; };struct wiimote_data {
@@ -113,6 +115,8 @@ enum wiiproto_reqs {extern void wiiproto_req_drm (struct wiimote_data *wdata,__u8 drm);extern int wiimote_cmd_write (struct wiimote_data *wdata,__u32 offset,const __u8 *wmem,__u8 size); +extern ssize_t wiimote_cmd_read (struct wiimote_data *wdata,__u32 offset, +__u8 *rmem,__u8 size);/* requires the state.lock spinlock to be held */ static inline bool wiimote_cmd_pending(struct wiimote_data *wdata, int cmd,
Signature · 1 line
1.7.7.3
PATCH v2 4/17 HID: wiimote: Add extension support stub
In reply to David HerrmannThe wiimote supports several extensions. This adds a separate source file which handles all extensions and can be disabled at compile-time.
The driver reacts on "plug"-events on the extension port and starts a worker which initializes or deinitializes the extensions.
Currently, the initialization logic is not fully understood and we can only detect and enable all extensions when all extensions are deactivated. Therefore, we need to disable all extensions, then detect and activate them again to react on "plug"-events.
However, deactivating extensions will generate a new "plug"-event and we will never leave that loop. Hence, we only support extensions if they are plugged before the wiimote is connected (or before the ext-input device is opened). In the future we may support full extension hotplug support, but reverse-engineering this may take a while.
---
V2: Add comment which explains the unusual order in the deinitialization function.
drivers/hid/Kconfig | 9 +++ drivers/hid/Makefile | 3 + drivers/hid/hid-wiimote-core.c | 23 ++++++- drivers/hid/hid-wiimote-ext.c | 130 ++++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-wiimote.h | 17 +++++ 5 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 drivers/hid/hid-wiimote-ext.c
drivers/hid/Kconfig +9 -0
@@ -594,6 +594,15 @@ config HID_WIIMOTE ---help--- Support for the Nintendo Wii Remote bluetooth device. +config HID_WIIMOTE_EXT + bool "Nintendo Wii Remote Extension support" + depends on HID_WIIMOTE + default HID_WIIMOTE + ---help--- + Support for extension controllers of the Nintendo Wii Remote. Say yes + here if you want to use the Nintendo Motion+, Nunchuck or Classic + extension controllers with your Wii Remote. + config HID_ZEROPLUS tristate "Zeroplus based game controller support" depends on USB_HID
drivers/hid/Makefile +3 -0
@@ -26,6 +26,9 @@ ifdef CONFIG_LOGIWII_FF endif hid-wiimote-y := hid-wiimote-core.o +ifdef CONFIG_HID_WIIMOTE_EXT + hid-wiimote-y += hid-wiimote-ext.o +endif obj-$(CONFIG_HID_A4TECH) += hid-a4tech.o obj-$(CONFIG_HID_ACRUX) += hid-axff.o
drivers/hid/hid-wiimote-core.c +19 -4
@@ -201,6 +201,7 @@ static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)static __u8 select_drm (struct wiimote_data *wdata) {__u8 ir = wdata->state.flags &WIIPROTO_FLAGS_IR ; +bool ext =wiiext_active (wdata);if (ir ==WIIPROTO_FLAG_IR_BASIC ) {if (wdata->state.flags &WIIPROTO_FLAG_ACCEL )
@@ -212,10 +213,17 @@ static __u8 select_drm(struct wiimote_data *wdata) }else if (ir ==WIIPROTO_FLAG_IR_FULL ) {return WIIPROTO_REQ_DRM_SKAI1 ; }else { -if (wdata->state.flags &WIIPROTO_FLAG_ACCEL ) -return WIIPROTO_REQ_DRM_KA ; -else -return WIIPROTO_REQ_DRM_K ; +if (wdata->state.flags &WIIPROTO_FLAG_ACCEL ) { +if (ext) +return WIIPROTO_REQ_DRM_KAE ; +else +return WIIPROTO_REQ_DRM_KA ; + }else { +if (ext) +return WIIPROTO_REQ_DRM_KE ; +else +return WIIPROTO_REQ_DRM_K ; + } } }
@@ -795,6 +803,8 @@ static void handler_status(struct wiimote_data *wdata, const __u8 *payload)/* on status reports the drm is reset so we need to resend the drm */ wiiproto_req_drm (wdata,WIIPROTO_REQ_NULL ); +wiiext_event (wdata, payload[2 ] &0x02 ); +if (wiimote_cmd_pending (wdata,WIIPROTO_REQ_SREQ ,0 )) { wdata->state.cmd_battery = payload[5 ];wiimote_cmd_complete (wdata);
@@ -1145,6 +1155,7 @@ err:static void wiimote_destroy (struct wiimote_data *wdata) { +wiiext_deinit (wdata);wiimote_leds_destroy (wdata);power_supply_unregister (&wdata->battery);
@@ -1216,6 +1227,10 @@ static int wiimote_hid_probe(struct hid_device *hdev,if (ret) goto err_free; + ret =wiiext_init (wdata); +if (ret) + goto err_free; +hid_info (hdev,"New device registered\n" );/* by default set led1 after device initialization */
drivers/hid/hid-wiimote-ext.c new file +130 -0
130 lines not on this page · raw message
drivers/hid/hid-wiimote.h +17 -0
@@ -73,6 +73,7 @@ struct wiimote_data {struct input_dev *accel;struct input_dev *ir;struct power_supply battery; +struct wiimote_ext *ext;spinlock_t qlock;__u8 head;
@@ -118,6 +119,22 @@ extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,extern ssize_t wiimote_cmd_read (struct wiimote_data *wdata,__u32 offset,__u8 *rmem,__u8 size); +#ifdef CONFIG_HID_WIIMOTE_EXT + +extern int wiiext_init (struct wiimote_data *wdata); +extern void wiiext_deinit (struct wiimote_data *wdata); +extern void wiiext_event (struct wiimote_data *wdata,bool plugged); +extern bool wiiext_active (struct wiimote_data *wdata); + +#else + +static inline int wiiext_init (void *u) {return 0 ; } +static inline void wiiext_deinit (void *u) { } +static inline void wiiext_event (void *u,bool p) { } +static inline bool wiiext_active (void *u) {return false; } + +#endif +/* requires the state.lock spinlock to be held */ static inline bool wiimote_cmd_pending (struct wiimote_data *wdata,int cmd,__u32 opt)
Signature · 1 line
1.7.7.3
PATCH v2 5/17 HID: wiimote: Add extension initializer stubs
In reply to David HerrmannAdd stub functions to read and identify extensions and then initialize all connected extensions.
--- drivers/hid/hid-wiimote-ext.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 files changed, 38 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote-ext.c +38 -0
@@ -33,10 +33,48 @@ enum wiiext_type { WIIEXT_NUNCHUCK, /* Nintendo nunchuck controller */ }; +/* diable all extensions */ +static void ext_disable (struct wiimote_ext *ext) +{ +unsigned long flags; + +spin_lock_irqsave (&ext->wdata->state.lock, flags); + ext->motionp = false; + ext->ext_type =WIIEXT_NONE ; +spin_unlock_irqrestore (&ext->wdata->state.lock, flags); +} + +static bool motionp_read (struct wiimote_ext *ext) +{ +return false; +} + +static __u8 ext_read (struct wiimote_ext *ext) +{ +return WIIEXT_NONE ; +} + +static void ext_enable (struct wiimote_ext *ext,bool motionp,__u8 ext_type) +{ +unsigned long flags; + +spin_lock_irqsave (&ext->wdata->state.lock, flags); + ext->motionp = motionp; + ext->ext_type = ext_type; +spin_unlock_irqrestore (&ext->wdata->state.lock, flags); +} +static void wiiext_worker (struct work_struct *work) { struct wiimote_ext *ext = container_of(work, struct wiimote_ext, worker); +bool motionp; +__u8 ext_type; + +ext_disable (ext); + motionp =motionp_read (ext); + ext_type =ext_read (ext); +ext_enable (ext, motionp, ext_type); }/* schedule work only once, otherwise mark for reschedule */
Signature · 1 line
1.7.7.3
PATCH v2 6/17 HID: wiimote: Add extension initializers
In reply to David HerrmannThe wiimote extension registers are not fully understood, so we always disable all extensions on extension-port events. Then we reinitialize and reidentify them and activate all requested extensions.
--- drivers/hid/hid-wiimote-ext.c | 81 ++++++++++++++++++++++++++++++++++++++++- 1 files changed, 79 insertions(+), 2 deletions(-)
drivers/hid/hid-wiimote-ext.c +79 -2
@@ -37,30 +37,107 @@ enum wiiext_type {static void ext_disable (struct wiimote_ext *ext) {unsigned long flags; +__u8 wmem =0x55 ; + +if (!wiimote_cmd_acquire (ext->wdata)) { +wiimote_cmd_write (ext->wdata,0xa400f0 , &wmem,sizeof (wmem)); +wiimote_cmd_release (ext->wdata); + }spin_lock_irqsave (&ext->wdata->state.lock, flags); ext->motionp = false; ext->ext_type =WIIEXT_NONE ; +wiiproto_req_drm (ext->wdata,WIIPROTO_REQ_NULL );spin_unlock_irqrestore (&ext->wdata->state.lock, flags); }static bool motionp_read (struct wiimote_ext *ext) { -return false; +__u8 rmem[2 ], wmem; +ssize_t ret; +bool avail = false; + +if (wiimote_cmd_acquire (ext->wdata)) +return false; + +/* initialize motion plus */ + wmem =0x55 ; + ret =wiimote_cmd_write (ext->wdata,0xa600f0 , &wmem,sizeof (wmem)); +if (ret) + goto error; + +/* read motion plus ID */ + ret =wiimote_cmd_read (ext->wdata,0xa600fe , rmem,2 ); +if (ret ==2 || rmem[1 ] ==0x5 ) + avail = true; + +error: +wiimote_cmd_release (ext->wdata); +return avail; }static __u8 ext_read (struct wiimote_ext *ext) { -return WIIEXT_NONE ; +ssize_t ret; +__u8 rmem[2 ], wmem; +__u8 type =WIIEXT_NONE ; + +if (!ext->plugged) +return WIIEXT_NONE ; + +if (wiimote_cmd_acquire (ext->wdata)) +return WIIEXT_NONE ; + +/* initialize extension */ + wmem =0x55 ; + ret =wiimote_cmd_write (ext->wdata,0xa400f0 , &wmem,sizeof (wmem)); +if (!ret) { +/* disable encryption */ + wmem =0x0 ; +wiimote_cmd_write (ext->wdata,0xa400fb , &wmem,sizeof (wmem)); + } + +/* read extension ID */ + ret =wiimote_cmd_read (ext->wdata,0xa400fe , rmem,2 ); +if (ret ==2 ) { +if (rmem[0 ] ==0 && rmem[1 ] ==0 ) + type =WIIEXT_NUNCHUCK ; +else if (rmem[0 ] ==0x01 && rmem[1 ] ==0x01 ) + type =WIIEXT_CLASSIC ; + } + +wiimote_cmd_release (ext->wdata); + +return type; }static void ext_enable (struct wiimote_ext *ext,bool motionp,__u8 ext_type) {unsigned long flags; +__u8 wmem; +int ret; + +if (motionp) { +if (wiimote_cmd_acquire (ext->wdata)) +return ; + +if (ext_type ==WIIEXT_CLASSIC ) + wmem =0x07 ; +else if (ext_type ==WIIEXT_NUNCHUCK ) + wmem =0x05 ; +else + wmem =0x04 ; + + ret =wiimote_cmd_write (ext->wdata,0xa600fe , &wmem,sizeof (wmem)); +wiimote_cmd_release (ext->wdata); +if (ret) +return ; + }spin_lock_irqsave (&ext->wdata->state.lock, flags); ext->motionp = motionp; ext->ext_type = ext_type; +wiiproto_req_drm (ext->wdata,WIIPROTO_REQ_NULL );spin_unlock_irqrestore (&ext->wdata->state.lock, flags); }
Signature · 1 line
1.7.7.3
PATCH v2 7/17 HID: wiimote: Add extension sysfs attribute
In reply to David HerrmannAdd new sysfs attribute "extension" which returns the currently connected and initialized extensions.
--- Documentation/ABI/testing/sysfs-driver-hid-wiimote | 12 +++++ drivers/hid/hid-wiimote-ext.c | 46 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 0 deletions(-)
Documentation/ABI/testing/sysfs-driver-hid-wiimote +12 -0
@@ -8,3 +8,15 @@ Contact: David Herrmann <[email protected]> Description: Make it possible to set/get current led state. Reading from it returns 0 if led is off and 1 if it is on. Writing 0 to it disables the led, writing 1 enables it. + +What: /sys/bus/hid/drivers/wiimote/<dev>/extension +Date: August 2011 +KernelVersion: 3.2 +Contact: David Herrmann <[email protected]> +Description: This file contains the currently connected and initialized + extensions. It can be one of: none, motionp, nunchuck, classic, + motionp+nunchuck, motionp+classic + motionp is the official Nintendo Motion+ extension, nunchuck is + the official Nintendo Nunchuck extension and classic is the + Nintendo Classic Controller extension. The motionp extension can + be combined with the other two.
drivers/hid/hid-wiimote-ext.c +46 -0
74 lines not on this page · raw message
Signature · 1 line
1.7.7.3
PATCH v2 8/17 HID: wiimote: Register input devices for extensions
In reply to David HerrmannMotion+ and regular extensions are physical adapters for the wiimote so create one input device for each of them. This also allows to enable only opened extensions and turn unused extenions off to save battery power.
--- drivers/hid/hid-wiimote-ext.c | 109 +++++++++++++++++++++++++++++++++++++++- 1 files changed, 106 insertions(+), 3 deletions(-)
drivers/hid/hid-wiimote-ext.c +106 -3
@@ -19,6 +19,8 @@struct wiimote_ext {struct wiimote_data *wdata;struct work_struct worker; +struct input_dev *input; +struct input_dev *mp_input;atomic_t opened;atomic_t mp_opened;
@@ -57,6 +59,9 @@ static bool motionp_read(struct wiimote_ext *ext)ssize_t ret;bool avail = false; +if (!atomic_read (&ext->mp_opened)) +return false; +if (wiimote_cmd_acquire (ext->wdata))return false;
@@ -82,7 +87,7 @@ static __u8 ext_read(struct wiimote_ext *ext)__u8 rmem[2 ], wmem;__u8 type =WIIEXT_NONE ; -if (!ext->plugged) +if (!ext->plugged || !atomic_read (&ext->opened))return WIIEXT_NONE ;if (wiimote_cmd_acquire (ext->wdata))
@@ -234,6 +239,54 @@ static ssize_t wiiext_show(struct device *dev, struct device_attribute *attr, static DEVICE_ATTR(extension, S_IRUGO, wiiext_show, NULL); +static int wiiext_input_open (struct input_dev *dev) +{ +struct wiimote_ext *ext =input_get_drvdata (dev); +int ret; + + ret =hid_hw_open (ext->wdata->hdev); +if (ret) +return ret; + +atomic_inc (&ext->opened); +wiiext_schedule (ext); + +return 0 ; +} + +static void wiiext_input_close (struct input_dev *dev) +{ +struct wiimote_ext *ext =input_get_drvdata (dev); + +atomic_dec (&ext->opened); +wiiext_schedule (ext); +hid_hw_close (ext->wdata->hdev); +} + +static int wiiext_mp_open (struct input_dev *dev) +{ +struct wiimote_ext *ext =input_get_drvdata (dev); +int ret; + + ret =hid_hw_open (ext->wdata->hdev); +if (ret) +return ret; + +atomic_inc (&ext->mp_opened); +wiiext_schedule (ext); + +return 0 ; +} + +static void wiiext_mp_close (struct input_dev *dev) +{ +struct wiimote_ext *ext =input_get_drvdata (dev); + +atomic_dec (&ext->mp_opened); +wiiext_schedule (ext); +hid_hw_close (ext->wdata->hdev); +} +/* Initializes the extension driver of a wiimote */ int wiiext_init (struct wiimote_data *wdata) {
@@ -248,9 +301,53 @@ int wiiext_init(struct wiimote_data *wdata) ext->wdata = wdata;INIT_WORK (&ext->worker, wiiext_worker); + ext->input =input_allocate_device (); +if (!ext->input) { + ret = -ENOMEM ; + goto err_input; + } + +input_set_drvdata (ext->input, ext); + ext->input->open = wiiext_input_open; + ext->input->close = wiiext_input_close; + ext->input->dev.parent = &wdata->hdev->dev; + ext->input->id.bustype = wdata->hdev->bus; + ext->input->id.vendor = wdata->hdev->vendor; + ext->input->id.product = wdata->hdev->product; + ext->input->id.version = wdata->hdev->version; + ext->input->name =WIIMOTE_NAME " Extension" ; + + ret =input_register_device (ext->input); +if (ret) { +input_free_device (ext->input); + goto err_input; + } + + ext->mp_input =input_allocate_device (); +if (!ext->mp_input) { + ret = -ENOMEM ; + goto err_mp; + } + +input_set_drvdata (ext->mp_input, ext); + ext->mp_input->open = wiiext_mp_open; + ext->mp_input->close = wiiext_mp_close; + ext->mp_input->dev.parent = &wdata->hdev->dev; + ext->mp_input->id.bustype = wdata->hdev->bus; + ext->mp_input->id.vendor = wdata->hdev->vendor; + ext->mp_input->id.product = wdata->hdev->product; + ext->mp_input->id.version = wdata->hdev->version; + ext->mp_input->name =WIIMOTE_NAME " Motion+" ; + + ret =input_register_device (ext->mp_input); +if (ret) { +input_free_device (ext->mp_input); + goto err_mp; + } + ret =device_create_file (&wdata->hdev->dev, &dev_attr_extension);if (ret) - goto err; + goto err_dev;spin_lock_irqsave (&wdata->state.lock, flags); wdata->ext = ext;
@@ -258,7 +355,11 @@ int wiiext_init(struct wiimote_data *wdata)return 0 ; -err: +err_dev: +input_unregister_device (ext->mp_input); +err_mp: +input_unregister_device (ext->input); +err_input:kfree (ext);return ret; }
@@ -285,6 +386,8 @@ void wiiext_deinit(struct wiimote_data *wdata)spin_unlock_irqrestore (&wdata->state.lock, flags);device_remove_file (&wdata->hdev->dev, &dev_attr_extension); +input_unregister_device (ext->mp_input); +input_unregister_device (ext->input);cancel_work_sync (&ext->worker);kfree (ext);
Signature · 1 line
1.7.7.3
PATCH v2 9/17 HID: wiimote: Add extension handler stubs
In reply to David HerrmannAll supported extensions report data as 6 byte block. All DRMs with extension data provide at least 6 extension bytes. Hence a generic handler for all extension bytes is sufficient and can be called on all DRMs.
The handler distinguishes the input and passes it to the right handler. Motion+ passes data interleaved so we can have Motion+ and a regular extension enabled simultaneously.
--- drivers/hid/hid-wiimote-core.c | 6 ++++++ drivers/hid/hid-wiimote-ext.c | 29 +++++++++++++++++++++++++++++ drivers/hid/hid-wiimote.h | 2 ++ 3 files changed, 37 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote-core.c +6 -0
@@ -857,6 +857,7 @@ static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)static void handler_drm_KE (struct wiimote_data *wdata,const __u8 *payload) {handler_keys (wdata, payload); +wiiext_handle (wdata, &payload[2 ]); }static void handler_drm_KAI (struct wiimote_data *wdata,const __u8 *payload)
@@ -873,6 +874,7 @@ static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)static void handler_drm_KEE (struct wiimote_data *wdata,const __u8 *payload) {handler_keys (wdata, payload); +wiiext_handle (wdata, &payload[2 ]); }static void handler_drm_KIE (struct wiimote_data *wdata,const __u8 *payload)
@@ -883,12 +885,14 @@ static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)ir_to_input2 (wdata, &payload[7 ], false);ir_to_input3 (wdata, &payload[9 ], true);input_sync (wdata->ir); +wiiext_handle (wdata, &payload[12 ]); }static void handler_drm_KAE (struct wiimote_data *wdata,const __u8 *payload) {handler_keys (wdata, payload);handler_accel (wdata, payload); +wiiext_handle (wdata, &payload[5 ]); }static void handler_drm_KAIE (struct wiimote_data *wdata,const __u8 *payload)
@@ -900,10 +904,12 @@ static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)ir_to_input2 (wdata, &payload[10 ], false);ir_to_input3 (wdata, &payload[12 ], true);input_sync (wdata->ir); +wiiext_handle (wdata, &payload[15 ]); }static void handler_drm_E (struct wiimote_data *wdata,const __u8 *payload) { +wiiext_handle (wdata, payload); }static void handler_drm_SKAI1 (struct wiimote_data *wdata,const __u8 *payload)
drivers/hid/hid-wiimote-ext.c +29 -0
@@ -204,6 +204,35 @@ bool wiiext_active(struct wiimote_data *wdata)return wdata->ext->motionp || wdata->ext->ext_type; } +static void handler_motionp (struct wiimote_ext *ext,const __u8 *payload) +{ +} + +static void handler_nunchuck (struct wiimote_ext *ext,const __u8 *payload) +{ +} + +static void handler_classic (struct wiimote_ext *ext,const __u8 *payload) +{ +} + +/* call this with state.lock spinlock held */ +void wiiext_handle (struct wiimote_data *wdata,const __u8 *payload) +{ +struct wiimote_ext *ext = wdata->ext; + +if (!ext) +return ; + +if (ext->motionp && (payload[5 ] &0x02 )) { +handler_motionp (ext, payload); + }else if (ext->ext_type ==WIIEXT_NUNCHUCK ) { +handler_nunchuck (ext, payload); + }else if (ext->ext_type ==WIIEXT_CLASSIC ) { +handler_classic (ext, payload); + } +} +static ssize_t wiiext_show (struct device *dev,struct device_attribute *attr,char *buf) {
drivers/hid/hid-wiimote.h +2 -0
@@ -125,6 +125,7 @@ extern int wiiext_init(struct wiimote_data *wdata);extern void wiiext_deinit (struct wiimote_data *wdata);extern void wiiext_event (struct wiimote_data *wdata,bool plugged);extern bool wiiext_active (struct wiimote_data *wdata); +extern void wiiext_handle (struct wiimote_data *wdata,const __u8 *payload);#else
@@ -132,6 +133,7 @@ static inline int wiiext_init(void *u) { return 0; }static inline void wiiext_deinit (void *u) { }static inline void wiiext_event (void *u,bool p) { }static inline bool wiiext_active (void *u) {return false; } +static inline void wiiext_handle (void *u,const __u8 *p) { }#endif
Signature · 1 line
1.7.7.3
PATCH v2 10/17 HID: wiimote: Parse motion+ data
In reply to David HerrmannMotion+ reports rotation gyro data which we report to userspace as ABS_RX/Y/Z values. The device reports them either in fast or slow mode. We adjust the values to get a linear scale so userspace does not need to know about slow and fast mode.
The motion+ also reports whether an extension is connected to it. We keep track of this value and reinitialize the extensions if an extension is plugged or unplugged.
--- drivers/hid/hid-wiimote-ext.c | 70 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 70 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote-ext.c +70 -0
@@ -25,6 +25,7 @@ struct wiimote_ext {atomic_t opened;atomic_t mp_opened;bool plugged; +bool mp_plugged;bool motionp;__u8 ext_type; };
@@ -182,6 +183,10 @@ void wiiext_event(struct wiimote_data *wdata, bool plugged)return ; wdata->ext->plugged = plugged; + +if (!plugged) + wdata->ext->mp_plugged = false; +/* * We need to call wiiext_schedule(wdata->ext) here, however, the * extension initialization logic is not fully understood and so
@@ -206,6 +211,63 @@ bool wiiext_active(struct wiimote_data *wdata)static void handler_motionp (struct wiimote_ext *ext,const __u8 *payload) { +__s32 x, y, z; +bool plugged; + +/* | 8 7 6 5 4 3 | 2 | 1 | +* -----+------------------------------+-----+-----+ +* 1 | Yaw Speed <7:0> | +* 2 | Roll Speed <7:0> | +* 3 | Pitch Speed <7:0> | +* -----+------------------------------+-----+-----+ +* 4 | Yaw Speed <13:8> | Yaw |Pitch| +* -----+------------------------------+-----+-----+ +* 5 | Roll Speed <13:8> |Roll | Ext | +* -----+------------------------------+-----+-----+ +* 6 | Pitch Speed <13:8> | 1 | 0 | +* -----+------------------------------+-----+-----+ +* The single bits Yaw, Roll, Pitch in the lower right corner specify +* whether the wiimote is rotating fast (0) or slow (1). Speed for slow +* roation is 440 deg/s and for fast rotation 2000 deg/s. To get a +* linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast +* and 9 for slow. +* If the wiimote is not rotating the sensor reports 2^13 = 8192. +* Ext specifies whether an extension is connected to the motionp. +*/ + + x = payload[0 ]; + y = payload[1 ]; + z = payload[2 ]; + + x |= (((__u16 )payload[3 ]) <<6 ) &0xff00 ; + y |= (((__u16 )payload[4 ]) <<6 ) &0xff00 ; + z |= (((__u16 )payload[5 ]) <<6 ) &0xff00 ; + + x -=8192 ; + y -=8192 ; + z -=8192 ; + +if (!(payload[3 ] &0x02 )) + x *=18 ; +else + x *=9 ; +if (!(payload[4 ] &0x02 )) + y *=18 ; +else + y *=9 ; +if (!(payload[3 ] &0x01 )) + z *=18 ; +else + z *=9 ; + +input_report_abs (ext->mp_input,ABS_RX , x); +input_report_abs (ext->mp_input,ABS_RY , y); +input_report_abs (ext->mp_input,ABS_RZ , z); +input_sync (ext->mp_input); + + plugged = payload[5 ] &0x01 ; +if (plugged != ext->mp_plugged) + ext->mp_plugged = plugged; }static void handler_nunchuck (struct wiimote_ext *ext,const __u8 *payload)
@@ -368,6 +430,14 @@ int wiiext_init(struct wiimote_data *wdata) ext->mp_input->id.version = wdata->hdev->version; ext->mp_input->name =WIIMOTE_NAME " Motion+" ; +set_bit (EV_ABS , ext->mp_input->evbit); +set_bit (ABS_RX , ext->mp_input->absbit); +set_bit (ABS_RY , ext->mp_input->absbit); +set_bit (ABS_RZ , ext->mp_input->absbit); +input_set_abs_params (ext->mp_input,ABS_RX ,-160000 ,160000 ,4 ,8 ); +input_set_abs_params (ext->mp_input,ABS_RY ,-160000 ,160000 ,4 ,8 ); +input_set_abs_params (ext->mp_input,ABS_RZ ,-160000 ,160000 ,4 ,8 ); + ret =input_register_device (ext->mp_input);if (ret) {input_free_device (ext->mp_input);
Signature · 1 line
1.7.7.3
PATCH v2 11/17 HID: wiimote: Parse nunchuck data
In reply to David HerrmannThe Nintendo Nunchuck extension reports accelerometer values, one analog stick and two buttons. See inline comments for data layout.
We report all data to userspace through extension input device.
--- drivers/hid/hid-wiimote-ext.c | 105 ++++++++++++++++++++++++++++++++++++++++- 1 files changed, 104 insertions(+), 1 deletions(-)
drivers/hid/hid-wiimote-ext.c +104 -1
@@ -36,6 +36,17 @@ enum wiiext_type { WIIEXT_NUNCHUCK, /* Nintendo nunchuck controller */ }; +enum wiiext_keys { +WIIEXT_KEY_C , +WIIEXT_KEY_Z , +WIIEXT_KEY_COUNT +}; + +static __u16 wiiext_keymap[] = { +BTN_C ,/* WIIEXT_KEY_C */ +BTN_Z ,/* WIIEXT_KEY_Z */ +}; +/* diable all extensions */ static void ext_disable (struct wiimote_ext *ext) {
@@ -272,6 +283,82 @@ static void handler_motionp(struct wiimote_ext *ext, const __u8 *payload)static void handler_nunchuck (struct wiimote_ext *ext,const __u8 *payload) { +__s16 x, y, z, bx, by; + +/* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 | +* -----+----------+---------+---------+----+-----+ +* 1 | Button X <7:0> | +* 2 | Button Y <7:0> | +* -----+----------+---------+---------+----+-----+ +* 3 | Speed X <9:2> | +* 4 | Speed Y <9:2> | +* 5 | Speed Z <9:2> | +* -----+----------+---------+---------+----+-----+ +* 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ | +* -----+----------+---------+---------+----+-----+ +* Button X/Y is the analog stick. Speed X, Y and Z are the +* accelerometer data in the same format as the wiimote's accelerometer. +* The 6th byte contains the LSBs of the accelerometer data. +* BC and BZ are the C and Z buttons: 0 means pressed +* +* If reported interleaved with motionp, then the layout changes. The +* 5th and 6th byte changes to: +* -----+-----------------------------------+-----+ +* 5 | Speed Z <9:3> | EXT | +* -----+--------+-----+-----+----+----+----+-----+ +* 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 | +* -----+--------+-----+-----+----+----+----+-----+ +* All three accelerometer values lose their LSB. The other data is +* still available but slightly moved. +* +* Center data for button values is 128. Center value for accelerometer +* values it 512 / 0x200 +*/ + + bx = payload[0 ]; + by = payload[1 ]; + bx -=128 ; + by -=128 ; + + x = payload[2 ] <<2 ; + y = payload[3 ] <<2 ; + z = payload[4 ] <<2 ; + +if (ext->motionp) { + x |= (payload[5 ] >>3 ) &0x02 ; + y |= (payload[5 ] >>4 ) &0x02 ; + z &= ~0x4 ; + z |= (payload[5 ] >>5 ) &0x06 ; + }else { + x |= (payload[5 ] >>2 ) &0x03 ; + y |= (payload[5 ] >>4 ) &0x03 ; + z |= (payload[5 ] >>6 ) &0x03 ; + } + + x -=0x200 ; + y -=0x200 ; + z -=0x200 ; + +input_report_abs (ext->input,ABS_HAT0X , bx); +input_report_abs (ext->input,ABS_HAT0Y , by); + +input_report_abs (ext->input,ABS_RX , x); +input_report_abs (ext->input,ABS_RY , y); +input_report_abs (ext->input,ABS_RZ , z); + +if (ext->motionp) { +input_report_key (ext->input, + wiiext_keymap[WIIEXT_KEY_Z ], !!(payload[5 ] &0x04 )); +input_report_key (ext->input, + wiiext_keymap[WIIEXT_KEY_C ], !!(payload[5 ] &0x08 )); + }else { +input_report_key (ext->input, + wiiext_keymap[WIIEXT_KEY_Z ], !!(payload[5 ] &0x01 )); +input_report_key (ext->input, + wiiext_keymap[WIIEXT_KEY_C ], !!(payload[5 ] &0x02 )); + } + +input_sync (ext->input); }static void handler_classic (struct wiimote_ext *ext,const __u8 *payload)
@@ -383,7 +470,7 @@ int wiiext_init(struct wiimote_data *wdata) {struct wiimote_ext *ext;unsigned long flags; -int ret; +int ret, i; ext =kzalloc (sizeof (*ext),GFP_KERNEL );if (!ext)
@@ -408,6 +495,22 @@ int wiiext_init(struct wiimote_data *wdata) ext->input->id.version = wdata->hdev->version; ext->input->name =WIIMOTE_NAME " Extension" ; +set_bit (EV_KEY , ext->input->evbit); +for (i =0 ; i <WIIEXT_KEY_COUNT ; ++i) +set_bit (wiiext_keymap[i], ext->input->keybit); + +set_bit (EV_ABS , ext->input->evbit); +set_bit (ABS_HAT0X , ext->input->absbit); +set_bit (ABS_HAT0Y , ext->input->absbit); +input_set_abs_params (ext->input,ABS_HAT0X ,-120 ,120 ,2 ,4 ); +input_set_abs_params (ext->input,ABS_HAT0Y ,-120 ,120 ,2 ,4 ); +set_bit (ABS_RX , ext->input->absbit); +set_bit (ABS_RY , ext->input->absbit); +set_bit (ABS_RZ , ext->input->absbit); +input_set_abs_params (ext->input,ABS_RX ,-500 ,500 ,2 ,4 ); +input_set_abs_params (ext->input,ABS_RY ,-500 ,500 ,2 ,4 ); +input_set_abs_params (ext->input,ABS_RZ ,-500 ,500 ,2 ,4 ); + ret =input_register_device (ext->input);if (ret) {input_free_device (ext->input);
Signature · 1 line
1.7.7.3
PATCH v2 12/17 HID: wiimote: Parse classic controller data
In reply to David HerrmannNintendo Classic Controller extension reports lots of keys, two analog sticks and two analog buttons. We report all data through extension input device to userspace.
--- drivers/hid/hid-wiimote-ext.c | 156 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 156 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote-ext.c +156 -0
@@ -39,12 +39,42 @@ enum wiiext_type {enum wiiext_keys {WIIEXT_KEY_C ,WIIEXT_KEY_Z , +WIIEXT_KEY_A , +WIIEXT_KEY_B , +WIIEXT_KEY_X , +WIIEXT_KEY_Y , +WIIEXT_KEY_ZL , +WIIEXT_KEY_ZR , +WIIEXT_KEY_PLUS , +WIIEXT_KEY_MINUS , +WIIEXT_KEY_HOME , +WIIEXT_KEY_LEFT , +WIIEXT_KEY_RIGHT , +WIIEXT_KEY_UP , +WIIEXT_KEY_DOWN , +WIIEXT_KEY_LT , +WIIEXT_KEY_RT ,WIIEXT_KEY_COUNT };static __u16 wiiext_keymap[] = {BTN_C ,/* WIIEXT_KEY_C */ BTN_Z ,/* WIIEXT_KEY_Z */ +BTN_A ,/* WIIEXT_KEY_A */ +BTN_B ,/* WIIEXT_KEY_B */ +BTN_X ,/* WIIEXT_KEY_X */ +BTN_Y ,/* WIIEXT_KEY_Y */ +BTN_TL2 ,/* WIIEXT_KEY_ZL */ +BTN_TR2 ,/* WIIEXT_KEY_ZR */ +KEY_NEXT ,/* WIIEXT_KEY_PLUS */ +KEY_PREVIOUS ,/* WIIEXT_KEY_MINUS */ +BTN_MODE ,/* WIIEXT_KEY_HOME */ +KEY_LEFT ,/* WIIEXT_KEY_LEFT */ +KEY_RIGHT ,/* WIIEXT_KEY_RIGHT */ +KEY_UP ,/* WIIEXT_KEY_UP */ +KEY_DOWN ,/* WIIEXT_KEY_DOWN */ +BTN_TL ,/* WIIEXT_KEY_LT */ +BTN_TR ,/* WIIEXT_KEY_RT */ };/* diable all extensions */
@@ -363,6 +393,120 @@ static void handler_nunchuck(struct wiimote_ext *ext, const __u8 *payload)static void handler_classic (struct wiimote_ext *ext,const __u8 *payload) { +__s8 rx, ry, lx, ly, lt, rt; + +/* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* 1 | RX <5:4> | LX <5:0> | +* 2 | RX <3:2> | LY <5:0> | +* -----+-----+-----+-----+-----------------------------+ +* 3 |RX<1>| LT <5:4> | RY <5:1> | +* -----+-----+-----------+-----------------------------+ +* 4 | LT <3:1> | RT <5:1> | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* All buttons are 0 if pressed +* RX and RY are right analog stick +* LX and LY are left analog stick +* LT is left trigger, RT is right trigger +* BLT is 0 if left trigger is fully pressed +* BRT is 0 if right trigger is fully pressed +* BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons +* BZL is left Z button and BZR is right Z button +* B-, BH, B+ are +, HOME and - buttons +* BB, BY, BA, BX are A, B, X, Y buttons +* LSB of RX, RY, LT, and RT are not transmitted and always 0. +* +* With motionp enabled it changes slightly to this: +* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* 1 | RX <4:3> | LX <5:1> | BDU | +* 2 | RX <2:1> | LY <5:1> | BDL | +* -----+-----+-----+-----+-----------------------+-----+ +* 3 |RX<0>| LT <4:3> | RY <4:0> | +* -----+-----+-----------+-----------------------------+ +* 4 | LT <2:0> | RT <4:0> | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 | +* -----+-----+-----+-----+-----+-----+-----+-----+-----+ +* Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest +* is the same as before. +*/ + +if (ext->motionp) { + lx = payload[0 ] &0x3e ; + ly = payload[0 ] &0x3e ; + }else { + lx = payload[0 ] &0x3f ; + ly = payload[0 ] &0x3f ; + } + + rx = (payload[0 ] >>3 ) &0x14 ; + rx |= (payload[1 ] >>5 ) &0x06 ; + rx |= (payload[2 ] >>7 ) &0x01 ; + ry = payload[2 ] &0x1f ; + + rt = payload[3 ] &0x1f ; + lt = (payload[2 ] >>2 ) &0x18 ; + lt |= (payload[3 ] >>5 ) &0x07 ; + + rx <<=1 ; + ry <<=1 ; + rt <<=1 ; + lt <<=1 ; + +input_report_abs (ext->input,ABS_HAT1X , lx -0x20 ); +input_report_abs (ext->input,ABS_HAT1Y , ly -0x20 ); +input_report_abs (ext->input,ABS_HAT2X , rx -0x20 ); +input_report_abs (ext->input,ABS_HAT2Y , ry -0x20 ); +input_report_abs (ext->input,ABS_HAT3X , rt -0x20 ); +input_report_abs (ext->input,ABS_HAT3Y , lt -0x20 ); + +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_RIGHT ], + !!(payload[4 ] &0x80 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_DOWN ], + !!(payload[4 ] &0x40 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_LT ], + !!(payload[4 ] &0x20 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_MINUS ], + !!(payload[4 ] &0x10 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_HOME ], + !!(payload[4 ] &0x08 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_PLUS ], + !!(payload[4 ] &0x04 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_RT ], + !!(payload[4 ] &0x02 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_ZL ], + !!(payload[5 ] &0x80 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_B ], + !!(payload[5 ] &0x40 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_Y ], + !!(payload[5 ] &0x20 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_A ], + !!(payload[5 ] &0x10 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_X ], + !!(payload[5 ] &0x08 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_ZR ], + !!(payload[5 ] &0x04 )); + +if (ext->motionp) { +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_UP ], + !!(payload[0 ] &0x01 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_LEFT ], + !!(payload[1 ] &0x01 )); + }else { +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_UP ], + !!(payload[5 ] &0x01 )); +input_report_key (ext->input, wiiext_keymap[WIIEXT_KEY_LEFT ], + !!(payload[5 ] &0x02 )); + } + +input_sync (ext->input); }/* call this with state.lock spinlock held */
@@ -502,8 +646,20 @@ int wiiext_init(struct wiimote_data *wdata)set_bit (EV_ABS , ext->input->evbit);set_bit (ABS_HAT0X , ext->input->absbit);set_bit (ABS_HAT0Y , ext->input->absbit); +set_bit (ABS_HAT1X , ext->input->absbit); +set_bit (ABS_HAT1Y , ext->input->absbit); +set_bit (ABS_HAT2X , ext->input->absbit); +set_bit (ABS_HAT2Y , ext->input->absbit); +set_bit (ABS_HAT3X , ext->input->absbit); +set_bit (ABS_HAT3Y , ext->input->absbit);input_set_abs_params (ext->input,ABS_HAT0X ,-120 ,120 ,2 ,4 );input_set_abs_params (ext->input,ABS_HAT0Y ,-120 ,120 ,2 ,4 ); +input_set_abs_params (ext->input,ABS_HAT1X ,-30 ,30 ,1 ,1 ); +input_set_abs_params (ext->input,ABS_HAT1Y ,-30 ,30 ,1 ,1 ); +input_set_abs_params (ext->input,ABS_HAT2X ,-30 ,30 ,1 ,1 ); +input_set_abs_params (ext->input,ABS_HAT2Y ,-30 ,30 ,1 ,1 ); +input_set_abs_params (ext->input,ABS_HAT3X ,-30 ,30 ,1 ,1 ); +input_set_abs_params (ext->input,ABS_HAT3Y ,-30 ,30 ,1 ,1 );set_bit (ABS_RX , ext->input->absbit);set_bit (ABS_RY , ext->input->absbit);set_bit (ABS_RZ , ext->input->absbit);
Signature · 1 line
1.7.7.3
PATCH v2 13/17 HID: wiimote: Add debugfs support stubs
In reply to David HerrmannAdd initializer and deinitializer for debugfs support. This will later allow raw eeprom access and direct DRM modifications to debug wiimote behaviour and further protocol reverse-engineerings.
--- drivers/hid/Makefile | 3 ++ drivers/hid/hid-wiimote-core.c | 5 +++ drivers/hid/hid-wiimote-debug.c | 52 +++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-wiimote.h | 13 +++++++++ 4 files changed, 73 insertions(+), 0 deletions(-) create mode 100644 drivers/hid/hid-wiimote-debug.c
drivers/hid/Makefile +3 -0
@@ -29,6 +29,9 @@ hid-wiimote-y := hid-wiimote-core.o ifdef CONFIG_HID_WIIMOTE_EXT hid-wiimote-y += hid-wiimote-ext.o endif +ifdef CONFIG_DEBUG_FS + hid-wiimote-y += hid-wiimote-debug.o +endif obj-$(CONFIG_HID_A4TECH) += hid-a4tech.o obj-$(CONFIG_HID_ACRUX) += hid-axff.o
drivers/hid/hid-wiimote-core.c +5 -0
@@ -1161,6 +1161,7 @@ err:static void wiimote_destroy (struct wiimote_data *wdata) { +wiidebug_deinit (wdata);wiiext_deinit (wdata);wiimote_leds_destroy (wdata);
@@ -1237,6 +1238,10 @@ static int wiimote_hid_probe(struct hid_device *hdev,if (ret) goto err_free; + ret =wiidebug_init (wdata); +if (ret) + goto err_free; +hid_info (hdev,"New device registered\n" );/* by default set led1 after device initialization */
drivers/hid/hid-wiimote-debug.c new file +52 -0
52 lines not on this page · raw message
drivers/hid/hid-wiimote.h +13 -0
@@ -74,6 +74,7 @@ struct wiimote_data {struct input_dev *ir;struct power_supply battery;struct wiimote_ext *ext; +struct wiimote_debug *debug;spinlock_t qlock;__u8 head;
@@ -137,6 +138,18 @@ static inline void wiiext_handle(void *u, const __u8 *p) { }#endif +#ifdef CONFIG_DEBUG_FS + +extern int wiidebug_init (struct wiimote_data *wdata); +extern void wiidebug_deinit (struct wiimote_data *wdata); + +#else + +static inline int wiidebug_init (void *u) {return 0 ; } +static inline void wiidebug_deinit (void *u) { } + +#endif +/* requires the state.lock spinlock to be held */ static inline bool wiimote_cmd_pending (struct wiimote_data *wdata,int cmd,__u32 opt)
Signature · 1 line
1.7.7.3
PATCH v2 14/17 HID: wiimote: Allow direct eeprom access
In reply to David HerrmannThe wiimote provides direct access to parts of its eeprom. This implements read support for small chunks of the eeprom. This isn't very fast but prevents the reader from blocking the wiimote stream for too long.
Write support is not yet supported as the wiimote breaks if we overwrite its memory. Use hidraw to reverse-engineer the eeprom before implementing write support here.
--- drivers/hid/hid-wiimote-core.c | 10 +---- drivers/hid/hid-wiimote-debug.c | 76 +++++++++++++++++++++++++++++++++++++++ drivers/hid/hid-wiimote.h | 7 ++++ 3 files changed, 85 insertions(+), 8 deletions(-)
drivers/hid/hid-wiimote-core.c +2 -8
@@ -320,14 +320,8 @@ static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,wiimote_queue (wdata, cmd,sizeof (cmd)); } -#define wiiproto_req_rreg (wdata, os, sz) \ - wiiproto_req_rmem((wdata), false, (os), (sz)) - -#define wiiproto_req_reeprom (wdata, os, sz) \ - wiiproto_req_rmem((wdata), true, (os), (sz)) - -static void wiiproto_req_rmem (struct wiimote_data *wdata,bool eeprom, -__u32 offset,__u16 size) +void wiiproto_req_rmem (struct wiimote_data *wdata,bool eeprom,__u32 offset, +__u16 size) {__u8 cmd[7 ];
drivers/hid/hid-wiimote-debug.c +76 -0
99 lines not on this page · raw message
drivers/hid/hid-wiimote.h +7 -0
@@ -120,6 +120,13 @@ extern int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,extern ssize_t wiimote_cmd_read (struct wiimote_data *wdata,__u32 offset,__u8 *rmem,__u8 size); +#define wiiproto_req_rreg (wdata, os, sz) \ + wiiproto_req_rmem((wdata), false, (os), (sz)) +#define wiiproto_req_reeprom (wdata, os, sz) \ + wiiproto_req_rmem((wdata), true, (os), (sz)) +extern void wiiproto_req_rmem (struct wiimote_data *wdata,bool eeprom, +__u32 offset,__u16 size); +#ifdef CONFIG_HID_WIIMOTE_EXT extern int wiiext_init (struct wiimote_data *wdata);
Signature · 1 line
1.7.7.3
PATCH v2 15/17 HID: wiimote: Allow direct DRM debug access
In reply to David HerrmannKeep track of current drm and add new debugfs file which reads or writes the current DRM.
--- drivers/hid/hid-wiimote-core.c | 2 + drivers/hid/hid-wiimote-debug.c | 107 +++++++++++++++++++++++++++++++++++++-- drivers/hid/hid-wiimote.h | 2 + 3 files changed, 107 insertions(+), 4 deletions(-)
drivers/hid/hid-wiimote-core.c +2 -0
@@ -238,6 +238,7 @@ void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm) cmd[1 ] =0 ; cmd[2 ] = drm; + wdata->state.drm = drm;wiiproto_keep_rumble (wdata, &cmd[1 ]);wiimote_queue (wdata, cmd,sizeof (cmd)); }
@@ -1141,6 +1142,7 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)spin_lock_init (&wdata->state.lock);init_completion (&wdata->state.ready);mutex_init (&wdata->state.sync); + wdata->state.drm =WIIPROTO_REQ_DRM_K ;return wdata;
drivers/hid/hid-wiimote-debug.c +103 -4
147 lines not on this page · raw message
drivers/hid/hid-wiimote.h +2 -0
@@ -52,6 +52,7 @@ struct wiimote_state {spinlock_t lock;__u8 flags;__u8 accel_split[2 ]; +__u8 drm;/* synchronous cmd requests */ struct mutex sync;
@@ -109,6 +110,7 @@ enum wiiproto_reqs {WIIPROTO_REQ_DRM_E =0x3d ,WIIPROTO_REQ_DRM_SKAI1 =0x3e ,WIIPROTO_REQ_DRM_SKAI2 =0x3f , + WIIPROTO_REQ_MAX };#define dev_to_wii (pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
Signature · 1 line
1.7.7.3
PATCH v2 16/17 HID: wiimote: Remove module version number
In reply to David HerrmannThe version number is not needed at all for in-tree drivers. Upstream git is used to track module versions.
---
V2: Remove version number.
drivers/hid/hid-wiimote-core.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
drivers/hid/hid-wiimote-core.c +0 -3
@@ -22,8 +22,6 @@#include "hid-ids.h" #include "hid-wiimote.h" -#define WIIMOTE_VERSION "0.2" -enum wiiproto_keys {WIIPROTO_KEY_LEFT ,WIIPROTO_KEY_RIGHT ,
@@ -1314,4 +1312,3 @@ module_exit(wiimote_exit);MODULE_LICENSE ("GPL" );MODULE_AUTHOR ("David Herrmann <[email protected]>" );MODULE_DESCRIPTION (WIIMOTE_NAME " Device Driver" ); -MODULE_VERSION (WIIMOTE_VERSION );
Signature · 1 line
1.7.7.3
PATCH v2 17/17 HID: wiimote: Enable NO_INIT_REPORTS quirk
In reply to David HerrmannNewer bluetooth stack supports the NO_INIT_REPORTS quirk. The wiimote does not support report initialization so enable it by default.
--- drivers/hid/hid-wiimote-core.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote-core.c +2 -0
@@ -1175,6 +1175,8 @@ static int wiimote_hid_probe(struct hid_device *hdev,struct wiimote_data *wdata;int ret; + hdev->quirks |=HID_QUIRK_NO_INIT_REPORTS ; + wdata =wiimote_create (hdev);if (!wdata) {hid_err (hdev,"Can't alloc device\n" );
Signature · 1 line
1.7.7.3
Quoted 2 lines · David Herrmann
Version 2 of the wiimote extension support removes the module-versioning and adds a comment to the deinitialization function.
David,
thanks a lot for the patch. I have now queued the series for 3.3 in my tree.
Signature · 2 lines
Jiri Kosina SUSE Labs