winebus.sys: Improve unloading the winebus driver.
[wine.git] / dlls / winebus.sys / bus_udev.c
blobdabda6a270509174f9eef203ef1fc639dfe1a7a5
1 /*
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
21 #include "config.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #ifdef HAVE_POLL_H
31 # include <poll.h>
32 #endif
33 #ifdef HAVE_SYS_POLL_H
34 # include <sys/poll.h>
35 #endif
36 #ifdef HAVE_LIBUDEV_H
37 # include <libudev.h>
38 #endif
39 #ifdef HAVE_LINUX_HIDRAW_H
40 # include <linux/hidraw.h>
41 #endif
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
46 #ifdef HAVE_LINUX_INPUT_H
47 # include <linux/input.h>
48 # undef SW_MAX
49 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
50 # define HAS_PROPER_INPUT_HEADER
51 # endif
52 # ifndef SYN_DROPPED
53 # define SYN_DROPPED 3
54 # endif
55 #endif
57 #define NONAMELESSUNION
59 #include "ntstatus.h"
60 #define WIN32_NO_STATUS
61 #include "windef.h"
62 #include "winbase.h"
63 #include "winnls.h"
64 #include "winternl.h"
65 #include "ddk/wdm.h"
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"
73 #endif
75 #ifdef WORDS_BIGENDIAN
76 #define LE_WORD(x) RtlUshortByteSwap(x)
77 #define LE_DWORD(x) RtlUlongByteSwap(x)
78 #else
79 #define LE_WORD(x) (x)
80 #define LE_DWORD(x) (x)
81 #endif
83 #include "controller.h"
84 #include "bus.h"
86 WINE_DEFAULT_DEBUG_CHANNEL(plugplay);
88 #ifdef HAVE_UDEV
90 WINE_DECLARE_DEBUG_CHANNEL(hid_report);
92 static struct udev *udev_context = NULL;
93 static DRIVER_OBJECT *udev_driver_obj = NULL;
94 static DWORD disable_hidraw = 0;
95 static DWORD disable_input = 0;
97 static const WCHAR hidraw_busidW[] = {'H','I','D','R','A','W',0};
98 static const WCHAR lnxev_busidW[] = {'L','N','X','E','V',0};
100 #include "initguid.h"
101 DEFINE_GUID(GUID_DEVCLASS_HIDRAW, 0x3def44ad,0x242e,0x46e5,0x82,0x6d,0x70,0x72,0x13,0xf3,0xaa,0x81);
102 DEFINE_GUID(GUID_DEVCLASS_LINUXEVENT, 0x1b932c0d,0xfea7,0x42cd,0x8e,0xaa,0x0e,0x48,0x79,0xb6,0x9e,0xaa);
104 struct platform_private
106 struct udev_device *udev_device;
107 int device_fd;
109 HANDLE report_thread;
110 int control_pipe[2];
113 static inline struct platform_private *impl_from_DEVICE_OBJECT(DEVICE_OBJECT *device)
115 return (struct platform_private *)get_platform_private(device);
118 #ifdef HAS_PROPER_INPUT_HEADER
120 static const BYTE REPORT_ABS_AXIS_TAIL[] = {
121 0x17, 0x00, 0x00, 0x00, 0x00, /* LOGICAL_MINIMUM (0) */
122 0x27, 0xff, 0x00, 0x00, 0x00, /* LOGICAL_MAXIMUM (0xff) */
123 0x37, 0x00, 0x00, 0x00, 0x00, /* PHYSICAL_MINIMUM (0) */
124 0x47, 0xff, 0x00, 0x00, 0x00, /* PHYSICAL_MAXIMUM (256) */
125 0x75, 0x20, /* REPORT_SIZE (32) */
126 0x95, 0x00, /* REPORT_COUNT (2) */
127 0x81, 0x02, /* INPUT (Data,Var,Abs) */
129 #define IDX_ABS_LOG_MINIMUM 1
130 #define IDX_ABS_LOG_MAXIMUM 6
131 #define IDX_ABS_PHY_MINIMUM 11
132 #define IDX_ABS_PHY_MAXIMUM 16
133 #define IDX_ABS_AXIS_COUNT 23
135 static const BYTE ABS_TO_HID_MAP[][2] = {
136 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_X}, /*ABS_X*/
137 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_Y}, /*ABS_Y*/
138 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_Z}, /*ABS_Z*/
139 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_RX}, /*ABS_RX*/
140 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_RY}, /*ABS_RY*/
141 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_RZ}, /*ABS_RZ*/
142 {HID_USAGE_PAGE_SIMULATION, HID_USAGE_SIMULATION_THROTTLE}, /*ABS_THROTTLE*/
143 {HID_USAGE_PAGE_SIMULATION, HID_USAGE_SIMULATION_RUDDER}, /*ABS_RUDDER*/
144 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_WHEEL}, /*ABS_WHEEL*/
145 {HID_USAGE_PAGE_SIMULATION, 0xC4}, /*ABS_GAS*/
146 {HID_USAGE_PAGE_SIMULATION, 0xC5}, /*ABS_BRAKE*/
147 {0,0}, /*ABS_HAT0X*/
148 {0,0}, /*ABS_HAT0Y*/
149 {0,0}, /*ABS_HAT1X*/
150 {0,0}, /*ABS_HAT1Y*/
151 {0,0}, /*ABS_HAT2X*/
152 {0,0}, /*ABS_HAT2Y*/
153 {0,0}, /*ABS_HAT3X*/
154 {0,0}, /*ABS_HAT3Y*/
155 {HID_USAGE_PAGE_DIGITIZER, 0x30}, /*ABS_PRESSURE*/
156 {0, 0}, /*ABS_DISTANCE*/
157 {HID_USAGE_PAGE_DIGITIZER, 0x3D}, /*ABS_TILT_X*/
158 {HID_USAGE_PAGE_DIGITIZER, 0x3F}, /*ABS_TILT_Y*/
159 {0, 0}, /*ABS_TOOL_WIDTH*/
160 {0, 0},
161 {0, 0},
162 {0, 0},
163 {HID_USAGE_PAGE_CONSUMER, 0xE0} /*ABS_VOLUME*/
165 #define HID_ABS_MAX (ABS_VOLUME+1)
166 #define TOP_ABS_PAGE (HID_USAGE_PAGE_DIGITIZER+1)
168 static const BYTE REL_TO_HID_MAP[][2] = {
169 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_X}, /* REL_X */
170 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_Y}, /* REL_Y */
171 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_Z}, /* REL_Z */
172 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_RX}, /* REL_RX */
173 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_RY}, /* REL_RY */
174 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_RZ}, /* REL_RZ */
175 {0, 0}, /* REL_HWHEEL */
176 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_DIAL}, /* REL_DIAL */
177 {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_WHEEL}, /* REL_WHEEL */
178 {0, 0} /* REL_MISC */
181 #define HID_REL_MAX (REL_MISC+1)
182 #define TOP_REL_PAGE (HID_USAGE_PAGE_CONSUMER+1)
184 struct wine_input_absinfo {
185 struct input_absinfo info;
186 BYTE report_index;
189 struct wine_input_private {
190 struct platform_private base;
192 int buffer_length;
193 BYTE *last_report_buffer;
194 BYTE *current_report_buffer;
195 enum { FIRST, NORMAL, DROPPED } report_state;
197 int report_descriptor_size;
198 BYTE *report_descriptor;
200 BYTE button_map[KEY_MAX];
201 BYTE rel_map[HID_REL_MAX];
202 BYTE hat_map[8];
203 int hat_values[8];
204 struct wine_input_absinfo abs_map[HID_ABS_MAX];
207 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
209 static BYTE *add_axis_block(BYTE *report_ptr, BYTE count, BYTE page, BYTE *usages, BOOL absolute, const struct wine_input_absinfo *absinfo)
211 int i;
212 memcpy(report_ptr, REPORT_AXIS_HEADER, sizeof(REPORT_AXIS_HEADER));
213 report_ptr[IDX_AXIS_PAGE] = page;
214 report_ptr += sizeof(REPORT_AXIS_HEADER);
215 for (i = 0; i < count; i++)
217 memcpy(report_ptr, REPORT_AXIS_USAGE, sizeof(REPORT_AXIS_USAGE));
218 report_ptr[IDX_AXIS_USAGE] = usages[i];
219 report_ptr += sizeof(REPORT_AXIS_USAGE);
221 if (absolute)
223 memcpy(report_ptr, REPORT_ABS_AXIS_TAIL, sizeof(REPORT_ABS_AXIS_TAIL));
224 if (absinfo)
226 *((int*)&report_ptr[IDX_ABS_LOG_MINIMUM]) = LE_DWORD(absinfo->info.minimum);
227 *((int*)&report_ptr[IDX_ABS_LOG_MAXIMUM]) = LE_DWORD(absinfo->info.maximum);
228 *((int*)&report_ptr[IDX_ABS_PHY_MINIMUM]) = LE_DWORD(absinfo->info.minimum);
229 *((int*)&report_ptr[IDX_ABS_PHY_MAXIMUM]) = LE_DWORD(absinfo->info.maximum);
231 report_ptr[IDX_ABS_AXIS_COUNT] = count;
232 report_ptr += sizeof(REPORT_ABS_AXIS_TAIL);
234 else
236 memcpy(report_ptr, REPORT_REL_AXIS_TAIL, sizeof(REPORT_REL_AXIS_TAIL));
237 report_ptr[IDX_REL_AXIS_COUNT] = count;
238 report_ptr += sizeof(REPORT_REL_AXIS_TAIL);
240 return report_ptr;
243 static const BYTE* what_am_I(struct udev_device *dev)
245 static const BYTE Unknown[2] = {HID_USAGE_PAGE_GENERIC, 0};
246 static const BYTE Mouse[2] = {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_MOUSE};
247 static const BYTE Keyboard[2] = {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_KEYBOARD};
248 static const BYTE Gamepad[2] = {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_GAMEPAD};
249 static const BYTE Keypad[2] = {HID_USAGE_PAGE_GENERIC, HID_USAGE_GENERIC_KEYPAD};
250 static const BYTE Tablet[2] = {HID_USAGE_PAGE_DIGITIZER, 0x2};
251 static const BYTE Touchscreen[2] = {HID_USAGE_PAGE_DIGITIZER, 0x4};
252 static const BYTE Touchpad[2] = {HID_USAGE_PAGE_DIGITIZER, 0x5};
254 struct udev_device *parent = dev;
256 /* Look to the parents until we get a clue */
257 while (parent)
259 if (udev_device_get_property_value(parent, "ID_INPUT_MOUSE"))
260 return Mouse;
261 else if (udev_device_get_property_value(parent, "ID_INPUT_KEYBOARD"))
262 return Keyboard;
263 else if (udev_device_get_property_value(parent, "ID_INPUT_JOYSTICK"))
264 return Gamepad;
265 else if (udev_device_get_property_value(parent, "ID_INPUT_KEY"))
266 return Keypad;
267 else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHPAD"))
268 return Touchpad;
269 else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHSCREEN"))
270 return Touchscreen;
271 else if (udev_device_get_property_value(parent, "ID_INPUT_TABLET"))
272 return Tablet;
274 parent = udev_device_get_parent_with_subsystem_devtype(parent, "input", NULL);
276 return Unknown;
279 static void set_abs_axis_value(struct wine_input_private *ext, int code, int value)
281 int index;
282 /* check for hatswitches */
283 if (code <= ABS_HAT3Y && code >= ABS_HAT0X)
285 index = code - ABS_HAT0X;
286 ext->hat_values[index] = value;
287 if ((code - ABS_HAT0X) % 2)
288 index--;
289 if (ext->hat_values[index] == 0)
291 if (ext->hat_values[index+1] == 0)
292 value = 8;
293 else if (ext->hat_values[index+1] < 0)
294 value = 0;
295 else
296 value = 4;
298 else if (ext->hat_values[index] > 0)
300 if (ext->hat_values[index+1] == 0)
301 value = 2;
302 else if (ext->hat_values[index+1] < 0)
303 value = 1;
304 else
305 value = 3;
307 else
309 if (ext->hat_values[index+1] == 0)
310 value = 6;
311 else if (ext->hat_values[index+1] < 0)
312 value = 7;
313 else
314 value = 5;
316 ext->current_report_buffer[ext->hat_map[index]] = value;
318 else if (code < HID_ABS_MAX && ABS_TO_HID_MAP[code][0] != 0)
320 index = ext->abs_map[code].report_index;
321 *((DWORD*)&ext->current_report_buffer[index]) = LE_DWORD(value);
325 static void set_rel_axis_value(struct wine_input_private *ext, int code, int value)
327 int index;
328 if (code < HID_REL_MAX && REL_TO_HID_MAP[code][0] != 0)
330 index = ext->rel_map[code];
331 if (value > 127) value = 127;
332 if (value < -127) value = -127;
333 ext->current_report_buffer[index] = value;
337 static INT count_buttons(int device_fd, BYTE *map)
339 int i;
340 int button_count = 0;
341 BYTE keybits[(KEY_MAX+7)/8];
343 if (ioctl(device_fd, EVIOCGBIT(EV_KEY, sizeof(keybits)), keybits) == -1)
345 WARN("ioctl(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno, strerror(errno));
346 return FALSE;
349 for (i = BTN_MISC; i < KEY_MAX; i++)
351 if (test_bit(keybits, i))
353 if (map) map[i] = button_count;
354 button_count++;
357 return button_count;
360 static INT count_abs_axis(int device_fd)
362 BYTE absbits[(ABS_MAX+7)/8];
363 int abs_count = 0;
364 int i;
366 if (ioctl(device_fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits) == -1)
368 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
369 return 0;
372 for (i = 0; i < HID_ABS_MAX; i++)
373 if (test_bit(absbits, i) &&
374 (ABS_TO_HID_MAP[i][1] >= HID_USAGE_GENERIC_X &&
375 ABS_TO_HID_MAP[i][1] <= HID_USAGE_GENERIC_WHEEL))
376 abs_count++;
377 return abs_count;
380 static BOOL build_report_descriptor(struct wine_input_private *ext, struct udev_device *dev)
382 int abs_pages[TOP_ABS_PAGE][HID_ABS_MAX+1];
383 int rel_pages[TOP_REL_PAGE][HID_REL_MAX+1];
384 BYTE absbits[(ABS_MAX+7)/8];
385 BYTE relbits[(REL_MAX+7)/8];
386 BYTE *report_ptr;
387 INT i, descript_size;
388 INT report_size;
389 INT button_count, abs_count, rel_count, hat_count;
390 const BYTE *device_usage = what_am_I(dev);
392 if (ioctl(ext->base.device_fd, EVIOCGBIT(EV_REL, sizeof(relbits)), relbits) == -1)
394 WARN("ioctl(EVIOCGBIT, EV_REL) failed: %d %s\n", errno, strerror(errno));
395 return FALSE;
397 if (ioctl(ext->base.device_fd, EVIOCGBIT(EV_ABS, sizeof(absbits)), absbits) == -1)
399 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
400 return FALSE;
403 descript_size = sizeof(REPORT_HEADER) + sizeof(REPORT_TAIL);
404 report_size = 0;
406 /* For now lump all buttons just into incremental usages, Ignore Keys */
407 button_count = count_buttons(ext->base.device_fd, ext->button_map);
408 if (button_count)
410 descript_size += sizeof(REPORT_BUTTONS);
411 if (button_count % 8)
412 descript_size += sizeof(REPORT_PADDING);
413 report_size = (button_count + 7) / 8;
416 abs_count = 0;
417 memset(abs_pages, 0, sizeof(abs_pages));
418 for (i = 0; i < HID_ABS_MAX; i++)
419 if (test_bit(absbits, i))
421 abs_pages[ABS_TO_HID_MAP[i][0]][0]++;
422 abs_pages[ABS_TO_HID_MAP[i][0]][abs_pages[ABS_TO_HID_MAP[i][0]][0]] = i;
424 ioctl(ext->base.device_fd, EVIOCGABS(i), &(ext->abs_map[i]));
425 if (abs_pages[ABS_TO_HID_MAP[i][0]][0] == 1)
427 descript_size += sizeof(REPORT_AXIS_HEADER);
428 descript_size += sizeof(REPORT_ABS_AXIS_TAIL);
431 /* Skip page 0, aka HID_USAGE_PAGE_UNDEFINED */
432 for (i = 1; i < TOP_ABS_PAGE; i++)
433 if (abs_pages[i][0] > 0)
435 int j;
436 descript_size += sizeof(REPORT_AXIS_USAGE) * abs_pages[i][0];
437 for (j = 1; j <= abs_pages[i][0]; j++)
439 ext->abs_map[abs_pages[i][j]].report_index = report_size;
440 report_size+=4;
442 abs_count++;
445 rel_count = 0;
446 memset(rel_pages, 0, sizeof(rel_pages));
447 for (i = 0; i < HID_REL_MAX; i++)
448 if (test_bit(relbits, i))
450 rel_pages[REL_TO_HID_MAP[i][0]][0]++;
451 rel_pages[REL_TO_HID_MAP[i][0]][rel_pages[REL_TO_HID_MAP[i][0]][0]] = i;
452 if (rel_pages[REL_TO_HID_MAP[i][0]][0] == 1)
454 descript_size += sizeof(REPORT_AXIS_HEADER);
455 descript_size += sizeof(REPORT_REL_AXIS_TAIL);
458 /* Skip page 0, aka HID_USAGE_PAGE_UNDEFINED */
459 for (i = 1; i < TOP_REL_PAGE; i++)
460 if (rel_pages[i][0] > 0)
462 int j;
463 descript_size += sizeof(REPORT_AXIS_USAGE) * rel_pages[i][0];
464 for (j = 1; j <= rel_pages[i][0]; j++)
466 ext->rel_map[rel_pages[i][j]] = report_size;
467 report_size++;
469 rel_count++;
472 hat_count = 0;
473 for (i = ABS_HAT0X; i <=ABS_HAT3X; i+=2)
474 if (test_bit(absbits, i))
476 ext->hat_map[i - ABS_HAT0X] = report_size;
477 ext->hat_values[i - ABS_HAT0X] = 0;
478 ext->hat_values[i - ABS_HAT0X + 1] = 0;
479 report_size++;
480 hat_count++;
483 TRACE("Report Descriptor will be %i bytes\n", descript_size);
484 TRACE("Report will be %i bytes\n", report_size);
486 ext->report_descriptor = HeapAlloc(GetProcessHeap(), 0, descript_size);
487 if (!ext->report_descriptor)
489 ERR("Failed to alloc report descriptor\n");
490 return FALSE;
492 report_ptr = ext->report_descriptor;
494 memcpy(report_ptr, REPORT_HEADER, sizeof(REPORT_HEADER));
495 report_ptr[IDX_HEADER_PAGE] = device_usage[0];
496 report_ptr[IDX_HEADER_USAGE] = device_usage[1];
497 report_ptr += sizeof(REPORT_HEADER);
498 if (button_count)
500 report_ptr = add_button_block(report_ptr, 1, button_count);
501 if (button_count % 8)
503 BYTE padding = 8 - (button_count % 8);
504 report_ptr = add_padding_block(report_ptr, padding);
507 if (abs_count)
509 for (i = 1; i < TOP_ABS_PAGE; i++)
511 if (abs_pages[i][0])
513 BYTE usages[HID_ABS_MAX];
514 int j;
515 for (j = 0; j < abs_pages[i][0]; j++)
516 usages[j] = ABS_TO_HID_MAP[abs_pages[i][j+1]][1];
517 report_ptr = add_axis_block(report_ptr, abs_pages[i][0], i, usages, TRUE, &ext->abs_map[abs_pages[i][1]]);
521 if (rel_count)
523 for (i = 1; i < TOP_REL_PAGE; i++)
525 if (rel_pages[i][0])
527 BYTE usages[HID_REL_MAX];
528 int j;
529 for (j = 0; j < rel_pages[i][0]; j++)
530 usages[j] = REL_TO_HID_MAP[rel_pages[i][j+1]][1];
531 report_ptr = add_axis_block(report_ptr, rel_pages[i][0], i, usages, FALSE, NULL);
535 if (hat_count)
536 report_ptr = add_hatswitch(report_ptr, hat_count);
538 memcpy(report_ptr, REPORT_TAIL, sizeof(REPORT_TAIL));
540 ext->report_descriptor_size = descript_size;
541 ext->buffer_length = report_size;
542 ext->current_report_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, report_size);
543 if (ext->current_report_buffer == NULL)
545 ERR("Failed to alloc report buffer\n");
546 HeapFree(GetProcessHeap(), 0, ext->report_descriptor);
547 return FALSE;
549 ext->last_report_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, report_size);
550 if (ext->last_report_buffer == NULL)
552 ERR("Failed to alloc report buffer\n");
553 HeapFree(GetProcessHeap(), 0, ext->report_descriptor);
554 HeapFree(GetProcessHeap(), 0, ext->current_report_buffer);
555 return FALSE;
557 ext->report_state = FIRST;
559 /* Initialize axis in the report */
560 for (i = 0; i < HID_ABS_MAX; i++)
561 if (test_bit(absbits, i))
562 set_abs_axis_value(ext, i, ext->abs_map[i].info.value);
564 return TRUE;
567 static BOOL set_report_from_event(struct wine_input_private *ext, struct input_event *ie)
569 switch(ie->type)
571 #ifdef EV_SYN
572 case EV_SYN:
573 switch (ie->code)
575 case SYN_REPORT:
576 if (ext->report_state == NORMAL)
578 memcpy(ext->last_report_buffer, ext->current_report_buffer, ext->buffer_length);
579 return TRUE;
581 else
583 if (ext->report_state == DROPPED)
584 memcpy(ext->current_report_buffer, ext->last_report_buffer, ext->buffer_length);
585 ext->report_state = NORMAL;
587 break;
588 case SYN_DROPPED:
589 TRACE_(hid_report)("received SY_DROPPED\n");
590 ext->report_state = DROPPED;
592 return FALSE;
593 #endif
594 #ifdef EV_MSC
595 case EV_MSC:
596 return FALSE;
597 #endif
598 case EV_KEY:
599 set_button_value(ext->button_map[ie->code], ie->value, ext->current_report_buffer);
600 return FALSE;
601 case EV_ABS:
602 set_abs_axis_value(ext, ie->code, ie->value);
603 return FALSE;
604 case EV_REL:
605 set_rel_axis_value(ext, ie->code, ie->value);
606 return FALSE;
607 default:
608 ERR("TODO: Process Report (%i, %i)\n",ie->type, ie->code);
609 return FALSE;
612 #endif
614 static inline WCHAR *strdupAtoW(const char *src)
616 WCHAR *dst;
617 DWORD len;
618 if (!src) return NULL;
619 len = MultiByteToWideChar(CP_UNIXCP, 0, src, -1, NULL, 0);
620 if ((dst = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
621 MultiByteToWideChar(CP_UNIXCP, 0, src, -1, dst, len);
622 return dst;
625 static WCHAR *get_sysattr_string(struct udev_device *dev, const char *sysattr)
627 const char *attr = udev_device_get_sysattr_value(dev, sysattr);
628 if (!attr)
630 WARN("Could not get %s from device\n", sysattr);
631 return NULL;
633 return strdupAtoW(attr);
636 static int compare_platform_device(DEVICE_OBJECT *device, void *platform_dev)
638 struct udev_device *dev1 = impl_from_DEVICE_OBJECT(device)->udev_device;
639 struct udev_device *dev2 = platform_dev;
640 return strcmp(udev_device_get_syspath(dev1), udev_device_get_syspath(dev2));
643 static NTSTATUS hidraw_get_reportdescriptor(DEVICE_OBJECT *device, BYTE *buffer, DWORD length, DWORD *out_length)
645 #ifdef HAVE_LINUX_HIDRAW_H
646 struct hidraw_report_descriptor descriptor;
647 struct platform_private *private = impl_from_DEVICE_OBJECT(device);
649 if (ioctl(private->device_fd, HIDIOCGRDESCSIZE, &descriptor.size) == -1)
651 WARN("ioctl(HIDIOCGRDESCSIZE) failed: %d %s\n", errno, strerror(errno));
652 return STATUS_UNSUCCESSFUL;
655 *out_length = descriptor.size;
657 if (length < descriptor.size)
658 return STATUS_BUFFER_TOO_SMALL;
659 if (!descriptor.size)
660 return STATUS_SUCCESS;
662 if (ioctl(private->device_fd, HIDIOCGRDESC, &descriptor) == -1)
664 WARN("ioctl(HIDIOCGRDESC) failed: %d %s\n", errno, strerror(errno));
665 return STATUS_UNSUCCESSFUL;
668 memcpy(buffer, descriptor.value, descriptor.size);
669 return STATUS_SUCCESS;
670 #else
671 return STATUS_NOT_IMPLEMENTED;
672 #endif
675 static NTSTATUS hidraw_get_string(DEVICE_OBJECT *device, DWORD index, WCHAR *buffer, DWORD length)
677 struct udev_device *hiddev;
678 struct platform_private *private = impl_from_DEVICE_OBJECT(device);
679 WCHAR *str = NULL;
681 hiddev = udev_device_get_parent_with_subsystem_devtype(private->udev_device, "hid", NULL);
682 if (hiddev)
684 switch (index)
686 case HID_STRING_ID_IPRODUCT:
687 str = get_sysattr_string(hiddev, "product");
688 break;
689 case HID_STRING_ID_IMANUFACTURER:
690 str = get_sysattr_string(hiddev, "manufacturer");
691 break;
692 case HID_STRING_ID_ISERIALNUMBER:
693 str = get_sysattr_string(hiddev, "serial");
694 break;
695 default:
696 ERR("Unhandled string index %08x\n", index);
697 return STATUS_NOT_IMPLEMENTED;
700 else
702 #ifdef HAVE_LINUX_HIDRAW_H
703 switch (index)
705 case HID_STRING_ID_IPRODUCT:
707 char buf[MAX_PATH];
708 if (ioctl(private->device_fd, HIDIOCGRAWNAME(MAX_PATH), buf) == -1)
709 WARN("ioctl(HIDIOCGRAWNAME) failed: %d %s\n", errno, strerror(errno));
710 else
711 str = strdupAtoW(buf);
712 break;
714 case HID_STRING_ID_IMANUFACTURER:
715 break;
716 case HID_STRING_ID_ISERIALNUMBER:
717 break;
718 default:
719 ERR("Unhandled string index %08x\n", index);
720 return STATUS_NOT_IMPLEMENTED;
722 #else
723 return STATUS_NOT_IMPLEMENTED;
724 #endif
727 if (!str)
729 if (!length) return STATUS_BUFFER_TOO_SMALL;
730 buffer[0] = 0;
731 return STATUS_SUCCESS;
734 if (length <= strlenW(str))
736 HeapFree(GetProcessHeap(), 0, str);
737 return STATUS_BUFFER_TOO_SMALL;
740 strcpyW(buffer, str);
741 HeapFree(GetProcessHeap(), 0, str);
742 return STATUS_SUCCESS;
745 static DWORD CALLBACK device_report_thread(void *args)
747 DEVICE_OBJECT *device = (DEVICE_OBJECT*)args;
748 struct platform_private *private = impl_from_DEVICE_OBJECT(device);
749 struct pollfd plfds[2];
751 plfds[0].fd = private->device_fd;
752 plfds[0].events = POLLIN;
753 plfds[0].revents = 0;
754 plfds[1].fd = private->control_pipe[0];
755 plfds[1].events = POLLIN;
756 plfds[1].revents = 0;
758 while (1)
760 int size;
761 BYTE report_buffer[1024];
763 if (poll(plfds, 2, -1) <= 0) continue;
764 if (plfds[1].revents)
765 break;
766 size = read(plfds[0].fd, report_buffer, sizeof(report_buffer));
767 if (size == -1)
768 TRACE_(hid_report)("Read failed. Likely an unplugged device\n");
769 else if (size == 0)
770 TRACE_(hid_report)("Failed to read report\n");
771 else
772 process_hid_report(device, report_buffer, size);
774 return 0;
777 static NTSTATUS begin_report_processing(DEVICE_OBJECT *device)
779 struct platform_private *private = impl_from_DEVICE_OBJECT(device);
781 if (private->report_thread)
782 return STATUS_SUCCESS;
784 if (pipe(private->control_pipe) != 0)
786 ERR("Control pipe creation failed\n");
787 return STATUS_UNSUCCESSFUL;
790 private->report_thread = CreateThread(NULL, 0, device_report_thread, device, 0, NULL);
791 if (!private->report_thread)
793 ERR("Unable to create device report thread\n");
794 close(private->control_pipe[0]);
795 close(private->control_pipe[1]);
796 return STATUS_UNSUCCESSFUL;
798 else
799 return STATUS_SUCCESS;
802 static NTSTATUS hidraw_set_output_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *written)
804 struct platform_private* ext = impl_from_DEVICE_OBJECT(device);
805 ssize_t rc;
807 if (id != 0)
808 rc = write(ext->device_fd, report, length);
809 else
811 BYTE report_buffer[1024];
813 if (length + 1 > sizeof(report_buffer))
815 ERR("Output report buffer too small\n");
816 return STATUS_UNSUCCESSFUL;
819 report_buffer[0] = 0;
820 memcpy(&report_buffer[1], report, length);
821 rc = write(ext->device_fd, report_buffer, length + 1);
823 if (rc > 0)
825 *written = rc;
826 return STATUS_SUCCESS;
828 else
830 *written = 0;
831 return STATUS_UNSUCCESSFUL;
835 static NTSTATUS hidraw_get_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *read)
837 #if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCGFEATURE)
838 int rc;
839 struct platform_private* ext = impl_from_DEVICE_OBJECT(device);
840 report[0] = id;
841 length = min(length, 0x1fff);
842 rc = ioctl(ext->device_fd, HIDIOCGFEATURE(length), report);
843 if (rc >= 0)
845 *read = rc;
846 return STATUS_SUCCESS;
848 else
850 *read = 0;
851 return STATUS_UNSUCCESSFUL;
853 #else
854 *read = 0;
855 return STATUS_NOT_IMPLEMENTED;
856 #endif
859 static NTSTATUS hidraw_set_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *written)
861 #if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCSFEATURE)
862 int rc;
863 struct platform_private* ext = impl_from_DEVICE_OBJECT(device);
864 BYTE *feature_buffer;
865 BYTE buffer[1024];
867 if (id == 0)
869 if (length + 1 > sizeof(buffer))
871 ERR("Output feature buffer too small\n");
872 return STATUS_UNSUCCESSFUL;
874 buffer[0] = 0;
875 memcpy(&buffer[1], report, length);
876 feature_buffer = buffer;
877 length = length + 1;
879 else
880 feature_buffer = report;
881 length = min(length, 0x1fff);
882 rc = ioctl(ext->device_fd, HIDIOCSFEATURE(length), feature_buffer);
883 if (rc >= 0)
885 *written = rc;
886 return STATUS_SUCCESS;
888 else
890 *written = 0;
891 return STATUS_UNSUCCESSFUL;
893 #else
894 *written = 0;
895 return STATUS_NOT_IMPLEMENTED;
896 #endif
899 static const platform_vtbl hidraw_vtbl =
901 compare_platform_device,
902 hidraw_get_reportdescriptor,
903 hidraw_get_string,
904 begin_report_processing,
905 hidraw_set_output_report,
906 hidraw_get_feature_report,
907 hidraw_set_feature_report,
910 #ifdef HAS_PROPER_INPUT_HEADER
912 static inline struct wine_input_private *input_impl_from_DEVICE_OBJECT(DEVICE_OBJECT *device)
914 return (struct wine_input_private*)get_platform_private(device);
917 static NTSTATUS lnxev_get_reportdescriptor(DEVICE_OBJECT *device, BYTE *buffer, DWORD length, DWORD *out_length)
919 struct wine_input_private *ext = input_impl_from_DEVICE_OBJECT(device);
921 *out_length = ext->report_descriptor_size;
923 if (length < ext->report_descriptor_size)
924 return STATUS_BUFFER_TOO_SMALL;
926 memcpy(buffer, ext->report_descriptor, ext->report_descriptor_size);
928 return STATUS_SUCCESS;
931 static NTSTATUS lnxev_get_string(DEVICE_OBJECT *device, DWORD index, WCHAR *buffer, DWORD length)
933 struct wine_input_private *ext = input_impl_from_DEVICE_OBJECT(device);
934 char str[255];
936 str[0] = 0;
937 switch (index)
939 case HID_STRING_ID_IPRODUCT:
940 ioctl(ext->base.device_fd, EVIOCGNAME(sizeof(str)), str);
941 break;
942 case HID_STRING_ID_IMANUFACTURER:
943 strcpy(str,"evdev");
944 break;
945 case HID_STRING_ID_ISERIALNUMBER:
946 ioctl(ext->base.device_fd, EVIOCGUNIQ(sizeof(str)), str);
947 break;
948 default:
949 ERR("Unhandled string index %i\n", index);
952 MultiByteToWideChar(CP_ACP, 0, str, -1, buffer, length);
953 return STATUS_SUCCESS;
956 static DWORD CALLBACK lnxev_device_report_thread(void *args)
958 DEVICE_OBJECT *device = (DEVICE_OBJECT*)args;
959 struct wine_input_private *private = input_impl_from_DEVICE_OBJECT(device);
960 struct pollfd plfds[2];
962 plfds[0].fd = private->base.device_fd;
963 plfds[0].events = POLLIN;
964 plfds[0].revents = 0;
965 plfds[1].fd = private->base.control_pipe[0];
966 plfds[1].events = POLLIN;
967 plfds[1].revents = 0;
969 while (1)
971 int size;
972 struct input_event ie;
974 if (poll(plfds, 2, -1) <= 0) continue;
975 if (plfds[1].revents || !private->current_report_buffer || private->buffer_length == 0)
976 break;
977 size = read(plfds[0].fd, &ie, sizeof(ie));
978 if (size == -1)
979 TRACE_(hid_report)("Read failed. Likely an unplugged device\n");
980 else if (size == 0)
981 TRACE_(hid_report)("Failed to read report\n");
982 else if (set_report_from_event(private, &ie))
983 process_hid_report(device, private->current_report_buffer, private->buffer_length);
985 return 0;
988 static NTSTATUS lnxev_begin_report_processing(DEVICE_OBJECT *device)
990 struct wine_input_private *private = input_impl_from_DEVICE_OBJECT(device);
992 if (private->base.report_thread)
993 return STATUS_SUCCESS;
995 if (pipe(private->base.control_pipe) != 0)
997 ERR("Control pipe creation failed\n");
998 return STATUS_UNSUCCESSFUL;
1001 private->base.report_thread = CreateThread(NULL, 0, lnxev_device_report_thread, device, 0, NULL);
1002 if (!private->base.report_thread)
1004 ERR("Unable to create device report thread\n");
1005 close(private->base.control_pipe[0]);
1006 close(private->base.control_pipe[1]);
1007 return STATUS_UNSUCCESSFUL;
1009 return STATUS_SUCCESS;
1012 static NTSTATUS lnxev_set_output_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *written)
1014 *written = 0;
1015 return STATUS_NOT_IMPLEMENTED;
1018 static NTSTATUS lnxev_get_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *read)
1020 *read = 0;
1021 return STATUS_NOT_IMPLEMENTED;
1024 static NTSTATUS lnxev_set_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *written)
1026 *written = 0;
1027 return STATUS_NOT_IMPLEMENTED;
1030 static const platform_vtbl lnxev_vtbl = {
1031 compare_platform_device,
1032 lnxev_get_reportdescriptor,
1033 lnxev_get_string,
1034 lnxev_begin_report_processing,
1035 lnxev_set_output_report,
1036 lnxev_get_feature_report,
1037 lnxev_set_feature_report,
1039 #endif
1041 static int check_same_device(DEVICE_OBJECT *device, void* context)
1043 return !compare_platform_device(device, context);
1046 static int parse_uevent_info(const char *uevent, DWORD *vendor_id,
1047 DWORD *product_id, WCHAR **serial_number)
1049 DWORD bus_type;
1050 char *tmp;
1051 char *saveptr = NULL;
1052 char *line;
1053 char *key;
1054 char *value;
1056 int found_id = 0;
1057 int found_serial = 0;
1059 tmp = heap_alloc(strlen(uevent) + 1);
1060 strcpy(tmp, uevent);
1061 line = strtok_r(tmp, "\n", &saveptr);
1062 while (line != NULL)
1064 /* line: "KEY=value" */
1065 key = line;
1066 value = strchr(line, '=');
1067 if (!value)
1069 goto next_line;
1071 *value = '\0';
1072 value++;
1074 if (strcmp(key, "HID_ID") == 0)
1077 * type vendor product
1078 * HID_ID=0003:000005AC:00008242
1080 int ret = sscanf(value, "%x:%x:%x", &bus_type, vendor_id, product_id);
1081 if (ret == 3)
1082 found_id = 1;
1084 else if (strcmp(key, "HID_UNIQ") == 0)
1086 /* The caller has to free the serial number */
1087 if (strlen(value))
1089 *serial_number = (WCHAR*)strdupAtoW(value);
1090 found_serial = 1;
1094 next_line:
1095 line = strtok_r(NULL, "\n", &saveptr);
1098 heap_free(tmp);
1099 return (found_id && found_serial);
1102 static void try_add_device(struct udev_device *dev)
1104 DWORD vid = 0, pid = 0, version = 0;
1105 struct udev_device *hiddev = NULL;
1106 DEVICE_OBJECT *device = NULL;
1107 const char *subsystem;
1108 const char *devnode;
1109 WCHAR *serial = NULL;
1110 BOOL is_gamepad = FALSE;
1111 int fd;
1112 static const CHAR *base_serial = "0000";
1114 if (!(devnode = udev_device_get_devnode(dev)))
1115 return;
1117 if ((fd = open(devnode, O_RDWR)) == -1)
1119 WARN("Unable to open udev device %s: %s\n", debugstr_a(devnode), strerror(errno));
1120 return;
1123 subsystem = udev_device_get_subsystem(dev);
1124 hiddev = udev_device_get_parent_with_subsystem_devtype(dev, "hid", NULL);
1125 if (hiddev)
1127 #ifdef HAS_PROPER_INPUT_HEADER
1128 const platform_vtbl *other_vtbl = NULL;
1129 DEVICE_OBJECT *dup = NULL;
1130 if (strcmp(subsystem, "hidraw") == 0)
1131 other_vtbl = &lnxev_vtbl;
1132 else if (strcmp(subsystem, "input") == 0)
1133 other_vtbl = &hidraw_vtbl;
1135 if (other_vtbl)
1136 dup = bus_enumerate_hid_devices(other_vtbl, check_same_device, dev);
1137 if (dup)
1139 TRACE("Duplicate cross bus device (%p) found, not adding the new one\n", dup);
1140 close(fd);
1141 return;
1143 #endif
1144 parse_uevent_info(udev_device_get_sysattr_value(hiddev, "uevent"),
1145 &vid, &pid, &serial);
1146 if (serial == NULL)
1147 serial = strdupAtoW(base_serial);
1149 #ifdef HAS_PROPER_INPUT_HEADER
1150 else
1152 struct input_id device_id = {0};
1153 char device_uid[255];
1155 if (ioctl(fd, EVIOCGID, &device_id) < 0)
1156 WARN("ioctl(EVIOCGID) failed: %d %s\n", errno, strerror(errno));
1157 device_uid[0] = 0;
1158 if (ioctl(fd, EVIOCGUNIQ(254), device_uid) >= 0 && device_uid[0])
1159 serial = strdupAtoW(device_uid);
1161 vid = device_id.vendor;
1162 pid = device_id.product;
1163 version = device_id.version;
1165 #else
1166 else
1167 WARN("Could not get device to query VID, PID, Version and Serial\n");
1168 #endif
1170 if (is_xbox_gamepad(vid, pid))
1171 is_gamepad = TRUE;
1172 #ifdef HAS_PROPER_INPUT_HEADER
1173 else
1175 int axes=0, buttons=0;
1176 axes = count_abs_axis(fd);
1177 buttons = count_buttons(fd, NULL);
1178 is_gamepad = (axes == 6 && buttons >= 14);
1180 #endif
1183 TRACE("Found udev device %s (vid %04x, pid %04x, version %u, serial %s)\n",
1184 debugstr_a(devnode), vid, pid, version, debugstr_w(serial));
1186 if (strcmp(subsystem, "hidraw") == 0)
1188 device = bus_create_hid_device(udev_driver_obj, hidraw_busidW, vid, pid, version, 0, serial, is_gamepad,
1189 &GUID_DEVCLASS_HIDRAW, &hidraw_vtbl, sizeof(struct platform_private));
1191 #ifdef HAS_PROPER_INPUT_HEADER
1192 else if (strcmp(subsystem, "input") == 0)
1194 device = bus_create_hid_device(udev_driver_obj, lnxev_busidW, vid, pid, version, 0, serial, is_gamepad,
1195 &GUID_DEVCLASS_LINUXEVENT, &lnxev_vtbl, sizeof(struct wine_input_private));
1197 #endif
1199 if (device)
1201 struct platform_private *private = impl_from_DEVICE_OBJECT(device);
1202 private->udev_device = udev_device_ref(dev);
1203 private->device_fd = fd;
1204 #ifdef HAS_PROPER_INPUT_HEADER
1205 if (strcmp(subsystem, "input") == 0)
1206 if (!build_report_descriptor((struct wine_input_private*)private, dev))
1208 ERR("Building report descriptor failed, removing device\n");
1209 close(fd);
1210 udev_device_unref(dev);
1211 bus_remove_hid_device(device);
1212 HeapFree(GetProcessHeap(), 0, serial);
1213 return;
1215 #endif
1216 IoInvalidateDeviceRelations(device, BusRelations);
1218 else
1220 WARN("Ignoring device %s with subsystem %s\n", debugstr_a(devnode), subsystem);
1221 close(fd);
1224 HeapFree(GetProcessHeap(), 0, serial);
1227 static void try_remove_device(struct udev_device *dev)
1229 DEVICE_OBJECT *device = NULL;
1230 struct platform_private* private;
1231 #ifdef HAS_PROPER_INPUT_HEADER
1232 BOOL is_input = FALSE;
1233 #endif
1235 device = bus_find_hid_device(&hidraw_vtbl, dev);
1236 #ifdef HAS_PROPER_INPUT_HEADER
1237 if (device == NULL)
1239 device = bus_find_hid_device(&lnxev_vtbl, dev);
1240 is_input = TRUE;
1242 #endif
1243 if (!device) return;
1245 IoInvalidateDeviceRelations(device, RemovalRelations);
1247 private = impl_from_DEVICE_OBJECT(device);
1249 if (private->report_thread)
1251 write(private->control_pipe[1], "q", 1);
1252 WaitForSingleObject(private->report_thread, INFINITE);
1253 close(private->control_pipe[0]);
1254 close(private->control_pipe[1]);
1255 CloseHandle(private->report_thread);
1256 #ifdef HAS_PROPER_INPUT_HEADER
1257 if (strcmp(udev_device_get_subsystem(dev), "input") == 0)
1259 HeapFree(GetProcessHeap(), 0, ((struct wine_input_private*)private)->current_report_buffer);
1260 HeapFree(GetProcessHeap(), 0, ((struct wine_input_private*)private)->last_report_buffer);
1262 #endif
1265 #ifdef HAS_PROPER_INPUT_HEADER
1266 if (is_input)
1268 struct wine_input_private *ext = (struct wine_input_private*)private;
1269 HeapFree(GetProcessHeap(), 0, ext->report_descriptor);
1271 #endif
1273 dev = private->udev_device;
1274 close(private->device_fd);
1275 bus_remove_hid_device(device);
1276 udev_device_unref(dev);
1279 static void build_initial_deviceset(void)
1281 struct udev_enumerate *enumerate;
1282 struct udev_list_entry *devices, *dev_list_entry;
1284 enumerate = udev_enumerate_new(udev_context);
1285 if (!enumerate)
1287 WARN("Unable to create udev enumeration object\n");
1288 return;
1291 if (!disable_hidraw)
1292 if (udev_enumerate_add_match_subsystem(enumerate, "hidraw") < 0)
1293 WARN("Failed to add subsystem 'hidraw' to enumeration\n");
1294 #ifdef HAS_PROPER_INPUT_HEADER
1295 if (!disable_input)
1297 if (udev_enumerate_add_match_subsystem(enumerate, "input") < 0)
1298 WARN("Failed to add subsystem 'input' to enumeration\n");
1300 #endif
1302 if (udev_enumerate_scan_devices(enumerate) < 0)
1303 WARN("Enumeration scan failed\n");
1305 devices = udev_enumerate_get_list_entry(enumerate);
1306 udev_list_entry_foreach(dev_list_entry, devices)
1308 struct udev_device *dev;
1309 const char *path;
1311 path = udev_list_entry_get_name(dev_list_entry);
1312 if ((dev = udev_device_new_from_syspath(udev_context, path)))
1314 try_add_device(dev);
1315 udev_device_unref(dev);
1319 udev_enumerate_unref(enumerate);
1322 static struct udev_monitor *create_monitor(struct pollfd *pfd)
1324 struct udev_monitor *monitor;
1325 int systems = 0;
1327 monitor = udev_monitor_new_from_netlink(udev_context, "udev");
1328 if (!monitor)
1330 WARN("Unable to get udev monitor object\n");
1331 return NULL;
1334 if (!disable_hidraw)
1336 if (udev_monitor_filter_add_match_subsystem_devtype(monitor, "hidraw", NULL) < 0)
1337 WARN("Failed to add 'hidraw' subsystem to monitor\n");
1338 else
1339 systems++;
1341 #ifdef HAS_PROPER_INPUT_HEADER
1342 if (!disable_input)
1344 if (udev_monitor_filter_add_match_subsystem_devtype(monitor, "input", NULL) < 0)
1345 WARN("Failed to add 'input' subsystem to monitor\n");
1346 else
1347 systems++;
1349 #endif
1350 if (systems == 0)
1352 WARN("No subsystems added to monitor\n");
1353 goto error;
1356 if (udev_monitor_enable_receiving(monitor) < 0)
1357 goto error;
1359 if ((pfd->fd = udev_monitor_get_fd(monitor)) >= 0)
1361 pfd->events = POLLIN;
1362 return monitor;
1365 error:
1366 WARN("Failed to start monitoring\n");
1367 udev_monitor_unref(monitor);
1368 return NULL;
1371 static void process_monitor_event(struct udev_monitor *monitor)
1373 struct udev_device *dev;
1374 const char *action;
1376 dev = udev_monitor_receive_device(monitor);
1377 if (!dev)
1379 FIXME("Failed to get device that has changed\n");
1380 return;
1383 action = udev_device_get_action(dev);
1384 TRACE("Received action %s for udev device %s\n", debugstr_a(action),
1385 debugstr_a(udev_device_get_devnode(dev)));
1387 if (!action)
1388 WARN("No action received\n");
1389 else if (strcmp(action, "add") == 0)
1390 try_add_device(dev);
1391 else if (strcmp(action, "remove") == 0)
1392 try_remove_device(dev);
1393 else
1394 WARN("Unhandled action %s\n", debugstr_a(action));
1396 udev_device_unref(dev);
1399 static DWORD CALLBACK deviceloop_thread(void *args)
1401 struct udev_monitor *monitor;
1402 HANDLE init_done = args;
1403 struct pollfd pfd;
1405 monitor = create_monitor(&pfd);
1406 build_initial_deviceset();
1407 SetEvent(init_done);
1409 while (monitor)
1411 if (poll(&pfd, 1, -1) <= 0) continue;
1412 process_monitor_event(monitor);
1415 TRACE("Monitor thread exiting\n");
1416 if (monitor)
1417 udev_monitor_unref(monitor);
1418 return 0;
1421 void udev_driver_unload( void )
1423 TRACE("Unload Driver\n");
1426 NTSTATUS WINAPI udev_driver_init(DRIVER_OBJECT *driver, UNICODE_STRING *registry_path)
1428 HANDLE events[2];
1429 DWORD result;
1430 static const WCHAR hidraw_disabledW[] = {'D','i','s','a','b','l','e','H','i','d','r','a','w',0};
1431 static const UNICODE_STRING hidraw_disabled = {sizeof(hidraw_disabledW) - sizeof(WCHAR), sizeof(hidraw_disabledW), (WCHAR*)hidraw_disabledW};
1432 static const WCHAR input_disabledW[] = {'D','i','s','a','b','l','e','I','n','p','u','t',0};
1433 static const UNICODE_STRING input_disabled = {sizeof(input_disabledW) - sizeof(WCHAR), sizeof(input_disabledW), (WCHAR*)input_disabledW};
1435 TRACE("(%p, %s)\n", driver, debugstr_w(registry_path->Buffer));
1437 if (!(udev_context = udev_new()))
1439 ERR("Can't create udev object\n");
1440 return STATUS_UNSUCCESSFUL;
1443 udev_driver_obj = driver;
1444 driver->MajorFunction[IRP_MJ_PNP] = common_pnp_dispatch;
1445 driver->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = hid_internal_dispatch;
1447 disable_hidraw = check_bus_option(registry_path, &hidraw_disabled, 0);
1448 if (disable_hidraw)
1449 TRACE("UDEV hidraw devices disabled in registry\n");
1451 #ifdef HAS_PROPER_INPUT_HEADER
1452 disable_input = check_bus_option(registry_path, &input_disabled, 0);
1453 if (disable_input)
1454 TRACE("UDEV input devices disabled in registry\n");
1455 #endif
1457 if (!(events[0] = CreateEventW(NULL, TRUE, FALSE, NULL)))
1458 goto error;
1459 if (!(events[1] = CreateThread(NULL, 0, deviceloop_thread, events[0], 0, NULL)))
1461 CloseHandle(events[0]);
1462 goto error;
1465 result = WaitForMultipleObjects(2, events, FALSE, INFINITE);
1466 CloseHandle(events[0]);
1467 CloseHandle(events[1]);
1468 if (result == WAIT_OBJECT_0)
1470 TRACE("Initialization successful\n");
1471 return STATUS_SUCCESS;
1474 error:
1475 ERR("Failed to initialize udev device thread\n");
1476 udev_unref(udev_context);
1477 udev_context = NULL;
1478 udev_driver_obj = NULL;
1479 return STATUS_UNSUCCESSFUL;
1482 #else
1484 NTSTATUS WINAPI udev_driver_init(DRIVER_OBJECT *driver, UNICODE_STRING *registry_path)
1486 WARN("Wine was compiled without UDEV support\n");
1487 return STATUS_NOT_IMPLEMENTED;
1490 void udev_driver_unload( void )
1492 TRACE("Stub: Unload Driver\n");
1495 #endif /* HAVE_UDEV */