Changeset f8e1a2c in mainline for uspace/drv/uhci-hcd/batch.c


Ignore:
Timestamp:
2011-03-21T13:19:19Z (13 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0e45e7f, 8a951ca
Parents:
e387d0f (diff), 2e1d5d70 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge development/ changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/uhci-hcd/batch.c

    re387d0f rf8e1a2c  
    4242#include "uhci_hc.h"
    4343#include "utils/malloc32.h"
     44#include "uhci_struct/transfer_descriptor.h"
    4445
    4546#define DEFAULT_ERROR_COUNT 3
     47
     48typedef struct uhci_batch {
     49        qh_t *qh;
     50        td_t *tds;
     51        size_t packets;
     52        device_keeper_t *manager;
     53} uhci_batch_t;
    4654
    4755static void batch_control(batch_t *instance,
    4856    usb_packet_id data_stage, usb_packet_id status_stage);
    4957static void batch_data(batch_t *instance, usb_packet_id pid);
    50 static void batch_call_in(batch_t *instance);
    51 static void batch_call_out(batch_t *instance);
    5258static void batch_call_in_and_dispose(batch_t *instance);
    5359static void batch_call_out_and_dispose(batch_t *instance);
     
    7884batch_t * batch_get(ddf_fun_t *fun, usb_target_t target,
    7985    usb_transfer_type_t transfer_type, size_t max_packet_size,
    80     usb_speed_t speed, char *buffer, size_t size,
     86    usb_speed_t speed, char *buffer, size_t buffer_size,
    8187    char* setup_buffer, size_t setup_size,
    8288    usbhc_iface_transfer_in_callback_t func_in,
     
    100106        CHECK_NULL_DISPOSE_RETURN(instance,
    101107            "Failed to allocate batch instance.\n");
    102         bzero(instance, sizeof(batch_t));
    103 
    104         instance->qh = malloc32(sizeof(qh_t));
    105         CHECK_NULL_DISPOSE_RETURN(instance->qh,
     108        batch_init(instance, target, transfer_type, speed, max_packet_size,
     109            buffer, NULL, buffer_size, NULL, setup_size, func_in,
     110            func_out, arg, fun, NULL);
     111
     112
     113        uhci_batch_t *data = malloc(sizeof(uhci_batch_t));
     114        CHECK_NULL_DISPOSE_RETURN(instance,
     115            "Failed to allocate batch instance.\n");
     116        bzero(data, sizeof(uhci_batch_t));
     117        data->manager = manager;
     118        instance->private_data = data;
     119
     120        data->packets = (buffer_size + max_packet_size - 1) / max_packet_size;
     121        if (transfer_type == USB_TRANSFER_CONTROL) {
     122                data->packets += 2;
     123        }
     124
     125        data->tds = malloc32(sizeof(td_t) * data->packets);
     126        CHECK_NULL_DISPOSE_RETURN(
     127            data->tds, "Failed to allocate transfer descriptors.\n");
     128        bzero(data->tds, sizeof(td_t) * data->packets);
     129
     130        data->qh = malloc32(sizeof(qh_t));
     131        CHECK_NULL_DISPOSE_RETURN(data->qh,
    106132            "Failed to allocate batch queue head.\n");
    107         qh_init(instance->qh);
    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 
    114         instance->tds = malloc32(sizeof(td_t) * instance->packets);
    115         CHECK_NULL_DISPOSE_RETURN(
    116             instance->tds, "Failed to allocate transfer descriptors.\n");
    117         bzero(instance->tds, sizeof(td_t) * instance->packets);
    118 
    119         if (size > 0) {
    120                 instance->transport_buffer = malloc32(size);
     133        qh_init(data->qh);
     134        qh_set_element_td(data->qh, addr_to_phys(data->tds));
     135
     136        if (buffer_size > 0) {
     137                instance->transport_buffer = malloc32(buffer_size);
    121138                CHECK_NULL_DISPOSE_RETURN(instance->transport_buffer,
    122139                    "Failed to allocate device accessible buffer.\n");
     
    130147        }
    131148
    132 
    133         link_initialize(&instance->link);
    134 
    135         instance->max_packet_size = max_packet_size;
    136         instance->target = target;
    137         instance->transfer_type = transfer_type;
    138         instance->buffer = buffer;
    139         instance->buffer_size = size;
    140         instance->setup_size = setup_size;
    141         instance->fun = fun;
    142         instance->arg = arg;
    143         instance->speed = speed;
    144         instance->manager = manager;
    145         instance->callback_out = func_out;
    146         instance->callback_in = func_in;
    147 
    148         qh_set_element_td(instance->qh, addr_to_phys(instance->tds));
    149 
    150149        usb_log_debug("Batch(%p) %d:%d memory structures ready.\n",
    151150            instance, target.address, target.endpoint);
     
    153152}
    154153/*----------------------------------------------------------------------------*/
    155 /** Mark batch as failed and continue with next step.
    156  *
    157  * @param[in] instance Batch structure to use.
    158  *
    159  */
    160 void batch_abort(batch_t *instance)
    161 {
    162         assert(instance);
    163         instance->error = EIO;
    164         instance->next_step(instance);
    165 }
    166 /*----------------------------------------------------------------------------*/
    167154/** Check batch TDs for activity.
    168155 *
     
    177164{
    178165        assert(instance);
     166        uhci_batch_t *data = instance->private_data;
     167        assert(data);
     168
    179169        usb_log_debug2("Batch(%p) checking %d packet(s) for completion.\n",
    180             instance, instance->packets);
     170            instance, data->packets);
    181171        instance->transfered_size = 0;
    182172        size_t i = 0;
    183         for (;i < instance->packets; ++i) {
    184                 if (td_is_active(&instance->tds[i])) {
     173        for (;i < data->packets; ++i) {
     174                if (td_is_active(&data->tds[i])) {
    185175                        return false;
    186176                }
    187177
    188                 instance->error = td_status(&instance->tds[i]);
     178                instance->error = td_status(&data->tds[i]);
    189179                if (instance->error != EOK) {
    190180                        usb_log_debug("Batch(%p) found error TD(%d):%x.\n",
    191                             instance, i, instance->tds[i].status);
    192                         td_print_status(&instance->tds[i]);
    193 
    194                         device_keeper_set_toggle(instance->manager,
    195                             instance->target, td_toggle(&instance->tds[i]));
     181                            instance, i, data->tds[i].status);
     182                        td_print_status(&data->tds[i]);
     183
     184                        device_keeper_set_toggle(data->manager,
     185                            instance->target, td_toggle(&data->tds[i]));
    196186                        if (i > 0)
    197187                                goto substract_ret;
     
    199189                }
    200190
    201                 instance->transfered_size += td_act_size(&instance->tds[i]);
    202                 if (td_is_short(&instance->tds[i]))
     191                instance->transfered_size += td_act_size(&data->tds[i]);
     192                if (td_is_short(&data->tds[i]))
    203193                        goto substract_ret;
    204194        }
     
    312302{
    313303        assert(instance);
     304        uhci_batch_t *data = instance->private_data;
     305        assert(data);
     306
    314307        const bool low_speed = instance->speed == USB_SPEED_LOW;
    315         int toggle =
    316             device_keeper_get_toggle(instance->manager, instance->target);
     308        int toggle = device_keeper_get_toggle(data->manager, instance->target);
    317309        assert(toggle == 0 || toggle == 1);
    318310
     
    320312        size_t remain_size = instance->buffer_size;
    321313        while (remain_size > 0) {
    322                 char *data =
     314                char *trans_data =
    323315                    instance->transport_buffer + instance->buffer_size
    324316                    - remain_size;
     
    328320                    remain_size : instance->max_packet_size;
    329321
    330                 td_t *next_packet = (packet + 1 < instance->packets)
    331                     ? &instance->tds[packet + 1] : NULL;
    332 
    333                 assert(packet < instance->packets);
     322                td_t *next_packet = (packet + 1 < data->packets)
     323                    ? &data->tds[packet + 1] : NULL;
     324
     325                assert(packet < data->packets);
    334326                assert(packet_size <= remain_size);
    335327
    336328                td_init(
    337                     &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
    338                     toggle, false, low_speed, instance->target, pid, data,
     329                    &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
     330                    toggle, false, low_speed, instance->target, pid, trans_data,
    339331                    next_packet);
    340332
     
    344336                ++packet;
    345337        }
    346         td_set_ioc(&instance->tds[packet - 1]);
    347         device_keeper_set_toggle(instance->manager, instance->target, toggle);
     338        td_set_ioc(&data->tds[packet - 1]);
     339        device_keeper_set_toggle(data->manager, instance->target, toggle);
    348340}
    349341/*----------------------------------------------------------------------------*/
     
    363355{
    364356        assert(instance);
     357        uhci_batch_t *data = instance->private_data;
     358        assert(data);
     359        assert(data->packets >= 2);
    365360
    366361        const bool low_speed = instance->speed == USB_SPEED_LOW;
    367362        int toggle = 0;
    368363        /* setup stage */
    369         td_init(instance->tds, DEFAULT_ERROR_COUNT,
    370             instance->setup_size, toggle, false, low_speed, instance->target,
    371             USB_PID_SETUP, instance->setup_buffer, &instance->tds[1]);
     364        td_init(
     365            data->tds, DEFAULT_ERROR_COUNT, instance->setup_size, toggle, false,
     366            low_speed, instance->target, USB_PID_SETUP, instance->setup_buffer,
     367            &data->tds[1]);
    372368
    373369        /* data stage */
     
    375371        size_t remain_size = instance->buffer_size;
    376372        while (remain_size > 0) {
    377                 char *data =
     373                char *control_data =
    378374                    instance->transport_buffer + instance->buffer_size
    379375                    - remain_size;
     
    386382
    387383                td_init(
    388                     &instance->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
     384                    &data->tds[packet], DEFAULT_ERROR_COUNT, packet_size,
    389385                    toggle, false, low_speed, instance->target, data_stage,
    390                     data, &instance->tds[packet + 1]);
     386                    control_data, &data->tds[packet + 1]);
    391387
    392388                ++packet;
    393                 assert(packet < instance->packets);
     389                assert(packet < data->packets);
    394390                assert(packet_size <= remain_size);
    395391                remain_size -= packet_size;
     
    397393
    398394        /* status stage */
    399         assert(packet == instance->packets - 1);
    400         td_init(&instance->tds[packet], DEFAULT_ERROR_COUNT,
    401             0, 1, false, low_speed, instance->target, status_stage, NULL, NULL);
    402 
    403         td_set_ioc(&instance->tds[packet]);
     395        assert(packet == data->packets - 1);
     396
     397        td_init(
     398            &data->tds[packet], DEFAULT_ERROR_COUNT, 0, 1, false, low_speed,
     399            instance->target, status_stage, NULL, NULL);
     400        td_set_ioc(&data->tds[packet]);
     401
    404402        usb_log_debug2("Control last TD status: %x.\n",
    405             instance->tds[packet].status);
    406 }
    407 /*----------------------------------------------------------------------------*/
    408 /** Prepare data, get error status and call callback in.
    409  *
    410  * @param[in] instance Batch structure to use.
    411  * Copies data from transport buffer, and calls callback with appropriate
    412  * parameters.
    413  */
    414 void batch_call_in(batch_t *instance)
    415 {
    416         assert(instance);
    417         assert(instance->callback_in);
    418 
    419         /* We are data in, we need data */
    420         memcpy(instance->buffer, instance->transport_buffer,
    421             instance->buffer_size);
    422 
    423         int err = instance->error;
    424         usb_log_debug("Batch(%p) callback IN(type:%d): %s(%d), %zu.\n",
    425             instance, instance->transfer_type, str_error(err), err,
    426             instance->transfered_size);
    427 
    428         instance->callback_in(
    429             instance->fun, err, instance->transfered_size, instance->arg);
    430 }
    431 /*----------------------------------------------------------------------------*/
    432 /** Get error status and call callback out.
    433  *
    434  * @param[in] instance Batch structure to use.
    435  */
    436 void batch_call_out(batch_t *instance)
    437 {
    438         assert(instance);
    439         assert(instance->callback_out);
    440 
    441         int err = instance->error;
    442         usb_log_debug("Batch(%p) callback OUT(type:%d): %s(%d).\n",
    443             instance, instance->transfer_type, str_error(err), err);
    444         instance->callback_out(instance->fun,
    445             err, instance->arg);
     403            data->tds[packet].status);
     404}
     405/*----------------------------------------------------------------------------*/
     406qh_t * batch_qh(batch_t *instance)
     407{
     408        assert(instance);
     409        uhci_batch_t *data = instance->private_data;
     410        assert(data);
     411        return data->qh;
    446412}
    447413/*----------------------------------------------------------------------------*/
     
    475441{
    476442        assert(instance);
     443        uhci_batch_t *data = instance->private_data;
     444        assert(data);
    477445        usb_log_debug("Batch(%p) disposing.\n", instance);
    478446        /* free32 is NULL safe */
    479         free32(instance->tds);
    480         free32(instance->qh);
     447        free32(data->tds);
     448        free32(data->qh);
    481449        free32(instance->setup_buffer);
    482450        free32(instance->transport_buffer);
     451        free(data);
    483452        free(instance);
    484453}
Note: See TracChangeset for help on using the changeset viewer.