Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Vibration support for Mega World controllers
0004  *
0005  * Copyright 2022 Frank Zago
0006  *
0007  * Derived from hid-zpff.c:
0008  *   Copyright (c) 2005, 2006 Anssi Hannula <anssi.hannula@gmail.com>
0009  */
0010 
0011 #include <linux/hid.h>
0012 #include <linux/input.h>
0013 #include <linux/module.h>
0014 #include <linux/slab.h>
0015 
0016 #include "hid-ids.h"
0017 
0018 struct mwctrl_device {
0019     struct hid_report *report;
0020     s32 *weak;
0021     s32 *strong;
0022 };
0023 
0024 static int mwctrl_play(struct input_dev *dev, void *data,
0025                struct ff_effect *effect)
0026 {
0027     struct hid_device *hid = input_get_drvdata(dev);
0028     struct mwctrl_device *mwctrl = data;
0029 
0030     *mwctrl->strong = effect->u.rumble.strong_magnitude >> 8;
0031     *mwctrl->weak = effect->u.rumble.weak_magnitude >> 8;
0032 
0033     hid_hw_request(hid, mwctrl->report, HID_REQ_SET_REPORT);
0034 
0035     return 0;
0036 }
0037 
0038 static int mwctrl_init(struct hid_device *hid)
0039 {
0040     struct mwctrl_device *mwctrl;
0041     struct hid_report *report;
0042     struct hid_input *hidinput;
0043     struct input_dev *dev;
0044     int error;
0045     int i;
0046 
0047     if (list_empty(&hid->inputs)) {
0048         hid_err(hid, "no inputs found\n");
0049         return -ENODEV;
0050     }
0051     hidinput = list_entry(hid->inputs.next, struct hid_input, list);
0052     dev = hidinput->input;
0053 
0054     for (i = 0; i < 4; i++) {
0055         report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, i, 1);
0056         if (!report)
0057             return -ENODEV;
0058     }
0059 
0060     mwctrl = kzalloc(sizeof(struct mwctrl_device), GFP_KERNEL);
0061     if (!mwctrl)
0062         return -ENOMEM;
0063 
0064     set_bit(FF_RUMBLE, dev->ffbit);
0065 
0066     error = input_ff_create_memless(dev, mwctrl, mwctrl_play);
0067     if (error) {
0068         kfree(mwctrl);
0069         return error;
0070     }
0071 
0072     mwctrl->report = report;
0073 
0074     /* Field 0 is always 2, and field 1 is always 0. The original
0075      * windows driver has a 5 bytes command, where the 5th byte is
0076      * a repeat of the 3rd byte, however the device has only 4
0077      * fields. It could be a bug in the driver, or there is a
0078      * different device that needs it.
0079      */
0080     report->field[0]->value[0] = 0x02;
0081 
0082     mwctrl->strong = &report->field[2]->value[0];
0083     mwctrl->weak = &report->field[3]->value[0];
0084 
0085     return 0;
0086 }
0087 
0088 static int mwctrl_probe(struct hid_device *hdev, const struct hid_device_id *id)
0089 {
0090     int ret;
0091 
0092     ret = hid_parse(hdev);
0093     if (ret) {
0094         hid_err(hdev, "parse failed\n");
0095         return ret;
0096     }
0097 
0098     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
0099     if (ret) {
0100         hid_err(hdev, "hw start failed\n");
0101         return ret;
0102     }
0103 
0104     ret = mwctrl_init(hdev);
0105     if (ret)
0106         hid_hw_stop(hdev);
0107 
0108     return ret;
0109 }
0110 
0111 static const struct hid_device_id mwctrl_devices[] = {
0112     { HID_USB_DEVICE(USB_VENDOR_MEGAWORLD,
0113              USB_DEVICE_ID_MEGAWORLD_GAMEPAD) },
0114     { }
0115 };
0116 MODULE_DEVICE_TABLE(hid, mwctrl_devices);
0117 
0118 static struct hid_driver mwctrl_driver = {
0119     .name = "megaworld",
0120     .id_table = mwctrl_devices,
0121     .probe = mwctrl_probe,
0122 };
0123 module_hid_driver(mwctrl_driver);
0124 
0125 MODULE_LICENSE("GPL");