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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d1582b50 was d1582b50, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Fix spacing in single-line comments using latest ccheck

This found incorrectly formatted section comments (with blocks of
asterisks or dashes). I strongly believe against using section comments
but I am not simply removing them since that would probably be
controversial.

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