source: mainline/uspace/drv/ohci/batch.c@ 78d4e1f

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

Init ED queue registers AFTER device reset

Disable and enabled control queue when adding an ED
More debug output
OHCI lives and responds with STALL.

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