Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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