[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 |
|
---|
[237df2f] | 36 | #include <usb/classes/classes.h>
|
---|
[53332b5b] | 37 | #include <usb/debug.h>
|
---|
[237df2f] | 38 | #include <usb/descriptor.h>
|
---|
| 39 | #include <usb/request.h>
|
---|
[8d2e251] | 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>
|
---|
[53332b5b] | 48 | #include <errno.h>
|
---|
[8d2e251] | 49 | #include <fibril_synch.h>
|
---|
[8d7552c] | 50 | #include <macros.h>
|
---|
[8d2e251] | 51 | #include <stdio.h>
|
---|
| 52 | #include <stdlib.h>
|
---|
[53332b5b] | 53 | #include <str_error.h>
|
---|
[8d2e251] | 54 | #include <usb_iface.h>
|
---|
[53332b5b] | 55 |
|
---|
| 56 | #include "ddf_helpers.h"
|
---|
| 57 |
|
---|
[237df2f] | 58 | #define CTRL_PIPE_MIN_PACKET_SIZE 8
|
---|
| 59 |
|
---|
[14dd4c9] | 60 | typedef struct usb_dev {
|
---|
| 61 | link_t link;
|
---|
[daf59d1] | 62 | list_t devices;
|
---|
[b995183] | 63 | fibril_mutex_t guard;
|
---|
[14dd4c9] | 64 | ddf_fun_t *fun;
|
---|
| 65 | usb_address_t address;
|
---|
| 66 | usb_speed_t speed;
|
---|
[fa9d3af] | 67 | usb_address_t tt_address;
|
---|
[14dd4c9] | 68 | unsigned port;
|
---|
| 69 | } usb_dev_t;
|
---|
| 70 |
|
---|
| 71 | typedef struct hc_dev {
|
---|
| 72 | ddf_fun_t *ctl_fun;
|
---|
[e991937] | 73 | hcd_t hcd;
|
---|
[14dd4c9] | 74 | usb_dev_t *root_hub;
|
---|
[53332b5b] | 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);
|
---|
[e991937] | 85 | if (!hc_dev) {
|
---|
[2b0929e] | 86 | usb_log_error("Invalid HCD device.\n");
|
---|
[53332b5b] | 87 | return NULL;
|
---|
| 88 | }
|
---|
[e991937] | 89 | return &hc_dev->hcd;
|
---|
[53332b5b] | 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
[0918382f] | 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);
|
---|
[2757247] | 95 |
|
---|
| 96 |
|
---|
| 97 | /* DDF INTERFACE */
|
---|
| 98 |
|
---|
[d2cfe72] | 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,
|
---|
[4e732f1a] | 112 | size_t max_packet_size, unsigned packets, unsigned interval)
|
---|
[d2cfe72] | 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,
|
---|
[4e732f1a] | 128 | max_packet_size, packets, size, dev->tt_address, dev->port);
|
---|
[d2cfe72] | 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 |
|
---|
[56bd6f11] | 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 |
|
---|
[0918382f] | 178 | static int device_enumerate(ddf_fun_t *fun, unsigned port)
|
---|
[56bd6f11] | 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);
|
---|
[0918382f] | 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);
|
---|
[56bd6f11] | 188 | }
|
---|
| 189 |
|
---|
[0918382f] | 190 | static int device_remove(ddf_fun_t *fun, unsigned port)
|
---|
[56bd6f11] | 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);
|
---|
[0918382f] | 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);
|
---|
[56bd6f11] | 200 | }
|
---|
| 201 |
|
---|
[3121b5f] | 202 | /** Gets handle of the respective device.
|
---|
[8e4219ab] | 203 | *
|
---|
[3121b5f] | 204 | * @param[in] fun Device function.
|
---|
[8e4219ab] | 205 | * @param[out] handle Place to write the handle.
|
---|
| 206 | * @return Error code.
|
---|
| 207 | */
|
---|
[3121b5f] | 208 | static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
[8e4219ab] | 209 | {
|
---|
| 210 | assert(fun);
|
---|
| 211 | if (handle)
|
---|
| 212 | *handle = ddf_fun_get_handle(fun);
|
---|
| 213 | return EOK;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[1845003] | 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 |
|
---|
[2757247] | 268 | /** USB device interface */
|
---|
[53332b5b] | 269 | static usb_iface_t usb_iface = {
|
---|
[3121b5f] | 270 | .get_my_device_handle = get_my_device_handle,
|
---|
[8e4219ab] | 271 |
|
---|
[56bd6f11] | 272 | .reserve_default_address = reserve_default_address,
|
---|
| 273 | .release_default_address = release_default_address,
|
---|
[4b8ecff] | 274 |
|
---|
[56bd6f11] | 275 | .device_enumerate = device_enumerate,
|
---|
| 276 | .device_remove = device_remove,
|
---|
[4b8ecff] | 277 |
|
---|
[d2cfe72] | 278 | .register_endpoint = register_endpoint,
|
---|
| 279 | .unregister_endpoint = unregister_endpoint,
|
---|
[4b8ecff] | 280 |
|
---|
[1845003] | 281 | .read = dev_read,
|
---|
| 282 | .write = dev_write,
|
---|
[53332b5b] | 283 | };
|
---|
[8e4219ab] | 284 |
|
---|
[2757247] | 285 | /** Standard USB device interface) */
|
---|
[53332b5b] | 286 | static ddf_dev_ops_t usb_ops = {
|
---|
| 287 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
| 288 | };
|
---|
| 289 |
|
---|
[2757247] | 290 |
|
---|
| 291 | /* DDF HELPERS */
|
---|
| 292 |
|
---|
[237df2f] | 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 |
|
---|
[2757247] | 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,
|
---|
[53332b5b] | 317 | const match_id_list_t *mids)
|
---|
| 318 | {
|
---|
| 319 | assert(parent);
|
---|
| 320 |
|
---|
[237df2f] | 321 | char default_name[10] = { 0 }; /* usbxyz-ss */
|
---|
[0a12879] | 322 | if (!name) {
|
---|
| 323 | snprintf(default_name, sizeof(default_name) - 1,
|
---|
[237df2f] | 324 | "usb%u-%cs", address, usb_str_speed(speed)[0]);
|
---|
[0a12879] | 325 | name = default_name;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[53332b5b] | 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;
|
---|
[2757247] | 339 | info->port = port;
|
---|
[fa9d3af] | 340 | info->tt_address = hub_dev ? hub_dev->tt_address : -1;
|
---|
[53332b5b] | 341 | link_initialize(&info->link);
|
---|
[2757247] | 342 | list_initialize(&info->devices);
|
---|
[14dd4c9] | 343 | fibril_mutex_initialize(&info->guard);
|
---|
[53332b5b] | 344 |
|
---|
[5dad73d] | 345 | if (hub_dev && hub_dev->speed == USB_SPEED_HIGH && usb_speed_is_11(speed))
|
---|
[fa9d3af] | 346 | info->tt_address = hub_dev->address;
|
---|
| 347 |
|
---|
[53332b5b] | 348 | ddf_fun_set_ops(fun, &usb_ops);
|
---|
[3f03199] | 349 | list_foreach(mids->ids, link, const match_id_t, mid) {
|
---|
[53332b5b] | 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 |
|
---|
[2757247] | 359 | if (hub_dev) {
|
---|
[14dd4c9] | 360 | fibril_mutex_lock(&hub_dev->guard);
|
---|
[2757247] | 361 | list_append(&info->link, &hub_dev->devices);
|
---|
[14dd4c9] | 362 | fibril_mutex_unlock(&hub_dev->guard);
|
---|
[2757247] | 363 | } else {
|
---|
| 364 | hc_dev_t *hc_dev = dev_to_hc_dev(parent);
|
---|
[14dd4c9] | 365 | assert(hc_dev->root_hub == NULL);
|
---|
| 366 | hc_dev->root_hub = info;
|
---|
[2757247] | 367 | }
|
---|
[53332b5b] | 368 | return EOK;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[237df2f] | 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)
|
---|
[b995183] | 389 |
|
---|
[237df2f] | 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 |
|
---|
[0ee999d] | 404 | /* Next, without release number. */
|
---|
[237df2f] | 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 |
|
---|
[2757247] | 420 | static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub,
|
---|
[0918382f] | 421 | unsigned port)
|
---|
[237df2f] | 422 | {
|
---|
| 423 | assert(device);
|
---|
| 424 |
|
---|
| 425 | hcd_t *hcd = dev_to_hcd(device);
|
---|
| 426 | assert(hcd);
|
---|
[b995183] | 427 |
|
---|
| 428 | hc_dev_t *hc_dev = dev_to_hc_dev(device);
|
---|
| 429 | assert(hc_dev);
|
---|
| 430 |
|
---|
[14dd4c9] | 431 | fibril_mutex_lock(&hub->guard);
|
---|
[b995183] | 432 |
|
---|
| 433 | usb_dev_t *victim = NULL;
|
---|
| 434 |
|
---|
[3f03199] | 435 | list_foreach(hub->devices, link, usb_dev_t, it) {
|
---|
| 436 | if (it->port == port) {
|
---|
| 437 | victim = it;
|
---|
[b995183] | 438 | break;
|
---|
[3f03199] | 439 | }
|
---|
[b995183] | 440 | }
|
---|
[9ff59981] | 441 | if (victim) {
|
---|
| 442 | assert(victim->port == port);
|
---|
[b995183] | 443 | list_remove(&victim->link);
|
---|
[14dd4c9] | 444 | fibril_mutex_unlock(&hub->guard);
|
---|
[e6becb9] | 445 | const int ret = ddf_fun_unbind(victim->fun);
|
---|
| 446 | if (ret == EOK) {
|
---|
[e657635] | 447 | usb_address_t address = victim->address;
|
---|
[e6becb9] | 448 | ddf_fun_destroy(victim->fun);
|
---|
[e657635] | 449 | hcd_release_address(hcd, address);
|
---|
[e6becb9] | 450 | } else {
|
---|
[0918382f] | 451 | usb_log_warning("Failed to unbind device `%s': %s\n",
|
---|
| 452 | ddf_fun_get_name(victim->fun), str_error(ret));
|
---|
[e6becb9] | 453 | }
|
---|
[b995183] | 454 | return EOK;
|
---|
| 455 | }
|
---|
[9ff59981] | 456 | fibril_mutex_unlock(&hub->guard);
|
---|
[b995183] | 457 | return ENOENT;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[0918382f] | 460 | static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port)
|
---|
[b995183] | 461 | {
|
---|
| 462 | assert(device);
|
---|
| 463 |
|
---|
| 464 | hcd_t *hcd = dev_to_hcd(device);
|
---|
| 465 | assert(hcd);
|
---|
| 466 |
|
---|
[237df2f] | 467 | usb_speed_t speed = USB_SPEED_MAX;
|
---|
| 468 |
|
---|
| 469 | /* This checks whether the default address is reserved and gets speed */
|
---|
[4cf5b8e0] | 470 | int ret = usb_bus_get_speed(&hcd->bus, USB_ADDRESS_DEFAULT, &speed);
|
---|
[237df2f] | 471 | if (ret != EOK) {
|
---|
[f29643d5] | 472 | usb_log_error("Failed to verify speed: %s.", str_error(ret));
|
---|
[237df2f] | 473 | return ret;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[f29643d5] | 476 | usb_log_debug("Found new %s speed USB device.", usb_str_speed(speed));
|
---|
[c86ca9a] | 477 |
|
---|
[237df2f] | 478 | static const usb_target_t default_target = {{
|
---|
| 479 | .address = USB_ADDRESS_DEFAULT,
|
---|
| 480 | .endpoint = 0,
|
---|
| 481 | }};
|
---|
| 482 |
|
---|
| 483 | const usb_address_t address = hcd_request_address(hcd, speed);
|
---|
[f29643d5] | 484 | if (address < 0) {
|
---|
| 485 | usb_log_error("Failed to reserve new address: %s.",
|
---|
| 486 | str_error(address));
|
---|
[237df2f] | 487 | return address;
|
---|
[f29643d5] | 488 | }
|
---|
[237df2f] | 489 |
|
---|
[c86ca9a] | 490 | usb_log_debug("Reserved new address: %d\n", address);
|
---|
| 491 |
|
---|
[237df2f] | 492 | const usb_target_t target = {{
|
---|
| 493 | .address = address,
|
---|
| 494 | .endpoint = 0,
|
---|
| 495 | }};
|
---|
| 496 |
|
---|
[0ee999d] | 497 | const usb_address_t tt_address = hub ? hub->tt_address : -1;
|
---|
| 498 |
|
---|
[237df2f] | 499 | /* Add default pipe on default address */
|
---|
[c86ca9a] | 500 | usb_log_debug("Device(%d): Adding default target(0:0)\n", address);
|
---|
[ec6766a] | 501 | ret = hcd_add_ep(hcd,
|
---|
[237df2f] | 502 | default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
|
---|
[4e732f1a] | 503 | CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE, 1,
|
---|
[0ee999d] | 504 | tt_address, port);
|
---|
[237df2f] | 505 | if (ret != EOK) {
|
---|
[f29643d5] | 506 | usb_log_error("Device(%d): Failed to add default target: %s.",
|
---|
| 507 | address, str_error(ret));
|
---|
[237df2f] | 508 | hcd_release_address(hcd, address);
|
---|
| 509 | return ret;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
| 512 | /* Get max packet size for default pipe */
|
---|
| 513 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
[2003739] | 514 | const usb_device_request_setup_packet_t get_device_desc_8 =
|
---|
[237df2f] | 515 | GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
|
---|
| 516 |
|
---|
| 517 | // TODO CALLBACKS
|
---|
[f29643d5] | 518 | usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
|
---|
[c86ca9a] | 519 | address);
|
---|
[237df2f] | 520 | ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
|
---|
| 521 | &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
|
---|
| 522 | "read first 8 bytes of dev descriptor");
|
---|
| 523 |
|
---|
| 524 | if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
|
---|
[f29643d5] | 525 | ret = got < 0 ? got : EOVERFLOW;
|
---|
| 526 | usb_log_error("Device(%d): Failed to get 8B of dev descr: %s.",
|
---|
| 527 | address, str_error(ret));
|
---|
[237df2f] | 528 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
---|
| 529 | hcd_release_address(hcd, address);
|
---|
[f29643d5] | 530 | return ret;
|
---|
[237df2f] | 531 | }
|
---|
| 532 |
|
---|
| 533 | /* Register EP on the new address */
|
---|
[f29643d5] | 534 | usb_log_debug("Device(%d): Registering control EP.", address);
|
---|
[237df2f] | 535 | ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
|
---|
[4e732f1a] | 536 | ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
|
---|
| 537 | ED_MPS_TRANS_OPPORTUNITIES_GET(uint16_usb2host(desc.max_packet_size)),
|
---|
| 538 | ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
|
---|
| 539 | tt_address, port);
|
---|
[237df2f] | 540 | if (ret != EOK) {
|
---|
[f29643d5] | 541 | usb_log_error("Device(%d): Failed to register EP0: %s",
|
---|
| 542 | address, str_error(ret));
|
---|
[237df2f] | 543 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
---|
| 544 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
| 545 | hcd_release_address(hcd, address);
|
---|
| 546 | return ret;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | /* Set new address */
|
---|
| 550 | const usb_device_request_setup_packet_t set_address =
|
---|
| 551 | SET_ADDRESS(target.address);
|
---|
| 552 |
|
---|
[f29643d5] | 553 | usb_log_debug("Device(%d): Setting USB address.", address);
|
---|
[237df2f] | 554 | got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
|
---|
| 555 | NULL, 0, *(uint64_t *)&set_address, "set address");
|
---|
| 556 |
|
---|
[f29643d5] | 557 | usb_log_debug("Device(%d): Removing default (0:0) EP.", address);
|
---|
[237df2f] | 558 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
---|
| 559 |
|
---|
| 560 | if (got != 0) {
|
---|
[f29643d5] | 561 | usb_log_error("Device(%d): Failed to set new address: %s.",
|
---|
| 562 | address, str_error(got));
|
---|
[237df2f] | 563 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
| 564 | hcd_release_address(hcd, address);
|
---|
| 565 | return got;
|
---|
| 566 | }
|
---|
[2003739] | 567 |
|
---|
[237df2f] | 568 | /* Get std device descriptor */
|
---|
[2003739] | 569 | const usb_device_request_setup_packet_t get_device_desc =
|
---|
[237df2f] | 570 | GET_DEVICE_DESC(sizeof(desc));
|
---|
| 571 |
|
---|
[f29643d5] | 572 | usb_log_debug("Device(%d): Requesting full device descriptor.",
|
---|
[c86ca9a] | 573 | address);
|
---|
[237df2f] | 574 | got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
|
---|
| 575 | &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
---|
| 576 | "read device descriptor");
|
---|
| 577 | if (ret != EOK) {
|
---|
[f29643d5] | 578 | usb_log_error("Device(%d): Failed to set get dev descriptor: %s",
|
---|
| 579 | address, str_error(ret));
|
---|
[237df2f] | 580 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
| 581 | hcd_release_address(hcd, target.address);
|
---|
[f29643d5] | 582 | return ret;
|
---|
[237df2f] | 583 | }
|
---|
[b995183] | 584 |
|
---|
[237df2f] | 585 | /* Create match ids from the device descriptor */
|
---|
| 586 | match_id_list_t mids;
|
---|
| 587 | init_match_ids(&mids);
|
---|
| 588 |
|
---|
[f29643d5] | 589 | usb_log_debug("Device(%d): Creating match IDs.", address);
|
---|
[237df2f] | 590 | ret = create_match_ids(&mids, &desc);
|
---|
| 591 | if (ret != EOK) {
|
---|
[f29643d5] | 592 | usb_log_error("Device(%d): Failed to create match ids: %s",
|
---|
| 593 | address, str_error(ret));
|
---|
[237df2f] | 594 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
| 595 | hcd_release_address(hcd, target.address);
|
---|
| 596 | return ret;
|
---|
| 597 | }
|
---|
| 598 |
|
---|
[b995183] | 599 | /* Register device */
|
---|
[f29643d5] | 600 | usb_log_debug("Device(%d): Registering DDF device.", address);
|
---|
[2757247] | 601 | ret = hcd_ddf_add_device(device, hub, port, address, speed, NULL, &mids);
|
---|
[237df2f] | 602 | clean_match_ids(&mids);
|
---|
| 603 | if (ret != EOK) {
|
---|
[f29643d5] | 604 | usb_log_error("Device(%d): Failed to register: %s.",
|
---|
| 605 | address, str_error(ret));
|
---|
[237df2f] | 606 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
| 607 | hcd_release_address(hcd, target.address);
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | return ret;
|
---|
| 611 | }
|
---|
| 612 |
|
---|
| 613 | /** Announce root hub to the DDF
|
---|
| 614 | *
|
---|
| 615 | * @param[in] device Host controller ddf device
|
---|
| 616 | * @return Error code
|
---|
| 617 | */
|
---|
[cb8ede1] | 618 | int hcd_ddf_setup_root_hub(ddf_dev_t *device)
|
---|
[237df2f] | 619 | {
|
---|
| 620 | assert(device);
|
---|
| 621 | hcd_t *hcd = dev_to_hcd(device);
|
---|
| 622 | assert(hcd);
|
---|
| 623 |
|
---|
[4cf5b8e0] | 624 | hcd_reserve_default_address(hcd, hcd->bus.max_speed);
|
---|
[0918382f] | 625 | const int ret = hcd_ddf_new_device(device, NULL, 0);
|
---|
[237df2f] | 626 | hcd_release_default_address(hcd);
|
---|
| 627 | return ret;
|
---|
| 628 | }
|
---|
| 629 |
|
---|
[53332b5b] | 630 | /** Initialize hc structures.
|
---|
| 631 | *
|
---|
| 632 | * @param[in] device DDF instance of the device to use.
|
---|
[ce33c10] | 633 | * @param[in] max_speed Maximum supported USB speed.
|
---|
| 634 | * @param[in] bw available bandwidth.
|
---|
| 635 | * @param[in] bw_count Function to compute required ep bandwidth.
|
---|
[53332b5b] | 636 | *
|
---|
[ce33c10] | 637 | * @return Error code.
|
---|
[2b0929e] | 638 | * This function does all the ddf work for hc driver.
|
---|
[53332b5b] | 639 | */
|
---|
[e991937] | 640 | int hcd_ddf_setup_hc(ddf_dev_t *device, usb_speed_t max_speed,
|
---|
| 641 | size_t bw, bw_count_func_t bw_count)
|
---|
[53332b5b] | 642 | {
|
---|
[e991937] | 643 | assert(device);
|
---|
[53332b5b] | 644 |
|
---|
| 645 | hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
|
---|
| 646 | if (instance == NULL) {
|
---|
[2b0929e] | 647 | usb_log_error("Failed to allocate HCD ddf structure.\n");
|
---|
[53332b5b] | 648 | return ENOMEM;
|
---|
| 649 | }
|
---|
[14dd4c9] | 650 | instance->root_hub = NULL;
|
---|
[e991937] | 651 | hcd_init(&instance->hcd, max_speed, bw, bw_count);
|
---|
[53332b5b] | 652 |
|
---|
[e991937] | 653 | int ret = ENOMEM;
|
---|
| 654 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
---|
| 655 | if (!instance->ctl_fun) {
|
---|
[cce3228] | 656 | usb_log_error("Failed to create HCD ddf fun.\n");
|
---|
| 657 | goto err_destroy_fun;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
[e991937] | 660 | ret = ddf_fun_bind(instance->ctl_fun);
|
---|
[cce3228] | 661 | if (ret != EOK) {
|
---|
[e991937] | 662 | usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
|
---|
[cce3228] | 663 | goto err_destroy_fun;
|
---|
| 664 | }
|
---|
| 665 |
|
---|
[e991937] | 666 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
---|
[cce3228] | 667 | if (ret != EOK) {
|
---|
| 668 | usb_log_error("Failed to add fun to category: %s.\n",
|
---|
| 669 | str_error(ret));
|
---|
[e991937] | 670 | ddf_fun_unbind(instance->ctl_fun);
|
---|
[cce3228] | 671 | goto err_destroy_fun;
|
---|
| 672 | }
|
---|
[53332b5b] | 673 |
|
---|
| 674 | /* HC should be ok at this point (except it can't do anything) */
|
---|
| 675 | return EOK;
|
---|
[cce3228] | 676 |
|
---|
| 677 | err_destroy_fun:
|
---|
[e991937] | 678 | ddf_fun_destroy(instance->ctl_fun);
|
---|
| 679 | instance->ctl_fun = NULL;
|
---|
[cce3228] | 680 | return ret;
|
---|
[53332b5b] | 681 | }
|
---|
| 682 |
|
---|
[e991937] | 683 | void hcd_ddf_clean_hc(ddf_dev_t *device)
|
---|
| 684 | {
|
---|
| 685 | assert(device);
|
---|
| 686 | hc_dev_t *hc = dev_to_hc_dev(device);
|
---|
| 687 | assert(hc);
|
---|
| 688 | const int ret = ddf_fun_unbind(hc->ctl_fun);
|
---|
| 689 | if (ret == EOK)
|
---|
| 690 | ddf_fun_destroy(hc->ctl_fun);
|
---|
| 691 | }
|
---|
[57c8fc9] | 692 |
|
---|
[2bdf92a5] | 693 | //TODO: Cache parent session in HCD
|
---|
[cccd60c3] | 694 | /** Call the parent driver with a request to enable interrupt
|
---|
[57c8fc9] | 695 | *
|
---|
| 696 | * @param[in] device Device asking for interrupts
|
---|
[cccd60c3] | 697 | * @param[in] inum Interrupt number
|
---|
[57c8fc9] | 698 | * @return Error code.
|
---|
| 699 | */
|
---|
[cccd60c3] | 700 | int hcd_ddf_enable_interrupt(ddf_dev_t *device, int inum)
|
---|
[57c8fc9] | 701 | {
|
---|
[2bdf92a5] | 702 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
---|
| 703 | if (parent_sess == NULL)
|
---|
| 704 | return EIO;
|
---|
| 705 |
|
---|
[cccd60c3] | 706 | return hw_res_enable_interrupt(parent_sess, inum);
|
---|
[57c8fc9] | 707 | }
|
---|
| 708 |
|
---|
[2bdf92a5] | 709 | //TODO: Cache parent session in HCD
|
---|
[57c8fc9] | 710 | int hcd_ddf_get_registers(ddf_dev_t *device, hw_res_list_parsed_t *hw_res)
|
---|
| 711 | {
|
---|
[2bdf92a5] | 712 | async_sess_t *parent_sess = ddf_dev_parent_sess_get(device);
|
---|
| 713 | if (parent_sess == NULL)
|
---|
| 714 | return EIO;
|
---|
[57c8fc9] | 715 |
|
---|
| 716 | hw_res_list_parsed_init(hw_res);
|
---|
| 717 | const int ret = hw_res_get_list_parsed(parent_sess, hw_res, 0);
|
---|
[c898236] | 718 | if (ret != EOK)
|
---|
| 719 | hw_res_list_parsed_clean(hw_res);
|
---|
[57c8fc9] | 720 | return ret;
|
---|
| 721 | }
|
---|
[19d21728] | 722 |
|
---|
| 723 | // TODO: move this someplace else
|
---|
| 724 | static inline void irq_code_clean(irq_code_t *code)
|
---|
| 725 | {
|
---|
| 726 | if (code) {
|
---|
| 727 | free(code->ranges);
|
---|
| 728 | free(code->cmds);
|
---|
| 729 | code->ranges = NULL;
|
---|
| 730 | code->cmds = NULL;
|
---|
| 731 | code->rangecount = 0;
|
---|
| 732 | code->cmdcount = 0;
|
---|
| 733 | }
|
---|
| 734 | }
|
---|
| 735 |
|
---|
| 736 | /** Register interrupt handler
|
---|
| 737 | *
|
---|
| 738 | * @param[in] device Host controller DDF device
|
---|
| 739 | * @param[in] regs Register range
|
---|
| 740 | * @param[in] irq Interrupt number
|
---|
| 741 | * @paran[in] handler Interrupt handler
|
---|
| 742 | * @param[in] gen_irq_code IRQ code generator.
|
---|
| 743 | *
|
---|
[3f74275] | 744 | * @return IRQ capability handle on success.
|
---|
[e9d15d9] | 745 | * @return Negative error code.
|
---|
[19d21728] | 746 | */
|
---|
[ba4a03a5] | 747 | int hcd_ddf_setup_interrupts(ddf_dev_t *device,
|
---|
| 748 | const hw_res_list_parsed_t *hw_res,
|
---|
[19d21728] | 749 | interrupt_handler_t handler,
|
---|
[ba4a03a5] | 750 | int (*gen_irq_code)(irq_code_t *, const hw_res_list_parsed_t *hw_res))
|
---|
[19d21728] | 751 | {
|
---|
| 752 |
|
---|
| 753 | assert(device);
|
---|
[3e200736] | 754 | if (!handler || !gen_irq_code)
|
---|
| 755 | return ENOTSUP;
|
---|
[19d21728] | 756 |
|
---|
| 757 | irq_code_t irq_code = {0};
|
---|
| 758 |
|
---|
[d1df381] | 759 | const int irq = gen_irq_code(&irq_code, hw_res);
|
---|
[ba4a03a5] | 760 | if (irq < 0) {
|
---|
[19d21728] | 761 | usb_log_error("Failed to generate IRQ code: %s.\n",
|
---|
[ba4a03a5] | 762 | str_error(irq));
|
---|
| 763 | return irq;
|
---|
[19d21728] | 764 | }
|
---|
| 765 |
|
---|
| 766 | /* Register handler to avoid interrupt lockup */
|
---|
[e9d15d9] | 767 | const int irq_cap = register_interrupt_handler(device, irq, handler,
|
---|
| 768 | &irq_code);
|
---|
[19d21728] | 769 | irq_code_clean(&irq_code);
|
---|
[e9d15d9] | 770 | if (irq_cap < 0) {
|
---|
[19d21728] | 771 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
---|
[e9d15d9] | 772 | str_error(irq_cap));
|
---|
| 773 | return irq_cap;
|
---|
[19d21728] | 774 | }
|
---|
| 775 |
|
---|
| 776 | /* Enable interrupts */
|
---|
[cccd60c3] | 777 | int ret = hcd_ddf_enable_interrupt(device, irq);
|
---|
[19d21728] | 778 | if (ret != EOK) {
|
---|
| 779 | usb_log_error("Failed to register interrupt handler: %s.\n",
|
---|
| 780 | str_error(ret));
|
---|
[e9d15d9] | 781 | unregister_interrupt_handler(device, irq_cap);
|
---|
[19d21728] | 782 | return ret;
|
---|
| 783 | }
|
---|
[e9d15d9] | 784 | return irq_cap;
|
---|
[19d21728] | 785 | }
|
---|
[7191992] | 786 |
|
---|
| 787 | /** IRQ handling callback, forward status from call to diver structure.
|
---|
| 788 | *
|
---|
| 789 | * @param[in] dev DDF instance of the device to use.
|
---|
| 790 | * @param[in] iid (Unused).
|
---|
| 791 | * @param[in] call Pointer to the call from kernel.
|
---|
| 792 | */
|
---|
[8e7c9fe] | 793 | void ddf_hcd_gen_irq_handler(ipc_callid_t iid, ipc_call_t *call, ddf_dev_t *dev)
|
---|
[7191992] | 794 | {
|
---|
| 795 | assert(dev);
|
---|
| 796 | hcd_t *hcd = dev_to_hcd(dev);
|
---|
[b5f813c] | 797 | if (!hcd || !hcd->ops.irq_hook) {
|
---|
[7191992] | 798 | usb_log_error("Interrupt on not yet initialized device.\n");
|
---|
| 799 | return;
|
---|
| 800 | }
|
---|
| 801 | const uint32_t status = IPC_GET_ARG1(*call);
|
---|
[b5f813c] | 802 | hcd->ops.irq_hook(hcd, status);
|
---|
[7191992] | 803 | }
|
---|
[3e200736] | 804 |
|
---|
| 805 | static int interrupt_polling(void *arg)
|
---|
| 806 | {
|
---|
| 807 | hcd_t *hcd = arg;
|
---|
| 808 | assert(hcd);
|
---|
[b5f813c] | 809 | if (!hcd->ops.status_hook || !hcd->ops.irq_hook)
|
---|
[3e200736] | 810 | return ENOTSUP;
|
---|
| 811 | uint32_t status = 0;
|
---|
[b5f813c] | 812 | while (hcd->ops.status_hook(hcd, &status) == EOK) {
|
---|
| 813 | hcd->ops.irq_hook(hcd, status);
|
---|
[3e200736] | 814 | status = 0;
|
---|
| 815 | /* We should wait 1 frame - 1ms here, but this polling is a
|
---|
| 816 | * lame crutch anyway so don't hog the system. 10ms is still
|
---|
| 817 | * good enough for emergency mode */
|
---|
| 818 | async_usleep(10000);
|
---|
| 819 | }
|
---|
| 820 | return EOK;
|
---|
| 821 | }
|
---|
| 822 |
|
---|
[7191992] | 823 | /** Initialize hc and rh DDF structures and their respective drivers.
|
---|
| 824 | *
|
---|
| 825 | * @param device DDF instance of the device to use
|
---|
| 826 | * @param speed Maximum supported speed
|
---|
| 827 | * @param bw Available bandwidth (arbitrary units)
|
---|
| 828 | * @param bw_count Bandwidth computing function
|
---|
| 829 | * @param irq_handler IRQ handling function
|
---|
| 830 | * @param gen_irq_code Function to generate IRQ pseudocode
|
---|
| 831 | * (it needs to return used irq number)
|
---|
| 832 | * @param driver_init Function to initialize HC driver
|
---|
| 833 | * @param driver_fini Function to cleanup HC driver
|
---|
| 834 | * @return Error code
|
---|
| 835 | *
|
---|
| 836 | * This function does all the preparatory work for hc and rh drivers:
|
---|
| 837 | * - gets device's hw resources
|
---|
| 838 | * - attempts to enable interrupts
|
---|
| 839 | * - registers interrupt handler
|
---|
| 840 | * - calls driver specific initialization
|
---|
| 841 | * - registers root hub
|
---|
| 842 | */
|
---|
[d51ba359] | 843 | int hcd_ddf_add_hc(ddf_dev_t *device, const ddf_hc_driver_t *driver)
|
---|
[7191992] | 844 | {
|
---|
[d51ba359] | 845 | assert(driver);
|
---|
| 846 | static const struct { size_t bw; bw_count_func_t bw_count; }bw[] = {
|
---|
| 847 | [USB_SPEED_FULL] = { .bw = BANDWIDTH_AVAILABLE_USB11,
|
---|
| 848 | .bw_count = bandwidth_count_usb11 },
|
---|
| 849 | [USB_SPEED_HIGH] = { .bw = BANDWIDTH_AVAILABLE_USB11,
|
---|
| 850 | .bw_count = bandwidth_count_usb11 },
|
---|
| 851 | };
|
---|
| 852 |
|
---|
| 853 | int ret = EOK;
|
---|
| 854 | const usb_speed_t speed = driver->hc_speed;
|
---|
| 855 | if (speed >= ARRAY_SIZE(bw) || bw[speed].bw == 0) {
|
---|
| 856 | usb_log_error("Driver `%s' reported unsupported speed: %s",
|
---|
| 857 | driver->name, usb_str_speed(speed));
|
---|
| 858 | return ENOTSUP;
|
---|
| 859 | }
|
---|
| 860 |
|
---|
[7191992] | 861 | hw_res_list_parsed_t hw_res;
|
---|
[d51ba359] | 862 | ret = hcd_ddf_get_registers(device, &hw_res);
|
---|
[7191992] | 863 | if (ret != EOK) {
|
---|
| 864 | usb_log_error("Failed to get register memory addresses "
|
---|
[d51ba359] | 865 | "for `%s': %s.\n", ddf_dev_get_name(device),
|
---|
[7191992] | 866 | str_error(ret));
|
---|
| 867 | return ret;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
[d51ba359] | 870 | ret = hcd_ddf_setup_hc(device, speed, bw[speed].bw, bw[speed].bw_count);
|
---|
[7191992] | 871 | if (ret != EOK) {
|
---|
| 872 | usb_log_error("Failed to setup generic HCD.\n");
|
---|
| 873 | hw_res_list_parsed_clean(&hw_res);
|
---|
| 874 | return ret;
|
---|
| 875 | }
|
---|
| 876 |
|
---|
[d51ba359] | 877 | interrupt_handler_t *irq_handler =
|
---|
| 878 | driver->irq_handler ? driver->irq_handler : ddf_hcd_gen_irq_handler;
|
---|
[e9d15d9] | 879 | const int irq_cap = hcd_ddf_setup_interrupts(device, &hw_res,
|
---|
| 880 | irq_handler, driver->irq_code_gen);
|
---|
| 881 | bool irqs_enabled = !(irq_cap < 0);
|
---|
| 882 | if (irqs_enabled) {
|
---|
[7191992] | 883 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
| 884 | }
|
---|
| 885 |
|
---|
[f29643d5] | 886 | if (driver->claim)
|
---|
| 887 | ret = driver->claim(device);
|
---|
| 888 | if (ret != EOK) {
|
---|
| 889 | usb_log_error("Failed to claim `%s' for driver `%s'",
|
---|
| 890 | ddf_dev_get_name(device), driver->name);
|
---|
| 891 | return ret;
|
---|
| 892 | }
|
---|
| 893 |
|
---|
| 894 |
|
---|
[7191992] | 895 | /* Init hw driver */
|
---|
[3e200736] | 896 | hcd_t *hcd = dev_to_hcd(device);
|
---|
[e9d15d9] | 897 | ret = driver->init(hcd, &hw_res, irqs_enabled);
|
---|
[7191992] | 898 | hw_res_list_parsed_clean(&hw_res);
|
---|
| 899 | if (ret != EOK) {
|
---|
[c86ca9a] | 900 | usb_log_error("Failed to init HCD: %s.\n", str_error(ret));
|
---|
[7191992] | 901 | goto irq_unregister;
|
---|
| 902 | }
|
---|
| 903 |
|
---|
[3e200736] | 904 | /* Need working irq replacement to setup root hub */
|
---|
[e9d15d9] | 905 | if (!irqs_enabled && hcd->ops.status_hook) {
|
---|
[3e200736] | 906 | hcd->polling_fibril = fibril_create(interrupt_polling, hcd);
|
---|
| 907 | if (hcd->polling_fibril == 0) {
|
---|
| 908 | usb_log_error("Failed to create polling fibril\n");
|
---|
| 909 | ret = ENOMEM;
|
---|
| 910 | goto irq_unregister;
|
---|
| 911 | }
|
---|
| 912 | fibril_add_ready(hcd->polling_fibril);
|
---|
| 913 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
[e9d15d9] | 914 | " Falling back to polling.\n", str_error(irq_cap));
|
---|
[3e200736] | 915 | }
|
---|
| 916 |
|
---|
[7191992] | 917 | /*
|
---|
| 918 | * Creating root hub registers a new USB device so HC
|
---|
| 919 | * needs to be ready at this time.
|
---|
| 920 | */
|
---|
| 921 | ret = hcd_ddf_setup_root_hub(device);
|
---|
| 922 | if (ret != EOK) {
|
---|
[c86ca9a] | 923 | usb_log_error("Failed to setup HC root hub: %s.\n",
|
---|
[7191992] | 924 | str_error(ret));
|
---|
[d51ba359] | 925 | driver->fini(dev_to_hcd(device));
|
---|
[7191992] | 926 | irq_unregister:
|
---|
| 927 | /* Unregistering non-existent should be ok */
|
---|
[e9d15d9] | 928 | unregister_interrupt_handler(device, irq_cap);
|
---|
[7191992] | 929 | hcd_ddf_clean_hc(device);
|
---|
[d51ba359] | 930 | return ret;
|
---|
[7191992] | 931 | }
|
---|
[d51ba359] | 932 |
|
---|
| 933 | usb_log_info("Controlling new `%s' device `%s'.\n",
|
---|
| 934 | driver->name, ddf_dev_get_name(device));
|
---|
| 935 | return EOK;
|
---|
[7191992] | 936 | }
|
---|
[53332b5b] | 937 | /**
|
---|
| 938 | * @}
|
---|
| 939 | */
|
---|