/** Retrieve basic descriptors from the device. * * @param[in] ctrl_pipe Control endpoint pipe. * @param[out] descriptors Where to store the descriptors. * @return Error code. */ static int usb_device_retrieve_descriptors(usb_device_t *usb_dev) { assert(usb_dev); assert(usb_dev->descriptors.full_config == NULL); /* Get the device descriptor. */ int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe, &usb_dev->descriptors.device); if (rc != EOK) { return rc; } /* Get the full configuration descriptor. */ rc = usb_request_get_full_configuration_descriptor_alloc( &usb_dev->ctrl_pipe, 0, &usb_dev->descriptors.full_config, &usb_dev->descriptors.full_config_size); return rc; }
/** Create match ids describing attached device. * * @warning The list of match ids @p matches may change even when * function exits with error. * * @param ctrl_pipe Control pipe to given device (session must be already * started). * @param matches Initialized list of match ids. * @return Error code. */ int usb_device_create_match_ids(usb_pipe_t *ctrl_pipe, match_id_list_t *matches) { assert(ctrl_pipe); int rc; /* * Retrieve device descriptor and add matches from it. */ usb_standard_device_descriptor_t device_descriptor; rc = usb_request_get_device_descriptor(ctrl_pipe, &device_descriptor); if (rc != EOK) { return rc; } rc = usb_device_create_match_ids_from_device_descriptor( &device_descriptor, matches); if (rc != EOK) { return rc; } return EOK; }
usbinfo_device_t *prepare_device(const char *name, devman_handle_t hc_handle, usb_address_t dev_addr) { usbinfo_device_t *dev = malloc(sizeof(usbinfo_device_t)); if (dev == NULL) { fprintf(stderr, NAME ": memory allocation failed.\n"); return NULL; } int rc; bool transfer_started = false; usb_hc_connection_initialize(&dev->hc_conn, hc_handle); rc = usb_device_connection_initialize( &dev->wire, &dev->hc_conn, dev_addr); if (rc != EOK) { fprintf(stderr, NAME ": failed to create connection to device %s: %s.\n", name, str_error(rc)); goto leave; } rc = usb_pipe_initialize_default_control(&dev->ctrl_pipe, &dev->wire); if (rc != EOK) { fprintf(stderr, NAME ": failed to create default control pipe to %s: %s.\n", name, str_error(rc)); goto leave; } rc = usb_pipe_probe_default_control(&dev->ctrl_pipe); if (rc != EOK) { if (rc == ENOENT) { fprintf(stderr, NAME ": " \ "device %s not present or malfunctioning.\n", name); } else { fprintf(stderr, NAME ": " \ "probing default control pipe of %s failed: %s.\n", name, str_error(rc)); } goto leave; } usb_pipe_start_long_transfer(&dev->ctrl_pipe); transfer_started = true; rc = usb_request_get_device_descriptor(&dev->ctrl_pipe, &dev->device_descriptor); if (rc != EOK) { fprintf(stderr, NAME ": failed to retrieve device descriptor of %s: %s.\n", name, str_error(rc)); goto leave; } rc = usb_request_get_full_configuration_descriptor_alloc( &dev->ctrl_pipe, 0, (void **)&dev->full_configuration_descriptor, &dev->full_configuration_descriptor_size); if (rc != EOK) { fprintf(stderr, NAME ": " \ "failed to retrieve configuration descriptor of %s: %s.\n", name, str_error(rc)); goto leave; } return dev; leave: if (transfer_started) { usb_pipe_end_long_transfer(&dev->ctrl_pipe); } free(dev); return NULL; }