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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 629255a was db51a6a6, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

typo: transferred is spelled with two r

  • Property mode set to 100644
File size: 13.5 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"
[e6b9182]47#include "ohci_bus.h"
[09ace19]48
[5fd9c30]49static void (*const batch_setup[])(ohci_transfer_batch_t*);
[76fbd9a]50
[28d9c95]51/** Safely destructs ohci_transfer_batch_t structure
52 *
53 * @param[in] ohci_batch Instance to destroy.
54 */
[5fd9c30]55void ohci_transfer_batch_destroy(ohci_transfer_batch_t *ohci_batch)
[2cc6e97]56{
[5fd9c30]57 assert(ohci_batch);
[9c10e51]58 if (ohci_batch->tds) {
[11cb0565]59 const ohci_endpoint_t *ohci_ep =
[9162b27]60 ohci_endpoint_get(ohci_batch->base.ep);
[11cb0565]61 assert(ohci_ep);
[9b8958b]62 for (unsigned i = 0; i < ohci_batch->td_count; ++i) {
[11cb0565]63 if (ohci_batch->tds[i] != ohci_ep->td)
[9c10e51]64 free32(ohci_batch->tds[i]);
[9a6fde4]65 }
[9c10e51]66 free(ohci_batch->tds);
[9a6fde4]67 }
[9c10e51]68 free32(ohci_batch->device_buffer);
69 free(ohci_batch);
[2cc6e97]70}
[76fbd9a]71
[6fa04db]72/** Allocate memory and initialize internal data structure.
73 *
[9162b27]74 * @param[in] ep Endpoint for which the batch will be created
[6fa04db]75 * @return Valid pointer if all structures were successfully created,
76 * NULL otherwise.
77 */
[5fd9c30]78ohci_transfer_batch_t * ohci_transfer_batch_create(endpoint_t *ep)
[9c10e51]79{
[5fd9c30]80 assert(ep);
[e2976bb]81
[9c10e51]82 ohci_transfer_batch_t *ohci_batch =
[8e3d17f]83 calloc(1, sizeof(ohci_transfer_batch_t));
[584aa38]84 if (!ohci_batch) {
85 usb_log_error("Failed to allocate OHCI batch data.");
[5fd9c30]86 return NULL;
[584aa38]87 }
[5fd9c30]88
89 usb_transfer_batch_init(&ohci_batch->base, ep);
90
91 return ohci_batch;
92}
93
94/** Prepares a batch to be sent.
95 *
96 * Determines the number of needed transfer descriptors (TDs).
97 * Prepares a transport buffer (that is accessible by the hardware).
98 * Initializes parameters needed for the transfer and callback.
99 */
100int ohci_transfer_batch_prepare(ohci_transfer_batch_t *ohci_batch)
101{
102 assert(ohci_batch);
103
104 const size_t setup_size = (ohci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL)
105 ? USB_SETUP_PACKET_SIZE
106 : 0;
107
108 usb_transfer_batch_t *usb_batch = &ohci_batch->base;
109
110 ohci_batch->td_count = (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
[9c10e51]111 / OHCI_TD_MAX_TRANSFER;
[e2976bb]112 /* Control transfer need Setup and Status stage */
[9c10e51]113 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
114 ohci_batch->td_count += 2;
[e2976bb]115 }
116
[f974519]117 /* We need an extra place for TD that was left at ED */
[9c10e51]118 ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
[584aa38]119 if (!ohci_batch->tds) {
120 usb_log_error("Failed to allocate OHCI transfer descriptors.");
[5fd9c30]121 return ENOMEM;
[584aa38]122 }
[e2976bb]123
124 /* Add TD left over by the previous transfer */
[9c10e51]125 ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
126 ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
[9b8958b]127
128 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
[9c10e51]129 ohci_batch->tds[i] = malloc32(sizeof(td_t));
[584aa38]130 if (!ohci_batch->tds[i]) {
131 usb_log_error("Failed to allocate TD %d.", i);
[5fd9c30]132 return ENOMEM;
[584aa38]133 }
[e2976bb]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) */
[5fd9c30]140 if (setup_size + usb_batch->buffer_size > 0) {
[9c10e51]141 /* Use one buffer for setup and data stage */
142 ohci_batch->device_buffer =
[5fd9c30]143 malloc32(setup_size + usb_batch->buffer_size);
[584aa38]144 if (!ohci_batch->device_buffer) {
145 usb_log_error("Failed to allocate device buffer");
[5fd9c30]146 return ENOMEM;
[584aa38]147 }
[f974519]148 /* Copy setup data */
[5fd9c30]149 memcpy(ohci_batch->device_buffer, usb_batch->setup.buffer, setup_size);
[9c10e51]150 /* Copy generic data */
151 if (usb_batch->ep->direction != USB_DIRECTION_IN)
152 memcpy(
[5fd9c30]153 ohci_batch->device_buffer + setup_size,
[9c10e51]154 usb_batch->buffer, usb_batch->buffer_size);
[e2976bb]155 }
156
[790318e]157 assert(batch_setup[usb_batch->ep->transfer_type]);
[5fd9c30]158 batch_setup[usb_batch->ep->transfer_type](ohci_batch);
[90dd59dc]159
[5fd9c30]160 return EOK;
[e2976bb]161}
[76fbd9a]162
[28d9c95]163/** Check batch TDs' status.
164 *
[5f57929]165 * @param[in] ohci_batch Batch structure to use.
[28d9c95]166 * @return False, if there is an active TD, true otherwise.
167 *
168 * Walk all TDs (usually there is just one). Stop with false if there is an
169 * active TD. Stop with true if an error is found. Return true if the walk
170 * completes with the last TD.
171 */
[5fd9c30]172bool ohci_transfer_batch_check_completed(ohci_transfer_batch_t *ohci_batch)
[f98b8269]173{
[9c10e51]174 assert(ohci_batch);
175
[a1732929]176 usb_log_debug("Batch %p checking %zu td(s) for completion.",
[9162b27]177 &ohci_batch->base, ohci_batch->td_count);
[a1732929]178 usb_log_debug2("ED: %08x:%08x:%08x:%08x.",
[9c10e51]179 ohci_batch->ed->status, ohci_batch->ed->td_head,
180 ohci_batch->ed->td_tail, ohci_batch->ed->next);
181
[d5abaf4]182 if (!ed_inactive(ohci_batch->ed) && ed_transfer_pending(ohci_batch->ed))
183 return false;
184
185 /* Now we may be sure that either the ED is inactive because of errors
186 * or all transfer descriptors completed successfully */
187
188 /* Assume all data got through */
[db51a6a6]189 ohci_batch->base.transferred_size = ohci_batch->base.buffer_size;
[dab3112]190
191 /* Assume we will leave the last(unused) TD behind */
[11cb0565]192 unsigned leave_td = ohci_batch->td_count;
[d5abaf4]193
194 /* Check all TDs */
195 for (size_t i = 0; i < ohci_batch->td_count; ++i) {
[9c10e51]196 assert(ohci_batch->tds[i] != NULL);
[a1732929]197 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.", i,
[9c10e51]198 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
199 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
[d5abaf4]200
[5fd9c30]201 ohci_batch->base.error = td_error(ohci_batch->tds[i]);
202 if (ohci_batch->base.error == EOK) {
[7f7e6f5]203 /* If the TD got all its data through, it will report
204 * 0 bytes remain, the sole exception is INPUT with
205 * data rounding flag (short), i.e. every INPUT.
206 * Nice thing is that short packets will correctly
207 * report remaining data, thus making this computation
208 * correct (short packets need to be produced by the
209 * last TD)
210 * NOTE: This also works for CONTROL transfer as
211 * the first TD will return 0 remain.
212 * NOTE: Short packets don't break the assumption that
213 * we leave the very last(unused) TD behind.
214 */
[db51a6a6]215 ohci_batch->base.transferred_size
[7f7e6f5]216 -= td_remain_size(ohci_batch->tds[i]);
217 } else {
[a1732929]218 usb_log_debug("Batch %p found error TD(%zu):%08x.",
[9162b27]219 &ohci_batch->base, i,
[9c10e51]220 ohci_batch->tds[i]->status);
[d5abaf4]221
222 /* ED should be stopped because of errors */
223 assert((ohci_batch->ed->td_head & ED_TDHEAD_HALTED_FLAG) != 0);
224
225 /* Now we have a problem: we don't know what TD
226 * the head pointer points to, the retiring rules
227 * described in specs say it should be the one after
228 * the failed one so set the tail pointer accordingly.
229 * It will be the one TD we leave behind.
230 */
[11cb0565]231 leave_td = i + 1;
[d5abaf4]232
233 /* Check TD assumption */
[65eac7b]234 assert(ed_head_td(ohci_batch->ed) ==
235 addr_to_phys(ohci_batch->tds[leave_td]));
[d5abaf4]236
[65eac7b]237 /* Set tail to the same TD */
[d5abaf4]238 ed_set_tail_td(ohci_batch->ed,
[11cb0565]239 ohci_batch->tds[leave_td]);
[d5abaf4]240
241 /* Clear possible ED HALT */
[65eac7b]242 ed_clear_halt(ohci_batch->ed);
[9a6fde4]243 break;
[f1be95c8]244 }
245 }
[db51a6a6]246 assert(ohci_batch->base.transferred_size <=
[5fd9c30]247 ohci_batch->base.buffer_size);
248
[9162b27]249 const size_t setup_size = (ohci_batch->base.ep->transfer_type == USB_TRANSFER_CONTROL)
250 ? USB_SETUP_PACKET_SIZE
251 : 0;
252
[5fd9c30]253 if (ohci_batch->base.dir == USB_DIRECTION_IN)
[9162b27]254 memcpy(ohci_batch->base.buffer,
255 ohci_batch->device_buffer + setup_size,
[db51a6a6]256 ohci_batch->base.transferred_size);
[e20eaed]257
[d5abaf4]258 /* Store the remaining TD */
[5fd9c30]259 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->base.ep);
[e20eaed]260 assert(ohci_ep);
[11cb0565]261 ohci_ep->td = ohci_batch->tds[leave_td];
[dab3112]262
263 /* Make sure that we are leaving the right TD behind */
[65eac7b]264 assert(addr_to_phys(ohci_ep->td) == ed_head_td(ohci_batch->ed));
265 assert(addr_to_phys(ohci_ep->td) == ed_tail_td(ohci_batch->ed));
[d6522dd]266
[f1be95c8]267 return true;
[f98b8269]268}
[76fbd9a]269
[28d9c95]270/** Starts execution of the TD list
271 *
[5f57929]272 * @param[in] ohci_batch Batch structure to use
[28d9c95]273 */
[11cb0565]274void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
[7013b14]275{
[9c10e51]276 assert(ohci_batch);
[9515f674]277 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
[7013b14]278}
[76fbd9a]279
[28d9c95]280/** Prepare generic control transfer
281 *
[5f57929]282 * @param[in] ohci_batch Batch structure to use.
[058fd76]283 * @param[in] dir Communication direction
[28d9c95]284 *
285 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
286 * Data stage with alternating toggle and direction supplied by parameter.
287 * Status stage with toggle 1 and direction supplied by parameter.
288 */
[5fd9c30]289static void batch_control(ohci_transfer_batch_t *ohci_batch)
[7786cea]290{
[9c10e51]291 assert(ohci_batch);
[5fd9c30]292
293 usb_direction_t dir = ohci_batch->base.dir;
[058fd76]294 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
[5fd9c30]295
[a1732929]296 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.", ohci_batch->ed,
[9c10e51]297 ohci_batch->ed->status, ohci_batch->ed->td_tail,
298 ohci_batch->ed->td_head, ohci_batch->ed->next);
[058fd76]299 static const usb_direction_t reverse_dir[] = {
300 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
301 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
302 };
[9c10e51]303
[e42dd32]304 int toggle = 0;
[058fd76]305 const char* buffer = ohci_batch->device_buffer;
306 const usb_direction_t data_dir = dir;
307 const usb_direction_t status_dir = reverse_dir[dir];
[9c10e51]308
[dab3112]309 /* Setup stage */
[70d72dd]310 td_init(
311 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
[5fd9c30]312 buffer, USB_SETUP_PACKET_SIZE, toggle);
[a1732929]313 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.",
[9c10e51]314 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
315 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
[5fd9c30]316 buffer += USB_SETUP_PACKET_SIZE;
[e42dd32]317
[dab3112]318 /* Data stage */
[e42dd32]319 size_t td_current = 1;
[9162b27]320 size_t remain_size = ohci_batch->base.buffer_size;
[e42dd32]321 while (remain_size > 0) {
[9c10e51]322 const size_t transfer_size =
[3f162ab]323 min(remain_size, OHCI_TD_MAX_TRANSFER);
[e42dd32]324 toggle = 1 - toggle;
325
[70d72dd]326 td_init(ohci_batch->tds[td_current],
327 ohci_batch->tds[td_current + 1],
328 data_dir, buffer, transfer_size, toggle);
[a1732929]329 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.",
[9c10e51]330 ohci_batch->tds[td_current]->status,
331 ohci_batch->tds[td_current]->cbp,
332 ohci_batch->tds[td_current]->next,
333 ohci_batch->tds[td_current]->be);
[e42dd32]334
[d017cea]335 buffer += transfer_size;
[e42dd32]336 remain_size -= transfer_size;
[9c10e51]337 assert(td_current < ohci_batch->td_count - 1);
[e42dd32]338 ++td_current;
339 }
340
[dab3112]341 /* Status stage */
[9c10e51]342 assert(td_current == ohci_batch->td_count - 1);
[70d72dd]343 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
344 status_dir, NULL, 0, 1);
[a1732929]345 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.",
[9c10e51]346 ohci_batch->tds[td_current]->status,
347 ohci_batch->tds[td_current]->cbp,
348 ohci_batch->tds[td_current]->next,
349 ohci_batch->tds[td_current]->be);
[058fd76]350 usb_log_debug2(
[a1732929]351 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.", \
[9162b27]352 &ohci_batch->base,
353 usb_str_transfer_type(ohci_batch->base.ep->transfer_type),
[058fd76]354 usb_str_direction(dir),
[9162b27]355 USB_TRANSFER_BATCH_ARGS(ohci_batch->base));
[f98b8269]356}
[76fbd9a]357
[28d9c95]358/** Prepare generic data transfer
359 *
[5f57929]360 * @param[in] ohci_batch Batch structure to use.
[058fd76]361 * @paramp[in] dir Communication direction.
[28d9c95]362 *
363 * Direction is supplied by the associated ep and toggle is maintained by the
364 * OHCI hw in ED.
365 */
[5fd9c30]366static void batch_data(ohci_transfer_batch_t *ohci_batch)
[c6fe469]367{
[9c10e51]368 assert(ohci_batch);
[5fd9c30]369
370 usb_direction_t dir = ohci_batch->base.dir;
[058fd76]371 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
[a1732929]372 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.", ohci_batch->ed,
[9c10e51]373 ohci_batch->ed->status, ohci_batch->ed->td_tail,
374 ohci_batch->ed->td_head, ohci_batch->ed->next);
[c6fe469]375
[aa9ccf7]376 size_t td_current = 0;
[9162b27]377 size_t remain_size = ohci_batch->base.buffer_size;
[9c10e51]378 char *buffer = ohci_batch->device_buffer;
[c6fe469]379 while (remain_size > 0) {
[02cacce]380 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
381 ? OHCI_TD_MAX_TRANSFER : remain_size;
[c6fe469]382
[70d72dd]383 td_init(
384 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
385 dir, buffer, transfer_size, -1);
[058fd76]386
[a1732929]387 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.",
[9c10e51]388 ohci_batch->tds[td_current]->status,
389 ohci_batch->tds[td_current]->cbp,
390 ohci_batch->tds[td_current]->next,
391 ohci_batch->tds[td_current]->be);
[c6fe469]392
[d017cea]393 buffer += transfer_size;
[c6fe469]394 remain_size -= transfer_size;
[9c10e51]395 assert(td_current < ohci_batch->td_count);
[c6fe469]396 ++td_current;
397 }
[5f57929]398 usb_log_debug2(
[a1732929]399 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.", \
[9162b27]400 &ohci_batch->base,
401 usb_str_transfer_type(ohci_batch->base.ep->transfer_type),
[058fd76]402 usb_str_direction(dir),
[9162b27]403 USB_TRANSFER_BATCH_ARGS(ohci_batch->base));
[7bce1fc]404}
[76fbd9a]405
[6fa04db]406/** Transfer setup table. */
[5fd9c30]407static void (*const batch_setup[])(ohci_transfer_batch_t*) =
[7bce1fc]408{
[790318e]409 [USB_TRANSFER_CONTROL] = batch_control,
410 [USB_TRANSFER_BULK] = batch_data,
411 [USB_TRANSFER_INTERRUPT] = batch_data,
412 [USB_TRANSFER_ISOCHRONOUS] = NULL,
[7bce1fc]413};
[41b96b4]414/**
415 * @}
416 */
Note: See TracBrowser for help on using the repository browser.