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