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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d7869d7e was d7869d7e, checked in by Petr Manek <petr.manek@…>, 8 years ago

Reading assigned USB addresses, issuing Configure Endpoint commands to devices if needed.

  • Property mode set to 100644
File size: 9.3 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 = malloc32(batch->buffer_size);
148
149 return transfer;
150}
151
152void xhci_transfer_fini(xhci_transfer_t* transfer) {
153 if (transfer) {
154 free32(transfer->hc_buffer);
155 free(transfer);
156 }
157}
158
159int xhci_schedule_control_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch)
160{
161 if (!batch->setup_size) {
162 usb_log_error("Missing setup packet for the control transfer.");
163 return EINVAL;
164 }
165 if (batch->ep->target.endpoint != 0 || batch->ep->transfer_type != USB_TRANSFER_CONTROL) {
166 /* This method only works for control transfers. */
167 usb_log_error("Attempted to schedule control transfer to non 0 endpoint.");
168 return EINVAL;
169 }
170
171 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
172
173 uint8_t slot_id = xhci_ep->device->slot_id;
174 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[0];
175
176 usb_device_request_setup_packet_t* setup =
177 (usb_device_request_setup_packet_t*) batch->setup_buffer;
178
179 /* For the TRB formats, see xHCI specification 6.4.1.2 */
180 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
181 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
182
183 xhci_trb_t trb_setup;
184 memset(&trb_setup, 0, sizeof(xhci_trb_t));
185
186 TRB_CTRL_SET_SETUP_WVALUE(trb_setup, setup->value);
187 TRB_CTRL_SET_SETUP_WLENGTH(trb_setup, setup->length);
188 TRB_CTRL_SET_SETUP_WINDEX(trb_setup, setup->index);
189 TRB_CTRL_SET_SETUP_BREQ(trb_setup, setup->request);
190 TRB_CTRL_SET_SETUP_BMREQTYPE(trb_setup, setup->request_type);
191
192 /* Size of the setup packet is always 8 */
193 TRB_CTRL_SET_XFER_LEN(trb_setup, 8);
194 // if we want an interrupt after this td is done, use
195 // TRB_CTRL_SET_IOC(trb_setup, 1);
196
197 /* Immediate data */
198 TRB_CTRL_SET_IDT(trb_setup, 1);
199 TRB_CTRL_SET_TRB_TYPE(trb_setup, XHCI_TRB_TYPE_SETUP_STAGE);
200 TRB_CTRL_SET_TRT(trb_setup, get_transfer_type(&trb_setup, setup->request_type, setup->length));
201
202 /* Data stage */
203 xhci_trb_t trb_data;
204 memset(&trb_data, 0, sizeof(xhci_trb_t));
205
206 if (setup->length > 0) {
207 trb_data.parameter = addr_to_phys(transfer->hc_buffer);
208
209 // data size (sent for OUT, or buffer size)
210 TRB_CTRL_SET_XFER_LEN(trb_data, batch->buffer_size);
211 // FIXME: TD size 4.11.2.4
212 TRB_CTRL_SET_TD_SIZE(trb_data, 1);
213
214 // if we want an interrupt after this td is done, use
215 // TRB_CTRL_SET_IOC(trb_data, 1);
216
217 // Some more fields here, no idea what they mean
218 TRB_CTRL_SET_TRB_TYPE(trb_data, XHCI_TRB_TYPE_DATA_STAGE);
219
220 transfer->direction = get_data_direction(&trb_setup, setup->request_type, setup->length);
221 TRB_CTRL_SET_DIR(trb_data, transfer->direction);
222 }
223
224 /* Status stage */
225 xhci_trb_t trb_status;
226 memset(&trb_status, 0, sizeof(xhci_trb_t));
227
228 // FIXME: Evaluate next TRB? 4.12.3
229 // TRB_CTRL_SET_ENT(trb_status, 1);
230
231 // if we want an interrupt after this td is done, use
232 TRB_CTRL_SET_IOC(trb_status, 1);
233
234 TRB_CTRL_SET_TRB_TYPE(trb_status, XHCI_TRB_TYPE_STATUS_STAGE);
235 TRB_CTRL_SET_DIR(trb_status, get_status_direction(&trb_setup, setup->request_type, setup->length));
236
237 uintptr_t dummy = 0;
238 xhci_trb_ring_enqueue(ring, &trb_setup, &dummy);
239 if (setup->length > 0) {
240 xhci_trb_ring_enqueue(ring, &trb_data, &dummy);
241 }
242 xhci_trb_ring_enqueue(ring, &trb_status, &transfer->interrupt_trb_phys);
243
244 list_append(&transfer->link, &hc->transfers);
245
246 /* For control transfers, the target is always 1. */
247 // FIXME: ignoring return code
248 hc_ring_doorbell(hc, slot_id, 1);
249
250 // Issue a Configure Endpoint command, if needed.
251 if (configure_endpoint_needed(setup)) {
252 // TODO: figure out the best time to issue this command
253 // FIXME: ignoring return code
254 xhci_device_configure(xhci_ep->device, hc);
255 }
256
257 return EOK;
258}
259
260int xhci_schedule_bulk_transfer(xhci_hc_t* hc, usb_transfer_batch_t* batch) {
261 if (batch->setup_size) {
262 usb_log_warning("Setup packet present for a bulk transfer.");
263 }
264
265 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(batch->ep);
266 uint8_t slot_id = xhci_ep->device->slot_id;
267 xhci_trb_ring_t* ring = hc->dcbaa_virt[slot_id].trs[batch->ep->target.endpoint];
268
269 xhci_transfer_t *transfer = xhci_transfer_alloc(batch);
270 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
271
272 xhci_trb_t trb;
273 memset(&trb, 0, sizeof(xhci_trb_t));
274 trb.parameter = addr_to_phys(transfer->hc_buffer);
275
276 // data size (sent for OUT, or buffer size)
277 TRB_CTRL_SET_XFER_LEN(trb, batch->buffer_size);
278 // FIXME: TD size 4.11.2.4
279 TRB_CTRL_SET_TD_SIZE(trb, 1);
280
281 // we want an interrupt after this td is done
282 TRB_CTRL_SET_IOC(trb, 1);
283
284 TRB_CTRL_SET_TRB_TYPE(trb, XHCI_TRB_TYPE_NORMAL);
285
286 xhci_trb_ring_enqueue(ring, &trb, &transfer->interrupt_trb_phys);
287 list_append(&transfer->link, &hc->transfers);
288
289 // TODO: target = endpoint | stream_id << 16
290 hc_ring_doorbell(hc, slot_id, xhci_ep->base.target.endpoint);
291 return EOK;
292}
293
294int xhci_handle_transfer_event(xhci_hc_t* hc, xhci_trb_t* trb)
295{
296 uintptr_t addr = trb->parameter;
297 xhci_transfer_t *transfer = NULL;
298
299 link_t *transfer_link = list_first(&hc->transfers);
300 while (transfer_link) {
301 transfer = list_get_instance(transfer_link, xhci_transfer_t, link);
302
303 if (transfer->interrupt_trb_phys == addr)
304 break;
305
306 transfer_link = list_next(transfer_link, &hc->transfers);
307 }
308
309 if (!transfer_link) {
310 usb_log_warning("Transfer not found.");
311 return ENOENT;
312 }
313
314 list_remove(transfer_link);
315 usb_transfer_batch_t *batch = transfer->batch;
316
317 batch->error = (TRB_COMPLETION_CODE(*trb) == XHCI_TRBC_SUCCESS) ? EOK : ENAK;
318 batch->transfered_size = batch->buffer_size - TRB_TRANSFER_LENGTH(*trb);
319 if (transfer->direction) {
320 /* Device-to-host, IN */
321 if (batch->callback_in)
322 batch->callback_in(batch->error, batch->transfered_size, batch->arg);
323 }
324 else {
325 /* Host-to-device, OUT */
326 if (batch->callback_out)
327 batch->callback_out(batch->error, batch->arg);
328 }
329
330 xhci_transfer_fini(transfer);
331 return EOK;
332}
Note: See TracBrowser for help on using the repository browser.