xref: /linux/drivers/hid/hid-gaff.c (revision 1a59d1b8e05ea6ab45f7e18897de1ef0e6bc3da6)
1*1a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
242859e0bSLukasz Lubojanski /*
342859e0bSLukasz Lubojanski  *  Force feedback support for GreenAsia (Product ID 0x12) based devices
442859e0bSLukasz Lubojanski  *
542859e0bSLukasz Lubojanski  *  The devices are distributed under various names and the same USB device ID
642859e0bSLukasz Lubojanski  *  can be used in many game controllers.
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  */
1642859e0bSLukasz Lubojanski 
1742859e0bSLukasz Lubojanski #include <linux/input.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
1942859e0bSLukasz Lubojanski #include <linux/hid.h>
208f86a2c3SPaul Gortmaker #include <linux/module.h>
2142859e0bSLukasz Lubojanski #include "hid-ids.h"
220f6f4319SJiri Kosina 
230f6f4319SJiri Kosina #ifdef CONFIG_GREENASIA_FF
2442859e0bSLukasz Lubojanski 
2542859e0bSLukasz Lubojanski struct gaff_device {
2642859e0bSLukasz Lubojanski 	struct hid_report *report;
2742859e0bSLukasz Lubojanski };
2842859e0bSLukasz Lubojanski 
2942859e0bSLukasz Lubojanski static int hid_gaff_play(struct input_dev *dev, void *data,
3042859e0bSLukasz Lubojanski 			 struct ff_effect *effect)
3142859e0bSLukasz Lubojanski {
3242859e0bSLukasz Lubojanski 	struct hid_device *hid = input_get_drvdata(dev);
3342859e0bSLukasz Lubojanski 	struct gaff_device *gaff = data;
3442859e0bSLukasz Lubojanski 	int left, right;
3542859e0bSLukasz Lubojanski 
3642859e0bSLukasz Lubojanski 	left = effect->u.rumble.strong_magnitude;
3742859e0bSLukasz Lubojanski 	right = effect->u.rumble.weak_magnitude;
3842859e0bSLukasz Lubojanski 
3942859e0bSLukasz Lubojanski 	dbg_hid("called with 0x%04x 0x%04x", left, right);
4042859e0bSLukasz Lubojanski 
4142859e0bSLukasz Lubojanski 	left = left * 0xfe / 0xffff;
4242859e0bSLukasz Lubojanski 	right = right * 0xfe / 0xffff;
4342859e0bSLukasz Lubojanski 
4442859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0x51;
4542859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0x0;
4642859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[2] = right;
4742859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[3] = 0;
4842859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[4] = left;
4942859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[5] = 0;
5042859e0bSLukasz Lubojanski 	dbg_hid("running with 0x%02x 0x%02x", left, right);
51d8814272SBenjamin Tissoires 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
5242859e0bSLukasz Lubojanski 
5342859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0xfa;
5442859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0xfe;
5542859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[2] = 0x0;
5642859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[4] = 0x0;
5742859e0bSLukasz Lubojanski 
58d8814272SBenjamin Tissoires 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
5942859e0bSLukasz Lubojanski 
6042859e0bSLukasz Lubojanski 	return 0;
6142859e0bSLukasz Lubojanski }
6242859e0bSLukasz Lubojanski 
6342859e0bSLukasz Lubojanski static int gaff_init(struct hid_device *hid)
6442859e0bSLukasz Lubojanski {
6542859e0bSLukasz Lubojanski 	struct gaff_device *gaff;
6642859e0bSLukasz Lubojanski 	struct hid_report *report;
6742859e0bSLukasz Lubojanski 	struct hid_input *hidinput = list_entry(hid->inputs.next,
6842859e0bSLukasz Lubojanski 						struct hid_input, list);
6942859e0bSLukasz Lubojanski 	struct list_head *report_list =
7042859e0bSLukasz Lubojanski 			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
7142859e0bSLukasz Lubojanski 	struct list_head *report_ptr = report_list;
7242859e0bSLukasz Lubojanski 	struct input_dev *dev = hidinput->input;
7342859e0bSLukasz Lubojanski 	int error;
7442859e0bSLukasz Lubojanski 
7542859e0bSLukasz Lubojanski 	if (list_empty(report_list)) {
764291ee30SJoe Perches 		hid_err(hid, "no output reports found\n");
7742859e0bSLukasz Lubojanski 		return -ENODEV;
7842859e0bSLukasz Lubojanski 	}
7942859e0bSLukasz Lubojanski 
8042859e0bSLukasz Lubojanski 	report_ptr = report_ptr->next;
8142859e0bSLukasz Lubojanski 
8242859e0bSLukasz Lubojanski 	report = list_entry(report_ptr, struct hid_report, list);
8342859e0bSLukasz Lubojanski 	if (report->maxfield < 1) {
844291ee30SJoe Perches 		hid_err(hid, "no fields in the report\n");
8542859e0bSLukasz Lubojanski 		return -ENODEV;
8642859e0bSLukasz Lubojanski 	}
8742859e0bSLukasz Lubojanski 
8842859e0bSLukasz Lubojanski 	if (report->field[0]->report_count < 6) {
894291ee30SJoe Perches 		hid_err(hid, "not enough values in the field\n");
9042859e0bSLukasz Lubojanski 		return -ENODEV;
9142859e0bSLukasz Lubojanski 	}
9242859e0bSLukasz Lubojanski 
9342859e0bSLukasz Lubojanski 	gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL);
9442859e0bSLukasz Lubojanski 	if (!gaff)
9542859e0bSLukasz Lubojanski 		return -ENOMEM;
9642859e0bSLukasz Lubojanski 
9742859e0bSLukasz Lubojanski 	set_bit(FF_RUMBLE, dev->ffbit);
9842859e0bSLukasz Lubojanski 
9942859e0bSLukasz Lubojanski 	error = input_ff_create_memless(dev, gaff, hid_gaff_play);
10042859e0bSLukasz Lubojanski 	if (error) {
10142859e0bSLukasz Lubojanski 		kfree(gaff);
10242859e0bSLukasz Lubojanski 		return error;
10342859e0bSLukasz Lubojanski 	}
10442859e0bSLukasz Lubojanski 
10542859e0bSLukasz Lubojanski 	gaff->report = report;
10642859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0x51;
10742859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0x00;
10842859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[2] = 0x00;
10942859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[3] = 0x00;
110d8814272SBenjamin Tissoires 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
11142859e0bSLukasz Lubojanski 
11242859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[0] = 0xfa;
11342859e0bSLukasz Lubojanski 	gaff->report->field[0]->value[1] = 0xfe;
11442859e0bSLukasz Lubojanski 
115d8814272SBenjamin Tissoires 	hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
11642859e0bSLukasz Lubojanski 
1174291ee30SJoe Perches 	hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n");
11842859e0bSLukasz Lubojanski 
11942859e0bSLukasz Lubojanski 	return 0;
12042859e0bSLukasz Lubojanski }
1210f6f4319SJiri Kosina #else
1220f6f4319SJiri Kosina static inline int gaff_init(struct hid_device *hdev)
1230f6f4319SJiri Kosina {
1240f6f4319SJiri Kosina 	return 0;
1250f6f4319SJiri Kosina }
1260f6f4319SJiri Kosina #endif
12742859e0bSLukasz Lubojanski 
12842859e0bSLukasz Lubojanski static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id)
12942859e0bSLukasz Lubojanski {
13042859e0bSLukasz Lubojanski 	int ret;
13142859e0bSLukasz Lubojanski 
13242859e0bSLukasz Lubojanski 	dev_dbg(&hdev->dev, "Greenasia HID hardware probe...");
13342859e0bSLukasz Lubojanski 
13442859e0bSLukasz Lubojanski 	ret = hid_parse(hdev);
13542859e0bSLukasz Lubojanski 	if (ret) {
1364291ee30SJoe Perches 		hid_err(hdev, "parse failed\n");
13742859e0bSLukasz Lubojanski 		goto err;
13842859e0bSLukasz Lubojanski 	}
13942859e0bSLukasz Lubojanski 
14042859e0bSLukasz Lubojanski 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
14142859e0bSLukasz Lubojanski 	if (ret) {
1424291ee30SJoe Perches 		hid_err(hdev, "hw start failed\n");
14342859e0bSLukasz Lubojanski 		goto err;
14442859e0bSLukasz Lubojanski 	}
14542859e0bSLukasz Lubojanski 
14642859e0bSLukasz Lubojanski 	gaff_init(hdev);
14742859e0bSLukasz Lubojanski 
14842859e0bSLukasz Lubojanski 	return 0;
14942859e0bSLukasz Lubojanski err:
15042859e0bSLukasz Lubojanski 	return ret;
15142859e0bSLukasz Lubojanski }
15242859e0bSLukasz Lubojanski 
15342859e0bSLukasz Lubojanski static const struct hid_device_id ga_devices[] = {
15442859e0bSLukasz Lubojanski 	{ HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012),  },
15542859e0bSLukasz Lubojanski 	{ }
15642859e0bSLukasz Lubojanski };
15742859e0bSLukasz Lubojanski MODULE_DEVICE_TABLE(hid, ga_devices);
15842859e0bSLukasz Lubojanski 
15942859e0bSLukasz Lubojanski static struct hid_driver ga_driver = {
16042859e0bSLukasz Lubojanski 	.name = "greenasia",
16142859e0bSLukasz Lubojanski 	.id_table = ga_devices,
16242859e0bSLukasz Lubojanski 	.probe = ga_probe,
16342859e0bSLukasz Lubojanski };
164f425458eSH Hartley Sweeten module_hid_driver(ga_driver);
16542859e0bSLukasz Lubojanski 
16642859e0bSLukasz Lubojanski MODULE_LICENSE("GPL");
167