rndis_wlan: convert scan to cfg80211
[linux-2.6/cjktty.git] / drivers / char / hw_random / virtio-rng.c
blobd0e563e4fc399bf37f06a4d19cea0bfcb5017713
1 /*
2 * Randomness driver for virtio
3 * Copyright (C) 2007, 2008 Rusty Russell IBM Corporation
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.
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.
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
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>
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
31 static struct virtqueue *vq;
32 static u32 *random_data;
33 static unsigned int data_left;
34 static DECLARE_COMPLETION(have_data);
36 static void random_recv_done(struct virtqueue *vq)
38 int len;
40 /* We never get spurious callbacks. */
41 if (!vq->vq_ops->get_buf(vq, &len))
42 BUG();
44 data_left = len / sizeof(random_data[0]);
45 complete(&have_data);
48 static void register_buffer(void)
50 struct scatterlist sg;
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);
59 /* At least we don't udelay() in a loop like some other drivers. */
60 static int virtio_data_present(struct hwrng *rng, int wait)
62 if (data_left)
63 return 1;
65 if (!wait)
66 return 0;
68 wait_for_completion(&have_data);
69 return 1;
72 /* virtio_data_present() must have succeeded before this is called. */
73 static int virtio_data_read(struct hwrng *rng, u32 *data)
75 BUG_ON(!data_left);
77 *data = random_data[--data_left];
79 if (!data_left) {
80 init_completion(&have_data);
81 register_buffer();
83 return sizeof(*data);
86 static struct hwrng virtio_hwrng = {
87 .name = "virtio",
88 .data_present = virtio_data_present,
89 .data_read = virtio_data_read,
92 static int virtrng_probe(struct virtio_device *vdev)
94 int err;
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);
101 err = hwrng_register(&virtio_hwrng);
102 if (err) {
103 vdev->config->del_vq(vq);
104 return err;
107 register_buffer();
108 return 0;
111 static void virtrng_remove(struct virtio_device *vdev)
113 vdev->config->reset(vdev);
114 hwrng_unregister(&virtio_hwrng);
115 vdev->config->del_vq(vq);
118 static struct virtio_device_id id_table[] = {
119 { VIRTIO_ID_RNG, VIRTIO_DEV_ANY_ID },
120 { 0 },
123 static 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),
131 static int __init init(void)
133 int err;
135 random_data = kmalloc(RANDOM_DATA_SIZE, GFP_KERNEL);
136 if (!random_data)
137 return -ENOMEM;
139 err = register_virtio_driver(&virtio_rng);
140 if (err)
141 kfree(random_data);
142 return err;
145 static void __exit fini(void)
147 kfree(random_data);
148 unregister_virtio_driver(&virtio_rng);
150 module_init(init);
151 module_exit(fini);
153 MODULE_DEVICE_TABLE(virtio, id_table);
154 MODULE_DESCRIPTION("Virtio random number driver");
155 MODULE_LICENSE("GPL");