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

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

xhci: cleanup

And by the way… the USB mouse is now working :)

  • Property mode set to 100644
File size: 10.4 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
96int xhci_init_transfers(xhci_hc_t *hc)
97{
98 assert(hc);
99
100 list_initialize(&hc->transfers);
101 return EOK;
102}
103
104void xhci_fini_transfers(xhci_hc_t *hc)
105{
106 // Note: Untested.
107 assert(hc);
108}
109
110xhci_transfer_t* xhci_transfer_alloc(usb_transfer_batch_t* batch) {
111 xhci_transfer_t* transfer = malloc(sizeof(xhci_transfer_t));
112 if (!transfer)
113 return NULL;
114
115 memset(transfer, 0, sizeof(xhci_transfer_t));
116 transfer->batch = batch;
117 link_initialize(&transfer->link);
118 transfer->hc_buffer = batch->buffer_size > 0 ? malloc32(batch->buffer_size) : NULL;
119
120 return transfer;
121}
122
123void xhci_transfer_fini(xhci_transfer_t* transfer) {
124 if (transfer) {
125 if (transfer->batch->buffer_size > 0)
126 free32(transfer->hc_buffer);
127
128 usb_transfer_batch_destroy(transfer->batch);
129
130 free(transfer);
131 }
132}
133
134int xhci_schedule_control_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch)
135{
136 if (!batch->setup_size) {
137 usb_log_error("Missing setup packet for the control transfer.");
138 return EINVAL;
139 }
140 if (batch->ep->transfer_type != USB_TRANSFER_CONTROL) {
141 /* This method only works for control transfers. */
142 usb_log_error("Attempted to schedule a control transfer to non control endpoint.");
143 return EINVAL;
144 }
145
146 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
147
148 uint8_t slot_id = xhci_ep->device->slot_id;
149 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->target.endpoint];
150
151 usb_device_request_setup_packet_t* setup =
152 (usb_device_request_setup_packet_t*) batch->setup_buffer;
153
154 /* For the TRB formats, see xHCI specification 6.4.1.2 */
155 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
156
157 if (!transfer->direction) {
158 // Sending stuff from host to device, we need to copy the actual data.
159 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
160 }
161
162 xhci_trb_t trb_setup;
163 xhci_trb_clean(&trb_setup);
164
165 TRB_CTRL_SET_SETUP_WVALUE(trb_setup, setup->value);
166 TRB_CTRL_SET_SETUP_WLENGTH(trb_setup, setup->length);
167 TRB_CTRL_SET_SETUP_WINDEX(trb_setup, setup->index);
168 TRB_CTRL_SET_SETUP_BREQ(trb_setup, setup->request);
169 TRB_CTRL_SET_SETUP_BMREQTYPE(trb_setup, setup->request_type);
170
171 /* Size of the setup packet is always 8 */
172 TRB_CTRL_SET_XFER_LEN(trb_setup, 8);
173 // if we want an interrupt after this td is done, use
174 // TRB_CTRL_SET_IOC(trb_setup, 1);
175
176 /* Immediate data */
177 TRB_CTRL_SET_IDT(trb_setup, 1);
178 TRB_CTRL_SET_TRB_TYPE(trb_setup, XHCI_TRB_TYPE_SETUP_STAGE);
179 TRB_CTRL_SET_TRT(trb_setup, get_transfer_type(&trb_setup, setup->request_type, setup->length));
180
181 /* Data stage */
182 xhci_trb_t trb_data;
183 xhci_trb_clean(&trb_data);
184
185 if (setup->length > 0) {
186 trb_data.parameter = addr_to_phys(transfer->hc_buffer);
187
188 // data size (sent for OUT, or buffer size)
189 TRB_CTRL_SET_XFER_LEN(trb_data, batch->buffer_size);
190 // FIXME: TD size 4.11.2.4
191 TRB_CTRL_SET_TD_SIZE(trb_data, 1);
192
193 // if we want an interrupt after this td is done, use
194 // TRB_CTRL_SET_IOC(trb_data, 1);
195
196 // Some more fields here, no idea what they mean
197 TRB_CTRL_SET_TRB_TYPE(trb_data, XHCI_TRB_TYPE_DATA_STAGE);
198
199 transfer->direction = REQUEST_TYPE_IS_DEVICE_TO_HOST(setup->request_type)
200 ? STAGE_IN : STAGE_OUT;
201 TRB_CTRL_SET_DIR(trb_data, transfer->direction);
202 }
203
204 /* Status stage */
205 xhci_trb_t trb_status;
206 xhci_trb_clean(&trb_status);
207
208 // FIXME: Evaluate next TRB? 4.12.3
209 // TRB_CTRL_SET_ENT(trb_status, 1);
210
211 // if we want an interrupt after this td is done, use
212 TRB_CTRL_SET_IOC(trb_status, 1);
213
214 TRB_CTRL_SET_TRB_TYPE(trb_status, XHCI_TRB_TYPE_STATUS_STAGE);
215 TRB_CTRL_SET_DIR(trb_status, get_status_direction_flag(&trb_setup, setup->request_type, setup->length));
216
217 uintptr_t dummy = 0;
218 xhci_trb_ring_enqueue(ring, &trb_setup, &dummy);
219 if (setup->length > 0) {
220 xhci_trb_ring_enqueue(ring, &trb_data, &dummy);
221 }
222 xhci_trb_ring_enqueue(ring, &trb_status, &transfer->interrupt_trb_phys);
223
224 list_append(&transfer->link, &hc->transfers);
225
226 // Issue a Configure Endpoint command, if needed.
227 if (configure_endpoint_needed(setup)) {
228 // TODO: figure out the best time to issue this command
229 // FIXME: ignoring return code
230 xhci_device_configure(xhci_ep->device, hc);
231 }
232
233 const uint8_t target = xhci_endpoint_index(xhci_ep) + 1; /* EP Doorbels start at 1 */
234 return hc_ring_doorbell(hc, slot_id, target);
235}
236
237int xhci_schedule_bulk_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch)
238{
239 if (batch->setup_size) {
240 usb_log_warning("Setup packet present for a bulk transfer.");
241 }
242
243 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
244 uint8_t slot_id = xhci_ep->device->slot_id;
245 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->target.endpoint];
246
247 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
248 if (!transfer->direction) {
249 // Sending stuff from host to device, we need to copy the actual data.
250 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
251 }
252
253 xhci_trb_t trb;
254 xhci_trb_clean(&trb);
255 trb.parameter = addr_to_phys(transfer->hc_buffer);
256
257 // data size (sent for OUT, or buffer size)
258 TRB_CTRL_SET_XFER_LEN(trb, batch->buffer_size);
259 // FIXME: TD size 4.11.2.4
260 TRB_CTRL_SET_TD_SIZE(trb, 1);
261
262 // we want an interrupt after this td is done
263 TRB_CTRL_SET_IOC(trb, 1);
264
265 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
266
267 xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
268 list_append(&transfer->link, &hc->transfers);
269
270 // TODO: target = endpoint | stream_id << 16
271 const uint8_t target = xhci_endpoint_index(xhci_ep) + 1; /* EP Doorbells start at 1 */
272 return hc_ring_doorbell(hc, slot_id, target);
273}
274
275int xhci_schedule_interrupt_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch)
276{
277 if (batch->setup_size) {
278 usb_log_warning("Setup packet present for a interrupt transfer.");
279 }
280
281 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
282 uint8_t slot_id = xhci_ep->device->slot_id;
283 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->target.endpoint];
284
285 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
286 if (!transfer->direction) {
287 // Sending stuff from host to device, we need to copy the actual data.
288 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
289 }
290
291 xhci_trb_t trb;
292 xhci_trb_clean(&trb);
293 trb.parameter = addr_to_phys(transfer->hc_buffer);
294
295 // data size (sent for OUT, or buffer size)
296 TRB_CTRL_SET_XFER_LEN(trb, batch->buffer_size);
297 // FIXME: TD size 4.11.2.4
298 TRB_CTRL_SET_TD_SIZE(trb, 1);
299
300 // we want an interrupt after this td is done
301 TRB_CTRL_SET_IOC(trb, 1);
302
303 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
304
305 xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
306 list_append(&transfer->link, &hc->transfers);
307
308 const uint8_t target = xhci_endpoint_index(xhci_ep) + 1; /* EP Doorbells start at 1 */
309 return hc_ring_doorbell(hc, slot_id, target);
310}
311
312int xhci_handle_transfer_event(xhci_hc_t* hc, xhci_trb_t* trb)
313{
314 uintptr_t addr = trb->parameter;
315 xhci_transfer_t *transfer = NULL;
316
317 link_t *transfer_link = list_first(&hc->transfers);
318 while (transfer_link) {
319 transfer = list_get_instance(transfer_link, xhci_transfer_t, link);
320
321 if (transfer->interrupt_trb_phys == addr)
322 break;
323
324 transfer_link = list_next(transfer_link, &hc->transfers);
325 }
326
327 if (!transfer_link) {
328 usb_log_warning("Transfer not found.");
329 return ENOENT;
330 }
331
332 list_remove(transfer_link);
333 usb_transfer_batch_t *batch = transfer->batch;
334
335 const int err = (TRB_COMPLETION_CODE(*trb) == XHCI_TRBC_SUCCESS) ? EOK : ENAK;
336 const size_t size = batch->buffer_size - TRB_TRANSFER_LENGTH(*trb);
337 usb_transfer_batch_finish_error(batch, transfer->hc_buffer, size, err);
338 xhci_transfer_fini(transfer);
339 return EOK;
340}
Note: See TracBrowser for help on using the repository browser.