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
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 const size_t setup_size = (ehci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL)
241 ? USB_SETUP_PACKET_SIZE
242 : 0;
243
244 if (ehci_batch->base.dir == USB_DIRECTION_IN)
245 memcpy(ehci_batch->base.buffer,
246 ehci_batch->device_buffer + setup_size,
247 ehci_batch->base.transfered_size);
248
249 /* Clear TD pointers */
250 ehci_batch->qh->next = LINK_POINTER_TERM;
251 ehci_batch->qh->current = LINK_POINTER_TERM;
252 usb_log_debug("Batch %p complete: %s", ehci_batch,
253 str_error(ehci_batch->base.error));
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);
265 qh_set_next_td(ehci_batch->qh, ehci_batch->tds[0]);
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)
273 * Data stage with alternating toggle and direction
274 * Status stage with toggle 1 and direction
275 */
276static void batch_control(ehci_transfer_batch_t *ehci_batch)
277{
278 assert(ehci_batch);
279
280 usb_direction_t dir = ehci_batch->base.dir;
281 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
282
283 usb_log_debug2("Batch %p: Control QH(%"PRIxn"): "
284 "%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
285 addr_to_phys(ehci_batch->qh),
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);
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 */
300 td_init(ehci_batch->tds[0], ehci_batch->tds[1], USB_DIRECTION_BOTH,
301 buffer, USB_SETUP_PACKET_SIZE, toggle, false);
302 usb_log_debug2("Batch %p: Created CONTROL SETUP TD(%"PRIxn"): "
303 "%08x:%08x:%08x", ehci_batch,
304 addr_to_phys(ehci_batch->tds[0]),
305 ehci_batch->tds[0]->status, ehci_batch->tds[0]->next,
306 ehci_batch->tds[0]->alternate);
307 buffer += USB_SETUP_PACKET_SIZE;
308
309 /* Data stage */
310 size_t td_current = 1;
311 size_t remain_size = ehci_batch->base.buffer_size;
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],
318 ehci_batch->tds[td_current + 1], data_dir, buffer,
319 transfer_size, toggle, false);
320 usb_log_debug2("Batch %p: Created CONTROL DATA TD(%"PRIxn"): "
321 "%08x:%08x:%08x", ehci_batch,
322 addr_to_phys(ehci_batch->tds[td_current]),
323 ehci_batch->tds[td_current]->status,
324 ehci_batch->tds[td_current]->next,
325 ehci_batch->tds[td_current]->alternate);
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);
335 td_init(ehci_batch->tds[td_current], NULL, status_dir, NULL, 0, 1, true);
336 usb_log_debug2("Batch %p: Created CONTROL STATUS TD(%"PRIxn"): "
337 "%08x:%08x:%08x", ehci_batch,
338 addr_to_phys(ehci_batch->tds[td_current]),
339 ehci_batch->tds[td_current]->status,
340 ehci_batch->tds[td_current]->next,
341 ehci_batch->tds[td_current]->alternate);
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 */
352static void batch_data(ehci_transfer_batch_t *ehci_batch)
353{
354 assert(ehci_batch);
355
356 usb_log_debug2("Batch %p: Data QH(%"PRIxn"): "
357 "%08x:%08x:%08x:%08x:%08x:%08x", ehci_batch,
358 addr_to_phys(ehci_batch->qh),
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);
362
363 size_t td_current = 0;
364 size_t remain_size = ehci_batch->base.buffer_size;
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
370 const bool last = (remain_size == transfer_size);
371 td_init(
372 ehci_batch->tds[td_current],
373 last ? NULL : ehci_batch->tds[td_current + 1],
374 ehci_batch->base.dir, buffer, transfer_size, -1, last);
375
376 usb_log_debug2("Batch %p: DATA TD(%"PRIxn": %08x:%08x:%08x",
377 ehci_batch,
378 addr_to_phys(ehci_batch->tds[td_current]),
379 ehci_batch->tds[td_current]->status,
380 ehci_batch->tds[td_current]->next,
381 ehci_batch->tds[td_current]->alternate);
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. */
391static void (*const batch_setup[])(ehci_transfer_batch_t*) =
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.