123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /*
- * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
- * The President and Fellows of Harvard College.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
- #include <types.h>
- #include <kern/errno.h>
- #include <kern/fcntl.h>
- #include <lib.h>
- #include <uio.h>
- #include <vfs.h>
- #include <generic/random.h>
- #include "autoconf.h"
- /*
- * Machine-independent generic randomness device.
- *
- * Remembers something that's a random source, and provides random()
- * and randmax() to the rest of the kernel.
- *
- * The kernel config mechanism can be used to explicitly choose which
- * of the available random sources to use, if more than one is
- * available.
- */
- static struct random_softc *the_random = NULL;
- /*
- * VFS device functions.
- * open: allow reading only.
- */
- static
- int
- randopen(struct device *dev, int openflags)
- {
- (void)dev;
- if (openflags != O_RDONLY) {
- return EIO;
- }
- return 0;
- }
- /*
- * VFS close function.
- */
- static
- int
- randclose(struct device *dev)
- {
- (void)dev;
- return 0;
- }
- /*
- * VFS I/O function. Hand off to implementation.
- */
- static
- int
- randio(struct device *dev, struct uio *uio)
- {
- struct random_softc *rs = dev->d_data;
- if (uio->uio_rw != UIO_READ) {
- return EIO;
- }
- return rs->rs_read(rs->rs_devdata, uio);
- }
- /*
- * VFS ioctl function.
- */
- static
- int
- randioctl(struct device *dev, int op, userptr_t data)
- {
- /*
- * We don't support any ioctls.
- */
- (void)dev;
- (void)op;
- (void)data;
- return EIOCTL;
- }
- /*
- * Config function.
- */
- int
- config_random(struct random_softc *rs, int unit)
- {
- int result;
- /* We use only the first random device. */
- if (unit!=0) {
- return ENODEV;
- }
- KASSERT(the_random==NULL);
- the_random = rs;
- rs->rs_dev.d_open = randopen;
- rs->rs_dev.d_close = randclose;
- rs->rs_dev.d_io = randio;
- rs->rs_dev.d_ioctl = randioctl;
- rs->rs_dev.d_blocks = 0;
- rs->rs_dev.d_blocksize = 1;
- rs->rs_dev.d_data = rs;
- /* Add the VFS device structure to the VFS device list. */
- result = vfs_adddev("random", &rs->rs_dev, 0);
- if (result) {
- return result;
- }
- return 0;
- }
- /*
- * Random number functions exported to the rest of the kernel.
- */
- uint32_t
- random(void)
- {
- if (the_random==NULL) {
- panic("No random device\n");
- }
- return the_random->rs_random(the_random->rs_devdata);
- }
- uint32_t
- randmax(void)
- {
- if (the_random==NULL) {
- panic("No random device\n");
- }
- return the_random->rs_randmax(the_random->rs_devdata);
- }
|