Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Force feedback support for Betop based devices
0004  *
0005  *  The devices are distributed under various names and the same USB device ID
0006  *  can be used in both adapters and actual game controllers.
0007  *
0008  *  0x11c2:0x2208 "BTP2185 BFM mode Joystick"
0009  *   - tested with BTP2185 BFM Mode.
0010  *
0011  *  0x11C0:0x5506 "BTP2185 PC mode Joystick"
0012  *   - tested with BTP2185 PC Mode.
0013  *
0014  *  0x8380:0x1850 "BTP2185 V2 PC mode USB Gamepad"
0015  *   - tested with BTP2185 PC Mode with another version.
0016  *
0017  *  0x20bc:0x5500 "BTP2185 V2 BFM mode Joystick"
0018  *   - tested with BTP2171s.
0019  *  Copyright (c) 2014 Huang Bo <huangbobupt@163.com>
0020  */
0021 
0022 /*
0023  */
0024 
0025 
0026 #include <linux/input.h>
0027 #include <linux/slab.h>
0028 #include <linux/module.h>
0029 #include <linux/hid.h>
0030 
0031 #include "hid-ids.h"
0032 
0033 struct betopff_device {
0034     struct hid_report *report;
0035 };
0036 
0037 static int hid_betopff_play(struct input_dev *dev, void *data,
0038              struct ff_effect *effect)
0039 {
0040     struct hid_device *hid = input_get_drvdata(dev);
0041     struct betopff_device *betopff = data;
0042     __u16 left, right;
0043 
0044     left = effect->u.rumble.strong_magnitude;
0045     right = effect->u.rumble.weak_magnitude;
0046 
0047     betopff->report->field[2]->value[0] = left / 256;
0048     betopff->report->field[3]->value[0] = right / 256;
0049 
0050     hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT);
0051 
0052     return 0;
0053 }
0054 
0055 static int betopff_init(struct hid_device *hid)
0056 {
0057     struct betopff_device *betopff;
0058     struct hid_report *report;
0059     struct hid_input *hidinput;
0060     struct list_head *report_list =
0061             &hid->report_enum[HID_OUTPUT_REPORT].report_list;
0062     struct input_dev *dev;
0063     int field_count = 0;
0064     int error;
0065     int i, j;
0066 
0067     if (list_empty(&hid->inputs)) {
0068         hid_err(hid, "no inputs found\n");
0069         return -ENODEV;
0070     }
0071 
0072     hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
0073     dev = hidinput->input;
0074 
0075     if (list_empty(report_list)) {
0076         hid_err(hid, "no output reports found\n");
0077         return -ENODEV;
0078     }
0079 
0080     report = list_first_entry(report_list, struct hid_report, list);
0081     /*
0082      * Actually there are 4 fields for 4 Bytes as below:
0083      * -----------------------------------------
0084      * Byte0  Byte1  Byte2    Byte3
0085      * 0x00   0x00   left_motor right_motor
0086      * -----------------------------------------
0087      * Do init them with default value.
0088      */
0089     for (i = 0; i < report->maxfield; i++) {
0090         for (j = 0; j < report->field[i]->report_count; j++) {
0091             report->field[i]->value[j] = 0x00;
0092             field_count++;
0093         }
0094     }
0095 
0096     if (field_count < 4) {
0097         hid_err(hid, "not enough fields in the report: %d\n",
0098                 field_count);
0099         return -ENODEV;
0100     }
0101 
0102     betopff = kzalloc(sizeof(*betopff), GFP_KERNEL);
0103     if (!betopff)
0104         return -ENOMEM;
0105 
0106     set_bit(FF_RUMBLE, dev->ffbit);
0107 
0108     error = input_ff_create_memless(dev, betopff, hid_betopff_play);
0109     if (error) {
0110         kfree(betopff);
0111         return error;
0112     }
0113 
0114     betopff->report = report;
0115     hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT);
0116 
0117     hid_info(hid, "Force feedback for betop devices by huangbo <huangbobupt@163.com>\n");
0118 
0119     return 0;
0120 }
0121 
0122 static int betop_probe(struct hid_device *hdev, const struct hid_device_id *id)
0123 {
0124     int ret;
0125 
0126     if (id->driver_data)
0127         hdev->quirks |= HID_QUIRK_MULTI_INPUT;
0128 
0129     ret = hid_parse(hdev);
0130     if (ret) {
0131         hid_err(hdev, "parse failed\n");
0132         goto err;
0133     }
0134 
0135     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
0136     if (ret) {
0137         hid_err(hdev, "hw start failed\n");
0138         goto err;
0139     }
0140 
0141     betopff_init(hdev);
0142 
0143     return 0;
0144 err:
0145     return ret;
0146 }
0147 
0148 static const struct hid_device_id betop_devices[] = {
0149     { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185BFM, 0x2208) },
0150     { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185PC, 0x5506) },
0151     { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2PC, 0x1850) },
0152     { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2BFM, 0x5500) },
0153     { }
0154 };
0155 MODULE_DEVICE_TABLE(hid, betop_devices);
0156 
0157 static struct hid_driver betop_driver = {
0158     .name = "betop",
0159     .id_table = betop_devices,
0160     .probe = betop_probe,
0161 };
0162 module_hid_driver(betop_driver);
0163 
0164 MODULE_LICENSE("GPL");