Changeset 276aeda in mainline


Ignore:
Timestamp:
2011-01-10T11:22:19Z (14 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
263a9f1
Parents:
409dce5
Message:

Wrappers for standard device requests

Will add comments later.

Location:
uspace/lib/usb
Files:
3 edited

Legend:

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

    r409dce5 r276aeda  
    8585} __attribute__ ((packed)) usb_device_request_setup_packet_t;
    8686
     87
     88int usb_drv_req_get_status(int, usb_address_t, usb_request_recipient_t,
     89    uint16_t, uint16_t *);
     90int usb_drv_req_clear_feature(int, usb_address_t, usb_request_recipient_t,
     91    uint16_t, uint16_t);
     92int usb_drv_req_set_feature(int, usb_address_t, usb_request_recipient_t,
     93    uint16_t, uint16_t);
    8794int usb_drv_req_set_address(int, usb_address_t, usb_address_t);
     95int usb_drv_req_get_descriptor(int, usb_address_t, usb_request_type_t,
     96    uint8_t, uint8_t, uint16_t, void *, size_t, size_t *);
    8897int usb_drv_req_get_device_descriptor(int, usb_address_t,
    8998    usb_standard_device_descriptor_t *);
     
    92101int usb_drv_req_get_full_configuration_descriptor(int, usb_address_t, int,
    93102    void *, size_t, size_t *);
    94 int usb_drv_req_get_descriptor(int, usb_address_t, usb_request_type_t,
    95     uint8_t, uint8_t, uint16_t, void *, size_t, size_t *);
    96 
     103int usb_drv_req_set_descriptor(int, usb_address_t, uint8_t, uint8_t, uint16_t,
     104    void *, size_t);
     105int usb_drv_req_get_configuration(int, usb_address_t, uint8_t *);
     106int usb_drv_req_set_configuration(int, usb_address_t, uint8_t);
     107int usb_drv_req_get_interface(int, usb_address_t, uint16_t, uint8_t *);
     108int usb_drv_req_set_interface(int, usb_address_t, uint16_t, uint8_t);
    97109
    98110#endif
  • uspace/lib/usb/include/usb/usb.h

    r409dce5 r276aeda  
    6161        USB_REQUEST_TYPE_VENDOR = 2
    6262} usb_request_type_t;
     63
     64/** USB request recipient. */
     65typedef enum {
     66        USB_REQUEST_RECIPIENT_DEVICE = 0,
     67        USB_REQUEST_RECIPIENT_INTERFACE = 1,
     68        USB_REQUEST_RECIPIENT_ENDPOINT = 2
     69} usb_request_recipient_t;
    6370
    6471/** USB transaction outcome. */
  • uspace/lib/usb/src/usbdrvreq.c

    r409dce5 r276aeda  
    3535#include <usb/usbdrv.h>
    3636#include <errno.h>
     37
     38#define PREPARE_TARGET(name, target_address) \
     39        usb_target_t name = { \
     40                .address = target_address, \
     41                .endpoint = 0 \
     42        }
     43
     44#define PREPARE_SETUP_PACKET(name, p_direction, p_type, p_recipient, p_request, p_value, p_index, p_length) \
     45        usb_device_request_setup_packet_t setup_packet = { \
     46                .request_type = \
     47                        ((p_direction) == USB_DIRECTION_IN ? 128 : 0) \
     48                        | ((p_type) << 5) \
     49                        | (p_recipient), \
     50                .request = (p_request), \
     51                { .value = (p_value) }, \
     52                .index = (p_index), \
     53                .length = (p_length) \
     54        }
     55
     56#define PREPARE_SETUP_PACKET_LOHI(name, p_direction, p_type, p_recipient, p_request, p_value_low, p_value_high, p_index, p_length) \
     57        PREPARE_SETUP_PACKET(name, p_direction, p_type, p_recipient, \
     58            p_request, (p_value_low) | ((p_value_high) << 8), \
     59            p_index, p_length)
     60
     61/** Retrieve status of a USB device.
     62 *
     63 * @param hc_phone
     64 * @param address
     65 * @param recipient
     66 * @param recipient_index
     67 * @param status
     68 * @return
     69 */
     70int usb_drv_req_get_status(int hc_phone, usb_address_t address,
     71    usb_request_recipient_t recipient, uint16_t recipient_index,
     72    uint16_t *status)
     73{
     74        if (status == NULL) {
     75                return EBADMEM;
     76        }
     77
     78        PREPARE_TARGET(target, address);
     79
     80        PREPARE_SETUP_PACKET(setup_packet,
     81            USB_DIRECTION_IN, USB_REQUEST_TYPE_STANDARD,
     82            recipient, USB_DEVREQ_GET_STATUS, 0, recipient_index, 2);
     83
     84        size_t transfered;
     85        uint16_t tmp_status;
     86        int rc = usb_drv_psync_control_read(hc_phone, target,
     87            &setup_packet, sizeof(setup_packet), &tmp_status, 2, &transfered);
     88        if (rc != EOK) {
     89                return rc;
     90        }
     91        if (transfered != 2) {
     92                return ERANGE;
     93        }
     94
     95        *status = tmp_status;
     96
     97        return EOK;
     98}
     99
     100/** Clear or disable USB device feature.
     101 *
     102 * @param hc_phone
     103 * @param address
     104 * @param recipient
     105 * @param selector
     106 * @param index
     107 * @return
     108 */
     109int usb_drv_req_clear_feature(int hc_phone, usb_address_t address,
     110    usb_request_recipient_t recipient,
     111    uint16_t selector, uint16_t index)
     112{
     113        PREPARE_TARGET(target, address);
     114
     115        PREPARE_SETUP_PACKET(setup_packet,
     116            USB_DIRECTION_OUT, USB_REQUEST_TYPE_STANDARD,
     117            recipient, USB_DEVREQ_CLEAR_FEATURE, selector, index, 0);
     118
     119        int rc = usb_drv_psync_control_write(hc_phone, target,
     120            &setup_packet, sizeof(setup_packet), NULL, 0);
     121
     122        return rc;
     123}
     124
     125/** Set or enable USB device feature.
     126 *
     127 * @param hc_phone
     128 * @param address
     129 * @param recipient
     130 * @param selector
     131 * @param index
     132 * @return
     133 */
     134int usb_drv_req_set_feature(int hc_phone, usb_address_t address,
     135    usb_request_recipient_t recipient,
     136    uint16_t selector, uint16_t index)
     137{
     138        PREPARE_TARGET(target, address);
     139
     140        PREPARE_SETUP_PACKET(setup_packet,
     141            USB_DIRECTION_OUT, USB_REQUEST_TYPE_STANDARD,
     142            recipient, USB_DEVREQ_SET_FEATURE, selector, index, 0);
     143
     144        int rc = usb_drv_psync_control_write(hc_phone, target,
     145            &setup_packet, sizeof(setup_packet), NULL, 0);
     146
     147        return rc;
     148}
    37149
    38150/** Change address of connected device.
     
    238350}
    239351
     352/** Update existing descriptor of a USB device.
     353 *
     354 * @param hc_phone
     355 * @param address
     356 * @param descriptor_type
     357 * @param descriptor_index
     358 * @param language
     359 * @param descriptor
     360 * @param descriptor_size
     361 * @return
     362 */
     363int usb_drv_req_set_descriptor(int hc_phone, usb_address_t address,
     364    uint8_t descriptor_type, uint8_t descriptor_index,
     365    uint16_t language,
     366    void *descriptor, size_t descriptor_size)
     367{
     368        PREPARE_TARGET(target, address);
     369
     370        PREPARE_SETUP_PACKET_LOHI(setup_packet, USB_DIRECTION_OUT,
     371            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     372            USB_DEVREQ_SET_DESCRIPTOR, descriptor_index, descriptor_type,
     373            language, descriptor_size);
     374
     375        int rc = usb_drv_psync_control_write(hc_phone, target,
     376            &setup_packet, sizeof(setup_packet),
     377            descriptor, descriptor_size);
     378
     379        return rc;
     380}
     381
     382/** Determine current configuration value of USB device.
     383 *
     384 * @param hc_phone
     385 * @param address
     386 * @param configuration_value
     387 * @return
     388 */
     389int usb_drv_req_get_configuration(int hc_phone, usb_address_t address,
     390    uint8_t *configuration_value)
     391{
     392        if (configuration_value == NULL) {
     393                return EBADMEM;
     394        }
     395
     396        PREPARE_TARGET(target, address);
     397
     398        PREPARE_SETUP_PACKET(setup_packet, USB_DIRECTION_IN,
     399            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     400            USB_DEVREQ_GET_CONFIGURATION, 0, 0, 1);
     401
     402        uint8_t value;
     403        size_t transfered;
     404        int rc = usb_drv_psync_control_read(hc_phone, target,
     405            &setup_packet, sizeof(setup_packet), &value, 1, &transfered);
     406
     407        if (rc != EOK) {
     408                return rc;
     409        }
     410
     411        if (transfered != 1) {
     412                return ERANGE;
     413        }
     414
     415        *configuration_value = value;
     416
     417        return EOK;
     418}
     419
     420/** Set configuration of USB device.
     421 *
     422 * @param hc_phone
     423 * @param address
     424 * @param configuration_value
     425 * @return
     426 */
     427int usb_drv_req_set_configuration(int hc_phone, usb_address_t address,
     428    uint8_t configuration_value)
     429{
     430        PREPARE_TARGET(target, address);
     431
     432        PREPARE_SETUP_PACKET_LOHI(setup_packet, USB_DIRECTION_OUT,
     433            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_DEVICE,
     434            USB_DEVREQ_SET_CONFIGURATION, configuration_value, 0,
     435            0, 0);
     436
     437        int rc = usb_drv_psync_control_write(hc_phone, target,
     438            &setup_packet, sizeof(setup_packet), NULL, 0);
     439
     440        return rc;
     441}
     442
     443/** Determine alternate setting of USB device interface.
     444 *
     445 * @param hc_phone
     446 * @param address
     447 * @param interface_index
     448 * @param alternate_setting
     449 * @return
     450 */
     451int usb_drv_req_get_interface(int hc_phone, usb_address_t address,
     452    uint16_t interface_index, uint8_t *alternate_setting)
     453{
     454        if (alternate_setting == NULL) {
     455                return EBADMEM;
     456        }
     457
     458        PREPARE_TARGET(target, address);
     459
     460        PREPARE_SETUP_PACKET(setup_packet, USB_DIRECTION_IN,
     461            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
     462            USB_DEVREQ_GET_INTERFACE, 0, interface_index, 1);
     463
     464        uint8_t alternate;
     465        size_t transfered;
     466        int rc = usb_drv_psync_control_read(hc_phone, target,
     467            &setup_packet, sizeof(setup_packet), &alternate, 1, &transfered);
     468
     469        if (rc != EOK) {
     470                return rc;
     471        }
     472
     473        if (transfered != 1) {
     474                return ERANGE;
     475        }
     476
     477        *alternate_setting = alternate;
     478
     479        return EOK;
     480}
     481
     482/** Select an alternate setting of USB device interface.
     483 *
     484 * @param hc_phone
     485 * @param address
     486 * @param interface_index
     487 * @param alternate_setting
     488 * @return
     489 */
     490int usb_drv_req_set_interface(int hc_phone, usb_address_t address,
     491    uint16_t interface_index, uint8_t alternate_setting)
     492{
     493        PREPARE_TARGET(target, address);
     494
     495        PREPARE_SETUP_PACKET_LOHI(setup_packet, USB_DIRECTION_OUT,
     496            USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
     497            USB_DEVREQ_SET_INTERFACE, alternate_setting, 0,
     498            0, 0);
     499
     500        int rc = usb_drv_psync_control_write(hc_phone, target,
     501            &setup_packet, sizeof(setup_packet), NULL, 0);
     502
     503        return rc;
     504}
    240505
    241506/**
Note: See TracChangeset for help on using the changeset viewer.