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/module.h>
44#include <linux/usb.h>
45#include <linux/hid.h>
46
47#include "hid-ids.h"
48
49#ifdef CONFIG_PANTHERLORD_FF
50#include "usbhid/usbhid.h"
51
52struct plff_device {
53 struct hid_report *report;
54 s32 *strong;
55 s32 *weak;
56};
57
58static int hid_plff_play(struct input_dev *dev, void *data,
59 struct ff_effect *effect)
60{
61 struct hid_device *hid = input_get_drvdata(dev);
62 struct plff_device *plff = data;
63 int left, right;
64
65 left = effect->u.rumble.strong_magnitude;
66 right = effect->u.rumble.weak_magnitude;
67 debug("called with 0x%04x 0x%04x", left, right);
68
69 left = left * 0x7f / 0xffff;
70 right = right * 0x7f / 0xffff;
71
72 *plff->strong = left;
73 *plff->weak = right;
74 debug("running with 0x%02x 0x%02x", left, right);
75 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
76
77 return 0;
78}
79
80static int plff_init(struct hid_device *hid)
81{
82 struct plff_device *plff;
83 struct hid_report *report;
84 struct hid_input *hidinput;
85 struct list_head *report_list =
86 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
87 struct list_head *report_ptr = report_list;
88 struct input_dev *dev;
89 int error;
90 s32 *strong;
91 s32 *weak;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 if (list_empty(report_list)) {
107 hid_err(hid, "no output reports found\n");
108 return -ENODEV;
109 }
110
111 list_for_each_entry(hidinput, &hid->inputs, list) {
112
113 report_ptr = report_ptr->next;
114
115 if (report_ptr == report_list) {
116 hid_err(hid, "required output report is missing\n");
117 return -ENODEV;
118 }
119
120 report = list_entry(report_ptr, struct hid_report, list);
121 if (report->maxfield < 1) {
122 hid_err(hid, "no fields in the report\n");
123 return -ENODEV;
124 }
125
126 if (report->field[0]->report_count >= 4) {
127 report->field[0]->value[0] = 0x00;
128 report->field[0]->value[1] = 0x00;
129 strong = &report->field[0]->value[2];
130 weak = &report->field[0]->value[3];
131 debug("detected single-field device");
132 } else if (report->maxfield >= 4 && report->field[0]->maxusage == 1 &&
133 report->field[0]->usage[0].hid == (HID_UP_LED | 0x43)) {
134 report->field[0]->value[0] = 0x00;
135 report->field[1]->value[0] = 0x00;
136 strong = &report->field[2]->value[0];
137 weak = &report->field[3]->value[0];
138 debug("detected 4-field device");
139 } else {
140 hid_err(hid, "not enough fields or values\n");
141 return -ENODEV;
142 }
143
144 plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
145 if (!plff)
146 return -ENOMEM;
147
148 dev = hidinput->input;
149
150 set_bit(FF_RUMBLE, dev->ffbit);
151
152 error = input_ff_create_memless(dev, plff, hid_plff_play);
153 if (error) {
154 kfree(plff);
155 return error;
156 }
157
158 plff->report = report;
159 plff->strong = strong;
160 plff->weak = weak;
161
162 *strong = 0x00;
163 *weak = 0x00;
164 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
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 },
207 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
208 .driver_data = 1 },
209 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
210 { }
211};
212MODULE_DEVICE_TABLE(hid, pl_devices);
213
214static struct hid_driver pl_driver = {
215 .name = "pantherlord",
216 .id_table = pl_devices,
217 .probe = pl_probe,
218};
219
220static int __init pl_init(void)
221{
222 return hid_register_driver(&pl_driver);
223}
224
225static void __exit pl_exit(void)
226{
227 hid_unregister_driver(&pl_driver);
228}
229
230module_init(pl_init);
231module_exit(pl_exit);
232MODULE_LICENSE("GPL");
233