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

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

xhci: fix crashing QEMU

  • Property mode set to 100644
File size: 11.0 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
45static inline uint8_t get_transfer_type(xhci_trb_t* trb, uint8_t bmRequestType, uint16_t wLength)
46{
47 /* See Table 7 of xHCI specification */
48 if (bmRequestType & 0x80) {
49 /* Device-to-host transfer */
50 if (wLength) {
51 /* IN data stage */
52 return 3;
53 }
54 else {
55 /* No data stage */
56 return 0;
57 }
58 }
59 else {
60 /* Host-to-device transfer */
61 if (wLength) {
62 /* OUT data stage */
63 return 2;
64 }
65 else {
66 /* No data stage */
67 return 0;
68 }
69 }
70}
71
72static inline uint8_t get_data_direction(xhci_trb_t* trb, uint8_t bmRequestType, uint16_t wLength)
73{
74 /* See Table 7 of xHCI specification */
75 if (bmRequestType & 0x80) {
76 /* Device-to-host transfer */
77 return 1;
78 }
79 else {
80 /* Host-to-device transfer */
81 return 0;
82 }
83}
84
85static inline uint8_t get_status_direction(xhci_trb_t* trb, uint8_t bmRequestType, uint16_t wLength)
86{
87 /* See Table 7 of xHCI specification */
88 if (bmRequestType & 0x80) {
89 /* Device-to-host transfer */
90 if (wLength) {
91 /* Out direction */
92 return 0;
93 }
94 else {
95 /* In direction */
96 return 1;
97 }
98 }
99 else {
100 /* Host-to-device transfer, always IN direction */
101 return 1;
102 }
103}
104
105static inline bool configure_endpoint_needed(usb_device_request_setup_packet_t *setup)
106{
107 usb_request_type_t request_type = SETUP_REQUEST_TYPE_GET_TYPE(setup->request_type);
108
109 if (request_type == USB_REQUEST_TYPE_STANDARD) {
110 usb_stddevreq_t request = setup->request;
111
112 switch (request) {
113 case USB_DEVREQ_SET_CONFIGURATION:
114 case USB_DEVREQ_SET_INTERFACE:
115 return true;
116
117 default:
118 return false;
119 }
120 }
121
122 return false;
123}
124
125int xhci_init_transfers(xhci_hc_t *hc)
126{
127 assert(hc);
128
129 list_initialize(&hc->transfers);
130 return EOK;
131}
132
133void xhci_fini_transfers(xhci_hc_t *hc)
134{
135 // Note: Untested.
136 assert(hc);
137}
138
139xhci_transfer_t* xhci_transfer_alloc(usb_transfer_batch_t* batch) {
140 xhci_transfer_t* transfer = malloc(sizeof(xhci_transfer_t));
141 if (!transfer)
142 return NULL;
143
144 memset(transfer, 0, sizeof(xhci_transfer_t));
145 transfer->batch = batch;
146 link_initialize(&transfer->link);
147 transfer->hc_buffer = batch->buffer_size > 0 ? malloc32(batch->buffer_size) : NULL;
148
149 return transfer;
150}
151
152void xhci_transfer_fini(xhci_transfer_t* transfer) {
153 if (transfer) {
154 if (transfer->batch->buffer_size > 0)
155 free32(transfer->hc_buffer);
156
157 usb_transfer_batch_destroy(transfer->batch);
158
159 free(transfer);
160 }
161}
162
163int xhci_schedule_control_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch)
164{
165 if (!batch->setup_size) {
166 usb_log_error("Missing setup packet for the control transfer.");
167 return EINVAL;
168 }
169 if (batch->ep->target.endpoint != 0 || batch->ep->transfer_type != USB_TRANSFER_CONTROL) {
170 /* This method only works for control transfers. */
171 usb_log_error("Attempted to schedule control transfer to non 0 endpoint.");
172 return EINVAL;
173 }
174
175 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
176
177 uint8_t slot_id = xhci_ep->device->slot_id;
178 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[0];
179
180 usb_device_request_setup_packet_t* setup =
181 (usb_device_request_setup_packet_t*) batch->setup_buffer;
182
183 /* For the TRB formats, see xHCI specification 6.4.1.2 */
184 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
185
186 if (!transfer->direction) {
187 // Sending stuff from host to device, we need to copy the actual data.
188 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
189 }
190
191 xhci_trb_t trb_setup;
192 memset(&trb_setup, 0, sizeof(xhci_trb_t));
193
194 TRB_CTRL_SET_SETUP_WVALUE(trb_setup, setup->value);
195 TRB_CTRL_SET_SETUP_WLENGTH(trb_setup, setup->length);
196 TRB_CTRL_SET_SETUP_WINDEX(trb_setup, setup->index);
197 TRB_CTRL_SET_SETUP_BREQ(trb_setup, setup->request);
198 TRB_CTRL_SET_SETUP_BMREQTYPE(trb_setup, setup->request_type);
199
200 /* Size of the setup packet is always 8 */
201 TRB_CTRL_SET_XFER_LEN(trb_setup, 8);
202 // if we want an interrupt after this td is done, use
203 // TRB_CTRL_SET_IOC(trb_setup, 1);
204
205 /* Immediate data */
206 TRB_CTRL_SET_IDT(trb_setup, 1);
207 TRB_CTRL_SET_TRB_TYPE(trb_setup, XHCI_TRB_TYPE_SETUP_STAGE);
208 TRB_CTRL_SET_TRT(trb_setup, get_transfer_type(&trb_setup, setup->request_type, setup->length));
209
210 /* Data stage */
211 xhci_trb_t trb_data;
212 memset(&trb_data, 0, sizeof(xhci_trb_t));
213
214 if (setup->length > 0) {
215 trb_data.parameter = addr_to_phys(transfer->hc_buffer);
216
217 // data size (sent for OUT, or buffer size)
218 TRB_CTRL_SET_XFER_LEN(trb_data, batch->buffer_size);
219 // FIXME: TD size 4.11.2.4
220 TRB_CTRL_SET_TD_SIZE(trb_data, 1);
221
222 // if we want an interrupt after this td is done, use
223 // TRB_CTRL_SET_IOC(trb_data, 1);
224
225 // Some more fields here, no idea what they mean
226 TRB_CTRL_SET_TRB_TYPE(trb_data, XHCI_TRB_TYPE_DATA_STAGE);
227
228 transfer->direction = get_data_direction(&trb_setup, setup->request_type, setup->length);
229 TRB_CTRL_SET_DIR(trb_data, transfer->direction);
230 }
231
232 /* Status stage */
233 xhci_trb_t trb_status;
234 memset(&trb_status, 0, sizeof(xhci_trb_t));
235
236 // FIXME: Evaluate next TRB? 4.12.3
237 // TRB_CTRL_SET_ENT(trb_status, 1);
238
239 // if we want an interrupt after this td is done, use
240 TRB_CTRL_SET_IOC(trb_status, 1);
241
242 TRB_CTRL_SET_TRB_TYPE(trb_status, XHCI_TRB_TYPE_STATUS_STAGE);
243 TRB_CTRL_SET_DIR(trb_status, get_status_direction(&trb_setup, setup->request_type, setup->length));
244
245 uintptr_t dummy = 0;
246 xhci_trb_ring_enqueue(ring, &trb_setup, &dummy);
247 if (setup->length > 0) {
248 xhci_trb_ring_enqueue(ring, &trb_data, &dummy);
249 }
250 xhci_trb_ring_enqueue(ring, &trb_status, &transfer->interrupt_trb_phys);
251
252 list_append(&transfer->link, &hc->transfers);
253
254 /* For control transfers, the target is always 1. */
255 // FIXME: ignoring return code
256 hc_ring_doorbell(hc, slot_id, 1);
257
258 // Issue a Configure Endpoint command, if needed.
259 if (configure_endpoint_needed(setup)) {
260 // TODO: figure out the best time to issue this command
261 // FIXME: ignoring return code
262 xhci_device_configure(xhci_ep->device, hc);
263 }
264
265 return EOK;
266}
267
268int xhci_schedule_bulk_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch)
269{
270 if (batch->setup_size) {
271 usb_log_warning("Setup packet present for a bulk transfer.");
272 }
273
274 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
275 uint8_t slot_id = xhci_ep->device->slot_id;
276 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->target.endpoint];
277
278 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
279 if (!transfer->direction) {
280 // Sending stuff from host to device, we need to copy the actual data.
281 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
282 }
283
284 xhci_trb_t trb;
285 memset(&trb, 0, sizeof(xhci_trb_t));
286 trb.parameter = addr_to_phys(transfer->hc_buffer);
287
288 // data size (sent for OUT, or buffer size)
289 TRB_CTRL_SET_XFER_LEN(trb, batch->buffer_size);
290 // FIXME: TD size 4.11.2.4
291 TRB_CTRL_SET_TD_SIZE(trb, 1);
292
293 // we want an interrupt after this td is done
294 TRB_CTRL_SET_IOC(trb, 1);
295
296 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
297
298 xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
299 list_append(&transfer->link, &hc->transfers);
300
301 // TODO: target = endpoint | stream_id << 16
302 hc_ring_doorbell(hc, slot_id, xhci_ep->base.target.endpoint);
303 return EOK;
304}
305
306int xhci_schedule_interrupt_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch)
307{
308 if (batch->setup_size) {
309 usb_log_warning("Setup packet present for a interrupt transfer.");
310 }
311
312 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
313 uint8_t slot_id = xhci_ep->device->slot_id;
314 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->target.endpoint];
315
316 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
317 if (!transfer->direction) {
318 // Sending stuff from host to device, we need to copy the actual data.
319 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
320 }
321
322 xhci_trb_t trb;
323 memset(&trb, 0, sizeof(xhci_trb_t));
324 trb.parameter = addr_to_phys(transfer->hc_buffer);
325
326 // data size (sent for OUT, or buffer size)
327 TRB_CTRL_SET_XFER_LEN(trb, batch->buffer_size);
328 // FIXME: TD size 4.11.2.4
329 TRB_CTRL_SET_TD_SIZE(trb, 1);
330
331 // we want an interrupt after this td is done
332 TRB_CTRL_SET_IOC(trb, 1);
333
334 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
335
336 xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
337 list_append(&transfer->link, &hc->transfers);
338
339 const uint8_t target = 2 * batch->ep->target.endpoint
340 + (batch->ep->direction == USB_DIRECTION_IN ? 1 : 0);
341 usb_log_debug("Ringing doorbell for slot_id = %d, target = %d", slot_id, target);
342 return hc_ring_doorbell(hc, slot_id, target);
343}
344
345int xhci_handle_transfer_event(xhci_hc_t* hc, xhci_trb_t* trb)
346{
347 uintptr_t addr = trb->parameter;
348 xhci_transfer_t *transfer = NULL;
349
350 link_t *transfer_link = list_first(&hc->transfers);
351 while (transfer_link) {
352 transfer = list_get_instance(transfer_link, xhci_transfer_t, link);
353
354 if (transfer->interrupt_trb_phys == addr)
355 break;
356
357 transfer_link = list_next(transfer_link, &hc->transfers);
358 }
359
360 if (!transfer_link) {
361 usb_log_warning("Transfer not found.");
362 return ENOENT;
363 }
364
365 list_remove(transfer_link);
366 usb_transfer_batch_t *batch = transfer->batch;
367
368 batch->error = (TRB_COMPLETION_CODE(*trb) == XHCI_TRBC_SUCCESS) ? EOK : ENAK;
369 batch->transfered_size = batch->buffer_size - TRB_TRANSFER_LENGTH(*trb);
370 if (transfer->direction) {
371 memcpy(batch->buffer, transfer->hc_buffer, batch->buffer_size);
372
373 /* Device-to-host, IN */
374 if (batch->callback_in)
375 batch->callback_in(batch->error, batch->transfered_size, batch->arg);
376 }
377 else {
378 /* Host-to-device, OUT */
379 if (batch->callback_out)
380 batch->callback_out(batch->error, batch->arg);
381 }
382
383 xhci_transfer_fini(transfer);
384 return EOK;
385}
Note: See TracBrowser for help on using the repository browser.