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