[devmgr][coord] Add driver.h for drivers

Move driver-specific code to a driver.h, and rename drivers.cpp to
driver.cpp for consistency. In addition, slightly simplify the
coordinator unit tests to use load_driver.

ZX-3286

Test: Ran Fuchsia and /system/test/ddk tests.
Change-Id: I873539a1b1c0ea6c0626834568ffe31146eee86d
diff --git a/system/core/devmgr/devmgr/coordinator-test.cpp b/system/core/devmgr/devmgr/coordinator-test.cpp
index ab61bad..071eacc 100644
--- a/system/core/devmgr/devmgr/coordinator-test.cpp
+++ b/system/core/devmgr/devmgr/coordinator-test.cpp
@@ -2,8 +2,7 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include <set>
-
+#include <ddk/driver.h>
 #include <fbl/algorithm.h>
 #include <fuchsia/device/manager/c/fidl.h>
 #include <lib/async-loop/cpp/loop.h>
@@ -20,6 +19,8 @@
 }
 } // namespace devmgr
 
+static constexpr char kDriverPath[] = "/boot/driver/test/mock-device.so";
+
 static devmgr::CoordinatorConfig default_config(async_dispatcher_t* dispatcher) {
     devmgr::CoordinatorConfig config;
     config.dispatcher = dispatcher;
@@ -99,7 +100,7 @@
     END_TEST;
 }
 
-bool find_loadable_drivers() {
+bool load_driver() {
     BEGIN_TEST;
 
     bool found_driver = false;
@@ -107,7 +108,7 @@
         delete drv;
         found_driver = true;
     };
-    devmgr::find_loadable_drivers("/boot/driver/test", callback);
+    devmgr::load_driver(kDriverPath, callback);
     ASSERT_TRUE(found_driver);
 
     END_TEST;
@@ -123,17 +124,15 @@
     ASSERT_EQ(ZX_OK, status);
     coordinator.set_running(true);
 
-    std::set<const devmgr::Driver*> drivers;
-    auto callback = [&coordinator, &drivers](devmgr::Driver* drv, const char* version) {
-        drivers.insert(drv);
+    devmgr::Driver* driver;
+    auto callback = [&coordinator, &driver](devmgr::Driver* drv, const char* version) {
+        driver = drv;
         return coordinator.DriverAdded(drv, version);
     };
-    devmgr::find_loadable_drivers("/boot/driver/test", callback);
+    devmgr::load_driver(kDriverPath, callback);
     loop.RunUntilIdle();
-    ASSERT_FALSE(coordinator.drivers().is_empty());
-    for (const devmgr::Driver& drv : coordinator.drivers()) {
-        ASSERT_TRUE(drivers.find(&drv) != drivers.end());
-    }
+    ASSERT_EQ(1, coordinator.drivers().size_slow());
+    ASSERT_EQ(driver, &coordinator.drivers().front());
 
     END_TEST;
 }
@@ -165,8 +164,8 @@
     ASSERT_EQ(1, coordinator.devices().size_slow());
 
     // Add the driver.
-    devmgr::find_loadable_drivers(
-        "/boot/driver/test", fit::bind_member(&coordinator, &devmgr::Coordinator::DriverAdded));
+    devmgr::load_driver(
+        kDriverPath, fit::bind_member(&coordinator, &devmgr::Coordinator::DriverAdded));
     loop.RunUntilIdle();
     ASSERT_FALSE(coordinator.drivers().is_empty());
 
@@ -174,7 +173,7 @@
     devmgr::Device* dev = &coordinator.devices().front();
     devmgr::Devhost host;
     dev->host = &host;
-    status = coordinator.BindDevice(dev, "/boot/driver/test/mock-device.so");
+    status = coordinator.BindDevice(dev, kDriverPath);
     ASSERT_EQ(ZX_OK, status);
 
     // Wait for the BindDriver request.
@@ -203,7 +202,7 @@
                          actual_bytes, handles, actual_handles, nullptr);
     ASSERT_EQ(ZX_OK, status);
     auto req = reinterpret_cast<fuchsia_device_manager_ControllerBindDriverRequest*>(bytes);
-    ASSERT_STR_EQ("/boot/driver/test/mock-device.so", req->driver_path.data);
+    ASSERT_STR_EQ(kDriverPath, req->driver_path.data);
 
     // Write the BindDriver response.
     memset(bytes, 0, sizeof(bytes));
@@ -230,7 +229,7 @@
 RUN_TEST(initialize_core_devices)
 RUN_TEST(open_virtcon)
 RUN_TEST(dump_state)
-RUN_TEST(find_loadable_drivers)
+RUN_TEST(load_driver)
 RUN_TEST(bind_drivers)
 RUN_TEST(bind_devices)
 END_TEST_CASE(coordinator_tests)
diff --git a/system/core/devmgr/devmgr/coordinator.h b/system/core/devmgr/devmgr/coordinator.h
index 2b51ddb..5b51e55 100644
--- a/system/core/devmgr/devmgr/coordinator.h
+++ b/system/core/devmgr/devmgr/coordinator.h
@@ -6,13 +6,11 @@
 
 #include <ddk/binding.h>
 #include <ddk/device.h>
-#include <ddk/driver.h>
 #include <fbl/intrusive_double_list.h>
 #include <fbl/string.h>
 #include <fbl/unique_ptr.h>
 #include <fbl/vector.h>
 #include <lib/async/cpp/wait.h>
-#include <lib/fit/function.h>
 #include <lib/zx/channel.h>
 #include <lib/zx/event.h>
 #include <lib/zx/job.h>
@@ -23,6 +21,7 @@
 #include <utility>
 
 #include "device.h"
