1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Force feedback support for Betop based devices 4 * 5 * The devices are distributed under various names and the same USB device ID 6 * can be used in both adapters and actual game controllers. 7 * 8 * 0x11c2:0x2208 "BTP2185 BFM mode Joystick" 9 * - tested with BTP2185 BFM Mode. 10 * 11 * 0x11C0:0x5506 "BTP2185 PC mode Joystick" 12 * - tested with BTP2185 PC Mode. 13 * 14 * 0x8380:0x1850 "BTP2185 V2 PC mode USB Gamepad" 15 * - tested with BTP2185 PC Mode with another version. 16 * 17 * 0x20bc:0x5500 "BTP2185 V2 BFM mode Joystick" 18 * - tested with BTP2171s. 19 * Copyright (c) 2014 Huang Bo <huangbobupt@163.com> 20 */ 21 22 /* 23 */ 24 25 26 #include <linux/input.h> 27 #include <linux/slab.h> 28 #include <linux/module.h> 29 #include <linux/hid.h> 30 31 #include "hid-ids.h" 32 33 struct betopff_device { 34 struct hid_report *report; 35 }; 36 37 static int hid_betopff_play(struct input_dev *dev, void *data, 38 struct ff_effect *effect) 39 { 40 struct hid_device *hid = input_get_drvdata(dev); 41 struct betopff_device *betopff = data; 42 __u16 left, right; 43 44 left = effect->u.rumble.strong_magnitude; 45 right = effect->u.rumble.weak_magnitude; 46 47 betopff->report->field[2]->value[0] = left / 256; 48 betopff->report->field[3]->value[0] = right / 256; 49 50 hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT); 51 52 return 0; 53 } 54 55 static int betop_input_configured(struct hid_device *hid, struct hid_input *hidinput) 56 { 57 struct betopff_device *betopff; 58 struct hid_report *report; 59 struct list_head *report_list = 60 &hid->report_enum[HID_OUTPUT_REPORT].report_list; 61 struct input_dev *dev = hidinput->input; 62 int error; 63 int i, j; 64 65 if (!list_is_first(&hidinput->list, &hid->inputs)) 66 return 0; 67 68 report = list_first_entry_or_null(report_list, struct hid_report, list); 69 if (!report) { 70 hid_err(hid, "no output reports found\n"); 71 return -ENODEV; 72 } 73 /* 74 * Actually there are 4 fields for 4 Bytes as below: 75 * ----------------------------------------- 76 * Byte0 Byte1 Byte2 Byte3 77 * 0x00 0x00 left_motor right_motor 78 * ----------------------------------------- 79 * Do init them with default value. 80 */ 81 if (report->maxfield < 4) { 82 hid_err(hid, "not enough fields in the report: %d\n", 83 report->maxfield); 84 return -ENODEV; 85 } 86 for (i = 0; i < report->maxfield; i++) { 87 if (report->field[i]->report_count < 1) { 88 hid_err(hid, "no values in the field\n"); 89 return -ENODEV; 90 } 91 for (j = 0; j < report->field[i]->report_count; j++) { 92 report->field[i]->value[j] = 0x00; 93 } 94 } 95 96 betopff = kzalloc_obj(*betopff); 97 if (!betopff) 98 return -ENOMEM; 99 100 betopff->report = report; 101 set_bit(FF_RUMBLE, dev->ffbit); 102 103 error = input_ff_create_memless(dev, betopff, hid_betopff_play); 104 if (error) { 105 kfree(betopff); 106 return error; 107 } 108 109 hid_hw_request(hid, betopff->report, HID_REQ_SET_REPORT); 110 111 hid_info(hid, "Force feedback for betop devices by huangbo <huangbobupt@163.com>\n"); 112 113 return 0; 114 } 115 116 static int betop_probe(struct hid_device *hdev, const struct hid_device_id *id) 117 { 118 int ret; 119 120 if (id->driver_data) 121 hdev->quirks |= HID_QUIRK_MULTI_INPUT; 122 123 ret = hid_parse(hdev); 124 if (ret) { 125 hid_err(hdev, "parse failed\n"); 126 return ret; 127 } 128 129 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 130 if (ret) { 131 hid_err(hdev, "hw start failed\n"); 132 return ret; 133 } 134 return 0; 135 } 136 137 static const struct hid_device_id betop_devices[] = { 138 { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185BFM, 0x2208) }, 139 { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185PC, 0x5506) }, 140 { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2PC, 0x1850) }, 141 { HID_USB_DEVICE(USB_VENDOR_ID_BETOP_2185V2BFM, 0x5500) }, 142 { } 143 }; 144 MODULE_DEVICE_TABLE(hid, betop_devices); 145 146 static struct hid_driver betop_driver = { 147 .name = "betop", 148 .id_table = betop_devices, 149 .probe = betop_probe, 150 .input_configured = betop_input_configured, 151 }; 152 module_hid_driver(betop_driver); 153 154 MODULE_DESCRIPTION("Force feedback support for Betop based devices"); 155 MODULE_LICENSE("GPL"); 156