| 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 libusbhost
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * USB transfer transaction structures (implementation).
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <usb/host/usb_transfer_batch.h>
|
|---|
| 36 | #include <usb/host/endpoint.h>
|
|---|
| 37 | #include <usb/host/bus.h>
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <assert.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <str_error.h>
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /** Create a batch on given endpoint.
|
|---|
| 46 | */
|
|---|
| 47 | usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
|
|---|
| 48 | {
|
|---|
| 49 | assert(ep);
|
|---|
| 50 | assert(ep->bus);
|
|---|
| 51 |
|
|---|
| 52 | usb_transfer_batch_t *batch;
|
|---|
| 53 | if (ep->bus->ops.create_batch)
|
|---|
| 54 | batch = ep->bus->ops.create_batch(ep->bus, ep);
|
|---|
| 55 | else
|
|---|
| 56 | batch = malloc(sizeof(usb_transfer_batch_t));
|
|---|
| 57 |
|
|---|
| 58 | return batch;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /** Initialize given batch structure.
|
|---|
| 62 | */
|
|---|
| 63 | void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
|
|---|
| 64 | {
|
|---|
| 65 | endpoint_use(ep);
|
|---|
| 66 |
|
|---|
| 67 | memset(batch, 0, sizeof(*batch));
|
|---|
| 68 | batch->ep = ep;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /** Call the handler of the batch.
|
|---|
| 72 | *
|
|---|
| 73 | * @param[in] batch Batch structure to use.
|
|---|
| 74 | */
|
|---|
| 75 | static int batch_complete(usb_transfer_batch_t *batch)
|
|---|
| 76 | {
|
|---|
| 77 | assert(batch);
|
|---|
| 78 |
|
|---|
| 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);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 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 | */
|
|---|
| 99 | void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
|
|---|
| 100 | {
|
|---|
| 101 | assert(batch);
|
|---|
| 102 | assert(batch->ep);
|
|---|
| 103 | assert(batch->ep->bus);
|
|---|
| 104 |
|
|---|
| 105 | bus_t *bus = batch->ep->bus;
|
|---|
| 106 | if (bus->ops.destroy_batch) {
|
|---|
| 107 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " destroying.\n",
|
|---|
| 108 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
|---|
| 109 | bus->ops.destroy_batch(batch);
|
|---|
| 110 | }
|
|---|
| 111 | else {
|
|---|
| 112 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.\n",
|
|---|
| 113 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
|---|
| 114 | free(batch);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | endpoint_release(batch->ep);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /** Finish a transfer batch: call handler, destroy batch, release endpoint.
|
|---|
| 121 | *
|
|---|
| 122 | * Call only after the batch have been scheduled && completed!
|
|---|
| 123 | *
|
|---|
| 124 | * @param[in] batch Batch structure to use.
|
|---|
| 125 | */
|
|---|
| 126 | void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
|
|---|
| 127 | {
|
|---|
| 128 | const int err = batch_complete(batch);
|
|---|
| 129 | if (err)
|
|---|
| 130 | usb_log_warning("batch %p failed to complete: %s", batch, str_error(err));
|
|---|
| 131 |
|
|---|
| 132 | usb_transfer_batch_destroy(batch);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | struct old_handler_wrapper_data {
|
|---|
| 137 | usbhc_iface_transfer_in_callback_t in_callback;
|
|---|
| 138 | usbhc_iface_transfer_out_callback_t out_callback;
|
|---|
| 139 | void *arg;
|
|---|
| 140 | };
|
|---|
| 141 |
|
|---|
| 142 | static int old_handler_wrapper(usb_transfer_batch_t *batch)
|
|---|
| 143 | {
|
|---|
| 144 | struct old_handler_wrapper_data *data = batch->on_complete_data;
|
|---|
| 145 |
|
|---|
| 146 | assert(data);
|
|---|
| 147 |
|
|---|
| 148 | if (data->out_callback)
|
|---|
| 149 | data->out_callback(batch->error, data->arg);
|
|---|
| 150 |
|
|---|
| 151 | if (data->in_callback)
|
|---|
| 152 | data->in_callback(batch->error, batch->transfered_size, data->arg);
|
|---|
| 153 |
|
|---|
| 154 | free(data);
|
|---|
| 155 | return EOK;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | void usb_transfer_batch_set_old_handlers(usb_transfer_batch_t *batch,
|
|---|
| 159 | usbhc_iface_transfer_in_callback_t in_callback,
|
|---|
| 160 | usbhc_iface_transfer_out_callback_t out_callback,
|
|---|
| 161 | void *arg)
|
|---|
| 162 | {
|
|---|
| 163 | struct old_handler_wrapper_data *data = malloc(sizeof(*data));
|
|---|
| 164 |
|
|---|
| 165 | assert((!in_callback) != (!out_callback));
|
|---|
| 166 |
|
|---|
| 167 | data->in_callback = in_callback;
|
|---|
| 168 | data->out_callback = out_callback;
|
|---|
| 169 | data->arg = arg;
|
|---|
| 170 |
|
|---|
| 171 | batch->on_complete = old_handler_wrapper;
|
|---|
| 172 | batch->on_complete_data = data;
|
|---|
| 173 | }
|
|---|
| 174 | /**
|
|---|
| 175 | * @}
|
|---|
| 176 | */
|
|---|