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

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

Remove EP information stored in usb_transfer_batch_t

rename usb_transfer_batch_t.transport_buffer ⇒ data_buffer

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