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

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

usbhost: remove unused endpoint link

  • Property mode set to 100644
File size: 8.4 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>
[6ce42e85]45
[64fea02]46#include "usb_transfer_batch.h"
47#include "bus.h"
48
49#include "endpoint.h"
50
[1102eca]51/**
52 * Initialize provided endpoint structure.
[17412546]53 */
[9efad54]54void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_descriptors_t *desc)
[6ce42e85]55{
[41924f30]56 memset(ep, 0, sizeof(endpoint_t));
[a76b01b4]57
[6832245]58 assert(dev);
59 ep->device = dev;
60
[41924f30]61 atomic_set(&ep->refcnt, 0);
62 fibril_mutex_initialize(&ep->guard);
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;
76
77 ep->bandwidth = endpoint_count_bw(ep, ep->max_transfer_size);
[6832245]78}
79
[1102eca]80/**
81 * Get the bus endpoint belongs to.
82 */
[6832245]83static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
84{
85 return ep->device->bus->ops;
[6ce42e85]86}
[a76b01b4]87
[1102eca]88/**
89 * Increase the reference count on endpoint.
90 */
[41924f30]91void endpoint_add_ref(endpoint_t *ep)
[f527f58]92{
[41924f30]93 atomic_inc(&ep->refcnt);
[f527f58]94}
95
[1102eca]96/**
97 * Call the desctruction callback. Default behavior is to free the memory directly.
98 */
[6832245]99static inline void endpoint_destroy(endpoint_t *ep)
100{
101 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_destroy);
102 if (ops) {
103 ops->endpoint_destroy(ep);
104 } else {
105 assert(ep->active_batch == NULL);
106
107 /* Assume mostly the eps will be allocated by malloc. */
108 free(ep);
109 }
110}
111
[1102eca]112/**
113 * Decrease the reference count.
114 */
[41924f30]115void endpoint_del_ref(endpoint_t *ep)
[f527f58]116{
[41924f30]117 if (atomic_predec(&ep->refcnt) == 0) {
[6832245]118 endpoint_destroy(ep);
[41924f30]119 }
[3d932af6]120}
[a76b01b4]121
[56257ba]122static void endpoint_toggle_reset(endpoint_t *ep, toggle_reset_mode_t mode);
123
[0892663a]124/**
125 * Wait until the endpoint have no transfer scheduled.
126 */
127void endpoint_wait_timeout_locked(endpoint_t *ep, suseconds_t timeout)
128{
129 assert(fibril_mutex_is_locked(&ep->guard));
130
[92caadd]131 if (ep->active_batch != NULL)
132 fibril_condvar_wait_timeout(&ep->avail, &ep->guard, timeout);
133
134 while (timeout == 0 && ep->active_batch != NULL)
[0892663a]135 fibril_condvar_wait_timeout(&ep->avail, &ep->guard, timeout);
136}
137
[1102eca]138/**
139 * Mark the endpoint as active and block access for further fibrils. If the
140 * endpoint is already active, it will block on ep->avail condvar.
141 *
142 * Call only under endpoint guard. After you activate the endpoint and release
143 * the guard, you must assume that particular transfer is already finished/aborted.
144 *
[41924f30]145 * @param ep endpoint_t structure.
[1102eca]146 * @param batch Transfer batch this endpoint is bocked by.
[17412546]147 */
[17873ac7]148void endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
[6a32665d]149{
[41924f30]150 assert(ep);
[17873ac7]151 assert(batch);
152 assert(batch->ep == ep);
153
[0892663a]154 endpoint_wait_timeout_locked(ep, 0);
[17873ac7]155 ep->active_batch = batch;
[6a32665d]156}
[a76b01b4]157
[1102eca]158/**
159 * Mark the endpoint as inactive and allow access for further fibrils.
160 *
[41924f30]161 * @param ep endpoint_t structure.
[17412546]162 */
[17873ac7]163void endpoint_deactivate_locked(endpoint_t *ep)
[6a32665d]164{
[41924f30]165 assert(ep);
[17873ac7]166 assert(fibril_mutex_is_locked(&ep->guard));
167
168 if (ep->active_batch && ep->active_batch->error == EOK)
[56257ba]169 endpoint_toggle_reset(ep, ep->active_batch->toggle_reset_mode);
[17873ac7]170
171 ep->active_batch = NULL;
172 fibril_condvar_signal(&ep->avail);
173}
174
[1102eca]175/**
176 * The transfer on an endpoint can trigger a reset of the toggle bit. This
177 * function calls the respective bus callbacks to resolve it.
178 *
179 * @param ep The endpoint that triggered the reset
180 * @param mode Whether to reset no, one or all endpoints on a device.
181 */
[56257ba]182static void endpoint_toggle_reset(endpoint_t *ep, toggle_reset_mode_t mode)
[f567bcf]183{
[41924f30]184 assert(ep);
[17873ac7]185
[56257ba]186 if (mode == RESET_NONE)
187 return;
[a76b01b4]188
[56257ba]189 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_toggle_reset);
190 if (!ops)
191 return;
[17873ac7]192
[56257ba]193
194 if (mode == RESET_ALL) {
[1102eca]195 const device_t *dev = ep->device;
[56257ba]196 for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
197 if (dev->endpoints[i])
198 ops->endpoint_toggle_reset(dev->endpoints[i]);
199 }
200 } else {
201 ops->endpoint_toggle_reset(ep);
[41924f30]202 }
[f567bcf]203}
[f527f58]204
[1102eca]205/**
206 * Call the bus operation to count bandwidth.
207 *
208 * @param ep Endpoint on which the transfer will take place.
209 * @param size The payload size.
210 */
211ssize_t endpoint_count_bw(endpoint_t *ep, size_t size)
[6832245]212{
213 assert(ep);
214
215 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_count_bw);
216 if (!ops)
217 return 0;
218
[1102eca]219 return ops->endpoint_count_bw(ep, size);
[6832245]220}
221
[1102eca]222/**
223 * Initiate a transfer on an endpoint. Creates a transfer batch, checks the
224 * bandwidth requirements and schedules the batch.
225 *
226 * @param endpoint Endpoint for which to send the batch
227 * @param target The target of the transfer.
228 * @param direction A direction of the transfer.
229 * @param data A pointer to the data buffer.
230 * @param size Size of the data buffer.
231 * @param setup_data Data to use in the setup stage (Control communication type)
232 * @param on_complete Callback which is called after the batch is complete
[32fb6bce]233 * @param arg Callback parameter.
234 * @param name Communication identifier (for nicer output).
235 */
236int endpoint_send_batch(endpoint_t *ep, usb_target_t target,
237 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
238 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
239{
[a1732929]240 usb_log_debug2("%s %d:%d %zu(%zu).",
[32fb6bce]241 name, target.address, target.endpoint, size, ep->max_packet_size);
242
[9748336]243 device_t * const device = ep->device;
244 if (!device) {
245 usb_log_warning("Endpoint detached");
246 return EAGAIN;
247 }
248
249 const bus_ops_t *ops = BUS_OPS_LOOKUP(device->bus->ops, batch_schedule);
[32fb6bce]250 if (!ops) {
[a1732929]251 usb_log_error("HCD does not implement scheduler.");
[32fb6bce]252 return ENOTSUP;
253 }
254
[deb2e55]255 /* Offline devices don't schedule transfers other than on EP0. */
[9748336]256 if (!device->online && ep->endpoint > 0) {
[deb2e55]257 return EAGAIN;
258 }
259
[32fb6bce]260 const size_t bw = endpoint_count_bw(ep, size);
261 /* Check if we have enough bandwidth reserved */
262 if (ep->bandwidth < bw) {
263 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
264 "but only %zu is reserved.\n",
[9748336]265 device->address, ep->endpoint, name, bw, ep->bandwidth);
[32fb6bce]266 return ENOSPC;
267 }
268
269 usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
270 if (!batch) {
[a1732929]271 usb_log_error("Failed to create transfer batch.");
[32fb6bce]272 return ENOMEM;
273 }
274
275 batch->target = target;
276 batch->buffer = data;
277 batch->buffer_size = size;
278 batch->setup.packed = setup_data;
279 batch->dir = direction;
280 batch->on_complete = on_complete;
281 batch->on_complete_data = arg;
282
283 /* Check for commands that reset toggle bit */
284 if (ep->transfer_type == USB_TRANSFER_CONTROL)
285 batch->toggle_reset_mode
286 = hcd_get_request_toggle_reset_mode(&batch->setup.packet);
287
288 const int ret = ops->batch_schedule(batch);
289 if (ret != EOK) {
290 usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
291 usb_transfer_batch_destroy(batch);
292 }
293
294 return ret;
295}
296
[6ce42e85]297/**
298 * @}
299 */
Note: See TracBrowser for help on using the repository browser.