[usb][xhci-rewrite] Migrate xhci-rewrite to new bind mechanism

Change-Id: Ib6584c9d5c102deca7660a6b672f84f1db5810ee
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/489307
Commit-Queue: Sam Hansen <hansens@google.com>
Reviewed-by: Alex Legg <alexlegg@google.com>
diff --git a/src/devices/usb/drivers/xhci-rewrite/BUILD.gn b/src/devices/usb/drivers/xhci-rewrite/BUILD.gn
index 35daff5..cdb266c 100644
--- a/src/devices/usb/drivers/xhci-rewrite/BUILD.gn
+++ b/src/devices/usb/drivers/xhci-rewrite/BUILD.gn
@@ -2,10 +2,23 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+import("//build/bind/bind.gni")
 import("//build/config/fuchsia/rules.gni")
 import("//build/test.gni")
 import("//src/sys/build/components.gni")
 
+bind_rules("usb_xhci_bind") {
+  rules = "usb_xhci.bind"
+  output = "usb_xhci_bind.h"
+  tests = "tests.json"
+  deps = [
+    "//src/devices/bind/fuchsia.pci",
+    "//src/devices/bind/fuchsia.pci:serial",
+    "//src/devices/bind/fuchsia.pci:serial.usb",
+    "//src/devices/bind/fuchsia.platform",
+  ]
+}
+
 driver_module("xhci") {
   configs += [
     "//build/config:all_source",
@@ -21,6 +34,7 @@
     "xhci-transfer-ring.cc",
   ]
   deps = [
+    ":usb_xhci_bind",
     "//sdk/banjo/fuchsia.hardware.pci",
     "//sdk/banjo/fuchsia.hardware.platform.device",
     "//sdk/banjo/fuchsia.hardware.usb",
@@ -38,9 +52,6 @@
     "//src/devices/pci/lib/device-protocol-pci",
     "//src/devices/usb/lib/usb",
     "//src/lib/ddk",
-
-    # TODO(fxb/38132): Migrate to the new bind rules and delete the below
-    "//src/lib/ddk:ddk-deprecated-binding-headers",
     "//src/lib/ddktl",
     "//zircon/public/lib/async",
     "//zircon/public/lib/async-cpp",
@@ -60,6 +71,7 @@
 source_set("test-libs") {
   testonly = true
   public_deps = [
+    ":usb_xhci_bind",
     "//sdk/banjo/fuchsia.hardware.acpi",
     "//sdk/banjo/fuchsia.hardware.platform.bus",
     "//sdk/banjo/fuchsia.hardware.platform.device",
@@ -79,9 +91,6 @@
     "//src/devices/testing/fake_ddk",
     "//src/devices/usb/lib/usb",
     "//src/lib/ddk",
-
-    # TODO(fxb/38132): Migrate to the new bind rules and delete the below
-    "//src/lib/ddk:ddk-deprecated-binding-headers",
     "//src/lib/ddktl",
     "//zircon/public/lib/fbl",
     "//zircon/public/lib/fit",
@@ -216,6 +225,7 @@
 group("tests") {
   testonly = true
   deps = [
+    ":usb_xhci_bind_test",
     ":xhci-core-unittest",
     ":xhci-event-ring-unittest",
     ":xhci-transfer-ring-unittest",
diff --git a/src/devices/usb/drivers/xhci-rewrite/tests.json b/src/devices/usb/drivers/xhci-rewrite/tests.json
new file mode 100644
index 0000000..d3d23bb
--- /dev/null
+++ b/src/devices/usb/drivers/xhci-rewrite/tests.json
@@ -0,0 +1,32 @@
+[
+    {
+        "device": {
+            "fuchsia.BIND_PROTOCOL": "fuchsia.platform.BIND_PROTOCOL.DEVICE",
+            "fuchsia.BIND_PLATFORM_DEV_VID": "fuchsia.platform.BIND_PLATFORM_DEV_VID.GENERIC",
+            "fuchsia.BIND_PLATFORM_DEV_PID": "fuchsia.platform.BIND_PLATFORM_DEV_PID.GENERIC",
+            "fuchsia.BIND_PLATFORM_DEV_DID": "fuchsia.platform.BIND_PLATFORM_DEV_DID.XHCI"
+        },
+        "expected": "match",
+        "name": "MatchPlatBusDevice"
+    },
+    {
+        "device": {
+            "fuchsia.BIND_PROTOCOL": "fuchsia.platform.BIND_PROTOCOL.DEVICE",
+            "fuchsia.BIND_PLATFORM_DEV_VID": "fuchsia.platform.BIND_PLATFORM_DEV_VID.GENERIC",
+            "fuchsia.BIND_PLATFORM_DEV_PID": "fuchsia.platform.BIND_PLATFORM_DEV_PID.GENERIC",
+            "fuchsia.BIND_PLATFORM_DEV_DID": "0x02"
+        },
+        "expected": "match",
+        "name": "MatchPlatBusDevice_Vim2Hack"
+    },
+    {
+        "device": {
+            "fuchsia.BIND_PROTOCOL": "fuchsia.pci.BIND_PROTOCOL.DEVICE",
+            "fuchsia.BIND_PCI_CLASS": "fuchsia.pci.BIND_PCI_CLASS.SERIAL_BUS_CONTROLLER",
+            "fuchsia.BIND_PCI_SUBCLASS": "fuchsia.pci.serial.BIND_PCI_SUBCLASS.USB",
+            "fuchsia.BIND_PCI_INTERFACE": "fuchsia.pci.serial.usb.BIND_PCI_INTERFACE.XHCI"
+        },
+        "expected": "match",
+        "name": "MatchPCIDevice"
+    }
+]
diff --git a/src/devices/usb/drivers/xhci-rewrite/usb-xhci.cc b/src/devices/usb/drivers/xhci-rewrite/usb-xhci.cc
index 9afa956..466e716 100644
--- a/src/devices/usb/drivers/xhci-rewrite/usb-xhci.cc
+++ b/src/devices/usb/drivers/xhci-rewrite/usb-xhci.cc
@@ -20,7 +20,6 @@
 #include <memory>
 #include <string>
 
-#include <ddk/binding.h>
 #include <ddk/debug.h>
 #include <ddk/metadata.h>
 #include <ddk/platform-defs.h>
@@ -29,6 +28,8 @@
 #include <hw/arch_ops.h>
 #include <hw/reg.h>
 
+#include "src/devices/usb/drivers/xhci-rewrite/usb_xhci_bind.h"
+
 namespace usb_xhci {
 
 namespace {
@@ -2098,30 +2099,4 @@
 
 }  // namespace usb_xhci
 
-// clang-format off
-ZIRCON_DRIVER_BEGIN(usb_xhci, usb_xhci::driver_ops, "zircon", "0.1", 17)
-    BI_GOTO_IF(EQ, BIND_PROTOCOL, ZX_PROTOCOL_PDEV, 0),
-    BI_GOTO_IF(EQ, BIND_COMPOSITE, 1, 1),
-
-    // PCI binding support
-    BI_ABORT_IF(NE, BIND_PROTOCOL, ZX_PROTOCOL_PCI),
-    BI_ABORT_IF(NE, BIND_PCI_CLASS, 0x0C),
-    BI_ABORT_IF(NE, BIND_PCI_SUBCLASS, 0x03),
-    BI_MATCH_IF(EQ, BIND_PCI_INTERFACE, 0x30),
-    BI_ABORT(),
-
-    // platform bus support
-    BI_LABEL(0),
-    BI_ABORT_IF(NE, BIND_PLATFORM_DEV_VID, PDEV_VID_GENERIC),
-    BI_ABORT_IF(NE, BIND_PLATFORM_DEV_PID, PDEV_PID_GENERIC),
-    BI_MATCH_IF(EQ, BIND_PLATFORM_DEV_DID, PDEV_DID_USB_XHCI),
-    BI_ABORT(),
-
-    // composite binding support
-    BI_LABEL(1),
-    BI_ABORT_IF(NE, BIND_PLATFORM_DEV_VID, PDEV_VID_GENERIC),
-    BI_ABORT_IF(NE, BIND_PLATFORM_DEV_PID, PDEV_PID_GENERIC),
-    BI_MATCH_IF(EQ, BIND_PLATFORM_DEV_DID, PDEV_DID_USB_XHCI_COMPOSITE),
-
-    BI_ABORT(),
-ZIRCON_DRIVER_END(usb_xhci)
+ZIRCON_DRIVER(usb_xhci, usb_xhci::driver_ops, "zircon", "0.1");
diff --git a/src/devices/usb/drivers/xhci-rewrite/usb_xhci.bind b/src/devices/usb/drivers/xhci-rewrite/usb_xhci.bind
new file mode 100644
index 0000000..01dd492
--- /dev/null
+++ b/src/devices/usb/drivers/xhci-rewrite/usb_xhci.bind
@@ -0,0 +1,23 @@
+// Copyright 2021 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+using fuchsia.pci;
+using fuchsia.pci.serial;
+using fuchsia.pci.serial.usb;
+using fuchsia.platform;
+
+if fuchsia.BIND_PROTOCOL == fuchsia.pci.BIND_PROTOCOL.DEVICE {
+  // PCI binding support.
+  fuchsia.BIND_PCI_CLASS == fuchsia.pci.BIND_PCI_CLASS.SERIAL_BUS_CONTROLLER;
+  fuchsia.BIND_PCI_SUBCLASS == fuchsia.pci.serial.BIND_PCI_SUBCLASS.USB;
+  fuchsia.BIND_PCI_INTERFACE == fuchsia.pci.serial.usb.BIND_PCI_INTERFACE.XHCI;
+} else {
+  // Platform bus support.
+  fuchsia.BIND_PLATFORM_DEV_VID == fuchsia.platform.BIND_PLATFORM_DEV_VID.GENERIC;
+  fuchsia.BIND_PLATFORM_DEV_PID == fuchsia.platform.BIND_PLATFORM_DEV_PID.GENERIC;
+  accept fuchsia.BIND_PLATFORM_DEV_DID {
+    fuchsia.platform.BIND_PLATFORM_DEV_DID.XHCI,
+    0x02,  // Only relevant to vim2, needs to be cleaned up.
+  }
+}