libport: Remove support for PPC32.
[wine.git] / dlls / winebus.sys / bus_udev.c
blob1473579c72c076636fdf4eab347e8c221c137eb4
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 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;
104 int device_fd;
106 HANDLE report_thread;
107 int control_pipe[2];
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*/
144 {0,0}, /*ABS_HAT0X*/
145 {0,0}, /*ABS_HAT0Y*/
146 {0,0}, /*ABS_HAT1X*/
147 {0,0}, /*ABS_HAT1Y*/
148 {0,0}, /*ABS_HAT2X*/
149 {0,0}, /*ABS_HAT2Y*/
150 {0,0}, /*ABS_HAT3X*/
151 {0,0}, /*ABS_HAT3Y*/
152 {HID_USAGE_PAGE_DIGITIZER, HID_USAGE_DIGITIZER_TIP_PRESSURE}, /*ABS_PRESSURE*/
153 {0, 0}, /*ABS_DISTANCE*/
154 {HID_USAGE_PAGE_DIGITIZER, HID_USAGE_DIGITIZER_X_TILT}, /*ABS_TILT_X*/
155 {HID_USAGE_PAGE_DIGITIZER, HID_USAGE_DIGITIZER_Y_TILT}, /*ABS_TILT_Y*/
156 {0, 0}, /*ABS_TOOL_WIDTH*/
157 {0, 0},
158 {0, 0},
159 {0, 0},
160 {HID_USAGE_PAGE_CONSUMER, HID_USAGE_CONSUMER_VOLUME} /*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;
183 BYTE report_index;
186 struct wine_input_private {
187 struct platform_private base;
189 int buffer_length;
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;
197 int button_start;
198 BYTE button_map[KEY_MAX];
199 BYTE rel_map[HID_REL_MAX];
200 BYTE hat_map[8];
201 int hat_values[8];
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)
209 int i;
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);
219 if (absolute)
221 memcpy(report_ptr, REPORT_ABS_AXIS_TAIL, sizeof(REPORT_ABS_AXIS_TAIL));
222 if (absinfo)
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);
232 else
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);
238 return report_ptr;
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, HID_USAGE_DIGITIZER_PEN};
249 static const BYTE Touchscreen[2] = {HID_USAGE_PAGE_DIGITIZER, HID_USAGE_DIGITIZER_TOUCH_SCREEN};
250 static const BYTE Touchpad[2] = {HID_USAGE_PAGE_DIGITIZER, HID_USAGE_DIGITIZER_TOUCH_PAD};
252 struct udev_device *parent = dev;
254 /* Look to the parents until we get a clue */
255 while (parent)
257 if (udev_device_get_property_value(parent, "ID_INPUT_MOUSE"))
258 return Mouse;
259 else if (udev_device_get_property_value(parent, "ID_INPUT_KEYBOARD"))
260 return Keyboard;
261 else if (udev_device_get_property_value(parent, "ID_INPUT_JOYSTICK"))
262 return Gamepad;
263 else if (udev_device_get_property_value(parent, "ID_INPUT_KEY"))
264 return Keypad;
265 else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHPAD"))
266 return Touchpad;
267 else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHSCREEN"))
268 return Touchscreen;
269 else if (udev_device_get_property_value(parent, "ID_INPUT_TABLET"))
270 return Tablet;
272 parent = udev_device_get_parent_with_subsystem_devtype(parent, "input", NULL);
274 return Unknown;
277 static void set_button_value(int index, int value, BYTE* buffer)
279 int bindex = index / 8;
280 int b = index % 8;
281 BYTE mask;
283 mask = 1<<b;
284 if (value)
285 buffer[bindex] = buffer[bindex] | mask;
286 else
288 mask = ~mask;
289 buffer[bindex] = buffer[bindex] & mask;
293 static void set_abs_axis_value(struct wine_input_private *ext, int code, int value)
295 int index;
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)
302 index--;
303 /* 8 1 2
304 * 7 0 3
305 * 6 5 4 */
306 if (ext->hat_values[index] == 0)
308 if (ext->hat_values[index+1] == 0)
309 value = 0;
310 else if (ext->hat_values[index+1] < 0)
311 value = 1;
312 else
313 value = 5;
315 else if (ext->hat_values[index] > 0)
317 if (ext->hat_values[index+1] == 0)
318 value = 3;
319 else if (ext->hat_values[index+1] < 0)
320 value = 2;
321 else
322 value = 4;
324 else
326 if (ext->hat_values[index+1] == 0)
327 value = 7;
328 else if (ext->hat_values[index+1] < 0)
329 value = 8;
330 else
331 value = 6;
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)
344 int index;
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)
356 int i;
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));
363 return FALSE;
366 for (i = BTN_MISC; i < KEY_MAX; i++)
368 if (test_bit(keybits, i))
370 if (map) map[i] = button_count;
371 button_count++;
374 return button_count;
377 static INT count_abs_axis(int device_fd)
379 BYTE absbits[(ABS_MAX+7)/8];
380 int abs_count = 0;
381 int i;
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));
386 return 0;
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))
393 abs_count++;
394 return abs_count;
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];
403 BYTE *report_ptr;
404 INT i, descript_size;
405 INT report_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));
412 return FALSE;
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));
417 return FALSE;
420 descript_size = sizeof(REPORT_HEADER) + sizeof(REPORT_TAIL);
421 report_size = 0;
423 abs_count = 0;
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)
437 int j;
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;
442 report_size+=4;
444 abs_count++;
446 descript_size += sizeof(REPORT_AXIS_HEADER) * abs_count;
447 descript_size += sizeof(REPORT_ABS_AXIS_TAIL) * abs_count;
449 rel_count = 0;
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)
461 int j;
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;
466 report_size++;
468 rel_count++;
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);
476 if (button_count)
478 descript_size += sizeof(REPORT_BUTTONS);
479 if (button_count % 8)
480 descript_size += sizeof(REPORT_PADDING);
481 report_size += (button_count + 7) / 8;
484 hat_count = 0;
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;
491 report_size++;
492 hat_count++;
494 if (hat_count > 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");
504 return FALSE;
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);
512 if (abs_count)
514 for (i = 1; i < TOP_ABS_PAGE; i++)
516 if (abs_pages[i][0])
518 BYTE usages[HID_ABS_MAX];
519 int j;
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]]);
526 if (rel_count)
528 for (i = 1; i < TOP_REL_PAGE; i++)
530 if (rel_pages[i][0])
532 BYTE usages[HID_REL_MAX];
533 int j;
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);
540 if (button_count)
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);
549 if (hat_count)
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);
561 return FALSE;
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);
569 return FALSE;
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);
578 return TRUE;
581 static BOOL set_report_from_event(struct wine_input_private *ext, struct input_event *ie)
583 switch(ie->type)
585 #ifdef EV_SYN
586 case EV_SYN:
587 switch (ie->code)
589 case SYN_REPORT:
590 if (ext->report_state == NORMAL)
592 memcpy(ext->last_report_buffer, ext->current_report_buffer, ext->buffer_length);
593 return TRUE;
595 else
597 if (ext->report_state == DROPPED)
598 memcpy(ext->current_report_buffer, ext->last_report_buffer, ext->buffer_length);
599 ext->report_state = NORMAL;
601 break;
602 case SYN_DROPPED:
603 TRACE_(hid_report)("received SY_DROPPED\n");
604 ext->report_state = DROPPED;
606 return FALSE;
607 #endif
608 #ifdef EV_MSC
609 case EV_MSC:
610 return FALSE;
611 #endif
612 case EV_KEY:
613 set_button_value(ext->button_start + ext->button_map[ie->code], ie->value, ext->current_report_buffer);
614 return FALSE;
615 case EV_ABS:
616 set_abs_axis_value(ext, ie->code, ie->value);
617 return FALSE;
618 case EV_REL:
619 set_rel_axis_value(ext, ie->code, ie->value);
620 return FALSE;
621 default:
622 ERR("TODO: Process Report (%i, %i)\n",ie->type, ie->code);
623 return FALSE;
626 #endif
628 static inline WCHAR *strdupAtoW(const char *src)
630 WCHAR *dst;
631 DWORD len;
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);
636 return dst;
639 static WCHAR *get_sysattr_string(struct udev_device *dev, const char *sysattr)
641 const char *attr = udev_device_get_sysattr_value(dev, sysattr);
642 if (!attr)
644 WARN("Could not get %s from device\n", sysattr);
645 return NULL;
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;
684 #else
685 return STATUS_NOT_IMPLEMENTED;
686 #endif
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);
693 WCHAR *str = NULL;
695 usbdev = udev_device_get_parent_with_subsystem_devtype(private->udev_device, "usb", "usb_device");
696 if (usbdev)
698 switch (index)
700 case HID_STRING_ID_IPRODUCT:
701 str = get_sysattr_string(usbdev, "product");
702 break;
703 case HID_STRING_ID_IMANUFACTURER:
704 str = get_sysattr_string(usbdev, "manufacturer");
705 break;
706 case HID_STRING_ID_ISERIALNUMBER:
707 str = get_sysattr_string(usbdev, "serial");
708 break;
709 default:
710 ERR("Unhandled string index %08x\n", index);
711 return STATUS_NOT_IMPLEMENTED;
714 else
716 #ifdef HAVE_LINUX_HIDRAW_H
717 switch (index)
719 case HID_STRING_ID_IPRODUCT:
721 char buf[MAX_PATH];
722 if (ioctl(private->device_fd, HIDIOCGRAWNAME(MAX_PATH), buf) == -1)
723 WARN("ioctl(HIDIOCGRAWNAME) failed: %d %s\n", errno, strerror(errno));
724 else
725 str = strdupAtoW(buf);
726 break;
728 case HID_STRING_ID_IMANUFACTURER:
729 break;
730 case HID_STRING_ID_ISERIALNUMBER:
731 break;
732 default:
733 ERR("Unhandled string index %08x\n", index);
734 return STATUS_NOT_IMPLEMENTED;
736 #else
737 return STATUS_NOT_IMPLEMENTED;
738 #endif
741 if (!str)
743 if (!length) return STATUS_BUFFER_TOO_SMALL;
744 buffer[0] = 0;
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;
772 while (1)
774 int size;
775 BYTE report_buffer[1024];
777 if (poll(plfds, 2, -1) <= 0) continue;
778 if (plfds[1].revents)
779 break;
780 size = read(plfds[0].fd, report_buffer, sizeof(report_buffer));
781 if (size == -1)
782 TRACE_(hid_report)("Read failed. Likely an unplugged device %d %s\n", errno, strerror(errno));
783 else if (size == 0)
784 TRACE_(hid_report)("Failed to read report\n");
785 else
786 process_hid_report(device, report_buffer, size);
788 return 0;
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;
812 else
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);
819 int rc;
821 if (id != 0)
822 rc = write(ext->device_fd, report, length);
823 else
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);
837 if (rc > 0)
839 *written = rc;
840 return STATUS_SUCCESS;
842 else
844 TRACE("write failed: %d %d %s\n", rc, errno, strerror(errno));
845 *written = 0;
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)
853 int rc;
854 struct platform_private* ext = impl_from_DEVICE_OBJECT(device);
855 report[0] = id;
856 length = min(length, 0x1fff);
857 rc = ioctl(ext->device_fd, HIDIOCGFEATURE(length), report);
858 if (rc >= 0)
860 *read = rc;
861 return STATUS_SUCCESS;
863 else
865 TRACE_(hid_report)("ioctl(HIDIOCGFEATURE(%d)) failed: %d %s\n", length, errno, strerror(errno));
866 *read = 0;
867 return STATUS_UNSUCCESSFUL;
869 #else
870 *read = 0;
871 return STATUS_NOT_IMPLEMENTED;
872 #endif
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)
878 int rc;
879 struct platform_private* ext = impl_from_DEVICE_OBJECT(device);
880 BYTE *feature_buffer;
881 BYTE buffer[8192];
883 if (id == 0)
885 if (length + 1 > sizeof(buffer))
887 ERR("Output feature buffer too small\n");
888 return STATUS_UNSUCCESSFUL;
890 buffer[0] = 0;
891 memcpy(&buffer[1], report, length);
892 feature_buffer = buffer;
893 length = length + 1;
895 else
896 feature_buffer = report;
897 length = min(length, 0x1fff);
898 rc = ioctl(ext->device_fd, HIDIOCSFEATURE(length), feature_buffer);
899 if (rc >= 0)
901 *written = rc;
902 return STATUS_SUCCESS;
904 else
906 TRACE_(hid_report)("ioctl(HIDIOCSFEATURE(%d)) failed: %d %s\n", length, errno, strerror(errno));
907 *written = 0;
908 return STATUS_UNSUCCESSFUL;
910 #else
911 *written = 0;
912 return STATUS_NOT_IMPLEMENTED;
913 #endif
916 static const platform_vtbl hidraw_vtbl =
918 compare_platform_device,
919 hidraw_get_reportdescriptor,
920 hidraw_get_string,
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);
951 char str[255];
953 str[0] = 0;
954 switch (index)
956 case HID_STRING_ID_IPRODUCT:
957 ioctl(ext->base.device_fd, EVIOCGNAME(sizeof(str)), str);
958 break;
959 case HID_STRING_ID_IMANUFACTURER:
960 strcpy(str,"evdev");
961 break;
962 case HID_STRING_ID_ISERIALNUMBER:
963 ioctl(ext->base.device_fd, EVIOCGUNIQ(sizeof(str)), str);
964 break;
965 default:
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;
986 while (1)
988 int size;
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)
993 break;
994 size = read(plfds[0].fd, &ie, sizeof(ie));
995 if (size == -1)
996 TRACE_(hid_report)("Read failed. Likely an unplugged device\n");
997 else if (size == 0)
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);
1002 return 0;
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)
1031 *written = 0;
1032 return STATUS_NOT_IMPLEMENTED;
1035 static NTSTATUS lnxev_get_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *read)
1037 *read = 0;
1038 return STATUS_NOT_IMPLEMENTED;
1041 static NTSTATUS lnxev_set_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *written)
1043 *written = 0;
1044 return STATUS_NOT_IMPLEMENTED;
1047 static const platform_vtbl lnxev_vtbl = {
1048 compare_platform_device,
1049 lnxev_get_reportdescriptor,
1050 lnxev_get_string,
1051 lnxev_begin_report_processing,
1052 lnxev_set_output_report,
1053 lnxev_get_feature_report,
1054 lnxev_set_feature_report,
1056 #endif
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)
1066 DWORD bus_type;
1067 char *tmp;
1068 char *saveptr = NULL;
1069 char *line;
1070 char *key;
1071 char *value;
1073 int found_id = 0;
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" */
1082 key = line;
1083 value = strchr(line, '=');
1084 if (!value)
1086 goto next_line;
1088 *value = '\0';
1089 value++;
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);
1098 if (ret == 3)
1099 found_id = 1;
1101 else if (strcmp(key, "HID_UNIQ") == 0)
1103 /* The caller has to free the serial number */
1104 if (*value)
1106 *serial_number = strdupAtoW(value);
1107 found_serial = 1;
1110 else if (strcmp(key, "HID_PHYS") == 0)
1112 const char *input_no = strstr(value, "input");
1113 if (input_no)
1114 *input = atoi(input_no+5 );
1117 next_line:
1118 line = strtok_r(NULL, "\n", &saveptr);
1121 heap_free(tmp);
1122 return (found_id && found_serial);
1125 static DWORD a_to_bcd(const char *s)
1127 DWORD r = 0;
1128 const char *c;
1129 int shift = strlen(s) - 1;
1130 for (c = s; *c; ++c)
1132 r |= (*c - '0') << (shift * 4);
1133 --shift;
1135 return r;
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;
1147 WORD input = -1;
1148 int fd;
1149 static const CHAR *base_serial = "0000";
1151 if (!(devnode = udev_device_get_devnode(dev)))
1152 return;
1154 if ((fd = open(devnode, O_RDWR)) == -1)
1156 WARN("Unable to open udev device %s: %s\n", debugstr_a(devnode), strerror(errno));
1157 return;
1160 subsystem = udev_device_get_subsystem(dev);
1161 hiddev = udev_device_get_parent_with_subsystem_devtype(dev, "hid", NULL);
1162 if (hiddev)
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;
1173 if (other_vtbl)
1174 dup = bus_enumerate_hid_devices(other_vtbl, check_same_device, dev);
1175 if (dup)
1177 TRACE("Duplicate cross bus device (%p) found, not adding the new one\n", dup);
1178 close(fd);
1179 return;
1181 #endif
1182 parse_uevent_info(udev_device_get_sysattr_value(hiddev, "uevent"),
1183 &vid, &pid, &input, &serial);
1184 if (serial == NULL)
1185 serial = strdupAtoW(base_serial);
1187 walk_device = dev;
1188 while (walk_device && !bcdDevice)
1190 bcdDevice = udev_device_get_sysattr_value(walk_device, "bcdDevice");
1191 walk_device = udev_device_get_parent(walk_device);
1193 if (bcdDevice)
1195 version = a_to_bcd(bcdDevice);
1198 #ifdef HAS_PROPER_INPUT_HEADER
1199 else
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));
1206 device_uid[0] = 0;
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;
1214 #else
1215 else
1216 WARN("Could not get device to query VID, PID, Version and Serial\n");
1217 #endif
1219 if (is_xbox_gamepad(vid, pid))
1220 is_gamepad = TRUE;
1221 #ifdef HAS_PROPER_INPUT_HEADER
1222 else
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);
1229 #endif
1230 if (input == (WORD)-1 && is_gamepad)
1231 input = 0;
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));
1248 #endif
1250 if (device)
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");
1260 close(fd);
1261 udev_device_unref(dev);
1262 bus_unlink_hid_device(device);
1263 bus_remove_hid_device(device);
1264 HeapFree(GetProcessHeap(), 0, serial);
1265 return;
1267 #endif
1268 IoInvalidateDeviceRelations(bus_pdo, BusRelations);
1270 else
1272 WARN("Ignoring device %s with subsystem %s\n", debugstr_a(devnode), subsystem);
1273 close(fd);
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;
1285 #endif
1287 device = bus_find_hid_device(&hidraw_vtbl, dev);
1288 #ifdef HAS_PROPER_INPUT_HEADER
1289 if (device == NULL)
1291 device = bus_find_hid_device(&lnxev_vtbl, dev);
1292 is_input = TRUE;
1294 #endif
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);
1315 #endif
1318 #ifdef HAS_PROPER_INPUT_HEADER
1319 if (is_input)
1321 struct wine_input_private *ext = (struct wine_input_private*)private;
1322 HeapFree(GetProcessHeap(), 0, ext->report_descriptor);
1324 #endif
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);
1338 if (!enumerate)
1340 WARN("Unable to create udev enumeration object\n");
1341 return;
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
1348 if (!disable_input)
1350 if (udev_enumerate_add_match_subsystem(enumerate, "input") < 0)
1351 WARN("Failed to add subsystem 'input' to enumeration\n");
1353 #endif
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;
1362 const char *path;
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;
1378 int systems = 0;
1380 monitor = udev_monitor_new_from_netlink(udev_context, "udev");
1381 if (!monitor)
1383 WARN("Unable to get udev monitor object\n");
1384 return NULL;
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");
1391 else
1392 systems++;
1394 #ifdef HAS_PROPER_INPUT_HEADER
1395 if (!disable_input)
1397 if (udev_monitor_filter_add_match_subsystem_devtype(monitor, "input", NULL) < 0)
1398 WARN("Failed to add 'input' subsystem to monitor\n");
1399 else
1400 systems++;
1402 #endif
1403 if (systems == 0)
1405 WARN("No subsystems added to monitor\n");
1406 goto error;
1409 if (udev_monitor_enable_receiving(monitor) < 0)
1410 goto error;
1412 if ((pfd->fd = udev_monitor_get_fd(monitor)) >= 0)
1414 pfd->events = POLLIN;
1415 return monitor;
1418 error:
1419 WARN("Failed to start monitoring\n");
1420 udev_monitor_unref(monitor);
1421 return NULL;
1424 static void process_monitor_event(struct udev_monitor *monitor)
1426 struct udev_device *dev;
1427 const char *action;
1429 dev = udev_monitor_receive_device(monitor);
1430 if (!dev)
1432 FIXME("Failed to get device that has changed\n");
1433 return;
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)));
1440 if (!action)
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);
1446 else
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;
1460 pfd[1].revents = 0;
1462 monitor = create_monitor(&pfd[0]);
1463 build_initial_deviceset();
1464 SetEvent(init_done);
1466 while (monitor)
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");
1474 if (monitor)
1475 udev_monitor_unref(monitor);
1476 return 0;
1479 static int device_unload(DEVICE_OBJECT *device, void *context)
1481 try_remove_device(impl_from_DEVICE_OBJECT(device)->udev_device);
1482 return 1;
1485 void udev_driver_unload( void )
1487 TRACE("Unload Driver\n");
1489 if (!deviceloop_handle)
1490 return;
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);
1501 #endif
1504 NTSTATUS udev_driver_init(void)
1506 HANDLE events[2];
1507 DWORD result;
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");
1522 goto error;
1525 disable_hidraw = check_bus_option(&hidraw_disabled, 0);
1526 if (disable_hidraw)
1527 TRACE("UDEV hidraw devices disabled in registry\n");
1529 #ifdef HAS_PROPER_INPUT_HEADER
1530 disable_input = check_bus_option(&input_disabled, 0);
1531 if (disable_input)
1532 TRACE("UDEV input devices disabled in registry\n");
1533 #endif
1535 if (!(events[0] = CreateEventW(NULL, TRUE, FALSE, NULL)))
1536 goto error;
1537 if (!(events[1] = CreateThread(NULL, 0, deviceloop_thread, events[0], 0, NULL)))
1539 CloseHandle(events[0]);
1540 goto error;
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]);
1553 error:
1554 ERR("Failed to initialize udev device thread\n");
1555 close(deviceloop_control[0]);
1556 close(deviceloop_control[1]);
1557 if (udev_context)
1559 udev_unref(udev_context);
1560 udev_context = NULL;
1562 return STATUS_UNSUCCESSFUL;
1565 #else
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 */