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

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

ohci: Remove dead code.

  • Property mode set to 100644
File size: 13.9 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 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
202 if (ohci_batch->usb_batch->error == EOK) {
203 /* If the TD got all its data through, it will report
204 * 0 bytes remain, the sole exception is INPUT with
205 * data rounding flag (short), i.e. every INPUT.
206 * Nice thing is that short packets will correctly
207 * report remaining data, thus making this computation
208 * correct (short packets need to be produced by the
209 * last TD)
210 * NOTE: This also works for CONTROL transfer as
211 * the first TD will return 0 remain.
212 * NOTE: Short packets don't break the assumption that
213 * we leave the very last(unused) TD behind.
214 */
215 ohci_batch->usb_batch->transfered_size
216 -= td_remain_size(ohci_batch->tds[i]);
217 } else {
218 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
219 ohci_batch->usb_batch, i,
220 ohci_batch->tds[i]->status);
221
222 /* ED should be stopped because of errors */
223 assert((ohci_batch->ed->td_head & ED_TDHEAD_HALTED_FLAG) != 0);
224
225 /* Now we have a problem: we don't know what TD
226 * the head pointer points to, the retiring rules
227 * described in specs say it should be the one after
228 * the failed one so set the tail pointer accordingly.
229 * It will be the one TD we leave behind.
230 */
231 leave_td = i + 1;
232
233 /* Check TD assumption */
234 assert(ed_head_td(ohci_batch->ed) ==
235 addr_to_phys(ohci_batch->tds[leave_td]));
236
237 /* Set tail to the same TD */
238 ed_set_tail_td(ohci_batch->ed,
239 ohci_batch->tds[leave_td]);
240
241 /* Clear possible ED HALT */
242 ed_clear_halt(ohci_batch->ed);
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 assert(addr_to_phys(ohci_ep->td) == ed_head_td(ohci_batch->ed));
256 assert(addr_to_phys(ohci_ep->td) == ed_tail_td(ohci_batch->ed));
257
258 return true;
259}
260
261/** Starts execution of the TD list
262 *
263 * @param[in] ohci_batch Batch structure to use
264 */
265void ohci_transfer_batch_commit(const ohci_transfer_batch_t *ohci_batch)
266{
267 assert(ohci_batch);
268 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
269}
270
271/** Prepare generic control transfer
272 *
273 * @param[in] ohci_batch Batch structure to use.
274 * @param[in] dir Communication direction
275 *
276 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
277 * Data stage with alternating toggle and direction supplied by parameter.
278 * Status stage with toggle 1 and direction supplied by parameter.
279 */
280static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
281{
282 assert(ohci_batch);
283 assert(ohci_batch->usb_batch);
284 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
285 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
286 ohci_batch->ed->status, ohci_batch->ed->td_tail,
287 ohci_batch->ed->td_head, ohci_batch->ed->next);
288 static const usb_direction_t reverse_dir[] = {
289 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
290 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
291 };
292
293 int toggle = 0;
294 const char* buffer = ohci_batch->device_buffer;
295 const usb_direction_t data_dir = dir;
296 const usb_direction_t status_dir = reverse_dir[dir];
297
298 /* Setup stage */
299 td_init(
300 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
301 buffer, ohci_batch->usb_batch->setup_size, toggle);
302 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
303 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
304 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
305 buffer += ohci_batch->usb_batch->setup_size;
306
307 /* Data stage */
308 size_t td_current = 1;
309 size_t remain_size = ohci_batch->usb_batch->buffer_size;
310 while (remain_size > 0) {
311 const size_t transfer_size =
312 min(remain_size, OHCI_TD_MAX_TRANSFER);
313 toggle = 1 - toggle;
314
315 td_init(ohci_batch->tds[td_current],
316 ohci_batch->tds[td_current + 1],
317 data_dir, buffer, transfer_size, toggle);
318 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
319 ohci_batch->tds[td_current]->status,
320 ohci_batch->tds[td_current]->cbp,
321 ohci_batch->tds[td_current]->next,
322 ohci_batch->tds[td_current]->be);
323
324 buffer += transfer_size;
325 remain_size -= transfer_size;
326 assert(td_current < ohci_batch->td_count - 1);
327 ++td_current;
328 }
329
330 /* Status stage */
331 assert(td_current == ohci_batch->td_count - 1);
332 td_init(ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
333 status_dir, NULL, 0, 1);
334 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
335 ohci_batch->tds[td_current]->status,
336 ohci_batch->tds[td_current]->cbp,
337 ohci_batch->tds[td_current]->next,
338 ohci_batch->tds[td_current]->be);
339 usb_log_debug2(
340 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
341 ohci_batch->usb_batch,
342 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
343 usb_str_direction(dir),
344 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
345}
346
347/** Prepare generic data transfer
348 *
349 * @param[in] ohci_batch Batch structure to use.
350 * @paramp[in] dir Communication direction.
351 *
352 * Direction is supplied by the associated ep and toggle is maintained by the
353 * OHCI hw in ED.
354 */
355static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
356{
357 assert(ohci_batch);
358 assert(ohci_batch->usb_batch);
359 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
360 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
361 ohci_batch->ed->status, ohci_batch->ed->td_tail,
362 ohci_batch->ed->td_head, ohci_batch->ed->next);
363
364 size_t td_current = 0;
365 size_t remain_size = ohci_batch->usb_batch->buffer_size;
366 char *buffer = ohci_batch->device_buffer;
367 while (remain_size > 0) {
368 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
369 ? OHCI_TD_MAX_TRANSFER : remain_size;
370
371 td_init(
372 ohci_batch->tds[td_current], ohci_batch->tds[td_current + 1],
373 dir, buffer, transfer_size, -1);
374
375 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
376 ohci_batch->tds[td_current]->status,
377 ohci_batch->tds[td_current]->cbp,
378 ohci_batch->tds[td_current]->next,
379 ohci_batch->tds[td_current]->be);
380
381 buffer += transfer_size;
382 remain_size -= transfer_size;
383 assert(td_current < ohci_batch->td_count);
384 ++td_current;
385 }
386 usb_log_debug2(
387 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
388 ohci_batch->usb_batch,
389 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
390 usb_str_direction(dir),
391 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
392}
393
394/** Transfer setup table. */
395static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
396{
397 [USB_TRANSFER_CONTROL] = batch_control,
398 [USB_TRANSFER_BULK] = batch_data,
399 [USB_TRANSFER_INTERRUPT] = batch_data,
400 [USB_TRANSFER_ISOCHRONOUS] = NULL,
401};
402/**
403 * @}
404 */
Note: See TracBrowser for help on using the repository browser.