Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / device.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <string.h>
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>
28
29 /**
30  * @file
31  *
32  * Device model
33  *
34  */
35
36 /** Registered root devices */
37 static LIST_HEAD ( devices );
38
39 /** Device removal inhibition counter */
40 int device_keep_count = 0;
41
42 /**
43  * Probe a root device
44  *
45  * @v rootdev           Root device
46  * @ret rc              Return status code
47  */
48 static int rootdev_probe ( struct root_device *rootdev ) {
49         int rc;
50
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 ) );
55                 return rc;
56         }
57
58         return 0;
59 }
60
61 /**
62  * Remove a root device
63  *
64  * @v rootdev           Root device
65  */
66 static void rootdev_remove ( struct root_device *rootdev ) {
67         rootdev->driver->remove ( rootdev );
68         DBG ( "Removed %s root bus\n", rootdev->dev.name );
69 }
70
71 /**
72  * Probe all devices
73  *
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.
77  */
78 static void probe_devices ( void ) {
79         struct root_device *rootdev;
80         int rc;
81
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 );
87         }
88 }
89
90 /**
91  * Remove all devices
92  *
93  */
94 static void remove_devices ( int booting __unused ) {
95         struct root_device *rootdev;
96         struct root_device *tmp;
97
98         if ( device_keep_count != 0 ) {
99                 DBG ( "Refusing to remove devices on shutdown\n" );
100                 return;
101         }
102
103         list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
104                 rootdev_remove ( rootdev );
105                 list_del ( &rootdev->dev.siblings );
106         }
107 }
108
109 struct startup_fn startup_devices __startup_fn ( STARTUP_NORMAL ) = {
110         .startup = probe_devices,
111         .shutdown = remove_devices,
112 };
113
114 /**
115  * Identify a device behind an interface
116  *
117  * @v intf              Interface
118  * @ret device          Device, or NULL
119  */
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 );
125         void *device;
126
127         if ( op ) {
128                 device = op ( object );
129         } else {
130                 /* Default is to return NULL */
131                 device = NULL;
132         }
133
134         intf_put ( dest );
135         return device;
136 }