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

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

usb: unified logging

Use logger instead of printf. Logger adds newlines automatically.

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