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