Initial support for USB hubs.
[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.h" // struct usb_s
17 #include "biosvar.h" // GET_GLOBAL
18
19 struct usb_s USBControllers[16] VAR16VISIBLE;
20
21 static int
22 send_control(u32 endp, int dir, const void *cmd, int cmdsize
23              , void *data, int datasize)
24 {
25     struct usb_s *cntl = endp2cntl(endp);
26     switch (cntl->type) {
27     default:
28     case USB_TYPE_UHCI:
29         return uhci_control(endp, dir, cmd, cmdsize, data, datasize);
30     case USB_TYPE_OHCI:
31         return ohci_control(endp, dir, cmd, cmdsize, data, datasize);
32     }
33 }
34
35 struct usb_pipe *
36 alloc_intr_pipe(u32 endp, int period)
37 {
38     struct usb_s *cntl = endp2cntl(endp);
39     // Find the exponential period of the requested time.
40     if (period <= 0)
41         period = 1;
42     int frameexp = __fls(period);
43     switch (cntl->type) {
44     default:
45     case USB_TYPE_UHCI:
46         return uhci_alloc_intr_pipe(endp, frameexp);
47     case USB_TYPE_OHCI:
48         return ohci_alloc_intr_pipe(endp, frameexp);
49     }
50 }
51
52 int noinline
53 usb_poll_intr(struct usb_pipe *pipe, void *data)
54 {
55     u32 endp = GET_FLATPTR(pipe->endp);
56     struct usb_s *cntl = endp2cntl(endp);
57     switch (GET_GLOBAL(cntl->type)) {
58     default:
59     case USB_TYPE_UHCI:
60         return uhci_poll_intr(pipe, data);
61     case USB_TYPE_OHCI:
62         return ohci_poll_intr(pipe, data);
63     }
64 }
65
66 int
67 send_default_control(u32 endp, const struct usb_ctrlrequest *req, void *data)
68 {
69     return send_control(endp, req->bRequestType & USB_DIR_IN
70                         , req, sizeof(*req), data, req->wLength);
71 }
72
73 // Get the first 8 bytes of the device descriptor.
74 static int
75 get_device_info8(struct usb_device_descriptor *dinfo, u32 endp)
76 {
77     struct usb_ctrlrequest req;
78     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
79     req.bRequest = USB_REQ_GET_DESCRIPTOR;
80     req.wValue = USB_DT_DEVICE<<8;
81     req.wIndex = 0;
82     req.wLength = 8;
83     return send_default_control(endp, &req, dinfo);
84 }
85
86 static struct usb_config_descriptor *
87 get_device_config(u32 endp)
88 {
89     struct usb_config_descriptor cfg;
90
91     struct usb_ctrlrequest req;
92     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
93     req.bRequest = USB_REQ_GET_DESCRIPTOR;
94     req.wValue = USB_DT_CONFIG<<8;
95     req.wIndex = 0;
96     req.wLength = sizeof(cfg);
97     int ret = send_default_control(endp, &req, &cfg);
98     if (ret)
99         return NULL;
100
101     void *config = malloc_tmphigh(cfg.wTotalLength);
102     if (!config)
103         return NULL;
104     req.wLength = cfg.wTotalLength;
105     ret = send_default_control(endp, &req, config);
106     if (ret)
107         return NULL;
108     //hexdump(config, cfg.wTotalLength);
109     return config;
110 }
111
112 static u32
113 set_address(u32 endp)
114 {
115     dprintf(3, "set_address %x\n", endp);
116     struct usb_s *cntl = endp2cntl(endp);
117     if (cntl->maxaddr >= USB_MAXADDR)
118         return 0;
119
120     struct usb_ctrlrequest req;
121     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
122     req.bRequest = USB_REQ_SET_ADDRESS;
123     req.wValue = cntl->maxaddr + 1;
124     req.wIndex = 0;
125     req.wLength = 0;
126     int ret = send_default_control(endp, &req, NULL);
127     if (ret)
128         return 0;
129     msleep(USB_TIME_SETADDR_RECOVERY);
130
131     cntl->maxaddr++;
132     return mkendp(cntl, cntl->maxaddr, 0, endp2speed(endp), endp2maxsize(endp));
133 }
134
135 static int
136 set_configuration(u32 endp, u16 val)
137 {
138     struct usb_ctrlrequest req;
139     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
140     req.bRequest = USB_REQ_SET_CONFIGURATION;
141     req.wValue = val;
142     req.wIndex = 0;
143     req.wLength = 0;
144     return send_default_control(endp, &req, NULL);
145 }
146
147 // Called for every found device - see if a driver is available for
148 // this device and do setup if so.
149 int
150 configure_usb_device(struct usb_s *cntl, int lowspeed)
151 {
152     dprintf(3, "config_usb: %p %d\n", cntl, lowspeed);
153
154     // Get device info
155     u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
156     struct usb_device_descriptor dinfo;
157     int ret = get_device_info8(&dinfo, endp);
158     if (ret)
159         return 0;
160     dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
161             , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
162             , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
163     if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
164         return 0;
165     endp = mkendp(cntl, 0, 0, lowspeed, dinfo.bMaxPacketSize0);
166
167     // Get configuration
168     struct usb_config_descriptor *config = get_device_config(endp);
169     if (!config)
170         return 0;
171
172     // Determine if a driver exists for this device - only look at the
173     // first interface of the first configuration.
174     struct usb_interface_descriptor *iface = (void*)(&config[1]);
175     if ((iface->bInterfaceClass != USB_CLASS_HID
176          || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
177          || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
178         && (iface->bInterfaceClass != USB_CLASS_HUB))
179         // Not a supported device.
180         goto fail;
181
182     // Set the address and configure device.
183     endp = set_address(endp);
184     if (!endp)
185         goto fail;
186     ret = set_configuration(endp, config->bConfigurationValue);
187     if (ret)
188         goto fail;
189
190     // Configure driver.
191     if (iface->bInterfaceClass == USB_CLASS_HUB) {
192         free(config);
193         return usb_hub_init(endp);
194     }
195     ret = usb_keyboard_init(endp, iface, ((void*)config + config->wTotalLength
196                                           - (void*)iface));
197     if (ret)
198         goto fail;
199
200     free(config);
201     return 1;
202 fail:
203     free(config);
204     return 0;
205 }
206
207 void
208 usb_setup(void)
209 {
210     ASSERT32FLAT();
211     if (! CONFIG_USB)
212         return;
213
214     dprintf(3, "init usb\n");
215
216     usb_keyboard_setup();
217
218     // Look for USB controllers
219     int count = 0;
220     int bdf, max;
221     foreachpci(bdf, max) {
222         u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
223
224         if (code >> 8 != PCI_CLASS_SERIAL_USB)
225             continue;
226
227         struct usb_s *cntl = &USBControllers[count];
228         cntl->bdf = bdf;
229
230         if (code == PCI_CLASS_SERIAL_USB_UHCI)
231             run_thread(uhci_init, cntl);
232         else if (code == PCI_CLASS_SERIAL_USB_OHCI)
233             run_thread(ohci_init, cntl);
234         else
235             continue;
236
237         count++;
238         if (count >= ARRAY_SIZE(USBControllers))
239             break;
240     }
241 }