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

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

ehci: Add ehci batch skeleton

copied from ohci driver

  • Property mode set to 100644
File size: 13.9 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
48static void (*const batch_setup[])(ehci_transfer_batch_t*, usb_direction_t);
49
50/** Safely destructs ehci_transfer_batch_t structure
51 *
52 * @param[in] ehci_batch Instance to destroy.
53 */
54static void ehci_transfer_batch_dispose(ehci_transfer_batch_t *ehci_batch)
55{
56 if (!ehci_batch)
57 return;
58 if (ehci_batch->tds) {
59 const ehci_endpoint_t *ehci_ep =
60 ehci_endpoint_get(ehci_batch->usb_batch->ep);
61 assert(ehci_ep);
62 for (unsigned i = 0; i < ehci_batch->td_count; ++i) {
63 if (ehci_batch->tds[i] != ehci_ep->td)
64 free32(ehci_batch->tds[i]);
65 }
66 free(ehci_batch->tds);
67 }
68 usb_transfer_batch_destroy(ehci_batch->usb_batch);
69 free32(ehci_batch->device_buffer);
70 free(ehci_batch);
71}
72
73/** Finishes usb_transfer_batch and destroys the structure.
74 *
75 * @param[in] uhci_batch Instance to finish and destroy.
76 */
77void ehci_transfer_batch_finish_dispose(ehci_transfer_batch_t *ehci_batch)
78{
79 assert(ehci_batch);
80 assert(ehci_batch->usb_batch);
81 usb_transfer_batch_finish(ehci_batch->usb_batch,
82 ehci_batch->device_buffer + ehci_batch->usb_batch->setup_size);
83 ehci_transfer_batch_dispose(ehci_batch);
84}
85
86/** Allocate memory and initialize internal data structure.
87 *
88 * @param[in] usb_batch Pointer to generic USB batch structure.
89 * @return Valid pointer if all structures were successfully created,
90 * NULL otherwise.
91 *
92 * Determines the number of needed transfer descriptors (TDs).
93 * Prepares a transport buffer (that is accessible by the hardware).
94 * Initializes parameters needed for the transfer and callback.
95 */
96ehci_transfer_batch_t * ehci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
97{
98 assert(usb_batch);
99
100 ehci_transfer_batch_t *ehci_batch =
101 calloc(1, sizeof(ehci_transfer_batch_t));
102 if (!ehci_batch) {
103 usb_log_error("Failed to allocate EHCI batch data.");
104 goto dispose;
105 }
106 link_initialize(&ehci_batch->link);
107// ehci_batch->td_count =
108// (usb_batch->buffer_size + EHCI_TD_MAX_TRANSFER - 1)
109// / EHCI_TD_MAX_TRANSFER;
110 /* Control transfer need Setup and Status stage */
111 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
112 ehci_batch->td_count += 2;
113 }
114
115 /* We need an extra place for TD that was left at ED */
116 ehci_batch->tds = calloc(ehci_batch->td_count + 1, sizeof(td_t*));
117 if (!ehci_batch->tds) {
118 usb_log_error("Failed to allocate EHCI transfer descriptors.");
119 goto dispose;
120 }
121
122 /* Add TD left over by the previous transfer */
123 ehci_batch->qh = ehci_endpoint_get(usb_batch->ep)->qh;
124 ehci_batch->tds[0] = ehci_endpoint_get(usb_batch->ep)->td;
125
126 for (unsigned i = 1; i <= ehci_batch->td_count; ++i) {
127 ehci_batch->tds[i] = malloc32(sizeof(td_t));
128 if (!ehci_batch->tds[i]) {
129 usb_log_error("Failed to allocate TD %d.", i);
130 goto dispose;
131 }
132 }
133
134
135 /* NOTE: EHCI is capable of handling buffer that crosses page boundaries
136 * it is, however, not capable of handling buffer that occupies more
137 * than two pages (the first page is computed using start pointer, the
138 * other using the end pointer) */
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 return ehci_batch;
163dispose:
164 ehci_transfer_batch_dispose(ehci_batch);
165 return NULL;
166}
167
168/** Check batch TDs' status.
169 *
170 * @param[in] ehci_batch Batch structure to use.
171 * @return False, if there is an active TD, true otherwise.
172 *
173 * Walk all TDs (usually there is just one). Stop with false if there is an
174 * active TD. Stop with true if an error is found. Return true if the walk
175 * completes with the last TD.
176 */
177bool ehci_transfer_batch_is_complete(const ehci_transfer_batch_t *ehci_batch)
178{
179 assert(ehci_batch);
180 assert(ehci_batch->usb_batch);
181
182 usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
183 ehci_batch->usb_batch, ehci_batch->td_count);
184#if 0
185 usb_log_debug2("ED: %08x:%08x:%08x:%08x.\n",
186 ehci_batch->ed->status, ehci_batch->ed->td_head,
187 ehci_batch->ed->td_tail, ehci_batch->ed->next);
188
189 if (!ed_inactive(ehci_batch->ed) && ed_transfer_pending(ehci_batch->ed))
190 return false;
191
192 /* Now we may be sure that either the ED is inactive because of errors
193 * or all transfer descriptors completed successfully */
194
195 /* Assume all data got through */
196 ehci_batch->usb_batch->transfered_size =
197 ehci_batch->usb_batch->buffer_size;
198
199 /* Assume we will leave the last(unused) TD behind */
200 unsigned leave_td = ehci_batch->td_count;
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:%08x.\n", i,
206 ehci_batch->tds[i]->status, ehci_batch->tds[i]->cbp,
207 ehci_batch->tds[i]->next, ehci_batch->tds[i]->be);
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
230 /* ED should be stopped because of errors */
231 assert((ehci_batch->ed->td_head & ED_TDHEAD_HALTED_FLAG) != 0);
232
233 /* Now we have a problem: we don't know what TD
234 * the head pointer points to, the retiring rules
235 * described in specs say it should be the one after
236 * the failed one so set the tail pointer accordingly.
237 * It will be the one TD we leave behind.
238 */
239 leave_td = i + 1;
240
241 /* Check TD assumption */
242 assert(ed_head_td(ehci_batch->ed) ==
243 addr_to_phys(ehci_batch->tds[leave_td]));
244
245 /* Set tail to the same TD */
246 ed_set_tail_td(ehci_batch->ed,
247 ehci_batch->tds[leave_td]);
248
249 /* Clear possible ED HALT */
250 ed_clear_halt(ehci_batch->ed);
251 break;
252 }
253 }
254 assert(ehci_batch->usb_batch->transfered_size <=
255 ehci_batch->usb_batch->buffer_size);
256
257 /* Store the remaining TD */
258 ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ehci_batch->usb_batch->ep);
259 assert(ehci_ep);
260 ehci_ep->td = ehci_batch->tds[leave_td];
261
262 /* Make sure that we are leaving the right TD behind */
263 assert(addr_to_phys(ehci_ep->td) == ed_head_td(ehci_batch->ed));
264 assert(addr_to_phys(ehci_ep->td) == ed_tail_td(ehci_batch->ed));
265#endif
266 return true;
267}
268
269/** Starts execution of the TD list
270 *
271 * @param[in] ehci_batch Batch structure to use
272 */
273void ehci_transfer_batch_commit(const ehci_transfer_batch_t *ehci_batch)
274{
275 assert(ehci_batch);
276// ed_set_tail_td(ehci_batch->ed, ehci_batch->tds[ehci_batch->td_count]);
277}
278
279/** Prepare generic control transfer
280 *
281 * @param[in] ehci_batch Batch structure to use.
282 * @param[in] dir Communication direction
283 *
284 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
285 * Data stage with alternating toggle and direction supplied by parameter.
286 * Status stage with toggle 1 and direction supplied by parameter.
287 */
288static void batch_control(ehci_transfer_batch_t *ehci_batch, usb_direction_t dir)
289{
290 assert(ehci_batch);
291 assert(ehci_batch->usb_batch);
292 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
293#if 0
294 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ehci_batch->ed,
295 ehci_batch->ed->status, ehci_batch->ed->td_tail,
296 ehci_batch->ed->td_head, ehci_batch->ed->next);
297 static const usb_direction_t reverse_dir[] = {
298 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
299 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
300 };
301
302 int toggle = 0;
303 const char* buffer = ehci_batch->device_buffer;
304 const usb_direction_t data_dir = dir;
305 const usb_direction_t status_dir = reverse_dir[dir];
306
307 /* Setup stage */
308 td_init(
309 ehci_batch->tds[0], ehci_batch->tds[1], USB_DIRECTION_BOTH,
310 buffer, ehci_batch->usb_batch->setup_size, toggle);
311 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
312 ehci_batch->tds[0]->status, ehci_batch->tds[0]->cbp,
313 ehci_batch->tds[0]->next, ehci_batch->tds[0]->be);
314 buffer += ehci_batch->usb_batch->setup_size;
315
316 /* Data stage */
317 size_t td_current = 1;
318 size_t remain_size = ehci_batch->usb_batch->buffer_size;
319 while (remain_size > 0) {
320 const size_t transfer_size =
321 min(remain_size, EHCI_TD_MAX_TRANSFER);
322 toggle = 1 - toggle;
323
324 td_init(ehci_batch->tds[td_current],
325 ehci_batch->tds[td_current + 1],
326 data_dir, buffer, transfer_size, toggle);
327 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
328 ehci_batch->tds[td_current]->status,
329 ehci_batch->tds[td_current]->cbp,
330 ehci_batch->tds[td_current]->next,
331 ehci_batch->tds[td_current]->be);
332
333 buffer += transfer_size;
334 remain_size -= transfer_size;
335 assert(td_current < ehci_batch->td_count - 1);
336 ++td_current;
337 }
338
339 /* Status stage */
340 assert(td_current == ehci_batch->td_count - 1);
341 td_init(ehci_batch->tds[td_current], ehci_batch->tds[td_current + 1],
342 status_dir, NULL, 0, 1);
343 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
344 ehci_batch->tds[td_current]->status,
345 ehci_batch->tds[td_current]->cbp,
346 ehci_batch->tds[td_current]->next,
347 ehci_batch->tds[td_current]->be);
348#endif
349 usb_log_debug2(
350 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
351 ehci_batch->usb_batch,
352 usb_str_transfer_type(ehci_batch->usb_batch->ep->transfer_type),
353 usb_str_direction(dir),
354 USB_TRANSFER_BATCH_ARGS(*ehci_batch->usb_batch));
355}
356
357/** Prepare generic data transfer
358 *
359 * @param[in] ehci_batch Batch structure to use.
360 * @paramp[in] dir Communication direction.
361 *
362 * Direction is supplied by the associated ep and toggle is maintained by the
363 * EHCI hw in ED.
364 */
365static void batch_data(ehci_transfer_batch_t *ehci_batch, usb_direction_t dir)
366{
367 assert(ehci_batch);
368 assert(ehci_batch->usb_batch);
369 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
370#if 0
371 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ehci_batch->ed,
372 ehci_batch->ed->status, ehci_batch->ed->td_tail,
373 ehci_batch->ed->td_head, ehci_batch->ed->next);
374
375 size_t td_current = 0;
376 size_t remain_size = ehci_batch->usb_batch->buffer_size;
377 char *buffer = ehci_batch->device_buffer;
378 while (remain_size > 0) {
379 const size_t transfer_size = remain_size > EHCI_TD_MAX_TRANSFER
380 ? EHCI_TD_MAX_TRANSFER : remain_size;
381
382 td_init(
383 ehci_batch->tds[td_current], ehci_batch->tds[td_current + 1],
384 dir, buffer, transfer_size, -1);
385
386 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
387 ehci_batch->tds[td_current]->status,
388 ehci_batch->tds[td_current]->cbp,
389 ehci_batch->tds[td_current]->next,
390 ehci_batch->tds[td_current]->be);
391
392 buffer += transfer_size;
393 remain_size -= transfer_size;
394 assert(td_current < ehci_batch->td_count);
395 ++td_current;
396 }
397#endif
398 usb_log_debug2(
399 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
400 ehci_batch->usb_batch,
401 usb_str_transfer_type(ehci_batch->usb_batch->ep->transfer_type),
402 usb_str_direction(dir),
403 USB_TRANSFER_BATCH_ARGS(*ehci_batch->usb_batch));
404}
405
406/** Transfer setup table. */
407static void (*const batch_setup[])(ehci_transfer_batch_t*, usb_direction_t) =
408{
409 [USB_TRANSFER_CONTROL] = batch_control,
410 [USB_TRANSFER_BULK] = batch_data,
411 [USB_TRANSFER_INTERRUPT] = batch_data,
412 [USB_TRANSFER_ISOCHRONOUS] = NULL,
413};
414/**
415 * @}
416 */
417
Note: See TracBrowser for help on using the repository browser.