iPXE
Defines | Functions | Variables
usbio.c File Reference

EFI_USB_IO_PROTOCOL pseudo Host Controller Interface driver. More...

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/efi_utils.h>
#include <ipxe/efi/Protocol/UsbIo.h>
#include <ipxe/usb.h>
#include "usbio.h"

Go to the source code of this file.

Defines

#define ENOTSUP_MORONIC_SPECIFICATION   __einfo_error ( EINFO_ENOTSUP_MORONIC_SPECIFICATION )
#define EINFO_ENOTSUP_MORONIC_SPECIFICATION

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int usbio_interface (struct usbio_device *usbio, struct usb_endpoint *ep)
 Determine endpoint interface number.
static int usbio_open (struct usbio_device *usbio, unsigned int interface)
 Open USB I/O interface.
static void usbio_close (struct usbio_device *usbio, unsigned int interface)
 Close USB I/O interface.
static int usbio_control_open (struct usbio_endpoint *endpoint __unused)
 Open control endpoint.
static void usbio_control_close (struct usbio_endpoint *endpoint __unused)
 Close control endpoint.
static void usbio_control_poll (struct usbio_endpoint *endpoint)
 Poll control endpoint.
static int usbio_bulk_in_open (struct usbio_endpoint *endpoint __unused)
 Open bulk IN endpoint.
static void usbio_bulk_in_close (struct usbio_endpoint *endpoint __unused)
 Close bulk IN endpoint.
static void usbio_bulk_in_poll (struct usbio_endpoint *endpoint)
 Poll bulk IN endpoint.
static int usbio_bulk_out_open (struct usbio_endpoint *endpoint __unused)
 Open bulk OUT endpoint.
static void usbio_bulk_out_close (struct usbio_endpoint *endpoint __unused)
 Close bulk OUT endpoint.
static void usbio_bulk_out_poll (struct usbio_endpoint *endpoint)
 Poll bulk OUT endpoint.
static EFI_STATUS EFIAPI usbio_interrupt_callback (VOID *data, UINTN len, VOID *context, UINT32 status)
 Interrupt endpoint callback.
static int usbio_interrupt_open (struct usbio_endpoint *endpoint)
 Open interrupt endpoint.
static void usbio_interrupt_close (struct usbio_endpoint *endpoint)
 Close interrupt endpoint.
static void usbio_interrupt_poll (struct usbio_endpoint *endpoint)
 Poll interrupt endpoint.
static int usbio_endpoint_open (struct usb_endpoint *ep)
 Open endpoint.
static void usbio_endpoint_close (struct usb_endpoint *ep)
 Close endpoint.
static int usbio_endpoint_reset (struct usb_endpoint *ep __unused)
 Reset endpoint.
static int usbio_endpoint_mtu (struct usb_endpoint *ep __unused)
 Update MTU.
static int usbio_endpoint_enqueue (struct usb_endpoint *ep, struct io_buffer *iobuf, unsigned int flags)
 Enqueue transfer.
static int usbio_endpoint_message (struct usb_endpoint *ep, struct io_buffer *iobuf)
 Enqueue message transfer.
static int usbio_endpoint_stream (struct usb_endpoint *ep, struct io_buffer *iobuf, int zlp)
 Enqueue stream transfer.
static void usbio_endpoint_poll (struct usbio_endpoint *endpoint)
 Poll for completions.
static int usbio_device_open (struct usb_device *usb)
 Open device.
static void usbio_device_close (struct usb_device *usb __unused)
 Close device.
static int usbio_device_address (struct usb_device *usb __unused)
 Assign device address.
static int usbio_hub_open (struct usb_hub *hub)
 Open hub.
static void usbio_hub_close (struct usb_hub *hub __unused)
 Close hub.
static int usbio_root_open (struct usb_hub *hub __unused)
 Open root hub.
static void usbio_root_close (struct usb_hub *hub __unused)
 Close root hub.
static int usbio_root_enable (struct usb_hub *hub __unused, struct usb_port *port __unused)
 Enable port.
static int usbio_root_disable (struct usb_hub *hub __unused, struct usb_port *port __unused)
 Disable port.
static int usbio_root_speed (struct usb_hub *hub __unused, struct usb_port *port)
 Update root hub port speed.
static int usbio_root_clear_tt (struct usb_hub *hub __unused, struct usb_port *port __unused, struct usb_endpoint *ep __unused)
 Clear transaction translator buffer.
static int usbio_bus_open (struct usb_bus *bus __unused)
 Open USB bus.
static void usbio_bus_close (struct usb_bus *bus __unused)
 Close USB bus.
static void usbio_bus_poll (struct usb_bus *bus)
 Poll USB bus.
static int usbio_supported (EFI_HANDLE handle)
 Check to see if driver supports a device.
static int usbio_config (struct usbio_device *usbio)
 Fetch configuration descriptor.
static int usbio_path (struct usbio_device *usbio)
 Construct device path for opening other interfaces.
static int usbio_interfaces (struct usbio_device *usbio)
 Construct interface list.
static int usbio_start (struct efi_device *efidev)
 Attach driver to device.
static void usbio_stop (struct efi_device *efidev)
 Detach driver from device.
struct efi_driver usbio_driver __efi_driver (EFI_DRIVER_NORMAL)
 EFI USB I/O driver.

Variables

static struct usbio_operations usbio_control_operations
 Control endpoint operations.
static struct usbio_operations usbio_bulk_in_operations
 Bulk endpoint operations.
static struct usbio_operations usbio_bulk_out_operations
 Bulk endpoint operations.
static struct usbio_operations usbio_interrupt_operations
 Interrupt endpoint operations.
static struct usb_host_operations usbio_operations
 USB I/O host controller driver operations.

Detailed Description

EFI_USB_IO_PROTOCOL pseudo Host Controller Interface driver.

The EFI_USB_IO_PROTOCOL is an almost unbelievably poorly designed abstraction of a USB device. It would be just about forgivable for an API to support only synchronous operation for bulk OUT endpoints. It is imbecilic to support only synchronous operation for bulk IN endpoints. This apparently intern-designed API throttles a typical NIC down to 1.5% of its maximum throughput. That isn't a typo. It really is that slow.

We can't even work around this stupidity by talking to the host controller abstraction directly, because an identical limitation exists in the EFI_USB2_HC_PROTOCOL.

Unless you derive therapeutic value from watching download progress indicators lethargically creep through every single integer from 0 to 100, you should use iPXE's native USB host controller drivers instead. (Or just upgrade from UEFI to "legacy" BIOS, which will produce a similar speed increase.)

For added excitement, the EFI_USB_IO_PROTOCOL makes the (demonstrably incorrect) assumption that a USB driver needs to attach to exactly one interface within a USB device, and provides a helper method to retrieve "the" interface descriptor. Since pretty much every USB network device requires binding to a pair of control+data interfaces, this aspect of EFI_USB_IO_PROTOCOL is of no use to us.

We have our own existing code for reading USB descriptors, so we don't actually care that the UsbGetInterfaceDescriptor() method provided by EFI_USB_IO_PROTOCOL is useless for network devices. We can read the descriptors ourselves (via UsbControlTransfer()) and get all of the information we need this way. We can even work around the fact that EFI_USB_IO_PROTOCOL provides separate handles for each of the two interfaces comprising our network device.

However, if we discover that we need to select an alternative device configuration (e.g. for devices exposing both RNDIS and ECM), then all hell breaks loose. EFI_USB_IO_PROTOCOL starts to panic because its cached interface and endpoint descriptors will no longer be valid. As mentioned above, the cached descriptors are useless for network devices anyway so we _really_ don't care about this, but EFI_USB_IO_PROTOCOL certainly cares. It prints out a manic warning message containing no fewer than six exclamation marks and then literally commits seppuku in the middle of the UsbControlTransfer() method by attempting to uninstall itself. Quite how the caller is supposed to react when asked to stop using the EFI_USB_IO_PROTOCOL instance while in the middle of an uninterruptible call to said instance is left as an exercise for the interested reader.

There is no sensible way to work around this, so we just preemptively fail if asked to change the device configuration, on the basis that reporting a sarcastic error message is often preferable to jumping through a NULL pointer and crashing the system.

