Changeset 56257ba in mainline for uspace/drv


Ignore:
Timestamp:
2018-01-07T01:01:42Z (8 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
63431db2
Parents:
9efad54
git-author:
Ondřej Hlavatý <aearsis@…> (2018-01-07 01:01:41)
git-committer:
Ondřej Hlavatý <aearsis@…> (2018-01-07 01:01:42)
Message:

usbhost: manage endpoints by library + get/set_toggle → reset_toggle

That simplifies things A LOT. Now you can find endpoints for device in
an array inside device. This array is managed automatically in
register/unregister endpoint. HC drivers still needs to write to it when
setting up/tearing down the device.

Sorry for these two changes being in one commit, but splitting them
would be simply more work for no benefit.

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

Legend:

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

    r9efad54 r56257ba  
    4848 * @param[in] toggle new value of toggle bit
    4949 */
    50 static void ehci_ep_toggle_set(endpoint_t *ep, bool toggle)
     50static void ehci_ep_toggle_reset(endpoint_t *ep)
    5151{
    5252        ehci_endpoint_t *instance = ehci_endpoint_get(ep);
    53         assert(instance);
    54         assert(instance->qh);
    55         ep->toggle = toggle;
    5653        if (qh_toggle_from_td(instance->qh))
    57                 usb_log_warning("EP(%p): Setting toggle bit for transfer "
    58                     "directed EP", instance);
    59         qh_toggle_set(instance->qh, toggle);
     54                usb_log_warning("EP(%p): Resetting toggle bit for transfer directed EP", instance);
     55        qh_toggle_set(instance->qh, 0);
     56        ep->toggle = 0;
    6057}
    6158
    62 /** Callback to get value of toggle bit.
    63  *
    64  * @param[in] hcd_ep hcd endpoint structure
    65  * @return Current value of toggle bit.
    66  */
    67 static bool ehci_ep_toggle_get(endpoint_t *ep)
    68 {
    69         ehci_endpoint_t *instance = ehci_endpoint_get(ep);
    70         assert(instance);
    71         assert(instance->qh);
    72 
    73         if (qh_toggle_from_td(instance->qh))
    74                 usb_log_warning("EP(%p): Reading useless toggle bit", instance);
    75         return qh_toggle_get(instance->qh);
    76 }
    7759
    7860/** Creates new hcd endpoint representation.
     
    166148        .endpoint_register = ehci_register_ep,
    167149        .endpoint_unregister = ehci_unregister_ep,
    168         .endpoint_set_toggle = ehci_ep_toggle_set,
    169         .endpoint_get_toggle = ehci_ep_toggle_get,
     150        .endpoint_toggle_reset = ehci_ep_toggle_reset,
    170151        .endpoint_count_bw = bandwidth_count_usb11,
    171152        .batch_create = ehci_create_batch,
  • uspace/drv/bus/usb/ohci/ohci_bus.c

    r9efad54 r56257ba  
    4343#include "hc.h"
    4444
    45 /** Callback to set toggle on ED.
     45/** Callback to reset toggle on ED.
    4646 *
    4747 * @param[in] hcd_ep hcd endpoint structure
    4848 * @param[in] toggle new value of toggle bit
    4949 */
    50 static void ohci_ep_toggle_set(endpoint_t *ep, bool toggle)
     50static void ohci_ep_toggle_reset(endpoint_t *ep)
    5151{
    5252        ohci_endpoint_t *instance = ohci_endpoint_get(ep);
    5353        assert(instance);
    5454        assert(instance->ed);
    55         ep->toggle = toggle;
    56         ed_toggle_set(instance->ed, toggle);
    57 }
    58 
    59 /** Callback to get value of toggle bit.
    60  *
    61  * @param[in] hcd_ep hcd endpoint structure
    62  * @return Current value of toggle bit.
    63  */
    64 static bool ohci_ep_toggle_get(endpoint_t *ep)
    65 {
    66         ohci_endpoint_t *instance = ohci_endpoint_get(ep);
    67         assert(instance);
    68         assert(instance->ed);
    69         return ed_toggle_get(instance->ed);
     55        ep->toggle = 0;
     56        ed_toggle_set(instance->ed, 0);
    7057}
    7158
     
    166153        .endpoint_unregister = ohci_unregister_ep,
    167154        .endpoint_count_bw = bandwidth_count_usb11,
    168         .endpoint_set_toggle = ohci_ep_toggle_set,
    169         .endpoint_get_toggle = ohci_ep_toggle_get,
     155        .endpoint_toggle_reset = ohci_ep_toggle_reset,
    170156        .batch_create = ohci_create_batch,
    171157        .batch_destroy = ohci_destroy_batch,
  • uspace/drv/bus/usb/uhci/uhci_batch.c

    r9efad54 r56257ba  
    178178                        td_print_status(&uhci_batch->tds[i]);
    179179
    180                         endpoint_toggle_set(batch->ep,
    181                             td_toggle(&uhci_batch->tds[i]));
     180                        batch->ep->toggle = td_toggle(&uhci_batch->tds[i]);
    182181                        if (i > 0)
    183182                                goto substract_ret;
     
    195194
    196195        fibril_mutex_lock(&batch->ep->guard);
    197         usb_transfer_batch_reset_toggle(batch);
    198196        endpoint_deactivate_locked(batch->ep);
    199197        fibril_mutex_unlock(&batch->ep->guard);
     
    236234        const size_t mps = uhci_batch->base.ep->max_packet_size;
    237235
    238         int toggle = endpoint_toggle_get(uhci_batch->base.ep);
     236        int toggle = uhci_batch->base.ep->toggle;
    239237        assert(toggle == 0 || toggle == 1);
    240238
     
    260258        }
    261259        td_set_ioc(&uhci_batch->tds[td - 1]);
    262         endpoint_toggle_set(uhci_batch->base.ep, toggle);
     260        uhci_batch->base.ep->toggle = toggle;
    263261        usb_log_debug2(
    264262            "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
  • uspace/drv/bus/usb/xhci/bus.c

    r9efad54 r56257ba  
    7777                goto err_slot;
    7878
    79         /* Temporary reference */
     79        /* Bus reference */
    8080        endpoint_add_ref(ep0_base);
     81        dev->base.endpoints[0] = ep0_base;
    8182
    8283        xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
    8384
    8485        if ((err = xhci_endpoint_alloc_transfer_ds(ep0)))
    85                 goto err_ep;
    86 
    87         /* Register EP0 */
    88         if ((err = xhci_device_add_endpoint(dev, ep0)))
    89                 goto err_prepared;
     86                goto err_added;
    9087
    9188        /* Address device */
    9289        if ((err = hc_address_device(bus->hc, dev, ep0)))
    93                 goto err_added;
    94 
    95         /* Temporary reference */
    96         endpoint_del_ref(ep0_base);
    97         return EOK;
    98 
    99 err_added:
    100         xhci_device_remove_endpoint(ep0);
     90                goto err_prepared;
     91
     92        return EOK;
     93
    10194err_prepared:
    10295        xhci_endpoint_free_transfer_ds(ep0);
    103 err_ep:
    104         /* Temporary reference */
     96err_added:
     97        /* Bus reference */
    10598        endpoint_del_ref(ep0_base);
     99        dev->base.endpoints[0] = NULL;
    106100err_slot:
    107101        hc_disable_slot(bus->hc, dev);
     
    123117                return err;
    124118
    125         xhci_endpoint_t *ep0 = dev->endpoints[0];
     119        xhci_endpoint_t *ep0 = xhci_device_get_endpoint(dev, 0);
    126120        assert(ep0);
    127121
     
    218212        /* Abort running transfers. */
    219213        usb_log_debug2("Aborting all active transfers to device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*xhci_dev));
    220         for (size_t i = 0; i < ARRAY_SIZE(xhci_dev->endpoints); ++i) {
    221                 xhci_endpoint_t *ep = xhci_dev->endpoints[i];
     214        for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
     215                xhci_endpoint_t *ep = xhci_device_get_endpoint(xhci_dev, i);
    222216                if (!ep)
    223217                        continue;
     
    244238
    245239        /* Unregister remaining endpoints, freeing memory. */
    246         for (unsigned i = 0; i < ARRAY_SIZE(xhci_dev->endpoints); ++i) {
    247                 if (!xhci_dev->endpoints[i])
     240        for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
     241                if (!dev->endpoints[i])
    248242                        continue;
    249243
    250                 if ((err = endpoint_unregister(&xhci_dev->endpoints[i]->base))) {
     244                if ((err = endpoint_unregister(dev->endpoints[i]))) {
    251245                        usb_log_warning("Failed to unregister endpoint " XHCI_EP_FMT ": %s",
    252                             XHCI_EP_ARGS(*xhci_dev->endpoints[i]), str_error(err));
     246                            XHCI_EP_ARGS(*xhci_device_get_endpoint(xhci_dev, i)), str_error(err));
    253247                }
    254248        }
     
    332326
    333327        /* We will need the endpoint array later for DS deallocation. */
    334         xhci_endpoint_t *endpoints[ARRAY_SIZE(dev->endpoints)];
    335         memcpy(endpoints, dev->endpoints, sizeof(dev->endpoints));
    336 
    337         /* Remove all endpoints except zero. */
    338         for (unsigned i = 1; i < ARRAY_SIZE(endpoints); ++i) {
    339                 if (!endpoints[i])
    340                         continue;
    341 
     328        endpoint_t *endpoints[USB_ENDPOINT_MAX];
     329        memcpy(endpoints, dev->base.endpoints, sizeof(endpoints));
     330
     331        for (usb_endpoint_t i = 1; i < USB_ENDPOINT_MAX; ++i) {
    342332                /* FIXME: Asserting here that the endpoint is not active. If not, EBUSY? */
    343 
    344                 xhci_device_remove_endpoint(endpoints[i]);
     333                dev->base.endpoints[i] = NULL;
    345334        }
    346335
     
    356345                        continue;
    357346
    358                 xhci_endpoint_free_transfer_ds(endpoints[i]);
    359         }
    360 
    361         /* FIXME: What happens to unregistered endpoints now? Destroy them? */
     347                xhci_endpoint_free_transfer_ds(xhci_endpoint_get(endpoints[i]));
     348                /* Bus reference */
     349                endpoint_del_ref(endpoints[i]);
     350        }
    362351
    363352        return EOK;
     
    397386                return err;
    398387
    399         if ((err = xhci_device_add_endpoint(dev, ep)))
    400                 goto err_prepared;
    401 
    402388        usb_log_info("Endpoint " XHCI_EP_FMT " registered to XHCI bus.", XHCI_EP_ARGS(*ep));
    403389
     
    406392
    407393        if ((err = hc_add_endpoint(bus->hc, dev->slot_id, xhci_endpoint_index(ep), &ep_ctx)))
    408                 goto err_added;
    409 
    410         return EOK;
    411 
    412 err_added:
    413         xhci_device_remove_endpoint(ep);
     394                goto err_prepared;
     395
     396        return EOK;
     397
    414398err_prepared:
    415399        xhci_endpoint_free_transfer_ds(ep);
     
    425409
    426410        usb_log_info("Endpoint " XHCI_EP_FMT " unregistered from XHCI bus.", XHCI_EP_ARGS(*ep));
    427 
    428         xhci_device_remove_endpoint(ep);
    429411
    430412        /* If device slot is still available, drop the endpoint. */
     
    444426}
    445427
    446 static endpoint_t* device_find_endpoint(device_t *dev_base, usb_target_t target, usb_direction_t direction)
    447 {
    448         xhci_device_t *dev = xhci_device_get(dev_base);
    449 
    450         xhci_endpoint_t *ep = xhci_device_get_endpoint(dev, target.endpoint);
    451         if (!ep)
    452                 return NULL;
    453 
    454         return &ep->base;
    455 }
    456 
    457 static int reset_toggle(bus_t *bus_base, usb_target_t target, toggle_reset_mode_t mode)
    458 {
    459         // TODO: Implement me!
    460         return ENOTSUP;
    461 }
    462 
    463 /* Endpoint ops, optional (have generic fallback) */
    464 static bool endpoint_get_toggle(endpoint_t *ep)
    465 {
    466         // TODO: Implement me!
    467         return ENOTSUP;
    468 }
    469 
    470 static void endpoint_set_toggle(endpoint_t *ep, bool toggle)
    471 {
    472         // TODO: Implement me!
    473 }
    474 
    475428static int reserve_default_address(bus_t *bus_base, usb_speed_t speed)
    476429{
     
    510463        BIND_OP(reserve_default_address)
    511464        BIND_OP(release_default_address)
    512         BIND_OP(reset_toggle)
    513465
    514466        BIND_OP(device_enumerate)
     
    516468        BIND_OP(device_online)
    517469        BIND_OP(device_offline)
    518         BIND_OP(device_find_endpoint)
    519470
    520471        BIND_OP(endpoint_create)
     
    522473        BIND_OP(endpoint_register)
    523474        BIND_OP(endpoint_unregister)
    524         BIND_OP(endpoint_get_toggle)
    525         BIND_OP(endpoint_set_toggle)
    526475
    527476        BIND_OP(batch_create)
  • uspace/drv/bus/usb/xhci/endpoint.c

    r9efad54 r56257ba  
    469469}
    470470
    471 /** Add a new XHCI endpoint to a device. The device must be online unless
    472  * the added endpoint is number 0.
    473  * @param[in] dev XHCI device, to which to add the endpoint
    474  * @param[in] ep XHCI endpoint to add.
    475  *
    476  * @return Error code.
    477  */
    478 int xhci_device_add_endpoint(xhci_device_t *dev, xhci_endpoint_t *ep)
    479 {
    480         assert(dev);
    481         assert(ep);
    482 
    483         /* Offline devices don't create new endpoints other than EP0. */
    484         if (!dev->base.online && ep->base.endpoint > 0) {
    485                 return EAGAIN;
    486         }
    487 
    488         const usb_endpoint_t ep_num = ep->base.endpoint;
    489 
    490         if (dev->endpoints[ep_num])
    491                 return EEXIST;
    492 
    493         /* Device reference */
    494         endpoint_add_ref(&ep->base);
    495         ep->base.device = &dev->base;
    496         dev->endpoints[ep_num] = ep;
    497 
    498         return EOK;
    499 }
    500 
    501 /** Remove XHCI endpoint from a device.
    502  * @param[in] ep XHCI endpoint to remove.
    503  */
    504 void xhci_device_remove_endpoint(xhci_endpoint_t *ep)
    505 {
    506         assert(ep);
    507         xhci_device_t *dev = xhci_device_get(ep->base.device);
    508 
    509         assert(dev->endpoints[ep->base.endpoint]);
    510         dev->endpoints[ep->base.endpoint] = NULL;
    511         ep->base.device = NULL;
    512 }
    513 
    514471/** Retrieve XHCI endpoint from a device by the endpoint number.
    515472 * @param[in] dev XHCI device to query.
     
    520477xhci_endpoint_t *xhci_device_get_endpoint(xhci_device_t *dev, usb_endpoint_t ep)
    521478{
    522         return dev->endpoints[ep];
     479        return xhci_endpoint_get(dev->base.endpoints[ep]);
    523480}
    524481
  • uspace/drv/bus/usb/xhci/endpoint.h

    r9efad54 r56257ba  
    139139        dma_buffer_t dev_ctx;
    140140
    141         /** All endpoints of the device. Dropped ones are NULL */
    142         xhci_endpoint_t *endpoints[XHCI_EP_COUNT];
    143 
    144141        /** Flag indicating whether the device is USB3 (it's USB2 otherwise). */
    145142        bool usb3;
     
    161158void xhci_setup_endpoint_context(xhci_endpoint_t *, xhci_ep_ctx_t *);
    162159
    163 int xhci_device_add_endpoint(xhci_device_t *, xhci_endpoint_t *);
    164 void xhci_device_remove_endpoint(xhci_endpoint_t *);
    165160xhci_endpoint_t * xhci_device_get_endpoint(xhci_device_t *, usb_endpoint_t);
    166161
  • uspace/drv/bus/usb/xhci/transfers.c

    r9efad54 r56257ba  
    513513        }
    514514
    515         usb_transfer_batch_reset_toggle(batch);
    516515        endpoint_deactivate_locked(&ep->base);
    517516        fibril_mutex_unlock(&ep->base.guard);
Note: See TracChangeset for help on using the changeset viewer.