diff --git a/src/MagicTrackpad2PtpDevice/AppleDefinition.h b/src/MagicTrackpad2PtpDevice/AppleDefinition.h index c8f90a9..a3d29f1 100644 --- a/src/MagicTrackpad2PtpDevice/AppleDefinition.h +++ b/src/MagicTrackpad2PtpDevice/AppleDefinition.h @@ -1,7 +1,5 @@ #pragma once -#include "Types.h" - #define USB_VENDOR_ID_APPLE 0x05ac /* MacbookAir, aka wellspring */ @@ -61,10 +59,10 @@ /* button data structure */ struct TRACKPAD_BUTTON_DATA { - u8 unknown1; /* constant */ - u8 button; /* left button */ - u8 rel_x; /* relative x coordinate */ - u8 rel_y; /* relative y coordinate */ + UCHAR unknown1; /* constant */ + UCHAR button; /* left button */ + UCHAR rel_x; /* relative x coordinate */ + UCHAR rel_y; /* relative y coordinate */ }; /* trackpad header types */ diff --git a/src/MagicTrackpad2PtpDevice/Device.c b/src/MagicTrackpad2PtpDevice/Device.c index 86bcf63..fbe3389 100644 --- a/src/MagicTrackpad2PtpDevice/Device.c +++ b/src/MagicTrackpad2PtpDevice/Device.c @@ -5,17 +5,15 @@ _IRQL_requires_(PASSIVE_LEVEL) static const struct BCM5974_CONFIG* -MagicTrackpad2GetConfig( +AmtPtpGetDeviceConfig( _In_ USB_DEVICE_DESCRIPTOR deviceInfo ) { - u16 id = deviceInfo.idProduct; + USHORT id = deviceInfo.idProduct; const struct BCM5974_CONFIG *cfg; - for (cfg = Bcm5974ConfigTable; cfg->ansi; ++cfg) - { - if (cfg->ansi == id || cfg->iso == id || cfg->jis == id) - { + for (cfg = Bcm5974ConfigTable; cfg->ansi; ++cfg) { + if (cfg->ansi == id || cfg->iso == id || cfg->jis == id) { return cfg; } } @@ -39,7 +37,11 @@ AmtPtpCreateDevice( UNREFERENCED_PARAMETER(Driver); PAGED_CODE(); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); // Initialize Power Callback WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpPowerCallbacks); @@ -53,10 +55,15 @@ AmtPtpCreateDevice( // Create WDF device object WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, DEVICE_CONTEXT); - status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device); + status = WdfDeviceCreate( + &DeviceInit, + &deviceAttributes, + &device + ); + if (!NT_SUCCESS(status)) { TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, - "WdfDeviceCreate failed with Status code %!STATUS!\n", status); + "%!FUNC! WdfDeviceCreate failed with Status code %!STATUS!", status); return status; } @@ -78,7 +85,10 @@ AmtPtpCreateDevice( // WDF_DEVICE_PNP_CAPABILITIES_INIT(&pnpCaps); pnpCaps.SurpriseRemovalOK = WdfTrue; - WdfDeviceSetPnpCapabilities(device, &pnpCaps); + WdfDeviceSetPnpCapabilities( + device, + &pnpCaps + ); // // Create a device interface so that applications can find and talk @@ -97,7 +107,11 @@ AmtPtpCreateDevice( status = MagicTrackpad2PtpDeviceQueueInitialize(device); } - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; } @@ -121,7 +135,11 @@ AmtPtpEvtDevicePrepareHardware( UNREFERENCED_PARAMETER(ResourceListTranslated); PAGED_CODE(); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); status = STATUS_SUCCESS; pDeviceContext = DeviceGetContext(Device); @@ -129,21 +147,36 @@ AmtPtpEvtDevicePrepareHardware( if (pDeviceContext->UsbDevice == NULL) { status = WdfUsbTargetDeviceCreate(Device, WDF_NO_OBJECT_ATTRIBUTES, - &pDeviceContext->UsbDevice); + &pDeviceContext->UsbDevice + ); + if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, - "WdfUsbTargetDeviceCreate failed with Status code %!STATUS!\n", status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfUsbTargetDeviceCreate failed with Status code %!STATUS!", + status + ); return status; } } // Retrieve device information - WdfUsbTargetDeviceGetDeviceDescriptor(pDeviceContext->UsbDevice, &pDeviceContext->DeviceDescriptor); + WdfUsbTargetDeviceGetDeviceDescriptor( + pDeviceContext->UsbDevice, + &pDeviceContext->DeviceDescriptor + ); + if (NT_SUCCESS(status)) { // Get correct configuration from conf store - pDeviceContext->DeviceInfo = MagicTrackpad2GetConfig(pDeviceContext->DeviceDescriptor); + pDeviceContext->DeviceInfo = AmtPtpGetDeviceConfig(pDeviceContext->DeviceDescriptor); if (pDeviceContext->DeviceInfo == NULL) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "%!FUNC! failed with %!STATUS!\n", status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DEVICE, + "%!FUNC! failed with %!STATUS!", + status + ); return status; } @@ -164,11 +197,15 @@ AmtPtpEvtDevicePrepareHardware( (pDeviceContext->DeviceInfo->w.max - pDeviceContext->DeviceInfo->w.min) / pDeviceContext->DeviceInfo->w.snratio : 0.0; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "%!FUNC! fuzz information: h = %f, v = %f, p = %f, w = %f", + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DEVICE, + "%!FUNC! fuzz information: h = %f, v = %f, p = %f, w = %f", pDeviceContext->HorizonalFuzz, pDeviceContext->VerticalFuzz, pDeviceContext->PressureFuzz, - pDeviceContext->WidthFuzz); + pDeviceContext->WidthFuzz + ); } // @@ -176,44 +213,52 @@ AmtPtpEvtDevicePrepareHardware( // capabilites such as speed, power, etc. // WDF_USB_DEVICE_INFORMATION_INIT(&deviceInfo); - status = WdfUsbTargetDeviceRetrieveInformation(pDeviceContext->UsbDevice, &deviceInfo); + status = WdfUsbTargetDeviceRetrieveInformation( + pDeviceContext->UsbDevice, + &deviceInfo + ); - if (NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "IsDeviceHighSpeed: %s\n", + if (NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DEVICE, + "%!FUNC! IsDeviceHighSpeed: %s", (deviceInfo.Traits & WDF_USB_DEVICE_TRAIT_AT_HIGH_SPEED) ? "TRUE" : "FALSE"); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, - "IsDeviceSelfPowered: %s\n", + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DEVICE, + "%!FUNC! IsDeviceSelfPowered: %s", (deviceInfo.Traits & WDF_USB_DEVICE_TRAIT_SELF_POWERED) ? "TRUE" : "FALSE"); waitWakeEnable = deviceInfo.Traits & WDF_USB_DEVICE_TRAIT_REMOTE_WAKE_CAPABLE; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, - "IsDeviceRemoteWakeable: %s\n", + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DEVICE, + "%!FUNC! IsDeviceRemoteWakeable: %s", waitWakeEnable ? "TRUE" : "FALSE"); // // Save these for use later. // pDeviceContext->UsbDeviceTraits = deviceInfo.Traits; - } - else - { + } else { pDeviceContext->UsbDeviceTraits = 0; } // Select interface to use status = SelectInterruptInterface(Device); if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "SelectInterruptInterface failed 0x%x\n", status); + TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "%!FUNC! SelectInterruptInterface failed with %!STATUS!", status); return status; } // Set up interrupt status = AmtPtpConfigContReaderForInterruptEndPoint(pDeviceContext); if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "MagicTrackpad2PtpDeviceConfigContReaderForInterruptEndPoint failed 0x%x\n", status); + TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "%!FUNC! AmtPtpConfigContReaderForInterruptEndPoint failed with %!STATUS!", status); return status; } @@ -232,61 +277,119 @@ AmtPtpSetWellspringMode( _In_ BOOL IsWellspringModeOn ) { - - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC!: entry"); - NTSTATUS status; WDF_USB_CONTROL_SETUP_PACKET setupPacket; - WDF_MEMORY_DESCRIPTOR memoryDescriptor; ULONG cbTransferred; - WDFMEMORY bufHandle = NULL; unsigned char* buffer; - status = WdfMemoryCreate(WDF_NO_OBJECT_ATTRIBUTES, PagedPool, 0, DeviceContext->DeviceInfo->um_size, &bufHandle, &buffer); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); + + status = WdfMemoryCreate( + WDF_NO_OBJECT_ATTRIBUTES, + PagedPool, + 0, + DeviceContext->DeviceInfo->um_size, + &bufHandle, + &buffer + ); + if (!NT_SUCCESS(status)) { goto cleanup; } - RtlZeroMemory(buffer, DeviceContext->DeviceInfo->um_size); + RtlZeroMemory( + buffer, + DeviceContext->DeviceInfo->um_size + ); - WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(&memoryDescriptor, buffer, sizeof(DeviceContext->DeviceInfo->um_size)); + WDF_MEMORY_DESCRIPTOR_INIT_BUFFER( + &memoryDescriptor, + buffer, + sizeof(DeviceContext->DeviceInfo->um_size) + ); - WDF_USB_CONTROL_SETUP_PACKET_INIT(&setupPacket, BmRequestDeviceToHost, BmRequestToInterface, + WDF_USB_CONTROL_SETUP_PACKET_INIT( + &setupPacket, + BmRequestDeviceToHost, + BmRequestToInterface, BCM5974_WELLSPRING_MODE_READ_REQUEST_ID, - (USHORT)DeviceContext->DeviceInfo->um_req_val, (USHORT)DeviceContext->DeviceInfo->um_req_idx); + (USHORT) DeviceContext->DeviceInfo->um_req_val, + (USHORT) DeviceContext->DeviceInfo->um_req_idx + ); // Set stuffs right setupPacket.Packet.bm.Request.Type = BmRequestClass; - status = WdfUsbTargetDeviceSendControlTransferSynchronously(DeviceContext->UsbDevice, WDF_NO_HANDLE, NULL, &setupPacket, &memoryDescriptor, &cbTransferred); - if (!NT_SUCCESS(status) || cbTransferred != (ULONG)DeviceContext->DeviceInfo->um_size) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "WdfUsbTargetDeviceSendControlTransferSynchronously (Read) failed 0x%x\n", status); + status = WdfUsbTargetDeviceSendControlTransferSynchronously( + DeviceContext->UsbDevice, + WDF_NO_HANDLE, + NULL, + &setupPacket, + &memoryDescriptor, + &cbTransferred + ); + + if (!NT_SUCCESS(status) || cbTransferred != (ULONG) DeviceContext->DeviceInfo->um_size) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DEVICE, + "%!FUNC! WdfUsbTargetDeviceSendControlTransferSynchronously (Read) failed with %!STATUS!", + status + ); goto cleanup; } // Apply the mode switch buffer[DeviceContext->DeviceInfo->um_switch_idx] = IsWellspringModeOn ? - (unsigned char)DeviceContext->DeviceInfo->um_switch_on : (unsigned char)DeviceContext->DeviceInfo->um_switch_off; + (unsigned char)DeviceContext->DeviceInfo->um_switch_on : + (unsigned char)DeviceContext->DeviceInfo->um_switch_off; // Write configuration - WDF_USB_CONTROL_SETUP_PACKET_INIT(&setupPacket, BmRequestHostToDevice, BmRequestToInterface, + WDF_USB_CONTROL_SETUP_PACKET_INIT( + &setupPacket, + BmRequestHostToDevice, + BmRequestToInterface, BCM5974_WELLSPRING_MODE_WRITE_REQUEST_ID, - (USHORT)DeviceContext->DeviceInfo->um_req_val, (USHORT)DeviceContext->DeviceInfo->um_req_idx); + (USHORT) DeviceContext->DeviceInfo->um_req_val, + (USHORT) DeviceContext->DeviceInfo->um_req_idx + ); // Set stuffs right setupPacket.Packet.bm.Request.Type = BmRequestClass; - status = WdfUsbTargetDeviceSendControlTransferSynchronously(DeviceContext->UsbDevice, WDF_NO_HANDLE, NULL, &setupPacket, &memoryDescriptor, &cbTransferred); - if (!NT_SUCCESS(status) /* || cbTransferred != (ULONG) DeviceContext->DeviceInfo->um_size */) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "WdfUsbTargetDeviceSendControlTransferSynchronously (Write) failed 0x%x\n", status); + + status = WdfUsbTargetDeviceSendControlTransferSynchronously( + DeviceContext->UsbDevice, + WDF_NO_HANDLE, + NULL, + &setupPacket, + &memoryDescriptor, + &cbTransferred + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DEVICE, + "%!FUNC! WdfUsbTargetDeviceSendControlTransferSynchronously (Write) failed with %!STATUS!", + status + ); goto cleanup; } // Set status DeviceContext->IsWellspringModeOn = IsWellspringModeOn; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC!: exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); cleanup: bufHandle = NULL; @@ -308,8 +411,12 @@ AmtPtpEvtDeviceD0Entry( pDeviceContext = DeviceGetContext(Device); isTargetStarted = FALSE; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "-->MagicTrackpad2PtpDeviceEvtDeviceD0Entry - coming from %s\n", - DbgDevicePowerString(PreviousState)); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! -->MagicTrackpad2PtpDeviceEvtDeviceD0Entry - coming from %s", + DbgDevicePowerString(PreviousState) + ); // // Since continuous reader is configured for this interrupt-pipe, we must explicitly start @@ -317,7 +424,12 @@ AmtPtpEvtDeviceD0Entry( // status = WdfIoTargetStart(WdfUsbTargetPipeGetIoTarget(pDeviceContext->InterruptPipe)); if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "Failed to start interrupt pipe %!STATUS!\n", status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! Failed to start interrupt pipe %!STATUS!", + status + ); goto End; } @@ -331,11 +443,18 @@ AmtPtpEvtDeviceD0Entry( // reader in preparation for the ensuing remove. // if (isTargetStarted) { - WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(pDeviceContext->InterruptPipe), WdfIoTargetCancelSentIo); + WdfIoTargetStop( + WdfUsbTargetPipeGetIoTarget(pDeviceContext->InterruptPipe), + WdfIoTargetCancelSentIo + ); } } - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "<--MagicTrackpad2PtpDeviceEvtDeviceD0Entry\n"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! <--MagicTrackpad2PtpDeviceEvtDeviceD0Entry" + ); return status; } @@ -348,14 +467,27 @@ AmtPtpEvtDeviceD0Exit( ) { PDEVICE_CONTEXT pDeviceContext; + PAGED_CODE(); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, - "-->MagicTrackpad2PtpDeviceEvtDeviceD0Exit - moving to %s\n", DbgDevicePowerString(TargetState)); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! -->MagicTrackpad2PtpDeviceEvtDeviceD0Exit - moving to %s", + DbgDevicePowerString(TargetState) + ); pDeviceContext = DeviceGetContext(Device); - WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget(pDeviceContext->InterruptPipe), WdfIoTargetCancelSentIo); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "<--MagicTrackpad2PtpDeviceEvtDeviceD0Exit\n"); + WdfIoTargetStop(WdfUsbTargetPipeGetIoTarget( + pDeviceContext->InterruptPipe), + WdfIoTargetCancelSentIo + ); + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! <--MagicTrackpad2PtpDeviceEvtDeviceD0Exit" + ); return STATUS_SUCCESS; } @@ -378,16 +510,20 @@ SelectInterruptInterface( PAGED_CODE(); pDeviceContext = DeviceGetContext(Device); - WDF_USB_DEVICE_SELECT_CONFIG_PARAMS_INIT_SINGLE_INTERFACE(&configParams); - - usbInterface = WdfUsbTargetDeviceGetInterface(pDeviceContext->UsbDevice, 0); + usbInterface = WdfUsbTargetDeviceGetInterface( + pDeviceContext->UsbDevice, + 0 + ); if (NULL == usbInterface) { status = STATUS_UNSUCCESSFUL; - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, - "WdfUsbTargetDeviceGetInterface 0 failed %!STATUS! \n", - status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DEVICE, + "%!FUNC! WdfUsbTargetDeviceGetInterface 0 failed %!STATUS!", + status + ); return status; } @@ -395,7 +531,6 @@ SelectInterruptInterface( configParams.Types.SingleInterface.NumberConfiguredPipes = WdfUsbInterfaceGetNumConfiguredPipes(usbInterface); pDeviceContext->UsbInterface = configParams.Types.SingleInterface.ConfiguredUsbInterface; - numberConfiguredPipes = configParams.Types.SingleInterface.NumberConfiguredPipes; // @@ -418,9 +553,7 @@ SelectInterruptInterface( WdfUsbTargetPipeSetNoMaximumPacketSizeCheck(pipe); if (WdfUsbPipeTypeInterrupt == pipeInfo.PipeType) { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DEVICE, "Interrupt Pipe is 0x%p\n", pipe); pDeviceContext->InterruptPipe = pipe; - // One interrupt is enough break; } @@ -431,7 +564,12 @@ SelectInterruptInterface( // if (!pDeviceContext->InterruptPipe) { status = STATUS_INVALID_DEVICE_STATE; - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DEVICE, "Device is not configured properly %!STATUS!\n", status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DEVICE, + "%!FUNC! Device is not configured properly %!STATUS!", + status + ); return status; } diff --git a/src/MagicTrackpad2PtpDevice/Driver.c b/src/MagicTrackpad2PtpDevice/Driver.c index 9c529ca..7cd298d 100644 --- a/src/MagicTrackpad2PtpDevice/Driver.c +++ b/src/MagicTrackpad2PtpDevice/Driver.c @@ -17,9 +17,16 @@ DriverEntry( // // Initialize WPP Tracing // - WPP_INIT_TRACING(DriverObject, RegistryPath); + WPP_INIT_TRACING( + DriverObject, + RegistryPath + ); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); // // Register a cleanup callback so that we can call WPP_CLEANUP when @@ -40,12 +47,21 @@ DriverEntry( ); if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfDriverCreate failed %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfDriverCreate failed %!STATUS!", + status + ); WPP_CLEANUP(DriverObject); return status; } - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; } @@ -58,15 +74,30 @@ MagicTrackpad2PtpDeviceEvtDeviceAdd( { NTSTATUS status = STATUS_SUCCESS; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "Set FDO driver filter\n"); - WdfFdoInitSetFilter(DeviceInit); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Set FDO driver filter" + ); - status = AmtPtpCreateDevice(Driver, DeviceInit); + WdfFdoInitSetFilter(DeviceInit); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + status = AmtPtpCreateDevice( + Driver, + DeviceInit + ); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; } @@ -79,7 +110,11 @@ MagicTrackpad2PtpDeviceEvtDriverContextCleanup( UNREFERENCED_PARAMETER(DriverObject); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); // // Stop WPP Tracing diff --git a/src/MagicTrackpad2PtpDevice/Driver.h b/src/MagicTrackpad2PtpDevice/Driver.h index 9f8e557..4b3859d 100644 --- a/src/MagicTrackpad2PtpDevice/Driver.h +++ b/src/MagicTrackpad2PtpDevice/Driver.h @@ -7,11 +7,11 @@ #include #include +#include "Trace.h" #include "AppleDefinition.h" #include "Hid.h" -#include "device.h" -#include "queue.h" -#include "trace.h" +#include "Device.h" +#include "Queue.h" EXTERN_C_START diff --git a/src/MagicTrackpad2PtpDevice/Hid.c b/src/MagicTrackpad2PtpDevice/Hid.c index 7906c4a..3adfede 100644 --- a/src/MagicTrackpad2PtpDevice/Hid.c +++ b/src/MagicTrackpad2PtpDevice/Hid.c @@ -31,42 +31,77 @@ MagicTrackpad2GetHidDescriptor( _In_ WDFREQUEST Request ) { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); NTSTATUS status = STATUS_SUCCESS; PDEVICE_CONTEXT pContext = DeviceGetContext(Device); size_t szCopy = 0; WDFMEMORY reqMemory; - status = WdfRequestRetrieveOutputMemory(Request, &reqMemory); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfRequestRetrieveOutputBuffer failed with %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); + + status = WdfRequestRetrieveOutputMemory( + Request, + &reqMemory + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveOutputBuffer failed with %!STATUS!", + status + ); return status; } - if (pContext->DeviceDescriptor.idProduct == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) - { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "Request HID Report Descriptor for AAPL Magic Trackpad 2"); - szCopy = AAPLMagicTrackpad2DefaultHidDescriptor.bLength; + if (pContext->DeviceDescriptor.idProduct == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Request HID Report Descriptor for AAPL Magic Trackpad 2" + ); - status = WdfMemoryCopyFromBuffer(reqMemory, 0, (PVOID)&AAPLMagicTrackpad2DefaultHidDescriptor, szCopy); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfMemoryCopyFromBuffer failed with %!STATUS!", status); + szCopy = AAPLMagicTrackpad2DefaultHidDescriptor.bLength; + status = WdfMemoryCopyFromBuffer( + reqMemory, + 0, + (PVOID) &AAPLMagicTrackpad2DefaultHidDescriptor, + szCopy + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfMemoryCopyFromBuffer failed with %!STATUS!", + status + ); return status; } - WdfRequestSetInformation(Request, szCopy); - } - else - { - TraceEvents(TRACE_LEVEL_WARNING, TRACE_DRIVER, "Device HID registry is not found"); + WdfRequestSetInformation( + Request, + szCopy + ); + } else { + TraceEvents( + TRACE_LEVEL_WARNING, + TRACE_DRIVER, + "%!FUNC! Device HID registry is not found" + ); status = STATUS_INVALID_DEVICE_STATE; return status; } - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; } @@ -79,18 +114,29 @@ AmtPtpGetDeviceAttribs( ) { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); - - NTSTATUS status = STATUS_SUCCESS; - PDEVICE_CONTEXT pContext = DeviceGetContext(Device); + NTSTATUS status = STATUS_SUCCESS; + PDEVICE_CONTEXT pContext = DeviceGetContext(Device); PHID_DEVICE_ATTRIBUTES pDeviceAttributes = NULL; - status = WdfRequestRetrieveOutputBuffer(Request, sizeof(HID_DEVICE_ATTRIBUTES), - &pDeviceAttributes, NULL); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfRequestRetrieveOutputBuffer failed with %!STATUS!", status); + status = WdfRequestRetrieveOutputBuffer( + Request, + sizeof(HID_DEVICE_ATTRIBUTES), + &pDeviceAttributes, + NULL); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveOutputBuffer failed with %!STATUS!", + status + ); return status; } @@ -99,9 +145,16 @@ AmtPtpGetDeviceAttribs( pDeviceAttributes->VendorID = pContext->DeviceDescriptor.idVendor; pDeviceAttributes->VersionNumber = DEVICE_VERSION; - // Set length - WdfRequestSetInformation(Request, sizeof(HID_DEVICE_ATTRIBUTES)); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + WdfRequestSetInformation( + Request, + sizeof(HID_DEVICE_ATTRIBUTES) + ); + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; @@ -114,48 +167,88 @@ MagicTrackpad2GetReportDescriptor( _In_ WDFREQUEST Request ) { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); - + NTSTATUS status = STATUS_SUCCESS; PDEVICE_CONTEXT pContext = DeviceGetContext(Device); size_t szCopy = 0; WDFMEMORY reqMemory; - status = WdfRequestRetrieveOutputMemory(Request, &reqMemory); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfRequestRetrieveOutputBuffer failed with %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); + + status = WdfRequestRetrieveOutputMemory( + Request, + &reqMemory + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveOutputBuffer failed with %!STATUS!", + status + ); return status; } - if (pContext->DeviceDescriptor.idProduct == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) - { + if (pContext->DeviceDescriptor.idProduct == USB_DEVICE_ID_APPLE_MAGICTRACKPAD2) { szCopy = AAPLMagicTrackpad2DefaultHidDescriptor.DescriptorList[0].wReportLength; - if (szCopy == 0) - { + if (szCopy == 0) { + status = STATUS_INVALID_DEVICE_STATE; - TraceEvents(TRACE_LEVEL_WARNING, TRACE_DRIVER, "Device HID report length is zero\n"); + TraceEvents( + TRACE_LEVEL_WARNING, + TRACE_DRIVER, + "%!FUNC! Device HID report length is zero" + ); return status; + } - status = WdfMemoryCopyFromBuffer(reqMemory, 0, (PVOID)&AAPLMagicTrackpad2ReportDescriptor, szCopy); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfMemoryCopyFromBuffer failed with %!STATUS!", status); + status = WdfMemoryCopyFromBuffer( + reqMemory, + 0, + (PVOID) &AAPLMagicTrackpad2ReportDescriptor, + szCopy + ); + + if (!NT_SUCCESS(status)) { + + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfMemoryCopyFromBuffer failed with %!STATUS!", + status + ); return status; + } - WdfRequestSetInformation(Request, szCopy); - } - else - { - TraceEvents(TRACE_LEVEL_WARNING, TRACE_DRIVER, "Device HID registry is not found\n"); + WdfRequestSetInformation( + Request, + szCopy + ); + } else { + TraceEvents( + TRACE_LEVEL_WARNING, + TRACE_DRIVER, + "%!FUNC! Device HID registry is not found" + ); + status = STATUS_INVALID_DEVICE_STATE; return status; } - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; + } _IRQL_requires_(PASSIVE_LEVEL) @@ -165,8 +258,7 @@ AmtPtpGetStrings( _In_ WDFREQUEST Request ) { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); - + NTSTATUS status = STATUS_SUCCESS; PDEVICE_CONTEXT pContext = DeviceGetContext(Device); void *pStringBuffer = NULL; @@ -181,23 +273,44 @@ AmtPtpGetStrings( PVOID inputBuffer; ULONG languageId, stringId; - status = WdfRequestRetrieveInputMemory(Request, &inputMemory); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); + + status = WdfRequestRetrieveInputMemory( + Request, + &inputMemory + ); + if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, - "%!FUNC! WdfRequestRetrieveInputMemory failed with status %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveInputMemory failed with status %!STATUS!", + status + ); return status; } - inputBuffer = WdfMemoryGetBuffer(inputMemory, &inputBufferLength); + inputBuffer = WdfMemoryGetBuffer( + inputMemory, + &inputBufferLength + ); // // make sure buffer is big enough. // - if (inputBufferLength < sizeof(ULONG)) - { + if (inputBufferLength < sizeof(ULONG)) { status = STATUS_INVALID_BUFFER_SIZE; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! GetStringId: invalid input buffer. size %d, expect %d\n", - (int)inputBufferLength, (int)sizeof(ULONG)); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! GetStringId: invalid input buffer. size %d, expect %d", + (int)inputBufferLength, + (int) sizeof(ULONG) + ); return status; } @@ -209,41 +322,77 @@ AmtPtpGetStrings( switch (stringId) { case HID_STRING_ID_IMANUFACTURER: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! HID_STRING_ID_IMANUFACTURER is requested\n"); strIndex = pContext->DeviceDescriptor.iManufacturer; break; case HID_STRING_ID_IPRODUCT: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! HID_STRING_ID_IPRODUCT is requested\n"); strIndex = pContext->DeviceDescriptor.iProduct; break; case HID_STRING_ID_ISERIALNUMBER: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! HID_STRING_ID_ISERIALNUMBER is requested\n"); strIndex = pContext->DeviceDescriptor.iSerialNumber; break; default: - TraceEvents(TRACE_LEVEL_WARNING, TRACE_DRIVER, "%!FUNC! gets invalid string type\n"); + TraceEvents( + TRACE_LEVEL_WARNING, + TRACE_DRIVER, + "%!FUNC! gets invalid string type" + ); return status; } - status = WdfUsbTargetDeviceAllocAndQueryString(pContext->UsbDevice, - WDF_NO_OBJECT_ATTRIBUTES, &memHandle, &wcharCount, strIndex, (USHORT) languageId); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfUsbTargetDeviceAllocAndQueryString failed with %!STATUS!", status); + + status = WdfUsbTargetDeviceAllocAndQueryString( + pContext->UsbDevice, + WDF_NO_OBJECT_ATTRIBUTES, + &memHandle, + &wcharCount, + strIndex, + (USHORT) languageId + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, "%!FUNC! WdfUsbTargetDeviceAllocAndQueryString failed with %!STATUS!", + status + ); return status; } - status = WdfRequestRetrieveOutputBuffer(Request, wcharCount * sizeof(WCHAR), &pStringBuffer, &actualSize); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "WdfMemoryCopyFromBuffer failed with %!STATUS!", status); + status = WdfRequestRetrieveOutputBuffer( + Request, + wcharCount * sizeof(WCHAR), + &pStringBuffer, + &actualSize + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfMemoryCopyFromBuffer failed with %!STATUS!", + status + ); return status; } - WdfMemoryCopyToBuffer(memHandle, 0, &pStringBuffer, actualSize); - WdfRequestSetInformation(Request, actualSize); - - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + WdfMemoryCopyToBuffer( + memHandle, + 0, + &pStringBuffer, + actualSize + ); + + WdfRequestSetInformation( + Request, + actualSize + ); + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; + } _IRQL_requires_(PASSIVE_LEVEL) @@ -253,6 +402,7 @@ RequestGetHidXferPacketToReadFromDevice( _Out_ HID_XFER_PACKET *Packet ) { + // // Driver need to write to the output buffer (so that App can read from it) // @@ -271,37 +421,67 @@ RequestGetHidXferPacketToReadFromDevice( // // Get report Id from input buffer // - status = WdfRequestRetrieveInputMemory(Request, &inputMemory); + status = WdfRequestRetrieveInputMemory( + Request, + &inputMemory + ); + if (!NT_SUCCESS(status)) { - KdPrint(("WdfRequestRetrieveInputMemory failed 0x%x\n", status)); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveInputMemory failed with %!STATUS!", + status + ); return status; } - inputBuffer = WdfMemoryGetBuffer(inputMemory, &inputBufferLength); + inputBuffer = WdfMemoryGetBuffer( + inputMemory, + &inputBufferLength + ); if (inputBufferLength < sizeof(UCHAR)) { status = STATUS_INVALID_BUFFER_SIZE; - KdPrint(("WdfRequestRetrieveInputMemory: invalid input buffer. size %d, expect %d\n", - (int)inputBufferLength, (int)sizeof(UCHAR))); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveInputMemory: invalid input buffer. size %d, expect %d", + (int) inputBufferLength, + (int) sizeof(UCHAR) + ); return status; } - Packet->reportId = *(PUCHAR)inputBuffer; + Packet->reportId = *(PUCHAR) inputBuffer; // // Get report buffer from output buffer // - status = WdfRequestRetrieveOutputMemory(Request, &outputMemory); + status = WdfRequestRetrieveOutputMemory( + Request, + &outputMemory + ); + if (!NT_SUCCESS(status)) { - KdPrint(("WdfRequestRetrieveOutputMemory failed 0x%x\n", status)); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveOutputMemory failed with %!STATUS!", + status + ); return status; } - outputBuffer = WdfMemoryGetBuffer(outputMemory, &outputBufferLength); + outputBuffer = WdfMemoryGetBuffer( + outputMemory, + &outputBufferLength + ); - Packet->reportBuffer = (PUCHAR)outputBuffer; - Packet->reportBufferLen = (ULONG)outputBufferLength; + Packet->reportBuffer = (PUCHAR) outputBuffer; + Packet->reportBufferLen = (ULONG) outputBufferLength; return status; + } _IRQL_requires_(PASSIVE_LEVEL) @@ -311,6 +491,7 @@ RequestGetHidXferPacketToWriteToDevice( _Out_ HID_XFER_PACKET *Packet ) { + // // Driver need to read from the input buffer (which was written by App) // @@ -335,28 +516,51 @@ RequestGetHidXferPacketToWriteToDevice( // // Get report Id from output buffer length // - status = WdfRequestRetrieveOutputMemory(Request, &outputMemory); + status = WdfRequestRetrieveOutputMemory( + Request, + &outputMemory + ); if (!NT_SUCCESS(status)) { - KdPrint(("WdfRequestRetrieveOutputMemory failed 0x%x\n", status)); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveOutputMemory failed with %!STATUS!", + status + ); return status; } - WdfMemoryGetBuffer(outputMemory, &outputBufferLength); - Packet->reportId = (UCHAR)outputBufferLength; + WdfMemoryGetBuffer( + outputMemory, + &outputBufferLength + ); + Packet->reportId = (UCHAR) outputBufferLength; // // Get report buffer from input buffer // - status = WdfRequestRetrieveInputMemory(Request, &inputMemory); + status = WdfRequestRetrieveInputMemory( + Request, + &inputMemory + ); if (!NT_SUCCESS(status)) { - KdPrint(("WdfRequestRetrieveInputMemory failed 0x%x\n", status)); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveInputMemory failed with %!STATUS!", + status + ); return status; } - inputBuffer = WdfMemoryGetBuffer(inputMemory, &inputBufferLength); + inputBuffer = WdfMemoryGetBuffer( + inputMemory, + &inputBufferLength + ); - Packet->reportBuffer = (PUCHAR)inputBuffer; - Packet->reportBufferLen = (ULONG)inputBufferLength; + Packet->reportBuffer = (PUCHAR) inputBuffer; + Packet->reportBufferLen = (ULONG) inputBufferLength; return status; + } _IRQL_requires_(PASSIVE_LEVEL) @@ -366,20 +570,33 @@ AmtPtpReportFeatures( _In_ WDFREQUEST Request ) { + NTSTATUS status; PDEVICE_CONTEXT deviceContext; HID_XFER_PACKET packet; size_t reportSize; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); status = STATUS_SUCCESS; deviceContext = DeviceGetContext(Device); - status = RequestGetHidXferPacketToReadFromDevice(Request, &packet); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC! RequestGetHidXferPacketToReadFromDevice failed with status %!STATUS!", status); + status = RequestGetHidXferPacketToReadFromDevice( + Request, + &packet + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! RequestGetHidXferPacketToReadFromDevice failed with status %!STATUS!", + status + ); goto exit; } @@ -387,13 +604,21 @@ AmtPtpReportFeatures( { case REPORTID_DEVICE_CAPS: { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_DEVICE_CAPS is requested.\n"); - reportSize = sizeof(PPTP_DEVICE_CAPS_FEATURE_REPORT) + sizeof(packet.reportId); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_DEVICE_CAPS is requested" + ); + // Size sanity check - if (packet.reportBufferLen < reportSize) - { + reportSize = sizeof(PPTP_DEVICE_CAPS_FEATURE_REPORT) + sizeof(packet.reportId); + if (packet.reportBufferLen < reportSize) { status = STATUS_INVALID_BUFFER_SIZE; - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC! Report buffer is too small.\n"); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! Report buffer is too small" + ); goto exit; } @@ -403,23 +628,47 @@ AmtPtpReportFeatures( capsReport->ButtonType = PTP_BUTTON_TYPE_CLICK_PAD; capsReport->ReportID = REPORTID_DEVICE_CAPS; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_DEVICE_CAPS has maximum contact points of %d.\n", - capsReport->MaximumContactPoints); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_DEVICE_CAPS has touchpad type %d.\n", - capsReport->ButtonType); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_DEVICE_CAPS is fulfilled.\n"); - WdfRequestSetInformation(Request, reportSize); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_DEVICE_CAPS has maximum contact points of %d", + capsReport->MaximumContactPoints + ); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_DEVICE_CAPS has touchpad type %d", + capsReport->ButtonType + ); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_DEVICE_CAPS is fulfilled" + ); + + WdfRequestSetInformation( + Request, + reportSize + ); break; } case REPORTID_PTPHQA: { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_PTPHQA is requested.\n"); - reportSize = sizeof(PPTP_DEVICE_HQA_CERTIFICATION_REPORT) + sizeof(packet.reportId); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_PTPHQA is requested" + ); + // Size sanity check - if (packet.reportBufferLen < reportSize) - { + reportSize = sizeof(PPTP_DEVICE_HQA_CERTIFICATION_REPORT) + sizeof(packet.reportId); + if (packet.reportBufferLen < reportSize) { status = STATUS_INVALID_BUFFER_SIZE; - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC! Report buffer is too small.\n"); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! Report buffer is too small." + ); goto exit; } @@ -428,18 +677,37 @@ AmtPtpReportFeatures( *certReport->CertificationBlob = DEFAULT_PTP_HQA_BLOB; certReport->ReportID = REPORTID_PTPHQA; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_PTPHQA is fulfilled.\n"); - WdfRequestSetInformation(Request, reportSize); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_PTPHQA is fulfilled" + ); + + WdfRequestSetInformation( + Request, + reportSize + ); break; } default: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Unsupported type %d is requested.\n", packet.reportId); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Unsupported type %d is requested", + packet.reportId + ); + status = STATUS_NOT_SUPPORTED; goto exit; } exit: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; + } _IRQL_requires_(PASSIVE_LEVEL) @@ -449,19 +717,32 @@ AmtPtpSetFeatures( _In_ WDFREQUEST Request ) { + NTSTATUS status; HID_XFER_PACKET packet; PDEVICE_CONTEXT deviceContext; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); status = STATUS_SUCCESS; deviceContext = DeviceGetContext(Device); - status = RequestGetHidXferPacketToWriteToDevice(Request, &packet); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC! RequestGetHidXferPacketToWriteToDevice failed with status %!STATUS!", status); + status = RequestGetHidXferPacketToWriteToDevice( + Request, + &packet + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! RequestGetHidXferPacketToWriteToDevice failed with status %!STATUS!", + status + ); goto exit; } @@ -469,56 +750,119 @@ AmtPtpSetFeatures( { case REPORTID_REPORTMODE: { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_REPORTMODE is requested.\n"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_REPORTMODE is requested" + ); PPTP_DEVICE_INPUT_MODE_REPORT devInputMode = (PPTP_DEVICE_INPUT_MODE_REPORT) packet.reportBuffer; switch (devInputMode->Mode) { case PTP_COLLECTION_MOUSE: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_REPORTMODE requested Mouse Input.\n"); - status = AmtPtpSetWellspringMode(deviceContext, FALSE); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC! MagicTrackpad2PtpDeviceSetWellspringMode failed with status %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_REPORTMODE requested Mouse Input" + ); + status = AmtPtpSetWellspringMode( + deviceContext, + FALSE + ); + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! MagicTrackpad2PtpDeviceSetWellspringMode failed with status %!STATUS!", + status + ); goto exit; } break; case PTP_COLLECTION_WINDOWS: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_REPORTMODE requested Windows PTP Input.\n"); - status = AmtPtpSetWellspringMode(deviceContext, TRUE); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC! MagicTrackpad2PtpDeviceSetWellspringMode failed with status %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_REPORTMODE requested Windows PTP Input" + ); + + status = AmtPtpSetWellspringMode( + deviceContext, + TRUE + ); + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! MagicTrackpad2PtpDeviceSetWellspringMode failed with status %!STATUS!", + status + ); goto exit; } break; } - WdfRequestSetInformation(Request, sizeof(PTP_DEVICE_INPUT_MODE_REPORT)); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_REPORTMODE is fulfilled.\n"); + WdfRequestSetInformation( + Request, + sizeof(PTP_DEVICE_INPUT_MODE_REPORT) + ); + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_REPORTMODE is fulfilled" + ); break; } case REPORTID_FUNCSWITCH: { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_FUNCSWITCH is requested.\n"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_FUNCSWITCH is requested" + ); PPTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT secInput = (PPTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT) packet.reportBuffer; deviceContext->IsButtonReportOn = secInput->ButtonReport; deviceContext->IsSurfaceReportOn = secInput->SurfaceReport; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_FUNCSWITCH requested Button = %d, Surface = %d.\n", - secInput->ButtonReport, secInput->SurfaceReport); - WdfRequestSetInformation(Request, sizeof(PTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT)); - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Report REPORTID_FUNCSWITCH is fulfilled.\n"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_FUNCSWITCH requested Button = %d, Surface = %d", + secInput->ButtonReport, + secInput->SurfaceReport + ); + + WdfRequestSetInformation( + Request, + sizeof(PTP_DEVICE_SELECTIVE_REPORT_MODE_REPORT) + ); + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Report REPORTID_FUNCSWITCH is fulfilled" + ); break; } default: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Unsupported type %d is requested.\n", packet.reportId); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Unsupported type %d is requested", + packet.reportId + ); status = STATUS_NOT_SUPPORTED; goto exit; } exit: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return STATUS_SUCCESS; + } \ No newline at end of file diff --git a/src/MagicTrackpad2PtpDevice/InputInterrupt.c b/src/MagicTrackpad2PtpDevice/InputInterrupt.c index c6be51a..06e2336 100644 --- a/src/MagicTrackpad2PtpDevice/InputInterrupt.c +++ b/src/MagicTrackpad2PtpDevice/InputInterrupt.c @@ -9,19 +9,24 @@ AmtPtpConfigContReaderForInterruptEndPoint( _In_ PDEVICE_CONTEXT DeviceContext ) { + WDF_USB_CONTINUOUS_READER_CONFIG contReaderConfig; NTSTATUS status; - WDF_USB_CONTINUOUS_READER_CONFIG_INIT(&contReaderConfig, + WDF_USB_CONTINUOUS_READER_CONFIG_INIT( + &contReaderConfig, AmtPtpEvtUsbInterruptPipeReadComplete, DeviceContext, // Context - HEADER_TYPE5 + FSIZE_TYPE5 * MAX_FINGERS); // TransferLength + HEADER_TYPE5 + FSIZE_TYPE5 * MAX_FINGERS + ); // TransferLength contReaderConfig.EvtUsbTargetPipeReadersFailed = AmtPtpEvtUsbInterruptReadersFailed; // Remember to turn it on in D0 entry - status = WdfUsbTargetPipeConfigContinuousReader(DeviceContext->InterruptPipe, - &contReaderConfig); + status = WdfUsbTargetPipeConfigContinuousReader( + DeviceContext->InterruptPipe, + &contReaderConfig + ); if (!NT_SUCCESS(status)) { // TODO: Trace @@ -29,6 +34,7 @@ AmtPtpConfigContReaderForInterruptEndPoint( } return STATUS_SUCCESS; + } _IRQL_requires_(PASSIVE_LEVEL) @@ -40,6 +46,7 @@ AmtPtpEvtUsbInterruptPipeReadComplete( _In_ WDFCONTEXT Context ) { + UNREFERENCED_PARAMETER(Pipe); WDFDEVICE device; @@ -52,33 +59,45 @@ AmtPtpEvtUsbInterruptPipeReadComplete( size_t headerSize = (unsigned int) pDeviceContext->DeviceInfo->tp_header; size_t fingerprintSize = (unsigned int) pDeviceContext->DeviceInfo->tp_fsize; - if (!pDeviceContext->IsWellspringModeOn) - { - return; - } + if (!pDeviceContext->IsWellspringModeOn) return; - if (pDeviceContext->DeviceInfo->tp_type != TYPE5) - { - TraceEvents(TRACE_LEVEL_WARNING, TRACE_DRIVER, "%!FUNC!: Mode not yet supported\n"); + if (pDeviceContext->DeviceInfo->tp_type != TYPE5) { + TraceEvents( + TRACE_LEVEL_WARNING, + TRACE_DRIVER, + "%!FUNC! Mode not yet supported" + ); return; } - if (NumBytesTransferred < headerSize || (NumBytesTransferred - headerSize) % fingerprintSize != 0) - { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, - "%!FUNC!: Malformed input received. Length = %llu\n", NumBytesTransferred); - } - else - { - // Dispatch to touch routine - szBuffer = WdfMemoryGetBuffer(Buffer, NULL); - status = AmtPtpServiceTouchInputInterruptType5(pDeviceContext, szBuffer, NumBytesTransferred); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_WARNING, TRACE_DRIVER, - "%!FUNC!: AmtPtpServiceTouchInputInterrupt failed with %!STATUS!", status); + if (NumBytesTransferred < headerSize || (NumBytesTransferred - headerSize) % fingerprintSize != 0) { + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Malformed input received. Length = %llu", + NumBytesTransferred + ); + } else { + szBuffer = WdfMemoryGetBuffer( + Buffer, + NULL + ); + status = AmtPtpServiceTouchInputInterruptType5( + pDeviceContext, + szBuffer, + NumBytesTransferred + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_WARNING, + TRACE_DRIVER, + "%!FUNC! AmtPtpServiceTouchInputInterrupt failed with %!STATUS!", + status + ); } } + } _IRQL_requires_(PASSIVE_LEVEL) @@ -90,7 +109,6 @@ AmtPtpEvtUsbInterruptReadersFailed( ) { WDFDEVICE device = WdfIoTargetGetDevice(WdfUsbTargetPipeGetIoTarget(Pipe)); - // PDEVICE_CONTEXT pDeviceContext = GetDeviceContext(device); UNREFERENCED_PARAMETER(device); UNREFERENCED_PARAMETER(UsbdStatus); @@ -107,6 +125,7 @@ AmtPtpServiceTouchInputInterruptType5( _In_ size_t NumBytesTransferred ) { + NTSTATUS status; WDFREQUEST request; WDFMEMORY reqMemory; @@ -115,146 +134,210 @@ AmtPtpServiceTouchInputInterruptType5( const struct TRACKPAD_FINGER *f; const struct TRACKPAD_FINGER_TYPE5 *f_type5; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Entry"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Entry" + ); status = STATUS_SUCCESS; - s32 x, y = 0; + INT x, y = 0; size_t raw_n, i = 0; - size_t headerSize = (unsigned int)DeviceContext->DeviceInfo->tp_header; - size_t fingerprintSize = (unsigned int)DeviceContext->DeviceInfo->tp_fsize; + size_t headerSize = (unsigned int) DeviceContext->DeviceInfo->tp_header; + size_t fingerprintSize = (unsigned int) DeviceContext->DeviceInfo->tp_fsize; UCHAR actualFingers = 0; status = WdfIoQueueRetrieveNextRequest( DeviceContext->InputQueue, - &request); + &request + ); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC!: No pending PTP request. Interrupt disposed."); + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! No pending PTP request. Interrupt disposed" + ); goto exit; } - status = WdfRequestRetrieveOutputMemory(request, &reqMemory); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC!: WdfRequestRetrieveOutputBuffer failed with %!STATUS!", status); + status = WdfRequestRetrieveOutputMemory( + request, + &reqMemory + ); + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestRetrieveOutputBuffer failed with %!STATUS!", + status + ); goto exit; } - // Iterations to read - raw_n = (NumBytesTransferred - headerSize) / fingerprintSize; - if (raw_n >= PTP_MAX_CONTACT_POINTS) raw_n = PTP_MAX_CONTACT_POINTS; - - // Fingers - for (i = 0; i < raw_n; i++) - { - u8 *f_base = Buffer + headerSize + DeviceContext->DeviceInfo->tp_delta; - f = (const struct TRACKPAD_FINGER*) (f_base + i * fingerprintSize); - f_type5 = (const struct TRACKPAD_FINGER_TYPE5*) f; - - u16 tmp_x; - u32 tmp_y; - tmp_x = (*((__le16*)f_type5)) & 0x1fff; - tmp_y = (s32)(*((__le32*)f_type5)); - - x = (s16)(tmp_x << 3) >> 3; - y = -(s32)(tmp_y << 6) >> 19; - - // We need to defuzz input - if (DeviceContext->ContactRepository[i].ContactId == f_type5->ContactIdentifier.Id) - { - DeviceContext->ContactRepository[i].X = (USHORT)AmtPtpDefuzzInput( - x, DeviceContext->ContactRepository[i].X, DeviceContext->HorizonalFuzz); - DeviceContext->ContactRepository[i].Y = (USHORT)AmtPtpDefuzzInput( - y, DeviceContext->ContactRepository[i].Y, DeviceContext->VerticalFuzz); - DeviceContext->ContactRepository[i].Pressure = (UCHAR)AmtPtpDefuzzInput( - f_type5->Pressure, DeviceContext->ContactRepository[i].Pressure, DeviceContext->PressureFuzz); - DeviceContext->ContactRepository[i].Size = (UCHAR)AmtPtpDefuzzInput( - f_type5->Size, DeviceContext->ContactRepository[i].Size, DeviceContext->WidthFuzz); - } - else - { - DeviceContext->ContactRepository[i].ContactId = f_type5->ContactIdentifier.Id; - DeviceContext->ContactRepository[i].X = (USHORT)x; - DeviceContext->ContactRepository[i].Y = (USHORT)y; - DeviceContext->ContactRepository[i].Pressure = f_type5->Pressure; - DeviceContext->ContactRepository[i].Size = f_type5->Size; - } + // First things + report.ReportID = REPORTID_MULTITOUCH; + report.ScanTime = 10000; - // Set ID. - report.Contacts[i].ContactID = f_type5->ContactIdentifier.Id; - - // Adjust position. Apple uses different coordinate system. - report.Contacts[i].X = (USHORT)(DeviceContext->ContactRepository[i].X - DeviceContext->DeviceInfo->x.min); - report.Contacts[i].Y = (USHORT)(DeviceContext->ContactRepository[i].Y - DeviceContext->DeviceInfo->y.min); - - // Set flags (by cases) - if (raw_n == 1) - { - report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD; - report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= SIZE_QUALIFICATION_THRESHOLD; - - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, - "(SG) Finger %d, X: %d, Y: %d, Pressure: %d, Size: %d, TipSwitch: %d, Confidence: %d", - f_type5->ContactIdentifier.Id, - report.Contacts[i].X, - report.Contacts[i].Y, - DeviceContext->ContactRepository[i].Pressure, - DeviceContext->ContactRepository[i].Size, - report.Contacts[i].TipSwitch, - report.Contacts[i].Confidence); - } - else - { - // Save the information - // Use size to determine confidence in MU scenario - report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD; - report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= SIZE_MU_LOWER_THRESHOLD; - - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, - "(MU) Finger %d, X: %d, Y: %d, Pressure: %d, Size: %d, TipSwitch: %d, Confidence: %d", - f_type5->ContactIdentifier.Id, - report.Contacts[i].X, - report.Contacts[i].Y, - DeviceContext->ContactRepository[i].Pressure, - DeviceContext->ContactRepository[i].Size, - report.Contacts[i].TipSwitch, - report.Contacts[i].Confidence); - } + // Check things to report + if (DeviceContext->IsSurfaceReportOn) { + // Iterations to read + raw_n = (NumBytesTransferred - headerSize) / fingerprintSize; + if (raw_n >= PTP_MAX_CONTACT_POINTS) raw_n = PTP_MAX_CONTACT_POINTS; + + // Fingers + for (i = 0; i < raw_n; i++) { + UCHAR *f_base = Buffer + headerSize + DeviceContext->DeviceInfo->tp_delta; + f = (const struct TRACKPAD_FINGER*) (f_base + i * fingerprintSize); + f_type5 = (const struct TRACKPAD_FINGER_TYPE5*) f; + + USHORT tmp_x; + UINT tmp_y; + tmp_x = (*((USHORT*)f_type5)) & 0x1fff; + tmp_y = (INT) (*((UINT*)f_type5)); + + x = (SHORT) (tmp_x << 3) >> 3; + y = -(INT) (tmp_y << 6) >> 19; + + // We need to defuzz input + if (DeviceContext->ContactRepository[i].ContactId == f_type5->ContactIdentifier.Id) { + DeviceContext->ContactRepository[i].X = (USHORT) AmtPtpDefuzzInput( + x, + DeviceContext->ContactRepository[i].X, + DeviceContext->HorizonalFuzz + ); + DeviceContext->ContactRepository[i].Y = (USHORT) AmtPtpDefuzzInput( + y, + DeviceContext->ContactRepository[i].Y, + DeviceContext->VerticalFuzz + ); + DeviceContext->ContactRepository[i].Pressure = (UCHAR) AmtPtpDefuzzInput( + f_type5->Pressure, + DeviceContext->ContactRepository[i].Pressure, + DeviceContext->PressureFuzz + ); + DeviceContext->ContactRepository[i].Size = (UCHAR) AmtPtpDefuzzInput( + f_type5->Size, + DeviceContext->ContactRepository[i].Size, + DeviceContext->WidthFuzz + ); + } else { + DeviceContext->ContactRepository[i].ContactId = f_type5->ContactIdentifier.Id; + DeviceContext->ContactRepository[i].X = (USHORT) x; + DeviceContext->ContactRepository[i].Y = (USHORT) y; + DeviceContext->ContactRepository[i].Pressure = f_type5->Pressure; + DeviceContext->ContactRepository[i].Size = f_type5->Size; + } + + // Set ID. + report.Contacts[i].ContactID = f_type5->ContactIdentifier.Id; + + // Adjust position. Apple uses different coordinate system. + report.Contacts[i].X = (USHORT) (DeviceContext->ContactRepository[i].X - DeviceContext->DeviceInfo->x.min); + report.Contacts[i].Y = (USHORT) (DeviceContext->ContactRepository[i].Y - DeviceContext->DeviceInfo->y.min); + + // Set flags (by cases) + if (raw_n == 1) { + report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD; + report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= SIZE_QUALIFICATION_THRESHOLD; + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_INPUT, + "(SG) Finger %d, X: %d, Y: %d, Pressure: %d, Size: %d, TipSwitch: %d, Confidence: %d", + f_type5->ContactIdentifier.Id, + report.Contacts[i].X, + report.Contacts[i].Y, + DeviceContext->ContactRepository[i].Pressure, + DeviceContext->ContactRepository[i].Size, + report.Contacts[i].TipSwitch, + report.Contacts[i].Confidence + ); + } else { + // Save the information + // Use size to determine confidence in MU scenario + report.Contacts[i].TipSwitch = DeviceContext->ContactRepository[i].Pressure > PRESSURE_QUALIFICATION_THRESHOLD; + report.Contacts[i].Confidence = DeviceContext->ContactRepository[i].Size >= SIZE_MU_LOWER_THRESHOLD; + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_INPUT, + "(MU) Finger %d, X: %d, Y: %d, Pressure: %d, Size: %d, TipSwitch: %d, Confidence: %d", + f_type5->ContactIdentifier.Id, + report.Contacts[i].X, + report.Contacts[i].Y, + DeviceContext->ContactRepository[i].Pressure, + DeviceContext->ContactRepository[i].Size, + report.Contacts[i].TipSwitch, + report.Contacts[i].Confidence + ); + } - actualFingers++; - } + actualFingers++; + } - // Set header information - report.ContactCount = actualFingers; - report.ReportID = REPORTID_MULTITOUCH; - report.ScanTime = 10000; + // Set header information + report.ContactCount = actualFingers; + + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_INPUT, + "%!FUNC! With %d fingers", + report.ContactCount + ); + } // Button - if (Buffer[DeviceContext->DeviceInfo->tp_button] && actualFingers < 2) - { - report.IsButtonClicked = TRUE; - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC!: Trackpad button clicked\n"); + if (DeviceContext->IsButtonReportOn) { + if (Buffer[DeviceContext->DeviceInfo->tp_button] && actualFingers < 2) { + report.IsButtonClicked = TRUE; + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_INPUT, + "%!FUNC!: Trackpad button clicked" + ); + } } // Write output - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! With %d fingers.\n", report.ContactCount); - status = WdfMemoryCopyFromBuffer(reqMemory, 0, (PVOID)&report, sizeof(PTP_REPORT)); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC!: WdfMemoryCopyFromBuffer failed with %!STATUS!", status); + status = WdfMemoryCopyFromBuffer( + reqMemory, + 0, + (PVOID) &report, + sizeof(PTP_REPORT) + ); + + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfMemoryCopyFromBuffer failed with %!STATUS!", + status + ); goto exit; } // Set result - WdfRequestSetInformation(request, sizeof(PTP_REPORT)); + WdfRequestSetInformation( + request, + sizeof(PTP_REPORT) + ); + // Set completion flag - WdfRequestComplete(request, status); + WdfRequestComplete( + request, + status + ); exit: - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, "%!FUNC! Exit"); + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! Exit" + ); return status; + } // Debuzz function from Linux Kernel: drivers/input/input.c @@ -265,8 +348,8 @@ static INT AmtPtpDefuzzInput( _In_ double Fuzz ) { - if (Fuzz) - { + + if (Fuzz) { if (NewValue > OldValue - Fuzz / 2 && NewValue < OldValue + Fuzz / 2) return OldValue; @@ -278,4 +361,5 @@ static INT AmtPtpDefuzzInput( } return NewValue; + } \ No newline at end of file diff --git a/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj b/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj index 891176e..3860a5b 100644 --- a/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj +++ b/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj @@ -49,7 +49,6 @@ - diff --git a/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj.filters b/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj.filters index be37d42..d9f446f 100644 --- a/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj.filters +++ b/src/MagicTrackpad2PtpDevice/MagicTrackpad2PtpDevice.vcxproj.filters @@ -39,9 +39,6 @@ Header Files - - Header Files - Header Files diff --git a/src/MagicTrackpad2PtpDevice/Queue.c b/src/MagicTrackpad2PtpDevice/Queue.c index 66179c4..043f0f3 100644 --- a/src/MagicTrackpad2PtpDevice/Queue.c +++ b/src/MagicTrackpad2PtpDevice/Queue.c @@ -7,28 +7,8 @@ NTSTATUS MagicTrackpad2PtpDeviceQueueInitialize( _In_ WDFDEVICE Device ) -/*++ - -Routine Description: - - - The I/O dispatch callbacks for the frameworks device object - are configured in this function. - - A single default I/O Queue is configured for parallel request - processing, and a driver context memory allocation is created - to hold our structure QUEUE_CONTEXT. - -Arguments: - - Device - Handle to a framework device object. - -Return Value: - - VOID - ---*/ { + WDFQUEUE queue; NTSTATUS status; WDF_IO_QUEUE_CONFIG queueConfig; @@ -57,7 +37,12 @@ Return Value: ); if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_QUEUE, "WdfIoQueueCreate (Primary) failed %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_QUEUE, + "%!FUNC! WdfIoQueueCreate (Primary) failed %!STATUS!", + status + ); return status; } @@ -72,14 +57,21 @@ Return Value: Device, &queueConfig, WDF_NO_OBJECT_ATTRIBUTES, - &deviceContext->InputQueue); + &deviceContext->InputQueue + ); if (!NT_SUCCESS(status)) { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_QUEUE, "WdfIoQueueCreate (Input) failed %!STATUS!", status); + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_QUEUE, + "%!FUNC! WdfIoQueueCreate (Input) failed %!STATUS!", + status + ); return status; } return status; + } _IRQL_requires_(PASSIVE_LEVEL) @@ -88,6 +80,7 @@ DbgIoControlGetString( _In_ ULONG IoControlCode ) { + switch (IoControlCode) { case IOCTL_HID_GET_DEVICE_DESCRIPTOR: @@ -119,6 +112,7 @@ DbgIoControlGetString( default: return "IOCTL_UNKNOWN"; } + } VOID @@ -135,33 +129,61 @@ MagicTrackpad2PtpDeviceEvtIoDeviceControl( WDFDEVICE device = WdfIoQueueGetDevice(Queue); BOOLEAN requestPending = FALSE; - TraceEvents(TRACE_LEVEL_INFORMATION, + TraceEvents( + TRACE_LEVEL_INFORMATION, TRACE_QUEUE, - "%!FUNC!: Queue 0x%p, Request 0x%p OutputBufferLength %d InputBufferLength %d IoControlCode %d\n", - Queue, Request, (int)OutputBufferLength, (int)InputBufferLength, IoControlCode); + "%!FUNC!: Queue 0x%p, Request 0x%p OutputBufferLength %d InputBufferLength %d IoControlCode %d", + Queue, + Request, + (int) OutputBufferLength, + (int) InputBufferLength, + IoControlCode + ); switch (IoControlCode) { case IOCTL_HID_GET_DEVICE_DESCRIPTOR: - status = MagicTrackpad2GetHidDescriptor(device, Request); + status = MagicTrackpad2GetHidDescriptor( + device, + Request + ); break; case IOCTL_HID_GET_DEVICE_ATTRIBUTES: - status = AmtPtpGetDeviceAttribs(device, Request); + status = AmtPtpGetDeviceAttribs( + device, + Request + ); break; case IOCTL_HID_GET_REPORT_DESCRIPTOR: - status = MagicTrackpad2GetReportDescriptor(device, Request); + status = MagicTrackpad2GetReportDescriptor( + device, + Request + ); break; case IOCTL_HID_GET_STRING: - status = AmtPtpGetStrings(device, Request); + status = AmtPtpGetStrings( + device, + Request + ); break; case IOCTL_HID_READ_REPORT: - status = AmtPtpDispatchReadReportRequests(device, Request, &requestPending); + status = AmtPtpDispatchReadReportRequests( + device, + Request, + &requestPending + ); break; case IOCTL_UMDF_HID_GET_FEATURE: - status = AmtPtpReportFeatures(device, Request); + status = AmtPtpReportFeatures( + device, + Request + ); break; case IOCTL_UMDF_HID_SET_FEATURE: - status = AmtPtpSetFeatures(device, Request); + status = AmtPtpSetFeatures( + device, + Request + ); break; case IOCTL_HID_WRITE_REPORT: case IOCTL_UMDF_HID_SET_OUTPUT_REPORT: @@ -171,13 +193,20 @@ MagicTrackpad2PtpDeviceEvtIoDeviceControl( case IOCTL_HID_SEND_IDLE_NOTIFICATION_REQUEST: default: status = STATUS_NOT_SUPPORTED; - TraceEvents(TRACE_LEVEL_WARNING, TRACE_QUEUE, "%!FUNC!: %s is not yet implemented.\n", DbgIoControlGetString(IoControlCode)); + TraceEvents( + TRACE_LEVEL_WARNING, + TRACE_QUEUE, + "%!FUNC!: %s is not yet implemented", + DbgIoControlGetString(IoControlCode) + ); break; } - if (requestPending != TRUE) - { - WdfRequestComplete(Request, status); + if (requestPending != TRUE) { + WdfRequestComplete( + Request, + status + ); } return; @@ -189,33 +218,16 @@ MagicTrackpad2PtpDeviceEvtIoStop( _In_ WDFREQUEST Request, _In_ ULONG ActionFlags ) -/*++ - -Routine Description: - - This event is invoked for a power-managed queue before the device leaves the working state (D0). - -Arguments: - - Queue - Handle to the framework queue object that is associated with the - I/O request. - - Request - Handle to a framework request object. - - ActionFlags - A bitwise OR of one or more WDF_REQUEST_STOP_ACTION_FLAGS-typed flags - that identify the reason that the callback function is being called - and whether the request is cancelable. - -Return Value: - - VOID - ---*/ { - TraceEvents(TRACE_LEVEL_INFORMATION, + + TraceEvents( + TRACE_LEVEL_INFORMATION, TRACE_QUEUE, "%!FUNC! Queue 0x%p, Request 0x%p ActionFlags %d", - Queue, Request, ActionFlags); + Queue, + Request, + ActionFlags + ); // // In most cases, the EvtIoStop callback function completes, cancels, or postpones @@ -242,6 +254,7 @@ Return Value: // return; + } NTSTATUS @@ -251,30 +264,39 @@ AmtPtpDispatchReadReportRequests( _Out_ BOOLEAN *Pending ) { + NTSTATUS status; PDEVICE_CONTEXT devContext; status = STATUS_SUCCESS; devContext = DeviceGetContext(Device); - status = WdfRequestForwardToIoQueue(Request, devContext->InputQueue); + status = WdfRequestForwardToIoQueue( + Request, + devContext->InputQueue + ); - if (!NT_SUCCESS(status)) - { - TraceEvents(TRACE_LEVEL_ERROR, TRACE_DRIVER, "%!FUNC!: WdfRequestForwardToIoQueue failed with %!STATUS!", status); + if (!NT_SUCCESS(status)) { + TraceEvents( + TRACE_LEVEL_ERROR, + TRACE_DRIVER, + "%!FUNC! WdfRequestForwardToIoQueue failed with %!STATUS!", + status + ); return status; - } - else - { - TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_DRIVER, - "%!FUNC!: A report has been forwarded to input queue.\n"); + } else { + TraceEvents( + TRACE_LEVEL_INFORMATION, + TRACE_DRIVER, + "%!FUNC! A report has been forwarded to input queue" + ); } - if (NULL != Pending) - { + if (NULL != Pending) { *Pending = TRUE; } return status; + } \ No newline at end of file diff --git a/src/MagicTrackpad2PtpDevice/Trace.h b/src/MagicTrackpad2PtpDevice/Trace.h index b695454..7ce7423 100644 --- a/src/MagicTrackpad2PtpDevice/Trace.h +++ b/src/MagicTrackpad2PtpDevice/Trace.h @@ -13,14 +13,15 @@ DEFINE_GUID(GUID_DEVINTERFACE_MagicTrackpad2PtpDevice, // Tracing GUID - efc3ce99-43ff-4b59-afe4-c856e1afd8b0 // -#define WPP_CONTROL_GUIDS \ - WPP_DEFINE_CONTROL_GUID( \ - MyDriver1TraceGuid, (efc3ce99,43ff,4b59,afe4,c856e1afd8b0), \ - \ - WPP_DEFINE_BIT(MYDRIVER_ALL_INFO) \ - WPP_DEFINE_BIT(TRACE_DRIVER) \ - WPP_DEFINE_BIT(TRACE_DEVICE) \ - WPP_DEFINE_BIT(TRACE_QUEUE) \ +#define WPP_CONTROL_GUIDS \ + WPP_DEFINE_CONTROL_GUID( \ + AmtPtpDriverTraceGuid, (efc3ce99,43ff,4b59,afe4,c856e1afd8b0), \ + \ + WPP_DEFINE_BIT(AMTPTPDRIVER_ALL_INFO) \ + WPP_DEFINE_BIT(TRACE_DRIVER) \ + WPP_DEFINE_BIT(TRACE_DEVICE) \ + WPP_DEFINE_BIT(TRACE_QUEUE) \ + WPP_DEFINE_BIT(TRACE_INPUT) \ ) #define WPP_FLAG_LEVEL_LOGGER(flag, level) \ diff --git a/src/MagicTrackpad2PtpDevice/Types.h b/src/MagicTrackpad2PtpDevice/Types.h deleted file mode 100644 index 80cfe74..0000000 --- a/src/MagicTrackpad2PtpDevice/Types.h +++ /dev/null @@ -1,13 +0,0 @@ -// Types.h: Type definition to maintain compatibility - -#pragma once - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; - -typedef signed short s16; -typedef signed int s32; - -typedef unsigned short __le16; -typedef unsigned int __le32; \ No newline at end of file