[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 |
|
---|
[1102eca] | 113 | endpoint_t *ep = bus_find_endpoint(dev, pipe_desc->endpoint_no);
|
---|
[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 |
|
---|
[32fb6bce] | 338 | hc_device_t *hcd = dev_to_hcd(device);
|
---|
[237df2f] | 339 | assert(hcd);
|
---|
[20eaa82] | 340 | assert(hcd->bus);
|
---|
[b995183] | 341 |
|
---|
[14dd4c9] | 342 | fibril_mutex_lock(&hub->guard);
|
---|
[b995183] | 343 |
|
---|
[20eaa82] | 344 | device_t *victim = NULL;
|
---|
[b995183] | 345 |
|
---|
[20eaa82] | 346 | list_foreach(hub->devices, link, device_t, it) {
|
---|
| 347 | if (it->port == port) {
|
---|
[3f03199] | 348 | victim = it;
|
---|
[b995183] | 349 | break;
|
---|
[3f03199] | 350 | }
|
---|
[b995183] | 351 | }
|
---|
[9ff59981] | 352 | if (victim) {
|
---|
[867b375] | 353 | assert(victim->fun);
|
---|
[20eaa82] | 354 | assert(victim->port == port);
|
---|
| 355 | assert(victim->hub == hub);
|
---|
[b995183] | 356 | list_remove(&victim->link);
|
---|
[14dd4c9] | 357 | fibril_mutex_unlock(&hub->guard);
|
---|
[e6becb9] | 358 | const int ret = ddf_fun_unbind(victim->fun);
|
---|
| 359 | if (ret == EOK) {
|
---|
[6832245] | 360 | bus_device_remove(victim);
|
---|
[e6becb9] | 361 | ddf_fun_destroy(victim->fun);
|
---|
| 362 | } else {
|
---|
[0918382f] | 363 | usb_log_warning("Failed to unbind device `%s': %s\n",
|
---|
| 364 | ddf_fun_get_name(victim->fun), str_error(ret));
|
---|
[e6becb9] | 365 | }
|
---|
[b995183] | 366 | return EOK;
|
---|
| 367 | }
|
---|
[9ff59981] | 368 | fibril_mutex_unlock(&hub->guard);
|
---|
[b995183] | 369 | return ENOENT;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[32fb6bce] | 372 | device_t *hcd_ddf_fun_create(hc_device_t *hc)
|
---|
[867b375] | 373 | {
|
---|
| 374 | /* Create DDF function for the new device */
|
---|
[32fb6bce] | 375 | ddf_fun_t *fun = ddf_fun_create(hc->ddf_dev, fun_inner, NULL);
|
---|
[867b375] | 376 | if (!fun)
|
---|
| 377 | return NULL;
|
---|
| 378 |
|
---|
| 379 | ddf_fun_set_ops(fun, &usb_ops);
|
---|
| 380 |
|
---|
| 381 | /* Create USB device node for the new device */
|
---|
[32fb6bce] | 382 | device_t *dev = ddf_fun_data_alloc(fun, hc->bus->device_size);
|
---|
[20eaa82] | 383 | if (!dev) {
|
---|
[867b375] | 384 | ddf_fun_destroy(fun);
|
---|
| 385 | return NULL;
|
---|
[237df2f] | 386 | }
|
---|
[2003739] | 387 |
|
---|
[32fb6bce] | 388 | bus_device_init(dev, hc->bus);
|
---|
[20eaa82] | 389 | dev->fun = fun;
|
---|
| 390 | return dev;
|
---|
[867b375] | 391 | }
|
---|
| 392 |
|
---|
[32fb6bce] | 393 | void hcd_ddf_fun_destroy(device_t *dev)
|
---|
[867b375] | 394 | {
|
---|
| 395 | assert(dev);
|
---|
| 396 | assert(dev->fun);
|
---|
| 397 | ddf_fun_destroy(dev->fun);
|
---|
| 398 | }
|
---|
| 399 |
|
---|
[32fb6bce] | 400 | int hcd_device_explore(device_t *device)
|
---|
[867b375] | 401 | {
|
---|
| 402 | int err;
|
---|
| 403 | match_id_list_t mids;
|
---|
| 404 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
| 405 |
|
---|
| 406 | init_match_ids(&mids);
|
---|
| 407 |
|
---|
[327f147] | 408 | const usb_target_t control_ep = {{
|
---|
[20eaa82] | 409 | .address = device->address,
|
---|
[327f147] | 410 | .endpoint = 0,
|
---|
[20eaa82] | 411 | }};
|
---|
[867b375] | 412 |
|
---|
[237df2f] | 413 | /* Get std device descriptor */
|
---|
[2003739] | 414 | const usb_device_request_setup_packet_t get_device_desc =
|
---|
[237df2f] | 415 | GET_DEVICE_DESC(sizeof(desc));
|
---|
| 416 |
|
---|
[f29643d5] | 417 | usb_log_debug("Device(%d): Requesting full device descriptor.",
|
---|
[20eaa82] | 418 | device->address);
|
---|
[32fb6bce] | 419 | ssize_t got = bus_device_send_batch_sync(device, control_ep, USB_DIRECTION_IN,
|
---|
[327f147] | 420 | (char *) &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
---|
[237df2f] | 421 | "read device descriptor");
|
---|
[867b375] | 422 | if (got < 0) {
|
---|
| 423 | err = got < 0 ? got : EOVERFLOW;
|
---|
[f29643d5] | 424 | usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
|
---|
[20eaa82] | 425 | device->address, str_error(err));
|
---|
[867b375] | 426 | goto out;
|
---|
[237df2f] | 427 | }
|
---|
[b995183] | 428 |
|
---|
[237df2f] | 429 | /* Create match ids from the device descriptor */
|
---|
[20eaa82] | 430 | usb_log_debug("Device(%d): Creating match IDs.", device->address);
|
---|
[867b375] | 431 | if ((err = create_match_ids(&mids, &desc))) {
|
---|
[20eaa82] | 432 | usb_log_error("Device(%d): Failed to create match ids: %s", device->address, str_error(err));
|
---|
[867b375] | 433 | goto out;
|
---|
| 434 | }
|
---|
[237df2f] | 435 |
|
---|
[867b375] | 436 | list_foreach(mids.ids, link, const match_id_t, mid) {
|
---|
[20eaa82] | 437 | ddf_fun_add_match_id(device->fun, mid->id, mid->score);
|
---|
[237df2f] | 438 | }
|
---|
| 439 |
|
---|
[867b375] | 440 | out:
|
---|
[237df2f] | 441 | clean_match_ids(&mids);
|
---|
[867b375] | 442 | return err;
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[32fb6bce] | 445 | static int hcd_ddf_new_device(hc_device_t *hcd, ddf_dev_t *hc, device_t *hub, unsigned port)
|
---|
[867b375] | 446 | {
|
---|
| 447 | int err;
|
---|
| 448 | assert(hcd);
|
---|
[20eaa82] | 449 | assert(hcd->bus);
|
---|
| 450 | assert(hub);
|
---|
[867b375] | 451 | assert(hc);
|
---|
| 452 |
|
---|
[32fb6bce] | 453 | device_t *dev = hcd_ddf_fun_create(hcd);
|
---|
[20eaa82] | 454 | if (!dev) {
|
---|
[867b375] | 455 | usb_log_error("Failed to create USB device function.");
|
---|
[20eaa82] | 456 | return ENOMEM;
|
---|
[867b375] | 457 | }
|
---|
| 458 |
|
---|
[20eaa82] | 459 | dev->hub = hub;
|
---|
| 460 | dev->port = port;
|
---|
[867b375] | 461 |
|
---|
[6832245] | 462 | if ((err = bus_device_enumerate(dev))) {
|
---|
[20eaa82] | 463 | usb_log_error("Failed to initialize USB dev memory structures.");
|
---|
| 464 | return err;
|
---|
[867b375] | 465 | }
|
---|
| 466 |
|
---|
[20eaa82] | 467 | /* If the driver didn't name the dev when enumerating,
|
---|
| 468 | * do it in some generic way.
|
---|
[867b375] | 469 | */
|
---|
[20eaa82] | 470 | if (!ddf_fun_get_name(dev->fun)) {
|
---|
[6832245] | 471 | bus_device_set_default_name(dev);
|
---|
[867b375] | 472 | }
|
---|
| 473 |
|
---|
[20eaa82] | 474 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
| 475 | usb_log_error("Device(%d): Failed to register: %s.", dev->address, str_error(err));
|
---|
[867b375] | 476 | goto err_usb_dev;
|
---|
| 477 | }
|
---|
| 478 |
|
---|
[20eaa82] | 479 | fibril_mutex_lock(&hub->guard);
|
---|
| 480 | list_append(&dev->link, &hub->devices);
|
---|
| 481 | fibril_mutex_unlock(&hub->guard);
|
---|
[867b375] | 482 |
|
---|
| 483 | return EOK;
|
---|
| 484 |
|
---|
| 485 | err_usb_dev:
|
---|
[32fb6bce] | 486 | hcd_ddf_fun_destroy(dev);
|
---|
[867b375] | 487 | return err;
|
---|
[237df2f] | 488 | }
|
---|
| 489 |
|
---|
| 490 | /** Announce root hub to the DDF
|
---|
| 491 | *
|
---|
| 492 | * @param[in] device Host controller ddf device
|
---|
| 493 | * @return Error code
|
---|
| 494 | */
|
---|
[32fb6bce] | 495 | int hcd_setup_virtual_root_hub(hc_device_t *hcd)
|
---|
[237df2f] | 496 | {
|
---|
[867b375] | 497 | int err;
|
---|
| 498 |
|
---|
[237df2f] | 499 | assert(hcd);
|
---|
| 500 |
|
---|
[20eaa82] | 501 | if ((err = bus_reserve_default_address(hcd->bus, USB_SPEED_MAX))) {
|
---|
[867b375] | 502 | usb_log_error("Failed to reserve default address for roothub setup: %s", str_error(err));
|
---|
| 503 | return err;
|
---|
| 504 | }
|
---|
| 505 |
|
---|
[32fb6bce] | 506 | device_t *dev = hcd_ddf_fun_create(hcd);
|
---|
[20eaa82] | 507 | if (!dev) {
|
---|
[867b375] | 508 | usb_log_error("Failed to create function for the root hub.");
|
---|
| 509 | goto err_default_address;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
[20eaa82] | 512 | ddf_fun_set_name(dev->fun, "roothub");
|
---|
[867b375] | 513 |
|
---|
[20eaa82] | 514 | /* Assign an address to the device */
|
---|
[6832245] | 515 | if ((err = bus_device_enumerate(dev))) {
|
---|
[20eaa82] | 516 | usb_log_error("Failed to enumerate roothub device: %s", str_error(err));
|
---|
[867b375] | 517 | goto err_usb_dev;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[20eaa82] | 520 | if ((err = ddf_fun_bind(dev->fun))) {
|
---|
[867b375] | 521 | usb_log_error("Failed to register roothub: %s.", str_error(err));
|
---|
| 522 | goto err_usb_dev;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[20eaa82] | 525 | bus_release_default_address(hcd->bus);
|
---|
[867b375] | 526 | return EOK;
|
---|
| 527 |
|
---|
| 528 | err_usb_dev:
|
---|
[32fb6bce] | 529 | hcd_ddf_fun_destroy(dev);
|
---|
[867b375] | 530 | err_default_address:
|
---|
[20eaa82] | 531 | bus_release_default_address(hcd->bus);
|
---|
[867b375] | 532 | return err;
|
---|
[237df2f] | 533 | }
|
---|
| 534 |
|
---|
[53332b5b] | 535 | /** Initialize hc structures.
|
---|
| 536 | *
|
---|
| 537 | * @param[in] device DDF instance of the device to use.
|
---|
[ce33c10] | 538 | * @param[in] max_speed Maximum supported USB speed.
|
---|
| 539 | * @param[in] bw available bandwidth.
|
---|
| 540 | * @param[in] bw_count Function to compute required ep bandwidth.
|
---|
[53332b5b] | 541 | *
|
---|
[ce33c10] | 542 | * @return Error code.
|
---|
[2b0929e] | 543 | * This function does all the ddf work for hc driver.
|
---|
[53332b5b] | 544 | */
|
---|
[32fb6bce] | 545 | int hcd_ddf_setup_hc(ddf_dev_t *device, size_t size)
|
---|
[53332b5b] | 546 | {
|
---|
[e991937] | 547 | assert(device);
|
---|
[53332b5b] | 548 |
|
---|
[32fb6bce] | 549 | hc_device_t *instance = ddf_dev_data_alloc(device, size);
|
---|
[53332b5b] | 550 | if (instance == NULL) {
|
---|
[2b0929e] | 551 | usb_log_error("Failed to allocate HCD ddf structure.\n");
|
---|
[53332b5b] | 552 | return ENOMEM;
|
---|
| 553 | }
|
---|
[32fb6bce] | 554 | instance->ddf_dev = device;
|
---|
[53332b5b] | 555 |
|
---|
[e991937] | 556 | int ret = ENOMEM;
|
---|
| 557 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
---|
| 558 | if (!instance->ctl_fun) {
|
---|
[cce3228] | 559 | usb_log_error("Failed to create HCD ddf fun.\n");
|
---|
| 560 | goto err_destroy_fun;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[e991937] | 563 | ret = ddf_fun_bind(instance->ctl_fun);
|
---|
[cce3228] | 564 | if (ret != EOK) {
|
---|
[e991937] | 565 | usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
|
---|
[cce3228] | 566 | goto err_destroy_fun;
|
---|
| 567 | }
|
---|
| 568 |
|
---|
[e991937] | 569 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
---|
[cce3228] | 570 | if (ret != EOK) {
|
---|
| 571 | usb_log_error("Failed to add fun to category: %s.\n",
|
---|
| 572 | str_error(ret));
|
---|
[e991937] | 573 | ddf_fun_unbind(instance->ctl_fun);
|
---|
[cce3228] | 574 | goto err_destroy_fun;
|
---|
| 575 | }
|
---|
[53332b5b] | 576 |
|
---|
| 577 | /* HC should be ok at this point (except it can't do anything) */
|
---|
| 578 | return EOK;
|
---|
[cce3228] | 579 |
|
---|
| 580 | err_destroy_fun:
|
---|
[e991937] | 581 | ddf_fun_destroy(instance->ctl_fun);
|
---|
| 582 | instance->ctl_fun = NULL;
|
---|
[cce3228] | 583 | return ret;
|
---|
[53332b5b] | 584 | }
|
---|
| 585 |
|
---|
[32fb6bce] | 586 | void hcd_ddf_clean_hc(hc_device_t *hcd)
|
---|
[e991937] | 587 | {
|
---|
[32fb6bce] | 588 | if (ddf_fun_unbind(hcd->ctl_fun) == EOK)
|
---|
| 589 | ddf_fun_destroy(hcd->ctl_fun);
|
---|
[e991937] | 590 | }
|
---|
[57c8fc9] | 591 |
|
---|
[cccd60c3] | 592 | /** Call the parent driver with a request to enable interrupt
|
---|
[57c8fc9] | 593 | *
|
---|
| 594 | * @param[in] device Device asking for interrupts
|
---|
[cccd60c3] | 595 | * @param[in] inum Interrupt number
|
---|
[57c8fc9] | 596 | * @return Error code.
|
---|
| 597 | */
|
---|
[32fb6bce] | 598 | int hcd_ddf_enable_interrupt(hc_device_t *hcd, int inum)
|
---|
[57c8fc9] | 599 | {
|
---|
[32fb6bce] | 600 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
[2bdf92a5] | 601 | if (parent_sess == NULL)
|
---|
| 602 | return EIO;
|
---|
[57c8fc9] | 603 |
|
---|
[cccd60c3] | 604 | return hw_res_enable_interrupt(parent_sess, inum);
|
---|
[57c8fc9] | 605 | }
|
---|
| 606 |
|
---|
[32fb6bce] | 607 | int hcd_ddf_get_registers(hc_device_t *hcd, hw_res_list_parsed_t *hw_res)
|
---|
[57c8fc9] | 608 | {
|
---|
[32fb6bce] | 609 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(hcd->ddf_dev);
|
---|
[2bdf92a5] | 610 | if (parent_sess == NULL)
|
---|
| 611 | return EIO;
|
---|
[57c8fc9] | 612 |
|
---|
| 613 | hw_res_list_parsed_init(hw_res);
|
---|
| 614 | const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
|
---|
[c898236] | 615 | if (ret != EOK)
|
---|
| 616 | hw_res_list_parsed_clean(hw_res);
|
---|
[57c8fc9] | 617 | return ret;
|
---|
| 618 | }
|
---|
[19d21728] | 619 |
|
---|
[53332b5b] | 620 | /**
|
---|
| 621 | * @}
|
---|
| 622 | */
|
---|