Changeset b4b534ac in mainline for uspace/drv/bus/usb/usbhub/usbhub.c


Ignore:
Timestamp:
2016-07-22T08:24:47Z (8 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f76d2c2
Parents:
5b18137 (diff), 8351f9a4 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge from lp:~jan.vesely/helenos/usb

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/usbhub/usbhub.c

    r5b18137 rb4b534ac  
    4545#include <usb/dev/pipes.h>
    4646#include <usb/classes/classes.h>
    47 #include <usb/ddfiface.h>
    4847#include <usb/descriptor.h>
    4948#include <usb/dev/recognise.h>
     
    5756
    5857#define HUB_FNC_NAME "hub"
     58/** Hub status-change endpoint description.
     59 *
     60 * For more information see section 11.15.1 of USB 1.1 specification.
     61 */
     62const usb_endpoint_description_t hub_status_change_endpoint_description =
     63{
     64        .transfer_type = USB_TRANSFER_INTERRUPT,
     65        .direction = USB_DIRECTION_IN,
     66        .interface_class = USB_CLASS_HUB,
     67        .interface_subclass = 0,
     68        .interface_protocol = 0,
     69        .flags = 0
     70};
    5971
    6072/** Standard get hub global status request */
     
    99111        fibril_condvar_initialize(&hub_dev->pending_ops_cv);
    100112
    101 
    102         int opResult = usb_pipe_start_long_transfer(&usb_dev->ctrl_pipe);
    103         if (opResult != EOK) {
    104                 usb_log_error("Failed to start long ctrl pipe transfer: %s\n",
    105                     str_error(opResult));
    106                 return opResult;
    107         }
    108 
    109113        /* Set hub's first configuration. (There should be only one) */
    110         opResult = usb_set_first_configuration(usb_dev);
    111         if (opResult != EOK) {
    112                 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
     114        int opResult = usb_set_first_configuration(usb_dev);
     115        if (opResult != EOK) {
    113116                usb_log_error("Could not set hub configuration: %s\n",
    114117                    str_error(opResult));
     
    119122        opResult = usb_hub_process_hub_specific_info(hub_dev);
    120123        if (opResult != EOK) {
    121                 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
    122124                usb_log_error("Could process hub specific info, %s\n",
    123125                    str_error(opResult));
     
    127129        /* Create hub control function. */
    128130        usb_log_debug("Creating DDF function '" HUB_FNC_NAME "'.\n");
    129         hub_dev->hub_fun = ddf_fun_create(hub_dev->usb_device->ddf_dev,
     131        hub_dev->hub_fun = usb_device_ddf_fun_create(hub_dev->usb_device,
    130132            fun_exposed, HUB_FNC_NAME);
    131133        if (hub_dev->hub_fun == NULL) {
    132                 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
    133134                usb_log_error("Failed to create hub function.\n");
    134135                return ENOMEM;
     
    138139        opResult = ddf_fun_bind(hub_dev->hub_fun);
    139140        if (opResult != EOK) {
    140                 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
    141141                usb_log_error("Failed to bind hub function: %s.\n",
    142142                   str_error(opResult));
     
    146146
    147147        /* Start hub operation. */
    148         opResult = usb_device_auto_poll(hub_dev->usb_device, 0,
    149             hub_port_changes_callback, ((hub_dev->port_count + 1 + 8) / 8),
    150             usb_hub_polling_terminated_callback, hub_dev);
    151         if (opResult != EOK) {
    152                 usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
     148        opResult = usb_device_auto_poll_desc(hub_dev->usb_device,
     149            &hub_status_change_endpoint_description,
     150            hub_port_changes_callback, ((hub_dev->port_count + 1 + 7) / 8),
     151            -1, usb_hub_polling_terminated_callback, hub_dev);
     152        if (opResult != EOK) {
    153153                /* Function is already bound */
    154154                ddf_fun_unbind(hub_dev->hub_fun);
     
    159159        }
    160160        hub_dev->running = true;
    161         usb_log_info("Controlling hub '%s' (%zu ports).\n",
    162             ddf_dev_get_name(hub_dev->usb_device->ddf_dev), hub_dev->port_count);
    163 
    164         usb_pipe_end_long_transfer(&usb_dev->ctrl_pipe);
     161        usb_log_info("Controlling hub '%s' (%p: %zu ports).\n",
     162            usb_device_get_name(hub_dev->usb_device), hub_dev,
     163            hub_dev->port_count);
     164
    165165        return EOK;
    166166}
     
    185185{
    186186        assert(usb_dev);
    187         usb_hub_dev_t *hub = usb_dev->driver_data;
     187        usb_hub_dev_t *hub = usb_device_data_get(usb_dev);
    188188        assert(hub);
    189189        unsigned tries = 10;
     
    191191                async_usleep(100000);
    192192                if (!tries--) {
    193                         usb_log_error("Can't remove hub, still running.\n");
     193                        usb_log_error("(%p): Can't remove hub, still running.",
     194                            hub);
    194195                        return EBUSY;
    195196                }
     
    199200
    200201        for (size_t port = 0; port < hub->port_count; ++port) {
    201                 if (hub->ports[port].attached_device.fun) {
    202                         const int ret =
    203                             usb_hub_port_fini(&hub->ports[port], hub);
    204                         if (ret != EOK)
    205                                 return ret;
    206                 }
     202                const int ret = usb_hub_port_fini(&hub->ports[port], hub);
     203                if (ret != EOK)
     204                        return ret;
    207205        }
    208206        free(hub->ports);
     
    210208        const int ret = ddf_fun_unbind(hub->hub_fun);
    211209        if (ret != EOK) {
    212                 usb_log_error("Failed to unbind '%s' function: %s.\n",
    213                    HUB_FNC_NAME, str_error(ret));
     210                usb_log_error("(%p) Failed to unbind '%s' function: %s.",
     211                   hub, HUB_FNC_NAME, str_error(ret));
    214212                return ret;
    215213        }
    216214        ddf_fun_destroy(hub->hub_fun);
    217215
    218         usb_log_info("USB hub driver, stopped and cleaned.\n");
     216        usb_log_info("(%p) USB hub driver stopped and cleaned.", hub);
    219217        return EOK;
    220218}
     
    231229    uint8_t *change_bitmap, size_t change_bitmap_size, void *arg)
    232230{
    233         usb_log_debug("hub_port_changes_callback\n");
     231//      usb_log_debug("hub_port_changes_callback\n");
    234232        usb_hub_dev_t *hub = arg;
    235233        assert(hub);
     
    247245
    248246        /* N + 1 bit indicates change on port N */
    249         for (size_t port = 0; port < hub->port_count + 1; port++) {
     247        for (size_t port = 0; port < hub->port_count; ++port) {
    250248                const size_t bit = port + 1;
    251249                const bool change = (change_bitmap[bit / 8] >> (bit % 8)) & 1;
     
    272270
    273271        /* Get hub descriptor. */
    274         usb_log_debug("Retrieving descriptor\n");
    275         usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
     272        usb_log_debug("(%p): Retrieving descriptor.", hub_dev);
     273        usb_pipe_t *control_pipe =
     274            usb_device_get_default_pipe(hub_dev->usb_device);
    276275
    277276        usb_hub_descriptor_header_t descriptor;
     
    282281            sizeof(usb_hub_descriptor_header_t), &received_size);
    283282        if (opResult != EOK) {
    284                 usb_log_error("Failed to receive hub descriptor: %s.\n",
    285                     str_error(opResult));
     283                usb_log_error("(%p): Failed to receive hub descriptor: %s.\n",
     284                    hub_dev, str_error(opResult));
    286285                return opResult;
    287286        }
    288287
    289         usb_log_debug("Setting port count to %d.\n", descriptor.port_count);
     288        usb_log_debug("(%p): Setting port count to %d.\n", hub_dev,
     289            descriptor.port_count);
    290290        hub_dev->port_count = descriptor.port_count;
    291291
     
    306306
    307307        if (!hub_dev->power_switched) {
    308                 usb_log_info(
    309                    "Power switching not supported, ports always powered.\n");
     308                usb_log_info("(%p): Power switching not supported, "
     309                    "ports always powered.", hub_dev);
    310310                return EOK;
    311311        }
    312312
    313         usb_log_info("Hub port power switching enabled.\n");
    314 
    315         for (size_t port = 0; port < hub_dev->port_count; ++port) {
    316                 usb_log_debug("Powering port %zu.\n", port);
     313        usb_log_info("(%p): Hub port power switching enabled (%s).\n", hub_dev,
     314            hub_dev->per_port_power ? "per port" : "ganged");
     315
     316        for (unsigned port = 0; port < hub_dev->port_count; ++port) {
     317                usb_log_debug("(%p): Powering port %u.", hub_dev, port);
    317318                const int ret = usb_hub_port_set_feature(
    318319                    &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
    319320
    320321                if (ret != EOK) {
    321                         usb_log_error("Cannot power on port %zu: %s.\n",
    322                             hub_dev->ports[port].port_number, str_error(ret));
     322                        usb_log_error("(%p-%u): Cannot power on port: %s.\n",
     323                            hub_dev, hub_dev->ports[port].port_number,
     324                            str_error(ret));
    323325                } else {
    324326                        if (!hub_dev->per_port_power) {
    325                                 usb_log_debug("Ganged power switching, "
    326                                     "one port is enough.\n");
     327                                usb_log_debug("(%p) Ganged power switching, "
     328                                    "one port is enough.", hub_dev);
    327329                                break;
    328330                        }
     
    345347        /* Get number of possible configurations from device descriptor */
    346348        const size_t configuration_count =
    347             usb_device->descriptors.device.configuration_count;
     349            usb_device_descriptors(usb_device)->device.configuration_count;
    348350        usb_log_debug("Hub has %zu configurations.\n", configuration_count);
    349351
     
    353355        }
    354356
    355         if (usb_device->descriptors.configuration_size
    356             < sizeof(usb_standard_configuration_descriptor_t)) {
     357        const size_t config_size =
     358            usb_device_descriptors(usb_device)->full_config_size;
     359        const usb_standard_configuration_descriptor_t *config_descriptor =
     360            usb_device_descriptors(usb_device)->full_config;
     361
     362        if (config_size < sizeof(usb_standard_configuration_descriptor_t)) {
    357363            usb_log_error("Configuration descriptor is not big enough"
    358364                " to fit standard configuration descriptor.\n");
     
    360366        }
    361367
    362         // TODO: Make sure that the cast is correct
    363         usb_standard_configuration_descriptor_t *config_descriptor
    364             = (usb_standard_configuration_descriptor_t *)
    365             usb_device->descriptors.configuration;
    366 
    367368        /* Set configuration. Use the configuration that was in
    368369         * usb_device->descriptors.configuration i.e. The first one. */
    369370        const int opResult = usb_request_set_configuration(
    370             &usb_device->ctrl_pipe, config_descriptor->configuration_number);
     371            usb_device_get_default_pipe(usb_device),
     372            config_descriptor->configuration_number);
    371373        if (opResult != EOK) {
    372374                usb_log_error("Failed to set hub configuration: %s.\n",
     
    392394        if (status & USB_HUB_STATUS_OVER_CURRENT) {
    393395                /* Hub should remove power from all ports if it detects OC */
    394                 usb_log_warning("Detected hub over-current condition, "
    395                     "all ports should be powered off.");
     396                usb_log_warning("(%p) Detected hub over-current condition, "
     397                    "all ports should be powered off.", hub_dev);
    396398                return;
    397399        }
     
    406408                    &hub_dev->ports[port], USB_HUB_FEATURE_PORT_POWER);
    407409                if (ret != EOK) {
    408                         usb_log_warning("HUB OVER-CURRENT GONE: Cannot power on"
    409                             " port %zu: %s\n", hub_dev->ports[port].port_number,
    410                             str_error(ret));
     410                        usb_log_warning("(%p-%u): HUB OVER-CURRENT GONE: Cannot"
     411                            " power on port: %s\n", hub_dev,
     412                            hub_dev->ports[port].port_number, str_error(ret));
    411413                } else {
    412414                        if (!hub_dev->per_port_power)
     
    427429        assert(hub_dev);
    428430        assert(hub_dev->usb_device);
    429         usb_log_debug("Global interrupt on a hub\n");
    430         usb_pipe_t *control_pipe = &hub_dev->usb_device->ctrl_pipe;
     431        usb_log_debug("(%p): Global interrupt on th hub.",  hub_dev);
     432        usb_pipe_t *control_pipe =
     433            usb_device_get_default_pipe(hub_dev->usb_device);
    431434
    432435        usb_hub_status_t status;
     
    438441            &status, sizeof(usb_hub_status_t), &rcvd_size);
    439442        if (opResult != EOK) {
    440                 usb_log_error("Could not get hub status: %s\n",
     443                usb_log_error("(%p): Could not get hub status: %s.", hub_dev,
    441444                    str_error(opResult));
    442445                return;
    443446        }
    444447        if (rcvd_size != sizeof(usb_hub_status_t)) {
    445                 usb_log_error("Received status has incorrect size\n");
     448                usb_log_error("(%p): Received status has incorrect size: "
     449                    "%zu != %zu", hub_dev, rcvd_size, sizeof(usb_hub_status_t));
    446450                return;
    447451        }
     
    452456                /* Ack change in hub OC flag */
    453457                const int ret = usb_request_clear_feature(
    454                     &hub_dev->usb_device->ctrl_pipe, USB_REQUEST_TYPE_CLASS,
     458                    control_pipe, USB_REQUEST_TYPE_CLASS,
    455459                    USB_REQUEST_RECIPIENT_DEVICE,
    456460                    USB_HUB_FEATURE_C_HUB_OVER_CURRENT, 0);
    457461                if (ret != EOK) {
    458                         usb_log_error("Failed to clear hub over-current "
    459                             "change flag: %s.\n", str_error(opResult));
     462                        usb_log_error("(%p): Failed to clear hub over-current "
     463                            "change flag: %s.\n", hub_dev, str_error(opResult));
    460464                }
    461465        }
     
    480484                    USB_HUB_FEATURE_C_HUB_LOCAL_POWER, 0);
    481485                if (opResult != EOK) {
    482                         usb_log_error("Failed to clear hub power change "
    483                             "flag: %s.\n", str_error(ret));
     486                        usb_log_error("(%p): Failed to clear hub power change "
     487                            "flag: %s.\n", hub_dev, str_error(ret));
    484488                }
    485489        }
Note: See TracChangeset for help on using the changeset viewer.