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

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

ohci: Display leading zeros in TD and ED debug messages.

  • Property mode set to 100644
File size: 12.9 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 }
[9c10e51]61 usb_transfer_batch_dispose(ohci_batch->usb_batch);
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
[f1be95c8]165 size_t i = 0;
[9c10e51]166 for (; i < ohci_batch->td_count; ++i) {
167 assert(ohci_batch->tds[i] != NULL);
[49a98c8]168 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
[9c10e51]169 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
170 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
171 if (!td_is_finished(ohci_batch->tds[i])) {
[f1be95c8]172 return false;
[aa9ccf7]173 }
[9c10e51]174 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
175 if (ohci_batch->usb_batch->error != EOK) {
[49a98c8]176 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
[9c10e51]177 ohci_batch->usb_batch, i,
178 ohci_batch->tds[i]->status);
[c9dc471]179 /* Make sure TD queue is empty (one TD),
180 * ED should be marked as halted */
[9c10e51]181 ohci_batch->ed->td_tail =
182 (ohci_batch->ed->td_head & ED_TDTAIL_PTR_MASK);
[c9dc471]183 ++i;
[9a6fde4]184 break;
[f1be95c8]185 }
186 }
[e20eaed]187
[9c10e51]188 assert(i <= ohci_batch->td_count);
189 ohci_batch->leave_td = i;
190
191 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
[e20eaed]192 assert(ohci_ep);
[9c10e51]193 ohci_ep->td = ohci_batch->tds[ohci_batch->leave_td];
[33d19a7]194 assert(i > 0);
[9c10e51]195 ohci_batch->usb_batch->transfered_size =
196 ohci_batch->usb_batch->buffer_size;
[9b8958b]197 for (--i;i < ohci_batch->td_count; ++i) {
[9c10e51]198 ohci_batch->usb_batch->transfered_size
199 -= td_remain_size(ohci_batch->tds[i]);
[9b8958b]200 }
[ad86349]201
[c9dc471]202 /* Clear possible ED HALT */
[9c10e51]203 ohci_batch->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
204 /* just make sure that we are leaving the right TD behind */
[e20eaed]205 const uint32_t pa = addr_to_phys(ohci_ep->td);
[9c10e51]206 assert(pa == (ohci_batch->ed->td_head & ED_TDHEAD_PTR_MASK));
207 assert(pa == (ohci_batch->ed->td_tail & ED_TDTAIL_PTR_MASK));
[d6522dd]208
[f1be95c8]209 return true;
[f98b8269]210}
211/*----------------------------------------------------------------------------*/
[28d9c95]212/** Starts execution of the TD list
213 *
[5f57929]214 * @param[in] ohci_batch Batch structure to use
[28d9c95]215 */
[9c10e51]216void ohci_transfer_batch_commit(ohci_transfer_batch_t *ohci_batch)
[7013b14]217{
[9c10e51]218 assert(ohci_batch);
219 ed_set_end_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
[7013b14]220}
221/*----------------------------------------------------------------------------*/
[28d9c95]222/** Prepare generic control transfer
223 *
[5f57929]224 * @param[in] ohci_batch Batch structure to use.
[058fd76]225 * @param[in] dir Communication direction
[28d9c95]226 *
227 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
228 * Data stage with alternating toggle and direction supplied by parameter.
229 * Status stage with toggle 1 and direction supplied by parameter.
230 */
[058fd76]231static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
[7786cea]232{
[9c10e51]233 assert(ohci_batch);
234 assert(ohci_batch->usb_batch);
[058fd76]235 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
[49a98c8]236 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
[9c10e51]237 ohci_batch->ed->status, ohci_batch->ed->td_tail,
238 ohci_batch->ed->td_head, ohci_batch->ed->next);
[058fd76]239 static const usb_direction_t reverse_dir[] = {
240 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
241 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
242 };
[9c10e51]243
[e42dd32]244 int toggle = 0;
[058fd76]245 const char* buffer = ohci_batch->device_buffer;
246 const usb_direction_t data_dir = dir;
247 const usb_direction_t status_dir = reverse_dir[dir];
[9c10e51]248
[e42dd32]249 /* setup stage */
[9c10e51]250 td_init(ohci_batch->tds[0], USB_DIRECTION_BOTH, buffer,
251 ohci_batch->usb_batch->setup_size, toggle);
252 td_set_next(ohci_batch->tds[0], ohci_batch->tds[1]);
[49a98c8]253 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]254 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
255 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
256 buffer += ohci_batch->usb_batch->setup_size;
[e42dd32]257
258 /* data stage */
259 size_t td_current = 1;
[9c10e51]260 size_t remain_size = ohci_batch->usb_batch->buffer_size;
[e42dd32]261 while (remain_size > 0) {
[9c10e51]262 const size_t transfer_size =
263 remain_size > OHCI_TD_MAX_TRANSFER ?
[e42dd32]264 OHCI_TD_MAX_TRANSFER : remain_size;
265 toggle = 1 - toggle;
266
[9c10e51]267 td_init(ohci_batch->tds[td_current], data_dir, buffer,
[e42dd32]268 transfer_size, toggle);
[9c10e51]269 td_set_next(ohci_batch->tds[td_current],
270 ohci_batch->tds[td_current + 1]);
[49a98c8]271 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]272 ohci_batch->tds[td_current]->status,
273 ohci_batch->tds[td_current]->cbp,
274 ohci_batch->tds[td_current]->next,
275 ohci_batch->tds[td_current]->be);
[e42dd32]276
[d017cea]277 buffer += transfer_size;
[e42dd32]278 remain_size -= transfer_size;
[9c10e51]279 assert(td_current < ohci_batch->td_count - 1);
[e42dd32]280 ++td_current;
281 }
282
283 /* status stage */
[9c10e51]284 assert(td_current == ohci_batch->td_count - 1);
285 td_init(ohci_batch->tds[td_current], status_dir, NULL, 0, 1);
286 td_set_next(ohci_batch->tds[td_current],
287 ohci_batch->tds[td_current + 1]);
[49a98c8]288 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]289 ohci_batch->tds[td_current]->status,
290 ohci_batch->tds[td_current]->cbp,
291 ohci_batch->tds[td_current]->next,
292 ohci_batch->tds[td_current]->be);
[058fd76]293 usb_log_debug2(
294 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
295 ohci_batch->usb_batch,
296 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
297 usb_str_direction(dir),
298 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
[f98b8269]299}
300/*----------------------------------------------------------------------------*/
[28d9c95]301/** Prepare generic data transfer
302 *
[5f57929]303 * @param[in] ohci_batch Batch structure to use.
[058fd76]304 * @paramp[in] dir Communication direction.
[28d9c95]305 *
306 * Direction is supplied by the associated ep and toggle is maintained by the
307 * OHCI hw in ED.
308 */
[058fd76]309static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
[c6fe469]310{
[9c10e51]311 assert(ohci_batch);
[058fd76]312 assert(ohci_batch->usb_batch);
313 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
[49a98c8]314 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
[9c10e51]315 ohci_batch->ed->status, ohci_batch->ed->td_tail,
316 ohci_batch->ed->td_head, ohci_batch->ed->next);
[c6fe469]317
[aa9ccf7]318 size_t td_current = 0;
[9c10e51]319 size_t remain_size = ohci_batch->usb_batch->buffer_size;
320 char *buffer = ohci_batch->device_buffer;
[c6fe469]321 while (remain_size > 0) {
[02cacce]322 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
323 ? OHCI_TD_MAX_TRANSFER : remain_size;
[c6fe469]324
[058fd76]325 td_init(ohci_batch->tds[td_current], dir, buffer,
326 transfer_size, -1);
[9c10e51]327 td_set_next(ohci_batch->tds[td_current],
328 ohci_batch->tds[td_current + 1]);
[058fd76]329
[49a98c8]330 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
[9c10e51]331 ohci_batch->tds[td_current]->status,
332 ohci_batch->tds[td_current]->cbp,
333 ohci_batch->tds[td_current]->next,
334 ohci_batch->tds[td_current]->be);
[c6fe469]335
[d017cea]336 buffer += transfer_size;
[c6fe469]337 remain_size -= transfer_size;
[9c10e51]338 assert(td_current < ohci_batch->td_count);
[c6fe469]339 ++td_current;
340 }
[5f57929]341 usb_log_debug2(
342 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
343 ohci_batch->usb_batch,
[7bce1fc]344 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
[058fd76]345 usb_str_direction(dir),
[5f57929]346 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
[7bce1fc]347}
348/*----------------------------------------------------------------------------*/
[790318e]349static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
[7bce1fc]350{
[790318e]351 [USB_TRANSFER_CONTROL] = batch_control,
352 [USB_TRANSFER_BULK] = batch_data,
353 [USB_TRANSFER_INTERRUPT] = batch_data,
354 [USB_TRANSFER_ISOCHRONOUS] = NULL,
[7bce1fc]355};
[41b96b4]356/**
357 * @}
358 */
Note: See TracBrowser for help on using the repository browser.