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

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

usbhost refactoring: let transfer_batch be initialized by bus

Currently makes older HCs fail, work in progress.

  • Property mode set to 100644
File size: 12.8 KB
Line 
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>
40#include <str_error.h>
41
42#include <usb/usb.h>
43#include <usb/debug.h>
44#include <usb/host/utils/malloc32.h>
45
46#include "ehci_batch.h"
47#include "ehci_bus.h"
48
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
55static void (*const batch_setup[])(ehci_transfer_batch_t*);
56
57/** Safely destructs ehci_transfer_batch_t structure
58 *
59 * @param[in] ehci_batch Instance to destroy.
60 */
61void ehci_transfer_batch_destroy(ehci_transfer_batch_t *ehci_batch)
62{
63 assert(ehci_batch);
64 if (ehci_batch->tds) {
65 for (size_t i = 0; i < ehci_batch->td_count; ++i) {
66 free32(ehci_batch->tds[i]);
67 }
68 free(ehci_batch->tds);
69 }
70 free32(ehci_batch->device_buffer);
71 free(ehci_batch);
72 usb_log_debug2("Batch(%p): disposed", ehci_batch);
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 */
82ehci_transfer_batch_t * ehci_transfer_batch_create(endpoint_t *ep)
83{
84 assert(ep);
85
86 ehci_transfer_batch_t *ehci_batch = calloc(1, sizeof(ehci_transfer_batch_t));
87 if (!ehci_batch) {
88 usb_log_error("Failed to allocate EHCI batch data.");
89 return NULL;
90 }
91
92 usb_transfer_batch_init(&ehci_batch->base, ep);
93 link_initialize(&ehci_batch->link);
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;
138
139 /* Control transfer need Setup and Status stage */
140 if (ehci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL) {
141 ehci_batch->td_count += 2;
142 }
143
144 ehci_batch->tds = calloc(ehci_batch->td_count, sizeof(td_t*));
145 if (!ehci_batch->tds) {
146 usb_log_error("Batch %p: Failed to allocate EHCI transfer "
147 "descriptors.", ehci_batch);
148 return ENOMEM;
149 }
150
151 for (unsigned i = 0; i < ehci_batch->td_count; ++i) {
152 ehci_batch->tds[i] = malloc32(sizeof(td_t));
153 if (!ehci_batch->tds[i]) {
154 usb_log_error("Batch %p: Failed to allocate TD %d.",
155 ehci_batch, i);
156 return ENOMEM;
157 }
158 memset(ehci_batch->tds[i], 0, sizeof(td_t));
159 }
160
161 assert(batch_setup[ehci_batch->base.ep->transfer_type]);
162 batch_setup[ehci_batch->base.ep->transfer_type](ehci_batch);
163
164 usb_log_debug("Batch %p %s " USB_TRANSFER_BATCH_FMT " initialized.\n",
165 ehci_batch, usb_str_direction(ehci_batch->base.dir),
166 USB_TRANSFER_BATCH_ARGS(ehci_batch->base));
167
168 return EOK;
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 */
180bool ehci_transfer_batch_check_completed(ehci_transfer_batch_t *ehci_batch)
181{
182 assert(ehci_batch);
183
184 usb_log_debug("Batch %p: checking %zu td(s) for completion.\n",
185 ehci_batch, ehci_batch->td_count);
186
187 usb_log_debug2("Batch %p: QH: %08x:%08x:%08x:%08x:%08x:%08x.\n",
188 ehci_batch,
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
193 if (!qh_halted(ehci_batch->qh) && (qh_transfer_pending(ehci_batch->qh)
194 || qh_transfer_active(ehci_batch->qh)))
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 */
201 ehci_batch->base.transfered_size = ehci_batch->base.buffer_size;
202
203 /* Check all TDs */
204 for (size_t i = 0; i < ehci_batch->td_count; ++i) {
205 assert(ehci_batch->tds[i] != NULL);
206 usb_log_debug("Batch %p: TD %zu: %08x:%08x:%08x.",
207 ehci_batch, i,
208 ehci_batch->tds[i]->status, ehci_batch->tds[i]->next,
209 ehci_batch->tds[i]->alternate);
210
211 ehci_batch->base.error = td_error(ehci_batch->tds[i]);
212 if (ehci_batch->base.error == EOK) {
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 */
225 ehci_batch->base.transfered_size
226 -= td_remain_size(ehci_batch->tds[i]);
227 } else {
228 usb_log_debug("Batch %p found error TD(%zu):%08x (%d).",
229 ehci_batch, i,
230 ehci_batch->tds[i]->status,
231 ehci_batch->base.error);
232 /* Clear possible ED HALT */
233 qh_clear_halt(ehci_batch->qh);
234 break;
235 }
236 }
237
238 assert(ehci_batch->base.transfered_size <= ehci_batch->base.buffer_size);
239
240 if (ehci_batch->base.dir == USB_DIRECTION_IN)
241 memcpy(ehci_batch->base.buffer, ehci_batch->device_buffer, ehci_batch->base.transfered_size);
242
243 /* Clear TD pointers */
244 ehci_batch->qh->next = LINK_POINTER_TERM;
245 ehci_batch->qh->current = LINK_POINTER_TERM;
246 usb_log_debug("Batch %p complete: %s", ehci_batch,
247 str_error(ehci_batch->base.error));
248
249 return true;
250}
251
252/** Starts execution of the TD list
253 *
254 * @param[in] ehci_batch Batch structure to use
255 */
256void ehci_transfer_batch_commit(const ehci_transfer_batch_t *ehci_batch)
257{
258 assert(ehci_batch);
259 qh_set_next_td(ehci_batch->qh, ehci_batch->tds[0]);
260}
261
262/** Prepare generic control transfer
263 *
264 * @param[in] ehci_batch Batch structure to use.
265 *
266 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
267 * Data stage with alternating toggle and direction
268 * Status stage with toggle 1 and direction
269 */
270static void batch_control(ehci_transfer_batch_t *ehci_batch)
271{
272 assert(ehci_batch);
273
274 usb_direction_t dir = ehci_batch->base.dir;
275 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
276
277 usb_log_debug2("Batch %p: Control QH(%"PRIxn"): "
278 "%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
279 addr_to_phys(ehci_batch->qh),
280 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
281 ehci_batch->qh->status, ehci_batch->qh->current,
282 ehci_batch->qh->next, ehci_batch->qh->alternate);
283 static const usb_direction_t reverse_dir[] = {
284 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
285 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
286 };
287
288 int toggle = 0;
289 const char* buffer = ehci_batch->device_buffer;
290 const usb_direction_t data_dir = dir;
291 const usb_direction_t status_dir = reverse_dir[dir];
292
293 /* Setup stage */
294 td_init(ehci_batch->tds[0], ehci_batch->tds[1], USB_DIRECTION_BOTH,
295 buffer, USB_SETUP_PACKET_SIZE, toggle, false);
296 usb_log_debug2("Batch %p: Created CONTROL SETUP TD(%"PRIxn"): "
297 "%08x:%08x:%08x", ehci_batch,
298 addr_to_phys(ehci_batch->tds[0]),
299 ehci_batch->tds[0]->status, ehci_batch->tds[0]->next,
300 ehci_batch->tds[0]->alternate);
301 buffer += USB_SETUP_PACKET_SIZE;
302
303 /* Data stage */
304 size_t td_current = 1;
305 size_t remain_size = ehci_batch->base.buffer_size;
306 while (remain_size > 0) {
307 const size_t transfer_size =
308 min(remain_size, EHCI_TD_MAX_TRANSFER);
309 toggle = 1 - toggle;
310
311 td_init(ehci_batch->tds[td_current],
312 ehci_batch->tds[td_current + 1], data_dir, buffer,
313 transfer_size, toggle, false);
314 usb_log_debug2("Batch %p: Created CONTROL DATA TD(%"PRIxn"): "
315 "%08x:%08x:%08x", ehci_batch,
316 addr_to_phys(ehci_batch->tds[td_current]),
317 ehci_batch->tds[td_current]->status,
318 ehci_batch->tds[td_current]->next,
319 ehci_batch->tds[td_current]->alternate);
320
321 buffer += transfer_size;
322 remain_size -= transfer_size;
323 assert(td_current < ehci_batch->td_count - 1);
324 ++td_current;
325 }
326
327 /* Status stage */
328 assert(td_current == ehci_batch->td_count - 1);
329 td_init(ehci_batch->tds[td_current], NULL, status_dir, NULL, 0, 1, true);
330 usb_log_debug2("Batch %p: Created CONTROL STATUS TD(%"PRIxn"): "
331 "%08x:%08x:%08x", ehci_batch,
332 addr_to_phys(ehci_batch->tds[td_current]),
333 ehci_batch->tds[td_current]->status,
334 ehci_batch->tds[td_current]->next,
335 ehci_batch->tds[td_current]->alternate);
336}
337
338/** Prepare generic data transfer
339 *
340 * @param[in] ehci_batch Batch structure to use.
341 * @paramp[in] dir Communication direction.
342 *
343 * Direction is supplied by the associated ep and toggle is maintained by the
344 * EHCI hw in ED.
345 */
346static void batch_data(ehci_transfer_batch_t *ehci_batch)
347{
348 assert(ehci_batch);
349
350 usb_log_debug2("Batch %p: Data QH(%"PRIxn"): "
351 "%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
352 addr_to_phys(ehci_batch->qh),
353 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
354 ehci_batch->qh->status, ehci_batch->qh->current,
355 ehci_batch->qh->next, ehci_batch->qh->alternate);
356
357 size_t td_current = 0;
358 size_t remain_size = ehci_batch->base.buffer_size;
359 char *buffer = ehci_batch->device_buffer;
360 while (remain_size > 0) {
361 const size_t transfer_size = remain_size > EHCI_TD_MAX_TRANSFER
362 ? EHCI_TD_MAX_TRANSFER : remain_size;
363
364 const bool last = (remain_size == transfer_size);
365 td_init(
366 ehci_batch->tds[td_current],
367 last ? NULL : ehci_batch->tds[td_current + 1],
368 ehci_batch->base.dir, buffer, transfer_size, -1, last);
369
370 usb_log_debug2("Batch %p: DATA TD(%"PRIxn": %08x:%08x:%08x",
371 ehci_batch,
372 addr_to_phys(ehci_batch->tds[td_current]),
373 ehci_batch->tds[td_current]->status,
374 ehci_batch->tds[td_current]->next,
375 ehci_batch->tds[td_current]->alternate);
376
377 buffer += transfer_size;
378 remain_size -= transfer_size;
379 assert(td_current < ehci_batch->td_count);
380 ++td_current;
381 }
382}
383
384/** Transfer setup table. */
385static void (*const batch_setup[])(ehci_transfer_batch_t*) =
386{
387 [USB_TRANSFER_CONTROL] = batch_control,
388 [USB_TRANSFER_BULK] = batch_data,
389 [USB_TRANSFER_INTERRUPT] = batch_data,
390 [USB_TRANSFER_ISOCHRONOUS] = NULL,
391};
392/**
393 * @}
394 */
395
Note: See TracBrowser for help on using the repository browser.