Merge tag '6.10-rc6-smb3-server-fixes' of git://git.samba.org/ksmbd
[linux.git] / drivers / hid / hid-megaworld.c
blob599657863cb9a432daade21cd1c5a8b1b2736693
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
9 */
11 #include <linux/hid.h>
12 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include "hid-ids.h"
18 struct mwctrl_device {
19 struct hid_report *report;
20 s32 *weak;
21 s32 *strong;
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);
35 return 0;
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;
44 int error;
45 int i;
47 if (list_empty(&hid->inputs)) {
48 hid_err(hid, "no inputs found\n");
49 return -ENODEV;
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);
56 if (!report)
57 return -ENODEV;
60 mwctrl = kzalloc(sizeof(struct mwctrl_device), GFP_KERNEL);
61 if (!mwctrl)
62 return -ENOMEM;
64 set_bit(FF_RUMBLE, dev->ffbit);
66 error = input_ff_create_memless(dev, mwctrl, mwctrl_play);
67 if (error) {
68 kfree(mwctrl);
69 return error;
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];
85 return 0;
88 static int mwctrl_probe(struct hid_device *hdev, const struct hid_device_id *id)
90 int ret;
92 ret = hid_parse(hdev);
93 if (ret) {
94 hid_err(hdev, "parse failed\n");
95 return ret;
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;
104 ret = mwctrl_init(hdev);
105 if (ret)
106 hid_hw_stop(hdev);
108 return ret;
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 = {
119 .name = "megaworld",
120 .id_table = mwctrl_devices,
121 .probe = mwctrl_probe,
123 module_hid_driver(mwctrl_driver);
125 MODULE_LICENSE("GPL");