[0a37e14] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 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 |
|
---|
[160b75e] | 29 | /** @addtogroup libusbdev
|
---|
[0a37e14] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Standard USB requests (implementation).
|
---|
| 34 | */
|
---|
[7d521e24] | 35 | #include <usb/dev/request.h>
|
---|
[c01987c] | 36 | #include <usb/request.h>
|
---|
| 37 | #include <usb/usb.h>
|
---|
| 38 |
|
---|
[0a37e14] | 39 | #include <errno.h>
|
---|
[c01987c] | 40 | #include <mem.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include <str.h>
|
---|
[0a37e14] | 43 |
|
---|
[3e9074f] | 44 | #define MAX_DATA_LENGTH ((size_t)(0xFFFF))
|
---|
[a4a8cca] | 45 |
|
---|
| 46 | /** Generic wrapper for SET requests using standard control request format.
|
---|
| 47 | *
|
---|
[3954a63b] | 48 | * @see usb_pipe_control_write
|
---|
[a4a8cca] | 49 | *
|
---|
| 50 | * @param pipe Pipe used for the communication.
|
---|
| 51 | * @param request_type Request type (standard/class/vendor).
|
---|
| 52 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
| 53 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
| 54 | * @param value Value of @c wValue field of setup packet
|
---|
[f8d43aa] | 55 | * (must be in USB endianness).
|
---|
[a4a8cca] | 56 | * @param index Value of @c wIndex field of setup packet
|
---|
[f8d43aa] | 57 | * (must be in USB endianness).
|
---|
[a4a8cca] | 58 | * @param data Data to be sent during DATA stage
|
---|
[f8d43aa] | 59 | * (expected to be in USB endianness).
|
---|
[a4a8cca] | 60 | * @param data_size Size of the @p data buffer (in native endianness).
|
---|
| 61 | * @return Error code.
|
---|
| 62 | * @retval EBADMEM @p pipe is NULL.
|
---|
| 63 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
| 64 | * @retval ERANGE Data buffer too large.
|
---|
| 65 | */
|
---|
[a372663] | 66 | int usb_control_request_set(usb_pipe_t *pipe,
|
---|
[a4a8cca] | 67 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
[f8d43aa] | 68 | uint8_t request, uint16_t value, uint16_t index,
|
---|
| 69 | const void *data, size_t data_size)
|
---|
[a4a8cca] | 70 | {
|
---|
| 71 | if (pipe == NULL) {
|
---|
| 72 | return EBADMEM;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | if (data_size > MAX_DATA_LENGTH) {
|
---|
| 76 | return ERANGE;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | if ((data_size > 0) && (data == NULL)) {
|
---|
| 80 | return EBADMEM;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /*
|
---|
| 84 | * TODO: check that @p request_type and @p recipient are
|
---|
| 85 | * within ranges.
|
---|
| 86 | */
|
---|
| 87 |
|
---|
[f8d43aa] | 88 | const usb_device_request_setup_packet_t setup_packet = {
|
---|
| 89 | .request_type = (request_type << 5) | recipient,
|
---|
| 90 | .request = request,
|
---|
| 91 | .value = value,
|
---|
| 92 | .index = index,
|
---|
| 93 | .length = (uint16_t) data_size,
|
---|
| 94 | };
|
---|
[a4a8cca] | 95 |
|
---|
[f8d43aa] | 96 | return usb_pipe_control_write(pipe,
|
---|
| 97 | &setup_packet, sizeof(setup_packet), data, data_size);
|
---|
[a4a8cca] | 98 | }
|
---|
| 99 |
|
---|
| 100 | /** Generic wrapper for GET requests using standard control request format.
|
---|
| 101 | *
|
---|
[3954a63b] | 102 | * @see usb_pipe_control_read
|
---|
[a4a8cca] | 103 | *
|
---|
| 104 | * @param pipe Pipe used for the communication.
|
---|
| 105 | * @param request_type Request type (standard/class/vendor).
|
---|
| 106 | * @param recipient Request recipient (e.g. device or endpoint).
|
---|
| 107 | * @param request Actual request (e.g. GET_DESCRIPTOR).
|
---|
| 108 | * @param value Value of @c wValue field of setup packet
|
---|
[f8d43aa] | 109 | * (must be in USB endianness).
|
---|
[a4a8cca] | 110 | * @param index Value of @c wIndex field of setup packet
|
---|
| 111 | * (must be in USB endianness).
|
---|
| 112 | * @param data Buffer where to store data accepted during the DATA stage.
|
---|
[a6add7a] | 113 | * (they will come in USB endianness).
|
---|
[a4a8cca] | 114 | * @param data_size Size of the @p data buffer
|
---|
[94c40ce2] | 115 | * (in native endianness).
|
---|
[a4a8cca] | 116 | * @param actual_data_size Actual size of transfered data
|
---|
[f8d43aa] | 117 | * (in native endianness).
|
---|
[a4a8cca] | 118 | * @return Error code.
|
---|
| 119 | * @retval EBADMEM @p pipe is NULL.
|
---|
| 120 | * @retval EBADMEM @p data is NULL and @p data_size is not zero.
|
---|
| 121 | * @retval ERANGE Data buffer too large.
|
---|
| 122 | */
|
---|
[a372663] | 123 | int usb_control_request_get(usb_pipe_t *pipe,
|
---|
[a4a8cca] | 124 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
[f8d43aa] | 125 | uint8_t request, uint16_t value, uint16_t index,
|
---|
[a4a8cca] | 126 | void *data, size_t data_size, size_t *actual_data_size)
|
---|
| 127 | {
|
---|
| 128 | if (pipe == NULL) {
|
---|
| 129 | return EBADMEM;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | if (data_size > MAX_DATA_LENGTH) {
|
---|
| 133 | return ERANGE;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | if ((data_size > 0) && (data == NULL)) {
|
---|
| 137 | return EBADMEM;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /*
|
---|
| 141 | * TODO: check that @p request_type and @p recipient are
|
---|
| 142 | * within ranges.
|
---|
| 143 | */
|
---|
| 144 |
|
---|
[095bddfc] | 145 | const usb_device_request_setup_packet_t setup_packet = {
|
---|
| 146 | .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST
|
---|
| 147 | | (request_type << 5) | recipient,
|
---|
| 148 | .request = request,
|
---|
[9f807c3] | 149 | .value = uint16_host2usb(value),
|
---|
| 150 | .index = uint16_host2usb(index),
|
---|
| 151 | .length = uint16_host2usb(data_size),
|
---|
[095bddfc] | 152 | };
|
---|
[a4a8cca] | 153 |
|
---|
[095bddfc] | 154 | return usb_pipe_control_read(pipe, &setup_packet, sizeof(setup_packet),
|
---|
[a4a8cca] | 155 | data, data_size, actual_data_size);
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[9cf1c5a] | 158 | /** Retrieve status of a USB device.
|
---|
| 159 | *
|
---|
| 160 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 161 | * @param[in] index Recipient index (in native endianness).
|
---|
| 162 | * @param[in] recipient Recipient of the GET_STATUS request.
|
---|
| 163 | * @param[out] status Recipient status (in native endianness).
|
---|
| 164 | * @return Error code.
|
---|
| 165 | */
|
---|
[a372663] | 166 | int usb_request_get_status(usb_pipe_t *pipe,
|
---|
[9cf1c5a] | 167 | usb_request_recipient_t recipient, uint16_t index,
|
---|
| 168 | uint16_t *status)
|
---|
| 169 | {
|
---|
| 170 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0)) {
|
---|
| 171 | return EINVAL;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | if (status == NULL) {
|
---|
| 175 | return EBADMEM;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | uint16_t status_usb_endianess;
|
---|
| 179 | size_t data_transfered_size;
|
---|
| 180 | int rc = usb_control_request_get(pipe, USB_REQUEST_TYPE_STANDARD,
|
---|
| 181 | recipient, USB_DEVREQ_GET_STATUS, 0, uint16_host2usb(index),
|
---|
| 182 | &status_usb_endianess, 2, &data_transfered_size);
|
---|
| 183 | if (rc != EOK) {
|
---|
| 184 | return rc;
|
---|
| 185 | }
|
---|
| 186 | if (data_transfered_size != 2) {
|
---|
| 187 | return ELIMIT;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | *status = uint16_usb2host(status_usb_endianess);
|
---|
| 191 |
|
---|
| 192 | return EOK;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /** Clear or disable specific device feature.
|
---|
| 196 | *
|
---|
| 197 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 198 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
| 199 | * @param[in] recipient Recipient of the CLEAR_FEATURE request.
|
---|
| 200 | * @param[in] feature_selector Feature selector (in native endianness).
|
---|
| 201 | * @param[in] index Recipient index (in native endianness).
|
---|
| 202 | * @return Error code.
|
---|
| 203 | */
|
---|
[a372663] | 204 | int usb_request_clear_feature(usb_pipe_t *pipe,
|
---|
[9cf1c5a] | 205 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
| 206 | uint16_t feature_selector, uint16_t index)
|
---|
| 207 | {
|
---|
| 208 | if (request_type == USB_REQUEST_TYPE_STANDARD) {
|
---|
[f8d43aa] | 209 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
|
---|
| 210 | {
|
---|
[9cf1c5a] | 211 | return EINVAL;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[f8d43aa] | 215 | return usb_control_request_set(pipe,
|
---|
| 216 | request_type, recipient, USB_DEVREQ_CLEAR_FEATURE,
|
---|
| 217 | uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
|
---|
[9cf1c5a] | 218 | }
|
---|
| 219 |
|
---|
| 220 | /** Set or enable specific device feature.
|
---|
| 221 | *
|
---|
| 222 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 223 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
| 224 | * @param[in] recipient Recipient of the SET_FEATURE request.
|
---|
| 225 | * @param[in] feature_selector Feature selector (in native endianness).
|
---|
| 226 | * @param[in] index Recipient index (in native endianness).
|
---|
| 227 | * @return Error code.
|
---|
| 228 | */
|
---|
[a372663] | 229 | int usb_request_set_feature(usb_pipe_t *pipe,
|
---|
[9cf1c5a] | 230 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
| 231 | uint16_t feature_selector, uint16_t index)
|
---|
| 232 | {
|
---|
| 233 | if (request_type == USB_REQUEST_TYPE_STANDARD) {
|
---|
[f8d43aa] | 234 | if ((recipient == USB_REQUEST_RECIPIENT_DEVICE) && (index != 0))
|
---|
| 235 | {
|
---|
[9cf1c5a] | 236 | return EINVAL;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[f8d43aa] | 240 | return usb_control_request_set(pipe,
|
---|
| 241 | request_type, recipient, USB_DEVREQ_SET_FEATURE,
|
---|
| 242 | uint16_host2usb(feature_selector), uint16_host2usb(index), NULL, 0);
|
---|
[9cf1c5a] | 243 | }
|
---|
| 244 |
|
---|
[0a37e14] | 245 | /** Retrieve USB descriptor of a USB device.
|
---|
| 246 | *
|
---|
| 247 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 248 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
[bc1c6fb] | 249 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
[0a37e14] | 250 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
| 251 | * @param[in] descriptor_index Descriptor index.
|
---|
| 252 | * @param[in] language Language index.
|
---|
| 253 | * @param[out] buffer Buffer where to store the retrieved descriptor.
|
---|
| 254 | * @param[in] size Size of the @p buffer.
|
---|
| 255 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
| 256 | * @return Error code.
|
---|
| 257 | */
|
---|
[a372663] | 258 | int usb_request_get_descriptor(usb_pipe_t *pipe,
|
---|
[ad4562c2] | 259 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
[0a37e14] | 260 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
| 261 | uint16_t language,
|
---|
| 262 | void *buffer, size_t size, size_t *actual_size)
|
---|
| 263 | {
|
---|
| 264 | if (buffer == NULL) {
|
---|
| 265 | return EBADMEM;
|
---|
| 266 | }
|
---|
| 267 | if (size == 0) {
|
---|
| 268 | return EINVAL;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[94c40ce2] | 271 | /* The wValue field specifies the descriptor type in the high byte
|
---|
| 272 | * and the descriptor index in the low byte. USB 1.1 spec p. 189
|
---|
| 273 | */
|
---|
[095bddfc] | 274 | const uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
[0a37e14] | 275 |
|
---|
[787421c] | 276 | return usb_control_request_get(pipe,
|
---|
[ad4562c2] | 277 | request_type, recipient,
|
---|
[787421c] | 278 | USB_DEVREQ_GET_DESCRIPTOR,
|
---|
| 279 | wValue, language,
|
---|
[0a37e14] | 280 | buffer, size, actual_size);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[071b5f8] | 283 | /** Retrieve USB descriptor, allocate space for it.
|
---|
| 284 | *
|
---|
| 285 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 286 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
[bc1c6fb] | 287 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
[071b5f8] | 288 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
| 289 | * @param[in] descriptor_index Descriptor index.
|
---|
| 290 | * @param[in] language Language index.
|
---|
| 291 | * @param[out] buffer_ptr Where to store pointer to allocated buffer.
|
---|
| 292 | * @param[out] buffer_size Where to store the size of the descriptor.
|
---|
| 293 | * @return
|
---|
| 294 | */
|
---|
[a372663] | 295 | int usb_request_get_descriptor_alloc(usb_pipe_t * pipe,
|
---|
[ad4562c2] | 296 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
[071b5f8] | 297 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
| 298 | uint16_t language,
|
---|
| 299 | void **buffer_ptr, size_t *buffer_size)
|
---|
| 300 | {
|
---|
| 301 | if (buffer_ptr == NULL) {
|
---|
| 302 | return EBADMEM;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | int rc;
|
---|
| 306 |
|
---|
| 307 | /*
|
---|
| 308 | * Get only first byte to retrieve descriptor length.
|
---|
| 309 | */
|
---|
[f8d43aa] | 310 | uint8_t tmp_buffer;
|
---|
[071b5f8] | 311 | size_t bytes_transfered;
|
---|
[ad4562c2] | 312 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
[071b5f8] | 313 | descriptor_type, descriptor_index, language,
|
---|
[f8d43aa] | 314 | &tmp_buffer, sizeof(tmp_buffer), &bytes_transfered);
|
---|
[071b5f8] | 315 | if (rc != EOK) {
|
---|
| 316 | return rc;
|
---|
| 317 | }
|
---|
| 318 | if (bytes_transfered != 1) {
|
---|
[f8d43aa] | 319 | return ELIMIT;
|
---|
[071b5f8] | 320 | }
|
---|
| 321 |
|
---|
[f8d43aa] | 322 | const size_t size = tmp_buffer;
|
---|
[071b5f8] | 323 | if (size == 0) {
|
---|
[f8d43aa] | 324 | return ELIMIT;
|
---|
[071b5f8] | 325 | }
|
---|
| 326 |
|
---|
| 327 | /*
|
---|
| 328 | * Allocate buffer and get the descriptor again.
|
---|
| 329 | */
|
---|
| 330 | void *buffer = malloc(size);
|
---|
| 331 | if (buffer == NULL) {
|
---|
| 332 | return ENOMEM;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[ad4562c2] | 335 | rc = usb_request_get_descriptor(pipe, request_type, recipient,
|
---|
[071b5f8] | 336 | descriptor_type, descriptor_index, language,
|
---|
| 337 | buffer, size, &bytes_transfered);
|
---|
| 338 | if (rc != EOK) {
|
---|
| 339 | free(buffer);
|
---|
| 340 | return rc;
|
---|
| 341 | }
|
---|
| 342 | if (bytes_transfered != size) {
|
---|
| 343 | free(buffer);
|
---|
[f8d43aa] | 344 | return ELIMIT;
|
---|
[071b5f8] | 345 | }
|
---|
| 346 |
|
---|
| 347 | *buffer_ptr = buffer;
|
---|
| 348 | if (buffer_size != NULL) {
|
---|
| 349 | *buffer_size = size;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | return EOK;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[abe8ac5] | 355 | /** Retrieve standard device descriptor of a USB device.
|
---|
| 356 | *
|
---|
| 357 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 358 | * @param[out] descriptor Storage for the device descriptor.
|
---|
| 359 | * @return Error code.
|
---|
| 360 | */
|
---|
[a372663] | 361 | int usb_request_get_device_descriptor(usb_pipe_t *pipe,
|
---|
[abe8ac5] | 362 | usb_standard_device_descriptor_t *descriptor)
|
---|
| 363 | {
|
---|
| 364 | if (descriptor == NULL) {
|
---|
| 365 | return EBADMEM;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | size_t actually_transferred = 0;
|
---|
| 369 | usb_standard_device_descriptor_t descriptor_tmp;
|
---|
| 370 | int rc = usb_request_get_descriptor(pipe,
|
---|
[9f807c3] | 371 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
[f8d43aa] | 372 | USB_DESCTYPE_DEVICE, 0, 0, &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
[abe8ac5] | 373 | &actually_transferred);
|
---|
| 374 |
|
---|
| 375 | if (rc != EOK) {
|
---|
| 376 | return rc;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | /* Verify that all data has been transferred. */
|
---|
| 380 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
| 381 | return ELIMIT;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /* Everything is okay, copy the descriptor. */
|
---|
[49bd7ae2] | 385 | memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
|
---|
[abe8ac5] | 386 |
|
---|
| 387 | return EOK;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | /** Retrieve configuration descriptor of a USB device.
|
---|
| 391 | *
|
---|
| 392 | * The function does not retrieve additional data binded with configuration
|
---|
| 393 | * descriptor (such as its interface and endpoint descriptors) - use
|
---|
| 394 | * usb_request_get_full_configuration_descriptor() instead.
|
---|
| 395 | *
|
---|
| 396 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 397 | * @param[in] index Descriptor index.
|
---|
| 398 | * @param[out] descriptor Storage for the device descriptor.
|
---|
| 399 | * @return Error code.
|
---|
| 400 | */
|
---|
[a372663] | 401 | int usb_request_get_bare_configuration_descriptor(usb_pipe_t *pipe,
|
---|
[abe8ac5] | 402 | int index, usb_standard_configuration_descriptor_t *descriptor)
|
---|
| 403 | {
|
---|
| 404 | if (descriptor == NULL) {
|
---|
| 405 | return EBADMEM;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | if ((index < 0) || (index > 0xFF)) {
|
---|
| 409 | return ERANGE;
|
---|
| 410 | }
|
---|
| 411 |
|
---|
| 412 | size_t actually_transferred = 0;
|
---|
| 413 | usb_standard_configuration_descriptor_t descriptor_tmp;
|
---|
[f8d43aa] | 414 | const int rc = usb_request_get_descriptor(pipe,
|
---|
[ad4562c2] | 415 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 416 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
[abe8ac5] | 417 | &descriptor_tmp, sizeof(descriptor_tmp),
|
---|
| 418 | &actually_transferred);
|
---|
| 419 | if (rc != EOK) {
|
---|
| 420 | return rc;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /* Verify that all data has been transferred. */
|
---|
| 424 | if (actually_transferred < sizeof(descriptor_tmp)) {
|
---|
| 425 | return ELIMIT;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /* Everything is okay, copy the descriptor. */
|
---|
[49bd7ae2] | 429 | memcpy(descriptor, &descriptor_tmp, sizeof(descriptor_tmp));
|
---|
[abe8ac5] | 430 | return EOK;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | /** Retrieve full configuration descriptor of a USB device.
|
---|
| 434 | *
|
---|
| 435 | * @warning The @p buffer might be touched (i.e. its contents changed)
|
---|
| 436 | * even when error occurs.
|
---|
| 437 | *
|
---|
| 438 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 439 | * @param[in] index Descriptor index.
|
---|
| 440 | * @param[out] descriptor Storage for the device descriptor.
|
---|
| 441 | * @param[in] descriptor_size Size of @p descriptor buffer.
|
---|
| 442 | * @param[out] actual_size Number of bytes actually transferred.
|
---|
| 443 | * @return Error code.
|
---|
| 444 | */
|
---|
[a372663] | 445 | int usb_request_get_full_configuration_descriptor(usb_pipe_t *pipe,
|
---|
[abe8ac5] | 446 | int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
|
---|
| 447 | {
|
---|
| 448 | if ((index < 0) || (index > 0xFF)) {
|
---|
| 449 | return ERANGE;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | return usb_request_get_descriptor(pipe,
|
---|
[ad4562c2] | 453 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 454 | USB_DESCTYPE_CONFIGURATION, index, 0,
|
---|
[abe8ac5] | 455 | descriptor, descriptor_size, actual_size);
|
---|
| 456 | }
|
---|
| 457 |
|
---|
[4723444] | 458 | /** Retrieve full configuration descriptor, allocate space for it.
|
---|
| 459 | *
|
---|
| 460 | * The function takes care that full configuration descriptor is returned
|
---|
| 461 | * (i.e. the function will fail when less data then descriptor.totalLength
|
---|
| 462 | * is returned).
|
---|
| 463 | *
|
---|
| 464 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 465 | * @param[in] index Configuration index.
|
---|
| 466 | * @param[out] descriptor_ptr Where to store pointer to allocated buffer.
|
---|
| 467 | * @param[out] descriptor_size Where to store the size of the descriptor.
|
---|
| 468 | * @return Error code.
|
---|
| 469 | */
|
---|
| 470 | int usb_request_get_full_configuration_descriptor_alloc(
|
---|
[a372663] | 471 | usb_pipe_t *pipe, int index,
|
---|
[0eb2a0f] | 472 | const void **descriptor_ptr, size_t *descriptor_size)
|
---|
[4723444] | 473 | {
|
---|
| 474 | int rc;
|
---|
| 475 |
|
---|
| 476 | if (descriptor_ptr == NULL) {
|
---|
| 477 | return EBADMEM;
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | usb_standard_configuration_descriptor_t bare_config;
|
---|
| 481 | rc = usb_request_get_bare_configuration_descriptor(pipe, index,
|
---|
| 482 | &bare_config);
|
---|
| 483 | if (rc != EOK) {
|
---|
| 484 | return rc;
|
---|
| 485 | }
|
---|
| 486 | if (bare_config.descriptor_type != USB_DESCTYPE_CONFIGURATION) {
|
---|
| 487 | return ENOENT;
|
---|
| 488 | }
|
---|
[12e689f] | 489 |
|
---|
| 490 | const size_t total_length = uint16_usb2host(bare_config.total_length);
|
---|
| 491 | if (total_length < sizeof(bare_config)) {
|
---|
[4723444] | 492 | return ELIMIT;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
[12e689f] | 495 | void *buffer = malloc(total_length);
|
---|
[4723444] | 496 | if (buffer == NULL) {
|
---|
| 497 | return ENOMEM;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | size_t transferred = 0;
|
---|
| 501 | rc = usb_request_get_full_configuration_descriptor(pipe, index,
|
---|
[12e689f] | 502 | buffer, total_length, &transferred);
|
---|
[4723444] | 503 | if (rc != EOK) {
|
---|
| 504 | free(buffer);
|
---|
| 505 | return rc;
|
---|
| 506 | }
|
---|
| 507 |
|
---|
[12e689f] | 508 | if (transferred != total_length) {
|
---|
[4723444] | 509 | free(buffer);
|
---|
| 510 | return ELIMIT;
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | /* Everything looks okay, copy the pointers. */
|
---|
| 514 |
|
---|
| 515 | *descriptor_ptr = buffer;
|
---|
| 516 |
|
---|
| 517 | if (descriptor_size != NULL) {
|
---|
[12e689f] | 518 | *descriptor_size = total_length;
|
---|
[4723444] | 519 | }
|
---|
| 520 |
|
---|
| 521 | return EOK;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[9cf1c5a] | 524 | /** Update existing or add new USB descriptor to a USB device.
|
---|
| 525 | *
|
---|
| 526 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 527 | * @param[in] request_type Request type (standard/class/vendor).
|
---|
| 528 | * @param[in] recipient Request recipient (device/interface/endpoint).
|
---|
| 529 | * @param[in] descriptor_type Descriptor type (device/configuration/HID/...).
|
---|
| 530 | * @param[in] descriptor_index Descriptor index.
|
---|
| 531 | * @param[in] language Language index (in native endianness).
|
---|
| 532 | * @param[in] buffer Buffer with the new descriptor (in USB endianness).
|
---|
| 533 | * @param[in] size Size of the @p buffer in bytes (in native endianness).
|
---|
| 534 | * @return Error code.
|
---|
| 535 | */
|
---|
[a372663] | 536 | int usb_request_set_descriptor(usb_pipe_t *pipe,
|
---|
[9cf1c5a] | 537 | usb_request_type_t request_type, usb_request_recipient_t recipient,
|
---|
| 538 | uint8_t descriptor_type, uint8_t descriptor_index,
|
---|
[f8d43aa] | 539 | uint16_t language, const void *buffer, size_t size)
|
---|
[9cf1c5a] | 540 | {
|
---|
| 541 | if (buffer == NULL) {
|
---|
| 542 | return EBADMEM;
|
---|
| 543 | }
|
---|
| 544 | if (size == 0) {
|
---|
| 545 | return EINVAL;
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | /* FIXME: proper endianness. */
|
---|
| 549 | uint16_t wValue = descriptor_index | (descriptor_type << 8);
|
---|
| 550 |
|
---|
| 551 | return usb_control_request_set(pipe,
|
---|
[f8d43aa] | 552 | request_type, recipient, USB_DEVREQ_SET_DESCRIPTOR,
|
---|
| 553 | wValue, language, buffer, size);
|
---|
[9cf1c5a] | 554 | }
|
---|
| 555 |
|
---|
| 556 | /** Get current configuration value of USB device.
|
---|
| 557 | *
|
---|
| 558 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 559 | * @param[out] configuration_value Current configuration value.
|
---|
| 560 | * @return Error code.
|
---|
| 561 | */
|
---|
[a372663] | 562 | int usb_request_get_configuration(usb_pipe_t *pipe,
|
---|
[9cf1c5a] | 563 | uint8_t *configuration_value)
|
---|
| 564 | {
|
---|
| 565 | uint8_t value;
|
---|
| 566 | size_t actual_size;
|
---|
| 567 |
|
---|
[f8d43aa] | 568 | const int rc = usb_control_request_get(pipe,
|
---|
[9cf1c5a] | 569 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
[f8d43aa] | 570 | USB_DEVREQ_GET_CONFIGURATION, 0, 0, &value, 1, &actual_size);
|
---|
[9cf1c5a] | 571 |
|
---|
| 572 | if (rc != EOK) {
|
---|
| 573 | return rc;
|
---|
| 574 | }
|
---|
| 575 | if (actual_size != 1) {
|
---|
| 576 | return ELIMIT;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | if (configuration_value != NULL) {
|
---|
| 580 | *configuration_value = value;
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | return EOK;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[abe8ac5] | 586 | /** Set configuration of USB device.
|
---|
| 587 | *
|
---|
| 588 | * @param pipe Control endpoint pipe (session must be already started).
|
---|
| 589 | * @param configuration_value New configuration value.
|
---|
| 590 | * @return Error code.
|
---|
| 591 | */
|
---|
[a372663] | 592 | int usb_request_set_configuration(usb_pipe_t *pipe,
|
---|
[abe8ac5] | 593 | uint8_t configuration_value)
|
---|
| 594 | {
|
---|
[f8d43aa] | 595 | const uint16_t config_value
|
---|
[2f4438f5] | 596 | = uint16_host2usb((uint16_t) configuration_value);
|
---|
| 597 |
|
---|
[abe8ac5] | 598 | return usb_control_request_set(pipe,
|
---|
| 599 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
[2f4438f5] | 600 | USB_DEVREQ_SET_CONFIGURATION, config_value, 0,
|
---|
[abe8ac5] | 601 | NULL, 0);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
[9cf1c5a] | 604 | /** Get selected alternate setting for USB interface.
|
---|
| 605 | *
|
---|
| 606 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 607 | * @param[in] interface_index Interface index.
|
---|
| 608 | * @param[out] alternate_setting Alternate setting for the interface.
|
---|
| 609 | * @return Error code.
|
---|
| 610 | */
|
---|
[a372663] | 611 | int usb_request_get_interface(usb_pipe_t *pipe,
|
---|
[9cf1c5a] | 612 | uint8_t interface_index, uint8_t *alternate_setting)
|
---|
| 613 | {
|
---|
| 614 | uint8_t value;
|
---|
| 615 | size_t actual_size;
|
---|
| 616 |
|
---|
[f8d43aa] | 617 | const int rc = usb_control_request_get(pipe,
|
---|
[9cf1c5a] | 618 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
| 619 | USB_DEVREQ_GET_INTERFACE,
|
---|
| 620 | 0, uint16_host2usb((uint16_t) interface_index),
|
---|
[f8d43aa] | 621 | &value, sizeof(value), &actual_size);
|
---|
[9cf1c5a] | 622 |
|
---|
| 623 | if (rc != EOK) {
|
---|
| 624 | return rc;
|
---|
| 625 | }
|
---|
| 626 | if (actual_size != 1) {
|
---|
| 627 | return ELIMIT;
|
---|
| 628 | }
|
---|
| 629 |
|
---|
| 630 | if (alternate_setting != NULL) {
|
---|
| 631 | *alternate_setting = value;
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | return EOK;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | /** Select alternate setting for USB interface.
|
---|
| 638 | *
|
---|
| 639 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 640 | * @param[in] interface_index Interface index.
|
---|
| 641 | * @param[in] alternate_setting Alternate setting to select.
|
---|
| 642 | * @return Error code.
|
---|
| 643 | */
|
---|
[a372663] | 644 | int usb_request_set_interface(usb_pipe_t *pipe,
|
---|
[9cf1c5a] | 645 | uint8_t interface_index, uint8_t alternate_setting)
|
---|
| 646 | {
|
---|
| 647 | return usb_control_request_set(pipe,
|
---|
| 648 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
|
---|
| 649 | USB_DEVREQ_SET_INTERFACE,
|
---|
| 650 | uint16_host2usb((uint16_t) alternate_setting),
|
---|
| 651 | uint16_host2usb((uint16_t) interface_index),
|
---|
| 652 | NULL, 0);
|
---|
| 653 | }
|
---|
| 654 |
|
---|
[1a6a234] | 655 | /** Get list of supported languages by USB device.
|
---|
| 656 | *
|
---|
| 657 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
| 658 | * @param[out] languages_ptr Where to store pointer to allocated array of
|
---|
| 659 | * supported languages.
|
---|
| 660 | * @param[out] languages_count Number of supported languages.
|
---|
| 661 | * @return Error code.
|
---|
| 662 | */
|
---|
[a372663] | 663 | int usb_request_get_supported_languages(usb_pipe_t *pipe,
|
---|
[1a6a234] | 664 | l18_win_locales_t **languages_ptr, size_t *languages_count)
|
---|
| 665 | {
|
---|
[f8d43aa] | 666 | if (languages_ptr == NULL || languages_count == NULL) {
|
---|
[1a6a234] | 667 | return EBADMEM;
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | uint8_t *string_descriptor = NULL;
|
---|
| 671 | size_t string_descriptor_size = 0;
|
---|
[f8d43aa] | 672 | const int rc = usb_request_get_descriptor_alloc(pipe,
|
---|
[ad4562c2] | 673 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 674 | USB_DESCTYPE_STRING, 0, 0,
|
---|
[1a6a234] | 675 | (void **) &string_descriptor, &string_descriptor_size);
|
---|
| 676 | if (rc != EOK) {
|
---|
| 677 | return rc;
|
---|
| 678 | }
|
---|
| 679 | if (string_descriptor_size <= 2) {
|
---|
| 680 | free(string_descriptor);
|
---|
| 681 | return EEMPTY;
|
---|
| 682 | }
|
---|
[a6add7a] | 683 | /* Subtract first 2 bytes (length and descriptor type). */
|
---|
[1a6a234] | 684 | string_descriptor_size -= 2;
|
---|
| 685 |
|
---|
| 686 | /* Odd number of bytes - descriptor is broken? */
|
---|
| 687 | if ((string_descriptor_size % 2) != 0) {
|
---|
| 688 | /* FIXME: shall we return with error or silently ignore? */
|
---|
| 689 | free(string_descriptor);
|
---|
| 690 | return ESTALL;
|
---|
| 691 | }
|
---|
| 692 |
|
---|
[f8d43aa] | 693 | const size_t langs_count = string_descriptor_size / 2;
|
---|
| 694 | l18_win_locales_t *langs =
|
---|
| 695 | calloc(langs_count, sizeof(l18_win_locales_t));
|
---|
[1a6a234] | 696 | if (langs == NULL) {
|
---|
| 697 | free(string_descriptor);
|
---|
| 698 | return ENOMEM;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
[f8d43aa] | 701 | for (size_t i = 0; i < langs_count; i++) {
|
---|
[a6add7a] | 702 | /* Language code from the descriptor is in USB endianness. */
|
---|
[1a6a234] | 703 | /* FIXME: is this really correct? */
|
---|
[f8d43aa] | 704 | const uint16_t lang_code =
|
---|
| 705 | (string_descriptor[2 + 2 * i + 1] << 8)
|
---|
[1a6a234] | 706 | + string_descriptor[2 + 2 * i];
|
---|
| 707 | langs[i] = uint16_usb2host(lang_code);
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | free(string_descriptor);
|
---|
| 711 |
|
---|
| 712 | *languages_ptr = langs;
|
---|
| 713 | *languages_count =langs_count;
|
---|
| 714 |
|
---|
| 715 | return EOK;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[b84e114] | 718 | /** Get string (descriptor) from USB device.
|
---|
| 719 | *
|
---|
| 720 | * The string is returned in native encoding of the operating system.
|
---|
| 721 | * For HelenOS, that is UTF-8.
|
---|
| 722 | *
|
---|
| 723 | * @param[in] pipe Control endpoint pipe (session must be already started).
|
---|
[a6add7a] | 724 | * @param[in] index String index (in native endianness),
|
---|
[37b4794e] | 725 | * first index has number 1 (index from descriptors can be used directly).
|
---|
[a6add7a] | 726 | * @param[in] lang String language (in native endianness).
|
---|
[b84e114] | 727 | * @param[out] string_ptr Where to store allocated string in native encoding.
|
---|
| 728 | * @return Error code.
|
---|
| 729 | */
|
---|
[a372663] | 730 | int usb_request_get_string(usb_pipe_t *pipe,
|
---|
[b84e114] | 731 | size_t index, l18_win_locales_t lang, char **string_ptr)
|
---|
| 732 | {
|
---|
| 733 | if (string_ptr == NULL) {
|
---|
| 734 | return EBADMEM;
|
---|
| 735 | }
|
---|
[37b4794e] | 736 | /*
|
---|
| 737 | * Index is actually one byte value and zero index is used
|
---|
| 738 | * to retrieve list of supported languages.
|
---|
| 739 | */
|
---|
| 740 | if ((index < 1) || (index > 0xFF)) {
|
---|
[b84e114] | 741 | return ERANGE;
|
---|
| 742 | }
|
---|
| 743 | /* Language is actually two byte value. */
|
---|
[f8d43aa] | 744 | if (lang > L18N_WIN_LOCALE_MAX) {
|
---|
[b84e114] | 745 | return ERANGE;
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 | int rc;
|
---|
| 749 |
|
---|
| 750 | /* Prepare dynamically allocated variables. */
|
---|
| 751 | uint8_t *string = NULL;
|
---|
| 752 | wchar_t *string_chars = NULL;
|
---|
| 753 |
|
---|
| 754 | /* Get the actual descriptor. */
|
---|
| 755 | size_t string_size;
|
---|
| 756 | rc = usb_request_get_descriptor_alloc(pipe,
|
---|
[ad4562c2] | 757 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
|
---|
| 758 | USB_DESCTYPE_STRING, index, uint16_host2usb(lang),
|
---|
[b84e114] | 759 | (void **) &string, &string_size);
|
---|
| 760 | if (rc != EOK) {
|
---|
| 761 | goto leave;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 | if (string_size <= 2) {
|
---|
| 765 | rc = EEMPTY;
|
---|
| 766 | goto leave;
|
---|
| 767 | }
|
---|
[a6add7a] | 768 | /* Subtract first 2 bytes (length and descriptor type). */
|
---|
[b84e114] | 769 | string_size -= 2;
|
---|
| 770 |
|
---|
| 771 | /* Odd number of bytes - descriptor is broken? */
|
---|
| 772 | if ((string_size % 2) != 0) {
|
---|
| 773 | /* FIXME: shall we return with error or silently ignore? */
|
---|
| 774 | rc = ESTALL;
|
---|
| 775 | goto leave;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
[f8d43aa] | 778 | const size_t string_char_count = string_size / 2;
|
---|
[b84e114] | 779 | string_chars = malloc(sizeof(wchar_t) * (string_char_count + 1));
|
---|
| 780 | if (string_chars == NULL) {
|
---|
| 781 | rc = ENOMEM;
|
---|
| 782 | goto leave;
|
---|
| 783 | }
|
---|
| 784 |
|
---|
| 785 | /*
|
---|
| 786 | * Build a wide string.
|
---|
| 787 | * And do not forget to set NULL terminator (string descriptors
|
---|
| 788 | * do not have them).
|
---|
| 789 | */
|
---|
[f8d43aa] | 790 | for (size_t i = 0; i < string_char_count; i++) {
|
---|
| 791 | const uint16_t uni_char = (string[2 + 2 * i + 1] << 8)
|
---|
[b84e114] | 792 | + string[2 + 2 * i];
|
---|
| 793 | string_chars[i] = uni_char;
|
---|
| 794 | }
|
---|
| 795 | string_chars[string_char_count] = 0;
|
---|
| 796 |
|
---|
| 797 |
|
---|
| 798 | /* Convert to normal string. */
|
---|
| 799 | char *str = wstr_to_astr(string_chars);
|
---|
| 800 | if (str == NULL) {
|
---|
| 801 | rc = ENOMEM;
|
---|
| 802 | goto leave;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 | *string_ptr = str;
|
---|
| 806 | rc = EOK;
|
---|
| 807 |
|
---|
| 808 | leave:
|
---|
[f8d43aa] | 809 | free(string);
|
---|
| 810 | free(string_chars);
|
---|
[b84e114] | 811 |
|
---|
| 812 | return rc;
|
---|
| 813 | }
|
---|
| 814 |
|
---|
[c19329a] | 815 | /** Clear halt bit of an endpoint pipe (after pipe stall).
|
---|
| 816 | *
|
---|
| 817 | * @param pipe Control pipe.
|
---|
| 818 | * @param ep_index Endpoint index (in native endianness).
|
---|
| 819 | * @return Error code.
|
---|
| 820 | */
|
---|
| 821 | int usb_request_clear_endpoint_halt(usb_pipe_t *pipe, uint16_t ep_index)
|
---|
| 822 | {
|
---|
| 823 | return usb_request_clear_feature(pipe,
|
---|
| 824 | USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_ENDPOINT,
|
---|
[4ee5272] | 825 | uint16_host2usb(USB_FEATURE_ENDPOINT_HALT),
|
---|
[c19329a] | 826 | uint16_host2usb(ep_index));
|
---|
| 827 | }
|
---|
| 828 |
|
---|
[19387b61] | 829 | /** Clear halt bit of an endpoint pipe (after pipe stall).
|
---|
| 830 | *
|
---|
| 831 | * @param ctrl_pipe Control pipe.
|
---|
| 832 | * @param target_pipe Which pipe is halted and shall be cleared.
|
---|
| 833 | * @return Error code.
|
---|
| 834 | */
|
---|
| 835 | int usb_pipe_clear_halt(usb_pipe_t *ctrl_pipe, usb_pipe_t *target_pipe)
|
---|
| 836 | {
|
---|
| 837 | if ((ctrl_pipe == NULL) || (target_pipe == NULL)) {
|
---|
| 838 | return EINVAL;
|
---|
| 839 | }
|
---|
| 840 | return usb_request_clear_endpoint_halt(ctrl_pipe,
|
---|
| 841 | target_pipe->endpoint_no);
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 | /** Get endpoint status.
|
---|
| 845 | *
|
---|
| 846 | * @param[in] ctrl_pipe Control pipe.
|
---|
| 847 | * @param[in] pipe Of which pipe the status shall be received.
|
---|
| 848 | * @param[out] status Where to store pipe status (in native endianness).
|
---|
| 849 | * @return Error code.
|
---|
| 850 | */
|
---|
| 851 | int usb_request_get_endpoint_status(usb_pipe_t *ctrl_pipe, usb_pipe_t *pipe,
|
---|
| 852 | uint16_t *status)
|
---|
| 853 | {
|
---|
| 854 | uint16_t status_tmp;
|
---|
| 855 | uint16_t pipe_index = (uint16_t) pipe->endpoint_no;
|
---|
| 856 | int rc = usb_request_get_status(ctrl_pipe,
|
---|
| 857 | USB_REQUEST_RECIPIENT_ENDPOINT, uint16_host2usb(pipe_index),
|
---|
| 858 | &status_tmp);
|
---|
| 859 | if (rc != EOK) {
|
---|
| 860 | return rc;
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | if (status != NULL) {
|
---|
| 864 | *status = uint16_usb2host(status_tmp);
|
---|
| 865 | }
|
---|
| 866 |
|
---|
| 867 | return EOK;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
[0a37e14] | 870 | /**
|
---|
| 871 | * @}
|
---|
| 872 | */
|
---|