xref: /linux/drivers/hid/hid-emsff.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Force feedback support for EMS Trio Linker Plus II
4  *
5  *  Copyright (c) 2010 Ignaz Forster <ignaz.forster@gmx.de>
6  */
7 
8 /*
9  */
10 
11 
12 #include <linux/hid.h>
13 #include <linux/input.h>
14 #include <linux/module.h>
15 
16 #include "hid-ids.h"
17 
18 struct emsff_device {
19 	struct hid_report *report;
20 };
21 
22 static int emsff_play(struct input_dev *dev, void *data,
23 			 struct ff_effect *effect)
24 {
25 	struct hid_device *hid = input_get_drvdata(dev);
26 	struct emsff_device *emsff = data;
27 	int weak, strong;
28 
29 	weak = effect->u.rumble.weak_magnitude;
30 	strong = effect->u.rumble.strong_magnitude;
31 
32 	dbg_hid("called with 0x%04x 0x%04x\n", strong, weak);
33 
34 	weak = weak * 0xff / 0xffff;
35 	strong = strong * 0xff / 0xffff;
36 
37 	emsff->report->field[0]->value[1] = weak;
38 	emsff->report->field[0]->value[2] = strong;
39 
40 	dbg_hid("running with 0x%02x 0x%02x\n", strong, weak);
41 	hid_hw_request(hid, emsff->report, HID_REQ_SET_REPORT);
42 
43 	return 0;
44 }
45 
46 static int ems_input_configured(struct hid_device *hid, struct hid_input *hidinput)
47 {
48 	struct emsff_device *emsff;
49 	struct hid_report *report;
50 	struct list_head *report_list =
51 			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
52 	struct input_dev *dev = hidinput->input;
53 	int error;
54 
55 	if (!list_is_first(&hidinput->list, &hid->inputs))
56 		return 0;
57 
58 	report = list_first_entry_or_null(report_list, struct hid_report, list);
59 	if (!report) {
60 		hid_err(hid, "no output reports found\n");
61 		return -ENODEV;
62 	}
63 	if (report->maxfield < 1) {
64 		hid_err(hid, "no fields in the report\n");
65 		return -ENODEV;
66 	}
67 
68 	if (report->field[0]->report_count < 7) {
69 		hid_err(hid, "not enough values in the field\n");
70 		return -ENODEV;
71 	}
72 
73 	emsff = kzalloc_obj(struct emsff_device);
74 	if (!emsff)
75 		return -ENOMEM;
76 
77 	emsff->report = report;
78 	set_bit(FF_RUMBLE, dev->ffbit);
79 
80 	error = input_ff_create_memless(dev, emsff, emsff_play);
81 	if (error) {
82 		kfree(emsff);
83 		return error;
84 	}
85 
86 	emsff->report->field[0]->value[0] = 0x01;
87 	emsff->report->field[0]->value[1] = 0x00;
88 	emsff->report->field[0]->value[2] = 0x00;
89 	emsff->report->field[0]->value[3] = 0x00;
90 	emsff->report->field[0]->value[4] = 0x00;
91 	emsff->report->field[0]->value[5] = 0x00;
92 	emsff->report->field[0]->value[6] = 0x00;
93 	hid_hw_request(hid, emsff->report, HID_REQ_SET_REPORT);
94 
95 	hid_info(hid, "force feedback for EMS based devices by Ignaz Forster <ignaz.forster@gmx.de>\n");
96 
97 	return 0;
98 }
99 
100 static const struct hid_device_id ems_devices[] = {
101 	{ HID_USB_DEVICE(USB_VENDOR_ID_EMS, USB_DEVICE_ID_EMS_TRIO_LINKER_PLUS_II) },
102 	{ }
103 };
104 MODULE_DEVICE_TABLE(hid, ems_devices);
105 
106 static struct hid_driver ems_driver = {
107 	.name = "hkems",
108 	.id_table = ems_devices,
109 	.input_configured = ems_input_configured,
110 };
111 module_hid_driver(ems_driver);
112 
113 MODULE_DESCRIPTION("Force feedback support for EMS Trio Linker Plus II");
114 MODULE_LICENSE("GPL");
115 
116