1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
40
41#include <linux/input.h>
42#include <linux/slab.h>
43#include <linux/usb.h>
44#include <linux/hid.h>
45
46#include "hid-ids.h"
47
48#ifdef CONFIG_PANTHERLORD_FF
49#include "usbhid/usbhid.h"
50
51struct plff_device {
52 struct hid_report *report;
53 s32 *strong;
54 s32 *weak;
55};
56
57static int hid_plff_play(struct input_dev *dev, void *data,
58 struct ff_effect *effect)
59{
60 struct hid_device *hid = input_get_drvdata(dev);
61 struct plff_device *plff = data;
62 int left, right;
63
64 left = effect->u.rumble.strong_magnitude;
65 right = effect->u.rumble.weak_magnitude;
66 debug("called with 0x%04x 0x%04x", left, right);
67
68 left = left * 0x7f / 0xffff;
69 right = right * 0x7f / 0xffff;
70
71 *plff->strong = left;
72 *plff->weak = right;
73 debug("running with 0x%02x 0x%02x", left, right);
74 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
75
76 return 0;
77}
78
79static int plff_init(struct hid_device *hid)
80{
81 struct plff_device *plff;
82 struct hid_report *report;
83 struct hid_input *hidinput;
84 struct list_head *report_list =
85 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
86 struct list_head *report_ptr = report_list;
87 struct input_dev *dev;
88 int error;
89 s32 *strong;
90 s32 *weak;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 if (list_empty(report_list)) {
106 hid_err(hid, "no output reports found\n");
107 return -ENODEV;
108 }
109
110 list_for_each_entry(hidinput, &hid->inputs, list) {
111
112 report_ptr = report_ptr->next;
113
114 if (report_ptr == report_list) {
115 hid_err(hid, "required output report is missing\n");
116 return -ENODEV;
117 }
118
119 report = list_entry(report_ptr, struct hid_report, list);
120 if (report->maxfield < 1) {
121 hid_err(hid, "no fields in the report\n");
122 return -ENODEV;
123 }
124
125 if (report->field[0]->report_count >= 4) {
126 report->field[0]->value[0] = 0x00;
127 report->field[0]->value[1] = 0x00;
128 strong = &report->field[0]->value[2];
129 weak = &report->field[0]->value[3];
130 debug("detected single-field device");
131 } else if (report->maxfield >= 4 && report->field[0]->maxusage == 1 &&
132 report->field[0]->usage[0].hid == (HID_UP_LED | 0x43)) {
133 report->field[0]->value[0] = 0x00;
134 report->field[1]->value[0] = 0x00;
135 strong = &report->field[2]->value[0];
136 weak = &report->field[3]->value[0];
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
161 *strong = 0x00;
162 *weak = 0x00;
163 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
164 }
165
166 hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
167
168 return 0;
169}
170#else
171static inline int plff_init(struct hid_device *hid)
172{
173 return 0;
174}
175#endif
176
177static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
178{
179 int ret;
180
181 if (id->driver_data)
182 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
183
184 ret = hid_parse(hdev);
185 if (ret) {
186 hid_err(hdev, "parse failed\n");
187 goto err;
188 }
189
190 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
191 if (ret) {
192 hid_err(hdev, "hw start failed\n");
193 goto err;
194 }
195
196 plff_init(hdev);
197
198 return 0;
199err:
200 return ret;
201}
202
203static const struct hid_device_id pl_devices[] = {
204 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
205 .driver_data = 1 },
206 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
207 .driver_data = 1 },
208 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
209 { }
210};
211MODULE_DEVICE_TABLE(hid, pl_devices);
212
213static struct hid_driver pl_driver = {
214 .name = "pantherlord",
215 .id_table = pl_devices,
216 .probe = pl_probe,
217};
218
219static int __init pl_init(void)
220{
221 return hid_register_driver(&pl_driver);
222}
223
224static void __exit pl_exit(void)
225{
226 hid_unregister_driver(&pl_driver);
227}
228
229module_init(pl_init);
230module_exit(pl_exit);
231MODULE_LICENSE("GPL");
232