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

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

Fix bulk out transfers

  • Property mode set to 100644
File size: 11.8 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
46typedef struct ohci_transfer_batch {
47 ed_t *ed;
48 td_t **tds;
49 size_t td_count;
50 size_t leave_td;
51 char *device_buffer;
52} ohci_transfer_batch_t;
53
54static void ohci_transfer_batch_dispose(void *ohci_batch)
55{
56 ohci_transfer_batch_t *instance = ohci_batch;
57 if (!instance)
58 return;
59 free32(instance->device_buffer);
60 unsigned i = 0;
61 if (instance->tds) {
62 for (; i< instance->td_count; ++i) {
63 if (i != instance->leave_td)
64 free32(instance->tds[i]);
65 }
66 free(instance->tds);
67 }
68 free(instance);
69}
70/*----------------------------------------------------------------------------*/
71static void batch_control(usb_transfer_batch_t *instance,
72 usb_direction_t data_dir, usb_direction_t status_dir);
73static void batch_data(usb_transfer_batch_t *instance);
74/*----------------------------------------------------------------------------*/
75usb_transfer_batch_t * batch_get(ddf_fun_t *fun, endpoint_t *ep,
76 char *buffer, size_t buffer_size, char* setup_buffer, size_t setup_size,
77 usbhc_iface_transfer_in_callback_t func_in,
78 usbhc_iface_transfer_out_callback_t func_out, void *arg)
79{
80#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
81 if (ptr == NULL) { \
82 usb_log_error(message); \
83 if (instance) { \
84 usb_transfer_batch_dispose(instance); \
85 } \
86 return NULL; \
87 } else (void)0
88
89 usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
90 CHECK_NULL_DISPOSE_RETURN(instance,
91 "Failed to allocate batch instance.\n");
92 usb_transfer_batch_init(instance, ep, buffer, NULL, buffer_size,
93 NULL, setup_size, func_in, func_out, arg, fun, NULL,
94 ohci_transfer_batch_dispose);
95
96 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(ep);
97 assert(hcd_ep);
98
99 ohci_transfer_batch_t *data = calloc(sizeof(ohci_transfer_batch_t), 1);
100 CHECK_NULL_DISPOSE_RETURN(data, "Failed to allocate batch data.\n");
101 instance->private_data = data;
102
103 data->td_count =
104 ((buffer_size + OHCI_TD_MAX_TRANSFER - 1) / OHCI_TD_MAX_TRANSFER);
105 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
106 data->td_count += 2;
107 }
108
109 /* we need one extra place for td that is currently assigned to hcd_ep*/
110 data->tds = calloc(sizeof(td_t*), data->td_count + 1);
111 CHECK_NULL_DISPOSE_RETURN(data->tds,
112 "Failed to allocate transfer descriptors.\n");
113
114 data->tds[0] = hcd_ep->td;
115 data->leave_td = 0;
116 unsigned i = 1;
117 for (; i <= data->td_count; ++i) {
118 data->tds[i] = malloc32(sizeof(td_t));
119 CHECK_NULL_DISPOSE_RETURN(data->tds[i],
120 "Failed to allocate TD %d.\n", i );
121 }
122
123 data->ed = hcd_ep->ed;
124
125 if (setup_size + buffer_size > 0) {
126 data->device_buffer = malloc32(setup_size + buffer_size);
127 CHECK_NULL_DISPOSE_RETURN(data->device_buffer,
128 "Failed to allocate device accessible buffer.\n");
129 instance->setup_buffer = data->device_buffer;
130 instance->data_buffer = data->device_buffer + setup_size;
131 memcpy(instance->setup_buffer, setup_buffer, setup_size);
132 }
133
134 return instance;
135}
136/*----------------------------------------------------------------------------*/
137bool batch_is_complete(usb_transfer_batch_t *instance)
138{
139 assert(instance);
140 ohci_transfer_batch_t *data = instance->private_data;
141 assert(data);
142 size_t tds = data->td_count;
143 usb_log_debug("Batch(%p) checking %d td(s) for completion.\n",
144 instance, tds);
145 usb_log_debug("ED: %x:%x:%x:%x.\n",
146 data->ed->status, data->ed->td_head, data->ed->td_tail,
147 data->ed->next);
148 size_t i = 0;
149 instance->transfered_size = instance->buffer_size;
150 for (; i < tds; ++i) {
151 assert(data->tds[i] != NULL);
152 usb_log_debug("TD %d: %x:%x:%x:%x.\n", i,
153 data->tds[i]->status, data->tds[i]->cbp, data->tds[i]->next,
154 data->tds[i]->be);
155 if (!td_is_finished(data->tds[i])) {
156 return false;
157 }
158 instance->error = td_error(data->tds[i]);
159 if (instance->error != EOK) {
160 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
161 instance, i, data->tds[i]->status);
162 /* Make sure TD queue is empty (one TD),
163 * ED should be marked as halted */
164 data->ed->td_tail =
165 (data->ed->td_head & ED_TDTAIL_PTR_MASK);
166 ++i;
167 break;
168 }
169 }
170 data->leave_td = i;
171 assert(data->leave_td <= data->td_count);
172 hcd_endpoint_t *hcd_ep = hcd_endpoint_get(instance->ep);
173 assert(hcd_ep);
174 hcd_ep->td = data->tds[i];
175 if (i > 0)
176 instance->transfered_size -= td_remain_size(data->tds[i - 1]);
177
178 /* Clear possible ED HALT */
179 data->ed->td_head &= ~ED_TDHEAD_HALTED_FLAG;
180 uint32_t pa = addr_to_phys(hcd_ep->td);
181 assert(pa == (data->ed->td_head & ED_TDHEAD_PTR_MASK));
182 assert(pa == (data->ed->td_tail & ED_TDTAIL_PTR_MASK));
183
184 return true;
185}
186/*----------------------------------------------------------------------------*/
187void batch_commit(usb_transfer_batch_t *instance)
188{
189 assert(instance);
190 ohci_transfer_batch_t *data = instance->private_data;
191 assert(data);
192 ed_set_end_td(data->ed, data->tds[data->td_count]);
193}
194/*----------------------------------------------------------------------------*/
195void batch_control_write(usb_transfer_batch_t *instance)
196{
197 assert(instance);
198 /* We are data out, we are supposed to provide data */
199 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
200 instance->next_step = usb_transfer_batch_call_out_and_dispose;
201 batch_control(instance, USB_DIRECTION_OUT, USB_DIRECTION_IN);
202 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
203}
204/*----------------------------------------------------------------------------*/
205void batch_control_read(usb_transfer_batch_t *instance)
206{
207 assert(instance);
208 instance->next_step = usb_transfer_batch_call_in_and_dispose;
209 batch_control(instance, USB_DIRECTION_IN, USB_DIRECTION_OUT);
210 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
211}
212/*----------------------------------------------------------------------------*/
213void batch_interrupt_in(usb_transfer_batch_t *instance)
214{
215 assert(instance);
216 instance->next_step = usb_transfer_batch_call_in_and_dispose;
217 batch_data(instance);
218 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
219}
220/*----------------------------------------------------------------------------*/
221void batch_interrupt_out(usb_transfer_batch_t *instance)
222{
223 assert(instance);
224 /* We are data out, we are supposed to provide data */
225 memcpy(instance->data_buffer, instance->buffer, instance->buffer_size);
226 instance->next_step = usb_transfer_batch_call_out_and_dispose;
227 batch_data(instance);
228 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
229}
230/*----------------------------------------------------------------------------*/
231void batch_bulk_in(usb_transfer_batch_t *instance)
232{
233 assert(instance);
234 instance->next_step = usb_transfer_batch_call_in_and_dispose;
235 batch_data(instance);
236 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
237}
238/*----------------------------------------------------------------------------*/
239void batch_bulk_out(usb_transfer_batch_t *instance)
240{
241 assert(instance);
242 instance->next_step = usb_transfer_batch_call_out_and_dispose;
243 batch_data(instance);
244 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
245}
246/*----------------------------------------------------------------------------*/
247ed_t * batch_ed(usb_transfer_batch_t *instance)
248{
249 assert(instance);
250 ohci_transfer_batch_t *data = instance->private_data;
251 assert(data);
252 return data->ed;
253}
254/*----------------------------------------------------------------------------*/
255void batch_control(usb_transfer_batch_t *instance,
256 usb_direction_t data_dir, usb_direction_t status_dir)
257{
258 assert(instance);
259 ohci_transfer_batch_t *data = instance->private_data;
260 assert(data);
261 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
262 data->ed->status, data->ed->td_tail, data->ed->td_head,
263 data->ed->next);
264 int toggle = 0;
265 /* setup stage */
266 td_init(data->tds[0], USB_DIRECTION_BOTH, instance->setup_buffer,
267 instance->setup_size, toggle);
268 td_set_next(data->tds[0], data->tds[1]);
269 usb_log_debug("Created SETUP TD: %x:%x:%x:%x.\n", data->tds[0]->status,
270 data->tds[0]->cbp, data->tds[0]->next, data->tds[0]->be);
271
272 /* data stage */
273 size_t td_current = 1;
274 size_t remain_size = instance->buffer_size;
275 char *buffer = instance->data_buffer;
276 while (remain_size > 0) {
277 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
278 OHCI_TD_MAX_TRANSFER : remain_size;
279 toggle = 1 - toggle;
280
281 td_init(data->tds[td_current], data_dir, buffer,
282 transfer_size, toggle);
283 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
284 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
285 data->tds[td_current]->status, data->tds[td_current]->cbp,
286 data->tds[td_current]->next, data->tds[td_current]->be);
287
288 buffer += transfer_size;
289 remain_size -= transfer_size;
290 assert(td_current < data->td_count - 1);
291 ++td_current;
292 }
293
294 /* status stage */
295 assert(td_current == data->td_count - 1);
296 td_init(data->tds[td_current], status_dir, NULL, 0, 1);
297 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
298 usb_log_debug("Created STATUS TD: %x:%x:%x:%x.\n",
299 data->tds[td_current]->status, data->tds[td_current]->cbp,
300 data->tds[td_current]->next, data->tds[td_current]->be);
301}
302/*----------------------------------------------------------------------------*/
303void batch_data(usb_transfer_batch_t *instance)
304{
305 assert(instance);
306 ohci_transfer_batch_t *data = instance->private_data;
307 assert(data);
308 usb_log_debug("Using ED(%p): %x:%x:%x:%x.\n", data->ed,
309 data->ed->status, data->ed->td_tail, data->ed->td_head,
310 data->ed->next);
311
312 size_t td_current = 0;
313 size_t remain_size = instance->buffer_size;
314 char *buffer = instance->data_buffer;
315 while (remain_size > 0) {
316 size_t transfer_size = remain_size > OHCI_TD_MAX_TRANSFER ?
317 OHCI_TD_MAX_TRANSFER : remain_size;
318
319 td_init(data->tds[td_current], instance->ep->direction,
320 buffer, transfer_size, -1);
321 td_set_next(data->tds[td_current], data->tds[td_current + 1]);
322 usb_log_debug("Created DATA TD: %x:%x:%x:%x.\n",
323 data->tds[td_current]->status, data->tds[td_current]->cbp,
324 data->tds[td_current]->next, data->tds[td_current]->be);
325
326 buffer += transfer_size;
327 remain_size -= transfer_size;
328 assert(td_current < data->td_count);
329 ++td_current;
330 }
331}
332/**
333 * @}
334 */
Note: See TracBrowser for help on using the repository browser.