[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 | /**
|
---|
| 116 | * DDF usbhc_iface callback. Calls the bus operation directly.
|
---|
| 117 | *
|
---|
| 118 | * @param fun DDF function of the device (hub) requesting the address.
|
---|
| 119 | */
|
---|
[eeca8a6] | 120 | static int reserve_default_address(ddf_fun_t *fun)
|
---|
[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 |
|
---|
[eeca8a6] | 129 | usb_log_debug("Device %d requested default address", dev->address);
|
---|
| 130 | return bus_reserve_default_address(hcd->bus, dev);
|
---|
[56bd6f11] | 131 | }
|
---|
| 132 |
|
---|
[1102eca] | 133 | /**
|
---|
| 134 | * DDF usbhc_iface callback. Calls the bus operation directly.
|
---|
| 135 | *
|
---|
| 136 | * @param fun DDF function of the device (hub) releasing the address.
|
---|
| 137 | */
|
---|
[56bd6f11] | 138 | static int release_default_address(ddf_fun_t *fun)
|
---|
| 139 | {
|
---|
| 140 | assert(fun);
|
---|
[32fb6bce] | 141 | hc_device_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
[20eaa82] | 142 | device_t *dev = ddf_fun_data_get(fun);
|
---|
[56bd6f11] | 143 | assert(hcd);
|
---|
[20eaa82] | 144 | assert(hcd->bus);
|
---|
[56bd6f11] | 145 | assert(dev);
|
---|
| 146 |
|
---|
[a1732929] | 147 | usb_log_debug("Device %d released default address", dev->address);
|
---|
[eeca8a6] | 148 | bus_release_default_address(hcd->bus, dev);
|
---|
[5e2b1ae6] | 149 |
|
---|
| 150 | return EOK;
|
---|
[56bd6f11] | 151 | }
|
---|
| 152 |
|
---|
[1102eca] | 153 | /**
|
---|
| 154 | * DDF usbhc_iface callback. Calls the bus operation directly.
|
---|
| 155 | *
|
---|
| 156 | * @param fun DDF function of the device (hub) requesting the address.
|
---|
[eeca8a6] | 157 | * @param speed USB speed of the new device
|
---|
[1102eca] | 158 | */
|
---|
[eeca8a6] | 159 | static int device_enumerate(ddf_fun_t *fun, unsigned port, usb_speed_t speed)
|
---|
[56bd6f11] | 160 | {
|
---|
| 161 | assert(fun);
|
---|
[867b375] | 162 | ddf_dev_t *hc = ddf_fun_get_dev(fun);
|
---|
| 163 | assert(hc);
|
---|
[32fb6bce] | 164 | hc_device_t *hcd = dev_to_hcd(hc);
|
---|
[867b375] | 165 | assert(hcd);
|
---|
[20eaa82] | 166 | device_t *hub = ddf_fun_data_get(fun);
|
---|
[867b375] | 167 | assert(hub);
|
---|
| 168 |
|
---|
[c952abc4] | 169 | int err;
|
---|
| 170 |
|
---|
[eeca8a6] | 171 | usb_log_debug("Hub %d reported a new %s device on port: %u",
|
---|
| 172 | hub->address, usb_str_speed(speed), port);
|
---|
[c952abc4] | 173 |
|
---|
[eeca8a6] | 174 | device_t *dev = hcd_ddf_fun_create(hcd, speed);
|
---|
[c952abc4] | 175 | if (!dev) {
|
---|
| 176 | usb_log_error("Failed to create USB device function.");
|
---|
| 177 | return ENOMEM;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | dev->hub = hub;
|
---|
| 181 | dev->port = port;
|
---|
[eeca8a6] | 182 | dev->speed = speed;
|
---|
[c952abc4] | 183 |
|
---|
| 184 | if ((err = bus_device_enumerate(dev))) {
|
---|
| 185 | usb_log_error("Failed to initialize USB dev memory structures.");
|
---|
| 186 | goto err_usb_dev;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /* If the driver didn't name the dev when enumerating,
|
---|
| 190 | * do it in some generic way.
|
---|
| 191 | */
|
---|
| 192 | if (!ddf_fun_get_name(dev->fun)) {
|
---|
| 193 | bus_device_set_default_name(dev);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
| 197 | usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
|
---|
| 198 | goto err_usb_dev;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | return EOK;
|
---|
| 202 |
|
---|
| 203 | err_usb_dev:
|
---|
| 204 | hcd_ddf_fun_destroy(dev);
|
---|
| 205 | return err;
|
---|
[56bd6f11] | 206 | }
|
---|
| 207 |
|
---|
[0918382f] | 208 | static int device_remove(ddf_fun_t *fun, unsigned port)
|
---|
[56bd6f11] | 209 | {
|
---|
| 210 | assert(fun);
|
---|
[c952abc4] | 211 | device_t *hub = ddf_fun_data_get(fun);
|
---|
| 212 | assert(hub);
|
---|
[a1732929] | 213 | usb_log_debug("Hub `%s' reported removal of device on port %u",
|
---|
[0918382f] | 214 | ddf_fun_get_name(fun), port);
|
---|
[c952abc4] | 215 |
|
---|
| 216 | device_t *victim = NULL;
|
---|
| 217 |
|
---|
| 218 | fibril_mutex_lock(&hub->guard);
|
---|
| 219 | list_foreach(hub->devices, link, device_t, it) {
|
---|
| 220 | if (it->port == port) {
|
---|
| 221 | victim = it;
|
---|
| 222 | break;
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | fibril_mutex_unlock(&hub->guard);
|
---|
| 226 |
|
---|
| 227 | if (!victim) {
|
---|
| 228 | usb_log_warning("Hub '%s' tried to remove non-existant"
|
---|
| 229 | " device.", ddf_fun_get_name(fun));
|
---|
| 230 | return ENOENT;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | assert(victim->fun);
|
---|
| 234 | assert(victim->port == port);
|
---|
| 235 | assert(victim->hub == hub);
|
---|
| 236 |
|
---|
| 237 | bus_device_gone(victim);
|
---|
| 238 | return EOK;
|
---|
[56bd6f11] | 239 | }
|
---|
| 240 |
|
---|
[3121b5f] | 241 | /** Gets handle of the respective device.
|
---|
[8e4219ab] | 242 | *
|
---|
[3121b5f] | 243 | * @param[in] fun Device function.
|
---|
[8e4219ab] | 244 | * @param[out] handle Place to write the handle.
|
---|
| 245 | * @return Error code.
|
---|
| 246 | */
|
---|
[3121b5f] | 247 | static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
[8e4219ab] | 248 | {
|
---|
| 249 | assert(fun);
|
---|
| 250 | if (handle)
|
---|
| 251 | *handle = ddf_fun_get_handle(fun);
|
---|
| 252 | return EOK;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[1845003] | 255 | /** Inbound communication interface function.
|
---|
| 256 | * @param fun DDF function.
|
---|
| 257 | * @param target Communication target.
|
---|
| 258 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
| 259 | * @param data Pointer to data buffer.
|
---|
| 260 | * @param size Size of the data buffer.
|
---|
| 261 | * @param callback Function to call on communication end.
|
---|
| 262 | * @param arg Argument passed to the callback function.
|
---|
| 263 | * @return Error code.
|
---|
| 264 | */
|
---|
[327f147] | 265 | static int dev_read(ddf_fun_t *fun, usb_target_t target,
|
---|
| 266 | uint64_t setup_data, char *data, size_t size,
|
---|
[41df71f9] | 267 | usbhc_iface_transfer_callback_t callback, void *arg)
|
---|
[1845003] | 268 | {
|
---|
| 269 | assert(fun);
|
---|
[20eaa82] | 270 | device_t *dev = ddf_fun_data_get(fun);
|
---|
| 271 | assert(dev);
|
---|
[327f147] | 272 |
|
---|
| 273 | target.address = dev->address;
|
---|
| 274 |
|
---|
[32fb6bce] | 275 | return bus_device_send_batch(dev, target, USB_DIRECTION_IN,
|
---|
[327f147] | 276 | data, size, setup_data,
|
---|
| 277 | callback, arg, "READ");
|
---|
[1845003] | 278 | }
|
---|
| 279 |
|
---|
| 280 | /** Outbound communication interface function.
|
---|
| 281 | * @param fun DDF function.
|
---|
| 282 | * @param target Communication target.
|
---|
| 283 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
| 284 | * @param data Pointer to data buffer.
|
---|
| 285 | * @param size Size of the data buffer.
|
---|
| 286 | * @param callback Function to call on communication end.
|
---|
| 287 | * @param arg Argument passed to the callback function.
|
---|
| 288 | * @return Error code.
|
---|
| 289 | */
|
---|
[327f147] | 290 | static int dev_write(ddf_fun_t *fun, usb_target_t target,
|
---|
| 291 | uint64_t setup_data, const char *data, size_t size,
|
---|
[41df71f9] | 292 | usbhc_iface_transfer_callback_t callback, void *arg)
|
---|
[1845003] | 293 | {
|
---|
| 294 | assert(fun);
|
---|
[20eaa82] | 295 | device_t *dev = ddf_fun_data_get(fun);
|
---|
| 296 | assert(dev);
|
---|
[327f147] | 297 |
|
---|
| 298 | target.address = dev->address;
|
---|
| 299 |
|
---|
[32fb6bce] | 300 | return bus_device_send_batch(dev, target, USB_DIRECTION_OUT,
|
---|
[327f147] | 301 | (char *) data, size, setup_data,
|
---|
[1845003] | 302 | callback, arg, "WRITE");
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[2757247] | 305 | /** USB device interface */
|
---|
[53332b5b] | 306 | static usb_iface_t usb_iface = {
|
---|
[3121b5f] | 307 | .get_my_device_handle = get_my_device_handle,
|
---|
[41df71f9] | 308 | };
|
---|
[8e4219ab] | 309 |
|
---|
[41df71f9] | 310 | /** USB host controller interface */
|
---|
| 311 | static usbhc_iface_t usbhc_iface = {
|
---|
[56bd6f11] | 312 | .reserve_default_address = reserve_default_address,
|
---|
| 313 | .release_default_address = release_default_address,
|
---|
[4b8ecff] | 314 |
|
---|
[56bd6f11] | 315 | .device_enumerate = device_enumerate,
|
---|
| 316 | .device_remove = device_remove,
|
---|
[4b8ecff] | 317 |
|
---|
[d2cfe72] | 318 | .register_endpoint = register_endpoint,
|
---|
| 319 | .unregister_endpoint = unregister_endpoint,
|
---|
[4b8ecff] | 320 |
|
---|
[1845003] | 321 | .read = dev_read,
|
---|
| 322 | .write = dev_write,
|
---|
[53332b5b] | 323 | };
|
---|
[8e4219ab] | 324 |
|
---|
[2757247] | 325 | /** Standard USB device interface) */
|
---|
[53332b5b] | 326 | static ddf_dev_ops_t usb_ops = {
|
---|
| 327 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
[41df71f9] | 328 | .interfaces[USBHC_DEV_IFACE] = &usbhc_iface,
|
---|
[53332b5b] | 329 | };
|
---|
| 330 |
|
---|
[2757247] | 331 |
|
---|
| 332 | /* DDF HELPERS */
|
---|
| 333 |
|
---|
[237df2f] | 334 | #define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
|
---|
| 335 | do { \
|
---|
| 336 | match_id_t *mid = malloc(sizeof(match_id_t)); \
|
---|
| 337 | if (!mid) { \
|
---|
| 338 | clean_match_ids(list); \
|
---|
| 339 | return ENOMEM; \
|
---|
| 340 | } \
|
---|
| 341 | char *id = NULL; \
|
---|
| 342 | int ret = asprintf(&id, str, ##__VA_ARGS__); \
|
---|
| 343 | if (ret < 0) { \
|
---|
| 344 | clean_match_ids(list); \
|
---|
| 345 | free(mid); \
|
---|
| 346 | return ENOMEM; \
|
---|
| 347 | } \
|
---|
| 348 | mid->score = sc; \
|
---|
| 349 | mid->id = id; \
|
---|
| 350 | add_match_id(list, mid); \
|
---|
| 351 | } while (0)
|
---|
[b995183] | 352 |
|
---|
[237df2f] | 353 | /* This is a copy of lib/usbdev/src/recognise.c */
|
---|
| 354 | static int create_match_ids(match_id_list_t *l,
|
---|
| 355 | usb_standard_device_descriptor_t *d)
|
---|
| 356 | {
|
---|
| 357 | assert(l);
|
---|
| 358 | assert(d);
|
---|
[42bc933] | 359 |
|
---|
[237df2f] | 360 | if (d->vendor_id != 0) {
|
---|
| 361 | /* First, with release number. */
|
---|
| 362 | ADD_MATCHID_OR_RETURN(l, 100,
|
---|
| 363 | "usb&vendor=%#04x&product=%#04x&release=%x.%x",
|
---|
| 364 | d->vendor_id, d->product_id, (d->device_version >> 8),
|
---|
| 365 | (d->device_version & 0xff));
|
---|
[42bc933] | 366 |
|
---|
[0ee999d] | 367 | /* Next, without release number. */
|
---|
[237df2f] | 368 | ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
|
---|
| 369 | d->vendor_id, d->product_id);
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | /* Class match id */
|
---|
| 373 | ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
|
---|
| 374 | usb_str_class(d->device_class));
|
---|
| 375 |
|
---|
| 376 | /* As a last resort, try fallback driver. */
|
---|
| 377 | ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
|
---|
| 378 |
|
---|
| 379 | return EOK;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[eeca8a6] | 382 | device_t *hcd_ddf_fun_create(hc_device_t *hc, usb_speed_t speed)
|
---|
[867b375] | 383 | {
|
---|
| 384 | /* Create DDF function for the new device */
|
---|
[32fb6bce] | 385 | ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
|
---|
[867b375] | 386 | if (!fun)
|
---|
| 387 | return NULL;
|
---|
| 388 |
|
---|
| 389 | ddf_fun_set_ops(fun, &usb_ops);
|
---|
| 390 |
|
---|
| 391 | /* Create USB device node for the new device */
|
---|
[32fb6bce] | 392 | device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
|
---|
[20eaa82] | 393 | if (!dev) {
|
---|
[867b375] | 394 | ddf_fun_destroy(fun);
|
---|
| 395 | return NULL;
|
---|
[237df2f] | 396 | }
|
---|
[2003739] | 397 |
|
---|
[32fb6bce] | 398 | bus_device_init(dev, hc->bus);
|
---|
[20eaa82] | 399 | dev->fun = fun;
|
---|
[eeca8a6] | 400 | dev->speed = speed;
|
---|
[20eaa82] | 401 | return dev;
|
---|
[867b375] | 402 | }
|
---|
| 403 |
|
---|
[32fb6bce] | 404 | void hcd_ddf_fun_destroy(device_t *dev)
|
---|
[867b375] | 405 | {
|
---|
| 406 | assert(dev);
|
---|
| 407 | assert(dev->fun);
|
---|
| 408 | ddf_fun_destroy(dev->fun);
|
---|
| 409 | }
|
---|
| 410 |
|
---|
[32fb6bce] | 411 | int hcd_device_explore(device_t *device)
|
---|
[867b375] | 412 | {
|
---|
| 413 | int err;
|
---|
| 414 | match_id_list_t mids;
|
---|
| 415 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
| 416 |
|
---|
| 417 | init_match_ids(&mids);
|
---|
| 418 |
|
---|
[327f147] | 419 | const usb_target_t control_ep = {{
|
---|
[20eaa82] | 420 | .address = device->address,
|
---|
[327f147] | 421 | .endpoint = 0,
|
---|
[20eaa82] | 422 | }};
|
---|
[867b375] | 423 |
|
---|
[237df2f] | 424 | /* Get std device descriptor */
|
---|
[2003739] | 425 | const usb_device_request_setup_packet_t get_device_desc =
|
---|
[237df2f] | 426 | GET_DEVICE_DESC(sizeof(desc));
|
---|
| 427 |
|
---|
[f29643d5] | 428 | usb_log_debug("Device(%d): Requesting full device descriptor.",
|
---|
[20eaa82] | 429 | device->address);
|
---|
[32fb6bce] | 430 | ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
|
---|
[327f147] | 431 | (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
---|
[237df2f] | 432 | "read device descriptor");
|
---|
[867b375] | 433 | if (got < 0) {
|
---|
| 434 | err = got < 0 ? got : EOVERFLOW;
|
---|
[f29643d5] | 435 | usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
|
---|
[20eaa82] | 436 | device->address, str_error(err));
|
---|
[867b375] | 437 | goto out;
|
---|
[237df2f] | 438 | }
|
---|
[b995183] | 439 |
|
---|
[237df2f] | 440 | /* Create match ids from the device descriptor */
|
---|
[20eaa82] | 441 | usb_log_debug("Device(%d): Creating match IDs.", device->address);
|
---|
[867b375] | 442 | if ((err = create_match_ids(&mids, &desc))) {
|
---|
[20eaa82] | 443 | usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
|
---|
[867b375] | 444 | goto out;
|
---|
| 445 | }
|
---|
[237df2f] | 446 |
|
---|
[867b375] | 447 | list_foreach(mids.ids, link, const match_id_t, mid) {
|
---|
[20eaa82] | 448 | ddf_fun_add_match_id(device->fun, mid->id, mid->score);
|
---|
[237df2f] | 449 | }
|
---|
| 450 |
|
---|
[867b375] | 451 | out:
|
---|
[237df2f] | 452 | clean_match_ids(&mids);
|
---|
[867b375] | 453 | return err;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
[237df2f] | 456 | /** Announce root hub to the DDF
|
---|
| 457 | *
|
---|
| 458 | * @param[in] device Host controller ddf device
|
---|
| 459 | * @return Error code
|
---|
| 460 | */
|
---|
[32fb6bce] | 461 | int hcd_setup_virtual_root_hub(hc_device_t *hcd)
|
---|
[237df2f] | 462 | {
|
---|
[867b375] | 463 | int err;
|
---|
| 464 |
|
---|
[237df2f] | 465 | assert(hcd);
|
---|
| 466 |
|
---|
[eeca8a6] | 467 | device_t *dev = hcd_ddf_fun_create(hcd, USB_SPEED_MAX);
|
---|
[20eaa82] | 468 | if (!dev) {
|
---|
[867b375] | 469 | usb_log_error("Failed to create function for the root hub.");
|
---|
[eeca8a6] | 470 | return ENOMEM;
|
---|
[867b375] | 471 | }
|
---|
| 472 |
|
---|
[20eaa82] | 473 | ddf_fun_set_name(dev->fun, "roothub");
|
---|
[867b375] | 474 |
|
---|
[20eaa82] | 475 | /* Assign an address to the device */
|
---|
[6832245] | 476 | if ((err = bus_device_enumerate(dev))) {
|
---|
[20eaa82] | 477 | usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
|
---|
[867b375] | 478 | goto err_usb_dev;
|
---|
| 479 | }
|
---|
| 480 |
|
---|
[20eaa82] | 481 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
[867b375] | 482 | usb_log_error("Failed to register roothub: %s.", str_error(err));
|
---|
[7242ba21] | 483 | goto err_enumerated;
|
---|
[867b375] | 484 | }
|
---|
| 485 |
|
---|
| 486 | return EOK;
|
---|
| 487 |
|
---|
[7242ba21] | 488 | err_enumerated:
|
---|
| 489 | bus_device_gone(dev);
|
---|
[867b375] | 490 | err_usb_dev:
|
---|
[32fb6bce] | 491 | hcd_ddf_fun_destroy(dev);
|
---|
[867b375] | 492 | return err;
|
---|
[237df2f] | 493 | }
|
---|
| 494 |
|
---|
[53332b5b] | 495 | /** Initialize hc structures.
|
---|
| 496 | *
|
---|
| 497 | * @param[in] device DDF instance of the device to use.
|
---|
[ce33c10] | 498 | * @param[in] max_speed Maximum supported USB speed.
|
---|
| 499 | * @param[in] bw available bandwidth.
|
---|
| 500 | * @param[in] bw_count Function to compute required ep bandwidth.
|
---|
[53332b5b] | 501 | *
|
---|
[ce33c10] | 502 | * @return Error code.
|
---|
[2b0929e] | 503 | * This function does all the ddf work for hc driver.
|
---|
[53332b5b] | 504 | */
|
---|
[32fb6bce] | 505 | int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
|
---|
[53332b5b] | 506 | {
|
---|
[e991937] | 507 | assert(device);
|
---|
[53332b5b] | 508 |
|
---|
[32fb6bce] | 509 | hc_device_t *instance = ddf_dev_data_alloc(device, size);
|
---|
[53332b5b] | 510 | if (instance == NULL) {
|
---|
[a1732929] | 511 | usb_log_error("Failed to allocate HCD ddf structure.");
|
---|
[53332b5b] | 512 | return ENOMEM;
|
---|
| 513 | }
|
---|
[32fb6bce] | 514 | instance->ddf_dev = device;
|
---|
[53332b5b] | 515 |
|
---|
[e991937] | 516 | int ret = ENOMEM;
|
---|
| 517 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
---|
| 518 | if (!instance->ctl_fun) {
|
---|
[a1732929] | 519 | usb_log_error("Failed to create HCD ddf fun.");
|
---|
[cce3228] | 520 | goto err_destroy_fun;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
[e991937] | 523 | ret = ddf_fun_bind(instance->ctl_fun);
|
---|
[cce3228] | 524 | if (ret != EOK) {
|
---|
[a1732929] | 525 | usb_log_error("Failed to bind ctl_fun: %s.", str_error(ret));
|
---|
[cce3228] | 526 | goto err_destroy_fun;
|
---|
| 527 | }
|
---|
| 528 |
|
---|
[e991937] | 529 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
---|
[cce3228] | 530 | if (ret != EOK) {
|
---|
[a1732929] | 531 | usb_log_error("Failed to add fun to category: %s.",
|
---|
[cce3228] | 532 | str_error(ret));
|
---|
[e991937] | 533 | ddf_fun_unbind(instance->ctl_fun);
|
---|
[cce3228] | 534 | goto err_destroy_fun;
|
---|
| 535 | }
|
---|
[53332b5b] | 536 |
|
---|
| 537 | /* HC should be ok at this point (except it can't do anything) */
|
---|
| 538 | return EOK;
|
---|
[cce3228] | 539 |
|
---|
| 540 | err_destroy_fun:
|
---|
[e991937] | 541 | ddf_fun_destroy(instance->ctl_fun);
|
---|
| 542 | instance->ctl_fun = NULL;
|
---|
[cce3228] | 543 | return ret;
|
---|
[53332b5b] | 544 | }
|
---|
| 545 |
|
---|
[32fb6bce] | 546 | void hcd_ddf_clean_hc(hc_device_t *hcd)
|
---|
[e991937] | 547 | {
|
---|
[32fb6bce] | 548 | if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
|
---|
| 549 | ddf_fun_destroy(hcd->ctl_fun);
|
---|
[e991937] | 550 | }
|
---|
[57c8fc9] | 551 |
|
---|
[cccd60c3] | 552 | /** Call the parent driver with a request to enable interrupt
|
---|
[57c8fc9] | 553 | *
|
---|
| 554 | * @param[in] device Device asking for interrupts
|
---|
[cccd60c3] | 555 | * @param[in] inum Interrupt number
|
---|
[57c8fc9] | 556 | * @return Error code.
|
---|
| 557 | */
|
---|
[32fb6bce] | 558 | int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
|
---|
[57c8fc9] | 559 | {
|
---|
[32fb6bce] | 560 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
[2bdf92a5] | 561 | if (parent_sess == NULL)
|
---|
| 562 | return EIO;
|
---|
[57c8fc9] | 563 |
|
---|
[cccd60c3] | 564 | return hw_res_enable_interrupt(parent_sess, inum);
|
---|
[57c8fc9] | 565 | }
|
---|
| 566 |
|
---|
[32fb6bce] | 567 | int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
|
---|
[57c8fc9] | 568 | {
|
---|
[32fb6bce] | 569 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
[2bdf92a5] | 570 | if (parent_sess == NULL)
|
---|
| 571 | return EIO;
|
---|
[57c8fc9] | 572 |
|
---|
| 573 | hw_res_list_parsed_init(hw_res);
|
---|
| 574 | const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
|
---|
[c898236] | 575 | if (ret != EOK)
|
---|
| 576 | hw_res_list_parsed_clean(hw_res);
|
---|
[57c8fc9] | 577 | return ret;
|
---|
| 578 | }
|
---|
[19d21728] | 579 |
|
---|
[53332b5b] | 580 | /**
|
---|
| 581 | * @}
|
---|
| 582 | */
|
---|