^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Force feedback support for PantherLord/GreenAsia based devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * The devices are distributed under various names and the same USB device ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * can be used in both adapters and actual game controllers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * 0810:0001 "Twin USB Joystick"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * - tested with PantherLord USB/PS2 2in1 Adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * 0e8f:0003 "GreenAsia Inc. USB Joystick "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * - tested with König Gaming gamepad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 0e8f:0003 "GASIA USB Gamepad"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * - another version of the König gamepad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * 0f30:0111 "Saitek Color Rumble Pad"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* #define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include "hid-ids.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #ifdef CONFIG_PANTHERLORD_FF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct plff_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) s32 maxval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) s32 *strong;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) s32 *weak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static int hid_plff_play(struct input_dev *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct hid_device *hid = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct plff_device *plff = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int left, right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) left = effect->u.rumble.strong_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) right = effect->u.rumble.weak_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) debug("called with 0x%04x 0x%04x", left, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) left = left * plff->maxval / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) right = right * plff->maxval / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *plff->strong = left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *plff->weak = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) debug("running with 0x%02x 0x%02x", left, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int plff_init(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct plff_device *plff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct hid_report *report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct hid_input *hidinput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct list_head *report_list =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) &hid->report_enum[HID_OUTPUT_REPORT].report_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct list_head *report_ptr = report_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) s32 maxval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) s32 *strong;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) s32 *weak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* The device contains one output report per physical device, all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) absolute values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) The input reports also contain a field which contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) 8 ff00.0001 usages and 8 boolean values. Their meaning is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) currently unknown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) A version of the 0e8f:0003 exists that has all the values in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) separate fields and misses the extra input field, thus resembling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) Zeroplus (hid-zpff) devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (list_empty(report_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) hid_err(hid, "no output reports found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) list_for_each_entry(hidinput, &hid->inputs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) report_ptr = report_ptr->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (report_ptr == report_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) hid_err(hid, "required output report is missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) report = list_entry(report_ptr, struct hid_report, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (report->maxfield < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) hid_err(hid, "no fields in the report\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) maxval = 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (report->field[0]->report_count >= 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) report->field[0]->value[0] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) report->field[0]->value[1] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) strong = &report->field[0]->value[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) weak = &report->field[0]->value[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) debug("detected single-field device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) } else if (report->field[0]->maxusage == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) report->field[0]->usage[0].hid ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) (HID_UP_LED | 0x43) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) report->maxfield >= 4 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) report->field[0]->report_count >= 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) report->field[1]->report_count >= 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) report->field[2]->report_count >= 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) report->field[3]->report_count >= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) report->field[0]->value[0] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) report->field[1]->value[0] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) strong = &report->field[2]->value[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) weak = &report->field[3]->value[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (hid->vendor == USB_VENDOR_ID_JESS2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) maxval = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) debug("detected 4-field device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) hid_err(hid, "not enough fields or values\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (!plff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dev = hidinput->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) set_bit(FF_RUMBLE, dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) error = input_ff_create_memless(dev, plff, hid_plff_play);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) kfree(plff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) plff->report = report;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) plff->strong = strong;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) plff->weak = weak;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) plff->maxval = maxval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *strong = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) *weak = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static inline int plff_init(struct hid_device *hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (id->driver_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) hdev->quirks |= HID_QUIRK_MULTI_INPUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = hid_parse(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) hid_err(hdev, "parse failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) hid_err(hdev, "hw start failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) plff_init(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static const struct hid_device_id pl_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .driver_data = 1 }, /* Twin USB Joystick */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .driver_data = 1 }, /* Twin USB Joystick */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD), },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) MODULE_DEVICE_TABLE(hid, pl_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static struct hid_driver pl_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) .name = "pantherlord",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) .id_table = pl_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) .probe = pl_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) module_hid_driver(pl_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) MODULE_LICENSE("GPL");