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

Changeset 2c5cefa in mainline


Ignore:
Timestamp:
2010-12-09T14:25:39Z (11 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master
Children:
99ea659c
Parents:
f5e39475
Message:

Add retrieval of configuration descriptor

The `usbinfo' app displays configuration descriptor as well.

Location:
uspace
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/usbinfo/dump.c

    rf5e39475 r2c5cefa  
    4747
    4848#define INDENT "  "
     49#define BYTES_PER_LINE 12
    4950
    5051#define BCD_INT(a) (((unsigned int)(a)) / 256)
     
    5354#define BCD_FMT "%x.%x"
    5455#define BCD_ARGS(a) BCD_INT((a)), BCD_FRAC((a))
     56
     57void dump_buffer(const char *msg, const uint8_t *buffer, size_t length)
     58{
     59        printf("%s\n", msg);
     60
     61        size_t i;
     62        for (i = 0; i < length; i++) {
     63                printf("  0x%02X", buffer[i]);
     64                if (((i > 0) && (((i+1) % BYTES_PER_LINE) == 0))
     65                    || (i + 1 == length)) {
     66                        printf("\n");
     67                }
     68        }
     69}
    5570
    5671void dump_standard_device_descriptor(usb_standard_device_descriptor_t *d)
     
    7590}
    7691
     92void dump_standard_configuration_descriptor(
     93    int index, usb_standard_configuration_descriptor_t *d)
     94{
     95        bool self_powered = d->attributes & 64;
     96        bool remote_wakeup = d->attributes & 32;
     97       
     98        printf("Standard configuration descriptor #%d\n", index);
     99        printf(INDENT "bLength = %d\n", d->length);
     100        printf(INDENT "bDescriptorType = 0x%02x\n", d->descriptor_type);
     101        printf(INDENT "wTotalLength = %d\n", d->total_length);
     102        printf(INDENT "bNumInterfaces = %d\n", d->interface_count);
     103        printf(INDENT "bConfigurationValue = %d\n", d->configuration_number);
     104        printf(INDENT "iConfiguration = %d\n", d->str_configuration);
     105        printf(INDENT "bmAttributes = %d [%s%s%s]\n", d->attributes,
     106            self_powered ? "self-powered" : "",
     107            (self_powered & remote_wakeup) ? ", " : "",
     108            remote_wakeup ? "remote-wakeup" : "");
     109        printf(INDENT "MaxPower = %d (%dmA)\n", d->max_power,
     110            2 * d->max_power);
     111        // printf(INDENT " = %d\n", d->);
     112}
     113
     114
    77115/** @}
    78116 */
  • uspace/app/usbinfo/main.c

    rf5e39475 r2c5cefa  
    107107         */
    108108        usb_standard_device_descriptor_t device_descriptor;
    109         usb_dprintf("usbinfo", 1,
     109        usb_dprintf(NAME, 1,
    110110            "usb_drv_req_get_device_descriptor(%d, %d, %p)\n",
    111111            hc_phone, (int) address, &device_descriptor);
     
    121121        dump_standard_device_descriptor(&device_descriptor);
    122122
     123        /*
     124         * Get first configuration descriptor and dump it.
     125         */
     126        usb_standard_configuration_descriptor_t config_descriptor;
     127        int config_index = 0;
     128        usb_dprintf(NAME, 1,
     129            "usb_drv_req_get_bare_configuration_descriptor(%d, %d, %d, %p)\n",
     130            hc_phone, (int) address, config_index, &config_descriptor);
     131
     132        rc = usb_drv_req_get_bare_configuration_descriptor(hc_phone, address,
     133            config_index, &config_descriptor );
     134        if (rc != EOK) {
     135                fprintf(stderr,
     136                    NAME ": failed to fetch standard configuration descriptor: %s.\n",
     137                    str_error(rc));
     138                return rc;
     139        }
     140        dump_standard_configuration_descriptor(config_index,
     141            &config_descriptor);
     142
     143        void *full_config_descriptor = malloc(config_descriptor.total_length);
     144        usb_dprintf(NAME, 1,
     145            "usb_drv_req_get_full_configuration_descriptor(%d, %d, %d, %p, %zu)\n",
     146            hc_phone, (int) address, config_index,
     147            full_config_descriptor, config_descriptor.total_length);
     148
     149        rc = usb_drv_req_get_full_configuration_descriptor(hc_phone, address,
     150            config_index,
     151            full_config_descriptor, config_descriptor.total_length, NULL);
     152        if (rc != EOK) {
     153                fprintf(stderr,
     154                    NAME ": failed to fetch full configuration descriptor: %s.\n",
     155                    str_error(rc));
     156                return rc;
     157        }
     158        dump_buffer("Full configuration descriptor:",
     159            full_config_descriptor, config_descriptor.total_length);
    123160
    124161        return EOK;
  • uspace/app/usbinfo/usbinfo.h

    rf5e39475 r2c5cefa  
    4343#define NAME "usbinfo"
    4444
     45void dump_buffer(const char *, const uint8_t *, size_t);
    4546void dump_standard_device_descriptor(usb_standard_device_descriptor_t *);
     47void dump_standard_configuration_descriptor(int,
     48    usb_standard_configuration_descriptor_t *);
    4649
    4750#endif
  • uspace/lib/usb/include/usb/devreq.h

    rf5e39475 r2c5cefa  
    3838#include <ipc/ipc.h>
    3939#include <async.h>
     40#include <usb/usb.h>
     41#include <usb/descriptor.h>
    4042
    4143/** Standard device request. */
     
    8385} __attribute__ ((packed)) usb_device_request_setup_packet_t;
    8486
     87int usb_drv_req_set_address(int, usb_address_t, usb_address_t);
     88int usb_drv_req_get_device_descriptor(int, usb_address_t,
     89    usb_standard_device_descriptor_t *);
     90int usb_drv_req_get_bare_configuration_descriptor(int, usb_address_t, int,
     91    usb_standard_configuration_descriptor_t *);
     92int usb_drv_req_get_full_configuration_descriptor(int, usb_address_t, int,
     93    void *, size_t, size_t *);
     94
     95
    8596#endif
    8697/**
  • uspace/lib/usb/include/usb/usbdrv.h

    rf5e39475 r2c5cefa  
    9595
    9696
    97 int usb_drv_req_set_address(int, usb_address_t, usb_address_t);
    98 int usb_drv_req_get_device_descriptor(int, usb_address_t,
    99     usb_standard_device_descriptor_t *);
    100 
    10197#endif
    10298/**
  • uspace/lib/usb/src/usbdrvreq.c

    rf5e39475 r2c5cefa  
    130130
    131131
     132/** Retrieve configuration descriptor of connected USB device.
     133 *
     134 * The function does not retrieve additional data binded with configuration
     135 * descriptor (such as its interface and endpoint descriptors) - use
     136 * usb_drv_req_get_full_configuration_descriptor() instead.
     137 *
     138 * @param[in] phone Open phone to HC driver.
     139 * @param[in] address Device USB address.
     140 * @param[in] index Configuration descriptor index.
     141 * @param[out] descriptor Storage for the configuration descriptor.
     142 * @return Error code.
     143 * @retval EBADMEM @p descriptor is NULL.
     144 */
     145int usb_drv_req_get_bare_configuration_descriptor(int phone,
     146    usb_address_t address, int index,
     147    usb_standard_configuration_descriptor_t *descriptor)
     148{
     149        if (descriptor == NULL) {
     150                return EBADMEM;
     151        }
     152
     153        /* Prepare the target. */
     154        usb_target_t target = {
     155                .address = address,
     156                .endpoint = 0
     157        };
     158
     159        /* Prepare the setup packet. */
     160        usb_device_request_setup_packet_t setup_packet = {
     161                .request_type = 128,
     162                .request = USB_DEVREQ_GET_DESCRIPTOR,
     163                .index = 0,
     164                .length = sizeof(usb_standard_device_descriptor_t)
     165        };
     166        setup_packet.value_high = USB_DESCTYPE_CONFIGURATION;
     167        setup_packet.value_low = index;
     168
     169        /* Prepare local descriptor. */
     170        size_t actually_transferred = 0;
     171        usb_standard_configuration_descriptor_t descriptor_tmp;
     172
     173        /* Perform the control read transaction. */
     174        int rc = usb_drv_psync_control_read(phone, target,
     175            &setup_packet, sizeof(setup_packet),
     176            &descriptor_tmp, sizeof(descriptor_tmp), &actually_transferred);
     177
     178        if (rc != EOK) {
     179                return rc;
     180        }
     181
     182        /* Verify that all data has been transferred. */
     183        if (actually_transferred < sizeof(descriptor_tmp)) {
     184                return ELIMIT;
     185        }
     186
     187        /* Everything is okay, copy the descriptor. */
     188        memcpy(descriptor, &descriptor_tmp,
     189            sizeof(descriptor_tmp));
     190
     191        return EOK;
     192}
     193
     194/** Retrieve full configuration descriptor of connected USB device.
     195 *
     196 * @warning The @p buffer might be touched (i.e. its contents changed)
     197 * even when error occurres.
     198 *
     199 * @param[in] phone Open phone to HC driver.
     200 * @param[in] address Device USB address.
     201 * @param[in] index Configuration descriptor index.
     202 * @param[out] buffer Buffer for the whole configuration descriptor.
     203 * @param[in] buffer_size Size of the prepared @p buffer.
     204 * @param[out] actual_buffer_size Bytes actually transfered.
     205 * @return Error code.
     206 * @retval EBADMEM @p descriptor is NULL.
     207 */
     208int usb_drv_req_get_full_configuration_descriptor(int phone,
     209    usb_address_t address, int index,
     210    void *buffer, size_t buffer_size, size_t *actual_buffer_size)
     211{
     212        if (buffer == NULL) {
     213                return EBADMEM;
     214        }
     215
     216        /* Prepare the target. */
     217        usb_target_t target = {
     218                .address = address,
     219                .endpoint = 0
     220        };
     221
     222        /* Prepare the setup packet. */
     223        usb_device_request_setup_packet_t setup_packet = {
     224                .request_type = 128,
     225                .request = USB_DEVREQ_GET_DESCRIPTOR,
     226                .index = 0,
     227                .length = sizeof(usb_standard_device_descriptor_t)
     228        };
     229        setup_packet.value_high = USB_DESCTYPE_CONFIGURATION;
     230        setup_packet.value_low = index;
     231
     232        /* Perform the control read transaction. */
     233        int rc = usb_drv_psync_control_read(phone, target,
     234            &setup_packet, sizeof(setup_packet),
     235            buffer, buffer_size, actual_buffer_size);
     236
     237        return rc;
     238}
     239
    132240
    133241/**
Note: See TracChangeset for help on using the changeset viewer.