| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#ifndef __USB_H |
| 30 |
#define __USB_H |
| 31 |
#include <libpayload.h> |
| 32 |
#include <pci/pci.h> |
| 33 |
#include <stdint.h> |
| 34 |
|
| 35 |
typedef enum { host_to_device = 0, device_to_host = 1 } dev_req_dir; |
| 36 |
typedef enum { standard_type = 0, class_type = 1, vendor_type = |
| 37 |
2, reserved_type = 3 |
| 38 |
} dev_req_type; |
| 39 |
typedef enum { dev_recp = 0, iface_recp = 1, endp_recp = 2, other_recp = 3 |
| 40 |
} dev_req_recp; |
| 41 |
|
| 42 |
enum { |
| 43 |
DT_DEV = 1, |
| 44 |
DT_CFG = 2, |
| 45 |
DT_STR = 3, |
| 46 |
DT_INTF = 4, |
| 47 |
DT_ENDP = 5, |
| 48 |
}; |
| 49 |
|
| 50 |
typedef enum { |
| 51 |
GET_STATUS = 0, |
| 52 |
CLEAR_FEATURE = 1, |
| 53 |
SET_FEATURE = 3, |
| 54 |
SET_ADDRESS = 5, |
| 55 |
GET_DESCRIPTOR = 6, |
| 56 |
SET_DESCRIPTOR = 7, |
| 57 |
GET_CONFIGURATION = 8, |
| 58 |
SET_CONFIGURATION = 9, |
| 59 |
GET_INTERFACE = 10, |
| 60 |
SET_INTERFACE = 11, |
| 61 |
SYNCH_FRAME = 12 |
| 62 |
} bRequest_Codes; |
| 63 |
|
| 64 |
typedef enum { |
| 65 |
ENDPOINT_HALT = 0, |
| 66 |
DEVICE_REMOTE_WAKEUP = 1, |
| 67 |
TEST_MODE = 2 |
| 68 |
} feature_selectors; |
| 69 |
|
| 70 |
|
| 71 |
#define SET_ADDRESS_MDELAY 2 |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#define USB_MAX_PROCESSING_TIME_US (5 * 1000 * 1000) |
| 88 |
|
| 89 |
#define USB_FULL_LOW_SPEED_FRAME_US 1000 |
| 90 |
|
| 91 |
typedef struct { |
| 92 |
unsigned char bDescLength; |
| 93 |
unsigned char bDescriptorType; |
| 94 |
unsigned char bNbrPorts; |
| 95 |
union { |
| 96 |
struct { |
| 97 |
unsigned long logicalPowerSwitchingMode:2; |
| 98 |
unsigned long isCompoundDevice:1; |
| 99 |
unsigned long overcurrentProtectionMode:2; |
| 100 |
unsigned long ttThinkTime:2; |
| 101 |
unsigned long arePortIndicatorsSupported:1; |
| 102 |
unsigned long:8; |
| 103 |
} __packed; |
| 104 |
unsigned short wHubCharacteristics; |
| 105 |
} __packed; |
| 106 |
unsigned char bPowerOn2PwrGood; |
| 107 |
unsigned char bHubContrCurrent; |
| 108 |
char DeviceRemovable[]; |
| 109 |
} __packed hub_descriptor_t; |
| 110 |
|
| 111 |
typedef struct { |
| 112 |
unsigned char bLength; |
| 113 |
unsigned char bDescriptorType; |
| 114 |
unsigned short bcdUSB; |
| 115 |
unsigned char bDeviceClass; |
| 116 |
unsigned char bDeviceSubClass; |
| 117 |
unsigned char bDeviceProtocol; |
| 118 |
unsigned char bMaxPacketSize0; |
| 119 |
unsigned short idVendor; |
| 120 |
unsigned short idProduct; |
| 121 |
unsigned short bcdDevice; |
| 122 |
unsigned char iManufacturer; |
| 123 |
unsigned char iProduct; |
| 124 |
unsigned char iSerialNumber; |
| 125 |
unsigned char bNumConfigurations; |
| 126 |
} __packed device_descriptor_t; |
| 127 |
|
| 128 |
typedef struct { |
| 129 |
unsigned char bLength; |
| 130 |
unsigned char bDescriptorType; |
| 131 |
unsigned short wTotalLength; |
| 132 |
unsigned char bNumInterfaces; |
| 133 |
unsigned char bConfigurationValue; |
| 134 |
unsigned char iConfiguration; |
| 135 |
unsigned char bmAttributes; |
| 136 |
unsigned char bMaxPower; |
| 137 |
} __packed configuration_descriptor_t; |
| 138 |
|
| 139 |
typedef struct { |
| 140 |
unsigned char bLength; |
| 141 |
unsigned char bDescriptorType; |
| 142 |
unsigned char bInterfaceNumber; |
| 143 |
unsigned char bAlternateSetting; |
| 144 |
unsigned char bNumEndpoints; |
| 145 |
unsigned char bInterfaceClass; |
| 146 |
unsigned char bInterfaceSubClass; |
| 147 |
unsigned char bInterfaceProtocol; |
| 148 |
unsigned char iInterface; |
| 149 |
} __packed interface_descriptor_t; |
| 150 |
|
| 151 |
typedef struct { |
| 152 |
unsigned char bLength; |
| 153 |
unsigned char bDescriptorType; |
| 154 |
unsigned char bEndpointAddress; |
| 155 |
unsigned char bmAttributes; |
| 156 |
unsigned short wMaxPacketSize; |
| 157 |
unsigned char bInterval; |
| 158 |
} __packed endpoint_descriptor_t; |
| 159 |
|
| 160 |
typedef struct { |
| 161 |
unsigned char bLength; |
| 162 |
unsigned char bDescriptorType; |
| 163 |
unsigned short bcdHID; |
| 164 |
unsigned char bCountryCode; |
| 165 |
unsigned char bNumDescriptors; |
| 166 |
unsigned char bReportDescriptorType; |
| 167 |
unsigned short wReportDescriptorLength; |
| 168 |
} __packed hid_descriptor_t; |
| 169 |
|
| 170 |
typedef struct { |
| 171 |
union { |
| 172 |
struct { |
| 173 |
dev_req_recp req_recp:5; |
| 174 |
dev_req_type req_type:2; |
| 175 |
dev_req_dir data_dir:1; |
| 176 |
} __packed; |
| 177 |
unsigned char bmRequestType; |
| 178 |
} __packed; |
| 179 |
unsigned char bRequest; |
| 180 |
unsigned short wValue; |
| 181 |
unsigned short wIndex; |
| 182 |
unsigned short wLength; |
| 183 |
} __packed dev_req_t; |
| 184 |
|
| 185 |
struct usbdev_hc; |
| 186 |
typedef struct usbdev_hc hci_t; |
| 187 |
|
| 188 |
struct usbdev; |
| 189 |
typedef struct usbdev usbdev_t; |
| 190 |
|
| 191 |
typedef enum { SETUP, IN, OUT } direction_t; |
| 192 |
typedef enum { CONTROL = 0, ISOCHRONOUS = 1, BULK = 2, INTERRUPT = 3 |
| 193 |
} endpoint_type; |
| 194 |
|
| 195 |
typedef struct { |
| 196 |
usbdev_t *dev; |
| 197 |
int endpoint; |
| 198 |
direction_t direction; |
| 199 |
int toggle; |
| 200 |
int maxpacketsize; |
| 201 |
endpoint_type type; |
| 202 |
int interval; |
| 203 |
|
| 204 |
} endpoint_t; |
| 205 |
|
| 206 |
typedef enum { |
| 207 |
UNKNOWN_SPEED = -1, |
| 208 |
FULL_SPEED = 0, |
| 209 |
LOW_SPEED = 1, |
| 210 |
HIGH_SPEED = 2, |
| 211 |
SUPER_SPEED = 3, |
| 212 |
SUPER_SPEED_PLUS = 4, |
| 213 |
} usb_speed; |
| 214 |
|
| 215 |
struct usbdev { |
| 216 |
hci_t *controller; |
| 217 |
endpoint_t endpoints[32]; |
| 218 |
int num_endp; |
| 219 |
int address; |
| 220 |
int hub; |
| 221 |
int port; |
| 222 |
usb_speed speed; |
| 223 |
u32 quirks; |
| 224 |
void *data; |
| 225 |
device_descriptor_t *descriptor; |
| 226 |
configuration_descriptor_t *configuration; |
| 227 |
void (*init) (usbdev_t *dev); |
| 228 |
void (*destroy) (usbdev_t *dev); |
| 229 |
void (*poll) (usbdev_t *dev); |
| 230 |
}; |
| 231 |
|
| 232 |
typedef enum { OHCI = 0, UHCI = 1, EHCI = 2, XHCI = 3, DWC2 = 4} hc_type; |
| 233 |
|
| 234 |
struct usbdev_hc { |
| 235 |
hci_t *next; |
| 236 |
uintptr_t reg_base; |
| 237 |
pcidev_t pcidev; |
| 238 |
hc_type type; |
| 239 |
int latest_address; |
| 240 |
usbdev_t *devices[128]; |
| 241 |
|
| 242 |
|
| 243 |
void (*start) (hci_t *controller); |
| 244 |
|
| 245 |
void (*stop) (hci_t *controller); |
| 246 |
|
| 247 |
|
| 248 |
void (*reset) (hci_t *controller); |
| 249 |
|
| 250 |
|
| 251 |
void (*init) (hci_t *controller); |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
void (*shutdown) (hci_t *controller); |
| 256 |
|
| 257 |
int (*bulk) (endpoint_t *ep, int size, u8 *data, int finalize); |
| 258 |
int (*control) (usbdev_t *dev, direction_t pid, int dr_length, |
| 259 |
void *devreq, int data_length, u8 *data); |
| 260 |
void* (*create_intr_queue) (endpoint_t *ep, int reqsize, int reqcount, int reqtiming); |
| 261 |
void (*destroy_intr_queue) (endpoint_t *ep, void *queue); |
| 262 |
u8* (*poll_intr_queue) (void *queue); |
| 263 |
void *instance; |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
usbdev_t *(*set_address) (hci_t *controller, usb_speed speed, |
| 271 |
int hubport, int hubaddr); |
| 272 |
|
| 273 |
|
| 274 |
int (*finish_device_config) (usbdev_t *dev); |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
void (*destroy_device) (hci_t *controller, int devaddr); |
| 279 |
}; |
| 280 |
|
| 281 |
hci_t *usb_add_mmio_hc(hc_type type, void *bar); |
| 282 |
hci_t *new_controller (void); |
| 283 |
void detach_controller (hci_t *controller); |
| 284 |
void usb_poll (void); |
| 285 |
usbdev_t *init_device_entry (hci_t *controller, int num); |
| 286 |
|
| 287 |
int usb_decode_mps0 (usb_speed speed, u8 bMaxPacketSize0); |
| 288 |
int speed_to_default_mps(usb_speed speed); |
| 289 |
int set_feature (usbdev_t *dev, int endp, int feature, int rtype); |
| 290 |
int get_status (usbdev_t *dev, int endp, int rtype, int len, void *data); |
| 291 |
int get_descriptor (usbdev_t *dev, int rtype, int descType, int descIdx, |
| 292 |
void *data, size_t len); |
| 293 |
int set_configuration (usbdev_t *dev); |
| 294 |
int clear_feature (usbdev_t *dev, int endp, int feature, int rtype); |
| 295 |
int clear_stall (endpoint_t *ep); |
| 296 |
_Bool is_usb_speed_ss(usb_speed speed); |
| 297 |
|
| 298 |
void usb_nop_init (usbdev_t *dev); |
| 299 |
void usb_hub_init (usbdev_t *dev); |
| 300 |
void usb_hid_init (usbdev_t *dev); |
| 301 |
void usb_msc_init (usbdev_t *dev); |
| 302 |
void usb_generic_init (usbdev_t *dev); |
| 303 |
|
| 304 |
int closest_usb2_hub(const usbdev_t *dev, int *const addr, int *const port); |
| 305 |
|
| 306 |
static inline unsigned char |
| 307 |
gen_bmRequestType (dev_req_dir dir, dev_req_type type, dev_req_recp recp) |
| 308 |
{ |
| 309 |
return (dir << 7) | (type << 5) | recp; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
usbdev_t *generic_set_address (hci_t *controller, usb_speed speed, |
| 314 |
int hubport, int hubaddr); |
| 315 |
|
| 316 |
void usb_detach_device(hci_t *controller, int devno); |
| 317 |
int usb_attach_device(hci_t *controller, int hubaddress, int port, |
| 318 |
usb_speed speed); |
| 319 |
|
| 320 |
u32 pci_quirk_check(pcidev_t controller); |
| 321 |
u32 usb_quirk_check(u16 vendor, u16 device); |
| 322 |
int usb_interface_check(u16 vendor, u16 device); |
| 323 |
|
| 324 |
#define USB_QUIRK_MSC_FORCE_PROTO_SCSI (1 << 0) |
| 325 |
#define USB_QUIRK_MSC_FORCE_PROTO_ATAPI (1 << 1) |
| 326 |
#define USB_QUIRK_MSC_FORCE_PROTO_UFI (1 << 2) |
| 327 |
#define USB_QUIRK_MSC_FORCE_PROTO_RBC (1 << 3) |
| 328 |
#define USB_QUIRK_MSC_FORCE_TRANS_BBB (1 << 4) |
| 329 |
#define USB_QUIRK_MSC_FORCE_TRANS_CBI (1 << 5) |
| 330 |
#define USB_QUIRK_MSC_FORCE_TRANS_CBI_I (1 << 6) |
| 331 |
#define USB_QUIRK_MSC_NO_TEST_UNIT_READY (1 << 7) |
| 332 |
#define USB_QUIRK_MSC_SHORT_INQUIRY (1 << 8) |
| 333 |
#define USB_QUIRK_HUB_NO_USBSTS_PCD (1 << 9) |
| 334 |
#define USB_QUIRK_TEST (1 << 31) |
| 335 |
#define USB_QUIRK_NONE 0 |
| 336 |
|
| 337 |
static inline void __attribute__((format(printf, 1, 2))) usb_debug(const char *fmt, ...) |
| 338 |
{ |
| 339 |
#ifdef USB_DEBUG |
| 340 |
va_list ap; |
| 341 |
va_start(ap, fmt); |
| 342 |
vprintf(fmt, ap); |
| 343 |
va_end(ap); |
| 344 |
#endif |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
void __attribute__((weak)) usb_poll_prepare (void); |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
void __attribute__((weak)) usb_generic_create (usbdev_t *dev); |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
void __attribute__((weak)) usb_generic_remove (usbdev_t *dev); |
| 371 |
|
| 372 |
#endif |