blob: 75412f91ae622707e03b22e07ddaae2e483208e9 [file] [log] [blame]
Kevin O'Connor114592f2009-09-28 21:32:08 -04001// Main code for handling USB controllers and devices.
2//
Kevin O'Connorec443ff2013-12-05 18:43:20 -05003// Copyright (C) 2009-2013 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor114592f2009-09-28 21:32:08 -04004//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04007#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor114592f2009-09-28 21:32:08 -04008#include "config.h" // CONFIG_*
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04009#include "malloc.h" // free
10#include "output.h" // dprintf
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "string.h" // memset
12#include "usb.h" // struct usb_s
Kevin O'Connord83c87b2013-01-21 01:14:12 -050013#include "usb-ehci.h" // ehci_setup
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020014#include "usb-xhci.h" // xhci_setup
Kevin O'Connor114592f2009-09-28 21:32:08 -040015#include "usb-hid.h" // usb_keyboard_setup
Kevin O'Connord83c87b2013-01-21 01:14:12 -050016#include "usb-hub.h" // usb_hub_setup
17#include "usb-msc.h" // usb_msc_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040018#include "usb-ohci.h" // ohci_setup
Kevin O'Connord83c87b2013-01-21 01:14:12 -050019#include "usb-uas.h" // usb_uas_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "usb-uhci.h" // uhci_setup
21#include "util.h" // msleep
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040022#include "x86.h" // __fls
Kevin O'Connor114592f2009-09-28 21:32:08 -040023
Kevin O'Connor3c160dd2010-02-17 23:06:52 -050024
25/****************************************************************
26 * Controller function wrappers
27 ****************************************************************/
28
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -040029// Allocate, update, or free a usb pipe.
30static struct usb_pipe *
31usb_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe
32 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor54bf2072012-03-09 07:52:33 -050033{
34 switch (usbdev->hub->cntl->type) {
Kevin O'Connor59f02832009-10-12 10:09:15 -040035 default:
36 case USB_TYPE_UHCI:
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -040037 return uhci_realloc_pipe(usbdev, pipe, epdesc);
Kevin O'Connor59f02832009-10-12 10:09:15 -040038 case USB_TYPE_OHCI:
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -040039 return ohci_realloc_pipe(usbdev, pipe, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -050040 case USB_TYPE_EHCI:
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -040041 return ehci_realloc_pipe(usbdev, pipe, epdesc);
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020042 case USB_TYPE_XHCI:
Kevin O'Connorc427def2014-10-16 13:23:08 -040043 return xhci_realloc_pipe(usbdev, pipe, epdesc);
Gerd Hoffmann63cbab12013-08-21 13:59:21 +020044 }
45}
46
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050047// Send a message on a control pipe using the default control descriptor.
48static int
Kevin O'Connor20c9be32014-10-16 11:55:16 -040049usb_send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
50 , void *data, int datasize)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050051{
52 ASSERT32FLAT();
Kevin O'Connor4547eb92010-02-28 02:17:28 -050053 switch (pipe->type) {
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050054 default:
55 case USB_TYPE_UHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040056 return uhci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050057 case USB_TYPE_OHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040058 return ohci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -050059 case USB_TYPE_EHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040060 return ehci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020061 case USB_TYPE_XHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040062 return xhci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor59f02832009-10-12 10:09:15 -040063 }
Kevin O'Connor114592f2009-09-28 21:32:08 -040064}
65
Kevin O'Connor7149fc82010-02-17 23:24:42 -050066int
Kevin O'Connor4547eb92010-02-28 02:17:28 -050067usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
Kevin O'Connor7149fc82010-02-17 23:24:42 -050068{
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -040069 switch (GET_LOWFLAT(pipe_fl->type)) {
Kevin O'Connor7149fc82010-02-17 23:24:42 -050070 default:
71 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -050072 return uhci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor7149fc82010-02-17 23:24:42 -050073 case USB_TYPE_OHCI:
Kevin O'Connor746d0a72014-12-29 13:06:23 -050074 if (MODESEGMENT)
75 return -1;
Kevin O'Connor2f968002010-09-19 22:10:08 -040076 return ohci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -050077 case USB_TYPE_EHCI:
78 return ehci_send_bulk(pipe_fl, dir, data, datasize);
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020079 case USB_TYPE_XHCI:
Kevin O'Connorde30dad2013-12-30 22:09:04 -050080 if (MODESEGMENT)
81 return -1;
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020082 return xhci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor7149fc82010-02-17 23:24:42 -050083 }
84}
85
Kevin O'Connorac7eb5e2012-05-28 14:42:16 -040086int
Kevin O'Connor4547eb92010-02-28 02:17:28 -050087usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -040088{
Kevin O'Connorde30dad2013-12-30 22:09:04 -050089 ASSERT16();
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -040090 switch (GET_LOWFLAT(pipe_fl->type)) {
Kevin O'Connor59f02832009-10-12 10:09:15 -040091 default:
92 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -050093 return uhci_poll_intr(pipe_fl, data);
Kevin O'Connor59f02832009-10-12 10:09:15 -040094 case USB_TYPE_OHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -050095 return ohci_poll_intr(pipe_fl, data);
Kevin O'Connor190cc622010-03-09 19:43:52 -050096 case USB_TYPE_EHCI:
97 return ehci_poll_intr(pipe_fl, data);
Kevin O'Connorde30dad2013-12-30 22:09:04 -050098 case USB_TYPE_XHCI: ;
99 extern void _cfunc32flat_xhci_poll_intr(void);
100 return call32_params(_cfunc32flat_xhci_poll_intr, (u32)pipe_fl
101 , (u32)MAKE_FLATPTR(GET_SEG(SS), (u32)data), 0, -1);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400102 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400103}
104
Kevin O'Connorde30dad2013-12-30 22:09:04 -0500105int usb_32bit_pipe(struct usb_pipe *pipe_fl)
106{
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500107 return (CONFIG_USB_XHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_XHCI)
108 || (CONFIG_USB_OHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_OHCI);
Kevin O'Connorde30dad2013-12-30 22:09:04 -0500109}
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500110
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -0400111
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500112/****************************************************************
113 * Helper functions
114 ****************************************************************/
115
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -0400116// Allocate a usb pipe.
117struct usb_pipe *
118usb_alloc_pipe(struct usbdevice_s *usbdev
119 , struct usb_endpoint_descriptor *epdesc)
120{
121 return usb_realloc_pipe(usbdev, NULL, epdesc);
122}
123
124// Free an allocated control or bulk pipe.
125void
126usb_free_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe)
127{
128 if (!pipe)
129 return;
130 usb_realloc_pipe(usbdev, pipe, NULL);
131}
132
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500133// Send a message to the default control pipe of a device.
134int
Kevin O'Connor222bad42014-10-16 12:11:12 -0400135usb_send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
136 , void *data)
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500137{
Kevin O'Connor20c9be32014-10-16 11:55:16 -0400138 return usb_send_control(pipe, req->bRequestType & USB_DIR_IN
139 , req, sizeof(*req), data, req->wLength);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500140}
141
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400142// Check if a pipe for a given controller is on the freelist
143int
144usb_is_freelist(struct usb_s *cntl, struct usb_pipe *pipe)
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500145{
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400146 return pipe->cntl != cntl;
147}
148
149// Add a pipe to the controller's freelist
150void
151usb_add_freelist(struct usb_pipe *pipe)
152{
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500153 if (!pipe)
154 return;
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500155 struct usb_s *cntl = pipe->cntl;
156 pipe->freenext = cntl->freelist;
157 cntl->freelist = pipe;
158}
159
160// Check for an available pipe on the freelist.
161struct usb_pipe *
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400162usb_get_freelist(struct usb_s *cntl, u8 eptype)
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500163{
164 struct usb_pipe **pfree = &cntl->freelist;
165 for (;;) {
166 struct usb_pipe *pipe = *pfree;
167 if (!pipe)
168 return NULL;
169 if (pipe->eptype == eptype) {
170 *pfree = pipe->freenext;
171 return pipe;
172 }
173 pfree = &pipe->freenext;
174 }
175}
176
177// Fill "pipe" endpoint info from an endpoint descriptor.
178void
179usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
180 , struct usb_endpoint_descriptor *epdesc)
181{
182 pipe->cntl = usbdev->hub->cntl;
183 pipe->type = usbdev->hub->cntl->type;
184 pipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
185 pipe->devaddr = usbdev->devaddr;
186 pipe->speed = usbdev->speed;
187 pipe->maxpacket = epdesc->wMaxPacketSize;
188 pipe->eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500189}
190
191// Find the exponential period of the requested interrupt end point.
192int
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400193usb_get_period(struct usbdevice_s *usbdev
194 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500195{
196 int period = epdesc->bInterval;
197 if (usbdev->speed != USB_HIGHSPEED)
198 return (period <= 0) ? 0 : __fls(period);
199 return (period <= 4) ? 0 : period - 4;
200}
201
Kevin O'Connor88fa2272014-06-14 12:25:17 -0400202// Maximum time (in ms) a data transfer should take
203int
204usb_xfer_time(struct usb_pipe *pipe, int datalen)
205{
206 // Use the maximum command time (5 seconds), except for
207 // set_address commands where we don't want to stall the boot if
208 // the device doesn't actually exist. Add 100ms to account for
209 // any controller delays.
Kevin O'Connor0806e9e2014-09-09 18:24:00 -0400210 if (!GET_LOWFLAT(pipe->devaddr))
Kevin O'Connor88fa2272014-06-14 12:25:17 -0400211 return USB_TIME_STATUS + 100;
212 return USB_TIME_COMMAND + 100;
213}
214
Kevin O'Connor674f1402014-10-16 12:08:00 -0400215// Find the first endpoint of a given type in an interface description.
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500216struct usb_endpoint_descriptor *
Kevin O'Connor674f1402014-10-16 12:08:00 -0400217usb_find_desc(struct usbdevice_s *usbdev, int type, int dir)
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500218{
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500219 struct usb_endpoint_descriptor *epdesc = (void*)&usbdev->iface[1];
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500220 for (;;) {
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500221 if ((void*)epdesc >= (void*)usbdev->iface + usbdev->imax
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500222 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
223 return NULL;
224 }
225 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
226 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
227 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
228 return epdesc;
229 epdesc = (void*)epdesc + epdesc->bLength;
230 }
231}
232
Kevin O'Connor114592f2009-09-28 21:32:08 -0400233// Get the first 8 bytes of the device descriptor.
234static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500235get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400236{
237 struct usb_ctrlrequest req;
238 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
239 req.bRequest = USB_REQ_GET_DESCRIPTOR;
240 req.wValue = USB_DT_DEVICE<<8;
241 req.wIndex = 0;
242 req.wLength = 8;
Kevin O'Connor222bad42014-10-16 12:11:12 -0400243 return usb_send_default_control(pipe, &req, dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400244}
245
246static struct usb_config_descriptor *
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500247get_device_config(struct usb_pipe *pipe)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400248{
249 struct usb_config_descriptor cfg;
250
251 struct usb_ctrlrequest req;
252 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
253 req.bRequest = USB_REQ_GET_DESCRIPTOR;
254 req.wValue = USB_DT_CONFIG<<8;
255 req.wIndex = 0;
256 req.wLength = sizeof(cfg);
Kevin O'Connor222bad42014-10-16 12:11:12 -0400257 int ret = usb_send_default_control(pipe, &req, &cfg);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400258 if (ret)
259 return NULL;
260
261 void *config = malloc_tmphigh(cfg.wTotalLength);
262 if (!config)
263 return NULL;
264 req.wLength = cfg.wTotalLength;
Kevin O'Connor222bad42014-10-16 12:11:12 -0400265 ret = usb_send_default_control(pipe, &req, config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400266 if (ret)
267 return NULL;
268 //hexdump(config, cfg.wTotalLength);
269 return config;
270}
271
Kevin O'Connor114592f2009-09-28 21:32:08 -0400272static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500273set_configuration(struct usb_pipe *pipe, u16 val)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400274{
275 struct usb_ctrlrequest req;
276 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
277 req.bRequest = USB_REQ_SET_CONFIGURATION;
278 req.wValue = val;
279 req.wIndex = 0;
280 req.wLength = 0;
Kevin O'Connor222bad42014-10-16 12:11:12 -0400281 return usb_send_default_control(pipe, &req, NULL);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400282}
283
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500284
285/****************************************************************
286 * Initialization and enumeration
287 ****************************************************************/
288
Kevin O'Connor3de73822014-01-26 17:28:04 -0500289static const int speed_to_ctlsize[] = {
290 [ USB_FULLSPEED ] = 8,
291 [ USB_LOWSPEED ] = 8,
292 [ USB_HIGHSPEED ] = 64,
293 [ USB_SUPERSPEED ] = 512,
294};
295
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500296// Assign an address to a device in the default state on the given
297// controller.
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500298static int
299usb_set_address(struct usbdevice_s *usbdev)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400300{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500301 ASSERT32FLAT();
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500302 struct usb_s *cntl = usbdev->hub->cntl;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500303 dprintf(3, "set_address %p\n", cntl);
304 if (cntl->maxaddr >= USB_MAXADDR)
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500305 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400306
Kevin O'Connor4ea2fa92013-12-27 19:25:46 -0500307 msleep(USB_TIME_RSTRCY);
308
Kevin O'Connor92650f22012-03-05 22:03:03 -0500309 // Create a pipe for the default address.
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500310 struct usb_endpoint_descriptor epdesc = {
Kevin O'Connor3de73822014-01-26 17:28:04 -0500311 .wMaxPacketSize = speed_to_ctlsize[usbdev->speed],
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500312 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
313 };
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500314 usbdev->defpipe = usb_alloc_pipe(usbdev, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500315 if (!usbdev->defpipe)
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500316 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500317
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500318 // Send set_address command.
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500319 struct usb_ctrlrequest req;
320 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
321 req.bRequest = USB_REQ_SET_ADDRESS;
322 req.wValue = cntl->maxaddr + 1;
323 req.wIndex = 0;
324 req.wLength = 0;
Kevin O'Connor222bad42014-10-16 12:11:12 -0400325 int ret = usb_send_default_control(usbdev->defpipe, &req, NULL);
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200326 if (ret) {
Kevin O'Connorf3329432014-10-16 11:59:45 -0400327 usb_free_pipe(usbdev, usbdev->defpipe);
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500328 return -1;
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200329 }
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500330
331 msleep(USB_TIME_SETADDR_RECOVERY);
332
333 cntl->maxaddr++;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500334 usbdev->devaddr = cntl->maxaddr;
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -0400335 usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500336 if (!usbdev->defpipe)
337 return -1;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500338 return 0;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500339}
340
341// Called for every found device - see if a driver is available for
342// this device and do setup if so.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400343static int
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500344configure_usb_device(struct usbdevice_s *usbdev)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500345{
346 ASSERT32FLAT();
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500347 dprintf(3, "config_usb: %p\n", usbdev->defpipe);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500348
349 // Set the max packet size for endpoint 0 of this device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400350 struct usb_device_descriptor dinfo;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500351 int ret = get_device_info8(usbdev->defpipe, &dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400352 if (ret)
353 return 0;
Kevin O'Connor828ef122014-01-24 14:34:00 -0500354 u16 maxpacket = dinfo.bMaxPacketSize0;
355 if (dinfo.bcdUSB >= 0x0300)
356 maxpacket = 1 << dinfo.bMaxPacketSize0;
357 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%d\n"
Kevin O'Connor114592f2009-09-28 21:32:08 -0400358 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
Kevin O'Connor828ef122014-01-24 14:34:00 -0500359 , dinfo.bDeviceProtocol, maxpacket);
360 if (maxpacket < 8)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400361 return 0;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500362 struct usb_endpoint_descriptor epdesc = {
Kevin O'Connor828ef122014-01-24 14:34:00 -0500363 .wMaxPacketSize = maxpacket,
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500364 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
365 };
Kevin O'Connor54a1c6d2014-10-16 14:03:39 -0400366 usbdev->defpipe = usb_realloc_pipe(usbdev, usbdev->defpipe, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500367 if (!usbdev->defpipe)
368 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400369
370 // Get configuration
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500371 struct usb_config_descriptor *config = get_device_config(usbdev->defpipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400372 if (!config)
373 return 0;
374
375 // Determine if a driver exists for this device - only look at the
376 // first interface of the first configuration.
377 struct usb_interface_descriptor *iface = (void*)(&config[1]);
Kevin O'Connor0e885762010-05-01 22:14:40 -0400378 if (iface->bInterfaceClass != USB_CLASS_HID
379 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
380 && iface->bInterfaceClass != USB_CLASS_HUB)
Kevin O'Connor54671c12010-02-15 18:58:12 -0500381 // Not a supported device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400382 goto fail;
383
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500384 // Set the configuration.
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500385 ret = set_configuration(usbdev->defpipe, config->bConfigurationValue);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400386 if (ret)
387 goto fail;
388
389 // Configure driver.
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500390 usbdev->config = config;
391 usbdev->iface = iface;
392 usbdev->imax = (void*)config + config->wTotalLength - (void*)iface;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500393 if (iface->bInterfaceClass == USB_CLASS_HUB)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500394 ret = usb_hub_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200395 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) {
396 if (iface->bInterfaceProtocol == US_PR_BULK)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500397 ret = usb_msc_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200398 if (iface->bInterfaceProtocol == US_PR_UAS)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500399 ret = usb_uas_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200400 } else
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500401 ret = usb_hid_setup(usbdev);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400402 if (ret)
403 goto fail;
404
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400405 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400406 return 1;
407fail:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400408 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400409 return 0;
410}
411
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400412static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500413usb_hub_port_setup(void *data)
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400414{
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500415 struct usbdevice_s *usbdev = data;
416 struct usbhub_s *hub = usbdev->hub;
417 u32 port = usbdev->port;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400418
Kevin O'Connor0f681302014-09-10 11:33:01 -0400419 for (;;) {
420 // Detect if device present (and possibly start reset)
421 int ret = hub->op->detect(hub, port);
422 if (ret > 0)
423 // Device connected.
424 break;
425 if (ret < 0 || timer_check(hub->detectend))
426 // No device found.
427 goto done;
428 msleep(5);
429 }
430
431 // XXX - wait USB_TIME_ATTDB time?
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400432
433 // Reset port and determine device speed
434 mutex_lock(&hub->cntl->resetlock);
Kevin O'Connor0f681302014-09-10 11:33:01 -0400435 int ret = hub->op->reset(hub, port);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400436 if (ret < 0)
437 // Reset failed
438 goto resetfail;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500439 usbdev->speed = ret;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400440
441 // Set address of port
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500442 ret = usb_set_address(usbdev);
443 if (ret) {
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400444 hub->op->disconnect(hub, port);
445 goto resetfail;
446 }
447 mutex_unlock(&hub->cntl->resetlock);
448
449 // Configure the device
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500450 int count = configure_usb_device(usbdev);
Kevin O'Connorf3329432014-10-16 11:59:45 -0400451 usb_free_pipe(usbdev, usbdev->defpipe);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400452 if (!count)
453 hub->op->disconnect(hub, port);
454 hub->devcount += count;
455done:
456 hub->threads--;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500457 free(usbdev);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400458 return;
459
460resetfail:
461 mutex_unlock(&hub->cntl->resetlock);
462 goto done;
463}
464
465void
466usb_enumerate(struct usbhub_s *hub)
467{
468 u32 portcount = hub->portcount;
469 hub->threads = portcount;
Kevin O'Connor0f681302014-09-10 11:33:01 -0400470 hub->detectend = timer_calc(USB_TIME_SIGATT);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400471
472 // Launch a thread for every port.
473 int i;
474 for (i=0; i<portcount; i++) {
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500475 struct usbdevice_s *usbdev = malloc_tmphigh(sizeof(*usbdev));
476 if (!usbdev) {
477 warn_noalloc();
478 continue;
479 }
480 memset(usbdev, 0, sizeof(*usbdev));
481 usbdev->hub = hub;
482 usbdev->port = i;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500483 run_thread(usb_hub_port_setup, usbdev);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400484 }
485
486 // Wait for threads to complete.
487 while (hub->threads)
488 yield();
489}
490
Kevin O'Connor114592f2009-09-28 21:32:08 -0400491void
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500492__usb_setup(void *data)
493{
494 dprintf(3, "init usb\n");
495 xhci_setup();
496 ehci_setup();
497 uhci_setup();
498 ohci_setup();
499}
500
501void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500502usb_setup(void)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400503{
Kevin O'Connor59c75742010-02-13 18:49:24 -0500504 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400505 if (! CONFIG_USB)
506 return;
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500507 run_thread(__usb_setup, NULL);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400508}