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
30#ifndef __USB_H
31#define __USB_H
32#include <libpayload.h>
33#include <pci.h>
34
35typedef enum { host_to_device = 0, device_to_host = 1 } dev_req_dir;
36typedef enum { standard_type = 0, class_type = 1, vendor_type =
37 2, reserved_type = 3
38} dev_req_type;
39typedef enum { dev_recp = 0, iface_recp = 1, endp_recp = 2, other_recp = 3
40} dev_req_recp;
41
42typedef enum {
43 GET_STATUS = 0,
44 CLEAR_FEATURE = 1,
45 SET_FEATURE = 3,
46 SET_ADDRESS = 5,
47 GET_DESCRIPTOR = 6,
48 SET_DESCRIPTOR = 7,
49 GET_CONFIGURATION = 8,
50 SET_CONFIGURATION = 9,
51 GET_INTERFACE = 10,
52 SET_INTERFACE = 11,
53 SYNCH_FRAME = 12
54} bRequest_Codes;
55
56typedef enum {
57 ENDPOINT_HALT = 0,
58 DEVICE_REMOTE_WAKEUP = 1,
59 TEST_MODE = 2
60} feature_selectors;
61
62typedef struct {
63 union {
64 struct {
65 dev_req_recp req_recp:5;
66 dev_req_type req_type:2;
67 dev_req_dir data_dir:1;
68 } __attribute__ ((packed));
69 unsigned char bmRequestType;
70 } __attribute__ ((packed));
71 unsigned char bRequest;
72 unsigned short wValue;
73 unsigned short wIndex;
74 unsigned short wLength;
75} __attribute__ ((packed)) dev_req_t;
76
77struct usbdev_hc;
78typedef struct usbdev_hc hci_t;
79
80struct usbdev;
81typedef struct usbdev usbdev_t;
82
83typedef enum { SETUP, IN, OUT } direction_t;
84typedef enum { CONTROL = 0, ISOCHRONOUS = 1, BULK = 2, INTERRUPT = 3
85} endpoint_type;
86
87typedef struct {
88 usbdev_t *dev;
89 int endpoint;
90 direction_t direction;
91 int toggle;
92 int maxpacketsize;
93 endpoint_type type;
94} endpoint_t;
95
96
97struct usbdev {
98 hci_t *controller;
99 endpoint_t endpoints[32];
100 int num_endp;
101 int address;
102 int hub;
103 int port;
104 int speed;
105 u32 quirks;
106 void *data;
107 u8 *descriptor;
108 u8 *configuration;
109 void (*init) (usbdev_t *dev);
110 void (*destroy) (usbdev_t *dev);
111 void (*poll) (usbdev_t *dev);
112};
113
114struct usbdev_hc {
115 struct usbdev_hc *next;
116 pcidev_t bus_address;
117 u32 reg_base;
118 usbdev_t *devices[128];
119 void (*start) (hci_t *controller);
120 void (*stop) (hci_t *controller);
121 void (*reset) (hci_t *controller);
122 void (*shutdown) (hci_t *controller);
123 int (*bulk) (endpoint_t *ep, int size, u8 *data, int finalize);
124 int (*control) (usbdev_t *dev, direction_t pid, int dr_length,
125 void *devreq, int data_length, u8 *data);
126 void* (*create_intr_queue) (endpoint_t *ep, int reqsize, int reqcount, int reqtiming);
127 void (*destroy_intr_queue) (endpoint_t *ep, void *queue);
128 u8* (*poll_intr_queue) (void *queue);
129 void *instance;
130};
131
132typedef struct {
133 unsigned char bDescLength;
134 unsigned char bDescriptorType;
135 unsigned char bNbrPorts;
136 union {
137 struct {
138 unsigned long logicalPowerSwitchingMode:2;
139 unsigned long isCompoundDevice:1;
140 unsigned long overcurrentProtectionMode:2;
141 unsigned long ttThinkTime:2;
142 unsigned long arePortIndicatorsSupported:1;
143 unsigned long:8;
144 } __attribute__ ((packed));
145 unsigned short wHubCharacteristics;
146 } __attribute__ ((packed));
147 unsigned char bPowerOn2PwrGood;
148 unsigned char bHubContrCurrent;
149 char DeviceRemovable[];
150} __attribute__ ((packed)) hub_descriptor_t;
151
152typedef struct {
153 unsigned char bLength;
154 unsigned char bDescriptorType;
155 unsigned short bcdUSB;
156 unsigned char bDeviceClass;
157 unsigned char bDeviceSubClass;
158 unsigned char bDeviceProtocol;
159 unsigned char bMaxPacketSize0;
160 unsigned short idVendor;
161 unsigned short idProduct;
162 unsigned short bcdDevice;
163 unsigned char iManufacturer;
164 unsigned char iProduct;
165 unsigned char iSerialNumber;
166 unsigned char bNumConfigurations;
167} __attribute__ ((packed)) device_descriptor_t;
168
169typedef struct {
170 unsigned char bLength;
171 unsigned char bDescriptorType;
172 unsigned short wTotalLength;
173 unsigned char bNumInterfaces;
174 unsigned char bConfigurationValue;
175 unsigned char iConfiguration;
176 unsigned char bmAttributes;
177 unsigned char bMaxPower;
178} __attribute__ ((packed)) configuration_descriptor_t;
179
180typedef struct {
181 unsigned char bLength;
182 unsigned char bDescriptorType;
183 unsigned char bInterfaceNumber;
184 unsigned char bAlternateSetting;
185 unsigned char bNumEndpoints;
186 unsigned char bInterfaceClass;
187 unsigned char bInterfaceSubClass;
188 unsigned char bInterfaceProtocol;
189 unsigned char iInterface;
190} __attribute__ ((packed)) interface_descriptor_t;
191
192typedef struct {
193 unsigned char bLength;
194 unsigned char bDescriptorType;
195 unsigned char bEndpointAddress;
196 unsigned char bmAttributes;
197 unsigned short wMaxPacketSize;
198 unsigned char bInterval;
199} __attribute__ ((packed)) endpoint_descriptor_t;
200
201typedef struct {
202 unsigned char bLength;
203 unsigned char bDescriptorType;
204 unsigned short bcdHID;
205 unsigned char bCountryCode;
206 unsigned char bNumDescriptors;
207 unsigned char bReportDescriptorType;
208 unsigned short wReportDescriptorLength;
209} __attribute__ ((packed)) hid_descriptor_t;
210
211hci_t *new_controller (void);
212void detach_controller (hci_t *controller);
213void usb_poll (void);
214void init_device_entry (hci_t *controller, int num);
215
216void set_feature (usbdev_t *dev, int endp, int feature, int rtype);
217void get_status (usbdev_t *dev, int endp, int rtype, int len, void *data);
218int clear_stall (endpoint_t *ep);
219
220void usb_nop_init (usbdev_t *dev);
221void usb_hub_init (usbdev_t *dev);
222void usb_hid_init (usbdev_t *dev);
223void usb_msc_init (usbdev_t *dev);
224
225int set_address (hci_t *controller, int speed);
226
227u8 *get_descriptor (usbdev_t *dev, unsigned char bmRequestType,
228 int descType, int descIdx, int langID);
229
230static inline unsigned char
231gen_bmRequestType (dev_req_dir dir, dev_req_type type, dev_req_recp recp)
232{
233 return (dir << 7) | (type << 5) | recp;
234}
235
236void usb_detach_device(hci_t *controller, int devno);
237int usb_attach_device(hci_t *controller, int hubaddress, int port, int speed);
238
239u32 usb_quirk_check(u16 vendor, u16 device);
240int usb_interface_check(u16 vendor, u16 device);
241
242#define USB_QUIRK_MSC_FORCE_PROTO_SCSI (1 << 0)
243#define USB_QUIRK_MSC_FORCE_PROTO_ATAPI (1 << 1)
244#define USB_QUIRK_MSC_FORCE_PROTO_UFI (1 << 2)
245#define USB_QUIRK_MSC_FORCE_PROTO_RBC (1 << 3)
246#define USB_QUIRK_MSC_FORCE_TRANS_BBB (1 << 4)
247#define USB_QUIRK_MSC_FORCE_TRANS_CBI (1 << 5)
248#define USB_QUIRK_MSC_FORCE_TRANS_CBI_I (1 << 6)
249#define USB_QUIRK_MSC_NO_TEST_UNIT_READY (1 << 7)
250#define USB_QUIRK_MSC_SHORT_INQUIRY (1 << 8)
251#define USB_QUIRK_TEST (1 << 31)
252#define USB_QUIRK_NONE 0
253
254#ifdef USB_DEBUG
255#define debug(x...) printf(x);
256#else
257#define debug(x...)
258#endif
259
260void usb_fatal(const char *message) __attribute__ ((noreturn));
261#endif
262