+#include "driver.h"
 #include "metadata.h"
 
 namespace devmgr {
@@ -209,27 +208,6 @@
 
 // clang-format on
 
-struct Driver {
-    Driver() = default;
-
-    fbl::String name;
-    fbl::unique_ptr<const zx_bind_inst_t[]> binding;
-    // Binding size in number of bytes, not number of entries
-    // TODO: Change it to number of entries
-    uint32_t binding_size = 0;
-    uint32_t flags = 0;
-    zx::vmo dso_vmo;
-
-    fbl::DoublyLinkedListNodeState<Driver*> node;
-    struct Node {
-        static fbl::DoublyLinkedListNodeState<Driver*>& node_state(Driver& obj) { return obj.node; }
-    };
-
-    fbl::String libname;
-};
-
-#define DRIVER_NAME_LEN_MAX 64
-
 zx_status_t devfs_publish(Device* parent, Device* dev);
 void devfs_unpublish(Device* dev);
 void devfs_advertise(Device* dev);
@@ -417,11 +395,6 @@
 
 void coordinator_setup(Coordinator* coordinator, DevmgrArgs args);
 
-using DriverLoadCallback = fit::function<void(Driver* driver, const char* version)>;
-
-void load_driver(const char* path, DriverLoadCallback func);
-void find_loadable_drivers(const char* path, DriverLoadCallback func);
-
 bool dc_is_bindable(const Driver* drv, uint32_t protocol_id, zx_device_prop_t* props,
                     size_t prop_count, bool autobind);
 
diff --git a/system/core/devmgr/devmgr/devfs.cpp b/system/core/devmgr/devmgr/devfs.cpp
index 72cd771..7bd0181 100644
--- a/system/core/devmgr/devmgr/devfs.cpp
+++ b/system/core/devmgr/devmgr/devfs.cpp
@@ -12,6 +12,7 @@
 #include <zircon/syscalls.h>
 #include <zircon/types.h>
 
+#include <ddk/driver.h>
 #include <fbl/intrusive_double_list.h>
 #include <fbl/string.h>
 #include <fs/connection.h>
diff --git a/system/core/devmgr/devmgr/drivers.cpp b/system/core/devmgr/devmgr/driver.cpp
similarity index 98%
rename from system/core/devmgr/devmgr/drivers.cpp
rename to system/core/devmgr/devmgr/driver.cpp
index a60ae89..026d9c7 100644
--- a/system/core/devmgr/devmgr/drivers.cpp
+++ b/system/core/devmgr/devmgr/driver.cpp
@@ -13,8 +13,7 @@
 #include "../shared/env.h"
 #include "../shared/fdio.h"
 #include "../shared/log.h"
-#include "coordinator.h"
-#include "devmgr.h"
+#include "driver.h"
 
 #include <driver-info/driver-info.h>
 
diff --git a/system/core/devmgr/devmgr/driver.h b/system/core/devmgr/devmgr/driver.h
new file mode 100644
index 0000000..4b938c4
--- /dev/null
+++ b/system/core/devmgr/devmgr/driver.h
@@ -0,0 +1,42 @@
+// Copyright 2019 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.
+
+#pragma once
+
+#include <ddk/binding.h>
+#include <fbl/intrusive_double_list.h>
+#include <fbl/string.h>
+#include <fbl/unique_ptr.h>
+#include <lib/fit/function.h>
+#include <lib/zx/vmo.h>
+
+namespace devmgr {
+
+struct Driver {
+    Driver() = default;
+
+    fbl::String name;
+    fbl::unique_ptr<const zx_bind_inst_t[]> binding;
+    // Binding size in number of bytes, not number of entries
+    // TODO: Change it to number of entries
+    uint32_t binding_size = 0;
+    uint32_t flags = 0;
+    zx::vmo dso_vmo;
+
+    fbl::DoublyLinkedListNodeState<Driver*> node;
+    struct Node {
+        static fbl::DoublyLinkedListNodeState<Driver*>& node_state(Driver& obj) { return obj.node; }
+    };
+
+    fbl::String libname;
+};
+
+#define DRIVER_NAME_LEN_MAX 64
+
+using DriverLoadCallback = fit::function<void(Driver* driver, const char* version)>;
+
+void load_driver(const char* path, DriverLoadCallback func);
+void find_loadable_drivers(const char* path, DriverLoadCallback func);
+
+} // namespace devmgr
diff --git a/system/core/devmgr/rules.mk b/system/core/devmgr/rules.mk
index 7aae530..cfcf856 100644
--- a/system/core/devmgr/rules.mk
+++ b/system/core/devmgr/rules.mk
@@ -18,7 +18,7 @@
     $(LOCAL_DIR)/devmgr/devfs.cpp \
     $(LOCAL_DIR)/devmgr/devhost-loader-service.cpp \
     $(LOCAL_DIR)/devmgr/device.cpp \
-    $(LOCAL_DIR)/devmgr/drivers.cpp \
+    $(LOCAL_DIR)/devmgr/driver.cpp \
     $(LOCAL_DIR)/devmgr/fidl.cpp \
     $(LOCAL_DIR)/devmgr/main.cpp \
     $(LOCAL_DIR)/shared/env.cpp \
@@ -82,7 +82,7 @@
     $(LOCAL_DIR)/devmgr/devhost-loader-service.cpp \
     $(LOCAL_DIR)/devmgr/devfs.cpp \
     $(LOCAL_DIR)/devmgr/device.cpp \
-    $(LOCAL_DIR)/devmgr/drivers.cpp \
+    $(LOCAL_DIR)/devmgr/driver.cpp \
     $(LOCAL_DIR)/devmgr/fidl.cpp \
     $(LOCAL_DIR)/devmgr/test-main.cpp \
     $(LOCAL_DIR)/shared/env.cpp \