| 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/debug.h>
|
|---|
| 38 | #include <usb/descriptor.h>
|
|---|
| 39 | #include <usb/request.h>
|
|---|
| 40 | #include <usb/usb.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <adt/list.h>
|
|---|
| 43 | #include <assert.h>
|
|---|
| 44 | #include <async.h>
|
|---|
| 45 | #include <ddf/driver.h>
|
|---|
| 46 | #include <ddf/interrupt.h>
|
|---|
| 47 | #include <device/hw_res_parsed.h>
|
|---|
| 48 | #include <devman.h>
|
|---|
| 49 | #include <errno.h>
|
|---|
| 50 | #include <fibril_synch.h>
|
|---|
| 51 | #include <stdio.h>
|
|---|
| 52 | #include <stdlib.h>
|
|---|
| 53 | #include <str_error.h>
|
|---|
| 54 | #include <usb_iface.h>
|
|---|
| 55 |
|
|---|
| 56 | #include "ddf_helpers.h"
|
|---|
| 57 |
|
|---|
| 58 | #define CTRL_PIPE_MIN_PACKET_SIZE 8
|
|---|
| 59 |
|
|---|
| 60 | typedef struct usb_dev {
|
|---|
| 61 | link_t link;
|
|---|
| 62 | list_t devices;
|
|---|
| 63 | fibril_mutex_t guard;
|
|---|
| 64 | ddf_fun_t *fun;
|
|---|
| 65 | usb_address_t address;
|
|---|
| 66 | usb_speed_t speed;
|
|---|
| 67 | usb_address_t tt_address;
|
|---|
| 68 | unsigned port;
|
|---|
| 69 | } usb_dev_t;
|
|---|
| 70 |
|
|---|
| 71 | typedef struct hc_dev {
|
|---|
| 72 | ddf_fun_t *ctl_fun;
|
|---|
| 73 | hcd_t hcd;
|
|---|
| 74 | usb_dev_t *root_hub;
|
|---|
| 75 | } hc_dev_t;
|
|---|
| 76 |
|
|---|
| 77 | static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
|
|---|
| 78 | {
|
|---|
| 79 | return ddf_dev_data_get(dev);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | hcd_t *dev_to_hcd(ddf_dev_t *dev)
|
|---|
| 83 | {
|
|---|
| 84 | hc_dev_t *hc_dev = dev_to_hc_dev(dev);
|
|---|
| 85 | if (!hc_dev) {
|
|---|
| 86 | usb_log_error("Invalid HCD device.\n");
|
|---|
| 87 | return NULL;
|
|---|
| 88 | }
|
|---|
| 89 | return &hc_dev->hcd;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
|
|---|
| 94 | static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | /* DDF INTERFACE */
|
|---|
| 98 |
|
|---|
| 99 | /** Register endpoint interface function.
|
|---|
| 100 | * @param fun DDF function.
|
|---|
| 101 | * @param address USB address of the device.
|
|---|
| 102 | * @param endpoint USB endpoint number to be registered.
|
|---|
| 103 | * @param transfer_type Endpoint's transfer type.
|
|---|
| 104 | * @param direction USB communication direction the endpoint is capable of.
|
|---|
| 105 | * @param max_packet_size Maximu size of packets the endpoint accepts.
|
|---|
| 106 | * @param interval Preferred timeout between communication.
|
|---|
| 107 | * @return Error code.
|
|---|
| 108 | */
|
|---|
| 109 | static int register_endpoint(
|
|---|
| 110 | ddf_fun_t *fun, usb_endpoint_t endpoint,
|
|---|
| 111 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
|---|
| 112 | size_t max_packet_size, unsigned interval)
|
|---|
| 113 | {
|
|---|
| 114 | assert(fun);
|
|---|
| 115 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 116 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
|---|
| 117 | assert(hcd);
|
|---|
| 118 | assert(dev);
|
|---|
| 119 | const size_t size = max_packet_size;
|
|---|
| 120 | const usb_target_t target =
|
|---|
| 121 | {{.address = dev->address, .endpoint = endpoint}};
|
|---|
| 122 |
|
|---|
| 123 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
|---|
| 124 | dev->address, endpoint, usb_str_transfer_type(transfer_type),
|
|---|
| 125 | usb_str_direction(direction), max_packet_size, interval);
|
|---|
| 126 |
|
|---|
| 127 | return hcd_add_ep(hcd, target, direction, transfer_type,
|
|---|
| 128 | max_packet_size, size, dev->tt_address, dev->port);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** Unregister endpoint interface function.
|
|---|
| 132 | * @param fun DDF function.
|
|---|
| 133 | * @param address USB address of the endpoint.
|
|---|
| 134 | * @param endpoint USB endpoint number.
|
|---|
| 135 | * @param direction Communication direction of the enpdoint to unregister.
|
|---|
| 136 | * @return Error code.
|
|---|
| 137 | */
|
|---|
| 138 | static int unregister_endpoint(
|
|---|
| 139 | ddf_fun_t *fun, usb_endpoint_t endpoint, usb_direction_t direction)
|
|---|
| 140 | {
|
|---|
| 141 | assert(fun);
|
|---|
| 142 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 143 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
|---|
| 144 | assert(hcd);
|
|---|
| 145 | assert(dev);
|
|---|
| 146 | const usb_target_t target =
|
|---|
| 147 | {{.address = dev->address, .endpoint = endpoint}};
|
|---|
| 148 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
|---|
| 149 | dev->address, endpoint, usb_str_direction(direction));
|
|---|
| 150 | return hcd_remove_ep(hcd, target, direction);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
|---|
| 154 | {
|
|---|
| 155 | assert(fun);
|
|---|
| 156 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 157 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
|---|
| 158 | assert(hcd);
|
|---|
| 159 | assert(dev);
|
|---|
| 160 |
|
|---|
| 161 | usb_log_debug("Device %d requested default address at %s speed\n",
|
|---|
| 162 | dev->address, usb_str_speed(speed));
|
|---|
| 163 | return hcd_reserve_default_address(hcd, speed);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | static int release_default_address(ddf_fun_t *fun)
|
|---|
| 167 | {
|
|---|
| 168 | assert(fun);
|
|---|
| 169 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
|---|
| 170 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
|---|
| 171 | assert(hcd);
|
|---|
| 172 | assert(dev);
|
|---|
| 173 |
|
|---|
| 174 | usb_log_debug("Device %d released default address\n", dev->address);
|
|---|
| 175 | return hcd_release_default_address(hcd);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | static int device_enumerate(ddf_fun_t *fun, unsigned port)
|
|---|
| 179 | {
|
|---|
| 180 | assert(fun);
|
|---|
| 181 | ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
|
|---|
| 182 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
|---|
| 183 | assert(ddf_dev);
|
|---|
| 184 | assert(dev);
|
|---|
| 185 | usb_log_debug("Hub %d reported a new USB device on port: %u\n",
|
|---|
| 186 | dev->address, port);
|
|---|
| 187 | return hcd_ddf_new_device(ddf_dev, dev, port);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | static int device_remove(ddf_fun_t *fun, unsigned port)
|
|---|
| 191 | {
|
|---|
| 192 | assert(fun);
|
|---|
| 193 | ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
|
|---|
| 194 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
|---|
| 195 | assert(ddf_dev);
|
|---|
| 196 | assert(dev);
|
|---|
| 197 | usb_log_debug("Hub `%s' reported removal of device on port %u\n",
|
|---|
| 198 | ddf_fun_get_name(fun), port);
|
|---|
| 199 | return hcd_ddf_remove_device(ddf_dev, dev, port);
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /** Gets handle of the respective device.
|
|---|
| 203 | *
|
|---|
| 204 | * @param[in] fun Device function.
|
|---|
| 205 | * @param[out] handle Place to write the handle.
|
|---|
| 206 | * @return Error code.
|
|---|
| 207 | */
|
|---|
| 208 | static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
|---|
| 209 | {
|
|---|
| 210 | assert(fun);
|
|---|
| 211 | if (handle)
|
|---|
| 212 | *handle = ddf_fun_get_handle(fun);
|
|---|
| 213 | return EOK;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /** Inbound communication interface function.
|
|---|
| 217 | * @param fun DDF function.
|
|---|
| 218 | * @param target Communication target.
|
|---|
| 219 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 220 | * @param data Pointer to data buffer.
|
|---|
| 221 | * @param size Size of the data buffer.
|
|---|
| 222 | * @param callback Function to call on communication end.
|
|---|
| 223 | * @param arg Argument passed to the callback function.
|
|---|
| 224 | * @return Error code.
|
|---|
| 225 | */
|
|---|
| 226 | static int dev_read(ddf_fun_t *fun, usb_endpoint_t endpoint,
|
|---|
| 227 | uint64_t setup_data, uint8_t *data, size_t size,
|
|---|
| 228 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
|---|
| 229 | {
|
|---|
| 230 | assert(fun);
|
|---|
| 231 | usb_dev_t *usb_dev = ddf_fun_data_get(fun);
|
|---|
| 232 | assert(usb_dev);
|
|---|
| 233 | const usb_target_t target = {{
|
|---|
| 234 | .address = usb_dev->address,
|
|---|
| 235 | .endpoint = endpoint,
|
|---|
| 236 | }};
|
|---|
| 237 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target,
|
|---|
| 238 | USB_DIRECTION_IN, data, size, setup_data, callback, NULL, arg,
|
|---|
| 239 | "READ");
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /** Outbound communication interface function.
|
|---|
| 243 | * @param fun DDF function.
|
|---|
| 244 | * @param target Communication target.
|
|---|
| 245 | * @param setup_data Data to use in setup stage (control transfers).
|
|---|
| 246 | * @param data Pointer to data buffer.
|
|---|
| 247 | * @param size Size of the data buffer.
|
|---|
| 248 | * @param callback Function to call on communication end.
|
|---|
| 249 | * @param arg Argument passed to the callback function.
|
|---|
| 250 | * @return Error code.
|
|---|
| 251 | */
|
|---|
| 252 | static int dev_write(ddf_fun_t *fun, usb_endpoint_t endpoint,
|
|---|
| 253 | uint64_t setup_data, const uint8_t *data, size_t size,
|
|---|
| 254 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
|---|
| 255 | {
|
|---|
| 256 | assert(fun);
|
|---|
| 257 | usb_dev_t *usb_dev = ddf_fun_data_get(fun);
|
|---|
| 258 | assert(usb_dev);
|
|---|
| 259 | const usb_target_t target = {{
|
|---|
| 260 | .address = usb_dev->address,
|
|---|
| 261 | .endpoint = endpoint,
|
|---|
| 262 | }};
|
|---|
| 263 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
|
|---|
| 264 | target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
|
|---|
| 265 | callback, arg, "WRITE");
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | /** USB device interface */
|
|---|
| 269 | static usb_iface_t usb_iface = {
|
|---|
| 270 | .get_my_device_handle = get_my_device_handle,
|
|---|
| 271 |
|
|---|
| 272 | .reserve_default_address = reserve_default_address,
|
|---|
| 273 | .release_default_address = release_default_address,
|
|---|
| 274 |
|
|---|
| 275 | .device_enumerate = device_enumerate,
|
|---|
| 276 | .device_remove = device_remove,
|
|---|
| 277 |
|
|---|
| 278 | .register_endpoint = register_endpoint,
|
|---|
| 279 | .unregister_endpoint = unregister_endpoint,
|
|---|
| 280 |
|
|---|
| 281 | .read = dev_read,
|
|---|
| 282 | .write = dev_write,
|
|---|
| 283 | };
|
|---|
| 284 |
|
|---|
| 285 | /** Standard USB device interface) */
|
|---|
| 286 | static ddf_dev_ops_t usb_ops = {
|
|---|
| 287 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
|---|
| 288 | };
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 | /* DDF HELPERS */
|
|---|
| 292 |
|
|---|
| 293 | #define GET_DEVICE_DESC(size) \
|
|---|
| 294 | { \
|
|---|
| 295 | .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST \
|
|---|
| 296 | | (USB_REQUEST_TYPE_STANDARD << 5) \
|
|---|
| 297 | | USB_REQUEST_RECIPIENT_DEVICE, \
|
|---|
| 298 | .request = USB_DEVREQ_GET_DESCRIPTOR, \
|
|---|
| 299 | .value = uint16_host2usb(USB_DESCTYPE_DEVICE << 8), \
|
|---|
| 300 | .index = uint16_host2usb(0), \
|
|---|
| 301 | .length = uint16_host2usb(size), \
|
|---|
| 302 | };
|
|---|
| 303 |
|
|---|
| 304 | #define SET_ADDRESS(address) \
|
|---|
| 305 | { \
|
|---|
| 306 | .request_type = SETUP_REQUEST_TYPE_HOST_TO_DEVICE \
|
|---|
| 307 | | (USB_REQUEST_TYPE_STANDARD << 5) \
|
|---|
| 308 | | USB_REQUEST_RECIPIENT_DEVICE, \
|
|---|
| 309 | .request = USB_DEVREQ_SET_ADDRESS, \
|
|---|
| 310 | .value = uint16_host2usb(address), \
|
|---|
| 311 | .index = uint16_host2usb(0), \
|
|---|
| 312 | .length = uint16_host2usb(0), \
|
|---|
| 313 | };
|
|---|
| 314 |
|
|---|
| 315 | static int hcd_ddf_add_device(ddf_dev_t *parent, usb_dev_t *hub_dev,
|
|---|
| 316 | unsigned port, usb_address_t address, usb_speed_t speed, const char *name,
|
|---|
| 317 | const match_id_list_t *mids)
|
|---|
| 318 | {
|
|---|
| 319 | assert(parent);
|
|---|
| 320 |
|
|---|
| 321 | char default_name[10] = { 0 }; /* usbxyz-ss */
|
|---|
| 322 | if (!name) {
|
|---|
| 323 | snprintf(default_name, sizeof(default_name) - 1,
|
|---|
| 324 | "usb%u-%cs", address, usb_str_speed(speed)[0]);
|
|---|
| 325 | name = default_name;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | ddf_fun_t *fun = ddf_fun_create(parent, fun_inner, name);
|
|---|
| 329 | if (!fun)
|
|---|
| 330 | return ENOMEM;
|
|---|
| 331 | usb_dev_t *info = ddf_fun_data_alloc(fun, sizeof(usb_dev_t));
|
|---|
| 332 | if (!info) {
|
|---|
| 333 | ddf_fun_destroy(fun);
|
|---|
| 334 | return ENOMEM;
|
|---|
| 335 | }
|
|---|
| 336 | info->address = address;
|
|---|
| 337 | info->speed = speed;
|
|---|
| 338 | info->fun = fun;
|
|---|
| 339 | info->port = port;
|
|---|
| 340 | info->tt_address = hub_dev ? hub_dev->tt_address : -1;
|
|---|
| 341 | link_initialize(&info->link);
|
|---|
| 342 | list_initialize(&info->devices);
|
|---|
| 343 | fibril_mutex_initialize(&info->guard);
|
|---|
| 344 |
|
|---|
| 345 | if (hub_dev && hub_dev->speed == USB_SPEED_HIGH && usb_speed_is_11(speed))
|
|---|
| 346 | info->tt_address = hub_dev->address;
|
|---|
| 347 |
|
|---|
| 348 | ddf_fun_set_ops(fun, &usb_ops);
|
|---|
| 349 | list_foreach(mids->ids, link, const match_id_t, mid) {
|
|---|
| 350 | ddf_fun_add_match_id(fun, mid->id, mid->score);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | int ret = ddf_fun_bind(fun);
|
|---|
| 354 | if (ret != EOK) {
|
|---|
| 355 | ddf_fun_destroy(fun);
|
|---|
| 356 | return ret;
|
|---|
| 357 | }
|
|---|
| 358 |
|
|---|
| 359 | if (hub_dev) {
|
|---|
| 360 | fibril_mutex_lock(&hub_dev->guard);
|
|---|
| 361 | list_append(&info->link, &hub_dev->devices);
|
|---|
| 362 | fibril_mutex_unlock(&hub_dev->guard);
|
|---|
| 363 | } else {
|
|---|
| 364 | hc_dev_t *hc_dev = dev_to_hc_dev(parent);
|
|---|
| 365 | assert(hc_dev->root_hub == NULL);
|
|---|
| 366 | hc_dev->root_hub = info;
|
|---|
| 367 | }
|
|---|
| 368 | return EOK;
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | #define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
|
|---|
| 372 | do { \
|
|---|
| 373 | match_id_t *mid = malloc(sizeof(match_id_t)); \
|
|---|
| 374 | if (!mid) { \
|
|---|
| 375 | clean_match_ids(list); \
|
|---|
| 376 | return ENOMEM; \
|
|---|
| 377 | } \
|
|---|
| 378 | char *id = NULL; \
|
|---|
| 379 | int ret = asprintf(&id, str, ##__VA_ARGS__); \
|
|---|
| 380 | if (ret < 0) { \
|
|---|
| 381 | clean_match_ids(list); \
|
|---|
| 382 | free(mid); \
|
|---|
| 383 | return ENOMEM; \
|
|---|
| 384 | } \
|
|---|
| 385 | mid->score = sc; \
|
|---|
| 386 | mid->id = id; \
|
|---|
| 387 | add_match_id(list, mid); \
|
|---|
| 388 | } while (0)
|
|---|
| 389 |
|
|---|
| 390 | /* This is a copy of lib/usbdev/src/recognise.c */
|
|---|
| 391 | static int create_match_ids(match_id_list_t *l,
|
|---|
| 392 | usb_standard_device_descriptor_t *d)
|
|---|
| 393 | {
|
|---|
| 394 | assert(l);
|
|---|
| 395 | assert(d);
|
|---|
| 396 |
|
|---|
| 397 | if (d->vendor_id != 0) {
|
|---|
| 398 | /* First, with release number. */
|
|---|
| 399 | ADD_MATCHID_OR_RETURN(l, 100,
|
|---|
| 400 | "usb&vendor=%#04x&product=%#04x&release=%x.%x",
|
|---|
| 401 | d->vendor_id, d->product_id, (d->device_version >> 8),
|
|---|
| 402 | (d->device_version & 0xff));
|
|---|
| 403 |
|
|---|
| 404 | /* Next, without release number. */
|
|---|
| 405 | ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
|
|---|
| 406 | d->vendor_id, d->product_id);
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | /* Class match id */
|
|---|
| 410 | ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
|
|---|
| 411 | usb_str_class(d->device_class));
|
|---|
| 412 |
|
|---|
| 413 | /* As a last resort, try fallback driver. */
|
|---|
| 414 | ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
|
|---|
| 415 |
|
|---|
| 416 | return EOK;
|
|---|
| 417 |
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub,
|
|---|
| 421 | unsigned port)
|
|---|
| 422 | {
|
|---|
| 423 | assert(device);
|
|---|
| 424 |
|
|---|
| 425 | hcd_t *hcd = dev_to_hcd(device);
|
|---|
| 426 | assert(hcd);
|
|---|
| 427 |
|
|---|
| 428 | hc_dev_t *hc_dev = dev_to_hc_dev(device);
|
|---|
| 429 | assert(hc_dev);
|
|---|
| 430 |
|
|---|
| 431 | fibril_mutex_lock(&hub->guard);
|
|---|
| 432 |
|
|---|
| 433 | usb_dev_t *victim = NULL;
|
|---|
| 434 |
|
|---|
| 435 | list_foreach(hub->devices, link, usb_dev_t, it) {
|
|---|
| 436 | if (it->port == port) {
|
|---|
| 437 | victim = it;
|
|---|
| 438 | break;
|
|---|
| 439 | }
|
|---|
| 440 | }
|
|---|
| 441 | if (victim && victim->port == port) {
|
|---|
| 442 | list_remove(&victim->link);
|
|---|
| 443 | fibril_mutex_unlock(&hub->guard);
|
|---|
| 444 | const int ret = ddf_fun_unbind(victim->fun);
|
|---|
| 445 | if (ret == EOK) {
|
|---|
| 446 | ddf_fun_destroy(victim->fun);
|
|---|
| 447 | hcd_release_address(hcd, victim->address);
|
|---|
| 448 | } else {
|
|---|
| 449 | usb_log_warning("Failed to unbind device `%s': %s\n",
|
|---|
| 450 | ddf_fun_get_name(victim->fun), str_error(ret));
|
|---|
| 451 | }
|
|---|
| 452 | return EOK;
|
|---|
| 453 | }
|
|---|
| 454 | return ENOENT;
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port)
|
|---|
| 458 | {
|
|---|
| 459 | assert(device);
|
|---|
| 460 |
|
|---|
| 461 | hcd_t *hcd = dev_to_hcd(device);
|
|---|
| 462 | assert(hcd);
|
|---|
| 463 |
|
|---|
| 464 | usb_speed_t speed = USB_SPEED_MAX;
|
|---|
| 465 |
|
|---|
| 466 | /* This checks whether the default address is reserved and gets speed */
|
|---|
| 467 | int ret = usb_bus_get_speed(&hcd->bus, USB_ADDRESS_DEFAULT, &speed);
|
|---|
| 468 | if (ret != EOK) {
|
|---|
| 469 | return ret;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | static const usb_target_t default_target = {{
|
|---|
| 473 | .address = USB_ADDRESS_DEFAULT,
|
|---|
| 474 | .endpoint = 0,
|
|---|
| 475 | }};
|
|---|
| 476 |
|
|---|
| 477 | const usb_address_t address = hcd_request_address(hcd, speed);
|
|---|
| 478 | if (address < 0)
|
|---|
| 479 | return address;
|
|---|
| 480 |
|
|---|
| 481 | const usb_target_t target = {{
|
|---|
| 482 | .address = address,
|
|---|
| 483 | .endpoint = 0,
|
|---|
| 484 | }};
|
|---|
| 485 |
|
|---|
| 486 | const usb_address_t tt_address = hub ? hub->tt_address : -1;
|
|---|
| 487 |
|
|---|
| 488 | /* Add default pipe on default address */
|
|---|
| 489 | ret = hcd_add_ep(hcd,
|
|---|
| 490 | default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
|
|---|
| 491 | CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE,
|
|---|
| 492 | tt_address, port);
|
|---|
| 493 |
|
|---|
| 494 | if (ret != EOK) {
|
|---|
| 495 | hcd_release_address(hcd, address);
|
|---|
| 496 | return ret;
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | /* Get max packet size for default pipe */
|
|---|
| 500 | usb_standard_device_descriptor_t desc = { 0 };
|
|---|
| 501 | static const usb_device_request_setup_packet_t get_device_desc_8 =
|
|---|
| 502 | GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
|
|---|
| 503 |
|
|---|
| 504 | // TODO CALLBACKS
|
|---|
| 505 | ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
|
|---|
| 506 | &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
|
|---|
| 507 | "read first 8 bytes of dev descriptor");
|
|---|
| 508 |
|
|---|
| 509 | if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
|
|---|
| 510 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
|---|
| 511 | hcd_release_address(hcd, address);
|
|---|
| 512 | return got < 0 ? got : EOVERFLOW;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | /* Register EP on the new address */
|
|---|
| 516 | ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
|
|---|
| 517 | desc.max_packet_size, desc.max_packet_size, tt_address, port);
|
|---|
| 518 | if (ret != EOK) {
|
|---|
| 519 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
|---|
| 520 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
|---|
| 521 | hcd_release_address(hcd, address);
|
|---|
| 522 | return ret;
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | /* Set new address */
|
|---|
| 526 | const usb_device_request_setup_packet_t set_address =
|
|---|
| 527 | SET_ADDRESS(target.address);
|
|---|
| 528 |
|
|---|
| 529 | got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
|
|---|
| 530 | NULL, 0, *(uint64_t *)&set_address, "set address");
|
|---|
| 531 |
|
|---|
| 532 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
|---|
| 533 |
|
|---|
| 534 | if (got != 0) {
|
|---|
| 535 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
|---|
| 536 | hcd_release_address(hcd, address);
|
|---|
| 537 | return got;
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | /* Get std device descriptor */
|
|---|
| 541 | static const usb_device_request_setup_packet_t get_device_desc =
|
|---|
| 542 | GET_DEVICE_DESC(sizeof(desc));
|
|---|
| 543 |
|
|---|
| 544 | got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
|
|---|
| 545 | &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
|---|
| 546 | "read device descriptor");
|
|---|
| 547 | if (ret != EOK) {
|
|---|
| 548 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
|---|
| 549 | hcd_release_address(hcd, target.address);
|
|---|
| 550 | return got < 0 ? got : EOVERFLOW;
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | /* Create match ids from the device descriptor */
|
|---|
| 554 | match_id_list_t mids;
|
|---|
| 555 | init_match_ids(&mids);
|
|---|
| 556 |
|
|---|
| 557 | ret = create_match_ids(&mids, &desc);
|
|---|
| 558 | if (ret != EOK) {
|
|---|
| 559 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
|---|
| 560 | hcd_release_address(hcd, target.address);
|
|---|
| 561 | return ret;
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | /* Register device */
|
|---|
| 565 | ret = hcd_ddf_add_device(device, hub, port, address, speed, NULL, &mids);
|
|---|
| 566 | clean_match_ids(&mids);
|
|---|
| 567 | if (ret != EOK) {
|
|---|
| 568 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
|---|
| 569 | hcd_release_address(hcd, target.address);
|
|---|
| 570 | }
|
|---|
| 571 |
|
|---|
| 572 | return ret;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | /** Announce root hub to the DDF
|
|---|
| 576 | *
|
|---|
| 577 | * @param[in] device Host controller ddf device
|
|---|
| 578 | * @return Error code
|
|---|
| 579 | */
|
|---|
| 580 | int hcd_ddf_setup_root_hub(ddf_dev_t *device)
|
|---|
| 581 | {
|
|---|
| 582 | assert(device);
|
|---|
| 583 | hcd_t *hcd = dev_to_hcd(device);
|
|---|
| 584 | assert(hcd);
|
|---|
| 585 |
|
|---|
| 586 | hcd_reserve_default_address(hcd, hcd->bus.max_speed);
|
|---|
| 587 | const int ret = hcd_ddf_new_device(device, NULL, 0);
|
|---|
| 588 | hcd_release_default_address(hcd);
|
|---|
| 589 | return ret;
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | /** Initialize hc structures.
|
|---|
| 593 | *
|
|---|
| 594 | * @param[in] device DDF instance of the device to use.
|
|---|
| 595 | * @param[in] max_speed Maximum supported USB speed.
|
|---|
| 596 | * @param[in] bw available bandwidth.
|
|---|
| 597 | * @param[in] bw_count Function to compute required ep bandwidth.
|
|---|
| 598 | *
|
|---|
| 599 | * @return Error code.
|
|---|
| 600 | * This function does all the ddf work for hc driver.
|
|---|
| 601 | */
|
|---|
| 602 | int hcd_ddf_setup_hc(ddf_dev_t *device, usb_speed_t max_speed,
|
|---|
| 603 | size_t bw, bw_count_func_t bw_count)
|
|---|
| 604 | {
|
|---|
| 605 | assert(device);
|
|---|
| 606 |
|
|---|
| 607 | hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
|
|---|
| 608 | if (instance == NULL) {
|
|---|
| 609 | usb_log_error("Failed to allocate HCD ddf structure.\n");
|
|---|
| 610 | return ENOMEM;
|
|---|
| 611 | }
|
|---|
| 612 | instance->root_hub = NULL;
|
|---|
| 613 | hcd_init(&instance->hcd, max_speed, bw, bw_count);
|
|---|
| 614 |
|
|---|
| 615 | int ret = ENOMEM;
|
|---|
| 616 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
|---|
| 617 | if (!instance->ctl_fun) {
|
|---|
| 618 | usb_log_error("Failed to create HCD ddf fun.\n");
|
|---|
| 619 | goto err_destroy_fun;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | ret = ddf_fun_bind(instance->ctl_fun);
|
|---|
| 623 | if (ret != EOK) {
|
|---|
| 624 | usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
|
|---|
| 625 | goto err_destroy_fun;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
|---|
| 629 | if (ret != EOK) {
|
|---|
| 630 | usb_log_error("Failed to add fun to category: %s.\n",
|
|---|
| 631 | str_error(ret));
|
|---|
| 632 | ddf_fun_unbind(instance->ctl_fun);
|
|---|
| 633 | goto err_destroy_fun;
|
|---|
| 634 | }
|
|---|
| 635 |
|
|---|
| 636 | /* HC should be ok at this point (except it can't do anything) */
|
|---|
| 637 | return EOK;
|
|---|
| 638 |
|
|---|
| 639 | err_destroy_fun:
|
|---|
| 640 | ddf_fun_destroy(instance->ctl_fun);
|
|---|
| 641 | instance->ctl_fun = NULL;
|
|---|
| 642 | return ret;
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | void hcd_ddf_clean_hc(ddf_dev_t *device)
|
|---|
| 646 | {
|
|---|
| 647 | assert(device);
|
|---|
| 648 | hc_dev_t *hc = dev_to_hc_dev(device);
|
|---|
| 649 | assert(hc);
|
|---|
| 650 | const int ret = ddf_fun_unbind(hc->ctl_fun);
|
|---|
| 651 | if (ret == EOK)
|
|---|
| 652 | ddf_fun_destroy(hc->ctl_fun);
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 | //TODO: Move this to generic ddf?
|
|---|
| 657 | /** Call the parent driver with a request to enable interrupts
|
|---|
| 658 | *
|
|---|
| 659 | * @param[in] device Device asking for interrupts
|
|---|
| 660 | * @return Error code.
|
|---|
| 661 | */
|
|---|
| 662 | int hcd_ddf_enable_interrupts(ddf_dev_t *device)
|
|---|
| 663 | {
|
|---|
| 664 | assert(device);
|
|---|
| 665 | async_sess_t *parent_sess =
|
|---|
| 666 | devman_parent_device_connect(EXCHANGE_SERIALIZE,
|
|---|
| 667 | ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
|
|---|
| 668 | const bool enabled = hw_res_enable_interrupt(parent_sess);
|
|---|
| 669 | async_hangup(parent_sess);
|
|---|
| 670 |
|
|---|
| 671 | return enabled ? EOK : EIO;
|
|---|
| 672 | }
|
|---|
| 673 |
|
|---|
| 674 | int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
|
|---|
| 675 | {
|
|---|
| 676 | assert(device);
|
|---|
| 677 | assert(hw_res);
|
|---|
| 678 |
|
|---|
| 679 | async_sess_t *parent_sess =
|
|---|
| 680 | devman_parent_device_connect(EXCHANGE_SERIALIZE,
|
|---|
| 681 | ddf_dev_get_handle(device), IPC_FLAG_BLOCKING);
|
|---|
| 682 | hw_res_list_parsed_init(hw_res);
|
|---|
| 683 | const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
|
|---|
| 684 | async_hangup(parent_sess);
|
|---|
| 685 | if (ret != EOK)
|
|---|
| 686 | hw_res_list_parsed_clean(hw_res);
|
|---|
| 687 | return ret;
|
|---|
| 688 | }
|
|---|
| 689 |
|
|---|
| 690 | // TODO: move this someplace else
|
|---|
| 691 | static inline void irq_code_clean(irq_code_t *code)
|
|---|
| 692 | {
|
|---|
| 693 | if (code) {
|
|---|
| 694 | free(code->ranges);
|
|---|
| 695 | free(code->cmds);
|
|---|
| 696 | code->ranges = NULL;
|
|---|
| 697 | code->cmds = NULL;
|
|---|
| 698 | code->rangecount = 0;
|
|---|
| 699 | code->cmdcount = 0;
|
|---|
| 700 | }
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 | /** Register interrupt handler
|
|---|
| 705 | *
|
|---|
| 706 | * @param[in] device Host controller DDF device
|
|---|
| 707 | * @param[in] regs Register range
|
|---|
| 708 | * @param[in] irq Interrupt number
|
|---|
| 709 | * @paran[in] handler Interrupt handler
|
|---|
| 710 | * @param[in] gen_irq_code IRQ code generator.
|
|---|
| 711 | *
|
|---|
| 712 | * @return EOK on success or negative error code
|
|---|
| 713 | */
|
|---|
| 714 | int hcd_ddf_setup_interrupts(ddf_dev_t *device, addr_range_t *regs, int irq,
|
|---|
| 715 | interrupt_handler_t handler,
|
|---|
| 716 | int (*gen_irq_code)(irq_code_t *, addr_range_t *))
|
|---|
| 717 | {
|
|---|
| 718 |
|
|---|
| 719 | assert(device);
|
|---|
| 720 | assert(regs);
|
|---|
| 721 | assert(handler);
|
|---|
| 722 | assert(gen_irq_code);
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 | irq_code_t irq_code = {0};
|
|---|
| 726 |
|
|---|
| 727 | int ret = gen_irq_code(&irq_code, regs);
|
|---|
| 728 | if (ret != EOK) {
|
|---|
| 729 | usb_log_error("Failed to generate IRQ code: %s.\n",
|
|---|
| 730 | str_error(ret));
|
|---|
| 731 | return ret;
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | /* Register handler to avoid interrupt lockup */
|
|---|
| 735 | ret = register_interrupt_handler(device, irq, handler, &irq_code);
|
|---|
| 736 | irq_code_clean(&irq_code);
|
|---|
| 737 | if (ret != EOK) {
|
|---|
| 738 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
|---|
| 739 | str_error(ret));
|
|---|
| 740 | return ret;
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | /* Enable interrupts */
|
|---|
| 744 | ret = hcd_ddf_enable_interrupts(device);
|
|---|
| 745 | if (ret != EOK) {
|
|---|
| 746 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
|---|
| 747 | str_error(ret));
|
|---|
| 748 | unregister_interrupt_handler(device, irq);
|
|---|
| 749 | return ret;
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | return EOK;
|
|---|
| 753 | }
|
|---|
| 754 | /**
|
|---|
| 755 | * @}
|
|---|
| 756 | */
|
|---|