Push 'struct pci_device' into USB code (instead of using u16 bdf).
authorKevin O'Connor <kevin@koconnor.net>
Sat, 9 Jul 2011 18:11:21 +0000 (14:11 -0400)
committerKevin O'Connor <kevin@koconnor.net>
Sun, 10 Jul 2011 19:34:51 +0000 (15:34 -0400)
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
src/usb-ehci.c
src/usb-ehci.h
src/usb-msc.c
src/usb-ohci.c
src/usb-ohci.h
src/usb-uhci.c
src/usb-uhci.h
src/usb.c
src/usb.h

index 5a0eb3eaacb64a7c44bbf8ed151586ed6597666a..d60465e86dbf7f1cf08b2e0e791243ba108ed6f4 100644 (file)
 #include "usb-uhci.h" // init_uhci
 #include "usb-ohci.h" // init_ohci
 
-struct companion_s {
-    u16 bdf;
-    u16 type;
-};
-
 struct usb_ehci_s {
     struct usb_s usb;
     struct ehci_caps *caps;
     struct ehci_regs *regs;
     struct ehci_qh *async_qh;
-    struct companion_s companion[8];
+    struct pci_device *companion[8];
     int checkports;
     int legacycount;
 };
@@ -52,13 +47,13 @@ ehci_note_port(struct usb_ehci_s *cntl)
     // Start companion controllers.
     int i;
     for (i=0; i<ARRAY_SIZE(cntl->companion); i++) {
-        u16 type = cntl->companion[i].type;
-        if (type == USB_TYPE_UHCI)
-            uhci_init(cntl->companion[i].bdf, cntl->usb.busid + i);
-        else if (type == USB_TYPE_OHCI)
-            ohci_init(cntl->companion[i].bdf, cntl->usb.busid + i);
-        else
-            return;
+        struct pci_device *pci = cntl->companion[i];
+        if (!pci)
+            break;
+        if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
+            uhci_init(pci, cntl->usb.busid + i);
+        else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
+            ohci_init(pci, cntl->usb.busid + i);
     }
 }
 
@@ -249,11 +244,12 @@ fail:
 }
 
 int
-ehci_init(u16 bdf, int busid, int compbdf)
+ehci_init(struct pci_device *pci, int busid, struct pci_device *comppci)
 {
     if (! CONFIG_USB_EHCI)
         return -1;
 
+    u16 bdf = pci->bdf;
     u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
     struct ehci_caps *caps = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
     u32 hcc_params = readl(&caps->hccparams);
@@ -265,7 +261,7 @@ ehci_init(u16 bdf, int busid, int compbdf)
     struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
     memset(cntl, 0, sizeof(*cntl));
     cntl->usb.busid = busid;
-    cntl->usb.bdf = bdf;
+    cntl->usb.pci = pci;
     cntl->usb.type = USB_TYPE_EHCI;
     cntl->caps = caps;
     cntl->regs = (void*)caps + readb(&caps->caplength);
@@ -281,19 +277,13 @@ ehci_init(u16 bdf, int busid, int compbdf)
     // Find companion controllers.
     int count = 0;
     for (;;) {
-        if (compbdf < 0 || compbdf >= bdf)
+        if (!comppci || comppci == pci)
             break;
-        u32 code = pci_config_readl(compbdf, PCI_CLASS_REVISION) >> 8;
-        if (code == PCI_CLASS_SERIAL_USB_UHCI) {
-            cntl->companion[count].bdf = compbdf;
-            cntl->companion[count].type = USB_TYPE_UHCI;
-            count++;
-        } else if (code == PCI_CLASS_SERIAL_USB_OHCI) {
-            cntl->companion[count].bdf = compbdf;
-            cntl->companion[count].type = USB_TYPE_OHCI;
-            count++;
-        }
-        compbdf = pci_next(compbdf+1, pci_bdf_to_bus(compbdf));
+        if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_UHCI)
+            cntl->companion[count++] = comppci;
+        else if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_OHCI)
+            cntl->companion[count++] = comppci;
+        comppci = comppci->next;
     }
 
     run_thread(configure_ehci, cntl);
index bb8df52d89dbc36115c6bd28fc7d85fb15a04e1c..1a2c6c78b76128f2a2f8339a28fa95ae5c59b4c4 100644 (file)
@@ -2,7 +2,7 @@
 #define __USB_EHCI_H
 
 // usb-ehci.c
-int ehci_init(u16 bdf, int busid, int compbdf);
+int ehci_init(struct pci_device *pci, int busid, struct pci_device *comppci);
 struct usb_pipe;
 void ehci_free_pipe(struct usb_pipe *p);
 struct usb_pipe *ehci_alloc_control_pipe(struct usb_pipe *dummy);
index 1aa57d1bf91a85f9ba954ac8bb0d7fe51a8abc7a..a57e4d2c35c50584411c41c8a20b04690e472565 100644 (file)
@@ -12,6 +12,7 @@
 #include "blockcmd.h" // cdb_read
 #include "disk.h" // DTYPE_USB
 #include "boot.h" // boot_add_hd
