mfplay: Implement GetCharacteristics().
[wine/zf.git] / dlls / winebus.sys / bus_udev.c
blob74cf6a28a1a3abb76b60369cbb6286912ba17720
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},
145 {0,0},
146 {0,0},
147 {0,0},
148 {0,0},
149 {0,0}, /*ABS_HAT0X*/
150 {0,0}, /*ABS_HAT0Y*/
151 {0,0}, /*ABS_HAT1X*/
152 {0,0}, /*ABS_HAT1Y*/
153 {0,0}, /*ABS_HAT2X*/
154 {0,0}, /*ABS_HAT2Y*/
155 {0,0}, /*ABS_HAT3X*/
156 {0,0}, /*ABS_HAT3Y*/
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*/
162 {0, 0},
163 {0, 0},
164 {0, 0},
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;
189 BYTE report_index;
192 struct wine_input_private {
193 struct platform_private base;
195 int buffer_length;
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;
203 int button_start;
204 BYTE button_map[KEY_MAX];
205 BYTE rel_map[HID_REL_MAX];
206 BYTE hat_map[8];
207 int hat_values[8];
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)
215 int i;
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);
225 if (absolute)
227 memcpy(report_ptr, REPORT_ABS_AXIS_TAIL, sizeof(REPORT_ABS_AXIS_TAIL));
228 if (absinfo)
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);
238 else
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);
244 return report_ptr;
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 */
261 while (parent)
263 if (udev_device_get_property_value(parent, "ID_INPUT_MOUSE"))
264 return Mouse;
265 else if (udev_device_get_property_value(parent, "ID_INPUT_KEYBOARD"))
266 return Keyboard;
267 else if (udev_device_get_property_value(parent, "ID_INPUT_JOYSTICK"))
268 return Gamepad;
269 else if (udev_device_get_property_value(parent, "ID_INPUT_KEY"))
270 return Keypad;
271 else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHPAD"))
272 return Touchpad;
273 else if (udev_device_get_property_value(parent, "ID_INPUT_TOUCHSCREEN"))
274 return Touchscreen;
275 else if (udev_device_get_property_value(parent, "ID_INPUT_TABLET"))
276 return Tablet;
278 parent = udev_device_get_parent_with_subsystem_devtype(parent, "input", NULL);
280 return Unknown;
283 static void set_button_value(int index, int value, BYTE* buffer)
285 int bindex = index / 8;
286 int b = index % 8;
287 BYTE mask;
289 mask = 1<<b;
290 if (value)
291 buffer[bindex] = buffer[bindex] | mask;
292 else
294 mask = ~mask;
295 buffer[bindex] = buffer[bindex] & mask;
299 static void set_abs_axis_value(struct wine_input_private *ext, int code, int value)
301 int index;
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)
308 index--;
309 /* 8 1 2
310 * 7 0 3
311 * 6 5 4 */
312 if (ext->hat_values[index] == 0)
314 if (ext->hat_values[index+1] == 0)
315 value = 0;
316 else if (ext->hat_values[index+1] < 0)
317 value = 1;
318 else
319 value = 5;
321 else if (ext->hat_values[index] > 0)
323 if (ext->hat_values[index+1] == 0)
324 value = 3;
325 else if (ext->hat_values[index+1] < 0)
326 value = 2;
327 else
328 value = 4;
330 else
332 if (ext->hat_values[index+1] == 0)
333 value = 7;
334 else if (ext->hat_values[index+1] < 0)
335 value = 8;
336 else
337 value = 6;
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)
350 int index;
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)
362 int i;
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));
369 return FALSE;
372 for (i = BTN_MISC; i < KEY_MAX; i++)
374 if (test_bit(keybits, i))
376 if (map) map[i] = button_count;
377 button_count++;
380 return button_count;
383 static INT count_abs_axis(int device_fd)
385 BYTE absbits[(ABS_MAX+7)/8];
386 int abs_count = 0;
387 int i;
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));
392 return 0;
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))
399 abs_count++;
400 return abs_count;
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];
409 BYTE *report_ptr;
410 INT i, descript_size;
411 INT report_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));
418 return FALSE;
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));
423 return FALSE;
426 descript_size = sizeof(REPORT_HEADER) + sizeof(REPORT_TAIL);
427 report_size = 0;
429 abs_count = 0;
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)
443 int j;
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;
448 report_size+=4;
450 abs_count++;
452 descript_size += sizeof(REPORT_AXIS_HEADER) * abs_count;
453 descript_size += sizeof(REPORT_ABS_AXIS_TAIL) * abs_count;
455 rel_count = 0;
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)
467 int j;
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;
472 report_size++;
474 rel_count++;
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);
482 if (button_count)
484 descript_size += sizeof(REPORT_BUTTONS);
485 if (button_count % 8)
486 descript_size += sizeof(REPORT_PADDING);
487 report_size += (button_count + 7) / 8;
490 hat_count = 0;
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;
497 report_size++;
498 hat_count++;
500 if (hat_count > 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");
510 return FALSE;
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);
518 if (abs_count)
520 for (i = 1; i < TOP_ABS_PAGE; i++)
522 if (abs_pages[i][0])
524 BYTE usages[HID_ABS_MAX];
525 int j;
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]]);
532 if (rel_count)
534 for (i = 1; i < TOP_REL_PAGE; i++)
536 if (rel_pages[i][0])
538 BYTE usages[HID_REL_MAX];
539 int j;
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);
546 if (button_count)
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);
555 if (hat_count)
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);
567 return FALSE;
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);
575 return FALSE;
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);
584 return TRUE;
587 static BOOL set_report_from_event(struct wine_input_private *ext, struct input_event *ie)
589 switch(ie->type)
591 #ifdef EV_SYN
592 case EV_SYN:
593 switch (ie->code)
595 case SYN_REPORT:
596 if (ext->report_state == NORMAL)
598 memcpy(ext->last_report_buffer, ext->current_report_buffer, ext->buffer_length);
599 return TRUE;
601 else
603 if (ext->report_state == DROPPED)
604 memcpy(ext->current_report_buffer, ext->last_report_buffer, ext->buffer_length);
605 ext->report_state = NORMAL;
607 break;
608 case SYN_DROPPED:
609 TRACE_(hid_report)("received SY_DROPPED\n");
610 ext->report_state = DROPPED;
612 return FALSE;
613 #endif
614 #ifdef EV_MSC
615 case EV_MSC:
616 return FALSE;
617 #endif
618 case EV_KEY:
619 set_button_value(ext->button_start + ext->button_map[ie->code], ie->value, ext->current_report_buffer);
620 return FALSE;
621 case EV_ABS:
622 set_abs_axis_value(ext, ie->code, ie->value);
623 return FALSE;
624 case EV_REL:
625 set_rel_axis_value(ext, ie->code, ie->value);
626 return FALSE;
627 default:
628 ERR("TODO: Process Report (%i, %i)\n",ie->type, ie->code);
629 return FALSE;
632 #endif
634 static inline WCHAR *strdupAtoW(const char *src)
636 WCHAR *dst;
637 DWORD len;
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);
642 return dst;
645 static WCHAR *get_sysattr_string(struct udev_device *dev, const char *sysattr)
647 const char *attr = udev_device_get_sysattr_value(dev, sysattr);
648 if (!attr)
650 WARN("Could not get %s from device\n", sysattr);
651 return NULL;
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;
690 #else
691 return STATUS_NOT_IMPLEMENTED;
692 #endif
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);
699 WCHAR *str = NULL;
701 usbdev = udev_device_get_parent_with_subsystem_devtype(private->udev_device, "usb", "usb_device");
702 if (usbdev)
704 switch (index)
706 case HID_STRING_ID_IPRODUCT:
707 str = get_sysattr_string(usbdev, "product");
708 break;
709 case HID_STRING_ID_IMANUFACTURER:
710 str = get_sysattr_string(usbdev, "manufacturer");
711 break;
712 case HID_STRING_ID_ISERIALNUMBER:
713 str = get_sysattr_string(usbdev, "serial");
714 break;
715 default:
716 ERR("Unhandled string index %08x\n", index);
717 return STATUS_NOT_IMPLEMENTED;
720 else
722 #ifdef HAVE_LINUX_HIDRAW_H
723 switch (index)
725 case HID_STRING_ID_IPRODUCT:
727 char buf[MAX_PATH];
728 if (ioctl(private->device_fd, HIDIOCGRAWNAME(MAX_PATH), buf) == -1)
729 WARN("ioctl(HIDIOCGRAWNAME) failed: %d %s\n", errno, strerror(errno));
730 else
731 str = strdupAtoW(buf);
732 break;
734 case HID_STRING_ID_IMANUFACTURER:
735 break;
736 case HID_STRING_ID_ISERIALNUMBER:
737 break;
738 default:
739 ERR("Unhandled string index %08x\n", index);
740 return STATUS_NOT_IMPLEMENTED;
742 #else
743 return STATUS_NOT_IMPLEMENTED;
744 #endif
747 if (!str)
749 if (!length) return STATUS_BUFFER_TOO_SMALL;
750 buffer[0] = 0;
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;
778 while (1)
780 int size;
781 BYTE report_buffer[1024];
783 if (poll(plfds, 2, -1) <= 0) continue;
784 if (plfds[1].revents)
785 break;
786 size = read(plfds[0].fd, report_buffer, sizeof(report_buffer));
787 if (size == -1)
788 TRACE_(hid_report)("Read failed. Likely an unplugged device %d %s\n", errno, strerror(errno));
789 else if (size == 0)
790 TRACE_(hid_report)("Failed to read report\n");
791 else
792 process_hid_report(device, report_buffer, size);
794 return 0;
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;
818 else
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);
825 int rc;
827 if (id != 0)
828 rc = write(ext->device_fd, report, length);
829 else
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);
843 if (rc > 0)
845 *written = rc;
846 return STATUS_SUCCESS;
848 else
850 TRACE("write failed: %d %d %s\n", rc, errno, strerror(errno));
851 *written = 0;
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)
859 int rc;
860 struct platform_private* ext = impl_from_DEVICE_OBJECT(device);
861 report[0] = id;
862 length = min(length, 0x1fff);
863 rc = ioctl(ext->device_fd, HIDIOCGFEATURE(length), report);
864 if (rc >= 0)
866 *read = rc;
867 return STATUS_SUCCESS;
869 else
871 TRACE_(hid_report)("ioctl(HIDIOCGFEATURE(%d)) failed: %d %s\n", length, errno, strerror(errno));
872 *read = 0;
873 return STATUS_UNSUCCESSFUL;
875 #else
876 *read = 0;
877 return STATUS_NOT_IMPLEMENTED;
878 #endif
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)
884 int rc;
885 struct platform_private* ext = impl_from_DEVICE_OBJECT(device);
886 BYTE *feature_buffer;
887 BYTE buffer[8192];
889 if (id == 0)
891 if (length + 1 > sizeof(buffer))
893 ERR("Output feature buffer too small\n");
894 return STATUS_UNSUCCESSFUL;
896 buffer[0] = 0;
897 memcpy(&buffer[1], report, length);
898 feature_buffer = buffer;
899 length = length + 1;
901 else
902 feature_buffer = report;
903 length = min(length, 0x1fff);
904 rc = ioctl(ext->device_fd, HIDIOCSFEATURE(length), feature_buffer);
905 if (rc >= 0)
907 *written = rc;
908 return STATUS_SUCCESS;
910 else
912 TRACE_(hid_report)("ioctl(HIDIOCSFEATURE(%d)) failed: %d %s\n", length, errno, strerror(errno));
913 *written = 0;
914 return STATUS_UNSUCCESSFUL;
916 #else
917 *written = 0;
918 return STATUS_NOT_IMPLEMENTED;
919 #endif
922 static const platform_vtbl hidraw_vtbl =
924 compare_platform_device,
925 hidraw_get_reportdescriptor,
926 hidraw_get_string,
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);
957 char str[255];
959 str[0] = 0;
960 switch (index)
962 case HID_STRING_ID_IPRODUCT:
963 ioctl(ext->base.device_fd, EVIOCGNAME(sizeof(str)), str);
964 break;
965 case HID_STRING_ID_IMANUFACTURER:
966 strcpy(str,"evdev");
967 break;
968 case HID_STRING_ID_ISERIALNUMBER:
969 ioctl(ext->base.device_fd, EVIOCGUNIQ(sizeof(str)), str);
970 break;
971 default:
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;
992 while (1)
994 int size;
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)
999 break;
1000 size = read(plfds[0].fd, &ie, sizeof(ie));
1001 if (size == -1)
1002 TRACE_(hid_report)("Read failed. Likely an unplugged device\n");
1003 else if (size == 0)
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);
1008 return 0;
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)
1037 *written = 0;
1038 return STATUS_NOT_IMPLEMENTED;
1041 static NTSTATUS lnxev_get_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *read)
1043 *read = 0;
1044 return STATUS_NOT_IMPLEMENTED;
1047 static NTSTATUS lnxev_set_feature_report(DEVICE_OBJECT *device, UCHAR id, BYTE *report, DWORD length, ULONG_PTR *written)
1049 *written = 0;
1050 return STATUS_NOT_IMPLEMENTED;
1053 static const platform_vtbl lnxev_vtbl = {
1054 compare_platform_device,
1055 lnxev_get_reportdescriptor,
1056 lnxev_get_string,
1057 lnxev_begin_report_processing,
1058 lnxev_set_output_report,
1059 lnxev_get_feature_report,
1060 lnxev_set_feature_report,
1062 #endif
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)
1072 DWORD bus_type;
1073 char *tmp;
1074 char *saveptr = NULL;
1075 char *line;
1076 char *key;
1077 char *value;
1079 int found_id = 0;
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" */
1088 key = line;
1089 value = strchr(line, '=');
1090 if (!value)
1092 goto next_line;
1094 *value = '\0';
1095 value++;
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);
1104 if (ret == 3)
1105 found_id = 1;
1107 else if (strcmp(key, "HID_UNIQ") == 0)
1109 /* The caller has to free the serial number */
1110 if (*value)
1112 *serial_number = strdupAtoW(value);
1113 found_serial = 1;
1116 else if (strcmp(key, "HID_PHYS") == 0)
1118 const char *input_no = strstr(value, "input");
1119 if (input_no)
1120 *input = atoi(input_no+5 );
1123 next_line:
1124 line = strtok_r(NULL, "\n", &saveptr);
1127 heap_free(tmp);
1128 return (found_id && found_serial);
1131 static DWORD a_to_bcd(const char *s)
1133 DWORD r = 0;
1134 const char *c;
1135 int shift = strlen(s) - 1;
1136 for (c = s; *c; ++c)
1138 r |= (*c - '0') << (shift * 4);
1139 --shift;
1141 return r;
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;
1153 WORD input = -1;
1154 int fd;
1155 static const CHAR *base_serial = "0000";
1157 if (!(devnode = udev_device_get_devnode(dev)))
1158 return;
1160 if ((fd = open(devnode, O_RDWR)) == -1)
1162 WARN("Unable to open udev device %s: %s\n", debugstr_a(devnode), strerror(errno));
1163 return;
1166 subsystem = udev_device_get_subsystem(dev);
1167 hiddev = udev_device_get_parent_with_subsystem_devtype(dev, "hid", NULL);
1168 if (hiddev)
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;
1179 if (other_vtbl)
1180 dup = bus_enumerate_hid_devices(other_vtbl, check_same_device, dev);
1181 if (dup)
1183 TRACE("Duplicate cross bus device (%p) found, not adding the new one\n", dup);
1184 close(fd);
1185 return;
1187 #endif
1188 parse_uevent_info(udev_device_get_sysattr_value(hiddev, "uevent"),
1189 &vid, &pid, &input, &serial);
1190 if (serial == NULL)
1191 serial = strdupAtoW(base_serial);
1193 walk_device = dev;
1194 while (walk_device && !bcdDevice)
1196 bcdDevice = udev_device_get_sysattr_value(walk_device, "bcdDevice");
1197 walk_device = udev_device_get_parent(walk_device);
1199 if (bcdDevice)
1201 version = a_to_bcd(bcdDevice);
1204 #ifdef HAS_PROPER_INPUT_HEADER
1205 else
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));
1212 device_uid[0] = 0;
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;
1220 #else
1221 else
1222 WARN("Could not get device to query VID, PID, Version and Serial\n");
1223 #endif
1225 if (is_xbox_gamepad(vid, pid))
1226 is_gamepad = TRUE;
1227 #ifdef HAS_PROPER_INPUT_HEADER
1228 else
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);
1235 #endif
1236 if (input == (WORD)-1 && is_gamepad)
1237 input = 0;
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));
1254 #endif
1256 if (device)
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");
1266 close(fd);
1267 udev_device_unref(dev);
1268 bus_unlink_hid_device(device);
1269 bus_remove_hid_device(device);
1270 HeapFree(GetProcessHeap(), 0, serial);
1271 return;
1273 #endif
1274 IoInvalidateDeviceRelations(bus_pdo, BusRelations);
1276 else
1278 WARN("Ignoring device %s with subsystem %s\n", debugstr_a(devnode), subsystem);
1279 close(fd);
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;
1291 #endif
1293 device = bus_find_hid_device(&hidraw_vtbl, dev);
1294 #ifdef HAS_PROPER_INPUT_HEADER
1295 if (device == NULL)
1297 device = bus_find_hid_device(&lnxev_vtbl, dev);
1298 is_input = TRUE;
1300 #endif
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);
1321 #endif
1324 #ifdef HAS_PROPER_INPUT_HEADER
1325 if (is_input)
1327 struct wine_input_private *ext = (struct wine_input_private*)private;
1328 HeapFree(GetProcessHeap(), 0, ext->report_descriptor);
1330 #endif
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);
1344 if (!enumerate)
1346 WARN("Unable to create udev enumeration object\n");
1347 return;
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
1354 if (!disable_input)
1356 if (udev_enumerate_add_match_subsystem(enumerate, "input") < 0)
1357 WARN("Failed to add subsystem 'input' to enumeration\n");
1359 #endif
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;
1368 const char *path;
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;
1384 int systems = 0;
1386 monitor = udev_monitor_new_from_netlink(udev_context, "udev");
1387 if (!monitor)
1389 WARN("Unable to get udev monitor object\n");
1390 return NULL;
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");
1397 else
1398 systems++;
1400 #ifdef HAS_PROPER_INPUT_HEADER
1401 if (!disable_input)
1403 if (udev_monitor_filter_add_match_subsystem_devtype(monitor, "input", NULL) < 0)
1404 WARN("Failed to add 'input' subsystem to monitor\n");
1405 else
1406 systems++;
1408 #endif
1409 if (systems == 0)
1411 WARN("No subsystems added to monitor\n");
1412 goto error;
1415 if (udev_monitor_enable_receiving(monitor) < 0)
1416 goto error;
1418 if ((pfd->fd = udev_monitor_get_fd(monitor)) >= 0)
1420 pfd->events = POLLIN;
1421 return monitor;
1424 error:
1425 WARN("Failed to start monitoring\n");
1426 udev_monitor_unref(monitor);
1427 return NULL;
1430 static void process_monitor_event(struct udev_monitor *monitor)
1432 struct udev_device *dev;
1433 const char *action;
1435 dev = udev_monitor_receive_device(monitor);
1436 if (!dev)
1438 FIXME("Failed to get device that has changed\n");
1439 return;
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)));
1446 if (!action)
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);
1452 else
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;
1466 pfd[1].revents = 0;
1468 monitor = create_monitor(&pfd[0]);
1469 build_initial_deviceset();
1470 SetEvent(init_done);
1472 while (monitor)
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");
1480 if (monitor)
1481 udev_monitor_unref(monitor);
1482 return 0;
1485 static int device_unload(DEVICE_OBJECT *device, void *context)
1487 try_remove_device(impl_from_DEVICE_OBJECT(device)->udev_device);
1488 return 1;
1491 void udev_driver_unload( void )
1493 TRACE("Unload Driver\n");
1495 if (!deviceloop_handle)
1496 return;
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);
1507 #endif
1510 NTSTATUS udev_driver_init(void)
1512 HANDLE events[2];
1513 DWORD result;
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");
1528 goto error;
1531 disable_hidraw = check_bus_option(&hidraw_disabled, 0);
1532 if (disable_hidraw)
1533 TRACE("UDEV hidraw devices disabled in registry\n");
1535 #ifdef HAS_PROPER_INPUT_HEADER
1536 disable_input = check_bus_option(&input_disabled, 0);
1537 if (disable_input)
1538 TRACE("UDEV input devices disabled in registry\n");
1539 #endif
1541 if (!(events[0] = CreateEventW(NULL, TRUE, FALSE, NULL)))
1542 goto error;
1543 if (!(events[1] = CreateThread(NULL, 0, deviceloop_thread, events[0], 0, NULL)))
1545 CloseHandle(events[0]);
1546 goto error;
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]);
1559 error:
1560 ERR("Failed to initialize udev device thread\n");
1561 close(deviceloop_control[0]);
1562 close(deviceloop_control[1]);
1563 if (udev_context)
1565 udev_unref(udev_context);
1566 udev_context = NULL;
1568 return STATUS_UNSUCCESSFUL;
1571 #else
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 */