Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1b7d991b authored by Mike Lockwood's avatar Mike Lockwood
Browse files

libusbhost: Add usb_device_send_control for sending raw commands on endpoint 0.



Change-Id: If883f2690c4031b9ba4d5cf943b5bf5c13193bce
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 8137a8d9
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -90,6 +90,15 @@ uint16_t usb_device_get_vendor_id(struct usb_device *device);
/* Returns the USB product ID from the device descriptor for the USB device */
uint16_t usb_device_get_product_id(struct usb_device *device);

/* Sends a control message to the specified device on endpoint zero */
int usb_device_send_control(struct usb_device *device,
                            int requestType,
                            int request,
                            int value,
                            int index,
                            int length,
                            void* buffer);

/* Returns a USB descriptor string for the given string ID.
 * Used to implement usb_device_get_manufacturer_name,
 * usb_device_get_product_name and usb_device_get_serial.
+34 −28
Original line number Diff line number Diff line
@@ -282,18 +282,17 @@ uint16_t usb_device_get_product_id(struct usb_device *device)
    return __le16_to_cpu(desc->idProduct);
}

char* usb_device_get_string(struct usb_device *device, int id)
int usb_device_send_control(struct usb_device *device,
                            int requestType,
                            int request,
                            int value,
                            int index,
                            int length,
                            void* buffer)
{
    char string[256];
    struct usbdevfs_ctrltransfer  ctrl;
    __u16 buffer[128];
    __u16 languages[128];
    int i, result;
    int languageCount = 0;

    string[0] = 0;

    // reading the string requires read/write permission
    // this usually requires read/write permission
    if (!device->writeable) {
        int fd = open(device->dev_name, O_RDWR);
        if (fd > 0) {
@@ -301,37 +300,44 @@ char* usb_device_get_string(struct usb_device *device, int id)
            device->fd = fd;
            device->writeable = 1;
        } else {
            return NULL;
            return -1;
        }
    }

    memset(languages, 0, sizeof(languages));
    memset(&ctrl, 0, sizeof(ctrl));
    ctrl.bRequestType = requestType;
    ctrl.bRequest = request;
    ctrl.wValue = value;
    ctrl.wIndex = index;
    ctrl.wLength = length;
    ctrl.data = buffer;
    return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
}

char* usb_device_get_string(struct usb_device *device, int id)
{
    char string[256];
    __u16 buffer[128];
    __u16 languages[128];
    int i, result;
    int languageCount = 0;

    string[0] = 0;
    memset(languages, 0, sizeof(languages));

    // read list of supported languages
    ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
    ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
    ctrl.wValue = (USB_DT_STRING << 8) | 0;
    ctrl.wIndex = 0;
    ctrl.wLength = sizeof(languages);
    ctrl.data = languages;

    result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
    result = usb_device_send_control(device,
            USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
            (USB_DT_STRING << 8) | 0, 0, sizeof(languages), languages);
    if (result > 0)
        languageCount = (result - 2) / 2;

    for (i = 1; i <= languageCount; i++) {
        memset(buffer, 0, sizeof(buffer));
        memset(&ctrl, 0, sizeof(ctrl));

        ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
        ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
        ctrl.wValue = (USB_DT_STRING << 8) | id;
        ctrl.wIndex = languages[i];
        ctrl.wLength = sizeof(buffer);
        ctrl.data = buffer;

        result = ioctl(device->fd, USBDEVFS_CONTROL, &ctrl);
        result = usb_device_send_control(device,
                USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR,
                (USB_DT_STRING << 8) | id, languages[i], sizeof(buffer), buffer);
        if (result > 0) {
            int i;
            // skip first word, and copy the rest to the string, changing shorts to bytes.