2015-09-01 22:02:13 +02:00
|
|
|
/*
|
|
|
|
* HIDClass device functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Aric Stewart
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2021-06-04 11:30:31 +02:00
|
|
|
#include <stdlib.h>
|
2015-09-01 22:02:13 +02:00
|
|
|
#include "hid.h"
|
|
|
|
#include "winreg.h"
|
|
|
|
#include "winuser.h"
|
|
|
|
|
|
|
|
#include "ddk/hidsdi.h"
|
|
|
|
#include "ddk/hidtypes.h"
|
2018-08-17 01:37:42 +02:00
|
|
|
#include "ddk/wdm.h"
|
2015-09-01 22:02:13 +02:00
|
|
|
|
2021-08-10 11:31:15 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/list.h"
|
|
|
|
|
2015-09-01 22:02:13 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(hid);
|
|
|
|
|
2019-05-08 23:09:16 +02:00
|
|
|
static void WINAPI read_cancel_routine(DEVICE_OBJECT *device, IRP *irp)
|
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
struct hid_queue *queue = irp->Tail.Overlay.OriginalFileObject->FsContext;
|
2021-09-24 10:07:12 +02:00
|
|
|
KIRQL irql;
|
2019-05-08 23:09:16 +02:00
|
|
|
|
|
|
|
TRACE("cancel %p IRP on device %p\n", irp, device);
|
|
|
|
|
|
|
|
IoReleaseCancelSpinLock(irp->CancelIrql);
|
|
|
|
|
2021-09-24 10:07:12 +02:00
|
|
|
KeAcquireSpinLock( &queue->lock, &irql );
|
2019-05-08 23:09:16 +02:00
|
|
|
|
2021-06-04 11:30:32 +02:00
|
|
|
RemoveEntryList(&irp->Tail.Overlay.ListEntry);
|
2019-05-08 23:09:16 +02:00
|
|
|
|
2021-09-24 10:07:12 +02:00
|
|
|
KeReleaseSpinLock( &queue->lock, irql );
|
2019-05-08 23:09:16 +02:00
|
|
|
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_CANCELLED;
|
2019-05-08 23:09:16 +02:00
|
|
|
irp->IoStatus.Information = 0;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:24:00 +01:00
|
|
|
static struct hid_report *hid_report_create( HID_XFER_PACKET *packet, ULONG length )
|
2021-08-10 11:31:17 +02:00
|
|
|
{
|
|
|
|
struct hid_report *report;
|
|
|
|
|
2022-01-12 17:24:00 +01:00
|
|
|
if (!(report = malloc( offsetof( struct hid_report, buffer[length] ) )))
|
2021-08-10 11:31:17 +02:00
|
|
|
return NULL;
|
|
|
|
report->ref = 1;
|
2022-01-12 17:24:00 +01:00
|
|
|
report->length = length;
|
|
|
|
memcpy( report->buffer, packet->reportBuffer, packet->reportBufferLen );
|
|
|
|
memset( report->buffer + packet->reportBufferLen, 0, length - packet->reportBufferLen );
|
2021-08-10 11:31:17 +02:00
|
|
|
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hid_report_incref( struct hid_report *report )
|
|
|
|
{
|
|
|
|
InterlockedIncrement( &report->ref );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hid_report_decref( struct hid_report *report )
|
|
|
|
{
|
|
|
|
if (!report) return;
|
|
|
|
if (InterlockedDecrement( &report->ref ) == 0) free( report );
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
static struct hid_queue *hid_queue_create( void )
|
2021-08-10 11:31:17 +02:00
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
struct hid_queue *queue;
|
2021-08-10 11:31:17 +02:00
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
if (!(queue = calloc( 1, sizeof(struct hid_queue) ))) return NULL;
|
2021-09-24 10:07:12 +02:00
|
|
|
InitializeListHead( &queue->irp_queue );
|
2021-08-10 11:31:17 +02:00
|
|
|
KeInitializeSpinLock( &queue->lock );
|
|
|
|
list_init( &queue->entry );
|
|
|
|
queue->length = 32;
|
|
|
|
queue->read_idx = 0;
|
|
|
|
queue->write_idx = 0;
|
|
|
|
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
static IRP *hid_queue_pop_irp( struct hid_queue *queue )
|
2021-09-24 10:07:12 +02:00
|
|
|
{
|
|
|
|
LIST_ENTRY *entry;
|
|
|
|
IRP *irp = NULL;
|
|
|
|
KIRQL irql;
|
|
|
|
|
|
|
|
KeAcquireSpinLock( &queue->lock, &irql );
|
|
|
|
|
|
|
|
while (!irp && (entry = RemoveHeadList( &queue->irp_queue )) != &queue->irp_queue)
|
|
|
|
{
|
|
|
|
irp = CONTAINING_RECORD( entry, IRP, Tail.Overlay.ListEntry );
|
|
|
|
if (!IoSetCancelRoutine( irp, NULL ))
|
|
|
|
{
|
|
|
|
/* cancel routine is already cleared, meaning that it was called. let it handle completion. */
|
|
|
|
InitializeListHead( &irp->Tail.Overlay.ListEntry );
|
|
|
|
irp = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeReleaseSpinLock( &queue->lock, irql );
|
|
|
|
return irp;
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
void hid_queue_remove_pending_irps( struct hid_queue *queue )
|
2021-09-24 10:07:12 +02:00
|
|
|
{
|
|
|
|
IRP *irp;
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
while ((irp = hid_queue_pop_irp( queue )))
|
2021-09-24 10:07:12 +02:00
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_DELETE_PENDING;
|
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
void hid_queue_destroy( struct hid_queue *queue )
|
2021-08-10 11:31:17 +02:00
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
hid_queue_remove_pending_irps( queue );
|
2021-08-10 11:31:17 +02:00
|
|
|
while (queue->length--) hid_report_decref( queue->reports[queue->length] );
|
2021-09-24 10:07:11 +02:00
|
|
|
list_remove( &queue->entry );
|
2021-08-10 11:31:17 +02:00
|
|
|
free( queue );
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
static NTSTATUS hid_queue_resize( struct hid_queue *queue, ULONG length )
|
2021-08-10 11:31:17 +02:00
|
|
|
{
|
|
|
|
struct hid_report *old_reports[512];
|
|
|
|
LONG old_length = queue->length;
|
|
|
|
KIRQL irql;
|
|
|
|
|
|
|
|
if (length < 2 || length > 512) return STATUS_INVALID_PARAMETER;
|
|
|
|
if (length == queue->length) return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
KeAcquireSpinLock( &queue->lock, &irql );
|
|
|
|
memcpy( old_reports, queue->reports, old_length * sizeof(void *) );
|
|
|
|
memset( queue->reports, 0, old_length * sizeof(void *) );
|
|
|
|
queue->length = length;
|
|
|
|
queue->write_idx = 0;
|
|
|
|
queue->read_idx = 0;
|
|
|
|
KeReleaseSpinLock( &queue->lock, irql );
|
|
|
|
|
|
|
|
while (old_length--) hid_report_decref( old_reports[old_length] );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
static NTSTATUS hid_queue_push_irp( struct hid_queue *queue, IRP *irp )
|
2021-09-24 10:07:12 +02:00
|
|
|
{
|
|
|
|
KIRQL irql;
|
|
|
|
|
|
|
|
KeAcquireSpinLock( &queue->lock, &irql );
|
|
|
|
|
|
|
|
IoSetCancelRoutine( irp, read_cancel_routine );
|
|
|
|
if (irp->Cancel && !IoSetCancelRoutine( irp, NULL ))
|
|
|
|
{
|
|
|
|
/* IRP was canceled before we set cancel routine */
|
|
|
|
InitializeListHead( &irp->Tail.Overlay.ListEntry );
|
|
|
|
KeReleaseSpinLock( &queue->lock, irql );
|
|
|
|
return STATUS_CANCELLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertTailList( &queue->irp_queue, &irp->Tail.Overlay.ListEntry );
|
|
|
|
irp->IoStatus.Status = STATUS_PENDING;
|
|
|
|
IoMarkIrpPending( irp );
|
|
|
|
|
|
|
|
KeReleaseSpinLock( &queue->lock, irql );
|
|
|
|
return STATUS_PENDING;
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
static void hid_queue_push_report( struct hid_queue *queue, struct hid_report *report )
|
2021-08-10 11:31:17 +02:00
|
|
|
{
|
|
|
|
ULONG i = queue->write_idx, next = i + 1;
|
|
|
|
struct hid_report *prev;
|
|
|
|
KIRQL irql;
|
|
|
|
|
|
|
|
if (next >= queue->length) next = 0;
|
|
|
|
hid_report_incref( report );
|
|
|
|
|
|
|
|
KeAcquireSpinLock( &queue->lock, &irql );
|
|
|
|
prev = queue->reports[i];
|
|
|
|
queue->reports[i] = report;
|
2021-10-04 10:20:19 +02:00
|
|
|
if (next == queue->read_idx) queue->read_idx = next + 1;
|
|
|
|
if (queue->read_idx >= queue->length) queue->read_idx = 0;
|
2021-08-10 11:31:17 +02:00
|
|
|
KeReleaseSpinLock( &queue->lock, irql );
|
|
|
|
|
|
|
|
hid_report_decref( prev );
|
2021-10-04 10:20:19 +02:00
|
|
|
queue->write_idx = next;
|
2021-08-10 11:31:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
static struct hid_report *hid_queue_pop_report( struct hid_queue *queue )
|
2021-08-10 11:31:17 +02:00
|
|
|
{
|
|
|
|
ULONG i = queue->read_idx, next = i + 1;
|
|
|
|
struct hid_report *report;
|
|
|
|
KIRQL irql;
|
|
|
|
|
|
|
|
if (next >= queue->length) next = 0;
|
|
|
|
|
|
|
|
KeAcquireSpinLock( &queue->lock, &irql );
|
|
|
|
report = queue->reports[i];
|
|
|
|
queue->reports[i] = NULL;
|
|
|
|
if (i != queue->write_idx) queue->read_idx = next;
|
|
|
|
KeReleaseSpinLock( &queue->lock, irql );
|
|
|
|
|
|
|
|
return report;
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:31:15 +02:00
|
|
|
static void hid_device_queue_input( DEVICE_OBJECT *device, HID_XFER_PACKET *packet )
|
2021-05-12 11:25:28 +02:00
|
|
|
{
|
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2022-01-12 17:24:00 +01:00
|
|
|
HIDP_COLLECTION_DESC *desc = ext->u.pdo.device_desc.CollectionDesc;
|
2021-09-14 10:38:43 +02:00
|
|
|
const BOOL polled = ext->u.pdo.information.Polled;
|
2022-01-12 17:24:00 +01:00
|
|
|
ULONG size, report_len = polled ? packet->reportBufferLen : desc->InputLength;
|
2021-08-10 11:31:17 +02:00
|
|
|
struct hid_report *last_report, *report;
|
2021-09-24 10:07:13 +02:00
|
|
|
struct hid_queue *queue;
|
2021-09-24 10:07:12 +02:00
|
|
|
LIST_ENTRY completed, *entry;
|
2021-05-12 11:25:28 +02:00
|
|
|
RAWINPUT *rawinput;
|
2021-08-10 11:31:15 +02:00
|
|
|
KIRQL irql;
|
2021-08-10 11:31:16 +02:00
|
|
|
IRP *irp;
|
2021-05-12 11:25:28 +02:00
|
|
|
|
2021-09-22 12:27:26 +02:00
|
|
|
if (IsEqualGUID( ext->class_guid, &GUID_DEVINTERFACE_HID ))
|
2021-05-12 11:25:28 +02:00
|
|
|
{
|
2022-01-12 17:24:00 +01:00
|
|
|
size = offsetof( RAWINPUT, data.hid.bRawData[report_len] );
|
2021-09-22 12:27:26 +02:00
|
|
|
if (!(rawinput = malloc( size ))) ERR( "Failed to allocate rawinput data!\n" );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INPUT input;
|
|
|
|
|
|
|
|
rawinput->header.dwType = RIM_TYPEHID;
|
|
|
|
rawinput->header.dwSize = size;
|
|
|
|
rawinput->header.hDevice = ULongToHandle( ext->u.pdo.rawinput_handle );
|
|
|
|
rawinput->header.wParam = RIM_INPUT;
|
|
|
|
rawinput->data.hid.dwCount = 1;
|
2022-01-12 17:24:00 +01:00
|
|
|
rawinput->data.hid.dwSizeHid = report_len;
|
2021-09-22 12:27:26 +02:00
|
|
|
memcpy( rawinput->data.hid.bRawData, packet->reportBuffer, packet->reportBufferLen );
|
2022-01-12 17:24:00 +01:00
|
|
|
memset( rawinput->data.hid.bRawData + packet->reportBufferLen, 0, report_len - packet->reportBufferLen );
|
2021-09-22 12:27:26 +02:00
|
|
|
|
|
|
|
input.type = INPUT_HARDWARE;
|
|
|
|
input.hi.uMsg = WM_INPUT;
|
|
|
|
input.hi.wParamH = 0;
|
|
|
|
input.hi.wParamL = 0;
|
|
|
|
__wine_send_input( 0, &input, rawinput );
|
|
|
|
|
|
|
|
free( rawinput );
|
|
|
|
}
|
2021-05-12 11:25:28 +02:00
|
|
|
}
|
|
|
|
|
2022-01-12 17:24:00 +01:00
|
|
|
if (!(last_report = hid_report_create( packet, report_len )))
|
2021-08-10 11:31:16 +02:00
|
|
|
{
|
2021-08-10 11:31:17 +02:00
|
|
|
ERR( "Failed to allocate hid_report!\n" );
|
2021-08-10 11:31:16 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-10-08 21:34:35 +02:00
|
|
|
|
2021-09-24 10:07:12 +02:00
|
|
|
InitializeListHead( &completed );
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
KeAcquireSpinLock( &ext->u.pdo.queues_lock, &irql );
|
|
|
|
LIST_FOR_EACH_ENTRY( queue, &ext->u.pdo.queues, struct hid_queue, entry )
|
2015-10-08 21:34:35 +02:00
|
|
|
{
|
2021-10-12 10:33:24 +02:00
|
|
|
if (!polled) hid_queue_push_report( queue, last_report );
|
2021-08-10 11:31:16 +02:00
|
|
|
|
2021-09-24 10:07:12 +02:00
|
|
|
do
|
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
if (!(irp = hid_queue_pop_irp( queue ))) break;
|
|
|
|
if (!(report = hid_queue_pop_report( queue ))) hid_report_incref( (report = last_report) );
|
2021-09-24 10:07:12 +02:00
|
|
|
|
2021-10-04 10:18:12 +02:00
|
|
|
memcpy( irp->AssociatedIrp.SystemBuffer, report->buffer, report->length );
|
2021-09-24 10:07:12 +02:00
|
|
|
irp->IoStatus.Information = report->length;
|
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
hid_report_decref( report );
|
2021-08-10 11:31:17 +02:00
|
|
|
|
2021-09-24 10:07:12 +02:00
|
|
|
InsertTailList( &completed, &irp->Tail.Overlay.ListEntry );
|
|
|
|
}
|
|
|
|
while (polled);
|
|
|
|
}
|
2021-09-24 10:07:13 +02:00
|
|
|
KeReleaseSpinLock( &ext->u.pdo.queues_lock, irql );
|
2021-09-24 10:07:12 +02:00
|
|
|
|
|
|
|
while ((entry = RemoveHeadList( &completed )) != &completed)
|
|
|
|
{
|
|
|
|
irp = CONTAINING_RECORD( entry, IRP, Tail.Overlay.ListEntry );
|
2015-10-08 21:34:35 +02:00
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
}
|
2021-08-10 11:31:16 +02:00
|
|
|
|
2021-08-10 11:31:17 +02:00
|
|
|
hid_report_decref( last_report );
|
2015-10-08 21:34:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 10:18:13 +02:00
|
|
|
static HIDP_REPORT_IDS *find_report_with_type_and_id( BASE_DEVICE_EXTENSION *ext, BYTE type, BYTE id, BOOL any_id )
|
|
|
|
{
|
|
|
|
HIDP_REPORT_IDS *report, *reports = ext->u.pdo.device_desc.ReportIDs;
|
|
|
|
ULONG report_count = ext->u.pdo.device_desc.ReportIDsLength;
|
|
|
|
|
|
|
|
for (report = reports; report != reports + report_count; report++)
|
|
|
|
{
|
|
|
|
if (!any_id && report->ReportID && report->ReportID != id) continue;
|
|
|
|
if (type == HidP_Input && report->InputLength) return report;
|
|
|
|
if (type == HidP_Output && report->OutputLength) return report;
|
|
|
|
if (type == HidP_Feature && report->FeatureLength) return report;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-08 21:34:35 +02:00
|
|
|
static DWORD CALLBACK hid_device_thread(void *args)
|
|
|
|
{
|
|
|
|
DEVICE_OBJECT *device = (DEVICE_OBJECT*)args;
|
2021-08-05 10:36:06 +02:00
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2021-08-16 10:07:53 +02:00
|
|
|
HIDP_COLLECTION_DESC *desc = ext->u.pdo.device_desc.CollectionDesc;
|
2021-09-14 10:38:41 +02:00
|
|
|
BOOL polled = ext->u.pdo.information.Polled;
|
2021-10-04 10:18:13 +02:00
|
|
|
HIDP_REPORT_IDS *report;
|
2021-08-05 10:36:06 +02:00
|
|
|
HID_XFER_PACKET *packet;
|
2021-10-12 10:33:23 +02:00
|
|
|
ULONG report_id = 0;
|
2021-08-16 10:07:50 +02:00
|
|
|
IO_STATUS_BLOCK io;
|
2021-08-05 10:36:06 +02:00
|
|
|
BYTE *buffer;
|
2021-08-16 10:07:50 +02:00
|
|
|
DWORD res;
|
2015-10-08 21:34:35 +02:00
|
|
|
|
2021-08-16 10:07:53 +02:00
|
|
|
packet = malloc( sizeof(*packet) + desc->InputLength );
|
2021-08-05 10:36:06 +02:00
|
|
|
buffer = (BYTE *)(packet + 1);
|
2016-10-26 21:55:10 +02:00
|
|
|
|
2021-10-04 10:18:13 +02:00
|
|
|
report = find_report_with_type_and_id( ext, HidP_Input, 0, TRUE );
|
|
|
|
if (!report) WARN("no input report found.\n");
|
|
|
|
else report_id = report->ReportID;
|
2021-08-16 10:07:53 +02:00
|
|
|
|
2021-08-16 10:07:50 +02:00
|
|
|
do
|
2015-10-08 21:34:35 +02:00
|
|
|
{
|
2021-08-16 10:07:50 +02:00
|
|
|
packet->reportId = buffer[0] = report_id;
|
2021-12-02 10:47:44 +01:00
|
|
|
packet->reportBuffer = buffer;
|
2021-08-16 10:07:53 +02:00
|
|
|
packet->reportBufferLen = desc->InputLength;
|
2015-10-08 21:34:35 +02:00
|
|
|
|
2021-08-16 10:07:50 +02:00
|
|
|
if (!report_id)
|
2015-10-08 21:34:35 +02:00
|
|
|
{
|
2021-08-16 10:07:50 +02:00
|
|
|
packet->reportBuffer++;
|
|
|
|
packet->reportBufferLen--;
|
|
|
|
}
|
2021-08-05 10:36:06 +02:00
|
|
|
|
2021-09-10 09:22:45 +02:00
|
|
|
call_minidriver( IOCTL_HID_READ_REPORT, ext->u.pdo.parent_fdo, NULL, 0,
|
|
|
|
packet->reportBuffer, packet->reportBufferLen, &io );
|
2015-10-08 21:34:35 +02:00
|
|
|
|
2021-08-16 10:07:50 +02:00
|
|
|
if (io.Status == STATUS_SUCCESS)
|
|
|
|
{
|
|
|
|
if (!report_id) io.Information++;
|
2021-12-02 10:47:44 +01:00
|
|
|
if (!(report = find_report_with_type_and_id( ext, HidP_Input, buffer[0], FALSE )))
|
|
|
|
WARN( "dropping unknown input id %u\n", buffer[0] );
|
|
|
|
else if (!polled && io.Information < report->InputLength)
|
2022-02-01 09:35:48 +01:00
|
|
|
WARN( "dropping short report, len %Iu expected %u\n", io.Information, report->InputLength );
|
2021-12-02 10:47:44 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
packet->reportId = buffer[0];
|
|
|
|
packet->reportBuffer = buffer;
|
|
|
|
packet->reportBufferLen = io.Information;
|
2021-09-14 10:38:41 +02:00
|
|
|
hid_device_queue_input( device, packet );
|
2021-12-02 10:47:44 +01:00
|
|
|
}
|
2015-10-08 21:34:35 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 10:33:23 +02:00
|
|
|
res = WaitForSingleObject(ext->u.pdo.halt_event, polled ? ext->u.pdo.poll_interval : 0);
|
2021-08-16 10:07:50 +02:00
|
|
|
} while (res == WAIT_TIMEOUT);
|
|
|
|
|
2022-02-01 09:35:48 +01:00
|
|
|
TRACE( "device thread exiting, res %#lx\n", res );
|
2015-10-08 21:34:35 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HID_StartDeviceThread(DEVICE_OBJECT *device)
|
|
|
|
{
|
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2021-04-16 04:04:01 +02:00
|
|
|
ext->u.pdo.halt_event = CreateEventA(NULL, TRUE, FALSE, NULL);
|
|
|
|
ext->u.pdo.thread = CreateThread(NULL, 0, hid_device_thread, device, 0, NULL);
|
2015-10-08 21:34:35 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 18:55:18 +02:00
|
|
|
static void handle_IOCTL_HID_GET_COLLECTION_INFORMATION( IRP *irp, BASE_DEVICE_EXTENSION *ext )
|
2015-09-14 16:58:35 +02:00
|
|
|
{
|
|
|
|
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
|
|
|
|
if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(HID_COLLECTION_INFORMATION))
|
|
|
|
{
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_BUFFER_OVERFLOW;
|
2015-09-14 16:58:35 +02:00
|
|
|
irp->IoStatus.Information = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-16 04:04:01 +02:00
|
|
|
memcpy(irp->AssociatedIrp.SystemBuffer, &ext->u.pdo.information, sizeof(HID_COLLECTION_INFORMATION));
|
2015-09-14 16:58:35 +02:00
|
|
|
irp->IoStatus.Information = sizeof(HID_COLLECTION_INFORMATION);
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
2015-09-14 16:58:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:55:18 +02:00
|
|
|
static void handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR( IRP *irp, BASE_DEVICE_EXTENSION *ext )
|
2015-09-14 16:58:35 +02:00
|
|
|
{
|
2021-08-16 10:07:53 +02:00
|
|
|
HIDP_COLLECTION_DESC *desc = ext->u.pdo.device_desc.CollectionDesc;
|
2015-09-14 16:58:35 +02:00
|
|
|
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
|
|
|
|
|
2021-08-16 10:07:53 +02:00
|
|
|
if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < desc->PreparsedDataLength)
|
2015-09-14 16:58:35 +02:00
|
|
|
{
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_INVALID_BUFFER_SIZE;
|
2015-09-14 16:58:35 +02:00
|
|
|
irp->IoStatus.Information = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-16 10:07:53 +02:00
|
|
|
memcpy( irp->UserBuffer, desc->PreparsedData, desc->PreparsedDataLength );
|
|
|
|
irp->IoStatus.Information = desc->PreparsedDataLength;
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
2015-09-14 16:58:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 09:11:54 +02:00
|
|
|
struct device_strings
|
|
|
|
{
|
|
|
|
const WCHAR *id;
|
|
|
|
const WCHAR *product;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct device_strings device_strings[] =
|
|
|
|
{
|
2021-10-05 09:11:55 +02:00
|
|
|
/* Microsoft controllers */
|
2021-10-05 09:11:54 +02:00
|
|
|
{ .id = L"VID_045E&PID_028E", .product = L"Controller (XBOX 360 For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_028F", .product = L"Controller (XBOX 360 For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_02D1", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_02DD", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_02E3", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_02EA", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_02FD", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_0719", .product = L"Controller (XBOX 360 For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_0B00", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_0B05", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_0B12", .product = L"Controller (Xbox One For Windows)" },
|
|
|
|
{ .id = L"VID_045E&PID_0B13", .product = L"Controller (Xbox One For Windows)" },
|
2021-10-05 09:11:55 +02:00
|
|
|
/* Sony controllers */
|
|
|
|
{ .id = L"VID_054C&PID_05C4", .product = L"Wireless Controller" },
|
|
|
|
{ .id = L"VID_054C&PID_09CC", .product = L"Wireless Controller" },
|
|
|
|
{ .id = L"VID_054C&PID_0BA0", .product = L"Wireless Controller" },
|
|
|
|
{ .id = L"VID_054C&PID_0CE6", .product = L"Wireless Controller" },
|
2021-10-05 09:11:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const WCHAR *find_product_string( const WCHAR *device_id )
|
|
|
|
{
|
|
|
|
const WCHAR *match_id = wcsrchr( device_id, '\\' ) + 1;
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(device_strings); ++i)
|
|
|
|
if (!wcsnicmp( device_strings[i].id, match_id, 17 ))
|
|
|
|
return device_strings[i].product;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-05 09:11:53 +02:00
|
|
|
static void handle_minidriver_string( BASE_DEVICE_EXTENSION *ext, IRP *irp, ULONG index )
|
2015-09-14 16:58:35 +02:00
|
|
|
{
|
2021-10-05 09:11:53 +02:00
|
|
|
IO_STACK_LOCATION *stack = IoGetCurrentIrpStackLocation( irp );
|
|
|
|
WCHAR *output_buf = MmGetSystemAddressForMdlSafe( irp->MdlAddress, NormalPagePriority );
|
|
|
|
ULONG output_len = stack->Parameters.DeviceIoControl.OutputBufferLength;
|
2021-10-05 09:11:54 +02:00
|
|
|
const WCHAR *str = NULL;
|
2021-08-03 18:55:18 +02:00
|
|
|
|
2021-10-05 09:11:54 +02:00
|
|
|
if (index == HID_STRING_ID_IPRODUCT) str = find_product_string( ext->device_id );
|
|
|
|
|
|
|
|
if (!str) call_minidriver( IOCTL_HID_GET_STRING, ext->u.pdo.parent_fdo, ULongToPtr( index ),
|
|
|
|
sizeof(index), output_buf, output_len, &irp->IoStatus );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irp->IoStatus.Information = (wcslen( str ) + 1) * sizeof(WCHAR);
|
|
|
|
if (irp->IoStatus.Information > output_len)
|
|
|
|
irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( output_buf, str, irp->IoStatus.Information );
|
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 16:58:35 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 10:36:07 +02:00
|
|
|
static void hid_device_xfer_report( BASE_DEVICE_EXTENSION *ext, ULONG code, IRP *irp )
|
2016-01-11 16:18:38 +01:00
|
|
|
{
|
2021-08-05 10:36:07 +02:00
|
|
|
IO_STACK_LOCATION *stack = IoGetCurrentIrpStackLocation( irp );
|
2021-10-04 10:18:13 +02:00
|
|
|
ULONG offset = 0, report_len = 0, buffer_len = 0;
|
|
|
|
HIDP_REPORT_IDS *report = NULL;
|
2021-08-05 10:36:07 +02:00
|
|
|
HID_XFER_PACKET packet;
|
2021-08-06 09:49:34 +02:00
|
|
|
BYTE *buffer = NULL;
|
|
|
|
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case IOCTL_HID_GET_FEATURE:
|
|
|
|
case IOCTL_HID_GET_INPUT_REPORT:
|
|
|
|
buffer_len = stack->Parameters.DeviceIoControl.OutputBufferLength;
|
|
|
|
buffer = MmGetSystemAddressForMdlSafe( irp->MdlAddress, NormalPagePriority );
|
|
|
|
break;
|
2021-08-06 09:49:37 +02:00
|
|
|
case IOCTL_HID_SET_FEATURE:
|
2021-08-06 09:49:34 +02:00
|
|
|
case IOCTL_HID_SET_OUTPUT_REPORT:
|
|
|
|
buffer_len = stack->Parameters.DeviceIoControl.InputBufferLength;
|
|
|
|
buffer = irp->AssociatedIrp.SystemBuffer;
|
|
|
|
break;
|
2021-08-09 10:32:40 +02:00
|
|
|
case IOCTL_HID_WRITE_REPORT:
|
|
|
|
buffer_len = stack->Parameters.Write.Length;
|
|
|
|
buffer = irp->AssociatedIrp.SystemBuffer;
|
|
|
|
break;
|
2021-08-06 09:49:34 +02:00
|
|
|
}
|
2021-08-16 10:07:53 +02:00
|
|
|
if (!buffer || !buffer_len)
|
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_INVALID_USER_BUFFER;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:36:07 +02:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case IOCTL_HID_GET_INPUT_REPORT:
|
2021-10-04 10:18:13 +02:00
|
|
|
report = find_report_with_type_and_id( ext, HidP_Input, buffer[0], FALSE );
|
|
|
|
if (report) report_len = report->InputLength;
|
2021-08-05 10:36:07 +02:00
|
|
|
break;
|
2021-08-06 09:49:34 +02:00
|
|
|
case IOCTL_HID_SET_OUTPUT_REPORT:
|
2021-08-09 10:32:40 +02:00
|
|
|
case IOCTL_HID_WRITE_REPORT:
|
2021-10-04 10:18:13 +02:00
|
|
|
report = find_report_with_type_and_id( ext, HidP_Output, buffer[0], FALSE );
|
|
|
|
if (report) report_len = report->OutputLength;
|
2021-08-06 09:49:34 +02:00
|
|
|
break;
|
2021-08-05 10:36:07 +02:00
|
|
|
case IOCTL_HID_GET_FEATURE:
|
2021-08-06 09:49:37 +02:00
|
|
|
case IOCTL_HID_SET_FEATURE:
|
2021-10-04 10:18:13 +02:00
|
|
|
report = find_report_with_type_and_id( ext, HidP_Feature, buffer[0], FALSE );
|
|
|
|
if (report) report_len = report->FeatureLength;
|
2021-08-05 10:36:07 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-10-04 10:18:13 +02:00
|
|
|
|
|
|
|
if (!report || buffer_len < report_len)
|
2021-08-05 10:36:07 +02:00
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
|
2021-08-03 18:55:18 +02:00
|
|
|
return;
|
2021-06-07 11:11:37 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 10:18:13 +02:00
|
|
|
if (!report->ReportID) offset = 1;
|
|
|
|
packet.reportId = report->ReportID;
|
2021-09-13 10:48:58 +02:00
|
|
|
packet.reportBuffer = buffer + offset;
|
2016-01-11 16:18:38 +01:00
|
|
|
|
2021-08-06 09:49:35 +02:00
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case IOCTL_HID_GET_FEATURE:
|
|
|
|
case IOCTL_HID_GET_INPUT_REPORT:
|
2021-09-13 10:48:58 +02:00
|
|
|
packet.reportBufferLen = buffer_len - offset;
|
2021-08-06 09:49:35 +02:00
|
|
|
call_minidriver( code, ext->u.pdo.parent_fdo, NULL, 0, &packet, sizeof(packet), &irp->IoStatus );
|
|
|
|
break;
|
2021-08-06 09:49:37 +02:00
|
|
|
case IOCTL_HID_SET_FEATURE:
|
2021-08-06 09:49:35 +02:00
|
|
|
case IOCTL_HID_SET_OUTPUT_REPORT:
|
2021-08-09 10:32:40 +02:00
|
|
|
case IOCTL_HID_WRITE_REPORT:
|
2021-09-13 10:48:58 +02:00
|
|
|
packet.reportBufferLen = report_len - offset;
|
2021-08-06 09:49:35 +02:00
|
|
|
call_minidriver( code, ext->u.pdo.parent_fdo, NULL, sizeof(packet), &packet, 0, &irp->IoStatus );
|
2021-08-09 10:32:40 +02:00
|
|
|
if (code == IOCTL_HID_WRITE_REPORT && packet.reportId) irp->IoStatus.Information--;
|
2021-08-06 09:49:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-01-11 16:18:38 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:04:01 +02:00
|
|
|
NTSTATUS WINAPI pdo_ioctl(DEVICE_OBJECT *device, IRP *irp)
|
2015-09-14 16:58:35 +02:00
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
struct hid_queue *queue = irp->Tail.Overlay.OriginalFileObject->FsContext;
|
2015-09-14 16:58:35 +02:00
|
|
|
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation( irp );
|
2021-04-16 04:04:01 +02:00
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2021-08-03 18:55:18 +02:00
|
|
|
NTSTATUS status;
|
2021-07-01 18:53:25 +02:00
|
|
|
BOOL removed;
|
2021-08-05 10:36:07 +02:00
|
|
|
ULONG code;
|
2021-07-01 18:53:25 +02:00
|
|
|
KIRQL irql;
|
2015-09-14 16:58:35 +02:00
|
|
|
|
|
|
|
irp->IoStatus.Information = 0;
|
|
|
|
|
2022-02-01 09:35:48 +01:00
|
|
|
TRACE( "device %p code %#lx\n", device, irpsp->Parameters.DeviceIoControl.IoControlCode );
|
2015-09-14 16:58:35 +02:00
|
|
|
|
2021-07-01 18:53:25 +02:00
|
|
|
KeAcquireSpinLock(&ext->u.pdo.lock, &irql);
|
|
|
|
removed = ext->u.pdo.removed;
|
|
|
|
KeReleaseSpinLock(&ext->u.pdo.lock, irql);
|
|
|
|
|
|
|
|
if (removed)
|
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_DELETE_PENDING;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
return STATUS_DELETE_PENDING;
|
|
|
|
}
|
|
|
|
|
2021-08-05 10:36:07 +02:00
|
|
|
switch ((code = irpsp->Parameters.DeviceIoControl.IoControlCode))
|
2015-09-14 16:58:35 +02:00
|
|
|
{
|
|
|
|
case IOCTL_HID_GET_POLL_FREQUENCY_MSEC:
|
|
|
|
TRACE("IOCTL_HID_GET_POLL_FREQUENCY_MSEC\n");
|
|
|
|
if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(ULONG))
|
|
|
|
{
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_BUFFER_OVERFLOW;
|
2015-09-14 16:58:35 +02:00
|
|
|
irp->IoStatus.Information = 0;
|
|
|
|
break;
|
|
|
|
}
|
2021-04-16 04:04:01 +02:00
|
|
|
*(ULONG *)irp->AssociatedIrp.SystemBuffer = ext->u.pdo.poll_interval;
|
2015-09-14 16:58:35 +02:00
|
|
|
irp->IoStatus.Information = sizeof(ULONG);
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
case IOCTL_HID_SET_POLL_FREQUENCY_MSEC:
|
|
|
|
{
|
|
|
|
ULONG poll_interval;
|
|
|
|
TRACE("IOCTL_HID_SET_POLL_FREQUENCY_MSEC\n");
|
|
|
|
if (irpsp->Parameters.DeviceIoControl.InputBufferLength < sizeof(ULONG))
|
|
|
|
{
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
poll_interval = *(ULONG *)irp->AssociatedIrp.SystemBuffer;
|
2021-08-16 10:07:48 +02:00
|
|
|
if (poll_interval) ext->u.pdo.poll_interval = min(poll_interval, MAX_POLL_INTERVAL_MSEC);
|
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IOCTL_HID_GET_PRODUCT_STRING:
|
|
|
|
{
|
2021-08-03 18:55:18 +02:00
|
|
|
handle_minidriver_string( ext, irp, HID_STRING_ID_IPRODUCT );
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-10-29 20:59:17 +01:00
|
|
|
case IOCTL_HID_GET_SERIALNUMBER_STRING:
|
|
|
|
{
|
2021-08-03 18:55:18 +02:00
|
|
|
handle_minidriver_string( ext, irp, HID_STRING_ID_ISERIALNUMBER );
|
2015-10-29 20:59:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2015-09-14 16:58:35 +02:00
|
|
|
case IOCTL_HID_GET_MANUFACTURER_STRING:
|
|
|
|
{
|
2021-08-03 18:55:18 +02:00
|
|
|
handle_minidriver_string( ext, irp, HID_STRING_ID_IMANUFACTURER );
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IOCTL_HID_GET_COLLECTION_INFORMATION:
|
|
|
|
{
|
2021-08-03 18:55:18 +02:00
|
|
|
handle_IOCTL_HID_GET_COLLECTION_INFORMATION( irp, ext );
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IOCTL_HID_GET_COLLECTION_DESCRIPTOR:
|
|
|
|
{
|
2021-08-03 18:55:18 +02:00
|
|
|
handle_IOCTL_HID_GET_COLLECTION_DESCRIPTOR( irp, ext );
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-10-08 21:35:27 +02:00
|
|
|
case IOCTL_SET_NUM_DEVICE_INPUT_BUFFERS:
|
|
|
|
{
|
|
|
|
irp->IoStatus.Information = 0;
|
|
|
|
|
|
|
|
if (irpsp->Parameters.DeviceIoControl.InputBufferLength != sizeof(ULONG))
|
2021-08-03 18:55:18 +02:00
|
|
|
irp->IoStatus.Status = STATUS_BUFFER_OVERFLOW;
|
2015-10-08 21:35:27 +02:00
|
|
|
else
|
2021-09-24 10:07:13 +02:00
|
|
|
irp->IoStatus.Status = hid_queue_resize( queue, *(ULONG *)irp->AssociatedIrp.SystemBuffer );
|
2015-10-08 21:35:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IOCTL_GET_NUM_DEVICE_INPUT_BUFFERS:
|
|
|
|
{
|
|
|
|
if (irpsp->Parameters.DeviceIoControl.OutputBufferLength < sizeof(ULONG))
|
|
|
|
{
|
2021-08-02 12:25:22 +02:00
|
|
|
irp->IoStatus.Information = 0;
|
2021-08-03 18:55:18 +02:00
|
|
|
irp->IoStatus.Status = STATUS_BUFFER_TOO_SMALL;
|
2015-10-08 21:35:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-10 11:31:17 +02:00
|
|
|
*(ULONG *)irp->AssociatedIrp.SystemBuffer = queue->length;
|
2021-08-02 12:25:22 +02:00
|
|
|
irp->IoStatus.Information = sizeof(ULONG);
|
2021-08-03 18:55:18 +02:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
2015-10-08 21:35:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-01-11 16:18:38 +01:00
|
|
|
case IOCTL_HID_GET_FEATURE:
|
2021-08-06 09:49:37 +02:00
|
|
|
case IOCTL_HID_SET_FEATURE:
|
2021-08-05 10:36:07 +02:00
|
|
|
case IOCTL_HID_GET_INPUT_REPORT:
|
2021-08-06 09:49:34 +02:00
|
|
|
case IOCTL_HID_SET_OUTPUT_REPORT:
|
2021-08-05 10:36:07 +02:00
|
|
|
hid_device_xfer_report( ext, code, irp );
|
2016-01-11 16:18:38 +01:00
|
|
|
break;
|
2015-09-14 16:58:35 +02:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
ULONG code = irpsp->Parameters.DeviceIoControl.IoControlCode;
|
2022-02-01 09:35:48 +01:00
|
|
|
FIXME( "Unsupported ioctl %#lx (device=%lx access=%lx func=%lx method=%lx)\n", code,
|
|
|
|
code >> 16, (code >> 14) & 3, (code >> 2) & 0xfff, code & 3 );
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_NOT_SUPPORTED;
|
2015-09-14 16:58:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 18:55:18 +02:00
|
|
|
status = irp->IoStatus.Status;
|
|
|
|
if (status != STATUS_PENDING) IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return status;
|
2015-09-14 16:58:35 +02:00
|
|
|
}
|
2015-09-14 16:58:39 +02:00
|
|
|
|
2021-04-16 04:04:01 +02:00
|
|
|
NTSTATUS WINAPI pdo_read(DEVICE_OBJECT *device, IRP *irp)
|
2015-09-14 16:58:44 +02:00
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
struct hid_queue *queue = irp->Tail.Overlay.OriginalFileObject->FsContext;
|
2015-09-14 16:58:44 +02:00
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2021-08-16 10:07:53 +02:00
|
|
|
HIDP_COLLECTION_DESC *desc = ext->u.pdo.device_desc.CollectionDesc;
|
2017-11-07 17:08:10 +01:00
|
|
|
IO_STACK_LOCATION *irpsp = IoGetCurrentIrpStackLocation(irp);
|
2021-08-10 11:31:17 +02:00
|
|
|
struct hid_report *report;
|
2021-07-01 18:53:25 +02:00
|
|
|
BOOL removed;
|
|
|
|
KIRQL irql;
|
|
|
|
|
|
|
|
KeAcquireSpinLock(&ext->u.pdo.lock, &irql);
|
|
|
|
removed = ext->u.pdo.removed;
|
|
|
|
KeReleaseSpinLock(&ext->u.pdo.lock, irql);
|
|
|
|
|
|
|
|
if (removed)
|
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_DELETE_PENDING;
|
|
|
|
IoCompleteRequest(irp, IO_NO_INCREMENT);
|
|
|
|
return STATUS_DELETE_PENDING;
|
|
|
|
}
|
2015-09-14 16:58:44 +02:00
|
|
|
|
2021-08-16 10:07:53 +02:00
|
|
|
if (irpsp->Parameters.Read.Length < desc->InputLength)
|
2021-07-06 11:00:52 +02:00
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_INVALID_BUFFER_SIZE;
|
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return STATUS_INVALID_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
2015-09-14 16:58:44 +02:00
|
|
|
irp->IoStatus.Information = 0;
|
2021-09-24 10:07:13 +02:00
|
|
|
if ((report = hid_queue_pop_report( queue )))
|
2015-09-14 16:58:44 +02:00
|
|
|
{
|
2021-10-04 10:18:12 +02:00
|
|
|
memcpy( irp->AssociatedIrp.SystemBuffer, report->buffer, report->length );
|
2021-08-10 11:31:17 +02:00
|
|
|
irp->IoStatus.Information = report->length;
|
2021-08-05 10:36:05 +02:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
2021-08-10 11:31:17 +02:00
|
|
|
hid_report_decref( report );
|
2021-08-05 10:36:06 +02:00
|
|
|
|
2021-09-24 10:07:12 +02:00
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return STATUS_SUCCESS;
|
2015-09-14 16:58:44 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
return hid_queue_push_irp( queue, irp );
|
2021-09-24 10:07:12 +02:00
|
|
|
|
2015-09-14 16:58:44 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:04:01 +02:00
|
|
|
NTSTATUS WINAPI pdo_write(DEVICE_OBJECT *device, IRP *irp)
|
2015-10-08 21:34:49 +02:00
|
|
|
{
|
2019-05-14 12:52:58 +02:00
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2021-08-03 18:55:18 +02:00
|
|
|
NTSTATUS status;
|
2016-01-11 15:40:07 +01:00
|
|
|
|
2021-08-09 10:32:40 +02:00
|
|
|
hid_device_xfer_report( ext, IOCTL_HID_WRITE_REPORT, irp );
|
2015-10-08 21:34:49 +02:00
|
|
|
|
2021-08-03 18:55:18 +02:00
|
|
|
status = irp->IoStatus.Status;
|
2015-10-08 21:34:49 +02:00
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
2021-08-03 18:55:18 +02:00
|
|
|
return status;
|
2015-10-08 21:34:49 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 04:04:01 +02:00
|
|
|
NTSTATUS WINAPI pdo_create(DEVICE_OBJECT *device, IRP *irp)
|
2015-09-14 16:58:39 +02:00
|
|
|
{
|
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2021-09-24 10:07:13 +02:00
|
|
|
struct hid_queue *queue;
|
2021-09-24 10:07:11 +02:00
|
|
|
BOOL removed;
|
2021-08-10 11:31:15 +02:00
|
|
|
KIRQL irql;
|
2015-09-14 16:58:39 +02:00
|
|
|
|
|
|
|
TRACE("Open handle on device %p\n", device);
|
2021-08-10 11:31:15 +02:00
|
|
|
|
2021-09-24 10:07:11 +02:00
|
|
|
KeAcquireSpinLock( &ext->u.pdo.lock, &irql );
|
|
|
|
removed = ext->u.pdo.removed;
|
|
|
|
KeReleaseSpinLock( &ext->u.pdo.lock, irql );
|
|
|
|
|
|
|
|
if (removed)
|
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_DELETE_PENDING;
|
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return STATUS_DELETE_PENDING;
|
|
|
|
}
|
|
|
|
|
2021-09-24 10:07:13 +02:00
|
|
|
if (!(queue = hid_queue_create())) irp->IoStatus.Status = STATUS_NO_MEMORY;
|
2021-08-10 11:31:15 +02:00
|
|
|
else
|
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
KeAcquireSpinLock( &ext->u.pdo.queues_lock, &irql );
|
|
|
|
list_add_tail( &ext->u.pdo.queues, &queue->entry );
|
|
|
|
KeReleaseSpinLock( &ext->u.pdo.queues_lock, irql );
|
2021-08-10 11:31:15 +02:00
|
|
|
|
|
|
|
irp->Tail.Overlay.OriginalFileObject->FsContext = queue;
|
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-09-14 16:58:39 +02:00
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-04-16 04:04:01 +02:00
|
|
|
NTSTATUS WINAPI pdo_close(DEVICE_OBJECT *device, IRP *irp)
|
2015-09-14 16:58:39 +02:00
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
struct hid_queue *queue = irp->Tail.Overlay.OriginalFileObject->FsContext;
|
2015-09-14 16:58:39 +02:00
|
|
|
BASE_DEVICE_EXTENSION *ext = device->DeviceExtension;
|
2021-09-24 10:07:11 +02:00
|
|
|
BOOL removed;
|
2021-08-10 11:31:15 +02:00
|
|
|
KIRQL irql;
|
|
|
|
|
2015-09-14 16:58:39 +02:00
|
|
|
TRACE("Close handle on device %p\n", device);
|
2021-08-10 11:31:15 +02:00
|
|
|
|
2021-09-24 10:07:11 +02:00
|
|
|
KeAcquireSpinLock( &ext->u.pdo.lock, &irql );
|
|
|
|
removed = ext->u.pdo.removed;
|
|
|
|
KeReleaseSpinLock( &ext->u.pdo.lock, irql );
|
|
|
|
|
|
|
|
if (removed)
|
|
|
|
{
|
|
|
|
irp->IoStatus.Status = STATUS_DELETE_PENDING;
|
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return STATUS_DELETE_PENDING;
|
|
|
|
}
|
|
|
|
|
2021-08-10 11:31:15 +02:00
|
|
|
if (queue)
|
|
|
|
{
|
2021-09-24 10:07:13 +02:00
|
|
|
KeAcquireSpinLock( &ext->u.pdo.queues_lock, &irql );
|
2021-08-10 11:31:15 +02:00
|
|
|
list_remove( &queue->entry );
|
2021-09-24 10:07:13 +02:00
|
|
|
KeReleaseSpinLock( &ext->u.pdo.queues_lock, irql );
|
|
|
|
hid_queue_destroy( queue );
|
2021-08-10 11:31:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-04 11:30:32 +02:00
|
|
|
irp->IoStatus.Status = STATUS_SUCCESS;
|
2015-09-14 16:58:39 +02:00
|
|
|
IoCompleteRequest( irp, IO_NO_INCREMENT );
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|