1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID driver for Saitek devices.
5 * PS1000 (USB gamepad):
6 * Fixes the HID report descriptor by removing a non-existent axis and
7 * clearing the constant bit on the input reports for buttons and d-pad.
8 * (This module is based on "hid-ortek".)
9 * Copyright (c) 2012 Andreas Hübner
11 * R.A.T.7, R.A.T.9, M.M.O.7 (USB gaming mice):
12 * Fixes the mode button which cycles through three constantly pressed
13 * buttons. All three press events are mapped to one button and the
14 * missing release event is generated immediately.
20 #include <linux/device.h>
21 #include <linux/hid.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
27 #define SAITEK_FIX_PS1000 0x0001
28 #define SAITEK_RELEASE_MODE_RAT7 0x0002
29 #define SAITEK_RELEASE_MODE_MMO7 0x0004
36 static int saitek_probe(struct hid_device *hdev,
37 const struct hid_device_id *id)
39 unsigned long quirks = id->driver_data;
40 struct saitek_sc *ssc;
43 ssc = devm_kzalloc(&hdev->dev, sizeof(*ssc), GFP_KERNEL);
45 hid_err(hdev, "can't alloc saitek descriptor\n");
52 hid_set_drvdata(hdev, ssc);
54 ret = hid_parse(hdev);
56 hid_err(hdev, "parse failed\n");
60 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
62 hid_err(hdev, "hw start failed\n");
69 static __u8 *saitek_report_fixup(struct hid_device *hdev, __u8 *rdesc,
72 struct saitek_sc *ssc = hid_get_drvdata(hdev);
74 if ((ssc->quirks & SAITEK_FIX_PS1000) && *rsize == 137 &&
75 rdesc[20] == 0x09 && rdesc[21] == 0x33 &&
76 rdesc[94] == 0x81 && rdesc[95] == 0x03 &&
77 rdesc[110] == 0x81 && rdesc[111] == 0x03) {
79 hid_info(hdev, "Fixing up Saitek PS1000 report descriptor\n");
81 /* convert spurious axis to a "noop" Logical Minimum (0) */
85 /* clear constant bit on buttons and d-pad */
93 static int saitek_raw_event(struct hid_device *hdev,
94 struct hid_report *report, u8 *raw_data, int size)
96 struct saitek_sc *ssc = hid_get_drvdata(hdev);
98 if (ssc->quirks & SAITEK_RELEASE_MODE_RAT7 && size == 7) {
99 /* R.A.T.7 uses bits 13, 14, 15 for the mode */
101 if (raw_data[1] & 0x01)
103 else if (raw_data[1] & 0x02)
105 else if (raw_data[1] & 0x04)
108 /* clear mode bits */
109 raw_data[1] &= ~0x07;
111 if (mode != ssc->mode) {
112 hid_dbg(hdev, "entered mode %d\n", mode);
113 if (ssc->mode != -1) {
114 /* use bit 13 as the mode button */
119 } else if (ssc->quirks & SAITEK_RELEASE_MODE_MMO7 && size == 8) {
121 /* M.M.O.7 uses bits 8, 22, 23 for the mode */
123 if (raw_data[1] & 0x80)
125 else if (raw_data[2] & 0x01)
127 else if (raw_data[2] & 0x02)
130 /* clear mode bits */
131 raw_data[1] &= ~0x80;
132 raw_data[2] &= ~0x03;
134 if (mode != ssc->mode) {
135 hid_dbg(hdev, "entered mode %d\n", mode);
136 if (ssc->mode != -1) {
137 /* use bit 8 as the mode button, bits 22
138 * and 23 do not represent buttons
139 * according to the HID report descriptor
150 static int saitek_event(struct hid_device *hdev, struct hid_field *field,
151 struct hid_usage *usage, __s32 value)
153 struct saitek_sc *ssc = hid_get_drvdata(hdev);
154 struct input_dev *input = field->hidinput->input;
156 if (usage->type == EV_KEY && value &&
157 (((ssc->quirks & SAITEK_RELEASE_MODE_RAT7) &&
158 usage->code - BTN_MOUSE == 10) ||
159 ((ssc->quirks & SAITEK_RELEASE_MODE_MMO7) &&
160 usage->code - BTN_MOUSE == 15))) {
162 input_report_key(input, usage->code, 1);
164 /* report missing release event */
165 input_report_key(input, usage->code, 0);
173 static const struct hid_device_id saitek_devices[] = {
174 { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_PS1000),
175 .driver_data = SAITEK_FIX_PS1000 },
176 { HID_USB_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_RAT5),
177 .driver_data = SAITEK_RELEASE_MODE_RAT7 },
178 { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RAT7_OLD),
179 .driver_data = SAITEK_RELEASE_MODE_RAT7 },
180 { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RAT7),
181 .driver_data = SAITEK_RELEASE_MODE_RAT7 },
182 { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RAT7_CONTAGION),
183 .driver_data = SAITEK_RELEASE_MODE_RAT7 },
184 { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_RAT9),
185 .driver_data = SAITEK_RELEASE_MODE_RAT7 },
186 { HID_USB_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_RAT9),
187 .driver_data = SAITEK_RELEASE_MODE_RAT7 },
188 { HID_USB_DEVICE(USB_VENDOR_ID_SAITEK, USB_DEVICE_ID_SAITEK_MMO7),
189 .driver_data = SAITEK_RELEASE_MODE_MMO7 },
190 { HID_USB_DEVICE(USB_VENDOR_ID_MADCATZ, USB_DEVICE_ID_MADCATZ_MMO7),
191 .driver_data = SAITEK_RELEASE_MODE_MMO7 },
195 MODULE_DEVICE_TABLE(hid, saitek_devices);
197 static struct hid_driver saitek_driver = {
199 .id_table = saitek_devices,
200 .probe = saitek_probe,
201 .report_fixup = saitek_report_fixup,
202 .raw_event = saitek_raw_event,
203 .event = saitek_event,
205 module_hid_driver(saitek_driver);
207 MODULE_LICENSE("GPL");