Esempio n. 1
0
HRESULT
CMyDevice::OnPrepareHardware(
    __in IWDFDevice * /* FxDevice */
    )
/*++

Routine Description:

    This routine is invoked to ready the driver
    to talk to hardware. It opens the handle to the 
    device and talks to it using the WINUSB interface.
    It invokes WINUSB to discver the interfaces and stores
    the information related to bulk endpoints.

Arguments:

    FxDevice  : Pointer to the WDF device interface

Return Value:

    HRESULT 

--*/
{
    PWSTR deviceName = NULL;
    DWORD deviceNameCch = 0;

    HRESULT hr;

    //
    // Get the device name.
    // Get the length to allocate first
    //

    hr = m_FxDevice->RetrieveDeviceName(NULL, &deviceNameCch);

    if (FAILED(hr))
    {
        TraceEvents(TRACE_LEVEL_ERROR, 
                    TEST_TRACE_DEVICE, 
                    "%!FUNC! Cannot get device name %!HRESULT!",
                    hr
                    );
    }

    //
    // Allocate the buffer
    //

    if (SUCCEEDED(hr))
    {
        deviceName = new WCHAR[deviceNameCch];

        if (deviceName == NULL)
        {
            hr = E_OUTOFMEMORY;
        }
    }

    //
    // Get the actual name
    //

    if (SUCCEEDED(hr))
    {
        hr = m_FxDevice->RetrieveDeviceName(deviceName, &deviceNameCch);

        if (FAILED(hr))
        {
            TraceEvents(TRACE_LEVEL_ERROR, 
                        TEST_TRACE_DEVICE, 
                        "%!FUNC! Cannot get device name %!HRESULT!",
                        hr
                        );
        }
    }

    if (SUCCEEDED(hr))
    {
        TraceEvents(TRACE_LEVEL_INFORMATION, 
                    TEST_TRACE_DEVICE, 
                    "%!FUNC! Device name %S",
                    deviceName
                    );
    }

    //
    // Create USB I/O Targets and configure them
    //

    if (SUCCEEDED(hr))
    {
        hr = CreateUsbIoTargets();
    }

    if (SUCCEEDED(hr))
    {
        ULONG length = sizeof(m_Speed);

        hr = m_pIUsbTargetDevice->RetrieveDeviceInformation(DEVICE_SPEED, 
                                                            &length,
                                                            &m_Speed);
        if (FAILED(hr)) 
        {
            TraceEvents(TRACE_LEVEL_ERROR, 
                        TEST_TRACE_DEVICE, 
                        "%!FUNC! Cannot get usb device speed information %!HRESULT!",
                        hr
                        );
        }
    }

    if (SUCCEEDED(hr)) 
    {
        TraceEvents(TRACE_LEVEL_INFORMATION, 
                    TEST_TRACE_DEVICE, 
                    "%!FUNC! Speed - %x\n",
                    m_Speed
                    );
    }

    if (SUCCEEDED(hr))
    {
        hr = ConfigureUsbPipes();
    }

    //
    // Setup power-management settings on the device.
    //

    if (SUCCEEDED(hr))
    {
        hr = SetPowerManagement();
    }

    //
    // Clear the seven segement display to indicate that we're done with 
    // prepare hardware.
    //

    if (SUCCEEDED(hr))
    {
        hr = IndicateDeviceReady();
    }

#if defined(_NOT_POWER_POLICY_OWNER_)
    //
    // We have non-power managed queues, so we Stop them in OnReleaseHardware
    // and start them in OnPrepareHardware
    //
    
    if (SUCCEEDED(hr))
    {
        m_ReadWriteQueue->Start();
        m_ControlQueue->Start();        
    }
#endif

    if (SUCCEEDED(hr))
    {
        hr = ConfigContReaderForInterruptEndPoint();
    }

    delete[] deviceName;

    return hr;
}
Esempio n. 2
0
HRESULT
CMyDevice::OnPrepareHardware(
	_In_ IWDFDevice * /* FxDevice */
	)
