iPXE
device.c
Go to the documentation of this file.
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 */
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  .name = "devices",
115  .startup = probe_devices,
116  .shutdown = remove_devices,
117 };
118 
119 /**
120  * Identify a device behind an interface
121  *
122  * @v intf Interface
123  * @ret device Device, or NULL
124  */
125 struct device * identify_device ( struct interface *intf ) {
126  struct interface *dest;
127  identify_device_TYPE ( void * ) *op =
129  void *object = intf_object ( dest );
130  void *device;
131 
132  if ( op ) {
133  device = op ( object );
134  } else {
135  /* Default is to return NULL */
136  device = NULL;
137  }
138 
139  intf_put ( dest );
140  return device;
141 }
#define STARTUP_NORMAL
Normal startup.
Definition: init.h:63
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:69
int device_keep_count
Device removal inhibition counter.
Definition: device.c:44
void(* remove)(struct root_device *rootdev)
Remove root device.
Definition: device.h:125
char name[40]
Name.
Definition: device.h:75
A root device.
Definition: device.h:94
#define ROOT_DEVICES
Root device table.
Definition: device.h:129
uint16_t device
Device ID.
Definition: ena.h:24
struct device dev
Device chain.
Definition: device.h:99
const char * name
Definition: init.h:42
void * intf_object(struct interface *intf)
Get pointer to object containing object interface.
Definition: interface.c:154
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
A startup/shutdown function.
Definition: init.h:41
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
static LIST_HEAD(devices)
Registered root devices.
A hardware device.
Definition: device.h:73
An object interface.
Definition: interface.h:109
struct root_driver * driver
Root device driver.
Definition: device.h:101
Object interfaces.
static void * dest
Definition: strings.h:176
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:447
Linked lists.
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void remove_devices(int booting __unused)
Remove all devices.
Definition: device.c:98
struct list_head siblings
Devices on the same bus.
Definition: device.h:81
uint8_t * tmp
Definition: entropy.h:156
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:358
static int rootdev_probe(struct root_device *rootdev)
Probe a root device.
Definition: device.c:52
static void rootdev_remove(struct root_device *rootdev)
Remove a root device.
Definition: device.c:70
int(* probe)(struct root_device *rootdev)
Add root device.
Definition: device.h:116
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
static void probe_devices(void)
Probe all devices.
Definition: device.c:82
struct list_head children
Devices attached to this device.
Definition: device.h:83
void intf_put(struct interface *intf)
Decrement reference count on an object interface.
Definition: interface.c:144
#define identify_device_TYPE(object_type)
Definition: device.h:174
Linker tables.
Device model.
struct startup_fn startup_devices __startup_fn(STARTUP_NORMAL)
struct device * identify_device(struct interface *intf)
Identify a device behind an interface.
Definition: device.c:125
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
String functions.
#define intf_get_dest_op(intf, type, dest)
Get object interface destination and operation method.
Definition: interface.h:214