HID: prodikeys: make array keys static const, makes object smaller
[linux-block.git] / drivers / hid / hid-axff.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
c0dbcc33
SK
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"
b55ebc27 10 * - tested with an EXEQ EQ-PCU-02090 game controller.
c0dbcc33
SK
11 *
12 * Copyright (c) 2010 Sergei Kolzun <x0r@dv-life.ru>
13 */
14
15/*
c0dbcc33
SK
16 */
17
18#include <linux/input.h>
19#include <linux/slab.h>
c0dbcc33 20#include <linux/hid.h>
8f86a2c3 21#include <linux/module.h>
c0dbcc33
SK
22
23#include "hid-ids.h"
0ae43810
DT
24
25#ifdef CONFIG_HID_ACRUX_FF
c0dbcc33
SK
26
27struct axff_device {
28 struct hid_report *report;
29};
30
31static 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;
b55ebc27
SK
35 struct hid_report *report = axff->report;
36 int field_count = 0;
c0dbcc33 37 int left, right;
b55ebc27 38 int i, j;
c0dbcc33
SK
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
b55ebc27
SK
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
c0dbcc33 56 dbg_hid("running with 0x%02x 0x%02x", left, right);
d8814272 57 hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
c0dbcc33
SK
58
59 return 0;
60}
61
62static int axff_init(struct hid_device *hid)
63{
64 struct axff_device *axff;
65 struct hid_report *report;
66 struct hid_input *hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
67 struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
68 struct input_dev *dev = hidinput->input;
b55ebc27
SK
69 int field_count = 0;
70 int i, j;
c0dbcc33
SK
71 int error;
72
73 if (list_empty(report_list)) {
4291ee30 74 hid_err(hid, "no output reports found\n");
c0dbcc33
SK
75 return -ENODEV;
76 }
77
78 report = list_first_entry(report_list, struct hid_report, list);
b55ebc27
SK
79 for (i = 0; i < report->maxfield; i++) {
80 for (j = 0; j < report->field[i]->report_count; j++) {
81 report->field[i]->value[j] = 0x00;
82 field_count++;
83 }
84 }
c0dbcc33 85
e17f5d76 86 if (field_count < 4 && hid->product != 0xf705) {
b55ebc27
SK
87 hid_err(hid, "not enough fields in the report: %d\n",
88 field_count);
c0dbcc33
SK
89 return -ENODEV;
90 }
91
92 axff = kzalloc(sizeof(struct axff_device), GFP_KERNEL);
93 if (!axff)
94 return -ENOMEM;
95
96 set_bit(FF_RUMBLE, dev->ffbit);
97
98 error = input_ff_create_memless(dev, axff, axff_play);
99 if (error)
100 goto err_free_mem;
101
102 axff->report = report;
d8814272 103 hid_hw_request(hid, axff->report, HID_REQ_SET_REPORT);
c0dbcc33 104
b55ebc27 105 hid_info(hid, "Force Feedback for ACRUX game controllers by Sergei Kolzun <x0r@dv-life.ru>\n");
c0dbcc33
SK
106
107 return 0;
108
109err_free_mem:
110 kfree(axff);
111 return error;
112}
0ae43810
DT
113#else
114static inline int axff_init(struct hid_device *hid)
115{
116 return 0;
117}
118#endif
c0dbcc33
SK
119
120static int ax_probe(struct hid_device *hdev, const struct hid_device_id *id)
121{
122 int error;
123
4291ee30 124 dev_dbg(&hdev->dev, "ACRUX HID hardware probe...\n");
c0dbcc33
SK
125
126 error = hid_parse(hdev);
127 if (error) {
4291ee30 128 hid_err(hdev, "parse failed\n");
c0dbcc33
SK
129 return error;
130 }
131
132 error = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
133 if (error) {
4291ee30 134 hid_err(hdev, "hw start failed\n");
c0dbcc33
SK
135 return error;
136 }
137
138 error = axff_init(hdev);
139 if (error) {
140 /*
141 * Do not fail device initialization completely as device
142 * may still be partially operable, just warn.
143 */
4291ee30 144 hid_warn(hdev,
c0dbcc33
SK
145 "Failed to enable force feedback support, error: %d\n",
146 error);
147 }
148
0ae43810
DT
149 /*
150 * We need to start polling device right away, otherwise
151 * it will go into a coma.
152 */
153 error = hid_hw_open(hdev);
154 if (error) {
155 dev_err(&hdev->dev, "hw open failed\n");
b30d89d1 156 hid_hw_stop(hdev);
0ae43810
DT
157 return error;
158 }
159
c0dbcc33
SK
160 return 0;
161}
162
0ae43810
DT
163static void ax_remove(struct hid_device *hdev)
164{
165 hid_hw_close(hdev);
166 hid_hw_stop(hdev);
167}
168
c0dbcc33
SK
169static const struct hid_device_id ax_devices[] = {
170 { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0x0802), },
e17f5d76 171 { HID_USB_DEVICE(USB_VENDOR_ID_ACRUX, 0xf705), },
c0dbcc33
SK
172 { }
173};
174MODULE_DEVICE_TABLE(hid, ax_devices);
175
176static struct hid_driver ax_driver = {
0ae43810
DT
177 .name = "acrux",
178 .id_table = ax_devices,
179 .probe = ax_probe,
180 .remove = ax_remove,
c0dbcc33 181};
f425458e 182module_hid_driver(ax_driver);
c0dbcc33
SK
183
184MODULE_AUTHOR("Sergei Kolzun");
185MODULE_DESCRIPTION("Force feedback support for ACRUX game controllers");
186MODULE_LICENSE("GPL");