xref: /freebsd/stand/kshim/bsd_kernel.h (revision 2a63c3be)
1 /*-
2  * Copyright (c) 2011 Hans Petter Selasky. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef _BSD_KERNEL_H_
27 #define	_BSD_KERNEL_H_
28 
29 #if !defined(_STANDALONE)
30 #error "_STANDALONE is not defined!"
31 #endif
32 
33 #undef __FreeBSD_version
34 #define	__FreeBSD_version 1400000
35 
36 #include <sys/cdefs.h>
37 #include <sys/queue.h>
38 #include <sys/errno.h>
39 
40 #define	offsetof(type, field) __builtin_offsetof(type, field)
41 #define	howmany(x, y)	(((x)+((y)-1))/(y))
42 #define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
43 #define	isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
44 #define	isdigit(x) ((x) >= '0' && (x) <= '9')
45 #define	panic(...) do { printf("USB PANIC: " __VA_ARGS__); while (1) ; } while (0)
46 #define	rebooting 0
47 #define	M_USB 0
48 #define	M_USBDEV 0
49 #define	USB_PROC_MAX 3
50 #define	USB_BUS_GIANT_PROC(bus) (usb_process + 2)
51 #define	USB_BUS_NON_GIANT_BULK_PROC(bus) (usb_process + 2)
52 #define	USB_BUS_NON_GIANT_ISOC_PROC(bus) (usb_process + 2)
53 #define	USB_BUS_EXPLORE_PROC(bus) (usb_process + 0)
54 #define	USB_BUS_CONTROL_XFER_PROC(bus) (usb_process + 1)
55 #define	SYSCTL_DECL(...)
56 struct sysctl_ctx_list {
57 };
58 struct sysctl_req {
59 	void		*newptr;
60 };
61 #define	SYSCTL_HANDLER_ARGS void *oidp, void *arg1,	\
62 	uint32_t arg2, struct sysctl_req *req
63 #define	SYSCTL_NODE(name,...) struct { } name __used
64 #define	SYSCTL_INT(...)
65 #define	SYSCTL_UINT(...)
66 #define	SYSCTL_PROC(...)
67 #define	SYSCTL_ADD_NODE(...) NULL
68 #define	SYSCTL_ADD_U16(...) NULL
69 #define	SYSCTL_ADD_PROC(...) NULL
70 #define	sysctl_handle_int(...) EOPNOTSUPP
71 #define	sysctl_handle_string(...) EOPNOTSUPP
72 #define	sysctl_ctx_init(ctx) do { (void)(ctx); } while (0)
73 #define	sysctl_ctx_free(ctx) do { (void)(ctx); } while (0)
74 #define	TUNABLE_INT(...)
75 #define	MALLOC_DECLARE(...)
76 #define	MALLOC_DEFINE(...)
77 #define	EVENTHANDLER_DECLARE(...)
78 #define	EVENTHANDLER_INVOKE(...)
79 #define	KASSERT(...)
80 #define	CTASSERT(x) _Static_assert(x, "compile-time assertion failed")
81 #define	SCHEDULER_STOPPED(x) (0)
82 #define	PI_SWI(...) (0)
83 #define	UNIQ_NAME(x) x
84 #define	UNIQ_NAME_STR(x) #x
85 #define	DEVCLASS_MAXUNIT 32
86 #define	MOD_LOAD 1
87 #define	MOD_UNLOAD 2
88 #define	DEVMETHOD(what,func) { #what, (void *)&func }
89 #define	DEVMETHOD_END {0,0}
90 #define	EARLY_DRIVER_MODULE(a, b, c, d, e, f, g)	DRIVER_MODULE(a, b, c, d, e, f)
91 #define	DRIVER_MODULE(name, busname, driver, devclass, evh, arg)	\
92   static struct module_data bsd_##name##_##busname##_driver_mod = {	\
93 	evh, arg, #busname, #name, #busname "/" #name,			\
94 	&driver, &devclass, { 0, 0 } };					\
95 SYSINIT(bsd_##name##_##busname##_driver_mod, SI_SUB_DRIVERS,		\
96   SI_ORDER_MIDDLE, module_register,					\
97   &bsd_##name##_##busname##_driver_mod)
98 #define	SYSINIT(uniq, subs, order, _func, _data)	\
99 const struct sysinit UNIQ_NAME(sysinit_##uniq) = {	\
100 	.func = (_func),				\
101 	.data = __DECONST(void *, _data)		\
102 };							\
103 SYSINIT_ENTRY(uniq##_entry, "sysinit", (subs),		\
104     (order), "const struct sysinit",			\
105     UNIQ_NAME_STR(sysinit_##uniq), "SYSINIT")
106 
107 #define	SYSUNINIT(uniq, subs, order, _func, _data)	\
108 const struct sysinit UNIQ_NAME(sysuninit_##uniq) = {	\
109 	.func = (_func),				\
110 	.data = __DECONST(void *, _data)		\
111 };							\
112 SYSINIT_ENTRY(uniq##_entry, "sysuninit", (subs),	\
113     (order), "const struct sysuninit",			\
114     UNIQ_NAME_STR(sysuninit_##uniq), "SYSUNINIT")
115 #define	MODULE_DEPEND(...)
116 #define	MODULE_VERSION(...)
117 #define	NULL ((void *)0)
118 #define	BUS_SPACE_BARRIER_READ 0x01
119 #define	BUS_SPACE_BARRIER_WRITE 0x02
120 #define	hz 1000
121 #undef PAGE_SIZE
122 #define	PAGE_SIZE 4096
123 #undef PAGE_SHIFT
124 #define	PAGE_SHIFT 12
125 #undef MIN
126 #define	MIN(a,b) (((a) < (b)) ? (a) : (b))
127 #undef MAX
128 #define	MAX(a,b) (((a) > (b)) ? (a) : (b))
129 #define	MTX_DEF 0
130 #define	MTX_SPIN 0
131 #define	MTX_RECURSE 0
132 #define	SX_DUPOK 0
133 #define	SX_NOWITNESS 0
134 #define	WITNESS_WARN(...)
135 #define	cold 0
136 #define	BUS_PROBE_GENERIC 0
137 #define	BUS_PROBE_DEFAULT (-20)
138 #define	CALLOUT_RETURNUNLOCKED 0x1
139 #undef ffs
140 #define	ffs(x) __builtin_ffs(x)
141 #undef va_list
142 #define	va_list __builtin_va_list
143 #undef va_size
144 #define	va_size(type) __builtin_va_size(type)
145 #undef va_start
146 #define	va_start(ap, last) __builtin_va_start(ap, last)
147 #undef va_end
148 #define	va_end(ap) __builtin_va_end(ap)
149 #undef va_arg
150 #define	va_arg(ap, type) __builtin_va_arg((ap), type)
151 #define	DEVICE_ATTACH(dev, ...) \
152   (((device_attach_t *)(device_get_method(dev, "device_attach")))(dev,## __VA_ARGS__))
153 #define	DEVICE_DETACH(dev, ...) \
154   (((device_detach_t *)(device_get_method(dev, "device_detach")))(dev,## __VA_ARGS__))
155 #define	DEVICE_PROBE(dev, ...) \
156   (((device_probe_t *)(device_get_method(dev, "device_probe")))(dev,## __VA_ARGS__))
157 #define	DEVICE_RESUME(dev, ...) \
158   (((device_resume_t *)(device_get_method(dev, "device_resume")))(dev,## __VA_ARGS__))
159 #define	DEVICE_SHUTDOWN(dev, ...) \
160   (((device_shutdown_t *)(device_get_method(dev, "device_shutdown")))(dev,## __VA_ARGS__))
161 #define	DEVICE_SUSPEND(dev, ...) \
162   (((device_suspend_t *)(device_get_method(dev, "device_suspend")))(dev,## __VA_ARGS__))
163 #define	USB_HANDLE_REQUEST(dev, ...) \
164   (((usb_handle_request_t *)(device_get_method(dev, "usb_handle_request")))(dev,## __VA_ARGS__))
165 #define	USB_TAKE_CONTROLLER(dev, ...) \
166   (((usb_take_controller_t *)(device_get_method(dev, "usb_take_controller")))(dev,## __VA_ARGS__))
167 #define	GPIO_PIN_SET(dev, ...) \
168   (((gpio_pin_set_t *)(device_get_method(dev, "gpio_pin_set")))(dev,## __VA_ARGS__))
169 #define	GPIO_PIN_SETFLAGS(dev, ...) \
170   (((gpio_pin_setflags_t *)(device_get_method(dev, "gpio_pin_setflags")))(dev,## __VA_ARGS__))
171 
172 enum {
173 	SI_SUB_DUMMY = 0x0000000,
174 	SI_SUB_LOCK = 0x1B00000,
175 	SI_SUB_KLD = 0x2000000,
176 	SI_SUB_DRIVERS = 0x3100000,
177 	SI_SUB_PSEUDO = 0x7000000,
178 	SI_SUB_KICK_SCHEDULER = 0xa000000,
179 	SI_SUB_RUN_SCHEDULER = 0xfffffff
180 };
181 
182 enum {
183 	SI_ORDER_FIRST = 0x0000000,
184 	SI_ORDER_SECOND = 0x0000001,
185 	SI_ORDER_THIRD = 0x0000002,
186 	SI_ORDER_FOURTH = 0x0000003,
187 	SI_ORDER_MIDDLE = 0x1000000,
188 	SI_ORDER_ANY = 0xfffffff	/* last */
189 };
190 
191 struct uio;
192 struct thread;
193 struct malloc_type;
194 struct usb_process;
195 
196 #ifndef INT32_MAX
197 #define	INT32_MAX 0x7fffffff
198 #endif
199 
200 #ifndef HAVE_STANDARD_DEFS
201 #define	_UINT8_T_DECLARED
202 typedef unsigned char uint8_t;
203 #define	_INT8_T_DECLARED
204 typedef signed char int8_t;
205 #define	_UINT16_T_DECLARED
206 typedef unsigned short uint16_t;
207 #define	_INT16_T_DECLARED
208 typedef signed short int16_t;
209 #define	_UINT32_T_DECLARED
210 typedef unsigned int uint32_t;
211 #define	_INT32_T_DECLARED
212 typedef signed int int32_t;
213 #define	_UINT64_T_DECLARED
214 #ifndef __LP64__
215 typedef unsigned long long uint64_t;
216 #else
217 typedef unsigned long uint64_t;
218 #endif
219 #define	_INT64_T_DECLARED
220 #ifndef __LP64__
221 typedef signed long long int64_t;
222 #else
223 typedef signed long int64_t;
224 #endif
225 
226 typedef uint16_t uid_t;
227 typedef uint16_t gid_t;
228 typedef uint16_t mode_t;
229 
230 typedef uint8_t *caddr_t;
231 #define	_UINTPTR_T_DECLARED
232 typedef unsigned long uintptr_t;
233 #define	_UINTMAX_T_DECLARED
234 typedef unsigned long uintmax_t;
235 typedef unsigned long vm_paddr_t;
236 
237 #define	_SIZE_T_DECLARED
238 typedef unsigned long size_t;
239 #define	_SSIZE_T_DECLARED
240 typedef signed long ssize_t;
241 #define	_OFF_T_DECLARED
242 typedef unsigned long off_t;
243 
244 typedef int64_t sbintime_t;
245 
246 typedef unsigned char   u_char;
247 typedef unsigned short  u_short;
248 typedef unsigned int    u_int;
249 typedef unsigned long   u_long;
250 #endif
251 
252 typedef unsigned long bus_addr_t;
253 typedef unsigned long bus_size_t;
254 
255 typedef struct bus_dma_segment {
256 	bus_addr_t	ds_addr;	/* DMA address */
257 	bus_size_t	ds_len;		/* length of transfer */
258 } bus_dma_segment_t;
259 
260 struct bus_dma_tag {
261 	uint32_t	alignment;
262 	uint32_t	maxsize;
263 };
264 
265 typedef void *bus_dmamap_t;
266 typedef struct bus_dma_tag *bus_dma_tag_t;
267 
268 typedef enum {
269 	BUS_DMA_LOCK	= 0x01,
270 	BUS_DMA_UNLOCK	= 0x02,
271 } bus_dma_lock_op_t;
272 
273 typedef void *bus_space_tag_t;
274 typedef uint8_t *bus_space_handle_t;
275 typedef int bus_dma_filter_t(void *, bus_addr_t);
276 typedef void bus_dma_lock_t(void *, bus_dma_lock_op_t);
277 
278 #ifndef __bool_true_false_are_defined
279 #define	__bool_true_false_are_defined
280 typedef _Bool bool;
281 #define	true 1
282 #define	false 0
283 #endif
284 
285 /* SYSINIT API */
286 
287 #include <sysinit.h>
288 
289 struct sysinit {
290 	void    (*func) (void *arg);
291 	void   *data;
292 };
293 
294 /* MUTEX API */
295 
296 struct mtx {
297 	int	owned;
298 	struct mtx *parent;
299 };
300 
301 #define	mtx_assert(...) do { } while (0)
302 void	mtx_init(struct mtx *, const char *, const char *, int);
303 void	mtx_lock(struct mtx *);
304 void	mtx_unlock(struct mtx *);
305 #define	mtx_lock_spin(x) mtx_lock(x)
306 #define	mtx_unlock_spin(x) mtx_unlock(x)
307 int	mtx_owned(struct mtx *);
308 void	mtx_destroy(struct mtx *);
309 
310 extern struct mtx Giant;
311 
312 /* SX API */
313 
314 struct sx {
315 	int	owned;
316 };
317 
318 #define	sx_assert(...) do { } while (0)
319 #define	sx_init(...) sx_init_flags(__VA_ARGS__, 0)
320 void	sx_init_flags(struct sx *, const char *, int);
321 void	sx_destroy(struct sx *);
322 void	sx_xlock(struct sx *);
323 void	sx_xunlock(struct sx *);
324 int	sx_xlocked(struct sx *);
325 
326 /* CONDVAR API */
327 
328 struct cv {
329 	int	sleeping;
330 };
331 
332 void	cv_init(struct cv *, const char *desc);
333 void	cv_destroy(struct cv *);
334 void	cv_wait(struct cv *, struct mtx *);
335 int	cv_timedwait(struct cv *, struct mtx *, int);
336 void	cv_signal(struct cv *);
337 void	cv_broadcast(struct cv *);
338 
339 /* CALLOUT API */
340 
341 typedef void callout_fn_t (void *);
342 
343 extern volatile int ticks;
344 
345 struct callout {
346 	LIST_ENTRY(callout) entry;
347 	callout_fn_t *c_func;
348 	void   *c_arg;
349 	struct mtx *mtx;
350 	int	flags;
351 	int	timeout;
352 };
353 
354 void	callout_init_mtx(struct callout *, struct mtx *, int);
355 void	callout_reset(struct callout *, int, callout_fn_t *, void *);
356 void	callout_stop(struct callout *);
357 void	callout_drain(struct callout *);
358 int	callout_pending(struct callout *);
359 void	callout_process(int timeout);
360 
361 /* DEVICE API */
362 
363 struct driver;
364 struct devclass;
365 struct device;
366 struct module;
367 struct module_data;
368 struct sbuf;
369 
370 typedef struct driver driver_t;
371 typedef struct devclass *devclass_t;
372 typedef struct device *device_t;
373 typedef void (driver_intr_t)(void *arg);
374 typedef int (driver_filter_t)(void *arg);
375 #define	FILTER_STRAY		0x01
376 #define	FILTER_HANDLED		0x02
377 #define	FILTER_SCHEDULE_THREAD	0x04
378 
379 typedef int device_attach_t (device_t dev);
380 typedef int device_detach_t (device_t dev);
381 typedef int device_resume_t (device_t dev);
382 typedef int device_shutdown_t (device_t dev);
383 typedef int device_probe_t (device_t dev);
384 typedef int device_suspend_t (device_t dev);
385 typedef int gpio_pin_set_t (device_t dev, uint32_t, unsigned int);
386 typedef int gpio_pin_setflags_t (device_t dev, uint32_t, uint32_t);
387 
388 typedef int bus_child_location_str_t (device_t parent, device_t child, char *buf, size_t buflen);
389 typedef int bus_child_pnpinfo_str_t (device_t parent, device_t child, char *buf, size_t buflen);
390 typedef int bus_child_location_t (device_t parent, device_t child, struct sbuf *);
391 typedef int bus_child_pnpinfo_t (device_t parent, device_t child, struct sbuf *);
392 typedef int bus_get_device_path_t (device_t bus, device_t child, const char *locator, struct sbuf *sb);
393 
394 #define	bus_generic_get_device_path(...) EOPNOTSUPP
395 
396 typedef void bus_driver_added_t (device_t dev, driver_t *driver);
397 
398 struct device_method {
399 	const char *desc;
400 	void   *const func;
401 };
402 
403 typedef struct device_method device_method_t;
404 
405 struct device {
406 	TAILQ_HEAD(device_list, device) dev_children;
407 	TAILQ_ENTRY(device) dev_link;
408 
409 	struct device *dev_parent;
410 	const struct module_data *dev_module;
411 	void   *dev_sc;
412 	void   *dev_aux;
413 	driver_filter_t *dev_irq_filter;
414 	driver_intr_t *dev_irq_fn;
415 	void   *dev_irq_arg;
416 
417 	uint16_t dev_unit;
418 
419 	char	dev_nameunit[64];
420 	char	dev_desc[64];
421 
422 	uint8_t	dev_res_alloc:1;
423 	uint8_t	dev_quiet:1;
424 	uint8_t	dev_softc_set:1;
425 	uint8_t	dev_softc_alloc:1;
426 	uint8_t	dev_attached:1;
427 	uint8_t	dev_fixed_class:1;
428 	uint8_t	dev_unit_manual:1;
429 };
430 
431 struct devclass {
432 	device_t dev_list[DEVCLASS_MAXUNIT];
433 };
434 
435 struct driver {
436 	const char *name;
437 	const struct device_method *methods;
438 	uint32_t size;
439 };
440 
441 struct module_data {
442 	int     (*callback) (struct module *, int, void *arg);
443 	void   *arg;
444 	const char *bus_name;
445 	const char *mod_name;
446 	const char *long_name;
447 	const struct driver *driver;
448 	struct devclass **devclass_pp;
449 	TAILQ_ENTRY(module_data) entry;
450 };
451 
452 device_t device_get_parent(device_t dev);
453 void   *device_get_method(device_t dev, const char *what);
454 const char *device_get_name(device_t dev);
455 const char *device_get_nameunit(device_t dev);
456 
457 #define	device_printf(dev, fmt,...) \
458 	printf("%s: " fmt, device_get_nameunit(dev),## __VA_ARGS__)
459 device_t device_add_child(device_t dev, const char *name, int unit);
460 void	device_quiet(device_t dev);
461 void	device_set_interrupt(device_t dev, driver_filter_t *, driver_intr_t *, void *);
462 void	device_run_interrupts(device_t parent);
463 void	device_set_ivars(device_t dev, void *ivars);
464 void   *device_get_ivars(device_t dev);
465 const char *device_get_desc(device_t dev);
466 int	device_probe_and_attach(device_t dev);
467 int	device_detach(device_t dev);
468 void   *device_get_softc(device_t dev);
469 void	device_set_softc(device_t dev, void *softc);
470 int	device_delete_child(device_t dev, device_t child);
471 int	device_delete_children(device_t dev);
472 int	device_is_attached(device_t dev);
473 void	device_set_desc(device_t dev, const char *desc);
474 void	device_set_desc_copy(device_t dev, const char *desc);
475 int	device_get_unit(device_t dev);
476 void   *devclass_get_softc(devclass_t dc, int unit);
477 int	devclass_get_maxunit(devclass_t dc);
478 device_t devclass_get_device(devclass_t dc, int unit);
479 devclass_t devclass_find(const char *classname);
480 
481 #define	BUS_LOCATOR_UEFI "UEFI"
482 #define	bus_get_dma_tag(...) (NULL)
483 #define	bus_topo_lock(...) mtx_lock(&Giant)
484 #define	bus_topo_unlock(...) mtx_unlock(&Giant)
485 int	bus_generic_detach(device_t dev);
486 int	bus_generic_resume(device_t dev);
487 int	bus_generic_shutdown(device_t dev);
488 int	bus_generic_suspend(device_t dev);
489 int	bus_generic_print_child(device_t dev, device_t child);
490 void	bus_generic_driver_added(device_t dev, driver_t *driver);
491 int	bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh,
492     bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp);
493 
494 /* BUS SPACE API */
495 
496 void	bus_space_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t data);
497 void	bus_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t data);
498 void	bus_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t data);
499 
500 uint8_t	bus_space_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
501 uint16_t bus_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
502 uint32_t bus_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
503 
504 void	bus_space_read_multi_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t *datap, bus_size_t count);
505 void	bus_space_read_multi_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t *datap, bus_size_t count);
506 void	bus_space_read_multi_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t *datap, bus_size_t count);
507 
508 void	bus_space_write_multi_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t *datap, bus_size_t count);
509 void	bus_space_write_multi_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t *datap, bus_size_t count);
510 void	bus_space_write_multi_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t *datap, bus_size_t count);
511 
512 void	bus_space_read_region_1(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint8_t *datap, bus_size_t count);
513 void	bus_space_write_region_1(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint8_t *datap, bus_size_t count);
514 void	bus_space_read_region_4(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint32_t *datap, bus_size_t count);
515 void	bus_space_write_region_4(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, uint32_t *datap, bus_size_t count);
516 
517 void	bus_space_barrier(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, bus_size_t length, int flags);
518 
519 void	module_register(void *);
520 
521 /* LIB-C */
522 
523 void   *memset(void *, int, size_t len);
524 void   *memcpy(void *, const void *, size_t len);
525 int	memcmp(const void *, const void *, size_t len);
526 int	printf(const char *,...) __printflike(1, 2);
527 int	snprintf(char *restrict str, size_t size, const char *restrict format,...) __printflike(3, 4);
528 size_t	strlen(const char *s);
529 int	strcmp(const char *, const char *);
530 
531 /* MALLOC API */
532 
533 #undef malloc
534 #define	malloc(s,x,f) usb_malloc(s)
535 void   *usb_malloc(size_t);
536 
537 #undef free
538 #define	free(p,x) usb_free(p)
539 void	usb_free(void *);
540 
541 #define	strdup(p,x) usb_strdup(p)
542 char   *usb_strdup(const char *str);
543 
544 /* ENDIANNESS */
545 
546 #ifndef HAVE_ENDIAN_DEFS
547 
548 /* Assume little endian */
549 
550 #define	htole64(x) ((uint64_t)(x))
551 #define	le64toh(x) ((uint64_t)(x))
552 
553 #define	htole32(x) ((uint32_t)(x))
554 #define	le32toh(x) ((uint32_t)(x))
555 
556 #define	htole16(x) ((uint16_t)(x))
557 #define	le16toh(x) ((uint16_t)(x))
558 
559 #define	be32toh(x) ((uint32_t)(x))
560 #define	htobe32(x) ((uint32_t)(x))
561 
562 #else
563 #include <sys/endian.h>
564 #endif
565 
566 /* USB */
567 
568 typedef int usb_handle_request_t (device_t dev, const void *req, void **pptr, uint16_t *plen, uint16_t offset, uint8_t *pstate);
569 typedef int usb_take_controller_t (device_t dev);
570 
571 void	usb_idle(void);
572 void	usb_init(void);
573 void	usb_uninit(void);
574 
575 /* set some defaults */
576 
577 #ifndef USB_POOL_SIZE
578 #define	USB_POOL_SIZE (1024*1024)	/* 1 MByte */
579 #endif
580 
581 int	pause(const char *, int);
582 void	DELAY(unsigned int);
583 
584 /* OTHER */
585 
586 struct selinfo {
587 };
588 
589 /* SYSTEM STARTUP API */
590 
591 extern const void *sysinit_data[];
592 extern const void *sysuninit_data[];
593 
594 /* Resources */
595 
596 enum intr_type {
597 	INTR_TYPE_TTY = 1,
598 	INTR_TYPE_BIO = 2,
599 	INTR_TYPE_NET = 4,
600 	INTR_TYPE_CAM = 8,
601 	INTR_TYPE_MISC = 16,
602 	INTR_TYPE_CLK = 32,
603 	INTR_TYPE_AV = 64,
604 	INTR_EXCL = 256,		/* exclusive interrupt */
605 	INTR_MPSAFE = 512,		/* this interrupt is SMP safe */
606 	INTR_ENTROPY = 1024,		/* this interrupt provides entropy */
607 	INTR_MD1 = 4096,		/* flag reserved for MD use */
608 	INTR_MD2 = 8192,		/* flag reserved for MD use */
609 	INTR_MD3 = 16384,		/* flag reserved for MD use */
610 	INTR_MD4 = 32768		/* flag reserved for MD use */
611 };
612 
613 struct resource_i {
614 	u_long		r_start;	/* index of the first entry in this resource */
615 	u_long		r_end;		/* index of the last entry (inclusive) */
616 };
617 
618 struct resource {
619 	struct resource_i	*__r_i;
620 	bus_space_tag_t		r_bustag; /* bus_space tag */
621 	bus_space_handle_t	r_bushandle;	/* bus_space handle */
622 };
623 
624 struct resource_spec {
625 	int	type;
626 	int	rid;
627 	int	flags;
628 };
629 
630 #define	SYS_RES_IRQ	1	/* interrupt lines */
631 #define	SYS_RES_DRQ	2	/* isa dma lines */
632 #define	SYS_RES_MEMORY	3	/* i/o memory */
633 #define	SYS_RES_IOPORT	4	/* i/o ports */
634 
635 #define	RF_ALLOCATED	0x0001	/* resource has been reserved */
636 #define	RF_ACTIVE	0x0002	/* resource allocation has been activated */
637 #define	RF_SHAREABLE	0x0004	/* resource permits contemporaneous sharing */
638 #define	RF_SPARE1	0x0008
639 #define	RF_SPARE2	0x0010
640 #define	RF_FIRSTSHARE	0x0020	/* first in sharing list */
641 #define	RF_PREFETCHABLE	0x0040	/* resource is prefetchable */
642 #define	RF_OPTIONAL	0x0080	/* for bus_alloc_resources() */
643 
644 int bus_alloc_resources(device_t, struct resource_spec *, struct resource **);
645 int bus_release_resource(device_t, int, int, struct resource *);
646 void bus_release_resources(device_t, const struct resource_spec *,
647     struct resource **);
648 struct resource *bus_alloc_resource_any(device_t, int, int *, unsigned int);
649 int bus_generic_attach(device_t);
650 bus_space_tag_t rman_get_bustag(struct resource *);
651 bus_space_handle_t rman_get_bushandle(struct resource *);
652 u_long rman_get_size(struct resource *);
653 int bus_setup_intr(device_t, struct resource *, int, driver_filter_t,
654     driver_intr_t, void *, void **);
655 int bus_teardown_intr(device_t, struct resource *, void *);
656 int ofw_bus_status_okay(device_t);
657 int ofw_bus_is_compatible(device_t, char *);
658 
659 extern int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev,
660     int type, int *rid, unsigned int flags);
661 extern int (*ofw_bus_status_ok_cb)(device_t dev);
662 extern int (*ofw_bus_is_compatible_cb)(device_t dev, char *name);
663 
664 #ifndef strlcpy
665 #define	strlcpy(d,s,n) snprintf((d),(n),"%s",(s))
666 #endif
667 
668 /* Should be defined in user application since it is machine-dependent */
669 extern int delay(unsigned int);
670 
671 /* BUS dma */
672 #define	BUS_SPACE_MAXADDR_24BIT	0xFFFFFF
673 #define	BUS_SPACE_MAXADDR_32BIT	0xFFFFFFFF
674 #define	BUS_SPACE_MAXADDR	0xFFFFFFFF
675 #define	BUS_SPACE_MAXSIZE_24BIT	0xFFFFFF
676 #define	BUS_SPACE_MAXSIZE_32BIT	0xFFFFFFFF
677 #define	BUS_SPACE_MAXSIZE	0xFFFFFFFF
678 
679 #define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
680 #define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
681 #define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
682 #define	BUS_DMA_COHERENT	0x04	/* hint: map memory in a coherent way */
683 #define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
684 #define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
685 #define	BUS_DMA_BUS2		0x20
686 #define	BUS_DMA_BUS3		0x40
687 #define	BUS_DMA_BUS4		0x80
688 
689 #define	BUS_DMASYNC_PREREAD	0x01
690 #define	BUS_DMASYNC_POSTREAD	0x02
691 #define	BUS_DMASYNC_PREWRITE	0x04
692 #define	BUS_DMASYNC_POSTWRITE	0x08
693 
694 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
695 
696 int
697 bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
698 		   bus_size_t boundary, bus_addr_t lowaddr,
699 		   bus_addr_t highaddr, bus_dma_filter_t *filter,
700 		   void *filterarg, bus_size_t maxsize, int nsegments,
701 		   bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
702 		   void *lockfuncarg, bus_dma_tag_t *dmat);
703 
704 int bus_dmamem_alloc(bus_dma_tag_t, void** vaddr, int flags, bus_dmamap_t *);
705 void bus_dmamem_free(bus_dma_tag_t, void *vaddr, bus_dmamap_t);
706 int bus_dma_tag_destroy(bus_dma_tag_t);
707 
708 int bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *buf,
709     bus_size_t buflen, bus_dmamap_callback_t *,
710     void *callback_arg, int flags);
711 void bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
712 void bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t map, int flags);
713 
714 /* SBUF */
715 #define	sbuf_printf(...) do { } while (0)
716 
717 #endif					/* _BSD_KERNEL_H_ */
718