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