11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25f022298SJiri Slaby /*
35f022298SJiri Slaby * Force feedback support for PantherLord/GreenAsia based devices
45f022298SJiri Slaby *
55f022298SJiri Slaby * The devices are distributed under various names and the same USB device ID
65f022298SJiri Slaby * can be used in both adapters and actual game controllers.
75f022298SJiri Slaby *
85f022298SJiri Slaby * 0810:0001 "Twin USB Joystick"
95f022298SJiri Slaby * - tested with PantherLord USB/PS2 2in1 Adapter
105f022298SJiri Slaby * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
115f022298SJiri Slaby *
125f022298SJiri Slaby * 0e8f:0003 "GreenAsia Inc. USB Joystick "
13d36b6910SAl Viro * - tested with König Gaming gamepad
145f022298SJiri Slaby *
1527a9c179SAnssi Hannula * 0e8f:0003 "GASIA USB Gamepad"
16d36b6910SAl Viro * - another version of the König gamepad
1727a9c179SAnssi Hannula *
18aaca9cc0SMichael Karcher * 0f30:0111 "Saitek Color Rumble Pad"
19aaca9cc0SMichael Karcher *
2027a9c179SAnssi Hannula * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
215f022298SJiri Slaby */
225f022298SJiri Slaby
235f022298SJiri Slaby /*
245f022298SJiri Slaby */
255f022298SJiri Slaby
265f022298SJiri Slaby
275f022298SJiri Slaby /* #define DEBUG */
285f022298SJiri Slaby
295f022298SJiri Slaby #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
305f022298SJiri Slaby
315f022298SJiri Slaby #include <linux/input.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
338f86a2c3SPaul Gortmaker #include <linux/module.h>
345f022298SJiri Slaby #include <linux/hid.h>
355f022298SJiri Slaby
365f022298SJiri Slaby #include "hid-ids.h"
375f022298SJiri Slaby
385f022298SJiri Slaby #ifdef CONFIG_PANTHERLORD_FF
395f022298SJiri Slaby
405f022298SJiri Slaby struct plff_device {
415f022298SJiri Slaby struct hid_report *report;
42aaca9cc0SMichael Karcher s32 maxval;
4327a9c179SAnssi Hannula s32 *strong;
4427a9c179SAnssi Hannula s32 *weak;
455f022298SJiri Slaby };
465f022298SJiri Slaby
hid_plff_play(struct input_dev * dev,void * data,struct ff_effect * effect)475f022298SJiri Slaby static int hid_plff_play(struct input_dev *dev, void *data,
485f022298SJiri Slaby struct ff_effect *effect)
495f022298SJiri Slaby {
505f022298SJiri Slaby struct hid_device *hid = input_get_drvdata(dev);
515f022298SJiri Slaby struct plff_device *plff = data;
525f022298SJiri Slaby int left, right;
535f022298SJiri Slaby
545f022298SJiri Slaby left = effect->u.rumble.strong_magnitude;
555f022298SJiri Slaby right = effect->u.rumble.weak_magnitude;
565f022298SJiri Slaby debug("called with 0x%04x 0x%04x", left, right);
575f022298SJiri Slaby
58aaca9cc0SMichael Karcher left = left * plff->maxval / 0xffff;
59aaca9cc0SMichael Karcher right = right * plff->maxval / 0xffff;
605f022298SJiri Slaby
6127a9c179SAnssi Hannula *plff->strong = left;
6227a9c179SAnssi Hannula *plff->weak = right;
635f022298SJiri Slaby debug("running with 0x%02x 0x%02x", left, right);
64d8814272SBenjamin Tissoires hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
655f022298SJiri Slaby
665f022298SJiri Slaby return 0;
675f022298SJiri Slaby }
685f022298SJiri Slaby
plff_init(struct hid_device * hid)695f022298SJiri Slaby static int plff_init(struct hid_device *hid)
705f022298SJiri Slaby {
715f022298SJiri Slaby struct plff_device *plff;
725f022298SJiri Slaby struct hid_report *report;
735f022298SJiri Slaby struct hid_input *hidinput;
745f022298SJiri Slaby struct list_head *report_list =
755f022298SJiri Slaby &hid->report_enum[HID_OUTPUT_REPORT].report_list;
765f022298SJiri Slaby struct list_head *report_ptr = report_list;
775f022298SJiri Slaby struct input_dev *dev;
785f022298SJiri Slaby int error;
79aaca9cc0SMichael Karcher s32 maxval;
8027a9c179SAnssi Hannula s32 *strong;
8127a9c179SAnssi Hannula s32 *weak;
825f022298SJiri Slaby
835f022298SJiri Slaby /* The device contains one output report per physical device, all
845f022298SJiri Slaby containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
855f022298SJiri Slaby absolute values.
865f022298SJiri Slaby
875f022298SJiri Slaby The input reports also contain a field which contains
885f022298SJiri Slaby 8 ff00.0001 usages and 8 boolean values. Their meaning is
8927a9c179SAnssi Hannula currently unknown.
9027a9c179SAnssi Hannula
9127a9c179SAnssi Hannula A version of the 0e8f:0003 exists that has all the values in
9227a9c179SAnssi Hannula separate fields and misses the extra input field, thus resembling
9327a9c179SAnssi Hannula Zeroplus (hid-zpff) devices.
9427a9c179SAnssi Hannula */
955f022298SJiri Slaby
965f022298SJiri Slaby if (list_empty(report_list)) {
974291ee30SJoe Perches hid_err(hid, "no output reports found\n");
985f022298SJiri Slaby return -ENODEV;
995f022298SJiri Slaby }
1005f022298SJiri Slaby
1015f022298SJiri Slaby list_for_each_entry(hidinput, &hid->inputs, list) {
1025f022298SJiri Slaby
1035f022298SJiri Slaby report_ptr = report_ptr->next;
1045f022298SJiri Slaby
1055f022298SJiri Slaby if (report_ptr == report_list) {
1064291ee30SJoe Perches hid_err(hid, "required output report is missing\n");
1075f022298SJiri Slaby return -ENODEV;
1085f022298SJiri Slaby }
1095f022298SJiri Slaby
1105f022298SJiri Slaby report = list_entry(report_ptr, struct hid_report, list);
1115f022298SJiri Slaby if (report->maxfield < 1) {
1124291ee30SJoe Perches hid_err(hid, "no fields in the report\n");
1135f022298SJiri Slaby return -ENODEV;
1145f022298SJiri Slaby }
1155f022298SJiri Slaby
116aaca9cc0SMichael Karcher maxval = 0x7f;
11727a9c179SAnssi Hannula if (report->field[0]->report_count >= 4) {
11827a9c179SAnssi Hannula report->field[0]->value[0] = 0x00;
11927a9c179SAnssi Hannula report->field[0]->value[1] = 0x00;
12027a9c179SAnssi Hannula strong = &report->field[0]->value[2];
12127a9c179SAnssi Hannula weak = &report->field[0]->value[3];
12227a9c179SAnssi Hannula debug("detected single-field device");
123412f3010SKees Cook } else if (report->field[0]->maxusage == 1 &&
124412f3010SKees Cook report->field[0]->usage[0].hid ==
125412f3010SKees Cook (HID_UP_LED | 0x43) &&
126412f3010SKees Cook report->maxfield >= 4 &&
127412f3010SKees Cook report->field[0]->report_count >= 1 &&
128412f3010SKees Cook report->field[1]->report_count >= 1 &&
129412f3010SKees Cook report->field[2]->report_count >= 1 &&
130412f3010SKees Cook report->field[3]->report_count >= 1) {
13127a9c179SAnssi Hannula report->field[0]->value[0] = 0x00;
13227a9c179SAnssi Hannula report->field[1]->value[0] = 0x00;
13327a9c179SAnssi Hannula strong = &report->field[2]->value[0];
13427a9c179SAnssi Hannula weak = &report->field[3]->value[0];
135aaca9cc0SMichael Karcher if (hid->vendor == USB_VENDOR_ID_JESS2)
136aaca9cc0SMichael Karcher maxval = 0xff;
13727a9c179SAnssi Hannula debug("detected 4-field device");
13827a9c179SAnssi Hannula } else {
1394291ee30SJoe Perches hid_err(hid, "not enough fields or values\n");
1405f022298SJiri Slaby return -ENODEV;
1415f022298SJiri Slaby }
1425f022298SJiri Slaby
1435f022298SJiri Slaby plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
1445f022298SJiri Slaby if (!plff)
1455f022298SJiri Slaby return -ENOMEM;
1465f022298SJiri Slaby
1475f022298SJiri Slaby dev = hidinput->input;
1485f022298SJiri Slaby
1495f022298SJiri Slaby set_bit(FF_RUMBLE, dev->ffbit);
1505f022298SJiri Slaby
1515f022298SJiri Slaby error = input_ff_create_memless(dev, plff, hid_plff_play);
1525f022298SJiri Slaby if (error) {
1535f022298SJiri Slaby kfree(plff);
1545f022298SJiri Slaby return error;
1555f022298SJiri Slaby }
1565f022298SJiri Slaby
1575f022298SJiri Slaby plff->report = report;
15827a9c179SAnssi Hannula plff->strong = strong;
15927a9c179SAnssi Hannula plff->weak = weak;
160aaca9cc0SMichael Karcher plff->maxval = maxval;
16127a9c179SAnssi Hannula
16227a9c179SAnssi Hannula *strong = 0x00;
16327a9c179SAnssi Hannula *weak = 0x00;
164d8814272SBenjamin Tissoires hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
1655f022298SJiri Slaby }
1665f022298SJiri Slaby
1674291ee30SJoe Perches hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
1685f022298SJiri Slaby
1695f022298SJiri Slaby return 0;
1705f022298SJiri Slaby }
1715f022298SJiri Slaby #else
plff_init(struct hid_device * hid)1725f022298SJiri Slaby static inline int plff_init(struct hid_device *hid)
1735f022298SJiri Slaby {
1745f022298SJiri Slaby return 0;
1755f022298SJiri Slaby }
1765f022298SJiri Slaby #endif
1775f022298SJiri Slaby
pl_probe(struct hid_device * hdev,const struct hid_device_id * id)1785f022298SJiri Slaby static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
1795f022298SJiri Slaby {
1805f022298SJiri Slaby int ret;
1815f022298SJiri Slaby
1825f022298SJiri Slaby if (id->driver_data)
1835f022298SJiri Slaby hdev->quirks |= HID_QUIRK_MULTI_INPUT;
1845f022298SJiri Slaby
1855f022298SJiri Slaby ret = hid_parse(hdev);
1865f022298SJiri Slaby if (ret) {
1874291ee30SJoe Perches hid_err(hdev, "parse failed\n");
1885f022298SJiri Slaby goto err;
1895f022298SJiri Slaby }
1905f022298SJiri Slaby
1915f022298SJiri Slaby ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
1925f022298SJiri Slaby if (ret) {
1934291ee30SJoe Perches hid_err(hdev, "hw start failed\n");
1945f022298SJiri Slaby goto err;
1955f022298SJiri Slaby }
1965f022298SJiri Slaby
1975f022298SJiri Slaby plff_init(hdev);
1985f022298SJiri Slaby
1995f022298SJiri Slaby return 0;
2005f022298SJiri Slaby err:
2015f022298SJiri Slaby return ret;
2025f022298SJiri Slaby }
2035f022298SJiri Slaby
2045f022298SJiri Slaby static const struct hid_device_id pl_devices[] = {
2055f022298SJiri Slaby { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
2065f022298SJiri Slaby .driver_data = 1 }, /* Twin USB Joystick */
207578f3a35SJiri Kosina { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
208578f3a35SJiri Kosina .driver_data = 1 }, /* Twin USB Joystick */
20927a9c179SAnssi Hannula { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
210aaca9cc0SMichael Karcher { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD), },
2115f022298SJiri Slaby { }
2125f022298SJiri Slaby };
2135f022298SJiri Slaby MODULE_DEVICE_TABLE(hid, pl_devices);
2145f022298SJiri Slaby
2155f022298SJiri Slaby static struct hid_driver pl_driver = {
2165f022298SJiri Slaby .name = "pantherlord",
2175f022298SJiri Slaby .id_table = pl_devices,
2185f022298SJiri Slaby .probe = pl_probe,
2195f022298SJiri Slaby };
220f425458eSH Hartley Sweeten module_hid_driver(pl_driver);
2215f022298SJiri Slaby
222*9d262f35SJeff Johnson MODULE_DESCRIPTION("Force feedback support for PantherLord/GreenAsia based devices");
2235f022298SJiri Slaby MODULE_LICENSE("GPL");
224