#include "pci_regs.h" // PCI_BASE_ADDRESS_4
#include "usb.h" // struct usb_s
#include "farptr.h" // GET_FLATPTR
-#include "usb-hub.h" // struct usbhub_s
struct usb_uhci_s {
struct usb_s usb;
* Root hub
****************************************************************/
-static void
-init_uhci_port(void *data)
+// Check if device attached to a given port
+static int
+uhci_hub_detect(struct usbhub_s *hub, u32 port)
{
- struct usbhub_s *hub = data;
- u32 port = hub->port; // XXX - find better way to pass port
struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
u16 status = inw(ioport);
if (!(status & USBPORTSC_CCS))
// No device
- goto done;
+ return -1;
// XXX - if just powered up, need to wait for USB_TIME_ATTDB?
- // Reset port
+ // Begin reset on port
outw(USBPORTSC_PR, ioport);
msleep(USB_TIME_DRSTR);
- mutex_lock(&cntl->usb.resetlock);
+ return 0;
+}
+
+// Reset device on port
+static int
+uhci_hub_reset(struct usbhub_s *hub, u32 port)
+{
+ struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
+ u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
+
+ // Finish reset on port
outw(0, ioport);
udelay(6); // 64 high-speed bit times
- status = inw(ioport);
+ u16 status = inw(ioport);
if (!(status & USBPORTSC_CCS))
// No longer connected
- goto resetfail;
+ return -1;
outw(USBPORTSC_PE, ioport);
- struct usb_pipe *pipe = usb_set_address(
- &cntl->usb, !!(status & USBPORTSC_LSDA));
- if (!pipe)
- goto resetfail;
- mutex_unlock(&cntl->usb.resetlock);
-
- // Configure port
- int count = configure_usb_device(pipe);
- free_pipe(pipe);
- if (! count)
- // Disable port
- outw(0, ioport);
- hub->devcount += count;
-done:
- hub->threads--;
- return;
-
-resetfail:
+ return !!(status & USBPORTSC_LSDA);
+}
+
+// Disable port
+static void
+uhci_hub_disconnect(struct usbhub_s *hub, u32 port)
+{
+ struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
+ u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
outw(0, ioport);
- mutex_unlock(&cntl->usb.resetlock);
- goto done;
}
+static struct usbhub_op_s uhci_HubOp = {
+ .detect = uhci_hub_detect,
+ .reset = uhci_hub_reset,
+ .disconnect = uhci_hub_disconnect,
+};
+
// Find any devices connected to the root hub.
static int
check_uhci_ports(struct usb_uhci_s *cntl)
struct usbhub_s hub;
memset(&hub, 0, sizeof(hub));
hub.cntl = &cntl->usb;
- hub.threads = 2;
-
- // Launch a thread for every port.
- run_thread(init_uhci_port, &hub);
- hub.port = 1;
- run_thread(init_uhci_port, &hub);
-
- // Wait for threads to complete.
- while (hub.threads)
- yield();
-
+ hub.portcount = 2;
+ hub.op = &uhci_HubOp;
+ usb_enumerate(&hub);
return hub.devcount;
}
}
void
-uhci_init(u16 bdf, int busid)
+uhci_init(struct pci_device *pci, int busid)
{
if (! CONFIG_USB_UHCI)
return;
+ u16 bdf = pci->bdf;
struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
+ if (!cntl) {
+ warn_noalloc();
+ return;
+ }
memset(cntl, 0, sizeof(*cntl));
cntl->usb.busid = busid;
+ cntl->usb.pci = pci;
cntl->usb.type = USB_TYPE_UHCI;
cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
& PCI_BASE_ADDRESS_IO_MASK);
- dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
+ dprintf(1, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
, pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
, pci_bdf_to_fn(bdf), cntl->iobase);
for (;;) {
if (qh->element & UHCI_PTR_TERM)
return 0;
- if (check_time(end)) {
+ if (check_tsc(end)) {
warn_timeout();
struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
for (;;) {
if (inw(iobase + USBFRNUM) != startframe)
break;
- if (check_time(end)) {
+ if (check_tsc(end)) {
warn_timeout();
return;
}
pipe->pipe.cntl, struct usb_uhci_s, usb);
int maxpacket = pipe->pipe.maxpacket;
- int lowspeed = pipe->pipe.lowspeed;
+ int lowspeed = pipe->pipe.speed;
int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
// Setup transfer descriptors
status = td->status;
if (!(status & TD_CTRL_ACTIVE))
break;
- if (check_time(end)) {
+ if (check_tsc(end)) {
warn_timeout();
return -1;
}
dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
, &pipe->qh, dir, data, datasize);
int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
- int lowspeed = GET_FLATPTR(pipe->pipe.lowspeed);
+ int lowspeed = GET_FLATPTR(pipe->pipe.speed);
int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
| (GET_FLATPTR(pipe->pipe.ep) << 7));
int toggle = GET_FLATPTR(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
if (frameexp > 10)
frameexp = 10;
int maxpacket = dummy->maxpacket;
- int lowspeed = dummy->lowspeed;
+ int lowspeed = dummy->speed;
int devaddr = dummy->devaddr | (dummy->ep << 7);
// Determine number of entries needed for 2 timer ticks.
int ms = 1<<frameexp;