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 struct { 49 uint8_t bInterfaceNumber; 50 uint8_t bAlternateSetting; 51 uint8_t bNumEndpoints; 52 uint8_t bInterfaceClass; 53 uint8_t bInterfaceSubClass; 54 uint8_t bInterfaceProtocol; 55 uint8_t iInterface; 56 } interface; 57 } u; 58 } QEMU_PACKED USBDescriptor; 59 60 struct USBDescID { 61 uint16_t idVendor; 62 uint16_t idProduct; 63 uint16_t bcdDevice; 64 uint8_t iManufacturer; 65 uint8_t iProduct; 66 uint8_t iSerialNumber; 67 }; 68 69 struct USBDescDevice { 70 uint16_t bcdUSB; 71 uint8_t bDeviceClass; 72 uint8_t bDeviceSubClass; 73 uint8_t bDeviceProtocol; 74 uint8_t bMaxPacketSize0; 75 uint8_t bNumConfigurations; 76 77 const USBDescConfig *confs; 78 }; 79 80 struct USBDescConfig { 81 uint8_t bNumInterfaces; 82 uint8_t bConfigurationValue; 83 uint8_t iConfiguration; 84 uint8_t bmAttributes; 85 uint8_t bMaxPower; 86 87 /* grouped interfaces */ 88 uint8_t nif_groups; 89 const USBDescIfaceAssoc *if_groups; 90 91 /* "normal" interfaces */ 92 uint8_t nif; 93 const USBDescIface *ifs; 94 }; 95 96 /* conceptually an Interface Association Descriptor, and releated interfaces */ 97 struct USBDescIfaceAssoc { 98 uint8_t bFirstInterface; 99 uint8_t bInterfaceCount; 100 uint8_t bFunctionClass; 101 uint8_t bFunctionSubClass; 102 uint8_t bFunctionProtocol; 103 uint8_t iFunction; 104 105 uint8_t nif; 106 const USBDescIface *ifs; 107 }; 108 109 struct USBDescIface { 110 uint8_t bInterfaceNumber; 111 uint8_t bAlternateSetting; 112 uint8_t bNumEndpoints; 113 uint8_t bInterfaceClass; 114 uint8_t bInterfaceSubClass; 115 uint8_t bInterfaceProtocol; 116 uint8_t iInterface; 117 118 uint8_t ndesc; 119 USBDescOther *descs; 120 USBDescEndpoint *eps; 121 }; 122 123 struct USBDescEndpoint { 124 uint8_t bEndpointAddress; 125 uint8_t bmAttributes; 126 uint16_t wMaxPacketSize; 127 uint8_t bInterval; 128 uint8_t bRefresh; 129 uint8_t bSynchAddress; 130 131 uint8_t is_audio; /* has bRefresh + bSynchAddress */ 132 uint8_t *extra; 133 }; 134 135 struct USBDescOther { 136 uint8_t length; 137 const uint8_t *data; 138 }; 139 140 typedef const char *USBDescStrings[256]; 141 142 struct USBDesc { 143 USBDescID id; 144 const USBDescDevice *full; 145 const USBDescDevice *high; 146 const char* const *str; 147 }; 148 149 /* generate usb packages from structs */ 150 int usb_desc_device(const USBDescID *id, const USBDescDevice *dev, 151 uint8_t *dest, size_t len); 152 int usb_desc_device_qualifier(const USBDescDevice *dev, 153 uint8_t *dest, size_t len); 154 int usb_desc_config(const USBDescConfig *conf, uint8_t *dest, size_t len); 155 int usb_desc_iface_group(const USBDescIfaceAssoc *iad, uint8_t *dest, 156 size_t len); 157 int usb_desc_iface(const USBDescIface *iface, uint8_t *dest, size_t len); 158 int usb_desc_endpoint(const USBDescEndpoint *ep, uint8_t *dest, size_t len); 159 int usb_desc_other(const USBDescOther *desc, uint8_t *dest, size_t len); 160 161 /* control message emulation helpers */ 162 void usb_desc_init(USBDevice *dev); 163 void usb_desc_attach(USBDevice *dev); 164 void usb_desc_set_string(USBDevice *dev, uint8_t index, const char *str); 165 const char *usb_desc_get_string(USBDevice *dev, uint8_t index); 166 int usb_desc_string(USBDevice *dev, int index, uint8_t *dest, size_t len); 167 int usb_desc_get_descriptor(USBDevice *dev, int value, uint8_t *dest, size_t len); 168 int usb_desc_handle_control(USBDevice *dev, USBPacket *p, 169 int request, int value, int index, int length, uint8_t *data); 170 171 #endif /* QEMU_HW_USB_DESC_H */ 172