11a59d1b8SThomas 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
hid_gaff_play(struct input_dev * dev,void * data,struct ff_effect * effect)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
gaff_init(struct hid_device * hid)6342859e0bSLukasz Lubojanski static int gaff_init(struct hid_device *hid)
6442859e0bSLukasz Lubojanski {
6542859e0bSLukasz Lubojanski struct gaff_device *gaff;
6642859e0bSLukasz Lubojanski struct hid_report *report;
67d9d4b1e4SAlan Stern struct hid_input *hidinput;
6842859e0bSLukasz Lubojanski struct list_head *report_list =
6942859e0bSLukasz Lubojanski &hid->report_enum[HID_OUTPUT_REPORT].report_list;
7042859e0bSLukasz Lubojanski struct list_head *report_ptr = report_list;
71d9d4b1e4SAlan Stern struct input_dev *dev;
7242859e0bSLukasz Lubojanski int error;
7342859e0bSLukasz Lubojanski
74d9d4b1e4SAlan Stern if (list_empty(&hid->inputs)) {
75d9d4b1e4SAlan Stern hid_err(hid, "no inputs found\n");
76d9d4b1e4SAlan Stern return -ENODEV;
77d9d4b1e4SAlan Stern }
78d9d4b1e4SAlan Stern hidinput = list_entry(hid->inputs.next, struct hid_input, list);
79d9d4b1e4SAlan Stern dev = hidinput->input;
80d9d4b1e4SAlan Stern
8142859e0bSLukasz Lubojanski if (list_empty(report_list)) {
824291ee30SJoe Perches hid_err(hid, "no output reports found\n");
8342859e0bSLukasz Lubojanski return -ENODEV;
8442859e0bSLukasz Lubojanski }
8542859e0bSLukasz Lubojanski
8642859e0bSLukasz Lubojanski report_ptr = report_ptr->next;
8742859e0bSLukasz Lubojanski
8842859e0bSLukasz Lubojanski report = list_entry(report_ptr, struct hid_report, list);
8942859e0bSLukasz Lubojanski if (report->maxfield < 1) {
904291ee30SJoe Perches hid_err(hid, "no fields in the report\n");
9142859e0bSLukasz Lubojanski return -ENODEV;
9242859e0bSLukasz Lubojanski }
9342859e0bSLukasz Lubojanski
9442859e0bSLukasz Lubojanski if (report->field[0]->report_count < 6) {
954291ee30SJoe Perches hid_err(hid, "not enough values in the field\n");
9642859e0bSLukasz Lubojanski return -ENODEV;
9742859e0bSLukasz Lubojanski }
9842859e0bSLukasz Lubojanski
9942859e0bSLukasz Lubojanski gaff = kzalloc(sizeof(struct gaff_device), GFP_KERNEL);
10042859e0bSLukasz Lubojanski if (!gaff)
10142859e0bSLukasz Lubojanski return -ENOMEM;
10242859e0bSLukasz Lubojanski
10342859e0bSLukasz Lubojanski set_bit(FF_RUMBLE, dev->ffbit);
10442859e0bSLukasz Lubojanski
10542859e0bSLukasz Lubojanski error = input_ff_create_memless(dev, gaff, hid_gaff_play);
10642859e0bSLukasz Lubojanski if (error) {
10742859e0bSLukasz Lubojanski kfree(gaff);
10842859e0bSLukasz Lubojanski return error;
10942859e0bSLukasz Lubojanski }
11042859e0bSLukasz Lubojanski
11142859e0bSLukasz Lubojanski gaff->report = report;
11242859e0bSLukasz Lubojanski gaff->report->field[0]->value[0] = 0x51;
11342859e0bSLukasz Lubojanski gaff->report->field[0]->value[1] = 0x00;
11442859e0bSLukasz Lubojanski gaff->report->field[0]->value[2] = 0x00;
11542859e0bSLukasz Lubojanski gaff->report->field[0]->value[3] = 0x00;
116d8814272SBenjamin Tissoires hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
11742859e0bSLukasz Lubojanski
11842859e0bSLukasz Lubojanski gaff->report->field[0]->value[0] = 0xfa;
11942859e0bSLukasz Lubojanski gaff->report->field[0]->value[1] = 0xfe;
12042859e0bSLukasz Lubojanski
121d8814272SBenjamin Tissoires hid_hw_request(hid, gaff->report, HID_REQ_SET_REPORT);
12242859e0bSLukasz Lubojanski
1234291ee30SJoe Perches hid_info(hid, "Force Feedback for GreenAsia 0x12 devices by Lukasz Lubojanski <lukasz@lubojanski.info>\n");
12442859e0bSLukasz Lubojanski
12542859e0bSLukasz Lubojanski return 0;
12642859e0bSLukasz Lubojanski }
1270f6f4319SJiri Kosina #else
gaff_init(struct hid_device * hdev)1280f6f4319SJiri Kosina static inline int gaff_init(struct hid_device *hdev)
1290f6f4319SJiri Kosina {
1300f6f4319SJiri Kosina return 0;
1310f6f4319SJiri Kosina }
1320f6f4319SJiri Kosina #endif
13342859e0bSLukasz Lubojanski
ga_probe(struct hid_device * hdev,const struct hid_device_id * id)13442859e0bSLukasz Lubojanski static int ga_probe(struct hid_device *hdev, const struct hid_device_id *id)
13542859e0bSLukasz Lubojanski {
13642859e0bSLukasz Lubojanski int ret;
13742859e0bSLukasz Lubojanski
13842859e0bSLukasz Lubojanski dev_dbg(&hdev->dev, "Greenasia HID hardware probe...");
13942859e0bSLukasz Lubojanski
14042859e0bSLukasz Lubojanski ret = hid_parse(hdev);
14142859e0bSLukasz Lubojanski if (ret) {
1424291ee30SJoe Perches hid_err(hdev, "parse failed\n");
14342859e0bSLukasz Lubojanski goto err;
14442859e0bSLukasz Lubojanski }
14542859e0bSLukasz Lubojanski
14642859e0bSLukasz Lubojanski ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
14742859e0bSLukasz Lubojanski if (ret) {
1484291ee30SJoe Perches hid_err(hdev, "hw start failed\n");
14942859e0bSLukasz Lubojanski goto err;
15042859e0bSLukasz Lubojanski }
15142859e0bSLukasz Lubojanski
15242859e0bSLukasz Lubojanski gaff_init(hdev);
15342859e0bSLukasz Lubojanski
15442859e0bSLukasz Lubojanski return 0;
15542859e0bSLukasz Lubojanski err:
15642859e0bSLukasz Lubojanski return ret;
15742859e0bSLukasz Lubojanski }
15842859e0bSLukasz Lubojanski
15942859e0bSLukasz Lubojanski static const struct hid_device_id ga_devices[] = {
16042859e0bSLukasz Lubojanski { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012), },
16142859e0bSLukasz Lubojanski { }
16242859e0bSLukasz Lubojanski };
16342859e0bSLukasz Lubojanski MODULE_DEVICE_TABLE(hid, ga_devices);
16442859e0bSLukasz Lubojanski
16542859e0bSLukasz Lubojanski static struct hid_driver ga_driver = {
16642859e0bSLukasz Lubojanski .name = "greenasia",
16742859e0bSLukasz Lubojanski .id_table = ga_devices,
16842859e0bSLukasz Lubojanski .probe = ga_probe,
16942859e0bSLukasz Lubojanski };
170f425458eSH Hartley Sweeten module_hid_driver(ga_driver);
17142859e0bSLukasz Lubojanski
172*9d262f35SJeff Johnson MODULE_DESCRIPTION("Force feedback support for GreenAsia (Product ID 0x12) based devices");
17342859e0bSLukasz Lubojanski MODULE_LICENSE("GPL");
174