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

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

usbhost: survive unregistering detached endpoint

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