Changeset 5fd9c30 in mainline for uspace/lib/usbhost/src


Ignore:
Timestamp:
2017-10-21T20:52:56Z (8 years ago)
Author:
Ondřej Hlavatý <aearsis@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
766043c
Parents:
74b852b
Message:

usbhost refactoring: let transfer_batch be initialized by bus

Currently makes older HCs fail, work in progress.

Location:
uspace/lib/usbhost/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usbhost/src/hcd.c

    r74b852b r5fd9c30  
    6969}
    7070
    71 typedef struct {
    72         void *original_data;
    73         usbhc_iface_transfer_out_callback_t original_callback;
    74         usb_target_t target;
    75         hcd_t *hcd;
    76 } toggle_t;
    77 
    78 static void toggle_reset_callback(int retval, void *arg)
    79 {
    80         assert(arg);
    81         toggle_t *toggle = arg;
    82         if (retval == EOK) {
    83                 usb_log_debug2("Reseting toggle on %d:%d.\n",
    84                     toggle->target.address, toggle->target.endpoint);
    85                 bus_reset_toggle(toggle->hcd->bus,
    86                     toggle->target, toggle->target.endpoint == 0);
    87         }
    88 
    89         toggle->original_callback(retval, toggle->original_data);
    90 }
    91 
    9271/** Prepare generic usb_transfer_batch and schedule it.
    9372 * @param hcd Host controller driver.
    94  * @param fun DDF fun
    9573 * @param target address and endpoint number.
    9674 * @param setup_data Data to use in setup stage (Control communication type)
     
    10179 * @return Error code.
    10280 */
    103 int hcd_send_batch(
    104     hcd_t *hcd, usb_target_t target, usb_direction_t direction,
    105     void *data, size_t size, uint64_t setup_data,
    106     usbhc_iface_transfer_in_callback_t in,
    107     usbhc_iface_transfer_out_callback_t out, void *arg, const char* name)
     81int hcd_send_batch(hcd_t *hcd, usb_target_t target, usb_direction_t direction,
     82        void *data, size_t size, uint64_t setup_data,
     83        usbhc_iface_transfer_in_callback_t in, usbhc_iface_transfer_out_callback_t out,
     84        void *arg, const char *name)
    10885{
    10986        assert(hcd);
     
    132109        }
    133110
    134         /* Check for commands that reset toggle bit */
    135         if (ep->transfer_type == USB_TRANSFER_CONTROL) {
    136                 const int reset_toggle = usb_request_needs_toggle_reset(
    137                     (usb_device_request_setup_packet_t *) &setup_data);
    138                 if (reset_toggle >= 0) {
    139                         assert(out);
    140                         toggle_t *toggle = malloc(sizeof(toggle_t));
    141                         if (!toggle)
    142                                 return ENOMEM;
    143                         toggle->target.address = target.address;
    144                         toggle->target.endpoint = reset_toggle;
    145                         toggle->original_callback = out;
    146                         toggle->original_data = arg;
    147                         toggle->hcd = hcd;
    148 
    149                         arg = toggle;
    150                         out = toggle_reset_callback;
    151                 }
    152         }
    153 
    154         usb_transfer_batch_t *batch = usb_transfer_batch_create(
    155             ep, data, size, setup_data, in, out, arg);
     111        usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
    156112        if (!batch) {
    157113                usb_log_error("Failed to create transfer batch.\n");
    158114                return ENOMEM;
    159115        }
     116
     117        batch->buffer = data;
     118        batch->buffer_size = size;
     119        batch->setup.packed = setup_data;
     120        batch->dir = direction;
     121
     122        /* Check for commands that reset toggle bit */
     123        if (ep->transfer_type == USB_TRANSFER_CONTROL)
     124                batch->toggle_reset_mode
     125                        = usb_request_get_toggle_reset_mode(&batch->setup.packet);
     126
     127        usb_transfer_batch_set_old_handlers(batch, in, out, arg);
    160128
    161129        const int ret = hcd->ops.schedule(hcd, batch);
  • uspace/lib/usbhost/src/usb_transfer_batch.c

    r74b852b r5fd9c30  
    3434
    3535#include <usb/host/usb_transfer_batch.h>
     36#include <usb/host/endpoint.h>
     37#include <usb/host/bus.h>
    3638#include <usb/debug.h>
    3739
    3840#include <assert.h>
    3941#include <errno.h>
    40 #include <macros.h>
    41 #include <mem.h>
    42 #include <stdlib.h>
    43 #include <usbhc_iface.h>
    4442
    45 /** Allocate and initialize usb_transfer_batch structure.
    46  * @param ep endpoint used by the transfer batch.
    47  * @param buffer data to send/recieve.
    48  * @param buffer_size Size of data buffer.
    49  * @param setup_buffer Data to send in SETUP stage of control transfer.
    50  * @param func_in callback on IN transfer completion.
    51  * @param func_out callback on OUT transfer completion.
    52  * @param fun DDF function (passed to callback function).
    53  * @param arg Argument to pass to the callback function.
    54  * @param private_data driver specific per batch data.
    55  * @param private_data_dtor Function to properly destroy private_data.
    56  * @return Pointer to valid usb_transfer_batch_t structure, NULL on failure.
     43
     44/** Create a batch on given endpoint.
    5745 */
    58 usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep, char *buffer,
    59     size_t buffer_size,
    60     uint64_t setup_buffer,
    61     usbhc_iface_transfer_in_callback_t func_in,
    62     usbhc_iface_transfer_out_callback_t func_out,
    63     void *arg)
     46usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
    6447{
    65         if (func_in == NULL && func_out == NULL)
    66                 return NULL;
    67         if (func_in != NULL && func_out != NULL)
    68                 return NULL;
     48        assert(ep);
     49        assert(ep->bus);
    6950
    70         usb_transfer_batch_t *instance = malloc(sizeof(usb_transfer_batch_t));
    71         if (instance) {
    72                 instance->ep = ep;
    73                 instance->callback_in = func_in;
    74                 instance->callback_out = func_out;
    75                 instance->arg = arg;
    76                 instance->buffer = buffer;
    77                 instance->buffer_size = buffer_size;
    78                 instance->setup_size = 0;
    79                 instance->transfered_size = 0;
    80                 instance->error = EOK;
    81                 if (ep && ep->transfer_type == USB_TRANSFER_CONTROL) {
    82                         memcpy(instance->setup_buffer, &setup_buffer,
    83                             USB_SETUP_PACKET_SIZE);
    84                         instance->setup_size = USB_SETUP_PACKET_SIZE;
    85                 }
    86                 if (instance->ep)
    87                         endpoint_use(instance->ep);
    88         }
    89         return instance;
     51        usb_transfer_batch_t *batch;
     52        if (ep->bus->ops.create_batch)
     53                batch = ep->bus->ops.create_batch(ep->bus, ep);
     54        else
     55                batch = malloc(sizeof(usb_transfer_batch_t));
     56
     57        return batch;
    9058}
    9159
    92 /** Correctly dispose all used data structures.
    93  *
    94  * @param[in] instance Batch structure to use.
     60/** Initialize given batch structure.
    9561 */
    96 void usb_transfer_batch_destroy(const usb_transfer_batch_t *instance)
     62void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
    9763{
    98         if (!instance)
    99                 return;
    100         usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
    101             instance, USB_TRANSFER_BATCH_ARGS(*instance));
    102         if (instance->ep) {
    103                 endpoint_release(instance->ep);
    104         }
    105         free(instance);
     64        memset(batch, 0, sizeof(*batch));
     65
     66        batch->ep = ep;
     67
     68        endpoint_use(ep);
    10669}
    10770
    108 /** Prepare data and call the right callback.
     71/** Call the handler of the batch.
    10972 *
    110  * @param[in] instance Batch structure to use.
    111  * @param[in] data Data to copy to the output buffer.
    112  * @param[in] size Size of @p data.
    113  * @param[in] error Error value to use.
     73 * @param[in] batch Batch structure to use.
    11474 */
    115 void usb_transfer_batch_finish_error(const usb_transfer_batch_t *instance,
    116     const void *data, size_t size, int error)
     75static int batch_complete(usb_transfer_batch_t *batch)
    11776{
    118         assert(instance);
    119         usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.\n",
    120             instance, USB_TRANSFER_BATCH_ARGS(*instance));
     77        assert(batch);
    12178
    122         /* NOTE: Only one of these pointers should be set. */
    123         if (instance->callback_out) {
    124                 instance->callback_out(error, instance->arg);
     79        usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " completed.\n",
     80            batch, USB_TRANSFER_BATCH_ARGS(*batch));
     81
     82        if (batch->error == EOK && batch->toggle_reset_mode != RESET_NONE) {
     83                usb_log_debug2("Reseting %s due to transaction of %d:%d.\n",
     84                    batch->toggle_reset_mode == RESET_ALL ? "all EPs toggle" : "EP toggle",
     85                    batch->ep->target.address, batch->ep->target.endpoint);
     86                bus_reset_toggle(batch->ep->bus,
     87                    batch->ep->target, batch->toggle_reset_mode == RESET_ALL);
    12588        }
    12689
    127         if (instance->callback_in) {
    128                 /* We care about the data and there are some to copy */
    129                 const size_t safe_size = min(size, instance->buffer_size);
    130                 if (data) {
    131                         memcpy(instance->buffer, data, safe_size);
    132                 }
    133                 instance->callback_in(error, safe_size, instance->arg);
    134         }
     90        return batch->on_complete
     91                ? batch->on_complete(batch)
     92                : EOK;
     93}
     94
     95/** Destroy the batch.
     96 *
     97 * @param[in] batch Batch structure to use.
     98 */
     99void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
     100{
     101        assert(batch);
     102        assert(batch->ep);
     103        assert(batch->ep->bus);
     104
     105        usb_log_debug2("batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
     106            batch, USB_TRANSFER_BATCH_ARGS(*batch));
     107
     108        bus_t *bus = batch->ep->bus;
     109        if (bus->ops.destroy_batch)
     110                bus->ops.destroy_batch(batch);
     111        else
     112                free(batch);
     113
     114        endpoint_release(batch->ep);
     115}
     116
     117/** Finish a transfer batch: call handler, destroy batch, release endpoint.
     118 *
     119 * Call only after the batch have been scheduled && completed!
     120 *
     121 * @param[in] batch Batch structure to use.
     122 */
     123void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
     124{
     125        if (!batch_complete(batch))
     126                usb_log_warning("failed to complete batch %p!", batch);
     127
     128        usb_transfer_batch_destroy(batch);
     129}
     130
     131
     132struct old_handler_wrapper_data {
     133        usbhc_iface_transfer_in_callback_t in_callback;
     134        usbhc_iface_transfer_out_callback_t out_callback;
     135        void *arg;
     136};
     137
     138static int old_handler_wrapper(usb_transfer_batch_t *batch)
     139{
     140        struct old_handler_wrapper_data *data = batch->on_complete_data;
     141
     142        assert(data);
     143
     144        if (data->out_callback)
     145                data->out_callback(batch->error, data->arg);
     146
     147        if (data->in_callback)
     148                data->in_callback(batch->error, batch->transfered_size, data->arg);
     149
     150        free(data);
     151        return EOK;
     152}
     153
     154void usb_transfer_batch_set_old_handlers(usb_transfer_batch_t *batch,
     155        usbhc_iface_transfer_in_callback_t in_callback,
     156        usbhc_iface_transfer_out_callback_t out_callback,
     157        void *arg)
     158{
     159        struct old_handler_wrapper_data *data = malloc(sizeof(*data));
     160
     161        data->in_callback = in_callback;
     162        data->out_callback = out_callback;
     163        data->arg = arg;
     164
     165        batch->on_complete = old_handler_wrapper;
     166        batch->on_complete_data = data;
    135167}
    136168/**
Note: See TracChangeset for help on using the changeset viewer.