source: mainline/uspace/drv/bus/usb/xhci/transfers.c@ 45457265

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 45457265 was 45457265, checked in by Jenda <jenda.jzqk73@…>, 8 years ago

errno_t all the things!

  • Property mode set to 100644
File size: 16.2 KB
RevLine 
[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"
[47e9494]41#include "streams.h"
[e9e24f2]42#include "transfers.h"
43#include "trb_ring.h"
44
[dbf32b1]45typedef enum {
46 STAGE_OUT,
47 STAGE_IN,
48} stage_dir_flag_t;
49
50#define REQUEST_TYPE_DTD (0x80)
51#define REQUEST_TYPE_IS_DEVICE_TO_HOST(rq) ((rq) & REQUEST_TYPE_DTD)
52
[e9e24f2]53
[dbf32b1]54/** Get direction flag of data stage.
55 * See Table 7 of xHCI specification.
56 */
57static inline stage_dir_flag_t get_status_direction_flag(xhci_trb_t* trb,
58 uint8_t bmRequestType, uint16_t wLength)
[e9e24f2]59{
60 /* See Table 7 of xHCI specification */
[dbf32b1]61 return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType) && (wLength > 0)
62 ? STAGE_OUT
63 : STAGE_IN;
[e9e24f2]64}
65
[dbf32b1]66typedef enum {
67 DATA_STAGE_NO = 0,
68 DATA_STAGE_OUT = 2,
69 DATA_STAGE_IN = 3,
70} data_stage_type_t;
71
72/** Get transfer type flag.
73 * See Table 8 of xHCI specification.
74 */
75static inline data_stage_type_t get_transfer_type(xhci_trb_t* trb, uint8_t
76 bmRequestType, uint16_t wLength)
[e9e24f2]77{
[dbf32b1]78 if (wLength == 0)
79 return DATA_STAGE_NO;
80
[e9e24f2]81 /* See Table 7 of xHCI specification */
[dbf32b1]82 return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType)
83 ? DATA_STAGE_IN
84 : DATA_STAGE_NO;
[e9e24f2]85}
86
[d7869d7e]87static inline bool configure_endpoint_needed(usb_device_request_setup_packet_t *setup)
88{
89 usb_request_type_t request_type = SETUP_REQUEST_TYPE_GET_TYPE(setup->request_type);
90
[dbf32b1]91 return request_type == USB_REQUEST_TYPE_STANDARD &&
92 (setup->request == USB_DEVREQ_SET_CONFIGURATION
93 || setup->request == USB_DEVREQ_SET_INTERFACE);
[d7869d7e]94}
95
[2b61945]96/**
[eb928c4]97 * Create a xHCI-specific transfer batch.
98 *
99 * Bus callback.
[2b61945]100 */
[eb928c4]101usb_transfer_batch_t * xhci_transfer_create(endpoint_t* ep)
[e9e24f2]102{
[17873ac7]103 xhci_transfer_t *transfer = calloc(1, sizeof(xhci_transfer_t));
104 if (!transfer)
105 return NULL;
[e9e24f2]106
[5fd9c30]107 usb_transfer_batch_init(&transfer->batch, ep);
[eb928c4]108 return &transfer->batch;
[e9e24f2]109}
110
[eb928c4]111/**
112 * Destroy a xHCI transfer.
113 */
114void xhci_transfer_destroy(usb_transfer_batch_t* batch)
[5fd9c30]115{
[eb928c4]116 xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
[2770b66]117
[b80c1ab]118 dma_buffer_free(&transfer->hc_buffer);
[a0e09ef]119 free(transfer);
[e9e24f2]120}
121
[51c1d500]122static xhci_trb_ring_t *get_ring(xhci_transfer_t *transfer)
[e9e24f2]123{
[51c1d500]124 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(transfer->batch.ep);
125 return xhci_endpoint_get_ring(xhci_ep, transfer->batch.target.stream);
[5fd9c30]126}
[e9e24f2]127
[a1ce9bd]128static int calculate_trb_count(xhci_transfer_t *transfer)
129{
130 const size_t size = transfer->batch.buffer_size;
131 return (size + PAGE_SIZE - 1 )/ PAGE_SIZE;
132}
133
134static void trb_set_buffer(xhci_transfer_t *transfer, xhci_trb_t *trb,
[3038d51]135 size_t i, size_t total, size_t *remaining)
[a1ce9bd]136{
137 const uintptr_t ptr = dma_buffer_phys(&transfer->hc_buffer,
138 transfer->hc_buffer.virt + i * PAGE_SIZE);
139
140 trb->parameter = host2xhci(64, ptr);
141 TRB_CTRL_SET_TD_SIZE(*trb, max(31, total - i - 1));
[3038d51]142 if (*remaining > PAGE_SIZE) {
[a1ce9bd]143 TRB_CTRL_SET_XFER_LEN(*trb, PAGE_SIZE);
[3038d51]144 *remaining -= PAGE_SIZE;
[a1ce9bd]145 }
146 else {
[3038d51]147 TRB_CTRL_SET_XFER_LEN(*trb, *remaining);
148 *remaining = 0;
[a1ce9bd]149 }
150}
151
[45457265]152static errno_t schedule_control(xhci_hc_t* hc, xhci_transfer_t* transfer)
[5fd9c30]153{
154 usb_transfer_batch_t *batch = &transfer->batch;
155 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(transfer->batch.ep);
[2770b66]156
[5fd9c30]157 usb_device_request_setup_packet_t* setup = &batch->setup.packet;
[e9e24f2]158
[3038d51]159 size_t buffer_count = 0;
160 if (setup->length > 0) {
161 buffer_count = calculate_trb_count(transfer);
162 }
[e9e24f2]163
[3038d51]164 xhci_trb_t trbs[buffer_count + 2];
165
166 xhci_trb_t *trb_setup = trbs;
[d1d7a92]167 xhci_trb_clean(trb_setup);
168
169 TRB_CTRL_SET_SETUP_WVALUE(*trb_setup, setup->value);
170 TRB_CTRL_SET_SETUP_WLENGTH(*trb_setup, setup->length);
171 TRB_CTRL_SET_SETUP_WINDEX(*trb_setup, setup->index);
172 TRB_CTRL_SET_SETUP_BREQ(*trb_setup, setup->request);
173 TRB_CTRL_SET_SETUP_BMREQTYPE(*trb_setup, setup->request_type);
[e9e24f2]174
[eaf5e86]175 /* Size of the setup packet is always 8 */
[d1d7a92]176 TRB_CTRL_SET_XFER_LEN(*trb_setup, 8);
[e9e24f2]177
[eaf5e86]178 /* Immediate data */
[d1d7a92]179 TRB_CTRL_SET_IDT(*trb_setup, 1);
180 TRB_CTRL_SET_TRB_TYPE(*trb_setup, XHCI_TRB_TYPE_SETUP_STAGE);
[8033f89]181 TRB_CTRL_SET_TRT(*trb_setup,
182 get_transfer_type(trb_setup, setup->request_type, setup->length));
[e9e24f2]183
[eaf5e86]184 /* Data stage */
185 if (setup->length > 0) {
[3038d51]186 int stage_dir = REQUEST_TYPE_IS_DEVICE_TO_HOST(setup->request_type)
187 ? STAGE_IN : STAGE_OUT;
188 size_t remaining = transfer->batch.buffer_size;
[d1d7a92]189
[3038d51]190 for (size_t i = 0; i < buffer_count; ++i) {
191 xhci_trb_clean(&trbs[i + 1]);
192 trb_set_buffer(transfer, &trbs[i + 1], i, buffer_count, &remaining);
[e9e24f2]193
[3038d51]194 TRB_CTRL_SET_DIR(trbs[i + 1], stage_dir);
195 TRB_CTRL_SET_TRB_TYPE(trbs[i + 1], XHCI_TRB_TYPE_DATA_STAGE);
[e9e24f2]196
[3038d51]197 if (i == buffer_count - 1) break;
[e9e24f2]198
[3038d51]199 /* Set the chain bit as this is not the last TRB */
200 TRB_CTRL_SET_CHAIN(trbs[i], 1);
201 }
[eaf5e86]202 }
[e9e24f2]203
[eaf5e86]204 /* Status stage */
[3038d51]205 xhci_trb_t *trb_status = trbs + buffer_count + 1;
[d1d7a92]206 xhci_trb_clean(trb_status);
[e9e24f2]207
[d1d7a92]208 TRB_CTRL_SET_IOC(*trb_status, 1);
209 TRB_CTRL_SET_TRB_TYPE(*trb_status, XHCI_TRB_TYPE_STATUS_STAGE);
[8033f89]210 TRB_CTRL_SET_DIR(*trb_status, get_status_direction_flag(trb_setup,
211 setup->request_type, setup->length));
[e9e24f2]212
[d7869d7e]213 // Issue a Configure Endpoint command, if needed.
214 if (configure_endpoint_needed(setup)) {
[45457265]215 const errno_t err = hc_configure_device(xhci_ep_to_dev(xhci_ep));
[5fd9c30]216 if (err)
217 return err;
[d7869d7e]218 }
219
[8033f89]220 return xhci_trb_ring_enqueue_multiple(get_ring(transfer), trbs,
[3038d51]221 buffer_count + 2, &transfer->interrupt_trb_phys);
[e9e24f2]222}
223
[45457265]224static errno_t schedule_bulk(xhci_hc_t* hc, xhci_transfer_t *transfer)
[9b2f69e]225{
[47e9494]226 /* The stream-enabled endpoints need to chain ED trb */
227 xhci_endpoint_t *ep = xhci_endpoint_get(transfer->batch.ep);
228 if (!ep->primary_stream_data_size) {
[a1ce9bd]229 const size_t buffer_count = calculate_trb_count(transfer);
230 xhci_trb_t trbs[buffer_count];
[3038d51]231 size_t remaining = transfer->batch.buffer_size;
[42bc933]232
[a1ce9bd]233 for (size_t i = 0; i < buffer_count; ++i) {
234 xhci_trb_clean(&trbs[i]);
[3038d51]235 trb_set_buffer(transfer, &trbs[i], i, buffer_count, &remaining);
[a1ce9bd]236 TRB_CTRL_SET_TRB_TYPE(trbs[i], XHCI_TRB_TYPE_NORMAL);
[42bc933]237
[a1ce9bd]238 if (i == buffer_count - 1) break;
239
240 /* Set the chain bit as this is not the last TRB */
241 TRB_CTRL_SET_CHAIN(trbs[i], 1);
242 }
243 /* Set the interrupt bit for last TRB */
244 TRB_CTRL_SET_IOC(trbs[buffer_count - 1], 1);
[3038d51]245
246 xhci_trb_ring_t* ring = get_ring(transfer);
[a1ce9bd]247 return xhci_trb_ring_enqueue_multiple(ring, &trbs[0], buffer_count,
248 &transfer->interrupt_trb_phys);
[47e9494]249 }
250 else {
[51c1d500]251 xhci_trb_ring_t* ring = get_ring(transfer);
[4cc0c2e0]252 if (!ring) {
253 return EINVAL;
254 }
255
[3038d51]256 const size_t buffer_count = calculate_trb_count(transfer);
257 xhci_trb_t trbs[buffer_count + 1];
258 size_t remaining = transfer->batch.buffer_size;
[47e9494]259
[3038d51]260 for (size_t i = 0; i < buffer_count; ++i) {
261 xhci_trb_clean(&trbs[i]);
262 trb_set_buffer(transfer, &trbs[i], i, buffer_count + 1, &remaining);
263 TRB_CTRL_SET_TRB_TYPE(trbs[i], XHCI_TRB_TYPE_NORMAL);
264 TRB_CTRL_SET_CHAIN(trbs[i], 1);
[47e9494]265 }
[3038d51]266 TRB_CTRL_SET_ENT(trbs[buffer_count - 1], 1);
[47e9494]267
[3038d51]268 xhci_trb_clean(&trbs[buffer_count]);
269 trbs[buffer_count].parameter = host2xhci(64, (uintptr_t) transfer);
270 TRB_CTRL_SET_TRB_TYPE(trbs[buffer_count], XHCI_TRB_TYPE_EVENT_DATA);
271 TRB_CTRL_SET_IOC(trbs[buffer_count], 1);
[47e9494]272
[3038d51]273 return xhci_trb_ring_enqueue_multiple(ring, &trbs[0], buffer_count + 1,
274 &transfer->interrupt_trb_phys);
[47e9494]275 }
[42bc933]276}
277
[45457265]278static errno_t schedule_interrupt(xhci_hc_t* hc, xhci_transfer_t* transfer)
[9b2f69e]279{
[a1ce9bd]280 const size_t buffer_count = calculate_trb_count(transfer);
281 xhci_trb_t trbs[buffer_count];
[3038d51]282 size_t remaining = transfer->batch.buffer_size;
[9b2f69e]283
[a1ce9bd]284 for (size_t i = 0; i < buffer_count; ++i) {
285 xhci_trb_clean(&trbs[i]);
[3038d51]286 trb_set_buffer(transfer, &trbs[i], i, buffer_count, &remaining);
[a1ce9bd]287 TRB_CTRL_SET_TRB_TYPE(trbs[i], XHCI_TRB_TYPE_NORMAL);
[9b2f69e]288
[a1ce9bd]289 if (i == buffer_count - 1) break;
[9b2f69e]290
[a1ce9bd]291 /* Set the chain bit as this is not the last TRB */
292 TRB_CTRL_SET_CHAIN(trbs[i], 1);
293 }
294 /* Set the interrupt bit for last TRB */
295 TRB_CTRL_SET_IOC(trbs[buffer_count - 1], 1);
[3038d51]296
297 xhci_trb_ring_t* ring = get_ring(transfer);
[a1ce9bd]298 return xhci_trb_ring_enqueue_multiple(ring, &trbs[0], buffer_count,
299 &transfer->interrupt_trb_phys);
[9b2f69e]300}
301
[708d8fcd]302static int schedule_isochronous(xhci_transfer_t* transfer)
[d3086873]303{
[708d8fcd]304 endpoint_t *ep = transfer->batch.ep;
[d3086873]305
[708d8fcd]306 return ep->direction == USB_DIRECTION_OUT
307 ? isoch_schedule_out(transfer)
308 : isoch_schedule_in(transfer);
[1252e81]309}
310
[45457265]311errno_t xhci_handle_transfer_event(xhci_hc_t* hc, xhci_trb_t* trb)
[e9e24f2]312{
313 uintptr_t addr = trb->parameter;
[2b61945]314 const unsigned slot_id = XHCI_DWORD_EXTRACT(trb->control, 31, 24);
315 const unsigned ep_dci = XHCI_DWORD_EXTRACT(trb->control, 20, 16);
[e9e24f2]316
[2b61945]317 xhci_device_t *dev = hc->bus.devices_by_slot[slot_id];
318 if (!dev) {
[9620a54]319 usb_log_error("Transfer event on disabled slot %u", slot_id);
[2b61945]320 return ENOENT;
[e9e24f2]321 }
322
[2b61945]323 const usb_endpoint_t ep_num = ep_dci / 2;
[1ed3eb4]324 const usb_endpoint_t dir = ep_dci % 2 ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
[41abf3c]325 /* Creating temporary reference */
[1ed3eb4]326 endpoint_t *ep_base = bus_find_endpoint(&dev->base, ep_num, dir);
327 if (!ep_base) {
328 usb_log_error("Transfer event on dropped endpoint %u %s of device "
329 XHCI_DEV_FMT, ep_num, usb_str_direction(dir), XHCI_DEV_ARGS(*dev));
[e9e24f2]330 return ENOENT;
331 }
[1ed3eb4]332 xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
[e9e24f2]333
[47e9494]334 usb_transfer_batch_t *batch;
335 xhci_transfer_t *transfer;
[2b61945]336
[47e9494]337 if (TRB_EVENT_DATA(*trb)) {
[4db49344]338 /* We schedule those only when streams are involved */
339 assert(ep->primary_stream_ctx_array != NULL);
340
[47e9494]341 /* We are received transfer pointer instead - work with that */
342 transfer = (xhci_transfer_t *) addr;
[8033f89]343 xhci_trb_ring_update_dequeue(get_ring(transfer),
344 transfer->interrupt_trb_phys);
[47e9494]345 batch = &transfer->batch;
346 }
347 else {
348 xhci_trb_ring_update_dequeue(&ep->ring, addr);
349
350 if (ep->base.transfer_type == USB_TRANSFER_ISOCHRONOUS) {
351 isoch_handle_transfer_event(hc, ep, trb);
[41abf3c]352 /* Dropping temporary reference */
[47e9494]353 endpoint_del_ref(&ep->base);
354 return EOK;
355 }
356
[4db49344]357 fibril_mutex_lock(&ep->guard);
[47e9494]358 batch = ep->base.active_batch;
[4db49344]359 endpoint_deactivate_locked(&ep->base);
360 fibril_mutex_unlock(&ep->guard);
361
[47e9494]362 if (!batch) {
[41abf3c]363 /* Dropping temporary reference */
[47e9494]364 endpoint_del_ref(&ep->base);
365 return ENOENT;
366 }
367
368 transfer = xhci_transfer_from_batch(batch);
[17873ac7]369 }
[5fd9c30]370
[6d91888]371 const xhci_trb_completion_code_t completion_code = TRB_COMPLETION_CODE(*trb);
372 switch (completion_code) {
373 case XHCI_TRBC_SHORT_PACKET:
374 case XHCI_TRBC_SUCCESS:
375 batch->error = EOK;
[db51a6a6]376 batch->transferred_size = batch->buffer_size - TRB_TRANSFER_LENGTH(*trb);
[6d91888]377 break;
378
[feabe163]379 case XHCI_TRBC_DATA_BUFFER_ERROR:
380 usb_log_warning("Transfer ended with data buffer error.");
381 batch->error = EAGAIN;
[db51a6a6]382 batch->transferred_size = 0;
[feabe163]383 break;
384
385 case XHCI_TRBC_BABBLE_DETECTED_ERROR:
[8fe29a7c]386 usb_log_warning("Babble detected during the transfer.");
[feabe163]387 batch->error = EAGAIN;
[db51a6a6]388 batch->transferred_size = 0;
[feabe163]389 break;
390
391 case XHCI_TRBC_USB_TRANSACTION_ERROR:
[8fe29a7c]392 usb_log_warning("USB Transaction error.");
[e454d9c]393 batch->error = EAGAIN;
[db51a6a6]394 batch->transferred_size = 0;
[feabe163]395 break;
396
397 case XHCI_TRBC_TRB_ERROR:
398 usb_log_error("Invalid transfer parameters.");
399 batch->error = EINVAL;
[db51a6a6]400 batch->transferred_size = 0;
[feabe163]401 break;
402
403 case XHCI_TRBC_STALL_ERROR:
404 usb_log_warning("Stall condition detected.");
405 batch->error = ESTALL;
[db51a6a6]406 batch->transferred_size = 0;
[feabe163]407 break;
408
409 case XHCI_TRBC_SPLIT_TRANSACTION_ERROR:
410 usb_log_error("Split transcation error detected.");
411 batch->error = EAGAIN;
[db51a6a6]412 batch->transferred_size = 0;
[feabe163]413 break;
414
[6d91888]415 default:
416 usb_log_warning("Transfer not successfull: %u", completion_code);
417 batch->error = EIO;
418 }
419
[5fd9c30]420 if (batch->dir == USB_DIRECTION_IN) {
421 assert(batch->buffer);
[db51a6a6]422 assert(batch->transferred_size <= batch->buffer_size);
423 memcpy(batch->buffer, transfer->hc_buffer.virt, batch->transferred_size);
[5fd9c30]424 }
425
426 usb_transfer_batch_finish(batch);
[41abf3c]427 /* Dropping temporary reference */
[1ed3eb4]428 endpoint_del_ref(&ep->base);
[e9e24f2]429 return EOK;
430}
[5fd9c30]431
[45457265]432typedef errno_t (*transfer_handler)(xhci_hc_t *, xhci_transfer_t *);
[5fd9c30]433
434static const transfer_handler transfer_handlers[] = {
435 [USB_TRANSFER_CONTROL] = schedule_control,
[d3086873]436 [USB_TRANSFER_ISOCHRONOUS] = NULL,
[5fd9c30]437 [USB_TRANSFER_BULK] = schedule_bulk,
438 [USB_TRANSFER_INTERRUPT] = schedule_interrupt,
439};
440
[682c9354]441/**
442 * Schedule a batch for xHC.
443 *
444 * Bus callback.
445 */
[45457265]446errno_t xhci_transfer_schedule(usb_transfer_batch_t *batch)
[5fd9c30]447{
[17873ac7]448 endpoint_t *ep = batch->ep;
[5fd9c30]449
[682c9354]450 xhci_hc_t *hc = bus_to_hc(endpoint_get_bus(batch->ep));
[5fd9c30]451 xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
[17873ac7]452 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
[a4e26882]453 xhci_device_t *xhci_dev = xhci_ep_to_dev(xhci_ep);
454
[682c9354]455 if (!batch->target.address) {
456 usb_log_error("Attempted to schedule transfer to address 0.");
457 return EINVAL;
458 }
459
[7010861]460 // FIXME: find a better way to check if the ring is not initialized
461 if (!xhci_ep->ring.segment_count) {
[9620a54]462 usb_log_error("Ring not initialized for endpoint " XHCI_EP_FMT,
[ef1a3a8]463 XHCI_EP_ARGS(*xhci_ep));
[7010861]464 return EINVAL;
465 }
466
[d3086873]467 // Isochronous transfer needs to be handled differently
[deb2e55]468 if (batch->ep->transfer_type == USB_TRANSFER_ISOCHRONOUS) {
[708d8fcd]469 return schedule_isochronous(transfer);
[d3086873]470 }
471
[5fd9c30]472 const usb_transfer_type_t type = batch->ep->transfer_type;
473 assert(transfer_handlers[type]);
474
475 if (batch->buffer_size > 0) {
[b80c1ab]476 if (dma_buffer_alloc(&transfer->hc_buffer, batch->buffer_size))
[17873ac7]477 return ENOMEM;
[5fd9c30]478 }
479
480 if (batch->dir != USB_DIRECTION_IN) {
481 // Sending stuff from host to device, we need to copy the actual data.
[b80c1ab]482 memcpy(transfer->hc_buffer.virt, batch->buffer, batch->buffer_size);
[5fd9c30]483 }
484
[609f3f73]485 /*
486 * If this is a ClearFeature(ENDPOINT_HALT) request, we have to issue
487 * the Reset Endpoint command.
488 */
[8033f89]489 if (batch->ep->transfer_type == USB_TRANSFER_CONTROL
490 && batch->dir == USB_DIRECTION_OUT) {
[609f3f73]491 const usb_device_request_setup_packet_t *request = &batch->setup.packet;
492 if (request->request == USB_DEVREQ_CLEAR_FEATURE
493 && request->request_type == USB_REQUEST_RECIPIENT_ENDPOINT
494 && request->value == USB_FEATURE_ENDPOINT_HALT) {
495 const uint16_t index = uint16_usb2host(request->index);
496 const usb_endpoint_t ep_num = index & 0xf;
[8033f89]497 const usb_direction_t dir = (index >> 7)
498 ? USB_DIRECTION_IN
499 : USB_DIRECTION_OUT;
[609f3f73]500 endpoint_t *halted_ep = bus_find_endpoint(&xhci_dev->base, ep_num, dir);
501 if (halted_ep) {
502 /*
[8033f89]503 * TODO: Find out how to come up with stream_id. It might be
504 * possible that we have to clear all of them.
[609f3f73]505 */
[45457265]506 const errno_t err = xhci_endpoint_clear_halt(xhci_endpoint_get(halted_ep), 0);
[609f3f73]507 endpoint_del_ref(halted_ep);
[7d5287db]508 if (err) {
509 /*
510 * The endpoint halt condition failed to be cleared in HC.
511 * As it does not make sense to send the reset to the device
512 * itself, return as unschedulable answer.
513 *
514 * Furthermore, if this is a request to clear EP 0 stall, it
515 * would be gone forever, as the endpoint is halted.
516 */
517 return err;
518 }
[609f3f73]519 } else {
[8033f89]520 usb_log_warning("Device(%u): Resetting unregistered endpoint"
521 " %u %s.", xhci_dev->base.address, ep_num,
522 usb_str_direction(dir));
[609f3f73]523 }
524 }
525 }
526
527
[45457265]528 errno_t err;
[4db49344]529 fibril_mutex_lock(&xhci_ep->guard);
530
531 if ((err = endpoint_activate_locked(ep, batch))) {
532 fibril_mutex_unlock(&xhci_ep->guard);
533 return err;
534 }
[17873ac7]535
[4db49344]536 if ((err = transfer_handlers[batch->ep->transfer_type](hc, transfer))) {
[17873ac7]537 endpoint_deactivate_locked(ep);
[4db49344]538 fibril_mutex_unlock(&xhci_ep->guard);
[5fd9c30]539 return err;
[17873ac7]540 }
541
[51c1d500]542 hc_ring_ep_doorbell(xhci_ep, batch->target.stream);
[4db49344]543 fibril_mutex_unlock(&xhci_ep->guard);
[708d8fcd]544 return EOK;
[5fd9c30]545}
Note: See TracBrowser for help on using the repository browser.