xref: /openbmc/qemu/hw/usb/desc.h (revision 0a263db17a59a04b8f22540b284919956751e4f8)
1 #ifndef QEMU_HW_USB_DESC_H
2 #define QEMU_HW_USB_DESC_H
3 
4 #include <inttypes.h>
5 
6 /* binary representation */
7 typedef struct USBDescriptor {
8     uint8_t                   bLength;
9     uint8_t                   bDescriptorType;
10     union {
11         struct {
12             uint8_t           bcdUSB_lo;
13             uint8_t           bcdUSB_hi;
14             uint8_t           bDeviceClass;
15             uint8_t           bDeviceSubClass;
16             uint8_t           bDeviceProtocol;
17             uint8_t           bMaxPacketSize0;
18             uint8_t           idVendor_lo;
19             uint8_t           idVendor_hi;
20             uint8_t           idProduct_lo;
21             uint8_t           idProduct_hi;
22             uint8_t           bcdDevice_lo;
23             uint8_t           bcdDevice_hi;
24             uint8_t           iManufacturer;
25             uint8_t           iProduct;
26             uint8_t           iSerialNumber;
27             uint8_t           bNumConfigurations;
28         } device;
29         struct {
30             uint8_t           bcdUSB_lo;
31             uint8_t           bcdUSB_hi;
32             uint8_t           bDeviceClass;
33             uint8_t           bDeviceSubClass;
34             uint8_t           bDeviceProtocol;
35             uint8_t           bMaxPacketSize0;
36             uint8_t           bNumConfigurations;
37             uint8_t           bReserved;
38         } device_qualifier;
39         struct {
40             uint8_t           wTotalLength_lo;
41             uint8_t           wTotalLength_hi;
42             uint8_t           bNumInterfaces;
43             uint8_t           bConfigurationValue;
44             uint8_t           iConfiguration;
45             uint8_t           bmAttributes;
46             uint8_t           bMaxPower;
47         } config;
48     } u;
49 } QEMU_PACKED USBDescriptor;
50 
51 struct USBDescID {
52     uint16_t                  idVendor;
53     uint16_t                  idProduct;
54     uint16_t                  bcdDevice;
55     uint8_t                   iManufacturer;
56     uint8_t                   iProduct;
57     uint8_t                   iSerialNumber;
58 };
59 
60 struct USBDescDevice {
61     uint16_t                  bcdUSB;
62     uint8_t                   bDeviceClass;
63     uint8_t                   bDeviceSubClass;
64     uint8_t                   bDeviceProtocol;
65     uint8_t                   bMaxPacketSize0;
66     uint8_t                   bNumConfigurations;
67 
68     const USBDescConfig       *confs;
69 };
70 
71 struct USBDescConfig {
72     uint8_t                   bNumInterfaces;
73     uint8_t                   bConfigurationValue;
74     uint8_t                   iConfiguration;
75     uint8_t                   bmAttributes;
76     uint8_t                   bMaxPower;
77 
78     /* grouped interfaces */
79     uint8_t                   nif_groups;
80     const USBDescIfaceAssoc   *if_groups;
81 
82     /* "normal" interfaces */
83     uint8_t                   nif;
84     const USBDescIface        *ifs;
85 };
86 
87 /* conceptually an Interface Association Descriptor, and releated interfaces */
88 struct USBDescIfaceAssoc {
89     uint8_t                   bFirstInterface;
90     uint8_t                   bInterfaceCount;
91     uint8_t                   bFunctionClass;
92     uint8_t                   bFunctionSubClass;
93     uint8_t                   bFunctionProtocol;
94     uint8_t                   iFunction;
95 
96     uint8_t                   nif;
97     const USBDescIface        *ifs;
98 };
99 
100 struct USBDescIface {
101     uint8_t                   bInterfaceNumber;
102     uint8_t                   bAlternateSetting;
103     uint8_t                   bNumEndpoints;
104     uint8_t                   bInterfaceClass;
105     uint8_t                   bInterfaceSubClass;
106     uint8_t                   bInterfaceProtocol;
107     uint8_t                   iInterface;
108 
109     uint8_t                   ndesc;
110     USBDescOther              *descs;
111     USBDescEndpoint           *eps;
112 };
113 
114 struct USBDescEndpoint {
115     uint8_t                   bEndpointAddress;
116     uint8_t                   bmAttributes;
117     uint16_t                  wMaxPacketSize;
118     uint8_t                   bInterval;
119     uint8_t                   bRefresh;
120     uint8_t                   bSynchAddress;
121 
122     uint8_t                   is_audio; /* has bRefresh + bSynchAddress */
123     uint8_t                   *extra;
124 };
125 
126 struct USBDescOther {
127     uint8_t                   length;
128     const uint8_t             *data;
129 };
130 
131 typedef const char *USBDescStrings[256];
132 
133 struct USBDesc {
134     USBDescID                 id;
135     const USBDescDevice       *full;
136     const USBDescDevice       *high;
137     const char* const         *str;
138 };
139 
140 /* generate usb packages from structs */
141 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev,
142                     uint8_t *dest, size_t len);
143 int usb_desc_device_qualifier(const USBDescDevice *dev,
144                               uint8_t *dest, size_t len);
145 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len);
146 int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest,
147                          size_t len);
148 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len);
149 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len);
150 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len);
151 
152 /* control message emulation helpers */
153 void usb_desc_init(USBDevice *dev);
154 void usb_desc_attach(USBDevice *dev);
155 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str);
156 const char *usb_desc_get_string(USBDevice *dev, uint8_t index);
157 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len);
158 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len);
159 int usb_desc_handle_control(USBDevice *dev, USBPacket *p,
160         int request, int value, int index, int length, uint8_t *data);
161 
162 #endif /* QEMU_HW_USB_DESC_H */
163