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