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

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

ohci: Drop leave_td field. Make is_compete() and commit() parameters const.

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