source: mainline/uspace/drv/bus/usb/ehci/ehci_batch.c@ 2ca5a198

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

usb: fix some errors

  • Property mode set to 100644
File size: 12.7 KB
RevLine 
[8a81e431]1/*
2 * Copyright (c) 2014 Jan Vesely
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/** @addtogroup drvusbehci
29 * @{
30 */
31/** @file
32 * @brief EHCI driver USB transaction structure
33 */
34
35#include <assert.h>
36#include <errno.h>
37#include <macros.h>
38#include <mem.h>
39#include <stdbool.h>
[6ef69e9]40#include <str_error.h>
[8a81e431]41
42#include <usb/usb.h>
43#include <usb/debug.h>
44
45#include "ehci_batch.h"
[741bcdeb]46#include "ehci_bus.h"
[8a81e431]47
[6602e97]48/* The buffer pointer list in the qTD is long enough to support a maximum
49 * transfer size of 20K bytes. This case occurs when all five buffer pointers
50 * are used and the first offset is zero. A qTD handles a 16Kbyte buffer
51 * with any starting buffer alignment. EHCI specs p. 87 (pdf p. 97) */
52#define EHCI_TD_MAX_TRANSFER (16 * 1024)
53
[5fd9c30]54static void (*const batch_setup[])(ehci_transfer_batch_t*);
[8a81e431]55
56/** Safely destructs ehci_transfer_batch_t structure
57 *
58 * @param[in] ehci_batch Instance to destroy.
59 */
[5fd9c30]60void ehci_transfer_batch_destroy(ehci_transfer_batch_t *ehci_batch)
[8a81e431]61{
[5fd9c30]62 assert(ehci_batch);
[35c37fc]63 dma_buffer_free(&ehci_batch->dma_buffer);
[090eea68]64 usb_log_debug2("Batch(%p): disposed", ehci_batch);
[806a779]65 free(ehci_batch);
[8a81e431]66}
67
68/** Allocate memory and initialize internal data structure.
69 *
70 * @param[in] usb_batch Pointer to generic USB batch structure.
71 * @return Valid pointer if all structures were successfully created,
72 * NULL otherwise.
73 *
74 */
[5fd9c30]75ehci_transfer_batch_t * ehci_transfer_batch_create(endpoint_t *ep)
[8a81e431]76{
[5fd9c30]77 assert(ep);
[8a81e431]78
[5fd9c30]79 ehci_transfer_batch_t *ehci_batch = calloc(1, sizeof(ehci_transfer_batch_t));
[8a81e431]80 if (!ehci_batch) {
[5fd9c30]81 usb_log_error("Failed to allocate EHCI batch data.");
82 return NULL;
[8a81e431]83 }
[5fd9c30]84
85 usb_transfer_batch_init(&ehci_batch->base, ep);
86
87 return ehci_batch;
88}
89
90/** Prepares a batch to be sent.
91 *
92 * Determines the number of needed transfer descriptors (TDs).
93 * Prepares a transport buffer (that is accessible by the hardware).
94 * Initializes parameters needed for the transfer and callback.
95 */
96int ehci_transfer_batch_prepare(ehci_transfer_batch_t *ehci_batch)
97{
98 assert(ehci_batch);
99
100 const size_t setup_size = (ehci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL)
101 ? USB_SETUP_PACKET_SIZE
102 : 0;
103
104 const size_t size = ehci_batch->base.buffer_size;
105
106 /* Add TD left over by the previous transfer */
107 ehci_batch->qh = ehci_endpoint_get(ehci_batch->base.ep)->qh;
108
109 /* Determine number of TDs needed */
110 ehci_batch->td_count = (size + EHCI_TD_MAX_TRANSFER - 1)
111 / EHCI_TD_MAX_TRANSFER;
[6602e97]112
[8a81e431]113 /* Control transfer need Setup and Status stage */
[5fd9c30]114 if (ehci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL) {
[8a81e431]115 ehci_batch->td_count += 2;
116 }
117
[35c37fc]118 const size_t tds_size = ehci_batch->td_count * sizeof(td_t);
[8a81e431]119
[35c37fc]120 /* Mix setup stage, data and TDs together, we have enough space */
121 if (size + setup_size + tds_size > 0) {
122 if (dma_buffer_alloc(&ehci_batch->dma_buffer, tds_size + setup_size + size)) {
123 usb_log_error("Batch %p: Failed to allocate device "
124 "buffer", ehci_batch);
[5fd9c30]125 return ENOMEM;
[8a81e431]126 }
[35c37fc]127 /* Clean TDs */
128 ehci_batch->tds = ehci_batch->dma_buffer.virt;
129 memset(ehci_batch->tds, 0, tds_size);
130 /* Copy setup data */
131 ehci_batch->setup_buffer = ehci_batch->dma_buffer.virt + tds_size;
132 memcpy(ehci_batch->setup_buffer, ehci_batch->base.setup.buffer, setup_size);
133 /* Copy generic data */
134 ehci_batch->data_buffer = ehci_batch->setup_buffer + setup_size;
135 if (ehci_batch->base.dir != USB_DIRECTION_IN)
136 memcpy(ehci_batch->data_buffer,
137 ehci_batch->base.buffer,
138 ehci_batch->base.buffer_size);
[8a81e431]139 }
140
[2ca5a198]141 if (!batch_setup[ehci_batch->base.ep->transfer_type])
142 return ENOTSUP;
143
[5fd9c30]144 batch_setup[ehci_batch->base.ep->transfer_type](ehci_batch);
[8a81e431]145
[a1732929]146 usb_log_debug("Batch %p %s " USB_TRANSFER_BATCH_FMT " initialized.",
[5fd9c30]147 ehci_batch, usb_str_direction(ehci_batch->base.dir),
148 USB_TRANSFER_BATCH_ARGS(ehci_batch->base));
[6ef69e9]149
[5fd9c30]150 return EOK;
[8a81e431]151}
152
153/** Check batch TDs' status.
154 *
155 * @param[in] ehci_batch Batch structure to use.
156 * @return False, if there is an active TD, true otherwise.
157 *
158 * Walk all TDs (usually there is just one). Stop with false if there is an
159 * active TD. Stop with true if an error is found. Return true if the walk
160 * completes with the last TD.
161 */
[5fd9c30]162bool ehci_transfer_batch_check_completed(ehci_transfer_batch_t *ehci_batch)
[8a81e431]163{
164 assert(ehci_batch);
165
[a1732929]166 usb_log_debug("Batch %p: checking %zu td(s) for completion.",
[5fd9c30]167 ehci_batch, ehci_batch->td_count);
[8a81e431]168
[a1732929]169 usb_log_debug2("Batch %p: QH: %08x:%08x:%08x:%08x:%08x:%08x.",
[5fd9c30]170 ehci_batch,
[6602e97]171 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
172 ehci_batch->qh->status, ehci_batch->qh->current,
173 ehci_batch->qh->next, ehci_batch->qh->alternate);
174
[5c830587]175 if (!qh_halted(ehci_batch->qh) && (qh_transfer_pending(ehci_batch->qh)
176 || qh_transfer_active(ehci_batch->qh)))
[8a81e431]177 return false;
178
179 /* Now we may be sure that either the ED is inactive because of errors
180 * or all transfer descriptors completed successfully */
181
182 /* Assume all data got through */
[db51a6a6]183 ehci_batch->base.transferred_size = ehci_batch->base.buffer_size;
[8a81e431]184
185 /* Check all TDs */
186 for (size_t i = 0; i < ehci_batch->td_count; ++i) {
[090eea68]187 usb_log_debug("Batch %p: TD %zu: %08x:%08x:%08x.",
[5fd9c30]188 ehci_batch, i,
[35c37fc]189 ehci_batch->tds[i].status, ehci_batch->tds[i].next,
190 ehci_batch->tds[i].alternate);
[a752c78c]191
[35c37fc]192 ehci_batch->base.error = td_error(&ehci_batch->tds[i]);
[5fd9c30]193 if (ehci_batch->base.error == EOK) {
[8a81e431]194 /* If the TD got all its data through, it will report
195 * 0 bytes remain, the sole exception is INPUT with
196 * data rounding flag (short), i.e. every INPUT.
197 * Nice thing is that short packets will correctly
198 * report remaining data, thus making this computation
199 * correct (short packets need to be produced by the
200 * last TD)
201 * NOTE: This also works for CONTROL transfer as
202 * the first TD will return 0 remain.
203 * NOTE: Short packets don't break the assumption that
204 * we leave the very last(unused) TD behind.
205 */
[db51a6a6]206 ehci_batch->base.transferred_size
[35c37fc]207 -= td_remain_size(&ehci_batch->tds[i]);
[8a81e431]208 } else {
[c1694b6b]209 usb_log_debug("Batch %p found error TD(%zu):%08x: %s.",
[5fd9c30]210 ehci_batch, i,
[35c37fc]211 ehci_batch->tds[i].status,
[132ab5d1]212 str_error_name(ehci_batch->base.error));
[8a81e431]213 /* Clear possible ED HALT */
[a752c78c]214 qh_clear_halt(ehci_batch->qh);
[8a81e431]215 break;
216 }
217 }
[6602e97]218
[db51a6a6]219 assert(ehci_batch->base.transferred_size <= ehci_batch->base.buffer_size);
[5fd9c30]220
221 if (ehci_batch->base.dir == USB_DIRECTION_IN)
[27b0ea0]222 memcpy(ehci_batch->base.buffer,
[35c37fc]223 ehci_batch->data_buffer,
[db51a6a6]224 ehci_batch->base.transferred_size);
[5fd9c30]225
[a752c78c]226 /* Clear TD pointers */
227 ehci_batch->qh->next = LINK_POINTER_TERM;
228 ehci_batch->qh->current = LINK_POINTER_TERM;
[5fd9c30]229 usb_log_debug("Batch %p complete: %s", ehci_batch,
230 str_error(ehci_batch->base.error));
[8a81e431]231
232 return true;
233}
234
235/** Starts execution of the TD list
236 *
237 * @param[in] ehci_batch Batch structure to use
238 */
239void ehci_transfer_batch_commit(const ehci_transfer_batch_t *ehci_batch)
240{
241 assert(ehci_batch);
[35c37fc]242 qh_set_next_td(ehci_batch->qh, dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[0]));
[8a81e431]243}
244
245/** Prepare generic control transfer
246 *
247 * @param[in] ehci_batch Batch structure to use.
248 *
249 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
[5fd9c30]250 * Data stage with alternating toggle and direction
251 * Status stage with toggle 1 and direction
[8a81e431]252 */
[5fd9c30]253static void batch_control(ehci_transfer_batch_t *ehci_batch)
[8a81e431]254{
255 assert(ehci_batch);
[5fd9c30]256
257 usb_direction_t dir = ehci_batch->base.dir;
[8a81e431]258 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
[6602e97]259
[35c37fc]260 usb_log_debug2("Batch %p: Control QH(%p): "
[5fd9c30]261 "%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
[35c37fc]262 ehci_batch->qh,
[6602e97]263 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
264 ehci_batch->qh->status, ehci_batch->qh->current,
265 ehci_batch->qh->next, ehci_batch->qh->alternate);
[8a81e431]266 static const usb_direction_t reverse_dir[] = {
267 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
268 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
269 };
270
271 int toggle = 0;
272 const usb_direction_t data_dir = dir;
273 const usb_direction_t status_dir = reverse_dir[dir];
274
275 /* Setup stage */
[35c37fc]276 td_init(&ehci_batch->tds[0],
277 dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[1]),
278 dma_buffer_phys(&ehci_batch->dma_buffer, ehci_batch->setup_buffer),
279 USB_DIRECTION_BOTH, USB_SETUP_PACKET_SIZE, toggle, false);
[090eea68]280 usb_log_debug2("Batch %p: Created CONTROL SETUP TD(%"PRIxn"): "
[5fd9c30]281 "%08x:%08x:%08x", ehci_batch,
[35c37fc]282 dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[0]),
283 ehci_batch->tds[0].status, ehci_batch->tds[0].next,
284 ehci_batch->tds[0].alternate);
[8a81e431]285
286 /* Data stage */
[35c37fc]287 unsigned td_current = 1;
[5fd9c30]288 size_t remain_size = ehci_batch->base.buffer_size;
[35c37fc]289 uintptr_t buffer = dma_buffer_phys(&ehci_batch->dma_buffer, ehci_batch->data_buffer);
[8a81e431]290 while (remain_size > 0) {
[35c37fc]291 const size_t transfer_size = min(remain_size, EHCI_TD_MAX_TRANSFER);
[8a81e431]292 toggle = 1 - toggle;
293
[35c37fc]294 td_init(&ehci_batch->tds[td_current],
295 dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[td_current + 1]),
296 buffer, data_dir, transfer_size, toggle, false);
[090eea68]297 usb_log_debug2("Batch %p: Created CONTROL DATA TD(%"PRIxn"): "
[5fd9c30]298 "%08x:%08x:%08x", ehci_batch,
[35c37fc]299 dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[td_current]),
300 ehci_batch->tds[td_current].status,
301 ehci_batch->tds[td_current].next,
302 ehci_batch->tds[td_current].alternate);
[8a81e431]303
304 buffer += transfer_size;
305 remain_size -= transfer_size;
306 assert(td_current < ehci_batch->td_count - 1);
307 ++td_current;
308 }
309
310 /* Status stage */
311 assert(td_current == ehci_batch->td_count - 1);
[35c37fc]312 td_init(&ehci_batch->tds[td_current], 0, 0, status_dir, 0, 1, true);
313 usb_log_debug2("Batch %p: Created CONTROL STATUS TD %d(%"PRIxn"): "
314 "%08x:%08x:%08x", ehci_batch, td_current,
315 dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[td_current]),
316 ehci_batch->tds[td_current].status,
317 ehci_batch->tds[td_current].next,
318 ehci_batch->tds[td_current].alternate);
[8a81e431]319}
320
321/** Prepare generic data transfer
322 *
323 * @param[in] ehci_batch Batch structure to use.
324 * @paramp[in] dir Communication direction.
325 *
326 * Direction is supplied by the associated ep and toggle is maintained by the
327 * EHCI hw in ED.
328 */
[5fd9c30]329static void batch_data(ehci_transfer_batch_t *ehci_batch)
[8a81e431]330{
331 assert(ehci_batch);
[6602e97]332
[35c37fc]333 usb_log_debug2("Batch %p: Data QH(%p): "
[5fd9c30]334 "%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
[35c37fc]335 ehci_batch->qh,
[6602e97]336 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
337 ehci_batch->qh->status, ehci_batch->qh->current,
338 ehci_batch->qh->next, ehci_batch->qh->alternate);
[8a81e431]339
340 size_t td_current = 0;
[5fd9c30]341 size_t remain_size = ehci_batch->base.buffer_size;
[35c37fc]342 uintptr_t buffer = dma_buffer_phys(&ehci_batch->dma_buffer, ehci_batch->data_buffer);
[8a81e431]343 while (remain_size > 0) {
344 const size_t transfer_size = remain_size > EHCI_TD_MAX_TRANSFER
345 ? EHCI_TD_MAX_TRANSFER : remain_size;
346
[00bbc362]347 const bool last = (remain_size == transfer_size);
[35c37fc]348 td_init(&ehci_batch->tds[td_current],
349 last ? 0 : dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[td_current + 1]),
350 buffer, ehci_batch->base.dir, transfer_size, -1, last);
[8a81e431]351
[090eea68]352 usb_log_debug2("Batch %p: DATA TD(%"PRIxn": %08x:%08x:%08x",
[5fd9c30]353 ehci_batch,
[35c37fc]354 dma_buffer_phys(&ehci_batch->dma_buffer, &ehci_batch->tds[td_current]),
355 ehci_batch->tds[td_current].status,
356 ehci_batch->tds[td_current].next,
357 ehci_batch->tds[td_current].alternate);
[8a81e431]358
359 buffer += transfer_size;
360 remain_size -= transfer_size;
361 assert(td_current < ehci_batch->td_count);
362 ++td_current;
363 }
364}
365
366/** Transfer setup table. */
[5fd9c30]367static void (*const batch_setup[])(ehci_transfer_batch_t*) =
[8a81e431]368{
369 [USB_TRANSFER_CONTROL] = batch_control,
370 [USB_TRANSFER_BULK] = batch_data,
371 [USB_TRANSFER_INTERRUPT] = batch_data,
372 [USB_TRANSFER_ISOCHRONOUS] = NULL,
373};
374/**
375 * @}
376 */
377
Note: See TracBrowser for help on using the repository browser.