xref: /linux/drivers/hid/hid-gaff.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Force feedback support for GreenAsia (Product ID 0x12) based devices
4  *
5  *  The devices are distributed under various names and the same USB device ID
6  *  can be used in many game controllers.
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  */
16 
17 #include <linux/input.h>
18 #include <linux/slab.h>
19 #include <linux/hid.h>
20 #include <linux/module.h>
21 #include "hid-ids.h"
22 
23 #ifdef CONFIG_GREENASIA_FF
24 
25 struct gaff_device {
26 	struct hid_report *report;
27 };
28 
29 static int hid_gaff_play(struct input_dev *dev, void *data,
30 			 struct ff_effect *effect)
31 {
32 	struct hid_device *hid = input_get_drvdata(dev);
33 	struct gaff_device *gaff = data;
34 	int left, right;
35 
36 	left = effect->u.rumble.strong_magnitude;
37 	right = effect->u.rumble.weak_magnitude;
38 
39 	dbg_hid("called with 0x%04x 0x%04x", left, right);
40 
41 	left = left * 0xfe / 0xffff;
42 	right = right * 0xfe / 0xffff;
43 
44 	gaff->report->field[0]->value[0] = 0x51;
45 	gaff->report->field[0]->value[1] = 0x0;
46 	gaff->report->field[0]->value[2] = right;
47 	gaff->report->field[0]->value[3] = 0;
48 	gaff->report->field[0]->value[4] = left;
49 	gaff->report->field[0]->value[5] = 0;
50 	dbg_hid("running with 0x%02x 0x%02x", left, right);
51 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
52 
53 	gaff->report->field[0]->value[0] = 0xfa;
54 	gaff->report->field[0]->value[1] = 0xfe;
55 	gaff->report->field[0]->value[2] = 0x0;
56 	gaff->report->field[0]->value[4] = 0x0;
57 
58 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
59 
60 	return 0;
61 }
62 
63 static int gaff_input_configured(struct hid_device *hid, struct hid_input *hidinput)
64 {
65 	struct gaff_device *gaff;
66 	struct hid_report *report;
67 	struct list_head *report_list =
68 			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
69 	struct input_dev *dev = hidinput->input;
70 	int error;
71 
72 	if (!list_is_first(&hidinput->list, &hid->inputs))
73 		return 0;
74 
75 	report = list_first_entry_or_null(report_list, struct hid_report, list);
76 	if (!report) {
77 		hid_err(hid, "no output reports found\n");
78 		return -ENODEV;
79 	}
80 	if (report->maxfield < 1) {
81 		hid_err(hid, "no fields in the report\n");
82 		return -ENODEV;
83 	}
84 
85 	if (report->field[0]->report_count < 6) {
86 		hid_err(hid, "not enough values in the field\n");
87 		return -ENODEV;
88 	}
89 
90 	gaff = kzalloc_obj(struct gaff_device);
91 	if (!gaff)
92 		return -ENOMEM;
93 
94 	gaff->report = report;
95 	set_bit(FF_RUMBLE, dev->ffbit);
96 
97 	error = input_ff_create_memless(dev, gaff, hid_gaff_play);
98 	if (error) {
99 		kfree(gaff);
100 		return error;
101 	}
102 
103 	gaff->report->field[0]->value[0] = 0x51;
104 	gaff->report->field[0]->value[1] = 0x00;
105 	gaff->report->field[0]->value[2] = 0x00;
106 	gaff->report->field[0]->value[3] = 0x00;
107 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
108 
109 	gaff->report->field[0]->value[0] = 0xfa;
110 	gaff->report->field[0]->value[1] = 0xfe;
111 
112 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
113 
114 	hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n");
115 
116 	return 0;
117 }
118 #else
119 static inline int gaff_input_configured(struct hid_device *hdev,
120 					struct hid_input *hidinput)
121 {
122 	return 0;
123 }
124 #endif
125 
126 static const struct hid_device_id ga_devices[] = {
127 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012),  },
128 	{ }
129 };
130 MODULE_DEVICE_TABLE(hid, ga_devices);
131 
132 static struct hid_driver ga_driver = {
133 	.name = "greenasia",
134 	.id_table = ga_devices,
135 	.input_configured = gaff_input_configured,
136 };
137 module_hid_driver(ga_driver);
138 
139 MODULE_DESCRIPTION("Force feedback support for GreenAsia (Product ID 0x12) based devices");
140 MODULE_LICENSE("GPL");
141