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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
27 #include <ipxe/list.h>
28 #include <ipxe/tables.h>
29 #include <ipxe/init.h>
30 #include <ipxe/interface.h>
31 #include <ipxe/device.h>
40 /** Registered root devices */
41 static LIST_HEAD ( devices );
43 /** Device removal inhibition counter */
44 int device_keep_count = 0;
49 * @v rootdev Root device
50 * @ret rc Return status code
52 static int rootdev_probe ( struct root_device *rootdev ) {
55 DBG ( "Adding %s root bus\n", rootdev->dev.name );
56 if ( ( rc = rootdev->driver->probe ( rootdev ) ) != 0 ) {
57 DBG ( "Failed to add %s root bus: %s\n",
58 rootdev->dev.name, strerror ( rc ) );
66 * Remove a root device
68 * @v rootdev Root device
70 static void rootdev_remove ( struct root_device *rootdev ) {
71 rootdev->driver->remove ( rootdev );
72 DBG ( "Removed %s root bus\n", rootdev->dev.name );
78 * This initiates probing for all devices in the system. After this
79 * call, the device hierarchy will be populated, and all hardware
80 * should be ready to use.
82 static void probe_devices ( void ) {
83 struct root_device *rootdev;
86 for_each_table_entry ( rootdev, ROOT_DEVICES ) {
87 list_add ( &rootdev->dev.siblings, &devices );
88 INIT_LIST_HEAD ( &rootdev->dev.children );
89 if ( ( rc = rootdev_probe ( rootdev ) ) != 0 )
90 list_del ( &rootdev->dev.siblings );
98 static void remove_devices ( int booting __unused ) {
99 struct root_device *rootdev;
100 struct root_device *tmp;
102 if ( device_keep_count != 0 ) {
103 DBG ( "Refusing to remove devices on shutdown\n" );
107 list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
108 rootdev_remove ( rootdev );
109 list_del ( &rootdev->dev.siblings );
113 struct startup_fn startup_devices __startup_fn ( STARTUP_NORMAL ) = {
114 .startup = probe_devices,
115 .shutdown = remove_devices,
119 * Identify a device behind an interface
122 * @ret device Device, or NULL
124 struct device * identify_device ( struct interface *intf ) {
125 struct interface *dest;
126 identify_device_TYPE ( void * ) *op =
127 intf_get_dest_op ( intf, identify_device, &dest );
128 void *object = intf_object ( dest );
132 device = op ( object );
134 /* Default is to return NULL */