1 // Copyright 2016 The Fuchsia Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #pragma once 6 7 // clang-format off 8 9 #include <endian.h> 10 #include <stdint.h> 11 #include <zircon/compiler.h> 12 13 __BEGIN_CDECLS; 14 15 // maximum number of endpoints per device 16 #define USB_MAX_EPS 32 17 18 /* Request Types */ 19 #define USB_DIR_OUT (0 << 7) 20 #define USB_DIR_IN (1 << 7) 21 #define USB_DIR_MASK (1 << 7) 22 #define USB_TYPE_STANDARD (0 << 5) 23 #define USB_TYPE_CLASS (1 << 5) 24 #define USB_TYPE_VENDOR (2 << 5) 25 #define USB_TYPE_MASK (3 << 5) 26 #define USB_RECIP_DEVICE (0 << 0) 27 #define USB_RECIP_INTERFACE (1 << 0) 28 #define USB_RECIP_ENDPOINT (2 << 0) 29 #define USB_RECIP_OTHER (3 << 0) 30 #define USB_RECIP_MASK (0x1f << 0) 31 32 /* 1.0 Request Values */ 33 #define USB_REQ_GET_STATUS 0x00 34 #define USB_REQ_CLEAR_FEATURE 0x01 35 #define USB_REQ_SET_FEATURE 0x03 36 #define USB_REQ_SET_ADDRESS 0x05 37 #define USB_REQ_GET_DESCRIPTOR 0x06 38 #define USB_REQ_SET_DESCRIPTOR 0x07 39 #define USB_REQ_GET_CONFIGURATION 0x08 40 #define USB_REQ_SET_CONFIGURATION 0x09 41 #define USB_REQ_GET_INTERFACE 0x0A 42 #define USB_REQ_SET_INTERFACE 0x0B 43 #define USB_REQ_SYNCH_FRAME 0x0C 44 45 /* USB device/interface classes */ 46 #define USB_CLASS_AUDIO 0x01 47 #define USB_CLASS_COMM 0x02 48 #define USB_CLASS_HID 0x03 49 #define USB_CLASS_PHYSICAL 0x05 50 #define USB_CLASS_IMAGING 0x06 51 #define USB_CLASS_PRINTER 0x07 52 #define USB_CLASS_MSC 0x08 53 #define USB_CLASS_HUB 0x09 54 #define USB_CLASS_CDC 0x0a 55 #define USB_CLASS_CCID 0x0b 56 #define USB_CLASS_SECURITY 0x0d 57 #define USB_CLASS_VIDEO 0x0e 58 #define USB_CLASS_HEALTHCARE 0x0f 59 #define USB_CLASS_DIAGNOSTIC 0xdc 60 #define USB_CLASS_WIRELESS 0xe0 61 #define USB_CLASS_MISC 0xef 62 #define USB_CLASS_APPLICATION_SPECIFIC 0xfe 63 #define USB_CLASS_VENDOR 0xFf 64 65 #define USB_SUBCLASS_MSC_SCSI 0x06 66 #define USB_PROTOCOL_MSC_BULK_ONLY 0x50 67 68 #define USB_SUBCLASS_DFU 0x01 69 #define USB_PROTOCOL_DFU 0x02 70 71 /* Descriptor Types */ 72 #define USB_DT_DEVICE 0x01 73 #define USB_DT_CONFIG 0x02 74 #define USB_DT_STRING 0x03 75 #define USB_DT_INTERFACE 0x04 76 #define USB_DT_ENDPOINT 0x05 77 #define USB_DT_DEVICE_QUALIFIER 0x06 78 #define USB_DT_OTHER_SPEED_CONFIG 0x07 79 #define USB_DT_INTERFACE_POWER 0x08 80 #define USB_DT_INTERFACE_ASSOCIATION 0x0b 81 #define USB_DT_HID 0x21 82 #define USB_DT_HIDREPORT 0x22 83 #define USB_DT_HIDPHYSICAL 0x23 84 #define USB_DT_CS_INTERFACE 0x24 85 #define USB_DT_CS_ENDPOINT 0x25 86 #define USB_DT_SS_EP_COMPANION 0x30 87 #define USB_DT_SS_ISOCH_EP_COMPANION 0x31 88 89 /* USB device feature selectors */ 90 #define USB_DEVICE_SELF_POWERED 0x00 91 #define USB_DEVICE_REMOTE_WAKEUP 0x01 92 #define USB_DEVICE_TEST_MODE 0x02 93 94 /* Configuration attributes (bmAttributes) */ 95 #define USB_CONFIGURATION_REMOTE_WAKEUP 0x20 96 #define USB_CONFIGURATION_SELF_POWERED 0x40 97 #define USB_CONFIGURATION_RESERVED_7 0x80 // This bit must be set 98 99 /* Endpoint direction (bEndpointAddress) */ 100 #define USB_ENDPOINT_IN 0x80 101 #define USB_ENDPOINT_OUT 0x00 102 #define USB_ENDPOINT_DIR_MASK 0x80 103 #define USB_ENDPOINT_NUM_MASK 0x1F 104 105 /* Endpoint types (bmAttributes) */ 106 #define USB_ENDPOINT_CONTROL 0x00 107 #define USB_ENDPOINT_ISOCHRONOUS 0x01 108 #define USB_ENDPOINT_BULK 0x02 109 #define USB_ENDPOINT_INTERRUPT 0x03 110 #define USB_ENDPOINT_TYPE_MASK 0x03 111 112 /* Endpoint synchronization type (bmAttributes) */ 113 #define USB_ENDPOINT_NO_SYNCHRONIZATION 0x00 114 #define USB_ENDPOINT_ASYNCHRONOUS 0x04 115 #define USB_ENDPOINT_ADAPTIVE 0x08 116 #define USB_ENDPOINT_SYNCHRONOUS 0x0C 117 #define USB_ENDPOINT_SYNCHRONIZATION_MASK 0x0C 118 119 /* Endpoint usage type (bmAttributes) */ 120 #define USB_ENDPOINT_DATA 0x00 121 #define USB_ENDPOINT_FEEDBACK 0x10 122 #define USB_ENDPOINT_IMPLICIT_FEEDBACK 0x20 123 #define USB_ENDPOINT_USAGE_MASK 0x30 124 125 #define USB_ENDPOINT_HALT 0x00 126 127 // Values in this set match those used in XHCI and other parts of the USB specification 128 #define USB_SPEED_UNDEFINED 0 129 #define USB_SPEED_FULL 1 130 #define USB_SPEED_LOW 2 131 #define USB_SPEED_HIGH 3 132 #define USB_SPEED_SUPER 4 133 typedef uint32_t usb_speed_t; 134 135 /* general USB defines */ 136 typedef struct { 137 uint8_t bmRequestType; 138 uint8_t bRequest; 139 uint16_t wValue; 140 uint16_t wIndex; 141 uint16_t wLength; 142 } __attribute__ ((packed)) usb_setup_t; 143 144 typedef struct { 145 uint8_t bLength; 146 uint8_t bDescriptorType; 147 } __attribute__ ((packed)) usb_descriptor_header_t; 148 149 typedef struct { 150 uint8_t bLength; 151 uint8_t bDescriptorType; // USB_DT_DEVICE 152 uint16_t bcdUSB; 153 uint8_t bDeviceClass; 154 uint8_t bDeviceSubClass; 155 uint8_t bDeviceProtocol; 156 uint8_t bMaxPacketSize0; 157 uint16_t idVendor; 158 uint16_t idProduct; 159 uint16_t bcdDevice; 160 uint8_t iManufacturer; 161 uint8_t iProduct; 162 uint8_t iSerialNumber; 163 uint8_t bNumConfigurations; 164 } __attribute__ ((packed)) usb_device_descriptor_t; 165 166 typedef struct { 167 uint8_t bLength; 168 uint8_t bDescriptorType; // USB_DT_CONFIG 169 uint16_t wTotalLength; 170 uint8_t bNumInterfaces; 171 uint8_t bConfigurationValue; 172 uint8_t iConfiguration; 173 uint8_t bmAttributes; 174 uint8_t bMaxPower; 175 } __attribute__ ((packed)) usb_configuration_descriptor_t; 176 177 typedef struct { 178 uint8_t bLength; 179 uint8_t bDescriptorType; // USB_DT_STRING 180 uint8_t bString[]; 181 } __attribute__ ((packed)) usb_string_descriptor_t; 182 183 typedef struct { 184 uint8_t bLength; 185 uint8_t bDescriptorType; // USB_DT_INTERFACE 186 uint8_t bInterfaceNumber; 187 uint8_t bAlternateSetting; 188 uint8_t bNumEndpoints; 189 uint8_t bInterfaceClass; 190 uint8_t bInterfaceSubClass; 191 uint8_t bInterfaceProtocol; 192 uint8_t iInterface; 193 } __attribute__ ((packed)) usb_interface_descriptor_t; 194 195 typedef struct { 196 uint8_t bLength; 197 uint8_t bDescriptorType; // USB_DT_ENDPOINT 198 uint8_t bEndpointAddress; 199 uint8_t bmAttributes; 200 uint16_t wMaxPacketSize; 201 uint8_t bInterval; 202 } __attribute__ ((packed)) usb_endpoint_descriptor_t; 203 #define usb_ep_direction(ep) ((ep)->bEndpointAddress & USB_ENDPOINT_DIR_MASK) 204 #define usb_ep_type(ep) ((ep)->bmAttributes & USB_ENDPOINT_TYPE_MASK) 205 #define usb_ep_sync_type(ep) ((ep)->bmAttributes & USB_ENDPOINT_SYNCHRONIZATION_MASK) 206 // max packet size is in bits 10..0 207 #define usb_ep_max_packet(ep) (le16toh((ep)->wMaxPacketSize) & 0x07FF) 208 // for high speed interrupt and isochronous endpoints, additional transactions per microframe 209 // are in bits 12..11 210 #define usb_ep_add_mf_transactions(ep) ((le16toh((ep)->wMaxPacketSize) >> 11) & 3) 211 212 typedef struct { 213 uint8_t bLength; 214 uint8_t bDescriptorType; // USB_DT_SS_EP_COMPANION 215 uint8_t bMaxBurst; 216 uint8_t bmAttributes; 217 uint16_t wBytesPerInterval; 218 } __attribute__ ((packed)) usb_ss_ep_comp_descriptor_t; 219 #define usb_ss_ep_comp_isoc_mult(ep) ((ep)->bmAttributes & 0x3) 220 #define usb_ss_ep_comp_isoc_comp(ep) (!!((ep)->bmAttributes & 0x80)) 221 222 typedef struct { 223 uint8_t bLength; 224 uint8_t bDescriptorType; // USB_DT_SS_ISOCH_EP_COMPANION 225 uint16_t wReserved; 226 uint32_t dwBytesPerInterval; 227 } __attribute__ ((packed)) usb_ss_isoch_ep_comp_descriptor_t; 228 229 typedef struct { 230 uint8_t bLength; 231 uint8_t bDescriptorType; // USB_DT_INTERFACE_ASSOCIATION 232 uint8_t bFirstInterface; 233 uint8_t bInterfaceCount; 234 uint8_t bFunctionClass; 235 uint8_t bFunctionSubClass; 236 uint8_t bFunctionProtocol; 237 uint8_t iFunction; 238 } __attribute__ ((packed)) usb_interface_assoc_descriptor_t; 239 240 typedef struct { 241 uint8_t bLength; 242 uint8_t bDescriptorType; // USB_DT_CS_INTERFACE 243 uint8_t bDescriptorSubType; 244 } __attribute__ ((packed)) usb_cs_interface_descriptor_t; 245 246 __END_CDECLS; 247