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