Browse Source

Added generic device model.

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
ceba6ecb75
2 changed files with 167 additions and 0 deletions
  1. 97
    0
      src/core/device.c
  2. 70
    0
      src/include/gpxe/device.h

+ 97
- 0
src/core/device.c View File

@@ -0,0 +1,97 @@
1
+/*
2
+ * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <gpxe/list.h>
20
+#include <gpxe/tables.h>
21
+#include <gpxe/device.h>
22
+
23
+/**
24
+ * @file
25
+ *
26
+ * Device model
27
+ *
28
+ */
29
+
30
+static struct root_device root_devices[0] __table_start ( root_devices );
31
+static struct root_device root_devices_end[0] __table_end ( root_devices );
32
+
33
+/** Registered root devices */
34
+static LIST_HEAD ( devices );
35
+
36
+/**
37
+ * Register a root device
38
+ *
39
+ * @v rootdev		Root device
40
+ * @ret rc		Return status code
41
+ *
42
+ * Calls the root device driver's probe() method, and adds it to the
43
+ * list of registered root devices if successful.
44
+ */
45
+static int register_rootdev ( struct root_device *rootdev ) {
46
+	int rc;
47
+
48
+	DBG ( "Registering %s root bus\n", rootdev->name );
49
+
50
+	if ( ( rc = rootdev->driver->probe ( rootdev ) ) != 0 )
51
+		return rc;
52
+
53
+	list_add ( &rootdev->dev.siblings, &devices );
54
+	return 0;
55
+}
56
+
57
+/**
58
+ * Unregister a root device
59
+ *
60
+ * @v rootdev		Root device
61
+ */
62
+static void unregister_rootdev ( struct root_device *rootdev ) {
63
+	rootdev->driver->remove ( rootdev );
64
+	list_del ( &rootdev->dev.siblings );
65
+	DBG ( "Unregistered %s root bus\n", rootdev->name );
66
+}
67
+
68
+/**
69
+ * Probe all devices
70
+ *
71
+ * @ret rc		Return status code
72
+ *
73
+ * This initiates probing for all devices in the system.  After this
74
+ * call, the device hierarchy will be populated, and all hardware
75
+ * should be ready to use.
76
+ */
77
+int probe_devices ( void ) {
78
+	struct root_device *rootdev;
79
+
80
+	for ( rootdev = root_devices; rootdev < root_devices_end; rootdev++ ) {
81
+		register_rootdev ( rootdev );
82
+	}
83
+	return 0;
84
+}
85
+
86
+/**
87
+ * Remove all devices
88
+ *
89
+ */
90
+void remove_devices ( void ) {
91
+	struct root_device *rootdev;
92
+	struct root_device *tmp;
93
+
94
+	list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
95
+		unregister_rootdev ( rootdev );
96
+	}
97
+}

+ 70
- 0
src/include/gpxe/device.h View File

@@ -0,0 +1,70 @@
1
+#ifndef _GPXE_DEVICE_H
2
+#define _GPXE_DEVICE_H
3
+
4
+/**
5
+ * @file
6
+ *
7
+ * Device model
8
+ *
9
+ */
10
+
11
+#include <gpxe/list.h>
12
+#include <gpxe/tables.h>
13
+
14
+/** A hardware device */
15
+struct device {
16
+	/** Devices on the same bus */
17
+	struct list_head siblings;
18
+	/** Devices attached to this device */
19
+	struct list_head children;
20
+	/** Bus device */
21
+	struct device *parent;
22
+};
23
+
24
+/**
25
+ * A root device
26
+ *
27
+ * Root devices are system buses such as PCI, EISA, etc.
28
+ *
29
+ */
30
+struct root_device {
31
+	/** Name */
32
+	const char *name;
33
+	/** Device chain
34
+	 *
35
+	 * A root device has a NULL parent field.
36
+	 */
37
+	struct device dev;
38
+	/** Root device driver */
39
+	struct root_driver *driver;
40
+};
41
+
42
+/** A root device driver */
43
+struct root_driver {
44
+	/**
45
+	 * Add root device
46
+	 *
47
+	 * @v rootdev	Root device
48
+	 * @ret rc	Return status code
49
+	 *
50
+	 * Called from probe_devices() for all root devices in the build.
51
+	 */
52
+	int ( * probe ) ( struct root_device *rootdev );
53
+	/**
54
+	 * Remove root device
55
+	 *
56
+	 * @v rootdev	Root device
57
+	 *
58
+	 * Called from remove_device() for all successfully-probed
59
+	 * root devices.
60
+	 */
61
+	void ( * remove ) ( struct root_device *rootdev );
62
+};
63
+
64
+/** Declare a root device */
65
+#define __root_device __table ( root_devices, 01 )
66
+
67
+extern int probe_devices ( void );
68
+extern void remove_devices ( void );
69
+
70
+#endif /* _GPXE_DEVICE_H */

Loading…
Cancel
Save