| 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 USB driver - standard USB requests (implementation).
|
|---|
| 34 | */
|
|---|
| 35 | #include <usb/usbdrv.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 |
|
|---|
| 38 | /** Change address of connected device.
|
|---|
| 39 | *
|
|---|
| 40 | * @see usb_drv_reserve_default_address
|
|---|
| 41 | * @see usb_drv_release_default_address
|
|---|
| 42 | * @see usb_drv_request_address
|
|---|
| 43 | * @see usb_drv_release_address
|
|---|
| 44 | * @see usb_drv_bind_address
|
|---|
| 45 | *
|
|---|
| 46 | * @param phone Open phone to HC driver.
|
|---|
| 47 | * @param old_address Current address.
|
|---|
| 48 | * @param address Address to be set.
|
|---|
| 49 | * @return Error code.
|
|---|
| 50 | */
|
|---|
| 51 | int usb_drv_req_set_address(int phone, usb_address_t old_address,
|
|---|
| 52 | usb_address_t new_address)
|
|---|
| 53 | {
|
|---|
| 54 | /* Prepare the target. */
|
|---|
| 55 | usb_target_t target = {
|
|---|
| 56 | .address = old_address,
|
|---|
| 57 | .endpoint = 0
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | /* Prepare the setup packet. */
|
|---|
| 61 | usb_device_request_setup_packet_t setup_packet = {
|
|---|
| 62 | .request_type = 0,
|
|---|
| 63 | .request = USB_DEVREQ_SET_ADDRESS,
|
|---|
| 64 | .index = 0,
|
|---|
| 65 | .length = 0,
|
|---|
| 66 | };
|
|---|
| 67 | setup_packet.value = new_address;
|
|---|
| 68 |
|
|---|
| 69 | int rc = usb_drv_psync_control_write(phone, target,
|
|---|
| 70 | &setup_packet, sizeof(setup_packet), NULL, 0);
|
|---|
| 71 |
|
|---|
| 72 | return rc;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /** Retrieve USB descriptor of connected USB device.
|
|---|
| 76 | *
|
|---|
| 77 | * @param[in] hc_phone Open phone to HC driver.
|
|---|
| 78 | * @param[in] address Device USB address.
|
|---|
| 79 | * @param[in] request_type Request type (standard/class/vendor).
|
|---|
| 80 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
|---|
| 81 | * @param[in] descriptor_index Descriptor index.
|
|---|
| 82 | * @param[in] language Language index.
|
|---|
| 83 | * @param[out] buffer Buffer where to store the retrieved descriptor.
|
|---|
| 84 | * @param[in] size Size of the @p buffer.
|
|---|
| 85 | * @param[out] actual_size Number of bytes actually transferred.
|
|---|
| 86 | * @return Error code.
|
|---|
| 87 | */
|
|---|
| 88 | int usb_drv_req_get_descriptor(int hc_phone, usb_address_t address,
|
|---|
| 89 | usb_request_type_t request_type,
|
|---|
| 90 | uint8_t descriptor_type, uint8_t descriptor_index,
|
|---|
| 91 | uint16_t language,
|
|---|
| 92 | void *buffer, size_t size, size_t *actual_size)
|
|---|
| 93 | {
|
|---|
| 94 | if (buffer == NULL) {
|
|---|
| 95 | return EBADMEM;
|
|---|
| 96 | }
|
|---|
| 97 | if (size == 0) {
|
|---|
| 98 | return EINVAL;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /* Prepare the target. */
|
|---|
| 102 | usb_target_t target = {
|
|---|
| 103 | .address = address,
|
|---|
| 104 | .endpoint = 0
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | /* Prepare the setup packet. */
|
|---|
| 108 | usb_device_request_setup_packet_t setup_packet = {
|
|---|
| 109 | .request_type = 128 | (request_type << 5),
|
|---|
| 110 | .request = USB_DEVREQ_GET_DESCRIPTOR,
|
|---|
| 111 | .index = language,
|
|---|
| 112 | .length = (uint16_t) size,
|
|---|
| 113 | };
|
|---|
| 114 | setup_packet.value_high = descriptor_type;
|
|---|
| 115 | setup_packet.value_low = descriptor_index;
|
|---|
| 116 |
|
|---|
| 117 | /* Perform CONTROL READ */
|
|---|
| 118 | int rc = usb_drv_psync_control_read(hc_phone, target,
|
|---|
| 119 | &setup_packet, sizeof(setup_packet),
|
|---|
| 120 | buffer, size, actual_size);
|
|---|
| 121 |
|
|---|
| 122 | return rc;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** Retrieve device descriptor of connected USB device.
|
|---|
| 126 | *
|
|---|
| 127 | * @param[in] phone Open phone to HC driver.
|
|---|
| 128 | * @param[in] address Device USB address.
|
|---|
| 129 | * @param[out] descriptor Storage for the device descriptor.
|
|---|
| 130 | * @return Error code.
|
|---|
| 131 | * @retval EBADMEM @p descriptor is NULL.
|
|---|
| 132 | */
|
|---|
| 133 | int usb_drv_req_get_device_descriptor(int phone, usb_address_t address,
|
|---|
| 134 | usb_standard_device_descriptor_t *descriptor)
|
|---|
| 135 | {
|
|---|
| 136 | if (descriptor == NULL) {
|
|---|
| 137 | return EBADMEM;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | size_t actually_transferred = 0;
|
|---|
| 141 | usb_standard_device_descriptor_t descriptor_tmp;
|
|---|
| 142 | int rc = usb_drv_req_get_descriptor(phone, address,
|
|---|
| 143 | USB_REQUEST_TYPE_STANDARD,
|
|---|
| 144 | USB_DESCTYPE_DEVICE, 0,
|
|---|
| 145 | 0,
|
|---|
| 146 | &descriptor_tmp, sizeof(descriptor_tmp),
|
|---|
| 147 | &actually_transferred);
|
|---|
| 148 |
|
|---|
| 149 | if (rc != EOK) {
|
|---|
| 150 | return rc;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /* Verify that all data has been transferred. */
|
|---|
| 154 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
|---|
| 155 | return ELIMIT;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /* Everything is okay, copy the descriptor. */
|
|---|
| 159 | memcpy(descriptor, &descriptor_tmp,
|
|---|
| 160 | sizeof(descriptor_tmp));
|
|---|
| 161 |
|
|---|
| 162 | return EOK;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | /** Retrieve configuration descriptor of connected USB device.
|
|---|
| 167 | *
|
|---|
| 168 | * The function does not retrieve additional data binded with configuration
|
|---|
| 169 | * descriptor (such as its interface and endpoint descriptors) - use
|
|---|
| 170 | * usb_drv_req_get_full_configuration_descriptor() instead.
|
|---|
| 171 | *
|
|---|
| 172 | * @param[in] phone Open phone to HC driver.
|
|---|
| 173 | * @param[in] address Device USB address.
|
|---|
| 174 | * @param[in] index Configuration descriptor index.
|
|---|
| 175 | * @param[out] descriptor Storage for the configuration descriptor.
|
|---|
| 176 | * @return Error code.
|
|---|
| 177 | * @retval EBADMEM @p descriptor is NULL.
|
|---|
| 178 | */
|
|---|
| 179 | int usb_drv_req_get_bare_configuration_descriptor(int phone,
|
|---|
| 180 | usb_address_t address, int index,
|
|---|
| 181 | usb_standard_configuration_descriptor_t *descriptor)
|
|---|
| 182 | {
|
|---|
| 183 | if (descriptor == NULL) {
|
|---|
| 184 | return EBADMEM;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | size_t actually_transferred = 0;
|
|---|
| 188 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
|---|
| 189 | int rc = usb_drv_req_get_descriptor(phone, address,
|
|---|
| 190 | USB_REQUEST_TYPE_STANDARD,
|
|---|
| 191 | USB_DESCTYPE_CONFIGURATION, 0,
|
|---|
| 192 | 0,
|
|---|
| 193 | &descriptor_tmp, sizeof(descriptor_tmp),
|
|---|
| 194 | &actually_transferred);
|
|---|
| 195 |
|
|---|
| 196 | if (rc != EOK) {
|
|---|
| 197 | return rc;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /* Verify that all data has been transferred. */
|
|---|
| 201 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
|---|
| 202 | return ELIMIT;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /* Everything is okay, copy the descriptor. */
|
|---|
| 206 | memcpy(descriptor, &descriptor_tmp,
|
|---|
| 207 | sizeof(descriptor_tmp));
|
|---|
| 208 |
|
|---|
| 209 | return EOK;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /** Retrieve full configuration descriptor of connected USB device.
|
|---|
| 213 | *
|
|---|
| 214 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
|---|
| 215 | * even when error occurres.
|
|---|
| 216 | *
|
|---|
| 217 | * @param[in] phone Open phone to HC driver.
|
|---|
| 218 | * @param[in] address Device USB address.
|
|---|
| 219 | * @param[in] index Configuration descriptor index.
|
|---|
| 220 | * @param[out] buffer Buffer for the whole configuration descriptor.
|
|---|
| 221 | * @param[in] buffer_size Size of the prepared @p buffer.
|
|---|
| 222 | * @param[out] actual_buffer_size Bytes actually transfered.
|
|---|
| 223 | * @return Error code.
|
|---|
| 224 | * @retval EBADMEM @p descriptor is NULL.
|
|---|
| 225 | */
|
|---|
| 226 | int usb_drv_req_get_full_configuration_descriptor(int phone,
|
|---|
| 227 | usb_address_t address, int index,
|
|---|
| 228 | void *buffer, size_t buffer_size, size_t *actual_buffer_size)
|
|---|
| 229 | {
|
|---|
| 230 | int rc = usb_drv_req_get_descriptor(phone, address,
|
|---|
| 231 | USB_REQUEST_TYPE_STANDARD,
|
|---|
| 232 | USB_DESCTYPE_CONFIGURATION, 0,
|
|---|
| 233 | 0,
|
|---|
| 234 | buffer, buffer_size,
|
|---|
| 235 | actual_buffer_size);
|
|---|
| 236 |
|
|---|
| 237 | return rc;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | /**
|
|---|
| 242 | * @}
|
|---|
| 243 | */
|
|---|