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