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

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

usbhost: prepare bandwidth accounting privatization to usb2_bus

The endpoint does not account the bandwidth by default. The next step is
moving the count_bw callback to the usb2 bus.

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[6ce42e85]1/*
2 * Copyright (c) 2011 Jan Vesely
[41924f30]3 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
[6ce42e85]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 */
[f527f58]29
[17412546]30/** @addtogroup libusbhost
[6ce42e85]31 * @{
32 */
33/** @file
34 * @brief UHCI host controller driver structure
35 */
36
[6a32665d]37#include <assert.h>
[f527f58]38#include <atomic.h>
[41924f30]39#include <mem.h>
40#include <stdlib.h>
[32fb6bce]41#include <str_error.h>
42#include <usb/debug.h>
[9efad54]43#include <usb/descriptor.h>
[32fb6bce]44#include <usb/host/hcd.h>
[944f8fdd]45#include <usb/host/utility.h>
[6ce42e85]46
[64fea02]47#include "usb_transfer_batch.h"
48#include "bus.h"
49
50#include "endpoint.h"
51
[1102eca]52/**
53 * Initialize provided endpoint structure.
[17412546]54 */
[9efad54]55void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_descriptors_t *desc)
[6ce42e85]56{
[41924f30]57 memset(ep, 0, sizeof(endpoint_t));
[a76b01b4]58
[6832245]59 assert(dev);
60 ep->device = dev;
61
[41924f30]62 atomic_set(&ep->refcnt, 0);
63 fibril_condvar_initialize(&ep->avail);
[6832245]64
[9efad54]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;
[6832245]70
[9efad54]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;
[6832245]76}
77
[1102eca]78/**
79 * Get the bus endpoint belongs to.
80 */
[6832245]81static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
82{
83 return ep->device->bus->ops;
[6ce42e85]84}
[a76b01b4]85
[1102eca]86/**
87 * Increase the reference count on endpoint.
88 */
[41924f30]89void endpoint_add_ref(endpoint_t *ep)
[f527f58]90{
[41924f30]91 atomic_inc(&ep->refcnt);
[f527f58]92}
93
[1102eca]94/**
95 * Call the desctruction callback. Default behavior is to free the memory directly.
96 */
[6832245]97static inline void endpoint_destroy(endpoint_t *ep)
98{
99 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_destroy);
100 if (ops) {
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
[1102eca]110/**
111 * Decrease the reference count.
112 */
[41924f30]113void endpoint_del_ref(endpoint_t *ep)
[f527f58]114{
[41924f30]115 if (atomic_predec(&ep->refcnt) == 0) {
[6832245]116 endpoint_destroy(ep);
[41924f30]117 }
[3d932af6]118}
[a76b01b4]119
[0892663a]120/**
[4db49344]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).
[0892663a]146 */
147void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t timeout)
148{
[4db49344]149 assert(ep);
150 assert(fibril_mutex_is_locked(ep->guard));
[0892663a]151
[4db49344]152 if (ep->active_batch == NULL)
153 return;
[92caadd]154
[4db49344]155 fibril_condvar_wait_timeout(&ep->avail, ep->guard, timeout);
[0892663a]156}
157
[1102eca]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
[4db49344]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.
[1102eca]169 *
[4db49344]170 * @param batch Transfer batch this endpoint is blocked by.
[17412546]171 */
[4db49344]172int endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
[6a32665d]173{
[41924f30]174 assert(ep);
[17873ac7]175 assert(batch);
176 assert(batch->ep == ep);
[4db49344]177 assert(ep->guard);
178 assert(fibril_mutex_is_locked(ep->guard));
[17873ac7]179
[4db49344]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);
[17873ac7]187 ep->active_batch = batch;
[4db49344]188 return EOK;
[6a32665d]189}
[a76b01b4]190
[1102eca]191/**
192 * Mark the endpoint as inactive and allow access for further fibrils.
[17412546]193 */
[17873ac7]194void endpoint_deactivate_locked(endpoint_t *ep)
[6a32665d]195{
[41924f30]196 assert(ep);
[4db49344]197 assert(fibril_mutex_is_locked(ep->guard));
198
[17873ac7]199 ep->active_batch = NULL;
200 fibril_condvar_signal(&ep->avail);
201}
202
[1102eca]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
[32fb6bce]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{
[defaab2]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 }
[32fb6bce]232
[9748336]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 = BUS_OPS_LOOKUP(device->bus->ops, batch_schedule);
[32fb6bce]240 if (!ops) {
[a1732929]241 usb_log_error("HCD does not implement scheduler.");
[32fb6bce]242 return ENOTSUP;
243 }
244
[5f0b366]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
[deb2e55]251 /* Offline devices don't schedule transfers other than on EP0. */
[5f0b366]252 if (!device->online && ep->endpoint > 0)
[deb2e55]253 return EAGAIN;
[32fb6bce]254
255 usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
256 if (!batch) {
[a1732929]257 usb_log_error("Failed to create transfer batch.");
[32fb6bce]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
[6ce42e85]278/**
279 * @}
280 */
Note: See TracBrowser for help on using the repository browser.