+#include "pci.h" // struct pci_device
 
 struct usbdrive_s {
     struct drive_s drive;
@@ -145,7 +146,7 @@ setup_drive_cdrom(struct disk_op_s *op, char *desc)
     op->drive_g->sectors = (u64)-1;
     struct usb_pipe *pipe = container_of(
         op->drive_g, struct usbdrive_s, drive)->bulkout;
-    int prio = bootprio_find_usb(pipe->cntl->bdf, pipe->path);
+    int prio = bootprio_find_usb(pipe->cntl->pci->bdf, pipe->path);
     boot_add_cd(op->drive_g, desc, prio);
     return 0;
 }
@@ -173,7 +174,7 @@ setup_drive_hd(struct disk_op_s *op, char *desc)
     // Register with bcv system.
     struct usb_pipe *pipe = container_of(
         op->drive_g, struct usbdrive_s, drive)->bulkout;
-    int prio = bootprio_find_usb(pipe->cntl->bdf, pipe->path);
+    int prio = bootprio_find_usb(pipe->cntl->pci->bdf, pipe->path);
     boot_add_hd(op->drive_g, desc, prio);
 
     return 0;
index 72b9f68b7f50f3b40cd0ca7af9d5d4855f340022..317f200c65f0348ae1c2082d28a5265716e2e266 100644 (file)
@@ -204,16 +204,17 @@ free:
 }
 
 void
-ohci_init(u16 bdf, int busid)
+ohci_init(struct pci_device *pci, int busid)
 {
     if (! CONFIG_USB_OHCI)
         return;
     struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
     memset(cntl, 0, sizeof(*cntl));
     cntl->usb.busid = busid;
-    cntl->usb.bdf = bdf;
+    cntl->usb.pci = pci;
     cntl->usb.type = USB_TYPE_OHCI;
 
+    u16 bdf = pci->bdf;
     u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
     cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
 
index 7dd2f09d4cce173069b3027675ee17866b4cbac1..c7670ffd86c7669549fe438e4f69314cdaa28fa8 100644 (file)
@@ -2,7 +2,7 @@
 #define __USB_OHCI_H
 
 // usb-ohci.c
-void ohci_init(u16 bdf, int busid);
+void ohci_init(struct pci_device *pci, int busid);
 struct usb_pipe;
 void ohci_free_pipe(struct usb_pipe *p);
 struct usb_pipe *ohci_alloc_control_pipe(struct usb_pipe *dummy);
index 40f83bb9a850d57b1662fd2d1ab6d6ba136e2fed..6e454742e5d81fdb02deb84184441f87cb737d09 100644 (file)
@@ -179,14 +179,15 @@ fail:
 }
 
 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));
     memset(cntl, 0, sizeof(*cntl));
     cntl->usb.busid = busid;
-    cntl->usb.bdf = bdf;
+    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);
index 3c2c298d7d7ddd2c2413e81be16f316a2d5c6c2f..b5f70f77ba7f617ba4f92ad57c036c5d5e2e9a45 100644 (file)
@@ -2,7 +2,7 @@
 #define __USB_UHCI_H
 
 // usb-uhci.c
-void uhci_init(u16 bdf, int busid);
+void uhci_init(struct pci_device *pci, int busid);
 struct usb_pipe;
 void uhci_free_pipe(struct usb_pipe *p);
 struct usb_pipe *uhci_alloc_control_pipe(struct usb_pipe *dummy);
index 26d1017bda5e827adb9c0b220678b3da4ffdba7e..1f69d16aa2b738d732125891d8aff0b50602f45a 100644 (file)
--- a/src/usb.c
+++ b/src/usb.c
@@ -442,7 +442,7 @@ usb_setup(void)
             for (;;) {
                 if (pci_classprog(ehcipci) == PCI_CLASS_SERIAL_USB_EHCI) {
                     // Found an ehci controller.
-                    int ret = ehci_init(ehcipci->bdf, count++, pci->bdf);
+                    int ret = ehci_init(ehcipci, count++, pci);
                     if (ret)
                         // Error
                         break;
@@ -461,8 +461,8 @@ usb_setup(void)
         }
 
         if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
-            uhci_init(pci->bdf, count++);
+            uhci_init(pci, count++);
         else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
-            ohci_init(pci->bdf, count++);
+            ohci_init(pci, count++);
     }
 }
index 966e94b562d7a2d4a0928ad5dcaed5d403fd5661..8b2af405d917916319eaee35bf5c066d1ce50cbb 100644 (file)
--- a/src/usb.h
+++ b/src/usb.h
@@ -21,8 +21,8 @@ struct usb_pipe {
 struct usb_s {
     struct usb_pipe *defaultpipe;
     struct mutex_s resetlock;
+    struct pci_device *pci;
     int busid;
-    u16 bdf;
     u8 type;
     u8 maxaddr;
 };