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

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

usbhost: made device_remove and endpoint_unregister noexcept

  • Property mode set to 100644
File size: 10.4 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 *
[1102eca]34 * The Bus is a structure that serves as an interface of the HC driver
35 * implementation for the usbhost library. Every HC driver that uses libusbhost
36 * must use a bus_t (or its child), fill it with bus_ops and present it to the
37 * library. The library then handles the DDF interface and translates it to the
38 * bus callbacks.
[41924f30]39 */
40
[20eaa82]41#include <ddf/driver.h>
[41924f30]42#include <errno.h>
[64fea02]43#include <mem.h>
[20eaa82]44#include <stdio.h>
[64fea02]45#include <usb/debug.h>
46
47#include "endpoint.h"
48#include "bus.h"
[41924f30]49
50/**
[1102eca]51 * Initializes the base bus structure.
[41924f30]52 */
[32fb6bce]53void bus_init(bus_t *bus, size_t device_size)
[41924f30]54{
[20eaa82]55 assert(bus);
56 assert(device_size >= sizeof(device_t));
[41924f30]57 memset(bus, 0, sizeof(bus_t));
58
59 fibril_mutex_initialize(&bus->guard);
[20eaa82]60 bus->device_size = device_size;
[5e2b1ae6]61 bus->default_address_speed = USB_SPEED_MAX;
[20eaa82]62}
63
[1102eca]64/**
65 * Initialize the device_t structure belonging to a bus.
66 */
[6832245]67int bus_device_init(device_t *dev, bus_t *bus)
[20eaa82]68{
[6832245]69 assert(bus);
70
[20eaa82]71 memset(dev, 0, sizeof(*dev));
72
[6832245]73 dev->bus = bus;
74
[20eaa82]75 link_initialize(&dev->link);
76 list_initialize(&dev->devices);
77 fibril_mutex_initialize(&dev->guard);
78
79 return EOK;
80}
81
[1102eca]82/**
83 * Create a name of the ddf function node.
84 */
[6832245]85int bus_device_set_default_name(device_t *dev)
[20eaa82]86{
87 assert(dev);
88 assert(dev->fun);
89
90 char buf[10] = { 0 }; /* usbxyz-ss */
91 snprintf(buf, sizeof(buf) - 1, "usb%u-%cs",
92 dev->address, usb_str_speed(dev->speed)[0]);
93
94 return ddf_fun_set_name(dev->fun, buf);
95}
96
[1102eca]97/**
98 * Invoke the device_enumerate bus operation.
99 */
[6832245]100int bus_device_enumerate(device_t *dev)
[20eaa82]101{
102 assert(dev);
103
[6832245]104 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_enumerate);
105 if (!ops)
[20eaa82]106 return ENOTSUP;
107
[6832245]108 return ops->device_enumerate(dev);
[20eaa82]109}
110
[1102eca]111/**
112 * Invoke the device_remove bus operation.
113 */
[bad4a05]114void bus_device_remove(device_t *dev)
[20eaa82]115{
116 assert(dev);
117
[6832245]118 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_remove);
[bad4a05]119 assert(ops);
[20eaa82]120
[6832245]121 return ops->device_remove(dev);
[41924f30]122}
123
[1102eca]124/**
125 * Invoke the device_online bus operation.
126 */
[6832245]127int bus_device_online(device_t *dev)
[d37514e]128{
129 assert(dev);
130
[6832245]131 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_online);
132 if (!ops)
[d37514e]133 return ENOTSUP;
134
[6832245]135 return ops->device_online(dev);
[d37514e]136}
137
[1102eca]138/**
139 * Invoke the device_offline bus operation.
140 */
[6832245]141int bus_device_offline(device_t *dev)
[d37514e]142{
143 assert(dev);
144
[6832245]145 const bus_ops_t *ops = BUS_OPS_LOOKUP(dev->bus->ops, device_offline);
146 if (!ops)
[d37514e]147 return ENOTSUP;
148
[6832245]149 return ops->device_offline(dev);
[d37514e]150}
151
[1102eca]152/**
153 * Create and register new endpoint to the bus.
154 *
155 * @param[in] device The device of which the endpoint shall be created
156 * @param[in] desc Endpoint descriptors as reported by the device
157 * @param[out] out_ep The resulting new endpoint reference, if any. Can be NULL.
158 */
[9efad54]159int bus_endpoint_add(device_t *device, const usb_endpoint_descriptors_t *desc, endpoint_t **out_ep)
[41924f30]160{
[6832245]161 int err;
[0206d35]162 assert(device);
[41924f30]163
[6832245]164 bus_t *bus = device->bus;
165
166 const bus_ops_t *register_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_register);
[1102eca]167 if (!register_ops)
[6832245]168 return ENOTSUP;
[53db806]169
[1102eca]170 const bus_ops_t *create_ops = BUS_OPS_LOOKUP(bus->ops, endpoint_create);
171 endpoint_t *ep;
172 if (create_ops) {
173 ep = create_ops->endpoint_create(device, desc);
174 if (!ep)
175 return ENOMEM;
176 } else {
177 ep = calloc(1, sizeof(endpoint_t));
178 if (!ep)
179 return ENOMEM;
180 endpoint_init(ep, device, desc);
181 }
[0206d35]182
[9efad54]183 /* Bus reference */
[0206d35]184 endpoint_add_ref(ep);
185
[9efad54]186 if (ep->max_transfer_size == 0) {
187 usb_log_warning("Invalid endpoint description (mps %zu, "
188 "%u packets)", ep->max_packet_size, ep->packets_per_uframe);
189 /* Bus reference */
190 endpoint_del_ref(ep);
191 return EINVAL;
192 }
193
194 usb_log_debug("Register endpoint %d:%d %s-%s %zuB.\n",
195 device->address, ep->endpoint,
196 usb_str_transfer_type(ep->transfer_type),
197 usb_str_direction(ep->direction),
198 ep->max_transfer_size);
199
[a6c4597]200 fibril_mutex_lock(&device->guard);
[56257ba]201 if (!device->online && ep->endpoint != 0) {
202 err = EAGAIN;
203 } else if (device->endpoints[ep->endpoint] != NULL) {
204 err = EEXIST;
205 } else {
206 err = register_ops->endpoint_register(ep);
207 if (!err)
208 device->endpoints[ep->endpoint] = ep;
209 }
[a6c4597]210 fibril_mutex_unlock(&device->guard);
[56257ba]211 if (err) {
212 endpoint_del_ref(ep);
213 return err;
214 }
[0206d35]215
216 if (out_ep) {
[6832245]217 /* Exporting reference */
[41924f30]218 endpoint_add_ref(ep);
[0206d35]219 *out_ep = ep;
[41924f30]220 }
[0206d35]221
[56257ba]222 return EOK;
[41924f30]223}
224
[1102eca]225/**
226 * Search for an endpoint. Returns a reference.
[0206d35]227 */
[56257ba]228endpoint_t *bus_find_endpoint(device_t *device, usb_endpoint_t endpoint)
[41924f30]229{
[6832245]230 assert(device);
231
[a6c4597]232 fibril_mutex_lock(&device->guard);
[56257ba]233 endpoint_t *ep = device->endpoints[endpoint];
[0206d35]234 if (ep) {
235 /* Exporting reference */
236 endpoint_add_ref(ep);
237 }
[a6c4597]238 fibril_mutex_unlock(&device->guard);
[0206d35]239 return ep;
[41924f30]240}
241
[1102eca]242/**
243 * Remove an endpoint from the device. Consumes a reference.
244 */
[6832245]245int bus_endpoint_remove(endpoint_t *ep)
[41924f30]246{
247 assert(ep);
[9efad54]248 assert(ep->device);
[41924f30]249
[a6c4597]250 device_t *device = ep->device;
251 if (!device)
252 return ENOENT;
253
254 bus_t *bus = device->bus;
[6832245]255
[9efad54]256 const bus_ops_t *ops = BUS_OPS_LOOKUP(bus->ops, endpoint_unregister);
[6832245]257 if (!ops)
258 return ENOTSUP;
259
[9efad54]260 usb_log_debug("Unregister endpoint %d:%d %s-%s %zuB.\n",
[a6c4597]261 device->address, ep->endpoint,
[9efad54]262 usb_str_transfer_type(ep->transfer_type),
263 usb_str_direction(ep->direction),
264 ep->max_transfer_size);
265
[a6c4597]266 fibril_mutex_lock(&device->guard);
[bad4a05]267 ops->endpoint_unregister(ep);
268 device->endpoints[ep->endpoint] = NULL;
[a6c4597]269 fibril_mutex_unlock(&device->guard);
[41924f30]270
[94a0904]271 /* Abort a transfer batch, if there was any */
272 endpoint_abort(ep);
273
[41924f30]274 /* Bus reference */
275 endpoint_del_ref(ep);
276
[1102eca]277 /* Given reference */
278 endpoint_del_ref(ep);
279
[41924f30]280 return EOK;
281}
282
[1102eca]283/**
284 * Reserve the default address on the bus. Also, report the speed of the device
285 * that is listening on the default address.
286 *
287 * The speed is then used for devices enumerated while the address is reserved.
288 */
[10cd715]289int bus_reserve_default_address(bus_t *bus, usb_speed_t speed)
[41924f30]290{
291 assert(bus);
292
293 fibril_mutex_lock(&bus->guard);
[5e2b1ae6]294 if (bus->default_address_speed != USB_SPEED_MAX) {
295 fibril_mutex_unlock(&bus->guard);
296 return EAGAIN;
297 } else {
298 bus->default_address_speed = speed;
299 fibril_mutex_unlock(&bus->guard);
300 return EOK;
301 }
[41924f30]302}
303
[1102eca]304/**
305 * Release the default address.
306 */
[5e2b1ae6]307void bus_release_default_address(bus_t *bus)
[41924f30]308{
309 assert(bus);
[5e2b1ae6]310 bus->default_address_speed = USB_SPEED_MAX;
[41924f30]311}
312
[1102eca]313/**
314 * Initiate a transfer on the bus. Finds the target endpoint, creates
315 * a transfer batch and schedules it.
316 *
[32fb6bce]317 * @param device Device for which to send the batch
[1102eca]318 * @param target The target of the transfer.
319 * @param direction A direction of the transfer.
320 * @param data A pointer to the data buffer.
321 * @param size Size of the data buffer.
322 * @param setup_data Data to use in the setup stage (Control communication type)
323 * @param on_complete Callback which is called after the batch is complete
[32fb6bce]324 * @param arg Callback parameter.
325 * @param name Communication identifier (for nicer output).
326 * @return Error code.
327 */
328int bus_device_send_batch(device_t *device, usb_target_t target,
329 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
330 usbhc_iface_transfer_callback_t on_complete, void *arg, const char *name)
331{
332 assert(device->address == target.address);
333
334 /* Temporary reference */
[56257ba]335 endpoint_t *ep = bus_find_endpoint(device, target.endpoint);
[32fb6bce]336 if (ep == NULL) {
337 usb_log_error("Endpoint(%d:%d) not registered for %s.\n",
338 device->address, target.endpoint, name);
339 return ENOENT;
340 }
341
342 assert(ep->device == device);
343
344 const int err = endpoint_send_batch(ep, target, direction, data, size, setup_data,
345 on_complete, arg, name);
346
347 /* Temporary reference */
348 endpoint_del_ref(ep);
349
350 return err;
351}
352
353typedef struct {
354 fibril_mutex_t done_mtx;
355 fibril_condvar_t done_cv;
356 unsigned done;
357
358 size_t transfered_size;
359 int error;
360} sync_data_t;
361
[1102eca]362/**
363 * Callback for finishing the transfer. Wake the issuing thread.
364 */
[32fb6bce]365static int sync_transfer_complete(void *arg, int error, size_t transfered_size)
366{
367 sync_data_t *d = arg;
368 assert(d);
369 d->transfered_size = transfered_size;
370 d->error = error;
371 fibril_mutex_lock(&d->done_mtx);
372 d->done = 1;
373 fibril_condvar_broadcast(&d->done_cv);
374 fibril_mutex_unlock(&d->done_mtx);
375 return EOK;
376}
377
[1102eca]378/**
379 * Issue a transfer on the bus, wait for result.
380 *
381 * @param device Device for which to send the batch
382 * @param target The target of the transfer.
383 * @param direction A direction of the transfer.
384 * @param data A pointer to the data buffer.
385 * @param size Size of the data buffer.
386 * @param setup_data Data to use in the setup stage (Control communication type)
387 * @param name Communication identifier (for nicer output).
388 */
[32fb6bce]389ssize_t bus_device_send_batch_sync(device_t *device, usb_target_t target,
390 usb_direction_t direction, char *data, size_t size, uint64_t setup_data,
391 const char *name)
392{
393 sync_data_t sd = { .done = 0 };
394 fibril_mutex_initialize(&sd.done_mtx);
395 fibril_condvar_initialize(&sd.done_cv);
396
397 const int ret = bus_device_send_batch(device, target, direction,
398 data, size, setup_data,
399 sync_transfer_complete, &sd, name);
400 if (ret != EOK)
401 return ret;
402
403 fibril_mutex_lock(&sd.done_mtx);
404 while (!sd.done) {
[1102eca]405 fibril_condvar_wait(&sd.done_cv, &sd.done_mtx);
[32fb6bce]406 }
407 fibril_mutex_unlock(&sd.done_mtx);
408
409 return (sd.error == EOK)
410 ? (ssize_t) sd.transfered_size
411 : (ssize_t) sd.error;
412}
413
[41924f30]414/**
415 * @}
416 */
Note: See TracBrowser for help on using the repository browser.