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

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

usbhost: documentation & cleanup

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