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