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