Definition in file usbio.c.


Define Documentation

Definition at line 100 of file usbio.c.

Referenced by usbio_control_poll().

Value:
__einfo_uniqify ( EINFO_ENOTSUP, 0x01,                          \
                          "EFI_USB_IO_PROTOCOL was designed by morons" )

Definition at line 102 of file usbio.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int usbio_interface ( struct usbio_device usbio,
struct usb_endpoint ep 
) [static]

Determine endpoint interface number.

Parameters:
usbioUSB I/O device
epUSB Endpoint
Return values:
interfaceInterface number, or negative error

Definition at line 120 of file usbio.c.

References usb_endpoint::address, usbio_device::config, usb_configuration_descriptor::config, usb_function_descriptor::count, DBGC, usb_function::desc, efi_handle_name(), usb_endpoint_descriptor::endpoint, ENOENT, usbio_device::first, for_each_config_descriptor, for_each_interface_descriptor, usb_device::functions, handle, usbio_device::handle, usb_interface_descriptor::header, usb_endpoint_descriptor::header, usb_interface_descriptor::interface, usb_function::interface, usb_function::list, list_for_each_entry, usb_descriptor_header::type, usb_endpoint::usb, usb_endpoint_name(), and USB_EP0_ADDRESS.

                                                       {
        EFI_HANDLE handle = usbio->handle;
        struct usb_device *usb = ep->usb;
        struct usb_configuration_descriptor *config;
        struct usb_interface_descriptor *interface;
        struct usb_endpoint_descriptor *endpoint;
        struct usb_function *func;
        unsigned int i;

        /* The control endpoint is not part of a described interface */
        if ( ep->address == USB_EP0_ADDRESS )
                return 0;

        /* Iterate over all interface descriptors looking for a match */
        config = usbio->config;
        for_each_config_descriptor ( interface, config ) {

                /* Skip non-interface descriptors */
                if ( interface->header.type != USB_INTERFACE_DESCRIPTOR )
                        continue;

                /* Iterate over all endpoint descriptors looking for a match */
                for_each_interface_descriptor ( endpoint, config, interface ) {

                        /* Skip non-endpoint descriptors */
                        if ( endpoint->header.type != USB_ENDPOINT_DESCRIPTOR )
                                continue;

                        /* Check endpoint address */
                        if ( endpoint->endpoint != ep->address )
                                continue;

                        /* Check interface belongs to this function */
                        list_for_each_entry ( func, &usb->functions, list ) {

                                /* Skip non-matching functions */
                                if ( func->interface[0] != usbio->first )
                                        continue;

                                /* Iterate over all interfaces for a match */
                                for ( i = 0 ; i < func->desc.count ; i++ ) {
                                        if ( interface->interface ==
                                             func->interface[i] )
                                                return interface->interface;
                                }
                        }
                }
        }

        DBGC ( usbio, "USBIO %s cannot find interface for %s",
               efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
        return -ENOENT;
}
static int usbio_open ( struct usbio_device usbio,
unsigned int  interface 
) [static]

Open USB I/O interface.

Parameters:
usbioUSB I/O device
interfaceInterface number
Return values:
rcReturn status code

Definition at line 182 of file usbio.c.

References assert, EFI_SYSTEM_TABLE::BootServices, usbio_interface::count, DBGC, DBGC_EFI_OPENERS, EEFI, efi_devpath_end(), efi_devpath_text(), efi_handle_name(), efi_image_handle, EFI_OPEN_PROTOCOL_BY_DRIVER, EFI_OPEN_PROTOCOL_EXCLUSIVE, efi_systab, efi_usb_io_protocol_guid, end, EXDEV, handle, usbio_interface::handle, usbio_device::handle, usbio_device::interface, USB_DEVICE_PATH::InterfaceNumber, usbio_interface::io, EFI_BOOT_SERVICES::LocateDevicePath, EFI_BOOT_SERVICES::OpenProtocol, usbio_device::path, rc, strerror(), u, and usbio_device::usbpath.

Referenced by usbio_control_poll(), and usbio_endpoint_open().

                                                                             {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE handle = usbio->handle;
        struct usbio_interface *intf = &usbio->interface[interface];
        EFI_DEVICE_PATH_PROTOCOL *path;
        EFI_DEVICE_PATH_PROTOCOL *end;
        USB_DEVICE_PATH *usbpath;
        union {
                void *interface;
                EFI_USB_IO_PROTOCOL *io;
        } u;
        EFI_STATUS efirc;
        int rc;

        /* Sanity check */
        assert ( interface < usbio->config->interfaces );

        /* If interface is already open, just increment the usage count */
        if ( intf->count ) {
                intf->count++;
                return 0;
        }

        /* Construct device path for this interface */
        path = usbio->path;
        usbpath = usbio->usbpath;
        usbpath->InterfaceNumber = interface;
        end = efi_devpath_end ( path );

        /* Locate handle for this endpoint's interface */
        if ( ( efirc = bs->LocateDevicePath ( &efi_usb_io_protocol_guid, &path,
                                              &intf->handle ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s could not locate ",
                       efi_handle_name ( handle ) );
                DBGC ( usbio, "%s: %s\n",
                       efi_devpath_text ( usbio->path ), strerror ( rc ) );
                return rc;
        }

        /* Check that expected path was located */
        if ( path != end ) {
                DBGC ( usbio, "USBIO %s located incomplete ",
                       efi_handle_name ( handle ) );
                DBGC ( usbio, "%s\n", efi_handle_name ( intf->handle ) );
                return -EXDEV;
        }

        /* Open USB I/O protocol on this handle */
        if ( ( efirc = bs->OpenProtocol ( intf->handle,
                                          &efi_usb_io_protocol_guid,
                                          &u.interface, efi_image_handle,
                                          intf->handle,
                                          ( EFI_OPEN_PROTOCOL_BY_DRIVER |
                                            EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s cannot open ",
                       efi_handle_name ( handle ) );
                DBGC ( usbio, "%s: %s\n",
                       efi_handle_name ( intf->handle ), strerror ( rc ) );
                DBGC_EFI_OPENERS ( usbio, intf->handle,
                                   &efi_usb_io_protocol_guid );
                return rc;
        }
        intf->io = u.io;

        /* Increment usage count */
        intf->count++;

        return 0;
}
static void usbio_close ( struct usbio_device usbio,
unsigned int  interface 
) [static]

Close USB I/O interface.

Parameters:
usbioUSB I/O device
interfaceInterface number

Definition at line 260 of file usbio.c.

References assert, EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseProtocol, usbio_interface::count, efi_image_handle, efi_systab, efi_usb_io_protocol_guid, usbio_interface::handle, and usbio_device::interface.

Referenced by usbio_control_poll(), usbio_endpoint_close(), and usbio_endpoint_open().

                                                                               {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        struct usbio_interface *intf = &usbio->interface[interface];

        /* Sanity checks */
        assert ( interface < usbio->config->interfaces );
        assert ( intf->count > 0 );

        /* Decrement usage count */
        intf->count--;

        /* Do nothing if interface is still in use */
        if ( intf->count )
                return;

        /* Close USB I/O protocol */
        bs->CloseProtocol ( intf->handle, &efi_usb_io_protocol_guid,
                            efi_image_handle, intf->handle );
}
static int usbio_control_open ( struct usbio_endpoint *endpoint  __unused) [static]

Open control endpoint.

Parameters:
endpointEndpoint
Return values:
rcReturn status code

Definition at line 293 of file usbio.c.

                                                                           {

        /* Nothing to do */
        return 0;
}
static void usbio_control_close ( struct usbio_endpoint *endpoint  __unused) [static]

Close control endpoint.

Parameters:
endpointEndpoint

Definition at line 304 of file usbio.c.

                                                                             {

        /* Nothing to do */
}
static void usbio_control_poll ( struct usbio_endpoint endpoint) [static]

Poll control endpoint.

Parameters:
endpointEndpoint

Definition at line 314 of file usbio.c.

References assert, usbio_device::config, usb_configuration_descriptor::config, usbio_endpoint::cons, io_buffer::data, data, DBGC, direction, EEFI, efi_handle_name(), EfiUsbDataIn, EfiUsbDataOut, EfiUsbNoData, ENOTSUP, ENOTSUP_MORONIC_SPECIFICATION, usbio_endpoint::ep, usbio_endpoint::flags, flags, handle, usbio_interface::handle, usbio_device::handle, index, usbio_device::interface, usbio_interface::io, iob_len(), iob_pull, usbio_endpoint::iobuf, le16_to_cpu, len, msg(), NULL, usbio_endpoint::prod, rc, request, status, strerror(), usb_complete(), usb_complete_err(), USB_DIR_IN, usb_endpoint_name(), USB_RECIP_INTERFACE, USB_RECIP_MASK, USB_SET_CONFIGURATION, _EFI_USB_IO_PROTOCOL::UsbControlTransfer, usbio_endpoint::usbio, usbio_close(), USBIO_MESSAGE, usbio_open(), and USBIO_RING_COUNT.

                                                                   {
        struct usbio_device *usbio = endpoint->usbio;
        struct usb_endpoint *ep = endpoint->ep;
        EFI_HANDLE handle = usbio->handle;
        EFI_USB_IO_PROTOCOL *io;
        union {
                struct usb_setup_packet setup;
                EFI_USB_DEVICE_REQUEST efi;
        } *msg;
        EFI_USB_DATA_DIRECTION direction;
        struct io_buffer *iobuf;
        unsigned int index;
        unsigned int flags;
        unsigned int recipient;
        unsigned int interface;
        uint16_t request;
        void *data;
        size_t len;
        UINT32 status;
        EFI_STATUS efirc;
        int rc;

        /* Do nothing if ring is empty */
        if ( endpoint->cons == endpoint->prod )
                return;

        /* Consume next transfer */
        index = ( endpoint->cons++ % USBIO_RING_COUNT );
        iobuf = endpoint->iobuf[index];
        flags = endpoint->flags[index];

        /* Sanity check */
        if ( ! ( flags & USBIO_MESSAGE ) ) {
                DBGC ( usbio, "USBIO %s %s non-message transfer\n",
                       efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
                rc = -ENOTSUP;
                goto err_not_message;
        }

        /* Construct transfer */
        assert ( iob_len ( iobuf ) >= sizeof ( *msg ) );
        msg = iobuf->data;
        iob_pull ( iobuf, sizeof ( *msg ) );
        request = le16_to_cpu ( msg->setup.request );
        len = iob_len ( iobuf );
        if ( len ) {
                data = iobuf->data;
                direction = ( ( request & USB_DIR_IN ) ?
                              EfiUsbDataIn : EfiUsbDataOut );
        } else {
                data = NULL;
                direction = EfiUsbNoData;
        }

        /* Determine interface for this transfer */
        recipient = ( request & USB_RECIP_MASK );
        if ( recipient == USB_RECIP_INTERFACE ) {
                /* Recipient is an interface: use interface number directly */
                interface = le16_to_cpu ( msg->setup.index );
        } else {
                /* Route all other requests through the first interface */
                interface = 0;
        }

        /* Open interface */
        if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
                goto err_open;
        io = usbio->interface[interface].io;

        /* Due to the design of EFI_USB_IO_PROTOCOL, attempting to set
         * the configuration to a non-default value is basically a
         * self-destruct button.
         */
        if ( ( request == USB_SET_CONFIGURATION ) &&
             ( le16_to_cpu ( msg->setup.value ) != usbio->config->config ) ) {
                rc = -ENOTSUP_MORONIC_SPECIFICATION;
                DBGC ( usbio, "USBIO %s cannot change configuration: %s\n",
                       efi_handle_name ( handle ), strerror ( rc ) );
                goto err_moronic_specification;
        }

        /* Submit transfer */
        if ( ( efirc = io->UsbControlTransfer ( io, &msg->efi, direction, 0,
                                                data, len, &status ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s %s could not submit control transfer ",
                       efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
                DBGC ( usbio, "via %s: %s (status %04x)\n",
                       efi_handle_name ( usbio->interface[interface].handle ),
                       strerror ( rc ), status );
                goto err_transfer;
        }

        /* Close interface */
        usbio_close ( usbio, interface );

        /* Complete transfer */
        usb_complete ( ep, iobuf );

        return;

 err_transfer:
 err_moronic_specification:
        usbio_close ( usbio, interface );
 err_open:
 err_not_message:
        usb_complete_err ( ep, iobuf, rc );
}
static int usbio_bulk_in_open ( struct usbio_endpoint *endpoint  __unused) [static]

Open bulk IN endpoint.

Parameters:
endpointEndpoint
Return values:
rcReturn status code

Definition at line 443 of file usbio.c.

                                                                           {

        /* Nothing to do */
        return 0;
}
static void usbio_bulk_in_close ( struct usbio_endpoint *endpoint  __unused) [static]

Close bulk IN endpoint.

Parameters:
endpointEndpoint

Definition at line 454 of file usbio.c.

                                                                             {

        /* Nothing to do */
}
static void usbio_bulk_in_poll ( struct usbio_endpoint endpoint) [static]

Poll bulk IN endpoint.

Parameters:
endpointEndpoint

Definition at line 464 of file usbio.c.

References usb_endpoint::address, usbio_endpoint::cons, io_buffer::data, DBGC2, EEFI, efi_handle_name(), EFI_TIMEOUT, usbio_endpoint::ep, handle, usbio_device::handle, index, usbio_endpoint::io, iob_len(), iob_put, usbio_endpoint::iobuf, len, usbio_endpoint::prod, rc, status, strerror(), usb_complete(), usb_complete_err(), usb_endpoint_name(), _EFI_USB_IO_PROTOCOL::UsbBulkTransfer, usbio_endpoint::usbio, and USBIO_RING_COUNT.

                                                                   {
        struct usbio_device *usbio = endpoint->usbio;
        struct usb_endpoint *ep = endpoint->ep;
        EFI_USB_IO_PROTOCOL *io = endpoint->io;
        EFI_HANDLE handle = usbio->handle;
        struct io_buffer *iobuf;
        unsigned int index;
        UINTN len;
        UINT32 status;
        EFI_STATUS efirc;
        int rc;

        /* Do nothing if ring is empty */
        if ( endpoint->cons == endpoint->prod )
                return;

        /* Attempt (but do not yet consume) next transfer */
        index = ( endpoint->cons % USBIO_RING_COUNT );
        iobuf = endpoint->iobuf[index];

        /* Construct transfer */
        len = iob_len ( iobuf );

        /* Upon being turned on, the EFI_USB_IO_PROTOCOL did nothing
         * for several minutes before firing a small ARP packet a few
         * millimetres into the ether.
         */
        efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
                                      &len, 1, &status );
        if ( efirc == EFI_TIMEOUT )
                return;

        /* Consume transfer */
        endpoint->cons++;

        /* Check for failure */
        if ( efirc != 0 ) {
                rc = -EEFI ( efirc );
                DBGC2 ( usbio, "USBIO %s %s could not submit bulk IN transfer: "
                        "%s (status %04x)\n", efi_handle_name ( handle ),
                        usb_endpoint_name ( ep ), strerror ( rc ), status );
                usb_complete_err ( ep, iobuf, rc );
                return;
        }

        /* Update length */
        iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );

        /* Complete transfer */
        usb_complete ( ep, iobuf );
}
static int usbio_bulk_out_open ( struct usbio_endpoint *endpoint  __unused) [static]

Open bulk OUT endpoint.

Parameters:
endpointEndpoint
Return values:
rcReturn status code

Definition at line 536 of file usbio.c.

                                                                            {

        /* Nothing to do */
        return 0;
}
static void usbio_bulk_out_close ( struct usbio_endpoint *endpoint  __unused) [static]

Close bulk OUT endpoint.

Parameters:
endpointEndpoint

Definition at line 547 of file usbio.c.

                                                                              {

        /* Nothing to do */
}
static void usbio_bulk_out_poll ( struct usbio_endpoint endpoint) [static]

Poll bulk OUT endpoint.

Parameters:
endpointEndpoint

Definition at line 557 of file usbio.c.

References usb_endpoint::address, usbio_endpoint::cons, io_buffer::data, DBGC, EEFI, efi_handle_name(), usbio_endpoint::ep, usbio_endpoint::flags, flags, handle, usbio_device::handle, index, usbio_endpoint::io, iob_len(), iob_put, usbio_endpoint::iobuf, len, NULL, usbio_endpoint::prod, rc, status, strerror(), usb_complete(), usb_complete_err(), usb_endpoint_name(), _EFI_USB_IO_PROTOCOL::UsbBulkTransfer, usbio_endpoint::usbio, USBIO_RING_COUNT, and USBIO_ZLEN.

                                                                    {
        struct usbio_device *usbio = endpoint->usbio;
        struct usb_endpoint *ep = endpoint->ep;
        EFI_USB_IO_PROTOCOL *io = endpoint->io;
        EFI_HANDLE handle = usbio->handle;
        struct io_buffer *iobuf;
        unsigned int index;
        unsigned int flags;
        UINTN len;
        UINT32 status;
        EFI_STATUS efirc;
        int rc;

        /* Do nothing if ring is empty */
        if ( endpoint->cons == endpoint->prod )
                return;

        /* Consume next transfer */
        index = ( endpoint->cons++ % USBIO_RING_COUNT );
        iobuf = endpoint->iobuf[index];
        flags = endpoint->flags[index];

        /* Construct transfer */
        len = iob_len ( iobuf );

        /* Submit transfer */
        if ( ( efirc = io->UsbBulkTransfer ( io, ep->address, iobuf->data,
                                             &len, 0, &status ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s %s could not submit bulk OUT transfer: "
                       "%s (status %04x)\n", efi_handle_name ( handle ),
                       usb_endpoint_name ( ep ), strerror ( rc ), status );
                goto err;
        }

        /* Update length */
        iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );

        /* Submit zero-length transfer if required */
        len = 0;
        if ( ( flags & USBIO_ZLEN ) &&
             ( efirc = io->UsbBulkTransfer ( io, ep->address, NULL, &len, 0,
                                             &status ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s %s could not submit zero-length "
                       "transfer: %s (status %04x)\n",
                       efi_handle_name ( handle ), usb_endpoint_name ( ep ),
                       strerror ( rc ), status );
                goto err;
        }

        /* Complete transfer */
        usb_complete ( ep, iobuf );

        return;

 err:
        usb_complete_err ( ep, iobuf, rc );
}
static EFI_STATUS EFIAPI usbio_interrupt_callback ( VOID data,
UINTN  len,
VOID context,
UINT32  status 
) [static]

Interrupt endpoint callback.

Parameters:
dataReceived data
lenLength of received data
contextCallback context
statusTransfer status
Return values:
efircEFI status code

Definition at line 664 of file usbio.c.

References assert, usbio_interrupt_ring::cons, context, usbio_interrupt_ring::data, DBGC, efi_handle_name(), usbio_interrupt_ring::endpoint, usbio_endpoint::ep, fill, handle, usbio_device::handle, index, intr, usbio_interrupt_ring::len, len, memcpy(), mtu, usbio_interrupt_ring::prod, usb_endpoint_name(), usbio_endpoint::usbio, and USBIO_INTR_COUNT.

Referenced by usbio_interrupt_open().

                                                                    {
        struct usbio_interrupt_ring *intr = context;
        struct usbio_endpoint *endpoint = intr->endpoint;
        struct usbio_device *usbio = endpoint->usbio;
        struct usb_endpoint *ep = endpoint->ep;
        EFI_HANDLE handle = usbio->handle;
        unsigned int fill;
        unsigned int index;

        /* Sanity check */
        assert ( len <= ep->mtu );

        /* Do nothing if ring is empty */
        fill = ( intr->prod - intr->cons );
        if ( fill >= USBIO_INTR_COUNT ) {
                DBGC ( usbio, "USBIO %s %s dropped interrupt completion\n",
                       efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
                return 0;
        }

        /* Do nothing if transfer was unsuccessful */
        if ( status != 0 ) {
                DBGC ( usbio, "USBIO %s %s interrupt completion status %04x\n",
                       efi_handle_name ( handle ), usb_endpoint_name ( ep ),
                       status );
                return 0; /* Unclear what failure actually means here */
        }

        /* Copy data to buffer and increment producer counter */
        index = ( intr->prod % USBIO_INTR_COUNT );
        memcpy ( intr->data[index], data, len );
        intr->len[index] = len;
        intr->prod++;

        return 0;
}
static int usbio_interrupt_open ( struct usbio_endpoint endpoint) [static]

Open interrupt endpoint.

Parameters:
endpointEndpoint
Return values:
rcReturn status code

Definition at line 709 of file usbio.c.

References usb_endpoint::address, usbio_interrupt_ring::data, data, DBGC, EEFI, efi_handle_name(), usbio_interrupt_ring::endpoint, ENOMEM, usbio_endpoint::ep, FALSE, free, handle, usbio_device::handle, usb_endpoint::interval, usbio_endpoint::intr, intr, usbio_endpoint::io, usb_endpoint::mtu, NULL, rc, strerror(), TRUE, usb_endpoint_name(), _EFI_USB_IO_PROTOCOL::UsbAsyncInterruptTransfer, usbio_endpoint::usbio, usbio_interrupt_callback(), USBIO_INTR_COUNT, and zalloc().

                                                                    {
        struct usbio_device *usbio = endpoint->usbio;
        struct usbio_interrupt_ring *intr;
        struct usb_endpoint *ep = endpoint->ep;
        EFI_USB_IO_PROTOCOL *io = endpoint->io;
        EFI_HANDLE handle = usbio->handle;
        unsigned int interval;
        unsigned int i;
        void *data;
        EFI_STATUS efirc;
        int rc;

        /* Allocate interrupt ring buffer */
        intr = zalloc ( sizeof ( *intr ) + ( USBIO_INTR_COUNT * ep->mtu ) );
        if ( ! intr ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        endpoint->intr = intr;
        intr->endpoint = endpoint;
        data = ( ( ( void * ) intr ) + sizeof ( *intr ) );
        for ( i = 0 ; i < USBIO_INTR_COUNT ; i++ ) {
                intr->data[i] = data;
                data += ep->mtu;
        }

        /* Determine polling interval */
        interval = ( ep->interval >> 3 /* microframes -> milliseconds */ );
        if ( ! interval )
                interval = 1; /* May not be zero */

        /* Add to periodic schedule */
        if ( ( efirc = io->UsbAsyncInterruptTransfer ( io, ep->address, TRUE,
                                                       interval, ep->mtu,
                                                       usbio_interrupt_callback,
                                                       intr ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s %s could not schedule interrupt "
                       "transfer: %s\n", efi_handle_name ( handle ),
                       usb_endpoint_name ( ep ), strerror ( rc ) );
                goto err_schedule;
        }

        return 0;

        io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
                                        NULL, NULL );
 err_schedule:
        free ( intr );
 err_alloc:
        return rc;
}
static void usbio_interrupt_close ( struct usbio_endpoint endpoint) [static]

Close interrupt endpoint.

Parameters:
endpointEndpoint

Definition at line 767 of file usbio.c.

References usb_endpoint::address, usbio_endpoint::ep, FALSE, free, usbio_endpoint::intr, usbio_endpoint::io, NULL, and _EFI_USB_IO_PROTOCOL::UsbAsyncInterruptTransfer.

                                                                      {
        struct usb_endpoint *ep = endpoint->ep;
        EFI_USB_IO_PROTOCOL *io = endpoint->io;

        /* Remove from periodic schedule */
        io->UsbAsyncInterruptTransfer ( io, ep->address, FALSE, 0, 0,
                                        NULL, NULL );

        /* Free interrupt ring buffer */
        free ( endpoint->intr );
}
static void usbio_interrupt_poll ( struct usbio_endpoint endpoint) [static]

Poll interrupt endpoint.

Parameters:
endpointEndpoint

Definition at line 784 of file usbio.c.

References assert, usbio_interrupt_ring::cons, usbio_endpoint::cons, usbio_interrupt_ring::data, io_buffer::data, usbio_endpoint::ep, index, usbio_endpoint::intr, intr, iob_len(), iob_put, usbio_endpoint::iobuf, usbio_interrupt_ring::len, len, memcpy(), usbio_interrupt_ring::prod, usbio_endpoint::prod, usb_complete(), USBIO_INTR_COUNT, and USBIO_RING_COUNT.

                                                                     {
        struct usbio_interrupt_ring *intr = endpoint->intr;
        struct usb_endpoint *ep = endpoint->ep;
        struct io_buffer *iobuf;
        unsigned int index;
        unsigned int intr_index;
        size_t len;

        /* Do nothing if ring is empty */
        if ( endpoint->cons == endpoint->prod )
                return;

        /* Do nothing if interrupt ring is empty */
        if ( intr->cons == intr->prod )
                return;

        /* Consume next transfer */
        index = ( endpoint->cons++ % USBIO_RING_COUNT );
        iobuf = endpoint->iobuf[index];

        /* Populate I/O buffer */
        intr_index = ( intr->cons++ % USBIO_INTR_COUNT );
        len = intr->len[intr_index];
        assert ( len <= iob_len ( iobuf ) );
        iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
        memcpy ( iobuf->data, intr->data[intr_index], len );

        /* Complete transfer */
        usb_complete ( ep, iobuf );
}
static int usbio_endpoint_open ( struct usb_endpoint ep) [static]

Open endpoint.

Parameters:
epUSB endpoint
Return values:
rcReturn status code

Definition at line 835 of file usbio.c.

References usb_endpoint::address, attr, usb_endpoint::attributes, bus, usb_hub::bus, usbio_operations::close, DBGC, efi_handle_name(), ENOMEM, ENOTSUP, usbio_endpoint::ep, free, usbio_endpoint::handle, handle, usbio_interface::handle, usbio_device::handle, usb_port::hub, usbio_endpoint::interface, usbio_endpoint::io, usbio_interface::io, usbio_endpoint::list, list_add_tail, list_del, usbio_endpoint::op, usbio_operations::open, usb_device::port, rc, usb_endpoint::usb, usb_bus_get_hostdata(), USB_DIR_IN, USB_ENDPOINT_ATTR_BULK, USB_ENDPOINT_ATTR_CONTROL, USB_ENDPOINT_ATTR_INTERRUPT, USB_ENDPOINT_ATTR_TYPE_MASK, usb_endpoint_name(), usb_endpoint_set_hostdata(), usbio_endpoint::usbio, usbio_close(), usbio_control_operations, usbio_interrupt_operations, usbio_open(), and zalloc().

                                                           {
        struct usb_bus *bus = ep->usb->port->hub->bus;
        struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
        struct usbio_endpoint *endpoint;
        EFI_HANDLE handle = usbio->handle;
        unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
        int interface;
        int rc;

        /* Allocate and initialise structure */
        endpoint = zalloc ( sizeof ( *endpoint ) );
        if ( ! endpoint ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        usb_endpoint_set_hostdata ( ep, endpoint );
        endpoint->usbio = usbio;
        endpoint->ep = ep;

        /* Identify endpoint operations */
        if ( attr == USB_ENDPOINT_ATTR_CONTROL ) {
                endpoint->op = &usbio_control_operations;
        } else if ( attr == USB_ENDPOINT_ATTR_BULK ) {
                endpoint->op = ( ( ep->address & USB_DIR_IN ) ?
                                 &usbio_bulk_in_operations :
                                 &usbio_bulk_out_operations );
        } else if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
                endpoint->op = &usbio_interrupt_operations;
        } else {
                rc = -ENOTSUP;
                goto err_operations;
        }

        /* Identify interface for this endpoint */
        interface = usbio_interface ( usbio, ep );
        if ( interface < 0 ) {
                rc = interface;
                goto err_interface;
        }
        endpoint->interface = interface;

        /* Open interface */
        if ( ( rc = usbio_open ( usbio, interface ) ) != 0 )
                goto err_open_interface;
        endpoint->handle = usbio->interface[interface].handle;
        endpoint->io = usbio->interface[interface].io;
        DBGC ( usbio, "USBIO %s %s using ",
               efi_handle_name ( handle ), usb_endpoint_name ( ep ) );
        DBGC ( usbio, "%s\n", efi_handle_name ( endpoint->handle ) );

        /* Open endpoint */
        if ( ( rc = endpoint->op->open ( endpoint ) ) != 0 )
                goto err_open_endpoint;

        /* Add to list of endpoints */
        list_add_tail ( &endpoint->list, &usbio->endpoints );

        return 0;

        list_del ( &endpoint->list );
        endpoint->op->close ( endpoint );
 err_open_endpoint:
        usbio_close ( usbio, interface );
 err_open_interface:
 err_interface:
 err_operations:
        free ( endpoint );
 err_alloc:
        return rc;
}
static void usbio_endpoint_close ( struct usb_endpoint ep) [static]

Close endpoint.

Parameters:
epUSB endpoint

Definition at line 911 of file usbio.c.

References usbio_operations::close, usbio_endpoint::cons, ECANCELED, free, index, usbio_endpoint::interface, usbio_endpoint::iobuf, usbio_endpoint::list, list_del, usbio_endpoint::op, usbio_endpoint::prod, usb_complete_err(), usb_endpoint_get_hostdata(), usbio_endpoint::usbio, usbio_close(), and USBIO_RING_COUNT.

                                                             {
        struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
        struct usbio_device *usbio = endpoint->usbio;
        struct io_buffer *iobuf;
        unsigned int index;

        /* Remove from list of endpoints */
        list_del ( &endpoint->list );

        /* Close endpoint */
        endpoint->op->close ( endpoint );

        /* Close interface */
        usbio_close ( usbio, endpoint->interface );

        /* Cancel any incomplete transfers */
        while ( endpoint->cons != endpoint->prod ) {
                index = ( endpoint->cons++ % USBIO_RING_COUNT );
                iobuf = endpoint->iobuf[index];
                usb_complete_err ( ep, iobuf, -ECANCELED );
        }

        /* Free endpoint */
        free ( endpoint );
}
static int usbio_endpoint_reset ( struct usb_endpoint *ep  __unused) [static]

Reset endpoint.

Parameters:
epUSB endpoint
Return values:
rcReturn status code

Definition at line 943 of file usbio.c.

                                                                     {

        /* Nothing to do */
        return 0;
}
static int usbio_endpoint_mtu ( struct usb_endpoint *ep  __unused) [static]

Update MTU.

Parameters:
epUSB endpoint
Return values:
rcReturn status code

Definition at line 955 of file usbio.c.

                                                                   {

        /* Nothing to do */
        return 0;
}
static int usbio_endpoint_enqueue ( struct usb_endpoint ep,
struct io_buffer iobuf,
unsigned int  flags 
) [static]

Enqueue transfer.

Parameters:
epUSB endpoint
iobufI/O buffer
flagsTransfer flags
Return values:
rcReturn status code

Definition at line 969 of file usbio.c.

References usbio_endpoint::cons, ENOBUFS, fill, usbio_endpoint::flags, flags, index, usbio_endpoint::iobuf, usbio_endpoint::prod, usb_endpoint_get_hostdata(), and USBIO_RING_COUNT.

Referenced by usbio_endpoint_message(), and usbio_endpoint_stream().

                                                         {
        struct usbio_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
        unsigned int fill;
        unsigned int index;

        /* Fail if transfer ring is full */
        fill = ( endpoint->prod - endpoint->cons );
        if ( fill >= USBIO_RING_COUNT )
                return -ENOBUFS;

        /* Add to ring */
        index = ( endpoint->prod++ % USBIO_RING_COUNT );
        endpoint->iobuf[index] = iobuf;
        endpoint->flags[index] = flags;

        return 0;
}
static int usbio_endpoint_message ( struct usb_endpoint ep,
struct io_buffer iobuf 
) [static]

Enqueue message transfer.

Parameters:
epUSB endpoint
iobufI/O buffer
Return values:
rcReturn status code

Definition at line 996 of file usbio.c.

References usbio_endpoint_enqueue(), and USBIO_MESSAGE.

                                                              {

        /* Enqueue transfer */
        return usbio_endpoint_enqueue ( ep, iobuf, USBIO_MESSAGE );
}
static int usbio_endpoint_stream ( struct usb_endpoint ep,
struct io_buffer iobuf,
int  zlp 
) [static]

Enqueue stream transfer.

Parameters:
epUSB endpoint
iobufI/O buffer
zlpAppend a zero-length packet
Return values:
rcReturn status code

Definition at line 1011 of file usbio.c.

References usbio_endpoint_enqueue(), and USBIO_ZLEN.

                                                                      {

        /* Enqueue transfer */
        return usbio_endpoint_enqueue ( ep, iobuf, ( zlp ? USBIO_ZLEN : 0 ) );
}
static void usbio_endpoint_poll ( struct usbio_endpoint endpoint) [static]

Poll for completions.

Parameters:
endpointEndpoint

Definition at line 1023 of file usbio.c.

References usbio_endpoint::op, and usbio_operations::poll.

Referenced by usbio_bus_poll().

                                                                    {

        /* Poll endpoint */
        endpoint->op->poll ( endpoint );
}
static int usbio_device_open ( struct usb_device usb) [static]

Open device.

Parameters:
usbUSB device
Return values:
rcReturn status code

Definition at line 1042 of file usbio.c.

References usb_hub::bus, usb_port::hub, usb_device::port, usb_bus_get_hostdata(), and usb_set_hostdata().

                                                        {
        struct usbio_device *usbio =
                usb_bus_get_hostdata ( usb->port->hub->bus );

        usb_set_hostdata ( usb, usbio );
        return 0;
}
static void usbio_device_close ( struct usb_device *usb  __unused) [static]

Close device.

Parameters:
usbUSB device

Definition at line 1055 of file usbio.c.

                                                                   {

        /* Nothing to do */
}
static int usbio_device_address ( struct usb_device *usb  __unused) [static]

Assign device address.

Parameters:
usbUSB device
Return values:
rcReturn status code

Definition at line 1066 of file usbio.c.

                                                                    {

        /* Nothing to do */
        return 0;
}
static int usbio_hub_open ( struct usb_hub hub) [static]

Open hub.

Parameters:
hubUSB hub
Return values:
rcReturn status code

Definition at line 1085 of file usbio.c.

References ENOTSUP, and usb_hub::usb.

                                                  {

        /* Disallow non-root hubs */
        if ( hub->usb )
                return -ENOTSUP;

        /* Nothing to do */
        return 0;
}
static void usbio_hub_close ( struct usb_hub *hub  __unused) [static]

Close hub.

Parameters:
hubUSB hub

Definition at line 1100 of file usbio.c.

                                                             {

        /* Nothing to do */
}
static int usbio_root_open ( struct usb_hub *hub  __unused) [static]

Open root hub.

Parameters:
hubUSB hub
Return values:
rcReturn status code

Definition at line 1118 of file usbio.c.

                                                            {

        /* Nothing to do */
        return 0;
}
static void usbio_root_close ( struct usb_hub *hub  __unused) [static]

Close root hub.

Parameters:
hubUSB hub

Definition at line 1129 of file usbio.c.

                                                              {

        /* Nothing to do */
}
static int usbio_root_enable ( struct usb_hub *hub  __unused,
struct usb_port *port  __unused 
) [static]

Enable port.

Parameters:
hubUSB hub
portUSB port
Return values:
rcReturn status code

Definition at line 1141 of file usbio.c.

                                                                {

        /* Nothing to do */
        return 0;
}
static int usbio_root_disable ( struct usb_hub *hub  __unused,
struct usb_port *port  __unused 
) [static]

Disable port.

Parameters:
hubUSB hub
portUSB port
Return values:
rcReturn status code

Definition at line 1155 of file usbio.c.

                                                                 {

        /* Nothing to do */
        return 0;
}
static int usbio_root_speed ( struct usb_hub *hub  __unused,
struct usb_port port 
) [static]

Update root hub port speed.

Parameters:
hubUSB hub
portUSB port
Return values:
rcReturn status code

Definition at line 1169 of file usbio.c.

References usb_port::speed, and USB_SPEED_HIGH.

                                                      {

        /* Not actually exposed via EFI_USB_IO_PROTOCOL */
        port->speed = USB_SPEED_HIGH;
        return 0;
}
static int usbio_root_clear_tt ( struct usb_hub *hub  __unused,
struct usb_port *port  __unused,
struct usb_endpoint *ep  __unused 
) [static]

Clear transaction translator buffer.

Parameters:
hubUSB hub
portUSB port
epUSB endpoint
Return values:
rcReturn status code

Definition at line 1185 of file usbio.c.

References ENOTSUP.

                                                                    {

        /* Should never be called; this is a root hub */
        return -ENOTSUP;
}
static int usbio_bus_open ( struct usb_bus *bus  __unused) [static]

Open USB bus.

Parameters:
busUSB bus
Return values:
rcReturn status code

Definition at line 1206 of file usbio.c.

                                                           {

        /* Nothing to do */
        return 0;
}
static void usbio_bus_close ( struct usb_bus *bus  __unused) [static]

Close USB bus.

Parameters:
busUSB bus

Definition at line 1217 of file usbio.c.

                                                             {

        /* Nothing to do */
}
static void usbio_bus_poll ( struct usb_bus bus) [static]

Poll USB bus.

Parameters:
busUSB bus

Definition at line 1227 of file usbio.c.

References usbio_device::endpoints, usbio_endpoint::list, list_for_each_entry, usb_bus_get_hostdata(), and usbio_endpoint_poll().

                                                   {
        struct usbio_device *usbio = usb_bus_get_hostdata ( bus );
        struct usbio_endpoint *endpoint;

        /* Poll all endpoints.  We trust that completion handlers are
         * minimal and will not do anything that could plausibly
         * affect the endpoint list itself.
         */
        list_for_each_entry ( endpoint, &usbio->endpoints, list )
                usbio_endpoint_poll ( endpoint );
}
static int usbio_supported ( EFI_HANDLE  handle) [static]

Check to see if driver supports a device.

Parameters:
handleEFI device handle
Return values:
rcReturn status code

Definition at line 1286 of file usbio.c.

References EFI_SYSTEM_TABLE::BootServices, usb_class::class, usb_class_descriptor::class, usb_function_descriptor::class, EFI_BOOT_SERVICES::CloseProtocol, DBGC, DBGCP, device, driver, EEFI, efi_handle_name(), efi_image_handle, EFI_OPEN_PROTOCOL_GET_PROTOCOL, efi_systab, efi_usb_io_protocol_guid, ENOTSUP, id, USB_DEVICE_DESCRIPTOR::IdProduct, USB_DEVICE_DESCRIPTOR::IdVendor, USB_INTERFACE_DESCRIPTOR::InterfaceClass, USB_INTERFACE_DESCRIPTOR::InterfaceProtocol, USB_INTERFACE_DESCRIPTOR::InterfaceSubClass, memset(), EFI_BOOT_SERVICES::OpenProtocol, usb_function_descriptor::product, usb_class::protocol, rc, strerror(), usb_class::subclass, usb_find_driver(), and usb_function_descriptor::vendor.

                                                 {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_USB_DEVICE_DESCRIPTOR device;
        EFI_USB_INTERFACE_DESCRIPTOR interface;
        struct usb_function_descriptor desc;
        struct usb_driver *driver;
        struct usb_device_id *id;
        union {
                void *interface;
                EFI_USB_IO_PROTOCOL *io;
        } usb;
        EFI_STATUS efirc;
        int rc;

        /* Get protocol */
        if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
                                          &usb.interface, efi_image_handle,
                                          handle,
                                          EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
                rc = -EEFI ( efirc );
                DBGCP ( handle, "USB %s is not a USB device\n",
                        efi_handle_name ( handle ) );
                goto err_open_protocol;
        }

        /* Get device descriptor */
        if ( ( efirc = usb.io->UsbGetDeviceDescriptor ( usb.io,
                                                        &device ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( handle, "USB %s could not get device descriptor: "
                       "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
                goto err_get_device_descriptor;
        }
        memset ( &desc, 0, sizeof ( desc ) );
        desc.vendor = device.IdVendor;
        desc.product = device.IdProduct;

        /* Get interface descriptor */
        if ( ( efirc = usb.io->UsbGetInterfaceDescriptor ( usb.io,
                                                           &interface ) ) !=0){
                rc = -EEFI ( efirc );
                DBGC ( handle, "USB %s could not get interface descriptor: "
                       "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
                goto err_get_interface_descriptor;
        }
        desc.class.class.class = interface.InterfaceClass;
        desc.class.class.subclass = interface.InterfaceSubClass;
        desc.class.class.protocol = interface.InterfaceProtocol;

        /* Look for a driver for this interface */
        driver = usb_find_driver ( &desc, &id );
        if ( ! driver ) {
                rc = -ENOTSUP;
                goto err_unsupported;
        }

        /* Success */
        rc = 0;

 err_unsupported:
 err_get_interface_descriptor:
 err_get_device_descriptor:
        bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
                            efi_image_handle, handle );
 err_open_protocol:
        return rc;
}
static int usbio_config ( struct usbio_device usbio) [static]

Fetch configuration descriptor.

Parameters:
usbioUSB I/O device
Return values:
rcReturn status code

Definition at line 1360 of file usbio.c.

References usbio_device::config, usb_configuration_descriptor::config, USB_CONFIG_DESCRIPTOR::ConfigurationValue, count, cpu_to_le16, DBGC, device, EEFI, efi_handle_name(), EfiUsbDataIn, EINVAL, ENOENT, ENOMEM, free, handle, usbio_device::handle, usbio_device::io, le16_to_cpu, usb_configuration_descriptor::len, len, malloc(), msg(), USB_DEVICE_DESCRIPTOR::NumConfigurations, rc, status, strerror(), USB_CONFIG_DESCRIPTOR::TotalLength, USB_CONFIGURATION_DESCRIPTOR, USB_GET_DESCRIPTOR, _EFI_USB_IO_PROTOCOL::UsbControlTransfer, _EFI_USB_IO_PROTOCOL::UsbGetConfigDescriptor, _EFI_USB_IO_PROTOCOL::UsbGetDeviceDescriptor, and value.

Referenced by usbio_start().

                                                       {
        EFI_HANDLE handle = usbio->handle;
        EFI_USB_IO_PROTOCOL *io = usbio->io;
        EFI_USB_DEVICE_DESCRIPTOR device;
        EFI_USB_CONFIG_DESCRIPTOR partial;
        union {
                struct usb_setup_packet setup;
                EFI_USB_DEVICE_REQUEST efi;
        } msg;
        UINT32 status;
        size_t len;
        unsigned int count;
        unsigned int value;
        unsigned int i;
        EFI_STATUS efirc;
        int rc;

        /* Get device descriptor */
        if ( ( efirc = io->UsbGetDeviceDescriptor ( io, &device ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USB %s could not get device descriptor: "
                       "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
                goto err_get_device_descriptor;
        }
        count = device.NumConfigurations;

        /* Get current partial configuration descriptor */
        if ( ( efirc = io->UsbGetConfigDescriptor ( io, &partial ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USB %s could not get partial configuration "
                       "descriptor: %s\n", efi_handle_name ( handle ),
                       strerror ( rc ) );
                goto err_get_configuration_descriptor;
        }
        len = le16_to_cpu ( partial.TotalLength );

        /* Allocate configuration descriptor */
        usbio->config = malloc ( len );
        if ( ! usbio->config ) {
                rc = -ENOMEM;
                goto err_alloc;
        }

        /* There is, naturally, no way to retrieve the entire device
         * configuration descriptor via EFI_USB_IO_PROTOCOL.  Worse,
         * there is no way to even retrieve the index of the current
         * configuration descriptor.  We have to iterate over all
         * possible configuration descriptors looking for the
         * descriptor that matches the current configuration value.
         */
        for ( i = 0 ; i < count ; i++ ) {

                /* Construct request */
                msg.setup.request = cpu_to_le16 ( USB_GET_DESCRIPTOR );
                value = ( ( USB_CONFIGURATION_DESCRIPTOR << 8 ) | i );
                msg.setup.value = cpu_to_le16 ( value );
                msg.setup.index = 0;
                msg.setup.len = cpu_to_le16 ( len );

                /* Get full configuration descriptor */
                if ( ( efirc = io->UsbControlTransfer ( io, &msg.efi,
                                                        EfiUsbDataIn, 0,
                                                        usbio->config, len,
                                                        &status ) ) != 0 ) {
                        rc = -EEFI ( efirc );
                        DBGC ( usbio, "USB %s could not get configuration %d "
                               "descriptor: %s\n", efi_handle_name ( handle ),
                               i, strerror ( rc ) );
                        goto err_control_transfer;
                }

                /* Ignore unless this is the current configuration */
                if ( usbio->config->config != partial.ConfigurationValue )
                        continue;

                /* Check length */
                if ( le16_to_cpu ( usbio->config->len ) != len ) {
                        DBGC ( usbio, "USB %s configuration descriptor length "
                               "mismatch\n", efi_handle_name ( handle ) );
                        rc = -EINVAL;
                        goto err_len;
                }

                return 0;
        }

        /* No match found */
        DBGC ( usbio, "USB %s could not find current configuration "
               "descriptor\n", efi_handle_name ( handle ) );
        rc = -ENOENT;

 err_len:
 err_control_transfer:
        free ( usbio->config );
 err_alloc:
 err_get_configuration_descriptor:
 err_get_device_descriptor:
        return rc;
}
static int usbio_path ( struct usbio_device usbio) [static]

Construct device path for opening other interfaces.

Parameters:
usbioUSB I/O device
Return values:
rcReturn status code

Definition at line 1466 of file usbio.c.

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseProtocol, DBGC, EEFI, efi_device_path_protocol_guid, efi_devpath_len(), efi_devpath_text(), efi_handle_name(), efi_image_handle, EFI_OPEN_PROTOCOL_GET_PROTOCOL, efi_systab, EINVAL, end, ENOMEM, free, handle, usbio_device::handle, USB_DEVICE_PATH::Header, len, malloc(), memcpy(), MESSAGING_DEVICE_PATH, MSG_USB_DP, EFI_BOOT_SERVICES::OpenProtocol, usbio_device::path, rc, strerror(), EFI_DEVICE_PATH_PROTOCOL::SubType, EFI_DEVICE_PATH_PROTOCOL::Type, u, and usbio_device::usbpath.

Referenced by usbio_start().

                                                     {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE handle = usbio->handle;
        EFI_DEVICE_PATH_PROTOCOL *path;
        EFI_DEVICE_PATH_PROTOCOL *end;
        USB_DEVICE_PATH *usbpath;
        union {
                void *interface;
                EFI_DEVICE_PATH_PROTOCOL *path;
        } u;
        size_t len;
        EFI_STATUS efirc;
        int rc;

        /* Open device path protocol */
        if ( ( efirc = bs->OpenProtocol ( handle,
                                          &efi_device_path_protocol_guid,
                                          &u.interface, efi_image_handle,
                                          handle,
                                          EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s cannot open device path protocol: "
                       "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
                goto err_open_protocol;
        }
        path = u.interface;

        /* Locate end of device path and sanity check */
        len = efi_devpath_len ( path );
        if ( len < sizeof ( *usbpath ) ) {
                DBGC ( usbio, "USBIO %s underlength device path\n",
                       efi_handle_name ( handle ) );
                rc = -EINVAL;
                goto err_underlength;
        }
        usbpath = ( ( ( void * ) path ) + len - sizeof ( *usbpath ) );
        if ( ! ( ( usbpath->Header.Type == MESSAGING_DEVICE_PATH ) &&
                 ( usbpath->Header.SubType == MSG_USB_DP ) ) ) {
                DBGC ( usbio, "USBIO %s not a USB device path: ",
                       efi_handle_name ( handle ) );
                DBGC ( usbio, "%s\n", efi_devpath_text ( path ) );
                rc = -EINVAL;
                goto err_non_usb;
        }

        /* Allocate copy of device path */
        usbio->path = malloc ( len + sizeof ( *end ) );
        if ( ! usbio->path ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        memcpy ( usbio->path, path, ( len + sizeof ( *end ) ) );
        usbio->usbpath = ( ( ( void * ) usbio->path ) + len -
                           sizeof ( *usbpath ) );

        /* Close protocol */
        bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
                            efi_image_handle, handle );

        return 0;

        free ( usbio->path );
 err_alloc:
 err_non_usb:
 err_underlength:
        bs->CloseProtocol ( handle, &efi_device_path_protocol_guid,
                            efi_image_handle, handle );
 err_open_protocol:
        return rc;
}
static int usbio_interfaces ( struct usbio_device usbio) [static]

Construct interface list.

Parameters:
usbioUSB I/O device
Return values:
rcReturn status code

Definition at line 1543 of file usbio.c.

References assert, usbio_device::config, usbio_interface::count, count, DBGC, EEFI, efi_handle_name(), ENOMEM, first, usbio_device::first, free, handle, usbio_interface::handle, usbio_device::handle, usbio_device::interface, USB_INTERFACE_DESCRIPTOR::InterfaceNumber, usb_configuration_descriptor::interfaces, usbio_interface::io, usbio_device::io, rc, strerror(), _EFI_USB_IO_PROTOCOL::UsbGetInterfaceDescriptor, and zalloc().

Referenced by usbio_start().

                                                           {
        EFI_HANDLE handle = usbio->handle;
        EFI_USB_IO_PROTOCOL *io = usbio->io;
        EFI_USB_INTERFACE_DESCRIPTOR interface;
        unsigned int first;
        unsigned int count;
        EFI_STATUS efirc;
        int rc;

        /* Get interface descriptor */
        if ( ( efirc = io->UsbGetInterfaceDescriptor ( io, &interface ) ) != 0){
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USB %s could not get interface descriptor: "
                       "%s\n", efi_handle_name ( handle ), strerror ( rc ) );
                goto err_get_interface_descriptor;
        }

        /* Record first interface number */
        first = interface.InterfaceNumber;
        count = usbio->config->interfaces;
        assert ( first < count );
        usbio->first = first;

        /* Allocate interface list */
        usbio->interface = zalloc ( count * sizeof ( usbio->interface[0] ) );
        if ( ! usbio->interface ) {
                rc = -ENOMEM;
                goto err_alloc;
        }

        /* Use already-opened protocol for control transfers and for
         * the first interface.
         */
        usbio->interface[0].handle = handle;
        usbio->interface[0].io = io;
        usbio->interface[0].count = 1;
        usbio->interface[first].handle = handle;
        usbio->interface[first].io = io;
        usbio->interface[first].count = 1;

        return 0;

        free ( usbio->interface );
 err_alloc:
 err_get_interface_descriptor:
        return rc;
}
static int usbio_start ( struct efi_device efidev) [static]

Attach driver to device.

Parameters:
efidevEFI device
Return values:
rcReturn status code

Definition at line 1597 of file usbio.c.

References alloc_usb_bus(), EFI_SYSTEM_TABLE::BootServices, usbio_device::bus, device::children, EFI_BOOT_SERVICES::CloseProtocol, usbio_device::config, DBGC, DBGC_EFI_OPENERS, efi_device::dev, usbio_device::dev, efi_device::device, EEFI, efi_device_info(), efi_handle_name(), efi_image_handle, EFI_OPEN_PROTOCOL_BY_DRIVER, EFI_OPEN_PROTOCOL_EXCLUSIVE, efi_systab, efi_usb_io_protocol_guid, efidev_set_drvdata(), usbio_device::endpoints, ENOMEM, free, free_usb_bus(), handle, usbio_device::handle, usb_bus::hub, INIT_LIST_HEAD, usbio_device::interface, usbio_device::io, list_add, list_del, EFI_BOOT_SERVICES::OpenProtocol, device::parent, usbio_device::path, port, usb_port::protocol, rc, register_usb_bus(), device::siblings, strerror(), u, unregister_usb_bus(), usb_bus_set_hostdata(), usb_hub_set_drvdata(), usb_port(), USB_PROTO_2_0, usbio_config(), usbio_interfaces(), USBIO_MTU, usbio_path(), and zalloc().

                                                     {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_HANDLE handle = efidev->device;
        struct usbio_device *usbio;
        struct usb_port *port;
        union {
                void *interface;
                EFI_USB_IO_PROTOCOL *io;
        } u;
        EFI_STATUS efirc;
        int rc;

        /* Allocate and initialise structure */
        usbio = zalloc ( sizeof ( *usbio ) );
        if ( ! usbio ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        efidev_set_drvdata ( efidev, usbio );
        usbio->handle = handle;
        INIT_LIST_HEAD ( &usbio->endpoints );

        /* Open USB I/O protocol */
        if ( ( efirc = bs->OpenProtocol ( handle, &efi_usb_io_protocol_guid,
                                          &u.interface, efi_image_handle,
                                          handle,
                                          ( EFI_OPEN_PROTOCOL_BY_DRIVER |
                                            EFI_OPEN_PROTOCOL_EXCLUSIVE )))!=0){
                rc = -EEFI ( efirc );
                DBGC ( usbio, "USBIO %s cannot open USB I/O protocol: %s\n",
                       efi_handle_name ( handle ), strerror ( rc ) );
                DBGC_EFI_OPENERS ( usbio, handle, &efi_usb_io_protocol_guid );
                goto err_open_usbio;
        }
        usbio->io = u.io;

        /* Describe generic device */
        efi_device_info ( handle, "USB", &usbio->dev );
        usbio->dev.parent = &efidev->dev;
        list_add ( &usbio->dev.siblings, &efidev->dev.children );
        INIT_LIST_HEAD ( &usbio->dev.children );

        /* Fetch configuration descriptor */
        if ( ( rc = usbio_config ( usbio ) ) != 0 )
                goto err_config;

        /* Construct device path */
        if ( ( rc = usbio_path ( usbio ) ) != 0 )
                goto err_path;

        /* Construct interface list */
        if ( ( rc = usbio_interfaces ( usbio ) ) != 0 )
                goto err_interfaces;

        /* Allocate USB bus */
        usbio->bus = alloc_usb_bus ( &usbio->dev, 1 /* single "port" */,
                                     USBIO_MTU, &usbio_operations );
        if ( ! usbio->bus ) {
                rc = -ENOMEM;
                goto err_alloc_bus;
        }
        usb_bus_set_hostdata ( usbio->bus, usbio );
        usb_hub_set_drvdata ( usbio->bus->hub, usbio );

        /* Set port protocol */
        port = usb_port ( usbio->bus->hub, 1 );
        port->protocol = USB_PROTO_2_0;

        /* Register USB bus */
        if ( ( rc = register_usb_bus ( usbio->bus ) ) != 0 )
                goto err_register;

        return 0;

        unregister_usb_bus ( usbio->bus );
 err_register:
        free_usb_bus ( usbio->bus );
 err_alloc_bus:
        free ( usbio->interface );
 err_interfaces:
        free ( usbio->path );
 err_path:
        free ( usbio->config );
 err_config:
        list_del ( &usbio->dev.siblings );
        bs->CloseProtocol ( handle, &efi_usb_io_protocol_guid,
                            efi_image_handle, handle );
 err_open_usbio:
        free ( usbio );
 err_alloc:
        return rc;
}
static void usbio_stop ( struct efi_device efidev) [static]
struct efi_driver usbio_driver __efi_driver ( EFI_DRIVER_NORMAL  ) [read]

EFI USB I/O driver.


Variable Documentation

Initial value:

Control endpoint operations.

Definition at line 424 of file usbio.c.

Referenced by usbio_endpoint_open().

Initial value:

Bulk endpoint operations.

Definition at line 517 of file usbio.c.

Initial value:

Bulk endpoint operations.

Definition at line 618 of file usbio.c.

Initial value:

Interrupt endpoint operations.

Definition at line 816 of file usbio.c.

Referenced by usbio_endpoint_open().

USB I/O host controller driver operations.

Definition at line 1247 of file usbio.c.