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