xref: /haiku/src/tests/system/kernel/device_manager/playground/device_manager.h (revision fc1ca2da5cfcb00ffdf791606d5ae97fdd58a638)
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 	IO_MEM = 1,
17 	IO_PORT = 2,
18 	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 	uint32	length;
33 		// I/O memory: size of address range (32 bit)
34 		// I/O port: size of port range (16 bit)
35 		// ISA DMA channel: must be 1
36 } io_resource;
37 
38 // attribute of a device node
39 typedef struct {
40 	const char		*name;
41 	type_code		type;			// for supported types, see value
42 	union {
43 		uint8		ui8;			// B_UINT8_TYPE
44 		uint16		ui16;			// B_UINT16_TYPE
45 		uint32		ui32;			// B_UINT32_TYPE
46 		uint64		ui64;			// B_UINT64_TYPE
47 		const char	*string;		// B_STRING_TYPE
48 		struct {					// B_RAW_TYPE
49 			const void *data;
50 			size_t	length;
51 		} raw;
52 	} value;
53 } device_attr;
54 
55 
56 typedef struct device_node device_node;
57 typedef struct driver_module_info driver_module_info;
58 
59 
60 // interface of the device manager
61 
62 typedef struct device_manager_info {
63 	module_info info;
64 
65 	status_t (*rescan)(device_node *node);
66 
67 	status_t (*register_device)(device_node *parent, const char *moduleName,
68 					const device_attr *attrs, const io_resource *ioResources,
69 					device_node **_node);
70 	status_t (*unregister_device)(device_node *node);
71 
72 	driver_module_info *(*driver_module)(device_node *node);
73 	void *(*driver_data)(device_node *node);
74 
75 	device_node *(*root_device)();
76 	status_t (*get_next_child_device)(device_node *parent, device_node *node,
77 					const device_attr *attrs);
78 	device_node *(*get_parent)(device_node *node);
79 	void (*put_device_node)(device_node *node);
80 
81 #if 0
82 	status_t (*acquire_io_resources)(io_resource *resources);
83 	status_t (*release_io_resources)(const io_resource *resources);
84 
85 	int32 (*create_id)(const char *generator);
86 	status_t (*free_id)(const char *generator, uint32 id);
87 #endif
88 
89 	status_t (*get_attr_uint8)(device_node *node, const char *name,
90 					uint8 *value, bool recursive);
91 	status_t (*get_attr_uint16)(device_node *node, const char *name,
92 					uint16 *value, bool recursive);
93 	status_t (*get_attr_uint32)(device_node *node, const char *name,
94 					uint32 *value, bool recursive);
95 	status_t (*get_attr_uint64)(device_node *node, const char *name,
96 					uint64 *value, bool recursive);
97 	status_t (*get_attr_string)(device_node *node, const char *name,
98 					const char **_value, bool recursive);
99 	status_t (*get_attr_raw)(device_node *node, const char *name,
100 					const void **_data, size_t *_size, bool recursive);
101 
102 	status_t (*get_next_attr)(device_node *node, device_attr **_attr);
103 } device_manager_info;
104 
105 
106 #define B_DEVICE_MANAGER_MODULE_NAME "system/device_manager/v1"
107 
108 
109 // interface of device driver
110 
111 struct driver_module_info {
112 	module_info info;
113 
114 	float (*supports_device)(device_node *parent);
115 	status_t (*register_device)(device_node *parent);
116 
117 	status_t (*init_driver)(device_node *node, void **_driverData);
118 	void (*uninit_driver)(device_node *node);
119 	status_t (*register_child_devices)(device_node *node);
120 	status_t (*rescan_child_devices)(device_node *node);
121 	void (*device_removed)(device_node *node);
122 };
123 
124 
125 // standard device node attributes
126 
127 #define B_DRIVER_PRETTY_NAME		"driver/pretty name"	// string
128 #define B_DRIVER_MAPPING			"driver/mapping"		// string
129 #define B_DRIVER_IS_BUS				"driver/is_bus"			// uint8
130 #define B_DRIVER_BUS				"driver/bus"			// string
131 #define B_DRIVER_FIXED_CHILD		"fixed child"			// string
132 #define B_DRIVER_FIND_CHILD_FLAGS	"find child flags"		// uint32
133 #define B_DRIVER_UNIQUE_DEVICE_ID	"unique id"				// string
134 #define B_DRIVER_DEVICE_TYPE		"device type"			// string
135 
136 // find child flags
137 #define B_FIND_CHILD_ON_DEMAND		0x01
138 #define B_FIND_MULTIPLE_CHILDREN	0x02
139 
140 // driver types
141 #define B_AUDIO_DRIVER_TYPE			"audio"
142 #define B_BUS_DRIVER_TYPE			"bus"
143 #define B_DISK_DRIVER_TYPE			"disk"
144 #define B_GRAPHICS_DRIVER_TYPE		"graphics"
145 #define B_INPUT_DRIVER_TYPE			"input"
146 #define B_MISC_DRIVER_TYPE			"misc"
147 #define B_NETWORK_DRIVER_TYPE		"net"
148 #define B_VIDEO_DRIVER_TYPE			"video"
149 #define B_INTERRUPT_CONTROLLER_DRIVER_TYPE	"interrupt controller"
150 
151 
152 // interface of device
153 
154 typedef struct io_request io_request;
155 
156 struct device_module_info {
157 	module_info info;
158 
159 	status_t (*init_device)(void *cookie);
160 	void (*uninit_device)(void *cookie);
161 
162 	status_t (*device_open)(void *deviceCookie, int openMode, void **_cookie);
163 	status_t (*device_close)(void *cookie);
164 	status_t (*device_free)(void *cookie);
165 	status_t (*device_read)(void *cookie, off_t pos, void *buffer,
166 		size_t *_length);
167 	status_t (*device_write)(void *cookie, off_t pos, const void *buffer,
168 		size_t *_length);
169 	status_t (*device_ioctl)(void *cookie, int32 op, void *buffer,
170 		size_t length);
171 	status_t (*device_io)(void *cookie, io_request *request);
172 };
173 
174 extern struct device_manager_info *gDeviceManager;
175 
176 #endif	/* _DEVICE_MANAGER_H */
177