Changeset abe8ac5 in mainline for uspace/lib/usb/src/request.c


Ignore:
Timestamp:
2011-02-02T00:00:19Z (13 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0484d92
Parents:
787421c
Message:

Add most needed pipe wrappers for USB requests

Only requests so far needed were implemented now.

This commit might have bugs as no device driver actually uses these
requests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/usb/src/request.c

    r787421c rabe8ac5  
    3636#include <usb/devreq.h>
    3737#include <errno.h>
    38 
    39 /** Prepare setup packet.
    40  *
    41  * @param name Variable name with the setup packet.
    42  * @param p_direction Data transfer direction.
    43  * @param p_type Request type (standard/class/vendor)
    44  * @param p_recipient Recipient of the request.
    45  * @param p_request Request.
    46  * @param p_value wValue field of setup packet.
    47  * @param p_index wIndex field of setup packet.
    48  * @param p_length Length of extra data.
    49  */
    50 #define PREPARE_SETUP_PACKET(name, p_direction, p_type, p_recipient, \
    51     p_request, p_value, p_index, p_length) \
    52         usb_device_request_setup_packet_t name = { \
    53                 .request_type = \
    54                         ((p_direction) == USB_DIRECTION_IN ? 128 : 0) \
    55                         | ((p_type) << 5) \
    56                         | (p_recipient), \
    57                 .request = (p_request), \
    58                 { .value = (p_value) }, \
    59                 .index = (p_index), \
    60                 .length = (p_length) \
    61         }
    62 
    63 /** Prepare setup packet.
    64  *
    65  * @param name Variable name with the setup packet.
    66  * @param p_direction Data transfer direction.
    67  * @param p_type Request type (standard/class/vendor)
    68  * @param p_recipient Recipient of the request.
    69  * @param p_request Request.
    70  * @param p_value_low wValue field of setup packet (low byte).
    71  * @param p_value_high wValue field of setup packet (high byte).
    72  * @param p_index wIndex field of setup packet.
    73  * @param p_length Length of extra data.
    74  */
    75 #define PREPARE_SETUP_PACKET_LOHI(name, p_direction, p_type, p_recipient, \
    76     p_request, p_value_low, p_value_high, p_index, p_length) \
    77         PREPARE_SETUP_PACKET(name, p_direction, p_type, p_recipient, \
    78             p_request, (p_value_low) | ((p_value_high) << 8), \
    79             p_index, p_length)
    8038
    8139#define MAX_DATA_LENGTH ((size_t)(0xFFFF))
     
    198156}
    199157
     158/** Change address of connected device.
     159 * This function automatically updates the backing connection to point to
     160 * the new address.
     161 *
     162 * @see usb_drv_reserve_default_address
     163 * @see usb_drv_release_default_address
     164 * @see usb_drv_request_address
     165 * @see usb_drv_release_address
     166 * @see usb_drv_bind_address
     167 *
     168 * @param pipe Control endpoint pipe (session must be already started).
     169 * @param new_address New USB address to be set.
     170 * @return Error code.
     171 */
     172int usb_request_set_address(usb_endpoint_pipe_t *pipe,
     173    usb_address_t new_address)
     174{
     175        if ((new_address < 0) || (new_address >= USB11_ADDRESS_MAX)) {
     176                return EINVAL;
     177        }
     178
     179        int rc = usb_control_request_set(pipe,
     180            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     181            USB_DEVREQ_SET_ADDRESS,
     182            new_address, 0,
     183            NULL, 0);
     184
     185        if (rc != EOK) {
     186                return rc;
     187        }
     188
     189        assert(pipe->wire != NULL);
     190        /* TODO: prevent other from accessing wire now. */
     191        pipe->wire->address = new_address;
     192
     193        return EOK;
     194}
    200195
    201196/** Retrieve USB descriptor of a USB device.
     
    233228}
    234229
     230/** Retrieve standard device descriptor of a USB device.
     231 *
     232 * @param[in] pipe Control endpoint pipe (session must be already started).
     233 * @param[out] descriptor Storage for the device descriptor.
     234 * @return Error code.
     235 */
     236int usb_request_get_device_descriptor(usb_endpoint_pipe_t *pipe,
     237    usb_standard_device_descriptor_t *descriptor)
     238{
     239        if (descriptor == NULL) {
     240                return EBADMEM;
     241        }
     242
     243        size_t actually_transferred = 0;
     244        usb_standard_device_descriptor_t descriptor_tmp;
     245        int rc = usb_request_get_descriptor(pipe,
     246            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_DEVICE,
     247            0, 0,
     248            &descriptor_tmp, sizeof(descriptor_tmp),
     249            &actually_transferred);
     250
     251        if (rc != EOK) {
     252                return rc;
     253        }
     254
     255        /* Verify that all data has been transferred. */
     256        if (actually_transferred < sizeof(descriptor_tmp)) {
     257                return ELIMIT;
     258        }
     259
     260        /* Everything is okay, copy the descriptor. */
     261        memcpy(descriptor, &descriptor_tmp,
     262            sizeof(descriptor_tmp));
     263
     264        return EOK;
     265}
     266
     267/** Retrieve configuration descriptor of a USB device.
     268 *
     269 * The function does not retrieve additional data binded with configuration
     270 * descriptor (such as its interface and endpoint descriptors) - use
     271 * usb_request_get_full_configuration_descriptor() instead.
     272 *
     273 * @param[in] pipe Control endpoint pipe (session must be already started).
     274 * @param[in] index Descriptor index.
     275 * @param[out] descriptor Storage for the device descriptor.
     276 * @return Error code.
     277 */
     278int usb_request_get_bare_configuration_descriptor(usb_endpoint_pipe_t *pipe,
     279    int index, usb_standard_configuration_descriptor_t *descriptor)
     280{
     281        if (descriptor == NULL) {
     282                return EBADMEM;
     283        }
     284
     285        if ((index < 0) || (index > 0xFF)) {
     286                return ERANGE;
     287        }
     288
     289        size_t actually_transferred = 0;
     290        usb_standard_configuration_descriptor_t descriptor_tmp;
     291        int rc = usb_request_get_descriptor(pipe,
     292            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
     293            index, 0,
     294            &descriptor_tmp, sizeof(descriptor_tmp),
     295            &actually_transferred);
     296        if (rc != EOK) {
     297                return rc;
     298        }
     299
     300        /* Verify that all data has been transferred. */
     301        if (actually_transferred < sizeof(descriptor_tmp)) {
     302                return ELIMIT;
     303        }
     304
     305        /* Everything is okay, copy the descriptor. */
     306        memcpy(descriptor, &descriptor_tmp,
     307            sizeof(descriptor_tmp));
     308
     309        return EOK;
     310}
     311
     312/** Retrieve full configuration descriptor of a USB device.
     313 *
     314 * @warning The @p buffer might be touched (i.e. its contents changed)
     315 * even when error occurs.
     316 *
     317 * @param[in] pipe Control endpoint pipe (session must be already started).
     318 * @param[in] index Descriptor index.
     319 * @param[out] descriptor Storage for the device descriptor.
     320 * @param[in] descriptor_size Size of @p descriptor buffer.
     321 * @param[out] actual_size Number of bytes actually transferred.
     322 * @return Error code.
     323 */
     324int usb_request_get_full_configuration_descriptor(usb_endpoint_pipe_t *pipe,
     325    int index, void *descriptor, size_t descriptor_size, size_t *actual_size)
     326{
     327        if ((index < 0) || (index > 0xFF)) {
     328                return ERANGE;
     329        }
     330
     331        return usb_request_get_descriptor(pipe,
     332            USB_REQUEST_TYPE_STANDARD, USB_DESCTYPE_CONFIGURATION,
     333            index, 0,
     334            descriptor, descriptor_size, actual_size);
     335}
     336
     337/** Set configuration of USB device.
     338 *
     339 * @param pipe Control endpoint pipe (session must be already started).
     340 * @param configuration_value New configuration value.
     341 * @return Error code.
     342 */
     343int usb_request_set_configuration(usb_endpoint_pipe_t *pipe,
     344    uint8_t configuration_value)
     345{
     346        return usb_control_request_set(pipe,
     347            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     348            USB_DEVREQ_SET_CONFIGURATION, configuration_value, 0,
     349            NULL, 0);
     350}
     351
    235352/**
    236353 * @}
Note: See TracChangeset for help on using the changeset viewer.