Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Force feedback support for PantherLord/GreenAsia based devices
0004  *
0005  *  The devices are distributed under various names and the same USB device ID
0006  *  can be used in both adapters and actual game controllers.
0007  *
0008  *  0810:0001 "Twin USB Joystick"
0009  *   - tested with PantherLord USB/PS2 2in1 Adapter
0010  *   - contains two reports, one for each port (HID_QUIRK_MULTI_INPUT)
0011  *
0012  *  0e8f:0003 "GreenAsia Inc.    USB Joystick     "
0013  *   - tested with König Gaming gamepad
0014  *
0015  *  0e8f:0003 "GASIA USB Gamepad"
0016  *   - another version of the König gamepad
0017  *
0018  *  0f30:0111 "Saitek Color Rumble Pad"
0019  *
0020  *  Copyright (c) 2007, 2009 Anssi Hannula <anssi.hannula@gmail.com>
0021  */
0022 
0023 /*
0024  */
0025 
0026 
0027 /* #define DEBUG */
0028 
0029 #define debug(format, arg...) pr_debug("hid-plff: " format "\n" , ## arg)
0030 
0031 #include <linux/input.h>
0032 #include <linux/slab.h>
0033 #include <linux/module.h>
0034 #include <linux/hid.h>
0035 
0036 #include "hid-ids.h"
0037 
0038 #ifdef CONFIG_PANTHERLORD_FF
0039 
0040 struct plff_device {
0041     struct hid_report *report;
0042     s32 maxval;
0043     s32 *strong;
0044     s32 *weak;
0045 };
0046 
0047 static int hid_plff_play(struct input_dev *dev, void *data,
0048              struct ff_effect *effect)
0049 {
0050     struct hid_device *hid = input_get_drvdata(dev);
0051     struct plff_device *plff = data;
0052     int left, right;
0053 
0054     left = effect->u.rumble.strong_magnitude;
0055     right = effect->u.rumble.weak_magnitude;
0056     debug("called with 0x%04x 0x%04x", left, right);
0057 
0058     left = left * plff->maxval / 0xffff;
0059     right = right * plff->maxval / 0xffff;
0060 
0061     *plff->strong = left;
0062     *plff->weak = right;
0063     debug("running with 0x%02x 0x%02x", left, right);
0064     hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
0065 
0066     return 0;
0067 }
0068 
0069 static int plff_init(struct hid_device *hid)
0070 {
0071     struct plff_device *plff;
0072     struct hid_report *report;
0073     struct hid_input *hidinput;
0074     struct list_head *report_list =
0075             &hid->report_enum[HID_OUTPUT_REPORT].report_list;
0076     struct list_head *report_ptr = report_list;
0077     struct input_dev *dev;
0078     int error;
0079     s32 maxval;
0080     s32 *strong;
0081     s32 *weak;
0082 
0083     /* The device contains one output report per physical device, all
0084        containing 1 field, which contains 4 ff00.0002 usages and 4 16bit
0085        absolute values.
0086 
0087        The input reports also contain a field which contains
0088        8 ff00.0001 usages and 8 boolean values. Their meaning is
0089        currently unknown.
0090        
0091        A version of the 0e8f:0003 exists that has all the values in
0092        separate fields and misses the extra input field, thus resembling
0093        Zeroplus (hid-zpff) devices.
0094     */
0095 
0096     if (list_empty(report_list)) {
0097         hid_err(hid, "no output reports found\n");
0098         return -ENODEV;
0099     }
0100 
0101     list_for_each_entry(hidinput, &hid->inputs, list) {
0102 
0103         report_ptr = report_ptr->next;
0104 
0105         if (report_ptr == report_list) {
0106             hid_err(hid, "required output report is missing\n");
0107             return -ENODEV;
0108         }
0109 
0110         report = list_entry(report_ptr, struct hid_report, list);
0111         if (report->maxfield < 1) {
0112             hid_err(hid, "no fields in the report\n");
0113             return -ENODEV;
0114         }
0115 
0116         maxval = 0x7f;
0117         if (report->field[0]->report_count >= 4) {
0118             report->field[0]->value[0] = 0x00;
0119             report->field[0]->value[1] = 0x00;
0120             strong = &report->field[0]->value[2];
0121             weak = &report->field[0]->value[3];
0122             debug("detected single-field device");
0123         } else if (report->field[0]->maxusage == 1 &&
0124                report->field[0]->usage[0].hid ==
0125                 (HID_UP_LED | 0x43) &&
0126                report->maxfield >= 4 &&
0127                report->field[0]->report_count >= 1 &&
0128                report->field[1]->report_count >= 1 &&
0129                report->field[2]->report_count >= 1 &&
0130                report->field[3]->report_count >= 1) {
0131             report->field[0]->value[0] = 0x00;
0132             report->field[1]->value[0] = 0x00;
0133             strong = &report->field[2]->value[0];
0134             weak = &report->field[3]->value[0];
0135             if (hid->vendor == USB_VENDOR_ID_JESS2)
0136                 maxval = 0xff;
0137             debug("detected 4-field device");
0138         } else {
0139             hid_err(hid, "not enough fields or values\n");
0140             return -ENODEV;
0141         }
0142 
0143         plff = kzalloc(sizeof(struct plff_device), GFP_KERNEL);
0144         if (!plff)
0145             return -ENOMEM;
0146 
0147         dev = hidinput->input;
0148 
0149         set_bit(FF_RUMBLE, dev->ffbit);
0150 
0151         error = input_ff_create_memless(dev, plff, hid_plff_play);
0152         if (error) {
0153             kfree(plff);
0154             return error;
0155         }
0156 
0157         plff->report = report;
0158         plff->strong = strong;
0159         plff->weak = weak;
0160         plff->maxval = maxval;
0161 
0162         *strong = 0x00;
0163         *weak = 0x00;
0164         hid_hw_request(hid, plff->report, HID_REQ_SET_REPORT);
0165     }
0166 
0167     hid_info(hid, "Force feedback for PantherLord/GreenAsia devices by Anssi Hannula <anssi.hannula@gmail.com>\n");
0168 
0169     return 0;
0170 }
0171 #else
0172 static inline int plff_init(struct hid_device *hid)
0173 {
0174     return 0;
0175 }
0176 #endif
0177 
0178 static int pl_probe(struct hid_device *hdev, const struct hid_device_id *id)
0179 {
0180     int ret;
0181 
0182     if (id->driver_data)
0183         hdev->quirks |= HID_QUIRK_MULTI_INPUT;
0184 
0185     ret = hid_parse(hdev);
0186     if (ret) {
0187         hid_err(hdev, "parse failed\n");
0188         goto err;
0189     }
0190 
0191     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
0192     if (ret) {
0193         hid_err(hdev, "hw start failed\n");
0194         goto err;
0195     }
0196 
0197     plff_init(hdev);
0198 
0199     return 0;
0200 err:
0201     return ret;
0202 }
0203 
0204 static const struct hid_device_id pl_devices[] = {
0205     { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PSX_ADAPTOR),
0206         .driver_data = 1 }, /* Twin USB Joystick */
0207     { HID_USB_DEVICE(USB_VENDOR_ID_GAMERON, USB_DEVICE_ID_GAMERON_DUAL_PCS_ADAPTOR),
0208         .driver_data = 1 }, /* Twin USB Joystick */
0209     { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0003), },
0210     { HID_USB_DEVICE(USB_VENDOR_ID_JESS2, USB_DEVICE_ID_JESS2_COLOR_RUMBLE_PAD), },
0211     { }
0212 };
0213 MODULE_DEVICE_TABLE(hid, pl_devices);
0214 
0215 static struct hid_driver pl_driver = {
0216     .name = "pantherlord",
0217     .id_table = pl_devices,
0218     .probe = pl_probe,
0219 };
0220 module_hid_driver(pl_driver);
0221 
0222 MODULE_LICENSE("GPL");