source: mainline/uspace/drv/bus/usb/ohci/batch.c@ e2976bb

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

OHCI: Begin move to new hcd arch, plug in batch init hook.

  • Property mode set to 100644
File size: 17.2 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 "batch.h"
41#include "hcd_endpoint.h"
42#include "utils/malloc32.h"
43#include "hw_struct/endpoint_descriptor.h"
44#include "hw_struct/transfer_descriptor.h"
45
46/** OHCI specific data required for USB transfer */
47typedef struct ohci_transfer_batch {
48 /** Endpoint descriptor of the target endpoint. */
49 ed_t *ed;
50 /** List of TDs needed for the transfer */
51 td_t **tds;
52 /** Number of TDs used by the transfer */
53 size_t td_count;
54 /** Dummy TD to be left at the ED and used by the next transfer */
55 size_t leave_td;
56 /** Data buffer, must be accessible byb the OHCI hw. */
57 void *device_buffer;
58} ohci_transfer_batch_t;
59/*----------------------------------------------------------------------------*/
60static void batch_control(usb_transfer_batch_t *instance,
61 usb_direction_t data_dir, usb_direction_t status_dir);
62static void batch_data(usb_transfer_batch_t *instance);
63/*----------------------------------------------------------------------------*/
64/** Safely destructs ohci_transfer_batch_t structure
65 *
66 * @param[in] ohci_batch Instance to destroy.
67 */
68static void ohci_batch_dispose(void *ohci_batch)
69{
70 ohci_transfer_batch_t *instance = ohci_batch;
71 if (!instance)
72 return;
73 free32(instance->device_buffer);
74 unsigned i = 0;
75 if (instance->tds) {
76 for (; i< instance->td_count; ++i) {
77 if (i != instance->leave_td)
78 free32(instance->tds[i]);
79 }
80 free(instance->tds);
81 }
82 free(instance);
83}
84/*----------------------------------------------------------------------------*/
85int batch_init_ohci(usb_transfer_batch_t *batch)
86{
87 assert(batch);
88#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
89 if (ptr == NULL) { \
90 usb_log_error(message); \
91 if (data) { \
92 ohci_batch_dispose(data); \
93 } \
94 return ENOMEM; \
95 } else (void)0
96
97 const hcd_endpoint_t *hcd_ep = hcd_endpoint_get(batch->ep);
98 assert(hcd_ep);
99
100 ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1);
101 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
102
103 data->td_count = ((batch->buffer_size + OHCI_TD_MAX_TRANSFER - 1)
104 / OHCI_TD_MAX_TRANSFER);
105 /* Control transfer need Setup and Status stage */
106 if (batch->ep->transfer_type == USB_TRANSFER_CONTROL) {
107 data->td_count += 2;
108 }
109
110 /* We need an extra place for TD that is currently assigned to hcd_ep*/
111 data->tds = calloc(sizeof(td_t*), data->td_count + 1);
112 CHECK_NULL_DISPOSE_RETURN(data->tds,
113 "Failed to allocate transfer descriptors.\n");
114
115 /* Add TD left over by the previous transfer */
116 data->tds[0] = hcd_ep->td;
117 data->leave_td = 0;
118 unsigned i = 1;
119 for (; i <= data->td_count; ++i) {
120 data->tds[i] = malloc32(sizeof(td_t));
121 CHECK_NULL_DISPOSE_RETURN(data->tds[i],
122 "Failed to allocate TD %d.\n", i );
123 }
124
125 data->ed = hcd_ep->ed;
126 batch->private_data = data;
127 batch->private_data_dtor = ohci_batch_dispose;
128
129 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
130 * it is, however, not capable of handling buffer that occupies more
131 * than two pages (the first page is computed using start pointer, the
132 * other using the end pointer) */
133 if (batch->setup_size + batch->buffer_size > 0) {
134 data->device_buffer =
135 malloc32(batch->setup_size + batch->buffer_size);
136 CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
137 "Failed to allocate device accessible buffer.\n");
138 memcpy(data->device_buffer, batch->setup_buffer,
139 batch->setup_size);
140 batch->data_buffer = data->device_buffer + batch->setup_size;
141 }
142
143 return EOK;
144#undef CHECK_NULL_DISPOSE_RETURN
145}
146/*----------------------------------------------------------------------------*/
147/** Allocate memory initialize internal structures
148 *
149 * @param[in] fun DDF function to pass to callback.
150 * @param[in] ep Communication target
151 * @param[in] buffer Data source/destination.
152 * @param[in] buffer_size Size of the buffer.
153 * @param[in] setup_buffer Setup data source (if not NULL)
154 * @param[in] setup_size Size of setup_buffer (should be always 8)
155 * @param[in] func_in function to call on inbound transfer completion
156 * @param[in] func_out function to call on outbound transfer completion
157 * @param[in] arg additional parameter to func_in or func_out
158 * @return Valid pointer if all structures were successfully created,
159 * NULL otherwise.
160 *
161 * Allocates and initializes structures needed by the OHCI hw for the transfer.
162 */
163usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
164 char *buffer, size_t buffer_size,
165 const char *setup_buffer, size_t setup_size,
166 usbhc_iface_transfer_in_callback_t func_in,
167 usbhc_iface_transfer_out_callback_t func_out, void *arg)
168{
169#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
170 if (ptr == NULL) { \
171 usb_log_error(message); \
172 if (instance) { \
173 usb_transfer_batch_dispose(instance); \
174 } \
175 return NULL; \
176 } else (void)0
177
178 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
179 CHECK_NULL_DISPOSE_RETURN(instance,
180 "Failed to allocate batch instance.\n");
181 usb_transfer_batch_init(instance, ep, buffer, NULL, buffer_size,
182 NULL, setup_size, func_in, func_out, arg, fun, NULL,
183 ohci_batch_dispose);
184
185 const hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep);
186 assert(hcd_ep);
187
188 ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1);
189 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
190 instance->private_data = data;
191
192 data->td_count =
193 ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
194 /* Control transfer need Setup and Status stage */
195 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
196 data->td_count += 2;
197 }
198
199 /* We need an extra place for TD that is currently assigned to hcd_ep*/
200 data->tds = calloc(sizeof(td_t*), data->td_count + 1);
201 CHECK_NULL_DISPOSE_RETURN(data->tds,
202 "Failed to allocate transfer descriptors.\n");
203
204 /* Add TD left over by the previous transfer */
205 data->tds[0] = hcd_ep->td;
206 data->leave_td = 0;
207 unsigned i = 1;
208 for (; i <= data->td_count; ++i) {
209 data->tds[i] = malloc32(sizeof(td_t));
210 CHECK_NULL_DISPOSE_RETURN(data->tds[i],
211 "Failed to allocate TD %d.\n", i );
212 }
213
214 data->ed = hcd_ep->ed;
215
216 /* NOTE: OHCI is capable of handling buffer that crosses page boundaries
217 * it is, however, not capable of handling buffer that occupies more
218 * than two pages (the first page is computed using start pointer, the
219 * other using the end pointer) */
220 if (setup_size + buffer_size > 0) {
221 data->device_buffer = malloc32(setup_size + buffer_size);
222 CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
223 "Failed to allocate device accessible buffer.\n");
224 instance->setup_buffer = data->device_buffer;
225 instance->data_buffer = data->device_buffer + setup_size;
226 memcpy(instance->setup_buffer, setup_buffer, setup_size);
227 }
228
229 return instance;
230}
231/*----------------------------------------------------------------------------*/
232/** Check batch TDs' status.
233 *
234 * @param[in] instance Batch structure to use.
235 * @return False, if there is an active TD, true otherwise.
236 *
237 * Walk all TDs (usually there is just one). Stop with false if there is an
238 * active TD. Stop with true if an error is found. Return true if the walk
239 * completes with the last TD.
240 */
241bool batch_is_complete(usb_transfer_batch_t *instance)
242{
243 assert(instance);
244 ohci_transfer_batch_t *data = instance->private_data;
245 assert(data);
246 usb_log_debug("Batch(%p) checking %zu td(s) for completion.\n",
247 instance, data->td_count);
248 usb_log_debug("ED: %x:%x:%x:%x.\n",
249 data->ed->status, data->ed->td_head, data->ed->td_tail,
250 data->ed->next);
251 size_t i = 0;
252 instance->transfered_size = instance->buffer_size;
253 for (; i < data->td_count; ++i) {
254 assert(data->tds[i] != NULL);
255 usb_log_debug("TD %zu: %x:%x:%x:%x.\n", i,
256 data->tds[i]->status, data->tds[i]->cbp, data->tds[i]->next,
257 data->tds[i]->be);
258 if (!td_is_finished(data->tds[i])) {
259 return false;
260 }
261 instance->error = td_error(data->tds[i]);
262 if (instance->error != EOK) {
263 usb_log_debug("Batch(%p) found error TD(%zu):%x.\n",
264 instance, i, data->tds[i]->status);
265 /* Make sure TD queue is empty (one TD),
266 * ED should be marked as halted */
267 data->ed->td_tail =
268 (data->ed->td_head & ED_TDTAIL_PTR_MASK);
269 ++i;
270 break;
271 }
272 }
273 data->leave_td = i;
274 assert(data->leave_td <= data->td_count);
275 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(instance->ep);
276 assert(hcd_ep);
277 hcd_ep->td = data->tds[i];
278 assert(i > 0);
279 for (--i;i < data->td_count; ++i)
280 instance->transfered_size -= td_remain_size(data->tds[i]);
281
282 /* Clear possible ED HALT */
283 data->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
284 const uint32_t pa = addr_to_phys(hcd_ep->td);
285 assert(pa == (data->ed->td_head & ED_TDHEAD_PTR_MASK));
286 assert(pa == (data->ed->td_tail & ED_TDTAIL_PTR_MASK));
287
288 return true;
289}
290/*----------------------------------------------------------------------------*/
291/** Starts execution of the TD list
292 *
293 * @param[in] instance Batch structure to use
294 */
295void batch_commit(usb_transfer_batch_t *instance)
296{
297 assert(instance);
298 ohci_transfer_batch_t *data = instance->private_data;
299 assert(data);
300 ed_set_end_td(data->ed, data->tds[data->td_count]);
301}
302/*----------------------------------------------------------------------------*/
303/** Prepares control write transfer.
304 *
305 * @param[in] instance Batch structure to use.
306 *
307 * Uses generic control transfer using direction OUT(data stage) and
308 * IN(status stage).
309 */
310void batch_control_write(usb_transfer_batch_t *instance)
311{
312 assert(instance);
313 /* We are data out, we are supposed to provide data */
314 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
315 instance->next_step = usb_transfer_batch_call_out_and_dispose;
316 batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
317 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
318}
319/*----------------------------------------------------------------------------*/
320/** Prepares control read transfer.
321 *
322 * @param[in] instance Batch structure to use.
323 *
324 * Uses generic control transfer using direction IN(data stage) and
325 * OUT(status stage).
326 */
327void batch_control_read(usb_transfer_batch_t *instance)
328{
329 assert(instance);
330 instance->next_step = usb_transfer_batch_call_in_and_dispose;
331 batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
332 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
333}
334/*----------------------------------------------------------------------------*/
335/** Prepare interrupt in transfer.
336 *
337 * @param[in] instance Batch structure to use.
338 *
339 * Data transfer.
340 */
341void batch_interrupt_in(usb_transfer_batch_t *instance)
342{
343 assert(instance);
344 instance->next_step = usb_transfer_batch_call_in_and_dispose;
345 batch_data(instance);
346 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
347}
348/*----------------------------------------------------------------------------*/
349/** Prepare interrupt out transfer.
350 *
351 * @param[in] instance Batch structure to use.
352 *
353 * Data transfer.
354 */
355void batch_interrupt_out(usb_transfer_batch_t *instance)
356{
357 assert(instance);
358 /* We are data out, we are supposed to provide data */
359 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
360 instance->next_step = usb_transfer_batch_call_out_and_dispose;
361 batch_data(instance);
362 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
363}
364/*----------------------------------------------------------------------------*/
365/** Prepare bulk in transfer.
366 *
367 * @param[in] instance Batch structure to use.
368 *
369 * Data transfer.
370 */
371void batch_bulk_in(usb_transfer_batch_t *instance)
372{
373 assert(instance);
374 instance->next_step = usb_transfer_batch_call_in_and_dispose;
375 batch_data(instance);
376 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
377}
378/*----------------------------------------------------------------------------*/
379/** Prepare bulk out transfer.
380 *
381 * @param[in] instance Batch structure to use.
382 *
383 * Data transfer.
384 */
385void batch_bulk_out(usb_transfer_batch_t *instance)
386{
387 assert(instance);
388 /* We are data out, we are supposed to provide data */
389 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
390 instance->next_step = usb_transfer_batch_call_out_and_dispose;
391 batch_data(instance);
392 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
393}
394/*----------------------------------------------------------------------------*/
395/** Prepare generic control transfer
396 *
397 * @param[in] instance Batch structure to use.
398 * @param[in] data_dir Direction to use for data stage.
399 * @param[in] status_dir Direction to use for status stage.
400 *
401 * Setup stage with toggle 0 and direction BOTH(SETUP_PID)
402 * Data stage with alternating toggle and direction supplied by parameter.
403 * Status stage with toggle 1 and direction supplied by parameter.
404 */
405void batch_control(usb_transfer_batch_t *instance,
406 usb_direction_t data_dir, usb_direction_t status_dir)
407{
408 assert(instance);
409 ohci_transfer_batch_t *data = instance->private_data;
410 assert(data);
411 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
412 data->ed->status, data->ed->td_tail, data->ed->td_head,
413 data->ed->next);
414 int toggle = 0;
415 /* setup stage */
416 td_init(data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
417 instance->setup_size, toggle);
418 td_set_next(data->tds[0], data->tds[1]);
419 usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0]->status,
420 data->tds[0]->cbp, data->tds[0]->next, data->tds[0]->be);
421
422 /* data stage */
423 size_t td_current = 1;
424 size_t remain_size = instance->buffer_size;
425 char *buffer = instance->data_buffer;
426 while (remain_size > 0) {
427 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
428 OHCI_TD_MAX_TRANSFER : remain_size;
429 toggle = 1 - toggle;
430
431 td_init(data->tds[td_current], data_dir, buffer,
432 transfer_size, toggle);
433 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
434 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
435 data->tds[td_current]->status, data->tds[td_current]->cbp,
436 data->tds[td_current]->next, data->tds[td_current]->be);
437
438 buffer += transfer_size;
439 remain_size -= transfer_size;
440 assert(td_current < data->td_count - 1);
441 ++td_current;
442 }
443
444 /* status stage */
445 assert(td_current == data->td_count - 1);
446 td_init(data->tds[td_current], status_dir, NULL, 0, 1);
447 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
448 usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
449 data->tds[td_current]->status, data->tds[td_current]->cbp,
450 data->tds[td_current]->next, data->tds[td_current]->be);
451}
452/*----------------------------------------------------------------------------*/
453/** Prepare generic data transfer
454 *
455 * @param[in] instance Batch structure to use.
456 *
457 * Direction is supplied by the associated ep and toggle is maintained by the
458 * OHCI hw in ED.
459 */
460void batch_data(usb_transfer_batch_t *instance)
461{
462 assert(instance);
463 ohci_transfer_batch_t *data = instance->private_data;
464 assert(data);
465 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
466 data->ed->status, data->ed->td_tail, data->ed->td_head,
467 data->ed->next);
468
469 size_t td_current = 0;
470 size_t remain_size = instance->buffer_size;
471 char *buffer = instance->data_buffer;
472 while (remain_size > 0) {
473 const size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER
474 ? OHCI_TD_MAX_TRANSFER : remain_size;
475
476 td_init(data->tds[td_current], instance->ep->direction,
477 buffer, transfer_size, -1);
478 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
479 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
480 data->tds[td_current]->status, data->tds[td_current]->cbp,
481 data->tds[td_current]->next, data->tds[td_current]->be);
482
483 buffer += transfer_size;
484 remain_size -= transfer_size;
485 assert(td_current < data->td_count);
486 ++td_current;
487 }
488}
489/**
490 * @}
491 */
Note: See TracBrowser for help on using the repository browser.