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

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

ehci: Reorganize debug messages

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