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
, HID_USAGE_SIMULATION_ACCELERATOR
}, /*ABS_GAS*/
143 {HID_USAGE_PAGE_SIMULATION
, HID_USAGE_SIMULATION_BRAKE
}, /*ABS_BRAKE*/
157 {HID_USAGE_PAGE_DIGITIZER
, HID_USAGE_DIGITIZER_TIP_PRESSURE
}, /*ABS_PRESSURE*/
158 {0, 0}, /*ABS_DISTANCE*/
159 {HID_USAGE_PAGE_DIGITIZER
, HID_USAGE_DIGITIZER_X_TILT
}, /*ABS_TILT_X*/
160 {HID_USAGE_PAGE_DIGITIZER
, HID_USAGE_DIGITIZER_Y_TILT
}, /*ABS_TILT_Y*/
161 {0, 0}, /*ABS_TOOL_WIDTH*/
165 {HID_USAGE_PAGE_CONSUMER
, HID_USAGE_CONSUMER_VOLUME
} /*ABS_VOLUME*/
167 #define HID_ABS_MAX (ABS_VOLUME+1)
168 C_ASSERT(ARRAY_SIZE(ABS_TO_HID_MAP
) == HID_ABS_MAX
);
169 #define TOP_ABS_PAGE (HID_USAGE_PAGE_DIGITIZER+1)
171 static const BYTE REL_TO_HID_MAP
[][2] = {
172 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_X
}, /* REL_X */
173 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_Y
}, /* REL_Y */
174 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_Z
}, /* REL_Z */
175 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RX
}, /* REL_RX */
176 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RY
}, /* REL_RY */
177 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_RZ
}, /* REL_RZ */
178 {0, 0}, /* REL_HWHEEL */
179 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_DIAL
}, /* REL_DIAL */
180 {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_WHEEL
}, /* REL_WHEEL */
181 {0, 0} /* REL_MISC */
184 #define HID_REL_MAX (REL_MISC+1)
185 #define TOP_REL_PAGE (HID_USAGE_PAGE_CONSUMER+1)
187 struct wine_input_absinfo
{
188 struct input_absinfo info
;
192 struct wine_input_private
{
193 struct platform_private base
;
196 BYTE
*last_report_buffer
;
197 BYTE
*current_report_buffer
;
198 enum { FIRST
, NORMAL
, DROPPED
} report_state
;
200 int report_descriptor_size
;
201 BYTE
*report_descriptor
;
204 BYTE button_map
[KEY_MAX
];
205 BYTE rel_map
[HID_REL_MAX
];
208 struct wine_input_absinfo abs_map
[HID_ABS_MAX
];
211 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
213 static BYTE
*add_axis_block(BYTE
*report_ptr
, BYTE count
, BYTE page
, BYTE
*usages
, BOOL absolute
, const struct wine_input_absinfo
*absinfo
)
216 memcpy(report_ptr
, REPORT_AXIS_HEADER
, sizeof(REPORT_AXIS_HEADER
));
217 report_ptr
[IDX_AXIS_PAGE
] = page
;
218 report_ptr
+= sizeof(REPORT_AXIS_HEADER
);
219 for (i
= 0; i
< count
; i
++)
221 memcpy(report_ptr
, REPORT_AXIS_USAGE
, sizeof(REPORT_AXIS_USAGE
));
222 report_ptr
[IDX_AXIS_USAGE
] = usages
[i
];
223 report_ptr
+= sizeof(REPORT_AXIS_USAGE
);
227 memcpy(report_ptr
, REPORT_ABS_AXIS_TAIL
, sizeof(REPORT_ABS_AXIS_TAIL
));
230 *((int*)&report_ptr
[IDX_ABS_LOG_MINIMUM
]) = LE_DWORD(absinfo
->info
.minimum
);
231 *((int*)&report_ptr
[IDX_ABS_LOG_MAXIMUM
]) = LE_DWORD(absinfo
->info
.maximum
);
232 *((int*)&report_ptr
[IDX_ABS_PHY_MINIMUM
]) = LE_DWORD(absinfo
->info
.minimum
);
233 *((int*)&report_ptr
[IDX_ABS_PHY_MAXIMUM
]) = LE_DWORD(absinfo
->info
.maximum
);
235 report_ptr
[IDX_ABS_AXIS_COUNT
] = count
;
236 report_ptr
+= sizeof(REPORT_ABS_AXIS_TAIL
);
240 memcpy(report_ptr
, REPORT_REL_AXIS_TAIL
, sizeof(REPORT_REL_AXIS_TAIL
));
241 report_ptr
[IDX_REL_AXIS_COUNT
] = count
;
242 report_ptr
+= sizeof(REPORT_REL_AXIS_TAIL
);
247 static const BYTE
* what_am_I(struct udev_device
*dev
)
249 static const BYTE Unknown
[2] = {HID_USAGE_PAGE_GENERIC
, 0};
250 static const BYTE Mouse
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_MOUSE
};
251 static const BYTE Keyboard
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_KEYBOARD
};
252 static const BYTE Gamepad
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_GAMEPAD
};
253 static const BYTE Keypad
[2] = {HID_USAGE_PAGE_GENERIC
, HID_USAGE_GENERIC_KEYPAD
};
254 static const BYTE Tablet
[2] = {HID_USAGE_PAGE_DIGITIZER
, HID_USAGE_DIGITIZER_PEN
};
255 static const BYTE Touchscreen
[2] = {HID_USAGE_PAGE_DIGITIZER
, HID_USAGE_DIGITIZER_TOUCH_SCREEN
};
256 static const BYTE Touchpad
[2] = {HID_USAGE_PAGE_DIGITIZER
, HID_USAGE_DIGITIZER_TOUCH_PAD
};
258 struct udev_device
*parent
= dev
;
260 /* Look to the parents until we get a clue */
263 if (udev_device_get_property_value(parent
, "ID_INPUT_MOUSE"))
265 else if (udev_device_get_property_value(parent
, "ID_INPUT_KEYBOARD"))
267 else if (udev_device_get_property_value(parent
, "ID_INPUT_JOYSTICK"))
269 else if (udev_device_get_property_value(parent
, "ID_INPUT_KEY"))
271 else if (udev_device_get_property_value(parent
, "ID_INPUT_TOUCHPAD"))
273 else if (udev_device_get_property_value(parent
, "ID_INPUT_TOUCHSCREEN"))
275 else if (udev_device_get_property_value(parent
, "ID_INPUT_TABLET"))
278 parent
= udev_device_get_parent_with_subsystem_devtype(parent
, "input", NULL
);
283 static void set_button_value(int index
, int value
, BYTE
* buffer
)
285 int bindex
= index
/ 8;
291 buffer
[bindex
] = buffer
[bindex
] | mask
;
295 buffer
[bindex
] = buffer
[bindex
] & mask
;
299 static void set_abs_axis_value(struct wine_input_private
*ext
, int code
, int value
)
302 /* check for hatswitches */
303 if (code
<= ABS_HAT3Y
&& code
>= ABS_HAT0X
)
305 index
= code
- ABS_HAT0X
;
306 ext
->hat_values
[index
] = value
;
307 if ((code
- ABS_HAT0X
) % 2)
312 if (ext
->hat_values
[index
] == 0)
314 if (ext
->hat_values
[index
+1] == 0)
316 else if (ext
->hat_values
[index
+1] < 0)
321 else if (ext
->hat_values
[index
] > 0)
323 if (ext
->hat_values
[index
+1] == 0)
325 else if (ext
->hat_values
[index
+1] < 0)
332 if (ext
->hat_values
[index
+1] == 0)
334 else if (ext
->hat_values
[index
+1] < 0)
339 ext
->current_report_buffer
[ext
->hat_map
[index
]] = value
;
341 else if (code
< HID_ABS_MAX
&& ABS_TO_HID_MAP
[code
][0] != 0)
343 index
= ext
->abs_map
[code
].report_index
;
344 *((DWORD
*)&ext
->current_report_buffer
[index
]) = LE_DWORD(value
);
348 static void set_rel_axis_value(struct wine_input_private
*ext
, int code
, int value
)
351 if (code
< HID_REL_MAX
&& REL_TO_HID_MAP
[code
][0] != 0)
353 index
= ext
->rel_map
[code
];
354 if (value
> 127) value
= 127;
355 if (value
< -127) value
= -127;
356 ext
->current_report_buffer
[index
] = value
;
360 static INT
count_buttons(int device_fd
, BYTE
*map
)
363 int button_count
= 0;
364 BYTE keybits
[(KEY_MAX
+7)/8];
366 if (ioctl(device_fd
, EVIOCGBIT(EV_KEY
, sizeof(keybits
)), keybits
) == -1)
368 WARN("ioctl(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno
, strerror(errno
));
372 for (i
= BTN_MISC
; i
< KEY_MAX
; i
++)
374 if (test_bit(keybits
, i
))
376 if (map
) map
[i
] = button_count
;
383 static INT
count_abs_axis(int device_fd
)
385 BYTE absbits
[(ABS_MAX
+7)/8];
389 if (ioctl(device_fd
, EVIOCGBIT(EV_ABS
, sizeof(absbits
)), absbits
) == -1)
391 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno
, strerror(errno
));
395 for (i
= 0; i
< HID_ABS_MAX
; i
++)
396 if (test_bit(absbits
, i
) &&
397 (ABS_TO_HID_MAP
[i
][1] >= HID_USAGE_GENERIC_X
&&
398 ABS_TO_HID_MAP
[i
][1] <= HID_USAGE_GENERIC_WHEEL
))
403 static BOOL
build_report_descriptor(struct wine_input_private
*ext
, struct udev_device
*dev
)
405 int abs_pages
[TOP_ABS_PAGE
][HID_ABS_MAX
+1];
406 int rel_pages
[TOP_REL_PAGE
][HID_REL_MAX
+1];
407 BYTE absbits
[(ABS_MAX
+7)/8];
408 BYTE relbits
[(REL_MAX
+7)/8];
410 INT i
, descript_size
;
412 INT button_count
, abs_count
, rel_count
, hat_count
;
413 const BYTE
*device_usage
= what_am_I(dev
);
415 if (ioctl(ext
->base
.device_fd
, EVIOCGBIT(EV_REL
, sizeof(relbits
)), relbits
) == -1)
417 WARN("ioctl(EVIOCGBIT, EV_REL) failed: %d %s\n", errno
, strerror(errno
));
420 if (ioctl(ext
->base
.device_fd
, EVIOCGBIT(EV_ABS
, sizeof(absbits
)), absbits
) == -1)
422 WARN("ioctl(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno
, strerror(errno
));
426 descript_size
= sizeof(REPORT_HEADER
) + sizeof(REPORT_TAIL
);
430 memset(abs_pages
, 0, sizeof(abs_pages
));
431 for (i
= 0; i
< HID_ABS_MAX
; i
++)
432 if (test_bit(absbits
, i
))
434 abs_pages
[ABS_TO_HID_MAP
[i
][0]][0]++;
435 abs_pages
[ABS_TO_HID_MAP
[i
][0]][abs_pages
[ABS_TO_HID_MAP
[i
][0]][0]] = i
;
437 ioctl(ext
->base
.device_fd
, EVIOCGABS(i
), &(ext
->abs_map
[i
]));
439 /* Skip page 0, aka HID_USAGE_PAGE_UNDEFINED */
440 for (i
= 1; i
< TOP_ABS_PAGE
; i
++)
441 if (abs_pages
[i
][0] > 0)
444 descript_size
+= sizeof(REPORT_AXIS_USAGE
) * abs_pages
[i
][0];
445 for (j
= 1; j
<= abs_pages
[i
][0]; j
++)
447 ext
->abs_map
[abs_pages
[i
][j
]].report_index
= report_size
;
452 descript_size
+= sizeof(REPORT_AXIS_HEADER
) * abs_count
;
453 descript_size
+= sizeof(REPORT_ABS_AXIS_TAIL
) * abs_count
;
456 memset(rel_pages
, 0, sizeof(rel_pages
));
457 for (i
= 0; i
< HID_REL_MAX
; i
++)
458 if (test_bit(relbits
, i
))
460 rel_pages
[REL_TO_HID_MAP
[i
][0]][0]++;
461 rel_pages
[REL_TO_HID_MAP
[i
][0]][rel_pages
[REL_TO_HID_MAP
[i
][0]][0]] = i
;
463 /* Skip page 0, aka HID_USAGE_PAGE_UNDEFINED */
464 for (i
= 1; i
< TOP_REL_PAGE
; i
++)
465 if (rel_pages
[i
][0] > 0)
468 descript_size
+= sizeof(REPORT_AXIS_USAGE
) * rel_pages
[i
][0];
469 for (j
= 1; j
<= rel_pages
[i
][0]; j
++)
471 ext
->rel_map
[rel_pages
[i
][j
]] = report_size
;
476 descript_size
+= sizeof(REPORT_AXIS_HEADER
) * rel_count
;
477 descript_size
+= sizeof(REPORT_REL_AXIS_TAIL
) * rel_count
;
479 /* For now lump all buttons just into incremental usages, Ignore Keys */
480 ext
->button_start
= report_size
;
481 button_count
= count_buttons(ext
->base
.device_fd
, ext
->button_map
);
484 descript_size
+= sizeof(REPORT_BUTTONS
);
485 if (button_count
% 8)
486 descript_size
+= sizeof(REPORT_PADDING
);
487 report_size
+= (button_count
+ 7) / 8;
491 for (i
= ABS_HAT0X
; i
<=ABS_HAT3X
; i
+=2)
492 if (test_bit(absbits
, i
))
494 ext
->hat_map
[i
- ABS_HAT0X
] = report_size
;
495 ext
->hat_values
[i
- ABS_HAT0X
] = 0;
496 ext
->hat_values
[i
- ABS_HAT0X
+ 1] = 0;
501 descript_size
+= sizeof(REPORT_HATSWITCH
);
503 TRACE("Report Descriptor will be %i bytes\n", descript_size
);
504 TRACE("Report will be %i bytes\n", report_size
);
506 ext
->report_descriptor
= HeapAlloc(GetProcessHeap(), 0, descript_size
);
507 if (!ext
->report_descriptor
)
509 ERR("Failed to alloc report descriptor\n");
512 report_ptr
= ext
->report_descriptor
;
514 memcpy(report_ptr
, REPORT_HEADER
, sizeof(REPORT_HEADER
));
515 report_ptr
[IDX_HEADER_PAGE
] = device_usage
[0];
516 report_ptr
[IDX_HEADER_USAGE
] = device_usage
[1];
517 report_ptr
+= sizeof(REPORT_HEADER
);
520 for (i
= 1; i
< TOP_ABS_PAGE
; i
++)
524 BYTE usages
[HID_ABS_MAX
];
526 for (j
= 0; j
< abs_pages
[i
][0]; j
++)
527 usages
[j
] = ABS_TO_HID_MAP
[abs_pages
[i
][j
+1]][1];
528 report_ptr
= add_axis_block(report_ptr
, abs_pages
[i
][0], i
, usages
, TRUE
, &ext
->abs_map
[abs_pages
[i
][1]]);
534 for (i
= 1; i
< TOP_REL_PAGE
; i
++)
538 BYTE usages
[HID_REL_MAX
];
540 for (j
= 0; j
< rel_pages
[i
][0]; j
++)
541 usages
[j
] = REL_TO_HID_MAP
[rel_pages
[i
][j
+1]][1];
542 report_ptr
= add_axis_block(report_ptr
, rel_pages
[i
][0], i
, usages
, FALSE
, NULL
);
548 report_ptr
= add_button_block(report_ptr
, 1, button_count
);
549 if (button_count
% 8)
551 BYTE padding
= 8 - (button_count
% 8);
552 report_ptr
= add_padding_block(report_ptr
, padding
);
556 report_ptr
= add_hatswitch(report_ptr
, hat_count
);
558 memcpy(report_ptr
, REPORT_TAIL
, sizeof(REPORT_TAIL
));
560 ext
->report_descriptor_size
= descript_size
;
561 ext
->buffer_length
= report_size
;
562 ext
->current_report_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, report_size
);
563 if (ext
->current_report_buffer
== NULL
)
565 ERR("Failed to alloc report buffer\n");
566 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
569 ext
->last_report_buffer
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, report_size
);
570 if (ext
->last_report_buffer
== NULL
)
572 ERR("Failed to alloc report buffer\n");
573 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
574 HeapFree(GetProcessHeap(), 0, ext
->current_report_buffer
);
577 ext
->report_state
= FIRST
;
579 /* Initialize axis in the report */
580 for (i
= 0; i
< HID_ABS_MAX
; i
++)
581 if (test_bit(absbits
, i
))
582 set_abs_axis_value(ext
, i
, ext
->abs_map
[i
].info
.value
);
587 static BOOL
set_report_from_event(struct wine_input_private
*ext
, struct input_event
*ie
)
596 if (ext
->report_state
== NORMAL
)
598 memcpy(ext
->last_report_buffer
, ext
->current_report_buffer
, ext
->buffer_length
);
603 if (ext
->report_state
== DROPPED
)
604 memcpy(ext
->current_report_buffer
, ext
->last_report_buffer
, ext
->buffer_length
);
605 ext
->report_state
= NORMAL
;
609 TRACE_(hid_report
)("received SY_DROPPED\n");
610 ext
->report_state
= DROPPED
;
619 set_button_value(ext
->button_start
+ ext
->button_map
[ie
->code
], ie
->value
, ext
->current_report_buffer
);
622 set_abs_axis_value(ext
, ie
->code
, ie
->value
);
625 set_rel_axis_value(ext
, ie
->code
, ie
->value
);
628 ERR("TODO: Process Report (%i, %i)\n",ie
->type
, ie
->code
);
634 static inline WCHAR
*strdupAtoW(const char *src
)
638 if (!src
) return NULL
;
639 len
= MultiByteToWideChar(CP_UNIXCP
, 0, src
, -1, NULL
, 0);
640 if ((dst
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
641 MultiByteToWideChar(CP_UNIXCP
, 0, src
, -1, dst
, len
);
645 static WCHAR
*get_sysattr_string(struct udev_device
*dev
, const char *sysattr
)
647 const char *attr
= udev_device_get_sysattr_value(dev
, sysattr
);
650 WARN("Could not get %s from device\n", sysattr
);
653 return strdupAtoW(attr
);
656 static int compare_platform_device(DEVICE_OBJECT
*device
, void *platform_dev
)
658 struct udev_device
*dev1
= impl_from_DEVICE_OBJECT(device
)->udev_device
;
659 struct udev_device
*dev2
= platform_dev
;
660 return strcmp(udev_device_get_syspath(dev1
), udev_device_get_syspath(dev2
));
663 static NTSTATUS
hidraw_get_reportdescriptor(DEVICE_OBJECT
*device
, BYTE
*buffer
, DWORD length
, DWORD
*out_length
)
665 #ifdef HAVE_LINUX_HIDRAW_H
666 struct hidraw_report_descriptor descriptor
;
667 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
669 if (ioctl(private->device_fd
, HIDIOCGRDESCSIZE
, &descriptor
.size
) == -1)
671 WARN("ioctl(HIDIOCGRDESCSIZE) failed: %d %s\n", errno
, strerror(errno
));
672 return STATUS_UNSUCCESSFUL
;
675 *out_length
= descriptor
.size
;
677 if (length
< descriptor
.size
)
678 return STATUS_BUFFER_TOO_SMALL
;
679 if (!descriptor
.size
)
680 return STATUS_SUCCESS
;
682 if (ioctl(private->device_fd
, HIDIOCGRDESC
, &descriptor
) == -1)
684 WARN("ioctl(HIDIOCGRDESC) failed: %d %s\n", errno
, strerror(errno
));
685 return STATUS_UNSUCCESSFUL
;
688 memcpy(buffer
, descriptor
.value
, descriptor
.size
);
689 return STATUS_SUCCESS
;
691 return STATUS_NOT_IMPLEMENTED
;
695 static NTSTATUS
hidraw_get_string(DEVICE_OBJECT
*device
, DWORD index
, WCHAR
*buffer
, DWORD length
)
697 struct udev_device
*usbdev
;
698 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
701 usbdev
= udev_device_get_parent_with_subsystem_devtype(private->udev_device
, "usb", "usb_device");
706 case HID_STRING_ID_IPRODUCT
:
707 str
= get_sysattr_string(usbdev
, "product");
709 case HID_STRING_ID_IMANUFACTURER
:
710 str
= get_sysattr_string(usbdev
, "manufacturer");
712 case HID_STRING_ID_ISERIALNUMBER
:
713 str
= get_sysattr_string(usbdev
, "serial");
716 ERR("Unhandled string index %08x\n", index
);
717 return STATUS_NOT_IMPLEMENTED
;
722 #ifdef HAVE_LINUX_HIDRAW_H
725 case HID_STRING_ID_IPRODUCT
:
728 if (ioctl(private->device_fd
, HIDIOCGRAWNAME(MAX_PATH
), buf
) == -1)
729 WARN("ioctl(HIDIOCGRAWNAME) failed: %d %s\n", errno
, strerror(errno
));
731 str
= strdupAtoW(buf
);
734 case HID_STRING_ID_IMANUFACTURER
:
736 case HID_STRING_ID_ISERIALNUMBER
:
739 ERR("Unhandled string index %08x\n", index
);
740 return STATUS_NOT_IMPLEMENTED
;
743 return STATUS_NOT_IMPLEMENTED
;
749 if (!length
) return STATUS_BUFFER_TOO_SMALL
;
751 return STATUS_SUCCESS
;
754 if (length
<= strlenW(str
))
756 HeapFree(GetProcessHeap(), 0, str
);
757 return STATUS_BUFFER_TOO_SMALL
;
760 strcpyW(buffer
, str
);
761 HeapFree(GetProcessHeap(), 0, str
);
762 return STATUS_SUCCESS
;
765 static DWORD CALLBACK
device_report_thread(void *args
)
767 DEVICE_OBJECT
*device
= (DEVICE_OBJECT
*)args
;
768 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
769 struct pollfd plfds
[2];
771 plfds
[0].fd
= private->device_fd
;
772 plfds
[0].events
= POLLIN
;
773 plfds
[0].revents
= 0;
774 plfds
[1].fd
= private->control_pipe
[0];
775 plfds
[1].events
= POLLIN
;
776 plfds
[1].revents
= 0;
781 BYTE report_buffer
[1024];
783 if (poll(plfds
, 2, -1) <= 0) continue;
784 if (plfds
[1].revents
)
786 size
= read(plfds
[0].fd
, report_buffer
, sizeof(report_buffer
));
788 TRACE_(hid_report
)("Read failed. Likely an unplugged device %d %s\n", errno
, strerror(errno
));
790 TRACE_(hid_report
)("Failed to read report\n");
792 process_hid_report(device
, report_buffer
, size
);
797 static NTSTATUS
begin_report_processing(DEVICE_OBJECT
*device
)
799 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
801 if (private->report_thread
)
802 return STATUS_SUCCESS
;
804 if (pipe(private->control_pipe
) != 0)
806 ERR("Control pipe creation failed\n");
807 return STATUS_UNSUCCESSFUL
;
810 private->report_thread
= CreateThread(NULL
, 0, device_report_thread
, device
, 0, NULL
);
811 if (!private->report_thread
)
813 ERR("Unable to create device report thread\n");
814 close(private->control_pipe
[0]);
815 close(private->control_pipe
[1]);
816 return STATUS_UNSUCCESSFUL
;
819 return STATUS_SUCCESS
;
822 static NTSTATUS
hidraw_set_output_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
824 struct platform_private
* ext
= impl_from_DEVICE_OBJECT(device
);
828 rc
= write(ext
->device_fd
, report
, length
);
831 BYTE report_buffer
[1024];
833 if (length
+ 1 > sizeof(report_buffer
))
835 ERR("Output report buffer too small\n");
836 return STATUS_UNSUCCESSFUL
;
839 report_buffer
[0] = 0;
840 memcpy(&report_buffer
[1], report
, length
);
841 rc
= write(ext
->device_fd
, report_buffer
, length
+ 1);
846 return STATUS_SUCCESS
;
850 TRACE("write failed: %d %d %s\n", rc
, errno
, strerror(errno
));
852 return STATUS_UNSUCCESSFUL
;
856 static NTSTATUS
hidraw_get_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*read
)
858 #if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCGFEATURE)
860 struct platform_private
* ext
= impl_from_DEVICE_OBJECT(device
);
862 length
= min(length
, 0x1fff);
863 rc
= ioctl(ext
->device_fd
, HIDIOCGFEATURE(length
), report
);
867 return STATUS_SUCCESS
;
871 TRACE_(hid_report
)("ioctl(HIDIOCGFEATURE(%d)) failed: %d %s\n", length
, errno
, strerror(errno
));
873 return STATUS_UNSUCCESSFUL
;
877 return STATUS_NOT_IMPLEMENTED
;
881 static NTSTATUS
hidraw_set_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
883 #if defined(HAVE_LINUX_HIDRAW_H) && defined(HIDIOCSFEATURE)
885 struct platform_private
* ext
= impl_from_DEVICE_OBJECT(device
);
886 BYTE
*feature_buffer
;
891 if (length
+ 1 > sizeof(buffer
))
893 ERR("Output feature buffer too small\n");
894 return STATUS_UNSUCCESSFUL
;
897 memcpy(&buffer
[1], report
, length
);
898 feature_buffer
= buffer
;
902 feature_buffer
= report
;
903 length
= min(length
, 0x1fff);
904 rc
= ioctl(ext
->device_fd
, HIDIOCSFEATURE(length
), feature_buffer
);
908 return STATUS_SUCCESS
;
912 TRACE_(hid_report
)("ioctl(HIDIOCSFEATURE(%d)) failed: %d %s\n", length
, errno
, strerror(errno
));
914 return STATUS_UNSUCCESSFUL
;
918 return STATUS_NOT_IMPLEMENTED
;
922 static const platform_vtbl hidraw_vtbl
=
924 compare_platform_device
,
925 hidraw_get_reportdescriptor
,
927 begin_report_processing
,
928 hidraw_set_output_report
,
929 hidraw_get_feature_report
,
930 hidraw_set_feature_report
,
933 #ifdef HAS_PROPER_INPUT_HEADER
935 static inline struct wine_input_private
*input_impl_from_DEVICE_OBJECT(DEVICE_OBJECT
*device
)
937 return (struct wine_input_private
*)get_platform_private(device
);
940 static NTSTATUS
lnxev_get_reportdescriptor(DEVICE_OBJECT
*device
, BYTE
*buffer
, DWORD length
, DWORD
*out_length
)
942 struct wine_input_private
*ext
= input_impl_from_DEVICE_OBJECT(device
);
944 *out_length
= ext
->report_descriptor_size
;
946 if (length
< ext
->report_descriptor_size
)
947 return STATUS_BUFFER_TOO_SMALL
;
949 memcpy(buffer
, ext
->report_descriptor
, ext
->report_descriptor_size
);
951 return STATUS_SUCCESS
;
954 static NTSTATUS
lnxev_get_string(DEVICE_OBJECT
*device
, DWORD index
, WCHAR
*buffer
, DWORD length
)
956 struct wine_input_private
*ext
= input_impl_from_DEVICE_OBJECT(device
);
962 case HID_STRING_ID_IPRODUCT
:
963 ioctl(ext
->base
.device_fd
, EVIOCGNAME(sizeof(str
)), str
);
965 case HID_STRING_ID_IMANUFACTURER
:
968 case HID_STRING_ID_ISERIALNUMBER
:
969 ioctl(ext
->base
.device_fd
, EVIOCGUNIQ(sizeof(str
)), str
);
972 ERR("Unhandled string index %i\n", index
);
975 MultiByteToWideChar(CP_ACP
, 0, str
, -1, buffer
, length
);
976 return STATUS_SUCCESS
;
979 static DWORD CALLBACK
lnxev_device_report_thread(void *args
)
981 DEVICE_OBJECT
*device
= (DEVICE_OBJECT
*)args
;
982 struct wine_input_private
*private = input_impl_from_DEVICE_OBJECT(device
);
983 struct pollfd plfds
[2];
985 plfds
[0].fd
= private->base
.device_fd
;
986 plfds
[0].events
= POLLIN
;
987 plfds
[0].revents
= 0;
988 plfds
[1].fd
= private->base
.control_pipe
[0];
989 plfds
[1].events
= POLLIN
;
990 plfds
[1].revents
= 0;
995 struct input_event ie
;
997 if (poll(plfds
, 2, -1) <= 0) continue;
998 if (plfds
[1].revents
|| !private->current_report_buffer
|| private->buffer_length
== 0)
1000 size
= read(plfds
[0].fd
, &ie
, sizeof(ie
));
1002 TRACE_(hid_report
)("Read failed. Likely an unplugged device\n");
1004 TRACE_(hid_report
)("Failed to read report\n");
1005 else if (set_report_from_event(private, &ie
))
1006 process_hid_report(device
, private->current_report_buffer
, private->buffer_length
);
1011 static NTSTATUS
lnxev_begin_report_processing(DEVICE_OBJECT
*device
)
1013 struct wine_input_private
*private = input_impl_from_DEVICE_OBJECT(device
);
1015 if (private->base
.report_thread
)
1016 return STATUS_SUCCESS
;
1018 if (pipe(private->base
.control_pipe
) != 0)
1020 ERR("Control pipe creation failed\n");
1021 return STATUS_UNSUCCESSFUL
;
1024 private->base
.report_thread
= CreateThread(NULL
, 0, lnxev_device_report_thread
, device
, 0, NULL
);
1025 if (!private->base
.report_thread
)
1027 ERR("Unable to create device report thread\n");
1028 close(private->base
.control_pipe
[0]);
1029 close(private->base
.control_pipe
[1]);
1030 return STATUS_UNSUCCESSFUL
;
1032 return STATUS_SUCCESS
;
1035 static NTSTATUS
lnxev_set_output_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
1038 return STATUS_NOT_IMPLEMENTED
;
1041 static NTSTATUS
lnxev_get_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*read
)
1044 return STATUS_NOT_IMPLEMENTED
;
1047 static NTSTATUS
lnxev_set_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
1050 return STATUS_NOT_IMPLEMENTED
;
1053 static const platform_vtbl lnxev_vtbl
= {
1054 compare_platform_device
,
1055 lnxev_get_reportdescriptor
,
1057 lnxev_begin_report_processing
,
1058 lnxev_set_output_report
,
1059 lnxev_get_feature_report
,
1060 lnxev_set_feature_report
,
1064 static int check_same_device(DEVICE_OBJECT
*device
, void* context
)
1066 return !compare_platform_device(device
, context
);
1069 static int parse_uevent_info(const char *uevent
, DWORD
*vendor_id
,
1070 DWORD
*product_id
, WORD
*input
, WCHAR
**serial_number
)
1074 char *saveptr
= NULL
;
1080 int found_serial
= 0;
1082 tmp
= heap_alloc(strlen(uevent
) + 1);
1083 strcpy(tmp
, uevent
);
1084 line
= strtok_r(tmp
, "\n", &saveptr
);
1085 while (line
!= NULL
)
1087 /* line: "KEY=value" */
1089 value
= strchr(line
, '=');
1097 if (strcmp(key
, "HID_ID") == 0)
1100 * type vendor product
1101 * HID_ID=0003:000005AC:00008242
1103 int ret
= sscanf(value
, "%x:%x:%x", &bus_type
, vendor_id
, product_id
);
1107 else if (strcmp(key
, "HID_UNIQ") == 0)
1109 /* The caller has to free the serial number */
1112 *serial_number
= strdupAtoW(value
);
1116 else if (strcmp(key
, "HID_PHYS") == 0)
1118 const char *input_no
= strstr(value
, "input");
1120 *input
= atoi(input_no
+5 );
1124 line
= strtok_r(NULL
, "\n", &saveptr
);
1128 return (found_id
&& found_serial
);
1131 static DWORD
a_to_bcd(const char *s
)
1135 int shift
= strlen(s
) - 1;
1136 for (c
= s
; *c
; ++c
)
1138 r
|= (*c
- '0') << (shift
* 4);
1144 static void try_add_device(struct udev_device
*dev
)
1146 DWORD vid
= 0, pid
= 0, version
= 0;
1147 struct udev_device
*hiddev
= NULL
, *walk_device
;
1148 DEVICE_OBJECT
*device
= NULL
;
1149 const char *subsystem
;
1150 const char *devnode
;
1151 WCHAR
*serial
= NULL
;
1152 BOOL is_gamepad
= FALSE
;
1155 static const CHAR
*base_serial
= "0000";
1157 if (!(devnode
= udev_device_get_devnode(dev
)))
1160 if ((fd
= open(devnode
, O_RDWR
)) == -1)
1162 WARN("Unable to open udev device %s: %s\n", debugstr_a(devnode
), strerror(errno
));
1166 subsystem
= udev_device_get_subsystem(dev
);
1167 hiddev
= udev_device_get_parent_with_subsystem_devtype(dev
, "hid", NULL
);
1170 const char *bcdDevice
= NULL
;
1171 #ifdef HAS_PROPER_INPUT_HEADER
1172 const platform_vtbl
*other_vtbl
= NULL
;
1173 DEVICE_OBJECT
*dup
= NULL
;
1174 if (strcmp(subsystem
, "hidraw") == 0)
1175 other_vtbl
= &lnxev_vtbl
;
1176 else if (strcmp(subsystem
, "input") == 0)
1177 other_vtbl
= &hidraw_vtbl
;
1180 dup
= bus_enumerate_hid_devices(other_vtbl
, check_same_device
, dev
);
1183 TRACE("Duplicate cross bus device (%p) found, not adding the new one\n", dup
);
1188 parse_uevent_info(udev_device_get_sysattr_value(hiddev
, "uevent"),
1189 &vid
, &pid
, &input
, &serial
);
1191 serial
= strdupAtoW(base_serial
);
1194 while (walk_device
&& !bcdDevice
)
1196 bcdDevice
= udev_device_get_sysattr_value(walk_device
, "bcdDevice");
1197 walk_device
= udev_device_get_parent(walk_device
);
1201 version
= a_to_bcd(bcdDevice
);
1204 #ifdef HAS_PROPER_INPUT_HEADER
1207 struct input_id device_id
= {0};
1208 char device_uid
[255];
1210 if (ioctl(fd
, EVIOCGID
, &device_id
) < 0)
1211 WARN("ioctl(EVIOCGID) failed: %d %s\n", errno
, strerror(errno
));
1213 if (ioctl(fd
, EVIOCGUNIQ(254), device_uid
) >= 0 && device_uid
[0])
1214 serial
= strdupAtoW(device_uid
);
1216 vid
= device_id
.vendor
;
1217 pid
= device_id
.product
;
1218 version
= device_id
.version
;
1222 WARN("Could not get device to query VID, PID, Version and Serial\n");
1225 if (is_xbox_gamepad(vid
, pid
))
1227 #ifdef HAS_PROPER_INPUT_HEADER
1230 int axes
=0, buttons
=0;
1231 axes
= count_abs_axis(fd
);
1232 buttons
= count_buttons(fd
, NULL
);
1233 is_gamepad
= (axes
== 6 && buttons
>= 14);
1236 if (input
== (WORD
)-1 && is_gamepad
)
1240 TRACE("Found udev device %s (vid %04x, pid %04x, version %u, serial %s)\n",
1241 debugstr_a(devnode
), vid
, pid
, version
, debugstr_w(serial
));
1243 if (strcmp(subsystem
, "hidraw") == 0)
1245 device
= bus_create_hid_device(hidraw_busidW
, vid
, pid
, input
, version
, 0, serial
, is_gamepad
,
1246 &hidraw_vtbl
, sizeof(struct platform_private
));
1248 #ifdef HAS_PROPER_INPUT_HEADER
1249 else if (strcmp(subsystem
, "input") == 0)
1251 device
= bus_create_hid_device(lnxev_busidW
, vid
, pid
, input
, version
, 0, serial
, is_gamepad
,
1252 &lnxev_vtbl
, sizeof(struct wine_input_private
));
1258 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
1259 private->udev_device
= udev_device_ref(dev
);
1260 private->device_fd
= fd
;
1261 #ifdef HAS_PROPER_INPUT_HEADER
1262 if (strcmp(subsystem
, "input") == 0)
1263 if (!build_report_descriptor((struct wine_input_private
*)private, dev
))
1265 ERR("Building report descriptor failed, removing device\n");
1267 udev_device_unref(dev
);
1268 bus_unlink_hid_device(device
);
1269 bus_remove_hid_device(device
);
1270 HeapFree(GetProcessHeap(), 0, serial
);
1274 IoInvalidateDeviceRelations(bus_pdo
, BusRelations
);
1278 WARN("Ignoring device %s with subsystem %s\n", debugstr_a(devnode
), subsystem
);
1282 HeapFree(GetProcessHeap(), 0, serial
);
1285 static void try_remove_device(struct udev_device
*dev
)
1287 DEVICE_OBJECT
*device
= NULL
;
1288 struct platform_private
* private;
1289 #ifdef HAS_PROPER_INPUT_HEADER
1290 BOOL is_input
= FALSE
;
1293 device
= bus_find_hid_device(&hidraw_vtbl
, dev
);
1294 #ifdef HAS_PROPER_INPUT_HEADER
1297 device
= bus_find_hid_device(&lnxev_vtbl
, dev
);
1301 if (!device
) return;
1303 bus_unlink_hid_device(device
);
1304 IoInvalidateDeviceRelations(bus_pdo
, BusRelations
);
1306 private = impl_from_DEVICE_OBJECT(device
);
1308 if (private->report_thread
)
1310 write(private->control_pipe
[1], "q", 1);
1311 WaitForSingleObject(private->report_thread
, INFINITE
);
1312 close(private->control_pipe
[0]);
1313 close(private->control_pipe
[1]);
1314 CloseHandle(private->report_thread
);
1315 #ifdef HAS_PROPER_INPUT_HEADER
1316 if (strcmp(udev_device_get_subsystem(dev
), "input") == 0)
1318 HeapFree(GetProcessHeap(), 0, ((struct wine_input_private
*)private)->current_report_buffer
);
1319 HeapFree(GetProcessHeap(), 0, ((struct wine_input_private
*)private)->last_report_buffer
);
1324 #ifdef HAS_PROPER_INPUT_HEADER
1327 struct wine_input_private
*ext
= (struct wine_input_private
*)private;
1328 HeapFree(GetProcessHeap(), 0, ext
->report_descriptor
);
1332 dev
= private->udev_device
;
1333 close(private->device_fd
);
1334 bus_remove_hid_device(device
);
1335 udev_device_unref(dev
);
1338 static void build_initial_deviceset(void)
1340 struct udev_enumerate
*enumerate
;
1341 struct udev_list_entry
*devices
, *dev_list_entry
;
1343 enumerate
= udev_enumerate_new(udev_context
);
1346 WARN("Unable to create udev enumeration object\n");
1350 if (!disable_hidraw
)
1351 if (udev_enumerate_add_match_subsystem(enumerate
, "hidraw") < 0)
1352 WARN("Failed to add subsystem 'hidraw' to enumeration\n");
1353 #ifdef HAS_PROPER_INPUT_HEADER
1356 if (udev_enumerate_add_match_subsystem(enumerate
, "input") < 0)
1357 WARN("Failed to add subsystem 'input' to enumeration\n");
1361 if (udev_enumerate_scan_devices(enumerate
) < 0)
1362 WARN("Enumeration scan failed\n");
1364 devices
= udev_enumerate_get_list_entry(enumerate
);
1365 udev_list_entry_foreach(dev_list_entry
, devices
)
1367 struct udev_device
*dev
;
1370 path
= udev_list_entry_get_name(dev_list_entry
);
1371 if ((dev
= udev_device_new_from_syspath(udev_context
, path
)))
1373 try_add_device(dev
);
1374 udev_device_unref(dev
);
1378 udev_enumerate_unref(enumerate
);
1381 static struct udev_monitor
*create_monitor(struct pollfd
*pfd
)
1383 struct udev_monitor
*monitor
;
1386 monitor
= udev_monitor_new_from_netlink(udev_context
, "udev");
1389 WARN("Unable to get udev monitor object\n");
1393 if (!disable_hidraw
)
1395 if (udev_monitor_filter_add_match_subsystem_devtype(monitor
, "hidraw", NULL
) < 0)
1396 WARN("Failed to add 'hidraw' subsystem to monitor\n");
1400 #ifdef HAS_PROPER_INPUT_HEADER
1403 if (udev_monitor_filter_add_match_subsystem_devtype(monitor
, "input", NULL
) < 0)
1404 WARN("Failed to add 'input' subsystem to monitor\n");
1411 WARN("No subsystems added to monitor\n");
1415 if (udev_monitor_enable_receiving(monitor
) < 0)
1418 if ((pfd
->fd
= udev_monitor_get_fd(monitor
)) >= 0)
1420 pfd
->events
= POLLIN
;
1425 WARN("Failed to start monitoring\n");
1426 udev_monitor_unref(monitor
);
1430 static void process_monitor_event(struct udev_monitor
*monitor
)
1432 struct udev_device
*dev
;
1435 dev
= udev_monitor_receive_device(monitor
);
1438 FIXME("Failed to get device that has changed\n");
1442 action
= udev_device_get_action(dev
);
1443 TRACE("Received action %s for udev device %s\n", debugstr_a(action
),
1444 debugstr_a(udev_device_get_devnode(dev
)));
1447 WARN("No action received\n");
1448 else if (strcmp(action
, "add") == 0)
1449 try_add_device(dev
);
1450 else if (strcmp(action
, "remove") == 0)
1451 try_remove_device(dev
);
1453 WARN("Unhandled action %s\n", debugstr_a(action
));
1455 udev_device_unref(dev
);
1458 static DWORD CALLBACK
deviceloop_thread(void *args
)
1460 struct udev_monitor
*monitor
;
1461 HANDLE init_done
= args
;
1462 struct pollfd pfd
[2];
1464 pfd
[1].fd
= deviceloop_control
[0];
1465 pfd
[1].events
= POLLIN
;
1468 monitor
= create_monitor(&pfd
[0]);
1469 build_initial_deviceset();
1470 SetEvent(init_done
);
1474 if (poll(pfd
, 2, -1) <= 0) continue;
1475 if (pfd
[1].revents
) break;
1476 process_monitor_event(monitor
);
1479 TRACE("Monitor thread exiting\n");
1481 udev_monitor_unref(monitor
);
1485 static int device_unload(DEVICE_OBJECT
*device
, void *context
)
1487 try_remove_device(impl_from_DEVICE_OBJECT(device
)->udev_device
);
1491 void udev_driver_unload( void )
1493 TRACE("Unload Driver\n");
1495 if (!deviceloop_handle
)
1498 write(deviceloop_control
[1], "q", 1);
1499 WaitForSingleObject(deviceloop_handle
, INFINITE
);
1500 close(deviceloop_control
[0]);
1501 close(deviceloop_control
[1]);
1502 CloseHandle(deviceloop_handle
);
1504 bus_enumerate_hid_devices(&hidraw_vtbl
, device_unload
, NULL
);
1505 #ifdef HAS_PROPER_INPUT_HEADER
1506 bus_enumerate_hid_devices(&lnxev_vtbl
, device_unload
, NULL
);
1510 NTSTATUS
udev_driver_init(void)
1514 static const WCHAR hidraw_disabledW
[] = {'D','i','s','a','b','l','e','H','i','d','r','a','w',0};
1515 static const UNICODE_STRING hidraw_disabled
= {sizeof(hidraw_disabledW
) - sizeof(WCHAR
), sizeof(hidraw_disabledW
), (WCHAR
*)hidraw_disabledW
};
1516 static const WCHAR input_disabledW
[] = {'D','i','s','a','b','l','e','I','n','p','u','t',0};
1517 static const UNICODE_STRING input_disabled
= {sizeof(input_disabledW
) - sizeof(WCHAR
), sizeof(input_disabledW
), (WCHAR
*)input_disabledW
};
1519 if (pipe(deviceloop_control
) != 0)
1521 ERR("Control pipe creation failed\n");
1522 return STATUS_UNSUCCESSFUL
;
1525 if (!(udev_context
= udev_new()))
1527 ERR("Can't create udev object\n");
1531 disable_hidraw
= check_bus_option(&hidraw_disabled
, 0);
1533 TRACE("UDEV hidraw devices disabled in registry\n");
1535 #ifdef HAS_PROPER_INPUT_HEADER
1536 disable_input
= check_bus_option(&input_disabled
, 0);
1538 TRACE("UDEV input devices disabled in registry\n");
1541 if (!(events
[0] = CreateEventW(NULL
, TRUE
, FALSE
, NULL
)))
1543 if (!(events
[1] = CreateThread(NULL
, 0, deviceloop_thread
, events
[0], 0, NULL
)))
1545 CloseHandle(events
[0]);
1549 result
= WaitForMultipleObjects(2, events
, FALSE
, INFINITE
);
1550 CloseHandle(events
[0]);
1551 if (result
== WAIT_OBJECT_0
)
1553 deviceloop_handle
= events
[1];
1554 TRACE("Initialization successful\n");
1555 return STATUS_SUCCESS
;
1557 CloseHandle(events
[1]);
1560 ERR("Failed to initialize udev device thread\n");
1561 close(deviceloop_control
[0]);
1562 close(deviceloop_control
[1]);
1565 udev_unref(udev_context
);
1566 udev_context
= NULL
;
1568 return STATUS_UNSUCCESSFUL
;
1573 NTSTATUS
udev_driver_init(void)
1575 return STATUS_NOT_IMPLEMENTED
;
1578 void udev_driver_unload( void )
1580 TRACE("Stub: Unload Driver\n");
1583 #endif /* HAVE_UDEV */