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

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

usb: Use macros from macros.h header.

  • 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#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/*----------------------------------------------------------------------------*/
67void ohci_transfer_batch_finish_dispose(ohci_transfer_batch_t *ohci_batch)
68{
69 assert(ohci_batch);
70 assert(ohci_batch->usb_batch);
71 usb_transfer_batch_finish(ohci_batch->usb_batch,
72 ohci_batch->device_buffer + ohci_batch->usb_batch->setup_size,
73 ohci_batch->usb_batch->buffer_size);
74 ohci_transfer_batch_dispose(ohci_batch);
75}
76/*----------------------------------------------------------------------------*/
77ohci_transfer_batch_t * ohci_transfer_batch_get(usb_transfer_batch_t *usb_batch)
78{
79 assert(usb_batch);
80#define CHECK_NULL_DISPOSE_RET(ptr, message...) \
81if (ptr == NULL) { \
82 usb_log_error(message); \
83 ohci_transfer_batch_dispose(ohci_batch); \
84 return NULL; \
85} else (void)0
86
87 ohci_transfer_batch_t *ohci_batch =
88 calloc(1, sizeof(ohci_transfer_batch_t));
89 CHECK_NULL_DISPOSE_RET(ohci_batch,
90 "Failed to allocate OHCI batch data.\n");
91 link_initialize(&ohci_batch->link);
92 ohci_batch->td_count =
93 (usb_batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
94 / OHCI_TD_MAX_TRANSFER;
95 /* Control transfer need Setup and Status stage */
96 if (usb_batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
97 ohci_batch->td_count += 2;
98 }
99
100 /* We need an extra place for TD that was left at ED */
101 ohci_batch->tds = calloc(ohci_batch->td_count + 1, sizeof(td_t*));
102 CHECK_NULL_DISPOSE_RET(ohci_batch->tds,
103 "Failed to allocate OHCI transfer descriptors.\n");
104
105 /* Add TD left over by the previous transfer */
106 ohci_batch->ed = ohci_endpoint_get(usb_batch->ep)->ed;
107 ohci_batch->tds[0] = ohci_endpoint_get(usb_batch->ep)->td;
108 ohci_batch->leave_td = 0;
109
110 for (unsigned i = 1; i <= ohci_batch->td_count; ++i) {
111 ohci_batch->tds[i] = malloc32(sizeof(td_t));
112 CHECK_NULL_DISPOSE_RET(ohci_batch->tds[i],
113 "Failed to allocate TD %d.\n", i );
114 }
115
116
117 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
118 * it is, however, not capable of handling buffer that occupies more
119 * than two pages (the first page is computed using start pointer, the
120 * other using the end pointer) */
121 if (usb_batch->setup_size + usb_batch->buffer_size > 0) {
122 /* Use one buffer for setup and data stage */
123 ohci_batch->device_buffer =
124 malloc32(usb_batch->setup_size + usb_batch->buffer_size);
125 CHECK_NULL_DISPOSE_RET(ohci_batch->device_buffer,
126 "Failed to allocate device accessible buffer.\n");
127 /* Copy setup data */
128 memcpy(ohci_batch->device_buffer, usb_batch->setup_buffer,
129 usb_batch->setup_size);
130 /* Copy generic data */
131 if (usb_batch->ep->direction != USB_DIRECTION_IN)
132 memcpy(
133 ohci_batch->device_buffer + usb_batch->setup_size,
134 usb_batch->buffer, usb_batch->buffer_size);
135 }
136 ohci_batch->usb_batch = usb_batch;
137
138 const usb_direction_t dir = usb_transfer_batch_direction(usb_batch);
139 assert(batch_setup[usb_batch->ep->transfer_type]);
140 batch_setup[usb_batch->ep->transfer_type](ohci_batch, dir);
141
142 return ohci_batch;
143#undef CHECK_NULL_DISPOSE_RET
144}
145/*----------------------------------------------------------------------------*/
146/** Check batch TDs' status.
147 *
148 * @param[in] ohci_batch Batch structure to use.
149 * @return False, if there is an active TD, true otherwise.
150 *
151 * Walk all TDs (usually there is just one). Stop with false if there is an
152 * active TD. Stop with true if an error is found. Return true if the walk
153 * completes with the last TD.
154 */
155bool ohci_transfer_batch_is_complete(ohci_transfer_batch_t *ohci_batch)
156{
157 assert(ohci_batch);
158 assert(ohci_batch->usb_batch);
159
160 usb_log_debug("Batch %p checking %zu td(s) for completion.\n",
161 ohci_batch->usb_batch, ohci_batch->td_count);
162 usb_log_debug2("ED: %08x:%08x:%08x:%08x.\n",
163 ohci_batch->ed->status, ohci_batch->ed->td_head,
164 ohci_batch->ed->td_tail, ohci_batch->ed->next);
165
166 if (!ed_inactive(ohci_batch->ed) && ed_transfer_pending(ohci_batch->ed))
167 return false;
168
169 /* Now we may be sure that either the ED is inactive because of errors
170 * or all transfer descriptors completed successfully */
171
172 /* Assume all data got through */
173 ohci_batch->usb_batch->transfered_size =
174 ohci_batch->usb_batch->buffer_size;
175
176 /* Assume we will leave the last(unused) TD behind */
177 ohci_batch->leave_td = ohci_batch->td_count;
178
179 /* Check all TDs */
180 for (size_t i = 0; i < ohci_batch->td_count; ++i) {
181 assert(ohci_batch->tds[i] != NULL);
182 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
183 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
184 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
185
186 /* If the TD got all its data through, it will report 0 bytes
187 * remain, the sole exception is INPUT with data rounding flag
188 * (short), i.e. every INPUT. Nice thing is that short packets
189 * will correctly report remaining data, thus making
190 * this computation correct (short packets need to be produced
191 * by the last TD)
192 * NOTE: This also works for CONTROL transfer as
193 * the first TD will return 0 remain.
194 * NOTE: Short packets don't break the assumption that
195 * we leave the very last(unused) TD behind.
196 */
197 ohci_batch->usb_batch->transfered_size
198 -= td_remain_size(ohci_batch->tds[i]);
199
200 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
201 if (ohci_batch->usb_batch->error != EOK) {
202 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
203 ohci_batch->usb_batch, i,
204 ohci_batch->tds[i]->status);
205
206 /* ED should be stopped because of errors */
207 assert((ohci_batch->ed->td_head & ED_TDHEAD_HALTED_FLAG) != 0);
208
209 /* Now we have a problem: we don't know what TD
210 * the head pointer points to, the retiring rules
211 * described in specs say it should be the one after
212 * the failed one so set the tail pointer accordingly.
213 * It will be the one TD we leave behind.
214 */
215 ohci_batch->leave_td = i + 1;
216
217 /* Check TD assumption */
218 const uint32_t pa = addr_to_phys(
219 ohci_batch->tds[ohci_batch->leave_td]);
220 assert((ohci_batch->ed->td_head & ED_TDTAIL_PTR_MASK)
221 == pa);
222
223 ed_set_tail_td(ohci_batch->ed,
224 ohci_batch->tds[ohci_batch->leave_td]);
225
226 /* Clear possible ED HALT */
227 ohci_batch->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
228 break;
229 }
230 }
231 assert(ohci_batch->usb_batch->transfered_size <=
232 ohci_batch->usb_batch->buffer_size);
233
234 /* Store the remaining TD */
235 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
236 assert(ohci_ep);
237 ohci_ep->td = ohci_batch->tds[ohci_batch->leave_td];
238
239 /* Make sure that we are leaving the right TD behind */
240 const uint32_t pa = addr_to_phys(ohci_ep->td);
241 assert(pa == (ohci_batch->ed->td_head & ED_TDHEAD_PTR_MASK));
242 assert(pa == (ohci_batch->ed->td_tail & ED_TDTAIL_PTR_MASK));
243
244 return true;
245}
246/*----------------------------------------------------------------------------*/
247/** Starts execution of the TD list
248 *
249 * @param[in] ohci_batch Batch structure to use
250 */
251void ohci_transfer_batch_commit(ohci_transfer_batch_t *ohci_batch)
252{
253 assert(ohci_batch);
254 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
255}
256/*----------------------------------------------------------------------------*/
257/** Prepare generic control transfer
258 *
259 * @param[in] ohci_batch Batch structure to use.
260 * @param[in] dir Communication direction
261 *
262 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
263 * Data stage with alternating toggle and direction supplied by parameter.
264 * Status stage with toggle 1 and direction supplied by parameter.
265 */
266static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
267{
268 assert(ohci_batch);
269 assert(ohci_batch->usb_batch);
270 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
271 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
272 ohci_batch->ed->status, ohci_batch->ed->td_tail,
273 ohci_batch->ed->td_head, ohci_batch->ed->next);
274 static const usb_direction_t reverse_dir[] = {
275 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
276 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
277 };
278
279 int toggle = 0;
280 const char* buffer = ohci_batch->device_buffer;
281 const usb_direction_t data_dir = dir;
282 const usb_direction_t status_dir = reverse_dir[dir];
283
284 /* Setup stage */
285 td_init(
286 ohci_batch->tds[0], ohci_batch->tds[1], USB_DIRECTION_BOTH,
287 buffer, ohci_batch->usb_batch->setup_size, toggle);
288 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
289 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
290 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
291 buffer += ohci_batch->usb_batch->setup_size;
292
293 /* Data stage */
294 size_t td_current = 1;
295 size_t remain_size = ohci_batch->usb_batch->buffer_size;
296 while (remain_size > 0) {
297 const size_t transfer_size =
298 min(remain_size, OHCI_TD_MAX_TRANSFER);
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.