1 // Code for handling standard USB hubs.
3 // Copyright (C) 2010 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
7 #include "util.h" // dprintf
8 #include "config.h" // CONFIG_USB_HUB
9 #include "usb-hub.h" // struct usb_hub_descriptor
10 #include "usb.h" // struct usb_s
13 get_hub_desc(struct usb_pipe *pipe, struct usb_hub_descriptor *desc)
15 struct usb_ctrlrequest req;
16 req.bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_DEVICE;
17 req.bRequest = USB_REQ_GET_DESCRIPTOR;
18 req.wValue = USB_DT_HUB<<8;
20 req.wLength = sizeof(*desc);
21 return send_default_control(pipe, &req, desc);
25 set_port_feature(struct usbhub_s *hub, int port, int feature)
27 struct usb_ctrlrequest req;
28 req.bRequestType = USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER;
29 req.bRequest = USB_REQ_SET_FEATURE;
33 mutex_lock(&hub->lock);
34 int ret = send_default_control(hub->pipe, &req, NULL);
35 mutex_unlock(&hub->lock);
40 clear_port_feature(struct usbhub_s *hub, int port, int feature)
42 struct usb_ctrlrequest req;
43 req.bRequestType = USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_OTHER;
44 req.bRequest = USB_REQ_CLEAR_FEATURE;
48 mutex_lock(&hub->lock);
49 int ret = send_default_control(hub->pipe, &req, NULL);
50 mutex_unlock(&hub->lock);
55 get_port_status(struct usbhub_s *hub, int port, struct usb_port_status *sts)
57 struct usb_ctrlrequest req;
58 req.bRequestType = USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_OTHER;
59 req.bRequest = USB_REQ_GET_STATUS;
62 req.wLength = sizeof(*sts);
63 mutex_lock(&hub->lock);
64 int ret = send_default_control(hub->pipe, &req, sts);
65 mutex_unlock(&hub->lock);
70 init_hub_port(void *data)
72 struct usbhub_s *hub = data;
73 u32 port = hub->port; // XXX - find better way to pass port
75 // Turn on power to port.
76 int ret = set_port_feature(hub, port, USB_PORT_FEAT_POWER);
80 // Wait for port power to stabilize.
81 msleep(hub->powerwait);
83 // Check periodically for a device connect.
84 struct usb_port_status sts;
85 u64 end = calc_future_tsc(USB_TIME_SIGATT);
87 ret = get_port_status(hub, port, &sts);
90 if (sts.wPortStatus & USB_PORT_STAT_CONNECTION)
99 // XXX - wait USB_TIME_ATTDB time?
102 mutex_lock(&hub->cntl->resetlock);
103 ret = set_port_feature(hub, port, USB_PORT_FEAT_RESET);
107 // Wait for reset to complete.
108 end = calc_future_tsc(USB_TIME_DRST * 2);
110 ret = get_port_status(hub, port, &sts);
113 if (!(sts.wPortStatus & USB_PORT_STAT_RESET))
115 if (check_time(end)) {
123 if (!(sts.wPortStatus & USB_PORT_STAT_CONNECTION))
124 // Device no longer present
127 // Set address of port
128 struct usb_pipe *pipe = usb_set_address(
129 hub, port, ((sts.wPortStatus & USB_PORT_STAT_SPEED_MASK)
130 >> USB_PORT_STAT_SPEED_SHIFT));
133 mutex_unlock(&hub->cntl->resetlock);
135 // Configure the device
136 int count = configure_usb_device(pipe);
139 ret = clear_port_feature(hub, port, USB_PORT_FEAT_ENABLE);
143 hub->devcount += count;
149 clear_port_feature(hub, port, USB_PORT_FEAT_ENABLE);
150 mutex_unlock(&hub->cntl->resetlock);
152 dprintf(1, "Failure on hub port %d setup\n", port);
156 // Configure a usb hub and then find devices connected to it.
158 usb_hub_init(struct usb_pipe *pipe)
164 struct usb_hub_descriptor desc;
165 int ret = get_hub_desc(pipe, &desc);
170 memset(&hub, 0, sizeof(hub));
172 hub.cntl = pipe->cntl;
173 hub.powerwait = desc.bPwrOn2PwrGood * 2;
175 // Launch a thread for every port.
177 for (i=1; i<=desc.bNbrPorts; i++) {
180 run_thread(init_hub_port, &hub);
183 // Wait for threads to complete.
187 dprintf(1, "Initialized USB HUB (%d ports used)\n", hub.devcount);