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

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

Doxygen comments, use helper function for setting IOC flag

  • Property mode set to 100644
File size: 14.7 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
[a7e2f0d]59/** Allocates memory and initializes internal data structures.
60 *
61 * @param[in] fun DDF function to pass to callback.
62 * @param[in] target Device and endpoint target of the transaction.
63 * @param[in] transfer_type Interrupt, Control or Bulk.
64 * @param[in] max_packet_size maximum allowed size of data packets.
65 * @param[in] speed Speed of the transaction.
66 * @param[in] buffer Data source/destination.
67 * @param[in] size Size of the buffer.
68 * @param[in] setup_buffer Setup data source (if not NULL)
69 * @param[in] setup_size Size of setup_buffer (should be always 8)
70 * @param[in] func_in function to call on inbound transaction completion
71 * @param[in] func_out function to call on outbound transaction completion
72 * @param[in] arg additional parameter to func_in or func_out
73 * @param[in] manager Pointer to toggle management structure.
74 * @return False, if there is an active TD, true otherwise.
75 */
[eb1a2f4]76batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
[9a818a9]77 usb_transfer_type_t transfer_type, size_t max_packet_size,
[1a93bb0]78 usb_speed_t speed, char *buffer, size_t size,
[7dd3318]79 char* setup_buffer, size_t setup_size,
[4192d3d6]80 usbhc_iface_transfer_in_callback_t func_in,
[5620bd4]81 usbhc_iface_transfer_out_callback_t func_out, void *arg,
82 device_keeper_t *manager
83 )
[4192d3d6]84{
85 assert(func_in == NULL || func_out == NULL);
86 assert(func_in != NULL || func_out != NULL);
87
[2ab6875]88#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
89 if (ptr == NULL) { \
90 usb_log_error(message); \
91 if (instance) { \
92 batch_dispose(instance); \
93 } \
94 return NULL; \
95 } else (void)0
96
[83c439c]97 batch_t *instance = malloc(sizeof(batch_t));
[2ab6875]98 CHECK_NULL_DISPOSE_RETURN(instance,
99 "Failed to allocate batch instance.\n");
100 bzero(instance, sizeof(batch_t));
[7dd3318]101
[2ab6875]102 instance->qh = malloc32(sizeof(queue_head_t));
103 CHECK_NULL_DISPOSE_RETURN(instance->qh,
104 "Failed to allocate batch queue head.\n");
105 queue_head_init(instance->qh);
[7dd3318]106
107 instance->packets = (size + max_packet_size - 1) / max_packet_size;
108 if (transfer_type == USB_TRANSFER_CONTROL) {
109 instance->packets += 2;
110 }
111
[c5b93dc]112 instance->tds = malloc32(sizeof(td_t) * instance->packets);
[2ab6875]113 CHECK_NULL_DISPOSE_RETURN(
114 instance->tds, "Failed to allocate transfer descriptors.\n");
[c5b93dc]115 bzero(instance->tds, sizeof(td_t) * instance->packets);
[9a818a9]116
[bcaefe3]117// const size_t transport_size = max_packet_size * instance->packets;
[7dd3318]118
[2ab6875]119 if (size > 0) {
[bcaefe3]120 instance->transport_buffer = malloc32(size);
[2ab6875]121 CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
122 "Failed to allocate device accessible buffer.\n");
[4192d3d6]123 }
124
[2ab6875]125 if (setup_size > 0) {
126 instance->setup_buffer = malloc32(setup_size);
127 CHECK_NULL_DISPOSE_RETURN(instance->setup_buffer,
128 "Failed to allocate device accessible setup buffer.\n");
[7dd3318]129 memcpy(instance->setup_buffer, setup_buffer, setup_size);
130 }
131
[4192d3d6]132
133 link_initialize(&instance->link);
[7dd3318]134
[2ab6875]135 instance->max_packet_size = max_packet_size;
[4192d3d6]136 instance->target = target;
[9a818a9]137 instance->transfer_type = transfer_type;
[4192d3d6]138 instance->buffer = buffer;
139 instance->buffer_size = size;
[7dd3318]140 instance->setup_size = setup_size;
[eb1a2f4]141 instance->fun = fun;
[4192d3d6]142 instance->arg = arg;
[014d5033]143 instance->speed = speed;
[5620bd4]144 instance->manager = manager;
[9a818a9]145
[2ab6875]146 if (func_out)
147 instance->callback_out = func_out;
148 if (func_in)
149 instance->callback_in = func_in;
150
151 queue_head_set_element_td(instance->qh, addr_to_phys(instance->tds));
152
[4abc304]153 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
154 instance, target.address, target.endpoint);
[9a818a9]155 return instance;
[4192d3d6]156}
157/*----------------------------------------------------------------------------*/
[a7e2f0d]158/** Checks batch TDs for activity.
159 *
160 * @param[in] instance Batch structure to use.
161 * @return False, if there is an active TD, true otherwise.
162 */
[83c439c]163bool batch_is_complete(batch_t *instance)
[4192d3d6]164{
165 assert(instance);
[48563a3]166 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
[7dd3318]167 instance, instance->packets);
[600733e]168 instance->transfered_size = 0;
[7dd3318]169 size_t i = 0;
170 for (;i < instance->packets; ++i) {
[c5b93dc]171 if (td_is_active(&instance->tds[i])) {
[7dd3318]172 return false;
[600733e]173 }
[c5b93dc]174
175 instance->error = td_status(&instance->tds[i]);
[7dd3318]176 if (instance->error != EOK) {
[48563a3]177 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
[c5b93dc]178 instance, i, instance->tds[i].status);
[67352d2]179 td_print_status(&instance->tds[i]);
[a60150a]180
[bcaefe3]181 device_keeper_set_toggle(instance->manager,
182 instance->target, td_toggle(&instance->tds[i]));
[c5b93dc]183 if (i > 0)
184 goto substract_ret;
[7dd3318]185 return true;
186 }
[c5b93dc]187
188 instance->transfered_size += td_act_size(&instance->tds[i]);
189 if (td_is_short(&instance->tds[i]))
190 goto substract_ret;
[4192d3d6]191 }
[c5b93dc]192substract_ret:
[600733e]193 instance->transfered_size -= instance->setup_size;
[7dd3318]194 return true;
[4192d3d6]195}
196/*----------------------------------------------------------------------------*/
[a7e2f0d]197/** Prepares control write transaction.
198 *
199 * @param[in] instance Batch structure to use.
200 */
[83c439c]201void batch_control_write(batch_t *instance)
[4192d3d6]202{
203 assert(instance);
[7dd3318]204 /* we are data out, we are supposed to provide data */
[a7e2f0d]205 memcpy(instance->transport_buffer, instance->buffer,
206 instance->buffer_size);
[bdc8ab1]207 batch_control(instance, USB_PID_OUT, USB_PID_IN);
[83c439c]208 instance->next_step = batch_call_out_and_dispose;
[4abc304]209 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[83c439c]210 batch_schedule(instance);
[4192d3d6]211}
212/*----------------------------------------------------------------------------*/
[a7e2f0d]213/** Prepares control read transaction.
214 *
215 * @param[in] instance Batch structure to use.
216 */
[83c439c]217void batch_control_read(batch_t *instance)
[4192d3d6]218{
219 assert(instance);
[bdc8ab1]220 batch_control(instance, USB_PID_IN, USB_PID_OUT);
[83c439c]221 instance->next_step = batch_call_in_and_dispose;
[4abc304]222 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[83c439c]223 batch_schedule(instance);
[4192d3d6]224}
225/*----------------------------------------------------------------------------*/
[a7e2f0d]226/** Prepares interrupt in transaction.
227 *
228 * @param[in] instance Batch structure to use.
229 */
[5620bd4]230void batch_interrupt_in(batch_t *instance)
[4192d3d6]231{
[c8dd5b1]232 assert(instance);
[5620bd4]233 batch_data(instance, USB_PID_IN);
[83c439c]234 instance->next_step = batch_call_in_and_dispose;
[4abc304]235 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[83c439c]236 batch_schedule(instance);
[4192d3d6]237}
238/*----------------------------------------------------------------------------*/
[a7e2f0d]239/** Prepares interrupt out transaction.
240 *
241 * @param[in] instance Batch structure to use.
242 */
[5620bd4]243void batch_interrupt_out(batch_t *instance)
[4192d3d6]244{
[c8dd5b1]245 assert(instance);
[bcaefe3]246 /* we are data out, we are supposed to provide data */
[7dd3318]247 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[5620bd4]248 batch_data(instance, USB_PID_OUT);
[0e06a14]249 instance->next_step = batch_call_out_and_dispose;
250 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
251 batch_schedule(instance);
252}
253/*----------------------------------------------------------------------------*/
[a7e2f0d]254/** Prepares bulk in transaction.
255 *
256 * @param[in] instance Batch structure to use.
257 */
[5620bd4]258void batch_bulk_in(batch_t *instance)
[0e06a14]259{
260 assert(instance);
[5620bd4]261 batch_data(instance, USB_PID_IN);
[0e06a14]262 instance->next_step = batch_call_in_and_dispose;
263 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
264 batch_schedule(instance);
265}
266/*----------------------------------------------------------------------------*/
[a7e2f0d]267/** Prepares bulk out transaction.
268 *
269 * @param[in] instance Batch structure to use.
270 */
[5620bd4]271void batch_bulk_out(batch_t *instance)
[0e06a14]272{
273 assert(instance);
274 memcpy(instance->transport_buffer, instance->buffer, instance->buffer_size);
[5620bd4]275 batch_data(instance, USB_PID_OUT);
[0e06a14]276 instance->next_step = batch_call_out_and_dispose;
277 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
278 batch_schedule(instance);
279}
280/*----------------------------------------------------------------------------*/
[a7e2f0d]281/** Prepares generic data transaction
282 *
283 * @param[in] instance Batch structure to use.
284 * @param[in] pid to use for data packets.
285 */
[5620bd4]286void batch_data(batch_t *instance, usb_packet_id pid)
[0e06a14]287{
288 assert(instance);
[c3ae877]289 const bool low_speed = instance->speed == USB_SPEED_LOW;
[bcaefe3]290 int toggle =
291 device_keeper_get_toggle(instance->manager, instance->target);
[3e37964]292 assert(toggle == 0 || toggle == 1);
[0e06a14]293
294 size_t packet = 0;
295 size_t remain_size = instance->buffer_size;
296 while (remain_size > 0) {
[7dd3318]297 char *data =
[0e06a14]298 instance->transport_buffer + instance->buffer_size
299 - remain_size;
300
301 const size_t packet_size =
302 (instance->max_packet_size > remain_size) ?
303 remain_size : instance->max_packet_size;
[c8dd5b1]304
[bcaefe3]305 td_t *next_packet = (packet + 1 < instance->packets)
306 ? &instance->tds[packet + 1] : NULL;
[30a4301]307
[bcaefe3]308 assert(packet < instance->packets);
[0e06a14]309 assert(packet_size <= remain_size);
[bcaefe3]310
311 td_init(
312 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
313 toggle, false, low_speed, instance->target, pid, data,
314 next_packet);
315
316
317 toggle = 1 - toggle;
[0e06a14]318 remain_size -= packet_size;
[bcaefe3]319 ++packet;
[0e06a14]320 }
[eb0dc58]321 td_set_ioc(&instance->tds[packet - 1]);
[5620bd4]322 device_keeper_set_toggle(instance->manager, instance->target, toggle);
[4192d3d6]323}
324/*----------------------------------------------------------------------------*/
[a7e2f0d]325/** Prepares generic control transaction
326 *
327 * @param[in] instance Batch structure to use.
328 * @param[in] data_stage to use for data packets.
329 * @param[in] status_stage to use for data packets.
330 */
[3e37964]331void batch_control(batch_t *instance,
[eae83aa]332 usb_packet_id data_stage, usb_packet_id status_stage)
[bdc8ab1]333{
334 assert(instance);
335
336 const bool low_speed = instance->speed == USB_SPEED_LOW;
337 int toggle = 0;
338 /* setup stage */
[c5b93dc]339 td_init(instance->tds, DEFAULT_ERROR_COUNT,
[bdc8ab1]340 instance->setup_size, toggle, false, low_speed, instance->target,
341 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
342
343 /* data stage */
344 size_t packet = 1;
345 size_t remain_size = instance->buffer_size;
346 while (remain_size > 0) {
347 char *data =
348 instance->transport_buffer + instance->buffer_size
349 - remain_size;
350
351 toggle = 1 - toggle;
352
353 const size_t packet_size =
354 (instance->max_packet_size > remain_size) ?
355 remain_size : instance->max_packet_size;
356
[bcaefe3]357 td_init(
358 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
359 toggle, false, low_speed, instance->target, data_stage,
360 data, &instance->tds[packet + 1]);
[bdc8ab1]361
362 ++packet;
363 assert(packet < instance->packets);
364 assert(packet_size <= remain_size);
365 remain_size -= packet_size;
366 }
367
368 /* status stage */
369 assert(packet == instance->packets - 1);
[c5b93dc]370 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
[bdc8ab1]371 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
372
[eb0dc58]373 td_set_ioc(&instance->tds[packet]);
[bdc8ab1]374 usb_log_debug2("Control last TD status: %x.\n",
375 instance->tds[packet].status);
376}
377/*----------------------------------------------------------------------------*/
[a7e2f0d]378/** Prepares data, gets error status and calls callback in.
379 *
380 * @param[in] instance Batch structure to use.
381 */
[83c439c]382void batch_call_in(batch_t *instance)
[4192d3d6]383{
384 assert(instance);
385 assert(instance->callback_in);
386
[bcaefe3]387 /* we are data in, we need data */
[a7e2f0d]388 memcpy(instance->buffer, instance->transport_buffer,
389 instance->buffer_size);
[7dd3318]390
391 int err = instance->error;
[4abc304]392 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
[48563a3]393 instance, instance->transfer_type, str_error(err), err,
394 instance->transfered_size);
[7dd3318]395
[bcaefe3]396 instance->callback_in(
397 instance->fun, err, instance->transfered_size, instance->arg);
[4192d3d6]398}
399/*----------------------------------------------------------------------------*/
[a7e2f0d]400/** Gets error status and calls callback out.
401 *
402 * @param[in] instance Batch structure to use.
403 */
[83c439c]404void batch_call_out(batch_t *instance)
[4192d3d6]405{
406 assert(instance);
407 assert(instance->callback_out);
408
[7dd3318]409 int err = instance->error;
[4abc304]410 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
[48563a3]411 instance, instance->transfer_type, str_error(err), err);
[eb1a2f4]412 instance->callback_out(instance->fun,
[7dd3318]413 err, instance->arg);
[4192d3d6]414}
415/*----------------------------------------------------------------------------*/
[a7e2f0d]416/** Prepares data, gets error status, calls callback in and dispose.
417 *
418 * @param[in] instance Batch structure to use.
419 */
[83c439c]420void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]421{
422 assert(instance);
[83c439c]423 batch_call_in(instance);
[a60150a]424 batch_dispose(instance);
[4192d3d6]425}
426/*----------------------------------------------------------------------------*/
[a7e2f0d]427/** Gets error status, calls callback out and dispose.
428 *
429 * @param[in] instance Batch structure to use.
430 */
[83c439c]431void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]432{
433 assert(instance);
[83c439c]434 batch_call_out(instance);
[a60150a]435 batch_dispose(instance);
436}
437/*----------------------------------------------------------------------------*/
[a7e2f0d]438/** Correctly disposes all used data structures.
439 *
440 * @param[in] instance Batch structure to use.
441 */
[a60150a]442void batch_dispose(batch_t *instance)
443{
444 assert(instance);
[48563a3]445 usb_log_debug("Batch(%p) disposing.\n", instance);
[bcaefe3]446 /* free32 is NULL safe */
[7dd3318]447 free32(instance->tds);
448 free32(instance->qh);
449 free32(instance->setup_buffer);
450 free32(instance->transport_buffer);
[4192d3d6]451 free(instance);
452}
[9a818a9]453/*----------------------------------------------------------------------------*/
[83c439c]454int batch_schedule(batch_t *instance)
[9a818a9]455{
456 assert(instance);
[eb1a2f4]457 uhci_t *hc = fun_to_uhci(instance->fun);
[9a818a9]458 assert(hc);
459 return uhci_schedule(hc, instance);
460}
[4192d3d6]461/**
462 * @}
463 */
Note: See TracBrowser for help on using the repository browser.