aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/hw_random
diff options
context:
space:
mode:
authorJonathan Corbet <corbet@lwn.net>2008-07-14 17:29:34 -0400
committerJonathan Corbet <corbet@lwn.net>2008-07-14 17:29:34 -0400
commit2fceef397f9880b212a74c418290ce69e7ac00eb (patch)
treed9cc09ab992825ef7fede4a688103503e3caf655 /drivers/char/hw_random
parentfeae1ef116ed381625d3731c5ae4f4ebcb3fa302 (diff)
parentbce7f793daec3e65ec5c5705d2457b81fe7b5725 (diff)
Merge commit 'v2.6.26' into bkl-removal
Diffstat (limited to 'drivers/char/hw_random')
-rw-r--r--drivers/char/hw_random/Kconfig9
-rw-r--r--drivers/char/hw_random/Makefile1
-rw-r--r--drivers/char/hw_random/intel-rng.c2
-rw-r--r--drivers/char/hw_random/virtio-rng.c155
4 files changed, 166 insertions, 1 deletions
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 8d6c2089d2a8..efd0b4db7c8e 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -112,3 +112,12 @@ config HW_RANDOM_PASEMI
112 112
113 If unsure, say Y. 113 If unsure, say Y.
114 114
115config HW_RANDOM_VIRTIO
116 tristate "VirtIO Random Number Generator support"
117 depends on HW_RANDOM && VIRTIO
118 ---help---
119 This driver provides kernel-side support for the virtual Random Number
120 Generator hardware.
121
122 To compile this driver as a module, choose M here: the
123 module will be called virtio-rng. If unsure, say N.
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index c8b7300e2fb1..b4940ddbb35f 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -11,3 +11,4 @@ obj-$(CONFIG_HW_RANDOM_VIA) += via-rng.o
11obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o 11obj-$(CONFIG_HW_RANDOM_IXP4XX) += ixp4xx-rng.o
12obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o 12obj-$(CONFIG_HW_RANDOM_OMAP) += omap-rng.o
13obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o 13obj-$(CONFIG_HW_RANDOM_PASEMI) += pasemi-rng.o
14obj-$(CONFIG_HW_RANDOM_VIRTIO) += virtio-rng.o
diff --git a/drivers/char/hw_random/intel-rng.c b/drivers/char/hw_random/intel-rng.c
index 5cc651ef75eb..27fdc0866496 100644
--- a/drivers/char/hw_random/intel-rng.c
+++ b/drivers/char/hw_random/intel-rng.c
@@ -273,7 +273,7 @@ static int __init intel_rng_hw_init(void *_intel_rng_hw)
273 if (mfc != INTEL_FWH_MANUFACTURER_CODE || 273 if (mfc != INTEL_FWH_MANUFACTURER_CODE ||
274 (dvc != INTEL_FWH_DEVICE_CODE_8M && 274 (dvc != INTEL_FWH_DEVICE_CODE_8M &&
275 dvc != INTEL_FWH_DEVICE_CODE_4M)) { 275 dvc != INTEL_FWH_DEVICE_CODE_4M)) {
276 printk(KERN_ERR PFX "FWH not detected\n"); 276 printk(KERN_NOTICE PFX "FWH not detected\n");
277 return -ENODEV; 277 return -ENODEV;
278 } 278 }
279 279
diff --git a/drivers/char/hw_random/virtio-rng.c b/drivers/char/hw_random/virtio-rng.c
new file mode 100644
index 000000000000..d0e563e4fc39
--- /dev/null
+++ b/drivers/char/hw_random/virtio-rng.c
@@ -0,0 +1,155 @@
1/*
2 * Randomness driver for virtio
3 * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include <linux/err.h>
20#include <linux/hw_random.h>
21#include <linux/scatterlist.h>
22#include <linux/spinlock.h>
23#include <linux/virtio.h>
24#include <linux/virtio_rng.h>
25
26/* The host will fill any buffer we give it with sweet, sweet randomness. We
27 * give it 64 bytes at a time, and the hwrng framework takes it 4 bytes at a
28 * time. */
29#define RANDOM_DATA_SIZE 64
30
31static struct virtqueue *vq;
32static u32 *random_data;
33static unsigned int data_left;
34static DECLARE_COMPLETION(have_data);
35
36static void random_recv_done(struct virtqueue *vq)
37{
38 int len;
39
40 /* We never get spurious callbacks. */
41 if (!vq->vq_ops->get_buf(vq, &len))
42 BUG();
43
44 data_left = len / sizeof(random_data[0]);
45 complete(&have_data);
46}
47
48static void register_buffer(void)
49{
50 struct scatterlist sg;
51
52 sg_init_one(&sg, random_data, RANDOM_DATA_SIZE);
53 /* There should always be room for one buffer. */
54 if (vq->vq_ops->add_buf(vq, &sg, 0, 1, random_data) != 0)
55 BUG();
56 vq->vq_ops->kick(vq);
57}
58
59/* At least we don't udelay() in a loop like some other drivers. */
60static int virtio_data_present(struct hwrng *rng, int wait)
61{
62 if (data_left)
63 return 1;
64
65 if (!wait)
66 return 0;
67
68 wait_for_completion(&have_data);
69 return 1;
70}
71
72/* virtio_data_present() must have succeeded before this is called. */
73static int virtio_data_read(struct hwrng *rng, u32 *data)
74{
75 BUG_ON(!data_left);
76
77 *data = random_data[--data_left];
78
79 if (!data_left) {
80 init_completion(&have_data);
81 register_buffer();
82 }
83 return sizeof(*data);
84}
85
86static struct hwrng virtio_hwrng = {
87 .name = "virtio",
88 .data_present = virtio_data_present,
89 .data_read = virtio_data_read,
90};
91
92static int virtrng_probe(struct virtio_device *vdev)
93{
94 int err;
95
96 /* We expect a single virtqueue. */
97 vq = vdev->config->find_vq(vdev, 0, random_recv_done);
98 if (IS_ERR(vq))
99 return PTR_ERR(vq);
100
101 err = hwrng_register(&virtio_hwrng);
102 if (err) {
103 vdev->config->del_vq(vq);
104 return err;
105 }
106
107 register_buffer();
108 return 0;
109}
110
111static void virtrng_remove(struct virtio_device *vdev)
112{
113 vdev->config->reset(vdev);
114 hwrng_unregister(&virtio_hwrng);
115 vdev->config->del_vq(vq);
116}
117
118static struct virtio_device_id id_table[] = {
119 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
120 { 0 },
121};
122
123static struct virtio_driver virtio_rng = {
124 .driver.name = KBUILD_MODNAME,
125 .driver.owner = THIS_MODULE,
126 .id_table = id_table,
127 .probe = virtrng_probe,
128 .remove = __devexit_p(virtrng_remove),
129};
130
131static int __init init(void)
132{
133 int err;
134
135 random_data = kmalloc(RANDOM_DATA_SIZE, GFP_KERNEL);
136 if (!random_data)
137 return -ENOMEM;
138
139 err = register_virtio_driver(&virtio_rng);
140 if (err)
141 kfree(random_data);
142 return err;
143}
144
145static void __exit fini(void)
146{
147 kfree(random_data);
148 unregister_virtio_driver(&virtio_rng);
149}
150module_init(init);
151module_exit(fini);
152
153MODULE_DEVICE_TABLE(virtio, id_table);
154MODULE_DESCRIPTION("Virtio random number driver");
155MODULE_LICENSE("GPL");