These changes are the raw update to qemu-2.6.
[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  * 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.
22  */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
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>
32
33 /**
34  * @file
35  *
36  * Device model
37  *
38  */
39
40 /** Registered root devices */
41 static LIST_HEAD ( devices );
42
43 /** Device removal inhibition counter */
44 int device_keep_count = 0;
45
46 /**
47  * Probe a root device
48  *
49  * @v rootdev           Root device
50  * @ret rc              Return status code
51  */
52 static int rootdev_probe ( struct root_device *rootdev ) {
53         int rc;
54
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 ) );
59                 return rc;
60         }
61
62         return 0;
63 }
64
65 /**
66  * Remove a root device
67  *
68  * @v rootdev           Root device
69  */
70 static void rootdev_remove ( struct root_device *rootdev ) {
71         rootdev->driver->remove ( rootdev );
72         DBG ( "Removed %s root bus\n", rootdev->dev.name );
73 }
74
75 /**
76  * Probe all devices
77  *
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.
81  */
82 static void probe_devices ( void ) {
83         struct root_device *rootdev;
84         int rc;
85
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 );
91         }
92 }
93
94 /**
95  * Remove all devices
96  *
97  */
98 static void remove_devices ( int booting __unused ) {
99         struct root_device *rootdev;
100         struct root_device *tmp;
101
102         if ( device_keep_count != 0 ) {
103                 DBG ( "Refusing to remove devices on shutdown\n" );
104                 return;
105         }
106
107         list_for_each_entry_safe ( rootdev, tmp, &devices, dev.siblings ) {
108                 rootdev_remove ( rootdev );
109                 list_del ( &rootdev->dev.siblings );
110         }
111 }
112
113 struct startup_fn startup_devices __startup_fn ( STARTUP_NORMAL ) = {
114         .startup = probe_devices,
115         .shutdown = remove_devices,
116 };
117
118 /**
119  * Identify a device behind an interface
120  *
121  * @v intf              Interface
122  * @ret device          Device, or NULL
123  */
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 );
129         void *device;
130
131         if ( op ) {
132                 device = op ( object );
133         } else {
134                 /* Default is to return NULL */
135                 device = NULL;
136         }
137
138         intf_put ( dest );
139         return device;
140 }