Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Force feedback support for hid-compliant for some of the devices from
0004  * Logitech, namely:
0005  * - WingMan Cordless RumblePad
0006  * - WingMan Force 3D
0007  *
0008  *  Copyright (c) 2002-2004 Johann Deneux
0009  *  Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
0010  */
0011 
0012 /*
0013  *
0014  * Should you need to contact me, the author, you can do so by
0015  * e-mail - mail your message to <johann.deneux@it.uu.se>
0016  */
0017 
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019 
0020 #include <linux/input.h>
0021 #include <linux/hid.h>
0022 
0023 #include "hid-lg.h"
0024 
0025 struct dev_type {
0026     u16 idVendor;
0027     u16 idProduct;
0028     const signed short *ff;
0029 };
0030 
0031 static const signed short ff_rumble[] = {
0032     FF_RUMBLE,
0033     -1
0034 };
0035 
0036 static const signed short ff_joystick[] = {
0037     FF_CONSTANT,
0038     -1
0039 };
0040 
0041 static const signed short ff_joystick_ac[] = {
0042     FF_CONSTANT,
0043     FF_AUTOCENTER,
0044     -1
0045 };
0046 
0047 static const struct dev_type devices[] = {
0048     { 0x046d, 0xc211, ff_rumble },
0049     { 0x046d, 0xc219, ff_rumble },
0050     { 0x046d, 0xc283, ff_joystick },
0051     { 0x046d, 0xc286, ff_joystick_ac },
0052     { 0x046d, 0xc287, ff_joystick_ac },
0053     { 0x046d, 0xc293, ff_joystick },
0054     { 0x046d, 0xc295, ff_joystick },
0055 };
0056 
0057 static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
0058 {
0059     struct hid_device *hid = input_get_drvdata(dev);
0060     struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
0061     struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
0062     int x, y;
0063     unsigned int left, right;
0064 
0065 #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
0066 
0067     switch (effect->type) {
0068     case FF_CONSTANT:
0069         x = effect->u.ramp.start_level + 0x7f;  /* 0x7f is center */
0070         y = effect->u.ramp.end_level + 0x7f;
0071         CLAMP(x);
0072         CLAMP(y);
0073         report->field[0]->value[0] = 0x51;
0074         report->field[0]->value[1] = 0x08;
0075         report->field[0]->value[2] = x;
0076         report->field[0]->value[3] = y;
0077         dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
0078         hid_hw_request(hid, report, HID_REQ_SET_REPORT);
0079         break;
0080 
0081     case FF_RUMBLE:
0082         right = effect->u.rumble.strong_magnitude;
0083         left = effect->u.rumble.weak_magnitude;
0084         right = right * 0xff / 0xffff;
0085         left = left * 0xff / 0xffff;
0086         CLAMP(left);
0087         CLAMP(right);
0088         report->field[0]->value[0] = 0x42;
0089         report->field[0]->value[1] = 0x00;
0090         report->field[0]->value[2] = left;
0091         report->field[0]->value[3] = right;
0092         dbg_hid("(left, right)=(%04x, %04x)\n", left, right);
0093         hid_hw_request(hid, report, HID_REQ_SET_REPORT);
0094         break;
0095     }
0096     return 0;
0097 }
0098 
0099 static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)
0100 {
0101     struct hid_device *hid = input_get_drvdata(dev);
0102     struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
0103     struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
0104     __s32 *value = report->field[0]->value;
0105     magnitude = (magnitude >> 12) & 0xf;
0106     *value++ = 0xfe;
0107     *value++ = 0x0d;
0108     *value++ = magnitude;   /* clockwise strength */
0109     *value++ = magnitude;   /* counter-clockwise strength */
0110     *value++ = 0x80;
0111     *value++ = 0x00;
0112     *value = 0x00;
0113     hid_hw_request(hid, report, HID_REQ_SET_REPORT);
0114 }
0115 
0116 int lgff_init(struct hid_device* hid)
0117 {
0118     struct hid_input *hidinput;
0119     struct input_dev *dev;
0120     const signed short *ff_bits = ff_joystick;
0121     int error;
0122     int i;
0123 
0124     if (list_empty(&hid->inputs)) {
0125         hid_err(hid, "no inputs found\n");
0126         return -ENODEV;
0127     }
0128     hidinput = list_entry(hid->inputs.next, struct hid_input, list);
0129     dev = hidinput->input;
0130 
0131     /* Check that the report looks ok */
0132     if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
0133         return -ENODEV;
0134 
0135     for (i = 0; i < ARRAY_SIZE(devices); i++) {
0136         if (dev->id.vendor == devices[i].idVendor &&
0137             dev->id.product == devices[i].idProduct) {
0138             ff_bits = devices[i].ff;
0139             break;
0140         }
0141     }
0142 
0143     for (i = 0; ff_bits[i] >= 0; i++)
0144         set_bit(ff_bits[i], dev->ffbit);
0145 
0146     error = input_ff_create_memless(dev, NULL, hid_lgff_play);
0147     if (error)
0148         return error;
0149 
0150     if ( test_bit(FF_AUTOCENTER, dev->ffbit) )
0151         dev->ff->set_autocenter = hid_lgff_set_autocenter;
0152 
0153     pr_info("Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
0154 
0155     return 0;
0156 }