From f2d3b625dae10cdf14bcfcacb965b02f5c0acc01 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 15 Aug 2018 19:44:09 -0700 Subject: HID: microsoft: Convert private data to be a proper struct In order to be able to have more than just an unsigned long worth of private data, convert the code to allocate and use a dedicated struct. Cc: Dmitry Torokhov Cc: Jiri Kosina Cc: Benjamin Tissoires Cc: linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: Pierre-Loup A. Griffais Signed-off-by: Juha Kuikka Signed-off-by: Andrey Smirnov Signed-off-by: Jiri Kosina --- drivers/hid/hid-microsoft.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/drivers/hid/hid-microsoft.c b/drivers/hid/hid-microsoft.c index 72d983626afd..1cbeeeabd2dd 100644 --- a/drivers/hid/hid-microsoft.c +++ b/drivers/hid/hid-microsoft.c @@ -30,10 +30,15 @@ #define MS_DUPLICATE_USAGES BIT(5) #define MS_SURFACE_DIAL BIT(6) +struct ms_data { + unsigned long quirks; +}; + static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc, unsigned int *rsize) { - unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); + struct ms_data *ms = hid_get_drvdata(hdev); + unsigned long quirks = ms->quirks; /* * Microsoft Wireless Desktop Receiver (Model 1028) has @@ -159,7 +164,8 @@ static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi, struct hid_field *field, struct hid_usage *usage, unsigned long **bit, int *max) { - unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); + struct ms_data *ms = hid_get_drvdata(hdev); + unsigned long quirks = ms->quirks; if (quirks & MS_ERGONOMY) { int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max); @@ -185,7 +191,8 @@ static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi, struct hid_field *field, struct hid_usage *usage, unsigned long **bit, int *max) { - unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); + struct ms_data *ms = hid_get_drvdata(hdev); + unsigned long quirks = ms->quirks; if (quirks & MS_DUPLICATE_USAGES) clear_bit(usage->code, *bit); @@ -196,7 +203,8 @@ static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi, static int ms_event(struct hid_device *hdev, struct hid_field *field, struct hid_usage *usage, __s32 value) { - unsigned long quirks = (unsigned long)hid_get_drvdata(hdev); + struct ms_data *ms = hid_get_drvdata(hdev); + unsigned long quirks = ms->quirks; struct input_dev *input; if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput || @@ -254,9 +262,16 @@ static int ms_event(struct hid_device *hdev, struct hid_field *field, static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id) { unsigned long quirks = id->driver_data; + struct ms_data *ms; int ret; - hid_set_drvdata(hdev, (void *)quirks); + ms = devm_kzalloc(&hdev->dev, sizeof(*ms), GFP_KERNEL); + if (ms == NULL) + return -ENOMEM; + + ms->quirks = quirks; + + hid_set_drvdata(hdev, ms); if (quirks & MS_NOGET) hdev->quirks |= HID_QUIRK_NOGET; -- cgit v1.2.3 From 73c5b254c36529c84c9d19e07905f7103bb32e79 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 15 Aug 2018 19:44:10 -0700 Subject: HID: microsoft: Add rumble support for Xbox One S controller Add HID quirk driver for Xbox One S controller over bluetooth. This driver only adds support for rumble. Standard controller functionality is exposed by default HID driver. [jkosina@suse.cz: straightforward rebase on more recent driver code] Cc: Dmitry Torokhov Cc: Jiri Kosina Cc: Benjamin Tissoires Cc: linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: Pierre-Loup A. Griffais Signed-off-by: Juha Kuikka Signed-off-by: Andrey Smirnov Signed-off-by: Jiri Kosina --- drivers/hid/hid-ids.h | 1 + drivers/hid/hid-microsoft.c | 116 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index 79bdf0c7e351..a523d54c4f75 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -800,6 +800,7 @@ #define USB_DEVICE_ID_MS_TOUCH_COVER_2 0x07a7 #define USB_DEVICE_ID_MS_TYPE_COVER_2 0x07a9 #define USB_DEVICE_ID_MS_POWER_COVER 0x07da +#define USB_DEVICE_ID_MS_XBOX_ONE_S_CONTROLLER 0x02fd #define USB_VENDOR_ID_MOJO 0x8282 #define USB_DEVICE_ID_RETRO_ADAPTER 0x3201 diff --git a/drivers/hid/hid-microsoft.c b/drivers/hid/hid-microsoft.c index 1cbeeeabd2dd..330cb073cb66 100644 --- a/drivers/hid/hid-microsoft.c +++ b/drivers/hid/hid-microsoft.c @@ -29,11 +29,36 @@ #define MS_NOGET BIT(4) #define MS_DUPLICATE_USAGES BIT(5) #define MS_SURFACE_DIAL BIT(6) +#define MS_QUIRK_FF BIT(7) struct ms_data { unsigned long quirks; + struct hid_device *hdev; + struct work_struct ff_worker; + __u8 strong; + __u8 weak; + void *output_report_dmabuf; }; +#define XB1S_FF_REPORT 3 +#define ENABLE_WEAK BIT(0) +#define ENABLE_STRONG BIT(1) + +enum { + MAGNITUDE_STRONG = 2, + MAGNITUDE_WEAK, + MAGNITUDE_NUM +}; + +struct xb1s_ff_report { + __u8 report_id; + __u8 enable; + __u8 magnitude[MAGNITUDE_NUM]; + __u8 duration_10ms; + __u8 start_delay_10ms; + __u8 loop_count; +} __packed; + static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc, unsigned int *rsize) { @@ -259,6 +284,84 @@ static int ms_event(struct hid_device *hdev, struct hid_field *field, return 0; } +static void ms_ff_worker(struct work_struct *work) +{ + struct ms_data *ms = container_of(work, struct ms_data, ff_worker); + struct hid_device *hdev = ms->hdev; + struct xb1s_ff_report *r = ms->output_report_dmabuf; + int ret; + + memset(r, 0, sizeof(*r)); + + r->report_id = XB1S_FF_REPORT; + r->enable = ENABLE_WEAK | ENABLE_STRONG; + /* + * Specifying maximum duration and maximum loop count should + * cover maximum duration of a single effect, which is 65536 + * ms + */ + r->duration_10ms = U8_MAX; + r->loop_count = U8_MAX; + r->magnitude[MAGNITUDE_STRONG] = ms->strong; /* left actuator */ + r->magnitude[MAGNITUDE_WEAK] = ms->weak; /* right actuator */ + + ret = hid_hw_output_report(hdev, (__u8 *)r, sizeof(*r)); + if (ret) + hid_warn(hdev, "failed to send FF report\n"); +} + +static int ms_play_effect(struct input_dev *dev, void *data, + struct ff_effect *effect) +{ + struct hid_device *hid = input_get_drvdata(dev); + struct ms_data *ms = hid_get_drvdata(hid); + + if (effect->type != FF_RUMBLE) + return 0; + + /* + * Magnitude is 0..100 so scale the 16-bit input here + */ + ms->strong = ((u32) effect->u.rumble.strong_magnitude * 100) / U16_MAX; + ms->weak = ((u32) effect->u.rumble.weak_magnitude * 100) / U16_MAX; + + schedule_work(&ms->ff_worker); + return 0; +} + +static int ms_init_ff(struct hid_device *hdev) +{ + struct hid_input *hidinput = list_entry(hdev->inputs.next, + struct hid_input, list); + struct input_dev *input_dev = hidinput->input; + struct ms_data *ms = hid_get_drvdata(hdev); + + if (!(ms->quirks & MS_QUIRK_FF)) + return 0; + + ms->hdev = hdev; + INIT_WORK(&ms->ff_worker, ms_ff_worker); + + ms->output_report_dmabuf = devm_kzalloc(&hdev->dev, + sizeof(struct xb1s_ff_report), + GFP_KERNEL); + if (ms->output_report_dmabuf == NULL) + return -ENOMEM; + + input_set_capability(input_dev, EV_FF, FF_RUMBLE); + return input_ff_create_memless(input_dev, NULL, ms_play_effect); +} + +static void ms_remove_ff(struct hid_device *hdev) +{ + struct ms_data *ms = hid_get_drvdata(hdev); + + if (!(ms->quirks & MS_QUIRK_FF)) + return; + + cancel_work_sync(&ms->ff_worker); +} + static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id) { unsigned long quirks = id->driver_data; @@ -292,11 +395,21 @@ static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id) goto err_free; } + ret = ms_init_ff(hdev); + if (ret) + hid_err(hdev, "could not initialize ff, continuing anyway"); + return 0; err_free: return ret; } +static void ms_remove(struct hid_device *hdev) +{ + hid_hw_stop(hdev); + ms_remove_ff(hdev); +} + static const struct hid_device_id ms_devices[] = { { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV), .driver_data = MS_HIDINPUT }, @@ -333,6 +446,8 @@ static const struct hid_device_id ms_devices[] = { .driver_data = MS_PRESENTER }, { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, 0x091B), .driver_data = MS_SURFACE_DIAL }, + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_XBOX_ONE_S_CONTROLLER), + .driver_data = MS_QUIRK_FF }, { } }; MODULE_DEVICE_TABLE(hid, ms_devices); @@ -345,6 +460,7 @@ static struct hid_driver ms_driver = { .input_mapped = ms_input_mapped, .event = ms_event, .probe = ms_probe, + .remove = ms_remove, }; module_hid_driver(ms_driver); -- cgit v1.2.3 From 830e82aa48f32518dfa5335c0134f611d4bab4b2 Mon Sep 17 00:00:00 2001 From: Jiri Kosina Date: Wed, 5 Sep 2018 13:24:47 +0200 Subject: HID: microsoft: the driver now neeed MEMLESS_FF infrastructure Reflect that fact in Kconfig, the same we do for other such drivers. Reported-by: kbuild test robot Signed-off-by: Jiri Kosina --- drivers/hid/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 61e1953ff921..e5ec47705fa2 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -596,6 +596,7 @@ config HID_MICROSOFT tristate "Microsoft non-fully HID-compliant devices" depends on HID default !EXPERT + select INPUT_FF_MEMLESS ---help--- Support for Microsoft devices that are not fully compliant with HID standard. -- cgit v1.2.3