]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/kshim/bsd_kernel.h
Update mandoc to cvs snaphot from 20150302
[FreeBSD/FreeBSD.git] / sys / boot / kshim / bsd_kernel.h
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2011 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #ifndef _BSD_KERNEL_H_
28 #define _BSD_KERNEL_H_
29
30 #define _KERNEL
31 #undef __FreeBSD_version
32 #define __FreeBSD_version 1100000
33
34 #include <sys/cdefs.h>
35 #include <sys/queue.h>
36 #include <sys/errno.h>
37
38 #define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
39 #define isdigit(x) ((x) >= '0' && (x) <= '9')
40 #define panic(...) do { printf("USB PANIC: " __VA_ARGS__); while (1) ; } while (0)
41 #define rebooting 0
42 #define M_USB 0
43 #define M_USBDEV 0
44 #define USB_PROC_MAX 3
45 #define USB_BUS_GIANT_PROC(bus) (usb_process + 2)
46 #define USB_BUS_NON_GIANT_PROC(bus) (usb_process + 2)
47 #define USB_BUS_EXPLORE_PROC(bus) (usb_process + 0)
48 #define USB_BUS_CONTROL_XFER_PROC(bus) (usb_process + 1)
49 #define SYSCTL_DECL(...)
50 #define SYSCTL_NODE(name,...) struct { } name __used
51 #define SYSCTL_INT(...)
52 #define TUNABLE_INT(...)
53 #define MALLOC_DECLARE(...)
54 #define MALLOC_DEFINE(...)
55 #define EVENTHANDLER_DECLARE(...)
56 #define EVENTHANDLER_INVOKE(...)
57 #define KASSERT(...)
58 #define SCHEDULER_STOPPED(x) (0)
59 #define PI_SWI(...) (0)
60 #define UNIQ_NAME(x) x
61 #define UNIQ_NAME_STR(x) #x
62 #define DEVCLASS_MAXUNIT 32
63 #define MOD_LOAD 1
64 #define MOD_UNLOAD 2
65 #define DEVMETHOD(what,func) { #what, (void *)&func }
66 #define DEVMETHOD_END {0,0}
67 #define DRIVER_MODULE(name, busname, driver, devclass, evh, arg)        \
68   static struct module_data bsd_##name##_##busname##_driver_mod = {     \
69         evh, arg, #busname, #name, #busname "/" #name,                  \
70         &driver, &devclass, { 0, 0 } };                                 \
71 SYSINIT(bsd_##name##_##busname##_driver_mod, SI_SUB_DRIVERS,            \
72   SI_ORDER_MIDDLE, module_register,                                     \
73   &bsd_##name##_##busname##_driver_mod)
74 #define SYSINIT(uniq, subs, order, _func, _data)        \
75 const struct sysinit UNIQ_NAME(sysinit_##uniq) = {      \
76         .func = (_func),                                \
77         .data = __DECONST(void *, _data)                \
78 };                                                      \
79 SYSINIT_ENTRY(uniq##_entry, "sysinit", (subs),          \
80     (order), "const struct sysinit",                    \
81     UNIQ_NAME_STR(sysinit_##uniq), "SYSINIT")
82
83 #define SYSUNINIT(uniq, subs, order, _func, _data)      \
84 const struct sysinit UNIQ_NAME(sysuninit_##uniq) = {    \
85         .func = (_func),                                \
86         .data = __DECONST(void *, _data)                \
87 };                                                      \
88 SYSINIT_ENTRY(uniq##_entry, "sysuninit", (subs),        \
89     (order), "const struct sysuninit",                  \
90     UNIQ_NAME_STR(sysuninit_##uniq), "SYSUNINIT")
91 #define MODULE_DEPEND(...)
92 #define MODULE_VERSION(...)
93 #define NULL ((void *)0)
94 #define BUS_SPACE_BARRIER_READ 0x01
95 #define BUS_SPACE_BARRIER_WRITE 0x02
96 #define hz 1000
97 #undef PAGE_SIZE
98 #define PAGE_SIZE 4096
99 #undef MIN
100 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
101 #undef MAX
102 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
103 #define MTX_DEF 0
104 #define MTX_SPIN 0
105 #define MTX_RECURSE 0
106 #define SX_DUPOK 0
107 #define SX_NOWITNESS 0
108 #define WITNESS_WARN(...)
109 #define cold 0
110 #define BUS_PROBE_GENERIC 0
111 #define CALLOUT_RETURNUNLOCKED 0x1
112 #undef ffs
113 #define ffs(x) __builtin_ffs(x)
114 #undef va_list
115 #define va_list __builtin_va_list
116 #undef va_size
117 #define va_size(type) __builtin_va_size(type)
118 #undef va_start
119 #define va_start(ap, last) __builtin_va_start(ap, last)
120 #undef va_end
121 #define va_end(ap) __builtin_va_end(ap)
122 #undef va_arg
123 #define va_arg(ap, type) __builtin_va_arg((ap), type)
124 #define DEVICE_ATTACH(dev, ...) \
125   (((device_attach_t *)(device_get_method(dev, "device_attach")))(dev,## __VA_ARGS__))
126 #define DEVICE_DETACH(dev, ...) \
127   (((device_detach_t *)(device_get_method(dev, "device_detach")))(dev,## __VA_ARGS__))
128 #define DEVICE_PROBE(dev, ...) \
129   (((device_probe_t *)(device_get_method(dev, "device_probe")))(dev,## __VA_ARGS__))
130 #define DEVICE_RESUME(dev, ...) \
131   (((device_resume_t *)(device_get_method(dev, "device_resume")))(dev,## __VA_ARGS__))
132 #define DEVICE_SHUTDOWN(dev, ...) \
133   (((device_shutdown_t *)(device_get_method(dev, "device_shutdown")))(dev,## __VA_ARGS__))
134 #define DEVICE_SUSPEND(dev, ...) \
135   (((device_suspend_t *)(device_get_method(dev, "device_suspend")))(dev,## __VA_ARGS__))
136 #define USB_HANDLE_REQUEST(dev, ...) \
137   (((usb_handle_request_t *)(device_get_method(dev, "usb_handle_request")))(dev,## __VA_ARGS__))
138 #define USB_TAKE_CONTROLLER(dev, ...) \
139   (((usb_take_controller_t *)(device_get_method(dev, "usb_take_controller")))(dev,## __VA_ARGS__))
140
141 enum {
142         SI_SUB_DUMMY = 0x0000000,
143         SI_SUB_LOCK = 0x1B00000,
144         SI_SUB_KLD = 0x2000000,
145         SI_SUB_DRIVERS = 0x3100000,
146         SI_SUB_PSEUDO = 0x7000000,
147         SI_SUB_KICK_SCHEDULER = 0xa000000,
148         SI_SUB_RUN_SCHEDULER = 0xfffffff
149 };
150
151 enum {
152         SI_ORDER_FIRST = 0x0000000,
153         SI_ORDER_SECOND = 0x0000001,
154         SI_ORDER_THIRD = 0x0000002,
155         SI_ORDER_FOURTH = 0x0000003,
156         SI_ORDER_MIDDLE = 0x1000000,
157         SI_ORDER_ANY = 0xfffffff        /* last */
158 };
159
160 struct uio;
161 struct thread;
162 struct malloc_type;
163 struct usb_process;
164
165 #ifndef HAVE_STANDARD_DEFS
166 #define _UINT8_T_DECLARED
167 typedef unsigned char uint8_t;
168 #define _INT8_T_DECLARED
169 typedef signed char int8_t;
170 #define _UINT16_T_DECLARED
171 typedef unsigned short uint16_t;
172 #define _INT16_T_DECLARED
173 typedef signed short int16_t;
174 #define _UINT32_T_DECLARED
175 typedef unsigned int uint32_t;
176 #define _INT32_T_DECLARED
177 typedef signed int int32_t;
178 #define _UINT64_T_DECLARED
179 typedef unsigned long long uint64_t;
180 #define _INT16_T_DECLARED
181 typedef signed long long int64_t;
182
183 typedef uint16_t uid_t;
184 typedef uint16_t gid_t;
185 typedef uint16_t mode_t;
186
187 typedef uint8_t *caddr_t;
188 #define _UINTPTR_T_DECLARED
189 typedef unsigned long uintptr_t;
190
191 #define _SIZE_T_DECLARED
192 typedef unsigned long size_t;
193 typedef unsigned long u_long;
194 #endif
195
196 typedef unsigned long bus_addr_t;
197 typedef unsigned long bus_size_t;
198
199 typedef void *bus_dmamap_t;
200 typedef void *bus_dma_tag_t;
201
202 typedef void *bus_space_tag_t;
203 typedef uint8_t *bus_space_handle_t;
204
205 /* SYSINIT API */
206
207 #include <sysinit.h>
208
209 struct sysinit {
210         void    (*func) (void *arg);
211         void   *data;
212 };
213
214 /* MUTEX API */
215
216 struct mtx {
217         int     owned;
218         struct mtx *parent;
219 };
220
221 #define mtx_assert(...) do { } while (0)
222 void    mtx_init(struct mtx *, const char *, const char *, int);
223 void    mtx_lock(struct mtx *);
224 void    mtx_unlock(struct mtx *);
225 #define mtx_lock_spin(x) mtx_lock(x)
226 #define mtx_unlock_spin(x) mtx_unlock(x)
227 int     mtx_owned(struct mtx *);
228 void    mtx_destroy(struct mtx *);
229
230 extern struct mtx Giant;
231
232 /* SX API */
233
234 struct sx {
235         int     owned;
236 };
237
238 #define sx_assert(...) do { } while (0)
239 #define sx_init(...) sx_init_flags(__VA_ARGS__, 0)
240 void    sx_init_flags(struct sx *, const char *, int);
241 void    sx_destroy(struct sx *);
242 void    sx_xlock(struct sx *);
243 void    sx_xunlock(struct sx *);
244 int     sx_xlocked(struct sx *);
245
246 /* CONDVAR API */
247
248 struct cv {
249         int     sleeping;
250 };
251
252 void    cv_init(struct cv *, const char *desc);
253 void    cv_destroy(struct cv *);
254 void    cv_wait(struct cv *, struct mtx *);
255 int     cv_timedwait(struct cv *, struct mtx *, int);
256 void    cv_signal(struct cv *);
257 void    cv_broadcast(struct cv *);
258
259 /* CALLOUT API */
260
261 typedef void callout_fn_t (void *);
262
263 extern volatile int ticks;
264
265 struct callout {
266         LIST_ENTRY(callout) entry;
267         callout_fn_t *func;
268         void   *arg;
269         struct mtx *mtx;
270         int     flags;
271         int     timeout;
272 };
273
274 void    callout_init_mtx(struct callout *, struct mtx *, int);
275 void    callout_reset(struct callout *, int, callout_fn_t *, void *);
276 void    callout_stop(struct callout *);
277 void    callout_drain(struct callout *);
278 int     callout_pending(struct callout *);
279 void    callout_process(int timeout);
280
281 /* DEVICE API */
282
283 struct driver;
284 struct devclass;
285 struct device;
286 struct module;
287 struct module_data;
288
289 typedef struct driver driver_t;
290 typedef struct devclass *devclass_t;
291 typedef struct device *device_t;
292 typedef void (driver_intr_t)(void *arg);
293 typedef int (driver_filter_t)(void *arg);
294 #define FILTER_STRAY            0x01
295 #define FILTER_HANDLED          0x02
296 #define FILTER_SCHEDULE_THREAD  0x04
297
298 typedef int device_attach_t (device_t dev);
299 typedef int device_detach_t (device_t dev);
300 typedef int device_resume_t (device_t dev);
301 typedef int device_shutdown_t (device_t dev);
302 typedef int device_probe_t (device_t dev);
303 typedef int device_suspend_t (device_t dev);
304
305 typedef int bus_child_location_str_t (device_t parent, device_t child, char *buf, size_t buflen);
306 typedef int bus_child_pnpinfo_str_t (device_t parent, device_t child, char *buf, size_t buflen);
307 typedef void bus_driver_added_t (device_t dev, driver_t *driver);
308
309 struct device_method {
310         const char *desc;
311         void   *const func;
312 };
313
314 typedef struct device_method device_method_t;
315
316 struct device {
317         TAILQ_HEAD(device_list, device) dev_children;
318         TAILQ_ENTRY(device) dev_link;
319
320         struct device *dev_parent;
321         const struct module_data *dev_module;
322         void   *dev_sc;
323         void   *dev_aux;
324         driver_filter_t *dev_irq_filter;
325         driver_intr_t *dev_irq_fn;
326         void   *dev_irq_arg;
327
328         uint16_t dev_unit;
329
330         char    dev_nameunit[64];
331         char    dev_desc[64];
332
333         uint8_t dev_res_alloc:1;
334         uint8_t dev_quiet:1;
335         uint8_t dev_softc_set:1;
336         uint8_t dev_softc_alloc:1;
337         uint8_t dev_attached:1;
338         uint8_t dev_fixed_class:1;
339         uint8_t dev_unit_manual:1;
340 };
341
342 struct devclass {
343         device_t dev_list[DEVCLASS_MAXUNIT];
344 };
345
346 struct driver {
347         const char *name;
348         const struct device_method *methods;
349         uint32_t size;
350 };
351
352 struct module_data {
353         int     (*callback) (struct module *, int, void *arg);
354         void   *arg;
355         const char *bus_name;
356         const char *mod_name;
357         const char *long_name;
358         const struct driver *driver;
359         struct devclass **devclass_pp;
360         TAILQ_ENTRY(module_data) entry;
361 };
362
363 device_t device_get_parent(device_t dev);
364 void   *device_get_method(device_t dev, const char *what);
365 const char *device_get_name(device_t dev);
366 const char *device_get_nameunit(device_t dev);
367
368 #define device_printf(dev, fmt,...) \
369         printf("%s: " fmt, device_get_nameunit(dev),## __VA_ARGS__)
370 device_t device_add_child(device_t dev, const char *name, int unit);
371 void    device_quiet(device_t dev);
372 void    device_set_interrupt(device_t dev, driver_filter_t *, driver_intr_t *, void *);
373 void    device_run_interrupts(device_t parent);
374 void    device_set_ivars(device_t dev, void *ivars);
375 void   *device_get_ivars(device_t dev);
376 const char *device_get_desc(device_t dev);
377 int     device_probe_and_attach(device_t dev);
378 int     device_detach(device_t dev);
379 void   *device_get_softc(device_t dev);
380 void    device_set_softc(device_t dev, void *softc);
381 int     device_delete_child(device_t dev, device_t child);
382 int     device_delete_children(device_t dev);
383 int     device_is_attached(device_t dev);
384 void    device_set_desc(device_t dev, const char *desc);
385 void    device_set_desc_copy(device_t dev, const char *desc);
386 int     device_get_unit(device_t dev);
387 void   *devclass_get_softc(devclass_t dc, int unit);
388 int     devclass_get_maxunit(devclass_t dc);
389 device_t devclass_get_device(devclass_t dc, int unit);
390 devclass_t devclass_find(const char *classname);
391
392 #define bus_get_dma_tag(...) (NULL)
393 int     bus_generic_detach(device_t dev);
394 int     bus_generic_resume(device_t dev);
395 int     bus_generic_shutdown(device_t dev);
396 int     bus_generic_suspend(device_t dev);
397 int     bus_generic_print_child(device_t dev, device_t child);
398 void    bus_generic_driver_added(device_t dev, driver_t *driver);
399
400 /* BUS SPACE API */
401
402 void    bus_space_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t data);
403 void    bus_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t data);
404 void    bus_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t data);
405
406 uint8_t bus_space_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
407 uint16_t bus_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
408 uint32_t bus_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset);
409
410 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);
411 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);
412 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);
413
414 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);
415 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);
416 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);
417
418 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);
419 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);
420 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);
421 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);
422
423 void    bus_space_barrier(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, bus_size_t length, int flags);
424
425 void    module_register(void *);
426
427 /* LIB-C */
428
429 void   *memset(void *, int, size_t len);
430 void   *memcpy(void *, const void *, size_t len);
431 int     printf(const char *,...) __printflike(1, 2);
432 int     snprintf(char *restrict str, size_t size, const char *restrict format,...) __printflike(3, 4);
433 size_t  strlen(const char *s);
434
435 /* MALLOC API */
436
437 #undef malloc
438 #define malloc(s,x,f) usb_malloc(s)
439 void   *usb_malloc(size_t);
440
441 #undef free
442 #define free(p,x) usb_free(p)
443 void    usb_free(void *);
444
445 #define strdup(p,x) usb_strdup(p)
446 char   *usb_strdup(const char *str);
447
448 /* ENDIANNESS */
449
450 #ifndef HAVE_ENDIAN_DEFS
451
452 /* Assume little endian */
453
454 #define htole64(x) ((uint64_t)(x))
455 #define le64toh(x) ((uint64_t)(x))
456
457 #define htole32(x) ((uint32_t)(x))
458 #define le32toh(x) ((uint32_t)(x))
459
460 #define htole16(x) ((uint16_t)(x))
461 #define le16toh(x) ((uint16_t)(x))
462
463 #define be32toh(x) ((uint32_t)(x))
464 #define htobe32(x) ((uint32_t)(x))
465
466 #else
467 #include <sys/endian.h>
468 #endif
469
470 /* USB */
471
472 typedef int usb_handle_request_t (device_t dev, const void *req, void **pptr, uint16_t *plen, uint16_t offset, uint8_t *pstate);
473 typedef int usb_take_controller_t (device_t dev);
474
475 void    usb_idle(void);
476 void    usb_init(void);
477 void    usb_uninit(void);
478
479 /* set some defaults */
480
481 #ifndef USB_POOL_SIZE
482 #define USB_POOL_SIZE (1024*1024)       /* 1 MByte */
483 #endif
484
485 int     pause(const char *, int);
486 void    DELAY(unsigned int);
487
488 /* OTHER */
489
490 struct selinfo {
491 };
492
493 /* SYSTEM STARTUP API */
494
495 extern const void *sysinit_data[];
496 extern const void *sysuninit_data[];
497
498 #endif                                  /* _BSD_KERNEL_H_ */