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