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