xref: /openbmc/linux/drivers/char/hw_random/core.c (revision 384740dc)
1 /*
2         Added support for the AMD Geode LX RNG
3 	(c) Copyright 2004-2005 Advanced Micro Devices, Inc.
4 
5 	derived from
6 
7  	Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 	(c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
9 
10  	derived from
11 
12         Hardware driver for the AMD 768 Random Number Generator (RNG)
13         (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
14 
15  	derived from
16 
17 	Hardware driver for Intel i810 Random Number Generator (RNG)
18 	Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 	Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
20 
21 	Added generic RNG API
22 	Copyright 2006 Michael Buesch <mbuesch@freenet.de>
23 	Copyright 2005 (c) MontaVista Software, Inc.
24 
25 	Please read Documentation/hw_random.txt for details on use.
26 
27 	----------------------------------------------------------
28 	This software may be used and distributed according to the terms
29         of the GNU General Public License, incorporated herein by reference.
30 
31  */
32 
33 
34 #include <linux/device.h>
35 #include <linux/hw_random.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/fs.h>
39 #include <linux/sched.h>
40 #include <linux/smp_lock.h>
41 #include <linux/init.h>
42 #include <linux/miscdevice.h>
43 #include <linux/delay.h>
44 #include <asm/uaccess.h>
45 
46 
47 #define RNG_MODULE_NAME		"hw_random"
48 #define PFX			RNG_MODULE_NAME ": "
49 #define RNG_MISCDEV_MINOR	183 /* official */
50 
51 
52 static struct hwrng *current_rng;
53 static LIST_HEAD(rng_list);
54 static DEFINE_MUTEX(rng_mutex);
55 
56 
57 static inline int hwrng_init(struct hwrng *rng)
58 {
59 	if (!rng->init)
60 		return 0;
61 	return rng->init(rng);
62 }
63 
64 static inline void hwrng_cleanup(struct hwrng *rng)
65 {
66 	if (rng && rng->cleanup)
67 		rng->cleanup(rng);
68 }
69 
70 static inline int hwrng_data_present(struct hwrng *rng, int wait)
71 {
72 	if (!rng->data_present)
73 		return 1;
74 	return rng->data_present(rng, wait);
75 }
76 
77 static inline int hwrng_data_read(struct hwrng *rng, u32 *data)
78 {
79 	return rng->data_read(rng, data);
80 }
81 
82 
83 static int rng_dev_open(struct inode *inode, struct file *filp)
84 {
85 	/* enforce read-only access to this chrdev */
86 	if ((filp->f_mode & FMODE_READ) == 0)
87 		return -EINVAL;
88 	if (filp->f_mode & FMODE_WRITE)
89 		return -EINVAL;
90 	cycle_kernel_lock();
91 	return 0;
92 }
93 
94 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
95 			    size_t size, loff_t *offp)
96 {
97 	u32 data;
98 	ssize_t ret = 0;
99 	int err = 0;
100 	int bytes_read;
101 
102 	while (size) {
103 		err = -ERESTARTSYS;
104 		if (mutex_lock_interruptible(&rng_mutex))
105 			goto out;
106 		if (!current_rng) {
107 			mutex_unlock(&rng_mutex);
108 			err = -ENODEV;
109 			goto out;
110 		}
111 
112 		bytes_read = 0;
113 		if (hwrng_data_present(current_rng,
114 				       !(filp->f_flags & O_NONBLOCK)))
115 			bytes_read = hwrng_data_read(current_rng, &data);
116 		mutex_unlock(&rng_mutex);
117 
118 		err = -EAGAIN;
119 		if (!bytes_read && (filp->f_flags & O_NONBLOCK))
120 			goto out;
121 		if (bytes_read < 0) {
122 			err = bytes_read;
123 			goto out;
124 		}
125 
126 		err = -EFAULT;
127 		while (bytes_read && size) {
128 			if (put_user((u8)data, buf++))
129 				goto out;
130 			size--;
131 			ret++;
132 			bytes_read--;
133 			data >>= 8;
134 		}
135 
136 		if (need_resched())
137 			schedule_timeout_interruptible(1);
138 		err = -ERESTARTSYS;
139 		if (signal_pending(current))
140 			goto out;
141 	}
142 out:
143 	return ret ? : err;
144 }
145 
146 
147 static const struct file_operations rng_chrdev_ops = {
148 	.owner		= THIS_MODULE,
149 	.open		= rng_dev_open,
150 	.read		= rng_dev_read,
151 };
152 
153 static struct miscdevice rng_miscdev = {
154 	.minor		= RNG_MISCDEV_MINOR,
155 	.name		= RNG_MODULE_NAME,
156 	.fops		= &rng_chrdev_ops,
157 };
158 
159 
160 static ssize_t hwrng_attr_current_store(struct device *dev,
161 					struct device_attribute *attr,
162 					const char *buf, size_t len)
163 {
164 	int err;
165 	struct hwrng *rng;
166 
167 	err = mutex_lock_interruptible(&rng_mutex);
168 	if (err)
169 		return -ERESTARTSYS;
170 	err = -ENODEV;
171 	list_for_each_entry(rng, &rng_list, list) {
172 		if (strcmp(rng->name, buf) == 0) {
173 			if (rng == current_rng) {
174 				err = 0;
175 				break;
176 			}
177 			err = hwrng_init(rng);
178 			if (err)
179 				break;
180 			hwrng_cleanup(current_rng);
181 			current_rng = rng;
182 			err = 0;
183 			break;
184 		}
185 	}
186 	mutex_unlock(&rng_mutex);
187 
188 	return err ? : len;
189 }
190 
191 static ssize_t hwrng_attr_current_show(struct device *dev,
192 				       struct device_attribute *attr,
193 				       char *buf)
194 {
195 	int err;
196 	ssize_t ret;
197 	const char *name = "none";
198 
199 	err = mutex_lock_interruptible(&rng_mutex);
200 	if (err)
201 		return -ERESTARTSYS;
202 	if (current_rng)
203 		name = current_rng->name;
204 	ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
205 	mutex_unlock(&rng_mutex);
206 
207 	return ret;
208 }
209 
210 static ssize_t hwrng_attr_available_show(struct device *dev,
211 					 struct device_attribute *attr,
212 					 char *buf)
213 {
214 	int err;
215 	ssize_t ret = 0;
216 	struct hwrng *rng;
217 
218 	err = mutex_lock_interruptible(&rng_mutex);
219 	if (err)
220 		return -ERESTARTSYS;
221 	buf[0] = '\0';
222 	list_for_each_entry(rng, &rng_list, list) {
223 		strncat(buf, rng->name, PAGE_SIZE - ret - 1);
224 		ret += strlen(rng->name);
225 		strncat(buf, " ", PAGE_SIZE - ret - 1);
226 		ret++;
227 	}
228 	strncat(buf, "\n", PAGE_SIZE - ret - 1);
229 	ret++;
230 	mutex_unlock(&rng_mutex);
231 
232 	return ret;
233 }
234 
235 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
236 		   hwrng_attr_current_show,
237 		   hwrng_attr_current_store);
238 static DEVICE_ATTR(rng_available, S_IRUGO,
239 		   hwrng_attr_available_show,
240 		   NULL);
241 
242 
243 static void unregister_miscdev(void)
244 {
245 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
246 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
247 	misc_deregister(&rng_miscdev);
248 }
249 
250 static int register_miscdev(void)
251 {
252 	int err;
253 
254 	err = misc_register(&rng_miscdev);
255 	if (err)
256 		goto out;
257 	err = device_create_file(rng_miscdev.this_device,
258 				 &dev_attr_rng_current);
259 	if (err)
260 		goto err_misc_dereg;
261 	err = device_create_file(rng_miscdev.this_device,
262 				 &dev_attr_rng_available);
263 	if (err)
264 		goto err_remove_current;
265 out:
266 	return err;
267 
268 err_remove_current:
269 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
270 err_misc_dereg:
271 	misc_deregister(&rng_miscdev);
272 	goto out;
273 }
274 
275 int hwrng_register(struct hwrng *rng)
276 {
277 	int must_register_misc;
278 	int err = -EINVAL;
279 	struct hwrng *old_rng, *tmp;
280 
281 	if (rng->name == NULL ||
282 	    rng->data_read == NULL)
283 		goto out;
284 
285 	mutex_lock(&rng_mutex);
286 
287 	/* Must not register two RNGs with the same name. */
288 	err = -EEXIST;
289 	list_for_each_entry(tmp, &rng_list, list) {
290 		if (strcmp(tmp->name, rng->name) == 0)
291 			goto out_unlock;
292 	}
293 
294 	must_register_misc = (current_rng == NULL);
295 	old_rng = current_rng;
296 	if (!old_rng) {
297 		err = hwrng_init(rng);
298 		if (err)
299 			goto out_unlock;
300 		current_rng = rng;
301 	}
302 	err = 0;
303 	if (must_register_misc) {
304 		err = register_miscdev();
305 		if (err) {
306 			if (!old_rng) {
307 				hwrng_cleanup(rng);
308 				current_rng = NULL;
309 			}
310 			goto out_unlock;
311 		}
312 	}
313 	INIT_LIST_HEAD(&rng->list);
314 	list_add_tail(&rng->list, &rng_list);
315 out_unlock:
316 	mutex_unlock(&rng_mutex);
317 out:
318 	return err;
319 }
320 EXPORT_SYMBOL_GPL(hwrng_register);
321 
322 void hwrng_unregister(struct hwrng *rng)
323 {
324 	int err;
325 
326 	mutex_lock(&rng_mutex);
327 
328 	list_del(&rng->list);
329 	if (current_rng == rng) {
330 		hwrng_cleanup(rng);
331 		if (list_empty(&rng_list)) {
332 			current_rng = NULL;
333 		} else {
334 			current_rng = list_entry(rng_list.prev, struct hwrng, list);
335 			err = hwrng_init(current_rng);
336 			if (err)
337 				current_rng = NULL;
338 		}
339 	}
340 	if (list_empty(&rng_list))
341 		unregister_miscdev();
342 
343 	mutex_unlock(&rng_mutex);
344 }
345 EXPORT_SYMBOL_GPL(hwrng_unregister);
346 
347 
348 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
349 MODULE_LICENSE("GPL");
350