1 /* Bus like function for mac HID devices
3 * Copyright 2016 CodeWeavers, Aric Stewart
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/port.h"
25 #if defined(HAVE_IOKIT_HID_IOHIDLIB_H)
27 #define LPDWORD UInt32*
29 #define LPLONG SInt32*
30 #define E_PENDING __carbon_E_PENDING
31 #define ULONG __carbon_ULONG
32 #define E_INVALIDARG __carbon_E_INVALIDARG
33 #define E_OUTOFMEMORY __carbon_E_OUTOFMEMORY
34 #define E_HANDLE __carbon_E_HANDLE
35 #define E_ACCESSDENIED __carbon_E_ACCESSDENIED
36 #define E_UNEXPECTED __carbon_E_UNEXPECTED
37 #define E_FAIL __carbon_E_FAIL
38 #define E_ABORT __carbon_E_ABORT
39 #define E_POINTER __carbon_E_POINTER
40 #define E_NOINTERFACE __carbon_E_NOINTERFACE
41 #define E_NOTIMPL __carbon_E_NOTIMPL
42 #define S_FALSE __carbon_S_FALSE
43 #define S_OK __carbon_S_OK
44 #define HRESULT_FACILITY __carbon_HRESULT_FACILITY
45 #define IS_ERROR __carbon_IS_ERROR
46 #define FAILED __carbon_FAILED
47 #define SUCCEEDED __carbon_SUCCEEDED
48 #define MAKE_HRESULT __carbon_MAKE_HRESULT
49 #define HRESULT __carbon_HRESULT
50 #define STDMETHODCALLTYPE __carbon_STDMETHODCALLTYPE
51 #define PAGE_SHIFT __carbon_PAGE_SHIFT
52 #include <IOKit/IOKitLib.h>
53 #include <IOKit/hid/IOHIDLib.h>
67 #undef HRESULT_FACILITY
73 #undef STDMETHODCALLTYPE
80 #endif /* HAVE_IOKIT_HID_IOHIDLIB_H */
82 #define NONAMELESSUNION
85 #define WIN32_NO_STATUS
91 #include "ddk/hidtypes.h"
92 #include "wine/debug.h"
96 WINE_DEFAULT_DEBUG_CHANNEL(plugplay
);
97 #ifdef HAVE_IOHIDMANAGERCREATE
99 static IOHIDManagerRef hid_manager
;
100 static CFRunLoopRef run_loop
;
101 static HANDLE run_loop_handle
;
103 static const WCHAR busidW
[] = {'I','O','H','I','D',0};
105 struct platform_private
107 IOHIDDeviceRef device
;
111 static inline struct platform_private
*impl_from_DEVICE_OBJECT(DEVICE_OBJECT
*device
)
113 return (struct platform_private
*)get_platform_private(device
);
116 static void CFStringToWSTR(CFStringRef cstr
, LPWSTR wstr
, int length
)
118 int len
= min(CFStringGetLength(cstr
), length
-1);
119 CFStringGetCharacters(cstr
, CFRangeMake(0, len
), (UniChar
*)wstr
);
123 static DWORD
CFNumberToDWORD(CFNumberRef num
)
127 CFNumberGetValue(num
, kCFNumberIntType
, &dwNum
);
131 static void handle_IOHIDDeviceIOHIDReportCallback(void *context
,
132 IOReturn result
, void *sender
, IOHIDReportType type
,
133 uint32_t reportID
, uint8_t *report
, CFIndex report_length
)
135 DEVICE_OBJECT
*device
= (DEVICE_OBJECT
*)context
;
136 process_hid_report(device
, report
, report_length
);
139 static int compare_platform_device(DEVICE_OBJECT
*device
, void *platform_dev
)
141 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
142 IOHIDDeviceRef dev2
= (IOHIDDeviceRef
)platform_dev
;
143 if (private->device
!= dev2
)
149 static NTSTATUS
get_reportdescriptor(DEVICE_OBJECT
*device
, BYTE
*buffer
, DWORD length
, DWORD
*out_length
)
151 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
152 CFDataRef data
= IOHIDDeviceGetProperty(private->device
, CFSTR(kIOHIDReportDescriptorKey
));
153 int data_length
= CFDataGetLength(data
);
156 *out_length
= data_length
;
157 if (length
< data_length
)
158 return STATUS_BUFFER_TOO_SMALL
;
160 ptr
= CFDataGetBytePtr(data
);
161 memcpy(buffer
, ptr
, data_length
);
162 return STATUS_SUCCESS
;
165 static NTSTATUS
get_string(DEVICE_OBJECT
*device
, DWORD index
, WCHAR
*buffer
, DWORD length
)
167 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
171 case HID_STRING_ID_IPRODUCT
:
172 str
= IOHIDDeviceGetProperty(private->device
, CFSTR(kIOHIDProductKey
));
174 case HID_STRING_ID_IMANUFACTURER
:
175 str
= IOHIDDeviceGetProperty(private->device
, CFSTR(kIOHIDManufacturerKey
));
177 case HID_STRING_ID_ISERIALNUMBER
:
178 str
= IOHIDDeviceGetProperty(private->device
, CFSTR(kIOHIDSerialNumberKey
));
181 ERR("Unknown string index\n");
182 return STATUS_NOT_IMPLEMENTED
;
187 if (length
< CFStringGetLength(str
) + 1)
188 return STATUS_BUFFER_TOO_SMALL
;
189 CFStringToWSTR(str
, buffer
, length
);
193 if (!length
) return STATUS_BUFFER_TOO_SMALL
;
197 return STATUS_SUCCESS
;
200 static NTSTATUS
begin_report_processing(DEVICE_OBJECT
*device
)
203 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
207 return STATUS_SUCCESS
;
209 num
= IOHIDDeviceGetProperty(private->device
, CFSTR(kIOHIDMaxInputReportSizeKey
));
210 length
= CFNumberToDWORD(num
);
211 private->buffer
= HeapAlloc(GetProcessHeap(), 0, length
);
213 IOHIDDeviceRegisterInputReportCallback(private->device
, private->buffer
, length
, handle_IOHIDDeviceIOHIDReportCallback
, device
);
214 return STATUS_SUCCESS
;
217 static NTSTATUS
set_output_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
220 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
221 result
= IOHIDDeviceSetReport(private->device
, kIOHIDReportTypeOutput
, id
, report
, length
);
222 if (result
== kIOReturnSuccess
)
225 return STATUS_SUCCESS
;
230 return STATUS_UNSUCCESSFUL
;
234 static NTSTATUS
get_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*read
)
237 CFIndex report_length
= length
;
238 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
240 ret
= IOHIDDeviceGetReport(private->device
, kIOHIDReportTypeFeature
, id
, report
, &report_length
);
241 if (ret
== kIOReturnSuccess
)
243 *read
= report_length
;
244 return STATUS_SUCCESS
;
249 return STATUS_UNSUCCESSFUL
;
253 static NTSTATUS
set_feature_report(DEVICE_OBJECT
*device
, UCHAR id
, BYTE
*report
, DWORD length
, ULONG_PTR
*written
)
256 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
258 result
= IOHIDDeviceSetReport(private->device
, kIOHIDReportTypeFeature
, id
, report
, length
);
259 if (result
== kIOReturnSuccess
)
262 return STATUS_SUCCESS
;
267 return STATUS_UNSUCCESSFUL
;
271 static const platform_vtbl iohid_vtbl
=
273 compare_platform_device
,
274 get_reportdescriptor
,
276 begin_report_processing
,
282 static void handle_DeviceMatchingCallback(void *context
, IOReturn result
, void *sender
, IOHIDDeviceRef IOHIDDevice
)
284 DEVICE_OBJECT
*device
;
285 DWORD vid
, pid
, version
, uid
;
286 CFStringRef str
= NULL
;
287 WCHAR serial_string
[256];
288 BOOL is_gamepad
= FALSE
;
291 TRACE("OS/X IOHID Device Added %p\n", IOHIDDevice
);
293 vid
= CFNumberToDWORD(IOHIDDeviceGetProperty(IOHIDDevice
, CFSTR(kIOHIDVendorIDKey
)));
294 pid
= CFNumberToDWORD(IOHIDDeviceGetProperty(IOHIDDevice
, CFSTR(kIOHIDProductIDKey
)));
295 version
= CFNumberToDWORD(IOHIDDeviceGetProperty(IOHIDDevice
, CFSTR(kIOHIDVersionNumberKey
)));
296 str
= IOHIDDeviceGetProperty(IOHIDDevice
, CFSTR(kIOHIDSerialNumberKey
));
297 if (str
) CFStringToWSTR(str
, serial_string
, ARRAY_SIZE(serial_string
));
298 uid
= CFNumberToDWORD(IOHIDDeviceGetProperty(IOHIDDevice
, CFSTR(kIOHIDLocationIDKey
)));
300 if (IOHIDDeviceOpen(IOHIDDevice
, 0) != kIOReturnSuccess
)
302 ERR("Failed to open HID device %p (vid %04x, pid %04x)\n", IOHIDDevice
, vid
, pid
);
305 IOHIDDeviceScheduleWithRunLoop(IOHIDDevice
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
307 if (IOHIDDeviceConformsTo(IOHIDDevice
, kHIDPage_GenericDesktop
, kHIDUsage_GD_GamePad
) ||
308 IOHIDDeviceConformsTo(IOHIDDevice
, kHIDPage_GenericDesktop
, kHIDUsage_GD_Joystick
))
310 if (is_xbox_gamepad(vid
, pid
))
314 int axes
=0, buttons
=0;
315 CFArrayRef element_array
= IOHIDDeviceCopyMatchingElements(
316 IOHIDDevice
, NULL
, kIOHIDOptionsTypeNone
);
320 CFIndex count
= CFArrayGetCount(element_array
);
321 for (index
= 0; index
< count
; index
++)
323 IOHIDElementRef element
= (IOHIDElementRef
)CFArrayGetValueAtIndex(element_array
, index
);
326 int type
= IOHIDElementGetType(element
);
327 if (type
== kIOHIDElementTypeInput_Button
) buttons
++;
328 if (type
== kIOHIDElementTypeInput_Axis
) axes
++;
329 if (type
== kIOHIDElementTypeInput_Misc
)
331 uint32_t usage
= IOHIDElementGetUsage(element
);
337 case kHIDUsage_GD_Rx
:
338 case kHIDUsage_GD_Ry
:
339 case kHIDUsage_GD_Rz
:
340 case kHIDUsage_GD_Slider
:
346 CFRelease(element_array
);
348 is_gamepad
= (axes
== 6 && buttons
>= 14);
354 device
= bus_create_hid_device(busidW
, vid
, pid
, input
,
355 version
, uid
, str
? serial_string
: NULL
, is_gamepad
,
356 &iohid_vtbl
, sizeof(struct platform_private
));
358 ERR("Failed to create device\n");
361 struct platform_private
*private = impl_from_DEVICE_OBJECT(device
);
362 private->device
= IOHIDDevice
;
363 private->buffer
= NULL
;
364 IoInvalidateDeviceRelations(bus_pdo
, BusRelations
);
368 static void handle_RemovalCallback(void *context
, IOReturn result
, void *sender
, IOHIDDeviceRef IOHIDDevice
)
370 DEVICE_OBJECT
*device
;
371 TRACE("OS/X IOHID Device Removed %p\n", IOHIDDevice
);
372 IOHIDDeviceRegisterInputReportCallback(IOHIDDevice
, NULL
, 0, NULL
, NULL
);
373 /* Note: Yes, we leak the buffer. But according to research there is no
374 safe way to deallocate that buffer. */
375 IOHIDDeviceUnscheduleFromRunLoop(IOHIDDevice
, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode
);
376 IOHIDDeviceClose(IOHIDDevice
, 0);
377 device
= bus_find_hid_device(&iohid_vtbl
, IOHIDDevice
);
380 bus_unlink_hid_device(device
);
381 IoInvalidateDeviceRelations(bus_pdo
, BusRelations
);
382 bus_remove_hid_device(device
);
386 /* This puts the relevant run loop for event handling into a WINE thread */
387 static DWORD CALLBACK
runloop_thread(void *args
)
389 run_loop
= CFRunLoopGetCurrent();
391 IOHIDManagerSetDeviceMatching(hid_manager
, NULL
);
392 IOHIDManagerRegisterDeviceMatchingCallback(hid_manager
, handle_DeviceMatchingCallback
, NULL
);
393 IOHIDManagerRegisterDeviceRemovalCallback(hid_manager
, handle_RemovalCallback
, NULL
);
394 IOHIDManagerScheduleWithRunLoop(hid_manager
, run_loop
, kCFRunLoopDefaultMode
);
397 TRACE("Run Loop exiting\n");
402 NTSTATUS
iohid_driver_init(void)
404 hid_manager
= IOHIDManagerCreate(kCFAllocatorDefault
, 0L);
405 if (!(run_loop_handle
= CreateThread(NULL
, 0, runloop_thread
, NULL
, 0, NULL
)))
407 ERR("Failed to initialize IOHID Manager thread\n");
408 CFRelease(hid_manager
);
409 return STATUS_UNSUCCESSFUL
;
412 TRACE("Initialization successful\n");
413 return STATUS_SUCCESS
;
416 void iohid_driver_unload( void )
418 TRACE("Unloading Driver\n");
420 if (!run_loop_handle
)
423 IOHIDManagerUnscheduleFromRunLoop(hid_manager
, run_loop
, kCFRunLoopDefaultMode
);
424 CFRunLoopStop(run_loop
);
425 WaitForSingleObject(run_loop_handle
, INFINITE
);
426 CloseHandle(run_loop_handle
);
427 IOHIDManagerRegisterDeviceMatchingCallback(hid_manager
, NULL
, NULL
);
428 IOHIDManagerRegisterDeviceRemovalCallback(hid_manager
, NULL
, NULL
);
429 CFRelease(hid_manager
);
430 TRACE("Driver Unloaded\n");
435 NTSTATUS
iohid_driver_init(void)
437 return STATUS_NOT_IMPLEMENTED
;
440 void iohid_driver_unload( void )
442 TRACE("Stub: Unload Driver\n");
445 #endif /* HAVE_IOHIDMANAGERCREATE */