blob: cfeeb8ee89bb9303bbca10fa977914e2f6030575 [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'Connor54bf2072012-03-09 07:52:33 -050029// Allocate an async pipe (control or bulk).
30struct usb_pipe *
Kevin O'Connorc3d96c22012-03-10 09:03:25 -050031usb_alloc_pipe(struct usbdevice_s *usbdev
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'Connorc3d96c22012-03-10 09:03:25 -050037 return uhci_alloc_pipe(usbdev, epdesc);
Kevin O'Connor59f02832009-10-12 10:09:15 -040038 case USB_TYPE_OHCI:
Kevin O'Connorc3d96c22012-03-10 09:03:25 -050039 return ohci_alloc_pipe(usbdev, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -050040 case USB_TYPE_EHCI:
Kevin O'Connorc3d96c22012-03-10 09:03:25 -050041 return ehci_alloc_pipe(usbdev, epdesc);
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020042 case USB_TYPE_XHCI:
43 return xhci_alloc_pipe(usbdev, epdesc);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050044 }
45}
46
Gerd Hoffmann63cbab12013-08-21 13:59:21 +020047// Update an pipe (used for control only)
48struct usb_pipe *
49usb_update_pipe(struct usbdevice_s *usbdev, struct usb_pipe *pipe
50 , struct usb_endpoint_descriptor *epdesc)
51{
52 switch (usbdev->hub->cntl->type) {
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020053 case USB_TYPE_XHCI:
54 return xhci_update_pipe(usbdev, pipe, epdesc);
Gerd Hoffmann63cbab12013-08-21 13:59:21 +020055 default:
56 free_pipe(pipe);
57 return usb_alloc_pipe(usbdev, epdesc);
58 }
59}
60
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050061// Send a message on a control pipe using the default control descriptor.
62static int
Kevin O'Connor20c9be32014-10-16 11:55:16 -040063usb_send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
64 , void *data, int datasize)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050065{
66 ASSERT32FLAT();
Kevin O'Connor4547eb92010-02-28 02:17:28 -050067 switch (pipe->type) {
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050068 default:
69 case USB_TYPE_UHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040070 return uhci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050071 case USB_TYPE_OHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040072 return ohci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -050073 case USB_TYPE_EHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040074 return ehci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020075 case USB_TYPE_XHCI:
Kevin O'Connor20c9be32014-10-16 11:55:16 -040076 return xhci_send_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor59f02832009-10-12 10:09:15 -040077 }
Kevin O'Connor114592f2009-09-28 21:32:08 -040078}
79
Kevin O'Connor7149fc82010-02-17 23:24:42 -050080int
Kevin O'Connor4547eb92010-02-28 02:17:28 -050081usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
Kevin O'Connor7149fc82010-02-17 23:24:42 -050082{
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -040083 switch (GET_LOWFLAT(pipe_fl->type)) {
Kevin O'Connor7149fc82010-02-17 23:24:42 -050084 default:
85 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -050086 return uhci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor7149fc82010-02-17 23:24:42 -050087 case USB_TYPE_OHCI:
Kevin O'Connor2f968002010-09-19 22:10:08 -040088 return ohci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -050089 case USB_TYPE_EHCI:
90 return ehci_send_bulk(pipe_fl, dir, data, datasize);
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020091 case USB_TYPE_XHCI:
Kevin O'Connorde30dad2013-12-30 22:09:04 -050092 if (MODESEGMENT)
93 return -1;
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020094 return xhci_send_bulk(pipe_fl, dir, data, datasize);
Kevin O'Connor7149fc82010-02-17 23:24:42 -050095 }
96}
97
Kevin O'Connorac7eb5e2012-05-28 14:42:16 -040098int
Kevin O'Connor4547eb92010-02-28 02:17:28 -050099usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400100{
Kevin O'Connorde30dad2013-12-30 22:09:04 -0500101 ASSERT16();
Kevin O'Connor1e3bd4f2012-05-24 23:56:19 -0400102 switch (GET_LOWFLAT(pipe_fl->type)) {
Kevin O'Connor59f02832009-10-12 10:09:15 -0400103 default:
104 case USB_TYPE_UHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500105 return uhci_poll_intr(pipe_fl, data);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400106 case USB_TYPE_OHCI:
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500107 return ohci_poll_intr(pipe_fl, data);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500108 case USB_TYPE_EHCI:
109 return ehci_poll_intr(pipe_fl, data);
Kevin O'Connorde30dad2013-12-30 22:09:04 -0500110 case USB_TYPE_XHCI: ;
111 extern void _cfunc32flat_xhci_poll_intr(void);
112 return call32_params(_cfunc32flat_xhci_poll_intr, (u32)pipe_fl
113 , (u32)MAKE_FLATPTR(GET_SEG(SS), (u32)data), 0, -1);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400114 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400115}
116
Kevin O'Connorde30dad2013-12-30 22:09:04 -0500117int usb_32bit_pipe(struct usb_pipe *pipe_fl)
118{
119 return CONFIG_USB_XHCI && GET_LOWFLAT(pipe_fl->type) == USB_TYPE_XHCI;
120}
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500121
122/****************************************************************
123 * Helper functions
124 ****************************************************************/
125
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500126// Send a message to the default control pipe of a device.
127int
128send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
129 , void *data)
130{
Kevin O'Connor20c9be32014-10-16 11:55:16 -0400131 return usb_send_control(pipe, req->bRequestType & USB_DIR_IN
132 , req, sizeof(*req), data, req->wLength);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500133}
134
135// Free an allocated control or bulk pipe.
136void
137free_pipe(struct usb_pipe *pipe)
138{
139 ASSERT32FLAT();
140 if (!pipe)
141 return;
142 // Add to controller's free list.
143 struct usb_s *cntl = pipe->cntl;
144 pipe->freenext = cntl->freelist;
145 cntl->freelist = pipe;
146}
147
148// Check for an available pipe on the freelist.
149struct usb_pipe *
150usb_getFreePipe(struct usb_s *cntl, u8 eptype)
151{
152 struct usb_pipe **pfree = &cntl->freelist;
153 for (;;) {
154 struct usb_pipe *pipe = *pfree;
155 if (!pipe)
156 return NULL;
157 if (pipe->eptype == eptype) {
158 *pfree = pipe->freenext;
159 return pipe;
160 }
161 pfree = &pipe->freenext;
162 }
163}
164
165// Fill "pipe" endpoint info from an endpoint descriptor.
166void
167usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
168 , struct usb_endpoint_descriptor *epdesc)
169{
170 pipe->cntl = usbdev->hub->cntl;
171 pipe->type = usbdev->hub->cntl->type;
172 pipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
173 pipe->devaddr = usbdev->devaddr;
174 pipe->speed = usbdev->speed;
175 pipe->maxpacket = epdesc->wMaxPacketSize;
176 pipe->eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500177}
178
179// Find the exponential period of the requested interrupt end point.
180int
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400181usb_get_period(struct usbdevice_s *usbdev
182 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500183{
184 int period = epdesc->bInterval;
185 if (usbdev->speed != USB_HIGHSPEED)
186 return (period <= 0) ? 0 : __fls(period);
187 return (period <= 4) ? 0 : period - 4;
188}
189
Kevin O'Connor88fa2272014-06-14 12:25:17 -0400190// Maximum time (in ms) a data transfer should take
191int
192usb_xfer_time(struct usb_pipe *pipe, int datalen)
193{
194 // Use the maximum command time (5 seconds), except for
195 // set_address commands where we don't want to stall the boot if
196 // the device doesn't actually exist. Add 100ms to account for
197 // any controller delays.
Kevin O'Connor0806e9e2014-09-09 18:24:00 -0400198 if (!GET_LOWFLAT(pipe->devaddr))
Kevin O'Connor88fa2272014-06-14 12:25:17 -0400199 return USB_TIME_STATUS + 100;
200 return USB_TIME_COMMAND + 100;
201}
202
Kevin O'Connor674f1402014-10-16 12:08:00 -0400203// Find the first endpoint of a given type in an interface description.
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500204struct usb_endpoint_descriptor *
Kevin O'Connor674f1402014-10-16 12:08:00 -0400205usb_find_desc(struct usbdevice_s *usbdev, int type, int dir)
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500206{
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500207 struct usb_endpoint_descriptor *epdesc = (void*)&usbdev->iface[1];
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500208 for (;;) {
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500209 if ((void*)epdesc >= (void*)usbdev->iface + usbdev->imax
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500210 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
211 return NULL;
212 }
213 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
214 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
215 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
216 return epdesc;
217 epdesc = (void*)epdesc + epdesc->bLength;
218 }
219}
220
Kevin O'Connor114592f2009-09-28 21:32:08 -0400221// Get the first 8 bytes of the device descriptor.
222static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500223get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400224{
225 struct usb_ctrlrequest req;
226 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
227 req.bRequest = USB_REQ_GET_DESCRIPTOR;
228 req.wValue = USB_DT_DEVICE<<8;
229 req.wIndex = 0;
230 req.wLength = 8;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500231 return send_default_control(pipe, &req, dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400232}
233
234static struct usb_config_descriptor *
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500235get_device_config(struct usb_pipe *pipe)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400236{
237 struct usb_config_descriptor cfg;
238
239 struct usb_ctrlrequest req;
240 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
241 req.bRequest = USB_REQ_GET_DESCRIPTOR;
242 req.wValue = USB_DT_CONFIG<<8;
243 req.wIndex = 0;
244 req.wLength = sizeof(cfg);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500245 int ret = send_default_control(pipe, &req, &cfg);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400246 if (ret)
247 return NULL;
248
249 void *config = malloc_tmphigh(cfg.wTotalLength);
250 if (!config)
251 return NULL;
252 req.wLength = cfg.wTotalLength;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500253 ret = send_default_control(pipe, &req, config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400254 if (ret)
255 return NULL;
256 //hexdump(config, cfg.wTotalLength);
257 return config;
258}
259
Kevin O'Connor114592f2009-09-28 21:32:08 -0400260static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500261set_configuration(struct usb_pipe *pipe, u16 val)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400262{
263 struct usb_ctrlrequest req;
264 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
265 req.bRequest = USB_REQ_SET_CONFIGURATION;
266 req.wValue = val;
267 req.wIndex = 0;
268 req.wLength = 0;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500269 return send_default_control(pipe, &req, NULL);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400270}
271
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500272
273/****************************************************************
274 * Initialization and enumeration
275 ****************************************************************/
276
Kevin O'Connor3de73822014-01-26 17:28:04 -0500277static const int speed_to_ctlsize[] = {
278 [ USB_FULLSPEED ] = 8,
279 [ USB_LOWSPEED ] = 8,
280 [ USB_HIGHSPEED ] = 64,
281 [ USB_SUPERSPEED ] = 512,
282};
283
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500284// Assign an address to a device in the default state on the given
285// controller.
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500286static int
287usb_set_address(struct usbdevice_s *usbdev)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400288{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500289 ASSERT32FLAT();
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500290 struct usb_s *cntl = usbdev->hub->cntl;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500291 dprintf(3, "set_address %p\n", cntl);
292 if (cntl->maxaddr >= USB_MAXADDR)
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500293 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400294
Kevin O'Connor4ea2fa92013-12-27 19:25:46 -0500295 msleep(USB_TIME_RSTRCY);
296
Kevin O'Connor92650f22012-03-05 22:03:03 -0500297 // Create a pipe for the default address.
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500298 struct usb_endpoint_descriptor epdesc = {
Kevin O'Connor3de73822014-01-26 17:28:04 -0500299 .wMaxPacketSize = speed_to_ctlsize[usbdev->speed],
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500300 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
301 };
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500302 usbdev->defpipe = usb_alloc_pipe(usbdev, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500303 if (!usbdev->defpipe)
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500304 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500305
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500306 // Send set_address command.
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500307 struct usb_ctrlrequest req;
308 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
309 req.bRequest = USB_REQ_SET_ADDRESS;
310 req.wValue = cntl->maxaddr + 1;
311 req.wIndex = 0;
312 req.wLength = 0;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500313 int ret = send_default_control(usbdev->defpipe, &req, NULL);
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200314 if (ret) {
315 free_pipe(usbdev->defpipe);
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500316 return -1;
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200317 }
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500318
319 msleep(USB_TIME_SETADDR_RECOVERY);
320
321 cntl->maxaddr++;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500322 usbdev->devaddr = cntl->maxaddr;
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200323 usbdev->defpipe = usb_update_pipe(usbdev, usbdev->defpipe, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500324 if (!usbdev->defpipe)
325 return -1;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500326 return 0;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500327}
328
329// Called for every found device - see if a driver is available for
330// this device and do setup if so.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400331static int
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500332configure_usb_device(struct usbdevice_s *usbdev)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500333{
334 ASSERT32FLAT();
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500335 dprintf(3, "config_usb: %p\n", usbdev->defpipe);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500336
337 // Set the max packet size for endpoint 0 of this device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400338 struct usb_device_descriptor dinfo;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500339 int ret = get_device_info8(usbdev->defpipe, &dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400340 if (ret)
341 return 0;
Kevin O'Connor828ef122014-01-24 14:34:00 -0500342 u16 maxpacket = dinfo.bMaxPacketSize0;
343 if (dinfo.bcdUSB >= 0x0300)
344 maxpacket = 1 << dinfo.bMaxPacketSize0;
345 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%d\n"
Kevin O'Connor114592f2009-09-28 21:32:08 -0400346 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
Kevin O'Connor828ef122014-01-24 14:34:00 -0500347 , dinfo.bDeviceProtocol, maxpacket);
348 if (maxpacket < 8)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400349 return 0;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500350 struct usb_endpoint_descriptor epdesc = {
Kevin O'Connor828ef122014-01-24 14:34:00 -0500351 .wMaxPacketSize = maxpacket,
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500352 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
353 };
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200354 usbdev->defpipe = usb_update_pipe(usbdev, usbdev->defpipe, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500355 if (!usbdev->defpipe)
356 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400357
358 // Get configuration
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500359 struct usb_config_descriptor *config = get_device_config(usbdev->defpipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400360 if (!config)
361 return 0;
362
363 // Determine if a driver exists for this device - only look at the
364 // first interface of the first configuration.
365 struct usb_interface_descriptor *iface = (void*)(&config[1]);
Kevin O'Connor0e885762010-05-01 22:14:40 -0400366 if (iface->bInterfaceClass != USB_CLASS_HID
367 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
368 && iface->bInterfaceClass != USB_CLASS_HUB)
Kevin O'Connor54671c12010-02-15 18:58:12 -0500369 // Not a supported device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400370 goto fail;
371
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500372 // Set the configuration.
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500373 ret = set_configuration(usbdev->defpipe, config->bConfigurationValue);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400374 if (ret)
375 goto fail;
376
377 // Configure driver.
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500378 usbdev->config = config;
379 usbdev->iface = iface;
380 usbdev->imax = (void*)config + config->wTotalLength - (void*)iface;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500381 if (iface->bInterfaceClass == USB_CLASS_HUB)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500382 ret = usb_hub_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200383 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) {
384 if (iface->bInterfaceProtocol == US_PR_BULK)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500385 ret = usb_msc_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200386 if (iface->bInterfaceProtocol == US_PR_UAS)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500387 ret = usb_uas_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200388 } else
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500389 ret = usb_hid_setup(usbdev);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400390 if (ret)
391 goto fail;
392
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400393 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400394 return 1;
395fail:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400396 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400397 return 0;
398}
399
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400400static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500401usb_hub_port_setup(void *data)
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400402{
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500403 struct usbdevice_s *usbdev = data;
404 struct usbhub_s *hub = usbdev->hub;
405 u32 port = usbdev->port;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400406
Kevin O'Connor0f681302014-09-10 11:33:01 -0400407 for (;;) {
408 // Detect if device present (and possibly start reset)
409 int ret = hub->op->detect(hub, port);
410 if (ret > 0)
411 // Device connected.
412 break;
413 if (ret < 0 || timer_check(hub->detectend))
414 // No device found.
415 goto done;
416 msleep(5);
417 }
418
419 // XXX - wait USB_TIME_ATTDB time?
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400420
421 // Reset port and determine device speed
422 mutex_lock(&hub->cntl->resetlock);
Kevin O'Connor0f681302014-09-10 11:33:01 -0400423 int ret = hub->op->reset(hub, port);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400424 if (ret < 0)
425 // Reset failed
426 goto resetfail;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500427 usbdev->speed = ret;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400428
429 // Set address of port
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500430 ret = usb_set_address(usbdev);
431 if (ret) {
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400432 hub->op->disconnect(hub, port);
433 goto resetfail;
434 }
435 mutex_unlock(&hub->cntl->resetlock);
436
437 // Configure the device
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500438 int count = configure_usb_device(usbdev);
439 free_pipe(usbdev->defpipe);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400440 if (!count)
441 hub->op->disconnect(hub, port);
442 hub->devcount += count;
443done:
444 hub->threads--;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500445 free(usbdev);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400446 return;
447
448resetfail:
449 mutex_unlock(&hub->cntl->resetlock);
450 goto done;
451}
452
453void
454usb_enumerate(struct usbhub_s *hub)
455{
456 u32 portcount = hub->portcount;
457 hub->threads = portcount;
Kevin O'Connor0f681302014-09-10 11:33:01 -0400458 hub->detectend = timer_calc(USB_TIME_SIGATT);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400459
460 // Launch a thread for every port.
461 int i;
462 for (i=0; i<portcount; i++) {
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500463 struct usbdevice_s *usbdev = malloc_tmphigh(sizeof(*usbdev));
464 if (!usbdev) {
465 warn_noalloc();
466 continue;
467 }
468 memset(usbdev, 0, sizeof(*usbdev));
469 usbdev->hub = hub;
470 usbdev->port = i;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500471 run_thread(usb_hub_port_setup, usbdev);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400472 }
473
474 // Wait for threads to complete.
475 while (hub->threads)
476 yield();
477}
478
Kevin O'Connor114592f2009-09-28 21:32:08 -0400479void
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500480__usb_setup(void *data)
481{
482 dprintf(3, "init usb\n");
483 xhci_setup();
484 ehci_setup();
485 uhci_setup();
486 ohci_setup();
487}
488
489void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500490usb_setup(void)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400491{
Kevin O'Connor59c75742010-02-13 18:49:24 -0500492 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400493 if (! CONFIG_USB)
494 return;
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500495 run_thread(__usb_setup, NULL);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400496}