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 ACRUX game controllers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * From what I have gathered, these devices are mass produced in China
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * by several vendors. They often share the same design as the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Xbox 360 controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * 1a34:0802 "ACRUX USB GAMEPAD 8116"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  - tested with an EXEQ EQ-PCU-02090 game controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Copyright (c) 2010 Sergei Kolzun <x0r@dv-life.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #ifdef CONFIG_HID_ACRUX_FF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct axff_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int axff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	struct hid_device *hid = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	struct axff_device *axff = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct hid_report *report = axff->report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	int field_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	int left, right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	left = effect->u.rumble.strong_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	right = effect->u.rumble.weak_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	dbg_hid("called with 0x%04x 0x%04x", left, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	left = left * 0xff / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	right = right * 0xff / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	for (i = 0; i < report->maxfield; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		for (j = 0; j < report->field[i]->report_count; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			report->field[i]->value[j] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 				field_count % 2 ? right : left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			field_count++;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	dbg_hid("running with 0x%02x 0x%02x", left, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int axff_init(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct axff_device *axff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct hid_input *hidinput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int field_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (list_empty(&hid->inputs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		hid_err(hid, "no inputs found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	dev = hidinput->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (list_empty(report_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		hid_err(hid, "no output reports found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	report = list_first_entry(report_list, struct hid_report, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	for (i = 0; i < report->maxfield; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		for (j = 0; j < report->field[i]->report_count; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			report->field[i]->value[j] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			field_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (field_count < 4 && hid->product != 0xf705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		hid_err(hid, "not enough fields in the report: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			field_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	axff = kzalloc(sizeof(struct axff_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (!axff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	set_bit(FF_RUMBLE, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	error = input_ff_create_memless(dev, axff, axff_play);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		goto err_free_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	axff->report = report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) err_free_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	kfree(axff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static inline int axff_init(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	error = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		hid_err(hdev, "parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		hid_err(hdev, "hw start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	error = axff_init(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		 * Do not fail device initialization completely as device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		 * may still be partially operable, just warn.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		hid_warn(hdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			 "Failed to enable force feedback support, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			 error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	 * We need to start polling device right away, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	 * it will go into a coma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	error = hid_hw_open(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		dev_err(&hdev->dev, "hw open failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static void ax_remove(struct hid_device *hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	hid_hw_close(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	hid_hw_stop(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static const struct hid_device_id ax_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	{ HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0xf705), },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) MODULE_DEVICE_TABLE(hid, ax_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static struct hid_driver ax_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	.name		= "acrux",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	.id_table	= ax_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	.probe		= ax_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	.remove		= ax_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) module_hid_driver(ax_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) MODULE_AUTHOR("Sergei Kolzun");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) MODULE_LICENSE("GPL");