Changeset 0892663a in mainline for uspace/drv


Ignore:
Timestamp:
2018-01-11T04:14:37Z (8 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9848c77
Parents:
bad4a05
git-author:
Ondřej Hlavatý <aearsis@…> (2018-01-11 03:59:03)
git-committer:
Ondřej Hlavatý <aearsis@…> (2018-01-11 04:14:37)
Message:

usbhost: device removal and off/onlining moved into the library

Also, it is just not possible to make generic transfer abortion. So the
current semantics of endpoint unregistering is also aborting the pending
transfer. As it is not yet implemented in XHCI, and the stub in UHCI is
missing the magic, it breaks offlining interrupt devices, such as mouse.

When finishing this commit, I came across the fact we need some more
synchronization above device. Guard can protect internal structures, but
it cannot synchronize multiple calls to offline, or offline & removal
between each other - they both need to allow driver to unregister
endpoints, and as such must release the guard.

Location:
uspace/drv/bus/usb
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/ehci/ehci_bus.c

    rbad4a05 r0892663a  
    135135}
    136136
    137 static int ehci_device_online(device_t *device)
    138 {
    139         int err;
    140 
    141         /* Allow creation of new endpoints and transfers. */
    142         usb_log_info("Device(%d): Going online.", device->address);
    143         fibril_mutex_lock(&device->guard);
    144         device->online = true;
    145         fibril_mutex_unlock(&device->guard);
    146 
    147         if ((err = ddf_fun_online(device->fun))) {
    148                 return err;
    149         }
    150 
    151         return EOK;
    152 }
    153 
    154 static int ehci_device_offline(device_t *device)
    155 {
    156         int err;
    157 
    158         /* Tear down all drivers working with the device. */
    159         if ((err = ddf_fun_offline(device->fun))) {
    160                 return err;
    161         }
    162 
    163         /* At this point, all drivers are assumed to have already terminated
    164          * in a consistent way. The following code just cleans up hanging
    165          * transfers if there are any. */
    166 
    167         /* Block creation of new endpoints and transfers. */
    168         usb_log_info("Device(%d): Going offline.", device->address);
    169         fibril_mutex_lock(&device->guard);
    170         device->online = false;
    171         fibril_mutex_unlock(&device->guard);
    172 
    173         /* FIXME: Abort all transfers to all endpoints. */
    174 
    175         return EOK;
    176 }
    177 
    178137static const bus_ops_t ehci_bus_ops = {
    179138        .parent = &usb2_bus_ops,
     
    190149        .batch_destroy = ehci_destroy_batch,
    191150        .batch_schedule = ehci_hc_schedule,
    192         .device_online = ehci_device_online,
    193         .device_offline = ehci_device_offline,
    194151};
    195152
  • uspace/drv/bus/usb/uhci/hc.c

    rbad4a05 r0892663a  
    321321}
    322322
    323 static int device_online(device_t *device)
    324 {
    325         int err;
    326         hc_t *instance = bus_to_hc(device->bus);
    327         assert(instance);
    328 
    329         /* Allow creation of new endpoints and transfers. */
    330         usb_log_info("Device(%d): Going online.", device->address);
    331         fibril_mutex_lock(&device->guard);
    332         device->online = true;
    333         fibril_mutex_unlock(&device->guard);
    334 
    335         if ((err = ddf_fun_online(device->fun))) {
    336                 return err;
    337         }
    338 
    339         return EOK;
    340 }
    341 
    342 static int device_offline(device_t *device)
    343 {
    344         int err;
    345         hc_t *instance = bus_to_hc(device->bus);
    346         assert(instance);
    347 
    348         /* Tear down all drivers working with the device. */
    349         if ((err = ddf_fun_offline(device->fun))) {
    350                 return err;
    351         }
    352 
    353         /* At this point, all drivers are assumed to have already terminated
    354          * in a consistent way. The following code just cleans up hanging
    355          * transfers if there are any. */
    356 
    357         /* Block creation of new endpoints and transfers. */
    358         usb_log_info("Device(%d): Going offline.", device->address);
    359         fibril_mutex_lock(&device->guard);
    360         device->online = false;
    361         fibril_mutex_unlock(&device->guard);
    362 
    363         /* Abort all transfers to all endpoints. */
    364         transfer_list_abort_device(&instance->transfers_interrupt, device->address);
    365         transfer_list_abort_device(&instance->transfers_control_slow, device->address);
    366         transfer_list_abort_device(&instance->transfers_control_full, device->address);
    367         transfer_list_abort_device(&instance->transfers_bulk_full, device->address);
    368 
    369         return EOK;
     323static void endpoint_unregister(endpoint_t *ep)
     324{
     325        usb2_bus_ops.endpoint_unregister(ep);
     326
     327        fibril_mutex_lock(&ep->guard);
     328        if (ep->active_batch) {
     329                uhci_transfer_batch_t *ub = uhci_transfer_batch_get(ep->active_batch);
     330                uhci_transfer_batch_abort(ub);
     331
     332                assert(ep->active_batch == NULL);
     333        }
     334        fibril_mutex_unlock(&ep->guard);
    370335}
    371336
     
    379344        .status = hc_status,
    380345
     346        .endpoint_unregister = endpoint_unregister,
    381347        .endpoint_count_bw = bandwidth_count_usb11,
     348
    382349        .batch_create = create_transfer_batch,
    383350        .batch_schedule = hc_schedule,
    384351        .batch_destroy = destroy_transfer_batch,
    385 
    386         .device_online = device_online,
    387         .device_offline = device_offline,
    388352};
    389353
     
    522486static int hc_schedule(usb_transfer_batch_t *batch)
    523487{
     488        assert(batch);
    524489        hc_t *instance = bus_to_hc(endpoint_get_bus(batch->ep));
    525         assert(batch);
    526490
    527491        if (batch->target.address == uhci_rh_get_address(&instance->rh))
  • uspace/drv/bus/usb/uhci/transfer_list.c

    rbad4a05 r0892663a  
    194194        while (!list_empty(&instance->batch_list)) {
    195195                link_t * const current = list_first(&instance->batch_list);
    196                 uhci_transfer_batch_t *batch =
    197                     uhci_transfer_batch_from_link(current);
     196                uhci_transfer_batch_t *batch = uhci_transfer_batch_from_link(current);
    198197                transfer_list_remove_batch(instance, batch);
    199                 endpoint_abort(batch->base.ep);
    200         }
    201         fibril_mutex_unlock(&instance->guard);
    202 }
    203 
    204 /** Walk the list and finish all batches of a specified device with EINTR.
    205  *
    206  * @param[in] instance List to use.
    207  * @param[in] address Address of the specified device. Other addresses are skipped.
    208  */
    209 void transfer_list_abort_device(transfer_list_t *instance, usb_address_t address)
    210 {
    211         fibril_mutex_lock(&instance->guard);
    212         link_t *current = list_first(&instance->batch_list);
    213         while (current && current != &instance->batch_list.head && !list_empty(&instance->batch_list)) {
    214                 link_t * const next = current->next;
    215                 uhci_transfer_batch_t *batch =
    216                     uhci_transfer_batch_from_link(current);
    217 
    218                 if (batch->base.target.address == address) {
    219                         transfer_list_remove_batch(instance, batch);
    220                         endpoint_abort(batch->base.ep);
    221                 }
    222 
    223                 current = next;
    224198        }
    225199        fibril_mutex_unlock(&instance->guard);
  • uspace/drv/bus/usb/uhci/transfer_list.h

    rbad4a05 r0892663a  
    6363void transfer_list_remove_finished(transfer_list_t *instance, list_t *done);
    6464void transfer_list_abort_all(transfer_list_t *instance);
    65 void transfer_list_abort_device(transfer_list_t *instance, usb_address_t address);
    6665
    6766#endif
  • uspace/drv/bus/usb/uhci/uhci_batch.c

    rbad4a05 r0892663a  
    6464}
    6565
     66/**
     67 * Abort a transfer that is currently running.
     68 * Call with endpoint guard locked.
     69 */
     70void uhci_transfer_batch_abort(uhci_transfer_batch_t *batch)
     71{
     72        assert(batch);
     73
     74        endpoint_t *ep = batch->base.ep;
     75        assert(ep);
     76        assert(fibril_mutex_is_locked(&ep->guard));
     77        assert(ep->active_batch == &batch->base);
     78
     79        /*
     80         * TODO: Do some magic here to remove the batch from schedule.
     81         */
     82
     83        /*
     84         * Wait for 2 frames. If the transfer was being processed,
     85         * it shall be marked as finished already after 1ms.
     86         */
     87        endpoint_wait_timeout_locked(ep, 2000);
     88        if (ep->active_batch != &batch->base)
     89                return;
     90
     91        /*
     92         * Now, we can be sure the transfer is not scheduled,
     93         * and as such will not be completed. We now own the batch.
     94         */
     95        endpoint_deactivate_locked(ep);
     96
     97        /* Leave the critical section for finishing the batch. */
     98        fibril_mutex_unlock(&ep->guard);
     99
     100        batch->base.error = EINTR;
     101        batch->base.transfered_size = 0;
     102        usb_transfer_batch_finish(&batch->base);
     103
     104        fibril_mutex_lock(&ep->guard);
     105}
     106
    66107/** Allocate memory and initialize internal data structure.
    67108 *
  • uspace/drv/bus/usb/uhci/uhci_batch.h

    rbad4a05 r0892663a  
    7070int uhci_transfer_batch_prepare(uhci_transfer_batch_t *);
    7171bool uhci_transfer_batch_check_completed(uhci_transfer_batch_t *);
    72 void uhci_transfer_batch_finish(uhci_transfer_batch_t *);
     72void uhci_transfer_batch_abort(uhci_transfer_batch_t *);
    7373void uhci_transfer_batch_destroy(uhci_transfer_batch_t *);
    7474
  • uspace/drv/bus/usb/xhci/bus.c

    rbad4a05 r0892663a  
    209209        xhci_device_t *xhci_dev = xhci_device_get(dev);
    210210
    211         /* Block creation of new endpoints and transfers. */
    212         usb_log_debug2("Device " XHCI_DEV_FMT " going offline.", XHCI_DEV_ARGS(*xhci_dev));
    213         fibril_mutex_lock(&dev->guard);
    214         dev->online = false;
    215         fibril_mutex_unlock(&dev->guard);
    216 
    217         /* Abort running transfers. */
    218         usb_log_debug2("Aborting all active transfers to device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*xhci_dev));
    219         for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
    220                 xhci_endpoint_t *ep = xhci_device_get_endpoint(xhci_dev, i);
    221                 if (!ep)
    222                         continue;
    223 
    224                 endpoint_abort(&ep->base);
    225         }
    226 
    227         /* TODO: Figure out how to handle errors here. So far, they are reported and skipped. */
    228 
    229         /* Make DDF (and all drivers) forget about the device. */
    230         if ((err = ddf_fun_unbind(dev->fun))) {
    231                 usb_log_warning("Failed to unbind DDF function of device " XHCI_DEV_FMT ": %s",
    232                     XHCI_DEV_ARGS(*xhci_dev), str_error(err));
    233         }
    234 
    235211        /* Disable the slot, dropping all endpoints. */
    236212        const uint32_t slot_id = xhci_dev->slot_id;
     
    241217
    242218        bus->devices_by_slot[slot_id] = NULL;
    243 
    244         /* Unregister remaining endpoints, freeing memory. */
    245         for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
    246                 if (!dev->endpoints[i])
    247                         continue;
    248 
    249                 bus_endpoint_remove(dev->endpoints[i]);
    250         }
    251 
    252         /* Destroy DDF device. */
    253         /* XXX: Not a good idea, this method should not destroy devices. */
    254         hcd_ddf_fun_destroy(dev);
    255219}
    256220
     
    273237        if ((err = hc_configure_device(bus->hc, dev->slot_id))) {
    274238                usb_log_warning("Failed to configure device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*dev));
    275         }
    276 
    277         /* Allow creation of new endpoints and transfers. */
    278         usb_log_debug2("Device " XHCI_DEV_FMT " going online.", XHCI_DEV_ARGS(*dev));
    279         fibril_mutex_lock(&dev_base->guard);
    280         dev_base->online = true;
    281         fibril_mutex_unlock(&dev_base->guard);
    282 
    283         if ((err = ddf_fun_online(dev_base->fun))) {
    284239                return err;
    285240        }
     
    294249 * Bus callback.
    295250 */
    296 static int device_offline(device_t *dev_base)
     251static void device_offline(device_t *dev_base)
    297252{
    298253        int err;
     
    303258        xhci_device_t *dev = xhci_device_get(dev_base);
    304259        assert(dev);
    305 
    306         /* Tear down all drivers working with the device. */
    307         if ((err = ddf_fun_offline(dev_base->fun))) {
    308                 return err;
    309         }
    310 
    311         /* Block creation of new endpoints and transfers. */
    312         usb_log_debug2("Device " XHCI_DEV_FMT " going offline.", XHCI_DEV_ARGS(*dev));
    313         fibril_mutex_lock(&dev_base->guard);
    314         dev_base->online = false;
    315         fibril_mutex_unlock(&dev_base->guard);
    316 
    317         /* We will need the endpoint array later for DS deallocation. */
    318         endpoint_t *endpoints[USB_ENDPOINT_MAX];
    319         memcpy(endpoints, dev->base.endpoints, sizeof(endpoints));
    320 
    321         for (usb_endpoint_t i = 1; i < USB_ENDPOINT_MAX; ++i) {
    322                 /* FIXME: Asserting here that the endpoint is not active. If not, EBUSY? */
    323                 dev->base.endpoints[i] = NULL;
    324         }
    325260
    326261        /* Issue one HC command to simultaneously drop all endpoints except zero. */
     
    329264                    XHCI_DEV_ARGS(*dev));
    330265        }
    331 
    332         /* Tear down TRB ring / PSA. */
    333         for (unsigned i = 1; i < ARRAY_SIZE(endpoints); ++i) {
    334                 if (!endpoints[i])
    335                         continue;
    336 
    337                 /* Bus reference */
    338                 endpoint_del_ref(endpoints[i]);
    339         }
    340 
    341         return EOK;
    342266}
    343267
     
    378302
    379303/**
    380  * Register an andpoint to the bus. Allocate its transfer ring(s) and inform
    381  * xHC about it.
     304 * Register an andpoint to the xHC.
    382305 *
    383306 * Bus callback.
    384307 */
    385308static int endpoint_register(endpoint_t *ep_base)
    386 {
    387         int err;
    388         xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
    389         xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
    390 
    391         xhci_device_t *dev = xhci_device_get(ep_base->device);
    392 
    393         usb_log_info("Endpoint " XHCI_EP_FMT " registered to XHCI bus.", XHCI_EP_ARGS(*ep));
    394 
    395         xhci_ep_ctx_t ep_ctx;
    396         xhci_setup_endpoint_context(ep, &ep_ctx);
    397 
    398         if ((err = hc_add_endpoint(bus->hc, dev->slot_id, xhci_endpoint_index(ep), &ep_ctx)))
    399                 return err;
    400 
    401         return EOK;
    402 }
    403 
    404 /**
    405  * Unregister an endpoint. If the device is still available, inform the xHC
    406  * about it. Destroy resources allocated when registering.
    407  *
    408  * Bus callback.
    409  */
    410 static void endpoint_unregister(endpoint_t *ep_base)
    411309{
    412310        int err;
     
    415313        xhci_device_t *dev = xhci_device_get(ep_base->device);
    416314
    417         usb_log_info("Endpoint " XHCI_EP_FMT " unregistered from XHCI bus.", XHCI_EP_ARGS(*ep));
     315        xhci_ep_ctx_t ep_ctx;
     316        xhci_setup_endpoint_context(ep, &ep_ctx);
     317
     318        if ((err = hc_add_endpoint(bus->hc, dev->slot_id, xhci_endpoint_index(ep), &ep_ctx)))
     319                return err;
     320
     321        return EOK;
     322}
     323
     324/**
     325 * Unregister an endpoint. If the device is still available, inform the xHC
     326 * about it.
     327 *
     328 * Bus callback.
     329 */
     330static void endpoint_unregister(endpoint_t *ep_base)
     331{
     332        int err;
     333        xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
     334        xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
     335        xhci_device_t *dev = xhci_device_get(ep_base->device);
    418336
    419337        /* If device slot is still available, drop the endpoint. */
Note: See TracChangeset for help on using the changeset viewer.