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

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

ehci batch: make sure the td holding memory is mapped

add completion debug line

  • 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 for (size_t i = 0; i < ehci_batch->td_count; ++i) {
66 free32(ehci_batch->tds[i]);
67 }
68 free(ehci_batch->tds);
69 }
70 usb_transfer_batch_destroy(ehci_batch->usb_batch);
71 free32(ehci_batch->device_buffer);
72 free(ehci_batch);
73}
74
75/** Finishes usb_transfer_batch and destroys the structure.
76 *
77 * @param[in] uhci_batch Instance to finish and destroy.
78 */
79void ehci_transfer_batch_finish_dispose(ehci_transfer_batch_t *ehci_batch)
80{
81 assert(ehci_batch);
82 assert(ehci_batch->usb_batch);
83 usb_transfer_batch_finish(ehci_batch->usb_batch,
84 ehci_batch->device_buffer + ehci_batch->usb_batch->setup_size);
85 ehci_transfer_batch_dispose(ehci_batch);
86}
87
88/** Allocate memory and initialize internal data structure.
89 *
90 * @param[in] usb_batch Pointer to generic USB batch structure.
91 * @return Valid pointer if all structures were successfully created,
92 * NULL otherwise.
93 *
94 * Determines the number of needed transfer descriptors (TDs).
95 * Prepares a transport buffer (that is accessible by the hardware).
96 * Initializes parameters needed for the transfer and callback.
97 */
98ehci_transfer_batch_t * ehci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
99{
100 assert(usb_batch);
101
102 ehci_transfer_batch_t *ehci_batch =
103 calloc(1, sizeof(ehci_transfer_batch_t));
104 if (!ehci_batch) {
105 usb_log_error("Failed to allocate EHCI batch data.");
106 goto dispose;
107 }
108 link_initialize(&ehci_batch->link);
109 ehci_batch->td_count =
110 (usb_batch->buffer_size + EHCI_TD_MAX_TRANSFER - 1)
111 / EHCI_TD_MAX_TRANSFER;
112
113 /* Control transfer need Setup and Status stage */
114 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
115 ehci_batch->td_count += 2;
116 }
117
118 ehci_batch->tds = calloc(ehci_batch->td_count, sizeof(td_t*));
119 if (!ehci_batch->tds) {
120 usb_log_error("Failed to allocate EHCI transfer descriptors.");
121 goto dispose;
122 }
123
124 /* Add TD left over by the previous transfer */
125 ehci_batch->qh = ehci_endpoint_get(usb_batch->ep)->qh;
126
127 for (unsigned i = 0; i < ehci_batch->td_count; ++i) {
128 ehci_batch->tds[i] = malloc32(sizeof(td_t));
129 if (!ehci_batch->tds[i]) {
130 usb_log_error("Failed to allocate TD %d.", i);
131 goto dispose;
132 }
133 memset(ehci_batch->tds[i], 0, sizeof(td_t));
134 }
135
136
137 /* Mix setup stage and data together, we have enough space */
138 if (usb_batch->setup_size + usb_batch->buffer_size > 0) {
139 /* Use one buffer for setup and data stage */
140 ehci_batch->device_buffer =
141 malloc32(usb_batch->setup_size + usb_batch->buffer_size);
142 if (!ehci_batch->device_buffer) {
143 usb_log_error("Failed to allocate device buffer");
144 goto dispose;
145 }
146 /* Copy setup data */
147 memcpy(ehci_batch->device_buffer, usb_batch->setup_buffer,
148 usb_batch->setup_size);
149 /* Copy generic data */
150 if (usb_batch->ep->direction != USB_DIRECTION_IN)
151 memcpy(
152 ehci_batch->device_buffer + usb_batch->setup_size,
153 usb_batch->buffer, usb_batch->buffer_size);
154 }
155 ehci_batch->usb_batch = usb_batch;
156
157 const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
158 assert(batch_setup[usb_batch->ep->transfer_type]);
159 batch_setup[usb_batch->ep->transfer_type](ehci_batch, dir);
160
161 return ehci_batch;
162dispose:
163 ehci_transfer_batch_dispose(ehci_batch);
164 return NULL;
165}
166
167/** Check batch TDs' status.
168 *
169 * @param[in] ehci_batch Batch structure to use.
170 * @return False, if there is an active TD, true otherwise.
171 *
172 * Walk all TDs (usually there is just one). Stop with false if there is an
173 * active TD. Stop with true if an error is found. Return true if the walk
174 * completes with the last TD.
175 */
176bool ehci_transfer_batch_is_complete(const ehci_transfer_batch_t *ehci_batch)
177{
178 assert(ehci_batch);
179 assert(ehci_batch->usb_batch);
180
181 usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
182 ehci_batch->usb_batch, ehci_batch->td_count);
183
184 usb_log_debug2("QH: %08x:%08x:%08x:%08x:%08x:%08x.\n",
185 ehci_batch->qh->ep_char, ehci_batch->qh->ep_cap,
186 ehci_batch->qh->status, ehci_batch->qh->current,
187 ehci_batch->qh->next, ehci_batch->qh->alternate);
188
189 if (!qh_halted(ehci_batch->qh) && (qh_transfer_pending(ehci_batch->qh)
190 || qh_transfer_active(ehci_batch->qh)))
191 return false;
192
193 /* Now we may be sure that either the ED is inactive because of errors
194 * or all transfer descriptors completed successfully */
195
196 /* Assume all data got through */
197 ehci_batch->usb_batch->transfered_size =
198 ehci_batch->usb_batch->buffer_size;
199
200 /* Check all TDs */
201 for (size_t i = 0; i < ehci_batch->td_count; ++i) {
202 assert(ehci_batch->tds[i] != NULL);
203 usb_log_debug("TD %zu: %08x:%08x:%08x.", i,
204 ehci_batch->tds[i]->status, ehci_batch->tds[i]->next,
205 ehci_batch->tds[i]->alternate);
206
207 ehci_batch->usb_batch->error = td_error(ehci_batch->tds[i]);
208 if (ehci_batch->usb_batch->error == EOK) {
209 /* If the TD got all its data through, it will report
210 * 0 bytes remain, the sole exception is INPUT with
211 * data rounding flag (short), i.e. every INPUT.
212 * Nice thing is that short packets will correctly
213 * report remaining data, thus making this computation
214 * correct (short packets need to be produced by the
215 * last TD)
216 * NOTE: This also works for CONTROL transfer as
217 * the first TD will return 0 remain.
218 * NOTE: Short packets don't break the assumption that
219 * we leave the very last(unused) TD behind.
220 */
221 ehci_batch->usb_batch->transfered_size
222 -= td_remain_size(ehci_batch->tds[i]);
223 } else {
224 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
225 ehci_batch->usb_batch, i,
226 ehci_batch->tds[i]->status);
227 /* Clear possible ED HALT */
228 qh_clear_halt(ehci_batch->qh);
229 break;
230 }
231 }
232
233 assert(ehci_batch->usb_batch->transfered_size <=
234 ehci_batch->usb_batch->buffer_size);
235 /* Clear TD pointers */
236 ehci_batch->qh->next = LINK_POINTER_TERM;
237 ehci_batch->qh->current = LINK_POINTER_TERM;
238 usb_log_debug("Batch %p complete.\n", ehci_batch->usb_batch);
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(ehci_batch->tds[0], ehci_batch->tds[1], USB_DIRECTION_BOTH,
285 buffer, ehci_batch->usb_batch->setup_size, toggle, false);
286 usb_log_debug2("Created CONTROL SETUP TD(%"PRIxn"): %08x:%08x:%08x",
287 addr_to_phys(ehci_batch->tds[0]),
288 ehci_batch->tds[0]->status, ehci_batch->tds[0]->next,
289 ehci_batch->tds[0]->alternate);
290 buffer += ehci_batch->usb_batch->setup_size;
291
292 /* Data stage */
293 size_t td_current = 1;
294 size_t remain_size = ehci_batch->usb_batch->buffer_size;
295 while (remain_size > 0) {
296 const size_t transfer_size =
297 min(remain_size, EHCI_TD_MAX_TRANSFER);
298 toggle = 1 - toggle;
299
300 td_init(ehci_batch->tds[td_current],
301 ehci_batch->tds[td_current + 1], data_dir, buffer,
302 transfer_size, toggle, false);
303 usb_log_debug2("Created CONTROL DATA TD(%"PRIxn"): %08x:%08x:%08x",
304 addr_to_phys(ehci_batch->tds[td_current]),
305 ehci_batch->tds[td_current]->status,
306 ehci_batch->tds[td_current]->next,
307 ehci_batch->tds[td_current]->alternate);
308
309 buffer += transfer_size;
310 remain_size -= transfer_size;
311 assert(td_current < ehci_batch->td_count - 1);
312 ++td_current;
313 }
314
315 /* Status stage */
316 assert(td_current == ehci_batch->td_count - 1);
317 td_init(ehci_batch->tds[td_current], NULL, status_dir, NULL, 0, 1, true);
318 usb_log_debug2("Created CONTROL STATUS TD(%"PRIxn"): %08x:%08x:%08x",
319 addr_to_phys(ehci_batch->tds[td_current]),
320 ehci_batch->tds[td_current]->status,
321 ehci_batch->tds[td_current]->next,
322 ehci_batch->tds[td_current]->alternate);
323
324 usb_log_debug(
325 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
326 ehci_batch->usb_batch,
327 usb_str_transfer_type(ehci_batch->usb_batch->ep->transfer_type),
328 usb_str_direction(dir),
329 USB_TRANSFER_BATCH_ARGS(*ehci_batch->usb_batch));
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_debug("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 usb_log_debug(
378 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized",
379 ehci_batch->usb_batch,
380 usb_str_transfer_type(ehci_batch->usb_batch->ep->transfer_type),
381 usb_str_direction(dir),
382 USB_TRANSFER_BATCH_ARGS(*ehci_batch->usb_batch));
383}
384
385/** Transfer setup table. */
386static void (*const batch_setup[])(ehci_transfer_batch_t*, usb_direction_t) =
387{
388 [USB_TRANSFER_CONTROL] = batch_control,
389 [USB_TRANSFER_BULK] = batch_data,
390 [USB_TRANSFER_INTERRUPT] = batch_data,
391 [USB_TRANSFER_ISOCHRONOUS] = NULL,
392};
393/**
394 * @}
395 */
396
Note: See TracBrowser for help on using the repository browser.