Changeset 0206d35 in mainline for uspace/drv/bus/usb/xhci/hc.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/hc.c

    r56db65d r0206d35  
    4343#include "rh.h"
    4444#include "hw_struct/trb.h"
     45#include "hw_struct/context.h"
     46#include "endpoint.h"
    4547#include "commands.h"
    4648#include "transfers.h"
     
    449451{
    450452        assert(batch);
     453        assert(batch->ep);
    451454
    452455        usb_log_debug2("Endpoint(%d:%d) started %s transfer of size %lu.",
     
    632635}
    633636
    634 int hc_address_device(xhci_hc_t *hc, uint32_t slot_id, xhci_input_ctx_t *ictx)
    635 {
    636         assert(hc);
    637 
    638         int err;
    639         xhci_cmd_t cmd;
    640         xhci_cmd_init(&cmd);
    641 
    642         cmd.slot_id = slot_id;
    643 
    644         if ((err = xhci_send_address_device_command(hc, &cmd, ictx)) != EOK)
    645                 return err;
    646 
    647         if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT)) != EOK)
    648                 return err;
    649 
    650         xhci_cmd_fini(&cmd);
    651         return EOK;
    652 }
    653 
    654637static int create_valid_input_ctx(xhci_input_ctx_t **out_ictx)
    655638{
     
    674657}
    675658
    676 int hc_address_rh_device(xhci_hc_t *hc, uint32_t slot_id, uint8_t port, xhci_ep_ctx_t *ep_ctx)
     659// TODO: This currently assumes the device is attached to rh directly
     660//      -> calculate route string
     661int hc_address_device(xhci_hc_t *hc, xhci_device_t *dev, xhci_endpoint_t *ep0)
     662{
     663        int err = ENOMEM;
     664
     665        /* Setup and register device context */
     666        dev->dev_ctx = malloc32(sizeof(xhci_device_ctx_t));
     667        if (!dev->dev_ctx)
     668                goto err;
     669        memset(dev->dev_ctx, 0, sizeof(xhci_device_ctx_t));
     670
     671        hc->dcbaa[dev->slot_id] = addr_to_phys(dev->dev_ctx);
     672
     673        /* Issue configure endpoint command (sec 4.3.5). */
     674        xhci_input_ctx_t *ictx;
     675        if ((err = create_valid_input_ctx(&ictx))) {
     676                goto err_dev_ctx;
     677        }
     678
     679        /* Initialize slot_ctx according to section 4.3.3 point 3. */
     680        XHCI_SLOT_ROOT_HUB_PORT_SET(ictx->slot_ctx, dev->base.port); // FIXME: This should be port at RH
     681        XHCI_SLOT_CTX_ENTRIES_SET(ictx->slot_ctx, 1);
     682
     683        /* Attaching to root hub port, root string equals to 0. */
     684        XHCI_SLOT_ROUTE_STRING_SET(ictx->slot_ctx, 0); // FIXME: This is apparently valid in limited cases
     685
     686        /* Copy endpoint 0 context and set A1 flag. */
     687        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
     688        xhci_setup_endpoint_context(ep0, &ictx->endpoint_ctx[0]);
     689
     690        xhci_cmd_t cmd;
     691        xhci_cmd_init(&cmd);
     692
     693        cmd.slot_id = dev->slot_id;
     694
     695        if ((err = xhci_send_address_device_command(hc, &cmd, ictx)) != EOK)
     696                goto err_cmd;
     697
     698        if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT)) != EOK)
     699                goto err_cmd;
     700
     701        dev->base.address = XHCI_SLOT_DEVICE_ADDRESS(dev->dev_ctx->slot_ctx);
     702        usb_log_debug2("Obtained USB address: %d.\n", dev->base.address);
     703
     704        /* From now on, the device is officially online, yay! */
     705        fibril_mutex_lock(&dev->base.guard);
     706        dev->online = true;
     707        fibril_mutex_unlock(&dev->base.guard);
     708
     709        xhci_cmd_fini(&cmd);
     710        free32(ictx);
     711        return EOK;
     712
     713err_cmd:
     714        xhci_cmd_fini(&cmd);
     715        free32(ictx);
     716err_dev_ctx:
     717        free32(dev->dev_ctx);
     718        hc->dcbaa[dev->slot_id] = 0;
     719err:
     720        return err;
     721}
     722
     723int hc_configure_device(xhci_hc_t *hc, uint32_t slot_id)
    677724{
    678725        int err;
     
    684731        }
    685732
    686         /* Initialize slot_ctx according to section 4.3.3 point 3. */
    687         XHCI_SLOT_ROOT_HUB_PORT_SET(ictx->slot_ctx, port);
    688         XHCI_SLOT_CTX_ENTRIES_SET(ictx->slot_ctx, 1);
    689 
    690         /* Attaching to root hub port, root string equals to 0. */
    691         XHCI_SLOT_ROUTE_STRING_SET(ictx->slot_ctx, 0);
    692 
    693         /* Copy endpoint 0 context and set A1 flag. */
    694         memcpy(&ictx->endpoint_ctx[0], ep_ctx, sizeof(xhci_ep_ctx_t));
    695         XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
    696 
    697         if ((err = hc_address_device(hc, slot_id, ictx))) {
     733        // TODO: Set slot context and other flags. (probably forgot a lot of 'em)
     734
     735        xhci_cmd_t cmd;
     736        xhci_cmd_init(&cmd);
     737
     738        cmd.slot_id = slot_id;
     739
     740        if ((err = xhci_send_configure_endpoint_command(hc, &cmd, ictx))) {
    698741                goto err_cmd;
    699742        }
     743
     744        if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT))) {
     745                goto err_cmd;
     746        }
     747
     748        xhci_cmd_fini(&cmd);
    700749
    701750        free32(ictx);
     
    708757}
    709758
    710 int hc_configure_device(xhci_hc_t *hc, uint32_t slot_id)
     759int hc_deconfigure_device(xhci_hc_t *hc, uint32_t slot_id)
     760{
     761        int err;
     762
     763        /* Issue configure endpoint command (sec 4.3.5) with the DC flag. */
     764        xhci_cmd_t cmd;
     765        xhci_cmd_init(&cmd);
     766
     767        cmd.slot_id = slot_id;
     768        cmd.deconfigure = true;
     769
     770        if ((err = xhci_send_configure_endpoint_command(hc, &cmd, NULL))) {
     771                return err;
     772        }
     773
     774        if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT))) {
     775                return err;
     776        }
     777
     778        xhci_cmd_fini(&cmd);
     779
     780        return EOK;
     781}
     782
     783int hc_add_endpoint(xhci_hc_t *hc, uint32_t slot_id, uint8_t ep_idx, xhci_ep_ctx_t *ep_ctx)
    711784{
    712785        int err;
     
    718791        }
    719792
     793        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
     794        memcpy(&ictx->endpoint_ctx[ep_idx], ep_ctx, sizeof(xhci_ep_ctx_t));
     795
    720796        // TODO: Set slot context and other flags. (probably forgot a lot of 'em)
    721797
     
    744820}
    745821
    746 int hc_deconfigure_device(xhci_hc_t *hc, uint32_t slot_id)
    747 {
    748         int err;
    749 
    750         /* Issue configure endpoint command (sec 4.3.5) with the DC flag. */
    751         xhci_cmd_t cmd;
    752         xhci_cmd_init(&cmd);
    753 
    754         cmd.slot_id = slot_id;
    755         cmd.deconfigure = true;
    756 
    757         if ((err = xhci_send_configure_endpoint_command(hc, &cmd, NULL))) {
    758                 return err;
    759         }
    760 
    761         if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT))) {
    762                 return err;
    763         }
    764 
    765         xhci_cmd_fini(&cmd);
    766 
    767         return EOK;
    768 }
    769 
    770 int hc_add_endpoint(xhci_hc_t *hc, uint32_t slot_id, uint8_t ep_idx, xhci_ep_ctx_t *ep_ctx)
     822int hc_drop_endpoint(xhci_hc_t *hc, uint32_t slot_id, uint8_t ep_idx)
    771823{
    772824        int err;
     
    778830        }
    779831
    780         XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
    781         memcpy(&ictx->endpoint_ctx[ep_idx], ep_ctx, sizeof(xhci_ep_ctx_t));
     832        XHCI_INPUT_CTRL_CTX_DROP_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
    782833
    783834        // TODO: Set slot context and other flags. (probably forgot a lot of 'em)
     
    807858}
    808859
    809 int hc_drop_endpoint(xhci_hc_t *hc, uint32_t slot_id, uint8_t ep_idx)
    810 {
    811         int err;
    812 
    813         /* Issue configure endpoint command (sec 4.3.5). */
    814         xhci_input_ctx_t *ictx;
    815         if ((err = create_valid_input_ctx(&ictx))) {
    816                 goto err;
    817         }
    818 
    819         XHCI_INPUT_CTRL_CTX_DROP_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
    820 
    821         // TODO: Set slot context and other flags. (probably forgot a lot of 'em)
    822 
    823         xhci_cmd_t cmd;
    824         xhci_cmd_init(&cmd);
    825 
    826         cmd.slot_id = slot_id;
    827 
    828         if ((err = xhci_send_configure_endpoint_command(hc, &cmd, ictx))) {
    829                 goto err_cmd;
    830         }
    831 
    832         if ((err = xhci_cmd_wait(&cmd, XHCI_DEFAULT_TIMEOUT))) {
    833                 goto err_cmd;
    834         }
    835 
    836         xhci_cmd_fini(&cmd);
    837 
    838         free32(ictx);
    839         return EOK;
    840 
    841 err_cmd:
    842         free32(ictx);
    843 err:
    844         return err;
    845 }
    846 
    847860/**
    848861 * @}
Note: See TracChangeset for help on using the changeset viewer.