^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 hid-compliant for some of the devices from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Logitech, namely:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * - WingMan Cordless RumblePad
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * - WingMan Force 3D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2002-2004 Johann Deneux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Should you need to contact me, the author, you can do so by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * e-mail - mail your message to <johann.deneux@it.uu.se>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "hid-lg.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct dev_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u16 idVendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u16 idProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const signed short *ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static const signed short ff_rumble[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) FF_RUMBLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static const signed short ff_joystick[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) FF_CONSTANT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static const signed short ff_joystick_ac[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) FF_CONSTANT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) FF_AUTOCENTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) -1
^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 const struct dev_type devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { 0x046d, 0xc211, ff_rumble },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { 0x046d, 0xc219, ff_rumble },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) { 0x046d, 0xc283, ff_joystick },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) { 0x046d, 0xc286, ff_joystick_ac },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) { 0x046d, 0xc287, ff_joystick_ac },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { 0x046d, 0xc293, ff_joystick },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) { 0x046d, 0xc295, ff_joystick },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct hid_device *hid = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unsigned int left, right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) switch (effect->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) case FF_CONSTANT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) x = effect->u.ramp.start_level + 0x7f; /* 0x7f is center */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) y = effect->u.ramp.end_level + 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) CLAMP(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) CLAMP(y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) report->field[0]->value[0] = 0x51;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) report->field[0]->value[1] = 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) report->field[0]->value[2] = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) report->field[0]->value[3] = y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) hid_hw_request(hid, report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case FF_RUMBLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) right = effect->u.rumble.strong_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) left = effect->u.rumble.weak_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) right = right * 0xff / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) left = left * 0xff / 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) CLAMP(left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) CLAMP(right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) report->field[0]->value[0] = 0x42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) report->field[0]->value[1] = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) report->field[0]->value[2] = left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) report->field[0]->value[3] = right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dbg_hid("(left, right)=(%04x, %04x)\n", left, right);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) hid_hw_request(hid, report, HID_REQ_SET_REPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct hid_device *hid = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __s32 *value = report->field[0]->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) magnitude = (magnitude >> 12) & 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) *value++ = 0xfe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) *value++ = 0x0d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) *value++ = magnitude; /* clockwise strength */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) *value++ = magnitude; /* counter-clockwise strength */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *value++ = 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *value++ = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *value = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) hid_hw_request(hid, report, HID_REQ_SET_REPORT);
^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) int lgff_init(struct hid_device* hid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct hid_input *hidinput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const signed short *ff_bits = ff_joystick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (list_empty(&hid->inputs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) hid_err(hid, "no inputs found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) hidinput = list_entry(hid->inputs.next, struct hid_input, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) dev = hidinput->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Check that the report looks ok */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) for (i = 0; i < ARRAY_SIZE(devices); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (dev->id.vendor == devices[i].idVendor &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev->id.product == devices[i].idProduct) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ff_bits = devices[i].ff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^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) for (i = 0; ff_bits[i] >= 0; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) set_bit(ff_bits[i], dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) error = input_ff_create_memless(dev, NULL, hid_lgff_play);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if ( test_bit(FF_AUTOCENTER, dev->ffbit) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dev->ff->set_autocenter = hid_lgff_set_autocenter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) pr_info("Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }