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

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

ohci: Doxygen

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