Introduce helper functions for finding USB end-points.
[seabios.git] / src / usb.h
1 // USB functions and data.
2 #ifndef __USB_H
3 #define __USB_H
4
5 // Local information for a usb controller.
6 struct usb_s {
7     u8 type;
8     u8 maxaddr;
9     u16 bdf;
10
11     union {
12         struct {
13             u16 iobase;
14             void *qh, *framelist;
15         } uhci;
16         struct {
17             struct ohci_regs *regs;
18             void *control_ed;
19         } ohci;
20     };
21 };
22
23 #define USB_TYPE_UHCI 1
24 #define USB_TYPE_OHCI 2
25
26 extern struct usb_s USBControllers[];
27
28 struct usb_pipe {
29     u32 endp;
30 };
31
32 #define USB_MAXADDR 127
33
34 // usb.c
35 void usb_setup(void);
36 int configure_usb_device(struct usb_s *cntl, int lowspeed);
37 struct usb_ctrlrequest;
38 int send_default_control(u32 endp, const struct usb_ctrlrequest *req
39                          , void *data);
40 struct usb_pipe *alloc_intr_pipe(u32 endp, int period);
41 int usb_poll_intr(struct usb_pipe *pipe, void *data);
42 struct usb_interface_descriptor;
43 struct usb_endpoint_descriptor *findEndPointDesc(
44     struct usb_interface_descriptor *iface, int imax, int type, int dir);
45 u32 mkendpFromDesc(u32 endp, struct usb_endpoint_descriptor *epdesc);
46
47
48 /****************************************************************
49  * endpoint definition
50  ****************************************************************/
51
52 static inline u32
53 mkendp(struct usb_s *cntl, u8 devaddr, u8 ep, u8 lowspeed, u8 maxsize)
54 {
55     u8 bus = cntl-USBControllers;
56     u8 size = __ffs(maxsize);
57     return (size<<25) | (lowspeed<<24) | (bus<<16) | (devaddr<<8) | ep;
58 }
59
60 static inline u8 endp2ep(u32 endp) {
61     return endp;
62 }
63 static inline u8 endp2devaddr(u32 endp) {
64     return endp>>8;
65 }
66 static inline struct usb_s *endp2cntl(u32 endp) {
67     u8 bus = endp>>16;
68     return &USBControllers[bus];
69 }
70 static inline u8 endp2speed(u32 endp) {
71     return (endp>>24) & 1;
72 }
73 static inline u8 endp2maxsize(u32 endp) {
74     return 1 << (endp>>25);
75 }
76
77
78 /****************************************************************
79  * usb structs and flags
80  ****************************************************************/
81
82 // USB mandated timings (in ms)
83 #define USB_TIME_SIGATT 100
84 #define USB_TIME_ATTDB  100
85 #define USB_TIME_DRST   10
86 #define USB_TIME_DRSTR  50
87 #define USB_TIME_RSTRCY 10
88
89 #define USB_TIME_SETADDR_RECOVERY 2
90
91 #define USB_PID_OUT                     0xe1
92 #define USB_PID_IN                      0x69
93 #define USB_PID_SETUP                   0x2d
94
95 #define USB_DIR_OUT                     0               /* to device */
96 #define USB_DIR_IN                      0x80            /* to host */
97
98 #define USB_TYPE_MASK                   (0x03 << 5)
99 #define USB_TYPE_STANDARD               (0x00 << 5)
100 #define USB_TYPE_CLASS                  (0x01 << 5)
101 #define USB_TYPE_VENDOR                 (0x02 << 5)
102 #define USB_TYPE_RESERVED               (0x03 << 5)
103
104 #define USB_RECIP_MASK                  0x1f
105 #define USB_RECIP_DEVICE                0x00
106 #define USB_RECIP_INTERFACE             0x01
107 #define USB_RECIP_ENDPOINT              0x02
108 #define USB_RECIP_OTHER                 0x03
109
110 #define USB_REQ_GET_STATUS              0x00
111 #define USB_REQ_CLEAR_FEATURE           0x01
112 #define USB_REQ_SET_FEATURE             0x03
113 #define USB_REQ_SET_ADDRESS             0x05
114 #define USB_REQ_GET_DESCRIPTOR          0x06
115 #define USB_REQ_SET_DESCRIPTOR          0x07
116 #define USB_REQ_GET_CONFIGURATION       0x08
117 #define USB_REQ_SET_CONFIGURATION       0x09
118 #define USB_REQ_GET_INTERFACE           0x0A
119 #define USB_REQ_SET_INTERFACE           0x0B
120 #define USB_REQ_SYNCH_FRAME             0x0C
121
122 struct usb_ctrlrequest {
123     u8 bRequestType;
124     u8 bRequest;
125     u16 wValue;
126     u16 wIndex;
127     u16 wLength;
128 } PACKED;
129
130 #define USB_DT_DEVICE                   0x01
131 #define USB_DT_CONFIG                   0x02
132 #define USB_DT_STRING                   0x03
133 #define USB_DT_INTERFACE                0x04
134 #define USB_DT_ENDPOINT                 0x05
135 #define USB_DT_DEVICE_QUALIFIER         0x06
136 #define USB_DT_OTHER_SPEED_CONFIG       0x07
137
138 struct usb_device_descriptor {
139     u8  bLength;
140     u8  bDescriptorType;
141
142     u16 bcdUSB;
143     u8  bDeviceClass;
144     u8  bDeviceSubClass;
145     u8  bDeviceProtocol;
146     u8  bMaxPacketSize0;
147     u16 idVendor;
148     u16 idProduct;
149     u16 bcdDevice;
150     u8  iManufacturer;
151     u8  iProduct;
152     u8  iSerialNumber;
153     u8  bNumConfigurations;
154 } PACKED;
155
156 #define USB_CLASS_PER_INTERFACE         0       /* for DeviceClass */
157 #define USB_CLASS_AUDIO                 1
158 #define USB_CLASS_COMM                  2
159 #define USB_CLASS_HID                   3
160 #define USB_CLASS_PHYSICAL              5
161 #define USB_CLASS_STILL_IMAGE           6
162 #define USB_CLASS_PRINTER               7
163 #define USB_CLASS_MASS_STORAGE          8
164 #define USB_CLASS_HUB                   9
165
166 struct usb_config_descriptor {
167     u8  bLength;
168     u8  bDescriptorType;
169
170     u16 wTotalLength;
171     u8  bNumInterfaces;
172     u8  bConfigurationValue;
173     u8  iConfiguration;
174     u8  bmAttributes;
175     u8  bMaxPower;
176 } PACKED;
177
178 struct usb_interface_descriptor {
179     u8  bLength;
180     u8  bDescriptorType;
181
182     u8  bInterfaceNumber;
183     u8  bAlternateSetting;
184     u8  bNumEndpoints;
185     u8  bInterfaceClass;
186     u8  bInterfaceSubClass;
187     u8  bInterfaceProtocol;
188     u8  iInterface;
189 } PACKED;
190
191 struct usb_endpoint_descriptor {
192     u8  bLength;
193     u8  bDescriptorType;
194
195     u8  bEndpointAddress;
196     u8  bmAttributes;
197     u16 wMaxPacketSize;
198     u8  bInterval;
199 } PACKED;
200
201 #define USB_ENDPOINT_NUMBER_MASK        0x0f    /* in bEndpointAddress */
202 #define USB_ENDPOINT_DIR_MASK           0x80
203
204 #define USB_ENDPOINT_XFERTYPE_MASK      0x03    /* in bmAttributes */
205 #define USB_ENDPOINT_XFER_CONTROL       0
206 #define USB_ENDPOINT_XFER_ISOC          1
207 #define USB_ENDPOINT_XFER_BULK          2
208 #define USB_ENDPOINT_XFER_INT           3
209 #define USB_ENDPOINT_MAX_ADJUSTABLE     0x80
210
211 #endif // usb.h