/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- randopen
- randclose
- randio
- randioctl
- config_random
- random
- randmax
1 /*
2 * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3 * The President and Fellows of Harvard College.
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 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <types.h>
31 #include <kern/errno.h>
32 #include <kern/fcntl.h>
33 #include <lib.h>
34 #include <uio.h>
35 #include <vfs.h>
36 #include <generic/random.h>
37 #include "autoconf.h"
38
39 /*
40 * Machine-independent generic randomness device.
41 *
42 * Remembers something that's a random source, and provides random()
43 * and randmax() to the rest of the kernel.
44 *
45 * The kernel config mechanism can be used to explicitly choose which
46 * of the available random sources to use, if more than one is
47 * available.
48 */
49
50 static struct random_softc *the_random = NULL;
51
52 /*
53 * VFS device functions.
54 * open: allow reading only.
55 */
56 static
57 int
58 randopen(struct device *dev, int openflags)
59 {
60 (void)dev;
61
62 if (openflags != O_RDONLY) {
63 return EIO;
64 }
65
66 return 0;
67 }
68
69 /*
70 * VFS close function.
71 */
72 static
73 int
74 randclose(struct device *dev)
75 {
76 (void)dev;
77 return 0;
78 }
79
80 /*
81 * VFS I/O function. Hand off to implementation.
82 */
83 static
84 int
85 randio(struct device *dev, struct uio *uio)
86 {
87 struct random_softc *rs = dev->d_data;
88
89 if (uio->uio_rw != UIO_READ) {
90 return EIO;
91 }
92
93 return rs->rs_read(rs->rs_devdata, uio);
94 }
95
96 /*
97 * VFS ioctl function.
98 */
99 static
100 int
101 randioctl(struct device *dev, int op, userptr_t data)
102 {
103 /*
104 * We don't support any ioctls.
105 */
106 (void)dev;
107 (void)op;
108 (void)data;
109 return EIOCTL;
110 }
111
112 /*
113 * Config function.
114 */
115 int
116 config_random(struct random_softc *rs, int unit)
117 {
118 int result;
119
120 /* We use only the first random device. */
121 if (unit!=0) {
122 return ENODEV;
123 }
124
125 KASSERT(the_random==NULL);
126 the_random = rs;
127
128 rs->rs_dev.d_open = randopen;
129 rs->rs_dev.d_close = randclose;
130 rs->rs_dev.d_io = randio;
131 rs->rs_dev.d_ioctl = randioctl;
132 rs->rs_dev.d_blocks = 0;
133 rs->rs_dev.d_blocksize = 1;
134 rs->rs_dev.d_data = rs;
135
136 /* Add the VFS device structure to the VFS device list. */
137 result = vfs_adddev("random", &rs->rs_dev, 0);
138 if (result) {
139 return result;
140 }
141
142 return 0;
143 }
144
145
146 /*
147 * Random number functions exported to the rest of the kernel.
148 */
149
150 uint32_t
151 random(void)
152 {
153 if (the_random==NULL) {
154 panic("No random device\n");
155 }
156 return the_random->rs_random(the_random->rs_devdata);
157 }
158
159 uint32_t
160 randmax(void)
161 {
162 if (the_random==NULL) {
163 panic("No random device\n");
164 }
165 return the_random->rs_randmax(the_random->rs_devdata);
166 }