Skip to content

Commit 73c5b25

Browse files
ndreysJiri Kosina
authored andcommitted
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 <dmitry.torokhov@gmail.com> Cc: Jiri Kosina <jikos@kernel.org> Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com> Cc: linux-input@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: Pierre-Loup A. Griffais <pgriffais@valvesoftware.com> Signed-off-by: Juha Kuikka <juha.kuikka@gmail.com> Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent f2d3b62 commit 73c5b25

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

drivers/hid/hid-ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@
800800
#define USB_DEVICE_ID_MS_TOUCH_COVER_2 0x07a7
801801
#define USB_DEVICE_ID_MS_TYPE_COVER_2 0x07a9
802802
#define USB_DEVICE_ID_MS_POWER_COVER 0x07da
803+
#define USB_DEVICE_ID_MS_XBOX_ONE_S_CONTROLLER 0x02fd
803804

804805
#define USB_VENDOR_ID_MOJO 0x8282
805806
#define USB_DEVICE_ID_RETRO_ADAPTER 0x3201

drivers/hid/hid-microsoft.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,36 @@
2929
#define MS_NOGET BIT(4)
3030
#define MS_DUPLICATE_USAGES BIT(5)
3131
#define MS_SURFACE_DIAL BIT(6)
32+
#define MS_QUIRK_FF BIT(7)
3233

3334
struct ms_data {
3435
unsigned long quirks;
36+
struct hid_device *hdev;
37+
struct work_struct ff_worker;
38+
__u8 strong;
39+
__u8 weak;
40+
void *output_report_dmabuf;
3541
};
3642

43+
#define XB1S_FF_REPORT 3
44+
#define ENABLE_WEAK BIT(0)
45+
#define ENABLE_STRONG BIT(1)
46+
47+
enum {
48+
MAGNITUDE_STRONG = 2,
49+
MAGNITUDE_WEAK,
50+
MAGNITUDE_NUM
51+
};
52+
53+
struct xb1s_ff_report {
54+
__u8 report_id;
55+
__u8 enable;
56+
__u8 magnitude[MAGNITUDE_NUM];
57+
__u8 duration_10ms;
58+
__u8 start_delay_10ms;
59+
__u8 loop_count;
60+
} __packed;
61+
3762
static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
3863
unsigned int *rsize)
3964
{
@@ -259,6 +284,84 @@ static int ms_event(struct hid_device *hdev, struct hid_field *field,
259284
return 0;
260285
}
261286

287+
static void ms_ff_worker(struct work_struct *work)
288+
{
289+
struct ms_data *ms = container_of(work, struct ms_data, ff_worker);
290+
struct hid_device *hdev = ms->hdev;
291+
struct xb1s_ff_report *r = ms->output_report_dmabuf;
292+
int ret;
293+
294+
memset(r, 0, sizeof(*r));
295+
296+
r->report_id = XB1S_FF_REPORT;
297+
r->enable = ENABLE_WEAK | ENABLE_STRONG;
298+
/*
299+
* Specifying maximum duration and maximum loop count should
300+
* cover maximum duration of a single effect, which is 65536
301+
* ms
302+
*/
303+
r->duration_10ms = U8_MAX;
304+
r->loop_count = U8_MAX;
305+
r->magnitude[MAGNITUDE_STRONG] = ms->strong; /* left actuator */
306+
r->magnitude[MAGNITUDE_WEAK] = ms->weak; /* right actuator */
307+
308+
ret = hid_hw_output_report(hdev, (__u8 *)r, sizeof(*r));
309+
if (ret)
310+
hid_warn(hdev, "failed to send FF report\n");
311+
}
312+
313+
static int ms_play_effect(struct input_dev *dev, void *data,
314+
struct ff_effect *effect)
315+
{
316+
struct hid_device *hid = input_get_drvdata(dev);
317+
struct ms_data *ms = hid_get_drvdata(hid);
318+
319+
if (effect->type != FF_RUMBLE)
320+
return 0;
321+
322+
/*
323+
* Magnitude is 0..100 so scale the 16-bit input here
324+
*/
325+
ms->strong = ((u32) effect->u.rumble.strong_magnitude * 100) / U16_MAX;
326+
ms->weak = ((u32) effect->u.rumble.weak_magnitude * 100) / U16_MAX;
327+
328+
schedule_work(&ms->ff_worker);
329+
return 0;
330+
}
331+
332+
static int ms_init_ff(struct hid_device *hdev)
333+
{
334+
struct hid_input *hidinput = list_entry(hdev->inputs.next,
335+
struct hid_input, list);
336+
struct input_dev *input_dev = hidinput->input;
337+
struct ms_data *ms = hid_get_drvdata(hdev);
338+
339+
if (!(ms->quirks & MS_QUIRK_FF))
340+
return 0;
341+
342+
ms->hdev = hdev;
343+
INIT_WORK(&ms->ff_worker, ms_ff_worker);
344+
345+
ms->output_report_dmabuf = devm_kzalloc(&hdev->dev,
346+
sizeof(struct xb1s_ff_report),
347+
GFP_KERNEL);
348+
if (ms->output_report_dmabuf == NULL)
349+
return -ENOMEM;
350+
351+
input_set_capability(input_dev, EV_FF, FF_RUMBLE);
352+
return input_ff_create_memless(input_dev, NULL, ms_play_effect);
353+
}
354+
355+
static void ms_remove_ff(struct hid_device *hdev)
356+
{
357+
struct ms_data *ms = hid_get_drvdata(hdev);
358+
359+
if (!(ms->quirks & MS_QUIRK_FF))
360+
return;
361+
362+
cancel_work_sync(&ms->ff_worker);
363+
}
364+
262365
static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
263366
{
264367
unsigned long quirks = id->driver_data;
@@ -292,11 +395,21 @@ static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
292395
goto err_free;
293396
}
294397

398+
ret = ms_init_ff(hdev);
399+
if (ret)
400+
hid_err(hdev, "could not initialize ff, continuing anyway");
401+
295402
return 0;
296403
err_free:
297404
return ret;
298405
}
299406

407+
static void ms_remove(struct hid_device *hdev)
408+
{
409+
hid_hw_stop(hdev);
410+
ms_remove_ff(hdev);
411+
}
412+
300413
static const struct hid_device_id ms_devices[] = {
301414
{ HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
302415
.driver_data = MS_HIDINPUT },
@@ -333,6 +446,8 @@ static const struct hid_device_id ms_devices[] = {
333446
.driver_data = MS_PRESENTER },
334447
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, 0x091B),
335448
.driver_data = MS_SURFACE_DIAL },
449+
{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_XBOX_ONE_S_CONTROLLER),
450+
.driver_data = MS_QUIRK_FF },
336451
{ }
337452
};
338453
MODULE_DEVICE_TABLE(hid, ms_devices);
@@ -345,6 +460,7 @@ static struct hid_driver ms_driver = {
345460
.input_mapped = ms_input_mapped,
346461
.event = ms_event,
347462
.probe = ms_probe,
463+
.remove = ms_remove,
348464
};
349465
module_hid_driver(ms_driver);
350466

0 commit comments

Comments
 (0)