/** Change address of connected device. * This function automatically updates the backing connection to point to * the new address. It also unregisterrs the old endpoint and registers * a new one. * This creates whole bunch of problems: * 1. All pipes using this wire are broken because they are not * registered for new address * 2. All other pipes for this device are using wrong address, * possibly targeting completely different device * * @param pipe Control endpoint pipe (session must be already started). * @param new_address New USB address to be set (in native endianness). * @return Error code. */ static int usb_request_set_address(usb_pipe_t *pipe, usb_address_t new_address) { if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) { return EINVAL; } assert(pipe); assert(pipe->wire != NULL); const uint16_t addr = uint16_host2usb((uint16_t)new_address); int rc = usb_control_request_set(pipe, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_SET_ADDRESS, addr, 0, NULL, 0); if (rc != EOK) { return rc; } /* TODO: prevent others from accessing the wire now. */ if (usb_pipe_unregister(pipe) != EOK) { usb_log_warning( "Failed to unregister the old pipe on address change.\n"); } /* Address changed. We can release the old one, thus * allowing other to us it. */ usb_hc_release_address(pipe->wire->hc_connection, pipe->wire->address); /* The address is already changed so set it in the wire */ pipe->wire->address = new_address; rc = usb_pipe_register(pipe, 0); if (rc != EOK) return EADDRNOTAVAIL; return EOK; }
/** Select alternate setting for USB interface. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] interface_index Interface index. * @param[in] alternate_setting Alternate setting to select. * @return Error code. */ int usb_request_set_interface(usb_pipe_t *pipe, uint8_t interface_index, uint8_t alternate_setting) { return usb_control_request_set(pipe, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE, USB_DEVREQ_SET_INTERFACE, uint16_host2usb((uint16_t) alternate_setting), uint16_host2usb((uint16_t) interface_index), NULL, 0); }
/** Set configuration of USB device. * * @param pipe Control endpoint pipe (session must be already started). * @param configuration_value New configuration value. * @return Error code. */ int usb_request_set_configuration(usb_pipe_t *pipe, uint8_t configuration_value) { uint16_t config_value = uint16_host2usb((uint16_t) configuration_value); return usb_control_request_set(pipe, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_SET_CONFIGURATION, config_value, 0, NULL, 0); }
/** Update existing or add new USB descriptor to a USB device. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] request_type Request type (standard/class/vendor). * @param[in] recipient Request recipient (device/interface/endpoint). * @param[in] descriptor_type Descriptor type (device/configuration/HID/...). * @param[in] descriptor_index Descriptor index. * @param[in] language Language index (in native endianness). * @param[in] buffer Buffer with the new descriptor (in USB endianness). * @param[in] size Size of the @p buffer in bytes (in native endianness). * @return Error code. */ int usb_request_set_descriptor(usb_pipe_t *pipe, usb_request_type_t request_type, usb_request_recipient_t recipient, uint8_t descriptor_type, uint8_t descriptor_index, uint16_t language, void *buffer, size_t size) { if (buffer == NULL) { return EBADMEM; } if (size == 0) { return EINVAL; } /* FIXME: proper endianness. */ uint16_t wValue = descriptor_index | (descriptor_type << 8); return usb_control_request_set(pipe, request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR, wValue, language, buffer, size); }
/** * Send Set Report request to the HID device. * * @param hid_dev HID device to send the request to. * @param type Type of the report. * @param buffer Report data. * @param buf_size Report data size (in bytes). * * @retval EOK if successful. * @retval EINVAL if no HID device is given. * @return Other value inherited from function usb_control_request_set(). */ int usbhid_req_set_report(usb_pipe_t *ctrl_pipe, int iface_no, usb_hid_report_type_t type, uint8_t *buffer, size_t buf_size) { if (ctrl_pipe == NULL) { usb_log_warning("usbhid_req_set_report(): no pipe given.\n"); return EINVAL; } if (iface_no < 0) { usb_log_warning("usbhid_req_set_report(): no interface given." "\n"); return EINVAL; } /* * No need for checking other parameters, as they are checked in * the called function (usb_control_request_set()). */ int rc; uint16_t value = 0; value |= (type << 8); usb_log_debug("Sending Set Report request to the device.\n"); rc = usb_control_request_set(ctrl_pipe, USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, USB_HIDREQ_SET_REPORT, value, iface_no, buffer, buf_size); if (rc != EOK) { usb_log_error("Error sending Set Report request to the " "device: %s.\n", str_error(rc)); return rc; } return EOK; }
/** * Send Set Idle request to the HID device. * * @param hid_dev HID device to send the request to. * @param duration Duration value (is multiplicated by 4 by the device to * get real duration in miliseconds). * * @retval EOK if successful. * @retval EINVAL if no HID device is given. * @return Other value inherited from function usb_control_request_set(). */ int usbhid_req_set_idle(usb_pipe_t *ctrl_pipe, int iface_no, uint8_t duration) { if (ctrl_pipe == NULL) { usb_log_warning("usbhid_req_set_report(): no pipe given.\n"); return EINVAL; } if (iface_no < 0) { usb_log_warning("usbhid_req_set_report(): no interface given." "\n"); return EINVAL; } /* * No need for checking other parameters, as they are checked in * the called function (usb_control_request_set()). */ int rc; usb_log_debug("Sending Set Idle request to the device (" "duration: %u, iface: %d).\n", duration, iface_no); uint16_t value = duration << 8; rc = usb_control_request_set(ctrl_pipe, USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, USB_HIDREQ_SET_IDLE, value, iface_no, NULL, 0); if (rc != EOK) { usb_log_warning("Error sending Set Idle request to the device: " "%s.\n", str_error(rc)); return rc; } return EOK; }
/** * Send Set Protocol request to the HID device. * * @param hid_dev HID device to send the request to. * @param protocol Protocol to set. * * @retval EOK if successful. * @retval EINVAL if no HID device is given. * @return Other value inherited from function usb_control_request_set(). */ int usbhid_req_set_protocol(usb_pipe_t *ctrl_pipe, int iface_no, usb_hid_protocol_t protocol) { if (ctrl_pipe == NULL) { usb_log_warning("usbhid_req_set_report(): no pipe given.\n"); return EINVAL; } if (iface_no < 0) { usb_log_warning("usbhid_req_set_report(): no interface given." "\n"); return EINVAL; } /* * No need for checking other parameters, as they are checked in * the called function (usb_control_request_set()). */ int rc; usb_log_debug("Sending Set Protocol request to the device (" "protocol: %d, iface: %d).\n", protocol, iface_no); rc = usb_control_request_set(ctrl_pipe, USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, USB_HIDREQ_SET_PROTOCOL, protocol, iface_no, NULL, 0); if (rc != EOK) { usb_log_warning("Error sending Set Protocol request to the " "device: %s.\n", str_error(rc)); return rc; } return EOK; }
/** Generic wrapper for GET requests using standard control request format. * * @see usb_pipe_control_read * * @param pipe Pipe used for the communication. * @param request_type Request type (standard/class/vendor). * @param recipient Request recipient (e.g. device or endpoint). * @param request Actual request (e.g. GET_DESCRIPTOR). * @param value Value of @c wValue field of setup packet * (must be in USB endianness). * @param index Value of @c wIndex field of setup packet * (must be in USB endianness). * @param data Buffer where to store data accepted during the DATA stage. * (they will come in USB endianness). * @param data_size Size of the @p data buffer * (in native endianness). * @param actual_data_size Actual size of transfered data * (in native endianness). * @return Error code. * @retval EBADMEM @p pipe is NULL. * @retval EBADMEM @p data is NULL and @p data_size is not zero. * @retval ERANGE Data buffer too large. */ int usb_control_request_get(usb_pipe_t *pipe, usb_request_type_t request_type, usb_request_recipient_t recipient, uint8_t request, uint16_t value, uint16_t index, void *data, size_t data_size, size_t *actual_data_size) { if (pipe == NULL) { return EBADMEM; } if (data_size > MAX_DATA_LENGTH) { return ERANGE; } if ((data_size > 0) && (data == NULL)) { return EBADMEM; } /* * TODO: check that @p request_type and @p recipient are * within ranges. */ const usb_device_request_setup_packet_t setup_packet = { .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST | (request_type << 5) | recipient, .request = request, .value = uint16_host2usb(value), .index = uint16_host2usb(index), .length = uint16_host2usb(data_size), }; return usb_pipe_control_read(pipe, &setup_packet, sizeof(setup_packet), data, data_size, actual_data_size); } /** Retrieve status of a USB device. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] index Recipient index (in native endianness). * @param[in] recipient Recipient of the GET_STATUS request. * @param[out] status Recipient status (in native endianness). * @return Error code. */ int usb_request_get_status(usb_pipe_t *pipe, usb_request_recipient_t recipient, uint16_t index, uint16_t *status) { if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) { return EINVAL; } if (status == NULL) { return EBADMEM; } uint16_t status_usb_endianess; size_t data_transfered_size; int rc = usb_control_request_get(pipe, USB_REQUEST_TYPE_STANDARD, recipient, USB_DEVREQ_GET_STATUS, 0, uint16_host2usb(index), &status_usb_endianess, 2, &data_transfered_size); if (rc != EOK) { return rc; } if (data_transfered_size != 2) { return ELIMIT; } *status = uint16_usb2host(status_usb_endianess); return EOK; } /** Clear or disable specific device feature. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] request_type Request type (standard/class/vendor). * @param[in] recipient Recipient of the CLEAR_FEATURE request. * @param[in] feature_selector Feature selector (in native endianness). * @param[in] index Recipient index (in native endianness). * @return Error code. */ int usb_request_clear_feature(usb_pipe_t *pipe, usb_request_type_t request_type, usb_request_recipient_t recipient, uint16_t feature_selector, uint16_t index) { if (request_type == USB_REQUEST_TYPE_STANDARD) { if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) { return EINVAL; } } int rc = usb_control_request_set(pipe, request_type, recipient, USB_DEVREQ_CLEAR_FEATURE, uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0); return rc; } /** Set or enable specific device feature. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] request_type Request type (standard/class/vendor). * @param[in] recipient Recipient of the SET_FEATURE request. * @param[in] feature_selector Feature selector (in native endianness). * @param[in] index Recipient index (in native endianness). * @return Error code. */ int usb_request_set_feature(usb_pipe_t *pipe, usb_request_type_t request_type, usb_request_recipient_t recipient, uint16_t feature_selector, uint16_t index) { if (request_type == USB_REQUEST_TYPE_STANDARD) { if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) { return EINVAL; } } int rc = usb_control_request_set(pipe, request_type, recipient, USB_DEVREQ_SET_FEATURE, uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0); return rc; } /** Retrieve USB descriptor of a USB device. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] request_type Request type (standard/class/vendor). * @param[in] recipient Request recipient (device/interface/endpoint). * @param[in] descriptor_type Descriptor type (device/configuration/HID/...). * @param[in] descriptor_index Descriptor index. * @param[in] language Language index. * @param[out] buffer Buffer where to store the retrieved descriptor. * @param[in] size Size of the @p buffer. * @param[out] actual_size Number of bytes actually transferred. * @return Error code. */ int usb_request_get_descriptor(usb_pipe_t *pipe, usb_request_type_t request_type, usb_request_recipient_t recipient, uint8_t descriptor_type, uint8_t descriptor_index, uint16_t language, void *buffer, size_t size, size_t *actual_size) { if (buffer == NULL) { return EBADMEM; } if (size == 0) { return EINVAL; } const uint16_t wValue = descriptor_index | (descriptor_type << 8); return usb_control_request_get(pipe, request_type, recipient, USB_DEVREQ_GET_DESCRIPTOR, wValue, language, buffer, size, actual_size); } /** Retrieve USB descriptor, allocate space for it. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] request_type Request type (standard/class/vendor). * @param[in] recipient Request recipient (device/interface/endpoint). * @param[in] descriptor_type Descriptor type (device/configuration/HID/...). * @param[in] descriptor_index Descriptor index. * @param[in] language Language index. * @param[out] buffer_ptr Where to store pointer to allocated buffer. * @param[out] buffer_size Where to store the size of the descriptor. * @return */ int usb_request_get_descriptor_alloc(usb_pipe_t * pipe, usb_request_type_t request_type, usb_request_recipient_t recipient, uint8_t descriptor_type, uint8_t descriptor_index, uint16_t language, void **buffer_ptr, size_t *buffer_size) { if (buffer_ptr == NULL) { return EBADMEM; } int rc; /* * Get only first byte to retrieve descriptor length. */ uint8_t tmp_buffer[1]; size_t bytes_transfered; rc = usb_request_get_descriptor(pipe, request_type, recipient, descriptor_type, descriptor_index, language, &tmp_buffer, 1, &bytes_transfered); if (rc != EOK) { return rc; } if (bytes_transfered != 1) { /* FIXME: some better error code? */ return ESTALL; } size_t size = tmp_buffer[0]; if (size == 0) { /* FIXME: some better error code? */ return ESTALL; } /* * Allocate buffer and get the descriptor again. */ void *buffer = malloc(size); if (buffer == NULL) { return ENOMEM; } rc = usb_request_get_descriptor(pipe, request_type, recipient, descriptor_type, descriptor_index, language, buffer, size, &bytes_transfered); if (rc != EOK) { free(buffer); return rc; } if (bytes_transfered != size) { free(buffer); /* FIXME: some better error code? */ return ESTALL; } *buffer_ptr = buffer; if (buffer_size != NULL) { *buffer_size = size; } return EOK; } /** Retrieve standard device descriptor of a USB device. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[out] descriptor Storage for the device descriptor. * @return Error code. */ int usb_request_get_device_descriptor(usb_pipe_t *pipe, usb_standard_device_descriptor_t *descriptor) { if (descriptor == NULL) { return EBADMEM; } size_t actually_transferred = 0; usb_standard_device_descriptor_t descriptor_tmp; int rc = usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp), &actually_transferred); if (rc != EOK) { return rc; } /* Verify that all data has been transferred. */ if (actually_transferred < sizeof(descriptor_tmp)) { return ELIMIT; } /* Everything is okay, copy the descriptor. */ memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp)); return EOK; } /** Retrieve configuration descriptor of a USB device. * * The function does not retrieve additional data binded with configuration * descriptor (such as its interface and endpoint descriptors) - use * usb_request_get_full_configuration_descriptor() instead. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] index Descriptor index. * @param[out] descriptor Storage for the device descriptor. * @return Error code. */ int usb_request_get_bare_configuration_descriptor(usb_pipe_t *pipe, int index, usb_standard_configuration_descriptor_t *descriptor) { if (descriptor == NULL) { return EBADMEM; } if ((index < 0) || (index > 0xFF)) { return ERANGE; } size_t actually_transferred = 0; usb_standard_configuration_descriptor_t descriptor_tmp; int rc = usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_CONFIGURATION, index, 0, &descriptor_tmp, sizeof(descriptor_tmp), &actually_transferred); if (rc != EOK) { return rc; } /* Verify that all data has been transferred. */ if (actually_transferred < sizeof(descriptor_tmp)) { return ELIMIT; } /* Everything is okay, copy the descriptor. */ memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp)); return EOK; } /** Retrieve full configuration descriptor of a USB device. * * @warning The @p buffer might be touched (i.e. its contents changed) * even when error occurs. * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] index Descriptor index. * @param[out] descriptor Storage for the device descriptor. * @param[in] descriptor_size Size of @p descriptor buffer. * @param[out] actual_size Number of bytes actually transferred. * @return Error code. */ int usb_request_get_full_configuration_descriptor(usb_pipe_t *pipe, int index, void *descriptor, size_t descriptor_size, size_t *actual_size) { if ((index < 0) || (index > 0xFF)) { return ERANGE; } return usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_CONFIGURATION, index, 0, descriptor, descriptor_size, actual_size); } /** Retrieve full configuration descriptor, allocate space for it. * * The function takes care that full configuration descriptor is returned * (i.e. the function will fail when less data then descriptor.totalLength * is returned). * * @param[in] pipe Control endpoint pipe (session must be already started). * @param[in] index Configuration index. * @param[out] descriptor_ptr Where to store pointer to allocated buffer. * @param[out] descriptor_size Where to store the size of the descriptor. * @return Error code. */ int usb_request_get_full_configuration_descriptor_alloc( usb_pipe_t *pipe, int index, void **descriptor_ptr, size_t *descriptor_size) { int rc; if (descriptor_ptr == NULL) { return EBADMEM; } usb_standard_configuration_descriptor_t bare_config; rc = usb_request_get_bare_configuration_descriptor(pipe, index, &bare_config); if (rc != EOK) { return rc; } if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) { return ENOENT; } const size_t total_length = uint16_usb2host(bare_config.total_length); if (total_length < sizeof(bare_config)) { return ELIMIT; } void *buffer = malloc(total_length); if (buffer == NULL) { return ENOMEM; } size_t transferred = 0; rc = usb_request_get_full_configuration_descriptor(pipe, index, buffer, total_length, &transferred); if (rc != EOK) { free(buffer); return rc; } if (transferred != total_length) { free(buffer); return ELIMIT; } /* Everything looks okay, copy the pointers. */ *descriptor_ptr = buffer; if (descriptor_size != NULL) { *descriptor_size = total_length; } return EOK; }