blob: c6d7dbc935b18b2f7445f948f2a6f5f0f0132fab [file] [log] [blame]
Anssi Hannula20eb1272007-01-11 16:51:18 +02001/*
Anssi Hannula5edc41e2007-09-19 16:13:20 +02002 * 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 "
Anssi Hannula27a9c172009-02-27 23:47:00 +020012 * - tested with König Gaming gamepad
Anssi Hannula20eb1272007-01-11 16:51:18 +020013 *
Anssi Hannula27a9c172009-02-27 23:47:00 +020014 * 0e8f:0003 "GASIA USB Gamepad"
15 * - another version of the König gamepad
16 *
17 * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
Anssi Hannula20eb1272007-01-11 16:51:18 +020018 */
19
20/*
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 */
35
36
37/* #define DEBUG */
38
39#define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
40
41#include <linux/input.h>
42#include <linux/usb.h>
43#include <linux/hid.h>
Jiri Slaby5f022292008-09-18 19:43:32 +020044
45#include "hid-ids.h"
46
47#ifdef CONFIG_PANTHERLORD_FF
48#include "usbhid/usbhid.h"
Anssi Hannula20eb1272007-01-11 16:51:18 +020049
50struct plff_device {
51 struct hid_report *report;
Anssi Hannula27a9c172009-02-27 23:47:00 +020052 s32 *strong;
53 s32 *weak;
Anssi Hannula20eb1272007-01-11 16:51:18 +020054};
55
56static int hid_plff_play(struct input_dev *dev, void *data,
57 struct ff_effect *effect)
58{
Dmitry Torokhove0712982007-05-09 10:17:31 +020059 struct hid_device *hid = input_get_drvdata(dev);
Anssi Hannula20eb1272007-01-11 16:51:18 +020060 struct plff_device *plff = data;
61 int left, right;
62
63 left = effect->u.rumble.strong_magnitude;
64 right = effect->u.rumble.weak_magnitude;
65 debug("called with 0x%04x 0x%04x", left, right);
66
67 left = left * 0x7f / 0xffff;
68 right = right * 0x7f / 0xffff;
69
Anssi Hannula27a9c172009-02-27 23:47:00 +020070 *plff->strong = left;
71 *plff->weak = right;
Anssi Hannula20eb1272007-01-11 16:51:18 +020072 debug("running with 0x%02x 0x%02x", left, right);
73 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
74
75 return 0;
76}
77
Jiri Slaby5f022292008-09-18 19:43:32 +020078static int plff_init(struct hid_device *hid)
Anssi Hannula20eb1272007-01-11 16:51:18 +020079{
80 struct plff_device *plff;
81 struct hid_report *report;
82 struct hid_input *hidinput;
83 struct list_head *report_list =
84 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
85 struct list_head *report_ptr = report_list;
86 struct input_dev *dev;
87 int error;
Anssi Hannula27a9c172009-02-27 23:47:00 +020088 s32 *strong;
89 s32 *weak;
Anssi Hannula20eb1272007-01-11 16:51:18 +020090
Anssi Hannula5edc41e2007-09-19 16:13:20 +020091 /* The device contains one output report per physical device, all
92 containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
93 absolute values.
Anssi Hannula20eb1272007-01-11 16:51:18 +020094
Anssi Hannula5edc41e2007-09-19 16:13:20 +020095 The input reports also contain a field which contains
Anssi Hannula20eb1272007-01-11 16:51:18 +020096 8 ff00.0001 usages and 8 boolean values. Their meaning is
Anssi Hannula27a9c172009-02-27 23:47:00 +020097 currently unknown.
98
99 A version of the 0e8f:0003 exists that has all the values in
100 separate fields and misses the extra input field, thus resembling
101 Zeroplus (hid-zpff) devices.
102 */
Anssi Hannula20eb1272007-01-11 16:51:18 +0200103
104 if (list_empty(report_list)) {
Jiri Slaby79575012008-09-18 12:23:34 +0200105 dev_err(&hid->dev, "no output reports found\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200106 return -ENODEV;
107 }
108
109 list_for_each_entry(hidinput, &hid->inputs, list) {
110
111 report_ptr = report_ptr->next;
112
113 if (report_ptr == report_list) {
Jiri Slaby79575012008-09-18 12:23:34 +0200114 dev_err(&hid->dev, "required output report is "
115 "missing\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200116 return -ENODEV;
117 }
118
119 report = list_entry(report_ptr, struct hid_report, list);
120 if (report->maxfield < 1) {
Jiri Slaby79575012008-09-18 12:23:34 +0200121 dev_err(&hid->dev, "no fields in the report\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200122 return -ENODEV;
123 }
124
Anssi Hannula27a9c172009-02-27 23:47:00 +0200125 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 dev_err(&hid->dev, "not enough fields or values\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200140 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;
Anssi Hannula27a9c172009-02-27 23:47:00 +0200158 plff->strong = strong;
159 plff->weak = weak;
160
161 *strong = 0x00;
162 *weak = 0x00;
Anssi Hannula20eb1272007-01-11 16:51:18 +0200163 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
164 }
165
Jiri Slaby79575012008-09-18 12:23:34 +0200166 dev_info(&hid->dev, "Force feedback for PantherLord/GreenAsia "
Anssi Hannula5edc41e2007-09-19 16:13:20 +0200167 "devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200168
169 return 0;
170}
Jiri Slaby5f022292008-09-18 19:43:32 +0200171#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 dev_err(&hdev->dev, "parse failed\n");
188 goto err;
189 }
190
191 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
192 if (ret) {
193 dev_err(&hdev->dev, "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 */
Jiri Kosina578f3a32008-11-20 15:55:38 +0100207 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
208 .driver_data = 1 }, /* Twin USB Joystick */
Anssi Hannula27a9c172009-02-27 23:47:00 +0200209 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
Jiri Slaby5f022292008-09-18 19:43:32 +0200210 { }
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
Peter Huewea24f4232009-07-02 19:08:38 +0200220static int __init pl_init(void)
Jiri Slaby5f022292008-09-18 19:43:32 +0200221{
222 return hid_register_driver(&pl_driver);
223}
224
Peter Huewea24f4232009-07-02 19:08:38 +0200225static void __exit pl_exit(void)
Jiri Slaby5f022292008-09-18 19:43:32 +0200226{
227 hid_unregister_driver(&pl_driver);
228}
229
230module_init(pl_init);
231module_exit(pl_exit);
232MODULE_LICENSE("GPL");