[c7137738] | 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
|
---|
[dac43be] | 33 | * @brief Common stuff for both HC driver and hub driver.
|
---|
[c7137738] | 34 | */
|
---|
[4b4c797] | 35 | #include <usb/hcdhubd.h>
|
---|
[8f62b0f] | 36 | #include <usb/devreq.h>
|
---|
[cb59f787] | 37 | #include <usbhc_iface.h>
|
---|
[da55d5b] | 38 | #include <usb/descriptor.h>
|
---|
[c7137738] | 39 | #include <driver.h>
|
---|
| 40 | #include <bool.h>
|
---|
| 41 | #include <errno.h>
|
---|
[03171de] | 42 | #include <usb/classes/hub.h>
|
---|
[c7137738] | 43 |
|
---|
[dac43be] | 44 | #include "hcdhubd_private.h"
|
---|
[8f62b0f] | 45 |
|
---|
[c7137738] | 46 | /** Callback when new device is detected and must be handled by this driver.
|
---|
| 47 | *
|
---|
| 48 | * @param dev New device.
|
---|
[dac43be] | 49 | * @return Error code.
|
---|
[c7137738] | 50 | */
|
---|
[da55d5b] | 51 | static int add_device(device_t *dev) {
|
---|
[0ee648a] | 52 | bool is_hc = str_cmp(dev->name, USB_HUB_DEVICE_NAME) != 0;
|
---|
| 53 | printf("%s: add_device(name=\"%s\")\n", hc_driver->name, dev->name);
|
---|
[c7137738] | 54 |
|
---|
| 55 | if (is_hc) {
|
---|
| 56 | /*
|
---|
| 57 | * We are the HC itself.
|
---|
| 58 | */
|
---|
[dac43be] | 59 | return usb_add_hc_device(dev);
|
---|
[c7137738] | 60 | } else {
|
---|
| 61 | /*
|
---|
[dac43be] | 62 | * We are some (maybe deeply nested) hub.
|
---|
[c7137738] | 63 | * Thus, assign our own operations and explore already
|
---|
| 64 | * connected devices.
|
---|
| 65 | */
|
---|
[dac43be] | 66 | return usb_add_hub_device(dev);
|
---|
[c7137738] | 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /** Operations for combined HC and HUB driver. */
|
---|
| 71 | static driver_ops_t hc_driver_generic_ops = {
|
---|
| 72 | .add_device = add_device
|
---|
| 73 | };
|
---|
| 74 |
|
---|
| 75 | /** Combined HC and HUB driver. */
|
---|
| 76 | static driver_t hc_driver_generic = {
|
---|
| 77 | .driver_ops = &hc_driver_generic_ops
|
---|
| 78 | };
|
---|
| 79 |
|
---|
| 80 | /** Main USB host controller driver routine.
|
---|
| 81 | *
|
---|
| 82 | * @see driver_main
|
---|
| 83 | *
|
---|
| 84 | * @param hc Host controller driver.
|
---|
| 85 | * @return Error code.
|
---|
| 86 | */
|
---|
[da55d5b] | 87 | int usb_hcd_main(usb_hc_driver_t *hc) {
|
---|
[c7137738] | 88 | hc_driver = hc;
|
---|
| 89 | hc_driver_generic.name = hc->name;
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | * Run the device driver framework.
|
---|
| 93 | */
|
---|
| 94 | return driver_main(&hc_driver_generic);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /** Add a root hub for given host controller.
|
---|
| 98 | * This function shall be called only once for each host controller driven
|
---|
| 99 | * by this driver.
|
---|
| 100 | * It takes care of creating child device - hub - that will be driven by
|
---|
| 101 | * this task.
|
---|
| 102 | *
|
---|
| 103 | * @param dev Host controller device.
|
---|
| 104 | * @return Error code.
|
---|
| 105 | */
|
---|
| 106 | int usb_hcd_add_root_hub(usb_hc_device_t *dev)
|
---|
| 107 | {
|
---|
[e4dbfda] | 108 | char *id;
|
---|
| 109 | int rc = asprintf(&id, "usb&hc=%s&hub", dev->generic->name);
|
---|
| 110 | if (rc <= 0) {
|
---|
| 111 | return rc;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | rc = usb_hc_add_child_device(dev->generic, USB_HUB_DEVICE_NAME, id);
|
---|
| 115 | if (rc != EOK) {
|
---|
| 116 | free(id);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | return rc;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** Info about child device. */
|
---|
| 123 | struct child_device_info {
|
---|
| 124 | device_t *parent;
|
---|
| 125 | const char *name;
|
---|
| 126 | const char *match_id;
|
---|
| 127 | };
|
---|
| 128 |
|
---|
| 129 | /** Adds a child device fibril worker. */
|
---|
| 130 | static int fibril_add_child_device(void *arg)
|
---|
| 131 | {
|
---|
| 132 | struct child_device_info *child_info
|
---|
| 133 | = (struct child_device_info *) arg;
|
---|
[7034be15] | 134 | int rc;
|
---|
| 135 |
|
---|
[e4dbfda] | 136 | device_t *child = create_device();
|
---|
[c7137738] | 137 | match_id_t *match_id = NULL;
|
---|
| 138 |
|
---|
[e4dbfda] | 139 | if (child == NULL) {
|
---|
[c7137738] | 140 | rc = ENOMEM;
|
---|
| 141 | goto failure;
|
---|
| 142 | }
|
---|
[e4dbfda] | 143 | child->name = child_info->name;
|
---|
[c7137738] | 144 |
|
---|
| 145 | match_id = create_match_id();
|
---|
| 146 | if (match_id == NULL) {
|
---|
| 147 | rc = ENOMEM;
|
---|
| 148 | goto failure;
|
---|
| 149 | }
|
---|
[e4dbfda] | 150 | match_id->id = child_info->match_id;
|
---|
| 151 | match_id->score = 10;
|
---|
| 152 | printf("adding child device with match \"%s\"\n", match_id->id);
|
---|
| 153 | add_match_id(&child->match_ids, match_id);
|
---|
[c7137738] | 154 |
|
---|
[e4dbfda] | 155 | rc = child_device_register(child, child_info->parent);
|
---|
| 156 | if (rc != EOK) {
|
---|
[c7137738] | 157 | goto failure;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[e4dbfda] | 160 | goto leave;
|
---|
[c7137738] | 161 |
|
---|
[e4dbfda] | 162 | failure:
|
---|
| 163 | if (child != NULL) {
|
---|
| 164 | child->name = NULL;
|
---|
| 165 | delete_device(child);
|
---|
| 166 | }
|
---|
[c7137738] | 167 |
|
---|
[e4dbfda] | 168 | if (match_id != NULL) {
|
---|
| 169 | match_id->id = NULL;
|
---|
| 170 | delete_match_id(match_id);
|
---|
[c7137738] | 171 | }
|
---|
| 172 |
|
---|
[e4dbfda] | 173 | leave:
|
---|
| 174 | free(arg);
|
---|
| 175 | return rc;
|
---|
| 176 | }
|
---|
[c7137738] | 177 |
|
---|
[e4dbfda] | 178 | /** Adds a child.
|
---|
| 179 | * Due to deadlock in devman when parent registers child that oughts to be
|
---|
| 180 | * driven by the same task, the child adding is done in separate fibril.
|
---|
| 181 | * Not optimal, but it works.
|
---|
| 182 | *
|
---|
| 183 | * @param parent Parent device.
|
---|
| 184 | * @param name Device name.
|
---|
| 185 | * @param match_id Match id.
|
---|
| 186 | * @return Error code.
|
---|
| 187 | */
|
---|
| 188 | int usb_hc_add_child_device(device_t *parent, const char *name,
|
---|
| 189 | const char *match_id)
|
---|
| 190 | {
|
---|
| 191 | struct child_device_info *child_info
|
---|
| 192 | = malloc(sizeof(struct child_device_info));
|
---|
| 193 |
|
---|
| 194 | child_info->parent = parent;
|
---|
| 195 | child_info->name = name;
|
---|
| 196 | child_info->match_id = match_id;
|
---|
| 197 |
|
---|
| 198 | fid_t fibril = fibril_create(fibril_add_child_device, child_info);
|
---|
| 199 | if (!fibril) {
|
---|
| 200 | return ENOMEM;
|
---|
[c7137738] | 201 | }
|
---|
[e4dbfda] | 202 | fibril_add_ready(fibril);
|
---|
[c7137738] | 203 |
|
---|
[e4dbfda] | 204 | return EOK;
|
---|
[c7137738] | 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * @}
|
---|
| 209 | */
|
---|