From 28774cb15d2443e325049ef410956f5635a1484b Mon Sep 17 00:00:00 2001
Message-Id: <28774cb15d2443e325049ef410956f5635a1484b.1334581530.git.minovotn@redhat.com>
From: Gerd Hoffmann <kraxel@redhat.com>
Date: Fri, 30 Mar 2012 12:35:31 +0200
Subject: [PATCH 1/6] usb: add USBDescriptor, use for device descriptors.

RH-Author: Gerd Hoffmann <kraxel@redhat.com>
Message-id: <1333110936-22700-2-git-send-email-kraxel@redhat.com>
Patchwork-id: 39046
O-Subject: [RHEL-6.3 qemu-kvm PATCH 1/6] usb: add USBDescriptor, use for device descriptors.
Bugzilla: 807878
RH-Acked-by: Hans de Goede <hdegoede@redhat.com>
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>

This patch adds a new type for the binary representation of usb
descriptors.  It is put into use for the descriptor generator code
where the struct replaces the hard-coded offsets.

upstream: http://patchwork.ozlabs.org/patch/149400/

[ rhel6: s/QEMU_PACKED/__attribute__((packed))/ ]

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/usb-desc.c |   37 +++++++++++++++++++------------------
 hw/usb-desc.h |   26 ++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 18 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/usb-desc.c |   43 ++++++++++++++++++++++---------------------
 hw/usb-desc.h |   26 ++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 21 deletions(-)

diff --git a/hw/usb-desc.c b/hw/usb-desc.c
index bc6858f..84482a2 100644
--- a/hw/usb-desc.c
+++ b/hw/usb-desc.c
@@ -18,32 +18,33 @@ int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
                     uint8_t *dest, size_t len)
 {
     uint8_t bLength = 0x12;
+    USBDescriptor *d = (void *)dest;
 
     if (len < bLength) {
         return -1;
     }
 
-    dest[0x00] = bLength;
-    dest[0x01] = USB_DT_DEVICE;
-
-    dest[0x02] = usb_lo(dev->bcdUSB);
-    dest[0x03] = usb_hi(dev->bcdUSB);
-    dest[0x04] = dev->bDeviceClass;
-    dest[0x05] = dev->bDeviceSubClass;
-    dest[0x06] = dev->bDeviceProtocol;
-    dest[0x07] = dev->bMaxPacketSize0;
-
-    dest[0x08] = usb_lo(id->idVendor);
-    dest[0x09] = usb_hi(id->idVendor);
-    dest[0x0a] = usb_lo(id->idProduct);
-    dest[0x0b] = usb_hi(id->idProduct);
-    dest[0x0c] = usb_lo(id->bcdDevice);
-    dest[0x0d] = usb_hi(id->bcdDevice);
-    dest[0x0e] = id->iManufacturer;
-    dest[0x0f] = id->iProduct;
-    dest[0x10] = id->iSerialNumber;
-
-    dest[0x11] = dev->bNumConfigurations;
+    d->bLength                     = bLength;
+    d->bDescriptorType             = USB_DT_DEVICE;
+
+    d->u.device.bcdUSB_lo          = usb_lo(dev->bcdUSB);
+    d->u.device.bcdUSB_hi          = usb_hi(dev->bcdUSB);
+    d->u.device.bDeviceClass       = dev->bDeviceClass;
+    d->u.device.bDeviceSubClass    = dev->bDeviceSubClass;
+    d->u.device.bDeviceProtocol    = dev->bDeviceProtocol;
+    d->u.device.bMaxPacketSize0    = dev->bMaxPacketSize0;
+
+    d->u.device.idVendor_lo        = usb_lo(id->idVendor);
+    d->u.device.idVendor_hi        = usb_hi(id->idVendor);
+    d->u.device.idProduct_lo       = usb_lo(id->idProduct);
+    d->u.device.idProduct_hi       = usb_hi(id->idProduct);
+    d->u.device.bcdDevice_lo       = usb_lo(id->bcdDevice);
+    d->u.device.bcdDevice_hi       = usb_hi(id->bcdDevice);
+    d->u.device.iManufacturer      = id->iManufacturer;
+    d->u.device.iProduct           = id->iProduct;
+    d->u.device.iSerialNumber      = id->iSerialNumber;
+
+    d->u.device.bNumConfigurations = dev->bNumConfigurations;
 
     return bLength;
 }
diff --git a/hw/usb-desc.h b/hw/usb-desc.h
index 9d7ed59..081866f 100644
--- a/hw/usb-desc.h
+++ b/hw/usb-desc.h
@@ -3,6 +3,32 @@
 
 #include <inttypes.h>
 
+/* binary representation */
+typedef struct USBDescriptor {
+    uint8_t                   bLength;
+    uint8_t                   bDescriptorType;
+    union {
+        struct {
+            uint8_t           bcdUSB_lo;
+            uint8_t           bcdUSB_hi;
+            uint8_t           bDeviceClass;
+            uint8_t           bDeviceSubClass;
+            uint8_t           bDeviceProtocol;
+            uint8_t           bMaxPacketSize0;
+            uint8_t           idVendor_lo;
+            uint8_t           idVendor_hi;
+            uint8_t           idProduct_lo;
+            uint8_t           idProduct_hi;
+            uint8_t           bcdDevice_lo;
+            uint8_t           bcdDevice_hi;
+            uint8_t           iManufacturer;
+            uint8_t           iProduct;
+            uint8_t           iSerialNumber;
+            uint8_t           bNumConfigurations;
+        } device;
+    } u;
+} __attribute__((packed)) USBDescriptor;
+
 struct USBDescID {
     uint16_t                  idVendor;
     uint16_t                  idProduct;
-- 
1.7.7.6

