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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 27b0ea0 was 27b0ea0, checked in by Aearsis <Hlavaty.Ondrej@…>, 8 years ago

ehci: fixed bugs introduced by refactoring

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