| 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 | *
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <usb/classes/classes.h>
|
|---|
| 37 | #include <usb/host/bus.h>
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 | #include <usb/descriptor.h>
|
|---|
| 40 | #include <usb/request.h>
|
|---|
| 41 | #include <usb/usb.h>
|
|---|
| 42 |
|
|---|
| 43 | #include <adt/list.h>
|
|---|
| 44 | #include <assert.h>
|
|---|
| 45 | #include <async.h>
|
|---|
| 46 | #include <ddf/driver.h>
|
|---|
| 47 | #include <ddf/interrupt.h>
|
|---|
| 48 | #include <device/hw_res_parsed.h>
|
|---|
| 49 | #include <errno.h>
|
|---|
| 50 | #include <fibril_synch.h>
|
|---|
| 51 | #include <macros.h>
|
|---|
| 52 | #include <stdlib.h>
|
|---|
| 53 | #include <str_error.h>
|
|---|
| 54 | #include <usb_iface.h>
|
|---|
| 55 |
|
|---|
| 56 | #include "ddf_helpers.h"
|
|---|
| 57 |
|
|---|
| 58 | typedef struct hc_dev {
|
|---|
| 59 | ddf_fun_t *ctl_fun;
|
|---|
| 60 | hcd_t hcd;
|
|---|
| 61 | } hc_dev_t;
|
|---|
| 62 |
|
|---|
| 63 | static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
|
|---|
| 64 | {
|
|---|
| 65 | return ddf_dev_data_get(dev);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | hcd_t *dev_to_hcd(ddf_dev_t *dev)
|
|---|
| 69 | {
|
|---|
| 70 | hc_dev_t *hc_dev = dev_to_hc_dev(dev);
|
|---|
| 71 | if (!hc_dev) {
|
|---|
| 72 | usb_log_error("Invalid HCD device.\n");
|
|---|
| 73 | return NULL;
|
|---|
| 74 | }
|
|---|
| 75 | return &hc_dev->hcd;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub_dev, unsigned port);
|
|---|
| 80 | static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub, unsigned port);
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 | /* DDF INTERFACE */
|
|---|
| 84 |
|
|---|
| 85 | /** Register endpoint interface function.
|
|---|
| 86 | * @param fun DDF function.
|
|---|
| 87 | * @param endpoint_desc Endpoint description.
|
|---|
| 88 | * @return Error code.
|
|---|
| 89 | */
|
|---|
| 90 | static int register_endpoint(
|
|---|
| 91 | ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
|
|---|
| 92 | {
|
|---|
| 93 | assert(fun);
|
|---|
| 94 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 95 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 96 | assert(hcd);
|
|---|
| 97 | assert(hcd->bus);
|
|---|
| 98 | assert(dev);
|
|---|
| 99 |
|
|---|
| 100 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
|---|
| 101 | dev->address, endpoint_desc->endpoint_no,
|
|---|
| 102 | usb_str_transfer_type(endpoint_desc->transfer_type),
|
|---|
| 103 | usb_str_direction(endpoint_desc->direction),
|
|---|
| 104 | endpoint_desc->max_packet_size, endpoint_desc->usb2.polling_interval);
|
|---|
| 105 |
|
|---|
| 106 | return bus_add_endpoint(hcd->bus, dev, endpoint_desc, NULL);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /** Unregister endpoint interface function.
|
|---|
| 110 | * @param fun DDF function.
|
|---|
| 111 | * @param endpoint_desc Endpoint description.
|
|---|
| 112 | * @return Error code.
|
|---|
| 113 | */
|
|---|
| 114 | static int unregister_endpoint(
|
|---|
| 115 | ddf_fun_t *fun, usb_endpoint_desc_t *endpoint_desc)
|
|---|
| 116 | {
|
|---|
| 117 | assert(fun);
|
|---|
| 118 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 119 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 120 | assert(hcd);
|
|---|
| 121 | assert(hcd->bus);
|
|---|
| 122 | assert(dev);
|
|---|
| 123 |
|
|---|
| 124 | const usb_target_t target = {{
|
|---|
| 125 | .address = dev->address,
|
|---|
| 126 | .endpoint = endpoint_desc->endpoint_no
|
|---|
| 127 | }};
|
|---|
| 128 |
|
|---|
| 129 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
|---|
| 130 | dev->address, endpoint_desc->endpoint_no,
|
|---|
| 131 | usb_str_direction(endpoint_desc->direction));
|
|---|
| 132 |
|
|---|
| 133 | endpoint_t *ep = bus_find_endpoint(hcd->bus, dev, target, endpoint_desc->direction);
|
|---|
| 134 | if (!ep)
|
|---|
| 135 | return ENOENT;
|
|---|
| 136 |
|
|---|
| 137 | return bus_remove_endpoint(hcd->bus, ep);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
|---|
| 141 | {
|
|---|
| 142 | assert(fun);
|
|---|
| 143 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 144 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 145 | assert(hcd);
|
|---|
| 146 | assert(hcd->bus);
|
|---|
| 147 | assert(dev);
|
|---|
| 148 |
|
|---|
| 149 | usb_log_debug("Device %d requested default address at %s speed\n",
|
|---|
| 150 | dev->address, usb_str_speed(speed));
|
|---|
| 151 | return bus_reserve_default_address(hcd->bus, speed);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | static int release_default_address(ddf_fun_t *fun)
|
|---|
| 155 | {
|
|---|
| 156 | assert(fun);
|
|---|
| 157 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 158 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 159 | assert(hcd);
|
|---|
| 160 | assert(hcd->bus);
|
|---|
| 161 | assert(dev);
|
|---|
| 162 |
|
|---|
| 163 | usb_log_debug("Device %d released default address\n", dev->address);
|
|---|
| 164 | return bus_release_default_address(hcd->bus);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | static int device_enumerate(ddf_fun_t *fun, unsigned port)
|
|---|
| 168 | {
|
|---|
| 169 | assert(fun);
|
|---|
| 170 | ddf_dev_t *hc = ddf_fun_get_dev(fun);
|
|---|
| 171 | assert(hc);
|
|---|
| 172 | hcd_t *hcd = dev_to_hcd(hc);
|
|---|
| 173 | assert(hcd);
|
|---|
| 174 | device_t *hub = ddf_fun_data_get(fun);
|
|---|
| 175 | assert(hub);
|
|---|
| 176 |
|
|---|
| 177 | usb_log_debug("Hub %d reported a new USB device on port: %u\n",
|
|---|
| 178 | hub->address, port);
|
|---|
| 179 | return hcd_ddf_new_device(hcd, hc, hub, port);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | static int device_remove(ddf_fun_t *fun, unsigned port)
|
|---|
| 183 | {
|
|---|
| 184 | assert(fun);
|
|---|
| 185 | ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
|
|---|
| 186 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 187 | assert(ddf_dev);
|
|---|
| 188 | assert(dev);
|
|---|
| 189 | usb_log_debug("Hub `%s' reported removal of device on port %u\n",
|
|---|
| 190 | ddf_fun_get_name(fun), port);
|
|---|
| 191 | return hcd_ddf_remove_device(ddf_dev, dev, port);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /** Gets handle of the respective device.
|
|---|
| 195 | *
|
|---|
| 196 | * @param[in] fun Device function.
|
|---|
| 197 | * @param[out] handle Place to write the handle.
|
|---|
| 198 | * @return Error code.
|
|---|
| 199 | */
|
|---|
| 200 | static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
|---|
| 201 | {
|
|---|
| 202 | assert(fun);
|
|---|
| 203 | if (handle)
|
|---|
| 204 | *handle = ddf_fun_get_handle(fun);
|
|---|
| 205 | return EOK;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /** Inbound communication interface function.
|
|---|
| 209 | * @param fun DDF function.
|
|---|
| 210 | * @param target Communication target.
|
|---|
| 211 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 212 | * @param data Pointer to data buffer.
|
|---|
| 213 | * @param size Size of the data buffer.
|
|---|
| 214 | * @param callback Function to call on communication end.
|
|---|
| 215 | * @param arg Argument passed to the callback function.
|
|---|
| 216 | * @return Error code.
|
|---|
| 217 | */
|
|---|
| 218 | static int dev_read(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 219 | uint64_t setup_data, char *data, size_t size,
|
|---|
| 220 | usb_iface_transfer_callback_t callback, void *arg)
|
|---|
| 221 | {
|
|---|
| 222 | assert(fun);
|
|---|
| 223 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 224 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 225 | assert(dev);
|
|---|
| 226 |
|
|---|
| 227 | target.address = dev->address;
|
|---|
| 228 |
|
|---|
| 229 | return hcd_send_batch(hcd, dev, target, USB_DIRECTION_IN,
|
|---|
| 230 | data, size, setup_data,
|
|---|
| 231 | callback, arg, "READ");
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /** Outbound communication interface function.
|
|---|
| 235 | * @param fun DDF function.
|
|---|
| 236 | * @param target Communication target.
|
|---|
| 237 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 238 | * @param data Pointer to data buffer.
|
|---|
| 239 | * @param size Size of the data buffer.
|
|---|
| 240 | * @param callback Function to call on communication end.
|
|---|
| 241 | * @param arg Argument passed to the callback function.
|
|---|
| 242 | * @return Error code.
|
|---|
| 243 | */
|
|---|
| 244 | static int dev_write(ddf_fun_t *fun, usb_target_t target,
|
|---|
| 245 | uint64_t setup_data, const char *data, size_t size,
|
|---|
| 246 | usb_iface_transfer_callback_t callback, void *arg)
|
|---|
| 247 | {
|
|---|
| 248 | assert(fun);
|
|---|
| 249 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 250 | device_t *dev = ddf_fun_data_get(fun);
|
|---|
| 251 | assert(dev);
|
|---|
| 252 |
|
|---|
| 253 | target.address = dev->address;
|
|---|
| 254 |
|
|---|
| 255 | return hcd_send_batch(hcd, dev, target, USB_DIRECTION_OUT,
|
|---|
| 256 | (char *) data, size, setup_data,
|
|---|
| 257 | callback, arg, "WRITE");
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | /** USB device interface */
|
|---|
| 261 | static usb_iface_t usb_iface = {
|
|---|
| 262 | .get_my_device_handle = get_my_device_handle,
|
|---|
| 263 |
|
|---|
| 264 | .reserve_default_address = reserve_default_address,
|
|---|
| 265 | .release_default_address = release_default_address,
|
|---|
| 266 |
|
|---|
| 267 | .device_enumerate = device_enumerate,
|
|---|
| 268 | .device_remove = device_remove,
|
|---|
| 269 |
|
|---|
| 270 | .register_endpoint = register_endpoint,
|
|---|
| 271 | .unregister_endpoint = unregister_endpoint,
|
|---|
| 272 |
|
|---|
| 273 | .read = dev_read,
|
|---|
| 274 | .write = dev_write,
|
|---|
| 275 | };
|
|---|
| 276 |
|
|---|
| 277 | /** Standard USB device interface) */
|
|---|
| 278 | static ddf_dev_ops_t usb_ops = {
|
|---|
| 279 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
|---|
| 280 | };
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 | /* DDF HELPERS */
|
|---|
| 284 |
|
|---|
| 285 | #define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
|
|---|
| 286 | do { \
|
|---|
| 287 | match_id_t *mid = malloc(sizeof(match_id_t)); \
|
|---|
| 288 | if (!mid) { \
|
|---|
| 289 | clean_match_ids(list); \
|
|---|
| 290 | return ENOMEM; \
|
|---|
| 291 | } \
|
|---|
| 292 | char *id = NULL; \
|
|---|
| 293 | int ret = asprintf(&id, str, ##__VA_ARGS__); \
|
|---|
| 294 | if (ret < 0) { \
|
|---|
| 295 | clean_match_ids(list); \
|
|---|
| 296 | free(mid); \
|
|---|
| 297 | return ENOMEM; \
|
|---|
| 298 | } \
|
|---|
| 299 | mid->score = sc; \
|
|---|
| 300 | mid->id = id; \
|
|---|
| 301 | add_match_id(list, mid); \
|
|---|
| 302 | } while (0)
|
|---|
| 303 |
|
|---|
| 304 | /* This is a copy of lib/usbdev/src/recognise.c */
|
|---|
| 305 | static int create_match_ids(match_id_list_t *l,
|
|---|
| 306 | usb_standard_device_descriptor_t *d)
|
|---|
| 307 | {
|
|---|
| 308 | assert(l);
|
|---|
| 309 | assert(d);
|
|---|
| 310 |
|
|---|
| 311 | if (d->vendor_id != 0) {
|
|---|
| 312 | /* First, with release number. */
|
|---|
| 313 | ADD_MATCHID_OR_RETURN(l, 100,
|
|---|
| 314 | "usb&vendor=%#04x&product=%#04x&release=%x.%x",
|
|---|
| 315 | d->vendor_id, d->product_id, (d->device_version >> 8),
|
|---|
| 316 | (d->device_version & 0xff));
|
|---|
| 317 |
|
|---|
| 318 | /* Next, without release number. */
|
|---|
| 319 | ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
|
|---|
| 320 | d->vendor_id, d->product_id);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /* Class match id */
|
|---|
| 324 | ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
|
|---|
| 325 | usb_str_class(d->device_class));
|
|---|
| 326 |
|
|---|
| 327 | /* As a last resort, try fallback driver. */
|
|---|
| 328 | ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
|
|---|
| 329 |
|
|---|
| 330 | return EOK;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | static int hcd_ddf_remove_device(ddf_dev_t *device, device_t *hub,
|
|---|
| 334 | unsigned port)
|
|---|
| 335 | {
|
|---|
| 336 | assert(device);
|
|---|
| 337 |
|
|---|
| 338 | hcd_t *hcd = dev_to_hcd(device);
|
|---|
| 339 | assert(hcd);
|
|---|
| 340 | assert(hcd->bus);
|
|---|
| 341 |
|
|---|
| 342 | hc_dev_t *hc_dev = dev_to_hc_dev(device);
|
|---|
| 343 | assert(hc_dev);
|
|---|
| 344 |
|
|---|
| 345 | fibril_mutex_lock(&hub->guard);
|
|---|
| 346 |
|
|---|
| 347 | device_t *victim = NULL;
|
|---|
| 348 |
|
|---|
| 349 | list_foreach(hub->devices, link, device_t, it) {
|
|---|
| 350 | if (it->port == port) {
|
|---|
| 351 | victim = it;
|
|---|
| 352 | break;
|
|---|
| 353 | }
|
|---|
| 354 | }
|
|---|
| 355 | if (victim) {
|
|---|
| 356 | assert(victim->fun);
|
|---|
| 357 | assert(victim->port == port);
|
|---|
| 358 | assert(victim->hub == hub);
|
|---|
| 359 | list_remove(&victim->link);
|
|---|
| 360 | fibril_mutex_unlock(&hub->guard);
|
|---|
| 361 | const int ret = ddf_fun_unbind(victim->fun);
|
|---|
| 362 | if (ret == EOK) {
|
|---|
| 363 | usb_address_t address = victim->address;
|
|---|
| 364 | bus_remove_device(hcd->bus, hcd, victim);
|
|---|
| 365 | ddf_fun_destroy(victim->fun);
|
|---|
| 366 | bus_release_address(hcd->bus, address);
|
|---|
| 367 | } else {
|
|---|
| 368 | usb_log_warning("Failed to unbind device `%s': %s\n",
|
|---|
| 369 | ddf_fun_get_name(victim->fun), str_error(ret));
|
|---|
| 370 | }
|
|---|
| 371 | return EOK;
|
|---|
| 372 | }
|
|---|
| 373 | fibril_mutex_unlock(&hub->guard);
|
|---|
| 374 | return ENOENT;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | device_t *hcd_ddf_device_create(ddf_dev_t *hc, size_t device_size)
|
|---|
| 378 | {
|
|---|
| 379 | /* Create DDF function for the new device */
|
|---|
| 380 | ddf_fun_t *fun = ddf_fun_create(hc, fun_inner, NULL);
|
|---|
| 381 | if (!fun)
|
|---|
| 382 | return NULL;
|
|---|
| 383 |
|
|---|
| 384 | ddf_fun_set_ops(fun, &usb_ops);
|
|---|
| 385 |
|
|---|
| 386 | /* Create USB device node for the new device */
|
|---|
| 387 | device_t *dev = ddf_fun_data_alloc(fun, device_size);
|
|---|
| 388 | if (!dev) {
|
|---|
| 389 | ddf_fun_destroy(fun);
|
|---|
| 390 | return NULL;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | device_init(dev);
|
|---|
| 394 | dev->fun = fun;
|
|---|
| 395 | return dev;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | void hcd_ddf_device_destroy(device_t *dev)
|
|---|
| 399 | {
|
|---|
| 400 | assert(dev);
|
|---|
| 401 | assert(dev->fun);
|
|---|
| 402 | ddf_fun_destroy(dev->fun);
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | int hcd_ddf_device_explore(hcd_t *hcd, device_t *device)
|
|---|
| 406 | {
|
|---|
| 407 | int err;
|
|---|
| 408 | match_id_list_t mids;
|
|---|
| 409 | usb_standard_device_descriptor_t desc = { 0 };
|
|---|
| 410 |
|
|---|
| 411 | init_match_ids(&mids);
|
|---|
| 412 |
|
|---|
| 413 | const usb_target_t control_ep = {{
|
|---|
| 414 | .address = device->address,
|
|---|
| 415 | .endpoint = 0,
|
|---|
| 416 | }};
|
|---|
| 417 |
|
|---|
| 418 | /* Get std device descriptor */
|
|---|
| 419 | const usb_device_request_setup_packet_t get_device_desc =
|
|---|
| 420 | GET_DEVICE_DESC(sizeof(desc));
|
|---|
| 421 |
|
|---|
| 422 | usb_log_debug("Device(%d): Requesting full device descriptor.",
|
|---|
| 423 | device->address);
|
|---|
| 424 | ssize_t got = hcd_send_batch_sync(hcd, device, control_ep, USB_DIRECTION_IN,
|
|---|
| 425 | (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
|---|
| 426 | "read device descriptor");
|
|---|
| 427 | if (got < 0) {
|
|---|
| 428 | err = got < 0 ? got : EOVERFLOW;
|
|---|
| 429 | usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
|
|---|
| 430 | device->address, str_error(err));
|
|---|
| 431 | goto out;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | /* Create match ids from the device descriptor */
|
|---|
| 435 | usb_log_debug("Device(%d): Creating match IDs.", device->address);
|
|---|
| 436 | if ((err = create_match_ids(&mids, &desc))) {
|
|---|
| 437 | usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
|
|---|
| 438 | goto out;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | list_foreach(mids.ids, link, const match_id_t, mid) {
|
|---|
| 442 | ddf_fun_add_match_id(device->fun, mid->id, mid->score);
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | out:
|
|---|
| 446 | clean_match_ids(&mids);
|
|---|
| 447 | return err;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | static int hcd_ddf_new_device(hcd_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
|
|---|
| 451 | {
|
|---|
| 452 | int err;
|
|---|
| 453 | assert(hcd);
|
|---|
| 454 | assert(hcd->bus);
|
|---|
| 455 | assert(hub);
|
|---|
| 456 | assert(hc);
|
|---|
| 457 |
|
|---|
| 458 | device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
|
|---|
| 459 | if (!dev) {
|
|---|
| 460 | usb_log_error("Failed to create USB device function.");
|
|---|
| 461 | return ENOMEM;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | dev->hub = hub;
|
|---|
| 465 | dev->port = port;
|
|---|
| 466 |
|
|---|
| 467 | if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
|
|---|
| 468 | usb_log_error("Failed to initialize USB dev memory structures.");
|
|---|
| 469 | return err;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | /* If the driver didn't name the dev when enumerating,
|
|---|
| 473 | * do it in some generic way.
|
|---|
| 474 | */
|
|---|
| 475 | if (!ddf_fun_get_name(dev->fun)) {
|
|---|
| 476 | device_set_default_name(dev);
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | if ((err = ddf_fun_bind(dev->fun))) {
|
|---|
| 480 | usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
|
|---|
| 481 | goto err_usb_dev;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | fibril_mutex_lock(&hub->guard);
|
|---|
| 485 | list_append(&dev->link, &hub->devices);
|
|---|
| 486 | fibril_mutex_unlock(&hub->guard);
|
|---|
| 487 |
|
|---|
| 488 | return EOK;
|
|---|
| 489 |
|
|---|
| 490 | err_usb_dev:
|
|---|
| 491 | hcd_ddf_device_destroy(dev);
|
|---|
| 492 | return err;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | /** Announce root hub to the DDF
|
|---|
| 496 | *
|
|---|
| 497 | * @param[in] device Host controller ddf device
|
|---|
| 498 | * @return Error code
|
|---|
| 499 | */
|
|---|
| 500 | int hcd_setup_virtual_root_hub(hcd_t *hcd, ddf_dev_t *hc)
|
|---|
| 501 | {
|
|---|
| 502 | int err;
|
|---|
| 503 |
|
|---|
| 504 | assert(hc);
|
|---|
| 505 | assert(hcd);
|
|---|
| 506 | assert(hcd->bus);
|
|---|
| 507 |
|
|---|
| 508 | if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
|
|---|
| 509 | usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
|
|---|
| 510 | return err;
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | device_t *dev = hcd_ddf_device_create(hc, hcd->bus->device_size);
|
|---|
| 514 | if (!dev) {
|
|---|
| 515 | usb_log_error("Failed to create function for the root hub.");
|
|---|
| 516 | goto err_default_address;
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | ddf_fun_set_name(dev->fun, "roothub");
|
|---|
| 520 |
|
|---|
| 521 | dev->tt = (usb_tt_address_t) {
|
|---|
| 522 | .address = -1,
|
|---|
| 523 | .port = 0,
|
|---|
| 524 | };
|
|---|
| 525 |
|
|---|
| 526 | /* Assign an address to the device */
|
|---|
| 527 | if ((err = bus_enumerate_device(hcd->bus, hcd, dev))) {
|
|---|
| 528 | usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
|
|---|
| 529 | goto err_usb_dev;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | if ((err = ddf_fun_bind(dev->fun))) {
|
|---|
| 533 | usb_log_error("Failed to register roothub: %s.", str_error(err));
|
|---|
| 534 | goto err_usb_dev;
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | bus_release_default_address(hcd->bus);
|
|---|
| 538 | return EOK;
|
|---|
| 539 |
|
|---|
| 540 | err_usb_dev:
|
|---|
| 541 | hcd_ddf_device_destroy(dev);
|
|---|
| 542 | err_default_address:
|
|---|
| 543 | bus_release_default_address(hcd->bus);
|
|---|
| 544 | return err;
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | /** Initialize hc structures.
|
|---|
| 548 | *
|
|---|
| 549 | * @param[in] device DDF instance of the device to use.
|
|---|
| 550 | * @param[in] max_speed Maximum supported USB speed.
|
|---|
| 551 | * @param[in] bw available bandwidth.
|
|---|
| 552 | * @param[in] bw_count Function to compute required ep bandwidth.
|
|---|
| 553 | *
|
|---|
| 554 | * @return Error code.
|
|---|
| 555 | * This function does all the ddf work for hc driver.
|
|---|
| 556 | */
|
|---|
| 557 | int hcd_ddf_setup_hc(ddf_dev_t *device)
|
|---|
| 558 | {
|
|---|
| 559 | assert(device);
|
|---|
| 560 |
|
|---|
| 561 | hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
|
|---|
| 562 | if (instance == NULL) {
|
|---|
| 563 | usb_log_error("Failed to allocate HCD ddf structure.\n");
|
|---|
| 564 | return ENOMEM;
|
|---|
| 565 | }
|
|---|
| 566 | hcd_init(&instance->hcd);
|
|---|
| 567 |
|
|---|
| 568 | int ret = ENOMEM;
|
|---|
| 569 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
|---|
| 570 | if (!instance->ctl_fun) {
|
|---|
| 571 | usb_log_error("Failed to create HCD ddf fun.\n");
|
|---|
| 572 | goto err_destroy_fun;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | ret = ddf_fun_bind(instance->ctl_fun);
|
|---|
| 576 | if (ret != EOK) {
|
|---|
| 577 | usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
|
|---|
| 578 | goto err_destroy_fun;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
|---|
| 582 | if (ret != EOK) {
|
|---|
| 583 | usb_log_error("Failed to add fun to category: %s.\n",
|
|---|
| 584 | str_error(ret));
|
|---|
| 585 | ddf_fun_unbind(instance->ctl_fun);
|
|---|
| 586 | goto err_destroy_fun;
|
|---|
| 587 | }
|
|---|
| 588 |
|
|---|
| 589 | /* HC should be ok at this point (except it can't do anything) */
|
|---|
| 590 | return EOK;
|
|---|
| 591 |
|
|---|
| 592 | err_destroy_fun:
|
|---|
| 593 | ddf_fun_destroy(instance->ctl_fun);
|
|---|
| 594 | instance->ctl_fun = NULL;
|
|---|
| 595 | return ret;
|
|---|
| 596 | }
|
|---|
| 597 |
|
|---|
| 598 | void hcd_ddf_clean_hc(ddf_dev_t *device)
|
|---|
| 599 | {
|
|---|
| 600 | assert(device);
|
|---|
| 601 | hc_dev_t *hc = dev_to_hc_dev(device);
|
|---|
| 602 | assert(hc);
|
|---|
| 603 | const int ret = ddf_fun_unbind(hc->ctl_fun);
|
|---|
| 604 | if (ret == EOK)
|
|---|
| 605 | ddf_fun_destroy(hc->ctl_fun);
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | //TODO: Cache parent session in HCD
|
|---|
| 609 | /** Call the parent driver with a request to enable interrupt
|
|---|
| 610 | *
|
|---|
| 611 | * @param[in] device Device asking for interrupts
|
|---|
| 612 | * @param[in] inum Interrupt number
|
|---|
| 613 | * @return Error code.
|
|---|
| 614 | */
|
|---|
| 615 | int hcd_ddf_enable_interrupt(ddf_dev_t *device, int inum)
|
|---|
| 616 | {
|
|---|
| 617 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
|---|
| 618 | if (parent_sess == NULL)
|
|---|
| 619 | return EIO;
|
|---|
| 620 |
|
|---|
| 621 | return hw_res_enable_interrupt(parent_sess, inum);
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | //TODO: Cache parent session in HCD
|
|---|
| 625 | int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
|
|---|
| 626 | {
|
|---|
| 627 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
|---|
| 628 | if (parent_sess == NULL)
|
|---|
| 629 | return EIO;
|
|---|
| 630 |
|
|---|
| 631 | hw_res_list_parsed_init(hw_res);
|
|---|
| 632 | const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
|
|---|
| 633 | if (ret != EOK)
|
|---|
| 634 | hw_res_list_parsed_clean(hw_res);
|
|---|
| 635 | return ret;
|
|---|
| 636 | }
|
|---|
| 637 |
|
|---|
| 638 | // TODO: move this someplace else
|
|---|
| 639 | static inline void irq_code_clean(irq_code_t *code)
|
|---|
| 640 | {
|
|---|
| 641 | if (code) {
|
|---|
| 642 | free(code->ranges);
|
|---|
| 643 | free(code->cmds);
|
|---|
| 644 | code->ranges = NULL;
|
|---|
| 645 | code->cmds = NULL;
|
|---|
| 646 | code->rangecount = 0;
|
|---|
| 647 | code->cmdcount = 0;
|
|---|
| 648 | }
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | /** Register interrupt handler
|
|---|
| 652 | *
|
|---|
| 653 | * @param[in] device Host controller DDF device
|
|---|
| 654 | * @param[in] regs Register range
|
|---|
| 655 | * @param[in] irq Interrupt number
|
|---|
| 656 | * @paran[in] handler Interrupt handler
|
|---|
| 657 | * @param[in] gen_irq_code IRQ code generator.
|
|---|
| 658 | *
|
|---|
| 659 | * @return IRQ capability handle on success.
|
|---|
| 660 | * @return Negative error code.
|
|---|
| 661 | */
|
|---|
| 662 | int hcd_ddf_setup_interrupts(ddf_dev_t *device,
|
|---|
| 663 | const hw_res_list_parsed_t *hw_res,
|
|---|
| 664 | interrupt_handler_t handler,
|
|---|
| 665 | irq_code_gen_t gen_irq_code)
|
|---|
| 666 | {
|
|---|
| 667 | assert(device);
|
|---|
| 668 |
|
|---|
| 669 | hcd_t *hcd = dev_to_hcd(device);
|
|---|
| 670 |
|
|---|
| 671 | if (!handler || !gen_irq_code)
|
|---|
| 672 | return ENOTSUP;
|
|---|
| 673 |
|
|---|
| 674 | irq_code_t irq_code = {0};
|
|---|
| 675 |
|
|---|
| 676 | const int irq = gen_irq_code(&irq_code, hcd, hw_res);
|
|---|
| 677 | if (irq < 0) {
|
|---|
| 678 | usb_log_error("Failed to generate IRQ code: %s.\n",
|
|---|
| 679 | str_error(irq));
|
|---|
| 680 | return irq;
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | /* Register handler to avoid interrupt lockup */
|
|---|
| 684 | const int irq_cap = register_interrupt_handler(device, irq, handler,
|
|---|
| 685 | &irq_code);
|
|---|
| 686 | irq_code_clean(&irq_code);
|
|---|
| 687 | if (irq_cap < 0) {
|
|---|
| 688 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
|---|
| 689 | str_error(irq_cap));
|
|---|
| 690 | return irq_cap;
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | /* Enable interrupts */
|
|---|
| 694 | int ret = hcd_ddf_enable_interrupt(device, irq);
|
|---|
| 695 | if (ret != EOK) {
|
|---|
| 696 | usb_log_error("Failed to enable interrupts: %s.\n",
|
|---|
| 697 | str_error(ret));
|
|---|
| 698 | unregister_interrupt_handler(device, irq_cap);
|
|---|
| 699 | return ret;
|
|---|
| 700 | }
|
|---|
| 701 | return irq_cap;
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | /** IRQ handling callback, forward status from call to diver structure.
|
|---|
| 705 | *
|
|---|
| 706 | * @param[in] dev DDF instance of the device to use.
|
|---|
| 707 | * @param[in] iid (Unused).
|
|---|
| 708 | * @param[in] call Pointer to the call from kernel.
|
|---|
| 709 | */
|
|---|
| 710 | void ddf_hcd_gen_irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
|---|
| 711 | {
|
|---|
| 712 | assert(dev);
|
|---|
| 713 | hcd_t *hcd = dev_to_hcd(dev);
|
|---|
| 714 | if (!hcd || !hcd->ops.irq_hook) {
|
|---|
| 715 | usb_log_error("Interrupt on not yet initialized device.\n");
|
|---|
| 716 | return;
|
|---|
| 717 | }
|
|---|
| 718 | const uint32_t status = IPC_GET_ARG1(*call);
|
|---|
| 719 | hcd->ops.irq_hook(hcd, status);
|
|---|
| 720 | }
|
|---|
| 721 |
|
|---|
| 722 | static int interrupt_polling(void *arg)
|
|---|
| 723 | {
|
|---|
| 724 | hcd_t *hcd = arg;
|
|---|
| 725 | assert(hcd);
|
|---|
| 726 | if (!hcd->ops.status_hook || !hcd->ops.irq_hook)
|
|---|
| 727 | return ENOTSUP;
|
|---|
| 728 | uint32_t status = 0;
|
|---|
| 729 | while (hcd->ops.status_hook(hcd, &status) == EOK) {
|
|---|
| 730 | hcd->ops.irq_hook(hcd, status);
|
|---|
| 731 | status = 0;
|
|---|
| 732 | /* We should wait 1 frame - 1ms here, but this polling is a
|
|---|
| 733 | * lame crutch anyway so don't hog the system. 10ms is still
|
|---|
| 734 | * good enough for emergency mode */
|
|---|
| 735 | async_usleep(10000);
|
|---|
| 736 | }
|
|---|
| 737 | return EOK;
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | /** Initialize hc and rh DDF structures and their respective drivers.
|
|---|
| 741 | *
|
|---|
| 742 | * @param device DDF instance of the device to use
|
|---|
| 743 | * @param speed Maximum supported speed
|
|---|
| 744 | * @param bw Available bandwidth (arbitrary units)
|
|---|
| 745 | * @param bw_count Bandwidth computing function
|
|---|
| 746 | * @param irq_handler IRQ handling function
|
|---|
| 747 | * @param gen_irq_code Function to generate IRQ pseudocode
|
|---|
| 748 | * (it needs to return used irq number)
|
|---|
| 749 | * @param driver_init Function to initialize HC driver
|
|---|
| 750 | * @param driver_fini Function to cleanup HC driver
|
|---|
| 751 | * @return Error code
|
|---|
| 752 | *
|
|---|
| 753 | * This function does all the preparatory work for hc and rh drivers:
|
|---|
| 754 | * - gets device's hw resources
|
|---|
| 755 | * - attempts to enable interrupts
|
|---|
| 756 | * - registers interrupt handler
|
|---|
| 757 | * - calls driver specific initialization
|
|---|
| 758 | * - registers root hub
|
|---|
| 759 | */
|
|---|
| 760 | int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver)
|
|---|
| 761 | {
|
|---|
| 762 | assert(driver);
|
|---|
| 763 |
|
|---|
| 764 | int ret = EOK;
|
|---|
| 765 |
|
|---|
| 766 | hw_res_list_parsed_t hw_res;
|
|---|
| 767 | ret = hcd_ddf_get_registers(device, &hw_res);
|
|---|
| 768 | if (ret != EOK) {
|
|---|
| 769 | usb_log_error("Failed to get register memory addresses "
|
|---|
| 770 | "for `%s': %s.\n", ddf_dev_get_name(device),
|
|---|
| 771 | str_error(ret));
|
|---|
| 772 | return ret;
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | ret = hcd_ddf_setup_hc(device);
|
|---|
| 776 | if (ret != EOK) {
|
|---|
| 777 | usb_log_error("Failed to setup generic HCD.\n");
|
|---|
| 778 | goto err_hw_res;
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | hcd_t *hcd = dev_to_hcd(device);
|
|---|
| 782 |
|
|---|
| 783 | if (driver->init)
|
|---|
| 784 | ret = driver->init(hcd, &hw_res, device);
|
|---|
| 785 | if (ret != EOK) {
|
|---|
| 786 | usb_log_error("Failed to init HCD.\n");
|
|---|
| 787 | goto err_hcd;
|
|---|
| 788 | }
|
|---|
| 789 |
|
|---|
| 790 | /* Setup interrupts */
|
|---|
| 791 | interrupt_handler_t *irq_handler =
|
|---|
| 792 | driver->irq_handler ? driver->irq_handler : ddf_hcd_gen_irq_handler;
|
|---|
| 793 | const int irq_cap = hcd_ddf_setup_interrupts(device, &hw_res,
|
|---|
| 794 | irq_handler, driver->irq_code_gen);
|
|---|
| 795 | bool irqs_enabled = !(irq_cap < 0);
|
|---|
| 796 | if (irqs_enabled) {
|
|---|
| 797 | usb_log_debug("Hw interrupts enabled.\n");
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 | /* Claim the device from BIOS */
|
|---|
| 801 | if (driver->claim)
|
|---|
| 802 | ret = driver->claim(hcd, device);
|
|---|
| 803 | if (ret != EOK) {
|
|---|
| 804 | usb_log_error("Failed to claim `%s' for driver `%s': %s",
|
|---|
| 805 | ddf_dev_get_name(device), driver->name, str_error(ret));
|
|---|
| 806 | goto err_irq;
|
|---|
| 807 | }
|
|---|
| 808 |
|
|---|
| 809 | /* Start hw driver */
|
|---|
| 810 | if (driver->start)
|
|---|
| 811 | ret = driver->start(hcd, irqs_enabled);
|
|---|
| 812 | if (ret != EOK) {
|
|---|
| 813 | usb_log_error("Failed to start HCD: %s.\n", str_error(ret));
|
|---|
| 814 | goto err_irq;
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | /* Need working irq replacement to setup root hub */
|
|---|
| 818 | if (!irqs_enabled && hcd->ops.status_hook) {
|
|---|
| 819 | hcd->polling_fibril = fibril_create(interrupt_polling, hcd);
|
|---|
| 820 | if (hcd->polling_fibril == 0) {
|
|---|
| 821 | usb_log_error("Failed to create polling fibril\n");
|
|---|
| 822 | ret = ENOMEM;
|
|---|
| 823 | goto err_started;
|
|---|
| 824 | }
|
|---|
| 825 | fibril_add_ready(hcd->polling_fibril);
|
|---|
| 826 | usb_log_warning("Failed to enable interrupts: %s."
|
|---|
| 827 | " Falling back to polling.\n", str_error(irq_cap));
|
|---|
| 828 | }
|
|---|
| 829 |
|
|---|
| 830 | /*
|
|---|
| 831 | * Creating root hub registers a new USB device so HC
|
|---|
| 832 | * needs to be ready at this time.
|
|---|
| 833 | */
|
|---|
| 834 | if (driver->setup_root_hub)
|
|---|
| 835 | ret = driver->setup_root_hub(hcd, device);
|
|---|
| 836 | if (ret != EOK) {
|
|---|
| 837 | usb_log_error("Failed to setup HC root hub: %s.\n",
|
|---|
| 838 | str_error(ret));
|
|---|
| 839 | goto err_polling;
|
|---|
| 840 | }
|
|---|
| 841 |
|
|---|
| 842 | usb_log_info("Controlling new `%s' device `%s'.\n",
|
|---|
| 843 | driver->name, ddf_dev_get_name(device));
|
|---|
| 844 | return EOK;
|
|---|
| 845 |
|
|---|
| 846 | err_polling:
|
|---|
| 847 | // TODO: Stop the polling fibril (refactor the interrupt_polling func)
|
|---|
| 848 | //
|
|---|
| 849 | err_started:
|
|---|
| 850 | if (driver->stop)
|
|---|
| 851 | driver->stop(hcd);
|
|---|
| 852 | err_irq:
|
|---|
| 853 | unregister_interrupt_handler(device, irq_cap);
|
|---|
| 854 | if (driver->fini)
|
|---|
| 855 | driver->fini(hcd);
|
|---|
| 856 | err_hcd:
|
|---|
| 857 | hcd_ddf_clean_hc(device);
|
|---|
| 858 | err_hw_res:
|
|---|
| 859 | hw_res_list_parsed_clean(&hw_res);
|
|---|
| 860 | return ret;
|
|---|
| 861 | }
|
|---|
| 862 |
|
|---|
| 863 | /**
|
|---|
| 864 | * @}
|
|---|
| 865 | */
|
|---|