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