1246145Shselasky/* $FreeBSD: stable/11/stand/kshim/bsd_kernel.h 329183 2018-02-12 20:51:28Z kevans $ */
2246145Shselasky/*-
3246145Shselasky * Copyright (c) 2011 Hans Petter Selasky. All rights reserved.
4246145Shselasky *
5246145Shselasky * Redistribution and use in source and binary forms, with or without
6246145Shselasky * modification, are permitted provided that the following conditions
7246145Shselasky * are met:
8246145Shselasky * 1. Redistributions of source code must retain the above copyright
9246145Shselasky *    notice, this list of conditions and the following disclaimer.
10246145Shselasky * 2. Redistributions in binary form must reproduce the above copyright
11246145Shselasky *    notice, this list of conditions and the following disclaimer in the
12246145Shselasky *    documentation and/or other materials provided with the distribution.
13246145Shselasky *
14246145Shselasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15246145Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16246145Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17246145Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18246145Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19246145Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20246145Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21246145Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22246145Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23246145Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24246145Shselasky * SUCH DAMAGE.
25246145Shselasky */
26246145Shselasky
27246145Shselasky#ifndef _BSD_KERNEL_H_
28246145Shselasky#define	_BSD_KERNEL_H_
29246145Shselasky
30246145Shselasky#define	_KERNEL
31266882Shselasky#undef __FreeBSD_version
32266882Shselasky#define	__FreeBSD_version 1100000
33246145Shselasky
34246145Shselasky#include <sys/cdefs.h>
35246145Shselasky#include <sys/queue.h>
36246145Shselasky#include <sys/errno.h>
37246145Shselasky
38298650Shselasky#define	howmany(x, y)	(((x)+((y)-1))/(y))
39298650Shselasky#define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
40246145Shselasky#define	isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
41246145Shselasky#define	isdigit(x) ((x) >= '0' && (x) <= '9')
42246145Shselasky#define	panic(...) do { printf("USB PANIC: " __VA_ARGS__); while (1) ; } while (0)
43267183Shselasky#define	rebooting 0
44246145Shselasky#define	M_USB 0
45246145Shselasky#define	M_USBDEV 0
46246145Shselasky#define	USB_PROC_MAX 3
47246363Shselasky#define	USB_BUS_GIANT_PROC(bus) (usb_process + 2)
48286773Shselasky#define	USB_BUS_NON_GIANT_BULK_PROC(bus) (usb_process + 2)
49286773Shselasky#define	USB_BUS_NON_GIANT_ISOC_PROC(bus) (usb_process + 2)
50246363Shselasky#define	USB_BUS_EXPLORE_PROC(bus) (usb_process + 0)
51246363Shselasky#define	USB_BUS_CONTROL_XFER_PROC(bus) (usb_process + 1)
52246145Shselasky#define	SYSCTL_DECL(...)
53329183Skevansstruct sysctl_ctx_list {
54329183Skevans};
55294547Swmastruct sysctl_req {
56294547Swma	void		*newptr;
57294547Swma};
58294547Swma#define	SYSCTL_HANDLER_ARGS void *oidp, void *arg1,	\
59294547Swma	uint32_t arg2, struct sysctl_req *req
60246145Shselasky#define	SYSCTL_NODE(name,...) struct { } name __used
61246145Shselasky#define	SYSCTL_INT(...)
62294547Swma#define	SYSCTL_UINT(...)
63294547Swma#define	SYSCTL_PROC(...)
64329183Skevans#define	SYSCTL_ADD_NODE(...) NULL
65329183Skevans#define	SYSCTL_ADD_U16(...) NULL
66329183Skevans#define	SYSCTL_ADD_PROC(...) NULL
67329183Skevans#define	sysctl_handle_int(...) EOPNOTSUPP
68329183Skevans#define	sysctl_handle_string(...) EOPNOTSUPP
69329183Skevans#define	sysctl_ctx_init(ctx) do { (void)(ctx); } while (0)
70329183Skevans#define	sysctl_ctx_free(ctx) do { (void)(ctx); } while (0)
71246145Shselasky#define	TUNABLE_INT(...)
72246145Shselasky#define	MALLOC_DECLARE(...)
73246145Shselasky#define	MALLOC_DEFINE(...)
74246145Shselasky#define	EVENTHANDLER_DECLARE(...)
75246145Shselasky#define	EVENTHANDLER_INVOKE(...)
76246145Shselasky#define	KASSERT(...)
77246145Shselasky#define	SCHEDULER_STOPPED(x) (0)
78246145Shselasky#define	PI_SWI(...) (0)
79246145Shselasky#define	UNIQ_NAME(x) x
80246145Shselasky#define	UNIQ_NAME_STR(x) #x
81246145Shselasky#define	DEVCLASS_MAXUNIT 32
82246145Shselasky#define	MOD_LOAD 1
83246145Shselasky#define	MOD_UNLOAD 2
84246145Shselasky#define	DEVMETHOD(what,func) { #what, (void *)&func }
85246145Shselasky#define	DEVMETHOD_END {0,0}
86294547Swma#define	EARLY_DRIVER_MODULE(a, b, c, d, e, f, g)	DRIVER_MODULE(a, b, c, d, e, f)
87246145Shselasky#define	DRIVER_MODULE(name, busname, driver, devclass, evh, arg)	\
88246145Shselasky  static struct module_data bsd_##name##_##busname##_driver_mod = {	\
89246145Shselasky	evh, arg, #busname, #name, #busname "/" #name,			\
90246145Shselasky	&driver, &devclass, { 0, 0 } };					\
91246145ShselaskySYSINIT(bsd_##name##_##busname##_driver_mod, SI_SUB_DRIVERS,		\
92246145Shselasky  SI_ORDER_MIDDLE, module_register,					\
93246145Shselasky  &bsd_##name##_##busname##_driver_mod)
94246145Shselasky#define	SYSINIT(uniq, subs, order, _func, _data)	\
95246145Shselaskyconst struct sysinit UNIQ_NAME(sysinit_##uniq) = {	\
96246145Shselasky	.func = (_func),				\
97246145Shselasky	.data = __DECONST(void *, _data)		\
98246145Shselasky};							\
99246145ShselaskySYSINIT_ENTRY(uniq##_entry, "sysinit", (subs),		\
100246145Shselasky    (order), "const struct sysinit",			\
101246145Shselasky    UNIQ_NAME_STR(sysinit_##uniq), "SYSINIT")
102246145Shselasky
103246145Shselasky#define	SYSUNINIT(uniq, subs, order, _func, _data)	\
104246145Shselaskyconst struct sysinit UNIQ_NAME(sysuninit_##uniq) = {	\
105246145Shselasky	.func = (_func),				\
106246145Shselasky	.data = __DECONST(void *, _data)		\
107246145Shselasky};							\
108246145ShselaskySYSINIT_ENTRY(uniq##_entry, "sysuninit", (subs),	\
109246145Shselasky    (order), "const struct sysuninit",			\
110246145Shselasky    UNIQ_NAME_STR(sysuninit_##uniq), "SYSUNINIT")
111246145Shselasky#define	MODULE_DEPEND(...)
112246145Shselasky#define	MODULE_VERSION(...)
113246145Shselasky#define	NULL ((void *)0)
114246145Shselasky#define	BUS_SPACE_BARRIER_READ 0x01
115246145Shselasky#define	BUS_SPACE_BARRIER_WRITE 0x02
116246145Shselasky#define	hz 1000
117266882Shselasky#undef PAGE_SIZE
118246145Shselasky#define	PAGE_SIZE 4096
119266882Shselasky#undef MIN
120246145Shselasky#define	MIN(a,b) (((a) < (b)) ? (a) : (b))
121266882Shselasky#undef MAX
122246145Shselasky#define	MAX(a,b) (((a) > (b)) ? (a) : (b))
123246145Shselasky#define	MTX_DEF 0
124266396Shselasky#define	MTX_SPIN 0
125246145Shselasky#define	MTX_RECURSE 0
126246145Shselasky#define	SX_DUPOK 0
127246145Shselasky#define	SX_NOWITNESS 0
128246145Shselasky#define	WITNESS_WARN(...)
129246145Shselasky#define	cold 0
130246145Shselasky#define	BUS_PROBE_GENERIC 0
131291405Szbb#define	BUS_PROBE_DEFAULT (-20)
132246145Shselasky#define	CALLOUT_RETURNUNLOCKED 0x1
133275506Shselasky#undef ffs
134275506Shselasky#define	ffs(x) __builtin_ffs(x)
135266882Shselasky#undef va_list
136246145Shselasky#define	va_list __builtin_va_list
137266882Shselasky#undef va_size
138246145Shselasky#define	va_size(type) __builtin_va_size(type)
139266882Shselasky#undef va_start
140246145Shselasky#define	va_start(ap, last) __builtin_va_start(ap, last)
141266882Shselasky#undef va_end
142246145Shselasky#define	va_end(ap) __builtin_va_end(ap)
143266882Shselasky#undef va_arg
144246145Shselasky#define	va_arg(ap, type) __builtin_va_arg((ap), type)
145246145Shselasky#define	DEVICE_ATTACH(dev, ...) \
146246145Shselasky  (((device_attach_t *)(device_get_method(dev, "device_attach")))(dev,## __VA_ARGS__))
147246145Shselasky#define	DEVICE_DETACH(dev, ...) \
148246145Shselasky  (((device_detach_t *)(device_get_method(dev, "device_detach")))(dev,## __VA_ARGS__))
149246145Shselasky#define	DEVICE_PROBE(dev, ...) \
150246145Shselasky  (((device_probe_t *)(device_get_method(dev, "device_probe")))(dev,## __VA_ARGS__))
151246145Shselasky#define	DEVICE_RESUME(dev, ...) \
152246145Shselasky  (((device_resume_t *)(device_get_method(dev, "device_resume")))(dev,## __VA_ARGS__))
153246145Shselasky#define	DEVICE_SHUTDOWN(dev, ...) \
154246145Shselasky  (((device_shutdown_t *)(device_get_method(dev, "device_shutdown")))(dev,## __VA_ARGS__))
155246145Shselasky#define	DEVICE_SUSPEND(dev, ...) \
156246145Shselasky  (((device_suspend_t *)(device_get_method(dev, "device_suspend")))(dev,## __VA_ARGS__))
157246145Shselasky#define	USB_HANDLE_REQUEST(dev, ...) \
158246145Shselasky  (((usb_handle_request_t *)(device_get_method(dev, "usb_handle_request")))(dev,## __VA_ARGS__))
159246145Shselasky#define	USB_TAKE_CONTROLLER(dev, ...) \
160246145Shselasky  (((usb_take_controller_t *)(device_get_method(dev, "usb_take_controller")))(dev,## __VA_ARGS__))
161291405Szbb#define	GPIO_PIN_SET(dev, ...) \
162291405Szbb  (((gpio_pin_set_t *)(device_get_method(dev, "gpio_pin_set")))(dev,## __VA_ARGS__))
163291405Szbb#define	GPIO_PIN_SETFLAGS(dev, ...) \
164291405Szbb  (((gpio_pin_setflags_t *)(device_get_method(dev, "gpio_pin_setflags")))(dev,## __VA_ARGS__))
165246145Shselasky
166246145Shselaskyenum {
167246145Shselasky	SI_SUB_DUMMY = 0x0000000,
168246145Shselasky	SI_SUB_LOCK = 0x1B00000,
169246145Shselasky	SI_SUB_KLD = 0x2000000,
170246145Shselasky	SI_SUB_DRIVERS = 0x3100000,
171246145Shselasky	SI_SUB_PSEUDO = 0x7000000,
172246145Shselasky	SI_SUB_KICK_SCHEDULER = 0xa000000,
173246145Shselasky	SI_SUB_RUN_SCHEDULER = 0xfffffff
174246145Shselasky};
175246145Shselasky
176246145Shselaskyenum {
177246145Shselasky	SI_ORDER_FIRST = 0x0000000,
178246145Shselasky	SI_ORDER_SECOND = 0x0000001,
179246145Shselasky	SI_ORDER_THIRD = 0x0000002,
180246145Shselasky	SI_ORDER_FOURTH = 0x0000003,
181246145Shselasky	SI_ORDER_MIDDLE = 0x1000000,
182246145Shselasky	SI_ORDER_ANY = 0xfffffff	/* last */
183246145Shselasky};
184246145Shselasky
185246145Shselaskystruct uio;
186246145Shselaskystruct thread;
187246145Shselaskystruct malloc_type;
188246145Shselaskystruct usb_process;
189246145Shselasky
190329183Skevans#ifndef INT32_MAX
191329183Skevans#define	INT32_MAX 0x7fffffff
192329183Skevans#endif
193329183Skevans
194266882Shselasky#ifndef HAVE_STANDARD_DEFS
195266882Shselasky#define	_UINT8_T_DECLARED
196246145Shselaskytypedef unsigned char uint8_t;
197266882Shselasky#define	_INT8_T_DECLARED
198246145Shselaskytypedef signed char int8_t;
199266882Shselasky#define	_UINT16_T_DECLARED
200246145Shselaskytypedef unsigned short uint16_t;
201266882Shselasky#define	_INT16_T_DECLARED
202246145Shselaskytypedef signed short int16_t;
203266882Shselasky#define	_UINT32_T_DECLARED
204246145Shselaskytypedef unsigned int uint32_t;
205266882Shselasky#define	_INT32_T_DECLARED
206246145Shselaskytypedef signed int int32_t;
207266882Shselasky#define	_UINT64_T_DECLARED
208246145Shselaskytypedef unsigned long long uint64_t;
209266882Shselasky#define	_INT16_T_DECLARED
210246145Shselaskytypedef signed long long int64_t;
211246145Shselasky
212266882Shselaskytypedef uint16_t uid_t;
213266882Shselaskytypedef uint16_t gid_t;
214266882Shselaskytypedef uint16_t mode_t;
215246145Shselasky
216266882Shselaskytypedef uint8_t *caddr_t;
217267183Shselasky#define	_UINTPTR_T_DECLARED
218266882Shselaskytypedef unsigned long uintptr_t;
219266882Shselasky
220267183Shselasky#define	_SIZE_T_DECLARED
221246145Shselaskytypedef unsigned long size_t;
222246145Shselaskytypedef unsigned long u_long;
223266882Shselasky#endif
224246145Shselasky
225266882Shselaskytypedef unsigned long bus_addr_t;
226266882Shselaskytypedef unsigned long bus_size_t;
227266882Shselasky
228294547Swmatypedef struct bus_dma_segment {
229294547Swma	bus_addr_t	ds_addr;	/* DMA address */
230294547Swma	bus_size_t	ds_len;		/* length of transfer */
231294547Swma} bus_dma_segment_t;
232294547Swma
233294547Swmastruct bus_dma_tag {
234294547Swma	uint32_t	alignment;
235294547Swma	uint32_t	maxsize;
236294547Swma};
237294547Swma
238246145Shselaskytypedef void *bus_dmamap_t;
239294547Swmatypedef struct bus_dma_tag *bus_dma_tag_t;
240246145Shselasky
241294547Swmatypedef enum {
242294547Swma	BUS_DMA_LOCK	= 0x01,
243294547Swma	BUS_DMA_UNLOCK	= 0x02,
244294547Swma} bus_dma_lock_op_t;
245294547Swma
246246145Shselaskytypedef void *bus_space_tag_t;
247246145Shselaskytypedef uint8_t *bus_space_handle_t;
248294547Swmatypedef int bus_dma_filter_t(void *, bus_addr_t);
249294547Swmatypedef void bus_dma_lock_t(void *, bus_dma_lock_op_t);
250246145Shselasky
251294547Swmatypedef uint32_t bool;
252294547Swma
253246145Shselasky/* SYSINIT API */
254246145Shselasky
255246145Shselasky#include <sysinit.h>
256246145Shselasky
257246145Shselaskystruct sysinit {
258246145Shselasky	void    (*func) (void *arg);
259246145Shselasky	void   *data;
260246145Shselasky};
261246145Shselasky
262246145Shselasky/* MUTEX API */
263246145Shselasky
264246145Shselaskystruct mtx {
265246145Shselasky	int	owned;
266246145Shselasky	struct mtx *parent;
267246145Shselasky};
268246145Shselasky
269246145Shselasky#define	mtx_assert(...) do { } while (0)
270246145Shselaskyvoid	mtx_init(struct mtx *, const char *, const char *, int);
271246145Shselaskyvoid	mtx_lock(struct mtx *);
272246145Shselaskyvoid	mtx_unlock(struct mtx *);
273266396Shselasky#define	mtx_lock_spin(x) mtx_lock(x)
274266396Shselasky#define	mtx_unlock_spin(x) mtx_unlock(x)
275246145Shselaskyint	mtx_owned(struct mtx *);
276246145Shselaskyvoid	mtx_destroy(struct mtx *);
277246145Shselasky
278246145Shselaskyextern struct mtx Giant;
279246145Shselasky
280246145Shselasky/* SX API */
281246145Shselasky
282246145Shselaskystruct sx {
283246145Shselasky	int	owned;
284246145Shselasky};
285246145Shselasky
286246145Shselasky#define	sx_assert(...) do { } while (0)
287246145Shselasky#define	sx_init(...) sx_init_flags(__VA_ARGS__, 0)
288246145Shselaskyvoid	sx_init_flags(struct sx *, const char *, int);
289246145Shselaskyvoid	sx_destroy(struct sx *);
290246145Shselaskyvoid	sx_xlock(struct sx *);
291246145Shselaskyvoid	sx_xunlock(struct sx *);
292246145Shselaskyint	sx_xlocked(struct sx *);
293246145Shselasky
294246145Shselasky/* CONDVAR API */
295246145Shselasky
296246145Shselaskystruct cv {
297246145Shselasky	int	sleeping;
298246145Shselasky};
299246145Shselasky
300246145Shselaskyvoid	cv_init(struct cv *, const char *desc);
301246145Shselaskyvoid	cv_destroy(struct cv *);
302246145Shselaskyvoid	cv_wait(struct cv *, struct mtx *);
303246145Shselaskyint	cv_timedwait(struct cv *, struct mtx *, int);
304246145Shselaskyvoid	cv_signal(struct cv *);
305246145Shselaskyvoid	cv_broadcast(struct cv *);
306246145Shselasky
307246145Shselasky/* CALLOUT API */
308246145Shselasky
309246145Shselaskytypedef void callout_fn_t (void *);
310246145Shselasky
311246145Shselaskyextern volatile int ticks;
312246145Shselasky
313246145Shselaskystruct callout {
314246145Shselasky	LIST_ENTRY(callout) entry;
315306255Shselasky	callout_fn_t *c_func;
316306255Shselasky	void   *c_arg;
317246145Shselasky	struct mtx *mtx;
318246145Shselasky	int	flags;
319246145Shselasky	int	timeout;
320246145Shselasky};
321246145Shselasky
322246145Shselaskyvoid	callout_init_mtx(struct callout *, struct mtx *, int);
323246145Shselaskyvoid	callout_reset(struct callout *, int, callout_fn_t *, void *);
324246145Shselaskyvoid	callout_stop(struct callout *);
325246145Shselaskyvoid	callout_drain(struct callout *);
326246145Shselaskyint	callout_pending(struct callout *);
327246145Shselaskyvoid	callout_process(int timeout);
328246145Shselasky
329246145Shselasky/* DEVICE API */
330246145Shselasky
331246145Shselaskystruct driver;
332246145Shselaskystruct devclass;
333246145Shselaskystruct device;
334246145Shselaskystruct module;
335246145Shselaskystruct module_data;
336246145Shselasky
337246145Shselaskytypedef struct driver driver_t;
338246145Shselaskytypedef struct devclass *devclass_t;
339246145Shselaskytypedef struct device *device_t;
340266396Shselaskytypedef void (driver_intr_t)(void *arg);
341266396Shselaskytypedef int (driver_filter_t)(void *arg);
342266396Shselasky#define	FILTER_STRAY		0x01
343266396Shselasky#define	FILTER_HANDLED		0x02
344266396Shselasky#define	FILTER_SCHEDULE_THREAD	0x04
345246145Shselasky
346246145Shselaskytypedef int device_attach_t (device_t dev);
347246145Shselaskytypedef int device_detach_t (device_t dev);
348246145Shselaskytypedef int device_resume_t (device_t dev);
349246145Shselaskytypedef int device_shutdown_t (device_t dev);
350246145Shselaskytypedef int device_probe_t (device_t dev);
351246145Shselaskytypedef int device_suspend_t (device_t dev);
352291405Szbbtypedef int gpio_pin_set_t (device_t dev, uint32_t, unsigned int);
353291405Szbbtypedef int gpio_pin_setflags_t (device_t dev, uint32_t, uint32_t);
354246145Shselasky
355246145Shselaskytypedef int bus_child_location_str_t (device_t parent, device_t child, char *buf, size_t buflen);
356246145Shselaskytypedef int bus_child_pnpinfo_str_t (device_t parent, device_t child, char *buf, size_t buflen);
357246145Shselaskytypedef void bus_driver_added_t (device_t dev, driver_t *driver);
358246145Shselasky
359246145Shselaskystruct device_method {
360246145Shselasky	const char *desc;
361246145Shselasky	void   *const func;
362246145Shselasky};
363246145Shselasky
364246145Shselaskytypedef struct device_method device_method_t;
365246145Shselasky
366246145Shselaskystruct device {
367246145Shselasky	TAILQ_HEAD(device_list, device) dev_children;
368246145Shselasky	TAILQ_ENTRY(device) dev_link;
369246145Shselasky
370246145Shselasky	struct device *dev_parent;
371246145Shselasky	const struct module_data *dev_module;
372246145Shselasky	void   *dev_sc;
373246145Shselasky	void   *dev_aux;
374266396Shselasky	driver_filter_t *dev_irq_filter;
375266396Shselasky	driver_intr_t *dev_irq_fn;
376246145Shselasky	void   *dev_irq_arg;
377246145Shselasky
378246145Shselasky	uint16_t dev_unit;
379246145Shselasky
380246145Shselasky	char	dev_nameunit[64];
381246145Shselasky	char	dev_desc[64];
382246145Shselasky
383246145Shselasky	uint8_t	dev_res_alloc:1;
384246145Shselasky	uint8_t	dev_quiet:1;
385246145Shselasky	uint8_t	dev_softc_set:1;
386246145Shselasky	uint8_t	dev_softc_alloc:1;
387246145Shselasky	uint8_t	dev_attached:1;
388246145Shselasky	uint8_t	dev_fixed_class:1;
389246145Shselasky	uint8_t	dev_unit_manual:1;
390246145Shselasky};
391246145Shselasky
392246145Shselaskystruct devclass {
393246145Shselasky	device_t dev_list[DEVCLASS_MAXUNIT];
394246145Shselasky};
395246145Shselasky
396246145Shselaskystruct driver {
397246145Shselasky	const char *name;
398246145Shselasky	const struct device_method *methods;
399246145Shselasky	uint32_t size;
400246145Shselasky};
401246145Shselasky
402246145Shselaskystruct module_data {
403246145Shselasky	int     (*callback) (struct module *, int, void *arg);
404246145Shselasky	void   *arg;
405246145Shselasky	const char *bus_name;
406246145Shselasky	const char *mod_name;
407246145Shselasky	const char *long_name;
408246145Shselasky	const struct driver *driver;
409246145Shselasky	struct devclass **devclass_pp;
410246145Shselasky	TAILQ_ENTRY(module_data) entry;
411246145Shselasky};
412246145Shselasky
413246145Shselaskydevice_t device_get_parent(device_t dev);
414246145Shselaskyvoid   *device_get_method(device_t dev, const char *what);
415246145Shselaskyconst char *device_get_name(device_t dev);
416246145Shselaskyconst char *device_get_nameunit(device_t dev);
417246145Shselasky
418246145Shselasky#define	device_printf(dev, fmt,...) \
419246145Shselasky	printf("%s: " fmt, device_get_nameunit(dev),## __VA_ARGS__)
420246145Shselaskydevice_t device_add_child(device_t dev, const char *name, int unit);
421246145Shselaskyvoid	device_quiet(device_t dev);
422266396Shselaskyvoid	device_set_interrupt(device_t dev, driver_filter_t *, driver_intr_t *, void *);
423246145Shselaskyvoid	device_run_interrupts(device_t parent);
424246145Shselaskyvoid	device_set_ivars(device_t dev, void *ivars);
425246145Shselaskyvoid   *device_get_ivars(device_t dev);
426246145Shselaskyconst char *device_get_desc(device_t dev);
427246145Shselaskyint	device_probe_and_attach(device_t dev);
428246145Shselaskyint	device_detach(device_t dev);
429246145Shselaskyvoid   *device_get_softc(device_t dev);
430246145Shselaskyvoid	device_set_softc(device_t dev, void *softc);
431246145Shselaskyint	device_delete_child(device_t dev, device_t child);
432246145Shselaskyint	device_delete_children(device_t dev);
433246145Shselaskyint	device_is_attached(device_t dev);
434246145Shselaskyvoid	device_set_desc(device_t dev, const char *desc);
435246145Shselaskyvoid	device_set_desc_copy(device_t dev, const char *desc);
436246145Shselaskyint	device_get_unit(device_t dev);
437246145Shselaskyvoid   *devclass_get_softc(devclass_t dc, int unit);
438246145Shselaskyint	devclass_get_maxunit(devclass_t dc);
439246145Shselaskydevice_t devclass_get_device(devclass_t dc, int unit);
440246145Shselaskydevclass_t devclass_find(const char *classname);
441246145Shselasky
442246145Shselasky#define	bus_get_dma_tag(...) (NULL)
443246145Shselaskyint	bus_generic_detach(device_t dev);
444246145Shselaskyint	bus_generic_resume(device_t dev);
445246145Shselaskyint	bus_generic_shutdown(device_t dev);
446246145Shselaskyint	bus_generic_suspend(device_t dev);
447246145Shselaskyint	bus_generic_print_child(device_t dev, device_t child);
448246145Shselaskyvoid	bus_generic_driver_added(device_t dev, driver_t *driver);
449291400Szbbint	bus_space_subregion(bus_space_tag_t t, bus_space_handle_t bsh,
450291400Szbb    bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp);
451246145Shselasky
452246145Shselasky/* BUS SPACE API */
453246145Shselasky
454246145Shselaskyvoid	bus_space_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t data);
455246145Shselaskyvoid	bus_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t data);
456246145Shselaskyvoid	bus_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t data);
457246145Shselasky
458246145Shselaskyuint8_t	bus_space_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
459246145Shselaskyuint16_t bus_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
460246145Shselaskyuint32_t bus_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
461246145Shselasky
462246145Shselaskyvoid	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);
463246145Shselaskyvoid	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);
464246145Shselaskyvoid	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);
465246145Shselasky
466246145Shselaskyvoid	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);
467246145Shselaskyvoid	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);
468246145Shselaskyvoid	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);
469246145Shselasky
470246145Shselaskyvoid	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);
471246145Shselaskyvoid	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);
472246145Shselaskyvoid	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);
473246145Shselaskyvoid	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);
474246145Shselasky
475246145Shselaskyvoid	bus_space_barrier(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, bus_size_t length, int flags);
476246145Shselasky
477246145Shselaskyvoid	module_register(void *);
478246145Shselasky
479246145Shselasky/* LIB-C */
480246145Shselasky
481246145Shselaskyvoid   *memset(void *, int, size_t len);
482246145Shselaskyvoid   *memcpy(void *, const void *, size_t len);
483246145Shselaskyint	printf(const char *,...) __printflike(1, 2);
484246145Shselaskyint	snprintf(char *restrict str, size_t size, const char *restrict format,...) __printflike(3, 4);
485246145Shselaskysize_t	strlen(const char *s);
486246145Shselasky
487246145Shselasky/* MALLOC API */
488246145Shselasky
489266882Shselasky#undef malloc
490246145Shselasky#define	malloc(s,x,f) usb_malloc(s)
491246145Shselaskyvoid   *usb_malloc(size_t);
492246145Shselasky
493266882Shselasky#undef free
494246145Shselasky#define	free(p,x) usb_free(p)
495246145Shselaskyvoid	usb_free(void *);
496246145Shselasky
497246145Shselasky#define	strdup(p,x) usb_strdup(p)
498246145Shselaskychar   *usb_strdup(const char *str);
499246145Shselasky
500246145Shselasky/* ENDIANNESS */
501246145Shselasky
502266882Shselasky#ifndef HAVE_ENDIAN_DEFS
503266882Shselasky
504246145Shselasky/* Assume little endian */
505246145Shselasky
506246145Shselasky#define	htole64(x) ((uint64_t)(x))
507246145Shselasky#define	le64toh(x) ((uint64_t)(x))
508246145Shselasky
509246145Shselasky#define	htole32(x) ((uint32_t)(x))
510246145Shselasky#define	le32toh(x) ((uint32_t)(x))
511246145Shselasky
512246145Shselasky#define	htole16(x) ((uint16_t)(x))
513246145Shselasky#define	le16toh(x) ((uint16_t)(x))
514246145Shselasky
515246145Shselasky#define	be32toh(x) ((uint32_t)(x))
516246145Shselasky#define	htobe32(x) ((uint32_t)(x))
517246145Shselasky
518266882Shselasky#else
519266882Shselasky#include <sys/endian.h>
520266882Shselasky#endif
521266882Shselasky
522246145Shselasky/* USB */
523246145Shselasky
524246145Shselaskytypedef int usb_handle_request_t (device_t dev, const void *req, void **pptr, uint16_t *plen, uint16_t offset, uint8_t *pstate);
525246145Shselaskytypedef int usb_take_controller_t (device_t dev);
526246145Shselasky
527246145Shselaskyvoid	usb_idle(void);
528246145Shselaskyvoid	usb_init(void);
529246145Shselaskyvoid	usb_uninit(void);
530246145Shselasky
531246145Shselasky/* set some defaults */
532246145Shselasky
533246145Shselasky#ifndef USB_POOL_SIZE
534246145Shselasky#define	USB_POOL_SIZE (1024*1024)	/* 1 MByte */
535246145Shselasky#endif
536246145Shselasky
537246145Shselaskyint	pause(const char *, int);
538246145Shselaskyvoid	DELAY(unsigned int);
539246145Shselasky
540246145Shselasky/* OTHER */
541246145Shselasky
542246145Shselaskystruct selinfo {
543246145Shselasky};
544246145Shselasky
545246145Shselasky/* SYSTEM STARTUP API */
546246145Shselasky
547246145Shselaskyextern const void *sysinit_data[];
548246145Shselaskyextern const void *sysuninit_data[];
549246145Shselasky
550291405Szbb/* Resources */
551291405Szbb
552291405Szbbenum intr_type {
553291405Szbb	INTR_TYPE_TTY = 1,
554291405Szbb	INTR_TYPE_BIO = 2,
555291405Szbb	INTR_TYPE_NET = 4,
556291405Szbb	INTR_TYPE_CAM = 8,
557291405Szbb	INTR_TYPE_MISC = 16,
558291405Szbb	INTR_TYPE_CLK = 32,
559291405Szbb	INTR_TYPE_AV = 64,
560291405Szbb	INTR_EXCL = 256,		/* exclusive interrupt */
561291405Szbb	INTR_MPSAFE = 512,		/* this interrupt is SMP safe */
562291405Szbb	INTR_ENTROPY = 1024,		/* this interrupt provides entropy */
563291405Szbb	INTR_MD1 = 4096,		/* flag reserved for MD use */
564291405Szbb	INTR_MD2 = 8192,		/* flag reserved for MD use */
565291405Szbb	INTR_MD3 = 16384,		/* flag reserved for MD use */
566291405Szbb	INTR_MD4 = 32768		/* flag reserved for MD use */
567291405Szbb};
568291405Szbb
569291405Szbbstruct resource_i {
570291405Szbb	u_long		r_start;	/* index of the first entry in this resource */
571291405Szbb	u_long		r_end;		/* index of the last entry (inclusive) */
572291405Szbb};
573291405Szbb
574291405Szbbstruct resource {
575291405Szbb	struct resource_i	*__r_i;
576291405Szbb	bus_space_tag_t		r_bustag; /* bus_space tag */
577291405Szbb	bus_space_handle_t	r_bushandle;	/* bus_space handle */
578291405Szbb};
579291405Szbb
580291405Szbbstruct resource_spec {
581291405Szbb	int	type;
582291405Szbb	int	rid;
583291405Szbb	int	flags;
584291405Szbb};
585291405Szbb
586291405Szbb#define	SYS_RES_IRQ	1	/* interrupt lines */
587291405Szbb#define	SYS_RES_DRQ	2	/* isa dma lines */
588291405Szbb#define	SYS_RES_MEMORY	3	/* i/o memory */
589291405Szbb#define	SYS_RES_IOPORT	4	/* i/o ports */
590291405Szbb
591291405Szbb#define	RF_ALLOCATED	0x0001	/* resource has been reserved */
592291405Szbb#define	RF_ACTIVE	0x0002	/* resource allocation has been activated */
593291405Szbb#define	RF_SHAREABLE	0x0004	/* resource permits contemporaneous sharing */
594291405Szbb#define	RF_SPARE1	0x0008
595291405Szbb#define	RF_SPARE2	0x0010
596291405Szbb#define	RF_FIRSTSHARE	0x0020	/* first in sharing list */
597291405Szbb#define	RF_PREFETCHABLE	0x0040	/* resource is prefetchable */
598291405Szbb#define	RF_OPTIONAL	0x0080	/* for bus_alloc_resources() */
599291405Szbb
600291405Szbbint bus_alloc_resources(device_t, struct resource_spec *, struct resource **);
601291405Szbbint bus_release_resource(device_t, int, int, struct resource *);
602291405Szbbvoid bus_release_resources(device_t, const struct resource_spec *,
603291405Szbb    struct resource **);
604291405Szbbstruct resource *bus_alloc_resource_any(device_t, int, int *, unsigned int);
605291405Szbbint bus_generic_attach(device_t);
606291405Szbbbus_space_tag_t rman_get_bustag(struct resource *);
607291405Szbbbus_space_handle_t rman_get_bushandle(struct resource *);
608291405Szbbu_long rman_get_size(struct resource *);
609291405Szbbint bus_setup_intr(device_t, struct resource *, int, driver_filter_t,
610291405Szbb    driver_intr_t, void *, void **);
611291405Szbbint bus_teardown_intr(device_t, struct resource *, void *);
612291405Szbbint ofw_bus_status_okay(device_t);
613291405Szbbint ofw_bus_is_compatible(device_t, char *);
614291405Szbb
615291405Szbbextern int (*bus_alloc_resource_any_cb)(struct resource *res, device_t dev,
616291405Szbb    int type, int *rid, unsigned int flags);
617291405Szbbextern int (*ofw_bus_status_ok_cb)(device_t dev);
618291405Szbbextern int (*ofw_bus_is_compatible_cb)(device_t dev, char *name);
619291405Szbb
620291405Szbb#ifndef strlcpy
621291405Szbb#define	strlcpy(d,s,n) snprintf((d),(n),"%s",(s))
622291405Szbb#endif
623291405Szbb
624294546Swma/* Should be defined in user application since it is machine-dependent */
625294546Swmaextern int delay(unsigned int);
626294546Swma
627294547Swma/* BUS dma */
628294547Swma#define	BUS_SPACE_MAXADDR_24BIT	0xFFFFFF
629294547Swma#define	BUS_SPACE_MAXADDR_32BIT	0xFFFFFFFF
630294547Swma#define	BUS_SPACE_MAXADDR	0xFFFFFFFF
631294547Swma#define	BUS_SPACE_MAXSIZE_24BIT	0xFFFFFF
632294547Swma#define	BUS_SPACE_MAXSIZE_32BIT	0xFFFFFFFF
633294547Swma#define	BUS_SPACE_MAXSIZE	0xFFFFFFFF
634294547Swma
635294547Swma#define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
636294547Swma#define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
637294547Swma#define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
638294547Swma#define	BUS_DMA_COHERENT	0x04	/* hint: map memory in a coherent way */
639294547Swma#define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
640294547Swma#define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
641294547Swma#define	BUS_DMA_BUS2		0x20
642294547Swma#define	BUS_DMA_BUS3		0x40
643294547Swma#define	BUS_DMA_BUS4		0x80
644294547Swma
645294547Swmatypedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
646294547Swma
647294547Swmaint
648294547Swmabus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
649294547Swma		   bus_size_t boundary, bus_addr_t lowaddr,
650294547Swma		   bus_addr_t highaddr, bus_dma_filter_t *filter,
651294547Swma		   void *filterarg, bus_size_t maxsize, int nsegments,
652294547Swma		   bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
653294547Swma		   void *lockfuncarg, bus_dma_tag_t *dmat);
654294547Swma
655294547Swmaint bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
656294547Swma    bus_dmamap_t *mapp);
657294547Swmavoid bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
658294547Swmaint bus_dma_tag_destroy(bus_dma_tag_t dmat);
659294547Swma
660246145Shselasky#endif					/* _BSD_KERNEL_H_ */
661