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