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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c910ecf was 5fd9c30, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost refactoring: let transfer_batch be initialized by bus

Currently makes older HCs fail, work in progress.

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