1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Vibration support for Mega World controllers
5 * Copyright 2022 Frank Zago
7 * Derived from hid-zpff.c:
8 * Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
11 #include <linux/hid.h>
12 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
18 struct mwctrl_device {
19 struct hid_report *report;
24 static int mwctrl_play(struct input_dev *dev, void *data,
25 struct ff_effect *effect)
27 struct hid_device *hid = input_get_drvdata(dev);
28 struct mwctrl_device *mwctrl = data;
30 *mwctrl->strong = effect->u.rumble.strong_magnitude >> 8;
31 *mwctrl->weak = effect->u.rumble.weak_magnitude >> 8;
33 hid_hw_request(hid, mwctrl->report, HID_REQ_SET_REPORT);
38 static int mwctrl_init(struct hid_device *hid)
40 struct mwctrl_device *mwctrl;
41 struct hid_report *report;
42 struct hid_input *hidinput;
43 struct input_dev *dev;
47 if (list_empty(&hid->inputs)) {
48 hid_err(hid, "no inputs found\n");
51 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
52 dev = hidinput->input;
54 for (i = 0; i < 4; i++) {
55 report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, i, 1);
60 mwctrl = kzalloc(sizeof(struct mwctrl_device), GFP_KERNEL);
64 set_bit(FF_RUMBLE, dev->ffbit);
66 error = input_ff_create_memless(dev, mwctrl, mwctrl_play);
72 mwctrl->report = report;
74 /* Field 0 is always 2, and field 1 is always 0. The original
75 * windows driver has a 5 bytes command, where the 5th byte is
76 * a repeat of the 3rd byte, however the device has only 4
77 * fields. It could be a bug in the driver, or there is a
78 * different device that needs it.
80 report->field[0]->value[0] = 0x02;
82 mwctrl->strong = &report->field[2]->value[0];
83 mwctrl->weak = &report->field[3]->value[0];
88 static int mwctrl_probe(struct hid_device *hdev, const struct hid_device_id *id)
92 ret = hid_parse(hdev);
94 hid_err(hdev, "parse failed\n");
98 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
100 hid_err(hdev, "hw start failed\n");
104 ret = mwctrl_init(hdev);
111 static const struct hid_device_id mwctrl_devices[] = {
112 { HID_USB_DEVICE(USB_VENDOR_MEGAWORLD,
113 USB_DEVICE_ID_MEGAWORLD_GAMEPAD) },
116 MODULE_DEVICE_TABLE(hid, mwctrl_devices);
118 static struct hid_driver mwctrl_driver = {
120 .id_table = mwctrl_devices,
121 .probe = mwctrl_probe,
123 module_hid_driver(mwctrl_driver);
125 MODULE_LICENSE("GPL");