blob: f7c74d5cb2d927e74a43d5fd3f14e616fc47a3bb [file] [log] [blame]
Lukasz Lubojanski42859e02008-12-11 22:07:59 +01001/*
2 * Force feedback support for GreenAsia (Product ID 0x12) based devices
3 *
4 * The devices are distributed under various names and the same USB device ID
5 * can be used in many game controllers.
6 *
7 *
8 * 0e8f:0012 "GreenAsia Inc. USB Joystick "
9 * - tested with MANTA Warior MM816 and SpeedLink Strike2 SL-6635.
10 *
11 * Copyright (c) 2008 Lukasz Lubojanski <lukasz@lubojanski.info>
12 */
13
14/*
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 */
29
30#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Lukasz Lubojanski42859e02008-12-11 22:07:59 +010032#include <linux/usb.h>
33#include <linux/hid.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040034#include <linux/module.h>
Lukasz Lubojanski42859e02008-12-11 22:07:59 +010035#include "hid-ids.h"
Jiri Kosina0f6f4312009-05-15 15:46:44 +020036
37#ifdef CONFIG_GREENASIA_FF
Lukasz Lubojanski42859e02008-12-11 22:07:59 +010038#include "usbhid/usbhid.h"
39
40struct gaff_device {
41 struct hid_report *report;
42};
43
44static int hid_gaff_play(struct input_dev *dev, void *data,
45 struct ff_effect *effect)
46{
47 struct hid_device *hid = input_get_drvdata(dev);
48 struct gaff_device *gaff = data;
49 int left, right;
50
51 left = effect->u.rumble.strong_magnitude;
52 right = effect->u.rumble.weak_magnitude;
53
54 dbg_hid("called with 0x%04x 0x%04x", left, right);
55
56 left = left * 0xfe / 0xffff;
57 right = right * 0xfe / 0xffff;
58
59 gaff->report->field[0]->value[0] = 0x51;
60 gaff->report->field[0]->value[1] = 0x0;
61 gaff->report->field[0]->value[2] = right;
62 gaff->report->field[0]->value[3] = 0;
63 gaff->report->field[0]->value[4] = left;
64 gaff->report->field[0]->value[5] = 0;
65 dbg_hid("running with 0x%02x 0x%02x", left, right);
66 usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
67
68 gaff->report->field[0]->value[0] = 0xfa;
69 gaff->report->field[0]->value[1] = 0xfe;
70 gaff->report->field[0]->value[2] = 0x0;
71 gaff->report->field[0]->value[4] = 0x0;
72
73 usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
74
75 return 0;
76}
77
78static int gaff_init(struct hid_device *hid)
79{
80 struct gaff_device *gaff;
81 struct hid_report *report;
Alan Sternb3c81632019-10-03 14:53:59 -040082 struct hid_input *hidinput;
Lukasz Lubojanski42859e02008-12-11 22:07:59 +010083 struct list_head *report_list =
84 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
85 struct list_head *report_ptr = report_list;
Alan Sternb3c81632019-10-03 14:53:59 -040086 struct input_dev *dev;
Lukasz Lubojanski42859e02008-12-11 22:07:59 +010087 int error;
88
Alan Sternb3c81632019-10-03 14:53:59 -040089 if (list_empty(&hid->inputs)) {
90 hid_err(hid, "no inputs found\n");
91 return -ENODEV;
92 }
93 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
94 dev = hidinput->input;
95
Lukasz Lubojanski42859e02008-12-11 22:07:59 +010096 if (list_empty(report_list)) {
Joe Perches4291ee32010-12-09 19:29:03 -080097 hid_err(hid, "no output reports found\n");
Lukasz Lubojanski42859e02008-12-11 22:07:59 +010098 return -ENODEV;
99 }
100
101 report_ptr = report_ptr->next;
102
103 report = list_entry(report_ptr, struct hid_report, list);
104 if (report->maxfield < 1) {
Joe Perches4291ee32010-12-09 19:29:03 -0800105 hid_err(hid, "no fields in the report\n");
Lukasz Lubojanski42859e02008-12-11 22:07:59 +0100106 return -ENODEV;
107 }
108
109 if (report->field[0]->report_count < 6) {
Joe Perches4291ee32010-12-09 19:29:03 -0800110 hid_err(hid, "not enough values in the field\n");
Lukasz Lubojanski42859e02008-12-11 22:07:59 +0100111 return -ENODEV;
112 }
113
114 gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL);
115 if (!gaff)
116 return -ENOMEM;
117
118 set_bit(FF_RUMBLE, dev->ffbit);
119
120 error = input_ff_create_memless(dev, gaff, hid_gaff_play);
121 if (error) {
122 kfree(gaff);
123 return error;
124 }
125
126 gaff->report = report;
127 gaff->report->field[0]->value[0] = 0x51;
128 gaff->report->field[0]->value[1] = 0x00;
129 gaff->report->field[0]->value[2] = 0x00;
130 gaff->report->field[0]->value[3] = 0x00;
131 usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
132
133 gaff->report->field[0]->value[0] = 0xfa;
134 gaff->report->field[0]->value[1] = 0xfe;
135
136 usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
137
Joe Perches4291ee32010-12-09 19:29:03 -0800138 hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n");
Lukasz Lubojanski42859e02008-12-11 22:07:59 +0100139
140 return 0;
141}
Jiri Kosina0f6f4312009-05-15 15:46:44 +0200142#else
143static inline int gaff_init(struct hid_device *hdev)
144{
145 return 0;
146}
147#endif
Lukasz Lubojanski42859e02008-12-11 22:07:59 +0100148
149static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id)
150{
151 int ret;
152
153 dev_dbg(&hdev->dev, "Greenasia HID hardware probe...");
154
155 ret = hid_parse(hdev);
156 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800157 hid_err(hdev, "parse failed\n");
Lukasz Lubojanski42859e02008-12-11 22:07:59 +0100158 goto err;
159 }
160
161 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
162 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800163 hid_err(hdev, "hw start failed\n");
Lukasz Lubojanski42859e02008-12-11 22:07:59 +0100164 goto err;
165 }
166
167 gaff_init(hdev);
168
169 return 0;
170err:
171 return ret;
172}
173
174static const struct hid_device_id ga_devices[] = {
175 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012), },
176 { }
177};
178MODULE_DEVICE_TABLE(hid, ga_devices);
179
180static struct hid_driver ga_driver = {
181 .name = "greenasia",
182 .id_table = ga_devices,
183 .probe = ga_probe,
184};
185
186static int __init ga_init(void)
187{
188 return hid_register_driver(&ga_driver);
189}
190
191static void __exit ga_exit(void)
192{
193 hid_unregister_driver(&ga_driver);
194}
195
196module_init(ga_init);
197module_exit(ga_exit);
198MODULE_LICENSE("GPL");