[e9e24f2] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Michal Staruch
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup drvusbxhci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief The host controller transfer ring management
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <usb/debug.h>
|
---|
[dcf0597] | 37 | #include <usb/request.h>
|
---|
[41924f30] | 38 | #include "endpoint.h"
|
---|
[e9e24f2] | 39 | #include "hc.h"
|
---|
| 40 | #include "hw_struct/trb.h"
|
---|
| 41 | #include "transfers.h"
|
---|
| 42 | #include "trb_ring.h"
|
---|
| 43 |
|
---|
[dbf32b1] | 44 | typedef enum {
|
---|
| 45 | STAGE_OUT,
|
---|
| 46 | STAGE_IN,
|
---|
| 47 | } stage_dir_flag_t;
|
---|
| 48 |
|
---|
| 49 | #define REQUEST_TYPE_DTD (0x80)
|
---|
| 50 | #define REQUEST_TYPE_IS_DEVICE_TO_HOST(rq) ((rq) & REQUEST_TYPE_DTD)
|
---|
| 51 |
|
---|
[e9e24f2] | 52 |
|
---|
[dbf32b1] | 53 | /** Get direction flag of data stage.
|
---|
| 54 | * See Table 7 of xHCI specification.
|
---|
| 55 | */
|
---|
| 56 | static inline stage_dir_flag_t get_status_direction_flag(xhci_trb_t* trb,
|
---|
| 57 | uint8_t bmRequestType, uint16_t wLength)
|
---|
[e9e24f2] | 58 | {
|
---|
| 59 | /* See Table 7 of xHCI specification */
|
---|
[dbf32b1] | 60 | return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType) && (wLength > 0)
|
---|
| 61 | ? STAGE_OUT
|
---|
| 62 | : STAGE_IN;
|
---|
[e9e24f2] | 63 | }
|
---|
| 64 |
|
---|
[dbf32b1] | 65 | typedef enum {
|
---|
| 66 | DATA_STAGE_NO = 0,
|
---|
| 67 | DATA_STAGE_OUT = 2,
|
---|
| 68 | DATA_STAGE_IN = 3,
|
---|
| 69 | } data_stage_type_t;
|
---|
| 70 |
|
---|
| 71 | /** Get transfer type flag.
|
---|
| 72 | * See Table 8 of xHCI specification.
|
---|
| 73 | */
|
---|
| 74 | static inline data_stage_type_t get_transfer_type(xhci_trb_t* trb, uint8_t
|
---|
| 75 | bmRequestType, uint16_t wLength)
|
---|
[e9e24f2] | 76 | {
|
---|
[dbf32b1] | 77 | if (wLength == 0)
|
---|
| 78 | return DATA_STAGE_NO;
|
---|
| 79 |
|
---|
[e9e24f2] | 80 | /* See Table 7 of xHCI specification */
|
---|
[dbf32b1] | 81 | return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType)
|
---|
| 82 | ? DATA_STAGE_IN
|
---|
| 83 | : DATA_STAGE_NO;
|
---|
[e9e24f2] | 84 | }
|
---|
| 85 |
|
---|
[d7869d7e] | 86 | static inline bool configure_endpoint_needed(usb_device_request_setup_packet_t *setup)
|
---|
| 87 | {
|
---|
| 88 | usb_request_type_t request_type = SETUP_REQUEST_TYPE_GET_TYPE(setup->request_type);
|
---|
| 89 |
|
---|
[dbf32b1] | 90 | return request_type == USB_REQUEST_TYPE_STANDARD &&
|
---|
| 91 | (setup->request == USB_DEVREQ_SET_CONFIGURATION
|
---|
| 92 | || setup->request == USB_DEVREQ_SET_INTERFACE);
|
---|
[d7869d7e] | 93 | }
|
---|
| 94 |
|
---|
[2b61945] | 95 | /**
|
---|
[eb928c4] | 96 | * Create a xHCI-specific transfer batch.
|
---|
| 97 | *
|
---|
| 98 | * Bus callback.
|
---|
[2b61945] | 99 | */
|
---|
[eb928c4] | 100 | usb_transfer_batch_t * xhci_transfer_create(endpoint_t* ep)
|
---|
[e9e24f2] | 101 | {
|
---|
[17873ac7] | 102 | xhci_transfer_t *transfer = calloc(1, sizeof(xhci_transfer_t));
|
---|
| 103 | if (!transfer)
|
---|
| 104 | return NULL;
|
---|
[e9e24f2] | 105 |
|
---|
[5fd9c30] | 106 | usb_transfer_batch_init(&transfer->batch, ep);
|
---|
[eb928c4] | 107 | return &transfer->batch;
|
---|
[e9e24f2] | 108 | }
|
---|
| 109 |
|
---|
[eb928c4] | 110 | /**
|
---|
| 111 | * Destroy a xHCI transfer.
|
---|
| 112 | */
|
---|
| 113 | void xhci_transfer_destroy(usb_transfer_batch_t* batch)
|
---|
[5fd9c30] | 114 | {
|
---|
[eb928c4] | 115 | xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
|
---|
[2770b66] | 116 |
|
---|
[b80c1ab] | 117 | dma_buffer_free(&transfer->hc_buffer);
|
---|
[a0e09ef] | 118 | free(transfer);
|
---|
[e9e24f2] | 119 | }
|
---|
| 120 |
|
---|
[5fd9c30] | 121 | static xhci_trb_ring_t *get_ring(xhci_hc_t *hc, xhci_transfer_t *transfer)
|
---|
[e9e24f2] | 122 | {
|
---|
[2b61945] | 123 | return &xhci_endpoint_get(transfer->batch.ep)->ring;
|
---|
[5fd9c30] | 124 | }
|
---|
[e9e24f2] | 125 |
|
---|
[5fd9c30] | 126 | static int schedule_control(xhci_hc_t* hc, xhci_transfer_t* transfer)
|
---|
| 127 | {
|
---|
| 128 | usb_transfer_batch_t *batch = &transfer->batch;
|
---|
| 129 | xhci_trb_ring_t *ring = get_ring(hc, transfer);
|
---|
| 130 | xhci_endpoint_t *xhci_ep = xhci_endpoint_get(transfer->batch.ep);
|
---|
[2770b66] | 131 |
|
---|
[5fd9c30] | 132 | usb_device_request_setup_packet_t* setup = &batch->setup.packet;
|
---|
[e9e24f2] | 133 |
|
---|
[5fd9c30] | 134 | xhci_trb_t trbs[3];
|
---|
| 135 | int trbs_used = 0;
|
---|
[e9e24f2] | 136 |
|
---|
[d1d7a92] | 137 | xhci_trb_t *trb_setup = trbs + trbs_used++;
|
---|
| 138 | xhci_trb_clean(trb_setup);
|
---|
| 139 |
|
---|
| 140 | TRB_CTRL_SET_SETUP_WVALUE(*trb_setup, setup->value);
|
---|
| 141 | TRB_CTRL_SET_SETUP_WLENGTH(*trb_setup, setup->length);
|
---|
| 142 | TRB_CTRL_SET_SETUP_WINDEX(*trb_setup, setup->index);
|
---|
| 143 | TRB_CTRL_SET_SETUP_BREQ(*trb_setup, setup->request);
|
---|
| 144 | TRB_CTRL_SET_SETUP_BMREQTYPE(*trb_setup, setup->request_type);
|
---|
[e9e24f2] | 145 |
|
---|
[eaf5e86] | 146 | /* Size of the setup packet is always 8 */
|
---|
[d1d7a92] | 147 | TRB_CTRL_SET_XFER_LEN(*trb_setup, 8);
|
---|
[e9e24f2] | 148 |
|
---|
[eaf5e86] | 149 | /* Immediate data */
|
---|
[d1d7a92] | 150 | TRB_CTRL_SET_IDT(*trb_setup, 1);
|
---|
| 151 | TRB_CTRL_SET_TRB_TYPE(*trb_setup, XHCI_TRB_TYPE_SETUP_STAGE);
|
---|
| 152 | TRB_CTRL_SET_TRT(*trb_setup, get_transfer_type(trb_setup, setup->request_type, setup->length));
|
---|
[e9e24f2] | 153 |
|
---|
[eaf5e86] | 154 | /* Data stage */
|
---|
[d1d7a92] | 155 | xhci_trb_t *trb_data = NULL;
|
---|
[eaf5e86] | 156 | if (setup->length > 0) {
|
---|
[5fd9c30] | 157 | trb_data = trbs + trbs_used++;
|
---|
| 158 | xhci_trb_clean(trb_data);
|
---|
[d1d7a92] | 159 |
|
---|
[b80c1ab] | 160 | trb_data->parameter = host2xhci(64, transfer->hc_buffer.phys);
|
---|
[e9e24f2] | 161 |
|
---|
[eaf5e86] | 162 | // data size (sent for OUT, or buffer size)
|
---|
[d1d7a92] | 163 | TRB_CTRL_SET_XFER_LEN(*trb_data, batch->buffer_size);
|
---|
[eaf5e86] | 164 | // FIXME: TD size 4.11.2.4
|
---|
[d1d7a92] | 165 | TRB_CTRL_SET_TD_SIZE(*trb_data, 1);
|
---|
[e9e24f2] | 166 |
|
---|
[eaf5e86] | 167 | // Some more fields here, no idea what they mean
|
---|
[d1d7a92] | 168 | TRB_CTRL_SET_TRB_TYPE(*trb_data, XHCI_TRB_TYPE_DATA_STAGE);
|
---|
[e9e24f2] | 169 |
|
---|
[5fd9c30] | 170 | int stage_dir = REQUEST_TYPE_IS_DEVICE_TO_HOST(setup->request_type)
|
---|
[dbf32b1] | 171 | ? STAGE_IN : STAGE_OUT;
|
---|
[5fd9c30] | 172 | TRB_CTRL_SET_DIR(*trb_data, stage_dir);
|
---|
[eaf5e86] | 173 | }
|
---|
[e9e24f2] | 174 |
|
---|
[eaf5e86] | 175 | /* Status stage */
|
---|
[d1d7a92] | 176 | xhci_trb_t *trb_status = trbs + trbs_used++;
|
---|
| 177 | xhci_trb_clean(trb_status);
|
---|
[e9e24f2] | 178 |
|
---|
[eaf5e86] | 179 | // FIXME: Evaluate next TRB? 4.12.3
|
---|
[d1d7a92] | 180 | // TRB_CTRL_SET_ENT(*trb_status, 1);
|
---|
[e9e24f2] | 181 |
|
---|
[d1d7a92] | 182 | TRB_CTRL_SET_IOC(*trb_status, 1);
|
---|
| 183 | TRB_CTRL_SET_TRB_TYPE(*trb_status, XHCI_TRB_TYPE_STATUS_STAGE);
|
---|
| 184 | TRB_CTRL_SET_DIR(*trb_status, get_status_direction_flag(trb_setup, setup->request_type, setup->length));
|
---|
[e9e24f2] | 185 |
|
---|
[d7869d7e] | 186 | // Issue a Configure Endpoint command, if needed.
|
---|
| 187 | if (configure_endpoint_needed(setup)) {
|
---|
[b724494] | 188 | const int err = hc_configure_device(hc, xhci_ep_to_dev(xhci_ep)->slot_id);
|
---|
[5fd9c30] | 189 | if (err)
|
---|
| 190 | return err;
|
---|
[d7869d7e] | 191 | }
|
---|
| 192 |
|
---|
[5fd9c30] | 193 | return xhci_trb_ring_enqueue_multiple(ring, trbs, trbs_used, &transfer->interrupt_trb_phys);
|
---|
[e9e24f2] | 194 | }
|
---|
| 195 |
|
---|
[5fd9c30] | 196 | static int schedule_bulk(xhci_hc_t* hc, xhci_transfer_t *transfer)
|
---|
[9b2f69e] | 197 | {
|
---|
[42bc933] | 198 | xhci_trb_t trb;
|
---|
[dbf32b1] | 199 | xhci_trb_clean(&trb);
|
---|
[b80c1ab] | 200 | trb.parameter = host2xhci(64, transfer->hc_buffer.phys);
|
---|
[42bc933] | 201 |
|
---|
| 202 | // data size (sent for OUT, or buffer size)
|
---|
[5fd9c30] | 203 | TRB_CTRL_SET_XFER_LEN(trb, transfer->batch.buffer_size);
|
---|
[eaf5e86] | 204 | // FIXME: TD size 4.11.2.4
|
---|
| 205 | TRB_CTRL_SET_TD_SIZE(trb, 1);
|
---|
[42bc933] | 206 |
|
---|
| 207 | // we want an interrupt after this td is done
|
---|
| 208 | TRB_CTRL_SET_IOC(trb, 1);
|
---|
| 209 |
|
---|
| 210 | TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
|
---|
| 211 |
|
---|
[2b61945] | 212 | xhci_trb_ring_t* ring = get_ring(hc, transfer);
|
---|
[42bc933] | 213 |
|
---|
[5fd9c30] | 214 | return xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
|
---|
[42bc933] | 215 | }
|
---|
| 216 |
|
---|
[5fd9c30] | 217 | static int schedule_interrupt(xhci_hc_t* hc, xhci_transfer_t* transfer)
|
---|
[9b2f69e] | 218 | {
|
---|
| 219 | xhci_trb_t trb;
|
---|
[dbf32b1] | 220 | xhci_trb_clean(&trb);
|
---|
[b80c1ab] | 221 | trb.parameter = host2xhci(64, transfer->hc_buffer.phys);
|
---|
[9b2f69e] | 222 |
|
---|
| 223 | // data size (sent for OUT, or buffer size)
|
---|
[5fd9c30] | 224 | TRB_CTRL_SET_XFER_LEN(trb, transfer->batch.buffer_size);
|
---|
[9b2f69e] | 225 | // FIXME: TD size 4.11.2.4
|
---|
| 226 | TRB_CTRL_SET_TD_SIZE(trb, 1);
|
---|
| 227 |
|
---|
| 228 | // we want an interrupt after this td is done
|
---|
| 229 | TRB_CTRL_SET_IOC(trb, 1);
|
---|
| 230 |
|
---|
| 231 | TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
|
---|
| 232 |
|
---|
[2b61945] | 233 | xhci_trb_ring_t* ring = get_ring(hc, transfer);
|
---|
[9b2f69e] | 234 |
|
---|
[5fd9c30] | 235 | return xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
|
---|
[9b2f69e] | 236 | }
|
---|
| 237 |
|
---|
[708d8fcd] | 238 | static int schedule_isochronous(xhci_transfer_t* transfer)
|
---|
[d3086873] | 239 | {
|
---|
[708d8fcd] | 240 | endpoint_t *ep = transfer->batch.ep;
|
---|
[d3086873] | 241 |
|
---|
[708d8fcd] | 242 | return ep->direction == USB_DIRECTION_OUT
|
---|
| 243 | ? isoch_schedule_out(transfer)
|
---|
| 244 | : isoch_schedule_in(transfer);
|
---|
[1252e81] | 245 | }
|
---|
| 246 |
|
---|
[e9e24f2] | 247 | int xhci_handle_transfer_event(xhci_hc_t* hc, xhci_trb_t* trb)
|
---|
| 248 | {
|
---|
| 249 | uintptr_t addr = trb->parameter;
|
---|
[2b61945] | 250 | const unsigned slot_id = XHCI_DWORD_EXTRACT(trb->control, 31, 24);
|
---|
| 251 | const unsigned ep_dci = XHCI_DWORD_EXTRACT(trb->control, 20, 16);
|
---|
[e9e24f2] | 252 |
|
---|
[2b61945] | 253 | xhci_device_t *dev = hc->bus.devices_by_slot[slot_id];
|
---|
| 254 | if (!dev) {
|
---|
[9620a54] | 255 | usb_log_error("Transfer event on disabled slot %u", slot_id);
|
---|
[2b61945] | 256 | return ENOENT;
|
---|
[e9e24f2] | 257 | }
|
---|
| 258 |
|
---|
[2b61945] | 259 | const usb_endpoint_t ep_num = ep_dci / 2;
|
---|
| 260 | xhci_endpoint_t *ep = xhci_device_get_endpoint(dev, ep_num);
|
---|
| 261 | if (!ep) {
|
---|
[9620a54] | 262 | usb_log_error("Transfer event on dropped endpoint %u of device "
|
---|
| 263 | XHCI_DEV_FMT, ep_num, XHCI_DEV_ARGS(*dev));
|
---|
[e9e24f2] | 264 | return ENOENT;
|
---|
| 265 | }
|
---|
[708d8fcd] | 266 | // No need to add reference for endpoint, it is held by the transfer batch.
|
---|
[e9e24f2] | 267 |
|
---|
[17873ac7] | 268 | /* FIXME: This is racy. Do we care? */
|
---|
[2b61945] | 269 | ep->ring.dequeue = addr;
|
---|
| 270 |
|
---|
[d3086873] | 271 | if (ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
---|
[708d8fcd] | 272 | return isoch_handle_transfer_event(hc, ep, trb);
|
---|
[d3086873] | 273 | }
|
---|
| 274 |
|
---|
[17873ac7] | 275 | fibril_mutex_lock(&ep->base.guard);
|
---|
| 276 | usb_transfer_batch_t *batch = ep->base.active_batch;
|
---|
| 277 | if (!batch) {
|
---|
| 278 | fibril_mutex_unlock(&ep->base.guard);
|
---|
| 279 | return ENOENT;
|
---|
| 280 | }
|
---|
[5fd9c30] | 281 |
|
---|
[6d91888] | 282 | const xhci_trb_completion_code_t completion_code = TRB_COMPLETION_CODE(*trb);
|
---|
| 283 | switch (completion_code) {
|
---|
| 284 | case XHCI_TRBC_SHORT_PACKET:
|
---|
| 285 | case XHCI_TRBC_SUCCESS:
|
---|
| 286 | batch->error = EOK;
|
---|
| 287 | batch->transfered_size = batch->buffer_size - TRB_TRANSFER_LENGTH(*trb);
|
---|
| 288 | break;
|
---|
| 289 |
|
---|
| 290 | default:
|
---|
| 291 | usb_log_warning("Transfer not successfull: %u", completion_code);
|
---|
| 292 | batch->error = EIO;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[17873ac7] | 295 | endpoint_deactivate_locked(&ep->base);
|
---|
| 296 | fibril_mutex_unlock(&ep->base.guard);
|
---|
| 297 |
|
---|
| 298 | xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
|
---|
[e9e24f2] | 299 |
|
---|
[5fd9c30] | 300 | if (batch->dir == USB_DIRECTION_IN) {
|
---|
| 301 | assert(batch->buffer);
|
---|
| 302 | assert(batch->transfered_size <= batch->buffer_size);
|
---|
[b80c1ab] | 303 | memcpy(batch->buffer, transfer->hc_buffer.virt, batch->transfered_size);
|
---|
[5fd9c30] | 304 | }
|
---|
| 305 |
|
---|
| 306 | usb_transfer_batch_finish(batch);
|
---|
[e9e24f2] | 307 | return EOK;
|
---|
| 308 | }
|
---|
[5fd9c30] | 309 |
|
---|
| 310 | typedef int (*transfer_handler)(xhci_hc_t *, xhci_transfer_t *);
|
---|
| 311 |
|
---|
| 312 | static const transfer_handler transfer_handlers[] = {
|
---|
| 313 | [USB_TRANSFER_CONTROL] = schedule_control,
|
---|
[d3086873] | 314 | [USB_TRANSFER_ISOCHRONOUS] = NULL,
|
---|
[5fd9c30] | 315 | [USB_TRANSFER_BULK] = schedule_bulk,
|
---|
| 316 | [USB_TRANSFER_INTERRUPT] = schedule_interrupt,
|
---|
| 317 | };
|
---|
| 318 |
|
---|
| 319 | int xhci_transfer_schedule(xhci_hc_t *hc, usb_transfer_batch_t *batch)
|
---|
| 320 | {
|
---|
| 321 | assert(hc);
|
---|
[17873ac7] | 322 | endpoint_t *ep = batch->ep;
|
---|
[5fd9c30] | 323 |
|
---|
| 324 | xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
|
---|
[17873ac7] | 325 | xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
|
---|
[a4e26882] | 326 | xhci_device_t *xhci_dev = xhci_ep_to_dev(xhci_ep);
|
---|
| 327 |
|
---|
[7010861] | 328 | // FIXME: find a better way to check if the ring is not initialized
|
---|
| 329 | if (!xhci_ep->ring.segment_count) {
|
---|
[9620a54] | 330 | usb_log_error("Ring not initialized for endpoint " XHCI_EP_FMT,
|
---|
[ef1a3a8] | 331 | XHCI_EP_ARGS(*xhci_ep));
|
---|
[7010861] | 332 | return EINVAL;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[d3086873] | 335 | // Isochronous transfer needs to be handled differently
|
---|
[deb2e55] | 336 | if (batch->ep->transfer_type == USB_TRANSFER_ISOCHRONOUS) {
|
---|
[708d8fcd] | 337 | return schedule_isochronous(transfer);
|
---|
[d3086873] | 338 | }
|
---|
| 339 |
|
---|
[5fd9c30] | 340 | const usb_transfer_type_t type = batch->ep->transfer_type;
|
---|
| 341 | assert(transfer_handlers[type]);
|
---|
| 342 |
|
---|
| 343 | if (batch->buffer_size > 0) {
|
---|
[b80c1ab] | 344 | if (dma_buffer_alloc(&transfer->hc_buffer, batch->buffer_size))
|
---|
[17873ac7] | 345 | return ENOMEM;
|
---|
[5fd9c30] | 346 | }
|
---|
| 347 |
|
---|
| 348 | if (batch->dir != USB_DIRECTION_IN) {
|
---|
| 349 | // Sending stuff from host to device, we need to copy the actual data.
|
---|
[b80c1ab] | 350 | memcpy(transfer->hc_buffer.virt, batch->buffer, batch->buffer_size);
|
---|
[5fd9c30] | 351 | }
|
---|
| 352 |
|
---|
[17873ac7] | 353 | fibril_mutex_lock(&ep->guard);
|
---|
| 354 | endpoint_activate_locked(ep, batch);
|
---|
[5fd9c30] | 355 | const int err = transfer_handlers[batch->ep->transfer_type](hc, transfer);
|
---|
[17873ac7] | 356 |
|
---|
| 357 | if (err) {
|
---|
| 358 | endpoint_deactivate_locked(ep);
|
---|
| 359 | fibril_mutex_unlock(&ep->guard);
|
---|
[5fd9c30] | 360 | return err;
|
---|
[17873ac7] | 361 | }
|
---|
| 362 |
|
---|
| 363 | /* After the critical section, the transfer can already be finished or aborted. */
|
---|
| 364 | transfer = NULL; batch = NULL;
|
---|
| 365 | fibril_mutex_unlock(&ep->guard);
|
---|
[5fd9c30] | 366 |
|
---|
[a4e26882] | 367 | const uint8_t slot_id = xhci_dev->slot_id;
|
---|
[5fd9c30] | 368 | const uint8_t target = xhci_endpoint_index(xhci_ep) + 1; /* EP Doorbells start at 1 */
|
---|
[708d8fcd] | 369 | hc_ring_doorbell(hc, slot_id, target);
|
---|
| 370 | return EOK;
|
---|
[5fd9c30] | 371 | }
|
---|