mstask: Implement ITask::DeleteTrigger().
[wine.git] / dlls / hidclass.sys / device.c
bloba5cbee915077c869742b7b9a73a3336c1159d245
1 /*
2 * HIDClass device functions
4 * Copyright (C) 2015 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"
23 #include <stdarg.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "hid.h"
27 #include "wine/unicode.h"
28 #include "winreg.h"
29 #include "winuser.h"
30 #include "setupapi.h"
32 #include "wine/debug.h"
33 #include "ddk/hidsdi.h"
34 #include "ddk/hidtypes.h"
36 #include "initguid.h"
37 #include "devguid.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(hid);
40 WINE_DECLARE_DEBUG_CHANNEL(hid_report);
42 static const WCHAR device_name_fmtW[] = {'\\','D','e','v','i','c','e',
43 '\\','H','I','D','#','%','p','&','%','p',0};
44 static const WCHAR device_link_fmtW[] = {'\\','?','?','\\','%','s','#','%','s',0};
45 /* GUID_DEVINTERFACE_HID */
46 static const WCHAR class_guid[] = {'{','4','D','1','E','5','5','B','2',
47 '-','F','1','6','F','-','1','1','C','F','-','8','8','C','B','-','0','0',
48 '1','1','1','1','0','0','0','0','3','0','}',0};
51 NTSTATUS HID_CreateDevice(DEVICE_OBJECT *native_device, HID_MINIDRIVER_REGISTRATION *driver, DEVICE_OBJECT **device)
53 WCHAR dev_name[255];
54 UNICODE_STRING nameW;
55 NTSTATUS status;
56 BASE_DEVICE_EXTENSION *ext;
58 sprintfW(dev_name, device_name_fmtW, driver->DriverObject, native_device);
59 RtlInitUnicodeString( &nameW, dev_name );
61 TRACE("Create base hid device %s\n", debugstr_w(dev_name));
63 status = IoCreateDevice(driver->DriverObject, driver->DeviceExtensionSize + sizeof(BASE_DEVICE_EXTENSION), &nameW, 0, 0, FALSE, device);
64 if (status)
66 FIXME( "failed to create device error %x\n", status );
67 return status;
70 ext = (*device)->DeviceExtension;
72 ext->deviceExtension.MiniDeviceExtension = ext + 1;
73 ext->deviceExtension.PhysicalDeviceObject = *device;
74 ext->deviceExtension.NextDeviceObject = native_device;
75 ext->device_name = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(dev_name) + 1) * sizeof(WCHAR));
76 lstrcpyW(ext->device_name, dev_name);
77 ext->link_name = NULL;
79 IoAttachDeviceToDeviceStack(*device, native_device);
81 return STATUS_SUCCESS;
84 NTSTATUS HID_LinkDevice(DEVICE_OBJECT *device)
86 WCHAR dev_link[255];
87 WCHAR *ptr;
88 SP_DEVINFO_DATA Data;
89 UNICODE_STRING nameW, linkW;
90 NTSTATUS status;
91 HDEVINFO devinfo;
92 GUID hidGuid;
93 BASE_DEVICE_EXTENSION *ext;
95 HidD_GetHidGuid(&hidGuid);
96 ext = device->DeviceExtension;
98 sprintfW(dev_link, device_link_fmtW, ext->instance_id, class_guid);
99 ptr = dev_link + 4;
100 do { if (*ptr == '\\') *ptr = '#'; } while (*ptr++);
101 struprW(dev_link);
103 RtlInitUnicodeString( &nameW, ext->device_name);
104 RtlInitUnicodeString( &linkW, dev_link );
106 TRACE("Create link %s\n", debugstr_w(dev_link));
108 ext->link_name = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (lstrlenW(dev_link) + 1));
109 lstrcpyW(ext->link_name, dev_link);
111 status = IoCreateSymbolicLink( &linkW, &nameW );
112 if (status)
114 FIXME( "failed to create link error %x\n", status );
115 return status;
118 devinfo = SetupDiGetClassDevsW(&GUID_DEVCLASS_HIDCLASS, NULL, NULL, DIGCF_DEVICEINTERFACE);
119 if (!devinfo)
121 FIXME( "failed to get ClassDevs %x\n", GetLastError());
122 return STATUS_UNSUCCESSFUL;
124 Data.cbSize = sizeof(Data);
125 if (!SetupDiCreateDeviceInfoW(devinfo, ext->instance_id, &GUID_DEVCLASS_HIDCLASS, NULL, NULL, DICD_INHERIT_CLASSDRVS, &Data))
127 if (GetLastError() == ERROR_DEVINST_ALREADY_EXISTS)
129 SetupDiDestroyDeviceInfoList(devinfo);
130 return STATUS_SUCCESS;
132 FIXME( "failed to Create Device Info %x\n", GetLastError());
133 goto error;
135 if (!SetupDiRegisterDeviceInfo( devinfo, &Data, 0, NULL, NULL, NULL ))
137 FIXME( "failed to Register Device Info %x\n", GetLastError());
138 goto error;
140 if (!SetupDiCreateDeviceInterfaceW( devinfo, &Data, &hidGuid, NULL, 0, NULL))
142 FIXME( "failed to Create Device Interface %x\n", GetLastError());
143 goto error;
146 SetupDiDestroyDeviceInfoList(devinfo);
147 return STATUS_SUCCESS;
149 error:
150 SetupDiDestroyDeviceInfoList(devinfo);
151 return STATUS_UNSUCCESSFUL;
154 void HID_DeleteDevice(HID_MINIDRIVER_REGISTRATION *driver, DEVICE_OBJECT *device)
156 NTSTATUS status;
157 BASE_DEVICE_EXTENSION *ext;
158 UNICODE_STRING linkW;
159 LIST_ENTRY *entry;
160 IRP *irp;
162 ext = device->DeviceExtension;
164 if (ext->link_name)
166 TRACE("Delete link %s\n", debugstr_w(ext->link_name));
167 RtlInitUnicodeString(&linkW, ext->link_name);
169 status = IoDeleteSymbolicLink(&linkW);
170 if (status != STATUS_SUCCESS)
171 ERR("Delete Symbolic Link failed (%x)\n",status);
174 if (ext->thread)
176 SetEvent(ext->halt_event);
177 WaitForSingleObject(ext->thread, INFINITE);
179 CloseHandle(ext->halt_event);
181 HeapFree(GetProcessHeap(), 0, ext->preparseData);
182 if (ext->ring_buffer)
183 RingBuffer_Destroy(ext->ring_buffer);
185 entry = RemoveHeadList(&ext->irp_queue);
186 while(entry != &ext->irp_queue)
188 irp = CONTAINING_RECORD(entry, IRP, Tail.Overlay.s.ListEntry);
189 irp->IoStatus.u.Status = STATUS_DEVICE_REMOVED;
190 IoCompleteRequest(irp, IO_NO_INCREMENT);
191 entry = RemoveHeadList(&ext->irp_queue);
194 TRACE("Delete device(%p) %s\n", device, debugstr_w(ext->device_name));
195 HeapFree(GetProcessHeap(), 0, ext->device_name);
196 HeapFree(GetProcessHeap(), 0, ext->link_name);
198 IoDeleteDevice(device);
201 static NTSTATUS copy_packet_into_buffer(HID_XFER_PACKET *packet, BYTE* buffer, ULONG buffer_length, ULONG *out_length)
203 BOOL zero_id = (packet->reportId == 0);
205 *out_length = 0;
207 if ((zero_id && buffer_length > packet->reportBufferLen) ||
208 (!zero_id && buffer_length >= packet->reportBufferLen))
210 if (packet->reportId != 0)
212 memcpy(buffer, packet->reportBuffer, packet->reportBufferLen);
213 *out_length = packet->reportBufferLen;
215 else
217 buffer[0] = 0;
218 memcpy(&buffer[1], packet->reportBuffer, packet->reportBufferLen);
219 *out_length = packet->reportBufferLen + 1;
221 return STATUS_SUCCESS;
223 else
224 return STATUS_BUFFER_OVERFLOW;
227 static void HID_Device_processQueue(DEVICE_OBJECT *device)
229 LIST_ENTRY *entry;
230 IRP *irp;
231 BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
232 UINT buffer_size = RingBuffer_GetBufferSize(ext->ring_buffer);
233 HID_XFER_PACKET *packet;
235 packet = HeapAlloc(GetProcessHeap(), 0, buffer_size);
237 entry = RemoveHeadList(&ext->irp_queue);
238 while(entry != &ext->irp_queue)
240 int ptr;
241 irp = CONTAINING_RECORD(entry, IRP, Tail.Overlay.s.ListEntry);
242 ptr = PtrToUlong( irp->Tail.Overlay.OriginalFileObject->FsContext );
244 RingBuffer_Read(ext->ring_buffer, ptr, packet, &buffer_size);
245 if (buffer_size)
247 NTSTATUS rc;
248 ULONG out_length;
249 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
250 packet->reportBuffer = (BYTE *)packet + sizeof(*packet);
251 TRACE_(hid_report)("Processing Request (%i)\n",ptr);
252 rc = copy_packet_into_buffer(packet, irp->AssociatedIrp.SystemBuffer, irpsp->Parameters.Read.Length, &out_length);
253 irp->IoStatus.u.Status = rc;
254 irp->IoStatus.Information = out_length;
256 else
258 irp->IoStatus.Information = 0;
259 irp->IoStatus.u.Status = STATUS_UNSUCCESSFUL;
261 IoCompleteRequest( irp, IO_NO_INCREMENT );
262 entry = RemoveHeadList(&ext->irp_queue);
264 HeapFree(GetProcessHeap(), 0, packet);
267 static NTSTATUS WINAPI read_Completion(DEVICE_OBJECT *deviceObject, IRP *irp, void *context)
269 HANDLE event = context;
270 SetEvent(event);
271 return STATUS_MORE_PROCESSING_REQUIRED;
274 static DWORD CALLBACK hid_device_thread(void *args)
276 DEVICE_OBJECT *device = (DEVICE_OBJECT*)args;
278 IRP *irp;
279 IO_STATUS_BLOCK irp_status;
280 HID_XFER_PACKET *packet;
281 DWORD rc;
282 HANDLE events[2];
283 NTSTATUS ntrc;
285 BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
286 events[0] = CreateEventA(NULL, TRUE, FALSE, NULL);
287 events[1] = ext->halt_event;
289 packet = HeapAlloc(GetProcessHeap(), 0, sizeof(*packet) + ext->preparseData->caps.InputReportByteLength);
290 packet->reportBuffer = (BYTE *)packet + sizeof(*packet);
292 if (ext->information.Polled)
294 while(1)
296 ResetEvent(events[0]);
298 packet->reportBufferLen = ext->preparseData->caps.InputReportByteLength;
299 packet->reportId = 0;
301 irp = IoBuildDeviceIoControlRequest(IOCTL_HID_GET_INPUT_REPORT,
302 device, NULL, 0, packet, sizeof(*packet), TRUE, NULL,
303 &irp_status);
305 IoSetCompletionRoutine(irp, read_Completion, events[0], TRUE, TRUE, TRUE);
306 ntrc = IoCallDriver(device, irp);
308 if (ntrc == STATUS_PENDING)
309 WaitForMultipleObjects(2, events, FALSE, INFINITE);
311 if (irp->IoStatus.u.Status == STATUS_SUCCESS)
313 RingBuffer_Write(ext->ring_buffer, packet);
314 HID_Device_processQueue(device);
317 IoCompleteRequest(irp, IO_NO_INCREMENT );
319 rc = WaitForSingleObject(ext->halt_event, ext->poll_interval ? ext->poll_interval : DEFAULT_POLL_INTERVAL);
321 if (rc == WAIT_OBJECT_0)
322 break;
323 else if (rc != WAIT_TIMEOUT)
324 ERR("Wait returned unexpected value %x\n",rc);
327 else
329 INT exit_now = FALSE;
331 while(1)
333 ResetEvent(events[0]);
335 irp = IoBuildDeviceIoControlRequest(IOCTL_HID_READ_REPORT,
336 device, NULL, 0, packet->reportBuffer,
337 ext->preparseData->caps.InputReportByteLength, TRUE, NULL,
338 &irp_status);
340 IoSetCompletionRoutine(irp, read_Completion, events[0], TRUE, TRUE, TRUE);
341 ntrc = IoCallDriver(device, irp);
343 if (ntrc == STATUS_PENDING)
345 WaitForMultipleObjects(2, events, FALSE, INFINITE);
348 rc = WaitForSingleObject(ext->halt_event, 0);
349 if (rc == WAIT_OBJECT_0)
350 exit_now = TRUE;
352 if (!exit_now && irp->IoStatus.u.Status == STATUS_SUCCESS)
354 packet->reportBufferLen = irp->IoStatus.Information;
355 if (ext->preparseData->InputReports[0].reportID)
356 packet->reportId = packet->reportBuffer[0];
357 else
358 packet->reportId = 0;
359 RingBuffer_Write(ext->ring_buffer, packet);
360 HID_Device_processQueue(device);
363 IoCompleteRequest(irp, IO_NO_INCREMENT );
365 if (exit_now)
366 break;
370 /* FIXME: releasing packet requires IRP cancellation support */
371 CloseHandle(events[0]);
373 TRACE("Device thread exiting\n");
374 return 1;
377 void HID_StartDeviceThread(DEVICE_OBJECT *device)
379 BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
380 ext->halt_event = CreateEventA(NULL, TRUE, FALSE, NULL);
381 ext->thread = CreateThread(NULL, 0, hid_device_thread, device, 0, NULL);
384 static NTSTATUS handle_IOCTL_HID_GET_COLLECTION_INFORMATION(IRP *irp, BASE_DEVICE_EXTENSION *base)
386 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
387 if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(HID_COLLECTION_INFORMATION))
389 irp->IoStatus.u.Status = STATUS_BUFFER_OVERFLOW;
390 irp->IoStatus.Information = 0;
392 else
394 memcpy(irp->AssociatedIrp.SystemBuffer, &base->information, sizeof(HID_COLLECTION_INFORMATION));
395 irp->IoStatus.Information = sizeof(HID_COLLECTION_INFORMATION);
396 irp->IoStatus.u.Status = STATUS_SUCCESS;
398 return STATUS_SUCCESS;
401 static NTSTATUS handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR(IRP *irp, BASE_DEVICE_EXTENSION *base)
403 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
405 if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < base->preparseData->dwSize)
407 irp->IoStatus.u.Status = STATUS_INVALID_BUFFER_SIZE;
408 irp->IoStatus.Information = 0;
410 else
412 memcpy(irp->UserBuffer, base->preparseData, base->preparseData->dwSize);
413 irp->IoStatus.Information = base->preparseData->dwSize;
414 irp->IoStatus.u.Status = STATUS_SUCCESS;
416 return STATUS_SUCCESS;
419 static NTSTATUS handle_minidriver_string(DEVICE_OBJECT *device, IRP *irp, SHORT index)
421 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
422 WCHAR buffer[127];
423 NTSTATUS status;
424 ULONG InputBuffer;
426 InputBuffer = MAKELONG(index, 0);
427 status = call_minidriver(IOCTL_HID_GET_STRING, device, ULongToPtr(InputBuffer), sizeof(InputBuffer), buffer, sizeof(buffer));
429 if (status == STATUS_SUCCESS)
431 WCHAR *out_buffer = MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority);
432 int length = irpsp->Parameters.DeviceIoControl.OutputBufferLength/sizeof(WCHAR);
433 TRACE("got string %s from minidriver\n",debugstr_w(buffer));
434 lstrcpynW(out_buffer, buffer, length);
435 irp->IoStatus.Information = (lstrlenW(buffer)+1) * sizeof(WCHAR);
437 irp->IoStatus.u.Status = status;
439 return STATUS_SUCCESS;
442 static NTSTATUS HID_get_feature(DEVICE_OBJECT *device, IRP *irp)
444 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
445 HID_XFER_PACKET *packet;
446 DWORD len;
447 NTSTATUS rc = STATUS_SUCCESS;
448 BYTE *out_buffer;
450 irp->IoStatus.Information = 0;
452 out_buffer = MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority);
453 TRACE_(hid_report)("Device %p Buffer length %i Buffer %p\n", device, irpsp->Parameters.DeviceIoControl.OutputBufferLength, out_buffer);
455 len = sizeof(*packet) + irpsp->Parameters.DeviceIoControl.OutputBufferLength;
456 packet = HeapAlloc(GetProcessHeap(), 0, len);
457 packet->reportBufferLen = irpsp->Parameters.DeviceIoControl.OutputBufferLength;
458 packet->reportBuffer = ((BYTE*)packet) + sizeof(*packet);
459 packet->reportId = out_buffer[0];
461 TRACE_(hid_report)("(id %i, len %i buffer %p)\n", packet->reportId, packet->reportBufferLen, packet->reportBuffer);
463 rc = call_minidriver(IOCTL_HID_GET_FEATURE, device, NULL, 0, packet, sizeof(*packet));
465 irp->IoStatus.u.Status = rc;
466 if (irp->IoStatus.u.Status == STATUS_SUCCESS)
468 irp->IoStatus.Information = packet->reportBufferLen;
469 memcpy(out_buffer, packet->reportBuffer, packet->reportBufferLen);
471 else
472 irp->IoStatus.Information = 0;
474 TRACE_(hid_report)("Result 0x%x get %li bytes\n", rc, irp->IoStatus.Information);
476 HeapFree(GetProcessHeap(), 0, packet);
478 return rc;
481 static NTSTATUS HID_set_to_device(DEVICE_OBJECT *device, IRP *irp)
483 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
484 HID_XFER_PACKET packet;
485 NTSTATUS rc;
487 TRACE_(hid_report)("Device %p Buffer length %i Buffer %p\n", device, irpsp->Parameters.DeviceIoControl.InputBufferLength, irp->AssociatedIrp.SystemBuffer);
488 packet.reportId = ((BYTE*)irp->AssociatedIrp.SystemBuffer)[0];
489 if (packet.reportId == 0)
491 packet.reportBuffer = &((BYTE*)irp->AssociatedIrp.SystemBuffer)[1];
492 packet.reportBufferLen = irpsp->Parameters.DeviceIoControl.InputBufferLength - 1;
494 else
496 packet.reportBuffer = irp->AssociatedIrp.SystemBuffer;
497 packet.reportBufferLen = irpsp->Parameters.DeviceIoControl.InputBufferLength;
499 TRACE_(hid_report)("(id %i, len %i buffer %p)\n", packet.reportId, packet.reportBufferLen, packet.reportBuffer);
501 rc = call_minidriver(irpsp->Parameters.DeviceIoControl.IoControlCode,
502 device, NULL, 0, &packet, sizeof(packet));
504 irp->IoStatus.u.Status = rc;
505 if (irp->IoStatus.u.Status == STATUS_SUCCESS)
506 irp->IoStatus.Information = irpsp->Parameters.DeviceIoControl.InputBufferLength;
507 else
508 irp->IoStatus.Information = 0;
510 TRACE_(hid_report)("Result 0x%x set %li bytes\n", rc, irp->IoStatus.Information);
512 return rc;
515 NTSTATUS WINAPI HID_Device_ioctl(DEVICE_OBJECT *device, IRP *irp)
517 NTSTATUS rc = STATUS_SUCCESS;
518 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
519 BASE_DEVICE_EXTENSION *extension = device->DeviceExtension;
521 irp->IoStatus.Information = 0;
523 TRACE("device %p ioctl(%x)\n", device, irpsp->Parameters.DeviceIoControl.IoControlCode);
525 switch (irpsp->Parameters.DeviceIoControl.IoControlCode)
527 case IOCTL_HID_GET_POLL_FREQUENCY_MSEC:
528 TRACE("IOCTL_HID_GET_POLL_FREQUENCY_MSEC\n");
529 if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(ULONG))
531 irp->IoStatus.u.Status = STATUS_BUFFER_OVERFLOW;
532 irp->IoStatus.Information = 0;
533 break;
535 *((ULONG*)irp->AssociatedIrp.SystemBuffer) = extension->poll_interval;
536 irp->IoStatus.Information = sizeof(ULONG);
537 irp->IoStatus.u.Status = STATUS_SUCCESS;
538 break;
539 case IOCTL_HID_SET_POLL_FREQUENCY_MSEC:
541 ULONG poll_interval;
542 TRACE("IOCTL_HID_SET_POLL_FREQUENCY_MSEC\n");
543 if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(ULONG))
545 irp->IoStatus.u.Status = STATUS_BUFFER_TOO_SMALL;
546 break;
548 poll_interval = *(ULONG *)irp->AssociatedIrp.SystemBuffer;
549 if (poll_interval <= MAX_POLL_INTERVAL_MSEC)
551 extension->poll_interval = poll_interval;
552 irp->IoStatus.u.Status = STATUS_SUCCESS;
554 else
555 irp->IoStatus.u.Status = STATUS_INVALID_PARAMETER;
556 break;
558 case IOCTL_HID_GET_PRODUCT_STRING:
560 rc = handle_minidriver_string(device, irp, HID_STRING_ID_IPRODUCT);
561 break;
563 case IOCTL_HID_GET_SERIALNUMBER_STRING:
565 rc = handle_minidriver_string(device, irp, HID_STRING_ID_ISERIALNUMBER);
566 break;
568 case IOCTL_HID_GET_MANUFACTURER_STRING:
570 rc = handle_minidriver_string(device, irp, HID_STRING_ID_IMANUFACTURER);
571 break;
573 case IOCTL_HID_GET_COLLECTION_INFORMATION:
575 rc = handle_IOCTL_HID_GET_COLLECTION_INFORMATION(irp, extension);
576 break;
578 case IOCTL_HID_GET_COLLECTION_DESCRIPTOR:
580 rc = handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR(irp, extension);
581 break;
583 case IOCTL_HID_GET_INPUT_REPORT:
585 HID_XFER_PACKET *packet;
586 UINT packet_size = sizeof(*packet) + irpsp->Parameters.DeviceIoControl.OutputBufferLength;
587 BYTE *buffer = MmGetSystemAddressForMdlSafe(irp->MdlAddress, NormalPagePriority);
588 ULONG out_length;
590 packet = HeapAlloc(GetProcessHeap(), 0, packet_size);
592 if (extension->preparseData->InputReports[0].reportID)
593 packet->reportId = buffer[0];
594 else
595 packet->reportId = 0;
596 packet->reportBuffer = (BYTE *)packet + sizeof(*packet);
597 packet->reportBufferLen = irpsp->Parameters.DeviceIoControl.OutputBufferLength - 1;
599 rc = call_minidriver(IOCTL_HID_GET_INPUT_REPORT, device, NULL, 0, packet, sizeof(*packet));
600 if (rc == STATUS_SUCCESS)
602 rc = copy_packet_into_buffer(packet, buffer, irpsp->Parameters.DeviceIoControl.OutputBufferLength, &out_length);
603 irp->IoStatus.Information = out_length;
605 else
606 irp->IoStatus.Information = 0;
607 irp->IoStatus.u.Status = rc;
608 HeapFree(GetProcessHeap(), 0, packet);
609 break;
611 case IOCTL_SET_NUM_DEVICE_INPUT_BUFFERS:
613 irp->IoStatus.Information = 0;
615 if (irpsp->Parameters.DeviceIoControl.InputBufferLength != sizeof(ULONG))
617 irp->IoStatus.u.Status = rc = STATUS_BUFFER_OVERFLOW;
619 else
621 rc = RingBuffer_SetSize(extension->ring_buffer, *(ULONG*)irp->AssociatedIrp.SystemBuffer);
622 irp->IoStatus.u.Status = rc;
624 break;
626 case IOCTL_GET_NUM_DEVICE_INPUT_BUFFERS:
628 if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(ULONG))
630 irp->IoStatus.u.Status = rc = STATUS_BUFFER_TOO_SMALL;
632 else
634 *(ULONG*)irp->AssociatedIrp.SystemBuffer = RingBuffer_GetSize(extension->ring_buffer);
635 rc = irp->IoStatus.u.Status = STATUS_SUCCESS;
637 break;
639 case IOCTL_HID_GET_FEATURE:
640 rc = HID_get_feature(device, irp);
641 break;
642 case IOCTL_HID_SET_FEATURE:
643 case IOCTL_HID_SET_OUTPUT_REPORT:
644 rc = HID_set_to_device(device, irp);
645 break;
646 default:
648 ULONG code = irpsp->Parameters.DeviceIoControl.IoControlCode;
649 FIXME("Unsupported ioctl %x (device=%x access=%x func=%x method=%x)\n",
650 code, code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3);
651 irp->IoStatus.u.Status = STATUS_NOT_SUPPORTED;
652 rc = STATUS_UNSUCCESSFUL;
653 break;
657 if (rc != STATUS_PENDING)
658 IoCompleteRequest( irp, IO_NO_INCREMENT );
660 return rc;
663 NTSTATUS WINAPI HID_Device_read(DEVICE_OBJECT *device, IRP *irp)
665 HID_XFER_PACKET *packet;
666 BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
667 UINT buffer_size = RingBuffer_GetBufferSize(ext->ring_buffer);
668 NTSTATUS rc = STATUS_SUCCESS;
669 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
670 int ptr = -1;
672 packet = HeapAlloc(GetProcessHeap(), 0, buffer_size);
673 ptr = PtrToUlong( irp->Tail.Overlay.OriginalFileObject->FsContext );
675 irp->IoStatus.Information = 0;
676 RingBuffer_ReadNew(ext->ring_buffer, ptr, packet, &buffer_size);
678 if (buffer_size)
680 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
681 NTSTATUS rc;
682 ULONG out_length;
683 packet->reportBuffer = (BYTE *)packet + sizeof(*packet);
684 TRACE_(hid_report)("Got Packet %p %i\n", packet->reportBuffer, packet->reportBufferLen);
686 rc = copy_packet_into_buffer(packet, irp->AssociatedIrp.SystemBuffer, irpsp->Parameters.Read.Length, &out_length);
687 irp->IoStatus.Information = out_length;
688 irp->IoStatus.u.Status = rc;
689 IoCompleteRequest(irp, IO_NO_INCREMENT);
691 else
693 BASE_DEVICE_EXTENSION *extension = device->DeviceExtension;
694 if (extension->poll_interval)
696 TRACE_(hid_report)("Queue irp\n");
697 InsertTailList(&ext->irp_queue, &irp->Tail.Overlay.s.ListEntry);
698 rc = STATUS_PENDING;
700 else
702 HID_XFER_PACKET packet;
703 TRACE("No packet, but opportunistic reads enabled\n");
704 packet.reportId = ((BYTE*)irp->AssociatedIrp.SystemBuffer)[0];
705 packet.reportBuffer = &((BYTE*)irp->AssociatedIrp.SystemBuffer)[1];
706 packet.reportBufferLen = irpsp->Parameters.Read.Length - 1;
707 rc = call_minidriver(IOCTL_HID_GET_INPUT_REPORT, device, NULL, 0, &packet, sizeof(packet));
709 if (rc == STATUS_SUCCESS)
711 ((BYTE*)irp->AssociatedIrp.SystemBuffer)[0] = packet.reportId;
712 irp->IoStatus.Information = packet.reportBufferLen + 1;
713 irp->IoStatus.u.Status = rc;
715 IoCompleteRequest(irp, IO_NO_INCREMENT);
718 HeapFree(GetProcessHeap(), 0, packet);
720 return rc;
723 NTSTATUS WINAPI HID_Device_write(DEVICE_OBJECT *device, IRP *irp)
725 IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
726 HID_XFER_PACKET packet;
727 NTSTATUS rc;
729 irp->IoStatus.Information = 0;
731 TRACE_(hid_report)("Device %p Buffer length %i Buffer %p\n", device, irpsp->Parameters.Write.Length, irp->AssociatedIrp.SystemBuffer);
732 packet.reportId = ((BYTE*)irp->AssociatedIrp.SystemBuffer)[0];
733 if (packet.reportId == 0)
735 packet.reportBuffer = &((BYTE*)irp->AssociatedIrp.SystemBuffer)[1];
736 packet.reportBufferLen = irpsp->Parameters.Write.Length - 1;
738 else
740 packet.reportBuffer = irp->AssociatedIrp.SystemBuffer;
741 packet.reportBufferLen = irpsp->Parameters.Write.Length;
743 TRACE_(hid_report)("(id %i, len %i buffer %p)\n", packet.reportId, packet.reportBufferLen, packet.reportBuffer);
745 rc = call_minidriver(IOCTL_HID_WRITE_REPORT, device, NULL, 0, &packet, sizeof(packet));
747 irp->IoStatus.u.Status = rc;
748 if (irp->IoStatus.u.Status == STATUS_SUCCESS)
749 irp->IoStatus.Information = irpsp->Parameters.Write.Length;
750 else
751 irp->IoStatus.Information = 0;
753 TRACE_(hid_report)("Result 0x%x wrote %li bytes\n", rc, irp->IoStatus.Information);
755 IoCompleteRequest( irp, IO_NO_INCREMENT );
756 return rc;
759 NTSTATUS WINAPI HID_Device_create(DEVICE_OBJECT *device, IRP *irp)
761 BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
763 TRACE("Open handle on device %p\n", device);
764 irp->Tail.Overlay.OriginalFileObject->FsContext = UlongToPtr(RingBuffer_AddPointer(ext->ring_buffer));
765 irp->IoStatus.u.Status = STATUS_SUCCESS;
766 IoCompleteRequest( irp, IO_NO_INCREMENT );
767 return STATUS_SUCCESS;
770 NTSTATUS WINAPI HID_Device_close(DEVICE_OBJECT *device, IRP *irp)
772 BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
773 int ptr = PtrToUlong(irp->Tail.Overlay.OriginalFileObject->FsContext);
774 TRACE("Close handle on device %p\n", device);
775 RingBuffer_RemovePointer(ext->ring_buffer, ptr);
776 irp->IoStatus.u.Status = STATUS_SUCCESS;
777 IoCompleteRequest( irp, IO_NO_INCREMENT );
778 return STATUS_SUCCESS;