Commit 23a5a4a3 authored by David Herrmann's avatar David Herrmann Committed by Jiri Kosina

HID: wiimote: Register led class devices

This registers 4 led devices to allow controlling the wiimote leds via standard
LED sysfs API. It removes the four sysfs attributes so we don't have two APIs
for one device.
Signed-off-by: default avatarDavid Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
parent 26af1748
...@@ -589,6 +589,7 @@ config HID_WACOM_POWER_SUPPLY ...@@ -589,6 +589,7 @@ config HID_WACOM_POWER_SUPPLY
config HID_WIIMOTE config HID_WIIMOTE
tristate "Nintendo Wii Remote support" tristate "Nintendo Wii Remote support"
depends on BT_HIDP depends on BT_HIDP
depends on LEDS_CLASS
---help--- ---help---
Support for the Nintendo Wii Remote bluetooth device. Support for the Nintendo Wii Remote bluetooth device.
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/hid.h> #include <linux/hid.h>
#include <linux/input.h> #include <linux/input.h>
#include <linux/leds.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include "hid-ids.h" #include "hid-ids.h"
...@@ -34,6 +35,7 @@ struct wiimote_state { ...@@ -34,6 +35,7 @@ struct wiimote_state {
struct wiimote_data { struct wiimote_data {
struct hid_device *hdev; struct hid_device *hdev;
struct input_dev *input; struct input_dev *input;
struct led_classdev *leds[4];
spinlock_t qlock; spinlock_t qlock;
__u8 head; __u8 head;
...@@ -51,6 +53,9 @@ struct wiimote_data { ...@@ -51,6 +53,9 @@ struct wiimote_data {
#define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \ #define WIIPROTO_FLAGS_LEDS (WIIPROTO_FLAG_LED1 | WIIPROTO_FLAG_LED2 | \
WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4) WIIPROTO_FLAG_LED3 | WIIPROTO_FLAG_LED4)
/* return flag for led \num */
#define WIIPROTO_FLAG_LED(num) (WIIPROTO_FLAG_LED1 << (num - 1))
enum wiiproto_reqs { enum wiiproto_reqs {
WIIPROTO_REQ_LED = 0x11, WIIPROTO_REQ_LED = 0x11,
WIIPROTO_REQ_DRM_K = 0x30, WIIPROTO_REQ_DRM_K = 0x30,
...@@ -85,9 +90,6 @@ static __u16 wiiproto_keymap[] = { ...@@ -85,9 +90,6 @@ static __u16 wiiproto_keymap[] = {
BTN_MODE, /* WIIPROTO_KEY_HOME */ BTN_MODE, /* WIIPROTO_KEY_HOME */
}; };
#define dev_to_wii(pdev) hid_get_drvdata(container_of(pdev, struct hid_device, \
dev))
static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer, static ssize_t wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
size_t count) size_t count)
{ {
...@@ -190,48 +192,53 @@ static void wiiproto_req_leds(struct wiimote_data *wdata, int leds) ...@@ -190,48 +192,53 @@ static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
wiimote_queue(wdata, cmd, sizeof(cmd)); wiimote_queue(wdata, cmd, sizeof(cmd));
} }
#define wiifs_led_show_set(num) \ static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
static ssize_t wiifs_led_show_##num(struct device *dev, \ {
struct device_attribute *attr, char *buf) \ struct wiimote_data *wdata;
{ \ struct device *dev = led_dev->dev->parent;
struct wiimote_data *wdata = dev_to_wii(dev); \ int i;
unsigned long flags; \ unsigned long flags;
int state; \ bool value = false;
\
spin_lock_irqsave(&wdata->state.lock, flags); \ wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
state = !!(wdata->state.flags & WIIPROTO_FLAG_LED##num); \
spin_unlock_irqrestore(&wdata->state.lock, flags); \ for (i = 0; i < 4; ++i) {
\ if (wdata->leds[i] == led_dev) {
return sprintf(buf, "%d\n", state); \ spin_lock_irqsave(&wdata->state.lock, flags);
} \ value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
static ssize_t wiifs_led_set_##num(struct device *dev, \ spin_unlock_irqrestore(&wdata->state.lock, flags);
struct device_attribute *attr, const char *buf, size_t count) \ break;
{ \ }
struct wiimote_data *wdata = dev_to_wii(dev); \ }
int tmp = simple_strtoul(buf, NULL, 10); \
unsigned long flags; \ return value ? LED_FULL : LED_OFF;
__u8 state; \ }
\
spin_lock_irqsave(&wdata->state.lock, flags); \ static void wiimote_leds_set(struct led_classdev *led_dev,
\ enum led_brightness value)
state = wdata->state.flags; \ {
\ struct wiimote_data *wdata;
if (tmp) \ struct device *dev = led_dev->dev->parent;
wiiproto_req_leds(wdata, state | WIIPROTO_FLAG_LED##num);\ int i;
else \ unsigned long flags;
wiiproto_req_leds(wdata, state & ~WIIPROTO_FLAG_LED##num);\ __u8 state, flag;
\
spin_unlock_irqrestore(&wdata->state.lock, flags); \ wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
\
return count; \ for (i = 0; i < 4; ++i) {
} \ if (wdata->leds[i] == led_dev) {
static DEVICE_ATTR(led##num, S_IRUGO | S_IWUSR, wiifs_led_show_##num, \ flag = WIIPROTO_FLAG_LED(i + 1);
wiifs_led_set_##num) spin_lock_irqsave(&wdata->state.lock, flags);
state = wdata->state.flags;
wiifs_led_show_set(1); if (value == LED_OFF)
wiifs_led_show_set(2); wiiproto_req_leds(wdata, state & ~flag);
wiifs_led_show_set(3); else
wiifs_led_show_set(4); wiiproto_req_leds(wdata, state | flag);
spin_unlock_irqrestore(&wdata->state.lock, flags);
break;
}
}
}
static int wiimote_input_event(struct input_dev *dev, unsigned int type, static int wiimote_input_event(struct input_dev *dev, unsigned int type,
unsigned int code, int value) unsigned int code, int value)
...@@ -315,6 +322,58 @@ static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report, ...@@ -315,6 +322,58 @@ static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
return 0; return 0;
} }
static void wiimote_leds_destroy(struct wiimote_data *wdata)
{
int i;
struct led_classdev *led;
for (i = 0; i < 4; ++i) {
if (wdata->leds[i]) {
led = wdata->leds[i];
wdata->leds[i] = NULL;
led_classdev_unregister(led);
kfree(led);
}
}
}
static int wiimote_leds_create(struct wiimote_data *wdata)
{
int i, ret;
struct device *dev = &wdata->hdev->dev;
size_t namesz = strlen(dev_name(dev)) + 9;
struct led_classdev *led;
char *name;
for (i = 0; i < 4; ++i) {
led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
if (!led) {
ret = -ENOMEM;
goto err;
}
name = (void*)&led[1];
snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
led->name = name;
led->brightness = 0;
led->max_brightness = 1;
led->brightness_get = wiimote_leds_get;
led->brightness_set = wiimote_leds_set;
ret = led_classdev_register(dev, led);
if (ret) {
kfree(led);
goto err;
}
wdata->leds[i] = led;
}
return 0;
err:
wiimote_leds_destroy(wdata);
return ret;
}
static struct wiimote_data *wiimote_create(struct hid_device *hdev) static struct wiimote_data *wiimote_create(struct hid_device *hdev)
{ {
struct wiimote_data *wdata; struct wiimote_data *wdata;
...@@ -358,10 +417,7 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev) ...@@ -358,10 +417,7 @@ static struct wiimote_data *wiimote_create(struct hid_device *hdev)
static void wiimote_destroy(struct wiimote_data *wdata) static void wiimote_destroy(struct wiimote_data *wdata)
{ {
device_remove_file(&wdata->hdev->dev, &dev_attr_led1); wiimote_leds_destroy(wdata);
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); input_unregister_device(wdata->input);
cancel_work_sync(&wdata->worker); cancel_work_sync(&wdata->worker);
...@@ -400,16 +456,7 @@ static int wiimote_hid_probe(struct hid_device *hdev, ...@@ -400,16 +456,7 @@ static int wiimote_hid_probe(struct hid_device *hdev,
goto err_stop; goto err_stop;
} }
ret = device_create_file(&hdev->dev, &dev_attr_led1); ret = wiimote_leds_create(wdata);
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) if (ret)
goto err_free; goto err_free;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment