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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5dfb70c9 was deb2e55, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbhost: refactoring

Moved the "online" attribute from xhci_device_t to device_t. Changed
USB2 bus implementation to produce online devices (not to break
functionality on older buses).

  • Property mode set to 100644
File size: 7.1 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/host/hcd.h>
44
45#include "usb_transfer_batch.h"
46#include "bus.h"
47
48#include "endpoint.h"
49
50/** Initialize provided endpoint structure.
51 */
52void endpoint_init(endpoint_t *ep, device_t *dev, const usb_endpoint_desc_t *desc)
53{
54 memset(ep, 0, sizeof(endpoint_t));
55
56 assert(dev);
57 ep->device = dev;
58
59 atomic_set(&ep->refcnt, 0);
60 link_initialize(&ep->link);
61 fibril_mutex_initialize(&ep->guard);
62 fibril_condvar_initialize(&ep->avail);
63
64 ep->endpoint = desc->endpoint_no;
65 ep->direction = desc->direction;
66 ep->transfer_type = desc->transfer_type;
67 ep->max_packet_size = desc->max_packet_size;
68 ep->packets = desc->packets;
69
70 ep->bandwidth = endpoint_count_bw(ep, desc->max_packet_size);
71}
72
73static inline const bus_ops_t *get_bus_ops(endpoint_t *ep)
74{
75 return ep->device->bus->ops;
76}
77
78void endpoint_add_ref(endpoint_t *ep)
79{
80 atomic_inc(&ep->refcnt);
81}
82
83static inline void endpoint_destroy(endpoint_t *ep)
84{
85 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_destroy);
86 if (ops) {
87 ops->endpoint_destroy(ep);
88 } else {
89 assert(ep->active_batch == NULL);
90
91 /* Assume mostly the eps will be allocated by malloc. */
92 free(ep);
93 }
94}
95
96void endpoint_del_ref(endpoint_t *ep)
97{
98 if (atomic_predec(&ep->refcnt) == 0) {
99 endpoint_destroy(ep);
100 }
101}
102
103/** Mark the endpoint as active and block access for further fibrils.
104 * @param ep endpoint_t structure.
105 */
106void endpoint_activate_locked(endpoint_t *ep, usb_transfer_batch_t *batch)
107{
108 assert(ep);
109 assert(batch);
110 assert(batch->ep == ep);
111 assert(fibril_mutex_is_locked(&ep->guard));
112
113 while (ep->active_batch != NULL)
114 fibril_condvar_wait(&ep->avail, &ep->guard);
115 ep->active_batch = batch;
116}
117
118/** Mark the endpoint as inactive and allow access for further fibrils.
119 * @param ep endpoint_t structure.
120 */
121void endpoint_deactivate_locked(endpoint_t *ep)
122{
123 assert(ep);
124 assert(fibril_mutex_is_locked(&ep->guard));
125
126 if (ep->active_batch && ep->active_batch->error == EOK)
127 usb_transfer_batch_reset_toggle(ep->active_batch);
128
129 ep->active_batch = NULL;
130 fibril_condvar_signal(&ep->avail);
131}
132
133/** Abort an active batch on endpoint, if any.
134 *
135 * @param[in] ep endpoint_t structure.
136 */
137void endpoint_abort(endpoint_t *ep)
138{
139 assert(ep);
140
141 fibril_mutex_lock(&ep->guard);
142 usb_transfer_batch_t *batch = ep->active_batch;
143 endpoint_deactivate_locked(ep);
144 fibril_mutex_unlock(&ep->guard);
145
146 if (batch)
147 usb_transfer_batch_abort(batch);
148}
149
150/** Get the value of toggle bit. Either uses the toggle_get op, or just returns
151 * the value of the toggle.
152 * @param ep endpoint_t structure.
153 */
154int endpoint_toggle_get(endpoint_t *ep)
155{
156 assert(ep);
157
158 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_get_toggle);
159 return ops
160 ? ops->endpoint_get_toggle(ep)
161 : ep->toggle;
162}
163
164/** Set the value of toggle bit. Either uses the toggle_set op, or just sets
165 * the toggle inside.
166 * @param ep endpoint_t structure.
167 */
168void endpoint_toggle_set(endpoint_t *ep, bool toggle)
169{
170 assert(ep);
171
172 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_set_toggle);
173 if (ops) {
174 ops->endpoint_set_toggle(ep, toggle);
175 }
176 else {
177 ep->toggle = toggle;
178 }
179}
180
181ssize_t endpoint_count_bw(endpoint_t *ep, size_t packet_size)
182{
183 assert(ep);
184
185 const bus_ops_t *ops = BUS_OPS_LOOKUP(get_bus_ops(ep), endpoint_count_bw);
186 if (!ops)
187 return 0;
188
189 return ops->endpoint_count_bw(ep, packet_size);
190}
191
192/** Prepare generic usb_transfer_batch and schedule it.
193 * @param ep Endpoint for which the batch shall be created.
194 * @param target address and endpoint number.
195 * @param setup_data Data to use in setup stage (Control communication type)
196 * @param in Callback for device to host communication.
197 * @param out Callback for host to device communication.
198 * @param arg Callback parameter.
199 * @param name Communication identifier (for nicer output).
200 * @return Error code.
201 */
202int endpoint_send_batch(endpoint_t *ep, usb_target_t target,
203 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
204 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
205{
206 usb_log_debug2("%s %d:%d %zu(%zu).\n",
207 name, target.address, target.endpoint, size, ep->max_packet_size);
208
209 bus_t *bus = endpoint_get_bus(ep);
210 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, batch_schedule);
211 if (!ops) {
212 usb_log_error("HCD does not implement scheduler.\n");
213 return ENOTSUP;
214 }
215
216 /* Offline devices don't schedule transfers other than on EP0. */
217 if (!ep->device->online && ep->endpoint > 0) {
218 return EAGAIN;
219 }
220
221 const size_t bw = endpoint_count_bw(ep, size);
222 /* Check if we have enough bandwidth reserved */
223 if (ep->bandwidth < bw) {
224 usb_log_error("Endpoint(%d:%d) %s needs %zu bw "
225 "but only %zu is reserved.\n",
226 ep->device->address, ep->endpoint, name, bw, ep->bandwidth);
227 return ENOSPC;
228 }
229
230 usb_transfer_batch_t *batch = usb_transfer_batch_create(ep);
231 if (!batch) {
232 usb_log_error("Failed to create transfer batch.\n");
233 return ENOMEM;
234 }
235
236 batch->target = target;
237 batch->buffer = data;
238 batch->buffer_size = size;
239 batch->setup.packed = setup_data;
240 batch->dir = direction;
241 batch->on_complete = on_complete;
242 batch->on_complete_data = arg;
243
244 /* Check for commands that reset toggle bit */
245 if (ep->transfer_type == USB_TRANSFER_CONTROL)
246 batch->toggle_reset_mode
247 = hcd_get_request_toggle_reset_mode(&batch->setup.packet);
248
249 const int ret = ops->batch_schedule(batch);
250 if (ret != EOK) {
251 usb_log_warning("Batch %p failed to schedule: %s", batch, str_error(ret));
252 usb_transfer_batch_destroy(batch);
253 }
254
255 return ret;
256}
257
258/**
259 * @}
260 */
Note: See TracBrowser for help on using the repository browser.