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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1c1b577 was 549ff23, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbhost: Cleanup usb_transfer_batch interface.

Remove redundant and unused call_in/call_out functions.
Rename functions: get ⇒ create, dispose ⇒ destroy.
Add/Fix doxygen comments.

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