├── croskblight ├── resource.h ├── croskblight.rc ├── hidcommon.h ├── Trace.h ├── croskblight.inf ├── eccmds.h ├── croskblight.h ├── croskblight.vcxproj ├── comm-mec_lpc.c └── croskblight.cpp ├── croskblight.sln ├── croskblight Package └── croskblight Package.vcxproj └── .gitignore /croskblight/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by croskblight.rc 4 | 5 | // Next default values for new objects 6 | // 7 | #ifdef APSTUDIO_INVOKED 8 | #ifndef APSTUDIO_READONLY_SYMBOLS 9 | #define _APS_NEXT_RESOURCE_VALUE 101 10 | #define _APS_NEXT_COMMAND_VALUE 40001 11 | #define _APS_NEXT_CONTROL_VALUE 1001 12 | #define _APS_NEXT_SYMED_VALUE 101 13 | #endif 14 | #endif 15 | -------------------------------------------------------------------------------- /croskblight/croskblight.rc: -------------------------------------------------------------------------------- 1 | /*++ 2 | 3 | Copyright (c) Microsoft Corporation All Rights Reserved 4 | 5 | Module Name: 6 | 7 | croskblight.rc 8 | 9 | Abstract: 10 | 11 | --*/ 12 | 13 | #include 14 | 15 | #define VER_FILETYPE VFT_DRV 16 | #define VER_FILESUBTYPE VFT2_DRV_KEYBOARD 17 | #define VER_FILEDESCRIPTION_STR "Chromebook Keyboard Backlight (Wilco)" 18 | #define VER_INTERNALNAME_STR "croskblight.sys" 19 | #define VER_ORIGINALFILENAME_STR "croskblight.sys" 20 | 21 | #define VER_LEGALCOPYRIGHT_YEARS "2023" 22 | #define VER_LEGALCOPYRIGHT_STR "Copyright (C) " VER_LEGALCOPYRIGHT_YEARS " CoolStar." 23 | 24 | #define VER_FILEVERSION 1,0,1,0 25 | #define VER_PRODUCTVERSION_STR "1.0.1.0" 26 | #define VER_PRODUCTVERSION 1,0,1,0 27 | #define LVER_PRODUCTVERSION_STR L"1.0.1.0" 28 | 29 | #define VER_FILEFLAGSMASK (VS_FF_DEBUG | VS_FF_PRERELEASE) 30 | #ifdef DEBUG 31 | #define VER_FILEFLAGS (VS_FF_DEBUG) 32 | #else 33 | #define VER_FILEFLAGS (0) 34 | #endif 35 | 36 | #define VER_FILEOS VOS_NT_WINDOWS32 37 | 38 | #define VER_COMPANYNAME_STR "CoolStar" 39 | #define VER_PRODUCTNAME_STR "Chromebook Keyboard Backlight (Wilco)" 40 | 41 | #include "common.ver" -------------------------------------------------------------------------------- /croskblight/hidcommon.h: -------------------------------------------------------------------------------- 1 | #if !defined(_CROSKBLIGHT_COMMON_H_) 2 | #define _CROSKBLIGHT_COMMON_H_ 3 | 4 | // 5 | //These are the device attributes returned by vmulti in response 6 | // to IOCTL_HID_GET_DEVICE_ATTRIBUTES. 7 | // 8 | 9 | #define CROSKBLIGHT_PID 0x0002 10 | #define CROSKBLIGHT_VID 0x18D1 11 | #define CROSKBLIGHT_VERSION 0x0001 12 | 13 | // 14 | // These are the report ids 15 | // 16 | 17 | #define REPORTID_KBLIGHT 0x01 18 | 19 | #pragma pack(1) 20 | typedef struct _CROSKBLIGHT_FEATURE_REPORT 21 | { 22 | 23 | BYTE ReportID; 24 | 25 | BYTE DeviceMode; 26 | 27 | BYTE DeviceIdentifier; 28 | 29 | } CrosKBLightFeatureReport; 30 | 31 | typedef struct _CROSKBLIGHT_MAXCOUNT_REPORT 32 | { 33 | 34 | BYTE ReportID; 35 | 36 | BYTE MaximumCount; 37 | 38 | } CrosKBLightMaxCountReport; 39 | #pragma pack() 40 | 41 | #pragma pack(1) 42 | typedef struct _CROSKBLIGHT_GETLIGHT_REPORT 43 | { 44 | 45 | BYTE ReportID; 46 | 47 | BYTE Brightness; 48 | 49 | } CrosKBLightGetLightReport; 50 | #pragma pack() 51 | 52 | #pragma pack(1) 53 | typedef struct _CROSKBLIGHT_SETTINGS_REPORT 54 | { 55 | 56 | BYTE ReportID; 57 | 58 | BYTE SetBrightness; 59 | 60 | BYTE Brightness; 61 | 62 | } CrosKBLightSettingsReport; 63 | #pragma pack() 64 | 65 | #endif 66 | #pragma once 67 | -------------------------------------------------------------------------------- /croskblight/Trace.h: -------------------------------------------------------------------------------- 1 | /*++ 2 | 3 | Module Name: 4 | 5 | Trace.h 6 | 7 | Abstract: 8 | 9 | Header file for the debug tracing related function defintions and macros. 10 | 11 | Environment: 12 | 13 | Kernel mode 14 | 15 | --*/ 16 | 17 | // 18 | // Define the tracing flags. 19 | // 20 | // Tracing GUID - 79182672-fe97-45b4-bd65-fe00a2fb2477 21 | // 22 | 23 | #define WPP_CONTROL_GUIDS \ 24 | WPP_DEFINE_CONTROL_GUID( \ 25 | croskeyboard3TraceGuid, (79182672,fe97,45b4,bd65,fe00a2fb2477), \ 26 | \ 27 | WPP_DEFINE_BIT(MYDRIVER_ALL_INFO) \ 28 | WPP_DEFINE_BIT(TRACE_DRIVER) \ 29 | WPP_DEFINE_BIT(TRACE_DEVICE) \ 30 | WPP_DEFINE_BIT(TRACE_QUEUE) \ 31 | ) 32 | 33 | #define WPP_FLAG_LEVEL_LOGGER(flag, level) \ 34 | WPP_LEVEL_LOGGER(flag) 35 | 36 | #define WPP_FLAG_LEVEL_ENABLED(flag, level) \ 37 | (WPP_LEVEL_ENABLED(flag) && \ 38 | WPP_CONTROL(WPP_BIT_ ## flag).Level >= level) 39 | 40 | #define WPP_LEVEL_FLAGS_LOGGER(lvl,flags) \ 41 | WPP_LEVEL_LOGGER(flags) 42 | 43 | #define WPP_LEVEL_FLAGS_ENABLED(lvl, flags) \ 44 | (WPP_LEVEL_ENABLED(flags) && WPP_CONTROL(WPP_BIT_ ## flags).Level >= lvl) 45 | 46 | // 47 | // This comment block is scanned by the trace preprocessor to define our 48 | // Trace function. 49 | // 50 | // begin_wpp config 51 | // FUNC Trace{FLAG=MYDRIVER_ALL_INFO}(LEVEL, MSG, ...); 52 | // FUNC TraceEvents(LEVEL, FLAGS, MSG, ...); 53 | // end_wpp 54 | // 55 | -------------------------------------------------------------------------------- /croskblight/croskblight.inf: -------------------------------------------------------------------------------- 1 | ;/*++ 2 | ; 3 | ;Copyright (c) CoolStar. All rights reserved. 4 | ; 5 | ;Module Name: 6 | ; coolstar.inf 7 | ; 8 | ;Abstract: 9 | ; INF file for installing the CrosKeyboard Backlight Driver 10 | ; 11 | ; 12 | ;--*/ 13 | 14 | [Version] 15 | Signature = "$WINDOWS NT$" 16 | Class = HIDClass 17 | ClassGuid = {745a17a0-74d3-11d0-b6fe-00a0c90f57da} 18 | Provider = CoolStar 19 | DriverVer = 06/21/2006,6.1.7779.2 20 | CatalogFile = croskblight.cat 21 | PnpLockdown=1 22 | 23 | [DestinationDirs] 24 | DefaultDestDir = 12 25 | 26 | ; ================= Class section ===================== 27 | 28 | [SourceDisksNames] 29 | 1 = %DiskId1%,,,"" 30 | 31 | [SourceDisksFiles] 32 | croskblight.sys = 1,, 33 | 34 | ;***************************************** 35 | ; CrosKbLight Install Section 36 | ;***************************************** 37 | 38 | [Manufacturer] 39 | %StdMfg%=Standard,NT$ARCH$ 40 | 41 | ; Decorated model section take precedence over undecorated 42 | ; ones on XP and later. 43 | [Standard.NT$ARCH$] 44 | %CrosKBLight.DeviceDesc%=CrosKBLight_Device, ACPI\GOOG000C 45 | 46 | [CrosKBLight_Device.NT] 47 | CopyFiles=Drivers_Dir 48 | 49 | [CrosKBLight_Device.NT.HW] 50 | AddReg=CrosKBLight_AddReg 51 | 52 | [Drivers_Dir] 53 | croskblight.sys 54 | 55 | [CrosKBLight_AddReg] 56 | ; Set to 1 to connect the first interrupt resource found, 0 to leave disconnected 57 | HKR,Settings,"ConnectInterrupt",0x00010001,0 58 | HKR,,"UpperFilters",0x00010000,"mshidkmdf" 59 | 60 | ;-------------- Service installation 61 | [CrosKBLight_Device.NT.Services] 62 | AddService = CrosKBLight,%SPSVCINST_ASSOCSERVICE%, CrosKBLight_Service_Inst 63 | 64 | ; -------------- CrosKBLight driver install sections 65 | [CrosKBLight_Service_Inst] 66 | DisplayName = %CrosKBLight.SVCDESC% 67 | ServiceType = 1 ; SERVICE_KERNEL_DRIVER 68 | StartType = 3 ; SERVICE_DEMAND_START 69 | ErrorControl = 1 ; SERVICE_ERROR_NORMAL 70 | ServiceBinary = %12%\croskblight.sys 71 | LoadOrderGroup = Base 72 | 73 | [Strings] 74 | SPSVCINST_ASSOCSERVICE= 0x00000002 75 | StdMfg = "CoolStar" 76 | DiskId1 = "CrosKBLight Installation Disk #1" 77 | CrosKBLight.DeviceDesc = "Chromebook Keyboard Backlight (Wilco)" 78 | CrosKBLight.SVCDESC = "CrosKBLight Service" 79 | -------------------------------------------------------------------------------- /croskblight.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "croskblight", "croskblight\croskblight.vcxproj", "{E0C3F023-6A54-419C-9C6E-E0638B55D99F}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "croskblight Package", "croskblight Package\croskblight Package.vcxproj", "{D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|x64 = Debug|x64 13 | Debug|x86 = Debug|x86 14 | Release|x64 = Release|x64 15 | Release|x86 = Release|x86 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Debug|x64.ActiveCfg = Debug|x64 19 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Debug|x64.Build.0 = Debug|x64 20 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Debug|x86.ActiveCfg = Debug|Win32 21 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Debug|x86.Build.0 = Debug|Win32 22 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Release|x64.ActiveCfg = Release|x64 23 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Release|x64.Build.0 = Release|x64 24 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Release|x86.ActiveCfg = Release|Win32 25 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F}.Release|x86.Build.0 = Release|Win32 26 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Debug|x64.ActiveCfg = Debug|x64 27 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Debug|x64.Build.0 = Debug|x64 28 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Debug|x64.Deploy.0 = Debug|x64 29 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Debug|x86.ActiveCfg = Debug|Win32 30 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Debug|x86.Build.0 = Debug|Win32 31 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Debug|x86.Deploy.0 = Debug|Win32 32 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Release|x64.ActiveCfg = Release|x64 33 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Release|x64.Build.0 = Release|x64 34 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Release|x64.Deploy.0 = Release|x64 35 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Release|x86.ActiveCfg = Release|Win32 36 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Release|x86.Build.0 = Release|Win32 37 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F}.Release|x86.Deploy.0 = Release|Win32 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | EndGlobal 43 | -------------------------------------------------------------------------------- /croskblight/eccmds.h: -------------------------------------------------------------------------------- 1 | #ifndef __CROS_EC_REGS_H__ 2 | #define __CROS_EC_REGS_H__ 3 | 4 | #define BIT(nr) (1UL << (nr)) 5 | 6 | /* Message flags for using the mailbox() interface */ 7 | #define WILCO_EC_FLAG_NO_RESPONSE BIT(0) /* EC does not respond */ 8 | 9 | /* Normal commands have a maximum 32 bytes of data */ 10 | #define EC_MAILBOX_DATA_SIZE 32 11 | 12 | #include 13 | 14 | /** 15 | * struct wilco_ec_request - Mailbox request message format. 16 | * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION 17 | * @checksum: Sum of all bytes must be 0. 18 | * @mailbox_id: Mailbox identifier, specifies the command set. 19 | * @mailbox_version: Mailbox interface version %EC_MAILBOX_VERSION 20 | * @reserved: Set to zero. 21 | * @data_size: Length of following data. 22 | */ 23 | struct wilco_ec_request { 24 | UINT8 struct_version; 25 | UINT8 checksum; 26 | UINT16 mailbox_id; 27 | UINT8 mailbox_version; 28 | UINT8 reserved; 29 | UINT16 data_size; 30 | }; 31 | 32 | /** 33 | * struct wilco_ec_response - Mailbox response message format. 34 | * @struct_version: Should be %EC_MAILBOX_PROTO_VERSION 35 | * @checksum: Sum of all bytes must be 0. 36 | * @result: Result code from the EC. Non-zero indicates an error. 37 | * @data_size: Length of the response data buffer. 38 | * @reserved: Set to zero. 39 | * @data: Response data buffer. Max size is %EC_MAILBOX_DATA_SIZE_EXTENDED. 40 | */ 41 | struct wilco_ec_response { 42 | UINT8 struct_version; 43 | UINT8 checksum; 44 | UINT16 result; 45 | UINT16 data_size; 46 | UINT8 reserved[2]; 47 | UINT8 data[]; 48 | }; 49 | 50 | #include 51 | 52 | /** 53 | * enum wilco_ec_msg_type - Message type to select a set of command codes. 54 | * @WILCO_EC_MSG_LEGACY: Legacy EC messages for standard EC behavior. 55 | * @WILCO_EC_MSG_PROPERTY: Get/Set/Sync EC controlled NVRAM property. 56 | * @WILCO_EC_MSG_TELEMETRY: Request telemetry data from the EC. 57 | */ 58 | enum wilco_ec_msg_type { 59 | WILCO_EC_MSG_LEGACY = 0x00f0, 60 | WILCO_EC_MSG_PROPERTY = 0x00f2, 61 | WILCO_EC_MSG_TELEMETRY = 0x00f5, 62 | }; 63 | 64 | /** 65 | * struct wilco_ec_message - Request and response message. 66 | * @type: Mailbox message type. 67 | * @flags: Message flags, e.g. %WILCO_EC_FLAG_NO_RESPONSE. 68 | * @request_size: Number of bytes to send to the EC. 69 | * @request_data: Buffer containing the request data. 70 | * @response_size: Number of bytes to read from EC. 71 | * @response_data: Buffer containing the response data, should be 72 | * response_size bytes and allocated by caller. 73 | */ 74 | struct wilco_ec_message { 75 | enum wilco_ec_msg_type type; 76 | UINT8 flags; 77 | size_t request_size; 78 | void* request_data; 79 | size_t response_size; 80 | void* response_data; 81 | }; 82 | 83 | #endif /* __CROS_EC_REGS_H__ */ -------------------------------------------------------------------------------- /croskblight/croskblight.h: -------------------------------------------------------------------------------- 1 | #if !defined(_CROSKEYBOARD_H_) 2 | #define _CROSKEYBOARD_H_ 3 | 4 | #pragma warning(disable:4200) // suppress nameless struct/union warning 5 | #pragma warning(disable:4201) // suppress nameless struct/union warning 6 | #pragma warning(disable:4214) // suppress bit field types other than int warning 7 | #include 8 | #include 9 | 10 | #pragma warning(default:4200) 11 | #pragma warning(default:4201) 12 | #pragma warning(default:4214) 13 | #include 14 | 15 | #include 16 | 17 | #pragma warning(disable:4201) // suppress nameless struct/union warning 18 | #pragma warning(disable:4214) // suppress bit field types other than int warning 19 | #include 20 | 21 | #include "hidcommon.h" 22 | #include "eccmds.h" 23 | 24 | #ifdef __cplusplus 25 | extern "C" 26 | #endif 27 | NTSTATUS 28 | DriverEntry( 29 | _In_ PDRIVER_OBJECT pDriverObject, 30 | _In_ PUNICODE_STRING pRegistryPath 31 | ); 32 | 33 | EVT_WDF_DRIVER_DEVICE_ADD OnDeviceAdd; 34 | EVT_WDF_OBJECT_CONTEXT_CLEANUP OnDriverCleanup; 35 | 36 | // 37 | // String definitions 38 | // 39 | 40 | #define DRIVERNAME "croskblight.sys: " 41 | 42 | #define CROSKBLIGHT_POOL_TAG (ULONG) 'lbkC' 43 | #define CROSKBLIGHT_HARDWARE_IDS L"CoolStar\\CrosKBLight\0\0" 44 | #define CROSKBLIGHT_HARDWARE_IDS_LENGTH sizeof(CROSKBLIGHT_HARDWARE_IDS) 45 | 46 | #define NTDEVICE_NAME_STRING L"\\Device\\CrosKBLight" 47 | #define SYMBOLIC_NAME_STRING L"\\DosDevices\\CrosKBLight" 48 | // 49 | // This is the default report descriptor for the Hid device provided 50 | // by the mini driver in response to IOCTL_HID_GET_REPORT_DESCRIPTOR. 51 | // 52 | 53 | typedef UCHAR HID_REPORT_DESCRIPTOR, *PHID_REPORT_DESCRIPTOR; 54 | 55 | #ifdef DESCRIPTOR_DEF 56 | HID_REPORT_DESCRIPTOR DefaultReportDescriptor[] = { 57 | 0x06, 0x00, 0xff, // USAGE_PAGE (Vendor Defined Page 1) 58 | 0x09, 0x01, // USAGE (Vendor Usage 1) 59 | 0xa1, 0x01, // COLLECTION (Application) 60 | 0x85, REPORTID_KBLIGHT, // REPORT_ID (Keyboard Backlight) 61 | 0x15, 0x00, // LOGICAL_MINIMUM (0) 62 | 0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (256) 63 | 0x75, 0x08, // REPORT_SIZE (8) - bits 64 | 0x95, 0x01, // REPORT_COUNT (1) - Bytes 65 | 0x09, 0x02, // USAGE (Vendor Usage 1) 66 | 0x91, 0x02, // OUTPUT (Data,Var,Abs) 67 | 0x09, 0x03, // USAGE (Vendor Usage 2) 68 | 0x91, 0x02, // OUTPUT (Data,Var,Abs) 69 | 0x09, 0x02, // USAGE (Vendor Usage 1) 70 | 0x81, 0x02, // INPUT (Data,Var,Abs) 71 | 0xc0, // END_COLLECTION 72 | }; 73 | 74 | 75 | // 76 | // This is the default HID descriptor returned by the mini driver 77 | // in response to IOCTL_HID_GET_DEVICE_DESCRIPTOR. The size 78 | // of report descriptor is currently the size of DefaultReportDescriptor. 79 | // 80 | 81 | CONST HID_DESCRIPTOR DefaultHidDescriptor = { 82 | 0x09, // length of HID descriptor 83 | 0x21, // descriptor type == HID 0x21 84 | 0x0100, // hid spec release 85 | 0x00, // country code == Not Specified 86 | 0x01, // number of HID class descriptors 87 | { 0x22, // descriptor type 88 | sizeof(DefaultReportDescriptor) } // total length of report descriptor 89 | }; 90 | #endif 91 | 92 | #define true 1 93 | #define false 0 94 | 95 | typedef struct _EC_PORT { 96 | PHYSICAL_ADDRESS Start; 97 | ULONG Length; 98 | } ECPort, *PECPort; 99 | 100 | typedef struct _CROSKBLIGHT_CONTEXT 101 | { 102 | WDFDEVICE FxDevice; 103 | 104 | WDFQUEUE ReportQueue; 105 | 106 | UINT8 currentBrightness; 107 | 108 | //S0IX Notify 109 | ACPI_INTERFACE_STANDARD2 S0ixNotifyAcpiInterface; 110 | 111 | WDFWAITLOCK ecLock; 112 | 113 | BOOLEAN ledExists; 114 | 115 | ECPort ecIoData; 116 | ECPort ecIoCommand; 117 | ECPort ecIoPacket; 118 | PVOID dataBuffer; 119 | 120 | WDFIOTARGET busIoTarget; 121 | 122 | } CROSKBLIGHT_CONTEXT, *PCROSKBLIGHT_CONTEXT; 123 | 124 | WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(CROSKBLIGHT_CONTEXT, GetDeviceContext) 125 | 126 | // 127 | // Function definitions 128 | // 129 | 130 | DRIVER_INITIALIZE DriverEntry; 131 | 132 | EVT_WDF_DRIVER_UNLOAD CrosKBLightDriverUnload; 133 | 134 | EVT_WDF_DRIVER_DEVICE_ADD CrosKBLightEvtDeviceAdd; 135 | 136 | EVT_WDFDEVICE_WDM_IRP_PREPROCESS CrosKBLightEvtWdmPreprocessMnQueryId; 137 | 138 | EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL CrosKBLightEvtInternalDeviceControl; 139 | 140 | NTSTATUS 141 | CrosKBLightGetHidDescriptor( 142 | IN WDFDEVICE Device, 143 | IN WDFREQUEST Request 144 | ); 145 | 146 | NTSTATUS 147 | CrosKBLightGetReportDescriptor( 148 | IN WDFDEVICE Device, 149 | IN WDFREQUEST Request 150 | ); 151 | 152 | NTSTATUS 153 | CrosKBLightGetDeviceAttributes( 154 | IN WDFREQUEST Request 155 | ); 156 | 157 | NTSTATUS 158 | CrosKBLightGetString( 159 | IN WDFREQUEST Request 160 | ); 161 | 162 | NTSTATUS 163 | CrosKBLightWriteReport( 164 | IN PCROSKBLIGHT_CONTEXT DevContext, 165 | IN WDFREQUEST Request 166 | ); 167 | 168 | NTSTATUS 169 | CrosKBLightProcessVendorReport( 170 | IN PCROSKBLIGHT_CONTEXT DevContext, 171 | IN PVOID ReportBuffer, 172 | IN ULONG ReportBufferLen, 173 | OUT size_t* BytesWritten 174 | ); 175 | 176 | NTSTATUS 177 | CrosKBLightReadReport( 178 | IN PCROSKBLIGHT_CONTEXT DevContext, 179 | IN WDFREQUEST Request, 180 | OUT BOOLEAN* CompleteRequest 181 | ); 182 | 183 | NTSTATUS 184 | CrosKBLightSetFeature( 185 | IN PCROSKBLIGHT_CONTEXT DevContext, 186 | IN WDFREQUEST Request, 187 | OUT BOOLEAN* CompleteRequest 188 | ); 189 | 190 | NTSTATUS 191 | CrosKBLightGetFeature( 192 | IN PCROSKBLIGHT_CONTEXT DevContext, 193 | IN WDFREQUEST Request, 194 | OUT BOOLEAN* CompleteRequest 195 | ); 196 | 197 | PCHAR 198 | DbgHidInternalIoctlString( 199 | IN ULONG IoControlCode 200 | ); 201 | 202 | // 203 | // Helper macros 204 | // 205 | 206 | #define DEBUG_LEVEL_ERROR 1 207 | #define DEBUG_LEVEL_INFO 2 208 | #define DEBUG_LEVEL_VERBOSE 3 209 | 210 | #define DBG_INIT 1 211 | #define DBG_PNP 2 212 | #define DBG_IOCTL 4 213 | 214 | #if DBG 215 | #define CrosKBLightPrint(dbglevel, dbgcatagory, fmt, ...) { \ 216 | if (CrosKBLightDebugLevel >= dbglevel && \ 217 | (CrosKBLightDebugCatagories && dbgcatagory)) \ 218 | { \ 219 | DbgPrint(DRIVERNAME); \ 220 | DbgPrint(fmt, __VA_ARGS__); \ 221 | } \ 222 | } 223 | #else 224 | #define CrosKBLightPrint(dbglevel, fmt, ...) { \ 225 | } 226 | #endif 227 | 228 | #endif 229 | #pragma once 230 | -------------------------------------------------------------------------------- /croskblight Package/croskblight Package.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | {e0c3f023-6a54-419c-9c6e-e0638b55d99f} 24 | 25 | 26 | 27 | {D47D5E26-2CB1-4445-8B46-6D1AC3E0694F} 28 | {4605da2c-74a5-4865-98e1-152ef136825f} 29 | v4.5 30 | 12.0 31 | Debug 32 | Win32 33 | croskblight_Package 34 | $(LatestTargetPlatformVersion) 35 | croskblight Package 36 | 37 | 38 | 39 | Windows10 40 | true 41 | WindowsKernelModeDriver10.0 42 | Utility 43 | Package 44 | true 45 | 46 | 47 | Windows10 48 | false 49 | WindowsKernelModeDriver10.0 50 | Utility 51 | Package 52 | true 53 | 54 | 55 | Windows10 56 | true 57 | WindowsKernelModeDriver10.0 58 | Utility 59 | Package 60 | true 61 | 62 | 63 | Windows10 64 | false 65 | WindowsKernelModeDriver10.0 66 | Utility 67 | Package 68 | true 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | DbgengKernelDebugger 80 | 81 | 82 | 83 | False 84 | False 85 | True 86 | 87 | 133563 88 | 89 | 90 | DbgengKernelDebugger 91 | 92 | 93 | 94 | False 95 | False 96 | True 97 | 98 | 133563 99 | 100 | 101 | DbgengKernelDebugger 102 | 103 | 104 | 105 | False 106 | False 107 | True 108 | 109 | 133563 110 | 111 | 112 | DbgengKernelDebugger 113 | 114 | 115 | 116 | False 117 | False 118 | True 119 | 120 | 133563 121 | 122 | 123 | 124 | SHA256 125 | 126 | 127 | 128 | 129 | SHA256 130 | 131 | 132 | 133 | 134 | SHA256 135 | 136 | 137 | 138 | 139 | SHA256 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | bld/ 24 | [Bb]in/ 25 | [Oo]bj/ 26 | [Ll]og/ 27 | 28 | # Visual Studio 2015/2017 cache/options directory 29 | .vs/ 30 | # Uncomment if you have tasks that create the project's static files in wwwroot 31 | #wwwroot/ 32 | 33 | # Visual Studio 2017 auto generated files 34 | Generated\ Files/ 35 | 36 | # MSTest test Results 37 | [Tt]est[Rr]esult*/ 38 | [Bb]uild[Ll]og.* 39 | 40 | # NUNIT 41 | *.VisualState.xml 42 | TestResult.xml 43 | 44 | # Build Results of an ATL Project 45 | [Dd]ebugPS/ 46 | [Rr]eleasePS/ 47 | dlldata.c 48 | 49 | # Benchmark Results 50 | BenchmarkDotNet.Artifacts/ 51 | 52 | # .NET Core 53 | project.lock.json 54 | project.fragment.lock.json 55 | artifacts/ 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_h.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *_wpftmp.csproj 81 | *.log 82 | *.vspscc 83 | *.vssscc 84 | .builds 85 | *.pidb 86 | *.svclog 87 | *.scc 88 | 89 | # Chutzpah Test files 90 | _Chutzpah* 91 | 92 | # Visual C++ cache files 93 | ipch/ 94 | *.aps 95 | *.ncb 96 | *.opendb 97 | *.opensdf 98 | *.sdf 99 | *.cachefile 100 | *.VC.db 101 | *.VC.VC.opendb 102 | 103 | # Visual Studio profiler 104 | *.psess 105 | *.vsp 106 | *.vspx 107 | *.sap 108 | 109 | # Visual Studio Trace Files 110 | *.e2e 111 | 112 | # TFS 2012 Local Workspace 113 | $tf/ 114 | 115 | # Guidance Automation Toolkit 116 | *.gpState 117 | 118 | # ReSharper is a .NET coding add-in 119 | _ReSharper*/ 120 | *.[Rr]e[Ss]harper 121 | *.DotSettings.user 122 | 123 | # JustCode is a .NET coding add-in 124 | .JustCode 125 | 126 | # TeamCity is a build add-in 127 | _TeamCity* 128 | 129 | # DotCover is a Code Coverage Tool 130 | *.dotCover 131 | 132 | # AxoCover is a Code Coverage Tool 133 | .axoCover/* 134 | !.axoCover/settings.json 135 | 136 | # Visual Studio code coverage results 137 | *.coverage 138 | *.coveragexml 139 | 140 | # NCrunch 141 | _NCrunch_* 142 | .*crunch*.local.xml 143 | nCrunchTemp_* 144 | 145 | # MightyMoose 146 | *.mm.* 147 | AutoTest.Net/ 148 | 149 | # Web workbench (sass) 150 | .sass-cache/ 151 | 152 | # Installshield output folder 153 | [Ee]xpress/ 154 | 155 | # DocProject is a documentation generator add-in 156 | DocProject/buildhelp/ 157 | DocProject/Help/*.HxT 158 | DocProject/Help/*.HxC 159 | DocProject/Help/*.hhc 160 | DocProject/Help/*.hhk 161 | DocProject/Help/*.hhp 162 | DocProject/Help/Html2 163 | DocProject/Help/html 164 | 165 | # Click-Once directory 166 | publish/ 167 | 168 | # Publish Web Output 169 | *.[Pp]ublish.xml 170 | *.azurePubxml 171 | # Note: Comment the next line if you want to checkin your web deploy settings, 172 | # but database connection strings (with potential passwords) will be unencrypted 173 | *.pubxml 174 | *.publishproj 175 | 176 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 177 | # checkin your Azure Web App publish settings, but sensitive information contained 178 | # in these scripts will be unencrypted 179 | PublishScripts/ 180 | 181 | # NuGet Packages 182 | *.nupkg 183 | # The packages folder can be ignored because of Package Restore 184 | **/[Pp]ackages/* 185 | # except build/, which is used as an MSBuild target. 186 | !**/[Pp]ackages/build/ 187 | # Uncomment if necessary however generally it will be regenerated when needed 188 | #!**/[Pp]ackages/repositories.config 189 | # NuGet v3's project.json files produces more ignorable files 190 | *.nuget.props 191 | *.nuget.targets 192 | 193 | # Microsoft Azure Build Output 194 | csx/ 195 | *.build.csdef 196 | 197 | # Microsoft Azure Emulator 198 | ecf/ 199 | rcf/ 200 | 201 | # Windows Store app package directories and files 202 | AppPackages/ 203 | BundleArtifacts/ 204 | Package.StoreAssociation.xml 205 | _pkginfo.txt 206 | *.appx 207 | 208 | # Visual Studio cache files 209 | # files ending in .cache can be ignored 210 | *.[Cc]ache 211 | # but keep track of directories ending in .cache 212 | !*.[Cc]ache/ 213 | 214 | # Others 215 | ClientBin/ 216 | ~$* 217 | *~ 218 | *.dbmdl 219 | *.dbproj.schemaview 220 | *.jfm 221 | *.pfx 222 | *.publishsettings 223 | orleans.codegen.cs 224 | 225 | # Including strong name files can present a security risk 226 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 227 | #*.snk 228 | 229 | # Since there are multiple workflows, uncomment next line to ignore bower_components 230 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 231 | #bower_components/ 232 | 233 | # RIA/Silverlight projects 234 | Generated_Code/ 235 | 236 | # Backup & report files from converting an old project file 237 | # to a newer Visual Studio version. Backup files are not needed, 238 | # because we have git ;-) 239 | _UpgradeReport_Files/ 240 | Backup*/ 241 | UpgradeLog*.XML 242 | UpgradeLog*.htm 243 | ServiceFabricBackup/ 244 | *.rptproj.bak 245 | 246 | # SQL Server files 247 | *.mdf 248 | *.ldf 249 | *.ndf 250 | 251 | # Business Intelligence projects 252 | *.rdl.data 253 | *.bim.layout 254 | *.bim_*.settings 255 | *.rptproj.rsuser 256 | 257 | # Microsoft Fakes 258 | FakesAssemblies/ 259 | 260 | # GhostDoc plugin setting file 261 | *.GhostDoc.xml 262 | 263 | # Node.js Tools for Visual Studio 264 | .ntvs_analysis.dat 265 | node_modules/ 266 | 267 | # Visual Studio 6 build log 268 | *.plg 269 | 270 | # Visual Studio 6 workspace options file 271 | *.opt 272 | 273 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 274 | *.vbw 275 | 276 | # Visual Studio LightSwitch build output 277 | **/*.HTMLClient/GeneratedArtifacts 278 | **/*.DesktopClient/GeneratedArtifacts 279 | **/*.DesktopClient/ModelManifest.xml 280 | **/*.Server/GeneratedArtifacts 281 | **/*.Server/ModelManifest.xml 282 | _Pvt_Extensions 283 | 284 | # Paket dependency manager 285 | .paket/paket.exe 286 | paket-files/ 287 | 288 | # FAKE - F# Make 289 | .fake/ 290 | 291 | # JetBrains Rider 292 | .idea/ 293 | *.sln.iml 294 | 295 | # CodeRush personal settings 296 | .cr/personal 297 | 298 | # Python Tools for Visual Studio (PTVS) 299 | __pycache__/ 300 | *.pyc 301 | 302 | # Cake - Uncomment if you are using it 303 | # tools/** 304 | # !tools/packages.config 305 | 306 | # Tabs Studio 307 | *.tss 308 | 309 | # Telerik's JustMock configuration file 310 | *.jmconfig 311 | 312 | # BizTalk build output 313 | *.btp.cs 314 | *.btm.cs 315 | *.odx.cs 316 | *.xsd.cs 317 | 318 | # OpenCover UI analysis results 319 | OpenCover/ 320 | 321 | # Azure Stream Analytics local run output 322 | ASALocalRun/ 323 | 324 | # MSBuild Binary and Structured Log 325 | *.binlog 326 | 327 | # NVidia Nsight GPU debugger configuration file 328 | *.nvuser 329 | 330 | # MFractors (Xamarin productivity tool) working folder 331 | .mfractor/ 332 | 333 | # Local History for Visual Studio 334 | .localhistory/ 335 | -------------------------------------------------------------------------------- /croskblight/croskblight.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | {E0C3F023-6A54-419C-9C6E-E0638B55D99F} 40 | {497e31cb-056b-4f31-abb8-447fd55ee5a5} 41 | v4.5 42 | 12.0 43 | Debug 44 | Win32 45 | croskeyboard3 46 | $(LatestTargetPlatformVersion) 47 | croskblight 48 | 49 | 50 | 51 | Windows10 52 | true 53 | WindowsKernelModeDriver10.0 54 | Driver 55 | KMDF 56 | 57 | 58 | 59 | 60 | Windows10 61 | false 62 | WindowsKernelModeDriver10.0 63 | Driver 64 | KMDF 65 | 66 | 67 | 68 | 69 | Windows10 70 | true 71 | WindowsKernelModeDriver10.0 72 | Driver 73 | KMDF 74 | 75 | 76 | 77 | 78 | Windows10 79 | false 80 | WindowsKernelModeDriver10.0 81 | Driver 82 | KMDF 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | DbgengKernelDebugger 96 | 97 | 98 | DbgengKernelDebugger 99 | 100 | 101 | DbgengKernelDebugger 102 | 103 | 104 | DbgengKernelDebugger 105 | 106 | 107 | 108 | true 109 | true 110 | trace.h 111 | true 112 | 113 | 114 | SHA256 115 | 116 | 117 | 1.0.1 118 | 119 | 120 | 121 | 122 | true 123 | true 124 | trace.h 125 | true 126 | 127 | 128 | SHA256 129 | 130 | 131 | 1.0.1 132 | 133 | 134 | 135 | 136 | true 137 | true 138 | trace.h 139 | true 140 | false 141 | 142 | 143 | SHA256 144 | 145 | 146 | 1.0.1 147 | 148 | 149 | 150 | 151 | true 152 | true 153 | trace.h 154 | true 155 | false 156 | Level4 157 | 158 | 159 | 1.0.1 160 | 161 | 162 | SHA256 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | -------------------------------------------------------------------------------- /croskblight/comm-mec_lpc.c: -------------------------------------------------------------------------------- 1 | #include "croskblight.h" 2 | 3 | static ULONG CrosKBLightDebugLevel = 100; 4 | static ULONG CrosKBLightDebugCatagories = DBG_INIT || DBG_PNP || DBG_IOCTL; 5 | 6 | /* Version of mailbox interface */ 7 | #define EC_MAILBOX_VERSION 0 8 | 9 | /* Command to start mailbox transaction */ 10 | #define EC_MAILBOX_START_COMMAND 0xda 11 | 12 | /* Version of EC protocol */ 13 | #define EC_MAILBOX_PROTO_VERSION 3 14 | 15 | /* Number of header bytes to be counted as data bytes */ 16 | #define EC_MAILBOX_DATA_EXTRA 2 17 | 18 | /* Maximum timeout */ 19 | #define EC_MAILBOX_TIMEOUT 1 20 | 21 | /* EC response flags */ 22 | #define EC_CMDR_DATA BIT(0) /* Data ready for host to read */ 23 | #define EC_CMDR_PENDING BIT(1) /* Write pending to EC */ 24 | #define EC_CMDR_BUSY BIT(2) /* EC is busy processing a command */ 25 | #define EC_CMDR_CMD BIT(3) /* Last host write was a command */ 26 | 27 | 28 | static __inline void outb(unsigned char __val, unsigned int __port) { 29 | WRITE_PORT_UCHAR((PUCHAR)__port, __val); 30 | } 31 | 32 | static __inline void outw(unsigned short __val, unsigned int __port) { 33 | WRITE_PORT_USHORT((PUSHORT)__port, __val); 34 | } 35 | 36 | static __inline unsigned char inb(unsigned int __port) { 37 | return READ_PORT_UCHAR((PUCHAR)__port); 38 | } 39 | 40 | static __inline unsigned short inw(unsigned int __port) { 41 | return READ_PORT_USHORT((PUSHORT)__port); 42 | } 43 | 44 | FAST_MUTEX MecAccessMutex; 45 | 46 | int wait_for_ec(int status_addr, int timeout_usec); 47 | 48 | // Thanks @DHowett! 49 | 50 | typedef enum _ec_xfer_direction { EC_MEC_WRITE, EC_MEC_READ } ec_xfer_direction; 51 | 52 | enum cros_ec_lpc_mec_emi_access_mode { 53 | /* 8-bit access */ 54 | MEC_EC_BYTE_ACCESS = 0x0, 55 | /* 16-bit access */ 56 | MEC_EC_WORD_ACCESS = 0x1, 57 | /* 32-bit access */ 58 | MEC_EC_LONG_ACCESS = 0x2, 59 | /* 60 | * 32-bit access, read or write of MEC_EMI_EC_DATA_B3 causes the 61 | * EC data register to be incremented. 62 | */ 63 | MEC_EC_LONG_ACCESS_AUTOINCREMENT = 0x3, 64 | }; 65 | 66 | /* EMI registers are relative to base */ 67 | #define MEC_EMI_HOST_TO_EC(MEC_EMI_BASE) ((MEC_EMI_BASE) + 0) 68 | #define MEC_EMI_EC_TO_HOST(MEC_EMI_BASE) ((MEC_EMI_BASE) + 1) 69 | #define MEC_EMI_EC_ADDRESS_B0(MEC_EMI_BASE) ((MEC_EMI_BASE) + 2) 70 | #define MEC_EMI_EC_ADDRESS_B1(MEC_EMI_BASE) ((MEC_EMI_BASE) + 3) 71 | #define MEC_EMI_EC_DATA_B0(MEC_EMI_BASE) ((MEC_EMI_BASE) + 4) 72 | #define MEC_EMI_EC_DATA_B1(MEC_EMI_BASE) ((MEC_EMI_BASE) + 5) 73 | #define MEC_EMI_EC_DATA_B2(MEC_EMI_BASE) ((MEC_EMI_BASE) + 6) 74 | #define MEC_EMI_EC_DATA_B3(MEC_EMI_BASE) ((MEC_EMI_BASE) + 7) 75 | 76 | UINT16 mec_emi_base = 0, mec_emi_end = 0; 77 | 78 | static void ec_mec_emi_write_access(UINT16 address, enum cros_ec_lpc_mec_emi_access_mode access_type) { 79 | outw((address & 0xFFFC) | (UINT16)access_type, MEC_EMI_EC_ADDRESS_B0(mec_emi_base)); 80 | } 81 | 82 | static int ec_mec_xfer(ec_xfer_direction direction, UINT16 address, 83 | UINT8* data, UINT16 size) 84 | { 85 | if (mec_emi_base == 0 || mec_emi_end == 0) 86 | return 0; 87 | 88 | ExAcquireFastMutex(&MecAccessMutex); 89 | 90 | /* 91 | * There's a cleverer way to do this, but it's somewhat less clear what's happening. 92 | * I prefer clarity over cleverness. :) 93 | */ 94 | int pos = 0; 95 | UINT16 temp[2]; 96 | if (address % 4 > 0) { 97 | ec_mec_emi_write_access(address, MEC_EC_BYTE_ACCESS); 98 | /* Unaligned start address */ 99 | for (int i = address % 4; i < 4; ++i) { 100 | UINT8* storage = &data[pos++]; 101 | if (direction == EC_MEC_WRITE) 102 | outb(*storage, MEC_EMI_EC_DATA_B0(mec_emi_base) + i); 103 | else if (direction == EC_MEC_READ) 104 | *storage = inb(MEC_EMI_EC_DATA_B0(mec_emi_base) + i); 105 | } 106 | address = (address + 4) & 0xFFFC; 107 | } 108 | 109 | if (size - pos >= 4) { 110 | ec_mec_emi_write_access(address, MEC_EC_LONG_ACCESS_AUTOINCREMENT); 111 | while (size - pos >= 4) { 112 | if (direction == EC_MEC_WRITE) { 113 | memcpy(temp, &data[pos], sizeof(temp)); 114 | outw(temp[0], MEC_EMI_EC_DATA_B0(mec_emi_base)); 115 | outw(temp[1], MEC_EMI_EC_DATA_B2(mec_emi_base)); 116 | } 117 | else if (direction == EC_MEC_READ) { 118 | temp[0] = inw(MEC_EMI_EC_DATA_B0(mec_emi_base)); 119 | temp[1] = inw(MEC_EMI_EC_DATA_B2(mec_emi_base)); 120 | memcpy(&data[pos], temp, sizeof(temp)); 121 | } 122 | 123 | pos += 4; 124 | address += 4; 125 | } 126 | } 127 | 128 | if (size - pos > 0) { 129 | ec_mec_emi_write_access(address, MEC_EC_BYTE_ACCESS); 130 | for (int i = 0; i < (size - pos); ++i) { 131 | UINT8* storage = &data[pos + i]; 132 | if (direction == EC_MEC_WRITE) 133 | outb(*storage, MEC_EMI_EC_DATA_B0(mec_emi_base) + i); 134 | else if (direction == EC_MEC_READ) 135 | *storage = inb(MEC_EMI_EC_DATA_B0(mec_emi_base) + i); 136 | } 137 | } 138 | 139 | ExReleaseFastMutex(&MecAccessMutex); 140 | 141 | return 0; 142 | } 143 | 144 | /** 145 | * wilco_ec_response_timed_out() - Wait for EC response. 146 | * @ec: EC device. 147 | * 148 | * Return: true if EC timed out, false if EC did not time out. 149 | */ 150 | static BOOLEAN wilco_ec_response_timed_out(PCROSKBLIGHT_CONTEXT pDevice) 151 | { 152 | LARGE_INTEGER CurrentTime; 153 | KeQuerySystemTimePrecise(&CurrentTime); 154 | 155 | LARGE_INTEGER Timeout; 156 | Timeout.QuadPart = CurrentTime.QuadPart + (10 * 1000 * 1000); 157 | 158 | do { 159 | UINT8 readByte = inb(pDevice->ecIoCommand.Start.LowPart); 160 | if (!(readByte & 161 | (EC_CMDR_PENDING | EC_CMDR_BUSY))) 162 | return FALSE; 163 | 164 | LARGE_INTEGER Interval; 165 | Interval.QuadPart = -10 * 100; 166 | KeDelayExecutionThread(KernelMode, FALSE, &Interval); 167 | 168 | KeQuerySystemTimePrecise(&CurrentTime); 169 | } while (CurrentTime.QuadPart < Timeout.QuadPart); 170 | 171 | return TRUE; 172 | } 173 | 174 | /** 175 | * wilco_ec_checksum() - Compute 8-bit checksum over data range. 176 | * @data: Data to checksum. 177 | * @size: Number of bytes to checksum. 178 | * 179 | * Return: 8-bit checksum of provided data. 180 | */ 181 | static UINT8 wilco_ec_checksum(const void* data, size_t size) 182 | { 183 | UINT8* data_bytes = (UINT8*)data; 184 | UINT8 checksum = 0; 185 | size_t i; 186 | 187 | for (i = 0; i < size; i++) 188 | checksum += data_bytes[i]; 189 | 190 | return checksum; 191 | } 192 | 193 | NTSTATUS wilco_ec_mailbox(PCROSKBLIGHT_CONTEXT pDevice, struct wilco_ec_message *msg) { 194 | NTSTATUS status = STATUS_SUCCESS; 195 | struct wilco_ec_response* rs = pDevice->dataBuffer; 196 | UINT8 checksum, flag; 197 | WdfWaitLockAcquire(pDevice->ecLock, NULL); 198 | 199 | struct wilco_ec_request rq = { 0 }; 200 | rq.struct_version = EC_MAILBOX_PROTO_VERSION; 201 | rq.mailbox_id = msg->type; 202 | rq.mailbox_version = EC_MAILBOX_VERSION; 203 | rq.data_size = msg->request_size; 204 | 205 | /* Checksum header and data */ 206 | rq.checksum = wilco_ec_checksum(&rq, sizeof(rq)); 207 | rq.checksum += wilco_ec_checksum(msg->request_data, msg->request_size); 208 | rq.checksum = -rq.checksum; 209 | 210 | //Start transfer 211 | 212 | ec_mec_xfer(EC_MEC_WRITE, 0, &rq, sizeof(rq)); 213 | ec_mec_xfer(EC_MEC_WRITE, sizeof(rq), msg->request_data, msg->request_size); 214 | 215 | //Start the command 216 | outb(EC_MAILBOX_START_COMMAND, pDevice->ecIoCommand.Start.LowPart); 217 | 218 | /* For some commands (eg shutdown) the EC will not respond, that's OK */ 219 | if (msg->flags & WILCO_EC_FLAG_NO_RESPONSE) { 220 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_IOCTL, 221 | "EC does not respond to this command\n"); 222 | status = STATUS_SUCCESS; 223 | goto out; 224 | } 225 | 226 | /* Wait for it to complete */ 227 | if (wilco_ec_response_timed_out(pDevice)) { 228 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 229 | "response timed out\n"); 230 | status = STATUS_IO_TIMEOUT; 231 | goto out; 232 | } 233 | 234 | /* Check result */ 235 | flag = inb(pDevice->ecIoData.Start.LowPart); 236 | if (flag) { 237 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 238 | "bad response: 0x%02x\n", flag); 239 | status = STATUS_IO_DEVICE_ERROR; 240 | goto out; 241 | } 242 | 243 | /* Read back response */ 244 | ec_mec_xfer(EC_MEC_READ, 0, rs, sizeof(*rs) + EC_MAILBOX_DATA_SIZE); 245 | 246 | if (rs->result) { 247 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 248 | "EC reported failure: 0x%02x\n", rs->result); 249 | status = STATUS_IO_DEVICE_ERROR; 250 | goto out; 251 | } 252 | 253 | if (rs->data_size != EC_MAILBOX_DATA_SIZE) { 254 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 255 | "unexpected packet size (%u != %u)\n", 256 | rs->data_size, EC_MAILBOX_DATA_SIZE); 257 | status = STATUS_IO_DEVICE_ERROR; 258 | goto out; 259 | } 260 | 261 | if (rs->data_size < msg->response_size) { 262 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 263 | "EC didn't return enough data (%u < %zu)\n", 264 | rs->data_size, msg->response_size); 265 | status = STATUS_IO_DEVICE_ERROR; 266 | goto out; 267 | } 268 | 269 | RtlCopyMemory(msg->response_data, rs->data, msg->response_size); 270 | 271 | out: 272 | WdfWaitLockRelease(pDevice->ecLock); 273 | return status; 274 | } 275 | 276 | NTSTATUS comm_init_lpc_mec(PCROSKBLIGHT_CONTEXT pDevice) 277 | { 278 | /* This function assumes some setup was done by comm_init_lpc. */ 279 | 280 | ExInitializeFastMutex(&MecAccessMutex); 281 | 282 | mec_emi_base = pDevice->ecIoPacket.Start.LowPart; 283 | mec_emi_end = pDevice->ecIoPacket.Start.LowPart + EC_MAILBOX_DATA_SIZE; 284 | 285 | return STATUS_SUCCESS; 286 | } -------------------------------------------------------------------------------- /croskblight/croskblight.cpp: -------------------------------------------------------------------------------- 1 | #define DESCRIPTOR_DEF 2 | #include "croskblight.h" 3 | #include 4 | #include 5 | 6 | extern "C" NTSTATUS comm_init_lpc_mec(PCROSKBLIGHT_CONTEXT pDevice); 7 | extern "C" NTSTATUS wilco_ec_mailbox(PCROSKBLIGHT_CONTEXT pDevice, struct wilco_ec_message* msg); 8 | 9 | VOID 10 | CrosKBLightS0ixNotifyCallback( 11 | PCROSKBLIGHT_CONTEXT pDevice, 12 | ULONG NotifyCode); 13 | 14 | static ULONG CrosKBLightDebugLevel = 100; 15 | static ULONG CrosKBLightDebugCatagories = DBG_INIT || DBG_PNP || DBG_IOCTL; 16 | 17 | #define WILCO_EC_COMMAND_KBBL 0x75 18 | #define WILCO_KBBL_MODE_FLAG_PWM BIT(1) /* Set brightness by percent. */ 19 | #define WILCO_KBBL_DEFAULT_BRIGHTNESS 0 20 | 21 | enum wilco_kbbl_subcommand { 22 | WILCO_KBBL_SUBCMD_GET_FEATURES = 0x00, 23 | WILCO_KBBL_SUBCMD_GET_STATE = 0x01, 24 | WILCO_KBBL_SUBCMD_SET_STATE = 0x02, 25 | }; 26 | 27 | /** 28 | * struct wilco_keyboard_leds_msg - Message to/from EC for keyboard LED control. 29 | * @command: Always WILCO_EC_COMMAND_KBBL. 30 | * @status: Set by EC to 0 on success, 0xFF on failure. 31 | * @subcmd: One of enum wilco_kbbl_subcommand. 32 | * @reserved3: Should be 0. 33 | * @mode: Bit flags for used mode, we want to use WILCO_KBBL_MODE_FLAG_PWM. 34 | * @reserved5to8: Should be 0. 35 | * @percent: Brightness in 0-100. Only meaningful in PWM mode. 36 | * @reserved10to15: Should be 0. 37 | */ 38 | #include 39 | struct wilco_keyboard_leds_msg { 40 | UINT8 command; 41 | UINT8 status; 42 | UINT8 subcmd; 43 | UINT8 reserved3; 44 | UINT8 mode; 45 | UINT8 reserved5to8[4]; 46 | UINT8 percent; 47 | UINT8 reserved10to15[6]; 48 | }; 49 | #include 50 | 51 | /* Send a request, get a response, and check that the response is good. */ 52 | static NTSTATUS send_kbbl_msg(_In_ PCROSKBLIGHT_CONTEXT pDevice, 53 | struct wilco_keyboard_leds_msg* request, 54 | struct wilco_keyboard_leds_msg* response) 55 | { 56 | struct wilco_ec_message msg; 57 | NTSTATUS status; 58 | 59 | memset(&msg, 0, sizeof(msg)); 60 | msg.type = WILCO_EC_MSG_LEGACY; 61 | msg.request_data = request; 62 | msg.request_size = sizeof(*request); 63 | msg.response_data = response; 64 | msg.response_size = sizeof(*response); 65 | 66 | status = wilco_ec_mailbox(pDevice, &msg); 67 | if (!NT_SUCCESS(status)) { 68 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 69 | "Failed sending keyboard LEDs command: 0x%x\n", status); 70 | return status; 71 | } 72 | 73 | return status; 74 | } 75 | 76 | static NTSTATUS set_kbbl(_In_ PCROSKBLIGHT_CONTEXT pDevice, UINT8 brightness) 77 | { 78 | struct wilco_keyboard_leds_msg request; 79 | struct wilco_keyboard_leds_msg response; 80 | NTSTATUS status; 81 | 82 | memset(&request, 0, sizeof(request)); 83 | request.command = WILCO_EC_COMMAND_KBBL; 84 | request.subcmd = WILCO_KBBL_SUBCMD_SET_STATE; 85 | request.mode = WILCO_KBBL_MODE_FLAG_PWM; 86 | request.percent = brightness; 87 | 88 | status = send_kbbl_msg(pDevice, &request, &response); 89 | if (!NT_SUCCESS(status)) 90 | return status; 91 | 92 | if (response.status) { 93 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_INIT, 94 | "EC reported failure sending keyboard LEDs command: %d\n", 95 | response.status); 96 | return STATUS_IO_DEVICE_ERROR; 97 | } 98 | 99 | return status; 100 | } 101 | 102 | /** 103 | * kbbl_init() - Initialize the state of the keyboard backlight. 104 | * @ec: EC device to talk to. 105 | * 106 | * Gets the current brightness, ensuring that the BIOS already initialized the 107 | * backlight to PWM mode. If not in PWM mode, then the current brightness is 108 | * meaningless, so set the brightness to WILCO_KBBL_DEFAULT_BRIGHTNESS. 109 | * 110 | * Return: Final brightness of the keyboard, or negative error code on failure. 111 | */ 112 | static int kbbl_init(_In_ PCROSKBLIGHT_CONTEXT pDevice) 113 | { 114 | struct wilco_keyboard_leds_msg request; 115 | struct wilco_keyboard_leds_msg response; 116 | NTSTATUS status; 117 | 118 | memset(&request, 0, sizeof(request)); 119 | request.command = WILCO_EC_COMMAND_KBBL; 120 | request.subcmd = WILCO_KBBL_SUBCMD_GET_STATE; 121 | 122 | status = send_kbbl_msg(pDevice, &request, &response); 123 | if (!NT_SUCCESS(status)) 124 | return status; 125 | 126 | if (response.status) { 127 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_INIT, 128 | "EC reported failure sending keyboard LEDs command: %d\n", 129 | response.status); 130 | return STATUS_IO_DEVICE_ERROR; 131 | } 132 | 133 | if (response.mode & WILCO_KBBL_MODE_FLAG_PWM) { 134 | if (pDevice->currentBrightness == 0) 135 | pDevice->currentBrightness = response.percent; 136 | return STATUS_SUCCESS; 137 | } 138 | 139 | status = set_kbbl(pDevice, WILCO_KBBL_DEFAULT_BRIGHTNESS); 140 | if (!NT_SUCCESS(status)) 141 | return status; 142 | 143 | return STATUS_SUCCESS; 144 | } 145 | 146 | static NTSTATUS kbbl_exist(_In_ PCROSKBLIGHT_CONTEXT pDevice, BOOLEAN* exists) 147 | { 148 | struct wilco_keyboard_leds_msg request; 149 | struct wilco_keyboard_leds_msg response; 150 | NTSTATUS status; 151 | 152 | memset(&request, 0, sizeof(request)); 153 | request.command = WILCO_EC_COMMAND_KBBL; 154 | request.subcmd = WILCO_KBBL_SUBCMD_GET_FEATURES; 155 | 156 | status = send_kbbl_msg(pDevice, &request, &response); 157 | if (!NT_SUCCESS(status)) 158 | return status; 159 | 160 | *exists = response.status != 0xFF; 161 | 162 | return STATUS_SUCCESS; 163 | } 164 | 165 | NTSTATUS 166 | DriverEntry( 167 | __in PDRIVER_OBJECT DriverObject, 168 | __in PUNICODE_STRING RegistryPath 169 | ) 170 | { 171 | NTSTATUS status = STATUS_SUCCESS; 172 | WDF_DRIVER_CONFIG config; 173 | WDF_OBJECT_ATTRIBUTES attributes; 174 | 175 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_INIT, 176 | "Driver Entry"); 177 | 178 | WDF_DRIVER_CONFIG_INIT(&config, CrosKBLightEvtDeviceAdd); 179 | 180 | WDF_OBJECT_ATTRIBUTES_INIT(&attributes); 181 | 182 | // 183 | // Create a framework driver object to represent our driver. 184 | // 185 | 186 | status = WdfDriverCreate(DriverObject, 187 | RegistryPath, 188 | &attributes, 189 | &config, 190 | WDF_NO_HANDLE 191 | ); 192 | 193 | if (!NT_SUCCESS(status)) 194 | { 195 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_INIT, 196 | "WdfDriverCreate failed with status 0x%x\n", status); 197 | } 198 | 199 | return status; 200 | } 201 | 202 | NTSTATUS 203 | OnPrepareHardware( 204 | _In_ WDFDEVICE FxDevice, 205 | _In_ WDFCMRESLIST FxResourcesRaw, 206 | _In_ WDFCMRESLIST FxResourcesTranslated 207 | ) 208 | /*++ 209 | 210 | Routine Description: 211 | 212 | This routine caches the SPB resource connection ID. 213 | 214 | Arguments: 215 | 216 | FxDevice - a handle to the framework device object 217 | FxResourcesRaw - list of translated hardware resources that 218 | the PnP manager has assigned to the device 219 | FxResourcesTranslated - list of raw hardware resources that 220 | the PnP manager has assigned to the device 221 | 222 | Return Value: 223 | 224 | Status 225 | 226 | --*/ 227 | { 228 | PCROSKBLIGHT_CONTEXT pDevice = GetDeviceContext(FxDevice); 229 | NTSTATUS status = STATUS_SUCCESS; 230 | 231 | UNREFERENCED_PARAMETER(FxResourcesRaw); 232 | 233 | // 234 | // Parse the peripheral's resources. 235 | // 236 | 237 | ULONG resourceCount = WdfCmResourceListGetCount(FxResourcesTranslated); 238 | 239 | ULONG portsFound = 0; 240 | for (ULONG i = 0; i < resourceCount; i++) 241 | { 242 | PCM_PARTIAL_RESOURCE_DESCRIPTOR pDescriptor; 243 | UCHAR Class; 244 | UCHAR Type; 245 | 246 | pDescriptor = WdfCmResourceListGetDescriptor( 247 | FxResourcesTranslated, i); 248 | 249 | switch (pDescriptor->Type) 250 | { 251 | case CmResourceTypePort: 252 | // 253 | // Look for I2C or SPI resource and save connection ID. 254 | // 255 | switch (portsFound) { 256 | case 0: 257 | pDevice->ecIoData.Start = pDescriptor->u.Port.Start; 258 | pDevice->ecIoData.Length = pDescriptor->u.Port.Length; 259 | break; 260 | case 1: 261 | pDevice->ecIoCommand.Start = pDescriptor->u.Port.Start; 262 | pDevice->ecIoCommand.Length = pDescriptor->u.Port.Length; 263 | break; 264 | case 2: 265 | pDevice->ecIoPacket.Start = pDescriptor->u.Port.Start; 266 | pDevice->ecIoPacket.Length = pDescriptor->u.Port.Length; 267 | break; 268 | default: 269 | break; 270 | } 271 | 272 | portsFound++; 273 | break; 274 | default: 275 | // 276 | // Ignoring all other resource types. 277 | // 278 | break; 279 | } 280 | } 281 | 282 | if (portsFound < 3) { 283 | status = STATUS_NOT_FOUND; 284 | return status; 285 | } 286 | 287 | pDevice->dataBuffer = ExAllocatePoolZero(NonPagedPool, sizeof(struct wilco_ec_response) + EC_MAILBOX_DATA_SIZE, CROSKBLIGHT_POOL_TAG); 288 | if (!pDevice->dataBuffer) { 289 | status = STATUS_NO_MEMORY; 290 | return status; 291 | } 292 | 293 | status = comm_init_lpc_mec(pDevice); 294 | if (!NT_SUCCESS(status)) { 295 | return status; 296 | } 297 | 298 | status = kbbl_exist(pDevice, &pDevice->ledExists); 299 | if (!NT_SUCCESS(status)) { 300 | return status; 301 | } 302 | 303 | status = WdfFdoQueryForInterface(FxDevice, 304 | &GUID_ACPI_INTERFACE_STANDARD2, 305 | (PINTERFACE)&pDevice->S0ixNotifyAcpiInterface, 306 | sizeof(ACPI_INTERFACE_STANDARD2), 307 | 1, 308 | NULL); 309 | 310 | if (!NT_SUCCESS(status)) { 311 | return status; 312 | } 313 | 314 | status = pDevice->S0ixNotifyAcpiInterface.RegisterForDeviceNotifications( 315 | pDevice->S0ixNotifyAcpiInterface.Context, 316 | (PDEVICE_NOTIFY_CALLBACK2)CrosKBLightS0ixNotifyCallback, 317 | pDevice); 318 | if (!NT_SUCCESS(status)) { 319 | return status; 320 | } 321 | 322 | return status; 323 | } 324 | 325 | NTSTATUS 326 | OnReleaseHardware( 327 | _In_ WDFDEVICE FxDevice, 328 | _In_ WDFCMRESLIST FxResourcesTranslated 329 | ) 330 | /*++ 331 | 332 | Routine Description: 333 | 334 | Arguments: 335 | 336 | FxDevice - a handle to the framework device object 337 | FxResourcesTranslated - list of raw hardware resources that 338 | the PnP manager has assigned to the device 339 | 340 | Return Value: 341 | 342 | Status 343 | 344 | --*/ 345 | { 346 | PCROSKBLIGHT_CONTEXT pDevice = GetDeviceContext(FxDevice); 347 | NTSTATUS status = STATUS_SUCCESS; 348 | 349 | UNREFERENCED_PARAMETER(FxResourcesTranslated); 350 | 351 | if (pDevice->dataBuffer) { 352 | ExFreePoolWithTag(pDevice->dataBuffer, CROSKBLIGHT_POOL_TAG); 353 | } 354 | 355 | if (pDevice->S0ixNotifyAcpiInterface.Context) { //Used for S0ix notifications 356 | pDevice->S0ixNotifyAcpiInterface.UnregisterForDeviceNotifications(pDevice->S0ixNotifyAcpiInterface.Context); 357 | } 358 | 359 | return status; 360 | } 361 | 362 | NTSTATUS 363 | OnD0Entry( 364 | _In_ WDFDEVICE FxDevice, 365 | _In_ WDF_POWER_DEVICE_STATE FxPreviousState 366 | ) 367 | /*++ 368 | 369 | Routine Description: 370 | 371 | This routine allocates objects needed by the driver. 372 | 373 | Arguments: 374 | 375 | FxDevice - a handle to the framework device object 376 | FxPreviousState - previous power state 377 | 378 | Return Value: 379 | 380 | Status 381 | 382 | --*/ 383 | { 384 | UNREFERENCED_PARAMETER(FxPreviousState); 385 | 386 | PCROSKBLIGHT_CONTEXT pDevice = GetDeviceContext(FxDevice); 387 | NTSTATUS status = STATUS_SUCCESS; 388 | 389 | if (pDevice->ledExists) { 390 | status = kbbl_init(pDevice); 391 | if (!NT_SUCCESS(status)) { 392 | return status; 393 | } 394 | 395 | status = set_kbbl(pDevice, pDevice->currentBrightness); 396 | } 397 | 398 | return status; 399 | } 400 | 401 | NTSTATUS 402 | OnD0Exit( 403 | _In_ WDFDEVICE FxDevice, 404 | _In_ WDF_POWER_DEVICE_STATE FxTargetState 405 | ) 406 | /*++ 407 | 408 | Routine Description: 409 | 410 | This routine destroys objects needed by the driver. 411 | 412 | Arguments: 413 | 414 | FxDevice - a handle to the framework device object 415 | FxTargetState - target power state 416 | 417 | Return Value: 418 | 419 | Status 420 | 421 | --*/ 422 | { 423 | PCROSKBLIGHT_CONTEXT pDevice = GetDeviceContext(FxDevice); 424 | 425 | if (FxTargetState != WdfPowerDeviceD3Final && 426 | FxTargetState != WdfPowerDevicePrepareForHibernation) { 427 | if (pDevice->ledExists) { 428 | set_kbbl(pDevice, 0); 429 | } 430 | } 431 | 432 | return STATUS_SUCCESS; 433 | } 434 | 435 | VOID 436 | CrosKBLightS0ixNotifyCallback( 437 | PCROSKBLIGHT_CONTEXT pDevice, 438 | ULONG NotifyCode) { 439 | if (NotifyCode) { 440 | OnD0Exit(pDevice->FxDevice, WdfPowerDeviceD3); 441 | } 442 | else { 443 | OnD0Entry(pDevice->FxDevice, WdfPowerDeviceD3); 444 | } 445 | } 446 | 447 | static void update_brightness(PCROSKBLIGHT_CONTEXT pDevice, BYTE brightness) { 448 | _CROSKBLIGHT_GETLIGHT_REPORT report; 449 | report.ReportID = REPORTID_KBLIGHT; 450 | report.Brightness = brightness; 451 | 452 | size_t bytesWritten; 453 | CrosKBLightProcessVendorReport(pDevice, &report, sizeof(report), &bytesWritten); 454 | } 455 | 456 | NTSTATUS 457 | CrosKBLightEvtDeviceAdd( 458 | IN WDFDRIVER Driver, 459 | IN PWDFDEVICE_INIT DeviceInit 460 | ) 461 | { 462 | NTSTATUS status = STATUS_SUCCESS; 463 | WDF_IO_QUEUE_CONFIG queueConfig; 464 | WDF_OBJECT_ATTRIBUTES attributes; 465 | WDFDEVICE device; 466 | WDF_INTERRUPT_CONFIG interruptConfig; 467 | WDFQUEUE queue; 468 | UCHAR minorFunction; 469 | PCROSKBLIGHT_CONTEXT devContext; 470 | 471 | UNREFERENCED_PARAMETER(Driver); 472 | 473 | PAGED_CODE(); 474 | 475 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_PNP, 476 | "CrosKBLightEvtDeviceAdd called\n"); 477 | 478 | // 479 | // Tell framework this is a filter driver. Filter drivers by default are 480 | // not power policy owners. This works well for this driver because 481 | // HIDclass driver is the power policy owner for HID minidrivers. 482 | // 483 | 484 | WdfFdoInitSetFilter(DeviceInit); 485 | 486 | { 487 | WDF_PNPPOWER_EVENT_CALLBACKS pnpCallbacks; 488 | WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&pnpCallbacks); 489 | 490 | pnpCallbacks.EvtDevicePrepareHardware = OnPrepareHardware; 491 | pnpCallbacks.EvtDeviceReleaseHardware = OnReleaseHardware; 492 | pnpCallbacks.EvtDeviceD0Entry = OnD0Entry; 493 | pnpCallbacks.EvtDeviceD0Exit = OnD0Exit; 494 | 495 | WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpCallbacks); 496 | } 497 | 498 | // 499 | // Setup the device context 500 | // 501 | 502 | WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, CROSKBLIGHT_CONTEXT); 503 | 504 | // 505 | // Create a framework device object.This call will in turn create 506 | // a WDM device object, attach to the lower stack, and set the 507 | // appropriate flags and attributes. 508 | // 509 | 510 | status = WdfDeviceCreate(&DeviceInit, &attributes, &device); 511 | 512 | if (!NT_SUCCESS(status)) 513 | { 514 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_PNP, 515 | "WdfDeviceCreate failed with status code 0x%x\n", status); 516 | 517 | return status; 518 | } 519 | 520 | { 521 | WDF_DEVICE_STATE deviceState; 522 | WDF_DEVICE_STATE_INIT(&deviceState); 523 | 524 | deviceState.NotDisableable = WdfFalse; 525 | WdfDeviceSetDeviceState(device, &deviceState); 526 | } 527 | 528 | WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&queueConfig, WdfIoQueueDispatchParallel); 529 | 530 | queueConfig.EvtIoInternalDeviceControl = CrosKBLightEvtInternalDeviceControl; 531 | 532 | status = WdfIoQueueCreate(device, 533 | &queueConfig, 534 | WDF_NO_OBJECT_ATTRIBUTES, 535 | &queue 536 | ); 537 | 538 | if (!NT_SUCCESS(status)) 539 | { 540 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_PNP, 541 | "WdfIoQueueCreate failed 0x%x\n", status); 542 | 543 | return status; 544 | } 545 | 546 | // 547 | // Create manual I/O queue to take care of hid report read requests 548 | // 549 | 550 | devContext = GetDeviceContext(device); 551 | devContext->FxDevice = device; 552 | 553 | WDF_IO_QUEUE_CONFIG_INIT(&queueConfig, WdfIoQueueDispatchManual); 554 | 555 | queueConfig.PowerManaged = WdfTrue; 556 | 557 | status = WdfIoQueueCreate(device, 558 | &queueConfig, 559 | WDF_NO_OBJECT_ATTRIBUTES, 560 | &devContext->ReportQueue 561 | ); 562 | 563 | if (!NT_SUCCESS(status)) 564 | { 565 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_PNP, 566 | "WdfIoQueueCreate failed 0x%x\n", status); 567 | 568 | return status; 569 | } 570 | 571 | status = WdfWaitLockCreate(WDF_NO_OBJECT_ATTRIBUTES, &devContext->ecLock); 572 | if (!NT_SUCCESS(status)) 573 | { 574 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_PNP, 575 | "WdfWaitLockCreate failed 0x%x\n", status); 576 | 577 | return status; 578 | } 579 | 580 | return status; 581 | } 582 | 583 | VOID 584 | CrosKBLightEvtInternalDeviceControl( 585 | IN WDFQUEUE Queue, 586 | IN WDFREQUEST Request, 587 | IN size_t OutputBufferLength, 588 | IN size_t InputBufferLength, 589 | IN ULONG IoControlCode 590 | ) 591 | { 592 | NTSTATUS status = STATUS_SUCCESS; 593 | WDFDEVICE device; 594 | PCROSKBLIGHT_CONTEXT devContext; 595 | BOOLEAN completeRequest = TRUE; 596 | 597 | UNREFERENCED_PARAMETER(OutputBufferLength); 598 | UNREFERENCED_PARAMETER(InputBufferLength); 599 | 600 | device = WdfIoQueueGetDevice(Queue); 601 | devContext = GetDeviceContext(device); 602 | 603 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_IOCTL, 604 | "%s, Queue:0x%p, Request:0x%p\n", 605 | DbgHidInternalIoctlString(IoControlCode), 606 | Queue, 607 | Request 608 | ); 609 | 610 | // 611 | // Please note that HIDCLASS provides the buffer in the Irp->UserBuffer 612 | // field irrespective of the ioctl buffer type. However, framework is very 613 | // strict about type checking. You cannot get Irp->UserBuffer by using 614 | // WdfRequestRetrieveOutputMemory if the ioctl is not a METHOD_NEITHER 615 | // internal ioctl. So depending on the ioctl code, we will either 616 | // use retreive function or escape to WDM to get the UserBuffer. 617 | // 618 | 619 | switch (IoControlCode) 620 | { 621 | 622 | case IOCTL_HID_GET_DEVICE_DESCRIPTOR: 623 | // 624 | // Retrieves the device's HID descriptor. 625 | // 626 | status = CrosKBLightGetHidDescriptor(device, Request); 627 | break; 628 | 629 | case IOCTL_HID_GET_DEVICE_ATTRIBUTES: 630 | // 631 | //Retrieves a device's attributes in a HID_DEVICE_ATTRIBUTES structure. 632 | // 633 | status = CrosKBLightGetDeviceAttributes(Request); 634 | break; 635 | 636 | case IOCTL_HID_GET_REPORT_DESCRIPTOR: 637 | // 638 | //Obtains the report descriptor for the HID device. 639 | // 640 | status = CrosKBLightGetReportDescriptor(device, Request); 641 | break; 642 | 643 | case IOCTL_HID_GET_STRING: 644 | // 645 | // Requests that the HID minidriver retrieve a human-readable string 646 | // for either the manufacturer ID, the product ID, or the serial number 647 | // from the string descriptor of the device. The minidriver must send 648 | // a Get String Descriptor request to the device, in order to retrieve 649 | // the string descriptor, then it must extract the string at the 650 | // appropriate index from the string descriptor and return it in the 651 | // output buffer indicated by the IRP. Before sending the Get String 652 | // Descriptor request, the minidriver must retrieve the appropriate 653 | // index for the manufacturer ID, the product ID or the serial number 654 | // from the device extension of a top level collection associated with 655 | // the device. 656 | // 657 | status = CrosKBLightGetString(Request); 658 | break; 659 | 660 | case IOCTL_HID_WRITE_REPORT: 661 | case IOCTL_HID_SET_OUTPUT_REPORT: 662 | // 663 | //Transmits a class driver-supplied report to the device. 664 | // 665 | status = CrosKBLightWriteReport(devContext, Request); 666 | break; 667 | 668 | case IOCTL_HID_READ_REPORT: 669 | case IOCTL_HID_GET_INPUT_REPORT: 670 | // 671 | // Returns a report from the device into a class driver-supplied buffer. 672 | // 673 | status = CrosKBLightReadReport(devContext, Request, &completeRequest); 674 | break; 675 | 676 | case IOCTL_HID_SET_FEATURE: 677 | // 678 | // This sends a HID class feature report to a top-level collection of 679 | // a HID class device. 680 | // 681 | status = CrosKBLightSetFeature(devContext, Request, &completeRequest); 682 | break; 683 | 684 | case IOCTL_HID_GET_FEATURE: 685 | // 686 | // returns a feature report associated with a top-level collection 687 | // 688 | status = CrosKBLightGetFeature(devContext, Request, &completeRequest); 689 | break; 690 | 691 | case IOCTL_HID_ACTIVATE_DEVICE: 692 | // 693 | // Makes the device ready for I/O operations. 694 | // 695 | case IOCTL_HID_DEACTIVATE_DEVICE: 696 | // 697 | // Causes the device to cease operations and terminate all outstanding 698 | // I/O requests. 699 | // 700 | default: 701 | status = STATUS_NOT_SUPPORTED; 702 | break; 703 | } 704 | 705 | if (completeRequest) 706 | { 707 | WdfRequestComplete(Request, status); 708 | 709 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_IOCTL, 710 | "%s completed, Queue:0x%p, Request:0x%p\n", 711 | DbgHidInternalIoctlString(IoControlCode), 712 | Queue, 713 | Request 714 | ); 715 | } 716 | else 717 | { 718 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_IOCTL, 719 | "%s deferred, Queue:0x%p, Request:0x%p\n", 720 | DbgHidInternalIoctlString(IoControlCode), 721 | Queue, 722 | Request 723 | ); 724 | } 725 | 726 | return; 727 | } 728 | 729 | NTSTATUS 730 | CrosKBLightGetHidDescriptor( 731 | IN WDFDEVICE Device, 732 | IN WDFREQUEST Request 733 | ) 734 | { 735 | NTSTATUS status = STATUS_SUCCESS; 736 | size_t bytesToCopy = 0; 737 | WDFMEMORY memory; 738 | 739 | UNREFERENCED_PARAMETER(Device); 740 | 741 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 742 | "CrosKBLightGetHidDescriptor Entry\n"); 743 | 744 | // 745 | // This IOCTL is METHOD_NEITHER so WdfRequestRetrieveOutputMemory 746 | // will correctly retrieve buffer from Irp->UserBuffer. 747 | // Remember that HIDCLASS provides the buffer in the Irp->UserBuffer 748 | // field irrespective of the ioctl buffer type. However, framework is very 749 | // strict about type checking. You cannot get Irp->UserBuffer by using 750 | // WdfRequestRetrieveOutputMemory if the ioctl is not a METHOD_NEITHER 751 | // internal ioctl. 752 | // 753 | status = WdfRequestRetrieveOutputMemory(Request, &memory); 754 | 755 | if (!NT_SUCCESS(status)) 756 | { 757 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 758 | "WdfRequestRetrieveOutputMemory failed 0x%x\n", status); 759 | 760 | return status; 761 | } 762 | 763 | // 764 | // Use hardcoded "HID Descriptor" 765 | // 766 | bytesToCopy = DefaultHidDescriptor.bLength; 767 | 768 | if (bytesToCopy == 0) 769 | { 770 | status = STATUS_INVALID_DEVICE_STATE; 771 | 772 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 773 | "DefaultHidDescriptor is zero, 0x%x\n", status); 774 | 775 | return status; 776 | } 777 | 778 | status = WdfMemoryCopyFromBuffer(memory, 779 | 0, // Offset 780 | (PVOID)&DefaultHidDescriptor, 781 | bytesToCopy); 782 | 783 | if (!NT_SUCCESS(status)) 784 | { 785 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 786 | "WdfMemoryCopyFromBuffer failed 0x%x\n", status); 787 | 788 | return status; 789 | } 790 | 791 | // 792 | // Report how many bytes were copied 793 | // 794 | WdfRequestSetInformation(Request, bytesToCopy); 795 | 796 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 797 | "CrosKBLightGetHidDescriptor Exit = 0x%x\n", status); 798 | 799 | return status; 800 | } 801 | 802 | NTSTATUS 803 | CrosKBLightGetReportDescriptor( 804 | IN WDFDEVICE Device, 805 | IN WDFREQUEST Request 806 | ) 807 | { 808 | NTSTATUS status = STATUS_SUCCESS; 809 | ULONG_PTR bytesToCopy; 810 | WDFMEMORY memory; 811 | 812 | UNREFERENCED_PARAMETER(Device); 813 | 814 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 815 | "CrosKBLightGetReportDescriptor Entry\n"); 816 | 817 | // 818 | // This IOCTL is METHOD_NEITHER so WdfRequestRetrieveOutputMemory 819 | // will correctly retrieve buffer from Irp->UserBuffer. 820 | // Remember that HIDCLASS provides the buffer in the Irp->UserBuffer 821 | // field irrespective of the ioctl buffer type. However, framework is very 822 | // strict about type checking. You cannot get Irp->UserBuffer by using 823 | // WdfRequestRetrieveOutputMemory if the ioctl is not a METHOD_NEITHER 824 | // internal ioctl. 825 | // 826 | status = WdfRequestRetrieveOutputMemory(Request, &memory); 827 | if (!NT_SUCCESS(status)) 828 | { 829 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 830 | "WdfRequestRetrieveOutputMemory failed 0x%x\n", status); 831 | 832 | return status; 833 | } 834 | 835 | // 836 | // Use hardcoded Report descriptor 837 | // 838 | bytesToCopy = DefaultHidDescriptor.DescriptorList[0].wReportLength; 839 | 840 | if (bytesToCopy == 0) 841 | { 842 | status = STATUS_INVALID_DEVICE_STATE; 843 | 844 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 845 | "DefaultHidDescriptor's reportLength is zero, 0x%x\n", status); 846 | 847 | return status; 848 | } 849 | 850 | status = WdfMemoryCopyFromBuffer(memory, 851 | 0, 852 | (PVOID)DefaultReportDescriptor, 853 | bytesToCopy); 854 | if (!NT_SUCCESS(status)) 855 | { 856 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 857 | "WdfMemoryCopyFromBuffer failed 0x%x\n", status); 858 | 859 | return status; 860 | } 861 | 862 | // 863 | // Report how many bytes were copied 864 | // 865 | WdfRequestSetInformation(Request, bytesToCopy); 866 | 867 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 868 | "CrosKBLightGetReportDescriptor Exit = 0x%x\n", status); 869 | 870 | return status; 871 | } 872 | 873 | 874 | NTSTATUS 875 | CrosKBLightGetDeviceAttributes( 876 | IN WDFREQUEST Request 877 | ) 878 | { 879 | NTSTATUS status = STATUS_SUCCESS; 880 | PHID_DEVICE_ATTRIBUTES deviceAttributes = NULL; 881 | 882 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 883 | "CrosKBLightGetDeviceAttributes Entry\n"); 884 | 885 | // 886 | // This IOCTL is METHOD_NEITHER so WdfRequestRetrieveOutputMemory 887 | // will correctly retrieve buffer from Irp->UserBuffer. 888 | // Remember that HIDCLASS provides the buffer in the Irp->UserBuffer 889 | // field irrespective of the ioctl buffer type. However, framework is very 890 | // strict about type checking. You cannot get Irp->UserBuffer by using 891 | // WdfRequestRetrieveOutputMemory if the ioctl is not a METHOD_NEITHER 892 | // internal ioctl. 893 | // 894 | status = WdfRequestRetrieveOutputBuffer(Request, 895 | sizeof(HID_DEVICE_ATTRIBUTES), 896 | (PVOID *)&deviceAttributes, 897 | NULL); 898 | if (!NT_SUCCESS(status)) 899 | { 900 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 901 | "WdfRequestRetrieveOutputBuffer failed 0x%x\n", status); 902 | 903 | return status; 904 | } 905 | 906 | // 907 | // Set USB device descriptor 908 | // 909 | 910 | deviceAttributes->Size = sizeof(HID_DEVICE_ATTRIBUTES); 911 | deviceAttributes->VendorID = CROSKBLIGHT_VID; 912 | deviceAttributes->ProductID = CROSKBLIGHT_PID; 913 | deviceAttributes->VersionNumber = CROSKBLIGHT_VERSION; 914 | 915 | // 916 | // Report how many bytes were copied 917 | // 918 | WdfRequestSetInformation(Request, sizeof(HID_DEVICE_ATTRIBUTES)); 919 | 920 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 921 | "CrosKBLightGetDeviceAttributes Exit = 0x%x\n", status); 922 | 923 | return status; 924 | } 925 | 926 | NTSTATUS 927 | CrosKBLightGetString( 928 | IN WDFREQUEST Request 929 | ) 930 | { 931 | 932 | NTSTATUS status = STATUS_SUCCESS; 933 | PWSTR pwstrID; 934 | size_t lenID; 935 | WDF_REQUEST_PARAMETERS params; 936 | void *pStringBuffer = NULL; 937 | 938 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 939 | "CrosKBLightGetString Entry\n"); 940 | 941 | WDF_REQUEST_PARAMETERS_INIT(¶ms); 942 | WdfRequestGetParameters(Request, ¶ms); 943 | 944 | switch ((ULONG_PTR)params.Parameters.DeviceIoControl.Type3InputBuffer & 0xFFFF) 945 | { 946 | case HID_STRING_ID_IMANUFACTURER: 947 | pwstrID = L"CrosKBLight.\0"; 948 | break; 949 | 950 | case HID_STRING_ID_IPRODUCT: 951 | pwstrID = L"MaxTouch Touch Screen\0"; 952 | break; 953 | 954 | case HID_STRING_ID_ISERIALNUMBER: 955 | pwstrID = L"123123123\0"; 956 | break; 957 | 958 | default: 959 | pwstrID = NULL; 960 | break; 961 | } 962 | 963 | lenID = pwstrID ? wcslen(pwstrID)*sizeof(WCHAR) + sizeof(UNICODE_NULL) : 0; 964 | 965 | if (pwstrID == NULL) 966 | { 967 | 968 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 969 | "CrosKBLightGetString Invalid request type\n"); 970 | 971 | status = STATUS_INVALID_PARAMETER; 972 | 973 | return status; 974 | } 975 | 976 | status = WdfRequestRetrieveOutputBuffer(Request, 977 | lenID, 978 | &pStringBuffer, 979 | &lenID); 980 | 981 | if (!NT_SUCCESS(status)) 982 | { 983 | 984 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 985 | "CrosKBLightGetString WdfRequestRetrieveOutputBuffer failed Status 0x%x\n", status); 986 | 987 | return status; 988 | } 989 | 990 | RtlCopyMemory(pStringBuffer, pwstrID, lenID); 991 | 992 | WdfRequestSetInformation(Request, lenID); 993 | 994 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 995 | "CrosKBLightGetString Exit = 0x%x\n", status); 996 | 997 | return status; 998 | } 999 | 1000 | NTSTATUS 1001 | CrosKBLightWriteReport( 1002 | IN PCROSKBLIGHT_CONTEXT DevContext, 1003 | IN WDFREQUEST Request 1004 | ) 1005 | { 1006 | NTSTATUS status = STATUS_SUCCESS; 1007 | WDF_REQUEST_PARAMETERS params; 1008 | PHID_XFER_PACKET transferPacket = NULL; 1009 | size_t bytesWritten = 0; 1010 | 1011 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1012 | "CrosKBLightWriteReport Entry\n"); 1013 | 1014 | WDF_REQUEST_PARAMETERS_INIT(¶ms); 1015 | WdfRequestGetParameters(Request, ¶ms); 1016 | 1017 | if (params.Parameters.DeviceIoControl.InputBufferLength < sizeof(HID_XFER_PACKET)) 1018 | { 1019 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1020 | "CrosKBLightWriteReport Xfer packet too small\n"); 1021 | 1022 | status = STATUS_BUFFER_TOO_SMALL; 1023 | } 1024 | else 1025 | { 1026 | 1027 | transferPacket = (PHID_XFER_PACKET)WdfRequestWdmGetIrp(Request)->UserBuffer; 1028 | 1029 | if (transferPacket == NULL) 1030 | { 1031 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1032 | "CrosKBLightWriteReport No xfer packet\n"); 1033 | 1034 | status = STATUS_INVALID_DEVICE_REQUEST; 1035 | } 1036 | else 1037 | { 1038 | // 1039 | // switch on the report id 1040 | // 1041 | 1042 | switch (transferPacket->reportId) 1043 | { 1044 | case REPORTID_KBLIGHT: { 1045 | CrosKBLightSettingsReport *pReport = (CrosKBLightSettingsReport *)transferPacket->reportBuffer; 1046 | 1047 | int reg = pReport->SetBrightness; 1048 | int val = pReport->Brightness; 1049 | 1050 | if (reg == 0) { 1051 | int brightness = DevContext->currentBrightness; 1052 | update_brightness(DevContext, brightness); 1053 | } 1054 | else if (reg == 1) { 1055 | DevContext->currentBrightness = val; 1056 | if (DevContext->ledExists) { 1057 | set_kbbl(DevContext, DevContext->currentBrightness); 1058 | } 1059 | } 1060 | break; 1061 | } 1062 | default: 1063 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1064 | "CrosKBLightWriteReport Unhandled report type %d\n", transferPacket->reportId); 1065 | 1066 | status = STATUS_INVALID_PARAMETER; 1067 | 1068 | break; 1069 | } 1070 | } 1071 | } 1072 | 1073 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1074 | "CrosKBLightWriteReport Exit = 0x%x\n", status); 1075 | 1076 | return status; 1077 | 1078 | } 1079 | 1080 | NTSTATUS 1081 | CrosKBLightProcessVendorReport( 1082 | IN PCROSKBLIGHT_CONTEXT DevContext, 1083 | IN PVOID ReportBuffer, 1084 | IN ULONG ReportBufferLen, 1085 | OUT size_t* BytesWritten 1086 | ) 1087 | { 1088 | NTSTATUS status = STATUS_SUCCESS; 1089 | WDFREQUEST reqRead; 1090 | PVOID pReadReport = NULL; 1091 | size_t bytesReturned = 0; 1092 | 1093 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1094 | "CrosKBLightProcessVendorReport Entry\n"); 1095 | 1096 | status = WdfIoQueueRetrieveNextRequest(DevContext->ReportQueue, 1097 | &reqRead); 1098 | 1099 | if (NT_SUCCESS(status)) 1100 | { 1101 | status = WdfRequestRetrieveOutputBuffer(reqRead, 1102 | ReportBufferLen, 1103 | &pReadReport, 1104 | &bytesReturned); 1105 | 1106 | if (NT_SUCCESS(status)) 1107 | { 1108 | // 1109 | // Copy ReportBuffer into read request 1110 | // 1111 | 1112 | if (bytesReturned > ReportBufferLen) 1113 | { 1114 | bytesReturned = ReportBufferLen; 1115 | } 1116 | 1117 | RtlCopyMemory(pReadReport, 1118 | ReportBuffer, 1119 | bytesReturned); 1120 | 1121 | // 1122 | // Complete read with the number of bytes returned as info 1123 | // 1124 | 1125 | WdfRequestCompleteWithInformation(reqRead, 1126 | status, 1127 | bytesReturned); 1128 | 1129 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_IOCTL, 1130 | "CrosKBLightProcessVendorReport %d bytes returned\n", bytesReturned); 1131 | 1132 | // 1133 | // Return the number of bytes written for the write request completion 1134 | // 1135 | 1136 | *BytesWritten = bytesReturned; 1137 | 1138 | CrosKBLightPrint(DEBUG_LEVEL_INFO, DBG_IOCTL, 1139 | "%s completed, Queue:0x%p, Request:0x%p\n", 1140 | DbgHidInternalIoctlString(IOCTL_HID_READ_REPORT), 1141 | DevContext->ReportQueue, 1142 | reqRead); 1143 | } 1144 | else 1145 | { 1146 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1147 | "WdfRequestRetrieveOutputBuffer failed Status 0x%x\n", status); 1148 | } 1149 | } 1150 | else 1151 | { 1152 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1153 | "WdfIoQueueRetrieveNextRequest failed Status 0x%x\n", status); 1154 | } 1155 | 1156 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1157 | "CrosKBLightProcessVendorReport Exit = 0x%x\n", status); 1158 | 1159 | return status; 1160 | } 1161 | 1162 | NTSTATUS 1163 | CrosKBLightReadReport( 1164 | IN PCROSKBLIGHT_CONTEXT DevContext, 1165 | IN WDFREQUEST Request, 1166 | OUT BOOLEAN* CompleteRequest 1167 | ) 1168 | { 1169 | NTSTATUS status = STATUS_SUCCESS; 1170 | 1171 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1172 | "CrosKBLightReadReport Entry\n"); 1173 | 1174 | // 1175 | // Forward this read request to our manual queue 1176 | // (in other words, we are going to defer this request 1177 | // until we have a corresponding write request to 1178 | // match it with) 1179 | // 1180 | 1181 | status = WdfRequestForwardToIoQueue(Request, DevContext->ReportQueue); 1182 | 1183 | if (!NT_SUCCESS(status)) 1184 | { 1185 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1186 | "WdfRequestForwardToIoQueue failed Status 0x%x\n", status); 1187 | } 1188 | else 1189 | { 1190 | *CompleteRequest = FALSE; 1191 | } 1192 | 1193 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1194 | "CrosKBLightReadReport Exit = 0x%x\n", status); 1195 | 1196 | return status; 1197 | } 1198 | 1199 | NTSTATUS 1200 | CrosKBLightSetFeature( 1201 | IN PCROSKBLIGHT_CONTEXT DevContext, 1202 | IN WDFREQUEST Request, 1203 | OUT BOOLEAN* CompleteRequest 1204 | ) 1205 | { 1206 | NTSTATUS status = STATUS_SUCCESS; 1207 | WDF_REQUEST_PARAMETERS params; 1208 | PHID_XFER_PACKET transferPacket = NULL; 1209 | CrosKBLightFeatureReport* pReport = NULL; 1210 | 1211 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1212 | "CrosKBLightSetFeature Entry\n"); 1213 | 1214 | WDF_REQUEST_PARAMETERS_INIT(¶ms); 1215 | WdfRequestGetParameters(Request, ¶ms); 1216 | 1217 | if (params.Parameters.DeviceIoControl.InputBufferLength < sizeof(HID_XFER_PACKET)) 1218 | { 1219 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1220 | "CrosKBLightSetFeature Xfer packet too small\n"); 1221 | 1222 | status = STATUS_BUFFER_TOO_SMALL; 1223 | } 1224 | else 1225 | { 1226 | 1227 | transferPacket = (PHID_XFER_PACKET)WdfRequestWdmGetIrp(Request)->UserBuffer; 1228 | 1229 | if (transferPacket == NULL) 1230 | { 1231 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1232 | "CrosKBLightWriteReport No xfer packet\n"); 1233 | 1234 | status = STATUS_INVALID_DEVICE_REQUEST; 1235 | } 1236 | else 1237 | { 1238 | // 1239 | // switch on the report id 1240 | // 1241 | 1242 | switch (transferPacket->reportId) 1243 | { 1244 | default: 1245 | 1246 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1247 | "CrosKBLightSetFeature Unhandled report type %d\n", transferPacket->reportId); 1248 | 1249 | status = STATUS_INVALID_PARAMETER; 1250 | 1251 | break; 1252 | } 1253 | } 1254 | } 1255 | 1256 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1257 | "CrosKBLightSetFeature Exit = 0x%x\n", status); 1258 | 1259 | return status; 1260 | } 1261 | 1262 | NTSTATUS 1263 | CrosKBLightGetFeature( 1264 | IN PCROSKBLIGHT_CONTEXT DevContext, 1265 | IN WDFREQUEST Request, 1266 | OUT BOOLEAN* CompleteRequest 1267 | ) 1268 | { 1269 | NTSTATUS status = STATUS_SUCCESS; 1270 | WDF_REQUEST_PARAMETERS params; 1271 | PHID_XFER_PACKET transferPacket = NULL; 1272 | 1273 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1274 | "CrosKBLightGetFeature Entry\n"); 1275 | 1276 | WDF_REQUEST_PARAMETERS_INIT(¶ms); 1277 | WdfRequestGetParameters(Request, ¶ms); 1278 | 1279 | if (params.Parameters.DeviceIoControl.OutputBufferLength < sizeof(HID_XFER_PACKET)) 1280 | { 1281 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1282 | "CrosKBLightGetFeature Xfer packet too small\n"); 1283 | 1284 | status = STATUS_BUFFER_TOO_SMALL; 1285 | } 1286 | else 1287 | { 1288 | 1289 | transferPacket = (PHID_XFER_PACKET)WdfRequestWdmGetIrp(Request)->UserBuffer; 1290 | 1291 | if (transferPacket == NULL) 1292 | { 1293 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1294 | "CrosKBLightGetFeature No xfer packet\n"); 1295 | 1296 | status = STATUS_INVALID_DEVICE_REQUEST; 1297 | } 1298 | else 1299 | { 1300 | // 1301 | // switch on the report id 1302 | // 1303 | 1304 | switch (transferPacket->reportId) 1305 | { 1306 | default: 1307 | 1308 | CrosKBLightPrint(DEBUG_LEVEL_ERROR, DBG_IOCTL, 1309 | "CrosKBLightGetFeature Unhandled report type %d\n", transferPacket->reportId); 1310 | 1311 | status = STATUS_INVALID_PARAMETER; 1312 | 1313 | break; 1314 | } 1315 | } 1316 | } 1317 | 1318 | CrosKBLightPrint(DEBUG_LEVEL_VERBOSE, DBG_IOCTL, 1319 | "CrosKBLightGetFeature Exit = 0x%x\n", status); 1320 | 1321 | return status; 1322 | } 1323 | 1324 | PCHAR 1325 | DbgHidInternalIoctlString( 1326 | IN ULONG IoControlCode 1327 | ) 1328 | { 1329 | switch (IoControlCode) 1330 | { 1331 | case IOCTL_HID_GET_DEVICE_DESCRIPTOR: 1332 | return "IOCTL_HID_GET_DEVICE_DESCRIPTOR"; 1333 | case IOCTL_HID_GET_REPORT_DESCRIPTOR: 1334 | return "IOCTL_HID_GET_REPORT_DESCRIPTOR"; 1335 | case IOCTL_HID_READ_REPORT: 1336 | return "IOCTL_HID_READ_REPORT"; 1337 | case IOCTL_HID_GET_DEVICE_ATTRIBUTES: 1338 | return "IOCTL_HID_GET_DEVICE_ATTRIBUTES"; 1339 | case IOCTL_HID_WRITE_REPORT: 1340 | return "IOCTL_HID_WRITE_REPORT"; 1341 | case IOCTL_HID_SET_FEATURE: 1342 | return "IOCTL_HID_SET_FEATURE"; 1343 | case IOCTL_HID_GET_FEATURE: 1344 | return "IOCTL_HID_GET_FEATURE"; 1345 | case IOCTL_HID_GET_STRING: 1346 | return "IOCTL_HID_GET_STRING"; 1347 | case IOCTL_HID_ACTIVATE_DEVICE: 1348 | return "IOCTL_HID_ACTIVATE_DEVICE"; 1349 | case IOCTL_HID_DEACTIVATE_DEVICE: 1350 | return "IOCTL_HID_DEACTIVATE_DEVICE"; 1351 | case IOCTL_HID_SEND_IDLE_NOTIFICATION_REQUEST: 1352 | return "IOCTL_HID_SEND_IDLE_NOTIFICATION_REQUEST"; 1353 | case IOCTL_HID_SET_OUTPUT_REPORT: 1354 | return "IOCTL_HID_SET_OUTPUT_REPORT"; 1355 | case IOCTL_HID_GET_INPUT_REPORT: 1356 | return "IOCTL_HID_GET_INPUT_REPORT"; 1357 | default: 1358 | return "Unknown IOCTL"; 1359 | } 1360 | } 1361 | --------------------------------------------------------------------------------