source: mainline/uspace/drv/uhci-hcd/batch.c@ 3de48b5

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

Switch back to polling

reduce debug verbosity

  • Property mode set to 100644
File size: 11.1 KB
RevLine 
[4192d3d6]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>
[86c2ccd]35#include <str_error.h>
[4192d3d6]36
37#include <usb/debug.h>
38
[83c439c]39#include "batch.h"
[53338bda]40#include "transfer_list.h"
[9a818a9]41#include "uhci.h"
42#include "utils/malloc32.h"
[4192d3d6]43
44#define DEFAULT_ERROR_COUNT 3
45
[83c439c]46static int batch_schedule(batch_t *instance);
[9a818a9]47
[83c439c]48static void batch_call_in(batch_t *instance);
49static void batch_call_out(batch_t *instance);
50static void batch_call_in_and_dispose(batch_t *instance);
51static void batch_call_out_and_dispose(batch_t *instance);
[4192d3d6]52
53
[eb1a2f4]54batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
[9a818a9]55 usb_transfer_type_t transfer_type, size_t max_packet_size,
[1a93bb0]56 usb_speed_t speed, char *buffer, size_t size,
[7dd3318]57 char* setup_buffer, size_t setup_size,
[4192d3d6]58 usbhc_iface_transfer_in_callback_t func_in,
59 usbhc_iface_transfer_out_callback_t func_out, void *arg)
60{
61 assert(func_in == NULL || func_out == NULL);
62 assert(func_in != NULL || func_out != NULL);
63
[83c439c]64 batch_t *instance = malloc(sizeof(batch_t));
[7dd3318]65 if (instance == NULL) {
[83c439c]66 usb_log_error("Failed to allocate batch instance.\n");
[7dd3318]67 return NULL;
68 }
69
70 instance->qh = queue_head_get();
71 if (instance->qh == NULL) {
72 usb_log_error("Failed to allocate queue head.\n");
73 free(instance);
74 return NULL;
75 }
76
77 instance->packets = (size + max_packet_size - 1) / max_packet_size;
78 if (transfer_type == USB_TRANSFER_CONTROL) {
79 instance->packets += 2;
80 }
81
82 instance->tds = malloc32(sizeof(transfer_descriptor_t) * instance->packets);
83 if (instance->tds == NULL) {
84 usb_log_error("Failed to allocate transfer descriptors.\n");
85 queue_head_dispose(instance->qh);
86 free(instance);
[9a818a9]87 return NULL;
88 }
[7dd3318]89 bzero(instance->tds, sizeof(transfer_descriptor_t) * instance->packets);
[9a818a9]90
[7dd3318]91 const size_t transport_size = max_packet_size * instance->packets;
92
93 instance->transport_buffer =
94 (size > 0) ? malloc32(transport_size) : NULL;
95 if ((size > 0) && (instance->transport_buffer == NULL)) {
96 usb_log_error("Failed to allocate device accessible buffer.\n");
97 queue_head_dispose(instance->qh);
98 free32(instance->tds);
[9a818a9]99 free(instance);
100 return NULL;
[4192d3d6]101 }
102
[7dd3318]103 instance->setup_buffer = setup_buffer ? malloc32(setup_size) : NULL;
104 if ((setup_size > 0) && (instance->setup_buffer == NULL)) {
105 usb_log_error("Failed to allocate device accessible setup buffer.\n");
106 queue_head_dispose(instance->qh);
107 free32(instance->tds);
108 free32(instance->transport_buffer);
[9a818a9]109 free(instance);
110 return NULL;
[4192d3d6]111 }
[7dd3318]112 if (instance->setup_buffer) {
113 memcpy(instance->setup_buffer, setup_buffer, setup_size);
114 }
115
[4192d3d6]116 instance->max_packet_size = max_packet_size;
117
118 link_initialize(&instance->link);
[7dd3318]119
[4192d3d6]120 instance->target = target;
[9a818a9]121 instance->transfer_type = transfer_type;
[4192d3d6]122
123 if (func_out)
124 instance->callback_out = func_out;
125 if (func_in)
126 instance->callback_in = func_in;
[7dd3318]127
[4192d3d6]128 instance->buffer = buffer;
129 instance->buffer_size = size;
[7dd3318]130 instance->setup_size = setup_size;
[eb1a2f4]131 instance->fun = fun;
[4192d3d6]132 instance->arg = arg;
[014d5033]133 instance->speed = speed;
[9a818a9]134
[7dd3318]135 queue_head_element_td(instance->qh, addr_to_phys(instance->tds));
[9a818a9]136 return instance;
[4192d3d6]137}
138/*----------------------------------------------------------------------------*/
[83c439c]139bool batch_is_complete(batch_t *instance)
[4192d3d6]140{
141 assert(instance);
[3de48b5]142 usb_log_debug2("Checking(%p) %d packet for completion.\n",
[7dd3318]143 instance, instance->packets);
[600733e]144 instance->transfered_size = 0;
[7dd3318]145 size_t i = 0;
146 for (;i < instance->packets; ++i) {
[600733e]147 if (transfer_descriptor_is_active(&instance->tds[i])) {
[7dd3318]148 return false;
[600733e]149 }
[7dd3318]150 instance->error = transfer_descriptor_status(&instance->tds[i]);
151 if (instance->error != EOK) {
[600733e]152 if (i > 0)
153 instance->transfered_size -= instance->setup_size;
[7dd3318]154 return true;
155 }
156 instance->transfered_size +=
157 transfer_descriptor_actual_size(&instance->tds[i]);
[4192d3d6]158 }
[600733e]159 instance->transfered_size -= instance->setup_size;
[7dd3318]160 return true;
[4192d3d6]161}
162/*----------------------------------------------------------------------------*/
[83c439c]163void batch_control_write(batch_t *instance)
[4192d3d6]164{
165 assert(instance);
166
[7dd3318]167 /* we are data out, we are supposed to provide data */
168 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
169
[c3ae877]170 const bool low_speed = instance->speed == USB_SPEED_LOW;
[7dd3318]171 int toggle = 0;
172 /* setup stage */
173 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
[c3ae877]174 instance->setup_size, toggle, false, low_speed,
175 instance->target, USB_PID_SETUP, instance->setup_buffer,
176 &instance->tds[1]);
[7dd3318]177
178 /* data stage */
179 size_t i = 1;
180 for (;i < instance->packets - 1; ++i) {
181 char *data =
182 instance->transport_buffer + ((i - 1) * instance->max_packet_size);
183 toggle = 1 - toggle;
184
185 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
[c3ae877]186 instance->max_packet_size, toggle++, false, low_speed,
187 instance->target, USB_PID_OUT, data, &instance->tds[i + 1]);
[4192d3d6]188 }
189
[7dd3318]190 /* status stage */
191 i = instance->packets - 1;
192 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
[c3ae877]193 0, 1, false, low_speed, instance->target, USB_PID_IN, NULL, NULL);
[9a818a9]194
[9013ad3]195 instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
[86c2ccd]196 usb_log_debug("Control write last TD status: %x.\n",
197 instance->tds[i].status);
[9013ad3]198
[83c439c]199 instance->next_step = batch_call_out_and_dispose;
200 batch_schedule(instance);
[4192d3d6]201}
202/*----------------------------------------------------------------------------*/
[83c439c]203void batch_control_read(batch_t *instance)
[4192d3d6]204{
205 assert(instance);
206
[c3ae877]207 const bool low_speed = instance->speed == USB_SPEED_LOW;
[7dd3318]208 int toggle = 0;
209 /* setup stage */
210 transfer_descriptor_init(instance->tds, DEFAULT_ERROR_COUNT,
[c3ae877]211 instance->setup_size, toggle, false, low_speed, instance->target,
[7dd3318]212 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
213
214 /* data stage */
215 size_t i = 1;
216 for (;i < instance->packets - 1; ++i) {
217 char *data =
218 instance->transport_buffer + ((i - 1) * instance->max_packet_size);
219 toggle = 1 - toggle;
220
221 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
[c3ae877]222 instance->max_packet_size, toggle, false, low_speed,
223 instance->target, USB_PID_IN, data, &instance->tds[i + 1]);
[4192d3d6]224 }
225
[7dd3318]226 /* status stage */
227 i = instance->packets - 1;
228 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
[c3ae877]229 0, 1, false, low_speed, instance->target, USB_PID_OUT, NULL, NULL);
[4192d3d6]230
[9013ad3]231 instance->tds[i].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
[86c2ccd]232 usb_log_debug("Control read last TD status: %x.\n",
233 instance->tds[i].status);
[9013ad3]234
[83c439c]235 instance->next_step = batch_call_in_and_dispose;
236 batch_schedule(instance);
[4192d3d6]237}
238/*----------------------------------------------------------------------------*/
[83c439c]239void batch_interrupt_in(batch_t *instance)
[4192d3d6]240{
[c8dd5b1]241 assert(instance);
242
[c3ae877]243 const bool low_speed = instance->speed == USB_SPEED_LOW;
[7dd3318]244 int toggle = 1;
245 size_t i = 0;
246 for (;i < instance->packets; ++i) {
247 char *data =
248 instance->transport_buffer + (i * instance->max_packet_size);
249 transfer_descriptor_t *next = (i + 1) < instance->packets ?
250 &instance->tds[i + 1] : NULL;
251 toggle = 1 - toggle;
252
253 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
[c3ae877]254 instance->max_packet_size, toggle, false, low_speed,
255 instance->target, USB_PID_IN, data, next);
[c8dd5b1]256 }
257
[30a4301]258 instance->tds[i - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
259
[83c439c]260 instance->next_step = batch_call_in_and_dispose;
261 batch_schedule(instance);
[4192d3d6]262}
263/*----------------------------------------------------------------------------*/
[83c439c]264void batch_interrupt_out(batch_t *instance)
[4192d3d6]265{
[c8dd5b1]266 assert(instance);
267
[7dd3318]268 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[c8dd5b1]269
[c3ae877]270 const bool low_speed = instance->speed == USB_SPEED_LOW;
[7dd3318]271 int toggle = 1;
272 size_t i = 0;
273 for (;i < instance->packets; ++i) {
274 char *data =
275 instance->transport_buffer + (i * instance->max_packet_size);
276 transfer_descriptor_t *next = (i + 1) < instance->packets ?
277 &instance->tds[i + 1] : NULL;
278 toggle = 1 - toggle;
[c8dd5b1]279
[7dd3318]280 transfer_descriptor_init(&instance->tds[i], DEFAULT_ERROR_COUNT,
[c3ae877]281 instance->max_packet_size, toggle++, false, low_speed,
282 instance->target, USB_PID_OUT, data, next);
[7dd3318]283 }
[c8dd5b1]284
[30a4301]285 instance->tds[i - 1].status |= TD_STATUS_COMPLETE_INTERRUPT_FLAG;
286
[83c439c]287 instance->next_step = batch_call_out_and_dispose;
288 batch_schedule(instance);
[4192d3d6]289}
290/*----------------------------------------------------------------------------*/
[83c439c]291void batch_call_in(batch_t *instance)
[4192d3d6]292{
293 assert(instance);
294 assert(instance->callback_in);
295
[7dd3318]296 memcpy(instance->buffer, instance->transport_buffer, instance->buffer_size);
297
298 int err = instance->error;
[86c2ccd]299 usb_log_info("Callback IN(%d): %s(%d), %zu.\n", instance->transfer_type,
300 str_error(err), err, instance->transfered_size);
[7dd3318]301
[eb1a2f4]302 instance->callback_in(instance->fun,
[7dd3318]303 err, instance->transfered_size,
[4192d3d6]304 instance->arg);
305}
306/*----------------------------------------------------------------------------*/
[83c439c]307void batch_call_out(batch_t *instance)
[4192d3d6]308{
309 assert(instance);
310 assert(instance->callback_out);
311
[7dd3318]312 int err = instance->error;
313 usb_log_info("Callback OUT(%d): %d.\n", instance->transfer_type, err);
[eb1a2f4]314 instance->callback_out(instance->fun,
[7dd3318]315 err, instance->arg);
[4192d3d6]316}
317/*----------------------------------------------------------------------------*/
[83c439c]318void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]319{
320 assert(instance);
[83c439c]321 batch_call_in(instance);
322 usb_log_debug("Disposing batch: %p.\n", instance);
[7dd3318]323 free32(instance->tds);
324 free32(instance->qh);
325 free32(instance->setup_buffer);
326 free32(instance->transport_buffer);
[4192d3d6]327 free(instance);
328}
329/*----------------------------------------------------------------------------*/
[83c439c]330void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]331{
332 assert(instance);
[83c439c]333 batch_call_out(instance);
334 usb_log_debug("Disposing batch: %p.\n", instance);
[7dd3318]335 free32(instance->tds);
336 free32(instance->qh);
337 free32(instance->setup_buffer);
338 free32(instance->transport_buffer);
[4192d3d6]339 free(instance);
340}
[9a818a9]341/*----------------------------------------------------------------------------*/
[83c439c]342int batch_schedule(batch_t *instance)
[9a818a9]343{
344 assert(instance);
[eb1a2f4]345 uhci_t *hc = fun_to_uhci(instance->fun);
[9a818a9]346 assert(hc);
347 return uhci_schedule(hc, instance);
348}
[4192d3d6]349/**
350 * @}
351 */
Note: See TracBrowser for help on using the repository browser.