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

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

Hw error handling.

  • Property mode set to 100644
File size: 15.6 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 */
[17ceb72]28/** @addtogroup drvusbuhcihc
[4192d3d6]29 * @{
30 */
31/** @file
[17ceb72]32 * @brief UHCI driver USB transaction structure
[4192d3d6]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"
[8850690]42#include "uhci_hc.h"
[9a818a9]43#include "utils/malloc32.h"
[4192d3d6]44
45#define DEFAULT_ERROR_COUNT 3
46
[eae83aa]47static void batch_control(batch_t *instance,
48 usb_packet_id data_stage, usb_packet_id status_stage);
[5620bd4]49static void batch_data(batch_t *instance, usb_packet_id pid);
[83c439c]50static void batch_call_in(batch_t *instance);
51static void batch_call_out(batch_t *instance);
52static void batch_call_in_and_dispose(batch_t *instance);
53static void batch_call_out_and_dispose(batch_t *instance);
[4192d3d6]54
55
[17ceb72]56/** Allocate memory and initialize internal data structure.
[a7e2f0d]57 *
58 * @param[in] fun DDF function to pass to callback.
59 * @param[in] target Device and endpoint target of the transaction.
60 * @param[in] transfer_type Interrupt, Control or Bulk.
61 * @param[in] max_packet_size maximum allowed size of data packets.
62 * @param[in] speed Speed of the transaction.
63 * @param[in] buffer Data source/destination.
64 * @param[in] size Size of the buffer.
65 * @param[in] setup_buffer Setup data source (if not NULL)
66 * @param[in] setup_size Size of setup_buffer (should be always 8)
67 * @param[in] func_in function to call on inbound transaction completion
68 * @param[in] func_out function to call on outbound transaction completion
69 * @param[in] arg additional parameter to func_in or func_out
70 * @param[in] manager Pointer to toggle management structure.
[17ceb72]71 * @return Valid pointer if all substructures were successfully created,
72 * NULL otherwise.
73 *
74 * Determines the number of needed packets (TDs). Prepares a transport buffer
75 * (that is accessible by the hardware). Initializes parameters needed for the
76 * transaction and callback.
[a7e2f0d]77 */
[eb1a2f4]78batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
[9a818a9]79 usb_transfer_type_t transfer_type, size_t max_packet_size,
[1a93bb0]80 usb_speed_t speed, char *buffer, size_t size,
[7dd3318]81 char* setup_buffer, size_t setup_size,
[4192d3d6]82 usbhc_iface_transfer_in_callback_t func_in,
[5620bd4]83 usbhc_iface_transfer_out_callback_t func_out, void *arg,
84 device_keeper_t *manager
85 )
[4192d3d6]86{
87 assert(func_in == NULL || func_out == NULL);
88 assert(func_in != NULL || func_out != NULL);
89
[2ab6875]90#define CHECK_NULL_DISPOSE_RETURN(ptr, message...) \
91 if (ptr == NULL) { \
92 usb_log_error(message); \
93 if (instance) { \
94 batch_dispose(instance); \
95 } \
96 return NULL; \
97 } else (void)0
98
[83c439c]99 batch_t *instance = malloc(sizeof(batch_t));
[2ab6875]100 CHECK_NULL_DISPOSE_RETURN(instance,
101 "Failed to allocate batch instance.\n");
102 bzero(instance, sizeof(batch_t));
[7dd3318]103
[6143ce3]104 instance->qh = malloc32(sizeof(qh_t));
[2ab6875]105 CHECK_NULL_DISPOSE_RETURN(instance->qh,
106 "Failed to allocate batch queue head.\n");
[6143ce3]107 qh_init(instance->qh);
[7dd3318]108
109 instance->packets = (size + max_packet_size - 1) / max_packet_size;
110 if (transfer_type == USB_TRANSFER_CONTROL) {
111 instance->packets += 2;
112 }
113
[c5b93dc]114 instance->tds = malloc32(sizeof(td_t) * instance->packets);
[2ab6875]115 CHECK_NULL_DISPOSE_RETURN(
116 instance->tds, "Failed to allocate transfer descriptors.\n");
[c5b93dc]117 bzero(instance->tds, sizeof(td_t) * instance->packets);
[9a818a9]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;
[6143ce3]145 instance->callback_out = func_out;
146 instance->callback_in = func_in;
[9a818a9]147
[6143ce3]148 qh_set_element_td(instance->qh, addr_to_phys(instance->tds));
[2ab6875]149
[4abc304]150 usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
151 instance, target.address, target.endpoint);
[9a818a9]152 return instance;
[4192d3d6]153}
154/*----------------------------------------------------------------------------*/
[a963a68]155/** Mark batch as failed and continue with next step.
156 *
157 * @param[in] instance Batch structure to use.
158 *
159 */
160void batch_abort(batch_t *instance)
161{
162 assert(instance);
163 instance->error = EIO;
164 instance->next_step(instance);
165}
166/*----------------------------------------------------------------------------*/
[17ceb72]167/** Check batch TDs for activity.
[a7e2f0d]168 *
169 * @param[in] instance Batch structure to use.
170 * @return False, if there is an active TD, true otherwise.
[17ceb72]171 *
172 * Walk all TDs. Stop with false if there is an active one (it is to be
173 * processed). Stop with true if an error is found. Return true if the last TS
174 * is reached.
[a7e2f0d]175 */
[83c439c]176bool batch_is_complete(batch_t *instance)
[4192d3d6]177{
178 assert(instance);
[48563a3]179 usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
[7dd3318]180 instance, instance->packets);
[600733e]181 instance->transfered_size = 0;
[7dd3318]182 size_t i = 0;
183 for (;i < instance->packets; ++i) {
[c5b93dc]184 if (td_is_active(&instance->tds[i])) {
[7dd3318]185 return false;
[600733e]186 }
[c5b93dc]187
188 instance->error = td_status(&instance->tds[i]);
[7dd3318]189 if (instance->error != EOK) {
[48563a3]190 usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
[c5b93dc]191 instance, i, instance->tds[i].status);
[67352d2]192 td_print_status(&instance->tds[i]);
[a60150a]193
[bcaefe3]194 device_keeper_set_toggle(instance->manager,
195 instance->target, td_toggle(&instance->tds[i]));
[c5b93dc]196 if (i > 0)
197 goto substract_ret;
[7dd3318]198 return true;
199 }
[c5b93dc]200
201 instance->transfered_size += td_act_size(&instance->tds[i]);
202 if (td_is_short(&instance->tds[i]))
203 goto substract_ret;
[4192d3d6]204 }
[c5b93dc]205substract_ret:
[600733e]206 instance->transfered_size -= instance->setup_size;
[7dd3318]207 return true;
[4192d3d6]208}
209/*----------------------------------------------------------------------------*/
[a7e2f0d]210/** Prepares control write transaction.
211 *
212 * @param[in] instance Batch structure to use.
[17ceb72]213 *
214 * Uses genercir control function with pids OUT and IN.
[a7e2f0d]215 */
[83c439c]216void batch_control_write(batch_t *instance)
[4192d3d6]217{
218 assert(instance);
[17ceb72]219 /* We are data out, we are supposed to provide data */
[a7e2f0d]220 memcpy(instance->transport_buffer, instance->buffer,
221 instance->buffer_size);
[bdc8ab1]222 batch_control(instance, USB_PID_OUT, USB_PID_IN);
[83c439c]223 instance->next_step = batch_call_out_and_dispose;
[4abc304]224 usb_log_debug("Batch(%p) CONTROL WRITE initialized.\n", instance);
[4192d3d6]225}
226/*----------------------------------------------------------------------------*/
[a7e2f0d]227/** Prepares control read transaction.
228 *
229 * @param[in] instance Batch structure to use.
[17ceb72]230 *
231 * Uses generic control with pids IN and OUT.
[a7e2f0d]232 */
[83c439c]233void batch_control_read(batch_t *instance)
[4192d3d6]234{
235 assert(instance);
[bdc8ab1]236 batch_control(instance, USB_PID_IN, USB_PID_OUT);
[83c439c]237 instance->next_step = batch_call_in_and_dispose;
[4abc304]238 usb_log_debug("Batch(%p) CONTROL READ initialized.\n", instance);
[4192d3d6]239}
240/*----------------------------------------------------------------------------*/
[17ceb72]241/** Prepare interrupt in transaction.
[a7e2f0d]242 *
243 * @param[in] instance Batch structure to use.
[17ceb72]244 *
245 * Data transaction with PID_IN.
[a7e2f0d]246 */
[5620bd4]247void batch_interrupt_in(batch_t *instance)
[4192d3d6]248{
[c8dd5b1]249 assert(instance);
[5620bd4]250 batch_data(instance, USB_PID_IN);
[83c439c]251 instance->next_step = batch_call_in_and_dispose;
[4abc304]252 usb_log_debug("Batch(%p) INTERRUPT IN initialized.\n", instance);
[4192d3d6]253}
254/*----------------------------------------------------------------------------*/
[17ceb72]255/** Prepare interrupt out transaction.
[a7e2f0d]256 *
257 * @param[in] instance Batch structure to use.
[17ceb72]258 *
259 * Data transaction with PID_OUT.
[a7e2f0d]260 */
[5620bd4]261void batch_interrupt_out(batch_t *instance)
[4192d3d6]262{
[c8dd5b1]263 assert(instance);
[17ceb72]264 /* We are data out, we are supposed to provide data */
[a963a68]265 memcpy(instance->transport_buffer, instance->buffer,
266 instance->buffer_size);
[5620bd4]267 batch_data(instance, USB_PID_OUT);
[0e06a14]268 instance->next_step = batch_call_out_and_dispose;
269 usb_log_debug("Batch(%p) INTERRUPT OUT initialized.\n", instance);
270}
271/*----------------------------------------------------------------------------*/
[17ceb72]272/** Prepare bulk in transaction.
[a7e2f0d]273 *
274 * @param[in] instance Batch structure to use.
[17ceb72]275 *
276 * Data transaction with PID_IN.
[a7e2f0d]277 */
[5620bd4]278void batch_bulk_in(batch_t *instance)
[0e06a14]279{
280 assert(instance);
[5620bd4]281 batch_data(instance, USB_PID_IN);
[0e06a14]282 instance->next_step = batch_call_in_and_dispose;
283 usb_log_debug("Batch(%p) BULK IN initialized.\n", instance);
284}
285/*----------------------------------------------------------------------------*/
[17ceb72]286/** Prepare bulk out transaction.
[a7e2f0d]287 *
288 * @param[in] instance Batch structure to use.
[17ceb72]289 *
290 * Data transaction with PID_OUT.
[a7e2f0d]291 */
[5620bd4]292void batch_bulk_out(batch_t *instance)
[0e06a14]293{
294 assert(instance);
[17ceb72]295 /* We are data out, we are supposed to provide data */
[a963a68]296 memcpy(instance->transport_buffer, instance->buffer,
297 instance->buffer_size);
[5620bd4]298 batch_data(instance, USB_PID_OUT);
[0e06a14]299 instance->next_step = batch_call_out_and_dispose;
300 usb_log_debug("Batch(%p) BULK OUT initialized.\n", instance);
301}
302/*----------------------------------------------------------------------------*/
[17ceb72]303/** Prepare generic data transaction
[a7e2f0d]304 *
305 * @param[in] instance Batch structure to use.
306 * @param[in] pid to use for data packets.
[17ceb72]307 *
308 * Packets with alternating toggle bit and supplied pid value.
309 * The last packet is marked with IOC flag.
[a7e2f0d]310 */
[5620bd4]311void batch_data(batch_t *instance, usb_packet_id pid)
[0e06a14]312{
313 assert(instance);
[c3ae877]314 const bool low_speed = instance->speed == USB_SPEED_LOW;
[bcaefe3]315 int toggle =
316 device_keeper_get_toggle(instance->manager, instance->target);
[3e37964]317 assert(toggle == 0 || toggle == 1);
[0e06a14]318
319 size_t packet = 0;
320 size_t remain_size = instance->buffer_size;
321 while (remain_size > 0) {
[7dd3318]322 char *data =
[0e06a14]323 instance->transport_buffer + instance->buffer_size
324 - remain_size;
325
326 const size_t packet_size =
327 (instance->max_packet_size > remain_size) ?
328 remain_size : instance->max_packet_size;
[c8dd5b1]329
[bcaefe3]330 td_t *next_packet = (packet + 1 < instance->packets)
331 ? &instance->tds[packet + 1] : NULL;
[30a4301]332
[bcaefe3]333 assert(packet < instance->packets);
[0e06a14]334 assert(packet_size <= remain_size);
[bcaefe3]335
336 td_init(
337 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
338 toggle, false, low_speed, instance->target, pid, data,
339 next_packet);
340
341
342 toggle = 1 - toggle;
[0e06a14]343 remain_size -= packet_size;
[bcaefe3]344 ++packet;
[0e06a14]345 }
[eb0dc58]346 td_set_ioc(&instance->tds[packet - 1]);
[5620bd4]347 device_keeper_set_toggle(instance->manager, instance->target, toggle);
[4192d3d6]348}
349/*----------------------------------------------------------------------------*/
[17ceb72]350/** Prepare generic control transaction
[a7e2f0d]351 *
352 * @param[in] instance Batch structure to use.
353 * @param[in] data_stage to use for data packets.
354 * @param[in] status_stage to use for data packets.
[17ceb72]355 *
356 * Setup stage with toggle 0 and USB_PID_SETUP.
357 * Data stage with alternating toggle and pid supplied by parameter.
358 * Status stage with toggle 1 and pid supplied by parameter.
359 * The last packet is marked with IOC.
[a7e2f0d]360 */
[3e37964]361void batch_control(batch_t *instance,
[eae83aa]362 usb_packet_id data_stage, usb_packet_id status_stage)
[bdc8ab1]363{
364 assert(instance);
365
366 const bool low_speed = instance->speed == USB_SPEED_LOW;
367 int toggle = 0;
368 /* setup stage */
[c5b93dc]369 td_init(instance->tds, DEFAULT_ERROR_COUNT,
[bdc8ab1]370 instance->setup_size, toggle, false, low_speed, instance->target,
371 USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
372
373 /* data stage */
374 size_t packet = 1;
375 size_t remain_size = instance->buffer_size;
376 while (remain_size > 0) {
377 char *data =
378 instance->transport_buffer + instance->buffer_size
379 - remain_size;
380
381 toggle = 1 - toggle;
382
383 const size_t packet_size =
384 (instance->max_packet_size > remain_size) ?
385 remain_size : instance->max_packet_size;
386
[bcaefe3]387 td_init(
388 &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
389 toggle, false, low_speed, instance->target, data_stage,
390 data, &instance->tds[packet + 1]);
[bdc8ab1]391
392 ++packet;
393 assert(packet < instance->packets);
394 assert(packet_size <= remain_size);
395 remain_size -= packet_size;
396 }
397
398 /* status stage */
399 assert(packet == instance->packets - 1);
[c5b93dc]400 td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
[bdc8ab1]401 0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
402
[eb0dc58]403 td_set_ioc(&instance->tds[packet]);
[bdc8ab1]404 usb_log_debug2("Control last TD status: %x.\n",
405 instance->tds[packet].status);
406}
407/*----------------------------------------------------------------------------*/
[17ceb72]408/** Prepare data, get error status and call callback in.
[a7e2f0d]409 *
410 * @param[in] instance Batch structure to use.
[17ceb72]411 * Copies data from transport buffer, and calls callback with appropriate
412 * parameters.
[a7e2f0d]413 */
[83c439c]414void batch_call_in(batch_t *instance)
[4192d3d6]415{
416 assert(instance);
417 assert(instance->callback_in);
418
[17ceb72]419 /* We are data in, we need data */
[a7e2f0d]420 memcpy(instance->buffer, instance->transport_buffer,
421 instance->buffer_size);
[7dd3318]422
423 int err = instance->error;
[4abc304]424 usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
[48563a3]425 instance, instance->transfer_type, str_error(err), err,
426 instance->transfered_size);
[7dd3318]427
[bcaefe3]428 instance->callback_in(
429 instance->fun, err, instance->transfered_size, instance->arg);
[4192d3d6]430}
431/*----------------------------------------------------------------------------*/
[17ceb72]432/** Get error status and call callback out.
[a7e2f0d]433 *
434 * @param[in] instance Batch structure to use.
435 */
[83c439c]436void batch_call_out(batch_t *instance)
[4192d3d6]437{
438 assert(instance);
439 assert(instance->callback_out);
440
[7dd3318]441 int err = instance->error;
[4abc304]442 usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
[48563a3]443 instance, instance->transfer_type, str_error(err), err);
[eb1a2f4]444 instance->callback_out(instance->fun,
[7dd3318]445 err, instance->arg);
[4192d3d6]446}
447/*----------------------------------------------------------------------------*/
[17ceb72]448/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]449 *
450 * @param[in] instance Batch structure to use.
451 */
[83c439c]452void batch_call_in_and_dispose(batch_t *instance)
[4192d3d6]453{
454 assert(instance);
[83c439c]455 batch_call_in(instance);
[a60150a]456 batch_dispose(instance);
[4192d3d6]457}
458/*----------------------------------------------------------------------------*/
[17ceb72]459/** Helper function calls callback and correctly disposes of batch structure.
[a7e2f0d]460 *
461 * @param[in] instance Batch structure to use.
462 */
[83c439c]463void batch_call_out_and_dispose(batch_t *instance)
[4192d3d6]464{
465 assert(instance);
[83c439c]466 batch_call_out(instance);
[a60150a]467 batch_dispose(instance);
468}
469/*----------------------------------------------------------------------------*/
[17ceb72]470/** Correctly dispose all used data structures.
[a7e2f0d]471 *
472 * @param[in] instance Batch structure to use.
473 */
[a60150a]474void batch_dispose(batch_t *instance)
475{
476 assert(instance);
[48563a3]477 usb_log_debug("Batch(%p) disposing.\n", instance);
[bcaefe3]478 /* free32 is NULL safe */
[7dd3318]479 free32(instance->tds);
480 free32(instance->qh);
481 free32(instance->setup_buffer);
482 free32(instance->transport_buffer);
[4192d3d6]483 free(instance);
484}
485/**
486 * @}
487 */
Note: See TracBrowser for help on using the repository browser.