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

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

ohci: OHCI ED routines refactoring.

Merge ed_set_td to ed_init: td should be set explicitly only during initialization.
Add doxygen and other comments.

  • Property mode set to 100644
File size: 12.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
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_dispose(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 size_t i = 0;
166 for (; i < ohci_batch->td_count; ++i) {
167 assert(ohci_batch->tds[i] != NULL);
168 usb_log_debug("TD %zu: %08x:%08x:%08x:%08x.\n", i,
169 ohci_batch->tds[i]->status, ohci_batch->tds[i]->cbp,
170 ohci_batch->tds[i]->next, ohci_batch->tds[i]->be);
171 if (!td_is_finished(ohci_batch->tds[i])) {
172 return false;
173 }
174 ohci_batch->usb_batch->error = td_error(ohci_batch->tds[i]);
175 if (ohci_batch->usb_batch->error != EOK) {
176 usb_log_debug("Batch %p found error TD(%zu):%08x.\n",
177 ohci_batch->usb_batch, i,
178 ohci_batch->tds[i]->status);
179 /* Make sure TD queue is empty (one TD),
180 * ED should be marked as halted */
181 ohci_batch->ed->td_tail =
182 (ohci_batch->ed->td_head & ED_TDTAIL_PTR_MASK);
183 ++i;
184 break;
185 }
186 }
187
188 assert(i <= ohci_batch->td_count);
189 ohci_batch->leave_td = i;
190
191 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ohci_batch->usb_batch->ep);
192 assert(ohci_ep);
193 ohci_ep->td = ohci_batch->tds[ohci_batch->leave_td];
194 assert(i > 0);
195 ohci_batch->usb_batch->transfered_size =
196 ohci_batch->usb_batch->buffer_size;
197 for (--i;i < ohci_batch->td_count; ++i) {
198 ohci_batch->usb_batch->transfered_size
199 -= td_remain_size(ohci_batch->tds[i]);
200 }
201
202 /* Clear possible ED HALT */
203 ohci_batch->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
204 /* just make sure that we are leaving the right TD behind */
205 const uint32_t pa = addr_to_phys(ohci_ep->td);
206 assert(pa == (ohci_batch->ed->td_head & ED_TDHEAD_PTR_MASK));
207 assert(pa == (ohci_batch->ed->td_tail & ED_TDTAIL_PTR_MASK));
208
209 return true;
210}
211/*----------------------------------------------------------------------------*/
212/** Starts execution of the TD list
213 *
214 * @param[in] ohci_batch Batch structure to use
215 */
216void ohci_transfer_batch_commit(ohci_transfer_batch_t *ohci_batch)
217{
218 assert(ohci_batch);
219 ed_set_tail_td(ohci_batch->ed, ohci_batch->tds[ohci_batch->td_count]);
220}
221/*----------------------------------------------------------------------------*/
222/** Prepare generic control transfer
223 *
224 * @param[in] ohci_batch Batch structure to use.
225 * @param[in] dir Communication direction
226 *
227 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
228 * Data stage with alternating toggle and direction supplied by parameter.
229 * Status stage with toggle 1 and direction supplied by parameter.
230 */
231static void batch_control(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
232{
233 assert(ohci_batch);
234 assert(ohci_batch->usb_batch);
235 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
236 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
237 ohci_batch->ed->status, ohci_batch->ed->td_tail,
238 ohci_batch->ed->td_head, ohci_batch->ed->next);
239 static const usb_direction_t reverse_dir[] = {
240 [USB_DIRECTION_IN] = USB_DIRECTION_OUT,
241 [USB_DIRECTION_OUT] = USB_DIRECTION_IN,
242 };
243
244 int toggle = 0;
245 const char* buffer = ohci_batch->device_buffer;
246 const usb_direction_t data_dir = dir;
247 const usb_direction_t status_dir = reverse_dir[dir];
248
249 /* setup stage */
250 td_init(ohci_batch->tds[0], USB_DIRECTION_BOTH, buffer,
251 ohci_batch->usb_batch->setup_size, toggle);
252 td_set_next(ohci_batch->tds[0], ohci_batch->tds[1]);
253 usb_log_debug("Created CONTROL SETUP TD: %08x:%08x:%08x:%08x.\n",
254 ohci_batch->tds[0]->status, ohci_batch->tds[0]->cbp,
255 ohci_batch->tds[0]->next, ohci_batch->tds[0]->be);
256 buffer += ohci_batch->usb_batch->setup_size;
257
258 /* data stage */
259 size_t td_current = 1;
260 size_t remain_size = ohci_batch->usb_batch->buffer_size;
261 while (remain_size > 0) {
262 const size_t transfer_size =
263 remain_size > OHCI_TD_MAX_TRANSFER ?
264 OHCI_TD_MAX_TRANSFER : remain_size;
265 toggle = 1 - toggle;
266
267 td_init(ohci_batch->tds[td_current], data_dir, buffer,
268 transfer_size, toggle);
269 td_set_next(ohci_batch->tds[td_current],
270 ohci_batch->tds[td_current + 1]);
271 usb_log_debug("Created CONTROL DATA TD: %08x:%08x:%08x:%08x.\n",
272 ohci_batch->tds[td_current]->status,
273 ohci_batch->tds[td_current]->cbp,
274 ohci_batch->tds[td_current]->next,
275 ohci_batch->tds[td_current]->be);
276
277 buffer += transfer_size;
278 remain_size -= transfer_size;
279 assert(td_current < ohci_batch->td_count - 1);
280 ++td_current;
281 }
282
283 /* status stage */
284 assert(td_current == ohci_batch->td_count - 1);
285 td_init(ohci_batch->tds[td_current], status_dir, NULL, 0, 1);
286 td_set_next(ohci_batch->tds[td_current],
287 ohci_batch->tds[td_current + 1]);
288 usb_log_debug("Created CONTROL STATUS TD: %08x:%08x:%08x:%08x.\n",
289 ohci_batch->tds[td_current]->status,
290 ohci_batch->tds[td_current]->cbp,
291 ohci_batch->tds[td_current]->next,
292 ohci_batch->tds[td_current]->be);
293 usb_log_debug2(
294 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
295 ohci_batch->usb_batch,
296 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
297 usb_str_direction(dir),
298 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
299}
300/*----------------------------------------------------------------------------*/
301/** Prepare generic data transfer
302 *
303 * @param[in] ohci_batch Batch structure to use.
304 * @paramp[in] dir Communication direction.
305 *
306 * Direction is supplied by the associated ep and toggle is maintained by the
307 * OHCI hw in ED.
308 */
309static void batch_data(ohci_transfer_batch_t *ohci_batch, usb_direction_t dir)
310{
311 assert(ohci_batch);
312 assert(ohci_batch->usb_batch);
313 assert(dir == USB_DIRECTION_IN || dir == USB_DIRECTION_OUT);
314 usb_log_debug("Using ED(%p): %08x:%08x:%08x:%08x.\n", ohci_batch->ed,
315 ohci_batch->ed->status, ohci_batch->ed->td_tail,
316 ohci_batch->ed->td_head, ohci_batch->ed->next);
317
318 size_t td_current = 0;
319 size_t remain_size = ohci_batch->usb_batch->buffer_size;
320 char *buffer = ohci_batch->device_buffer;
321 while (remain_size > 0) {
322 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
323 ? OHCI_TD_MAX_TRANSFER : remain_size;
324
325 td_init(ohci_batch->tds[td_current], dir, buffer,
326 transfer_size, -1);
327 td_set_next(ohci_batch->tds[td_current],
328 ohci_batch->tds[td_current + 1]);
329
330 usb_log_debug("Created DATA TD: %08x:%08x:%08x:%08x.\n",
331 ohci_batch->tds[td_current]->status,
332 ohci_batch->tds[td_current]->cbp,
333 ohci_batch->tds[td_current]->next,
334 ohci_batch->tds[td_current]->be);
335
336 buffer += transfer_size;
337 remain_size -= transfer_size;
338 assert(td_current < ohci_batch->td_count);
339 ++td_current;
340 }
341 usb_log_debug2(
342 "Batch %p %s %s " USB_TRANSFER_BATCH_FMT " initialized.\n", \
343 ohci_batch->usb_batch,
344 usb_str_transfer_type(ohci_batch->usb_batch->ep->transfer_type),
345 usb_str_direction(dir),
346 USB_TRANSFER_BATCH_ARGS(*ohci_batch->usb_batch));
347}
348/*----------------------------------------------------------------------------*/
349static void (*const batch_setup[])(ohci_transfer_batch_t*, usb_direction_t) =
350{
351 [USB_TRANSFER_CONTROL] = batch_control,
352 [USB_TRANSFER_BULK] = batch_data,
353 [USB_TRANSFER_INTERRUPT] = batch_data,
354 [USB_TRANSFER_ISOCHRONOUS] = NULL,
355};
356/**
357 * @}
358 */
Note: See TracBrowser for help on using the repository browser.