Changeset 0206d35 in mainline for uspace/drv/bus/usb/xhci/bus.c


Ignore:
Timestamp:
2017-10-25T00:03:57Z (8 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c3d926f3
Parents:
56db65d
Message:

Moving things around to improve isolation of responsibilities

Bus interface was simplified, xHCI implementation of address_device was spread into stack of rh → bus → hc and back.

File:
1 edited

Legend:

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

    r56db65d r0206d35  
    4949#include "transfers.h"
    5050
    51 /** TODO: Still some copy-pasta left...
    52  */
     51
     52/* FIXME Are these really static? Older HCs fetch it from descriptor. */
     53/* FIXME Add USB3 options, if applicable. */
     54static const usb_endpoint_desc_t ep0_desc = {
     55        .endpoint_no = 0,
     56        .direction = USB_DIRECTION_BOTH,
     57        .transfer_type = USB_TRANSFER_CONTROL,
     58        .max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
     59        .packets = 1,
     60};
     61
     62static int prepare_endpoint(xhci_endpoint_t *ep, const usb_endpoint_desc_t *desc)
     63{
     64        /* Extract information from endpoint_desc */
     65        ep->base.target = (usb_target_t) {{
     66                .address = ep->base.device->address,
     67                .endpoint = desc->endpoint_no,
     68        }};
     69        ep->base.direction = desc->direction;
     70        ep->base.transfer_type = desc->transfer_type;
     71        ep->base.max_packet_size = desc->max_packet_size;
     72        ep->base.packets = desc->packets;
     73        ep->max_streams = desc->usb3.max_streams;
     74        ep->max_burst = desc->usb3.max_burst;
     75        // TODO add this property to usb_endpoint_desc_t and fetch it from ss companion desc
     76        ep->mult = 0;
     77
     78        return xhci_endpoint_alloc_transfer_ds(ep);
     79}
     80
     81static endpoint_t *create_endpoint(bus_t *base);
     82
     83static int address_device(xhci_hc_t *hc, xhci_device_t *dev)
     84{
     85        int err;
     86
     87        /* Enable new slot. */
     88        if ((err = hc_enable_slot(hc, &dev->slot_id)) != EOK)
     89                return err;
     90        usb_log_debug2("Obtained slot ID: %u.\n", dev->slot_id);
     91
     92        /* Create and configure control endpoint. */
     93        endpoint_t *ep0_base = create_endpoint(&hc->bus.base);
     94        if (!ep0_base)
     95                goto err_slot;
     96
     97        /* Temporary reference */
     98        endpoint_add_ref(ep0_base);
     99
     100        ep0_base->device = &dev->base;
     101        xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
     102
     103        if ((err = prepare_endpoint(ep0, &ep0_desc)))
     104                goto err_ep;
     105
     106        /* Address device */
     107        if ((err = hc_address_device(hc, dev, ep0)))
     108                goto err_prepared_ep;
     109
     110        /* Register EP0, passing Temporary reference */
     111        ep0->base.target.address = dev->base.address;
     112        dev->endpoints[0] = ep0;
     113
     114        return EOK;
     115
     116err_prepared_ep:
     117        xhci_endpoint_free_transfer_ds(ep0);
     118err_ep:
     119        endpoint_del_ref(ep0_base);
     120err_slot:
     121        hc_disable_slot(hc, dev->slot_id);
     122        return err;
     123}
     124
    53125int xhci_bus_enumerate_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
    54126{
    55127        int err;
    56128        xhci_device_t *xhci_dev = xhci_device_get(dev);
    57 
    58         /* TODO: get speed from the default address reservation */
    59         dev->speed = USB_SPEED_FULL;
    60129
    61130        /* Manage TT */
     
    72141
    73142        /* Assign an address to the device */
    74         if ((err = xhci_rh_address_device(&hc->rh, dev, bus))) {
     143        if ((err = address_device(hc, xhci_dev))) {
    75144                usb_log_error("Failed to setup address of the new device: %s", str_error(err));
    76145                return err;
    77146        }
     147
     148        // TODO: Fetch descriptor of EP0 and reconfigure it accordingly
     149        assert(xhci_dev->endpoints[0]);
    78150
    79151        assert(bus->devices_by_slot[xhci_dev->slot_id] == NULL);
     
    93165}
    94166
     167static int unregister_endpoint(bus_t *, endpoint_t *);
     168
    95169int xhci_bus_remove_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
    96170{
     
    98172
    99173        /* Unregister remaining endpoints. */
    100         for (size_t i = 0; i < ARRAY_SIZE(xhci_dev->endpoints); ++i) {
     174        for (unsigned i = 0; i < ARRAY_SIZE(xhci_dev->endpoints); ++i) {
    101175                if (!xhci_dev->endpoints[i])
    102176                        continue;
    103177
    104                 // FIXME: ignoring return code
    105                 bus_unregister_endpoint(&bus->base, &xhci_dev->endpoints[i]->base);
     178                const int err = unregister_endpoint(&bus->base, &xhci_dev->endpoints[i]->base);
     179                if (err)
     180                        usb_log_warning("Failed to unregister EP (%u:%u): %s", dev->address, i, str_error(err));
    106181        }
    107182
     
    167242static int register_endpoint(bus_t *bus_base, endpoint_t *ep, const usb_endpoint_desc_t *desc)
    168243{
     244        int err;
    169245        xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
    170246        assert(bus);
    171247
    172248        assert(ep->device);
    173 
    174         /* Extract USB2-related information from endpoint_desc */
    175         ep->target = (usb_target_t) {{
    176                 .address = ep->device->address,
    177                 .endpoint = desc->endpoint_no,
    178         }};
    179         ep->direction = desc->direction;
    180         ep->transfer_type = desc->transfer_type;
    181         ep->max_packet_size = desc->max_packet_size;
    182         ep->packets = desc->packets;
    183249
    184250        xhci_device_t *xhci_dev = xhci_device_get(ep->device);
    185251        xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
    186252
    187         xhci_ep->max_streams = desc->usb3.max_streams;
    188         xhci_ep->max_burst = desc->usb3.max_burst;
    189         // TODO add this property to usb_endpoint_desc_t and fetch it from ss companion desc
    190         xhci_ep->mult = 0;
     253        if ((err = prepare_endpoint(xhci_ep, desc)))
     254                return err;
    191255
    192256        usb_log_info("Endpoint(%d:%d) registered to XHCI bus.", ep->target.address, ep->target.endpoint);
Note: See TracChangeset for help on using the changeset viewer.