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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 479e32d was 17873ac7, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost endpoint: endpoint→active replaced by tracking active batch

The mechanism is optional, synchronization over endpoint is now not forced. It will be used by xhci to utilize streams.

  • Property mode set to 100644
File size: 10.6 KB
Line 
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/host/utils/malloc32.h>
37#include <usb/debug.h>
38#include <usb/request.h>
39#include "endpoint.h"
40#include "hc.h"
41#include "hw_struct/trb.h"
42#include "transfers.h"
43#include "trb_ring.h"
44
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
53
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)
59{
60 /* See Table 7 of xHCI specification */
61 return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType) && (wLength > 0)
62 ? STAGE_OUT
63 : STAGE_IN;
64}
65
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)
77{
78 if (wLength == 0)
79 return DATA_STAGE_NO;
80
81 /* See Table 7 of xHCI specification */
82 return REQUEST_TYPE_IS_DEVICE_TO_HOST(bmRequestType)
83 ? DATA_STAGE_IN
84 : DATA_STAGE_NO;
85}
86
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
91 return request_type == USB_REQUEST_TYPE_STANDARD &&
92 (setup->request == USB_DEVREQ_SET_CONFIGURATION
93 || setup->request == USB_DEVREQ_SET_INTERFACE);
94}
95
96/**
97 * There can currently be only one active transfer, because
98 * usb_transfer_batch_init locks the endpoint by endpoint_use.
99 * Therefore, we store the only active transfer per endpoint there.
100 */
101xhci_transfer_t* xhci_transfer_create(endpoint_t* ep)
102{
103 xhci_transfer_t *transfer = calloc(1, sizeof(xhci_transfer_t));
104 if (!transfer)
105 return NULL;
106
107 usb_transfer_batch_init(&transfer->batch, ep);
108 return transfer;
109}
110
111void xhci_transfer_destroy(xhci_transfer_t* transfer)
112{
113 assert(transfer);
114
115 if (transfer->hc_buffer)
116 free32(transfer->hc_buffer);
117}
118
119static xhci_trb_ring_t *get_ring(xhci_hc_t *hc, xhci_transfer_t *transfer)
120{
121 return &xhci_endpoint_get(transfer->batch.ep)->ring;
122}
123
124static int schedule_control(xhci_hc_t* hc, xhci_transfer_t* transfer)
125{
126 usb_transfer_batch_t *batch = &transfer->batch;
127 xhci_trb_ring_t *ring = get_ring(hc, transfer);
128 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(transfer->batch.ep);
129
130 usb_device_request_setup_packet_t* setup = &batch->setup.packet;
131
132 xhci_trb_t trbs[3];
133 int trbs_used = 0;
134
135 xhci_trb_t *trb_setup = trbs + trbs_used++;
136 xhci_trb_clean(trb_setup);
137
138 TRB_CTRL_SET_SETUP_WVALUE(*trb_setup, setup->value);
139 TRB_CTRL_SET_SETUP_WLENGTH(*trb_setup, setup->length);
140 TRB_CTRL_SET_SETUP_WINDEX(*trb_setup, setup->index);
141 TRB_CTRL_SET_SETUP_BREQ(*trb_setup, setup->request);
142 TRB_CTRL_SET_SETUP_BMREQTYPE(*trb_setup, setup->request_type);
143
144 /* Size of the setup packet is always 8 */
145 TRB_CTRL_SET_XFER_LEN(*trb_setup, 8);
146
147 /* Immediate data */
148 TRB_CTRL_SET_IDT(*trb_setup, 1);
149 TRB_CTRL_SET_TRB_TYPE(*trb_setup, XHCI_TRB_TYPE_SETUP_STAGE);
150 TRB_CTRL_SET_TRT(*trb_setup, get_transfer_type(trb_setup, setup->request_type, setup->length));
151
152 /* Data stage */
153 xhci_trb_t *trb_data = NULL;
154 if (setup->length > 0) {
155 trb_data = trbs + trbs_used++;
156 xhci_trb_clean(trb_data);
157
158 trb_data->parameter = addr_to_phys(transfer->hc_buffer);
159
160 // data size (sent for OUT, or buffer size)
161 TRB_CTRL_SET_XFER_LEN(*trb_data, batch->buffer_size);
162 // FIXME: TD size 4.11.2.4
163 TRB_CTRL_SET_TD_SIZE(*trb_data, 1);
164
165 // Some more fields here, no idea what they mean
166 TRB_CTRL_SET_TRB_TYPE(*trb_data, XHCI_TRB_TYPE_DATA_STAGE);
167
168 int stage_dir = REQUEST_TYPE_IS_DEVICE_TO_HOST(setup->request_type)
169 ? STAGE_IN : STAGE_OUT;
170 TRB_CTRL_SET_DIR(*trb_data, stage_dir);
171 }
172
173 /* Status stage */
174 xhci_trb_t *trb_status = trbs + trbs_used++;
175 xhci_trb_clean(trb_status);
176
177 // FIXME: Evaluate next TRB? 4.12.3
178 // TRB_CTRL_SET_ENT(*trb_status, 1);
179
180 TRB_CTRL_SET_IOC(*trb_status, 1);
181 TRB_CTRL_SET_TRB_TYPE(*trb_status, XHCI_TRB_TYPE_STATUS_STAGE);
182 TRB_CTRL_SET_DIR(*trb_status, get_status_direction_flag(trb_setup, setup->request_type, setup->length));
183
184 // Issue a Configure Endpoint command, if needed.
185 if (configure_endpoint_needed(setup)) {
186 const int err = hc_configure_device(hc, xhci_ep_to_dev(xhci_ep)->slot_id);
187 if (err)
188 return err;
189 }
190
191 return xhci_trb_ring_enqueue_multiple(ring, trbs, trbs_used, &transfer->interrupt_trb_phys);
192}
193
194static int schedule_bulk(xhci_hc_t* hc, xhci_transfer_t *transfer)
195{
196 xhci_trb_t trb;
197 xhci_trb_clean(&trb);
198 trb.parameter = addr_to_phys(transfer->hc_buffer);
199
200 // data size (sent for OUT, or buffer size)
201 TRB_CTRL_SET_XFER_LEN(trb, transfer->batch.buffer_size);
202 // FIXME: TD size 4.11.2.4
203 TRB_CTRL_SET_TD_SIZE(trb, 1);
204
205 // we want an interrupt after this td is done
206 TRB_CTRL_SET_IOC(trb, 1);
207
208 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
209
210 xhci_trb_ring_t* ring = get_ring(hc, transfer);
211
212 return xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
213}
214
215static int schedule_interrupt(xhci_hc_t* hc, xhci_transfer_t* transfer)
216{
217 xhci_trb_t trb;
218 xhci_trb_clean(&trb);
219 trb.parameter = addr_to_phys(transfer->hc_buffer);
220
221 // data size (sent for OUT, or buffer size)
222 TRB_CTRL_SET_XFER_LEN(trb, transfer->batch.buffer_size);
223 // FIXME: TD size 4.11.2.4
224 TRB_CTRL_SET_TD_SIZE(trb, 1);
225
226 // we want an interrupt after this td is done
227 TRB_CTRL_SET_IOC(trb, 1);
228
229 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
230
231 xhci_trb_ring_t* ring = get_ring(hc, transfer);
232
233 return xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
234}
235
236static int schedule_isochronous(xhci_hc_t* hc, xhci_transfer_t* transfer)
237{
238 /* TODO: Implement me. */
239 usb_log_error("Isochronous transfers are not yet implemented!");
240 return ENOTSUP;
241}
242
243int xhci_handle_transfer_event(xhci_hc_t* hc, xhci_trb_t* trb)
244{
245 uintptr_t addr = trb->parameter;
246 const unsigned slot_id = XHCI_DWORD_EXTRACT(trb->control, 31, 24);
247 const unsigned ep_dci = XHCI_DWORD_EXTRACT(trb->control, 20, 16);
248
249 xhci_device_t *dev = hc->bus.devices_by_slot[slot_id];
250 if (!dev) {
251 usb_log_error("Transfer event on disabled slot %u", slot_id);
252 return ENOENT;
253 }
254
255 const usb_endpoint_t ep_num = ep_dci / 2;
256 xhci_endpoint_t *ep = xhci_device_get_endpoint(dev, ep_num);
257 if (!ep) {
258 usb_log_error("Transfer event on dropped endpoint %u of device "
259 XHCI_DEV_FMT, ep_num, XHCI_DEV_ARGS(*dev));
260 return ENOENT;
261 }
262
263 /* FIXME: This is racy. Do we care? */
264 ep->ring.dequeue = addr;
265
266 fibril_mutex_lock(&ep->base.guard);
267 usb_transfer_batch_t *batch = ep->base.active_batch;
268 if (!batch) {
269 fibril_mutex_unlock(&ep->base.guard);
270 return ENOENT;
271 }
272
273 batch->error = (TRB_COMPLETION_CODE(*trb) == XHCI_TRBC_SUCCESS) ? EOK : ENAK;
274 batch->transfered_size = batch->buffer_size - TRB_TRANSFER_LENGTH(*trb);
275 usb_transfer_batch_reset_toggle(batch);
276 endpoint_deactivate_locked(&ep->base);
277 fibril_mutex_unlock(&ep->base.guard);
278
279 xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
280
281 if (batch->dir == USB_DIRECTION_IN) {
282 assert(batch->buffer);
283 assert(batch->transfered_size <= batch->buffer_size);
284 memcpy(batch->buffer, transfer->hc_buffer, batch->transfered_size);
285 }
286
287 usb_transfer_batch_finish(batch);
288 return EOK;
289}
290
291typedef int (*transfer_handler)(xhci_hc_t *, xhci_transfer_t *);
292
293static const transfer_handler transfer_handlers[] = {
294 [USB_TRANSFER_CONTROL] = schedule_control,
295 [USB_TRANSFER_ISOCHRONOUS] = schedule_isochronous,
296 [USB_TRANSFER_BULK] = schedule_bulk,
297 [USB_TRANSFER_INTERRUPT] = schedule_interrupt,
298};
299
300int xhci_transfer_schedule(xhci_hc_t *hc, usb_transfer_batch_t *batch)
301{
302 assert(hc);
303 endpoint_t *ep = batch->ep;
304
305 xhci_transfer_t *transfer = xhci_transfer_from_batch(batch);
306 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
307 xhci_device_t *xhci_dev = xhci_ep_to_dev(xhci_ep);
308
309 /* Offline devices don't schedule transfers other than on EP0. */
310 if (!xhci_dev->online && ep->endpoint > 0) {
311 return EAGAIN;
312 }
313
314 // FIXME: find a better way to check if the ring is not initialized
315 if (!xhci_ep->ring.segment_count) {
316 usb_log_error("Ring not initialized for endpoint " XHCI_EP_FMT,
317 XHCI_EP_ARGS(*xhci_ep));
318 return EINVAL;
319 }
320
321 const usb_transfer_type_t type = batch->ep->transfer_type;
322 assert(type >= 0 && type < ARRAY_SIZE(transfer_handlers));
323 assert(transfer_handlers[type]);
324
325 if (batch->buffer_size > 0) {
326 transfer->hc_buffer = malloc32(batch->buffer_size);
327 if (!transfer->hc_buffer)
328 return ENOMEM;
329 }
330
331 if (batch->dir != USB_DIRECTION_IN) {
332 // Sending stuff from host to device, we need to copy the actual data.
333 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
334 }
335
336 fibril_mutex_lock(&ep->guard);
337 endpoint_activate_locked(ep, batch);
338 const int err = transfer_handlers[batch->ep->transfer_type](hc, transfer);
339
340 if (err) {
341 endpoint_deactivate_locked(ep);
342 fibril_mutex_unlock(&ep->guard);
343 return err;
344 }
345
346 /* After the critical section, the transfer can already be finished or aborted. */
347 transfer = NULL; batch = NULL;
348 fibril_mutex_unlock(&ep->guard);
349
350 const uint8_t slot_id = xhci_dev->slot_id;
351 const uint8_t target = xhci_endpoint_index(xhci_ep) + 1; /* EP Doorbells start at 1 */
352 return hc_ring_doorbell(hc, slot_id, target);
353}
Note: See TracBrowser for help on using the repository browser.