source: mainline/uspace/drv/ohci/batch.c@ f13381b

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

More debug output and some fixes

Add mutex for scheduling and removing of batches
batch_data initialize the first TD not the second
disable and enable both isochronous and interrupt processing when scheduling periodic transfer.
undef the right macro in transfer_list initialization

  • Property mode set to 100644
File size: 12.0 KB
RevLine 
[41b96b4]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 */
[81dce9f]28/** @addtogroup drvusbohci
[41b96b4]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"
[2bf8f8c]41#include "utils/malloc32.h"
[06c552c]42#include "hw_struct/endpoint_descriptor.h"
43#include "hw_struct/transfer_descriptor.h"
44
45typedef struct ohci_batch {
46 ed_t *ed;
47 td_t *tds;
48 size_t td_count;
49} ohci_batch_t;
[41b96b4]50
[e42dd32]51static void batch_control(usb_transfer_batch_t *instance,
52 usb_direction_t data_dir, usb_direction_t status_dir);
[c6fe469]53static void batch_data(usb_transfer_batch_t *instance);
[1387692]54static void batch_call_in_and_dispose(usb_transfer_batch_t *instance);
55static void batch_call_out_and_dispose(usb_transfer_batch_t *instance);
[d328172]56
[81dce9f]57#define DEFAULT_ERROR_COUNT 3
[6bec59b]58usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
59 char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size,
[be7950e8]60 usbhc_iface_transfer_in_callback_t func_in,
[6bec59b]61 usbhc_iface_transfer_out_callback_t func_out, void *arg)
[be7950e8]62{
[2bf8f8c]63#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
64 if (ptr == NULL) { \
65 usb_log_error(message); \
66 if (instance) { \
67 batch_dispose(instance); \
68 } \
69 return NULL; \
70 } else (void)0
71
[1387692]72 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
[2bf8f8c]73 CHECK_NULL_DISPOSE_RETURN(instance,
74 "Failed to allocate batch instance.\n");
[6bec59b]75 usb_target_t target =
76 { .address = ep->address, .endpoint = ep->endpoint };
77 usb_transfer_batch_init(instance, target, ep->transfer_type, ep->speed,
78 ep->max_packet_size, buffer, NULL, buffer_size, NULL, setup_size,
79 func_in, func_out, arg, fun, ep, NULL);
[2bf8f8c]80
[06c552c]81 ohci_batch_t *data = malloc(sizeof(ohci_batch_t));
82 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
83 bzero(data, sizeof(ohci_batch_t));
84 instance->private_data = data;
85
[7786cea]86 /* we needs + 1 transfer descriptor as the last one won't be executed */
[b854e56]87 data->td_count = 1 +
88 ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
[06c552c]89 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
90 data->td_count += 2;
91 }
92
93 data->tds = malloc32(sizeof(td_t) * data->td_count);
94 CHECK_NULL_DISPOSE_RETURN(data->tds,
95 "Failed to allocate transfer descriptors.\n");
96 bzero(data->tds, sizeof(td_t) * data->td_count);
97
98 data->ed = malloc32(sizeof(ed_t));
99 CHECK_NULL_DISPOSE_RETURN(data->ed,
100 "Failed to allocate endpoint descriptor.\n");
101
[2bf8f8c]102 if (buffer_size > 0) {
103 instance->transport_buffer = malloc32(buffer_size);
104 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
105 "Failed to allocate device accessible buffer.\n");
106 }
107
108 if (setup_size > 0) {
109 instance->setup_buffer = malloc32(setup_size);
110 CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
111 "Failed to allocate device accessible setup buffer.\n");
112 memcpy(instance->setup_buffer, setup_buffer, setup_size);
113 }
114
115 return instance;
[be7950e8]116}
117/*----------------------------------------------------------------------------*/
[1387692]118void batch_dispose(usb_transfer_batch_t *instance)
[be7950e8]119{
[2bf8f8c]120 assert(instance);
[f1be95c8]121 ohci_batch_t *data = instance->private_data;
122 assert(data);
123 free32(data->ed);
124 free32(data->tds);
[d328172]125 free32(instance->setup_buffer);
[f1be95c8]126 free32(instance->transport_buffer);
127 free(data);
[d328172]128 free(instance);
[be7950e8]129}
130/*----------------------------------------------------------------------------*/
[f98b8269]131bool batch_is_complete(usb_transfer_batch_t *instance)
132{
[f1be95c8]133 assert(instance);
134 ohci_batch_t *data = instance->private_data;
135 assert(data);
136 size_t tds = data->td_count - 1;
[78d4e1f]137 usb_log_debug("Batch(%p) checking %d td(s) for completion.\n",
[f1be95c8]138 instance, tds);
[78d4e1f]139 usb_log_debug("ED: %x:%x:%x:%x.\n",
140 data->ed->status, data->ed->td_head, data->ed->td_tail,
141 data->ed->next);
[f1be95c8]142 size_t i = 0;
143 for (; i < tds; ++i) {
[78d4e1f]144 usb_log_debug("TD %d: %x:%x:%x:%x.\n", i,
145 data->tds[i].status, data->tds[i].cbp, data->tds[i].next,
146 data->tds[i].be);
[aa9ccf7]147 if (!td_is_finished(&data->tds[i])) {
[f1be95c8]148 return false;
[aa9ccf7]149 }
[f1be95c8]150 instance->error = td_error(&data->tds[i]);
151 /* FIXME: calculate real transfered size */
152 instance->transfered_size = instance->buffer_size;
153 if (instance->error != EOK) {
154 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
155 instance, i, data->tds[i].status);
156 return true;
157// endpoint_toggle_set(instance->ep,
158 }
159 }
160 return true;
[f98b8269]161}
162/*----------------------------------------------------------------------------*/
[1387692]163void batch_control_write(usb_transfer_batch_t *instance)
[be7950e8]164{
[2bf8f8c]165 assert(instance);
[d328172]166 /* We are data out, we are supposed to provide data */
167 memcpy(instance->transport_buffer, instance->buffer,
168 instance->buffer_size);
169 instance->next_step = batch_call_out_and_dispose;
[e42dd32]170 batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
[d328172]171 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[be7950e8]172}
173/*----------------------------------------------------------------------------*/
[1387692]174void batch_control_read(usb_transfer_batch_t *instance)
[be7950e8]175{
[2bf8f8c]176 assert(instance);
[d328172]177 instance->next_step = batch_call_in_and_dispose;
[e42dd32]178 batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
[81001f6]179 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[be7950e8]180}
181/*----------------------------------------------------------------------------*/
[1387692]182void batch_interrupt_in(usb_transfer_batch_t *instance)
[be7950e8]183{
[2bf8f8c]184 assert(instance);
[7786cea]185 assert(instance->direction == USB_DIRECTION_IN);
[d328172]186 instance->next_step = batch_call_in_and_dispose;
[c6fe469]187 batch_data(instance);
[d328172]188 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[be7950e8]189}
190/*----------------------------------------------------------------------------*/
[1387692]191void batch_interrupt_out(usb_transfer_batch_t *instance)
[be7950e8]192{
[2bf8f8c]193 assert(instance);
[7786cea]194 assert(instance->direction == USB_DIRECTION_OUT);
[d328172]195 /* We are data out, we are supposed to provide data */
196 memcpy(instance->transport_buffer, instance->buffer,
197 instance->buffer_size);
198 instance->next_step = batch_call_out_and_dispose;
[c6fe469]199 batch_data(instance);
[d328172]200 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
[be7950e8]201}
202/*----------------------------------------------------------------------------*/
[1387692]203void batch_bulk_in(usb_transfer_batch_t *instance)
[be7950e8]204{
[2bf8f8c]205 assert(instance);
[d328172]206 instance->direction = USB_DIRECTION_IN;
207 instance->next_step = batch_call_in_and_dispose;
[c6fe469]208 batch_data(instance);
[d328172]209 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
[be7950e8]210}
211/*----------------------------------------------------------------------------*/
[1387692]212void batch_bulk_out(usb_transfer_batch_t *instance)
[be7950e8]213{
[2bf8f8c]214 assert(instance);
[d328172]215 instance->direction = USB_DIRECTION_IN;
216 instance->next_step = batch_call_in_and_dispose;
[c6fe469]217 batch_data(instance);
[d328172]218 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
219}
220/*----------------------------------------------------------------------------*/
[f98b8269]221ed_t * batch_ed(usb_transfer_batch_t *instance)
222{
[7786cea]223 assert(instance);
224 ohci_batch_t *data = instance->private_data;
225 assert(data);
226 return data->ed;
227}
228/*----------------------------------------------------------------------------*/
[e42dd32]229void batch_control(usb_transfer_batch_t *instance,
230 usb_direction_t data_dir, usb_direction_t status_dir)
[7786cea]231{
232 assert(instance);
233 ohci_batch_t *data = instance->private_data;
234 assert(data);
235 ed_init(data->ed, instance->ep);
[e42dd32]236 ed_add_tds(data->ed, &data->tds[0], &data->tds[data->td_count - 1]);
[8790650]237 usb_log_debug("Created ED(%p): %x:%x:%x:%x.\n", data->ed,
238 data->ed->status, data->ed->td_tail, data->ed->td_head,
239 data->ed->next);
[e42dd32]240 int toggle = 0;
241 /* setup stage */
242 td_init(&data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
243 instance->setup_size, toggle);
244 td_set_next(&data->tds[0], &data->tds[1]);
245 usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0].status,
246 data->tds[0].cbp, data->tds[0].next, data->tds[0].be);
247
248 /* data stage */
249 size_t td_current = 1;
250 size_t remain_size = instance->buffer_size;
251 char *transfer_buffer = instance->transport_buffer;
252 while (remain_size > 0) {
253 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
254 OHCI_TD_MAX_TRANSFER : remain_size;
255 toggle = 1 - toggle;
256
257 td_init(&data->tds[td_current], data_dir, transfer_buffer,
258 transfer_size, toggle);
259 td_set_next(&data->tds[td_current], &data->tds[td_current + 1]);
260 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
261 data->tds[td_current].status, data->tds[td_current].cbp,
262 data->tds[td_current].next, data->tds[td_current].be);
263
264 transfer_buffer += transfer_size;
265 remain_size -= transfer_size;
266 assert(td_current < data->td_count - 2);
267 ++td_current;
268 }
269
270 /* status stage */
271 assert(td_current == data->td_count - 2);
272 td_init(&data->tds[td_current], status_dir, NULL, 0, 1);
273 usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
274 data->tds[td_current].status, data->tds[td_current].cbp,
275 data->tds[td_current].next, data->tds[td_current].be);
[f98b8269]276}
277/*----------------------------------------------------------------------------*/
[c6fe469]278void batch_data(usb_transfer_batch_t *instance)
279{
280 assert(instance);
281 ohci_batch_t *data = instance->private_data;
282 assert(data);
283 ed_init(data->ed, instance->ep);
284 ed_add_tds(data->ed, &data->tds[0], &data->tds[data->td_count - 1]);
285 usb_log_debug("Created ED(%p): %x:%x:%x:%x.\n", data->ed,
286 data->ed->status, data->ed->td_tail, data->ed->td_head,
287 data->ed->next);
288
289 /* data stage */
[aa9ccf7]290 size_t td_current = 0;
[c6fe469]291 size_t remain_size = instance->buffer_size;
292 char *transfer_buffer = instance->transport_buffer;
293 while (remain_size > 0) {
294 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
295 OHCI_TD_MAX_TRANSFER : remain_size;
296
297 td_init(&data->tds[td_current], instance->ep->direction,
298 transfer_buffer, transfer_size, -1);
299 td_set_next(&data->tds[td_current], &data->tds[td_current + 1]);
300 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
301 data->tds[td_current].status, data->tds[td_current].cbp,
302 data->tds[td_current].next, data->tds[td_current].be);
303
304 transfer_buffer += transfer_size;
305 remain_size -= transfer_size;
306 assert(td_current < data->td_count);
307 ++td_current;
308 }
309}
310/*----------------------------------------------------------------------------*/
[d328172]311/** Helper function calls callback and correctly disposes of batch structure.
312 *
313 * @param[in] instance Batch structure to use.
314 */
[1387692]315void batch_call_in_and_dispose(usb_transfer_batch_t *instance)
[d328172]316{
317 assert(instance);
[1387692]318 usb_transfer_batch_call_in(instance);
[d328172]319 batch_dispose(instance);
320}
321/*----------------------------------------------------------------------------*/
322/** Helper function calls callback and correctly disposes of batch structure.
323 *
324 * @param[in] instance Batch structure to use.
325 */
[1387692]326void batch_call_out_and_dispose(usb_transfer_batch_t *instance)
[d328172]327{
328 assert(instance);
[1387692]329 usb_transfer_batch_call_out(instance);
[d328172]330 batch_dispose(instance);
[be7950e8]331}
[41b96b4]332/**
333 * @}
334 */
Note: See TracBrowser for help on using the repository browser.