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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2718e1 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
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
37#include <usb/usb.h>
38#include <usb/debug.h>
39
40#include "ohci_batch.h"
41#include "ohci_endpoint.h"
42#include "utils/malloc32.h"
43
44static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t);
45/*----------------------------------------------------------------------------*/
46/** Safely destructs ohci_transfer_batch_t structure
47 *
48 * @param[in] ohci_batch Instance to destroy.
49 */
50static void ohci_transfer_batch_dispose(ohci_transfer_batch_t *ohci_batch)
51{
52 if (!ohci_batch)
53 return;
54 if (ohci_batch->tds) {
55 for (unsigned i = 0; i < ohci_batch->td_count; ++i) {
56 if (i != ohci_batch->leave_td)
57 free32(ohci_batch->tds[i]);
58 }
59 free(ohci_batch->tds);
60 }
61 usb_transfer_batch_destroy(ohci_batch->usb_batch);
62 free32(ohci_batch->device_buffer);
63 free(ohci_batch);
64}
65/*----------------------------------------------------------------------------*/
66void ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *ohci_batch)
67{
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
85
86 ohci_transfer_batch_t *ohci_batch =
87 calloc(1, sizeof(ohci_transfer_batch_t));
88 CHECK_NULL_DISPOSE_RET(ohci_batch,
89 "Failed to allocate OHCI batch data.\n");
90 link_initialize(&ohci_batch->link);
91 ohci_batch->td_count =
92 (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
93 / OHCI_TD_MAX_TRANSFER;
94 /* Control transfer need Setup and Status stage */
95 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
96 ohci_batch->td_count += 2;
97 }
98
99 /* We need an extra place for TD that was left at ED */
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");
103
104 /* Add TD left over by the previous transfer */
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;
108
109 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
110 ohci_batch->tds[i] = malloc32(sizeof(td_t));
111 CHECK_NULL_DISPOSE_RET(ohci_batch->tds[i],
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) */
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,
125 "Failed to allocate device accessible buffer.\n");
126 /* Copy setup data */
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);
134 }
135 ohci_batch->usb_batch = usb_batch;
136
137 const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
138 assert(batch_setup[usb_batch->ep->transfer_type]);
139 batch_setup[usb_batch->ep->transfer_type](ohci_batch, dir);
140
141 return ohci_batch;
142#undef CHECK_NULL_DISPOSE_RET
143}
144/*----------------------------------------------------------------------------*/
145/** Check batch TDs' status.
146 *
147 * @param[in] ohci_batch Batch structure to use.
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 */
154bool ohci_transfer_batch_is_complete(ohci_transfer_batch_t *ohci_batch)
155{
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);
161 usb_log_debug2("ED: %08x:%08x:%08x:%08x.\n",
162 ohci_batch->ed->status, ohci_batch->ed->td_head,
163 ohci_batch->ed->td_tail, ohci_batch->ed->next);
164
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;
174
175 /* Assume we will leave the last(unused) TD behind */
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) {
180 assert(ohci_batch->tds[i] != NULL);
181 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
182 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
183 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
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
199 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
200 if (ohci_batch->usb_batch->error != EOK) {
201 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
202 ohci_batch->usb_batch, i,
203 ohci_batch->tds[i]->status);
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;
227 break;
228 }
229 }
230 assert(ohci_batch->usb_batch->transfered_size <=
231 ohci_batch->usb_batch->buffer_size);
232
233 /* Store the remaining TD */
234 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
235 assert(ohci_ep);
236 ohci_ep->td = ohci_batch->tds[ohci_batch->leave_td];
237
238 /* Make sure that we are leaving the right TD behind */
239 const uint32_t pa = addr_to_phys(ohci_ep->td);
240 assert(pa == (ohci_batch->ed->td_head & ED_TDHEAD_PTR_MASK));
241 assert(pa == (ohci_batch->ed->td_tail & ED_TDTAIL_PTR_MASK));
242
243 return true;
244}
245/*----------------------------------------------------------------------------*/
246/** Starts execution of the TD list
247 *
248 * @param[in] ohci_batch Batch structure to use
249 */
250void ohci_transfer_batch_commit(ohci_transfer_batch_t *ohci_batch)
251{
252 assert(ohci_batch);
253 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
254}
255/*----------------------------------------------------------------------------*/
256/** Prepare generic control transfer
257 *
258 * @param[in] ohci_batch Batch structure to use.
259 * @param[in] dir Communication direction
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 */
265static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
266{
267 assert(ohci_batch);
268 assert(ohci_batch->usb_batch);
269 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
270 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
271 ohci_batch->ed->status, ohci_batch->ed->td_tail,
272 ohci_batch->ed->td_head, ohci_batch->ed->next);
273 static const usb_direction_t reverse_dir[] = {
274 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
275 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
276 };
277
278 int toggle = 0;
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];
282
283 /* Setup stage */
284 td_init(
285 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
286 buffer, ohci_batch->usb_batch->setup_size, toggle);
287 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
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;
291
292 /* Data stage */
293 size_t td_current = 1;
294 size_t remain_size = ohci_batch->usb_batch->buffer_size;
295 while (remain_size > 0) {
296 const size_t transfer_size =
297 remain_size > OHCI_TD_MAX_TRANSFER ?
298 OHCI_TD_MAX_TRANSFER : remain_size;
299 toggle = 1 - toggle;
300
301 td_init(ohci_batch->tds[td_current],
302 ohci_batch->tds[td_current + 1],
303 data_dir, buffer, transfer_size, toggle);
304 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
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);
309
310 buffer += transfer_size;
311 remain_size -= transfer_size;
312 assert(td_current < ohci_batch->td_count - 1);
313 ++td_current;
314 }
315
316 /* Status stage */
317 assert(td_current == ohci_batch->td_count - 1);
318 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
319 status_dir, NULL, 0, 1);
320 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
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);
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));
331}
332/*----------------------------------------------------------------------------*/
333/** Prepare generic data transfer
334 *
335 * @param[in] ohci_batch Batch structure to use.
336 * @paramp[in] dir Communication direction.
337 *
338 * Direction is supplied by the associated ep and toggle is maintained by the
339 * OHCI hw in ED.
340 */
341static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
342{
343 assert(ohci_batch);
344 assert(ohci_batch->usb_batch);
345 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
346 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
347 ohci_batch->ed->status, ohci_batch->ed->td_tail,
348 ohci_batch->ed->td_head, ohci_batch->ed->next);
349
350 size_t td_current = 0;
351 size_t remain_size = ohci_batch->usb_batch->buffer_size;
352 char *buffer = ohci_batch->device_buffer;
353 while (remain_size > 0) {
354 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
355 ? OHCI_TD_MAX_TRANSFER : remain_size;
356
357 td_init(
358 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
359 dir, buffer, transfer_size, -1);
360
361 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
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);
366
367 buffer += transfer_size;
368 remain_size -= transfer_size;
369 assert(td_current < ohci_batch->td_count);
370 ++td_current;
371 }
372 usb_log_debug2(
373 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
374 ohci_batch->usb_batch,
375 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
376 usb_str_direction(dir),
377 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
378}
379/*----------------------------------------------------------------------------*/
380static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
381{
382 [USB_TRANSFER_CONTROL] = batch_control,
383 [USB_TRANSFER_BULK] = batch_data,
384 [USB_TRANSFER_INTERRUPT] = batch_data,
385 [USB_TRANSFER_ISOCHRONOUS] = NULL,
386};
387/**
388 * @}
389 */
Note: See TracBrowser for help on using the repository browser.