Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset a4a8cca in mainline


Ignore:
Timestamp:
2011-02-01T22:04:58Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial
Children:
23c7f4d
Parents:
bbc7d83e
Message:

Add generic wrappers for standard control requests

Location:
uspace/lib/usb
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usb/include/usb/request.h

    rbbc7d83e ra4a8cca  
    4040#include <usb/pipes.h>
    4141
     42int usb_control_request_set(usb_endpoint_pipe_t *,
     43    usb_request_type_t, usb_request_recipient_t, uint8_t,
     44    uint16_t, uint16_t, void *, size_t);
     45
     46int usb_control_request_get(usb_endpoint_pipe_t *,
     47    usb_request_type_t, usb_request_recipient_t, uint8_t,
     48    uint16_t, uint16_t, void *, size_t, size_t *);
     49
    4250int usb_request_get_descriptor(usb_endpoint_pipe_t *, usb_request_type_t,
    4351    uint8_t, uint8_t, uint16_t, void *, size_t, size_t *);
  • uspace/lib/usb/src/request.c

    rbbc7d83e ra4a8cca  
    7979            p_index, p_length)
    8080
     81#define MAX_DATA_LENGTH (~((uint16_t)0))
     82
     83/** Generic wrapper for SET requests using standard control request format.
     84 *
     85 * @see usb_endpoint_pipe_control_write
     86 *
     87 * @param pipe Pipe used for the communication.
     88 * @param request_type Request type (standard/class/vendor).
     89 * @param recipient Request recipient (e.g. device or endpoint).
     90 * @param request Actual request (e.g. GET_DESCRIPTOR).
     91 * @param value Value of @c wValue field of setup packet
     92 *      (must be in USB endianness).
     93 * @param index Value of @c wIndex field of setup packet
     94 *      (must be in USB endianness).
     95 * @param data Data to be sent during DATA stage
     96 *      (expected to be in USB endianness).
     97 * @param data_size Size of the @p data buffer (in native endianness).
     98 * @return Error code.
     99 * @retval EBADMEM @p pipe is NULL.
     100 * @retval EBADMEM @p data is NULL and @p data_size is not zero.
     101 * @retval ERANGE Data buffer too large.
     102 */
     103int usb_control_request_set(usb_endpoint_pipe_t *pipe,
     104    usb_request_type_t request_type, usb_request_recipient_t recipient,
     105    uint8_t request,
     106    uint16_t value, uint16_t index,
     107    void *data, size_t data_size)
     108{
     109        if (pipe == NULL) {
     110                return EBADMEM;
     111        }
     112
     113        if (data_size > MAX_DATA_LENGTH) {
     114                return ERANGE;
     115        }
     116
     117        if ((data_size > 0) && (data == NULL)) {
     118                return EBADMEM;
     119        }
     120
     121        /*
     122         * TODO: check that @p request_type and @p recipient are
     123         * within ranges.
     124         */
     125
     126        usb_device_request_setup_packet_t setup_packet;
     127        setup_packet.request_type = (request_type << 5) | recipient;
     128        setup_packet.request = request;
     129        setup_packet.value = value;
     130        setup_packet.index = index;
     131        setup_packet.length = (uint16_t) data_size;
     132
     133        int rc = usb_endpoint_pipe_control_write(pipe,
     134            &setup_packet, sizeof(setup_packet),
     135            data, data_size);
     136
     137        return rc;
     138}
     139
     140 /** Generic wrapper for GET requests using standard control request format.
     141  *
     142  * @see usb_endpoint_pipe_control_read
     143  *
     144  * @param pipe Pipe used for the communication.
     145  * @param request_type Request type (standard/class/vendor).
     146  * @param recipient Request recipient (e.g. device or endpoint).
     147  * @param request Actual request (e.g. GET_DESCRIPTOR).
     148  * @param value Value of @c wValue field of setup packet
     149  *     (must be in USB endianness).
     150  * @param index Value of @c wIndex field of setup packet
     151  *     (must be in USB endianness).
     152  * @param data Buffer where to store data accepted during the DATA stage.
     153  *     (they will come in USB endianess).
     154  * @param data_size Size of the @p data buffer
     155  *     (in native endianness).
     156  * @param actual_data_size Actual size of transfered data
     157  *     (in native endianness).
     158  * @return Error code.
     159  * @retval EBADMEM @p pipe is NULL.
     160  * @retval EBADMEM @p data is NULL and @p data_size is not zero.
     161  * @retval ERANGE Data buffer too large.
     162  */
     163int usb_control_request_get(usb_endpoint_pipe_t *pipe,
     164    usb_request_type_t request_type, usb_request_recipient_t recipient,
     165    uint8_t request,
     166    uint16_t value, uint16_t index,
     167    void *data, size_t data_size, size_t *actual_data_size)
     168{
     169        if (pipe == NULL) {
     170                return EBADMEM;
     171        }
     172
     173        if (data_size > MAX_DATA_LENGTH) {
     174                return ERANGE;
     175        }
     176
     177        if ((data_size > 0) && (data == NULL)) {
     178                return EBADMEM;
     179        }
     180
     181        /*
     182         * TODO: check that @p request_type and @p recipient are
     183         * within ranges.
     184         */
     185
     186        usb_device_request_setup_packet_t setup_packet;
     187        setup_packet.request_type = 128 | (request_type << 5) | recipient;
     188        setup_packet.request = request;
     189        setup_packet.value = value;
     190        setup_packet.index = index;
     191        setup_packet.length = (uint16_t) data_size;
     192
     193        int rc = usb_endpoint_pipe_control_read(pipe,
     194            &setup_packet, sizeof(setup_packet),
     195            data, data_size, actual_data_size);
     196
     197        return rc;
     198}
     199
    81200
    82201/** Retrieve USB descriptor of a USB device.
Note: See TracChangeset for help on using the changeset viewer.