| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Vojtech Horky
|
|---|
| 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 libusb usb
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @brief HC driver and hub driver (implementation).
|
|---|
| 34 | */
|
|---|
| 35 | #include <usb/hcdhubd.h>
|
|---|
| 36 | #include <usb_iface.h>
|
|---|
| 37 | #include <driver.h>
|
|---|
| 38 | #include <bool.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 |
|
|---|
| 41 | /** List of handled host controllers. */
|
|---|
| 42 | static LIST_INITIALIZE(hc_list);
|
|---|
| 43 |
|
|---|
| 44 | /** Our HC driver. */
|
|---|
| 45 | static usb_hc_driver_t *hc_driver = NULL;
|
|---|
| 46 |
|
|---|
| 47 | static usb_iface_t usb_interface = {
|
|---|
| 48 | .interrupt_out = NULL,
|
|---|
| 49 | .interrupt_in = NULL
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | static device_ops_t usb_device_ops = {
|
|---|
| 53 | .interfaces[USB_DEV_IFACE] = &usb_interface
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | /** Callback when new device is detected and must be handled by this driver.
|
|---|
| 57 | *
|
|---|
| 58 | * @param dev New device.
|
|---|
| 59 | * @return Error code.
|
|---|
| 60 | */
|
|---|
| 61 | static int add_device(device_t *dev)
|
|---|
| 62 | {
|
|---|
| 63 | /*
|
|---|
| 64 | * FIXME: use some magic to determine whether hub or another HC
|
|---|
| 65 | * was connected.
|
|---|
| 66 | */
|
|---|
| 67 | bool is_hc = true;
|
|---|
| 68 |
|
|---|
| 69 | if (is_hc) {
|
|---|
| 70 | /*
|
|---|
| 71 | * We are the HC itself.
|
|---|
| 72 | */
|
|---|
| 73 | usb_hc_device_t *hc_dev = malloc(sizeof(usb_hc_device_t));
|
|---|
| 74 | list_initialize(&hc_dev->link);
|
|---|
| 75 | hc_dev->transfer_ops = NULL;
|
|---|
| 76 |
|
|---|
| 77 | hc_dev->generic = dev;
|
|---|
| 78 | dev->ops = &usb_device_ops;
|
|---|
| 79 | hc_dev->generic->driver_data = hc_dev;
|
|---|
| 80 |
|
|---|
| 81 | int rc = hc_driver->add_hc(hc_dev);
|
|---|
| 82 | if (rc != EOK) {
|
|---|
| 83 | free(hc_dev);
|
|---|
| 84 | return rc;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*
|
|---|
| 88 | * FIXME: The following line causes devman to hang.
|
|---|
| 89 | * Will investigate later why.
|
|---|
| 90 | */
|
|---|
| 91 | // add_device_to_class(dev, "usbhc");
|
|---|
| 92 |
|
|---|
| 93 | list_append(&hc_dev->link, &hc_list);
|
|---|
| 94 |
|
|---|
| 95 | return EOK;
|
|---|
| 96 | } else {
|
|---|
| 97 | /*
|
|---|
| 98 | * We are some (probably deeply nested) hub.
|
|---|
| 99 | * Thus, assign our own operations and explore already
|
|---|
| 100 | * connected devices.
|
|---|
| 101 | */
|
|---|
| 102 | return ENOTSUP;
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Check changes on all known hubs.
|
|---|
| 107 | */
|
|---|
| 108 | static void check_hub_changes(void)
|
|---|
| 109 | {
|
|---|
| 110 | /*
|
|---|
| 111 | * Iterate through all HCs.
|
|---|
| 112 | */
|
|---|
| 113 | link_t *link_hc;
|
|---|
| 114 | for (link_hc = hc_list.next;
|
|---|
| 115 | link_hc != &hc_list;
|
|---|
| 116 | link_hc = link_hc->next) {
|
|---|
| 117 | usb_hc_device_t *hc = list_get_instance(link_hc,
|
|---|
| 118 | usb_hc_device_t, link);
|
|---|
| 119 | /*
|
|---|
| 120 | * Iterate through all their hubs.
|
|---|
| 121 | */
|
|---|
| 122 | link_t *link_hub;
|
|---|
| 123 | for (link_hub = hc->hubs.next;
|
|---|
| 124 | link_hub != &hc->hubs;
|
|---|
| 125 | link_hub = link_hub->next) {
|
|---|
| 126 | usb_hcd_hub_info_t *hub = list_get_instance(link_hub,
|
|---|
| 127 | usb_hcd_hub_info_t, link);
|
|---|
| 128 |
|
|---|
| 129 | /*
|
|---|
| 130 | * Check status change pipe of this hub.
|
|---|
| 131 | */
|
|---|
| 132 | usb_target_t target = {
|
|---|
| 133 | .address = hub->device->address,
|
|---|
| 134 | .endpoint = 1
|
|---|
| 135 | };
|
|---|
| 136 |
|
|---|
| 137 | // FIXME: count properly
|
|---|
| 138 | size_t byte_length = (hub->port_count / 8) + 1;
|
|---|
| 139 |
|
|---|
| 140 | void *change_bitmap = malloc(byte_length);
|
|---|
| 141 | size_t actual_size;
|
|---|
| 142 | usb_handle_t handle;
|
|---|
| 143 |
|
|---|
| 144 | /*
|
|---|
| 145 | * Send the request.
|
|---|
| 146 | * FIXME: check returned value for possible errors
|
|---|
| 147 | */
|
|---|
| 148 | usb_hc_async_interrupt_in(hc, target,
|
|---|
| 149 | change_bitmap, byte_length, &actual_size,
|
|---|
| 150 | &handle);
|
|---|
| 151 |
|
|---|
| 152 | usb_hc_async_wait_for(handle);
|
|---|
| 153 |
|
|---|
| 154 | /*
|
|---|
| 155 | * TODO: handle the changes.
|
|---|
| 156 | */
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /** Operations for combined HC and HUB driver. */
|
|---|
| 162 | static driver_ops_t hc_driver_generic_ops = {
|
|---|
| 163 | .add_device = add_device
|
|---|
| 164 | };
|
|---|
| 165 |
|
|---|
| 166 | /** Combined HC and HUB driver. */
|
|---|
| 167 | static driver_t hc_driver_generic = {
|
|---|
| 168 | .driver_ops = &hc_driver_generic_ops
|
|---|
| 169 | };
|
|---|
| 170 |
|
|---|
| 171 | /** Main USB host controller driver routine.
|
|---|
| 172 | *
|
|---|
| 173 | * @see driver_main
|
|---|
| 174 | *
|
|---|
| 175 | * @param hc Host controller driver.
|
|---|
| 176 | * @return Error code.
|
|---|
| 177 | */
|
|---|
| 178 | int usb_hcd_main(usb_hc_driver_t *hc)
|
|---|
| 179 | {
|
|---|
| 180 | hc_driver = hc;
|
|---|
| 181 | hc_driver_generic.name = hc->name;
|
|---|
| 182 |
|
|---|
| 183 | /*
|
|---|
| 184 | * Launch here fibril that will periodically check all
|
|---|
| 185 | * attached hubs for status change.
|
|---|
| 186 | * WARN: This call will effectively do nothing.
|
|---|
| 187 | */
|
|---|
| 188 | check_hub_changes();
|
|---|
| 189 |
|
|---|
| 190 | /*
|
|---|
| 191 | * Run the device driver framework.
|
|---|
| 192 | */
|
|---|
| 193 | return driver_main(&hc_driver_generic);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** Add a root hub for given host controller.
|
|---|
| 197 | * This function shall be called only once for each host controller driven
|
|---|
| 198 | * by this driver.
|
|---|
| 199 | * It takes care of creating child device - hub - that will be driven by
|
|---|
| 200 | * this task.
|
|---|
| 201 | *
|
|---|
| 202 | * @param dev Host controller device.
|
|---|
| 203 | * @return Error code.
|
|---|
| 204 | */
|
|---|
| 205 | int usb_hcd_add_root_hub(usb_hc_device_t *dev)
|
|---|
| 206 | {
|
|---|
| 207 | int rc;
|
|---|
| 208 |
|
|---|
| 209 | /*
|
|---|
| 210 | * For testing/debugging purposes only.
|
|---|
| 211 | * Try to send some data to default USB address.
|
|---|
| 212 | */
|
|---|
| 213 | usb_target_t target = {0, 0};
|
|---|
| 214 | usb_handle_t handle = 0;
|
|---|
| 215 | char *data = (char *) "Hello, World!";
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 | (void)usb_hc_async_interrupt_out(dev, target, data, str_length(data), &handle);
|
|---|
| 219 | (void)usb_hc_async_wait_for(handle);
|
|---|
| 220 |
|
|---|
| 221 | /*
|
|---|
| 222 | * Announce presence of child device.
|
|---|
| 223 | */
|
|---|
| 224 | device_t *hub = NULL;
|
|---|
| 225 | match_id_t *match_id = NULL;
|
|---|
| 226 |
|
|---|
| 227 | hub = create_device();
|
|---|
| 228 | if (hub == NULL) {
|
|---|
| 229 | rc = ENOMEM;
|
|---|
| 230 | goto failure;
|
|---|
| 231 | }
|
|---|
| 232 | hub->name = "usbhub";
|
|---|
| 233 |
|
|---|
| 234 | match_id = create_match_id();
|
|---|
| 235 | if (match_id == NULL) {
|
|---|
| 236 | rc = ENOMEM;
|
|---|
| 237 | goto failure;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | char *id;
|
|---|
| 241 | rc = asprintf(&id, "usb&hc=%s&hub", dev->generic->name);
|
|---|
| 242 | if (rc <= 0) {
|
|---|
| 243 | rc = ENOMEM;
|
|---|
| 244 | goto failure;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | match_id->id = id;
|
|---|
| 248 | match_id->score = 30;
|
|---|
| 249 |
|
|---|
| 250 | add_match_id(&hub->match_ids, match_id);
|
|---|
| 251 |
|
|---|
| 252 | rc = child_device_register(hub, dev->generic);
|
|---|
| 253 | if (rc != EOK) {
|
|---|
| 254 | goto failure;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | printf("%s: registered root hub\n", dev->generic->name);
|
|---|
| 258 | return EOK;
|
|---|
| 259 |
|
|---|
| 260 | failure:
|
|---|
| 261 | if (hub != NULL) {
|
|---|
| 262 | hub->name = NULL;
|
|---|
| 263 | delete_device(hub);
|
|---|
| 264 | }
|
|---|
| 265 | delete_match_id(match_id);
|
|---|
| 266 |
|
|---|
| 267 | return rc;
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | /** Issue interrupt OUT transfer to HC driven by current task.
|
|---|
| 271 | *
|
|---|
| 272 | * @param hc Host controller to handle the transfer.
|
|---|
| 273 | * @param target Target device address.
|
|---|
| 274 | * @param buffer Data buffer.
|
|---|
| 275 | * @param size Buffer size.
|
|---|
| 276 | * @param handle Transfer handle.
|
|---|
| 277 | * @return Error code.
|
|---|
| 278 | */
|
|---|
| 279 | int usb_hc_async_interrupt_out(usb_hc_device_t *hc, usb_target_t target,
|
|---|
| 280 | void *buffer, size_t size,
|
|---|
| 281 | usb_handle_t *handle)
|
|---|
| 282 | {
|
|---|
| 283 | if ((hc->transfer_ops == NULL)
|
|---|
| 284 | || (hc->transfer_ops->transfer_out == NULL)) {
|
|---|
| 285 | return ENOTSUP;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /*
|
|---|
| 289 | * For debugging purposes only.
|
|---|
| 290 | * We need to find appropriate device in list of managed device
|
|---|
| 291 | * and pass it to the transfer callback function.
|
|---|
| 292 | */
|
|---|
| 293 | usb_hcd_attached_device_info_t dev = {
|
|---|
| 294 | .address = target.address,
|
|---|
| 295 | .endpoint_count = 0,
|
|---|
| 296 | .endpoints = NULL,
|
|---|
| 297 | };
|
|---|
| 298 | usb_hc_endpoint_info_t endpoint = {
|
|---|
| 299 | .endpoint = target.endpoint,
|
|---|
| 300 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
|---|
| 301 | .direction = USB_DIRECTION_OUT,
|
|---|
| 302 | .data_toggle = 0
|
|---|
| 303 | };
|
|---|
| 304 |
|
|---|
| 305 | hc->transfer_ops->transfer_out(hc, &dev, &endpoint, buffer, size, NULL, NULL);
|
|---|
| 306 |
|
|---|
| 307 | *handle = NULL;
|
|---|
| 308 |
|
|---|
| 309 | return EOK;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 | /** Issue interrupt IN transfer to HC driven by current task.
|
|---|
| 314 | *
|
|---|
| 315 | * @warning The @p buffer and @p actual_size parameters shall not be
|
|---|
| 316 | * touched until this transfer is waited for by usb_hc_async_wait_for().
|
|---|
| 317 | *
|
|---|
| 318 | * @param hc Host controller to handle the transfer.
|
|---|
| 319 | * @param target Target device address.
|
|---|
| 320 | * @param buffer Data buffer.
|
|---|
| 321 | * @param size Buffer size.
|
|---|
| 322 | * @param actual_size Size of actually transferred data.
|
|---|
| 323 | * @param handle Transfer handle.
|
|---|
| 324 | * @return Error code.
|
|---|
| 325 | */
|
|---|
| 326 | int usb_hc_async_interrupt_in(usb_hc_device_t *hc, usb_target_t target,
|
|---|
| 327 | void *buffer, size_t size, size_t *actual_size,
|
|---|
| 328 | usb_handle_t *handle)
|
|---|
| 329 | {
|
|---|
| 330 | /*
|
|---|
| 331 | * TODO: verify that given endpoint is of interrupt type and
|
|---|
| 332 | * call hc->transfer_ops->transfer_in()
|
|---|
| 333 | */
|
|---|
| 334 | return ENOTSUP;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | /** Wait for transfer to complete.
|
|---|
| 338 | *
|
|---|
| 339 | * @param handle Transfer handle.
|
|---|
| 340 | * @return Error code.
|
|---|
| 341 | */
|
|---|
| 342 | int usb_hc_async_wait_for(usb_handle_t handle)
|
|---|
| 343 | {
|
|---|
| 344 | return ENOTSUP;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /**
|
|---|
| 348 | * @}
|
|---|
| 349 | */
|
|---|