xref: /linux/drivers/hid/hid-gaff.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
142859e0bSLukasz Lubojanski /*
242859e0bSLukasz Lubojanski  *  Force feedback support for GreenAsia (Product ID 0x12) based devices
342859e0bSLukasz Lubojanski  *
442859e0bSLukasz Lubojanski  *  The devices are distributed under various names and the same USB device ID
542859e0bSLukasz Lubojanski  *  can be used in many game controllers.
642859e0bSLukasz Lubojanski  *
742859e0bSLukasz Lubojanski  *
842859e0bSLukasz Lubojanski  *  0e8f:0012 "GreenAsia Inc.    USB Joystick     "
942859e0bSLukasz Lubojanski  *   - tested with MANTA Warior MM816 and SpeedLink Strike2 SL-6635.
1042859e0bSLukasz Lubojanski  *
1142859e0bSLukasz Lubojanski  *  Copyright (c) 2008 Lukasz Lubojanski <lukasz@lubojanski.info>
1242859e0bSLukasz Lubojanski  */
1342859e0bSLukasz Lubojanski 
1442859e0bSLukasz Lubojanski /*
1542859e0bSLukasz Lubojanski  * This program is free software; you can redistribute it and/or modify
1642859e0bSLukasz Lubojanski  * it under the terms of the GNU General Public License as published by
1742859e0bSLukasz Lubojanski  * the Free Software Foundation; either version 2 of the License, or
1842859e0bSLukasz Lubojanski  * (at your option) any later version.
1942859e0bSLukasz Lubojanski  *
2042859e0bSLukasz Lubojanski  * This program is distributed in the hope that it will be useful,
2142859e0bSLukasz Lubojanski  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2242859e0bSLukasz Lubojanski  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2342859e0bSLukasz Lubojanski  * GNU General Public License for more details.
2442859e0bSLukasz Lubojanski  *
2542859e0bSLukasz Lubojanski  * You should have received a copy of the GNU General Public License
2642859e0bSLukasz Lubojanski  * along with this program; if not, write to the Free Software
2742859e0bSLukasz Lubojanski  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2842859e0bSLukasz Lubojanski  */
2942859e0bSLukasz Lubojanski 
3042859e0bSLukasz Lubojanski #include <linux/input.h>
31*5a0e3ad6STejun Heo #include <linux/slab.h>
3242859e0bSLukasz Lubojanski #include <linux/usb.h>
3342859e0bSLukasz Lubojanski #include <linux/hid.h>
3442859e0bSLukasz Lubojanski #include "hid-ids.h"
350f6f4319SJiri Kosina 
360f6f4319SJiri Kosina #ifdef CONFIG_GREENASIA_FF
3742859e0bSLukasz Lubojanski #include "usbhid/usbhid.h"
3842859e0bSLukasz Lubojanski 
3942859e0bSLukasz Lubojanski struct gaff_device {
4042859e0bSLukasz Lubojanski 	struct hid_report *report;
4142859e0bSLukasz Lubojanski };
4242859e0bSLukasz Lubojanski 
4342859e0bSLukasz Lubojanski static int hid_gaff_play(struct input_dev *dev, void *data,
4442859e0bSLukasz Lubojanski 			 struct ff_effect *effect)
4542859e0bSLukasz Lubojanski {
4642859e0bSLukasz Lubojanski 	struct hid_device *hid = input_get_drvdata(dev);
4742859e0bSLukasz Lubojanski 	struct gaff_device *gaff = data;
4842859e0bSLukasz Lubojanski 	int left, right;
4942859e0bSLukasz Lubojanski 
5042859e0bSLukasz Lubojanski 	left = effect->u.rumble.strong_magnitude;
5142859e0bSLukasz Lubojanski 	right = effect->u.rumble.weak_magnitude;
5242859e0bSLukasz Lubojanski 
5342859e0bSLukasz Lubojanski 	dbg_hid("called with 0x%04x 0x%04x", left, right);
5442859e0bSLukasz Lubojanski 
5542859e0bSLukasz Lubojanski 	left = left * 0xfe / 0xffff;
5642859e0bSLukasz Lubojanski 	right = right * 0xfe / 0xffff;
5742859e0bSLukasz Lubojanski 
5842859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0x51;
5942859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0x0;
6042859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[2] = right;
6142859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[3] = 0;
6242859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[4] = left;
6342859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[5] = 0;
6442859e0bSLukasz Lubojanski 	dbg_hid("running with 0x%02x 0x%02x", left, right);
6542859e0bSLukasz Lubojanski 	usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
6642859e0bSLukasz Lubojanski 
6742859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0xfa;
6842859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0xfe;
6942859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[2] = 0x0;
7042859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[4] = 0x0;
7142859e0bSLukasz Lubojanski 
7242859e0bSLukasz Lubojanski 	usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
7342859e0bSLukasz Lubojanski 
7442859e0bSLukasz Lubojanski 	return 0;
7542859e0bSLukasz Lubojanski }
7642859e0bSLukasz Lubojanski 
7742859e0bSLukasz Lubojanski static int gaff_init(struct hid_device *hid)
7842859e0bSLukasz Lubojanski {
7942859e0bSLukasz Lubojanski 	struct gaff_device *gaff;
8042859e0bSLukasz Lubojanski 	struct hid_report *report;
8142859e0bSLukasz Lubojanski 	struct hid_input *hidinput = list_entry(hid->inputs.next,
8242859e0bSLukasz Lubojanski 						struct hid_input, list);
8342859e0bSLukasz Lubojanski 	struct list_head *report_list =
8442859e0bSLukasz Lubojanski 			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
8542859e0bSLukasz Lubojanski 	struct list_head *report_ptr = report_list;
8642859e0bSLukasz Lubojanski 	struct input_dev *dev = hidinput->input;
8742859e0bSLukasz Lubojanski 	int error;
8842859e0bSLukasz Lubojanski 
8942859e0bSLukasz Lubojanski 	if (list_empty(report_list)) {
9042859e0bSLukasz Lubojanski 		dev_err(&hid->dev, "no output reports found\n");
9142859e0bSLukasz Lubojanski 		return -ENODEV;
9242859e0bSLukasz Lubojanski 	}
9342859e0bSLukasz Lubojanski 
9442859e0bSLukasz Lubojanski 	report_ptr = report_ptr->next;
9542859e0bSLukasz Lubojanski 
9642859e0bSLukasz Lubojanski 	report = list_entry(report_ptr, struct hid_report, list);
9742859e0bSLukasz Lubojanski 	if (report->maxfield < 1) {
9842859e0bSLukasz Lubojanski 		dev_err(&hid->dev, "no fields in the report\n");
9942859e0bSLukasz Lubojanski 		return -ENODEV;
10042859e0bSLukasz Lubojanski 	}
10142859e0bSLukasz Lubojanski 
10242859e0bSLukasz Lubojanski 	if (report->field[0]->report_count < 6) {
10342859e0bSLukasz Lubojanski 		dev_err(&hid->dev, "not enough values in the field\n");
10442859e0bSLukasz Lubojanski 		return -ENODEV;
10542859e0bSLukasz Lubojanski 	}
10642859e0bSLukasz Lubojanski 
10742859e0bSLukasz Lubojanski 	gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL);
10842859e0bSLukasz Lubojanski 	if (!gaff)
10942859e0bSLukasz Lubojanski 		return -ENOMEM;
11042859e0bSLukasz Lubojanski 
11142859e0bSLukasz Lubojanski 	set_bit(FF_RUMBLE, dev->ffbit);
11242859e0bSLukasz Lubojanski 
11342859e0bSLukasz Lubojanski 	error = input_ff_create_memless(dev, gaff, hid_gaff_play);
11442859e0bSLukasz Lubojanski 	if (error) {
11542859e0bSLukasz Lubojanski 		kfree(gaff);
11642859e0bSLukasz Lubojanski 		return error;
11742859e0bSLukasz Lubojanski 	}
11842859e0bSLukasz Lubojanski 
11942859e0bSLukasz Lubojanski 	gaff->report = report;
12042859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0x51;
12142859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0x00;
12242859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[2] = 0x00;
12342859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[3] = 0x00;
12442859e0bSLukasz Lubojanski 	usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
12542859e0bSLukasz Lubojanski 
12642859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0xfa;
12742859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0xfe;
12842859e0bSLukasz Lubojanski 
12942859e0bSLukasz Lubojanski 	usbhid_submit_report(hid, gaff->report, USB_DIR_OUT);
13042859e0bSLukasz Lubojanski 
13142859e0bSLukasz Lubojanski 	dev_info(&hid->dev, "Force Feedback for GreenAsia 0x12"
13242859e0bSLukasz Lubojanski 	       " devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n");
13342859e0bSLukasz Lubojanski 
13442859e0bSLukasz Lubojanski 	return 0;
13542859e0bSLukasz Lubojanski }
1360f6f4319SJiri Kosina #else
1370f6f4319SJiri Kosina static inline int gaff_init(struct hid_device *hdev)
1380f6f4319SJiri Kosina {
1390f6f4319SJiri Kosina 	return 0;
1400f6f4319SJiri Kosina }
1410f6f4319SJiri Kosina #endif
14242859e0bSLukasz Lubojanski 
14342859e0bSLukasz Lubojanski static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id)
14442859e0bSLukasz Lubojanski {
14542859e0bSLukasz Lubojanski 	int ret;
14642859e0bSLukasz Lubojanski 
14742859e0bSLukasz Lubojanski 	dev_dbg(&hdev->dev, "Greenasia HID hardware probe...");
14842859e0bSLukasz Lubojanski 
14942859e0bSLukasz Lubojanski 	ret = hid_parse(hdev);
15042859e0bSLukasz Lubojanski 	if (ret) {
15142859e0bSLukasz Lubojanski 		dev_err(&hdev->dev, "parse failed\n");
15242859e0bSLukasz Lubojanski 		goto err;
15342859e0bSLukasz Lubojanski 	}
15442859e0bSLukasz Lubojanski 
15542859e0bSLukasz Lubojanski 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
15642859e0bSLukasz Lubojanski 	if (ret) {
15742859e0bSLukasz Lubojanski 		dev_err(&hdev->dev, "hw start failed\n");
15842859e0bSLukasz Lubojanski 		goto err;
15942859e0bSLukasz Lubojanski 	}
16042859e0bSLukasz Lubojanski 
16142859e0bSLukasz Lubojanski 	gaff_init(hdev);
16242859e0bSLukasz Lubojanski 
16342859e0bSLukasz Lubojanski 	return 0;
16442859e0bSLukasz Lubojanski err:
16542859e0bSLukasz Lubojanski 	return ret;
16642859e0bSLukasz Lubojanski }
16742859e0bSLukasz Lubojanski 
16842859e0bSLukasz Lubojanski static const struct hid_device_id ga_devices[] = {
16942859e0bSLukasz Lubojanski 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012),  },
17042859e0bSLukasz Lubojanski 	{ }
17142859e0bSLukasz Lubojanski };
17242859e0bSLukasz Lubojanski MODULE_DEVICE_TABLE(hid, ga_devices);
17342859e0bSLukasz Lubojanski 
17442859e0bSLukasz Lubojanski static struct hid_driver ga_driver = {
17542859e0bSLukasz Lubojanski 	.name = "greenasia",
17642859e0bSLukasz Lubojanski 	.id_table = ga_devices,
17742859e0bSLukasz Lubojanski 	.probe = ga_probe,
17842859e0bSLukasz Lubojanski };
17942859e0bSLukasz Lubojanski 
18042859e0bSLukasz Lubojanski static int __init ga_init(void)
18142859e0bSLukasz Lubojanski {
18242859e0bSLukasz Lubojanski 	return hid_register_driver(&ga_driver);
18342859e0bSLukasz Lubojanski }
18442859e0bSLukasz Lubojanski 
18542859e0bSLukasz Lubojanski static void __exit ga_exit(void)
18642859e0bSLukasz Lubojanski {
18742859e0bSLukasz Lubojanski 	hid_unregister_driver(&ga_driver);
18842859e0bSLukasz Lubojanski }
18942859e0bSLukasz Lubojanski 
19042859e0bSLukasz Lubojanski module_init(ga_init);
19142859e0bSLukasz Lubojanski module_exit(ga_exit);
19242859e0bSLukasz Lubojanski MODULE_LICENSE("GPL");
193