Changeset 2297fab in mainline


Ignore:
Timestamp:
2017-10-14T13:39:35Z (7 years ago)
Author:
Petr Manek <petr.manek@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d7869d7e
Parents:
dcf0597
Message:

Reintroduced alloc_dev(), brought it up to speed with recent changes.

File:
1 edited

Legend:

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

    rdcf0597 r2297fab  
    6969// TODO: Check device deallocation, we free device_ctx in hc.c, not
    7070//       sure about the other structs.
    71 // static int alloc_dev(xhci_hc_t *hc, uint8_t port, uint32_t route_str)
    72 // {
    73 //      int err;
    74 //
    75 //      xhci_cmd_t cmd;
    76 //      xhci_cmd_init(&cmd);
    77 //
    78 //      xhci_send_enable_slot_command(hc, &cmd);
    79 //      if ((err = xhci_cmd_wait(&cmd, 100000)) != EOK)
    80 //              return err;
    81 //
    82 //      uint32_t slot_id = cmd.slot_id;
    83 //
    84 //      usb_log_debug2("Obtained slot ID: %u.\n", slot_id);
    85 //      xhci_cmd_fini(&cmd);
    86 //
    87 //      xhci_input_ctx_t *ictx = malloc(sizeof(xhci_input_ctx_t));
    88 //      if (!ictx) {
    89 //              return ENOMEM;
    90 //      }
    91 //
    92 //      memset(ictx, 0, sizeof(xhci_input_ctx_t));
    93 //
    94 //      XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
    95 //      XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
    96 //
    97 //      /* Initialize slot_ctx according to section 4.3.3 point 3. */
    98 //      /* Attaching to root hub port, root string equals to 0. */
    99 //      XHCI_SLOT_ROOT_HUB_PORT_SET(ictx->slot_ctx, port);
    100 //      XHCI_SLOT_CTX_ENTRIES_SET(ictx->slot_ctx, 1);
    101 //      XHCI_SLOT_ROUTE_STRING_SET(ictx->slot_ctx, route_str);
    102 //
    103 //      xhci_trb_ring_t *ep_ring = malloc(sizeof(xhci_trb_ring_t));
    104 //      if (!ep_ring) {
    105 //              err = ENOMEM;
    106 //              goto err_ictx;
    107 //      }
    108 //
    109 //      err = xhci_trb_ring_init(ep_ring, hc);
    110 //      if (err)
    111 //              goto err_ring;
    112 //
    113 //      XHCI_EP_TYPE_SET(ictx->endpoint_ctx[0], EP_TYPE_CONTROL);
    114 //      // TODO: must be changed with a command after USB descriptor is read
    115 //      // See 4.6.5 in XHCI specification, first note
    116 //      XHCI_EP_MAX_PACKET_SIZE_SET(ictx->endpoint_ctx[0],
    117 //          xhci_is_usb3_port(&hc->rh, port) ? 512 : 8);
    118 //      XHCI_EP_MAX_BURST_SIZE_SET(ictx->endpoint_ctx[0], 0);
    119 //      /* FIXME physical pointer? */
    120 //      XHCI_EP_TR_DPTR_SET(ictx->endpoint_ctx[0], ep_ring->dequeue);
    121 //      XHCI_EP_DCS_SET(ictx->endpoint_ctx[0], 1);
    122 //      XHCI_EP_INTERVAL_SET(ictx->endpoint_ctx[0], 0);
    123 //      XHCI_EP_MAX_P_STREAMS_SET(ictx->endpoint_ctx[0], 0);
    124 //      XHCI_EP_MULT_SET(ictx->endpoint_ctx[0], 0);
    125 //      XHCI_EP_ERROR_COUNT_SET(ictx->endpoint_ctx[0], 3);
    126 //
    127 //      // TODO: What's the alignment?
    128 //      xhci_device_ctx_t *dctx = malloc(sizeof(xhci_device_ctx_t));
    129 //      if (!dctx) {
    130 //              err = ENOMEM;
    131 //              goto err_ring;
    132 //      }
    133 //      memset(dctx, 0, sizeof(xhci_device_ctx_t));
    134 //
    135 //      hc->dcbaa[slot_id] = addr_to_phys(dctx);
    136 //
    137 //      memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
    138 //      hc->dcbaa_virt[slot_id].dev_ctx = dctx;
    139 //      hc->dcbaa_virt[slot_id].trs[0] = ep_ring;
    140 //
    141 //      xhci_cmd_init(&cmd);
    142 //      cmd.slot_id = slot_id;
    143 //      xhci_send_address_device_command(hc, &cmd, ictx);
    144 //      if ((err = xhci_cmd_wait(&cmd, 100000)) != EOK)
    145 //              goto err_dctx;
    146 //
    147 //      xhci_cmd_fini(&cmd);
    148 //
    149 //      // TODO: Issue configure endpoint commands (sec 4.3.5).
    150 //
    151 //      return EOK;
    152 //
    153 // err_dctx:
    154 //      if (dctx) {
    155 //              free(dctx);
    156 //              hc->dcbaa[slot_id] = 0;
    157 //              memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
    158 //      }
    159 // err_ring:
    160 //      if (ep_ring) {
    161 //              xhci_trb_ring_fini(ep_ring);
    162 //              free(ep_ring);
    163 //      }
    164 // err_ictx:
    165 //      free(ictx);
    166 //      return err;
    167 // }
     71static int alloc_dev(xhci_hc_t *hc, uint8_t port, uint32_t route_str)
     72{
     73        int err;
     74
     75        xhci_cmd_t cmd;
     76        xhci_cmd_init(&cmd);
     77
     78        const xhci_port_speed_t *speed = xhci_rh_get_port_speed(&hc->rh, port);
     79
     80        xhci_send_enable_slot_command(hc, &cmd);
     81        if ((err = xhci_cmd_wait(&cmd, 100000)) != EOK)
     82                return err;
     83
     84        uint32_t slot_id = cmd.slot_id;
     85
     86        usb_log_debug2("Obtained slot ID: %u.\n", slot_id);
     87        xhci_cmd_fini(&cmd);
     88
     89        xhci_input_ctx_t *ictx = malloc(sizeof(xhci_input_ctx_t));
     90        if (!ictx) {
     91                return ENOMEM;
     92        }
     93
     94        memset(ictx, 0, sizeof(xhci_input_ctx_t));
     95
     96        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
     97        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 1);
     98
     99        /* Initialize slot_ctx according to section 4.3.3 point 3. */
     100        /* Attaching to root hub port, root string equals to 0. */
     101        XHCI_SLOT_ROOT_HUB_PORT_SET(ictx->slot_ctx, port);
     102        XHCI_SLOT_CTX_ENTRIES_SET(ictx->slot_ctx, 1);
     103        XHCI_SLOT_ROUTE_STRING_SET(ictx->slot_ctx, route_str);
     104
     105        xhci_trb_ring_t *ep_ring = malloc(sizeof(xhci_trb_ring_t));
     106        if (!ep_ring) {
     107                err = ENOMEM;
     108                goto err_ictx;
     109        }
     110
     111        err = xhci_trb_ring_init(ep_ring, hc);
     112        if (err)
     113                goto err_ring;
     114
     115        XHCI_EP_TYPE_SET(ictx->endpoint_ctx[0], EP_TYPE_CONTROL);
     116        // TODO: must be changed with a command after USB descriptor is read
     117        // See 4.6.5 in XHCI specification, first note
     118        XHCI_EP_MAX_PACKET_SIZE_SET(ictx->endpoint_ctx[0],
     119            speed->major == 3 ? 512 : 8);
     120        XHCI_EP_MAX_BURST_SIZE_SET(ictx->endpoint_ctx[0], 0);
     121        /* FIXME physical pointer? */
     122        XHCI_EP_TR_DPTR_SET(ictx->endpoint_ctx[0], ep_ring->dequeue);
     123        XHCI_EP_DCS_SET(ictx->endpoint_ctx[0], 1);
     124        XHCI_EP_INTERVAL_SET(ictx->endpoint_ctx[0], 0);
     125        XHCI_EP_MAX_P_STREAMS_SET(ictx->endpoint_ctx[0], 0);
     126        XHCI_EP_MULT_SET(ictx->endpoint_ctx[0], 0);
     127        XHCI_EP_ERROR_COUNT_SET(ictx->endpoint_ctx[0], 3);
     128
     129        // TODO: What's the alignment?
     130        xhci_device_ctx_t *dctx = malloc(sizeof(xhci_device_ctx_t));
     131        if (!dctx) {
     132                err = ENOMEM;
     133                goto err_ring;
     134        }
     135        memset(dctx, 0, sizeof(xhci_device_ctx_t));
     136
     137        hc->dcbaa[slot_id] = addr_to_phys(dctx);
     138
     139        memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
     140        hc->dcbaa_virt[slot_id].dev_ctx = dctx;
     141        hc->dcbaa_virt[slot_id].trs[0] = ep_ring;
     142
     143        xhci_cmd_init(&cmd);
     144        cmd.slot_id = slot_id;
     145        xhci_send_address_device_command(hc, &cmd, ictx);
     146        if ((err = xhci_cmd_wait(&cmd, 100000)) != EOK)
     147                goto err_dctx;
     148
     149        xhci_cmd_fini(&cmd);
     150
     151        // TODO: Issue configure endpoint commands (sec 4.3.5).
     152
     153        return EOK;
     154
     155err_dctx:
     156        if (dctx) {
     157                free(dctx);
     158                hc->dcbaa[slot_id] = 0;
     159                memset(&hc->dcbaa_virt[slot_id], 0, sizeof(xhci_virt_device_ctx_t));
     160        }
     161err_ring:
     162        if (ep_ring) {
     163                xhci_trb_ring_fini(ep_ring);
     164                free(ep_ring);
     165        }
     166err_ictx:
     167        free(ictx);
     168        return err;
     169}
    168170
    169171static int handle_connected_device(xhci_rh_t *rh, uint8_t port_id)
     
    179181                if (link_state == 0) {
    180182                        /* USB3 is automatically advanced to enabled. */
    181                         // return alloc_dev(hc, port_id, 0);
    182                         return ENOTSUP;
     183                        return alloc_dev(rh->hc, port_id, 0);
    183184                }
    184185                else if (link_state == 5) {
     
    258259                        usb_log_info("Port reset on port %u completed.", i);
    259260                        events &= ~XHCI_REG_MASK(XHCI_PORT_PRC);
     261
     262                        const xhci_port_speed_t *speed = xhci_rh_get_port_speed(rh, i);
     263                        if (speed->major != 3) {
     264                                /* FIXME: We probably don't want to do this
     265                                 * every time USB2 port is reset. This is a
     266                                 * temporary workaround. */
     267                                alloc_dev(rh->hc, i, 0);
     268                        }
    260269                }
    261270
     
    274283                }
    275284        }
    276        
     285
    277286        /**
    278287         * Theory:
Note: See TracChangeset for help on using the changeset viewer.