Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/random/random_infra.c
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
#if defined(RANDOM_LOADABLE) | #if defined(RANDOM_LOADABLE) | ||||
struct random_algorithm *p_random_alg_context = NULL; | struct random_algorithm *p_random_alg_context = NULL; | ||||
#else /* !defined(RANDOM_LOADABLE) */ | #else /* !defined(RANDOM_LOADABLE) */ | ||||
struct random_algorithm *p_random_alg_context = &random_alg_context; | struct random_algorithm *p_random_alg_context = &random_alg_context; | ||||
#endif /* defined(RANDOM_LOADABLE) */ | #endif /* defined(RANDOM_LOADABLE) */ | ||||
#if defined(RANDOM_LOADABLE) | #if defined(RANDOM_LOADABLE) | ||||
static void | |||||
null_read_random(void *dummy __unused, u_int dummy2 __unused) | |||||
{ | |||||
panic("%s: no random module is loaded", __func__); | |||||
} | |||||
struct random_readers { | struct random_readers { | ||||
int (*read_random_uio)(struct uio *, bool); | int (*read_random_uio)(struct uio *, bool); | ||||
u_int (*read_random)(void *, u_int); | void (*read_random)(void *, u_int); | ||||
} random_reader_context = { | } random_reader_context = { | ||||
(int (*)(struct uio *, bool))nullop, | (int (*)(struct uio *, bool))nullop, | ||||
(u_int (*)(void *, u_int))nullop, | null_read_random, | ||||
}; | }; | ||||
struct sx randomdev_config_lock; | struct sx randomdev_config_lock; | ||||
static void | static void | ||||
random_infra_sysinit(void *dummy __unused) | random_infra_sysinit(void *dummy __unused) | ||||
{ | { | ||||
RANDOM_CONFIG_INIT_LOCK(); | RANDOM_CONFIG_INIT_LOCK(); | ||||
} | } | ||||
SYSINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysinit, NULL); | SYSINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysinit, NULL); | ||||
void | void | ||||
random_infra_init(int (*p_random_read_uio)(struct uio *, bool), u_int (*p_random_read)(void *, u_int)) | random_infra_init(int (*p_random_read_uio)(struct uio *, bool), void (*p_random_read)(void *, u_int)) | ||||
{ | { | ||||
RANDOM_CONFIG_X_LOCK(); | RANDOM_CONFIG_X_LOCK(); | ||||
random_reader_context.read_random_uio = p_random_read_uio; | random_reader_context.read_random_uio = p_random_read_uio; | ||||
random_reader_context.read_random = p_random_read; | random_reader_context.read_random = p_random_read; | ||||
RANDOM_CONFIG_X_UNLOCK(); | RANDOM_CONFIG_X_UNLOCK(); | ||||
} | } | ||||
void | void | ||||
random_infra_uninit(void) | random_infra_uninit(void) | ||||
{ | { | ||||
RANDOM_CONFIG_X_LOCK(); | RANDOM_CONFIG_X_LOCK(); | ||||
random_reader_context.read_random_uio = (int (*)(struct uio *, bool))nullop; | random_reader_context.read_random_uio = (int (*)(struct uio *, bool))nullop; | ||||
random_reader_context.read_random = (u_int (*)(void *, u_int))nullop; | random_reader_context.read_random = null_read_random; | ||||
RANDOM_CONFIG_X_UNLOCK(); | RANDOM_CONFIG_X_UNLOCK(); | ||||
} | } | ||||
static void | static void | ||||
random_infra_sysuninit(void *dummy __unused) | random_infra_sysuninit(void *dummy __unused) | ||||
{ | { | ||||
RANDOM_CONFIG_DEINIT_LOCK(); | RANDOM_CONFIG_DEINIT_LOCK(); | ||||
} | } | ||||
SYSUNINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysuninit, NULL); | SYSUNINIT(random_device_h_init, SI_SUB_RANDOM, SI_ORDER_FIRST, random_infra_sysuninit, NULL); | ||||
int | int | ||||
read_random_uio(struct uio *uio, bool nonblock) | read_random_uio(struct uio *uio, bool nonblock) | ||||
{ | { | ||||
int retval; | int retval; | ||||
RANDOM_CONFIG_S_LOCK(); | RANDOM_CONFIG_S_LOCK(); | ||||
retval = random_reader_context.read_random_uio(uio, nonblock); | retval = random_reader_context.read_random_uio(uio, nonblock); | ||||
RANDOM_CONFIG_S_UNLOCK(); | RANDOM_CONFIG_S_UNLOCK(); | ||||
return (retval); | return (retval); | ||||
} | } | ||||
u_int | void | ||||
read_random(void *buf, u_int len) | read_random(void *buf, u_int len) | ||||
{ | { | ||||
u_int retval; | |||||
RANDOM_CONFIG_S_LOCK(); | RANDOM_CONFIG_S_LOCK(); | ||||
retval = random_reader_context.read_random(buf, len); | random_reader_context.read_random(buf, len); | ||||
RANDOM_CONFIG_S_UNLOCK(); | RANDOM_CONFIG_S_UNLOCK(); | ||||
return (retval); | |||||
} | } | ||||
#endif /* defined(RANDOM_LOADABLE) */ | #endif /* defined(RANDOM_LOADABLE) */ |