/*++

Routine Description:

	This routine is invoked to ready the driver
	to talk to hardware. It opens the handle to the 
	device and talks to it using the HID interface.

Arguments:

	FxDevice  : Pointer to the WDF device interface

Return Value:

	HRESULT 

--*/
{
	PWSTR instanceId = NULL;
	DWORD instanceIdCch = 0;
	HRESULT hr = S_OK;

	m_SwitchState.SwitchesAsUChar = 0;

	if (SUCCEEDED(hr))
	{	
		//
		// Find out the instance id buffer size.
		//
		hr = m_FxDevice->RetrieveDeviceInstanceId(NULL, &instanceIdCch);
	}

	if (SUCCEEDED(hr))
	{
		//
		// Allocate the buffer.
		//
		instanceId = new WCHAR[instanceIdCch];
		if (instanceId == NULL)
		{
			hr = E_OUTOFMEMORY;

			TraceEvents(TRACE_LEVEL_ERROR, 
						TEST_TRACE_DEVICE, 
						"%!FUNC! Out of memory"
						);
		}
	}

	if (SUCCEEDED(hr))
	{	
		//
		// Get the instance id.
		//
		hr = m_FxDevice->RetrieveDeviceInstanceId(instanceId, &instanceIdCch);
	}

	if (SUCCEEDED(hr))
	{
		TraceEvents(TRACE_LEVEL_INFORMATION, 
					TEST_TRACE_DEVICE, 
					"%!FUNC! Device instance id %S",
					instanceId
					);
	}
	else
	{
		TraceEvents(TRACE_LEVEL_ERROR, 
					TEST_TRACE_DEVICE, 
					"%!FUNC! Cannot get device instance id %!HRESULT!",
					hr
					);
	}

	if(SUCCEEDED(hr))
	{
		//
		// Create a file object.
		//
		hr = m_FxDevice->CreateWdfFile( NULL, &m_WdfFile );

		if (SUCCEEDED(hr) && nullptr != m_WdfFile)
		{
			//
			// Get our parent driver object.
			//
			m_FxDevice->GetDriver( &m_WdfDriver );

			//
			// Get the default I/O target.  This allows us to send IOCTLs
			// to the HID collection below us in the stack.
			//
			if (nullptr != m_WdfDriver)
			{
				m_FxDevice->GetDefaultIoTarget( &m_WdfIoTarget );
			}
			else
			{
				hr = E_UNEXPECTED;
				Trace(TRACE_LEVEL_ERROR, "Failed during GetDriver(), hr = %!HRESULT!", hr); 
			}

			if (nullptr == m_WdfIoTarget)
			{
				hr = E_UNEXPECTED;
				Trace(TRACE_LEVEL_ERROR, "Failed during GetDefaultIoTarget(), hr = %!HRESULT!", hr); 
			}
		}
		else
		{
			Trace(TRACE_LEVEL_ERROR, "m_WdfFile is NULL, hr = %!HRESULT!", hr); 
		}
	}
	else
	{
		Trace(TRACE_LEVEL_ERROR, "Failed during CreateWdfFile(), hr = %!HRESULT!", hr); 
	}
	
	if (SUCCEEDED(hr))
	{
		//
		// Get the HID capabilities and attributes.
		//
		hr = GetHidCapabilities();
	}

	if (SUCCEEDED(hr))
	{
		//
		// Queue up a pending read for input reports.
		//
		WUDF_TEST_DRIVER_ASSERT(m_Caps.InputReportByteLength == sizeof (m_SwitchStateReport));
		IWDFIoRequest *pWdfRequest;
		//
		// Create child request for the default I/O target
		//
		hr = m_FxDevice->CreateRequest(NULL, m_WdfIoTarget, &pWdfRequest);
		if (SUCCEEDED(hr))
		{
			hr = SendReadRequest(pWdfRequest);
			pWdfRequest->Release();
		}
		else
		{
			Trace(TRACE_LEVEL_ERROR, "Failed to create a request, hr = %!HRESULT!", hr); 
		}
	}

	if (SUCCEEDED(hr))
	{
		//
		// Set the seven segement display and bar graph to indicate that we're done with 
		// prepare hardware.
		//
		hr = IndicateDeviceReady();
	}

	if (FAILED(hr))
	{
		TraceEvents(TRACE_LEVEL_ERROR, 
					TEST_TRACE_DEVICE, 
					"%!FUNC! failed %!HRESULT!",
					hr
					);
	}

	delete[] instanceId;

	return hr;
}