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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c1694b6b was c1694b6b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Add str_error() in numerous places.

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