Dynamically allocate USB controller structures.
[seabios.git] / src / usb.c
1 // Main code for handling USB controllers and devices.
2 //
3 // Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // dprintf
8 #include "pci.h" // foreachpci
9 #include "config.h" // CONFIG_*
10 #include "pci_regs.h" // PCI_CLASS_REVISION
11 #include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
12 #include "usb-uhci.h" // uhci_init
13 #include "usb-ohci.h" // ohci_init
14 #include "usb-hid.h" // usb_keyboard_setup
15 #include "usb-hub.h" // usb_hub_init
16 #include "usb-msc.h" // usb_msc_init
17 #include "usb.h" // struct usb_s
18 #include "biosvar.h" // GET_GLOBAL
19
20
21 /****************************************************************
22  * Controller function wrappers
23  ****************************************************************/
24
25 // Free an allocated control or bulk pipe.
26 void
27 free_pipe(struct usb_pipe *pipe)
28 {
29     ASSERT32FLAT();
30     if (!pipe)
31         return;
32     switch (pipe->type) {
33     default:
34     case USB_TYPE_UHCI:
35         return uhci_free_pipe(pipe);
36     case USB_TYPE_OHCI:
37         return ohci_free_pipe(pipe);
38     }
39 }
40
41 // Allocate a control pipe to a default endpoint (which can only be
42 // used by 32bit code)
43 static struct usb_pipe *
44 alloc_default_control_pipe(struct usb_pipe *dummy)
45 {
46     switch (dummy->type) {
47     default:
48     case USB_TYPE_UHCI:
49         return uhci_alloc_control_pipe(dummy);
50     case USB_TYPE_OHCI:
51         return ohci_alloc_control_pipe(dummy);
52     }
53 }
54
55 // Send a message on a control pipe using the default control descriptor.
56 static int
57 send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
58              , void *data, int datasize)
59 {
60     ASSERT32FLAT();
61     switch (pipe->type) {
62     default:
63     case USB_TYPE_UHCI:
64         return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
65     case USB_TYPE_OHCI:
66         return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
67     }
68 }
69
70 // Fill "pipe" endpoint info from an endpoint descriptor.
71 static void
72 desc2pipe(struct usb_pipe *newpipe, struct usb_pipe *origpipe
73           , struct usb_endpoint_descriptor *epdesc)
74 {
75     memcpy(newpipe, origpipe, sizeof(*newpipe));
76     newpipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
77     newpipe->maxpacket = epdesc->wMaxPacketSize;
78 }
79
80 struct usb_pipe *
81 alloc_bulk_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
82 {
83     struct usb_pipe dummy;
84     desc2pipe(&dummy, pipe, epdesc);
85     switch (pipe->type) {
86     default:
87     case USB_TYPE_UHCI:
88         return uhci_alloc_bulk_pipe(&dummy);
89     case USB_TYPE_OHCI:
90         return NULL;
91     }
92 }
93
94 int
95 usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
96 {
97     switch (GET_FLATPTR(pipe_fl->type)) {
98     default:
99     case USB_TYPE_UHCI:
100         return uhci_send_bulk(pipe_fl, dir, data, datasize);
101     case USB_TYPE_OHCI:
102         return -1;
103     }
104 }
105
106 struct usb_pipe *
107 alloc_intr_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
108 {
109     struct usb_pipe dummy;
110     desc2pipe(&dummy, pipe, epdesc);
111     // Find the exponential period of the requested time.
112     int period = epdesc->bInterval;
113     if (period <= 0)
114         period = 1;
115     int frameexp = __fls(period);
116     switch (pipe->type) {
117     default:
118     case USB_TYPE_UHCI:
119         return uhci_alloc_intr_pipe(&dummy, frameexp);
120     case USB_TYPE_OHCI:
121         return ohci_alloc_intr_pipe(&dummy, frameexp);
122     }
123 }
124
125 int noinline
126 usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
127 {
128     switch (GET_FLATPTR(pipe_fl->type)) {
129     default:
130     case USB_TYPE_UHCI:
131         return uhci_poll_intr(pipe_fl, data);
132     case USB_TYPE_OHCI:
133         return ohci_poll_intr(pipe_fl, data);
134     }
135 }
136
137
138 /****************************************************************
139  * Helper functions
140  ****************************************************************/
141
142 // Find the first endpoing of a given type in an interface description.
143 struct usb_endpoint_descriptor *
144 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
145                  , int type, int dir)
146 {
147     struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
148     for (;;) {
149         if ((void*)epdesc >= (void*)iface + imax
150             || epdesc->bDescriptorType == USB_DT_INTERFACE) {
151             return NULL;
152         }
153         if (epdesc->bDescriptorType == USB_DT_ENDPOINT
154             && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
155             && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
156             return epdesc;
157         epdesc = (void*)epdesc + epdesc->bLength;
158     }
159 }
160
161 // Send a message to the default control pipe of a device.
162 int
163 send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
164                      , void *data)
165 {
166     return send_control(pipe, req->bRequestType & USB_DIR_IN
167                         , req, sizeof(*req), data, req->wLength);
168 }
169
170 // Get the first 8 bytes of the device descriptor.
171 static int
172 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
173 {
174     struct usb_ctrlrequest req;
175     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
176     req.bRequest = USB_REQ_GET_DESCRIPTOR;
177     req.wValue = USB_DT_DEVICE<<8;
178     req.wIndex = 0;
179     req.wLength = 8;
180     return send_default_control(pipe, &req, dinfo);
181 }
182
183 static struct usb_config_descriptor *
184 get_device_config(struct usb_pipe *pipe)
185 {
186     struct usb_config_descriptor cfg;
187
188     struct usb_ctrlrequest req;
189     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
190     req.bRequest = USB_REQ_GET_DESCRIPTOR;
191     req.wValue = USB_DT_CONFIG<<8;
192     req.wIndex = 0;
193     req.wLength = sizeof(cfg);
194     int ret = send_default_control(pipe, &req, &cfg);
195     if (ret)
196         return NULL;
197
198     void *config = malloc_tmphigh(cfg.wTotalLength);
199     if (!config)
200         return NULL;
201     req.wLength = cfg.wTotalLength;
202     ret = send_default_control(pipe, &req, config);
203     if (ret)
204         return NULL;
205     //hexdump(config, cfg.wTotalLength);
206     return config;
207 }
208
209 static int
210 set_configuration(struct usb_pipe *pipe, u16 val)
211 {
212     struct usb_ctrlrequest req;
213     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
214     req.bRequest = USB_REQ_SET_CONFIGURATION;
215     req.wValue = val;
216     req.wIndex = 0;
217     req.wLength = 0;
218     return send_default_control(pipe, &req, NULL);
219 }
220
221
222 /****************************************************************
223  * Initialization and enumeration
224  ****************************************************************/
225
226 // Assign an address to a device in the default state on the given
227 // controller.
228 struct usb_pipe *
229 usb_set_address(struct usb_s *cntl, int lowspeed)
230 {
231     ASSERT32FLAT();
232     dprintf(3, "set_address %p\n", cntl);
233     if (cntl->maxaddr >= USB_MAXADDR)
234         return NULL;
235
236     struct usb_pipe *defpipe = cntl->defaultpipe;
237     if (!defpipe) {
238         // Create a pipe for the default address.
239         struct usb_pipe dummy;
240         memset(&dummy, 0, sizeof(dummy));
241         dummy.cntl = cntl;
242         dummy.type = cntl->type;
243         dummy.maxpacket = 8;
244         cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
245         if (!defpipe)
246             return NULL;
247     }
248     defpipe->lowspeed = lowspeed;
249
250     msleep(USB_TIME_RSTRCY);
251
252     struct usb_ctrlrequest req;
253     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
254     req.bRequest = USB_REQ_SET_ADDRESS;
255     req.wValue = cntl->maxaddr + 1;
256     req.wIndex = 0;
257     req.wLength = 0;
258     int ret = send_default_control(defpipe, &req, NULL);
259     if (ret)
260         return NULL;
261
262     msleep(USB_TIME_SETADDR_RECOVERY);
263
264     cntl->maxaddr++;
265     defpipe->devaddr = cntl->maxaddr;
266     struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
267     defpipe->devaddr = 0;
268     return pipe;
269 }
270
271 // Called for every found device - see if a driver is available for
272 // this device and do setup if so.
273 int
274 configure_usb_device(struct usb_pipe *pipe)
275 {
276     ASSERT32FLAT();
277     dprintf(3, "config_usb: %p\n", pipe);
278
279     // Set the max packet size for endpoint 0 of this device.
280     struct usb_device_descriptor dinfo;
281     int ret = get_device_info8(pipe, &dinfo);
282     if (ret)
283         return 0;
284     dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
285             , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
286             , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
287     if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
288         return 0;
289     pipe->maxpacket = dinfo.bMaxPacketSize0;
290
291     // Get configuration
292     struct usb_config_descriptor *config = get_device_config(pipe);
293     if (!config)
294         return 0;
295
296     // Determine if a driver exists for this device - only look at the
297     // first interface of the first configuration.
298     struct usb_interface_descriptor *iface = (void*)(&config[1]);
299     if ((iface->bInterfaceClass != USB_CLASS_HID
300          || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
301          || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
302         && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
303         && (iface->bInterfaceClass != USB_CLASS_HUB))
304         // Not a supported device.
305         goto fail;
306
307     // Set the configuration.
308     ret = set_configuration(pipe, config->bConfigurationValue);
309     if (ret)
310         goto fail;
311
312     // Configure driver.
313     int imax = (void*)config + config->wTotalLength - (void*)iface;
314     if (iface->bInterfaceClass == USB_CLASS_HUB)
315         ret = usb_hub_init(pipe);
316     else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
317         ret = usb_msc_init(pipe, iface, imax);
318     else
319         ret = usb_keyboard_init(pipe, iface, imax);
320     if (ret)
321         goto fail;
322
323     free(config);
324     return 1;
325 fail:
326     free(config);
327     return 0;
328 }
329
330 void
331 usb_setup(void)
332 {
333     ASSERT32FLAT();
334     if (! CONFIG_USB)
335         return;
336
337     dprintf(3, "init usb\n");
338
339     usb_keyboard_setup();
340
341     // Look for USB controllers
342     int count = 0;
343     int bdf, max;
344     foreachpci(bdf, max) {
345         u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
346
347         if (code >> 8 != PCI_CLASS_SERIAL_USB)
348             continue;
349
350         if (code == PCI_CLASS_SERIAL_USB_UHCI)
351             uhci_init(bdf, count);
352         else if (code == PCI_CLASS_SERIAL_USB_OHCI)
353             ohci_init(bdf, count);
354         else
355             continue;
356
357         count++;
358     }
359 }