forked from ValveSoftware/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus_udev.c
2227 lines (1922 loc) · 75.1 KB
/
bus_udev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Plug and Play support for hid devices found through udev
*
* Copyright 2016 CodeWeavers, Aric Stewart
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#if 0
#pragma makedep unix
#endif
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <poll.h>
#include <sys/ioctl.h>
#ifdef HAVE_LIBUDEV_H
# include <libudev.h>
#endif
#ifdef HAVE_LINUX_HIDRAW_H
# include <linux/hidraw.h>
#endif
#ifdef HAVE_SYS_INOTIFY_H
# include <sys/inotify.h>
#endif
#include <limits.h>
#ifdef HAVE_LINUX_INPUT_H
# include <linux/input.h>
# undef SW_MAX
# if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
# define HAS_PROPER_INPUT_HEADER
# endif
# ifndef SYN_DROPPED
# define SYN_DROPPED 3
# endif
#endif
#ifndef BUS_BLUETOOTH
# define BUS_BLUETOOTH 5
#endif
#include <pthread.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "winternl.h"
#include "ddk/wdm.h"
#include "ddk/hidtypes.h"
#include "ddk/hidsdi.h"
#include "wine/debug.h"
#include "wine/hid.h"
#include "wine/unixlib.h"
#ifdef HAS_PROPER_INPUT_HEADER
# include "hidusage.h"
#endif
#ifdef WORDS_BIGENDIAN
#define LE_DWORD(x) RtlUlongByteSwap(x)
#else
#define LE_DWORD(x) (x)
#endif
#include "unix_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(hid);
#ifdef HAVE_UDEV
static pthread_mutex_t udev_cs = PTHREAD_MUTEX_INITIALIZER;
static struct udev *udev_context = NULL;
static struct udev_monitor *udev_monitor;
static int deviceloop_control[2];
static struct list event_queue = LIST_INIT(event_queue);
static struct list device_list = LIST_INIT(device_list);
static struct udev_bus_options options;
struct base_device
{
struct unix_device unix_device;
void (*read_report)(struct unix_device *iface);
struct udev_device *udev_device;
char devnode[MAX_PATH];
int device_fd;
};
static inline struct base_device *impl_from_unix_device(struct unix_device *iface)
{
return CONTAINING_RECORD(iface, struct base_device, unix_device);
}
#define QUIRK_DS4_BT 0x1
#define QUIRK_DUALSENSE_BT 0x2
struct hidraw_device
{
struct base_device base;
DWORD quirks;
};
static inline struct hidraw_device *hidraw_impl_from_unix_device(struct unix_device *iface)
{
return CONTAINING_RECORD(impl_from_unix_device(iface), struct hidraw_device, base);
}
#ifdef HAS_PROPER_INPUT_HEADER
static const USAGE_AND_PAGE absolute_usages[] =
{
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_X}, /* ABS_X */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_Y}, /* ABS_Y */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_Z}, /* ABS_Z */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_RX}, /* ABS_RX */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_RY}, /* ABS_RY */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_RZ}, /* ABS_RZ */
{.UsagePage = HID_USAGE_PAGE_SIMULATION, .Usage = HID_USAGE_SIMULATION_THROTTLE}, /* ABS_THROTTLE */
{.UsagePage = HID_USAGE_PAGE_SIMULATION, .Usage = HID_USAGE_SIMULATION_RUDDER}, /* ABS_RUDDER */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_WHEEL}, /* ABS_WHEEL */
{.UsagePage = HID_USAGE_PAGE_SIMULATION, .Usage = HID_USAGE_SIMULATION_ACCELERATOR}, /* ABS_GAS */
{.UsagePage = HID_USAGE_PAGE_SIMULATION, .Usage = HID_USAGE_SIMULATION_BRAKE}, /* ABS_BRAKE */
{0},
{0},
{0},
{0},
{0},
{0}, /* ABS_HAT0X */
{0}, /* ABS_HAT0Y */
{0}, /* ABS_HAT1X */
{0}, /* ABS_HAT1Y */
{0}, /* ABS_HAT2X */
{0}, /* ABS_HAT2Y */
{0}, /* ABS_HAT3X */
{0}, /* ABS_HAT3Y */
{.UsagePage = HID_USAGE_PAGE_DIGITIZER, .Usage = HID_USAGE_DIGITIZER_TIP_PRESSURE}, /* ABS_PRESSURE */
{0}, /* ABS_DISTANCE */
{.UsagePage = HID_USAGE_PAGE_DIGITIZER, .Usage = HID_USAGE_DIGITIZER_X_TILT}, /* ABS_TILT_X */
{.UsagePage = HID_USAGE_PAGE_DIGITIZER, .Usage = HID_USAGE_DIGITIZER_Y_TILT}, /* ABS_TILT_Y */
{0}, /* ABS_TOOL_WIDTH */
{0},
{0},
{0},
{.UsagePage = HID_USAGE_PAGE_CONSUMER, .Usage = HID_USAGE_CONSUMER_VOLUME}, /* ABS_VOLUME */
};
static const USAGE_AND_PAGE relative_usages[] =
{
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_X}, /* REL_X */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_Y}, /* REL_Y */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_Z}, /* REL_Z */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_RX}, /* REL_RX */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_RY}, /* REL_RY */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_RZ}, /* REL_RZ */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_SLIDER},/* REL_HWHEEL */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_DIAL}, /* REL_DIAL */
{.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_WHEEL}, /* REL_WHEEL */
{0}, /* REL_MISC */
};
struct lnxev_device
{
struct base_device base;
BYTE abs_map[ARRAY_SIZE(absolute_usages)];
BYTE rel_map[ARRAY_SIZE(relative_usages)];
BYTE hat_map[8];
BYTE button_map[KEY_MAX];
int haptic_effect_id;
int effect_ids[256];
LONG effect_flags;
};
static inline struct lnxev_device *lnxev_impl_from_unix_device(struct unix_device *iface)
{
return CONTAINING_RECORD(impl_from_unix_device(iface), struct lnxev_device, base);
}
#endif /* HAS_PROPER_INPUT_HEADER */
#define MAX_DEVICES 128
static int close_fds[MAX_DEVICES];
static struct pollfd poll_fds[MAX_DEVICES];
static struct base_device *poll_devs[MAX_DEVICES];
static int close_count, poll_count;
static void stop_polling_device(struct unix_device *iface)
{
struct base_device *impl = impl_from_unix_device(iface);
int i;
if (impl->device_fd == -1) return; /* already removed */
for (i = 2; i < poll_count; ++i)
if (poll_fds[i].fd == impl->device_fd) break;
if (i == poll_count)
ERR("could not find poll entry matching device %p fd\n", iface);
else
{
poll_count--;
poll_fds[i] = poll_fds[poll_count];
poll_devs[i] = poll_devs[poll_count];
close_fds[close_count++] = impl->device_fd;
impl->device_fd = -1;
}
}
static void start_polling_device(struct unix_device *iface)
{
struct base_device *impl = impl_from_unix_device(iface);
if (poll_count >= ARRAY_SIZE(poll_fds))
ERR("could not start polling device %p, too many fds\n", iface);
else
{
poll_devs[poll_count] = impl;
poll_fds[poll_count].fd = impl->device_fd;
poll_fds[poll_count].events = POLLIN;
poll_fds[poll_count].revents = 0;
poll_count++;
write(deviceloop_control[1], "u", 1);
}
}
static struct base_device *find_device_from_fd(int fd)
{
int i;
for (i = 2; i < poll_count; ++i) if (poll_fds[i].fd == fd) break;
if (i < poll_count) return poll_devs[i];
return NULL;
}
static struct base_device *find_device_from_devnode(const char *path)
{
struct base_device *impl;
LIST_FOR_EACH_ENTRY(impl, &device_list, struct base_device, unix_device.entry)
if (!strcmp(impl->devnode, path)) return impl;
return NULL;
}
static void hidraw_device_destroy(struct unix_device *iface)
{
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
udev_device_unref(impl->base.udev_device);
}
static NTSTATUS hidraw_device_start(struct unix_device *iface)
{
pthread_mutex_lock(&udev_cs);
start_polling_device(iface);
pthread_mutex_unlock(&udev_cs);
return STATUS_SUCCESS;
}
static void hidraw_device_stop(struct unix_device *iface)
{
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
pthread_mutex_lock(&udev_cs);
stop_polling_device(iface);
list_remove(&impl->base.unix_device.entry);
pthread_mutex_unlock(&udev_cs);
}
static NTSTATUS hidraw_device_get_report_descriptor(struct unix_device *iface, BYTE *buffer,
UINT length, UINT *out_length)
{
#ifdef HAVE_LINUX_HIDRAW_H
struct hidraw_report_descriptor descriptor;
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
if (ioctl(impl->base.device_fd, HIDIOCGRDESCSIZE, &descriptor.size) == -1)
{
WARN("ioctl(HIDIOCGRDESCSIZE) failed: %d %s\n", errno, strerror(errno));
return STATUS_UNSUCCESSFUL;
}
*out_length = descriptor.size;
if (length < descriptor.size)
return STATUS_BUFFER_TOO_SMALL;
if (!descriptor.size)
return STATUS_SUCCESS;
if (ioctl(impl->base.device_fd, HIDIOCGRDESC, &descriptor) == -1)
{
WARN("ioctl(HIDIOCGRDESC) failed: %d %s\n", errno, strerror(errno));
return STATUS_UNSUCCESSFUL;
}
memcpy(buffer, descriptor.value, descriptor.size);
return STATUS_SUCCESS;
#else
return STATUS_NOT_IMPLEMENTED;
#endif
}
static void hidraw_device_read_report(struct unix_device *iface)
{
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
BYTE report_buffer[1024], *buff = report_buffer;
int size = read(impl->base.device_fd, report_buffer, sizeof(report_buffer));
if (size == -1)
TRACE("Read failed. Likely an unplugged device %d %s\n", errno, strerror(errno));
else if (size == 0)
TRACE("Failed to read report\n");
else
{
/* As described in the Linux kernel driver, when connected over bluetooth, DS4 controllers
* start sending input through report #17 as soon as they receive a feature report #2, which
* the kernel sends anyway for calibration.
*
* Input report #17 is the same as the default input report #1, with additional gyro data and
* two additional bytes in front, but is only described as vendor specific in the report descriptor,
* and applications aren't expecting it.
*
* We have to translate it to input report #1, like native driver does.
*/
if ((impl->quirks & QUIRK_DS4_BT) && report_buffer[0] == 0x11 && size >= 12)
{
size = 10;
buff += 2;
buff[0] = 1;
}
/* The behavior of DualSense is very similar to DS4 described above with a few exceptions.
*
* The report number #41 is used for the extended bluetooth input report. The report comes
* with only one extra byte in front and the format is not exactly the same as the one used
* for the report #1 so we need to shuffle a few bytes around.
*
* Basic #1 report:
* X Y Z RZ Buttons[3] TriggerLeft TriggerRight
*
* Extended #41 report:
* Prefix X Y Z Rz TriggerLeft TriggerRight Counter Buttons[3] ...
*/
if ((impl->quirks & QUIRK_DUALSENSE_BT) && report_buffer[0] == 0x31 && size >= 11)
{
BYTE trigger[2];
size = 10;
buff += 1;
buff[0] = 1; /* fake report #1 */
trigger[0] = buff[5]; /* TriggerLeft*/
trigger[1] = buff[6]; /* TriggerRight */
buff[5] = buff[8]; /* Buttons[0] */
buff[6] = buff[9]; /* Buttons[1] */
buff[7] = buff[10]; /* Buttons[2] */
buff[8] = trigger[0]; /* TriggerLeft */
buff[9] = trigger[1]; /* TirggerRight */
}
bus_event_queue_input_report(&event_queue, iface, buff, size);
}
}
static void hidraw_disable_sony_quirks(struct unix_device *iface)
{
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
/* FIXME: we may want to validate CRC at the end of the outbound HID reports,
* as controllers do not switch modes if it is incorrect.
*/
if ((impl->quirks & QUIRK_DS4_BT))
{
TRACE("Disabling report quirk for Bluetooth DualShock4 controller iface %p\n", iface);
impl->quirks &= ~QUIRK_DS4_BT;
}
if ((impl->quirks & QUIRK_DUALSENSE_BT))
{
TRACE("Disabling report quirk for Bluetooth DualSense controller iface %p\n", iface);
impl->quirks &= ~QUIRK_DUALSENSE_BT;
}
}
static void hidraw_device_set_output_report(struct unix_device *iface, HID_XFER_PACKET *packet, IO_STATUS_BLOCK *io)
{
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
unsigned int length = packet->reportBufferLen;
BYTE buffer[8192];
int count = 0;
if ((buffer[0] = packet->reportId))
count = write(impl->base.device_fd, packet->reportBuffer, length);
else if (length > sizeof(buffer) - 1)
ERR("id %d length %u >= 8192, cannot write\n", packet->reportId, length);
else
{
memcpy(buffer + 1, packet->reportBuffer, length);
count = write(impl->base.device_fd, buffer, length + 1);
}
if (count > 0)
{
hidraw_disable_sony_quirks(iface);
io->Information = count;
io->Status = STATUS_SUCCESS;
}
else
{
ERR("id %d write failed error: %d %s\n", packet->reportId, errno, strerror(errno));
io->Information = 0;
io->Status = STATUS_UNSUCCESSFUL;
}
}
static void hidraw_device_get_feature_report(struct unix_device *iface, HID_XFER_PACKET *packet,
IO_STATUS_BLOCK *io)
{
#if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCGFEATURE)
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
unsigned int length = packet->reportBufferLen;
BYTE buffer[8192];
int count = 0;
if ((buffer[0] = packet->reportId) && length <= 0x1fff)
count = ioctl(impl->base.device_fd, HIDIOCGFEATURE(length), packet->reportBuffer);
else if (length > sizeof(buffer) - 1)
ERR("id %d length %u >= 8192, cannot read\n", packet->reportId, length);
else
{
count = ioctl(impl->base.device_fd, HIDIOCGFEATURE(length + 1), buffer);
memcpy(packet->reportBuffer, buffer + 1, length);
}
if (count > 0)
{
hidraw_disable_sony_quirks(iface);
io->Information = count;
io->Status = STATUS_SUCCESS;
}
else
{
ERR("id %d read failed, error: %d %s\n", packet->reportId, errno, strerror(errno));
io->Information = 0;
io->Status = STATUS_UNSUCCESSFUL;
}
#else
io->Information = 0;
io->Status = STATUS_NOT_IMPLEMENTED;
#endif
}
static void hidraw_device_set_feature_report(struct unix_device *iface, HID_XFER_PACKET *packet,
IO_STATUS_BLOCK *io)
{
#if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCSFEATURE)
struct hidraw_device *impl = hidraw_impl_from_unix_device(iface);
unsigned int length = packet->reportBufferLen;
BYTE buffer[8192];
int count = 0;
if ((buffer[0] = packet->reportId) && length <= 0x1fff)
count = ioctl(impl->base.device_fd, HIDIOCSFEATURE(length), packet->reportBuffer);
else if (length > sizeof(buffer) - 1)
ERR("id %d length %u >= 8192, cannot write\n", packet->reportId, length);
else
{
memcpy(buffer + 1, packet->reportBuffer, length);
count = ioctl(impl->base.device_fd, HIDIOCSFEATURE(length + 1), buffer);
}
if (count > 0)
{
hidraw_disable_sony_quirks(iface);
io->Information = count;
io->Status = STATUS_SUCCESS;
}
else
{
ERR("id %d write failed, error: %d %s\n", packet->reportId, errno, strerror(errno));
io->Information = 0;
io->Status = STATUS_UNSUCCESSFUL;
}
#else
io->Information = 0;
io->Status = STATUS_NOT_IMPLEMENTED;
#endif
}
static const struct raw_device_vtbl hidraw_device_vtbl =
{
hidraw_device_destroy,
hidraw_device_start,
hidraw_device_stop,
hidraw_device_get_report_descriptor,
hidraw_device_set_output_report,
hidraw_device_get_feature_report,
hidraw_device_set_feature_report,
};
#ifdef HAS_PROPER_INPUT_HEADER
static const char *get_device_syspath(struct udev_device *dev)
{
struct udev_device *parent;
if ((parent = udev_device_get_parent_with_subsystem_devtype(dev, "hid", NULL)))
return udev_device_get_syspath(parent);
if ((parent = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device")))
return udev_device_get_syspath(parent);
return "";
}
static struct base_device *find_device_from_syspath(const char *path)
{
struct base_device *impl;
LIST_FOR_EACH_ENTRY(impl, &device_list, struct base_device, unix_device.entry)
if (!strcmp(get_device_syspath(impl->udev_device), path)) return impl;
return NULL;
}
#define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
/* Minimal compatibility with code taken from steam-runtime-tools */
typedef int gboolean;
#define g_debug(fmt, ...) TRACE(fmt "\n", ## __VA_ARGS__)
#define G_N_ELEMENTS(arr) (sizeof(arr)/sizeof(arr[0]))
typedef enum
{
SRT_INPUT_DEVICE_TYPE_FLAGS_JOYSTICK = (1 << 0),
SRT_INPUT_DEVICE_TYPE_FLAGS_ACCELEROMETER = (1 << 1),
SRT_INPUT_DEVICE_TYPE_FLAGS_KEYBOARD = (1 << 2),
SRT_INPUT_DEVICE_TYPE_FLAGS_HAS_KEYS = (1 << 3),
SRT_INPUT_DEVICE_TYPE_FLAGS_MOUSE = (1 << 4),
SRT_INPUT_DEVICE_TYPE_FLAGS_TOUCHPAD = (1 << 5),
SRT_INPUT_DEVICE_TYPE_FLAGS_TOUCHSCREEN = (1 << 6),
SRT_INPUT_DEVICE_TYPE_FLAGS_TABLET = (1 << 7),
SRT_INPUT_DEVICE_TYPE_FLAGS_TABLET_PAD = (1 << 8),
SRT_INPUT_DEVICE_TYPE_FLAGS_POINTING_STICK = (1 << 9),
SRT_INPUT_DEVICE_TYPE_FLAGS_SWITCH = (1 << 10),
SRT_INPUT_DEVICE_TYPE_FLAGS_NONE = 0
} SrtInputDeviceTypeFlags;
#define BITS_PER_LONG (sizeof (unsigned long) * CHAR_BIT)
#define LONGS_FOR_BITS(x) ((((x)-1)/BITS_PER_LONG)+1)
typedef struct
{
unsigned long ev[LONGS_FOR_BITS (EV_MAX)];
unsigned long keys[LONGS_FOR_BITS (KEY_MAX)];
unsigned long abs[LONGS_FOR_BITS (ABS_MAX)];
unsigned long rel[LONGS_FOR_BITS (REL_MAX)];
unsigned long ff[LONGS_FOR_BITS (FF_MAX)];
unsigned long props[LONGS_FOR_BITS (INPUT_PROP_MAX)];
} SrtEvdevCapabilities;
static gboolean
_srt_get_caps_from_evdev (int fd,
unsigned int type,
unsigned long *bitmask,
size_t bitmask_len_longs)
{
size_t bitmask_len_bytes = bitmask_len_longs * sizeof (*bitmask);
memset (bitmask, 0, bitmask_len_bytes);
if (ioctl (fd, EVIOCGBIT (type, bitmask_len_bytes), bitmask) < 0)
return FALSE;
return TRUE;
}
static gboolean
_srt_evdev_capabilities_set_from_evdev (SrtEvdevCapabilities *caps,
int fd)
{
if (_srt_get_caps_from_evdev (fd, 0, caps->ev, G_N_ELEMENTS (caps->ev)))
{
_srt_get_caps_from_evdev (fd, EV_KEY, caps->keys, G_N_ELEMENTS (caps->keys));
_srt_get_caps_from_evdev (fd, EV_ABS, caps->abs, G_N_ELEMENTS (caps->abs));
_srt_get_caps_from_evdev (fd, EV_REL, caps->rel, G_N_ELEMENTS (caps->rel));
_srt_get_caps_from_evdev (fd, EV_FF, caps->ff, G_N_ELEMENTS (caps->ff));
ioctl (fd, EVIOCGPROP (sizeof (caps->props)), caps->props);
return TRUE;
}
memset (caps, 0, sizeof (*caps));
return FALSE;
}
#define JOYSTICK_ABS_AXES \
((1 << ABS_X) | (1 << ABS_Y) \
| (1 << ABS_RX) | (1 << ABS_RY) \
| (1 << ABS_THROTTLE) | (1 << ABS_RUDDER) \
| (1 << ABS_WHEEL) | (1 << ABS_GAS) | (1 << ABS_BRAKE) \
| (1 << ABS_HAT0X) | (1 << ABS_HAT0Y) \
| (1 << ABS_HAT1X) | (1 << ABS_HAT1Y) \
| (1 << ABS_HAT2X) | (1 << ABS_HAT2Y) \
| (1 << ABS_HAT3X) | (1 << ABS_HAT3Y))
static const unsigned int first_mouse_button = BTN_MOUSE;
static const unsigned int last_mouse_button = BTN_JOYSTICK - 1;
static const unsigned int first_joystick_button = BTN_JOYSTICK;
static const unsigned int last_joystick_button = BTN_GAMEPAD - 1;
static const unsigned int first_gamepad_button = BTN_GAMEPAD;
static const unsigned int last_gamepad_button = BTN_DIGI - 1;
static const unsigned int first_dpad_button = BTN_DPAD_UP;
static const unsigned int last_dpad_button = BTN_DPAD_RIGHT;
static const unsigned int first_extra_joystick_button = BTN_TRIGGER_HAPPY;
static const unsigned int last_extra_joystick_button = BTN_TRIGGER_HAPPY40;
SrtInputDeviceTypeFlags
_srt_evdev_capabilities_guess_type (const SrtEvdevCapabilities *caps)
{
SrtInputDeviceTypeFlags flags = SRT_INPUT_DEVICE_TYPE_FLAGS_NONE;
unsigned int i;
gboolean has_joystick_axes = FALSE;
gboolean has_joystick_buttons = FALSE;
/* Some properties let us be fairly sure about a device */
if (test_bit (caps->props, INPUT_PROP_ACCELEROMETER))
{
g_debug ("INPUT_PROP_ACCELEROMETER => is accelerometer");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_ACCELEROMETER;
}
if (test_bit (caps->props, INPUT_PROP_POINTING_STICK))
{
g_debug ("INPUT_PROP_POINTING_STICK => is pointing stick");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_POINTING_STICK;
}
if (test_bit (caps->props, INPUT_PROP_BUTTONPAD)
|| test_bit (caps->props, INPUT_PROP_TOPBUTTONPAD))
{
g_debug ("INPUT_PROP_[TOP]BUTTONPAD => is touchpad");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_TOUCHPAD;
}
/* Devices with a stylus or pen are assumed to be graphics tablets */
if (test_bit (caps->keys, BTN_STYLUS)
|| test_bit (caps->keys, BTN_TOOL_PEN))
{
g_debug ("Stylus or pen => is tablet");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_TABLET;
}
/* Devices that accept a finger touch are assumed to be touchpads or
* touchscreens.
*
* In Steam we mostly only care about these as a way to
* reject non-joysticks, so we're not very precise here yet.
*
* SDL assumes that TOUCH means a touchscreen and FINGER
* means a touchpad. */
if (flags == SRT_INPUT_DEVICE_TYPE_FLAGS_NONE
&& (test_bit (caps->keys, BTN_TOOL_FINGER)
|| test_bit (caps->keys, BTN_TOUCH)
|| test_bit (caps->props, INPUT_PROP_SEMI_MT)))
{
g_debug ("Finger or touch or semi-MT => is touchpad or touchscreen");
if (test_bit (caps->props, INPUT_PROP_POINTER))
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_TOUCHPAD;
else
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_TOUCHSCREEN;
}
/* Devices with mouse buttons are ... probably mice? */
if (flags == SRT_INPUT_DEVICE_TYPE_FLAGS_NONE)
{
for (i = first_mouse_button; i <= last_mouse_button; i++)
{
if (test_bit (caps->keys, i))
{
g_debug ("Mouse button => mouse");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_MOUSE;
}
}
}
if (flags == SRT_INPUT_DEVICE_TYPE_FLAGS_NONE)
{
for (i = ABS_X; i < ABS_Z; i++)
{
if (!test_bit (caps->abs, i))
break;
}
/* If it has 3 axes and no buttons it's probably an accelerometer. */
if (i == ABS_Z && !test_bit (caps->ev, EV_KEY))
{
g_debug ("3 left axes and no buttons => accelerometer");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_ACCELEROMETER;
}
/* Same for RX..RZ (e.g. Wiimote) */
for (i = ABS_RX; i < ABS_RZ; i++)
{
if (!test_bit (caps->abs, i))
break;
}
if (i == ABS_RZ && !test_bit (caps->ev, EV_KEY))
{
g_debug ("3 right axes and no buttons => accelerometer");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_ACCELEROMETER;
}
}
/* Bits 1 to 31 are ESC, numbers and Q to D, which SDL and udev both
* consider to be enough to count as a fully-functioned keyboard. */
if ((caps->keys[0] & 0xfffffffe) == 0xfffffffe)
{
g_debug ("First few keys => keyboard");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_KEYBOARD;
}
/* If we have *any* keys, consider it to be something a bit
* keyboard-like. Bits 0 to 63 are all keyboard keys.
* Make sure we stop before reaching KEY_UP which is sometimes
* used on game controller mappings, e.g. for the Wiimote. */
for (i = 0; i < (64 / BITS_PER_LONG); i++)
{
if (caps->keys[i] != 0)
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_HAS_KEYS;
}
if (caps->abs[0] & JOYSTICK_ABS_AXES)
has_joystick_axes = TRUE;
/* Flight stick buttons */
for (i = first_joystick_button; i <= last_joystick_button; i++)
{
if (test_bit (caps->keys, i))
has_joystick_buttons = TRUE;
}
/* Gamepad buttons (Xbox, PS3, etc.) */
for (i = first_gamepad_button; i <= last_gamepad_button; i++)
{
if (test_bit (caps->keys, i))
has_joystick_buttons = TRUE;
}
/* Gamepad digital dpad */
for (i = first_dpad_button; i <= last_dpad_button; i++)
{
if (test_bit (caps->keys, i))
has_joystick_buttons = TRUE;
}
/* Steering wheel gear-change buttons */
for (i = BTN_GEAR_DOWN; i <= BTN_GEAR_UP; i++)
{
if (test_bit (caps->keys, i))
has_joystick_buttons = TRUE;
}
/* Reserved space for extra game-controller buttons, e.g. on Corsair
* gaming keyboards */
for (i = first_extra_joystick_button; i <= last_extra_joystick_button; i++)
{
if (test_bit (caps->keys, i))
has_joystick_buttons = TRUE;
}
if (test_bit (caps->keys, last_mouse_button))
{
/* Mice with a very large number of buttons can apparently
* overflow into the joystick-button space, but they're still not
* joysticks. */
has_joystick_buttons = FALSE;
}
/* TODO: Do we want to consider BTN_0 up to BTN_9 to be joystick buttons?
* libmanette and SDL look for BTN_1, udev does not.
*
* They're used by some game controllers, like BTN_1 and BTN_2 for the
* Wiimote, BTN_1..BTN_9 for the SpaceTec SpaceBall and BTN_0..BTN_3
* for Playstation dance pads, but they're also used by
* non-game-controllers like Logitech mice. For now we entirely ignore
* these buttons: they are not evidence that it's a joystick, but
* neither are they evidence that it *isn't* a joystick. */
/* We consider it to be a joystick if there is some evidence that it is,
* and no evidence that it's something else.
*
* Unlike SDL, we accept devices with only axes and no buttons as a
* possible joystick, unless they have X/Y/Z axes in which case we
* assume they're accelerometers. */
if ((has_joystick_buttons || has_joystick_axes)
&& (flags == SRT_INPUT_DEVICE_TYPE_FLAGS_NONE))
{
g_debug ("Looks like a joystick");
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_JOYSTICK;
}
/* If we have *any* keys below BTN_MISC, consider it to be something
* a bit keyboard-like, but don't rule out *also* being considered
* to be a joystick (again for e.g. the Wiimote). */
for (i = 0; i < (BTN_MISC / BITS_PER_LONG); i++)
{
if (caps->keys[i] != 0)
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_HAS_KEYS;
}
/* Also non-exclusive: don't rule out a device being a joystick and
* having a switch */
if (test_bit (caps->ev, EV_SW))
flags |= SRT_INPUT_DEVICE_TYPE_FLAGS_SWITCH;
return flags;
}
static const USAGE_AND_PAGE *what_am_I(struct udev_device *dev, int fd)
{
static const USAGE_AND_PAGE Unknown = {.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = 0};
static const USAGE_AND_PAGE Mouse = {.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_MOUSE};
static const USAGE_AND_PAGE Keyboard = {.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_KEYBOARD};
static const USAGE_AND_PAGE Gamepad = {.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_GAMEPAD};
static const USAGE_AND_PAGE Keypad = {.UsagePage = HID_USAGE_PAGE_GENERIC, .Usage = HID_USAGE_GENERIC_KEYPAD};
static const USAGE_AND_PAGE Tablet = {.UsagePage = HID_USAGE_PAGE_DIGITIZER, .Usage = HID_USAGE_DIGITIZER_PEN};
static const USAGE_AND_PAGE Touchscreen = {.UsagePage = HID_USAGE_PAGE_DIGITIZER, .Usage = HID_USAGE_DIGITIZER_TOUCH_SCREEN};
static const USAGE_AND_PAGE Touchpad = {.UsagePage = HID_USAGE_PAGE_DIGITIZER, .Usage = HID_USAGE_DIGITIZER_TOUCH_PAD};
SrtEvdevCapabilities caps;
struct udev_device *parent = dev;
/* Look to the parents until we get a clue */
while (parent)
{
if (udev_device_get_property_value(parent, "ID_INPUT_MOUSE"))
return &Mouse;
else if (udev_device_get_property_value(parent, "ID_INPUT_KEYBOARD"))
return &Keyboard;
else if (udev_device_get_property_value(parent, "ID_INPUT_JOYSTICK"))
return &Gamepad;
else if (udev_device_get_property_value(parent, "ID_INPUT_KEY"))
return &Keypad;
else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHPAD"))
return &Touchpad;
else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHSCREEN"))
return &Touchscreen;
else if (udev_device_get_property_value(parent, "ID_INPUT_TABLET"))
return &Tablet;
parent = udev_device_get_parent_with_subsystem_devtype(parent, "input", NULL);
}
/* In a container, udev properties might not be available. Fall back to deriving the device
* type from the fd's evdev capabilities. */
if (_srt_evdev_capabilities_set_from_evdev (&caps, fd))
{
SrtInputDeviceTypeFlags guessed_type;
guessed_type = _srt_evdev_capabilities_guess_type (&caps);
if (guessed_type & (SRT_INPUT_DEVICE_TYPE_FLAGS_MOUSE
| SRT_INPUT_DEVICE_TYPE_FLAGS_POINTING_STICK))
return &Mouse;
else if (guessed_type & SRT_INPUT_DEVICE_TYPE_FLAGS_KEYBOARD)
return &Keyboard;
else if (guessed_type & SRT_INPUT_DEVICE_TYPE_FLAGS_JOYSTICK)
return &Gamepad;
else if (guessed_type & SRT_INPUT_DEVICE_TYPE_FLAGS_HAS_KEYS)
return &Keypad;
else if (guessed_type & SRT_INPUT_DEVICE_TYPE_FLAGS_TOUCHPAD)
return &Touchpad;
else if (guessed_type & SRT_INPUT_DEVICE_TYPE_FLAGS_TOUCHSCREEN)
return &Touchscreen;
else if (guessed_type & SRT_INPUT_DEVICE_TYPE_FLAGS_TABLET)
return &Tablet;
/* Mapped to Unknown: ACCELEROMETER, TABLET_PAD, SWITCH. */
}
return &Unknown;
}
static INT count_buttons(int device_fd, BYTE *map)
{
int i;
int button_count = 0;
BYTE keybits[(KEY_MAX+7)/8];
if (ioctl(device_fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits) == -1)
{
WARN("ioctl(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno, strerror(errno));
return FALSE;
}
for (i = BTN_MISC; i < KEY_MAX; i++)
{
if (test_bit(keybits, i))
{
if (map) map[i] = button_count;
button_count++;
}
}
return button_count;
}
static INT count_abs_axis(int device_fd)
{
BYTE absbits[(ABS_MAX+7)/8];
int abs_count = 0;
int i;
if (ioctl(device_fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits) == -1)
{
WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
return 0;
}
for (i = 0; i < ARRAY_SIZE(absolute_usages); i++)
if (test_bit(absbits, i)) abs_count++;
return abs_count;
}
static NTSTATUS build_report_descriptor(struct unix_device *iface, struct udev_device *dev)
{
struct input_absinfo abs_info[ARRAY_SIZE(absolute_usages)];
BYTE absbits[(ABS_MAX+7)/8];
BYTE relbits[(REL_MAX+7)/8];
BYTE ffbits[(FF_MAX+7)/8];
struct ff_effect effect;
USAGE_AND_PAGE usage;
USHORT count = 0;
USAGE usages[16];
INT i, button_count, abs_count, rel_count, hat_count;
struct lnxev_device *impl = lnxev_impl_from_unix_device(iface);
const USAGE_AND_PAGE device_usage = *what_am_I(dev, impl->base.device_fd);
if (ioctl(impl->base.device_fd, EVIOCGBIT(EV_REL, sizeof(relbits)), relbits) == -1)
{
WARN("ioctl(EVIOCGBIT, EV_REL) failed: %d %s\n", errno, strerror(errno));
memset(relbits, 0, sizeof(relbits));
}
if (ioctl(impl->base.device_fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits) == -1)
{
WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
memset(absbits, 0, sizeof(absbits));
}
if (ioctl(impl->base.device_fd, EVIOCGBIT(EV_FF, sizeof(ffbits)), ffbits) == -1)
{
WARN("ioctl(EVIOCGBIT, EV_FF) failed: %d %s\n", errno, strerror(errno));
memset(ffbits, 0, sizeof(ffbits));
}
if (!hid_device_begin_report_descriptor(iface, &device_usage))
return STATUS_NO_MEMORY;
if (!hid_device_begin_input_report(iface, &device_usage))
return STATUS_NO_MEMORY;
abs_count = 0;
for (i = 0; i < ARRAY_SIZE(absolute_usages); i++)
{
usage = absolute_usages[i];
if (!test_bit(absbits, i)) continue;