source: mainline/uspace/lib/usbhost/src/usb_transfer_batch.c@ 239eea41

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 239eea41 was c21e6a5, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

usbhost: prepare buffers for transfers in library

  • Property mode set to 100644
File size: 5.2 KB
Line 
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 <assert.h>
36#include <errno.h>
37#include <stdlib.h>
38#include <str_error.h>
39#include <usb/debug.h>
40
41#include "endpoint.h"
42#include "bus.h"
43
44#include "usb_transfer_batch.h"
45
46/**
47 * Create a batch on a given endpoint.
48 *
49 * If the bus callback is not defined, it just creates a default batch.
50 */
51usb_transfer_batch_t *usb_transfer_batch_create(endpoint_t *ep)
52{
53 assert(ep);
54
55 bus_t *bus = endpoint_get_bus(ep);
56
57 if (!bus->ops->batch_create) {
58 usb_transfer_batch_t *batch = calloc(1, sizeof(usb_transfer_batch_t));
59 if (!batch)
60 return NULL;
61 usb_transfer_batch_init(batch, ep);
62 return batch;
63 }
64
65 return bus->ops->batch_create(ep);
66}
67
68/**
69 * Initialize given batch structure.
70 */
71void usb_transfer_batch_init(usb_transfer_batch_t *batch, endpoint_t *ep)
72{
73 assert(ep);
74 /* Batch reference */
75 endpoint_add_ref(ep);
76 batch->ep = ep;
77}
78
79/**
80 * Destroy the batch. If there's no bus callback, just free it.
81 */
82void usb_transfer_batch_destroy(usb_transfer_batch_t *batch)
83{
84 assert(batch);
85 assert(batch->ep);
86
87 bus_t *bus = endpoint_get_bus(batch->ep);
88 endpoint_t *ep = batch->ep;
89
90 if (bus->ops) {
91 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " destroying.",
92 batch, USB_TRANSFER_BATCH_ARGS(*batch));
93 bus->ops->batch_destroy(batch);
94 }
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
105/**
106 * Prepare a DMA buffer according to endpoint policy.
107 *
108 * If the buffer is suitable to be used directly, it is. Otherwise, a bounce
109 * buffer is created.
110 */
111errno_t usb_transfer_batch_prepare_buffer(
112 usb_transfer_batch_t *batch, char *buf)
113{
114 const dma_policy_t policy = batch->ep->transfer_buffer_policy;
115
116 /* Empty transfers do not need a buffer */
117 if (batch->buffer_size == 0)
118 return EOK;
119
120 if (dma_buffer_check_policy(buf, batch->buffer_size, policy)) {
121 /* Mark this case with invalid address */
122 batch->original_buffer = NULL;
123
124 /* Fill the buffer with virtual address and lock it for DMA */
125 return dma_buffer_lock(&batch->dma_buffer, buf, batch->buffer_size);
126 }
127 else {
128 usb_log_debug("Batch(%p): Buffer cannot be used directly, "
129 "falling back to bounce buffer!", batch);
130 const errno_t err = dma_buffer_alloc_policy(&batch->dma_buffer,
131 batch->buffer_size, policy);
132
133 /* Copy the data out */
134 if (!err && batch->dir == USB_DIRECTION_OUT)
135 memcpy(batch->dma_buffer.virt, buf, batch->buffer_size);
136
137 /* Store the buffer to store the data back before finishing */
138 batch->original_buffer = buf;
139
140 return err;
141 }
142}
143
144/**
145 * Finish a transfer batch: call handler, destroy batch, release endpoint.
146 *
147 * Call only after the batch have been scheduled && completed!
148 */
149void usb_transfer_batch_finish(usb_transfer_batch_t *batch)
150{
151 assert(batch);
152 assert(batch->ep);
153
154 usb_log_debug2("Batch %p " USB_TRANSFER_BATCH_FMT " finishing.",
155 batch, USB_TRANSFER_BATCH_ARGS(*batch));
156
157 if (batch->error == EOK && batch->buffer_size > 0) {
158 if (batch->original_buffer == NULL) {
159 /* Unlock the buffer for DMA */
160 dma_buffer_unlock(&batch->dma_buffer,
161 batch->buffer_size);
162 }
163 else {
164 /* We we're forced to use bounce buffer, copy it back */
165 if (batch->dir == USB_DIRECTION_IN)
166 memcpy(batch->original_buffer,
167 batch->dma_buffer.virt,
168 batch->transferred_size);
169
170 dma_buffer_free(&batch->dma_buffer);
171 }
172 }
173
174 if (batch->on_complete) {
175 const int err = batch->on_complete(batch->on_complete_data, batch->error, batch->transferred_size);
176 if (err)
177 usb_log_warning("Batch %p failed to complete: %s",
178 batch, str_error(err));
179 }
180
181 usb_transfer_batch_destroy(batch);
182}
183
184/**
185 * @}
186 */
Note: See TracBrowser for help on using the repository browser.