Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Force feedback support for PantherLord/GreenAsia 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 * 0810:0001 "Twin USB Joystick"
9 * - tested with PantherLord USB/PS2 2in1 Adapter
10 * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
11 *
12 * 0e8f:0003 "GreenAsia Inc. USB Joystick "
13 * - tested with König Gaming gamepad
14 *
15 * 0e8f:0003 "GASIA USB Gamepad"
16 * - another version of the König gamepad
17 *
18 * 0f30:0111 "Saitek Color Rumble Pad"
19 *
20 * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
21 */
22
23/*
24 */
25
26
27/* #define DEBUG */
28
29#define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
30
31#include <linux/input.h>
32#include <linux/slab.h>
33#include <linux/module.h>
34#include <linux/hid.h>
35
36#include "hid-ids.h"
37
38#ifdef CONFIG_PANTHERLORD_FF
39
40struct plff_device {
41 struct hid_report *report;
42 s32 maxval;
43 s32 *strong;
44 s32 *weak;
45};
46
47static int hid_plff_play(struct input_dev *dev, void *data,
48 struct ff_effect *effect)
49{
50 struct hid_device *hid = input_get_drvdata(dev);
51 struct plff_device *plff = data;
52 int left, right;
53
54 left = effect->u.rumble.strong_magnitude;
55 right = effect->u.rumble.weak_magnitude;
56 debug("called with 0x%04x 0x%04x", left, right);
57
58 left = left * plff->maxval / 0xffff;
59 right = right * plff->maxval / 0xffff;
60
61 *plff->strong = left;
62 *plff->weak = right;
63 debug("running with 0x%02x 0x%02x", left, right);
64 hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
65
66 return 0;
67}
68
69static int plff_init(struct hid_device *hid)
70{
71 struct plff_device *plff;
72 struct hid_report *report;
73 struct hid_input *hidinput;
74 struct list_head *report_list =
75 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
76 struct list_head *report_ptr = report_list;
77 struct input_dev *dev;
78 int error;
79 s32 maxval;
80 s32 *strong;
81 s32 *weak;
82
83 /* The device contains one output report per physical device, all
84 containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
85 absolute values.
86
87 The input reports also contain a field which contains
88 8 ff00.0001 usages and 8 boolean values. Their meaning is
89 currently unknown.
90
91 A version of the 0e8f:0003 exists that has all the values in
92 separate fields and misses the extra input field, thus resembling
93 Zeroplus (hid-zpff) devices.
94 */
95
96 if (list_empty(report_list)) {
97 hid_err(hid, "no output reports found\n");
98 return -ENODEV;
99 }
100
101 list_for_each_entry(hidinput, &hid->inputs, list) {
102
103 report_ptr = report_ptr->next;
104
105 if (report_ptr == report_list) {
106 hid_err(hid, "required output report is missing\n");
107 return -ENODEV;
108 }
109
110 report = list_entry(report_ptr, struct hid_report, list);
111 if (report->maxfield < 1) {
112 hid_err(hid, "no fields in the report\n");
113 return -ENODEV;
114 }
115
116 maxval = 0x7f;
117 if (report->field[0]->report_count >= 4) {
118 report->field[0]->value[0] = 0x00;
119 report->field[0]->value[1] = 0x00;
120 strong = &report->field[0]->value[2];
121 weak = &report->field[0]->value[3];
122 debug("detected single-field device");
123 } else if (report->field[0]->maxusage == 1 &&
124 report->field[0]->usage[0].hid ==
125 (HID_UP_LED | 0x43) &&
126 report->maxfield >= 4 &&
127 report->field[0]->report_count >= 1 &&
128 report->field[1]->report_count >= 1 &&
129 report->field[2]->report_count >= 1 &&
130 report->field[3]->report_count >= 1) {
131 report->field[0]->value[0] = 0x00;
132 report->field[1]->value[0] = 0x00;
133 strong = &report->field[2]->value[0];
134 weak = &report->field[3]->value[0];
135 if (hid->vendor == USB_VENDOR_ID_JESS2)
136 maxval = 0xff;
137 debug("detected 4-field device");
138 } else {
139 hid_err(hid, "not enough fields or values\n");
140 return -ENODEV;
141 }
142
143 plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
144 if (!plff)
145 return -ENOMEM;
146
147 dev = hidinput->input;
148
149 set_bit(FF_RUMBLE, dev->ffbit);
150
151 error = input_ff_create_memless(dev, plff, hid_plff_play);
152 if (error) {
153 kfree(plff);
154 return error;
155 }
156
157 plff->report = report;
158 plff->strong = strong;
159 plff->weak = weak;
160 plff->maxval = maxval;
161
162 *strong = 0x00;
163 *weak = 0x00;
164 hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
165 }
166
167 hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
168
169 return 0;
170}
171#else
172static inline int plff_init(struct hid_device *hid)
173{
174 return 0;
175}
176#endif
177
178static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
179{
180 int ret;
181
182 if (id->driver_data)
183 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
184
185 ret = hid_parse(hdev);
186 if (ret) {
187 hid_err(hdev, "parse failed\n");
188 goto err;
189 }
190
191 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
192 if (ret) {
193 hid_err(hdev, "hw start failed\n");
194 goto err;
195 }
196
197 plff_init(hdev);
198
199 return 0;
200err:
201 return ret;
202}
203
204static const struct hid_device_id pl_devices[] = {
205 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
206 .driver_data = 1 }, /* Twin USB Joystick */
207 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
208 .driver_data = 1 }, /* Twin USB Joystick */
209 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
210 { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD), },
211 { }
212};
213MODULE_DEVICE_TABLE(hid, pl_devices);
214
215static struct hid_driver pl_driver = {
216 .name = "pantherlord",
217 .id_table = pl_devices,
218 .probe = pl_probe,
219};
220module_hid_driver(pl_driver);
221
222MODULE_LICENSE("GPL");
1/*
2 * Force feedback support for PantherLord/GreenAsia based devices
3 *
4 * The devices are distributed under various names and the same USB device ID
5 * can be used in both adapters and actual game controllers.
6 *
7 * 0810:0001 "Twin USB Joystick"
8 * - tested with PantherLord USB/PS2 2in1 Adapter
9 * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
10 *
11 * 0e8f:0003 "GreenAsia Inc. USB Joystick "
12 * - tested with König Gaming gamepad
13 *
14 * 0e8f:0003 "GASIA USB Gamepad"
15 * - another version of the König gamepad
16 *
17 * 0f30:0111 "Saitek Color Rumble Pad"
18 *
19 * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
20 */
21
22/*
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 */
37
38
39/* #define DEBUG */
40
41#define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
42
43#include <linux/input.h>
44#include <linux/slab.h>
45#include <linux/module.h>
46#include <linux/hid.h>
47
48#include "hid-ids.h"
49
50#ifdef CONFIG_PANTHERLORD_FF
51
52struct plff_device {
53 struct hid_report *report;
54 s32 maxval;
55 s32 *strong;
56 s32 *weak;
57};
58
59static int hid_plff_play(struct input_dev *dev, void *data,
60 struct ff_effect *effect)
61{
62 struct hid_device *hid = input_get_drvdata(dev);
63 struct plff_device *plff = data;
64 int left, right;
65
66 left = effect->u.rumble.strong_magnitude;
67 right = effect->u.rumble.weak_magnitude;
68 debug("called with 0x%04x 0x%04x", left, right);
69
70 left = left * plff->maxval / 0xffff;
71 right = right * plff->maxval / 0xffff;
72
73 *plff->strong = left;
74 *plff->weak = right;
75 debug("running with 0x%02x 0x%02x", left, right);
76 hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
77
78 return 0;
79}
80
81static int plff_init(struct hid_device *hid)
82{
83 struct plff_device *plff;
84 struct hid_report *report;
85 struct hid_input *hidinput;
86 struct list_head *report_list =
87 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
88 struct list_head *report_ptr = report_list;
89 struct input_dev *dev;
90 int error;
91 s32 maxval;
92 s32 *strong;
93 s32 *weak;
94
95 /* The device contains one output report per physical device, all
96 containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
97 absolute values.
98
99 The input reports also contain a field which contains
100 8 ff00.0001 usages and 8 boolean values. Their meaning is
101 currently unknown.
102
103 A version of the 0e8f:0003 exists that has all the values in
104 separate fields and misses the extra input field, thus resembling
105 Zeroplus (hid-zpff) devices.
106 */
107
108 if (list_empty(report_list)) {
109 hid_err(hid, "no output reports found\n");
110 return -ENODEV;
111 }
112
113 list_for_each_entry(hidinput, &hid->inputs, list) {
114
115 report_ptr = report_ptr->next;
116
117 if (report_ptr == report_list) {
118 hid_err(hid, "required output report is missing\n");
119 return -ENODEV;
120 }
121
122 report = list_entry(report_ptr, struct hid_report, list);
123 if (report->maxfield < 1) {
124 hid_err(hid, "no fields in the report\n");
125 return -ENODEV;
126 }
127
128 maxval = 0x7f;
129 if (report->field[0]->report_count >= 4) {
130 report->field[0]->value[0] = 0x00;
131 report->field[0]->value[1] = 0x00;
132 strong = &report->field[0]->value[2];
133 weak = &report->field[0]->value[3];
134 debug("detected single-field device");
135 } else if (report->field[0]->maxusage == 1 &&
136 report->field[0]->usage[0].hid ==
137 (HID_UP_LED | 0x43) &&
138 report->maxfield >= 4 &&
139 report->field[0]->report_count >= 1 &&
140 report->field[1]->report_count >= 1 &&
141 report->field[2]->report_count >= 1 &&
142 report->field[3]->report_count >= 1) {
143 report->field[0]->value[0] = 0x00;
144 report->field[1]->value[0] = 0x00;
145 strong = &report->field[2]->value[0];
146 weak = &report->field[3]->value[0];
147 if (hid->vendor == USB_VENDOR_ID_JESS2)
148 maxval = 0xff;
149 debug("detected 4-field device");
150 } else {
151 hid_err(hid, "not enough fields or values\n");
152 return -ENODEV;
153 }
154
155 plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
156 if (!plff)
157 return -ENOMEM;
158
159 dev = hidinput->input;
160
161 set_bit(FF_RUMBLE, dev->ffbit);
162
163 error = input_ff_create_memless(dev, plff, hid_plff_play);
164 if (error) {
165 kfree(plff);
166 return error;
167 }
168
169 plff->report = report;
170 plff->strong = strong;
171 plff->weak = weak;
172 plff->maxval = maxval;
173
174 *strong = 0x00;
175 *weak = 0x00;
176 hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
177 }
178
179 hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
180
181 return 0;
182}
183#else
184static inline int plff_init(struct hid_device *hid)
185{
186 return 0;
187}
188#endif
189
190static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
191{
192 int ret;
193
194 if (id->driver_data)
195 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
196
197 ret = hid_parse(hdev);
198 if (ret) {
199 hid_err(hdev, "parse failed\n");
200 goto err;
201 }
202
203 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
204 if (ret) {
205 hid_err(hdev, "hw start failed\n");
206 goto err;
207 }
208
209 plff_init(hdev);
210
211 return 0;
212err:
213 return ret;
214}
215
216static const struct hid_device_id pl_devices[] = {
217 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
218 .driver_data = 1 }, /* Twin USB Joystick */
219 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
220 .driver_data = 1 }, /* Twin USB Joystick */
221 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
222 { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD), },
223 { }
224};
225MODULE_DEVICE_TABLE(hid, pl_devices);
226
227static struct hid_driver pl_driver = {
228 .name = "pantherlord",
229 .id_table = pl_devices,
230 .probe = pl_probe,
231};
232module_hid_driver(pl_driver);
233
234MODULE_LICENSE("GPL");