New wiimote interface
Subsystems (2)
cover New wiimote interface
Hi
As discussed on the ML, this adds a new interface for LED and rumble devices on the wiimote. LED devices can now be controlled via led_classdev structure and the rumble motor is controlled via force-feedback FF_RUMBLE event.
Based on the new semaphore-lock this also simplifies the locking to reduce overhead and code complexity.
The last two patches correctly set DRM modes on new events. Currently, if an extension is plugged into the wiimote, this driver stops working. Those two patches make the driver reset the DRM so it still works with extensions plugged in. Hence, it would be nice to see them in 3.1, too.
I have omitted the accelerometer/IR/extension etc. patches since they probably don't go into 3.1 and I will resend them later after more testing.
Regards
David
David Herrmann (7):
HID: wiimote: Simplify synchronization
HID: wiimote: Correctly call HID open/close callbacks
HID: wiimote: Register led class devices
HID: wiimote: Support rumble device
HID: wiimote: Add force-feedback support
HID: wiimote: Add drm request
HID: wiimote: Add status and return request handlers
drivers/hid/Kconfig | 1 + drivers/hid/hid-wiimote.c | 345 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 276 insertions(+), 70 deletions(-)
Signature · 1 line
1.7.6
PATCH 1/7 HID: wiimote: Simplify synchronization
The new locking scheme in HID core allows us to remove a bit of synchronization. Since the HID layer acts synchronously we simply register input core last and there are no synchonization issues anymore.
Also register sysfs files last to simply the code.
--- drivers/hid/hid-wiimote.c | 77 ++++++++++++++++----------------------------- 1 files changed, 27 insertions(+), 50 deletions(-)
drivers/hid/hid-wiimote.c +27 -50
@@ -33,7 +33,6 @@ struct wiimote_state { };struct wiimote_data { -atomic_t ready;struct hid_device *hdev;struct input_dev *input;
@@ -200,9 +199,6 @@ static ssize_t wiifs_led_show_##num(struct device *dev, \unsigned long flags; \int state; \ \ -if (!atomic_read (&wdata->ready)) \ -return -EBUSY ; \ - \spin_lock_irqsave (&wdata->state.lock, flags); \ state = !!(wdata->state.flags & WIIPROTO_FLAG_LED##num); \spin_unlock_irqrestore (&wdata->state.lock, flags); \
@@ -217,9 +213,6 @@ static ssize_t wiifs_led_set_##num(struct device *dev, \unsigned long flags; \__u8 state; \ \ -if (!atomic_read (&wdata->ready)) \ -return -EBUSY ; \ - \spin_lock_irqsave (&wdata->state.lock, flags); \ \ state = wdata->state.flags; \
@@ -244,13 +237,6 @@ wiifs_led_show_set(4);static int wiimote_input_event (struct input_dev *dev,unsigned int type,unsigned int code,int value) { -struct wiimote_data *wdata =input_get_drvdata (dev); - -if (!atomic_read (&wdata->ready)) -return -EBUSY ; -/* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ -smp_rmb (); -return 0 ; }
@@ -300,11 +286,6 @@ static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,int i;unsigned long flags; -if (!atomic_read (&wdata->ready)) -return -EBUSY ; -/* smp_rmb: Make sure wdata->xy is available when wdata->ready is 1 */ -smp_rmb (); -if (size <1 )return -EINVAL ;
@@ -362,6 +343,15 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)static void wiimote_destroy (struct wiimote_data *wdata) { +device_remove_file (&wdata->hdev->dev, &dev_attr_led1); +device_remove_file (&wdata->hdev->dev, &dev_attr_led2); +device_remove_file (&wdata->hdev->dev, &dev_attr_led3); +device_remove_file (&wdata->hdev->dev, &dev_attr_led4); + +input_unregister_device (wdata->input); +hid_hw_stop (wdata->hdev); + +cancel_work_sync (&wdata->worker);kfree (wdata); }
@@ -377,19 +367,6 @@ static int wiimote_hid_probe(struct hid_device *hdev,return -ENOMEM ; } - ret =device_create_file (&hdev->dev, &dev_attr_led1); -if (ret) - goto err; - ret =device_create_file (&hdev->dev, &dev_attr_led2); -if (ret) - goto err; - ret =device_create_file (&hdev->dev, &dev_attr_led3); -if (ret) - goto err; - ret =device_create_file (&hdev->dev, &dev_attr_led4); -if (ret) - goto err; - ret =hid_parse (hdev);if (ret) {hid_err (hdev,"HID parse failed\n" );
@@ -408,9 +385,19 @@ static int wiimote_hid_probe(struct hid_device *hdev, goto err_stop; } -/* smp_wmb: Write wdata->xy first before wdata->ready is set to 1 */ -smp_wmb (); -atomic_set (&wdata->ready,1 ); + ret =device_create_file (&hdev->dev, &dev_attr_led1); +if (ret) + goto err_free; + ret =device_create_file (&hdev->dev, &dev_attr_led2); +if (ret) + goto err_free; + ret =device_create_file (&hdev->dev, &dev_attr_led3); +if (ret) + goto err_free; + ret =device_create_file (&hdev->dev, &dev_attr_led4); +if (ret) + goto err_free; +hid_info (hdev,"New device registered\n" );/* by default set led1 after device initialization */
@@ -420,15 +407,15 @@ static int wiimote_hid_probe(struct hid_device *hdev,return 0 ; +err_free: +wiimote_destroy (wdata); +return ret; + err_stop:hid_hw_stop (hdev); err:input_free_device (wdata->input); -device_remove_file (&hdev->dev, &dev_attr_led1); -device_remove_file (&hdev->dev, &dev_attr_led2); -device_remove_file (&hdev->dev, &dev_attr_led3); -device_remove_file (&hdev->dev, &dev_attr_led4); -wiimote_destroy (wdata); +kfree (wdata);return ret; }
@@ -437,16 +424,6 @@ static void wiimote_hid_remove(struct hid_device *hdev)struct wiimote_data *wdata =hid_get_drvdata (hdev);hid_info (hdev,"Device removed\n" ); - -device_remove_file (&hdev->dev, &dev_attr_led1); -device_remove_file (&hdev->dev, &dev_attr_led2); -device_remove_file (&hdev->dev, &dev_attr_led3); -device_remove_file (&hdev->dev, &dev_attr_led4); - -hid_hw_stop (hdev); -input_unregister_device (wdata->input); - -cancel_work_sync (&wdata->worker);wiimote_destroy (wdata); }
Signature · 1 line
1.7.6
Re: [PATCH 1/7] HID: wiimote: Simplify synchronization
Am Samstag, 13. August 2011, 15:29:09 schrieb David Herrmann:
Quoted 5 lines
drivers/hid/hid-wiimote.c +1
-0@@ -362,6 +343,15 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)static void wiimote_destroy (struct wiimote_data *wdata) { +device_remove_file (&wdata->hdev->dev, &dev_attr_led1);
drivers/hid/hid-wiimote.c static struct wiimote_data *wiimote_create(struct hid_device *hdev) · line 356
Quoted 10 lines
drivers/hid/hid-wiimote.c +8
-0+device_remove_file (&wdata->hdev->dev, &dev_attr_led2); +device_remove_file (&wdata->hdev->dev, &dev_attr_led3); +device_remove_file (&wdata->hdev->dev, &dev_attr_led4); + +input_unregister_device (wdata->input); +hid_hw_stop (wdata->hdev); + +cancel_work_sync (&wdata->worker);kfree (wdata); }
This looks like the work can run on a device whose hardware has already been stopped. Is this safe?
Regards
Oliver
Re: [PATCH 1/7] HID: wiimote: Simplify synchronization
Quoted 19 lines · Oliver Neukum
Am Samstag, 13. August 2011, 15:29:09 schrieb David Herrmann:
Quoted 5 lines
drivers/hid/hid-wiimote.c +1
-0@@ -362,6 +343,15 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev) static void wiimote_destroy(struct wiimote_data *wdata) { + device_remove_file(&wdata->hdev->dev, &dev_attr_led1);drivers/hid/hid-wiimote.c static struct wiimote_data *wiimote_create(struct hid_device *hdev) · line 356
Quoted 10 lines
drivers/hid/hid-wiimote.c +8
-0+ device_remove_file(&wdata->hdev->dev, &dev_attr_led2); + device_remove_file(&wdata->hdev->dev, &dev_attr_led3); + device_remove_file(&wdata->hdev->dev, &dev_attr_led4); + + input_unregister_device(wdata->input); + hid_hw_stop(wdata->hdev); + + cancel_work_sync(&wdata->worker); kfree(wdata); }This looks like the work can run on a device whose hardware has already been stopped. Is this safe?
With bthid this is safe, although not very nice. usbhid does not allow this so I will swap the cancel_work_sync() and hid_hw_stop() calls so I do not depend on special bthid behaviour.
Nice catch, thanks.
Quoted 3 lines
Regards
Oliver
Regards
David
--
To unsubscribe from this list: send the line "unsubscribe linux-input" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH 2/7 HID: wiimote: Correctly call HID open/close callbacks
In reply to David HerrmannEven though the bluetooth hid backend does not react on open/close callbacks, we should call them to be consistent with other hid drivers.
Also the new input open/close handlers will be used in future to prepare the wiimote device for IR/extension input.
--- drivers/hid/hid-wiimote.c | 16 ++++++++++++++++ 1 files changed, 16 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote.c +16 -0
@@ -240,6 +240,20 @@ static int wiimote_input_event(struct input_dev *dev, unsigned int type,return 0 ; } +static int wiimote_input_open (struct input_dev *dev) +{ +struct wiimote_data *wdata =input_get_drvdata (dev); + +return hid_hw_open (wdata->hdev); +} + +static void wiimote_input_close (struct input_dev *dev) +{ +struct wiimote_data *wdata =input_get_drvdata (dev); + +hid_hw_close (wdata->hdev); +} +static void handler_keys (struct wiimote_data *wdata,const __u8 *payload) { input_report_key(wdata->input, wiiproto_keymap[WIIPROTO_KEY_LEFT],
@@ -322,6 +336,8 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)input_set_drvdata (wdata->input, wdata); wdata->input->event = wiimote_input_event; + wdata->input->open = wiimote_input_open; + wdata->input->close = wiimote_input_close; wdata->input->dev.parent = &wdata->hdev->dev; wdata->input->id.bustype = wdata->hdev->bus; wdata->input->id.vendor = wdata->hdev->vendor;
Signature · 1 line
1.7.6
PATCH 3/7 HID: wiimote: Register led class devices
In reply to David HerrmannThis registers 4 led devices to allow controlling the wiimote leds via standard LED sysfs API.
--- drivers/hid/Kconfig | 1 + drivers/hid/hid-wiimote.c | 130 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 116 insertions(+), 15 deletions(-)
drivers/hid/Kconfig +1 -0
@@ -589,6 +589,7 @@ config HID_WACOM_POWER_SUPPLY config HID_WIIMOTE tristate "Nintendo Wii Remote support" depends on BT_HIDP + depends on LEDS_CLASS ---help--- Support for the Nintendo Wii Remote bluetooth device.
drivers/hid/hid-wiimote.c +115 -15
187 lines not on this page · raw message
Signature · 1 line
1.7.6
PATCH 4/7 HID: wiimote: Support rumble device
In reply to David HerrmannThis adds support for the wiimote's rumble device. Every output report can enable and disable the rumble motor. Hence, every output report must look up our new RUMBLE flag and make sure that it does not unintentionally toggle the rumble motor.
--- drivers/hid/hid-wiimote.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 40 insertions(+), 4 deletions(-)
drivers/hid/hid-wiimote.c +40 -4
@@ -47,10 +47,11 @@ struct wiimote_data {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_LED1 0x01 +#define WIIPROTO_FLAG_LED2 0x02 +#define WIIPROTO_FLAG_LED3 0x04 +#define WIIPROTO_FLAG_LED4 0x08 +#define WIIPROTO_FLAG_RUMBLE 0x10#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
@@ -58,6 +59,7 @@ struct wiimote_data {#define WIIPROTO_FLAG_LED (num) (WIIPROTO_FLAG_LED1 << (num - 1))enum wiiproto_reqs { +WIIPROTO_REQ_RUMBLE =0x10 ,WIIPROTO_REQ_LED =0x11 ,WIIPROTO_REQ_DRM_K =0x30 , };
@@ -172,6 +174,39 @@ static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,spin_unlock_irqrestore (&wdata->qlock, flags); } +/* +* This sets the rumble bit on the given output report if rumble is +* currently enabled. +* \cmd1 must point to the second byte in the output report => &cmd[1] +* This must be called on nearly every output report before passing it +* into the output queue! +*/ +static inline void wiiproto_keep_rumble (struct wiimote_data *wdata,__u8 *cmd1) +{ +if (wdata->state.flags &WIIPROTO_FLAG_RUMBLE ) + *cmd1 |=0x01 ; +} + +static void wiiproto_req_rumble (struct wiimote_data *wdata,__u8 rumble) +{ +__u8 cmd[2 ]; + + rumble = !!rumble; +if (rumble == !!(wdata->state.flags &WIIPROTO_FLAG_RUMBLE )) +return ; + +if (rumble) + wdata->state.flags |=WIIPROTO_FLAG_RUMBLE ; +else + wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE ; + + cmd[0 ] =WIIPROTO_REQ_RUMBLE ; + cmd[1 ] =0 ; + +wiiproto_keep_rumble (wdata, &cmd[1 ]); +wiimote_queue (wdata, cmd,sizeof (cmd)); +} +static void wiiproto_req_leds (struct wiimote_data *wdata,int leds) {__u8 cmd[2 ];
@@ -193,6 +228,7 @@ static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)if (leds &WIIPROTO_FLAG_LED4 ) cmd[1 ] |=0x80 ; +wiiproto_keep_rumble (wdata, &cmd[1 ]);wiimote_queue (wdata, cmd,sizeof (cmd)); }
Signature · 1 line
1.7.6
PATCH 5/7 HID: wiimote: Add force-feedback support
In reply to David HerrmannThe wiimote has a single rumble motor. This adds force feedback support for wiimote devices with FF_RUMBLE. The rumble motor is very simple and only supports an on/off switch so no complex ff-effects are supported.
This also removes the event callback that was registered before but unused. The ff-device overwrites this callback, anyway.
--- drivers/hid/hid-wiimote.c | 39 ++++++++++++++++++++++++++++++++------- 1 files changed, 32 insertions(+), 7 deletions(-)
drivers/hid/hid-wiimote.c +32 -7
@@ -280,6 +280,31 @@ static void wiimote_leds_set(struct led_classdev *led_dev, } } +static int wiimote_ff_play (struct input_dev *dev,void *data, +struct ff_effect *eff) +{ +struct wiimote_data *wdata =input_get_drvdata (dev); +__u8 value; +unsigned long flags; + +/* +* The wiimote supports only a single rumble motor so if any magnitude +* is set to non-zero then we start the rumble motor. If both are set to +* zero, we stop the rumble motor. +*/ + +if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude) + value =1 ; +else + value =0 ; + +spin_lock_irqsave (&wdata->state.lock, flags); +wiiproto_req_rumble (wdata, value); +spin_unlock_irqrestore (&wdata->state.lock, flags); + +return 0 ; +} +#define wiifs_led_show_set (num) \ static ssize_t wiifs_led_show_##num(struct device *dev, \ struct device_attribute *attr, char *buf) \
@@ -312,12 +337,6 @@ wiifs_led_show_set(2);wiifs_led_show_set (3 );wiifs_led_show_set (4 ); -static int wiimote_input_event (struct input_dev *dev,unsigned int type, -unsigned int code,int value) -{ -return 0 ; -} -static int wiimote_input_open (struct input_dev *dev) {struct wiimote_data *wdata =input_get_drvdata (dev);
@@ -465,7 +484,6 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)hid_set_drvdata (hdev, wdata);input_set_drvdata (wdata->input, wdata); - wdata->input->event = wiimote_input_event; wdata->input->open = wiimote_input_open; wdata->input->close = wiimote_input_close; wdata->input->dev.parent = &wdata->hdev->dev;
@@ -479,6 +497,13 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)for (i =0 ; i <WIIPROTO_KEY_COUNT ; ++i)set_bit (wiiproto_keymap[i], wdata->input->keybit); +set_bit (FF_RUMBLE , wdata->input->ffbit); +if (input_ff_create_memless (wdata->input,NULL , wiimote_ff_play)) { +input_free_device (wdata->input); +kfree (wdata); +return NULL ; + } +spin_lock_init (&wdata->qlock);INIT_WORK (&wdata->worker, wiimote_worker);
Signature · 1 line
1.7.6
PATCH 6/7 HID: wiimote: Add drm request
In reply to David HerrmannThe wiimote reports data in several data reporting modes (DRM). The DRM request makes the wiimote send data in the requested drm.
The DRM mode can be set explicitely or can be chosen by the driver. To let the driver choose the DRM mode, pass WIIPROTO_REQ_NULL placeholder to it. This is no valid request and is replaced with an appropriate DRM.
Currently, the driver always sets the basic DRM_K mode, but this will be extended when further peripherals like accelerometer and IR are supported.
--- drivers/hid/hid-wiimote.c | 27 +++++++++++++++++++++++++++ 1 files changed, 27 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote.c +27 -0
@@ -59,8 +59,10 @@ struct wiimote_data {#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_DRM_K =0x30 , };
@@ -232,6 +234,31 @@ static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)wiimote_queue (wdata, cmd,sizeof (cmd)); } +/* +* Check what peripherals of the wiimote are currently +* active and select a proper DRM that supports all of +* the requested data inputs. +*/ +static __u8 select_drm (struct wiimote_data *wdata) +{ +return WIIPROTO_REQ_DRM_K ; +} + +static void wiiproto_req_drm (struct wiimote_data *wdata,__u8 drm) +{ +__u8 cmd[3 ]; + +if (drm ==WIIPROTO_REQ_NULL ) + drm =select_drm (wdata); + + cmd[0 ] =WIIPROTO_REQ_DRM ; + cmd[1 ] =0 ; + cmd[2 ] = drm; + +wiiproto_keep_rumble (wdata, &cmd[1 ]); +wiimote_queue (wdata, cmd,sizeof (cmd)); +} +static enum led_brightness wiimote_leds_get (struct led_classdev *led_dev) {struct wiimote_data *wdata;
Signature · 1 line
1.7.6
PATCH 7/7 HID: wiimote: Add status and return request handlers
In reply to David HerrmannThe wiimote resets the current drm when an extension is plugged in. Fortunately, it also sends a status report in this situation so we just reset the drm on every status report to keep the drm consistent.
Also handle return reports from the wiimote which indicate success and failure of requests that we've sent.
--- drivers/hid/hid-wiimote.c | 24 ++++++++++++++++++++++++ 1 files changed, 24 insertions(+), 0 deletions(-)
drivers/hid/hid-wiimote.c +24 -0
@@ -63,6 +63,8 @@ enum wiiproto_reqs {WIIPROTO_REQ_RUMBLE =0x10 ,WIIPROTO_REQ_LED =0x11 ,WIIPROTO_REQ_DRM =0x12 , +WIIPROTO_REQ_STATUS =0x20 , +WIIPROTO_REQ_RETURN =0x22 , WIIPROTO_REQ_DRM_K = 0x30, };
@@ -405,6 +407,26 @@ static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)input_sync (wdata->input); } +static void handler_status (struct wiimote_data *wdata,const __u8 *payload) +{ +handler_keys (wdata, payload); + +/* on status reports the drm is reset so we need to resend the drm */ +wiiproto_req_drm (wdata,WIIPROTO_REQ_NULL ); +} + +static void handler_return (struct wiimote_data *wdata,const __u8 *payload) +{ +__u8 err = payload[3 ]; +__u8 cmd = payload[2 ]; + +handler_keys (wdata, payload); + +if (err) +hid_warn (wdata->hdev,"Remote error %hhu on req %hhu\n" , err, + cmd); +} +struct wiiproto_handler {__u8 id;size_t size;
@@ -412,6 +434,8 @@ struct wiiproto_handler { };static struct wiiproto_handler handlers[] = { + { .id =WIIPROTO_REQ_STATUS , .size =6 , .func = handler_status }, + { .id =WIIPROTO_REQ_RETURN , .size =4 , .func = handler_return }, { .id =WIIPROTO_REQ_DRM_K , .size =2 , .func = handler_keys }, { .id =0 } };
Signature · 1 line
1.7.6