source: mainline/uspace/lib/usbhost/src/bus.c@ c901632

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

usbhost: refactoring

This commit moves interrupt, status and schedule to bus
operations. Then the purpose of hcd_t is better defined, and split into
hc_driver_t and hc_device_t. hc_driver_t is used to wrap driver
implementation by the library (similar to how usb_driver_t is used to
wrap usb device drivers). hc_device_t is used as a parent for hc_t
inside drivers, and is allocated inside the DDF device node.

To support these changes, some local identifiers were renamed, some
functions were moved and/or renamed and their arguments changed. The
most notable one being hcd_send_batch → bus_device_send_batch.

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[41924f30]1/*
2 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 *
34 */
35
[20eaa82]36#include <ddf/driver.h>
[41924f30]37#include <errno.h>
[64fea02]38#include <mem.h>
[20eaa82]39#include <stdio.h>
[64fea02]40#include <usb/debug.h>
41
42#include "endpoint.h"
43#include "bus.h"
[41924f30]44
45/**
46 * Initializes the bus structure.
47 */
[32fb6bce]48void bus_init(bus_t *bus, size_t device_size)
[41924f30]49{
[20eaa82]50 assert(bus);
51 assert(device_size >= sizeof(device_t));
[41924f30]52 memset(bus, 0, sizeof(bus_t));
53
54 fibril_mutex_initialize(&bus->guard);
[20eaa82]55 bus->device_size = device_size;
56}
57
[6832245]58int bus_device_init(device_t *dev, bus_t *bus)
[20eaa82]59{
[6832245]60 assert(bus);
61
[20eaa82]62 memset(dev, 0, sizeof(*dev));
63
[6832245]64 dev->bus = bus;
65
[20eaa82]66 link_initialize(&dev->link);
67 list_initialize(&dev->devices);
68 fibril_mutex_initialize(&dev->guard);
69
70 return EOK;
71}
72
[6832245]73int bus_device_set_default_name(device_t *dev)
[20eaa82]74{
75 assert(dev);
76 assert(dev->fun);
77
78 char buf[10] = { 0 }; /* usbxyz-ss */
79 snprintf(buf, sizeof(buf) - 1, "usb%u-%cs",
80 dev->address, usb_str_speed(dev->speed)[0]);
81
82 return ddf_fun_set_name(dev->fun, buf);
83}
84
[6832245]85int bus_device_enumerate(device_t *dev)
[20eaa82]86{
87 assert(dev);
88
[6832245]89 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_enumerate);
90 if (!ops)
[20eaa82]91 return ENOTSUP;
92
[6832245]93 return ops->device_enumerate(dev);
[20eaa82]94}
95
[6832245]96int bus_device_remove(device_t *dev)
[20eaa82]97{
98 assert(dev);
99
[6832245]100 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_remove);
101
102 if (!ops)
[20eaa82]103 return ENOTSUP;
104
[6832245]105 return ops->device_remove(dev);
[41924f30]106}
107
[6832245]108int bus_device_online(device_t *dev)
[d37514e]109{
110 assert(dev);
111
[6832245]112 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
113 if (!ops)
[d37514e]114 return ENOTSUP;
115
[6832245]116 return ops->device_online(dev);
[d37514e]117}
118
[6832245]119int bus_device_offline(device_t *dev)
[d37514e]120{
121 assert(dev);
122
[6832245]123 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
124 if (!ops)
[d37514e]125 return ENOTSUP;
126
[6832245]127 return ops->device_offline(dev);
[d37514e]128}
129
[6832245]130int bus_endpoint_add(device_t *device, const usb_endpoint_desc_t *desc, endpoint_t **out_ep)
[41924f30]131{
[6832245]132 int err;
[0206d35]133 assert(device);
[41924f30]134
[6832245]135 bus_t *bus = device->bus;
136
[a312d8f]137 if (desc->max_packet_size == 0 || desc->packets == 0) {
138 usb_log_warning("Invalid endpoint description (mps %zu, %u packets)", desc->max_packet_size, desc->packets);
139 return EINVAL;
140 }
141
[6832245]142 const bus_ops_t *create_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_create);
143 const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
144 if (!create_ops || !register_ops)
145 return ENOTSUP;
[53db806]146
[6832245]147 endpoint_t *ep = create_ops->endpoint_create(device, desc);
[0206d35]148 if (!ep)
[6832245]149 return ENOMEM;
[0206d35]150
[6832245]151 /* Temporary reference */
[0206d35]152 endpoint_add_ref(ep);
153
[6832245]154 fibril_mutex_lock(&bus->guard);
155 err = register_ops->endpoint_register(ep);
156 fibril_mutex_unlock(&bus->guard);
[0206d35]157
158 if (out_ep) {
[6832245]159 /* Exporting reference */
[41924f30]160 endpoint_add_ref(ep);
[0206d35]161 *out_ep = ep;
[41924f30]162 }
[0206d35]163
[6832245]164 /* Temporary reference */
[0206d35]165 endpoint_del_ref(ep);
166 return err;
[41924f30]167}
168
[0206d35]169/** Searches for an endpoint. Returns a reference.
170 */
[6832245]171endpoint_t *bus_find_endpoint(device_t *device, usb_target_t endpoint, usb_direction_t dir)
[41924f30]172{
[6832245]173 assert(device);
174
175 bus_t *bus = device->bus;
176
177 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, device_find_endpoint);
178 if (!ops)
179 return NULL;
[41924f30]180
181 fibril_mutex_lock(&bus->guard);
[6832245]182 endpoint_t *ep = ops->device_find_endpoint(device, endpoint, dir);
[0206d35]183 if (ep) {
184 /* Exporting reference */
185 endpoint_add_ref(ep);
186 }
[41924f30]187
[0206d35]188 fibril_mutex_unlock(&bus->guard);
189 return ep;
[41924f30]190}
191
[6832245]192int bus_endpoint_remove(endpoint_t *ep)
[41924f30]193{
194 assert(ep);
195
[6832245]196 bus_t *bus = endpoint_get_bus(ep);
197
198 const bus_ops_t *ops = BUS_OPS_LOOKUP(ep->device->bus->ops, endpoint_unregister);
199 if (!ops)
200 return ENOTSUP;
201
[41924f30]202 fibril_mutex_lock(&bus->guard);
[6832245]203 const int r = ops->endpoint_unregister(ep);
[41924f30]204 fibril_mutex_unlock(&bus->guard);
205
[4594baa]206 if (r)
207 return r;
208
[41924f30]209 /* Bus reference */
210 endpoint_del_ref(ep);
211
212 return EOK;
213}
214
[10cd715]215int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
[41924f30]216{
217 assert(bus);
218
[6832245]219 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, reserve_default_address);
220 if (!ops)
[41924f30]221 return ENOTSUP;
222
223 fibril_mutex_lock(&bus->guard);
[6832245]224 const int r = ops->reserve_default_address(bus, speed);
[41924f30]225 fibril_mutex_unlock(&bus->guard);
226 return r;
227}
228
[10cd715]229int bus_release_default_address(bus_t *bus)
[41924f30]230{
231 assert(bus);
232
[6832245]233 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, release_default_address);
234 if (!ops)
[41924f30]235 return ENOTSUP;
236
237 fibril_mutex_lock(&bus->guard);
[6832245]238 const int r = ops->release_default_address(bus);
[41924f30]239 fibril_mutex_unlock(&bus->guard);
240 return r;
241}
242
243int bus_reset_toggle(bus_t *bus, usb_target_t target, bool all)
244{
245 assert(bus);
246
[6832245]247 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, reset_toggle);
248 if (!ops)
[41924f30]249 return ENOTSUP;
250
251 fibril_mutex_lock(&bus->guard);
[6832245]252 const int r = ops->reset_toggle(bus, target, all);
[41924f30]253 fibril_mutex_unlock(&bus->guard);
254 return r;
255}
256
[32fb6bce]257/** Prepare generic usb_transfer_batch and schedule it.
258 * @param device Device for which to send the batch
259 * @param target address and endpoint number.
260 * @param setup_data Data to use in setup stage (Control communication type)
261 * @param in Callback for device to host communication.
262 * @param out Callback for host to device communication.
263 * @param arg Callback parameter.
264 * @param name Communication identifier (for nicer output).
265 * @return Error code.
266 */
267int bus_device_send_batch(device_t *device, usb_target_t target,
268 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
269 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
270{
271 assert(device->address == target.address);
272
273 /* Temporary reference */
274 endpoint_t *ep = bus_find_endpoint(device, target, direction);
275 if (ep == NULL) {
276 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
277 device->address, target.endpoint, name);
278 return ENOENT;
279 }
280
281 assert(ep->device == device);
282
283 const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data,
284 on_complete, arg, name);
285
286 /* Temporary reference */
287 endpoint_del_ref(ep);
288
289 return err;
290}
291
292typedef struct {
293 fibril_mutex_t done_mtx;
294 fibril_condvar_t done_cv;
295 unsigned done;
296
297 size_t transfered_size;
298 int error;
299} sync_data_t;
300
301static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
302{
303 sync_data_t *d = arg;
304 assert(d);
305 d->transfered_size = transfered_size;
306 d->error = error;
307 fibril_mutex_lock(&d->done_mtx);
308 d->done = 1;
309 fibril_condvar_broadcast(&d->done_cv);
310 fibril_mutex_unlock(&d->done_mtx);
311 return EOK;
312}
313
314ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
315 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
316 const char *name)
317{
318 sync_data_t sd = { .done = 0 };
319 fibril_mutex_initialize(&sd.done_mtx);
320 fibril_condvar_initialize(&sd.done_cv);
321
322 const int ret = bus_device_send_batch(device, target, direction,
323 data, size, setup_data,
324 sync_transfer_complete, &sd, name);
325 if (ret != EOK)
326 return ret;
327
328 fibril_mutex_lock(&sd.done_mtx);
329 while (!sd.done) {
330 fibril_condvar_wait_timeout(&sd.done_cv, &sd.done_mtx, 3000000);
331 if (!sd.done)
332 usb_log_debug2("Still waiting...");
333 }
334 fibril_mutex_unlock(&sd.done_mtx);
335
336 return (sd.error == EOK)
337 ? (ssize_t) sd.transfered_size
338 : (ssize_t) sd.error;
339}
340
[41924f30]341/**
342 * @}
343 */
Note: See TracBrowser for help on using the repository browser.