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

Changeset 9501cced in mainline


Ignore:
Timestamp:
2010-12-08T15:16:52Z (11 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master
Children:
90fb679
Parents:
947d788
Message:

Add function for retrieving USB device descriptor

Location:
uspace/lib/usb
Files:
2 edited

Legend:

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

    r947d788 r9501cced  
    3838#include <usb/usb.h>
    3939#include <driver.h>
     40#include <usb/devreq.h>
     41#include <usb/descriptor.h>
    4042
    4143int usb_drv_hc_connect(device_t *, unsigned int);
     
    7274
    7375int usb_drv_req_set_address(int, usb_address_t, usb_address_t);
     76int usb_drv_req_get_device_descriptor(int, usb_address_t,
     77    usb_standard_device_descriptor_t *);
    7478
    7579#endif
  • uspace/lib/usb/src/usbdrvreq.c

    r947d788 r9501cced  
    3434 */
    3535#include <usb/usbdrv.h>
    36 #include <usb/devreq.h>
    3736#include <errno.h>
    3837
     
    9594}
    9695
     96/** Retrieve device descriptor of connected USB device.
     97 *
     98 * @param[in] phone Open phone to HC driver.
     99 * @param[in] address Device USB address.
     100 * @param[out] descriptor Storage for the device descriptor.
     101 * @return Error code.
     102 * @retval EBADMEM @p descriptor is NULL.
     103 */
     104int usb_drv_req_get_device_descriptor(int phone, usb_address_t address,
     105    usb_standard_device_descriptor_t *descriptor)
     106{
     107        if (descriptor == NULL) {
     108                return EBADMEM;
     109        }
     110
     111        /* Prepare the target. */
     112        usb_target_t target = {
     113                .address = address,
     114                .endpoint = 0
     115        };
     116
     117        /* Prepare the setup packet. */
     118        usb_device_request_setup_packet_t setup_packet = {
     119                .request_type = 0,
     120                .request = USB_DEVREQ_GET_DESCRIPTOR,
     121                .index = 0,
     122                .length = sizeof(usb_standard_device_descriptor_t)
     123        };
     124        setup_packet.value_high = USB_DESCTYPE_DEVICE;
     125        setup_packet.value_low = 0;
     126
     127        usb_handle_t handle;
     128        int rc;
     129
     130        /* Start the control read transfer. */
     131        rc = usb_drv_async_control_read_setup(phone, target,
     132            &setup_packet, sizeof(setup_packet), &handle);
     133        if (rc != EOK) {
     134                return rc;
     135        }
     136        rc = usb_drv_async_wait_for(handle);
     137        if (rc != EOK) {
     138                return rc;
     139        }
     140
     141        /* Retrieve the descriptor. */
     142        size_t actually_transferred = 0;
     143        usb_standard_device_descriptor_t descriptor_tmp;
     144        rc = usb_drv_async_control_read_data(phone, target,
     145            &descriptor_tmp, sizeof(usb_standard_device_descriptor_t),
     146            &actually_transferred, &handle);
     147        if (rc != EOK) {
     148                return rc;
     149        }
     150        rc = usb_drv_async_wait_for(handle);
     151        if (rc != EOK) {
     152                return rc;
     153        }
     154
     155        /* Finish the control read transfer. */
     156        rc = usb_drv_async_control_read_status(phone, target, &handle);
     157        if (rc != EOK) {
     158                return rc;
     159        }
     160        rc = usb_drv_async_wait_for(handle);
     161        if (rc != EOK) {
     162                return rc;
     163        }
     164
     165        if (actually_transferred < sizeof(usb_standard_device_descriptor_t)) {
     166                return ELIMIT;
     167        }
     168
     169        /*
     170         * Everything is okay, copy the descriptor.
     171         */
     172        memcpy(descriptor, &descriptor_tmp,
     173            sizeof(usb_standard_device_descriptor_t));
     174
     175        return EOK;
     176}
     177
     178
     179
    97180/**
    98181 * @}
Note: See TracChangeset for help on using the changeset viewer.