123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- /*
- * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * You can also choose to distribute this program under the terms of
- * the Unmodified Binary Distribution Licence (as given in the file
- * COPYING.UBDL), provided that you have satisfied its requirements.
- */
-
- FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
-
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <ipxe/usb.h>
- #include "usbhub.h"
-
- /** @file
- *
- * USB hub driver
- *
- */
-
- /**
- * Refill interrupt ring
- *
- * @v hubdev Hub device
- */
- static void hub_refill ( struct usb_hub_device *hubdev ) {
- int rc;
-
- /* Refill interrupt endpoint */
- if ( ( rc = usb_refill ( &hubdev->intr ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s could not refill interrupt: %s\n",
- hubdev->name, strerror ( rc ) );
- /* Continue attempting to refill */
- return;
- }
-
- /* Stop refill process */
- process_del ( &hubdev->refill );
- }
-
- /** Refill process descriptor */
- static struct process_descriptor hub_refill_desc =
- PROC_DESC ( struct usb_hub_device, refill, hub_refill );
-
- /**
- * Complete interrupt transfer
- *
- * @v ep USB endpoint
- * @v iobuf I/O buffer
- * @v rc Completion status code
- */
- static void hub_complete ( struct usb_endpoint *ep,
- struct io_buffer *iobuf, int rc ) {
- struct usb_hub_device *hubdev =
- container_of ( ep, struct usb_hub_device, intr );
- struct usb_hub *hub = hubdev->hub;
- uint8_t *data = iobuf->data;
- unsigned int bits = ( 8 * iob_len ( iobuf ) );
- unsigned int i;
-
- /* Ignore packets cancelled when the endpoint closes */
- if ( ! ep->open )
- goto done;
-
- /* Ignore packets with errors */
- if ( rc != 0 ) {
- DBGC ( hubdev, "HUB %s interrupt failed: %s\n",
- hubdev->name, strerror ( rc ) );
- DBGC_HDA ( hubdev, 0, iobuf->data, iob_len ( iobuf ) );
- goto done;
- }
-
- /* Report any port status changes */
- for ( i = 1 ; i <= hub->ports ; i++ ) {
-
- /* Sanity check */
- if ( i > bits ) {
- DBGC ( hubdev, "HUB %s underlength interrupt:\n",
- hubdev->name );
- DBGC_HDA ( hubdev, 0, iobuf->data, iob_len ( iobuf ) );
- goto done;
- }
-
- /* Report port status change if applicable */
- if ( data[ i / 8 ] & ( 1 << ( i % 8 ) ) ) {
- DBGC2 ( hubdev, "HUB %s port %d status changed\n",
- hubdev->name, i );
- usb_port_changed ( usb_port ( hub, i ) );
- }
- }
-
- done:
- /* Start refill process */
- process_add ( &hubdev->refill );
- }
-
- /** Interrupt endpoint operations */
- static struct usb_endpoint_driver_operations usb_hub_intr_operations = {
- .complete = hub_complete,
- };
-
- /**
- * Open hub
- *
- * @v hub USB hub
- * @ret rc Return status code
- */
- static int hub_open ( struct usb_hub *hub ) {
- struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
- struct usb_device *usb = hubdev->usb;
- unsigned int i;
- int rc;
-
- /* Ensure ports are powered */
- for ( i = 1 ; i <= hub->ports ; i++ ) {
- if ( ( rc = usb_hub_set_port_feature ( usb, i,
- USB_HUB_PORT_POWER,
- 0 ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s port %d could not apply power: "
- "%s\n", hubdev->name, i, strerror ( rc ) );
- goto err_power;
- }
- }
-
- /* Open interrupt endpoint */
- if ( ( rc = usb_endpoint_open ( &hubdev->intr ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s could not register interrupt: %s\n",
- hubdev->name, strerror ( rc ) );
- goto err_open;
- }
-
- /* Start refill process */
- process_add ( &hubdev->refill );
-
- /* Refill interrupt ring */
- hub_refill ( hubdev );
-
- /* Delay to allow ports to stabilise on out-of-spec hubs */
- if ( hubdev->flags & USB_HUB_SLOW_START )
- mdelay ( USB_HUB_SLOW_START_DELAY_MS );
-
- return 0;
-
- usb_endpoint_close ( &hubdev->intr );
- err_open:
- err_power:
- return rc;
- }
-
- /**
- * Close hub
- *
- * @v hub USB hub
- */
- static void hub_close ( struct usb_hub *hub ) {
- struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
-
- /* Close interrupt endpoint */
- usb_endpoint_close ( &hubdev->intr );
-
- /* Stop refill process */
- process_del ( &hubdev->refill );
- }
-
- /**
- * Enable port
- *
- * @v hub USB hub
- * @v port USB port
- * @ret rc Return status code
- */
- static int hub_enable ( struct usb_hub *hub, struct usb_port *port ) {
- struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
- struct usb_device *usb = hubdev->usb;
- struct usb_hub_port_status status;
- unsigned int current;
- unsigned int i;
- int rc;
-
- /* Initiate reset if applicable */
- if ( ( hub->protocol < USB_PROTO_3_0 ) &&
- ( ( rc = usb_hub_set_port_feature ( usb, port->address,
- USB_HUB_PORT_RESET, 0 ) )!=0)){
- DBGC ( hubdev, "HUB %s port %d could not initiate reset: %s\n",
- hubdev->name, port->address, strerror ( rc ) );
- return rc;
- }
-
- /* Wait for port to become enabled */
- for ( i = 0 ; i < USB_HUB_ENABLE_MAX_WAIT_MS ; i++ ) {
-
- /* Check for port being enabled */
- if ( ( rc = usb_hub_get_port_status ( usb, port->address,
- &status ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s port %d could not get status: "
- "%s\n", hubdev->name, port->address,
- strerror ( rc ) );
- return rc;
- }
- current = le16_to_cpu ( status.current );
- if ( current & ( 1 << USB_HUB_PORT_ENABLE ) )
- return 0;
-
- /* Delay */
- mdelay ( 1 );
- }
-
- DBGC ( hubdev, "HUB %s port %d timed out waiting for enable\n",
- hubdev->name, port->address );
- return -ETIMEDOUT;
- }
-
- /**
- * Disable port
- *
- * @v hub USB hub
- * @v port USB port
- * @ret rc Return status code
- */
- static int hub_disable ( struct usb_hub *hub, struct usb_port *port ) {
- struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
- struct usb_device *usb = hubdev->usb;
- int rc;
-
- /* Disable port */
- if ( ( rc = usb_hub_clear_port_feature ( usb, port->address,
- USB_HUB_PORT_ENABLE, 0 ) )!=0){
- DBGC ( hubdev, "HUB %s port %d could not disable: %s\n",
- hubdev->name, port->address, strerror ( rc ) );
- return rc;
- }
-
- return 0;
- }
-
- /**
- * Clear port status change bits
- *
- * @v hubdev USB hub device
- * @v port Port number
- * @v changed Port status change bits
- * @ret rc Return status code
- */
- static int hub_clear_changes ( struct usb_hub_device *hubdev,
- unsigned int port, uint16_t changed ) {
- struct usb_device *usb = hubdev->usb;
- unsigned int bit;
- unsigned int feature;
- int rc;
-
- /* Clear each set bit */
- for ( bit = 0 ; bit < 16 ; bit++ ) {
-
- /* Skip unset bits */
- if ( ! ( changed & ( 1 << bit ) ) )
- continue;
-
- /* Skip unused features */
- feature = USB_HUB_C_FEATURE ( bit );
- if ( ! ( hubdev->features & ( 1 << feature ) ) )
- continue;
-
- /* Clear bit */
- if ( ( rc = usb_hub_clear_port_feature ( usb, port,
- feature, 0 ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s port %d could not clear feature "
- "%d: %s\n", hubdev->name, port, feature,
- strerror ( rc ) );
- return rc;
- }
- }
-
- return 0;
- }
-
- /**
- * Update port speed
- *
- * @v hub USB hub
- * @v port USB port
- * @ret rc Return status code
- */
- static int hub_speed ( struct usb_hub *hub, struct usb_port *port ) {
- struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
- struct usb_device *usb = hubdev->usb;
- struct usb_hub_port_status status;
- unsigned int current;
- unsigned int changed;
- int rc;
-
- /* Get port status */
- if ( ( rc = usb_hub_get_port_status ( usb, port->address,
- &status ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s port %d could not get status: %s\n",
- hubdev->name, port->address, strerror ( rc ) );
- return rc;
- }
- current = le16_to_cpu ( status.current );
- changed = le16_to_cpu ( status.changed );
- DBGC2 ( hubdev, "HUB %s port %d status is %04x:%04x\n",
- hubdev->name, port->address, changed, current );
-
- /* Update port speed */
- if ( current & ( 1 << USB_HUB_PORT_CONNECTION ) ) {
- if ( hub->protocol >= USB_PROTO_3_0 ) {
- port->speed = USB_SPEED_SUPER;
- } else if ( current & ( 1 << USB_HUB_PORT_LOW_SPEED ) ) {
- port->speed = USB_SPEED_LOW;
- } else if ( current & ( 1 << USB_HUB_PORT_HIGH_SPEED ) ) {
- port->speed = USB_SPEED_HIGH;
- } else {
- port->speed = USB_SPEED_FULL;
- }
- } else {
- port->speed = USB_SPEED_NONE;
- }
-
- /* Record disconnections */
- port->disconnected |= ( changed & ( 1 << USB_HUB_PORT_CONNECTION ) );
-
- /* Clear port status change bits */
- if ( ( rc = hub_clear_changes ( hubdev, port->address, changed ) ) != 0)
- return rc;
-
- return 0;
- }
-
- /**
- * Clear transaction translator buffer
- *
- * @v hub USB hub
- * @v port USB port
- * @v ep USB endpoint
- * @ret rc Return status code
- */
- static int hub_clear_tt ( struct usb_hub *hub, struct usb_port *port,
- struct usb_endpoint *ep ) {
- struct usb_hub_device *hubdev = usb_hub_get_drvdata ( hub );
- struct usb_device *usb = hubdev->usb;
- int rc;
-
- /* Clear transaction translator buffer. All hubs must support
- * single-TT operation; we simplify our code by supporting
- * only this configuration.
- */
- if ( ( rc = usb_hub_clear_tt_buffer ( usb, ep->usb->address,
- ep->address, ep->attributes,
- USB_HUB_TT_SINGLE ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s port %d could not clear TT buffer: %s\n",
- hubdev->name, port->address, strerror ( rc ) );
- return rc;
- }
-
- return 0;
- }
-
- /** USB hub operations */
- static struct usb_hub_driver_operations hub_operations = {
- .open = hub_open,
- .close = hub_close,
- .enable = hub_enable,
- .disable = hub_disable,
- .speed = hub_speed,
- .clear_tt = hub_clear_tt,
- };
-
- /**
- * Probe USB hub
- *
- * @v func USB function
- * @v config Configuration descriptor
- * @ret rc Return status code
- */
- static int hub_probe ( struct usb_function *func,
- struct usb_configuration_descriptor *config ) {
- struct usb_device *usb = func->usb;
- struct usb_bus *bus = usb->port->hub->bus;
- struct usb_hub_device *hubdev;
- struct usb_interface_descriptor *interface;
- union usb_hub_descriptor desc;
- unsigned int depth;
- unsigned int ports;
- int enhanced;
- int rc;
-
- /* Allocate and initialise structure */
- hubdev = zalloc ( sizeof ( *hubdev ) );
- if ( ! hubdev ) {
- rc = -ENOMEM;
- goto err_alloc;
- }
- enhanced = ( usb->port->protocol >= USB_PROTO_3_0 );
- hubdev->name = func->name;
- hubdev->usb = usb;
- hubdev->features =
- ( enhanced ? USB_HUB_FEATURES_ENHANCED : USB_HUB_FEATURES );
- hubdev->flags = func->id->driver_data;
- usb_endpoint_init ( &hubdev->intr, usb, &usb_hub_intr_operations );
- usb_refill_init ( &hubdev->intr, 0, 0, USB_HUB_INTR_FILL );
- process_init_stopped ( &hubdev->refill, &hub_refill_desc, NULL );
-
- /* Locate hub interface descriptor */
- interface = usb_interface_descriptor ( config, func->interface[0], 0 );
- if ( ! interface ) {
- DBGC ( hubdev, "HUB %s has no interface descriptor\n",
- hubdev->name );
- rc = -EINVAL;
- goto err_interface;
- }
-
- /* Locate interrupt endpoint descriptor */
- if ( ( rc = usb_endpoint_described ( &hubdev->intr, config, interface,
- USB_INTERRUPT_IN, 0 ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s could not describe interrupt endpoint: "
- "%s\n", hubdev->name, strerror ( rc ) );
- goto err_endpoint;
- }
-
- /* Set hub depth */
- depth = usb_depth ( usb );
- if ( enhanced ) {
- if ( ( rc = usb_hub_set_hub_depth ( usb, depth ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s could not set hub depth to %d: "
- "%s\n", hubdev->name, depth, strerror ( rc ) );
- goto err_set_hub_depth;
- }
- }
-
- /* Get hub descriptor */
- if ( ( rc = usb_hub_get_descriptor ( usb, enhanced, &desc ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s could not get hub descriptor: %s\n",
- hubdev->name, strerror ( rc ) );
- goto err_hub_descriptor;
- }
- ports = desc.basic.ports;
- DBGC ( hubdev, "HUB %s has %d ports at depth %d%s\n", hubdev->name,
- ports, depth, ( enhanced ? " (enhanced)" : "" ) );
-
- /* Allocate hub */
- hubdev->hub = alloc_usb_hub ( bus, usb, ports, &hub_operations );
- if ( ! hubdev->hub ) {
- rc = -ENOMEM;
- goto err_alloc_hub;
- }
- usb_hub_set_drvdata ( hubdev->hub, hubdev );
-
- /* Register hub */
- if ( ( rc = register_usb_hub ( hubdev->hub ) ) != 0 ) {
- DBGC ( hubdev, "HUB %s could not register: %s\n",
- hubdev->name, strerror ( rc ) );
- goto err_register_hub;
- }
-
- usb_func_set_drvdata ( func, hubdev );
- return 0;
-
- unregister_usb_hub ( hubdev->hub );
- err_register_hub:
- free_usb_hub ( hubdev->hub );
- err_alloc_hub:
- err_hub_descriptor:
- err_set_hub_depth:
- err_endpoint:
- err_interface:
- free ( hubdev );
- err_alloc:
- return rc;
- }
-
- /**
- * Remove USB hub
- *
- * @v func USB function
- * @ret rc Return status code
- */
- static void hub_remove ( struct usb_function *func ) {
- struct usb_hub_device *hubdev = usb_func_get_drvdata ( func );
- struct usb_hub *hub = hubdev->hub;
- struct usb_device *usb = hubdev->usb;
- struct usb_port *port;
- unsigned int i;
-
- /* If hub has been unplugged, mark all ports as unplugged */
- if ( usb->port->disconnected ) {
- for ( i = 1 ; i <= hub->ports ; i++ ) {
- port = usb_port ( hub, i );
- port->disconnected = 1;
- port->speed = USB_SPEED_NONE;
- }
- }
-
- /* Unregister hub */
- unregister_usb_hub ( hubdev->hub );
- assert ( ! process_running ( &hubdev->refill ) );
-
- /* Free hub */
- free_usb_hub ( hubdev->hub );
-
- /* Free hub device */
- free ( hubdev );
- }
-
- /** USB hub device IDs */
- static struct usb_device_id hub_ids[] = {
- {
- .name = "avocent-hub",
- .vendor = 0x0624,
- .product = 0x0248,
- .driver_data = USB_HUB_SLOW_START,
- },
- {
- .name = "hub",
- .vendor = USB_ANY_ID,
- .product = USB_ANY_ID,
- },
- };
-
- /** USB hub driver */
- struct usb_driver usb_hub_driver __usb_driver = {
- .ids = hub_ids,
- .id_count = ( sizeof ( hub_ids ) / sizeof ( hub_ids[0] ) ),
- .class = USB_CLASS_ID ( USB_CLASS_HUB, 0, USB_ANY_ID ),
- .score = USB_SCORE_NORMAL,
- .probe = hub_probe,
- .remove = hub_remove,
- };
|