[81dce9f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[81dce9f] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
[160b75e] | 29 | /** @addtogroup libusbhost
|
---|
[81dce9f] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[c92c13f] | 33 | * USB transfer transaction structures (implementation).
|
---|
[81dce9f] | 34 | */
|
---|
| 35 |
|
---|
[8d2e251] | 36 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
[64fea02] | 38 | #include <stdlib.h>
|
---|
[2b61945] | 39 | #include <str_error.h>
|
---|
[64fea02] | 40 | #include <usb/debug.h>
|
---|
| 41 |
|
---|
| 42 | #include "endpoint.h"
|
---|
| 43 | #include "bus.h"
|
---|
[5fd9c30] | 44 |
|
---|
[64fea02] | 45 | #include "usb_transfer_batch.h"
|
---|
[5fd9c30] | 46 |
|
---|
[1102eca] | 47 | /**
|
---|
| 48 | * Create a batch on a given endpoint.
|
---|
| 49 | *
|
---|
| 50 | * If the bus callback is not defined, it just creates a default batch.
|
---|
[549ff23] | 51 | */
|
---|
[5fd9c30] | 52 | usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
|
---|
[81dce9f] | 53 | {
|
---|
[5fd9c30] | 54 | assert(ep);
|
---|
| 55 |
|
---|
[6832245] | 56 | bus_t *bus = endpoint_get_bus(ep);
|
---|
[5fd9c30] | 57 |
|
---|
[296d22fc] | 58 | if (!bus->ops->batch_create) {
|
---|
[6832245] | 59 | usb_transfer_batch_t *batch = calloc(1, sizeof(usb_transfer_batch_t));
|
---|
[1102eca] | 60 | if (!batch)
|
---|
| 61 | return NULL;
|
---|
[6832245] | 62 | usb_transfer_batch_init(batch, ep);
|
---|
| 63 | return batch;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[296d22fc] | 66 | return bus->ops->batch_create(ep);
|
---|
[2cc6e97] | 67 | }
|
---|
[a76b01b4] | 68 |
|
---|
[1102eca] | 69 | /**
|
---|
| 70 | * Initialize given batch structure.
|
---|
[5fd9c30] | 71 | */
|
---|
| 72 | void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
|
---|
| 73 | {
|
---|
[6832245] | 74 | assert(ep);
|
---|
[1102eca] | 75 | /* Batch reference */
|
---|
[6832245] | 76 | endpoint_add_ref(ep);
|
---|
[5fd9c30] | 77 | batch->ep = ep;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[1102eca] | 80 | /**
|
---|
| 81 | * Destroy the batch. If there's no bus callback, just free it.
|
---|
[549ff23] | 82 | */
|
---|
[5fd9c30] | 83 | void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
|
---|
[549ff23] | 84 | {
|
---|
[5fd9c30] | 85 | assert(batch);
|
---|
| 86 | assert(batch->ep);
|
---|
[549ff23] | 87 |
|
---|
[6832245] | 88 | bus_t *bus = endpoint_get_bus(batch->ep);
|
---|
[296d22fc] | 89 | endpoint_t *ep = batch->ep;
|
---|
[6832245] | 90 |
|
---|
[296d22fc] | 91 | if (bus->ops) {
|
---|
[a1732929] | 92 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " destroying.",
|
---|
[2b61945] | 93 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
[296d22fc] | 94 | bus->ops->batch_destroy(batch);
|
---|
[1433ecda] | 95 | } else {
|
---|
[a1732929] | 96 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.",
|
---|
[2b61945] | 97 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
[5fd9c30] | 98 | free(batch);
|
---|
[2b61945] | 99 | }
|
---|
[296d22fc] | 100 |
|
---|
| 101 | /* Batch reference */
|
---|
| 102 | endpoint_del_ref(ep);
|
---|
[5fd9c30] | 103 | }
|
---|
| 104 |
|
---|
[1d758fc] | 105 | bool usb_transfer_batch_bounce_required(usb_transfer_batch_t *batch)
|
---|
| 106 | {
|
---|
| 107 | if (!batch->size)
|
---|
| 108 | return false;
|
---|
| 109 |
|
---|
| 110 | unsigned flags = batch->dma_buffer.policy & DMA_POLICY_FLAGS_MASK;
|
---|
| 111 | unsigned required_flags =
|
---|
| 112 | batch->ep->required_transfer_buffer_policy & DMA_POLICY_FLAGS_MASK;
|
---|
| 113 |
|
---|
| 114 | if (required_flags & ~flags)
|
---|
| 115 | return true;
|
---|
| 116 |
|
---|
| 117 | size_t chunk_mask = dma_policy_chunk_mask(batch->dma_buffer.policy);
|
---|
| 118 | size_t required_chunk_mask =
|
---|
[1433ecda] | 119 | dma_policy_chunk_mask(batch->ep->required_transfer_buffer_policy);
|
---|
[1d758fc] | 120 |
|
---|
| 121 | /* If the chunks are at least as large as required, we're good */
|
---|
| 122 | if ((required_chunk_mask & ~chunk_mask) == 0)
|
---|
| 123 | return false;
|
---|
| 124 |
|
---|
| 125 | size_t start_chunk = batch->offset & ~chunk_mask;
|
---|
| 126 | size_t end_chunk = (batch->offset + batch->size - 1) & ~chunk_mask;
|
---|
| 127 |
|
---|
| 128 | /* The requested area crosses a chunk boundary */
|
---|
| 129 | if (start_chunk != end_chunk)
|
---|
| 130 | return true;
|
---|
| 131 |
|
---|
| 132 | return false;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[2f762a7] | 135 | errno_t usb_transfer_batch_bounce(usb_transfer_batch_t *batch)
|
---|
| 136 | {
|
---|
| 137 | assert(batch);
|
---|
| 138 | assert(!batch->is_bounced);
|
---|
| 139 |
|
---|
[1d758fc] | 140 | dma_buffer_release(&batch->dma_buffer);
|
---|
| 141 |
|
---|
| 142 | batch->original_buffer = batch->dma_buffer.virt + batch->offset;
|
---|
[2f762a7] | 143 |
|
---|
| 144 | usb_log_debug("Batch(%p): Buffer cannot be used directly, "
|
---|
| 145 | "falling back to bounce buffer!", batch);
|
---|
| 146 |
|
---|
| 147 | const errno_t err = dma_buffer_alloc_policy(&batch->dma_buffer,
|
---|
[1d758fc] | 148 | batch->size, batch->ep->transfer_buffer_policy);
|
---|
[2f762a7] | 149 | if (err)
|
---|
| 150 | return err;
|
---|
| 151 |
|
---|
| 152 | /* Copy the data out */
|
---|
| 153 | if (batch->dir == USB_DIRECTION_OUT)
|
---|
[1d758fc] | 154 | memcpy(batch->dma_buffer.virt,
|
---|
| 155 | batch->original_buffer,
|
---|
| 156 | batch->size);
|
---|
[2f762a7] | 157 |
|
---|
| 158 | batch->is_bounced = true;
|
---|
[1d758fc] | 159 | batch->offset = 0;
|
---|
[2f762a7] | 160 |
|
---|
[1d758fc] | 161 | return err;
|
---|
[c21e6a5] | 162 | }
|
---|
| 163 |
|
---|
[1102eca] | 164 | /**
|
---|
| 165 | * Finish a transfer batch: call handler, destroy batch, release endpoint.
|
---|
[5fd9c30] | 166 | *
|
---|
| 167 | * Call only after the batch have been scheduled && completed!
|
---|
| 168 | */
|
---|
| 169 | void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
|
---|
| 170 | {
|
---|
[17873ac7] | 171 | assert(batch);
|
---|
| 172 | assert(batch->ep);
|
---|
| 173 |
|
---|
[a1732929] | 174 | usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.",
|
---|
[17873ac7] | 175 | batch, USB_TRANSFER_BATCH_ARGS(*batch));
|
---|
| 176 |
|
---|
[1d758fc] | 177 | if (batch->error == EOK && batch->size > 0) {
|
---|
| 178 | if (batch->is_bounced) {
|
---|
[c21e6a5] | 179 | /* We we're forced to use bounce buffer, copy it back */
|
---|
| 180 | if (batch->dir == USB_DIRECTION_IN)
|
---|
[1433ecda] | 181 | memcpy(batch->original_buffer,
|
---|
| 182 | batch->dma_buffer.virt,
|
---|
| 183 | batch->transferred_size);
|
---|
[c21e6a5] | 184 |
|
---|
| 185 | dma_buffer_free(&batch->dma_buffer);
|
---|
[1433ecda] | 186 | } else {
|
---|
[1d758fc] | 187 | dma_buffer_release(&batch->dma_buffer);
|
---|
| 188 | }
|
---|
[c21e6a5] | 189 | }
|
---|
| 190 |
|
---|
[17873ac7] | 191 | if (batch->on_complete) {
|
---|
[db51a6a6] | 192 | const int err = batch->on_complete(batch->on_complete_data, batch->error, batch->transferred_size);
|
---|
[17873ac7] | 193 | if (err)
|
---|
[c21e6a5] | 194 | usb_log_warning("Batch %p failed to complete: %s",
|
---|
[17873ac7] | 195 | batch, str_error(err));
|
---|
| 196 | }
|
---|
[5fd9c30] | 197 |
|
---|
| 198 | usb_transfer_batch_destroy(batch);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[81dce9f] | 201 | /**
|
---|
| 202 | * @}
|
---|
| 203 | */
|
---|