source: mainline/uspace/drv/bus/usb/ohci/ohci_batch.c@ 25a179e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25a179e was 58563585, checked in by Martin Decky <martin@…>, 9 years ago

code review and cstyle cleanup (no change in functionality)

  • 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
29/** @addtogroup drvusbohci
30 * @{
31 */
32/** @file
33 * @brief OHCI driver USB transaction structure
34 */
35
36#include <assert.h>
37#include <errno.h>
38#include <macros.h>
39#include <mem.h>
40#include <stdbool.h>
41
42#include <usb/usb.h>
43#include <usb/debug.h>
44#include <usb/host/utils/malloc32.h>
45
46#include "ohci_batch.h"
47#include "ohci_endpoint.h"
48
49static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t);
50
51/** Safely destructs ohci_transfer_batch_t structure
52 *
53 * @param[in] ohci_batch Instance to destroy.
54 */
55static void ohci_transfer_batch_dispose(ohci_transfer_batch_t *ohci_batch)
56{
57 if (!ohci_batch)
58 return;
59 if (ohci_batch->tds) {
60 const ohci_endpoint_t *ohci_ep =
61 ohci_endpoint_get(ohci_batch->usb_batch->ep);
62 assert(ohci_ep);
63 for (unsigned i = 0; i < ohci_batch->td_count; ++i) {
64 if (ohci_batch->tds[i] != ohci_ep->td)
65 free32(ohci_batch->tds[i]);
66 }
67 free(ohci_batch->tds);
68 }
69 usb_transfer_batch_destroy(ohci_batch->usb_batch);
70 free32(ohci_batch->device_buffer);
71 free(ohci_batch);
72}
73
74/** Finishes usb_transfer_batch and destroys the structure.
75 *
76 * @param[in] uhci_batch Instance to finish and destroy.
77 */
78void ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *ohci_batch)
79{
80 assert(ohci_batch);
81 assert(ohci_batch->usb_batch);
82 usb_transfer_batch_finish(ohci_batch->usb_batch,
83 ohci_batch->device_buffer + ohci_batch->usb_batch->setup_size);
84 ohci_transfer_batch_dispose(ohci_batch);
85}
86
87/** Allocate memory and initialize internal data structure.
88 *
89 * @param[in] usb_batch Pointer to generic USB batch structure.
90 * @return Valid pointer if all structures were successfully created,
91 * NULL otherwise.
92 *
93 * Determines the number of needed transfer descriptors (TDs).
94 * Prepares a transport buffer (that is accessible by the hardware).
95 * Initializes parameters needed for the transfer and callback.
96 */
97ohci_transfer_batch_t * ohci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
98{
99 assert(usb_batch);
100
101 ohci_transfer_batch_t *ohci_batch =
102 calloc(1, sizeof(ohci_transfer_batch_t));
103 if (!ohci_batch) {
104 usb_log_error("Failed to allocate OHCI batch data.");
105 goto dispose;
106 }
107 link_initialize(&ohci_batch->link);
108 ohci_batch->td_count =
109 (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
110 / OHCI_TD_MAX_TRANSFER;
111 /* Control transfer need Setup and Status stage */
112 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
113 ohci_batch->td_count += 2;
114 }
115
116 /* We need an extra place for TD that was left at ED */
117 ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
118 if (!ohci_batch->tds) {
119 usb_log_error("Failed to allocate OHCI transfer descriptors.");
120 goto dispose;
121 }
122
123 /* Add TD left over by the previous transfer */
124 ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
125 ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
126
127 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
128 ohci_batch->tds[i] = malloc32(sizeof(td_t));
129 if (!ohci_batch->tds[i]) {
130 usb_log_error("Failed to allocate TD %d.", i);
131 goto dispose;
132 }
133 }
134
135
136 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
137 * it is, however, not capable of handling buffer that occupies more
138 * than two pages (the first page is computed using start pointer, the
139 * other using the end pointer) */
140 if (usb_batch->setup_size + usb_batch->buffer_size > 0) {
141 /* Use one buffer for setup and data stage */
142 ohci_batch->device_buffer =
143 malloc32(usb_batch->setup_size + usb_batch->buffer_size);
144 if (!ohci_batch->device_buffer) {
145 usb_log_error("Failed to allocate device buffer");
146 goto dispose;
147 }
148 /* Copy setup data */
149 memcpy(ohci_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 ohci_batch->device_buffer + usb_batch->setup_size,
155 usb_batch->buffer, usb_batch->buffer_size);
156 }
157 ohci_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](ohci_batch, dir);
162
163 return ohci_batch;
164dispose:
165 ohci_transfer_batch_dispose(ohci_batch);
166 return NULL;
167}
168
169/** Check batch TDs' status.
170 *
171 * @param[in] ohci_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 ohci_transfer_batch_is_complete(const ohci_transfer_batch_t *ohci_batch)
179{
180 assert(ohci_batch);
181 assert(ohci_batch->usb_batch);
182
183 usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
184 ohci_batch->usb_batch, ohci_batch->td_count);
185 usb_log_debug2("ED: %08x:%08x:%08x:%08x.\n",
186 ohci_batch->ed->status, ohci_batch->ed->td_head,
187 ohci_batch->ed->td_tail, ohci_batch->ed->next);
188
189 if (!ed_inactive(ohci_batch->ed) && ed_transfer_pending(ohci_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 ohci_batch->usb_batch->transfered_size =
197 ohci_batch->usb_batch->buffer_size;
198
199 /* Assume we will leave the last(unused) TD behind */
200 unsigned leave_td = ohci_batch->td_count;
201
202 /* Check all TDs */
203 for (size_t i = 0; i < ohci_batch->td_count; ++i) {
204 assert(ohci_batch->tds[i] != NULL);
205 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
206 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
207 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
208
209 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
210 if (ohci_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 ohci_batch->usb_batch->transfered_size
224 -= td_remain_size(ohci_batch->tds[i]);
225 } else {
226 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
227 ohci_batch->usb_batch, i,
228 ohci_batch->tds[i]->status);
229
230 /* ED should be stopped because of errors */
231 assert((ohci_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(ohci_batch->ed) ==
243 addr_to_phys(ohci_batch->tds[leave_td]));
244
245 /* Set tail to the same TD */
246 ed_set_tail_td(ohci_batch->ed,
247 ohci_batch->tds[leave_td]);
248
249 /* Clear possible ED HALT */
250 ed_clear_halt(ohci_batch->ed);
251 break;
252 }
253 }
254 assert(ohci_batch->usb_batch->transfered_size <=
255 ohci_batch->usb_batch->buffer_size);
256
257 /* Store the remaining TD */
258 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
259 assert(ohci_ep);
260 ohci_ep->td = ohci_batch->tds[leave_td];
261
262 /* Make sure that we are leaving the right TD behind */
263 assert(addr_to_phys(ohci_ep->td) == ed_head_td(ohci_batch->ed));
264 assert(addr_to_phys(ohci_ep->td) == ed_tail_td(ohci_batch->ed));
265
266 return true;
267}
268
269/** Starts execution of the TD list
270 *
271 * @param[in] ohci_batch Batch structure to use
272 */
273void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
274{
275 assert(ohci_batch);
276 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
277}
278
279/** Prepare generic control transfer
280 *
281 * @param[in] ohci_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(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
289{
290 assert(ohci_batch);
291 assert(ohci_batch->usb_batch);
292 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
293 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
294 ohci_batch->ed->status, ohci_batch->ed->td_tail,
295 ohci_batch->ed->td_head, ohci_batch->ed->next);
296 static const usb_direction_t reverse_dir[] = {
297 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
298 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
299 };
300
301 int toggle = 0;
302 const char* buffer = ohci_batch->device_buffer;
303 const usb_direction_t data_dir = dir;
304 const usb_direction_t status_dir = reverse_dir[dir];
305
306 /* Setup stage */
307 td_init(
308 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
309 buffer, ohci_batch->usb_batch->setup_size, toggle);
310 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
311 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
312 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
313 buffer += ohci_batch->usb_batch->setup_size;
314
315 /* Data stage */
316 size_t td_current = 1;
317 size_t remain_size = ohci_batch->usb_batch->buffer_size;
318 while (remain_size > 0) {
319 const size_t transfer_size =
320 min(remain_size, OHCI_TD_MAX_TRANSFER);
321 toggle = 1 - toggle;
322
323 td_init(ohci_batch->tds[td_current],
324 ohci_batch->tds[td_current + 1],
325 data_dir, buffer, transfer_size, toggle);
326 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
327 ohci_batch->tds[td_current]->status,
328 ohci_batch->tds[td_current]->cbp,
329 ohci_batch->tds[td_current]->next,
330 ohci_batch->tds[td_current]->be);
331
332 buffer += transfer_size;
333 remain_size -= transfer_size;
334 assert(td_current < ohci_batch->td_count - 1);
335 ++td_current;
336 }
337
338 /* Status stage */
339 assert(td_current == ohci_batch->td_count - 1);
340 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
341 status_dir, NULL, 0, 1);
342 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
343 ohci_batch->tds[td_current]->status,
344 ohci_batch->tds[td_current]->cbp,
345 ohci_batch->tds[td_current]->next,
346 ohci_batch->tds[td_current]->be);
347 usb_log_debug2(
348 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
349 ohci_batch->usb_batch,
350 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
351 usb_str_direction(dir),
352 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
353}
354
355/** Prepare generic data transfer
356 *
357 * @param[in] ohci_batch Batch structure to use.
358 * @paramp[in] dir Communication direction.
359 *
360 * Direction is supplied by the associated ep and toggle is maintained by the
361 * OHCI hw in ED.
362 */
363static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
364{
365 assert(ohci_batch);
366 assert(ohci_batch->usb_batch);
367 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
368 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
369 ohci_batch->ed->status, ohci_batch->ed->td_tail,
370 ohci_batch->ed->td_head, ohci_batch->ed->next);
371
372 size_t td_current = 0;
373 size_t remain_size = ohci_batch->usb_batch->buffer_size;
374 char *buffer = ohci_batch->device_buffer;
375 while (remain_size > 0) {
376 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
377 ? OHCI_TD_MAX_TRANSFER : remain_size;
378
379 td_init(
380 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
381 dir, buffer, transfer_size, -1);
382
383 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
384 ohci_batch->tds[td_current]->status,
385 ohci_batch->tds[td_current]->cbp,
386 ohci_batch->tds[td_current]->next,
387 ohci_batch->tds[td_current]->be);
388
389 buffer += transfer_size;
390 remain_size -= transfer_size;
391 assert(td_current < ohci_batch->td_count);
392 ++td_current;
393 }
394 usb_log_debug2(
395 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
396 ohci_batch->usb_batch,
397 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
398 usb_str_direction(dir),
399 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
400}
401
402/** Transfer setup table. */
403static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
404{
405 [USB_TRANSFER_CONTROL] = batch_control,
406 [USB_TRANSFER_BULK] = batch_data,
407 [USB_TRANSFER_INTERRUPT] = batch_data,
408 [USB_TRANSFER_ISOCHRONOUS] = NULL,
409};
410/**
411 * @}
412 */
Note: See TracBrowser for help on using the repository browser.