xref: /haiku/src/libs/compat/freebsd_network/compat/sys/haiku-module.h (revision 4780e4d4df849866072c5db2f6565fd2b7c547ca)
1 /*
2  * Copyright 2009, Colin Günther. All Rights Reserved.
3  * Copyright 2007, Hugo Santos. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _FBSD_COMPAT_SYS_HAIKU_MODULE_H_
7 #define _FBSD_COMPAT_SYS_HAIKU_MODULE_H_
8 
9 
10 #include <Drivers.h>
11 #include <KernelExport.h>
12 
13 #include <kernel/lock.h>
14 #include <sys/method-ids.h>
15 
16 #undef __unused
17 #define __unused
18 
19 #undef ASSERT
20 	/* private/kernel/debug.h sets it */
21 
22 typedef struct device *device_t;
23 typedef struct devclass *devclass_t;
24 
25 typedef int (*device_method_signature_t)(device_t dev);
26 
27 typedef int device_probe_t(device_t dev);
28 typedef int device_attach_t(device_t dev);
29 typedef int device_detach_t(device_t dev);
30 typedef int device_resume_t(device_t dev);
31 typedef int device_shutdown_t(device_t dev);
32 typedef int device_suspend_t(device_t dev);
33 
34 typedef int bus_child_location_str_t(device_t dev __unused, device_t child,
35 	char *buf, size_t buflen);
36 typedef int bus_child_pnpinfo_str_t(device_t dev __unused, device_t child,
37 	char *buf, size_t buflen);
38 typedef void bus_hinted_child_t(device_t dev, const char *name, int unit);
39 typedef int bus_print_child_t(device_t dev, device_t child);
40 typedef int bus_read_ivar_t(device_t dev, device_t child __unused, int which,
41     uintptr_t *result);
42 
43 typedef int miibus_readreg_t(device_t dev, int phy, int reg);
44 typedef int miibus_writereg_t(device_t dev, int phy, int reg, int data);
45 typedef void miibus_statchg_t(device_t dev);
46 typedef void miibus_linkchg_t(device_t dev);
47 typedef void miibus_mediainit_t(device_t dev);
48 
49 
50 struct device_method {
51 	const char* name;
52 	const int32 id;
53 		/* interfaces w/o function pointer structs use IDs for method lookups */
54 	device_method_signature_t method;
55 };
56 
57 typedef struct device_method device_method_t;
58 
59 #define DEVMETHOD(name, func) { #name, ID_##name, (device_method_signature_t)&func }
60 #define DEVMETHOD_END 		  { 0, 0 }
61 
62 
63 typedef struct {
64 	const char* name;
65 	device_method_t* methods;
66 	size_t size; /* softc size */
67 } driver_t;
68 
69 #define DEFINE_CLASS_0(name, driver, methods, size) \
70 	driver_t driver = { #name, methods, size }
71 
72 #define DRIVER_MODULE(name, busname, driver, evh, arg) \
73 	driver_t *DRIVER_MODULE_NAME(name, busname) = &(driver)
74 
75 #define DRIVER_MODULE_ORDERED(name, busname, driver, evh, arg, order) \
76 	DRIVER_MODULE(name, busname, driver, evh, arg)
77 
78 #define DRIVER_MODULE_NAME(name, busname) \
79 	__fbsd_ ## name ## _ ## busname
80 
81 
82 status_t _fbsd_init_hardware(driver_t* pci_drivers[], driver_t* uhub_drivers[]);
83 status_t _fbsd_init_drivers();
84 status_t _fbsd_uninit_drivers();
85 
86 extern const char *gDriverName;
87 driver_t *__haiku_select_miibus_driver(device_t dev);
88 driver_t *__haiku_probe_miibus(device_t dev, driver_t *drivers[]);
89 status_t __haiku_handle_fbsd_drivers_list(status_t (*handler)(driver_t *[], driver_t *[]));
90 
91 status_t init_wlan_stack(void);
92 void uninit_wlan_stack(void);
93 status_t start_wlan(device_t);
94 status_t stop_wlan(device_t);
95 status_t wlan_control(void*, uint32, void*, size_t);
96 status_t wlan_close(void*);
97 
98 /* we define the driver methods with HAIKU_FBSD_DRIVERS_GLUE to
99  * force the rest of the stuff to be linked back with the driver.
100  * While gcc 2.95 packs everything from the static library onto
101  * the final binary, gcc 4.x rightfuly doesn't. */
102 
103 #define HAIKU_FBSD_DRIVERS_CORE_GLUE(publicname)						\
104 	extern const char *gDeviceNameList[];								\
105 	extern device_hooks gDeviceHooks;									\
106 	const char *gDriverName = #publicname;								\
107 	int32 api_version = B_CUR_DRIVER_API_VERSION;						\
108 	status_t init_hardware()											\
109 	{																	\
110 		return __haiku_handle_fbsd_drivers_list(_fbsd_init_hardware);	\
111 	}																	\
112 	status_t init_driver()												\
113 	{																	\
114 		return _fbsd_init_drivers();	\
115 	}																	\
116 	void uninit_driver()												\
117 	{																	\
118 		_fbsd_uninit_drivers();			\
119 	}																	\
120 	const char **publish_devices()										\
121 		{ return gDeviceNameList; }										\
122 	device_hooks *find_device(const char *name)							\
123 		{ return &gDeviceHooks; }
124 
125 #define HAIKU_FBSD_DRIVERS_GLUE(publicname)								\
126 	HAIKU_FBSD_DRIVERS_CORE_GLUE(publicname)							\
127 	status_t init_wlan_stack(void)										\
128 		{ return B_OK; } 												\
129 	void uninit_wlan_stack(void) {}										\
130 	status_t start_wlan(device_t dev)									\
131 		{ return B_OK; }												\
132 	status_t stop_wlan(device_t dev)									\
133 		{ return B_OK; }												\
134 	status_t wlan_control(void *cookie, uint32 op, void *arg, 			\
135 		size_t length)													\
136 		{ return B_BAD_VALUE; }											\
137 	status_t wlan_close(void* cookie)									\
138 		{ return B_OK; }
139 
140 #define HAIKU_FBSD_DRIVER_GLUE(publicname, name, busname)				\
141 	extern driver_t* DRIVER_MODULE_NAME(name, busname);					\
142 	status_t __haiku_handle_fbsd_drivers_list(status_t (*proc)(driver_t *[], driver_t *[])) {\
143 		driver_t *drivers[] = {											\
144 			DRIVER_MODULE_NAME(name, busname),							\
145 			NULL														\
146 		};																\
147 		return (*proc)(drivers, NULL);									\
148 	}																	\
149 	HAIKU_FBSD_DRIVERS_GLUE(publicname);
150 
151 #define HAIKU_FBSD_WLAN_DRIVERS_GLUE(publicname)						\
152 	HAIKU_FBSD_DRIVERS_CORE_GLUE(publicname)
153 
154 #define HAIKU_FBSD_WLAN_DRIVER_GLUE(publicname, name, busname)			\
155 	extern driver_t *DRIVER_MODULE_NAME(name, busname);					\
156 	status_t __haiku_handle_fbsd_drivers_list(status_t (*proc)(driver_t *[], driver_t *[])) {\
157 		driver_t *drivers[] = {											\
158 			DRIVER_MODULE_NAME(name, busname),							\
159 			NULL														\
160 		};																\
161 		return (*proc)(drivers, NULL);									\
162 	}																	\
163 	HAIKU_FBSD_WLAN_DRIVERS_GLUE(publicname);
164 
165 #define HAIKU_FBSD_RETURN_MII_DRIVER(drivers)					\
166 	driver_t *__haiku_select_miibus_driver(device_t dev)		\
167 	{															\
168 		return __haiku_probe_miibus(dev, drivers);				\
169 	}
170 
171 #define HAIKU_FBSD_MII_DRIVER(name)								\
172 	extern driver_t *DRIVER_MODULE_NAME(name, miibus);			\
173 	driver_t *__haiku_select_miibus_driver(device_t dev)		\
174 	{															\
175 		driver_t *drivers[] = {									\
176 			DRIVER_MODULE_NAME(name, miibus),					\
177 			NULL												\
178 		};														\
179 		return __haiku_probe_miibus(dev, drivers);				\
180 	}
181 
182 #define NO_HAIKU_FBSD_MII_DRIVER()								\
183 	HAIKU_FBSD_RETURN_MII_DRIVER(NULL)
184 
185 extern spinlock __haiku_intr_spinlock;
186 extern int __haiku_disable_interrupts(device_t dev);
187 extern void __haiku_reenable_interrupts(device_t dev);
188 
189 #define HAIKU_CHECK_DISABLE_INTERRUPTS		__haiku_disable_interrupts
190 #define HAIKU_REENABLE_INTERRUPTS			__haiku_reenable_interrupts
191 
192 #define NO_HAIKU_CHECK_DISABLE_INTERRUPTS()				\
193 	int HAIKU_CHECK_DISABLE_INTERRUPTS(device_t dev) {	\
194 		panic("should never be called.");				\
195 		return -1; \
196 	}
197 
198 #define NO_HAIKU_REENABLE_INTERRUPTS() \
199 	void HAIKU_REENABLE_INTERRUPTS(device_t dev) {}
200 
201 extern int __haiku_driver_requirements;
202 
203 enum {
204 	FBSD_FAST_TASKQUEUE		= 1 << 0,
205 	FBSD_SWI_TASKQUEUE		= 1 << 1,
206 	FBSD_THREAD_TASKQUEUE	= 1 << 2,
207 	FBSD_WLAN_FEATURE		= 1 << 3,
208 
209 	FBSD_WLAN				= FBSD_WLAN_FEATURE | FBSD_THREAD_TASKQUEUE,
210 	OBSD_WLAN				= FBSD_WLAN_FEATURE | FBSD_FAST_TASKQUEUE,
211 
212 	FBSD_TASKQUEUES = FBSD_FAST_TASKQUEUE | FBSD_SWI_TASKQUEUE | FBSD_THREAD_TASKQUEUE,
213 };
214 
215 #define HAIKU_DRIVER_REQUIREMENTS(flags) \
216 	int __haiku_driver_requirements = (flags)
217 
218 #define HAIKU_DRIVER_REQUIRES(flag) (__haiku_driver_requirements & (flag))
219 
220 
221 /* #pragma mark - firmware loading */
222 
223 
224 /*
225  * Only needed to be specified in the glue code of drivers which actually need
226  * to load firmware. See iprowifi2100 for an example.
227  */
228 
229 extern const uint __haiku_firmware_version;
230 
231 /* Use 0 if driver doesn't care about firmware version. */
232 #define HAIKU_FIRMWARE_VERSION(version) \
233 	const uint __haiku_firmware_version = (version)
234 
235 extern const uint __haiku_firmware_parts_count;
236 extern const char* __haiku_firmware_name_map[][2];
237 
238 /*
239  * Provide a firmware name mapping as a multi-dimentional const char* array.
240  *
241  * HAIKU_FIRMWARE_NAME_MAP({
242  *   {"name-used-by-driver", "actual-name-of-firmware-file-on-disk"},
243  *   ...
244  * });
245  */
246 #define HAIKU_FIRMWARE_NAME_MAP(...) \
247 	const char* __haiku_firmware_name_map[][2] = __VA_ARGS__; \
248 	const uint __haiku_firmware_parts_count = B_COUNT_OF(__haiku_firmware_name_map)
249 
250 #define NO_HAIKU_FIRMWARE_NAME_MAP() \
251 	const uint __haiku_firmware_parts_count = 0; \
252 	const char* __haiku_firmware_name_map[0][2] = {}
253 
254 
255 /* #pragma mark - synchronization */
256 
257 
258 #define HAIKU_INTR_REGISTER_STATE \
259 	cpu_status __haiku_cpu_state = 0
260 
261 #define HAIKU_INTR_REGISTER_ENTER() do {		\
262 	__haiku_cpu_state = disable_interrupts();	\
263 	acquire_spinlock(&__haiku_intr_spinlock);	\
264 } while (0)
265 
266 #define HAIKU_INTR_REGISTER_LEAVE() do {		\
267 	release_spinlock(&__haiku_intr_spinlock);	\
268 	restore_interrupts(__haiku_cpu_state);		\
269 } while (0)
270 
271 #define HAIKU_PROTECT_INTR_REGISTER(x) do {		\
272 	HAIKU_INTR_REGISTER_STATE;					\
273 	HAIKU_INTR_REGISTER_ENTER();				\
274 	x;											\
275 	HAIKU_INTR_REGISTER_LEAVE();				\
276 } while (0)
277 
278 #define nitems(_a)     (sizeof((_a)) / sizeof((_a)[0]))
279 
280 #endif	/* _FBSD_COMPAT_SYS_HAIKU_MODULE_H_ */
281