123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- usbhub.c
- Abstract:
- This module implements support for the USB Hub driver.
- Author:
- Evan Green 16-Jan-2013
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/driver.h>
- #include <minoca/usb/usbhost.h>
- #include "usbhub.h"
- //
- // --------------------------------------------------------------------- Macros
- //
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- /*++
- Structure Description:
- This structure stores context about a USB Hub.
- Members:
- UsbCoreHandle - Stores the handle to the hub as identified by the USB core
- library.
- UsbHub - Stores an opaque pointer to the USB hub-specific context.
- --*/
- typedef struct _USB_HUB_DRIVER_CONTEXT {
- HANDLE UsbCoreHandle;
- PUSB_HUB UsbHub;
- } USB_HUB_DRIVER_CONTEXT, *PUSB_HUB_DRIVER_CONTEXT;
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- KSTATUS
- UsbHubAddDevice (
- PVOID Driver,
- PSTR DeviceId,
- PSTR ClassId,
- PSTR CompatibleIds,
- PVOID DeviceToken
- );
- VOID
- UsbHubDispatchStateChange (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- UsbHubDispatchOpen (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- UsbHubDispatchClose (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- UsbHubDispatchIo (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- UsbHubDispatchSystemControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- KSTATUS
- UsbHubpStartDevice (
- PIRP Irp,
- PUSB_HUB_DRIVER_CONTEXT Device
- );
- VOID
- UsbHubpEnumerateChildren (
- PIRP Irp,
- PUSB_HUB_DRIVER_CONTEXT Device
- );
- VOID
- UsbHubpRemoveDevice (
- PIRP Irp,
- PUSB_HUB_DRIVER_CONTEXT Device
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- PDRIVER UsbHubDriver = NULL;
- //
- // ------------------------------------------------------------------ Functions
- //
- KSTATUS
- DriverEntry (
- PDRIVER Driver
- )
- /*++
- Routine Description:
- This routine is the entry point for the USB Hub driver. It registers its
- other dispatch functions, and performs driver-wide initialization.
- Arguments:
- Driver - Supplies a pointer to the driver object.
- Return Value:
- STATUS_SUCCESS on success.
- Failure code on error.
- --*/
- {
- DRIVER_FUNCTION_TABLE FunctionTable;
- KSTATUS Status;
- UsbHubDriver = Driver;
- RtlZeroMemory(&FunctionTable, sizeof(DRIVER_FUNCTION_TABLE));
- FunctionTable.Version = DRIVER_FUNCTION_TABLE_VERSION;
- FunctionTable.AddDevice = UsbHubAddDevice;
- FunctionTable.DispatchStateChange = UsbHubDispatchStateChange;
- FunctionTable.DispatchOpen = UsbHubDispatchOpen;
- FunctionTable.DispatchClose = UsbHubDispatchClose;
- FunctionTable.DispatchIo = UsbHubDispatchIo;
- FunctionTable.DispatchSystemControl = UsbHubDispatchSystemControl;
- Status = IoRegisterDriverFunctions(Driver, &FunctionTable);
- return Status;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- KSTATUS
- UsbHubAddDevice (
- PVOID Driver,
- PSTR DeviceId,
- PSTR ClassId,
- PSTR CompatibleIds,
- PVOID DeviceToken
- )
- /*++
- Routine Description:
- This routine is called when a device is detected for which the USB Hub
- driver acts as the function driver. The driver will attach itself to the
- stack.
- Arguments:
- Driver - Supplies a pointer to the driver being called.
- DeviceId - Supplies a pointer to a string with the device ID.
- ClassId - Supplies a pointer to a string containing the device's class ID.
- CompatibleIds - Supplies a pointer to a string containing device IDs
- that would be compatible with this device.
- DeviceToken - Supplies an opaque token that the driver can use to identify
- the device in the system. This token should be used when attaching to
- the stack.
- Return Value:
- STATUS_SUCCESS on success.
- Failure code if the driver was unsuccessful in attaching itself.
- --*/
- {
- PUSB_HUB_DRIVER_CONTEXT NewDevice;
- KSTATUS Status;
- //
- // Create the device context and attach to the device.
- //
- NewDevice = MmAllocateNonPagedPool(sizeof(USB_HUB_DRIVER_CONTEXT),
- USB_HUB_ALLOCATION_TAG);
- if (NewDevice == NULL) {
- return STATUS_INSUFFICIENT_RESOURCES;
- }
- RtlZeroMemory(NewDevice, sizeof(USB_HUB_DRIVER_CONTEXT));
- NewDevice->UsbCoreHandle = INVALID_HANDLE;
- //
- // Attempt to attach to the USB core.
- //
- Status = UsbDriverAttach(DeviceToken,
- UsbHubDriver,
- &(NewDevice->UsbCoreHandle));
- if (!KSUCCESS(Status)) {
- goto AddDeviceEnd;
- }
- ASSERT(NewDevice->UsbCoreHandle != INVALID_HANDLE);
- //
- // Allow the USB core to create some hub context with this device.
- //
- Status = UsbCreateHub(NewDevice->UsbCoreHandle, &(NewDevice->UsbHub));
- if (!KSUCCESS(Status)) {
- goto AddDeviceEnd;
- }
- Status = IoAttachDriverToDevice(Driver, DeviceToken, NewDevice);
- AddDeviceEnd:
- if (!KSUCCESS(Status)) {
- if (NewDevice != NULL) {
- if (NewDevice->UsbCoreHandle != INVALID_HANDLE) {
- UsbDeviceClose(NewDevice->UsbCoreHandle);
- }
- MmFreeNonPagedPool(NewDevice);
- }
- }
- return Status;
- }
- VOID
- UsbHubDispatchStateChange (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles State Change IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- PUSB_HUB_DRIVER_CONTEXT Device;
- KSTATUS Status;
- ASSERT(Irp->MajorCode == IrpMajorStateChange);
- Device = (PUSB_HUB_DRIVER_CONTEXT)DeviceContext;
- //
- // If the device structure is NULL, then the USB hub driver is acting as a
- // bus driver. Complete some IRPs by default.
- //
- if (Device == NULL) {
- switch (Irp->MinorCode) {
- case IrpMinorRemoveDevice:
- case IrpMinorQueryResources:
- case IrpMinorStartDevice:
- case IrpMinorQueryChildren:
- if (Irp->Direction == IrpUp) {
- IoCompleteIrp(UsbHubDriver, Irp, STATUS_SUCCESS);
- }
- break;
- default:
- break;
- }
- //
- // If the device is non-NULL, the driver is acting as a function driver for
- // the USB hub.
- //
- } else {
- switch (Irp->MinorCode) {
- case IrpMinorQueryResources:
- if (Irp->Direction == IrpUp) {
- IoCompleteIrp(UsbHubDriver, Irp, STATUS_SUCCESS);
- }
- break;
- case IrpMinorStartDevice:
- //
- // Attempt to fire the thing up if the bus has already started it.
- //
- if (Irp->Direction == IrpUp) {
- Status = UsbHubpStartDevice(Irp, Device);
- if (!KSUCCESS(Status)) {
- IoCompleteIrp(UsbHubDriver, Irp, Status);
- }
- }
- break;
- case IrpMinorQueryChildren:
- if (Irp->Direction == IrpUp) {
- UsbHubpEnumerateChildren(Irp, Device);
- }
- break;
- case IrpMinorRemoveDevice:
- if (Irp->Direction == IrpUp) {
- UsbHubpRemoveDevice(Irp, Device);
- }
- break;
- //
- // For all other IRPs, do nothing.
- //
- default:
- break;
- }
- }
- return;
- }
- VOID
- UsbHubDispatchOpen (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles Open IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- return;
- }
- VOID
- UsbHubDispatchClose (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles Close IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- return;
- }
- VOID
- UsbHubDispatchIo (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles I/O IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- return;
- }
- VOID
- UsbHubDispatchSystemControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles System Control IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- ASSERT(Irp->MajorCode == IrpMajorSystemControl);
- //
- // Do no processing on any IRPs. Let them flow.
- //
- return;
- }
- KSTATUS
- UsbHubpStartDevice (
- PIRP Irp,
- PUSB_HUB_DRIVER_CONTEXT Device
- )
- /*++
- Routine Description:
- This routine starts up the USB Hub.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- Device - Supplies a pointer to this hub device.
- Return Value:
- Status code.
- --*/
- {
- KSTATUS Status;
- Status = UsbStartHub(Device->UsbHub);
- if (!KSUCCESS(Status)) {
- goto StartDeviceEnd;
- }
- StartDeviceEnd:
- return Status;
- }
- VOID
- UsbHubpEnumerateChildren (
- PIRP Irp,
- PUSB_HUB_DRIVER_CONTEXT Device
- )
- /*++
- Routine Description:
- This routine enumerates the USB Hub's children.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- Device - Supplies a pointer to this hub device.
- Return Value:
- None.
- --*/
- {
- KSTATUS Status;
- //
- // Forward this on to the USB core to figure out.
- //
- Status = UsbHubQueryChildren(Irp, Device->UsbHub);
- IoCompleteIrp(UsbHubDriver, Irp, Status);
- return;
- }
- VOID
- UsbHubpRemoveDevice (
- PIRP Irp,
- PUSB_HUB_DRIVER_CONTEXT Device
- )
- /*++
- Routine Description:
- This routine removes the USB hub device.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- Device - Supplies a pointer to this hub device.
- Return Value:
- None.
- --*/
- {
- //
- // Set the device for removal in USB core. This will mark the device as
- // disconnected and cancel all of its transfers.
- //
- UsbDetachDevice(Device->UsbCoreHandle);
- //
- // Destroy the hub. This will remove all of the device's children and
- // destroy any hub-specific state.
- //
- UsbDestroyHub(Device->UsbHub);
- //
- // Release the reference on the USB core handle that was taken when the hub
- // device was added.
- //
- UsbDeviceClose(Device->UsbCoreHandle);
- //
- // Free the hub context.
- //
- MmFreeNonPagedPool(Device);
- return;
- }
|