2 * Plug and Play support for hid devices found through udev
4 * Copyright 2016 CodeWeavers, Aric Stewart
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #ifdef HAVE_SYS_POLL_H
34 # include <sys/poll.h>
39 #ifdef HAVE_LINUX_HIDRAW_H
40 # include <linux/hidraw.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
46 #ifdef HAVE_LINUX_INPUT_H
47 # include <linux/input.h>
49 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
50 # define HAS_PROPER_INPUT_HEADER
53 # define SYN_DROPPED 3
57 #define NONAMELESSUNION
60 #define WIN32_NO_STATUS
66 #include "ddk/hidtypes.h"
67 #include "wine/debug.h"
68 #include "wine/heap.h"
69 #include "wine/unicode.h"
71 #ifdef HAS_PROPER_INPUT_HEADER
72 # include "hidusage.h"
75 #ifdef WORDS_BIGENDIAN
76 #define LE_WORD(x) RtlUshortByteSwap(x)
77 #define LE_DWORD(x) RtlUlongByteSwap(x)
79 #define LE_WORD(x) (x)
80 #define LE_DWORD(x) (x)
83 #include "controller.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(plugplay
);
90 WINE_DECLARE_DEBUG_CHANNEL(hid_report
);
92 static struct udev
*udev_context
= NULL
;
93 static DWORD disable_hidraw
= 0;
94 static DWORD disable_input
= 0;
95 static HANDLE deviceloop_handle
;
96 static int deviceloop_control
[2];
98 static const WCHAR hidraw_busidW
[] = {'H','I','D','R','A','W',0};
99 static const WCHAR lnxev_busidW
[] = {'L','N','X','E','V',0};
101 struct platform_private
103 struct udev_device
*udev_device
;
106 HANDLE report_thread
;
110 static inline struct platform_private
*impl_from_DEVICE_OBJECT(DEVICE_OBJECT
*device
)
112 return (struct platform_private
*)get_platform_private(device
);
115 #ifdef HAS_PROPER_INPUT_HEADER
117 static const BYTE REPORT_ABS_AXIS_TAIL
[] = {
118 0x17, 0x00, 0x00, 0x00, 0x00, /* LOGICAL_MINIMUM (0) */
119 0x27, 0xff, 0x00, 0x00, 0x00, /* LOGICAL_MAXIMUM (0xff) */
120 0x37, 0x00, 0x00, 0x00, 0x00, /* PHYSICAL_MINIMUM (0) */
121 0x47, 0xff, 0x00, 0x00, 0x00, /* PHYSICAL_MAXIMUM (256) */
122 0x75, 0x20, /* REPORT_SIZE (32) */
123 0x95, 0x00, /* REPORT_COUNT (2) */
124 0x81, 0x02, /* INPUT (Data,Var,Abs) */
126 #define IDX_ABS_LOG_MINIMUM 1
127 #define IDX_ABS_LOG_MAXIMUM 6
128 #define IDX_ABS_PHY_MINIMUM 11
129 #define IDX_ABS_PHY_MAXIMUM 16
130 #define IDX_ABS_AXIS_COUNT 23
132 static const BYTE ABS_TO_HID_MAP
[][2] = {
133 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_X
}, /*ABS_X*/
134 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_Y
}, /*ABS_Y*/
135 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_Z
}, /*ABS_Z*/
136 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RX
}, /*ABS_RX*/
137 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RY
}, /*ABS_RY*/
138 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RZ
}, /*ABS_RZ*/
139 {HID_USAGE_PAGE_SIMULATION
, HID_USAGE_SIMULATION_THROTTLE
}, /*ABS_THROTTLE*/
140 {HID_USAGE_PAGE_SIMULATION
, HID_USAGE_SIMULATION_RUDDER
}, /*ABS_RUDDER*/
141 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_WHEEL
}, /*ABS_WHEEL*/
142 {HID_USAGE_PAGE_SIMULATION
, 0xC4}, /*ABS_GAS*/
143 {HID_USAGE_PAGE_SIMULATION
, 0xC5}, /*ABS_BRAKE*/
152 {HID_USAGE_PAGE_DIGITIZER
, 0x30}, /*ABS_PRESSURE*/
153 {0, 0}, /*ABS_DISTANCE*/
154 {HID_USAGE_PAGE_DIGITIZER
, 0x3D}, /*ABS_TILT_X*/
155 {HID_USAGE_PAGE_DIGITIZER
, 0x3F}, /*ABS_TILT_Y*/
156 {0, 0}, /*ABS_TOOL_WIDTH*/
160 {HID_USAGE_PAGE_CONSUMER
, 0xE0} /*ABS_VOLUME*/
162 #define HID_ABS_MAX (ABS_VOLUME+1)
163 #define TOP_ABS_PAGE (HID_USAGE_PAGE_DIGITIZER+1)
165 static const BYTE REL_TO_HID_MAP
[][2] = {
166 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_X
}, /* REL_X */
167 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_Y
}, /* REL_Y */
168 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_Z
}, /* REL_Z */
169 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RX
}, /* REL_RX */
170 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RY
}, /* REL_RY */
171 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RZ
}, /* REL_RZ */
172 {0, 0}, /* REL_HWHEEL */
173 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_DIAL
}, /* REL_DIAL */
174 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_WHEEL
}, /* REL_WHEEL */
175 {0, 0} /* REL_MISC */
178 #define HID_REL_MAX (REL_MISC+1)
179 #define TOP_REL_PAGE (HID_USAGE_PAGE_CONSUMER+1)
181 struct wine_input_absinfo
{
182 struct input_absinfo info
;
186 struct wine_input_private
{
187 struct platform_private base
;
190 BYTE
*last_report_buffer
;
191 BYTE
*current_report_buffer
;
192 enum { FIRST
, NORMAL
, DROPPED
} report_state
;
194 int report_descriptor_size
;
195 BYTE
*report_descriptor
;
198 BYTE button_map
[KEY_MAX
];
199 BYTE rel_map
[HID_REL_MAX
];
202 struct wine_input_absinfo abs_map
[HID_ABS_MAX
];
205 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
207 static BYTE
*add_axis_block(BYTE
*report_ptr
, BYTE count
, BYTE page
, BYTE
*usages
, BOOL absolute
, const struct wine_input_absinfo
*absinfo
)
210 memcpy(report_ptr
, REPORT_AXIS_HEADER
, sizeof(REPORT_AXIS_HEADER
));
211 report_ptr
[IDX_AXIS_PAGE
] = page
;
212 report_ptr
+= sizeof(REPORT_AXIS_HEADER
);
213 for (i
= 0; i
< count
; i
++)
215 memcpy(report_ptr
, REPORT_AXIS_USAGE
, sizeof(REPORT_AXIS_USAGE
));
216 report_ptr
[IDX_AXIS_USAGE
] = usages
[i
];
217 report_ptr
+= sizeof(REPORT_AXIS_USAGE
);
221 memcpy(report_ptr
, REPORT_ABS_AXIS_TAIL
, sizeof(REPORT_ABS_AXIS_TAIL
));
224 *((int*)&report_ptr
[IDX_ABS_LOG_MINIMUM
]) = LE_DWORD(absinfo
->info
.minimum
);
225 *((int*)&report_ptr
[IDX_ABS_LOG_MAXIMUM
]) = LE_DWORD(absinfo
->info
.maximum
);
226 *((int*)&report_ptr
[IDX_ABS_PHY_MINIMUM
]) = LE_DWORD(absinfo
->info
.minimum
);
227 *((int*)&report_ptr
[IDX_ABS_PHY_MAXIMUM
]) = LE_DWORD(absinfo
->info
.maximum
);
229 report_ptr
[IDX_ABS_AXIS_COUNT
] = count
;
230 report_ptr
+= sizeof(REPORT_ABS_AXIS_TAIL
);
234 memcpy(report_ptr
, REPORT_REL_AXIS_TAIL
, sizeof(REPORT_REL_AXIS_TAIL
));
235 report_ptr
[IDX_REL_AXIS_COUNT
] = count
;
236 report_ptr
+= sizeof(REPORT_REL_AXIS_TAIL
);
241 static const BYTE
* what_am_I(struct udev_device
*dev
)
243 static const BYTE Unknown
[2] = {HID_USAGE_PAGE_GENERIC
, 0};
244 static const BYTE Mouse
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_MOUSE
};
245 static const BYTE Keyboard
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_KEYBOARD
};
246 static const BYTE Gamepad
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_GAMEPAD
};
247 static const BYTE Keypad
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_KEYPAD
};
248 static const BYTE Tablet
[2] = {HID_USAGE_PAGE_DIGITIZER
, 0x2};
249 static const BYTE Touchscreen
[2] = {HID_USAGE_PAGE_DIGITIZER
, 0x4};
250 static const BYTE Touchpad
[2] = {HID_USAGE_PAGE_DIGITIZER
, 0x5};
252 struct udev_device
*parent
= dev
;
254 /* Look to the parents until we get a clue */
257 if (udev_device_get_property_value(parent
, "ID_INPUT_MOUSE"))
259 else if (udev_device_get_property_value(parent
, "ID_INPUT_KEYBOARD"))
261 else if (udev_device_get_property_value(parent
, "ID_INPUT_JOYSTICK"))
263 else if (udev_device_get_property_value(parent
, "ID_INPUT_KEY"))
265 else if (udev_device_get_property_value(parent
, "ID_INPUT_TOUCHPAD"))
267 else if (udev_device_get_property_value(parent
, "ID_INPUT_TOUCHSCREEN"))
269 else if (udev_device_get_property_value(parent
, "ID_INPUT_TABLET"))
272 parent
= udev_device_get_parent_with_subsystem_devtype(parent
, "input", NULL
);
277 static void set_button_value(int index
, int value
, BYTE
* buffer
)
279 int bindex
= index
/ 8;
285 buffer
[bindex
] = buffer
[bindex
] | mask
;
289 buffer
[bindex
] = buffer
[bindex
] & mask
;
293 static void set_abs_axis_value(struct wine_input_private
*ext
, int code
, int value
)
296 /* check for hatswitches */
297 if (code
<= ABS_HAT3Y
&& code
>= ABS_HAT0X
)
299 index
= code
- ABS_HAT0X
;
300 ext
->hat_values
[index
] = value
;
301 if ((code
- ABS_HAT0X
) % 2)
306 if (ext
->hat_values
[index
] == 0)
308 if (ext
->hat_values
[index
+1] == 0)
310 else if (ext
->hat_values
[index
+1] < 0)
315 else if (ext
->hat_values
[index
] > 0)
317 if (ext
->hat_values
[index
+1] == 0)
319 else if (ext
->hat_values
[index
+1] < 0)
326 if (ext
->hat_values
[index
+1] == 0)
328 else if (ext
->hat_values
[index
+1] < 0)
333 ext
->current_report_buffer
[ext
->hat_map
[index
]] = value
;
335 else if (code
< HID_ABS_MAX
&& ABS_TO_HID_MAP
[code
][0] != 0)
337 index
= ext
->abs_map
[code
].report_index
;
338 *((DWORD
*)&ext
->current_report_buffer
[index
]) = LE_DWORD(value
);
342 static void set_rel_axis_value(struct wine_input_private
*ext
, int code
, int value
)
345 if (code
< HID_REL_MAX
&& REL_TO_HID_MAP
[code
][0] != 0)
347 index
= ext
->rel_map
[code
];
348 if (value
> 127) value
= 127;
349 if (value
< -127) value
= -127;
350 ext
->current_report_buffer
[index
] = value
;
354 static INT
count_buttons(int device_fd
, BYTE
*map
)
357 int button_count
= 0;
358 BYTE keybits
[(KEY_MAX
+7)/8];
360 if (ioctl(device_fd
, EVIOCGBIT(EV_KEY
, sizeof(keybits
)), keybits
) == -1)
362 WARN("ioctl(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno
, strerror(errno
));
366 for (i
= BTN_MISC
; i
< KEY_MAX
; i
++)
368 if (test_bit(keybits
, i
))
370 if (map
) map
[i
] = button_count
;
377 static INT
count_abs_axis(int device_fd
)
379 BYTE absbits
[(ABS_MAX
+7)/8];
383 if (ioctl(device_fd
, EVIOCGBIT(EV_ABS
, sizeof(absbits
)), absbits
) == -1)
385 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno
, strerror(errno
));
389 for (i
= 0; i
< HID_ABS_MAX
; i
++)
390 if (test_bit(absbits
, i
) &&
391 (ABS_TO_HID_MAP
[i
][1] >= HID_USAGE_GENERIC_X
&&
392 ABS_TO_HID_MAP
[i
][1] <= HID_USAGE_GENERIC_WHEEL
))
397 static BOOL
build_report_descriptor(struct wine_input_private
*ext
, struct udev_device
*dev
)
399 int abs_pages
[TOP_ABS_PAGE
][HID_ABS_MAX
+1];
400 int rel_pages
[TOP_REL_PAGE
][HID_REL_MAX
+1];
401 BYTE absbits
[(ABS_MAX
+7)/8];
402 BYTE relbits
[(REL_MAX
+7)/8];
404 INT i
, descript_size
;
406 INT button_count
, abs_count
, rel_count
, hat_count
;
407 const BYTE
*device_usage
= what_am_I(dev
);
409 if (ioctl(ext
->base
.device_fd
, EVIOCGBIT(EV_REL
, sizeof(relbits
)), relbits
) == -1)
411 WARN("ioctl(EVIOCGBIT, EV_REL) failed: %d %s\n", errno
, strerror(errno
));
414 if (ioctl(ext
->base
.device_fd
, EVIOCGBIT(EV_ABS
, sizeof(absbits
)), absbits
) == -1)
416 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno
, strerror(errno
));
420 descript_size
= sizeof(REPORT_HEADER
) + sizeof(REPORT_TAIL
);
424 memset(abs_pages
, 0, sizeof(abs_pages
));
425 for (i
= 0; i
< HID_ABS_MAX
; i
++)
426 if (test_bit(absbits
, i
))
428 abs_pages
[ABS_TO_HID_MAP
[i
][0]][0]++;
429 abs_pages
[ABS_TO_HID_MAP
[i
][0]][abs_pages
[ABS_TO_HID_MAP
[i
][0]][0]] = i
;
431 ioctl(ext
->base
.device_fd
, EVIOCGABS(i
), &(ext
->abs_map
[i
]));
433 /* Skip page 0, aka HID_USAGE_PAGE_UNDEFINED */
434 for (i
= 1; i
< TOP_ABS_PAGE
; i
++)
435 if (abs_pages
[i
][0] > 0)
438 descript_size
+= sizeof(REPORT_AXIS_USAGE
) * abs_pages
[i
][0];
439 for (j
= 1; j
<= abs_pages
[i
][0]; j
++)
441 ext
->abs_map
[abs_pages
[i
][j
]].report_index
= report_size
;
446 descript_size
+= sizeof(REPORT_AXIS_HEADER
) * abs_count
;
447 descript_size
+= sizeof(REPORT_ABS_AXIS_TAIL
) * abs_count
;
450 memset(rel_pages
, 0, sizeof(rel_pages
));
451 for (i
= 0; i
< HID_REL_MAX
; i
++)
452 if (test_bit(relbits
, i
))
454 rel_pages
[REL_TO_HID_MAP
[i
][0]][0]++;
455 rel_pages
[REL_TO_HID_MAP
[i
][0]][rel_pages
[REL_TO_HID_MAP
[i
][0]][0]] = i
;
457 /* Skip page 0, aka HID_USAGE_PAGE_UNDEFINED */
458 for (i
= 1; i
< TOP_REL_PAGE
; i
++)
459 if (rel_pages
[i
][0] > 0)
462 descript_size
+= sizeof(REPORT_AXIS_USAGE
) * rel_pages
[i
][0];
463 for (j
= 1; j
<= rel_pages
[i
][0]; j
++)
465 ext
->rel_map
[rel_pages
[i
][j
]] = report_size
;
470 descript_size
+= sizeof(REPORT_AXIS_HEADER
) * rel_count
;
471 descript_size
+= sizeof(REPORT_REL_AXIS_TAIL
) * rel_count
;
473 /* For now lump all buttons just into incremental usages, Ignore Keys */
474 ext
->button_start
= report_size
;
475 button_count
= count_buttons(ext
->base
.device_fd
, ext
->button_map
);
478 descript_size
+= sizeof(REPORT_BUTTONS
);
479 if (button_count
% 8)
480 descript_size
+= sizeof(REPORT_PADDING
);
481 report_size
+= (button_count
+ 7) / 8;
485 for (i
= ABS_HAT0X
; i
<=ABS_HAT3X
; i
+=2)
486 if (test_bit(absbits
, i
))
488 ext
->hat_map
[i
- ABS_HAT0X
] = report_size
;
489 ext
->hat_values
[i
- ABS_HAT0X
] = 0;
490 ext
->hat_values
[i
- ABS_HAT0X
+ 1] = 0;
495 descript_size
+= sizeof(REPORT_HATSWITCH
);
497 TRACE("Report Descriptor will be %i bytes\n", descript_size
);
498 TRACE("Report will be %i bytes\n", report_size
);
500 ext
->report_descriptor
= HeapAlloc(GetProcessHeap(), 0, descript_size
);
501 if (!ext
->report_descriptor
)
503 ERR("Failed to alloc report descriptor\n");
506 report_ptr
= ext
->report_descriptor
;
508 memcpy(report_ptr
, REPORT_HEADER
, sizeof(REPORT_HEADER
));
509 report_ptr
[IDX_HEADER_PAGE
] = device_usage
[0];
510 report_ptr
[IDX_HEADER_USAGE
] = device_usage
[1];
511 report_ptr
+= sizeof(REPORT_HEADER
);
514 for (i
= 1; i
< TOP_ABS_PAGE
; i
++)
518 BYTE usages
[HID_ABS_MAX
];
520 for (j
= 0; j
< abs_pages
[i
][0]; j
++)
521 usages
[j
] = ABS_TO_HID_MAP
[abs_pages
[i
][j
+1]][1];
522 report_ptr
= add_axis_block(report_ptr
, abs_pages
[i
][0], i
, usages
, TRUE
, &ext
->abs_map
[abs_pages
[i
][1]]);
528 for (i
= 1; i
< TOP_REL_PAGE
; i
++)
532 BYTE usages
[HID_REL_MAX
];
534 for (j
= 0; j
< rel_pages
[i
][0]; j
++)
535 usages
[j
] = REL_TO_HID_MAP
[rel_pages
[i
][j
+1]][1];
536 report_ptr
= add_axis_block(report_ptr
, rel_pages
[i
][0], i
, usages
, FALSE
, NULL
);
542 report_ptr
= add_button_block(report_ptr
, 1, button_count
);
543 if (button_count
% 8)
545 BYTE padding
= 8 - (button_count
% 8);
546 report_ptr
= add_padding_block(report_ptr
, padding
);
550 report_ptr
= add_hatswitch(report_ptr
, hat_count
);
552 memcpy(report_ptr
, REPORT_TAIL
, sizeof(REPORT_TAIL
));
554 ext
->report_descriptor_size
= descript_size
;
555 ext
->buffer_length
= report_size
;
556 ext
->current_report_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, report_size
);
557 if (ext
->current_report_buffer
== NULL
)
559 ERR("Failed to alloc report buffer\n");
560 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
563 ext
->last_report_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, report_size
);
564 if (ext
->last_report_buffer
== NULL
)
566 ERR("Failed to alloc report buffer\n");
567 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
568 HeapFree(GetProcessHeap(), 0, ext
->current_report_buffer
);
571 ext
->report_state
= FIRST
;
573 /* Initialize axis in the report */
574 for (i
= 0; i
< HID_ABS_MAX
; i
++)
575 if (test_bit(absbits
, i
))
576 set_abs_axis_value(ext
, i
, ext
->abs_map
[i
].info
.value
);
581 static BOOL
set_report_from_event(struct wine_input_private
*ext
, struct input_event
*ie
)
590 if (ext
->report_state
== NORMAL
)
592 memcpy(ext
->last_report_buffer
, ext
->current_report_buffer
, ext
->buffer_length
);
597 if (ext
->report_state
== DROPPED
)
598 memcpy(ext
->current_report_buffer
, ext
->last_report_buffer
, ext
->buffer_length
);
599 ext
->report_state
= NORMAL
;
603 TRACE_(hid_report
)("received SY_DROPPED\n");
604 ext
->report_state
= DROPPED
;
613 set_button_value(ext
->button_start
+ ext
->button_map
[ie
->code
], ie
->value
, ext
->current_report_buffer
);
616 set_abs_axis_value(ext
, ie
->code
, ie
->value
);
619 set_rel_axis_value(ext
, ie
->code
, ie
->value
);
622 ERR("TODO: Process Report (%i, %i)\n",ie
->type
, ie
->code
);
628 static inline WCHAR
*strdupAtoW(const char *src
)
632 if (!src
) return NULL
;
633 len
= MultiByteToWideChar(CP_UNIXCP
, 0, src
, -1, NULL
, 0);
634 if ((dst
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
635 MultiByteToWideChar(CP_UNIXCP
, 0, src
, -1, dst
, len
);
639 static WCHAR
*get_sysattr_string(struct udev_device
*dev
, const char *sysattr
)
641 const char *attr
= udev_device_get_sysattr_value(dev
, sysattr
);
644 WARN("Could not get %s from device\n", sysattr
);
647 return strdupAtoW(attr
);
650 static int compare_platform_device(DEVICE_OBJECT
*device
, void *platform_dev
)
652 struct udev_device
*dev1
= impl_from_DEVICE_OBJECT(device
)->udev_device
;
653 struct udev_device
*dev2
= platform_dev
;
654 return strcmp(udev_device_get_syspath(dev1
), udev_device_get_syspath(dev2
));
657 static NTSTATUS
hidraw_get_reportdescriptor(DEVICE_OBJECT
*device
, BYTE
*buffer
, DWORD length
, DWORD
*out_length
)
659 #ifdef HAVE_LINUX_HIDRAW_H
660 struct hidraw_report_descriptor descriptor
;
661 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
663 if (ioctl(private->device_fd
, HIDIOCGRDESCSIZE
, &descriptor
.size
) == -1)
665 WARN("ioctl(HIDIOCGRDESCSIZE) failed: %d %s\n", errno
, strerror(errno
));
666 return STATUS_UNSUCCESSFUL
;
669 *out_length
= descriptor
.size
;
671 if (length
< descriptor
.size
)
672 return STATUS_BUFFER_TOO_SMALL
;
673 if (!descriptor
.size
)
674 return STATUS_SUCCESS
;
676 if (ioctl(private->device_fd
, HIDIOCGRDESC
, &descriptor
) == -1)
678 WARN("ioctl(HIDIOCGRDESC) failed: %d %s\n", errno
, strerror(errno
));
679 return STATUS_UNSUCCESSFUL
;
682 memcpy(buffer
, descriptor
.value
, descriptor
.size
);
683 return STATUS_SUCCESS
;
685 return STATUS_NOT_IMPLEMENTED
;
689 static NTSTATUS
hidraw_get_string(DEVICE_OBJECT
*device
, DWORD index
, WCHAR
*buffer
, DWORD length
)
691 struct udev_device
*usbdev
;
692 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
695 usbdev
= udev_device_get_parent_with_subsystem_devtype(private->udev_device
, "usb", "usb_device");
700 case HID_STRING_ID_IPRODUCT
:
701 str
= get_sysattr_string(usbdev
, "product");
703 case HID_STRING_ID_IMANUFACTURER
:
704 str
= get_sysattr_string(usbdev
, "manufacturer");
706 case HID_STRING_ID_ISERIALNUMBER
:
707 str
= get_sysattr_string(usbdev
, "serial");
710 ERR("Unhandled string index %08x\n", index
);
711 return STATUS_NOT_IMPLEMENTED
;
716 #ifdef HAVE_LINUX_HIDRAW_H
719 case HID_STRING_ID_IPRODUCT
:
722 if (ioctl(private->device_fd
, HIDIOCGRAWNAME(MAX_PATH
), buf
) == -1)
723 WARN("ioctl(HIDIOCGRAWNAME) failed: %d %s\n", errno
, strerror(errno
));
725 str
= strdupAtoW(buf
);
728 case HID_STRING_ID_IMANUFACTURER
:
730 case HID_STRING_ID_ISERIALNUMBER
:
733 ERR("Unhandled string index %08x\n", index
);
734 return STATUS_NOT_IMPLEMENTED
;
737 return STATUS_NOT_IMPLEMENTED
;
743 if (!length
) return STATUS_BUFFER_TOO_SMALL
;
745 return STATUS_SUCCESS
;
748 if (length
<= strlenW(str
))
750 HeapFree(GetProcessHeap(), 0, str
);
751 return STATUS_BUFFER_TOO_SMALL
;
754 strcpyW(buffer
, str
);
755 HeapFree(GetProcessHeap(), 0, str
);
756 return STATUS_SUCCESS
;
759 static DWORD CALLBACK
device_report_thread(void *args
)
761 DEVICE_OBJECT
*device
= (DEVICE_OBJECT
*)args
;
762 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
763 struct pollfd plfds
[2];
765 plfds
[0].fd
= private->device_fd
;
766 plfds
[0].events
= POLLIN
;
767 plfds
[0].revents
= 0;
768 plfds
[1].fd
= private->control_pipe
[0];
769 plfds
[1].events
= POLLIN
;
770 plfds
[1].revents
= 0;
775 BYTE report_buffer
[1024];
777 if (poll(plfds
, 2, -1) <= 0) continue;
778 if (plfds
[1].revents
)
780 size
= read(plfds
[0].fd
, report_buffer
, sizeof(report_buffer
));
782 TRACE_(hid_report
)("Read failed. Likely an unplugged device %d %s\n", errno
, strerror(errno
));
784 TRACE_(hid_report
)("Failed to read report\n");
786 process_hid_report(device
, report_buffer
, size
);
791 static NTSTATUS
begin_report_processing(DEVICE_OBJECT
*device
)
793 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
795 if (private->report_thread
)
796 return STATUS_SUCCESS
;
798 if (pipe(private->control_pipe
) != 0)
800 ERR("Control pipe creation failed\n");
801 return STATUS_UNSUCCESSFUL
;
804 private->report_thread
= CreateThread(NULL
, 0, device_report_thread
, device
, 0, NULL
);
805 if (!private->report_thread
)
807 ERR("Unable to create device report thread\n");
808 close(private->control_pipe
[0]);
809 close(private->control_pipe
[1]);
810 return STATUS_UNSUCCESSFUL
;
813 return STATUS_SUCCESS
;
816 static NTSTATUS
hidraw_set_output_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
818 struct platform_private
* ext
= impl_from_DEVICE_OBJECT(device
);
822 rc
= write(ext
->device_fd
, report
, length
);
825 BYTE report_buffer
[1024];
827 if (length
+ 1 > sizeof(report_buffer
))
829 ERR("Output report buffer too small\n");
830 return STATUS_UNSUCCESSFUL
;
833 report_buffer
[0] = 0;
834 memcpy(&report_buffer
[1], report
, length
);
835 rc
= write(ext
->device_fd
, report_buffer
, length
+ 1);
840 return STATUS_SUCCESS
;
844 TRACE("write failed: %d %d %s\n", rc
, errno
, strerror(errno
));
846 return STATUS_UNSUCCESSFUL
;
850 static NTSTATUS
hidraw_get_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*read
)
852 #if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCGFEATURE)
854 struct platform_private
* ext
= impl_from_DEVICE_OBJECT(device
);
856 length
= min(length
, 0x1fff);
857 rc
= ioctl(ext
->device_fd
, HIDIOCGFEATURE(length
), report
);
861 return STATUS_SUCCESS
;
865 TRACE_(hid_report
)("ioctl(HIDIOCGFEATURE(%d)) failed: %d %s\n", length
, errno
, strerror(errno
));
867 return STATUS_UNSUCCESSFUL
;
871 return STATUS_NOT_IMPLEMENTED
;
875 static NTSTATUS
hidraw_set_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
877 #if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCSFEATURE)
879 struct platform_private
* ext
= impl_from_DEVICE_OBJECT(device
);
880 BYTE
*feature_buffer
;
885 if (length
+ 1 > sizeof(buffer
))
887 ERR("Output feature buffer too small\n");
888 return STATUS_UNSUCCESSFUL
;
891 memcpy(&buffer
[1], report
, length
);
892 feature_buffer
= buffer
;
896 feature_buffer
= report
;
897 length
= min(length
, 0x1fff);
898 rc
= ioctl(ext
->device_fd
, HIDIOCSFEATURE(length
), feature_buffer
);
902 return STATUS_SUCCESS
;
906 TRACE_(hid_report
)("ioctl(HIDIOCSFEATURE(%d)) failed: %d %s\n", length
, errno
, strerror(errno
));
908 return STATUS_UNSUCCESSFUL
;
912 return STATUS_NOT_IMPLEMENTED
;
916 static const platform_vtbl hidraw_vtbl
=
918 compare_platform_device
,
919 hidraw_get_reportdescriptor
,
921 begin_report_processing
,
922 hidraw_set_output_report
,
923 hidraw_get_feature_report
,
924 hidraw_set_feature_report
,
927 #ifdef HAS_PROPER_INPUT_HEADER
929 static inline struct wine_input_private
*input_impl_from_DEVICE_OBJECT(DEVICE_OBJECT
*device
)
931 return (struct wine_input_private
*)get_platform_private(device
);
934 static NTSTATUS
lnxev_get_reportdescriptor(DEVICE_OBJECT
*device
, BYTE
*buffer
, DWORD length
, DWORD
*out_length
)
936 struct wine_input_private
*ext
= input_impl_from_DEVICE_OBJECT(device
);
938 *out_length
= ext
->report_descriptor_size
;
940 if (length
< ext
->report_descriptor_size
)
941 return STATUS_BUFFER_TOO_SMALL
;
943 memcpy(buffer
, ext
->report_descriptor
, ext
->report_descriptor_size
);
945 return STATUS_SUCCESS
;
948 static NTSTATUS
lnxev_get_string(DEVICE_OBJECT
*device
, DWORD index
, WCHAR
*buffer
, DWORD length
)
950 struct wine_input_private
*ext
= input_impl_from_DEVICE_OBJECT(device
);
956 case HID_STRING_ID_IPRODUCT
:
957 ioctl(ext
->base
.device_fd
, EVIOCGNAME(sizeof(str
)), str
);
959 case HID_STRING_ID_IMANUFACTURER
:
962 case HID_STRING_ID_ISERIALNUMBER
:
963 ioctl(ext
->base
.device_fd
, EVIOCGUNIQ(sizeof(str
)), str
);
966 ERR("Unhandled string index %i\n", index
);
969 MultiByteToWideChar(CP_ACP
, 0, str
, -1, buffer
, length
);
970 return STATUS_SUCCESS
;
973 static DWORD CALLBACK
lnxev_device_report_thread(void *args
)
975 DEVICE_OBJECT
*device
= (DEVICE_OBJECT
*)args
;
976 struct wine_input_private
*private = input_impl_from_DEVICE_OBJECT(device
);
977 struct pollfd plfds
[2];
979 plfds
[0].fd
= private->base
.device_fd
;
980 plfds
[0].events
= POLLIN
;
981 plfds
[0].revents
= 0;
982 plfds
[1].fd
= private->base
.control_pipe
[0];
983 plfds
[1].events
= POLLIN
;
984 plfds
[1].revents
= 0;
989 struct input_event ie
;
991 if (poll(plfds
, 2, -1) <= 0) continue;
992 if (plfds
[1].revents
|| !private->current_report_buffer
|| private->buffer_length
== 0)
994 size
= read(plfds
[0].fd
, &ie
, sizeof(ie
));
996 TRACE_(hid_report
)("Read failed. Likely an unplugged device\n");
998 TRACE_(hid_report
)("Failed to read report\n");
999 else if (set_report_from_event(private, &ie
))
1000 process_hid_report(device
, private->current_report_buffer
, private->buffer_length
);
1005 static NTSTATUS
lnxev_begin_report_processing(DEVICE_OBJECT
*device
)
1007 struct wine_input_private
*private = input_impl_from_DEVICE_OBJECT(device
);
1009 if (private->base
.report_thread
)
1010 return STATUS_SUCCESS
;
1012 if (pipe(private->base
.control_pipe
) != 0)
1014 ERR("Control pipe creation failed\n");
1015 return STATUS_UNSUCCESSFUL
;
1018 private->base
.report_thread
= CreateThread(NULL
, 0, lnxev_device_report_thread
, device
, 0, NULL
);
1019 if (!private->base
.report_thread
)
1021 ERR("Unable to create device report thread\n");
1022 close(private->base
.control_pipe
[0]);
1023 close(private->base
.control_pipe
[1]);
1024 return STATUS_UNSUCCESSFUL
;
1026 return STATUS_SUCCESS
;
1029 static NTSTATUS
lnxev_set_output_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
1032 return STATUS_NOT_IMPLEMENTED
;
1035 static NTSTATUS
lnxev_get_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*read
)
1038 return STATUS_NOT_IMPLEMENTED
;
1041 static NTSTATUS
lnxev_set_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
1044 return STATUS_NOT_IMPLEMENTED
;
1047 static const platform_vtbl lnxev_vtbl
= {
1048 compare_platform_device
,
1049 lnxev_get_reportdescriptor
,
1051 lnxev_begin_report_processing
,
1052 lnxev_set_output_report
,
1053 lnxev_get_feature_report
,
1054 lnxev_set_feature_report
,
1058 static int check_same_device(DEVICE_OBJECT
*device
, void* context
)
1060 return !compare_platform_device(device
, context
);
1063 static int parse_uevent_info(const char *uevent
, DWORD
*vendor_id
,
1064 DWORD
*product_id
, WORD
*input
, WCHAR
**serial_number
)
1068 char *saveptr
= NULL
;
1074 int found_serial
= 0;
1076 tmp
= heap_alloc(strlen(uevent
) + 1);
1077 strcpy(tmp
, uevent
);
1078 line
= strtok_r(tmp
, "\n", &saveptr
);
1079 while (line
!= NULL
)
1081 /* line: "KEY=value" */
1083 value
= strchr(line
, '=');
1091 if (strcmp(key
, "HID_ID") == 0)
1094 * type vendor product
1095 * HID_ID=0003:000005AC:00008242
1097 int ret
= sscanf(value
, "%x:%x:%x", &bus_type
, vendor_id
, product_id
);
1101 else if (strcmp(key
, "HID_UNIQ") == 0)
1103 /* The caller has to free the serial number */
1106 *serial_number
= strdupAtoW(value
);
1110 else if (strcmp(key
, "HID_PHYS") == 0)
1112 const char *input_no
= strstr(value
, "input");
1114 *input
= atoi(input_no
+5 );
1118 line
= strtok_r(NULL
, "\n", &saveptr
);
1122 return (found_id
&& found_serial
);
1125 static DWORD
a_to_bcd(const char *s
)
1129 int shift
= strlen(s
) - 1;
1130 for (c
= s
; *c
; ++c
)
1132 r
|= (*c
- '0') << (shift
* 4);
1138 static void try_add_device(struct udev_device
*dev
)
1140 DWORD vid
= 0, pid
= 0, version
= 0;
1141 struct udev_device
*hiddev
= NULL
, *walk_device
;
1142 DEVICE_OBJECT
*device
= NULL
;
1143 const char *subsystem
;
1144 const char *devnode
;
1145 WCHAR
*serial
= NULL
;
1146 BOOL is_gamepad
= FALSE
;
1149 static const CHAR
*base_serial
= "0000";
1151 if (!(devnode
= udev_device_get_devnode(dev
)))
1154 if ((fd
= open(devnode
, O_RDWR
)) == -1)
1156 WARN("Unable to open udev device %s: %s\n", debugstr_a(devnode
), strerror(errno
));
1160 subsystem
= udev_device_get_subsystem(dev
);
1161 hiddev
= udev_device_get_parent_with_subsystem_devtype(dev
, "hid", NULL
);
1164 const char *bcdDevice
= NULL
;
1165 #ifdef HAS_PROPER_INPUT_HEADER
1166 const platform_vtbl
*other_vtbl
= NULL
;
1167 DEVICE_OBJECT
*dup
= NULL
;
1168 if (strcmp(subsystem
, "hidraw") == 0)
1169 other_vtbl
= &lnxev_vtbl
;
1170 else if (strcmp(subsystem
, "input") == 0)
1171 other_vtbl
= &hidraw_vtbl
;
1174 dup
= bus_enumerate_hid_devices(other_vtbl
, check_same_device
, dev
);
1177 TRACE("Duplicate cross bus device (%p) found, not adding the new one\n", dup
);
1182 parse_uevent_info(udev_device_get_sysattr_value(hiddev
, "uevent"),
1183 &vid
, &pid
, &input
, &serial
);
1185 serial
= strdupAtoW(base_serial
);
1188 while (walk_device
&& !bcdDevice
)
1190 bcdDevice
= udev_device_get_sysattr_value(walk_device
, "bcdDevice");
1191 walk_device
= udev_device_get_parent(walk_device
);
1195 version
= a_to_bcd(bcdDevice
);
1198 #ifdef HAS_PROPER_INPUT_HEADER
1201 struct input_id device_id
= {0};
1202 char device_uid
[255];
1204 if (ioctl(fd
, EVIOCGID
, &device_id
) < 0)
1205 WARN("ioctl(EVIOCGID) failed: %d %s\n", errno
, strerror(errno
));
1207 if (ioctl(fd
, EVIOCGUNIQ(254), device_uid
) >= 0 && device_uid
[0])
1208 serial
= strdupAtoW(device_uid
);
1210 vid
= device_id
.vendor
;
1211 pid
= device_id
.product
;
1212 version
= device_id
.version
;
1216 WARN("Could not get device to query VID, PID, Version and Serial\n");
1219 if (is_xbox_gamepad(vid
, pid
))
1221 #ifdef HAS_PROPER_INPUT_HEADER
1224 int axes
=0, buttons
=0;
1225 axes
= count_abs_axis(fd
);
1226 buttons
= count_buttons(fd
, NULL
);
1227 is_gamepad
= (axes
== 6 && buttons
>= 14);
1230 if (input
== (WORD
)-1 && is_gamepad
)
1234 TRACE("Found udev device %s (vid %04x, pid %04x, version %u, serial %s)\n",
1235 debugstr_a(devnode
), vid
, pid
, version
, debugstr_w(serial
));
1237 if (strcmp(subsystem
, "hidraw") == 0)
1239 device
= bus_create_hid_device(hidraw_busidW
, vid
, pid
, input
, version
, 0, serial
, is_gamepad
,
1240 &hidraw_vtbl
, sizeof(struct platform_private
));
1242 #ifdef HAS_PROPER_INPUT_HEADER
1243 else if (strcmp(subsystem
, "input") == 0)
1245 device
= bus_create_hid_device(lnxev_busidW
, vid
, pid
, input
, version
, 0, serial
, is_gamepad
,
1246 &lnxev_vtbl
, sizeof(struct wine_input_private
));
1252 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
1253 private->udev_device
= udev_device_ref(dev
);
1254 private->device_fd
= fd
;
1255 #ifdef HAS_PROPER_INPUT_HEADER
1256 if (strcmp(subsystem
, "input") == 0)
1257 if (!build_report_descriptor((struct wine_input_private
*)private, dev
))
1259 ERR("Building report descriptor failed, removing device\n");
1261 udev_device_unref(dev
);
1262 bus_unlink_hid_device(device
);
1263 bus_remove_hid_device(device
);
1264 HeapFree(GetProcessHeap(), 0, serial
);
1268 IoInvalidateDeviceRelations(bus_pdo
, BusRelations
);
1272 WARN("Ignoring device %s with subsystem %s\n", debugstr_a(devnode
), subsystem
);
1276 HeapFree(GetProcessHeap(), 0, serial
);
1279 static void try_remove_device(struct udev_device
*dev
)
1281 DEVICE_OBJECT
*device
= NULL
;
1282 struct platform_private
* private;
1283 #ifdef HAS_PROPER_INPUT_HEADER
1284 BOOL is_input
= FALSE
;
1287 device
= bus_find_hid_device(&hidraw_vtbl
, dev
);
1288 #ifdef HAS_PROPER_INPUT_HEADER
1291 device
= bus_find_hid_device(&lnxev_vtbl
, dev
);
1295 if (!device
) return;
1297 bus_unlink_hid_device(device
);
1298 IoInvalidateDeviceRelations(bus_pdo
, BusRelations
);
1300 private = impl_from_DEVICE_OBJECT(device
);
1302 if (private->report_thread
)
1304 write(private->control_pipe
[1], "q", 1);
1305 WaitForSingleObject(private->report_thread
, INFINITE
);
1306 close(private->control_pipe
[0]);
1307 close(private->control_pipe
[1]);
1308 CloseHandle(private->report_thread
);
1309 #ifdef HAS_PROPER_INPUT_HEADER
1310 if (strcmp(udev_device_get_subsystem(dev
), "input") == 0)
1312 HeapFree(GetProcessHeap(), 0, ((struct wine_input_private
*)private)->current_report_buffer
);
1313 HeapFree(GetProcessHeap(), 0, ((struct wine_input_private
*)private)->last_report_buffer
);
1318 #ifdef HAS_PROPER_INPUT_HEADER
1321 struct wine_input_private
*ext
= (struct wine_input_private
*)private;
1322 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
1326 dev
= private->udev_device
;
1327 close(private->device_fd
);
1328 bus_remove_hid_device(device
);
1329 udev_device_unref(dev
);
1332 static void build_initial_deviceset(void)
1334 struct udev_enumerate
*enumerate
;
1335 struct udev_list_entry
*devices
, *dev_list_entry
;
1337 enumerate
= udev_enumerate_new(udev_context
);
1340 WARN("Unable to create udev enumeration object\n");
1344 if (!disable_hidraw
)
1345 if (udev_enumerate_add_match_subsystem(enumerate
, "hidraw") < 0)
1346 WARN("Failed to add subsystem 'hidraw' to enumeration\n");
1347 #ifdef HAS_PROPER_INPUT_HEADER
1350 if (udev_enumerate_add_match_subsystem(enumerate
, "input") < 0)
1351 WARN("Failed to add subsystem 'input' to enumeration\n");
1355 if (udev_enumerate_scan_devices(enumerate
) < 0)
1356 WARN("Enumeration scan failed\n");
1358 devices
= udev_enumerate_get_list_entry(enumerate
);
1359 udev_list_entry_foreach(dev_list_entry
, devices
)
1361 struct udev_device
*dev
;
1364 path
= udev_list_entry_get_name(dev_list_entry
);
1365 if ((dev
= udev_device_new_from_syspath(udev_context
, path
)))
1367 try_add_device(dev
);
1368 udev_device_unref(dev
);
1372 udev_enumerate_unref(enumerate
);
1375 static struct udev_monitor
*create_monitor(struct pollfd
*pfd
)
1377 struct udev_monitor
*monitor
;
1380 monitor
= udev_monitor_new_from_netlink(udev_context
, "udev");
1383 WARN("Unable to get udev monitor object\n");
1387 if (!disable_hidraw
)
1389 if (udev_monitor_filter_add_match_subsystem_devtype(monitor
, "hidraw", NULL
) < 0)
1390 WARN("Failed to add 'hidraw' subsystem to monitor\n");
1394 #ifdef HAS_PROPER_INPUT_HEADER
1397 if (udev_monitor_filter_add_match_subsystem_devtype(monitor
, "input", NULL
) < 0)
1398 WARN("Failed to add 'input' subsystem to monitor\n");
1405 WARN("No subsystems added to monitor\n");
1409 if (udev_monitor_enable_receiving(monitor
) < 0)
1412 if ((pfd
->fd
= udev_monitor_get_fd(monitor
)) >= 0)
1414 pfd
->events
= POLLIN
;
1419 WARN("Failed to start monitoring\n");
1420 udev_monitor_unref(monitor
);
1424 static void process_monitor_event(struct udev_monitor
*monitor
)
1426 struct udev_device
*dev
;
1429 dev
= udev_monitor_receive_device(monitor
);
1432 FIXME("Failed to get device that has changed\n");
1436 action
= udev_device_get_action(dev
);
1437 TRACE("Received action %s for udev device %s\n", debugstr_a(action
),
1438 debugstr_a(udev_device_get_devnode(dev
)));
1441 WARN("No action received\n");
1442 else if (strcmp(action
, "add") == 0)
1443 try_add_device(dev
);
1444 else if (strcmp(action
, "remove") == 0)
1445 try_remove_device(dev
);
1447 WARN("Unhandled action %s\n", debugstr_a(action
));
1449 udev_device_unref(dev
);
1452 static DWORD CALLBACK
deviceloop_thread(void *args
)
1454 struct udev_monitor
*monitor
;
1455 HANDLE init_done
= args
;
1456 struct pollfd pfd
[2];
1458 pfd
[1].fd
= deviceloop_control
[0];
1459 pfd
[1].events
= POLLIN
;
1462 monitor
= create_monitor(&pfd
[0]);
1463 build_initial_deviceset();
1464 SetEvent(init_done
);
1468 if (poll(pfd
, 2, -1) <= 0) continue;
1469 if (pfd
[1].revents
) break;
1470 process_monitor_event(monitor
);
1473 TRACE("Monitor thread exiting\n");
1475 udev_monitor_unref(monitor
);
1479 static int device_unload(DEVICE_OBJECT
*device
, void *context
)
1481 try_remove_device(impl_from_DEVICE_OBJECT(device
)->udev_device
);
1485 void udev_driver_unload( void )
1487 TRACE("Unload Driver\n");
1489 if (!deviceloop_handle
)
1492 write(deviceloop_control
[1], "q", 1);
1493 WaitForSingleObject(deviceloop_handle
, INFINITE
);
1494 close(deviceloop_control
[0]);
1495 close(deviceloop_control
[1]);
1496 CloseHandle(deviceloop_handle
);
1498 bus_enumerate_hid_devices(&hidraw_vtbl
, device_unload
, NULL
);
1499 #ifdef HAS_PROPER_INPUT_HEADER
1500 bus_enumerate_hid_devices(&lnxev_vtbl
, device_unload
, NULL
);
1504 NTSTATUS
udev_driver_init(void)
1508 static const WCHAR hidraw_disabledW
[] = {'D','i','s','a','b','l','e','H','i','d','r','a','w',0};
1509 static const UNICODE_STRING hidraw_disabled
= {sizeof(hidraw_disabledW
) - sizeof(WCHAR
), sizeof(hidraw_disabledW
), (WCHAR
*)hidraw_disabledW
};
1510 static const WCHAR input_disabledW
[] = {'D','i','s','a','b','l','e','I','n','p','u','t',0};
1511 static const UNICODE_STRING input_disabled
= {sizeof(input_disabledW
) - sizeof(WCHAR
), sizeof(input_disabledW
), (WCHAR
*)input_disabledW
};
1513 if (pipe(deviceloop_control
) != 0)
1515 ERR("Control pipe creation failed\n");
1516 return STATUS_UNSUCCESSFUL
;
1519 if (!(udev_context
= udev_new()))
1521 ERR("Can't create udev object\n");
1525 disable_hidraw
= check_bus_option(&hidraw_disabled
, 0);
1527 TRACE("UDEV hidraw devices disabled in registry\n");
1529 #ifdef HAS_PROPER_INPUT_HEADER
1530 disable_input
= check_bus_option(&input_disabled
, 0);
1532 TRACE("UDEV input devices disabled in registry\n");
1535 if (!(events
[0] = CreateEventW(NULL
, TRUE
, FALSE
, NULL
)))
1537 if (!(events
[1] = CreateThread(NULL
, 0, deviceloop_thread
, events
[0], 0, NULL
)))
1539 CloseHandle(events
[0]);
1543 result
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
1544 CloseHandle(events
[0]);
1545 if (result
== WAIT_OBJECT_0
)
1547 deviceloop_handle
= events
[1];
1548 TRACE("Initialization successful\n");
1549 return STATUS_SUCCESS
;
1551 CloseHandle(events
[1]);
1554 ERR("Failed to initialize udev device thread\n");
1555 close(deviceloop_control
[0]);
1556 close(deviceloop_control
[1]);
1559 udev_unref(udev_context
);
1560 udev_context
= NULL
;
1562 return STATUS_UNSUCCESSFUL
;
1567 NTSTATUS
udev_driver_init(void)
1569 return STATUS_NOT_IMPLEMENTED
;
1572 void udev_driver_unload( void )
1574 TRACE("Stub: Unload Driver\n");
1577 #endif /* HAVE_UDEV */