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


Ignore:
Timestamp:
2017-12-14T23:01:57Z (6 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
837d53d
Parents:
bd05140
git-author:
Ondřej Hlavatý <aearsis@…> (2017-12-14 23:01:54)
git-committer:
Ondřej Hlavatý <aearsis@…> (2017-12-14 23:01:57)
Message:

usbhost bus: refactor the bus ops

This way, method names better represent the entity it is working with.
Their semantics was shifted a bit.

Regarding the tree of structures:

bus ← device ← endpoint ← batch

Previously, devices were kept in DDF function nodes, and endpoints had
pointer to the bus and device. Now, devices have pointer to bus,
endpoints don't.

Pointer to hcd_t in bus is WIP, and will be removed.

File:
1 edited

Legend:

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

    rbd05140 r6832245  
    5858};
    5959
    60 static int prepare_endpoint(xhci_endpoint_t *ep, const usb_endpoint_desc_t *desc)
    61 {
    62         /* Extract information from endpoint_desc */
    63         ep->base.endpoint = desc->endpoint_no;
    64         ep->base.direction = desc->direction;
    65         ep->base.transfer_type = desc->transfer_type;
    66         ep->base.max_packet_size = desc->max_packet_size;
    67         ep->base.packets = desc->packets;
    68         ep->max_streams = desc->usb3.max_streams;
    69         ep->max_burst = desc->usb3.max_burst;
    70         ep->mult = desc->usb3.mult;
    71 
    72         if (ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
    73                 if (ep->base.device->speed <= USB_SPEED_HIGH) {
    74                         ep->isoch_max_size = desc->max_packet_size * (desc->packets + 1);
    75                 }
    76                 else if (ep->base.device->speed == USB_SPEED_SUPER) {
    77                         ep->isoch_max_size = desc->usb3.bytes_per_interval;
    78                 }
    79                 /* Technically there could be superspeed plus too. */
    80 
    81                 /* Allocate and setup isochronous-specific structures. */
    82                 ep->isoch_enqueue = 0;
    83                 ep->isoch_dequeue = XHCI_ISOCH_BUFFER_COUNT - 1;
    84                 ep->isoch_started = false;
    85 
    86                 fibril_mutex_initialize(&ep->isoch_guard);
    87                 fibril_condvar_initialize(&ep->isoch_avail);
    88         }
    89 
    90         return xhci_endpoint_alloc_transfer_ds(ep);
    91 }
    92 
    93 static endpoint_t *create_endpoint(bus_t *base);
    94 
    95 static int address_device(xhci_hc_t *hc, xhci_device_t *dev)
     60static endpoint_t *endpoint_create(device_t *, const usb_endpoint_desc_t *);
     61
     62static int address_device(xhci_bus_t *bus, xhci_device_t *dev)
    9663{
    9764        int err;
    9865
    9966        /* Enable new slot. */
    100         if ((err = hc_enable_slot(hc, &dev->slot_id)) != EOK)
     67        if ((err = hc_enable_slot(bus->hc, &dev->slot_id)) != EOK)
    10168                return err;
    10269        usb_log_debug2("Obtained slot ID: %u.\n", dev->slot_id);
    10370
    10471        /* Create and configure control endpoint. */
    105         endpoint_t *ep0_base = create_endpoint(&hc->bus.base);
     72        endpoint_t *ep0_base = endpoint_create(&dev->base, &ep0_initial_desc);
    10673        if (!ep0_base)
    10774                goto err_slot;
     
    11279        xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
    11380
    114         if ((err = prepare_endpoint(ep0, &ep0_initial_desc)))
     81        if ((err = xhci_endpoint_alloc_transfer_ds(ep0)))
    11582                goto err_ep;
    11683
     
    12087
    12188        /* Address device */
    122         if ((err = hc_address_device(hc, dev, ep0)))
     89        if ((err = hc_address_device(bus->hc, dev, ep0)))
    12390                goto err_added;
    12491
     
    135102        endpoint_del_ref(ep0_base);
    136103err_slot:
    137         hc_disable_slot(hc, dev);
     104        hc_disable_slot(bus->hc, dev);
    138105        return err;
    139106}
     
    164131}
    165132
    166 int xhci_bus_enumerate_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
     133int xhci_bus_enumerate_device(xhci_bus_t *bus, device_t *dev)
    167134{
    168135        int err;
     
    184151
    185152        /* Assign an address to the device */
    186         if ((err = address_device(hc, xhci_dev))) {
     153        if ((err = address_device(bus, xhci_dev))) {
    187154                usb_log_error("Failed to setup address of the new device: %s", str_error(err));
    188155                return err;
     
    195162        fibril_mutex_unlock(&bus->base.guard);
    196163
    197         if ((err = setup_ep0_packet_size(hc, xhci_dev))) {
     164        if ((err = setup_ep0_packet_size(bus->hc, xhci_dev))) {
    198165                usb_log_error("Failed to setup control endpoint of the new device: %s", str_error(err));
    199166                goto err_address;
     
    201168
    202169        /* Read the device descriptor, derive the match ids */
    203         if ((err = hcd_ddf_device_explore(hc->hcd, dev))) {
     170        if ((err = hcd_ddf_device_explore(dev))) {
    204171                usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
    205172                goto err_address;
     
    213180}
    214181
    215 static int unregister_endpoint(bus_t *, endpoint_t *);
    216 
    217 int xhci_bus_remove_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
     182static int endpoint_unregister(endpoint_t *);
     183
     184int xhci_bus_remove_device(xhci_bus_t *bus, device_t *dev)
    218185{
    219186        int err;
     
    246213        /* Disable the slot, dropping all endpoints. */
    247214        const uint32_t slot_id = xhci_dev->slot_id;
    248         if ((err = hc_disable_slot(hc, xhci_dev))) {
     215        if ((err = hc_disable_slot(bus->hc, xhci_dev))) {
    249216                usb_log_warning("Failed to disable slot of device " XHCI_DEV_FMT ": %s",
    250217                    XHCI_DEV_ARGS(*xhci_dev), str_error(err));
     
    258225                        continue;
    259226
    260                 if ((err = unregister_endpoint(&bus->base, &xhci_dev->endpoints[i]->base))) {
     227                if ((err = endpoint_unregister(&xhci_dev->endpoints[i]->base))) {
    261228                        usb_log_warning("Failed to unregister endpoint " XHCI_EP_FMT ": %s",
    262229                            XHCI_EP_ARGS(*xhci_dev->endpoints[i]), str_error(err));
     
    278245}
    279246
    280 static int enumerate_device(bus_t *bus_base, hcd_t *hcd, device_t *dev)
    281 {
    282         xhci_hc_t *hc = hcd_get_driver_data(hcd);
    283         assert(hc);
    284 
    285         xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
    286         assert(bus);
    287 
    288         return xhci_bus_enumerate_device(bus, hc, dev);
    289 }
    290 
    291 static int remove_device(bus_t *bus_base, hcd_t *hcd, device_t *dev)
    292 {
    293         xhci_hc_t *hc = hcd_get_driver_data(hcd);
    294         assert(hc);
    295 
    296         xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
    297         assert(bus);
    298 
    299         return xhci_bus_remove_device(bus, hc, dev);
    300 }
    301 
    302 static int online_device(bus_t *bus_base, hcd_t *hcd, device_t *dev_base)
    303 {
    304         int err;
    305 
    306         xhci_hc_t *hc = hcd_get_driver_data(hcd);
    307         assert(hc);
    308 
    309         xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
     247static int device_enumerate(device_t *dev)
     248{
     249        xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
     250        return xhci_bus_enumerate_device(bus, dev);
     251}
     252
     253static int device_remove(device_t *dev)
     254{
     255        xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
     256        return xhci_bus_remove_device(bus, dev);
     257}
     258
     259static int device_online(device_t *dev_base)
     260{
     261        int err;
     262
     263        xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
    310264        assert(bus);
    311265
     
    314268
    315269        /* Transition the device from the Addressed to the Configured state. */
    316         if ((err = hc_configure_device(hc, dev->slot_id))) {
     270        if ((err = hc_configure_device(bus->hc, dev->slot_id))) {
    317271                usb_log_warning("Failed to configure device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*dev));
    318272        }
     
    331285}
    332286
    333 static int offline_device(bus_t *bus_base, hcd_t *hcd, device_t *dev_base)
    334 {
    335         int err;
    336 
    337         xhci_hc_t *hc = hcd_get_driver_data(hcd);
    338         assert(hc);
    339 
    340         xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
     287static int device_offline(device_t *dev_base)
     288{
     289        int err;
     290
     291        xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
    341292        assert(bus);
    342293
     
    370321
    371322        /* Issue one HC command to simultaneously drop all endpoints except zero. */
    372         if ((err = hc_deconfigure_device(hc, dev->slot_id))) {
     323        if ((err = hc_deconfigure_device(bus->hc, dev->slot_id))) {
    373324                usb_log_warning("Failed to deconfigure device " XHCI_DEV_FMT ".",
    374325                    XHCI_DEV_ARGS(*dev));
     
    388339}
    389340
    390 static endpoint_t *create_endpoint(bus_t *base)
    391 {
    392         xhci_bus_t *bus = bus_to_xhci_bus(base);
    393 
     341static endpoint_t *endpoint_create(device_t *dev, const usb_endpoint_desc_t *desc)
     342{
    394343        xhci_endpoint_t *ep = calloc(1, sizeof(xhci_endpoint_t));
    395344        if (!ep)
    396345                return NULL;
    397346
    398         if (xhci_endpoint_init(ep, bus)) {
     347        if (xhci_endpoint_init(ep, dev, desc)) {
    399348                free(ep);
    400349                return NULL;
     
    404353}
    405354
    406 static void destroy_endpoint(endpoint_t *ep)
     355static void endpoint_destroy(endpoint_t *ep)
    407356{
    408357        xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
     
    412361}
    413362
    414 static int register_endpoint(bus_t *bus_base, device_t *device, endpoint_t *ep_base, const usb_endpoint_desc_t *desc)
    415 {
    416         int err;
    417         xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
     363static int endpoint_register(endpoint_t *ep_base)
     364{
     365        int err;
     366        xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
    418367        xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
    419368
    420         xhci_device_t *dev = xhci_device_get(device);
    421 
    422         if ((err = prepare_endpoint(ep, desc)))
     369        xhci_device_t *dev = xhci_device_get(ep_base->device);
     370
     371        if ((err = xhci_endpoint_alloc_transfer_ds(ep)))
    423372                return err;
    424373
     
    443392}
    444393
    445 static int unregister_endpoint(bus_t *bus_base, endpoint_t *ep_base)
    446 {
    447         int err;
    448         xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
     394static int endpoint_unregister(endpoint_t *ep_base)
     395{
     396        int err;
     397        xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
    449398        xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
    450399        xhci_device_t *dev = xhci_device_get(ep_base->device);
     
    470419}
    471420
    472 static endpoint_t* find_endpoint(bus_t *bus_base, device_t *dev_base, usb_target_t target, usb_direction_t direction)
     421static endpoint_t* device_find_endpoint(device_t *dev_base, usb_target_t target, usb_direction_t direction)
    473422{
    474423        xhci_device_t *dev = xhci_device_get(dev_base);
     
    487436}
    488437
    489 static size_t count_bw(endpoint_t *ep, size_t size)
    490 {
    491         // TODO: Implement me!
    492         return 0;
    493 }
    494 
    495438/* Endpoint ops, optional (have generic fallback) */
    496439static bool endpoint_get_toggle(endpoint_t *ep)
     
    525468}
    526469
    527 static usb_transfer_batch_t *create_batch(bus_t *bus, endpoint_t *ep)
     470static usb_transfer_batch_t *batch_create(endpoint_t *ep)
    528471{
    529472        xhci_transfer_t *transfer = xhci_transfer_create(ep);
     
    531474}
    532475
    533 static void destroy_batch(usb_transfer_batch_t *batch)
     476static void batch_destroy(usb_transfer_batch_t *batch)
    534477{
    535478        xhci_transfer_destroy(xhci_transfer_from_batch(batch));
     
    538481static const bus_ops_t xhci_bus_ops = {
    539482#define BIND_OP(op) .op = op,
    540         BIND_OP(enumerate_device)
    541         BIND_OP(remove_device)
    542 
    543         BIND_OP(online_device)
    544         BIND_OP(offline_device)
    545 
    546         BIND_OP(create_endpoint)
    547         BIND_OP(destroy_endpoint)
    548 
    549         BIND_OP(register_endpoint)
    550         BIND_OP(unregister_endpoint)
    551         BIND_OP(find_endpoint)
    552 
    553483        BIND_OP(reserve_default_address)
    554484        BIND_OP(release_default_address)
    555 
    556485        BIND_OP(reset_toggle)
    557         BIND_OP(count_bw)
    558 
     486
     487        BIND_OP(device_enumerate)
     488        BIND_OP(device_remove)
     489        BIND_OP(device_online)
     490        BIND_OP(device_offline)
     491        BIND_OP(device_find_endpoint)
     492
     493        BIND_OP(endpoint_create)
     494        BIND_OP(endpoint_destroy)
     495        BIND_OP(endpoint_register)
     496        BIND_OP(endpoint_unregister)
    559497        BIND_OP(endpoint_get_toggle)
    560498        BIND_OP(endpoint_set_toggle)
    561499
    562         BIND_OP(create_batch)
    563         BIND_OP(destroy_batch)
     500        BIND_OP(batch_create)
     501        BIND_OP(batch_destroy)
    564502#undef BIND_OP
    565503};
     
    569507        assert(bus);
    570508
    571         bus_init(&bus->base, sizeof(xhci_device_t));
     509        bus_init(&bus->base, hc->hcd, sizeof(xhci_device_t));
    572510
    573511        bus->devices_by_slot = calloc(hc->max_slots, sizeof(xhci_device_t *));
     
    576514
    577515        bus->hc = hc;
    578         bus->base.ops = xhci_bus_ops;
     516        bus->base.ops = &xhci_bus_ops;
    579517        bus->default_address_speed = USB_SPEED_MAX;
    580518        return EOK;
Note: See TracChangeset for help on using the changeset viewer.