xref: /linux/drivers/hid/hid-megaworld.c (revision 8e1bb4a41aa78d6105e59186af3dcd545fc66e70)
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 <anssi.hannula@gmail.com>
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_DESCRIPTION("Vibration support for Mega World controllers");
126 MODULE_LICENSE("GPL");
127