1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID driver for some microsoft "special" devices
5 * Copyright (c) 1999 Andreas Gal
6 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
8 * Copyright (c) 2006-2007 Jiri Kosina
9 * Copyright (c) 2008 Jiri Slaby
15 #include <linux/device.h>
16 #include <linux/input.h>
17 #include <linux/hid.h>
18 #include <linux/module.h>
22 #define MS_HIDINPUT BIT(0)
23 #define MS_ERGONOMY BIT(1)
24 #define MS_PRESENTER BIT(2)
25 #define MS_RDESC BIT(3)
26 #define MS_NOGET BIT(4)
27 #define MS_DUPLICATE_USAGES BIT(5)
28 #define MS_SURFACE_DIAL BIT(6)
29 #define MS_QUIRK_FF BIT(7)
33 struct hid_device
*hdev
;
34 struct work_struct ff_worker
;
37 void *output_report_dmabuf
;
40 #define XB1S_FF_REPORT 3
41 #define ENABLE_WEAK BIT(0)
42 #define ENABLE_STRONG BIT(1)
50 struct xb1s_ff_report
{
53 __u8 magnitude
[MAGNITUDE_NUM
];
55 __u8 start_delay_10ms
;
59 static __u8
*ms_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
62 struct ms_data
*ms
= hid_get_drvdata(hdev
);
63 unsigned long quirks
= ms
->quirks
;
66 * Microsoft Wireless Desktop Receiver (Model 1028) has
67 * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
69 if ((quirks
& MS_RDESC
) && *rsize
== 571 && rdesc
[557] == 0x19 &&
71 hid_info(hdev
, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
78 #define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
80 static int ms_ergonomy_kb_quirk(struct hid_input
*hi
, struct hid_usage
*usage
,
81 unsigned long **bit
, int *max
)
83 struct input_dev
*input
= hi
->input
;
85 if ((usage
->hid
& HID_USAGE_PAGE
) == HID_UP_CONSUMER
) {
86 switch (usage
->hid
& HID_USAGE
) {
88 * Microsoft uses these 2 reserved usage ids for 2 keys on
89 * the MS office kb labelled "Office Home" and "Task Pane".
92 ms_map_key_clear(KEY_PROG1
);
95 ms_map_key_clear(KEY_PROG2
);
101 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_MSVENDOR
)
104 switch (usage
->hid
& HID_USAGE
) {
105 case 0xfd06: ms_map_key_clear(KEY_CHAT
); break;
106 case 0xfd07: ms_map_key_clear(KEY_PHONE
); break;
108 /* Special keypad keys */
109 ms_map_key_clear(KEY_KPEQUAL
);
110 set_bit(KEY_KPLEFTPAREN
, input
->keybit
);
111 set_bit(KEY_KPRIGHTPAREN
, input
->keybit
);
115 hid_map_usage_clear(hi
, usage
, bit
, max
, EV_REL
, REL_WHEEL
);
119 * This byte contains a copy of the modifier keys byte of a
120 * standard hid keyboard report, as send by interface 0
121 * (this usage is found on interface 1).
123 * This byte only gets send when another key in the same report
124 * changes state, and as such is useless, ignore it.
128 set_bit(EV_REP
, input
->evbit
);
129 ms_map_key_clear(KEY_F13
);
130 set_bit(KEY_F14
, input
->keybit
);
131 set_bit(KEY_F15
, input
->keybit
);
132 set_bit(KEY_F16
, input
->keybit
);
133 set_bit(KEY_F17
, input
->keybit
);
134 set_bit(KEY_F18
, input
->keybit
);
142 static int ms_presenter_8k_quirk(struct hid_input
*hi
, struct hid_usage
*usage
,
143 unsigned long **bit
, int *max
)
145 if ((usage
->hid
& HID_USAGE_PAGE
) != HID_UP_MSVENDOR
)
148 set_bit(EV_REP
, hi
->input
->evbit
);
149 switch (usage
->hid
& HID_USAGE
) {
150 case 0xfd08: ms_map_key_clear(KEY_FORWARD
); break;
151 case 0xfd09: ms_map_key_clear(KEY_BACK
); break;
152 case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE
); break;
153 case 0xfd0e: ms_map_key_clear(KEY_CLOSE
); break;
154 case 0xfd0f: ms_map_key_clear(KEY_PLAY
); break;
161 static int ms_surface_dial_quirk(struct hid_input
*hi
, struct hid_field
*field
,
162 struct hid_usage
*usage
, unsigned long **bit
, int *max
)
164 switch (usage
->hid
& HID_USAGE_PAGE
) {
167 case HID_UP_DIGITIZER
:
168 /* ignore those axis */
171 switch (usage
->hid
) {
176 case HID_GD_RFKILL_BTN
:
177 /* ignore those axis */
185 static int ms_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
186 struct hid_field
*field
, struct hid_usage
*usage
,
187 unsigned long **bit
, int *max
)
189 struct ms_data
*ms
= hid_get_drvdata(hdev
);
190 unsigned long quirks
= ms
->quirks
;
192 if (quirks
& MS_ERGONOMY
) {
193 int ret
= ms_ergonomy_kb_quirk(hi
, usage
, bit
, max
);
198 if ((quirks
& MS_PRESENTER
) &&
199 ms_presenter_8k_quirk(hi
, usage
, bit
, max
))
202 if (quirks
& MS_SURFACE_DIAL
) {
203 int ret
= ms_surface_dial_quirk(hi
, field
, usage
, bit
, max
);
212 static int ms_input_mapped(struct hid_device
*hdev
, struct hid_input
*hi
,
213 struct hid_field
*field
, struct hid_usage
*usage
,
214 unsigned long **bit
, int *max
)
216 struct ms_data
*ms
= hid_get_drvdata(hdev
);
217 unsigned long quirks
= ms
->quirks
;
219 if (quirks
& MS_DUPLICATE_USAGES
)
220 clear_bit(usage
->code
, *bit
);
225 static int ms_event(struct hid_device
*hdev
, struct hid_field
*field
,
226 struct hid_usage
*usage
, __s32 value
)
228 struct ms_data
*ms
= hid_get_drvdata(hdev
);
229 unsigned long quirks
= ms
->quirks
;
230 struct input_dev
*input
;
232 if (!(hdev
->claimed
& HID_CLAIMED_INPUT
) || !field
->hidinput
||
236 input
= field
->hidinput
->input
;
238 /* Handling MS keyboards special buttons */
239 if (quirks
& MS_ERGONOMY
&& usage
->hid
== (HID_UP_MSVENDOR
| 0xff00)) {
240 /* Special keypad keys */
241 input_report_key(input
, KEY_KPEQUAL
, value
& 0x01);
242 input_report_key(input
, KEY_KPLEFTPAREN
, value
& 0x02);
243 input_report_key(input
, KEY_KPRIGHTPAREN
, value
& 0x04);
247 if (quirks
& MS_ERGONOMY
&& usage
->hid
== (HID_UP_MSVENDOR
| 0xff01)) {
249 int step
= ((value
& 0x60) >> 5) + 1;
251 switch (value
& 0x1f) {
253 input_report_rel(input
, REL_WHEEL
, step
);
256 input_report_rel(input
, REL_WHEEL
, -step
);
262 if (quirks
& MS_ERGONOMY
&& usage
->hid
== (HID_UP_MSVENDOR
| 0xff05)) {
263 static unsigned int last_key
= 0;
264 unsigned int key
= 0;
266 case 0x01: key
= KEY_F14
; break;
267 case 0x02: key
= KEY_F15
; break;
268 case 0x04: key
= KEY_F16
; break;
269 case 0x08: key
= KEY_F17
; break;
270 case 0x10: key
= KEY_F18
; break;
273 input_event(input
, usage
->type
, key
, 1);
276 input_event(input
, usage
->type
, last_key
, 0);
284 static void ms_ff_worker(struct work_struct
*work
)
286 struct ms_data
*ms
= container_of(work
, struct ms_data
, ff_worker
);
287 struct hid_device
*hdev
= ms
->hdev
;
288 struct xb1s_ff_report
*r
= ms
->output_report_dmabuf
;
291 memset(r
, 0, sizeof(*r
));
293 r
->report_id
= XB1S_FF_REPORT
;
294 r
->enable
= ENABLE_WEAK
| ENABLE_STRONG
;
296 * Specifying maximum duration and maximum loop count should
297 * cover maximum duration of a single effect, which is 65536
300 r
->duration_10ms
= U8_MAX
;
301 r
->loop_count
= U8_MAX
;
302 r
->magnitude
[MAGNITUDE_STRONG
] = ms
->strong
; /* left actuator */
303 r
->magnitude
[MAGNITUDE_WEAK
] = ms
->weak
; /* right actuator */
305 ret
= hid_hw_output_report(hdev
, (__u8
*)r
, sizeof(*r
));
307 hid_warn(hdev
, "failed to send FF report\n");
310 static int ms_play_effect(struct input_dev
*dev
, void *data
,
311 struct ff_effect
*effect
)
313 struct hid_device
*hid
= input_get_drvdata(dev
);
314 struct ms_data
*ms
= hid_get_drvdata(hid
);
316 if (effect
->type
!= FF_RUMBLE
)
320 * Magnitude is 0..100 so scale the 16-bit input here
322 ms
->strong
= ((u32
) effect
->u
.rumble
.strong_magnitude
* 100) / U16_MAX
;
323 ms
->weak
= ((u32
) effect
->u
.rumble
.weak_magnitude
* 100) / U16_MAX
;
325 schedule_work(&ms
->ff_worker
);
329 static int ms_init_ff(struct hid_device
*hdev
)
331 struct hid_input
*hidinput
;
332 struct input_dev
*input_dev
;
333 struct ms_data
*ms
= hid_get_drvdata(hdev
);
335 if (list_empty(&hdev
->inputs
)) {
336 hid_err(hdev
, "no inputs found\n");
339 hidinput
= list_entry(hdev
->inputs
.next
, struct hid_input
, list
);
340 input_dev
= hidinput
->input
;
342 if (!(ms
->quirks
& MS_QUIRK_FF
))
346 INIT_WORK(&ms
->ff_worker
, ms_ff_worker
);
348 ms
->output_report_dmabuf
= devm_kzalloc(&hdev
->dev
,
349 sizeof(struct xb1s_ff_report
),
351 if (ms
->output_report_dmabuf
== NULL
)
354 input_set_capability(input_dev
, EV_FF
, FF_RUMBLE
);
355 return input_ff_create_memless(input_dev
, NULL
, ms_play_effect
);
358 static void ms_remove_ff(struct hid_device
*hdev
)
360 struct ms_data
*ms
= hid_get_drvdata(hdev
);
362 if (!(ms
->quirks
& MS_QUIRK_FF
))
365 cancel_work_sync(&ms
->ff_worker
);
368 static int ms_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
370 unsigned long quirks
= id
->driver_data
;
374 ms
= devm_kzalloc(&hdev
->dev
, sizeof(*ms
), GFP_KERNEL
);
380 hid_set_drvdata(hdev
, ms
);
382 if (quirks
& MS_NOGET
)
383 hdev
->quirks
|= HID_QUIRK_NOGET
;
385 if (quirks
& MS_SURFACE_DIAL
)
386 hdev
->quirks
|= HID_QUIRK_INPUT_PER_APP
;
388 ret
= hid_parse(hdev
);
390 hid_err(hdev
, "parse failed\n");
394 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
| ((quirks
& MS_HIDINPUT
) ?
395 HID_CONNECT_HIDINPUT_FORCE
: 0));
397 hid_err(hdev
, "hw start failed\n");
401 ret
= ms_init_ff(hdev
);
403 hid_err(hdev
, "could not initialize ff, continuing anyway");
410 static void ms_remove(struct hid_device
*hdev
)
416 static const struct hid_device_id ms_devices
[] = {
417 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_SIDEWINDER_GV
),
418 .driver_data
= MS_HIDINPUT
},
419 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_OFFICE_KB
),
420 .driver_data
= MS_ERGONOMY
},
421 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_NE4K
),
422 .driver_data
= MS_ERGONOMY
},
423 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_NE4K_JP
),
424 .driver_data
= MS_ERGONOMY
},
425 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_NE7K
),
426 .driver_data
= MS_ERGONOMY
},
427 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_LK6K
),
428 .driver_data
= MS_ERGONOMY
| MS_RDESC
},
429 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_PRESENTER_8K_USB
),
430 .driver_data
= MS_PRESENTER
},
431 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K
),
432 .driver_data
= MS_ERGONOMY
},
433 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_DIGITAL_MEDIA_7K
),
434 .driver_data
= MS_ERGONOMY
},
435 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_DIGITAL_MEDIA_600
),
436 .driver_data
= MS_ERGONOMY
},
437 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3KV1
),
438 .driver_data
= MS_ERGONOMY
},
439 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0
),
440 .driver_data
= MS_NOGET
},
441 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500
),
442 .driver_data
= MS_DUPLICATE_USAGES
},
443 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_POWER_COVER
),
444 .driver_data
= MS_HIDINPUT
},
445 { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_COMFORT_KEYBOARD
),
446 .driver_data
= MS_ERGONOMY
},
448 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_PRESENTER_8K_BT
),
449 .driver_data
= MS_PRESENTER
},
450 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT
, 0x091B),
451 .driver_data
= MS_SURFACE_DIAL
},
452 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_MS_XBOX_ONE_S_CONTROLLER
),
453 .driver_data
= MS_QUIRK_FF
},
454 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT
, USB_DEVICE_ID_8BITDO_SN30_PRO_PLUS
),
455 .driver_data
= MS_QUIRK_FF
},
458 MODULE_DEVICE_TABLE(hid
, ms_devices
);
460 static struct hid_driver ms_driver
= {
462 .id_table
= ms_devices
,
463 .report_fixup
= ms_report_fixup
,
464 .input_mapping
= ms_input_mapping
,
465 .input_mapped
= ms_input_mapped
,
470 module_hid_driver(ms_driver
);
472 MODULE_LICENSE("GPL");