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#define __FreeBSD_version 1000000 32 33#include <sys/cdefs.h> 34#include <sys/queue.h> 35#include <sys/errno.h> 36 37#define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 38#define isdigit(x) ((x) >= '0' && (x) <= '9') 39#define panic(...) do { printf("USB PANIC: " __VA_ARGS__); while (1) ; } while (0) 40#define rebooting 0 41#define M_USB 0 42#define M_USBDEV 0 43#define USB_PROC_MAX 3 44#define USB_BUS_GIANT_PROC(bus) (usb_process + 2) 45#define USB_BUS_NON_GIANT_BULK_PROC(bus) (usb_process + 2) 46#define USB_BUS_NON_GIANT_ISOC_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 } }; \ 71SYSINIT(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) \ 75const struct sysinit UNIQ_NAME(sysinit_##uniq) = { \ 76 .func = (_func), \ 77 .data = __DECONST(void *, _data) \ 78}; \ 79SYSINIT_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) \ 84const struct sysinit UNIQ_NAME(sysuninit_##uniq) = { \ 85 .func = (_func), \ 86 .data = __DECONST(void *, _data) \ 87}; \ 88SYSINIT_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#define PAGE_SIZE 4096 98#define MIN(a,b) (((a) < (b)) ? (a) : (b)) 99#define MAX(a,b) (((a) > (b)) ? (a) : (b)) 100#define MTX_DEF 0 101#define MTX_SPIN 0 102#define MTX_RECURSE 0 103#define SX_DUPOK 0 104#define SX_NOWITNESS 0 105#define WITNESS_WARN(...) 106#define cold 0 107#define BUS_PROBE_GENERIC 0 108#define CALLOUT_RETURNUNLOCKED 0x1 109#define va_list __builtin_va_list 110#define va_size(type) __builtin_va_size(type) 111#define va_start(ap, last) __builtin_va_start(ap, last) 112#define va_end(ap) __builtin_va_end(ap) 113#define va_arg(ap, type) __builtin_va_arg((ap), type) 114#define DEVICE_ATTACH(dev, ...) \ 115 (((device_attach_t *)(device_get_method(dev, "device_attach")))(dev,## __VA_ARGS__)) 116#define DEVICE_DETACH(dev, ...) \ 117 (((device_detach_t *)(device_get_method(dev, "device_detach")))(dev,## __VA_ARGS__)) 118#define DEVICE_PROBE(dev, ...) \ 119 (((device_probe_t *)(device_get_method(dev, "device_probe")))(dev,## __VA_ARGS__)) 120#define DEVICE_RESUME(dev, ...) \ 121 (((device_resume_t *)(device_get_method(dev, "device_resume")))(dev,## __VA_ARGS__)) 122#define DEVICE_SHUTDOWN(dev, ...) \ 123 (((device_shutdown_t *)(device_get_method(dev, "device_shutdown")))(dev,## __VA_ARGS__)) 124#define DEVICE_SUSPEND(dev, ...) \ 125 (((device_suspend_t *)(device_get_method(dev, "device_suspend")))(dev,## __VA_ARGS__)) 126#define USB_HANDLE_REQUEST(dev, ...) \ 127 (((usb_handle_request_t *)(device_get_method(dev, "usb_handle_request")))(dev,## __VA_ARGS__)) 128#define USB_TAKE_CONTROLLER(dev, ...) \ 129 (((usb_take_controller_t *)(device_get_method(dev, "usb_take_controller")))(dev,## __VA_ARGS__)) 130 131enum { 132 SI_SUB_DUMMY = 0x0000000, 133 SI_SUB_LOCK = 0x1B00000, 134 SI_SUB_KLD = 0x2000000, 135 SI_SUB_DRIVERS = 0x3100000, 136 SI_SUB_PSEUDO = 0x7000000, 137 SI_SUB_KICK_SCHEDULER = 0xa000000, 138 SI_SUB_RUN_SCHEDULER = 0xfffffff 139}; 140 141enum { 142 SI_ORDER_FIRST = 0x0000000, 143 SI_ORDER_SECOND = 0x0000001, 144 SI_ORDER_THIRD = 0x0000002, 145 SI_ORDER_FOURTH = 0x0000003, 146 SI_ORDER_MIDDLE = 0x1000000, 147 SI_ORDER_ANY = 0xfffffff /* last */ 148}; 149 150struct uio; 151struct thread; 152struct malloc_type; 153struct usb_process; 154 155typedef unsigned char uint8_t; 156typedef signed char int8_t; 157 158typedef unsigned short uint16_t; 159typedef signed short int16_t; 160 161typedef unsigned int uint32_t; 162typedef signed int int32_t; 163 164typedef unsigned long long uint64_t; 165typedef signed long long int64_t; 166 167typedef unsigned long bus_addr_t; 168typedef unsigned long bus_size_t; 169 170typedef unsigned long size_t; 171typedef unsigned long u_long; 172 173typedef void *bus_dmamap_t; 174typedef void *bus_dma_tag_t; 175 176typedef void *bus_space_tag_t; 177typedef uint8_t *bus_space_handle_t; 178 179typedef uint16_t uid_t; 180typedef uint16_t gid_t; 181typedef uint16_t mode_t; 182 183typedef uint8_t *caddr_t; 184typedef unsigned long __uintptr_t; 185typedef unsigned long uintptr_t; 186 187/* SYSINIT API */ 188 189#include <sysinit.h> 190 191struct sysinit { 192 void (*func) (void *arg); 193 void *data; 194}; 195 196/* MUTEX API */ 197 198struct mtx { 199 int owned; 200 struct mtx *parent; 201}; 202 203#define mtx_assert(...) do { } while (0) 204void mtx_init(struct mtx *, const char *, const char *, int); 205void mtx_lock(struct mtx *); 206void mtx_unlock(struct mtx *); 207#define mtx_lock_spin(x) mtx_lock(x) 208#define mtx_unlock_spin(x) mtx_unlock(x) 209int mtx_owned(struct mtx *); 210void mtx_destroy(struct mtx *); 211 212extern struct mtx Giant; 213 214/* SX API */ 215 216struct sx { 217 int owned; 218}; 219 220#define sx_assert(...) do { } while (0) 221#define sx_init(...) sx_init_flags(__VA_ARGS__, 0) 222void sx_init_flags(struct sx *, const char *, int); 223void sx_destroy(struct sx *); 224void sx_xlock(struct sx *); 225void sx_xunlock(struct sx *); 226int sx_xlocked(struct sx *); 227 228/* CONDVAR API */ 229 230struct cv { 231 int sleeping; 232}; 233 234void cv_init(struct cv *, const char *desc); 235void cv_destroy(struct cv *); 236void cv_wait(struct cv *, struct mtx *); 237int cv_timedwait(struct cv *, struct mtx *, int); 238void cv_signal(struct cv *); 239void cv_broadcast(struct cv *); 240 241/* CALLOUT API */ 242 243typedef void callout_fn_t (void *); 244 245extern volatile int ticks; 246 247struct callout { 248 LIST_ENTRY(callout) entry; 249 callout_fn_t *func; 250 void *arg; 251 struct mtx *mtx; 252 int flags; 253 int timeout; 254}; 255 256void callout_init_mtx(struct callout *, struct mtx *, int); 257void callout_reset(struct callout *, int, callout_fn_t *, void *); 258void callout_stop(struct callout *); 259void callout_drain(struct callout *); 260int callout_pending(struct callout *); 261void callout_process(int timeout); 262 263/* DEVICE API */ 264 265struct driver; 266struct devclass; 267struct device; 268struct module; 269struct module_data; 270 271typedef struct driver driver_t; 272typedef struct devclass *devclass_t; 273typedef struct device *device_t; 274typedef void (driver_intr_t)(void *arg); 275typedef int (driver_filter_t)(void *arg); 276#define FILTER_STRAY 0x01 277#define FILTER_HANDLED 0x02 278#define FILTER_SCHEDULE_THREAD 0x04 279 280typedef int device_attach_t (device_t dev); 281typedef int device_detach_t (device_t dev); 282typedef int device_resume_t (device_t dev); 283typedef int device_shutdown_t (device_t dev); 284typedef int device_probe_t (device_t dev); 285typedef int device_suspend_t (device_t dev); 286 287typedef int bus_child_location_str_t (device_t parent, device_t child, char *buf, size_t buflen); 288typedef int bus_child_pnpinfo_str_t (device_t parent, device_t child, char *buf, size_t buflen); 289typedef void bus_driver_added_t (device_t dev, driver_t *driver); 290 291struct device_method { 292 const char *desc; 293 void *const func; 294}; 295 296typedef struct device_method device_method_t; 297 298struct device { 299 TAILQ_HEAD(device_list, device) dev_children; 300 TAILQ_ENTRY(device) dev_link; 301 302 struct device *dev_parent; 303 const struct module_data *dev_module; 304 void *dev_sc; 305 void *dev_aux; 306 driver_filter_t *dev_irq_filter; 307 driver_intr_t *dev_irq_fn; 308 void *dev_irq_arg; 309 310 uint16_t dev_unit; 311 312 char dev_nameunit[64]; 313 char dev_desc[64]; 314 315 uint8_t dev_res_alloc:1; 316 uint8_t dev_quiet:1; 317 uint8_t dev_softc_set:1; 318 uint8_t dev_softc_alloc:1; 319 uint8_t dev_attached:1; 320 uint8_t dev_fixed_class:1; 321 uint8_t dev_unit_manual:1; 322}; 323 324struct devclass { 325 device_t dev_list[DEVCLASS_MAXUNIT]; 326}; 327 328struct driver { 329 const char *name; 330 const struct device_method *methods; 331 uint32_t size; 332}; 333 334struct module_data { 335 int (*callback) (struct module *, int, void *arg); 336 void *arg; 337 const char *bus_name; 338 const char *mod_name; 339 const char *long_name; 340 const struct driver *driver; 341 struct devclass **devclass_pp; 342 TAILQ_ENTRY(module_data) entry; 343}; 344 345device_t device_get_parent(device_t dev); 346void *device_get_method(device_t dev, const char *what); 347const char *device_get_name(device_t dev); 348const char *device_get_nameunit(device_t dev); 349 350#define device_printf(dev, fmt,...) \ 351 printf("%s: " fmt, device_get_nameunit(dev),## __VA_ARGS__) 352device_t device_add_child(device_t dev, const char *name, int unit); 353void device_quiet(device_t dev); 354void device_set_interrupt(device_t dev, driver_filter_t *, driver_intr_t *, void *); 355void device_run_interrupts(device_t parent); 356void device_set_ivars(device_t dev, void *ivars); 357void *device_get_ivars(device_t dev); 358const char *device_get_desc(device_t dev); 359int device_probe_and_attach(device_t dev); 360int device_detach(device_t dev); 361void *device_get_softc(device_t dev); 362void device_set_softc(device_t dev, void *softc); 363int device_delete_child(device_t dev, device_t child); 364int device_delete_children(device_t dev); 365int device_is_attached(device_t dev); 366void device_set_desc(device_t dev, const char *desc); 367void device_set_desc_copy(device_t dev, const char *desc); 368int device_get_unit(device_t dev); 369void *devclass_get_softc(devclass_t dc, int unit); 370int devclass_get_maxunit(devclass_t dc); 371device_t devclass_get_device(devclass_t dc, int unit); 372devclass_t devclass_find(const char *classname); 373 374#define bus_get_dma_tag(...) (NULL) 375int bus_generic_detach(device_t dev); 376int bus_generic_resume(device_t dev); 377int bus_generic_shutdown(device_t dev); 378int bus_generic_suspend(device_t dev); 379int bus_generic_print_child(device_t dev, device_t child); 380void bus_generic_driver_added(device_t dev, driver_t *driver); 381 382/* BUS SPACE API */ 383 384void bus_space_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint8_t data); 385void bus_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint16_t data); 386void bus_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset, uint32_t data); 387 388uint8_t bus_space_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset); 389uint16_t bus_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset); 390uint32_t bus_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset); 391 392void 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); 393void 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); 394void 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); 395 396void 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); 397void 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); 398void 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); 399 400void 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); 401void 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); 402void 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); 403void 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); 404 405void bus_space_barrier(bus_space_tag_t space, bus_space_handle_t handle, bus_size_t offset, bus_size_t length, int flags); 406 407void module_register(void *); 408 409/* LIB-C */ 410 411void *memset(void *, int, size_t len); 412void *memcpy(void *, const void *, size_t len); 413int printf(const char *,...) __printflike(1, 2); 414int snprintf(char *restrict str, size_t size, const char *restrict format,...) __printflike(3, 4); 415size_t strlen(const char *s); 416 417/* MALLOC API */ 418 419#define malloc(s,x,f) usb_malloc(s) 420void *usb_malloc(size_t); 421 422#define free(p,x) usb_free(p) 423void usb_free(void *); 424 425#define strdup(p,x) usb_strdup(p) 426char *usb_strdup(const char *str); 427 428/* ENDIANNESS */ 429 430/* Assume little endian */ 431 432#define htole64(x) ((uint64_t)(x)) 433#define le64toh(x) ((uint64_t)(x)) 434 435#define htole32(x) ((uint32_t)(x)) 436#define le32toh(x) ((uint32_t)(x)) 437 438#define htole16(x) ((uint16_t)(x)) 439#define le16toh(x) ((uint16_t)(x)) 440 441#define be32toh(x) ((uint32_t)(x)) 442#define htobe32(x) ((uint32_t)(x)) 443 444/* USB */ 445 446typedef int usb_handle_request_t (device_t dev, const void *req, void **pptr, uint16_t *plen, uint16_t offset, uint8_t *pstate); 447typedef int usb_take_controller_t (device_t dev); 448 449void usb_idle(void); 450void usb_init(void); 451void usb_uninit(void); 452 453/* set some defaults */ 454 455#ifndef USB_POOL_SIZE 456#define USB_POOL_SIZE (1024*1024) /* 1 MByte */ 457#endif 458 459int pause(const char *, int); 460void DELAY(unsigned int); 461 462/* OTHER */ 463 464struct selinfo { 465}; 466 467/* SYSTEM STARTUP API */ 468 469extern const void *sysinit_data[]; 470extern const void *sysuninit_data[]; 471 472#endif /* _BSD_KERNEL_H_ */ 473