source: mainline/uspace/drv/uhci-hcd/batch.c@ 13927cf

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

Yet another refactoring

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