blob: 00cd2f8bc0406b3bd41f149da0e03ed45302aff8 [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 "
Al Virod36b6912011-12-29 17:09:01 -050012 * - 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"
Al Virod36b6912011-12-29 17:09:01 -050015 * - another version of the König gamepad
Anssi Hannula27a9c172009-02-27 23:47:00 +020016 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040043#include <linux/module.h>
Anssi Hannula20eb1272007-01-11 16:51:18 +020044#include <linux/usb.h>
45#include <linux/hid.h>
Jiri Slaby5f022292008-09-18 19:43:32 +020046
47#include "hid-ids.h"
48
49#ifdef CONFIG_PANTHERLORD_FF
50#include "usbhid/usbhid.h"
Anssi Hannula20eb1272007-01-11 16:51:18 +020051
52struct plff_device {
53 struct hid_report *report;
Anssi Hannula27a9c172009-02-27 23:47:00 +020054 s32 *strong;
55 s32 *weak;
Anssi Hannula20eb1272007-01-11 16:51:18 +020056};
57
58static int hid_plff_play(struct input_dev *dev, void *data,
59 struct ff_effect *effect)
60{
Dmitry Torokhove0712982007-05-09 10:17:31 +020061 struct hid_device *hid = input_get_drvdata(dev);
Anssi Hannula20eb1272007-01-11 16:51:18 +020062 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
Anssi Hannula27a9c172009-02-27 23:47:00 +020072 *plff->strong = left;
73 *plff->weak = right;
Anssi Hannula20eb1272007-01-11 16:51:18 +020074 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
Jiri Slaby5f022292008-09-18 19:43:32 +020080static int plff_init(struct hid_device *hid)
Anssi Hannula20eb1272007-01-11 16:51:18 +020081{
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;
Anssi Hannula27a9c172009-02-27 23:47:00 +020090 s32 *strong;
91 s32 *weak;
Anssi Hannula20eb1272007-01-11 16:51:18 +020092
Anssi Hannula5edc41e2007-09-19 16:13:20 +020093 /* The device contains one output report per physical device, all
94 containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
95 absolute values.
Anssi Hannula20eb1272007-01-11 16:51:18 +020096
Anssi Hannula5edc41e2007-09-19 16:13:20 +020097 The input reports also contain a field which contains
Anssi Hannula20eb1272007-01-11 16:51:18 +020098 8 ff00.0001 usages and 8 boolean values. Their meaning is
Anssi Hannula27a9c172009-02-27 23:47:00 +020099 currently unknown.
100
101 A version of the 0e8f:0003 exists that has all the values in
102 separate fields and misses the extra input field, thus resembling
103 Zeroplus (hid-zpff) devices.
104 */
Anssi Hannula20eb1272007-01-11 16:51:18 +0200105
106 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -0800107 hid_err(hid, "no output reports found\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200108 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) {
Joe Perches4291ee32010-12-09 19:29:03 -0800116 hid_err(hid, "required output report is missing\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200117 return -ENODEV;
118 }
119
120 report = list_entry(report_ptr, struct hid_report, list);
121 if (report->maxfield < 1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800122 hid_err(hid, "no fields in the report\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200123 return -ENODEV;
124 }
125
Anssi Hannula27a9c172009-02-27 23:47:00 +0200126 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");
Kees Cook2f6f54f2013-08-28 22:30:49 +0200132 } else if (report->field[0]->maxusage == 1 &&
133 report->field[0]->usage[0].hid ==
134 (HID_UP_LED | 0x43) &&
135 report->maxfield >= 4 &&
136 report->field[0]->report_count >= 1 &&
137 report->field[1]->report_count >= 1 &&
138 report->field[2]->report_count >= 1 &&
139 report->field[3]->report_count >= 1) {
Anssi Hannula27a9c172009-02-27 23:47:00 +0200140 report->field[0]->value[0] = 0x00;
141 report->field[1]->value[0] = 0x00;
142 strong = &report->field[2]->value[0];
143 weak = &report->field[3]->value[0];
144 debug("detected 4-field device");
145 } else {
Joe Perches4291ee32010-12-09 19:29:03 -0800146 hid_err(hid, "not enough fields or values\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200147 return -ENODEV;
148 }
149
150 plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
151 if (!plff)
152 return -ENOMEM;
153
154 dev = hidinput->input;
155
156 set_bit(FF_RUMBLE, dev->ffbit);
157
158 error = input_ff_create_memless(dev, plff, hid_plff_play);
159 if (error) {
160 kfree(plff);
161 return error;
162 }
163
164 plff->report = report;
Anssi Hannula27a9c172009-02-27 23:47:00 +0200165 plff->strong = strong;
166 plff->weak = weak;
167
168 *strong = 0x00;
169 *weak = 0x00;
Anssi Hannula20eb1272007-01-11 16:51:18 +0200170 usbhid_submit_report(hid, plff->report, USB_DIR_OUT);
171 }
172
Joe Perches4291ee32010-12-09 19:29:03 -0800173 hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
Anssi Hannula20eb1272007-01-11 16:51:18 +0200174
175 return 0;
176}
Jiri Slaby5f022292008-09-18 19:43:32 +0200177#else
178static inline int plff_init(struct hid_device *hid)
179{
180 return 0;
181}
182#endif
183
184static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
185{
186 int ret;
187
188 if (id->driver_data)
189 hdev->quirks |= HID_QUIRK_MULTI_INPUT;
190
191 ret = hid_parse(hdev);
192 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800193 hid_err(hdev, "parse failed\n");
Jiri Slaby5f022292008-09-18 19:43:32 +0200194 goto err;
195 }
196
197 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
198 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800199 hid_err(hdev, "hw start failed\n");
Jiri Slaby5f022292008-09-18 19:43:32 +0200200 goto err;
201 }
202
203 plff_init(hdev);
204
205 return 0;
206err:
207 return ret;
208}
209
210static const struct hid_device_id pl_devices[] = {
211 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
212 .driver_data = 1 }, /* Twin USB Joystick */
Jiri Kosina578f3a32008-11-20 15:55:38 +0100213 { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
214 .driver_data = 1 }, /* Twin USB Joystick */
Anssi Hannula27a9c172009-02-27 23:47:00 +0200215 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
Jiri Slaby5f022292008-09-18 19:43:32 +0200216 { }
217};
218MODULE_DEVICE_TABLE(hid, pl_devices);
219
220static struct hid_driver pl_driver = {
221 .name = "pantherlord",
222 .id_table = pl_devices,
223 .probe = pl_probe,
224};
225
Peter Huewea24f4232009-07-02 19:08:38 +0200226static int __init pl_init(void)
Jiri Slaby5f022292008-09-18 19:43:32 +0200227{
228 return hid_register_driver(&pl_driver);
229}
230
Peter Huewea24f4232009-07-02 19:08:38 +0200231static void __exit pl_exit(void)
Jiri Slaby5f022292008-09-18 19:43:32 +0200232{
233 hid_unregister_driver(&pl_driver);
234}
235
236module_init(pl_init);
237module_exit(pl_exit);
238MODULE_LICENSE("GPL");