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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5c1051 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
RevLine 
[41b96b4]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 */
[58563585]28
[81dce9f]29/** @addtogroup drvusbohci
[41b96b4]30 * @{
31 */
32/** @file
33 * @brief OHCI driver USB transaction structure
34 */
[0d4b110]35
36#include <assert.h>
[41b96b4]37#include <errno.h>
[3f162ab]38#include <macros.h>
[0d4b110]39#include <mem.h>
40#include <stdbool.h>
[41b96b4]41
42#include <usb/usb.h>
43#include <usb/debug.h>
[2dbfe44]44#include <usb/host/utils/malloc32.h>
[41b96b4]45
[ff6dd73]46#include "ohci_batch.h"
[e20eaed]47#include "ohci_endpoint.h"
[09ace19]48
[790318e]49static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t);
[76fbd9a]50
[28d9c95]51/** Safely destructs ohci_transfer_batch_t structure
52 *
53 * @param[in] ohci_batch Instance to destroy.
54 */
[9c10e51]55static void ohci_transfer_batch_dispose(ohci_transfer_batch_t *ohci_batch)
[2cc6e97]56{
[9c10e51]57 if (!ohci_batch)
[9a6fde4]58 return;
[9c10e51]59 if (ohci_batch->tds) {
[11cb0565]60 const ohci_endpoint_t *ohci_ep =
61 ohci_endpoint_get(ohci_batch->usb_batch->ep);
62 assert(ohci_ep);
[9b8958b]63 for (unsigned i = 0; i < ohci_batch->td_count; ++i) {
[11cb0565]64 if (ohci_batch->tds[i] != ohci_ep->td)
[9c10e51]65 free32(ohci_batch->tds[i]);
[9a6fde4]66 }
[9c10e51]67 free(ohci_batch->tds);
[9a6fde4]68 }
[549ff23]69 usb_transfer_batch_destroy(ohci_batch->usb_batch);
[9c10e51]70 free32(ohci_batch->device_buffer);
71 free(ohci_batch);
[2cc6e97]72}
[76fbd9a]73
[6fa04db]74/** Finishes usb_transfer_batch and destroys the structure.
75 *
76 * @param[in] uhci_batch Instance to finish and destroy.
77 */
[9c10e51]78void ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *ohci_batch)
[e2976bb]79{
[9c10e51]80 assert(ohci_batch);
81 assert(ohci_batch->usb_batch);
82 usb_transfer_batch_finish(ohci_batch->usb_batch,
[5cfcc64]83 ohci_batch->device_buffer + ohci_batch->usb_batch->setup_size);
[9c10e51]84 ohci_transfer_batch_dispose(ohci_batch);
85}
[76fbd9a]86
[6fa04db]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 */
[9c10e51]97ohci_transfer_batch_t * ohci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
98{
99 assert(usb_batch);
[e2976bb]100
[9c10e51]101 ohci_transfer_batch_t *ohci_batch =
[8e3d17f]102 calloc(1, sizeof(ohci_transfer_batch_t));
[584aa38]103 if (!ohci_batch) {
104 usb_log_error("Failed to allocate OHCI batch data.");
105 goto dispose;
106 }
[7bce1fc]107 link_initialize(&ohci_batch->link);
[9c10e51]108 ohci_batch->td_count =
109 (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
110 / OHCI_TD_MAX_TRANSFER;
[e2976bb]111 /* Control transfer need Setup and Status stage */
[9c10e51]112 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
113 ohci_batch->td_count += 2;
[e2976bb]114 }
115
[f974519]116 /* We need an extra place for TD that was left at ED */
[9c10e51]117 ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
[584aa38]118 if (!ohci_batch->tds) {
119 usb_log_error("Failed to allocate OHCI transfer descriptors.");
120 goto dispose;
121 }
[e2976bb]122
123 /* Add TD left over by the previous transfer */
[9c10e51]124 ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
125 ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
[9b8958b]126
127 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
[9c10e51]128 ohci_batch->tds[i] = malloc32(sizeof(td_t));
[584aa38]129 if (!ohci_batch->tds[i]) {
130 usb_log_error("Failed to allocate TD %d.", i);
131 goto dispose;
132 }
[e2976bb]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) */
[9c10e51]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);
[584aa38]144 if (!ohci_batch->device_buffer) {
145 usb_log_error("Failed to allocate device buffer");
146 goto dispose;
147 }
[f974519]148 /* Copy setup data */
[9c10e51]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);
[e2976bb]156 }
[9c10e51]157 ohci_batch->usb_batch = usb_batch;
[e2976bb]158
[058fd76]159 const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
[790318e]160 assert(batch_setup[usb_batch->ep->transfer_type]);
161 batch_setup[usb_batch->ep->transfer_type](ohci_batch, dir);
[90dd59dc]162
[9c10e51]163 return ohci_batch;
[584aa38]164dispose:
165 ohci_transfer_batch_dispose(ohci_batch);
166 return NULL;
[e2976bb]167}
[76fbd9a]168
[28d9c95]169/** Check batch TDs' status.
170 *
[5f57929]171 * @param[in] ohci_batch Batch structure to use.
[28d9c95]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 */
[11cb0565]178bool ohci_transfer_batch_is_complete(const ohci_transfer_batch_t *ohci_batch)
[f98b8269]179{
[9c10e51]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);
[49a98c8]185 usb_log_debug2("ED: %08x:%08x:%08x:%08x.\n",
[9c10e51]186 ohci_batch->ed->status, ohci_batch->ed->td_head,
187 ohci_batch->ed->td_tail, ohci_batch->ed->next);
188
[d5abaf4]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;
[dab3112]198
199 /* Assume we will leave the last(unused) TD behind */
[11cb0565]200 unsigned leave_td = ohci_batch->td_count;
[d5abaf4]201
202 /* Check all TDs */
203 for (size_t i = 0; i < ohci_batch->td_count; ++i) {
[9c10e51]204 assert(ohci_batch->tds[i] != NULL);
[49a98c8]205 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
[9c10e51]206 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
207 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
[d5abaf4]208
[9c10e51]209 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
[7f7e6f5]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 {
[49a98c8]226 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
[9c10e51]227 ohci_batch->usb_batch, i,
228 ohci_batch->tds[i]->status);
[d5abaf4]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 */
[11cb0565]239 leave_td = i + 1;
[d5abaf4]240
241 /* Check TD assumption */
[65eac7b]242 assert(ed_head_td(ohci_batch->ed) ==
243 addr_to_phys(ohci_batch->tds[leave_td]));
[d5abaf4]244
[65eac7b]245 /* Set tail to the same TD */
[d5abaf4]246 ed_set_tail_td(ohci_batch->ed,
[11cb0565]247 ohci_batch->tds[leave_td]);
[d5abaf4]248
249 /* Clear possible ED HALT */
[65eac7b]250 ed_clear_halt(ohci_batch->ed);
[9a6fde4]251 break;
[f1be95c8]252 }
253 }
[d5abaf4]254 assert(ohci_batch->usb_batch->transfered_size <=
255 ohci_batch->usb_batch->buffer_size);
[e20eaed]256
[d5abaf4]257 /* Store the remaining TD */
[9c10e51]258 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
[e20eaed]259 assert(ohci_ep);
[11cb0565]260 ohci_ep->td = ohci_batch->tds[leave_td];
[dab3112]261
262 /* Make sure that we are leaving the right TD behind */
[65eac7b]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));
[d6522dd]265
[f1be95c8]266 return true;
[f98b8269]267}
[76fbd9a]268
[28d9c95]269/** Starts execution of the TD list
270 *
[5f57929]271 * @param[in] ohci_batch Batch structure to use
[28d9c95]272 */
[11cb0565]273void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
[7013b14]274{
[9c10e51]275 assert(ohci_batch);
[9515f674]276 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
[7013b14]277}
[76fbd9a]278
[28d9c95]279/** Prepare generic control transfer
280 *
[5f57929]281 * @param[in] ohci_batch Batch structure to use.
[058fd76]282 * @param[in] dir Communication direction
[28d9c95]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 */
[058fd76]288static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
[7786cea]289{
[9c10e51]290 assert(ohci_batch);
291 assert(ohci_batch->usb_batch);
[058fd76]292 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
[49a98c8]293 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
[9c10e51]294 ohci_batch->ed->status, ohci_batch->ed->td_tail,
295 ohci_batch->ed->td_head, ohci_batch->ed->next);
[058fd76]296 static const usb_direction_t reverse_dir[] = {
297 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
298 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
299 };
[9c10e51]300
[e42dd32]301 int toggle = 0;
[058fd76]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];
[9c10e51]305
[dab3112]306 /* Setup stage */
[70d72dd]307 td_init(
308 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
309 buffer, ohci_batch->usb_batch->setup_size, toggle);
[49a98c8]310 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]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;
[e42dd32]314
[dab3112]315 /* Data stage */
[e42dd32]316 size_t td_current = 1;
[9c10e51]317 size_t remain_size = ohci_batch->usb_batch->buffer_size;
[e42dd32]318 while (remain_size > 0) {
[9c10e51]319 const size_t transfer_size =
[3f162ab]320 min(remain_size, OHCI_TD_MAX_TRANSFER);
[e42dd32]321 toggle = 1 - toggle;
322
[70d72dd]323 td_init(ohci_batch->tds[td_current],
324 ohci_batch->tds[td_current + 1],
325 data_dir, buffer, transfer_size, toggle);
[49a98c8]326 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]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);
[e42dd32]331
[d017cea]332 buffer += transfer_size;
[e42dd32]333 remain_size -= transfer_size;
[9c10e51]334 assert(td_current < ohci_batch->td_count - 1);
[e42dd32]335 ++td_current;
336 }
337
[dab3112]338 /* Status stage */
[9c10e51]339 assert(td_current == ohci_batch->td_count - 1);
[70d72dd]340 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
341 status_dir, NULL, 0, 1);
[49a98c8]342 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]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);
[058fd76]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));
[f98b8269]353}
[76fbd9a]354
[28d9c95]355/** Prepare generic data transfer
356 *
[5f57929]357 * @param[in] ohci_batch Batch structure to use.
[058fd76]358 * @paramp[in] dir Communication direction.
[28d9c95]359 *
360 * Direction is supplied by the associated ep and toggle is maintained by the
361 * OHCI hw in ED.
362 */
[058fd76]363static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
[c6fe469]364{
[9c10e51]365 assert(ohci_batch);
[058fd76]366 assert(ohci_batch->usb_batch);
367 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
[49a98c8]368 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
[9c10e51]369 ohci_batch->ed->status, ohci_batch->ed->td_tail,
370 ohci_batch->ed->td_head, ohci_batch->ed->next);
[c6fe469]371
[aa9ccf7]372 size_t td_current = 0;
[9c10e51]373 size_t remain_size = ohci_batch->usb_batch->buffer_size;
374 char *buffer = ohci_batch->device_buffer;
[c6fe469]375 while (remain_size > 0) {
[02cacce]376 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
377 ? OHCI_TD_MAX_TRANSFER : remain_size;
[c6fe469]378
[70d72dd]379 td_init(
380 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
381 dir, buffer, transfer_size, -1);
[058fd76]382
[49a98c8]383 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]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);
[c6fe469]388
[d017cea]389 buffer += transfer_size;
[c6fe469]390 remain_size -= transfer_size;
[9c10e51]391 assert(td_current < ohci_batch->td_count);
[c6fe469]392 ++td_current;
393 }
[5f57929]394 usb_log_debug2(
395 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
396 ohci_batch->usb_batch,
[7bce1fc]397 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
[058fd76]398 usb_str_direction(dir),
[5f57929]399 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
[7bce1fc]400}
[76fbd9a]401
[6fa04db]402/** Transfer setup table. */
[790318e]403static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
[7bce1fc]404{
[790318e]405 [USB_TRANSFER_CONTROL] = batch_control,
406 [USB_TRANSFER_BULK] = batch_data,
407 [USB_TRANSFER_INTERRUPT] = batch_data,
408 [USB_TRANSFER_ISOCHRONOUS] = NULL,
[7bce1fc]409};
[41b96b4]410/**
411 * @}
412 */
Note: See TracBrowser for help on using the repository browser.