blob: 8ffddf8833d9ca0c1ab27a5608190d07be8dc986 [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
63send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
64 , void *data, int datasize)
65{
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:
70 return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
71 case USB_TYPE_OHCI:
72 return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -050073 case USB_TYPE_EHCI:
74 return ehci_control(pipe, dir, cmd, cmdsize, data, datasize);
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020075 case USB_TYPE_XHCI:
76 return xhci_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{
131 return send_control(pipe, req->bRequestType & USB_DIR_IN
132 , req, sizeof(*req), data, req->wLength);
133}
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
181usb_getFrameExp(struct usbdevice_s *usbdev
182 , struct usb_endpoint_descriptor *epdesc)
183{
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'Connor3c160dd2010-02-17 23:06:52 -0500190// Find the first endpoing of a given type in an interface description.
191struct usb_endpoint_descriptor *
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500192findEndPointDesc(struct usbdevice_s *usbdev, int type, int dir)
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500193{
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500194 struct usb_endpoint_descriptor *epdesc = (void*)&usbdev->iface[1];
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500195 for (;;) {
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500196 if ((void*)epdesc >= (void*)usbdev->iface + usbdev->imax
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500197 || epdesc->bDescriptorType == USB_DT_INTERFACE) {
198 return NULL;
199 }
200 if (epdesc->bDescriptorType == USB_DT_ENDPOINT
201 && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
202 && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
203 return epdesc;
204 epdesc = (void*)epdesc + epdesc->bLength;
205 }
206}
207
Kevin O'Connor114592f2009-09-28 21:32:08 -0400208// Get the first 8 bytes of the device descriptor.
209static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500210get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400211{
212 struct usb_ctrlrequest req;
213 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
214 req.bRequest = USB_REQ_GET_DESCRIPTOR;
215 req.wValue = USB_DT_DEVICE<<8;
216 req.wIndex = 0;
217 req.wLength = 8;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500218 return send_default_control(pipe, &req, dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400219}
220
221static struct usb_config_descriptor *
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500222get_device_config(struct usb_pipe *pipe)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400223{
224 struct usb_config_descriptor cfg;
225
226 struct usb_ctrlrequest req;
227 req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
228 req.bRequest = USB_REQ_GET_DESCRIPTOR;
229 req.wValue = USB_DT_CONFIG<<8;
230 req.wIndex = 0;
231 req.wLength = sizeof(cfg);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500232 int ret = send_default_control(pipe, &req, &cfg);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400233 if (ret)
234 return NULL;
235
236 void *config = malloc_tmphigh(cfg.wTotalLength);
237 if (!config)
238 return NULL;
239 req.wLength = cfg.wTotalLength;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500240 ret = send_default_control(pipe, &req, config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400241 if (ret)
242 return NULL;
243 //hexdump(config, cfg.wTotalLength);
244 return config;
245}
246
Kevin O'Connor114592f2009-09-28 21:32:08 -0400247static int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500248set_configuration(struct usb_pipe *pipe, u16 val)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400249{
250 struct usb_ctrlrequest req;
251 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
252 req.bRequest = USB_REQ_SET_CONFIGURATION;
253 req.wValue = val;
254 req.wIndex = 0;
255 req.wLength = 0;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500256 return send_default_control(pipe, &req, NULL);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400257}
258
Kevin O'Connor3c160dd2010-02-17 23:06:52 -0500259
260/****************************************************************
261 * Initialization and enumeration
262 ****************************************************************/
263
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500264// Assign an address to a device in the default state on the given
265// controller.
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500266static int
267usb_set_address(struct usbdevice_s *usbdev)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400268{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500269 ASSERT32FLAT();
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500270 struct usb_s *cntl = usbdev->hub->cntl;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500271 dprintf(3, "set_address %p\n", cntl);
272 if (cntl->maxaddr >= USB_MAXADDR)
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500273 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400274
Kevin O'Connor4ea2fa92013-12-27 19:25:46 -0500275 msleep(USB_TIME_RSTRCY);
276
Kevin O'Connor92650f22012-03-05 22:03:03 -0500277 // Create a pipe for the default address.
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500278 struct usb_endpoint_descriptor epdesc = {
279 .wMaxPacketSize = 8,
280 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
281 };
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500282 usbdev->defpipe = usb_alloc_pipe(usbdev, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500283 if (!usbdev->defpipe)
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500284 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500285
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500286 // Send set_address command.
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500287 struct usb_ctrlrequest req;
288 req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
289 req.bRequest = USB_REQ_SET_ADDRESS;
290 req.wValue = cntl->maxaddr + 1;
291 req.wIndex = 0;
292 req.wLength = 0;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500293 int ret = send_default_control(usbdev->defpipe, &req, NULL);
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200294 if (ret) {
295 free_pipe(usbdev->defpipe);
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500296 return -1;
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200297 }
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500298
299 msleep(USB_TIME_SETADDR_RECOVERY);
300
301 cntl->maxaddr++;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500302 usbdev->devaddr = cntl->maxaddr;
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200303 usbdev->defpipe = usb_update_pipe(usbdev, usbdev->defpipe, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500304 if (!usbdev->defpipe)
305 return -1;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500306 return 0;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500307}
308
309// Called for every found device - see if a driver is available for
310// this device and do setup if so.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400311static int
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500312configure_usb_device(struct usbdevice_s *usbdev)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500313{
314 ASSERT32FLAT();
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500315 dprintf(3, "config_usb: %p\n", usbdev->defpipe);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500316
317 // Set the max packet size for endpoint 0 of this device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400318 struct usb_device_descriptor dinfo;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500319 int ret = get_device_info8(usbdev->defpipe, &dinfo);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400320 if (ret)
321 return 0;
322 dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
323 , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
324 , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
325 if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
326 return 0;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500327 struct usb_endpoint_descriptor epdesc = {
328 .wMaxPacketSize = dinfo.bMaxPacketSize0,
329 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
330 };
Gerd Hoffmann63cbab12013-08-21 13:59:21 +0200331 usbdev->defpipe = usb_update_pipe(usbdev, usbdev->defpipe, &epdesc);
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500332 if (!usbdev->defpipe)
333 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400334
335 // Get configuration
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500336 struct usb_config_descriptor *config = get_device_config(usbdev->defpipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400337 if (!config)
338 return 0;
339
340 // Determine if a driver exists for this device - only look at the
341 // first interface of the first configuration.
342 struct usb_interface_descriptor *iface = (void*)(&config[1]);
Kevin O'Connor0e885762010-05-01 22:14:40 -0400343 if (iface->bInterfaceClass != USB_CLASS_HID
344 && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
345 && iface->bInterfaceClass != USB_CLASS_HUB)
Kevin O'Connor54671c12010-02-15 18:58:12 -0500346 // Not a supported device.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400347 goto fail;
348
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500349 // Set the configuration.
Kevin O'Connor77ab4e12012-03-08 08:44:32 -0500350 ret = set_configuration(usbdev->defpipe, config->bConfigurationValue);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400351 if (ret)
352 goto fail;
353
354 // Configure driver.
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500355 usbdev->config = config;
356 usbdev->iface = iface;
357 usbdev->imax = (void*)config + config->wTotalLength - (void*)iface;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500358 if (iface->bInterfaceClass == USB_CLASS_HUB)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500359 ret = usb_hub_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200360 else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) {
361 if (iface->bInterfaceProtocol == US_PR_BULK)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500362 ret = usb_msc_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200363 if (iface->bInterfaceProtocol == US_PR_UAS)
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500364 ret = usb_uas_setup(usbdev);
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200365 } else
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500366 ret = usb_hid_setup(usbdev);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400367 if (ret)
368 goto fail;
369
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400370 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400371 return 1;
372fail:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400373 free(config);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400374 return 0;
375}
376
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400377static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500378usb_hub_port_setup(void *data)
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400379{
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500380 struct usbdevice_s *usbdev = data;
381 struct usbhub_s *hub = usbdev->hub;
382 u32 port = usbdev->port;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400383
384 // Detect if device present (and possibly start reset)
385 int ret = hub->op->detect(hub, port);
386 if (ret)
387 // No device present
388 goto done;
389
390 // Reset port and determine device speed
391 mutex_lock(&hub->cntl->resetlock);
392 ret = hub->op->reset(hub, port);
393 if (ret < 0)
394 // Reset failed
395 goto resetfail;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500396 usbdev->speed = ret;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400397
398 // Set address of port
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500399 ret = usb_set_address(usbdev);
400 if (ret) {
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400401 hub->op->disconnect(hub, port);
402 goto resetfail;
403 }
404 mutex_unlock(&hub->cntl->resetlock);
405
406 // Configure the device
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500407 int count = configure_usb_device(usbdev);
408 free_pipe(usbdev->defpipe);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400409 if (!count)
410 hub->op->disconnect(hub, port);
411 hub->devcount += count;
412done:
413 hub->threads--;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500414 free(usbdev);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400415 return;
416
417resetfail:
418 mutex_unlock(&hub->cntl->resetlock);
419 goto done;
420}
421
422void
423usb_enumerate(struct usbhub_s *hub)
424{
425 u32 portcount = hub->portcount;
426 hub->threads = portcount;
427
428 // Launch a thread for every port.
429 int i;
430 for (i=0; i<portcount; i++) {
Kevin O'Connor4f74dad2012-03-06 08:20:40 -0500431 struct usbdevice_s *usbdev = malloc_tmphigh(sizeof(*usbdev));
432 if (!usbdev) {
433 warn_noalloc();
434 continue;
435 }
436 memset(usbdev, 0, sizeof(*usbdev));
437 usbdev->hub = hub;
438 usbdev->port = i;
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500439 run_thread(usb_hub_port_setup, usbdev);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400440 }
441
442 // Wait for threads to complete.
443 while (hub->threads)
444 yield();
445}
446
Kevin O'Connor114592f2009-09-28 21:32:08 -0400447void
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500448__usb_setup(void *data)
449{
450 dprintf(3, "init usb\n");
451 xhci_setup();
452 ehci_setup();
453 uhci_setup();
454 ohci_setup();
455}
456
457void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500458usb_setup(void)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400459{
Kevin O'Connor59c75742010-02-13 18:49:24 -0500460 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400461 if (! CONFIG_USB)
462 return;
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500463 run_thread(__usb_setup, NULL);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400464}