Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
be09062
changed to always use the device capabilities to change power states
itzandroidtab Dec 25, 2025
fef02e6
added helper function to forward to the next power driver
itzandroidtab Dec 25, 2025
3c576cf
bugfix dropping unhandled power requests
itzandroidtab Dec 25, 2025
a8102ff
added optional parameters for forward to power driver for a completio…
itzandroidtab Dec 25, 2025
a32af47
reworked MN_SET_POWER for the device power state
itzandroidtab Dec 25, 2025
612f3da
changed power_irp to a power_irp_count'er
itzandroidtab Dec 25, 2025
8a0d795
removed atomic functions around pipe_open_count as it is in a spinlock
itzandroidtab Dec 25, 2025
b0fab19
reduced duplicate code by moving to forward function
itzandroidtab Dec 25, 2025
b5cb1dc
moved variable
itzandroidtab Dec 25, 2025
6efb4e6
removed unused variable
itzandroidtab Dec 25, 2025
71e382b
changed IRP_MN_WAIT_WAKE
itzandroidtab Dec 25, 2025
7f484bf
reverted change spinlocks
itzandroidtab Dec 25, 2025
a0c85bd
removed dead code for waking up the host system (not supported by USB…
itzandroidtab Dec 25, 2025
808e8f8
bugfix not being able to "disable" the USB chief in device manager
itzandroidtab Dec 26, 2025
5b5fe6d
changed missed IofCallDriver to forward_to_next_driver
itzandroidtab Dec 26, 2025
95c7e2d
removed extra newline
itzandroidtab Dec 26, 2025
409ea56
Removed unused event
itzandroidtab Dec 26, 2025
655214f
removed unused function
itzandroidtab Dec 26, 2025
530136b
bugfix checking for wrong IRP_MN in last case MJ_PNP
itzandroidtab Dec 26, 2025
13f446e
renamed variable and added some comments
itzandroidtab Dec 26, 2025
7b8e553
renamed more variables and added more comments
itzandroidtab Dec 26, 2025
ddf2774
renamed spinlock functions to pipe_count functions
itzandroidtab Dec 26, 2025
7d8c648
renamed spinlock file to pipe
itzandroidtab Dec 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ string(REPLACE "/RTC1" "" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
set(SOURCES
chief/driver.cpp
chief/major_functions.cpp
chief/spinlock.cpp
chief/pipe.cpp
chief/usb.cpp
)

Expand Down
95 changes: 70 additions & 25 deletions chief/device_extension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,24 @@ extern "C" {

#include "maybe.hpp"

/**
* @brief Payload we are receiving/sending from the
* application in a mj device control
*
*/
struct usb_chief_vendor_request {
unsigned short Request;
// usb request fields. Is sometimes used as input
// and as output
unsigned short request;

// usb specific fields
unsigned short value;
unsigned short index;

// length of the data buffer
unsigned short length;

// pointer to the data buffer
void *data;
};

Expand All @@ -21,29 +34,61 @@ struct usb_chief_vendor_request {
*
*/
struct chief_device_extension {
PDEVICE_OBJECT attachedDeviceObject;
PDEVICE_OBJECT physicalDeviceObject;
POWER_STATE current_power_state;
PUSB_CONFIGURATION_DESCRIPTOR usb_config_desc;
PUSBD_INTERFACE_INFORMATION usb_interface_info;
DEVICE_CAPABILITIES device_capabilities;
IRP *power_irp;
KEVENT pipe_count_empty;
KEVENT event1;
KEVENT event2;
KEVENT power_complete_event;
KSPIN_LOCK device_lock;
LONG InterlockedValue1;
LONG pipe_open_count;
bool *allocated_pipes;
volatile bool is_ejecting;
volatile bool is_removing;
volatile bool is_stopped;
volatile bool wait_wake_in_progress;
volatile bool power_request_busy;
POWER_STATE target_power_state;

// The BCD version of the connected USB device
maybe<unsigned short> bcdUSB;
// the device objects we are connected to
PDEVICE_OBJECT attachedDeviceObject;
PDEVICE_OBJECT physicalDeviceObject;

// the current power state of the device
POWER_STATE current_power_state;

// the current usb configuration descriptor
PUSB_CONFIGURATION_DESCRIPTOR usb_config_desc;

// the current usb interface information
PUSBD_INTERFACE_INFORMATION usb_interface_info;

// the device capabilities structure. This is used
// to know what power state we need to go to for each
// system power state
DEVICE_CAPABILITIES device_capabilities;

// event to signal when the pipe count is zero. This
// can only happen when all opened pipes are closed
// and IRP_MN_REMOVE_DEVICE is called
KEVENT pipe_count_empty;

// spinlock to protect the active_pipe_count
KSPIN_LOCK device_lock;

// count of opened pipes
LONG active_pipe_count;

// an array with flags for each pipe if it is opened
// or not
bool *allocated_pipes;

// flag if the device has been removed. This means
// that we cannot accept new ioctls/reads/writes.
// When this flag is set we cannot talk to the device
// anymore
volatile bool device_removed;

// flag if the device is remove pending. This means
// that we should not accept new ioctls/reads/writes
// and that the device should be considered as being
// removed until it is deleted or canceled
volatile bool remove_pending;

// flag if new requests should be held. This is used during
// stop device to prevent new ioctls/reads/writes from
// being processed until the device is started again
volatile bool hold_new_requests;

// The BCD version of the connected USB device
maybe<unsigned short> bcdUSB;

// count of active power irps. Should only be modified
// using Interlocked functions
LONG power_irp_count;
};

14 changes: 5 additions & 9 deletions chief/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "driver.hpp"
#include "major_functions.hpp"
#include "device_extension.hpp"
#include "spinlock.hpp"
#include "pipe.hpp"

/**
* @brief Unload routine for the driver.
Expand Down Expand Up @@ -66,9 +66,6 @@ NTSTATUS add_chief_device(PDRIVER_OBJECT driver_object, PDEVICE_OBJECT& device_o

// initalize the events
KeInitializeEvent(&dev_ext->pipe_count_empty, NotificationEvent, FALSE);
KeInitializeEvent(&dev_ext->event1, NotificationEvent, FALSE);
KeInitializeEvent(&dev_ext->event2, NotificationEvent, FALSE);
KeInitializeEvent(&dev_ext->power_complete_event, NotificationEvent, FALSE);

// initialize spinlocks
KeInitializeSpinLock(&dev_ext->device_lock);
Expand Down Expand Up @@ -177,15 +174,14 @@ static NTSTATUS add_device(__in struct _DRIVER_OBJECT *DriverObject, __in struct
dev_ext->device_capabilities.Version = 1;
dev_ext->device_capabilities.Address = static_cast<ULONG>(-1);
dev_ext->device_capabilities.UINumber = static_cast<ULONG>(-1);
dev_ext->device_capabilities.DeviceWake = PowerDeviceUnspecified;

// start the device
io_call_start_device(dev_ext->attachedDeviceObject, &dev_ext->device_capabilities);

// set the current power state to unspecified
dev_ext->target_power_state.DeviceState = PowerDeviceUnspecified;

// acquire the spinlock
spinlock_increment(device_object);
// increment the pipe count for the first time so we
// never reach zero except when the device is removed
increment_active_pipe_count(device_object);

// clear the flag we are inititializing
device_object->Flags &= ~DO_DEVICE_INITIALIZING;
Expand Down
Loading