libusbK 
3.0
Library Documentation
© 2011-2021 Travis Lee Robinson. All rights reserved.
power-policy-suspend.c

Enables auto suspend, sets suspend timeout and transfers synchronously.

power-policy-suspend example:
  1. Finds a device by vid/pid (use vid=hhhh, pid=hhhhh and the command line).
  2. Initializes a new UsbK (usb device) handle.
  3. Gets the AUTO_SUSPEND and the SUSPEND_DELAY power policy values.
  4. Toggles the AUTO_SUSPEND power policy value.
  5. If AUTO_SUSPEND was enabled in the previous step, the SUSPEND_DELAY power policy is changed to a different value.
  6. Frees the UsbK (usb device) handle.
  7. Frees the LstK (device list) handle created in step #1.
Console Output
RUN #1
Looking for device vid/pid 04D8/FA2E..
Using 04D8:FA2E (LUSBW1): Benchmark Device - Microchip Technology, Inc.
[NOTE] If the DeviceIdleEnabled policy is not set in the .inf file when
       the device is installed, the AUTO_SUSPEND policy will be ignored.
Device opened successfully!
AUTO_SUSPEND  is currently enabled.
SUSPEND_DELAY is set to 5000 ms
Set AUTO_SUSPEND = Off.

RUN #2
Looking for device vid/pid 04D8/FA2E..
Using 04D8:FA2E (LUSBW1): Benchmark Device - Microchip Technology, Inc.
[NOTE] If the DeviceIdleEnabled policy is not set in the .inf file when
       the device is installed, the AUTO_SUSPEND policy will be ignored.
Device opened successfully!
AUTO_SUSPEND  is currently disabled.
Set AUTO_SUSPEND = On.
Set SUSPEND_DELAY = 10000 ms

RUN #3
Looking for device vid/pid 04D8/FA2E..
Using 04D8:FA2E (LUSBW1): Benchmark Device - Microchip Technology, Inc.
[NOTE] If the DeviceIdleEnabled policy is not set in the .inf file when
       the device is installed, the AUTO_SUSPEND policy will be ignored.
Device opened successfully!
AUTO_SUSPEND  is currently enabled.
SUSPEND_DELAY is set to 10000 ms
Set AUTO_SUSPEND = Off.

RUN #4
Looking for device vid/pid 04D8/FA2E..
Using 04D8:FA2E (LUSBW1): Benchmark Device - Microchip Technology, Inc.
[NOTE] If the DeviceIdleEnabled policy is not set in the .inf file when
       the device is installed, the AUTO_SUSPEND policy will be ignored.
Device opened successfully!
AUTO_SUSPEND  is currently disabled.
Set AUTO_SUSPEND = On.
Set SUSPEND_DELAY = 100 ms
#include "examples.h"
// Example configuration:
// Globals:
DWORD __cdecl main(int argc, char* argv[])
{
DWORD errorCode = ERROR_SUCCESS;
BOOL success;
KLST_HANDLE deviceList = NULL;
KLST_DEVINFO_HANDLE deviceInfo = NULL;
KUSB_HANDLE usbHandle = NULL;
ULONG polLength;
BYTE polAutoSuspend;
ULONG polSuspendDelay;
/*
Find the test device. Uses "vid=hhhh pid=hhhh" arguments supplied on the
command line. (default is: vid=04D8 pid=FA2E)
*/
if (!Examples_GetTestDevice(&deviceList, &deviceInfo, argc, argv))
return GetLastError();
if (deviceInfo->DriverID != KUSB_DRVID_LIBUSBK && deviceInfo->DriverID != KUSB_DRVID_WINUSB)
{
printf(
"[Warning] libusb-win32 driver (libusb0.sys) does not support power\n"
" management.\n");
}
else
{
printf(
"[Note] If the DeviceIdleEnabled policy is not set in the .inf file when\n"
" the device is installed, the AUTO_SUSPEND policy will be ignored.\n");
if (deviceInfo->DriverID == KUSB_DRVID_WINUSB)
{
printf(
"[Note] WinUSB behaves slightly different then libusbK because it reset\n"
" power policies back to their default values when the usb handle\n"
" is closed.\n");
}
}
/*
This example will use the dynamic driver api so that it can be used
with all supported drivers.
*/
LibK_LoadDriverAPI(&Usb, deviceInfo->DriverID);
/*
Initialize the device. This creates the physical usb handle.
*/
if (!Usb.Init(&usbHandle, deviceInfo))
{
errorCode = GetLastError();
printf("Init device failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("Device opened successfully!\n");
polLength = 1;
success = Usb.GetPowerPolicy(usbHandle, AUTO_SUSPEND, &polLength, &polAutoSuspend);
if (!success)
{
errorCode = GetLastError();
printf("GetPowerPolicy AUTO_SUSPEND failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
printf("AUTO_SUSPEND is currently %s.\n", (polAutoSuspend ? "enabled" : "disabled"));
polLength = 4;
success = Usb.GetPowerPolicy(usbHandle, SUSPEND_DELAY, &polLength, &polSuspendDelay);
if (!success)
{
errorCode = GetLastError();
printf("GetPowerPolicy SUSPEND_DELAY failed. ErrorCode: %08Xh\n", errorCode);
goto Done;
}
if (polAutoSuspend)
{
printf("SUSPEND_DELAY is set to %d ms\n", polSuspendDelay);
}
polAutoSuspend = polAutoSuspend ? 0 : 1;
polLength = 1;
success = Usb.SetPowerPolicy(usbHandle, AUTO_SUSPEND, polLength, &polAutoSuspend);
if (!success)
{
errorCode = GetLastError();
printf("SetPowerPolicy AUTO_SUSPEND=%u failed. ErrorCode: %08Xh\n", polAutoSuspend, errorCode);
goto Done;
}
printf("Set AUTO_SUSPEND = %s.\n", (polAutoSuspend ? "On" : "Off"));
if (polAutoSuspend)
{
polSuspendDelay *= 2;
if (!polSuspendDelay || polSuspendDelay > 12800)
polSuspendDelay = 100;
polLength = 4;
success = Usb.SetPowerPolicy(usbHandle, SUSPEND_DELAY, polLength, &polSuspendDelay);
if (!success)
{
errorCode = GetLastError();
printf("SetPowerPolicy SUSPEND_DELAY=%u failed. ErrorCode: %08Xh\n", polSuspendDelay, errorCode);
goto Done;
}
printf("Set SUSPEND_DELAY = %d ms\n", polSuspendDelay);
}
Done:
/*
Close the usb handle.
*/
if (usbHandle) Usb.Free(usbHandle);
/*
Free the device list.
*/
LstK_Free(deviceList);
return errorCode;
}