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 usb
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief UHCI driver
|
---|
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 "transfer_list.h"
|
---|
42 | #include "uhci.h"
|
---|
43 | #include "utils/malloc32.h"
|
---|
44 |
|
---|
45 | #define DEFAULT_ERROR_COUNT 3
|
---|
46 |
|
---|
47 | static int batch_schedule(batch_t *instance);
|
---|
48 |
|
---|
49 | static void batch_control(batch_t *instance,
|
---|
50 | usb_packet_id data_stage, usb_packet_id status_stage);
|
---|
51 | static void batch_data(batch_t *instance, usb_packet_id pid);
|
---|
52 | static void batch_call_in(batch_t *instance);
|
---|
53 | static void batch_call_out(batch_t *instance);
|
---|
54 | static void batch_call_in_and_dispose(batch_t *instance);
|
---|
55 | static void batch_call_out_and_dispose(batch_t *instance);
|
---|
56 |
|
---|
57 |
|
---|
58 | batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
|
---|
59 | usb_transfer_type_t transfer_type, size_t max_packet_size,
|
---|
60 | usb_speed_t speed, char *buffer, size_t size,
|
---|
61 | char* setup_buffer, size_t setup_size,
|
---|
62 | usbhc_iface_transfer_in_callback_t func_in,
|
---|
63 | usbhc_iface_transfer_out_callback_t func_out, void *arg,
|
---|
64 | device_keeper_t *manager
|
---|
65 | )
|
---|
66 | {
|
---|
67 | assert(func_in == NULL || func_out == NULL);
|
---|
68 | assert(func_in != NULL || func_out != NULL);
|
---|
69 |
|
---|
70 | batch_t *instance = malloc(sizeof(batch_t));
|
---|
71 | if (instance == NULL) {
|
---|
72 | usb_log_error("Failed to allocate batch instance.\n");
|
---|
73 | return NULL;
|
---|
74 | }
|
---|
75 |
|
---|
76 | instance->qh = queue_head_get();
|
---|
77 | if (instance->qh == NULL) {
|
---|
78 | usb_log_error("Failed to allocate queue head.\n");
|
---|
79 | free(instance);
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | instance->packets = (size + max_packet_size - 1) / max_packet_size;
|
---|
84 | if (transfer_type == USB_TRANSFER_CONTROL) {
|
---|
85 | instance->packets += 2;
|
---|
86 | }
|
---|
87 |
|
---|
88 | instance->tds = malloc32(sizeof(td_t) * instance->packets);
|
---|
89 | if (instance->tds == NULL) {
|
---|
90 | usb_log_error("Failed to allocate transfer descriptors.\n");
|
---|
91 | queue_head_dispose(instance->qh);
|
---|
92 | free(instance);
|
---|
93 | return NULL;
|
---|
94 | }
|
---|
95 | bzero(instance->tds, sizeof(td_t) * instance->packets);
|
---|
96 |
|
---|
97 | const size_t transport_size = max_packet_size * instance->packets;
|
---|
98 |
|
---|
99 | instance->transport_buffer =
|
---|
100 | (size > 0) ? malloc32(transport_size) : NULL;
|
---|
101 |
|
---|
102 | if ((size > 0) && (instance->transport_buffer == NULL)) {
|
---|
103 | usb_log_error("Failed to allocate device accessible buffer.\n");
|
---|
104 | queue_head_dispose(instance->qh);
|
---|
105 | free32(instance->tds);
|
---|
106 | free(instance);
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 | instance->setup_buffer = setup_buffer ? malloc32(setup_size) : NULL;
|
---|
111 | if ((setup_size > 0) && (instance->setup_buffer == NULL)) {
|
---|
112 | usb_log_error("Failed to allocate device accessible setup buffer.\n");
|
---|
113 | queue_head_dispose(instance->qh);
|
---|
114 | free32(instance->tds);
|
---|
115 | free32(instance->transport_buffer);
|
---|
116 | free(instance);
|
---|
117 | return NULL;
|
---|
118 | }
|
---|
119 | if (instance->setup_buffer) {
|
---|
120 | memcpy(instance->setup_buffer, setup_buffer, setup_size);
|
---|
121 | }
|
---|
122 |
|
---|
123 | instance->max_packet_size = max_packet_size;
|
---|
124 |
|
---|
125 | link_initialize(&instance->link);
|
---|
126 |
|
---|
127 | instance->target = target;
|
---|
128 | instance->transfer_type = transfer_type;
|
---|
129 |
|
---|
130 | if (func_out)
|
---|
131 | instance->callback_out = func_out;
|
---|
132 | if (func_in)
|
---|
133 | instance->callback_in = func_in;
|
---|
134 |
|
---|
135 | instance->buffer = buffer;
|
---|
136 | instance->buffer_size = size;
|
---|
137 | instance->setup_size = setup_size;
|
---|
138 | instance->fun = fun;
|
---|
139 | instance->arg = arg;
|
---|
140 | instance->speed = speed;
|
---|
141 | instance->manager = manager;
|
---|
142 |
|
---|
143 | queue_head_element_td(instance->qh, addr_to_phys(instance->tds));
|
---|
144 | usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
|
---|
145 | instance, target.address, target.endpoint);
|
---|
146 | return instance;
|
---|
147 | }
|
---|
148 | /*----------------------------------------------------------------------------*/
|
---|
149 | bool batch_is_complete(batch_t *instance)
|
---|
150 | {
|
---|
151 | assert(instance);
|
---|
152 | usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
|
---|
153 | instance, instance->packets);
|
---|
154 | instance->transfered_size = 0;
|
---|
155 | size_t i = 0;
|
---|
156 | for (;i < instance->packets; ++i) {
|
---|
157 | if (td_is_active(&instance->tds[i])) {
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 | instance->error = td_status(&instance->tds[i]);
|
---|
162 | if (instance->error != EOK) {
|
---|
163 | usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
|
---|
164 | instance, i, instance->tds[i].status);
|
---|
165 | if (i > 0)
|
---|
166 | goto substract_ret;
|
---|
167 | return true;
|
---|
168 | }
|
---|
169 |
|
---|
170 | instance->transfered_size += td_act_size(&instance->tds[i]);
|
---|
171 | if (td_is_short(&instance->tds[i]))
|
---|
172 | goto substract_ret;
|
---|
173 | }
|
---|
174 | substract_ret:
|
---|
175 | instance->transfered_size -= instance->setup_size;
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 | /*----------------------------------------------------------------------------*/
|
---|
179 | void batch_control_write(batch_t *instance)
|
---|
180 | {
|
---|
181 | assert(instance);
|
---|
182 | /* we are data out, we are supposed to provide data */
|
---|
183 | memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
|
---|
184 | batch_control(instance, USB_PID_OUT, USB_PID_IN);
|
---|
185 | instance->next_step = batch_call_out_and_dispose;
|
---|
186 | usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
|
---|
187 | batch_schedule(instance);
|
---|
188 | }
|
---|
189 | /*----------------------------------------------------------------------------*/
|
---|
190 | void batch_control_read(batch_t *instance)
|
---|
191 | {
|
---|
192 | assert(instance);
|
---|
193 | batch_control(instance, USB_PID_IN, USB_PID_OUT);
|
---|
194 | instance->next_step = batch_call_in_and_dispose;
|
---|
195 | usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
|
---|
196 | batch_schedule(instance);
|
---|
197 | }
|
---|
198 | /*----------------------------------------------------------------------------*/
|
---|
199 | void batch_interrupt_in(batch_t *instance)
|
---|
200 | {
|
---|
201 | assert(instance);
|
---|
202 | batch_data(instance, USB_PID_IN);
|
---|
203 | instance->next_step = batch_call_in_and_dispose;
|
---|
204 | usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
|
---|
205 | batch_schedule(instance);
|
---|
206 | }
|
---|
207 | /*----------------------------------------------------------------------------*/
|
---|
208 | void batch_interrupt_out(batch_t *instance)
|
---|
209 | {
|
---|
210 | assert(instance);
|
---|
211 | memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
|
---|
212 | batch_data(instance, USB_PID_OUT);
|
---|
213 | instance->next_step = batch_call_out_and_dispose;
|
---|
214 | usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
|
---|
215 | batch_schedule(instance);
|
---|
216 | }
|
---|
217 | /*----------------------------------------------------------------------------*/
|
---|
218 | void batch_bulk_in(batch_t *instance)
|
---|
219 | {
|
---|
220 | assert(instance);
|
---|
221 | batch_data(instance, USB_PID_IN);
|
---|
222 | instance->next_step = batch_call_in_and_dispose;
|
---|
223 | usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
|
---|
224 | batch_schedule(instance);
|
---|
225 | }
|
---|
226 | /*----------------------------------------------------------------------------*/
|
---|
227 | void batch_bulk_out(batch_t *instance)
|
---|
228 | {
|
---|
229 | assert(instance);
|
---|
230 | memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
|
---|
231 | batch_data(instance, USB_PID_OUT);
|
---|
232 | instance->next_step = batch_call_out_and_dispose;
|
---|
233 | usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
|
---|
234 | batch_schedule(instance);
|
---|
235 | }
|
---|
236 | /*----------------------------------------------------------------------------*/
|
---|
237 | void batch_data(batch_t *instance, usb_packet_id pid)
|
---|
238 | {
|
---|
239 | assert(instance);
|
---|
240 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
---|
241 | int toggle = device_keeper_get_toggle(instance->manager, instance->target);
|
---|
242 | assert(toggle == 0 || toggle == 1);
|
---|
243 |
|
---|
244 | size_t packet = 0;
|
---|
245 | size_t remain_size = instance->buffer_size;
|
---|
246 | while (remain_size > 0) {
|
---|
247 | char *data =
|
---|
248 | instance->transport_buffer + instance->buffer_size
|
---|
249 | - remain_size;
|
---|
250 |
|
---|
251 |
|
---|
252 | const size_t packet_size =
|
---|
253 | (instance->max_packet_size > remain_size) ?
|
---|
254 | remain_size : instance->max_packet_size;
|
---|
255 |
|
---|
256 | td_init(&instance->tds[packet],
|
---|
257 | DEFAULT_ERROR_COUNT, packet_size, toggle, false, low_speed,
|
---|
258 | instance->target, pid, data,
|
---|
259 | &instance->tds[packet + 1]);
|
---|
260 |
|
---|
261 | toggle = 1 - toggle;
|
---|
262 | ++packet;
|
---|
263 | assert(packet <= instance->packets);
|
---|
264 | assert(packet_size <= remain_size);
|
---|
265 | remain_size -= packet_size;
|
---|
266 | }
|
---|
267 | device_keeper_set_toggle(instance->manager, instance->target, toggle);
|
---|
268 |
|
---|
269 | instance->tds[packet - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
|
---|
270 | instance->tds[packet - 1].next = 0 | LINK_POINTER_TERMINATE_FLAG;
|
---|
271 | }
|
---|
272 | /*----------------------------------------------------------------------------*/
|
---|
273 | void batch_control(batch_t *instance,
|
---|
274 | usb_packet_id data_stage, usb_packet_id status_stage)
|
---|
275 | {
|
---|
276 | assert(instance);
|
---|
277 |
|
---|
278 | const bool low_speed = instance->speed == USB_SPEED_LOW;
|
---|
279 | int toggle = 0;
|
---|
280 | /* setup stage */
|
---|
281 | td_init(instance->tds, DEFAULT_ERROR_COUNT,
|
---|
282 | instance->setup_size, toggle, false, low_speed, instance->target,
|
---|
283 | USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
|
---|
284 |
|
---|
285 | /* data stage */
|
---|
286 | size_t packet = 1;
|
---|
287 | size_t remain_size = instance->buffer_size;
|
---|
288 | while (remain_size > 0) {
|
---|
289 | char *data =
|
---|
290 | instance->transport_buffer + instance->buffer_size
|
---|
291 | - remain_size;
|
---|
292 |
|
---|
293 | toggle = 1 - toggle;
|
---|
294 |
|
---|
295 | const size_t packet_size =
|
---|
296 | (instance->max_packet_size > remain_size) ?
|
---|
297 | remain_size : instance->max_packet_size;
|
---|
298 |
|
---|
299 | td_init(&instance->tds[packet],
|
---|
300 | DEFAULT_ERROR_COUNT, packet_size, toggle, false, low_speed,
|
---|
301 | instance->target, data_stage, data,
|
---|
302 | &instance->tds[packet + 1]);
|
---|
303 |
|
---|
304 | ++packet;
|
---|
305 | assert(packet < instance->packets);
|
---|
306 | assert(packet_size <= remain_size);
|
---|
307 | remain_size -= packet_size;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /* status stage */
|
---|
311 | assert(packet == instance->packets - 1);
|
---|
312 | td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
|
---|
313 | 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
|
---|
314 |
|
---|
315 |
|
---|
316 | instance->tds[packet].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
|
---|
317 | usb_log_debug2("Control last TD status: %x.\n",
|
---|
318 | instance->tds[packet].status);
|
---|
319 | }
|
---|
320 | /*----------------------------------------------------------------------------*/
|
---|
321 | void batch_call_in(batch_t *instance)
|
---|
322 | {
|
---|
323 | assert(instance);
|
---|
324 | assert(instance->callback_in);
|
---|
325 |
|
---|
326 | memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
|
---|
327 |
|
---|
328 | int err = instance->error;
|
---|
329 | usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
|
---|
330 | instance, instance->transfer_type, str_error(err), err,
|
---|
331 | instance->transfered_size);
|
---|
332 |
|
---|
333 | instance->callback_in(instance->fun,
|
---|
334 | err, instance->transfered_size,
|
---|
335 | instance->arg);
|
---|
336 | }
|
---|
337 | /*----------------------------------------------------------------------------*/
|
---|
338 | void batch_call_out(batch_t *instance)
|
---|
339 | {
|
---|
340 | assert(instance);
|
---|
341 | assert(instance->callback_out);
|
---|
342 |
|
---|
343 | int err = instance->error;
|
---|
344 | usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
|
---|
345 | instance, instance->transfer_type, str_error(err), err);
|
---|
346 | instance->callback_out(instance->fun,
|
---|
347 | err, instance->arg);
|
---|
348 | }
|
---|
349 | /*----------------------------------------------------------------------------*/
|
---|
350 | void batch_call_in_and_dispose(batch_t *instance)
|
---|
351 | {
|
---|
352 | assert(instance);
|
---|
353 | batch_call_in(instance);
|
---|
354 | usb_log_debug("Batch(%p) disposing.\n", instance);
|
---|
355 | free32(instance->tds);
|
---|
356 | free32(instance->qh);
|
---|
357 | free32(instance->setup_buffer);
|
---|
358 | free32(instance->transport_buffer);
|
---|
359 | free(instance);
|
---|
360 | }
|
---|
361 | /*----------------------------------------------------------------------------*/
|
---|
362 | void batch_call_out_and_dispose(batch_t *instance)
|
---|
363 | {
|
---|
364 | assert(instance);
|
---|
365 | batch_call_out(instance);
|
---|
366 | usb_log_debug("Batch(%p) disposing.\n", instance);
|
---|
367 | free32(instance->tds);
|
---|
368 | free32(instance->qh);
|
---|
369 | free32(instance->setup_buffer);
|
---|
370 | free32(instance->transport_buffer);
|
---|
371 | free(instance);
|
---|
372 | }
|
---|
373 | /*----------------------------------------------------------------------------*/
|
---|
374 | int batch_schedule(batch_t *instance)
|
---|
375 | {
|
---|
376 | assert(instance);
|
---|
377 | uhci_t *hc = fun_to_uhci(instance->fun);
|
---|
378 | assert(hc);
|
---|
379 | return uhci_schedule(hc, instance);
|
---|
380 | }
|
---|
381 | /**
|
---|
382 | * @}
|
---|
383 | */
|
---|