[02ccfcd] | 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 Functions for recognising kind of attached devices.
|
---|
| 34 | */
|
---|
[71ed4849] | 35 | #include <usb_iface.h>
|
---|
[02ccfcd] | 36 | #include <usb/usbdrv.h>
|
---|
| 37 | #include <usb/classes/classes.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 |
|
---|
[71ed4849] | 41 | static int usb_iface_get_hc_handle(device_t *dev, devman_handle_t *handle)
|
---|
| 42 | {
|
---|
| 43 | assert(dev);
|
---|
| 44 | assert(dev->parent != NULL);
|
---|
| 45 |
|
---|
| 46 | device_t *parent = dev->parent;
|
---|
| 47 |
|
---|
| 48 | if (parent->ops && parent->ops->interfaces[USB_DEV_IFACE]) {
|
---|
| 49 | usb_iface_t *usb_iface
|
---|
| 50 | = (usb_iface_t *) parent->ops->interfaces[USB_DEV_IFACE];
|
---|
| 51 | assert(usb_iface != NULL);
|
---|
| 52 | if (usb_iface->get_hc_handle) {
|
---|
| 53 | int rc = usb_iface->get_hc_handle(parent, handle);
|
---|
| 54 | return rc;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return ENOTSUP;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | static usb_iface_t usb_iface = {
|
---|
| 62 | .get_hc_handle = usb_iface_get_hc_handle
|
---|
| 63 | };
|
---|
| 64 |
|
---|
| 65 | static device_ops_t child_ops = {
|
---|
| 66 | .interfaces[USB_DEV_IFACE] = &usb_iface
|
---|
| 67 | };
|
---|
[02ccfcd] | 68 |
|
---|
| 69 | #define BCD_INT(a) (((unsigned int)(a)) / 256)
|
---|
| 70 | #define BCD_FRAC(a) (((unsigned int)(a)) % 256)
|
---|
| 71 |
|
---|
| 72 | #define BCD_FMT "%x.%x"
|
---|
| 73 | #define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
|
---|
| 74 |
|
---|
| 75 | /* FIXME: make this dynamic */
|
---|
| 76 | #define MATCH_STRING_MAX 256
|
---|
| 77 |
|
---|
| 78 | /** Add formatted match id.
|
---|
| 79 | *
|
---|
| 80 | * @param matches List of match ids where to add to.
|
---|
| 81 | * @param score Score of the match.
|
---|
| 82 | * @param format Printf-like format
|
---|
| 83 | * @return Error code.
|
---|
| 84 | */
|
---|
| 85 | static int usb_add_match_id(match_id_list_t *matches, int score,
|
---|
| 86 | const char *format, ...)
|
---|
| 87 | {
|
---|
| 88 | char *match_str = NULL;
|
---|
| 89 | match_id_t *match_id = NULL;
|
---|
| 90 | int rc;
|
---|
| 91 |
|
---|
| 92 | match_str = malloc(MATCH_STRING_MAX + 1);
|
---|
| 93 | if (match_str == NULL) {
|
---|
| 94 | rc = ENOMEM;
|
---|
| 95 | goto failure;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /*
|
---|
| 99 | * FIXME: replace with dynamic allocation of exact size
|
---|
| 100 | */
|
---|
| 101 | va_list args;
|
---|
| 102 | va_start(args, format );
|
---|
| 103 | vsnprintf(match_str, MATCH_STRING_MAX, format, args);
|
---|
| 104 | match_str[MATCH_STRING_MAX] = 0;
|
---|
| 105 | va_end(args);
|
---|
| 106 |
|
---|
| 107 | match_id = create_match_id();
|
---|
| 108 | if (match_id == NULL) {
|
---|
| 109 | rc = ENOMEM;
|
---|
| 110 | goto failure;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | match_id->id = match_str;
|
---|
| 114 | match_id->score = score;
|
---|
| 115 | add_match_id(matches, match_id);
|
---|
| 116 |
|
---|
| 117 | return EOK;
|
---|
| 118 |
|
---|
| 119 | failure:
|
---|
| 120 | if (match_str != NULL) {
|
---|
| 121 | free(match_str);
|
---|
| 122 | }
|
---|
| 123 | if (match_id != NULL) {
|
---|
| 124 | match_id->id = NULL;
|
---|
| 125 | delete_match_id(match_id);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | return rc;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[692c0d3e] | 131 | /** Create DDF match ids from USB device descriptor.
|
---|
| 132 | *
|
---|
| 133 | * @param matches List of match ids to extend.
|
---|
| 134 | * @param device_descriptor Device descriptor returned by given device.
|
---|
| 135 | * @return Error code.
|
---|
| 136 | */
|
---|
| 137 | int usb_drv_create_match_ids_from_device_descriptor(
|
---|
| 138 | match_id_list_t *matches,
|
---|
| 139 | const usb_standard_device_descriptor_t *device_descriptor)
|
---|
| 140 | {
|
---|
| 141 | int rc;
|
---|
| 142 |
|
---|
| 143 | /*
|
---|
| 144 | * Unless the vendor id is 0, the pair idVendor-idProduct
|
---|
| 145 | * quite uniquely describes the device.
|
---|
| 146 | */
|
---|
| 147 | if (device_descriptor->vendor_id != 0) {
|
---|
| 148 | /* First, with release number. */
|
---|
| 149 | rc = usb_add_match_id(matches, 100,
|
---|
| 150 | "usb&vendor=%d&product=%d&release=" BCD_FMT,
|
---|
| 151 | (int) device_descriptor->vendor_id,
|
---|
| 152 | (int) device_descriptor->product_id,
|
---|
| 153 | BCD_ARGS(device_descriptor->device_version));
|
---|
| 154 | if (rc != EOK) {
|
---|
| 155 | return rc;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /* Next, without release number. */
|
---|
| 159 | rc = usb_add_match_id(matches, 90, "usb&vendor=%d&product=%d",
|
---|
| 160 | (int) device_descriptor->vendor_id,
|
---|
| 161 | (int) device_descriptor->product_id);
|
---|
| 162 | if (rc != EOK) {
|
---|
| 163 | return rc;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * If the device class points to interface we skip adding
|
---|
| 169 | * class directly.
|
---|
| 170 | */
|
---|
| 171 | if (device_descriptor->device_class != USB_CLASS_USE_INTERFACE) {
|
---|
| 172 | rc = usb_add_match_id(matches, 50, "usb&class=%s",
|
---|
| 173 | usb_str_class(device_descriptor->device_class));
|
---|
| 174 | if (rc != EOK) {
|
---|
| 175 | return rc;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | return EOK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /** Create DDF match ids from USB configuration descriptor.
|
---|
| 183 | * The configuration descriptor is expected to be in the complete form,
|
---|
| 184 | * i.e. including interface, endpoint etc. descriptors.
|
---|
| 185 | *
|
---|
| 186 | * @param matches List of match ids to extend.
|
---|
| 187 | * @param config_descriptor Configuration descriptor returned by given device.
|
---|
| 188 | * @param total_size Size of the @p config_descriptor.
|
---|
| 189 | * @return Error code.
|
---|
| 190 | */
|
---|
| 191 | int usb_drv_create_match_ids_from_configuration_descriptor(
|
---|
| 192 | match_id_list_t *matches,
|
---|
| 193 | const void *config_descriptor, size_t total_size)
|
---|
| 194 | {
|
---|
| 195 | /*
|
---|
| 196 | * Iterate through config descriptor to find the interface
|
---|
| 197 | * descriptors.
|
---|
| 198 | */
|
---|
| 199 | size_t position = sizeof(usb_standard_configuration_descriptor_t);
|
---|
| 200 | while (position + 1 < total_size) {
|
---|
| 201 | uint8_t *current_descriptor
|
---|
| 202 | = ((uint8_t *) config_descriptor) + position;
|
---|
| 203 | uint8_t cur_descr_len = current_descriptor[0];
|
---|
| 204 | uint8_t cur_descr_type = current_descriptor[1];
|
---|
[d08abdb7] | 205 |
|
---|
| 206 | if (cur_descr_len == 0) {
|
---|
| 207 | return ENOENT;
|
---|
| 208 | }
|
---|
[692c0d3e] | 209 |
|
---|
| 210 | position += cur_descr_len;
|
---|
| 211 |
|
---|
| 212 | if (cur_descr_type != USB_DESCTYPE_INTERFACE) {
|
---|
| 213 | continue;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /*
|
---|
| 217 | * Finally, we found an interface descriptor.
|
---|
| 218 | */
|
---|
| 219 | usb_standard_interface_descriptor_t *interface
|
---|
| 220 | = (usb_standard_interface_descriptor_t *)
|
---|
| 221 | current_descriptor;
|
---|
| 222 |
|
---|
| 223 | int rc = usb_add_match_id(matches, 50,
|
---|
| 224 | "usb&interface&class=%s",
|
---|
| 225 | usb_str_class(interface->interface_class));
|
---|
| 226 | if (rc != EOK) {
|
---|
| 227 | return rc;
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | return EOK;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[a66225f3] | 234 | /** Add match ids based on configuration descriptor.
|
---|
| 235 | *
|
---|
| 236 | * @param hc Open phone to host controller.
|
---|
| 237 | * @param matches Match ids list to add matches to.
|
---|
| 238 | * @param address USB address of the attached device.
|
---|
| 239 | * @return Error code.
|
---|
| 240 | */
|
---|
| 241 | static int usb_add_config_descriptor_match_ids(int hc,
|
---|
| 242 | match_id_list_t *matches, usb_address_t address,
|
---|
| 243 | int config_count)
|
---|
| 244 | {
|
---|
| 245 | int final_rc = EOK;
|
---|
| 246 |
|
---|
| 247 | int config_index;
|
---|
| 248 | for (config_index = 0; config_index < config_count; config_index++) {
|
---|
| 249 | int rc;
|
---|
| 250 | usb_standard_configuration_descriptor_t config_descriptor;
|
---|
| 251 | rc = usb_drv_req_get_bare_configuration_descriptor(hc,
|
---|
| 252 | address, config_index, &config_descriptor);
|
---|
| 253 | if (rc != EOK) {
|
---|
| 254 | final_rc = rc;
|
---|
| 255 | continue;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | size_t full_config_descriptor_size;
|
---|
| 259 | void *full_config_descriptor
|
---|
| 260 | = malloc(config_descriptor.total_length);
|
---|
| 261 | rc = usb_drv_req_get_full_configuration_descriptor(hc,
|
---|
| 262 | address, config_index,
|
---|
| 263 | full_config_descriptor, config_descriptor.total_length,
|
---|
| 264 | &full_config_descriptor_size);
|
---|
| 265 | if (rc != EOK) {
|
---|
| 266 | final_rc = rc;
|
---|
| 267 | continue;
|
---|
| 268 | }
|
---|
| 269 | if (full_config_descriptor_size
|
---|
| 270 | != config_descriptor.total_length) {
|
---|
| 271 | final_rc = ERANGE;
|
---|
| 272 | continue;
|
---|
| 273 | }
|
---|
[692c0d3e] | 274 |
|
---|
| 275 | rc = usb_drv_create_match_ids_from_configuration_descriptor(
|
---|
| 276 | matches,
|
---|
| 277 | full_config_descriptor, full_config_descriptor_size);
|
---|
| 278 | if (rc != EOK) {
|
---|
| 279 | final_rc = rc;
|
---|
| 280 | continue;
|
---|
[a66225f3] | 281 | }
|
---|
[692c0d3e] | 282 |
|
---|
[a66225f3] | 283 | }
|
---|
| 284 |
|
---|
| 285 | return final_rc;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[02ccfcd] | 288 | /** Create match ids describing attached device.
|
---|
| 289 | *
|
---|
| 290 | * @warning The list of match ids @p matches may change even when
|
---|
| 291 | * function exits with error.
|
---|
| 292 | *
|
---|
| 293 | * @param hc Open phone to host controller.
|
---|
| 294 | * @param matches Initialized list of match ids.
|
---|
| 295 | * @param address USB address of the attached device.
|
---|
| 296 | * @return Error code.
|
---|
| 297 | */
|
---|
| 298 | int usb_drv_create_device_match_ids(int hc, match_id_list_t *matches,
|
---|
| 299 | usb_address_t address)
|
---|
| 300 | {
|
---|
| 301 | int rc;
|
---|
[692c0d3e] | 302 |
|
---|
| 303 | /*
|
---|
| 304 | * Retrieve device descriptor and add matches from it.
|
---|
| 305 | */
|
---|
[02ccfcd] | 306 | usb_standard_device_descriptor_t device_descriptor;
|
---|
| 307 |
|
---|
| 308 | rc = usb_drv_req_get_device_descriptor(hc, address,
|
---|
| 309 | &device_descriptor);
|
---|
| 310 | if (rc != EOK) {
|
---|
| 311 | return rc;
|
---|
| 312 | }
|
---|
[692c0d3e] | 313 |
|
---|
| 314 | rc = usb_drv_create_match_ids_from_device_descriptor(matches,
|
---|
| 315 | &device_descriptor);
|
---|
| 316 | if (rc != EOK) {
|
---|
| 317 | return rc;
|
---|
[a66225f3] | 318 | }
|
---|
[692c0d3e] | 319 |
|
---|
[a66225f3] | 320 | /*
|
---|
| 321 | * Go through all configurations and add matches
|
---|
| 322 | * based on interface class.
|
---|
| 323 | */
|
---|
| 324 | rc = usb_add_config_descriptor_match_ids(hc, matches,
|
---|
| 325 | address, device_descriptor.configuration_count);
|
---|
| 326 | if (rc != EOK) {
|
---|
| 327 | return rc;
|
---|
[02ccfcd] | 328 | }
|
---|
| 329 |
|
---|
| 330 | /*
|
---|
| 331 | * As a fallback, provide the simplest match id possible.
|
---|
| 332 | */
|
---|
| 333 | rc = usb_add_match_id(matches, 1, "usb&fallback");
|
---|
| 334 | if (rc != EOK) {
|
---|
| 335 | return rc;
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | return EOK;
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[7ed5b576] | 341 |
|
---|
[02ccfcd] | 342 | /** Probe for device kind and register it in devman.
|
---|
| 343 | *
|
---|
| 344 | * @param hc Open phone to the host controller.
|
---|
[7ed5b576] | 345 | * @param parent Parent device.
|
---|
[02ccfcd] | 346 | * @param address Address of the (unknown) attached device.
|
---|
| 347 | * @return Error code.
|
---|
| 348 | */
|
---|
[7ed5b576] | 349 | int usb_drv_register_child_in_devman(int hc, device_t *parent,
|
---|
| 350 | usb_address_t address, devman_handle_t *child_handle)
|
---|
[02ccfcd] | 351 | {
|
---|
[b207803] | 352 | static size_t device_name_index = 0;
|
---|
| 353 |
|
---|
[7ed5b576] | 354 | device_t *child = NULL;
|
---|
| 355 | char *child_name = NULL;
|
---|
| 356 | int rc;
|
---|
| 357 |
|
---|
| 358 | child = create_device();
|
---|
| 359 | if (child == NULL) {
|
---|
| 360 | rc = ENOMEM;
|
---|
| 361 | goto failure;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /*
|
---|
[b207803] | 365 | * TODO: Once the device driver framework support persistent
|
---|
| 366 | * naming etc., something more descriptive could be created.
|
---|
[7ed5b576] | 367 | */
|
---|
[b207803] | 368 | rc = asprintf(&child_name, "usbdev%02zu", device_name_index);
|
---|
[7ed5b576] | 369 | if (rc < 0) {
|
---|
| 370 | goto failure;
|
---|
| 371 | }
|
---|
[71ed4849] | 372 | child->parent = parent;
|
---|
[7ed5b576] | 373 | child->name = child_name;
|
---|
[71ed4849] | 374 | child->ops = &child_ops;
|
---|
[7ed5b576] | 375 |
|
---|
| 376 | rc = usb_drv_create_device_match_ids(hc, &child->match_ids, address);
|
---|
| 377 | if (rc != EOK) {
|
---|
| 378 | goto failure;
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | rc = child_device_register(child, parent);
|
---|
| 382 | if (rc != EOK) {
|
---|
| 383 | goto failure;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | if (child_handle != NULL) {
|
---|
| 387 | *child_handle = child->handle;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[b207803] | 390 | device_name_index++;
|
---|
| 391 |
|
---|
[7ed5b576] | 392 | return EOK;
|
---|
| 393 |
|
---|
| 394 | failure:
|
---|
| 395 | if (child != NULL) {
|
---|
| 396 | child->name = NULL;
|
---|
| 397 | /* This takes care of match_id deallocation as well. */
|
---|
| 398 | delete_device(child);
|
---|
| 399 | }
|
---|
| 400 | if (child_name != NULL) {
|
---|
| 401 | free(child_name);
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | return rc;
|
---|
[02ccfcd] | 405 |
|
---|
| 406 | }
|
---|
[7ed5b576] | 407 |
|
---|
[02ccfcd] | 408 |
|
---|
| 409 | /**
|
---|
| 410 | * @}
|
---|
| 411 | */
|
---|