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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e6d7df1 was 6ef69e9, checked in by Jan Vesely <jano.vesely@…>, 10 years ago

ehci,batch: consolidate debug messages

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