2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
23 #include <ipxe/list.h>
24 #include <ipxe/tables.h>
25 #include <ipxe/init.h>
26 #include <ipxe/interface.h>
27 #include <ipxe/device.h>
36 /** Registered root devices */
37 static LIST_HEAD ( devices );
39 /** Device removal inhibition counter */
40 int device_keep_count = 0;
45 * @v rootdev Root device
46 * @ret rc Return status code
48 static int rootdev_probe ( struct root_device *rootdev ) {
51 DBG ( "Adding %s root bus\n", rootdev->dev.name );
52 if ( ( rc = rootdev->driver->probe ( rootdev ) ) != 0 ) {
53 DBG ( "Failed to add %s root bus: %s\n",
54 rootdev->dev.name, strerror ( rc ) );
62 * Remove a root device
64 * @v rootdev Root device
66 static void rootdev_remove ( struct root_device *rootdev ) {
67 rootdev->driver->remove ( rootdev );
68 DBG ( "Removed %s root bus\n", rootdev->dev.name );
74 * This initiates probing for all devices in the system. After this
75 * call, the device hierarchy will be populated, and all hardware
76 * should be ready to use.
78 static void probe_devices ( void ) {
79 struct root_device *rootdev;
82 for_each_table_entry ( rootdev, ROOT_DEVICES ) {
83 list_add ( &rootdev->dev.siblings, &devices );
84 INIT_LIST_HEAD ( &rootdev->dev.children );
85 if ( ( rc = rootdev_probe ( rootdev ) ) != 0 )
86 list_del ( &rootdev->dev.siblings );
94 static void remove_devices ( int booting __unused ) {
95 struct root_device *rootdev;
96 struct root_device *tmp;
98 if ( device_keep_count != 0 ) {
99 DBG ( "Refusing to remove devices on shutdown\n" );
103 list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
104 rootdev_remove ( rootdev );
105 list_del ( &rootdev->dev.siblings );
109 struct startup_fn startup_devices __startup_fn ( STARTUP_NORMAL ) = {
110 .startup = probe_devices,
111 .shutdown = remove_devices,
115 * Identify a device behind an interface
118 * @ret device Device, or NULL
120 struct device * identify_device ( struct interface *intf ) {
121 struct interface *dest;
122 identify_device_TYPE ( void * ) *op =
123 intf_get_dest_op ( intf, identify_device, &dest );
124 void *object = intf_object ( dest );
128 device = op ( object );
130 /* Default is to return NULL */