source: mainline/uspace/lib/usbhost/src/ddf_helpers.c@ eb862fd

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

usbhost: fix reference leak

  • Property mode set to 100644
File size: 12.7 KB
Line 
1/*
2 * Copyright (c) 2013 Jan Vesely
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 * Helpers to work with the DDF interface.
34 */
35
36#include <adt/list.h>
37#include <assert.h>
38#include <async.h>
39#include <ddf/driver.h>
40#include <ddf/interrupt.h>
41#include <device/hw_res_parsed.h>
42#include <errno.h>
43#include <str_error.h>
44#include <usb/classes/classes.h>
45#include <usb/debug.h>
46#include <usb/descriptor.h>
47#include <usb/usb.h>
48#include <usb_iface.h>
49#include <usbhc_iface.h>
50
51#include "bus.h"
52#include "endpoint.h"
53
54#include "ddf_helpers.h"
55
56/**
57 * DDF usbhc_iface callback. Passes the endpoint descriptors, fills the pipe
58 * descriptor according to the contents of the endpoint.
59 *
60 * @param[in] fun DDF function of the device in question.
61 * @param[out] pipe_desc The pipe descriptor to be filled.
62 * @param[in] endpoint_desc Endpoint descriptors from the device.
63 * @return Error code.
64 */
65static errno_t register_endpoint(ddf_fun_t *fun, usb_pipe_desc_t *pipe_desc,
66 const usb_endpoint_descriptors_t *ep_desc)
67{
68 assert(fun);
69 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
70 device_t *dev = ddf_fun_data_get(fun);
71 assert(hcd);
72 assert(hcd->bus);
73 assert(dev);
74
75 endpoint_t *ep;
76 const int err = bus_endpoint_add(dev, ep_desc, &ep);
77 if (err)
78 return err;
79
80 if (pipe_desc) {
81 pipe_desc->endpoint_no = ep->endpoint;
82 pipe_desc->direction = ep->direction;
83 pipe_desc->transfer_type = ep->transfer_type;
84 pipe_desc->max_transfer_size = ep->max_transfer_size;
85 }
86 endpoint_del_ref(ep);
87
88 return EOK;
89}
90
91 /**
92 * DDF usbhc_iface callback. Unregister endpoint that makes the other end of
93 * the pipe described.
94 *
95 * @param fun DDF function of the device in question.
96 * @param pipe_desc Pipe description.
97 * @return Error code.
98 */
99static errno_t unregister_endpoint(ddf_fun_t *fun, const usb_pipe_desc_t *pipe_desc)
100{
101 assert(fun);
102 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
103 device_t *dev = ddf_fun_data_get(fun);
104 assert(hcd);
105 assert(hcd->bus);
106 assert(dev);
107
108 endpoint_t *ep = bus_find_endpoint(dev, pipe_desc->endpoint_no, pipe_desc->direction);
109 if (!ep)
110 return ENOENT;
111
112 const errno_t err = bus_endpoint_remove(ep);
113
114 endpoint_del_ref(ep);
115 return err;
116}
117
118/**
119 * DDF usbhc_iface callback. Calls the respective bus operation directly.
120 *
121 * @param fun DDF function of the device (hub) requesting the address.
122 */
123static errno_t default_address_reservation(ddf_fun_t *fun, bool reserve)
124{
125 assert(fun);
126 hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
127 device_t *dev = ddf_fun_data_get(fun);
128 assert(hcd);
129 assert(hcd->bus);
130 assert(dev);
131
132 usb_log_debug("Device %d %s default address", dev->address, reserve ? "requested" : "releasing");
133 if (reserve) {
134 return bus_reserve_default_address(hcd->bus, dev);
135 } else {
136 bus_release_default_address(hcd->bus, dev);
137 return EOK;
138 }
139}
140
141/**
142 * DDF usbhc_iface callback. Calls the bus operation directly.
143 *
144 * @param fun DDF function of the device (hub) requesting the address.
145 * @param speed USB speed of the new device
146 */
147static errno_t device_enumerate(ddf_fun_t *fun, unsigned port, usb_speed_t speed)
148{
149 assert(fun);
150 ddf_dev_t *hc = ddf_fun_get_dev(fun);
151 assert(hc);
152 hc_device_t *hcd = dev_to_hcd(hc);
153 assert(hcd);
154 device_t *hub = ddf_fun_data_get(fun);
155 assert(hub);
156
157 errno_t err;
158
159 if (!usb_speed_is_valid(speed))
160 return EINVAL;
161
162 usb_log_debug("Hub %d reported a new %s speed device on port: %u",
163 hub->address, usb_str_speed(speed), port);
164
165 device_t *dev = hcd_ddf_fun_create(hcd, speed);
166 if (!dev) {
167 usb_log_error("Failed to create USB device function.");
168 return ENOMEM;
169 }
170
171 dev->hub = hub;
172 dev->tier = hub->tier + 1;
173 dev->port = port;
174 dev->speed = speed;
175
176 if ((err = bus_device_enumerate(dev))) {
177 usb_log_error("Failed to initialize USB dev memory structures.");
178 goto err_usb_dev;
179 }
180
181 /* If the driver didn't name the dev when enumerating,
182 * do it in some generic way.
183 */
184 if (!ddf_fun_get_name(dev->fun)) {
185 bus_device_set_default_name(dev);
186 }
187
188 if ((err = ddf_fun_bind(dev->fun))) {
189 usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
190 goto err_usb_dev;
191 }
192
193 return EOK;
194
195err_usb_dev:
196 hcd_ddf_fun_destroy(dev);
197 return err;
198}
199
200static errno_t device_remove(ddf_fun_t *fun, unsigned port)
201{
202 assert(fun);
203 device_t *hub = ddf_fun_data_get(fun);
204 assert(hub);
205 usb_log_debug("Hub `%s' reported removal of device on port %u",
206 ddf_fun_get_name(fun), port);
207
208 device_t *victim = NULL;
209
210 fibril_mutex_lock(&hub->guard);
211 list_foreach(hub->devices, link, device_t, it) {
212 if (it->port == port) {
213 victim = it;
214 break;
215 }
216 }
217 fibril_mutex_unlock(&hub->guard);
218
219 if (!victim) {
220 usb_log_warning("Hub '%s' tried to remove non-existent"
221 " device.", ddf_fun_get_name(fun));
222 return ENOENT;
223 }
224
225 assert(victim->fun);
226 assert(victim->port == port);
227 assert(victim->hub == hub);
228
229 bus_device_gone(victim);
230 return EOK;
231}
232
233/**
234 * Gets description of the device that is calling.
235 *
236 * @param[in] fun Device function.
237 * @param[out] desc Device descriptor to be filled.
238 * @return Error code.
239 */
240static errno_t get_device_description(ddf_fun_t *fun, usb_device_desc_t *desc)
241{
242 assert(fun);
243 device_t *dev = ddf_fun_data_get(fun);
244 assert(dev);
245
246 if (!desc)
247 return EOK;
248
249 *desc = (usb_device_desc_t) {
250 .address = dev->address,
251 .depth = dev->tier,
252 .speed = dev->speed,
253 .handle = ddf_fun_get_handle(fun),
254 .iface = -1,
255 };
256 return EOK;
257}
258
259/**
260 * Transfer issuing interface function.
261 *
262 * @param fun DDF function.
263 * @param target Communication target.
264 * @param dir Communication direction.
265 * @param setup_data Data to use in setup stage (control transfers).
266 * @param data Pointer to data buffer.
267 * @param size Size of the data buffer.
268 * @param callback Function to call on communication end.
269 * @param arg Argument passed to the callback function.
270 * @return Error code.
271 */
272static errno_t transfer(ddf_fun_t *fun, usb_target_t target,
273 usb_direction_t dir, uint64_t setup_data, char *data, size_t size,
274 usbhc_iface_transfer_callback_t callback, void *arg)
275{
276 assert(fun);
277 device_t *dev = ddf_fun_data_get(fun);
278 assert(dev);
279
280 target.address = dev->address;
281
282 if (!usb_target_is_valid(&target))
283 return EINVAL;
284
285 if (size > 0 && data == NULL)
286 return EBADMEM;
287
288 if (!callback && arg)
289 return EBADMEM;
290
291 const char *name = (dir == USB_DIRECTION_IN) ? "READ" : "WRITE";
292
293 return bus_device_send_batch(dev, target, dir,
294 (char *) data, size, setup_data,
295 callback, arg, name);
296}
297
298/** USB device interface */
299static usb_iface_t usb_iface = {
300 .get_my_description = get_device_description,
301};
302
303/** USB host controller interface */
304static usbhc_iface_t usbhc_iface = {
305 .default_address_reservation = default_address_reservation,
306
307 .device_enumerate = device_enumerate,
308 .device_remove = device_remove,
309
310 .register_endpoint = register_endpoint,
311 .unregister_endpoint = unregister_endpoint,
312
313 .transfer = transfer,
314};
315
316/** Standard USB device interface) */
317static ddf_dev_ops_t usb_ops = {
318 .interfaces[USB_DEV_IFACE] = &usb_iface,
319 .interfaces[USBHC_DEV_IFACE] = &usbhc_iface,
320};
321
322
323/* DDF HELPERS */
324
325#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
326do { \
327 match_id_t *mid = malloc(sizeof(match_id_t)); \
328 if (!mid) { \
329 clean_match_ids(list); \
330 return ENOMEM; \
331 } \
332 char *id = NULL; \
333 int ret = asprintf(&id, str, ##__VA_ARGS__); \
334 if (ret < 0) { \
335 clean_match_ids(list); \
336 free(mid); \
337 return ENOMEM; \
338 } \
339 mid->score = sc; \
340 mid->id = id; \
341 add_match_id(list, mid); \
342} while (0)
343
344/* This is a copy of lib/usbdev/src/recognise.c */
345static errno_t create_match_ids(match_id_list_t *l,
346 usb_standard_device_descriptor_t *d)
347{
348 assert(l);
349 assert(d);
350
351 if (d->vendor_id != 0) {
352 /* First, with release number. */
353 ADD_MATCHID_OR_RETURN(l, 100,
354 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
355 d->vendor_id, d->product_id, (d->device_version >> 8),
356 (d->device_version & 0xff));
357
358 /* Next, without release number. */
359 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
360 d->vendor_id, d->product_id);
361 }
362
363 /* Class match id */
364 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
365 usb_str_class(d->device_class));
366
367 /* As a last resort, try fallback driver. */
368 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
369
370 return EOK;
371}
372
373device_t *hcd_ddf_fun_create(hc_device_t *hc, usb_speed_t speed)
374{
375 /* Create DDF function for the new device */
376 ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
377 if (!fun)
378 return NULL;
379
380 ddf_fun_set_ops(fun, &usb_ops);
381
382 /* Create USB device node for the new device */
383 device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
384 if (!dev) {
385 ddf_fun_destroy(fun);
386 return NULL;
387 }
388
389 bus_device_init(dev, hc->bus);
390 dev->fun = fun;
391 dev->speed = speed;
392 return dev;
393}
394
395void hcd_ddf_fun_destroy(device_t *dev)
396{
397 assert(dev);
398 assert(dev->fun);
399 ddf_fun_destroy(dev->fun);
400}
401
402errno_t hcd_ddf_setup_match_ids(device_t *device, usb_standard_device_descriptor_t *desc)
403{
404 errno_t err;
405 match_id_list_t mids;
406
407 init_match_ids(&mids);
408
409 /* Create match ids from the device descriptor */
410 usb_log_debug("Device(%d): Creating match IDs.", device->address);
411 if ((err = create_match_ids(&mids, desc))) {
412 return err;
413 }
414
415 list_foreach(mids.ids, link, const match_id_t, mid) {
416 ddf_fun_add_match_id(device->fun, mid->id, mid->score);
417 }
418
419 return EOK;
420}
421
422/** Initialize hc structures.
423 *
424 * @param[in] device DDF instance of the device to use.
425 * @param[in] max_speed Maximum supported USB speed.
426 * @param[in] bw available bandwidth.
427 * @param[in] bw_count Function to compute required ep bandwidth.
428 *
429 * @return Error code.
430 * This function does all the ddf work for hc driver.
431 */
432errno_t hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
433{
434 assert(device);
435
436 hc_device_t *instance = ddf_dev_data_alloc(device, size);
437 if (instance == NULL) {
438 usb_log_error("Failed to allocate HCD ddf structure.");
439 return ENOMEM;
440 }
441 instance->ddf_dev = device;
442
443 errno_t ret = ENOMEM;
444 instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
445 if (!instance->ctl_fun) {
446 usb_log_error("Failed to create HCD ddf fun.");
447 goto err_destroy_fun;
448 }
449
450 ret = ddf_fun_bind(instance->ctl_fun);
451 if (ret != EOK) {
452 usb_log_error("Failed to bind ctl_fun: %s.", str_error(ret));
453 goto err_destroy_fun;
454 }
455
456 ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
457 if (ret != EOK) {
458 usb_log_error("Failed to add fun to category: %s.",
459 str_error(ret));
460 ddf_fun_unbind(instance->ctl_fun);
461 goto err_destroy_fun;
462 }
463
464 /* HC should be ok at this point (except it can't do anything) */
465 return EOK;
466
467err_destroy_fun:
468 ddf_fun_destroy(instance->ctl_fun);
469 instance->ctl_fun = NULL;
470 return ret;
471}
472
473void hcd_ddf_clean_hc(hc_device_t *hcd)
474{
475 if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
476 ddf_fun_destroy(hcd->ctl_fun);
477}
478
479/** Call the parent driver with a request to enable interrupt
480 *
481 * @param[in] device Device asking for interrupts
482 * @param[in] inum Interrupt number
483 * @return Error code.
484 */
485errno_t hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
486{
487 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
488 if (parent_sess == NULL)
489 return EIO;
490
491 return hw_res_enable_interrupt(parent_sess, inum);
492}
493
494errno_t hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
495{
496 async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
497 if (parent_sess == NULL)
498 return EIO;
499
500 hw_res_list_parsed_init(hw_res);
501 const errno_t ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
502 if (ret != EOK)
503 hw_res_list_parsed_clean(hw_res);
504 return ret;
505}
506
507/**
508 * @}
509 */
Note: See TracBrowser for help on using the repository browser.