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

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

ehci: QHs with one active td are still considered active.

  • 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 || qh_transfer_active(ehci_batch->qh)))
193 return false;
194
195 /* Now we may be sure that either the ED is inactive because of errors
196 * or all transfer descriptors completed successfully */
197
198 /* Assume all data got through */
199 ehci_batch->usb_batch->transfered_size =
200 ehci_batch->usb_batch->buffer_size;
201
202 /* Check all TDs */
203 for (size_t i = 0; i < ehci_batch->td_count; ++i) {
204 assert(ehci_batch->tds[i] != NULL);
205 usb_log_debug("TD %zu: %08x:%08x:%08x.", i,
206 ehci_batch->tds[i]->status, ehci_batch->tds[i]->next,
207 ehci_batch->tds[i]->alternate);
208
209 ehci_batch->usb_batch->error = td_error(ehci_batch->tds[i]);
210 if (ehci_batch->usb_batch->error == EOK) {
211 /* If the TD got all its data through, it will report
212 * 0 bytes remain, the sole exception is INPUT with
213 * data rounding flag (short), i.e. every INPUT.
214 * Nice thing is that short packets will correctly
215 * report remaining data, thus making this computation
216 * correct (short packets need to be produced by the
217 * last TD)
218 * NOTE: This also works for CONTROL transfer as
219 * the first TD will return 0 remain.
220 * NOTE: Short packets don't break the assumption that
221 * we leave the very last(unused) TD behind.
222 */
223 ehci_batch->usb_batch->transfered_size
224 -= td_remain_size(ehci_batch->tds[i]);
225 } else {
226 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
227 ehci_batch->usb_batch, i,
228 ehci_batch->tds[i]->status);
229 /* Clear possible ED HALT */
230 qh_clear_halt(ehci_batch->qh);
231 break;
232 }
233 }
234
235 assert(ehci_batch->usb_batch->transfered_size <=
236 ehci_batch->usb_batch->buffer_size);
237 /* Clear TD pointers */
238 ehci_batch->qh->next = LINK_POINTER_TERM;
239 ehci_batch->qh->current = LINK_POINTER_TERM;
240
241 return true;
242}
243
244/** Starts execution of the TD list
245 *
246 * @param[in] ehci_batch Batch structure to use
247 */
248void ehci_transfer_batch_commit(const ehci_transfer_batch_t *ehci_batch)
249{
250 assert(ehci_batch);
251 qh_set_next_td(ehci_batch->qh, ehci_batch->tds[0]);
252}
253
254/** Prepare generic control transfer
255 *
256 * @param[in] ehci_batch Batch structure to use.
257 * @param[in] dir Communication direction
258 *
259 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
260 * Data stage with alternating toggle and direction supplied by parameter.
261 * Status stage with toggle 1 and direction supplied by parameter.
262 */
263static void batch_control(ehci_transfer_batch_t *ehci_batch, usb_direction_t dir)
264{
265 assert(ehci_batch);
266 assert(ehci_batch->usb_batch);
267 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
268
269 usb_log_debug2("Control QH(%"PRIxn"): %08x:%08x:%08x:%08x:%08x:%08x",
270 addr_to_phys(ehci_batch->qh),
271 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
272 ehci_batch->qh->status, ehci_batch->qh->current,
273 ehci_batch->qh->next, ehci_batch->qh->alternate);
274 static const usb_direction_t reverse_dir[] = {
275 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
276 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
277 };
278
279 int toggle = 0;
280 const char* buffer = ehci_batch->device_buffer;
281 const usb_direction_t data_dir = dir;
282 const usb_direction_t status_dir = reverse_dir[dir];
283
284 /* Setup stage */
285 td_init(
286 ehci_batch->tds[0], ehci_batch->tds[1], USB_DIRECTION_BOTH,
287 buffer, ehci_batch->usb_batch->setup_size, toggle, false);
288 usb_log_debug2("Created CONTROL SETUP TD(%"PRIxn"): %08x:%08x:%08x",
289 addr_to_phys(ehci_batch->tds[0]),
290 ehci_batch->tds[0]->status, ehci_batch->tds[0]->next,
291 ehci_batch->tds[0]->alternate);
292 buffer += ehci_batch->usb_batch->setup_size;
293
294 /* Data stage */
295 size_t td_current = 1;
296 size_t remain_size = ehci_batch->usb_batch->buffer_size;
297 while (remain_size > 0) {
298 const size_t transfer_size =
299 min(remain_size, EHCI_TD_MAX_TRANSFER);
300 toggle = 1 - toggle;
301
302 td_init(ehci_batch->tds[td_current],
303 ehci_batch->tds[td_current + 1], data_dir, buffer,
304 transfer_size, toggle, false);
305 usb_log_debug2("Created CONTROL DATA TD(%"PRIxn"): %08x:%08x:%08x",
306 addr_to_phys(ehci_batch->tds[td_current]),
307 ehci_batch->tds[td_current]->status,
308 ehci_batch->tds[td_current]->next,
309 ehci_batch->tds[td_current]->alternate);
310
311 buffer += transfer_size;
312 remain_size -= transfer_size;
313 assert(td_current < ehci_batch->td_count - 1);
314 ++td_current;
315 }
316
317 /* Status stage */
318 assert(td_current == ehci_batch->td_count - 1);
319 td_init(ehci_batch->tds[td_current], NULL, status_dir, NULL, 0, 1, true);
320 usb_log_debug2("Created CONTROL STATUS TD(%"PRIxn"): %08x:%08x:%08x",
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 usb_log_debug(
327 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
328 ehci_batch->usb_batch,
329 usb_str_transfer_type(ehci_batch->usb_batch->ep->transfer_type),
330 usb_str_direction(dir),
331 USB_TRANSFER_BATCH_ARGS(*ehci_batch->usb_batch));
332}
333
334/** Prepare generic data transfer
335 *
336 * @param[in] ehci_batch Batch structure to use.
337 * @paramp[in] dir Communication direction.
338 *
339 * Direction is supplied by the associated ep and toggle is maintained by the
340 * EHCI hw in ED.
341 */
342static void batch_data(ehci_transfer_batch_t *ehci_batch, usb_direction_t dir)
343{
344 assert(ehci_batch);
345 assert(ehci_batch->usb_batch);
346 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
347
348 usb_log_debug("Control QH(%"PRIxn"): %08x:%08x:%08x:%08x:%08x:%08x",
349 addr_to_phys(ehci_batch->qh),
350 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
351 ehci_batch->qh->status, ehci_batch->qh->current,
352 ehci_batch->qh->next, ehci_batch->qh->alternate);
353
354 size_t td_current = 0;
355 size_t remain_size = ehci_batch->usb_batch->buffer_size;
356 char *buffer = ehci_batch->device_buffer;
357 while (remain_size > 0) {
358 const size_t transfer_size = remain_size > EHCI_TD_MAX_TRANSFER
359 ? EHCI_TD_MAX_TRANSFER : remain_size;
360
361 const bool last = (remain_size == transfer_size);
362 td_init(
363 ehci_batch->tds[td_current],
364 last ? NULL : ehci_batch->tds[td_current + 1],
365 dir, buffer, transfer_size, -1, last);
366
367 usb_log_debug2("Created DATA TD(%"PRIxn": %08x:%08x:%08x",
368 addr_to_phys(ehci_batch->tds[td_current]),
369 ehci_batch->tds[td_current]->status,
370 ehci_batch->tds[td_current]->next,
371 ehci_batch->tds[td_current]->alternate);
372
373 buffer += transfer_size;
374 remain_size -= transfer_size;
375 assert(td_current < ehci_batch->td_count);
376 ++td_current;
377 }
378
379 usb_log_debug(
380 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized",
381 ehci_batch->usb_batch,
382 usb_str_transfer_type(ehci_batch->usb_batch->ep->transfer_type),
383 usb_str_direction(dir),
384 USB_TRANSFER_BATCH_ARGS(*ehci_batch->usb_batch));
385}
386
387/** Transfer setup table. */
388static void (*const batch_setup[])(ehci_transfer_batch_t*, usb_direction_t) =
389{
390 [USB_TRANSFER_CONTROL] = batch_control,
391 [USB_TRANSFER_BULK] = batch_data,
392 [USB_TRANSFER_INTERRUPT] = batch_data,
393 [USB_TRANSFER_ISOCHRONOUS] = NULL,
394};
395/**
396 * @}
397 */
398
Note: See TracBrowser for help on using the repository browser.