source: mainline/uspace/drv/bus/usb/ohci/ohci_batch.c@ 6de4b4a1

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

ohci: Remove error checking macro

  • Property mode set to 100644
File size: 13.8 KB
Line 
1/*
2 * Copyright (c) 2011 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 drvusbohci
29 * @{
30 */
31/** @file
32 * @brief OHCI 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 "ohci_batch.h"
45#include "ohci_endpoint.h"
46#include "utils/malloc32.h"
47
48static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t);
49
50/** Safely destructs ohci_transfer_batch_t structure
51 *
52 * @param[in] ohci_batch Instance to destroy.
53 */
54static void ohci_transfer_batch_dispose(ohci_transfer_batch_t *ohci_batch)
55{
56 if (!ohci_batch)
57 return;
58 if (ohci_batch->tds) {
59 const ohci_endpoint_t *ohci_ep =
60 ohci_endpoint_get(ohci_batch->usb_batch->ep);
61 assert(ohci_ep);
62 for (unsigned i = 0; i < ohci_batch->td_count; ++i) {
63 if (ohci_batch->tds[i] != ohci_ep->td)
64 free32(ohci_batch->tds[i]);
65 }
66 free(ohci_batch->tds);
67 }
68 usb_transfer_batch_destroy(ohci_batch->usb_batch);
69 free32(ohci_batch->device_buffer);
70 free(ohci_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 ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *ohci_batch)
78{
79 assert(ohci_batch);
80 assert(ohci_batch->usb_batch);
81 usb_transfer_batch_finish(ohci_batch->usb_batch,
82 ohci_batch->device_buffer + ohci_batch->usb_batch->setup_size);
83 ohci_transfer_batch_dispose(ohci_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 */
96ohci_transfer_batch_t * ohci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
97{
98 assert(usb_batch);
99
100 ohci_transfer_batch_t *ohci_batch =
101 calloc(1, sizeof(ohci_transfer_batch_t));
102 if (!ohci_batch) {
103 usb_log_error("Failed to allocate OHCI batch data.");
104 goto dispose;
105 }
106 link_initialize(&ohci_batch->link);
107 ohci_batch->td_count =
108 (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
109 / OHCI_TD_MAX_TRANSFER;
110 /* Control transfer need Setup and Status stage */
111 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
112 ohci_batch->td_count += 2;
113 }
114
115 /* We need an extra place for TD that was left at ED */
116 ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
117 if (!ohci_batch->tds) {
118 usb_log_error("Failed to allocate OHCI transfer descriptors.");
119 goto dispose;
120 }
121
122 /* Add TD left over by the previous transfer */
123 ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
124 ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
125
126 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
127 ohci_batch->tds[i] = malloc32(sizeof(td_t));
128 if (!ohci_batch->tds[i]) {
129 usb_log_error("Failed to allocate TD %d.", i);
130 goto dispose;
131 }
132 }
133
134
135 /* NOTE: OHCI 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 ohci_batch->device_buffer =
142 malloc32(usb_batch->setup_size + usb_batch->buffer_size);
143 if (!ohci_batch->device_buffer) {
144 usb_log_error("Failed to allocate device buffer");
145 goto dispose;
146 }
147 /* Copy setup data */
148 memcpy(ohci_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 ohci_batch->device_buffer + usb_batch->setup_size,
154 usb_batch->buffer, usb_batch->buffer_size);
155 }
156 ohci_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](ohci_batch, dir);
161
162 return ohci_batch;
163dispose:
164 ohci_transfer_batch_dispose(ohci_batch);
165 return NULL;
166}
167
168/** Check batch TDs' status.
169 *
170 * @param[in] ohci_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 ohci_transfer_batch_is_complete(const ohci_transfer_batch_t *ohci_batch)
178{
179 assert(ohci_batch);
180 assert(ohci_batch->usb_batch);
181
182 usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
183 ohci_batch->usb_batch, ohci_batch->td_count);
184 usb_log_debug2("ED: %08x:%08x:%08x:%08x.\n",
185 ohci_batch->ed->status, ohci_batch->ed->td_head,
186 ohci_batch->ed->td_tail, ohci_batch->ed->next);
187
188 if (!ed_inactive(ohci_batch->ed) && ed_transfer_pending(ohci_batch->ed))
189 return false;
190
191 /* Now we may be sure that either the ED is inactive because of errors
192 * or all transfer descriptors completed successfully */
193
194 /* Assume all data got through */
195 ohci_batch->usb_batch->transfered_size =
196 ohci_batch->usb_batch->buffer_size;
197
198 /* Assume we will leave the last(unused) TD behind */
199 unsigned leave_td = ohci_batch->td_count;
200
201 /* Check all TDs */
202 for (size_t i = 0; i < ohci_batch->td_count; ++i) {
203 assert(ohci_batch->tds[i] != NULL);
204 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
205 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
206 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
207
208 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
209 if (ohci_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 ohci_batch->usb_batch->transfered_size
223 -= td_remain_size(ohci_batch->tds[i]);
224 } else {
225 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
226 ohci_batch->usb_batch, i,
227 ohci_batch->tds[i]->status);
228
229 /* ED should be stopped because of errors */
230 assert((ohci_batch->ed->td_head & ED_TDHEAD_HALTED_FLAG) != 0);
231
232 /* Now we have a problem: we don't know what TD
233 * the head pointer points to, the retiring rules
234 * described in specs say it should be the one after
235 * the failed one so set the tail pointer accordingly.
236 * It will be the one TD we leave behind.
237 */
238 leave_td = i + 1;
239
240 /* Check TD assumption */
241 assert(ed_head_td(ohci_batch->ed) ==
242 addr_to_phys(ohci_batch->tds[leave_td]));
243
244 /* Set tail to the same TD */
245 ed_set_tail_td(ohci_batch->ed,
246 ohci_batch->tds[leave_td]);
247
248 /* Clear possible ED HALT */
249 ed_clear_halt(ohci_batch->ed);
250 break;
251 }
252 }
253 assert(ohci_batch->usb_batch->transfered_size <=
254 ohci_batch->usb_batch->buffer_size);
255
256 /* Store the remaining TD */
257 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
258 assert(ohci_ep);
259 ohci_ep->td = ohci_batch->tds[leave_td];
260
261 /* Make sure that we are leaving the right TD behind */
262 assert(addr_to_phys(ohci_ep->td) == ed_head_td(ohci_batch->ed));
263 assert(addr_to_phys(ohci_ep->td) == ed_tail_td(ohci_batch->ed));
264
265 return true;
266}
267
268/** Starts execution of the TD list
269 *
270 * @param[in] ohci_batch Batch structure to use
271 */
272void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
273{
274 assert(ohci_batch);
275 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
276}
277
278/** Prepare generic control transfer
279 *
280 * @param[in] ohci_batch Batch structure to use.
281 * @param[in] dir Communication direction
282 *
283 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
284 * Data stage with alternating toggle and direction supplied by parameter.
285 * Status stage with toggle 1 and direction supplied by parameter.
286 */
287static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
288{
289 assert(ohci_batch);
290 assert(ohci_batch->usb_batch);
291 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
292 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
293 ohci_batch->ed->status, ohci_batch->ed->td_tail,
294 ohci_batch->ed->td_head, ohci_batch->ed->next);
295 static const usb_direction_t reverse_dir[] = {
296 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
297 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
298 };
299
300 int toggle = 0;
301 const char* buffer = ohci_batch->device_buffer;
302 const usb_direction_t data_dir = dir;
303 const usb_direction_t status_dir = reverse_dir[dir];
304
305 /* Setup stage */
306 td_init(
307 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
308 buffer, ohci_batch->usb_batch->setup_size, toggle);
309 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
310 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
311 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
312 buffer += ohci_batch->usb_batch->setup_size;
313
314 /* Data stage */
315 size_t td_current = 1;
316 size_t remain_size = ohci_batch->usb_batch->buffer_size;
317 while (remain_size > 0) {
318 const size_t transfer_size =
319 min(remain_size, OHCI_TD_MAX_TRANSFER);
320 toggle = 1 - toggle;
321
322 td_init(ohci_batch->tds[td_current],
323 ohci_batch->tds[td_current + 1],
324 data_dir, buffer, transfer_size, toggle);
325 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
326 ohci_batch->tds[td_current]->status,
327 ohci_batch->tds[td_current]->cbp,
328 ohci_batch->tds[td_current]->next,
329 ohci_batch->tds[td_current]->be);
330
331 buffer += transfer_size;
332 remain_size -= transfer_size;
333 assert(td_current < ohci_batch->td_count - 1);
334 ++td_current;
335 }
336
337 /* Status stage */
338 assert(td_current == ohci_batch->td_count - 1);
339 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
340 status_dir, NULL, 0, 1);
341 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
342 ohci_batch->tds[td_current]->status,
343 ohci_batch->tds[td_current]->cbp,
344 ohci_batch->tds[td_current]->next,
345 ohci_batch->tds[td_current]->be);
346 usb_log_debug2(
347 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
348 ohci_batch->usb_batch,
349 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
350 usb_str_direction(dir),
351 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
352}
353
354/** Prepare generic data transfer
355 *
356 * @param[in] ohci_batch Batch structure to use.
357 * @paramp[in] dir Communication direction.
358 *
359 * Direction is supplied by the associated ep and toggle is maintained by the
360 * OHCI hw in ED.
361 */
362static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
363{
364 assert(ohci_batch);
365 assert(ohci_batch->usb_batch);
366 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
367 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
368 ohci_batch->ed->status, ohci_batch->ed->td_tail,
369 ohci_batch->ed->td_head, ohci_batch->ed->next);
370
371 size_t td_current = 0;
372 size_t remain_size = ohci_batch->usb_batch->buffer_size;
373 char *buffer = ohci_batch->device_buffer;
374 while (remain_size > 0) {
375 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
376 ? OHCI_TD_MAX_TRANSFER : remain_size;
377
378 td_init(
379 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
380 dir, buffer, transfer_size, -1);
381
382 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
383 ohci_batch->tds[td_current]->status,
384 ohci_batch->tds[td_current]->cbp,
385 ohci_batch->tds[td_current]->next,
386 ohci_batch->tds[td_current]->be);
387
388 buffer += transfer_size;
389 remain_size -= transfer_size;
390 assert(td_current < ohci_batch->td_count);
391 ++td_current;
392 }
393 usb_log_debug2(
394 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
395 ohci_batch->usb_batch,
396 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
397 usb_str_direction(dir),
398 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
399}
400
401/** Transfer setup table. */
402static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
403{
404 [USB_TRANSFER_CONTROL] = batch_control,
405 [USB_TRANSFER_BULK] = batch_data,
406 [USB_TRANSFER_INTERRUPT] = batch_data,
407 [USB_TRANSFER_ISOCHRONOUS] = NULL,
408};
409/**
410 * @}
411 */
Note: See TracBrowser for help on using the repository browser.