source: mainline/uspace/drv/bus/usb/ohci/ohci_batch.c@ 4e732f1a

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

ohci: Sanitize includes.

Include what you use.

  • Property mode set to 100644
File size: 13.9 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#define CHECK_NULL_DISPOSE_RET(ptr, message...) \
100if (ptr == NULL) { \
101 usb_log_error(message); \
102 ohci_transfer_batch_dispose(ohci_batch); \
103 return NULL; \
104} else (void)0
105
106 ohci_transfer_batch_t *ohci_batch =
107 calloc(1, sizeof(ohci_transfer_batch_t));
108 CHECK_NULL_DISPOSE_RET(ohci_batch,
109 "Failed to allocate OHCI batch data.\n");
110 link_initialize(&ohci_batch->link);
111 ohci_batch->td_count =
112 (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
113 / OHCI_TD_MAX_TRANSFER;
114 /* Control transfer need Setup and Status stage */
115 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
116 ohci_batch->td_count += 2;
117 }
118
119 /* We need an extra place for TD that was left at ED */
120 ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
121 CHECK_NULL_DISPOSE_RET(ohci_batch->tds,
122 "Failed to allocate OHCI transfer descriptors.\n");
123
124 /* Add TD left over by the previous transfer */
125 ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
126 ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
127
128 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
129 ohci_batch->tds[i] = malloc32(sizeof(td_t));
130 CHECK_NULL_DISPOSE_RET(ohci_batch->tds[i],
131 "Failed to allocate TD %d.\n", i );
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 CHECK_NULL_DISPOSE_RET(ohci_batch->device_buffer,
144 "Failed to allocate device accessible buffer.\n");
145 /* Copy setup data */
146 memcpy(ohci_batch->device_buffer, usb_batch->setup_buffer,
147 usb_batch->setup_size);
148 /* Copy generic data */
149 if (usb_batch->ep->direction != USB_DIRECTION_IN)
150 memcpy(
151 ohci_batch->device_buffer + usb_batch->setup_size,
152 usb_batch->buffer, usb_batch->buffer_size);
153 }
154 ohci_batch->usb_batch = usb_batch;
155
156 const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
157 assert(batch_setup[usb_batch->ep->transfer_type]);
158 batch_setup[usb_batch->ep->transfer_type](ohci_batch, dir);
159
160 return ohci_batch;
161#undef CHECK_NULL_DISPOSE_RET
162}
163
164/** Check batch TDs' status.
165 *
166 * @param[in] ohci_batch Batch structure to use.
167 * @return False, if there is an active TD, true otherwise.
168 *
169 * Walk all TDs (usually there is just one). Stop with false if there is an
170 * active TD. Stop with true if an error is found. Return true if the walk
171 * completes with the last TD.
172 */
173bool ohci_transfer_batch_is_complete(const ohci_transfer_batch_t *ohci_batch)
174{
175 assert(ohci_batch);
176 assert(ohci_batch->usb_batch);
177
178 usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
179 ohci_batch->usb_batch, ohci_batch->td_count);
180 usb_log_debug2("ED: %08x:%08x:%08x:%08x.\n",
181 ohci_batch->ed->status, ohci_batch->ed->td_head,
182 ohci_batch->ed->td_tail, ohci_batch->ed->next);
183
184 if (!ed_inactive(ohci_batch->ed) && ed_transfer_pending(ohci_batch->ed))
185 return false;
186
187 /* Now we may be sure that either the ED is inactive because of errors
188 * or all transfer descriptors completed successfully */
189
190 /* Assume all data got through */
191 ohci_batch->usb_batch->transfered_size =
192 ohci_batch->usb_batch->buffer_size;
193
194 /* Assume we will leave the last(unused) TD behind */
195 unsigned leave_td = ohci_batch->td_count;
196
197 /* Check all TDs */
198 for (size_t i = 0; i < ohci_batch->td_count; ++i) {
199 assert(ohci_batch->tds[i] != NULL);
200 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
201 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
202 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
203
204 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
205 if (ohci_batch->usb_batch->error == EOK) {
206 /* If the TD got all its data through, it will report
207 * 0 bytes remain, the sole exception is INPUT with
208 * data rounding flag (short), i.e. every INPUT.
209 * Nice thing is that short packets will correctly
210 * report remaining data, thus making this computation
211 * correct (short packets need to be produced by the
212 * last TD)
213 * NOTE: This also works for CONTROL transfer as
214 * the first TD will return 0 remain.
215 * NOTE: Short packets don't break the assumption that
216 * we leave the very last(unused) TD behind.
217 */
218 ohci_batch->usb_batch->transfered_size
219 -= td_remain_size(ohci_batch->tds[i]);
220 } else {
221 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
222 ohci_batch->usb_batch, i,
223 ohci_batch->tds[i]->status);
224
225 /* ED should be stopped because of errors */
226 assert((ohci_batch->ed->td_head & ED_TDHEAD_HALTED_FLAG) != 0);
227
228 /* Now we have a problem: we don't know what TD
229 * the head pointer points to, the retiring rules
230 * described in specs say it should be the one after
231 * the failed one so set the tail pointer accordingly.
232 * It will be the one TD we leave behind.
233 */
234 leave_td = i + 1;
235
236 /* Check TD assumption */
237 assert(ed_head_td(ohci_batch->ed) ==
238 addr_to_phys(ohci_batch->tds[leave_td]));
239
240 /* Set tail to the same TD */
241 ed_set_tail_td(ohci_batch->ed,
242 ohci_batch->tds[leave_td]);
243
244 /* Clear possible ED HALT */
245 ed_clear_halt(ohci_batch->ed);
246 break;
247 }
248 }
249 assert(ohci_batch->usb_batch->transfered_size <=
250 ohci_batch->usb_batch->buffer_size);
251
252 /* Store the remaining TD */
253 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
254 assert(ohci_ep);
255 ohci_ep->td = ohci_batch->tds[leave_td];
256
257 /* Make sure that we are leaving the right TD behind */
258 assert(addr_to_phys(ohci_ep->td) == ed_head_td(ohci_batch->ed));
259 assert(addr_to_phys(ohci_ep->td) == ed_tail_td(ohci_batch->ed));
260
261 return true;
262}
263
264/** Starts execution of the TD list
265 *
266 * @param[in] ohci_batch Batch structure to use
267 */
268void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
269{
270 assert(ohci_batch);
271 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
272}
273
274/** Prepare generic control transfer
275 *
276 * @param[in] ohci_batch Batch structure to use.
277 * @param[in] dir Communication direction
278 *
279 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
280 * Data stage with alternating toggle and direction supplied by parameter.
281 * Status stage with toggle 1 and direction supplied by parameter.
282 */
283static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
284{
285 assert(ohci_batch);
286 assert(ohci_batch->usb_batch);
287 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
288 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
289 ohci_batch->ed->status, ohci_batch->ed->td_tail,
290 ohci_batch->ed->td_head, ohci_batch->ed->next);
291 static const usb_direction_t reverse_dir[] = {
292 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
293 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
294 };
295
296 int toggle = 0;
297 const char* buffer = ohci_batch->device_buffer;
298 const usb_direction_t data_dir = dir;
299 const usb_direction_t status_dir = reverse_dir[dir];
300
301 /* Setup stage */
302 td_init(
303 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
304 buffer, ohci_batch->usb_batch->setup_size, toggle);
305 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
306 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
307 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
308 buffer += ohci_batch->usb_batch->setup_size;
309
310 /* Data stage */
311 size_t td_current = 1;
312 size_t remain_size = ohci_batch->usb_batch->buffer_size;
313 while (remain_size > 0) {
314 const size_t transfer_size =
315 min(remain_size, OHCI_TD_MAX_TRANSFER);
316 toggle = 1 - toggle;
317
318 td_init(ohci_batch->tds[td_current],
319 ohci_batch->tds[td_current + 1],
320 data_dir, buffer, transfer_size, toggle);
321 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
322 ohci_batch->tds[td_current]->status,
323 ohci_batch->tds[td_current]->cbp,
324 ohci_batch->tds[td_current]->next,
325 ohci_batch->tds[td_current]->be);
326
327 buffer += transfer_size;
328 remain_size -= transfer_size;
329 assert(td_current < ohci_batch->td_count - 1);
330 ++td_current;
331 }
332
333 /* Status stage */
334 assert(td_current == ohci_batch->td_count - 1);
335 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
336 status_dir, NULL, 0, 1);
337 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
338 ohci_batch->tds[td_current]->status,
339 ohci_batch->tds[td_current]->cbp,
340 ohci_batch->tds[td_current]->next,
341 ohci_batch->tds[td_current]->be);
342 usb_log_debug2(
343 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
344 ohci_batch->usb_batch,
345 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
346 usb_str_direction(dir),
347 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
348}
349
350/** Prepare generic data transfer
351 *
352 * @param[in] ohci_batch Batch structure to use.
353 * @paramp[in] dir Communication direction.
354 *
355 * Direction is supplied by the associated ep and toggle is maintained by the
356 * OHCI hw in ED.
357 */
358static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
359{
360 assert(ohci_batch);
361 assert(ohci_batch->usb_batch);
362 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
363 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
364 ohci_batch->ed->status, ohci_batch->ed->td_tail,
365 ohci_batch->ed->td_head, ohci_batch->ed->next);
366
367 size_t td_current = 0;
368 size_t remain_size = ohci_batch->usb_batch->buffer_size;
369 char *buffer = ohci_batch->device_buffer;
370 while (remain_size > 0) {
371 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
372 ? OHCI_TD_MAX_TRANSFER : remain_size;
373
374 td_init(
375 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
376 dir, buffer, transfer_size, -1);
377
378 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
379 ohci_batch->tds[td_current]->status,
380 ohci_batch->tds[td_current]->cbp,
381 ohci_batch->tds[td_current]->next,
382 ohci_batch->tds[td_current]->be);
383
384 buffer += transfer_size;
385 remain_size -= transfer_size;
386 assert(td_current < ohci_batch->td_count);
387 ++td_current;
388 }
389 usb_log_debug2(
390 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
391 ohci_batch->usb_batch,
392 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
393 usb_str_direction(dir),
394 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
395}
396
397/** Transfer setup table. */
398static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
399{
400 [USB_TRANSFER_CONTROL] = batch_control,
401 [USB_TRANSFER_BULK] = batch_data,
402 [USB_TRANSFER_INTERRUPT] = batch_data,
403 [USB_TRANSFER_ISOCHRONOUS] = NULL,
404};
405/**
406 * @}
407 */
Note: See TracBrowser for help on using the repository browser.