source: mainline/uspace/lib/usbhost/src/usb_transfer_batch.c@ 4e2d387

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e2d387 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2018 Ondrej Hlavaty
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 */
29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 * USB transfer transaction structures (implementation).
34 */
35
36#include <assert.h>
37#include <errno.h>
38#include <stdlib.h>
39#include <str_error.h>
40#include <usb/debug.h>
41
42#include "endpoint.h"
43#include "bus.h"
44
45#include "usb_transfer_batch.h"
46
47/**
48 * Create a batch on a given endpoint.
49 *
50 * If the bus callback is not defined, it just creates a default batch.
51 */
52usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
53{
54 assert(ep);
55
56 bus_t *bus = endpoint_get_bus(ep);
57
58 if (!bus->ops->batch_create) {
59 usb_transfer_batch_t *batch = calloc(1, sizeof(usb_transfer_batch_t));
60 if (!batch)
61 return NULL;
62 usb_transfer_batch_init(batch, ep);
63 return batch;
64 }
65
66 return bus->ops->batch_create(ep);
67}
68
69/**
70 * Initialize given batch structure.
71 */
72void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
73{
74 assert(ep);
75 /* Batch reference */
76 endpoint_add_ref(ep);
77 batch->ep = ep;
78}
79
80/**
81 * Destroy the batch. If there's no bus callback, just free it.
82 */
83void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
84{
85 assert(batch);
86 assert(batch->ep);
87
88 bus_t *bus = endpoint_get_bus(batch->ep);
89 endpoint_t *ep = batch->ep;
90
91 if (bus->ops) {
92 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " destroying.",
93 batch, USB_TRANSFER_BATCH_ARGS(*batch));
94 bus->ops->batch_destroy(batch);
95 } else {
96 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " disposing.",
97 batch, USB_TRANSFER_BATCH_ARGS(*batch));
98 free(batch);
99 }
100
101 /* Batch reference */
102 endpoint_del_ref(ep);
103}
104
105bool 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 =
119 dma_policy_chunk_mask(batch->ep->required_transfer_buffer_policy);
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
135errno_t usb_transfer_batch_bounce(usb_transfer_batch_t *batch)
136{
137 assert(batch);
138 assert(!batch->is_bounced);
139
140 dma_buffer_release(&batch->dma_buffer);
141
142 batch->original_buffer = batch->dma_buffer.virt + batch->offset;
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,
148 batch->size, batch->ep->transfer_buffer_policy);
149 if (err)
150 return err;
151
152 /* Copy the data out */
153 if (batch->dir == USB_DIRECTION_OUT)
154 memcpy(batch->dma_buffer.virt,
155 batch->original_buffer,
156 batch->size);
157
158 batch->is_bounced = true;
159 batch->offset = 0;
160
161 return err;
162}
163
164/**
165 * Finish a transfer batch: call handler, destroy batch, release endpoint.
166 *
167 * Call only after the batch have been scheduled && completed!
168 */
169void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
170{
171 assert(batch);
172 assert(batch->ep);
173
174 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.",
175 batch, USB_TRANSFER_BATCH_ARGS(*batch));
176
177 if (batch->error == EOK && batch->size > 0) {
178 if (batch->is_bounced) {
179 /* We we're forced to use bounce buffer, copy it back */
180 if (batch->dir == USB_DIRECTION_IN)
181 memcpy(batch->original_buffer,
182 batch->dma_buffer.virt,
183 batch->transferred_size);
184
185 dma_buffer_free(&batch->dma_buffer);
186 } else {
187 dma_buffer_release(&batch->dma_buffer);
188 }
189 }
190
191 if (batch->on_complete) {
192 const int err = batch->on_complete(batch->on_complete_data, batch->error, batch->transferred_size);
193 if (err)
194 usb_log_warning("Batch %p failed to complete: %s",
195 batch, str_error(err));
196 }
197
198 usb_transfer_batch_destroy(batch);
199}
200
201/**
202 * @}
203 */
Note: See TracBrowser for help on using the repository browser.