xref: /haiku/src/tests/system/kernel/device_manager/playground/device_manager.h (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2  * Copyright 2004-2008, Haiku Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT license.
4  */
5 #ifndef _DEVICE_MANAGER_H
6 #define _DEVICE_MANAGER_H
7 
8 
9 #include <TypeConstants.h>
10 #include <Drivers.h>
11 #include <module.h>
12 
13 
14 /* type of I/O resource */
15 enum {
16 	B_IO_MEMORY			= 1,
17 	B_IO_PORT			= 2,
18 	B_ISA_DMA_CHANNEL	= 3
19 };
20 
21 
22 /* I/O resource description */
23 typedef struct {
24 	uint32	type;
25 		/* type of I/O resource */
26 
27 	uint32	base;
28 		/* I/O memory: first physical address (32 bit)
29 		 * I/O port: first port address (16 bit)
30 		 * ISA DMA channel: channel number (0-7)
31 		 */
32 
33 	uint32	length;
34 		/* I/O memory: size of address range (32 bit)
35 		 * I/O port: size of port range (16 bit)
36 		 * ISA DMA channel: must be 1
37 		 */
38 } io_resource;
39 
40 /* attribute of a device node */
41 typedef struct {
42 	const char		*name;
43 	type_code		type;			/* for supported types, see value */
44 	union {
45 		uint8		ui8;			/* B_UINT8_TYPE */
46 		uint16		ui16;			/* B_UINT16_TYPE */
47 		uint32		ui32;			/* B_UINT32_TYPE */
48 		uint64		ui64;			/* B_UINT64_TYPE */
49 		const char	*string;		/* B_STRING_TYPE */
50 		struct {					/* B_RAW_TYPE */
51 			const void *data;
52 			size_t	length;
53 		} raw;
54 	} value;
55 } device_attr;
56 
57 
58 typedef struct device_node device_node;
59 typedef struct driver_module_info driver_module_info;
60 
61 
62 /* interface of the device manager */
63 
64 typedef struct device_manager_info {
65 	module_info info;
66 
67 	status_t (*rescan_node)(device_node *node);
68 
69 	status_t (*register_node)(device_node *parent, const char *moduleName,
70 					const device_attr *attrs, const io_resource *ioResources,
71 					device_node **_node);
72 	status_t (*unregister_node)(device_node *node);
73 
74 	status_t (*get_driver)(device_node *node, driver_module_info **_module,
75 					void **_cookie);
76 
77 	device_node *(*get_root_node)();
78 	status_t (*get_next_child_node)(device_node *parent,
79 					const device_attr *attrs, device_node **node);
80 	device_node *(*get_parent_node)(device_node *node);
81 	void (*put_node)(device_node *node);
82 
83 	status_t (*publish_device)(device_node *node, const char *path,
84 					const char *deviceModuleName);
85 	status_t (*unpublish_device)(device_node *node, const char *path);
86 
87 #if 0
88 	status_t (*acquire_io_resources)(io_resource *resources);
89 	status_t (*release_io_resources)(const io_resource *resources);
90 
91 	int32 (*create_id)(const char *generator);
92 	status_t (*free_id)(const char *generator, uint32 id);
93 #endif
94 
95 	status_t (*get_attr_uint8)(const device_node *node, const char *name,
96 					uint8 *value, bool recursive);
97 	status_t (*get_attr_uint16)(const device_node *node, const char *name,
98 					uint16 *value, bool recursive);
99 	status_t (*get_attr_uint32)(const device_node *node, const char *name,
100 					uint32 *value, bool recursive);
101 	status_t (*get_attr_uint64)(const device_node *node, const char *name,
102 					uint64 *value, bool recursive);
103 	status_t (*get_attr_string)(const device_node *node, const char *name,
104 					const char **_value, bool recursive);
105 	status_t (*get_attr_raw)(const device_node *node, const char *name,
106 					const void **_data, size_t *_size, bool recursive);
107 
108 	status_t (*get_next_attr)(device_node *node, device_attr **_attr);
109 } device_manager_info;
110 
111 
112 #define B_DEVICE_MANAGER_MODULE_NAME "system/device_manager/v1"
113 
114 
115 /* interface of device driver */
116 
117 struct driver_module_info {
118 	module_info info;
119 
120 	float (*supports_device)(device_node *parent);
121 	status_t (*register_device)(device_node *parent);
122 
123 	status_t (*init_driver)(device_node *node, void **_driverCookie);
124 	void (*uninit_driver)(void *driverCookie);
125 	status_t (*register_child_devices)(void *driverCookie);
126 	status_t (*rescan_child_devices)(void *driverCookie);
127 
128 	void (*device_removed)(void *driverCookie);
129 	status_t (*suspend)(void *driverCookie, int32 state);
130 	status_t (*resume)(void *driverCookie);
131 };
132 
133 
134 /* standard device node attributes */
135 
136 #define B_DEVICE_PRETTY_NAME		"device/pretty name"		/* string */
137 #define B_DEVICE_MAPPING			"device/mapping"			/* string */
138 #define B_DEVICE_BUS				"device/bus"				/* string */
139 #define B_DEVICE_FIXED_CHILD		"device/fixed child"		/* string */
140 #define B_DEVICE_FLAGS				"device/flags"				/* uint32 */
141 
142 #define B_DEVICE_VENDOR_ID			"device/vendor"				/* uint16 */
143 #define B_DEVICE_ID					"device/id"					/* uint16 */
144 #define B_DEVICE_TYPE				"device/type"
145 	/* uint16, PCI base class */
146 #define B_DEVICE_SUB_TYPE			"device/subtype"
147 	/* uint16, PCI sub type */
148 #define B_DEVICE_INTERFACE			"device/interface"
149 	/* uint16, PCI class API */
150 
151 #define B_DEVICE_UNIQUE_ID			"device/unique id"			/* string */
152 
153 /* device flags */
154 #define B_FIND_CHILD_ON_DEMAND		0x01
155 #define B_FIND_MULTIPLE_CHILDREN	0x02
156 #define B_KEEP_DRIVER_LOADED		0x04
157 
158 
159 /* interface of device */
160 
161 typedef struct io_request io_request;
162 
163 struct device_module_info {
164 	module_info info;
165 
166 	status_t (*init_device)(void *driverCookie, void **_deviceCookie);
167 	void (*uninit_device)(void *deviceCookie);
168 	void (*device_removed)(void *deviceCookie);
169 
170 	status_t (*device_open)(void *deviceCookie, int openMode, void **_cookie);
171 	status_t (*device_close)(void *cookie);
172 	status_t (*device_free)(void *cookie);
173 	status_t (*device_read)(void *cookie, off_t pos, void *buffer,
174 		size_t *_length);
175 	status_t (*device_write)(void *cookie, off_t pos, const void *buffer,
176 		size_t *_length);
177 	status_t (*device_ioctl)(void *cookie, int32 op, void *buffer,
178 		size_t length);
179 	status_t (*device_io)(void *cookie, io_request *request);
180 };
181 
182 extern struct device_manager_info *gDeviceManager;
183 
184 #endif	/* _DEVICE_MANAGER_H */
185