source: mainline/uspace/lib/usbhost/src/endpoint.c@ fa4b12d5

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

usbhub: revert the runtime binding of bus methods

It was just a dead end.

  • Property mode set to 100644
File size: 7.8 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
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
30/** @addtogroup libusbhost
31 * @{
32 */
33/** @file
34 * @brief UHCI host controller driver structure
35 */
36
37#include <assert.h>
38#include <atomic.h>
39#include <mem.h>
40#include <stdlib.h>
41#include <str_error.h>
42#include <usb/debug.h>
43#include <usb/descriptor.h>
44#include <usb/host/hcd.h>
45#include <usb/host/utility.h>
46
47#include "usb_transfer_batch.h"
48#include "bus.h"
49
50#include "endpoint.h"
51
52/**
53 * Initialize provided endpoint structure.
54 */
55void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_descriptors_t *desc)
56{
57 memset(ep, 0, sizeof(endpoint_t));
58
59 assert(dev);
60 ep->device = dev;
61
62 atomic_set(&ep->refcnt, 0);
63 fibril_condvar_initialize(&ep->avail);
64
65 ep->endpoint = USB_ED_GET_EP(desc->endpoint);
66 ep->direction = USB_ED_GET_DIR(desc->endpoint);
67 ep->transfer_type = USB_ED_GET_TRANSFER_TYPE(desc->endpoint);
68 ep->max_packet_size = USB_ED_GET_MPS(desc->endpoint);
69 ep->packets_per_uframe = USB_ED_GET_ADD_OPPS(desc->endpoint) + 1;
70
71 /** Direction both is our construct never present in descriptors */
72 if (ep->transfer_type == USB_TRANSFER_CONTROL)
73 ep->direction = USB_DIRECTION_BOTH;
74
75 ep->max_transfer_size = ep->max_packet_size * ep->packets_per_uframe;
76}
77
78/**
79 * Get the bus endpoint belongs to.
80 */
81static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
82{
83 return ep->device->bus->ops;
84}
85
86/**
87 * Increase the reference count on endpoint.
88 */
89void endpoint_add_ref(endpoint_t *ep)
90{
91 atomic_inc(&ep->refcnt);
92}
93
94/**
95 * Call the desctruction callback. Default behavior is to free the memory directly.
96 */
97static inline void endpoint_destroy(endpoint_t *ep)
98{
99 const bus_ops_t *ops = get_bus_ops(ep);
100 if (ops->endpoint_destroy) {
101 ops->endpoint_destroy(ep);
102 } else {
103 assert(ep->active_batch == NULL);
104
105 /* Assume mostly the eps will be allocated by malloc. */
106 free(ep);
107 }
108}
109
110/**
111 * Decrease the reference count.
112 */
113void endpoint_del_ref(endpoint_t *ep)
114{
115 if (atomic_predec(&ep->refcnt) == 0) {
116 endpoint_destroy(ep);
117 }
118}
119
120/**
121 * Mark the endpoint as online. Supply a guard to be used for this endpoint
122 * synchronization.
123 */
124void endpoint_set_online(endpoint_t *ep, fibril_mutex_t *guard)
125{
126 ep->guard = guard;
127 ep->online = true;
128}
129
130/**
131 * Mark the endpoint as offline. All other fibrils waiting to activate this
132 * endpoint will be interrupted.
133 */
134void endpoint_set_offline_locked(endpoint_t *ep)
135{
136 assert(ep);
137 assert(fibril_mutex_is_locked(ep->guard));
138
139 ep->online = false;
140 fibril_condvar_broadcast(&ep->avail);
141}
142
143/**
144 * Wait until a transfer finishes. Can be used even when the endpoint is
145 * offline (and is interrupted by the endpoint going offline).
146 */
147void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t timeout)
148{
149 assert(ep);
150 assert(fibril_mutex_is_locked(ep->guard));
151
152 if (ep->active_batch == NULL)
153 return;
154
155 fibril_condvar_wait_timeout(&ep->avail, ep->guard, timeout);
156}
157
158/**
159 * Mark the endpoint as active and block access for further fibrils. If the
160 * endpoint is already active, it will block on ep->avail condvar.
161 *
162 * Call only under endpoint guard. After you activate the endpoint and release
163 * the guard, you must assume that particular transfer is already
164 * finished/aborted.
165 *
166 * Activation and deactivation is not done by the library to maximize
167 * performance. The HC might want to prepare some memory buffers prior to
168 * interfering with other world.
169 *
170 * @param batch Transfer batch this endpoint is blocked by.
171 */
172int endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
173{
174 assert(ep);
175 assert(batch);
176 assert(batch->ep == ep);
177 assert(ep->guard);
178 assert(fibril_mutex_is_locked(ep->guard));
179
180 while (ep->online && ep->active_batch != NULL)
181 fibril_condvar_wait(&ep->avail, ep->guard);
182
183 if (!ep->online)
184 return EINTR;
185
186 assert(ep->active_batch == NULL);
187 ep->active_batch = batch;
188 return EOK;
189}
190
191/**
192 * Mark the endpoint as inactive and allow access for further fibrils.
193 */
194void endpoint_deactivate_locked(endpoint_t *ep)
195{
196 assert(ep);
197 assert(fibril_mutex_is_locked(ep->guard));
198
199 ep->active_batch = NULL;
200 fibril_condvar_signal(&ep->avail);
201}
202
203/**
204 * Initiate a transfer on an endpoint. Creates a transfer batch, checks the
205 * bandwidth requirements and schedules the batch.
206 *
207 * @param endpoint Endpoint for which to send the batch
208 * @param target The target of the transfer.
209 * @param direction A direction of the transfer.
210 * @param data A pointer to the data buffer.
211 * @param size Size of the data buffer.
212 * @param setup_data Data to use in the setup stage (Control communication type)
213 * @param on_complete Callback which is called after the batch is complete
214 * @param arg Callback parameter.
215 * @param name Communication identifier (for nicer output).
216 */
217int endpoint_send_batch(endpoint_t *ep, usb_target_t target,
218 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
219 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
220{
221 if (!ep)
222 return EBADMEM;
223
224 if (ep->transfer_type == USB_TRANSFER_CONTROL) {
225 usb_log_debug("%s %d:%d %zu/%zuB, setup %#016" PRIx64, name,
226 target.address, target.endpoint, size, ep->max_packet_size,
227 setup_data);
228 } else {
229 usb_log_debug("%s %d:%d %zu/%zuB", name, target.address,
230 target.endpoint, size, ep->max_packet_size);
231 }
232
233 device_t * const device = ep->device;
234 if (!device) {
235 usb_log_warning("Endpoint detached");
236 return EAGAIN;
237 }
238
239 const bus_ops_t *ops = device->bus->ops;
240 if (!ops->batch_schedule) {
241 usb_log_error("HCD does not implement scheduler.");
242 return ENOTSUP;
243 }
244
245 /** Limit transfers with reserved bandwidth to the amount reserved */
246 if ((ep->transfer_type == USB_TRANSFER_INTERRUPT
247 || ep->transfer_type == USB_TRANSFER_ISOCHRONOUS)
248 && size > ep->max_transfer_size)
249 return ENOSPC;
250
251 /* Offline devices don't schedule transfers other than on EP0. */
252 if (!device->online && ep->endpoint > 0)
253 return EAGAIN;
254
255 usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
256 if (!batch) {
257 usb_log_error("Failed to create transfer batch.");
258 return ENOMEM;
259 }
260
261 batch->target = target;
262 batch->buffer = data;
263 batch->buffer_size = size;
264 batch->setup.packed = setup_data;
265 batch->dir = direction;
266 batch->on_complete = on_complete;
267 batch->on_complete_data = arg;
268
269 const int ret = ops->batch_schedule(batch);
270 if (ret != EOK) {
271 usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
272 usb_transfer_batch_destroy(batch);
273 }
274
275 return ret;
276}
277
278/**
279 * @}
280 */
Note: See TracBrowser for help on using the repository browser.