Skip to content

Commit 06be0d6

Browse files
bibimbopJiri Kosina
authored andcommitted
HID: Add support for Mega World controller force feedback
This patch adds support for one of the several Mega World USB game controller with integrated force feedback. It is a HID based memory-less game controller, with a weak motor on the left, and a strong one on the right. Signed-off-by: frank zago <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 5e20645 commit 06be0d6

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

drivers/hid/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,14 @@ config HID_MAYFLASH
686686
Say Y here if you have HJZ Mayflash PS3 game controller adapters
687687
and want to enable force feedback support.
688688

689+
config HID_MEGAWORLD_FF
690+
tristate "Mega World based game controller force feedback support"
691+
depends on USB_HID
692+
select INPUT_FF_MEMLESS
693+
help
694+
Say Y here if you have a Mega World based game controller and want
695+
to have force feedback support for it.
696+
689697
config HID_REDRAGON
690698
tristate "Redragon keyboards"
691699
depends on HID

drivers/hid/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ obj-$(CONFIG_HID_MAGICMOUSE) += hid-magicmouse.o
7676
obj-$(CONFIG_HID_MALTRON) += hid-maltron.o
7777
obj-$(CONFIG_HID_MCP2221) += hid-mcp2221.o
7878
obj-$(CONFIG_HID_MAYFLASH) += hid-mf.o
79+
obj-$(CONFIG_HID_MEGAWORLD_FF) += hid-megaworld.o
7980
obj-$(CONFIG_HID_MICROSOFT) += hid-microsoft.o
8081
obj-$(CONFIG_HID_MONTEREY) += hid-monterey.o
8182
obj-$(CONFIG_HID_MULTITOUCH) += hid-multitouch.o

drivers/hid/hid-ids.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,9 @@
868868
#define USB_VENDOR_ID_MCS 0x16d0
869869
#define USB_DEVICE_ID_MCS_GAMEPADBLOCK 0x0bcc
870870

871+
#define USB_VENDOR_MEGAWORLD 0x07b5
872+
#define USB_DEVICE_ID_MEGAWORLD_GAMEPAD 0x0312
873+
871874
#define USB_VENDOR_ID_MGE 0x0463
872875
#define USB_DEVICE_ID_MGE_UPS 0xffff
873876
#define USB_DEVICE_ID_MGE_UPS1 0x0001

drivers/hid/hid-megaworld.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Vibration support for Mega World controllers
4+
*
5+
* Copyright 2022 Frank Zago
6+
*
7+
* Derived from hid-zpff.c:
8+
* Copyright (c) 2005, 2006 Anssi Hannula <[email protected]>
9+
*/
10+
11+
#include <linux/hid.h>
12+
#include <linux/input.h>
13+
#include <linux/module.h>
14+
#include <linux/slab.h>
15+
16+
#include "hid-ids.h"
17+
18+
struct mwctrl_device {
19+
struct hid_report *report;
20+
s32 *weak;
21+
s32 *strong;
22+
};
23+
24+
static int mwctrl_play(struct input_dev *dev, void *data,
25+
struct ff_effect *effect)
26+
{
27+
struct hid_device *hid = input_get_drvdata(dev);
28+
struct mwctrl_device *mwctrl = data;
29+
30+
*mwctrl->strong = effect->u.rumble.strong_magnitude >> 8;
31+
*mwctrl->weak = effect->u.rumble.weak_magnitude >> 8;
32+
33+
hid_hw_request(hid, mwctrl->report, HID_REQ_SET_REPORT);
34+
35+
return 0;
36+
}
37+
38+
static int mwctrl_init(struct hid_device *hid)
39+
{
40+
struct mwctrl_device *mwctrl;
41+
struct hid_report *report;
42+
struct hid_input *hidinput;
43+
struct input_dev *dev;
44+
int error;
45+
int i;
46+
47+
if (list_empty(&hid->inputs)) {
48+
hid_err(hid, "no inputs found\n");
49+
return -ENODEV;
50+
}
51+
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
52+
dev = hidinput->input;
53+
54+
for (i = 0; i < 4; i++) {
55+
report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, i, 1);
56+
if (!report)
57+
return -ENODEV;
58+
}
59+
60+
mwctrl = kzalloc(sizeof(struct mwctrl_device), GFP_KERNEL);
61+
if (!mwctrl)
62+
return -ENOMEM;
63+
64+
set_bit(FF_RUMBLE, dev->ffbit);
65+
66+
error = input_ff_create_memless(dev, mwctrl, mwctrl_play);
67+
if (error) {
68+
kfree(mwctrl);
69+
return error;
70+
}
71+
72+
mwctrl->report = report;
73+
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.
79+
*/
80+
report->field[0]->value[0] = 0x02;
81+
82+
mwctrl->strong = &report->field[2]->value[0];
83+
mwctrl->weak = &report->field[3]->value[0];
84+
85+
return 0;
86+
}
87+
88+
static int mwctrl_probe(struct hid_device *hdev, const struct hid_device_id *id)
89+
{
90+
int ret;
91+
92+
ret = hid_parse(hdev);
93+
if (ret) {
94+
hid_err(hdev, "parse failed\n");
95+
return ret;
96+
}
97+
98+
ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
99+
if (ret) {
100+
hid_err(hdev, "hw start failed\n");
101+
return ret;
102+
}
103+
104+
ret = mwctrl_init(hdev);
105+
if (ret)
106+
hid_hw_stop(hdev);
107+
108+
return ret;
109+
}
110+
111+
static const struct hid_device_id mwctrl_devices[] = {
112+
{ HID_USB_DEVICE(USB_VENDOR_MEGAWORLD,
113+
USB_DEVICE_ID_MEGAWORLD_GAMEPAD) },
114+
{ }
115+
};
116+
MODULE_DEVICE_TABLE(hid, mwctrl_devices);
117+
118+
static struct hid_driver mwctrl_driver = {
119+
.name = "megaworld",
120+
.id_table = mwctrl_devices,
121+
.probe = mwctrl_probe,
122+
};
123+
module_hid_driver(mwctrl_driver);
124+
125+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)