[device] Provide a driver-private data field for root devices
[ipxe.git] / src / include / ipxe / device.h
1 #ifndef _IPXE_DEVICE_H
2 #define _IPXE_DEVICE_H
3
4 /**
5 * @file
6 *
7 * Device model
8 *
9 */
10
11 FILE_LICENCE ( GPL2_OR_LATER );
12
13 #include <ipxe/list.h>
14 #include <ipxe/tables.h>
15
16 struct interface;
17
18 /** A hardware device description */
19 struct device_description {
20 /** Bus type
21 *
22 * This must be a BUS_TYPE_XXX constant.
23 */
24 unsigned int bus_type;
25 /** Location
26 *
27 * The interpretation of this field is bus-type-specific.
28 */
29 unsigned int location;
30 /** Vendor ID */
31 unsigned int vendor;
32 /** Device ID */
33 unsigned int device;
34 /** Device class */
35 unsigned long class;
36 /** I/O address */
37 unsigned long ioaddr;
38 /** IRQ */
39 unsigned int irq;
40 };
41
42 /** PCI bus type */
43 #define BUS_TYPE_PCI 1
44
45 /** ISAPnP bus type */
46 #define BUS_TYPE_ISAPNP 2
47
48 /** EISA bus type */
49 #define BUS_TYPE_EISA 3
50
51 /** MCA bus type */
52 #define BUS_TYPE_MCA 4
53
54 /** ISA bus type */
55 #define BUS_TYPE_ISA 5
56
57 /** TAP bus type */
58 #define BUS_TYPE_TAP 6
59
60 /** EFI bus type */
61 #define BUS_TYPE_EFI 7
62
63 /** Xen bus type */
64 #define BUS_TYPE_XEN 8
65
66 /** A hardware device */
67 struct device {
68 /** Name */
69 char name[16];
70 /** Driver name */
71 const char *driver_name;
72 /** Device description */
73 struct device_description desc;
74 /** Devices on the same bus */
75 struct list_head siblings;
76 /** Devices attached to this device */
77 struct list_head children;
78 /** Bus device */
79 struct device *parent;
80 };
81
82 /**
83 * A root device
84 *
85 * Root devices are system buses such as PCI, EISA, etc.
86 *
87 */
88 struct root_device {
89 /** Device chain
90 *
91 * A root device has a NULL parent field.
92 */
93 struct device dev;
94 /** Root device driver */
95 struct root_driver *driver;
96 /** Driver-private data */
97 void *priv;
98 };
99
100 /** A root device driver */
101 struct root_driver {
102 /**
103 * Add root device
104 *
105 * @v rootdev Root device
106 * @ret rc Return status code
107 *
108 * Called from probe_devices() for all root devices in the build.
109 */
110 int ( * probe ) ( struct root_device *rootdev );
111 /**
112 * Remove root device
113 *
114 * @v rootdev Root device
115 *
116 * Called from remove_device() for all successfully-probed
117 * root devices.
118 */
119 void ( * remove ) ( struct root_device *rootdev );
120 };
121
122 /** Root device table */
123 #define ROOT_DEVICES __table ( struct root_device, "root_devices" )
124
125 /** Declare a root device */
126 #define __root_device __table_entry ( ROOT_DEVICES, 01 )
127
128 /**
129 * Set root device driver-private data
130 *
131 * @v rootdev Root device
132 * @v priv Private data
133 */
134 static inline void rootdev_set_drvdata ( struct root_device *rootdev,
135 void *priv ){
136 rootdev->priv = priv;
137 }
138
139 /**
140 * Get root device driver-private data
141 *
142 * @v rootdev Root device
143 * @ret priv Private data
144 */
145 static inline void * rootdev_get_drvdata ( struct root_device *rootdev ) {
146 return rootdev->priv;
147 }
148
149 extern int device_keep_count;
150
151 /**
152 * Prevent devices from being removed on shutdown
153 *
154 */
155 static inline void devices_get ( void ) {
156 device_keep_count++;
157 }
158
159 /**
160 * Allow devices to be removed on shutdown
161 *
162 */
163 static inline void devices_put ( void ) {
164 device_keep_count--;
165 }
166
167 extern struct device * identify_device ( struct interface *intf );
168 #define identify_device_TYPE( object_type ) \
169 typeof ( struct device * ( object_type ) )
170
171 #endif /* _IPXE_DEVICE_H */