.. | .. |
---|
11 | 11 | #include <linux/fs.h> |
---|
12 | 12 | #include <linux/interrupt.h> |
---|
13 | 13 | #include <linux/miscdevice.h> |
---|
| 14 | +#include <linux/hw_random.h> |
---|
14 | 15 | #include <linux/delay.h> |
---|
15 | 16 | #include <linux/uaccess.h> |
---|
16 | 17 | #include <init.h> |
---|
.. | .. |
---|
18 | 19 | #include <os.h> |
---|
19 | 20 | |
---|
20 | 21 | /* |
---|
21 | | - * core module and version information |
---|
| 22 | + * core module information |
---|
22 | 23 | */ |
---|
23 | | -#define RNG_VERSION "1.0.0" |
---|
24 | 24 | #define RNG_MODULE_NAME "hw_random" |
---|
25 | | - |
---|
26 | | -#define RNG_MISCDEV_MINOR 183 /* official */ |
---|
27 | 25 | |
---|
28 | 26 | /* Changed at init time, in the non-modular case, and at module load |
---|
29 | 27 | * time, in the module case. Presumably, the module subsystem |
---|
30 | 28 | * protects against a module being loaded twice at the same time. |
---|
31 | 29 | */ |
---|
32 | 30 | static int random_fd = -1; |
---|
33 | | -static DECLARE_WAIT_QUEUE_HEAD(host_read_wait); |
---|
| 31 | +static struct hwrng hwrng; |
---|
| 32 | +static DECLARE_COMPLETION(have_data); |
---|
34 | 33 | |
---|
35 | | -static int rng_dev_open (struct inode *inode, struct file *filp) |
---|
| 34 | +static int rng_dev_read(struct hwrng *rng, void *buf, size_t max, bool block) |
---|
36 | 35 | { |
---|
37 | | - /* enforce read-only access to this chrdev */ |
---|
38 | | - if ((filp->f_mode & FMODE_READ) == 0) |
---|
39 | | - return -EINVAL; |
---|
40 | | - if ((filp->f_mode & FMODE_WRITE) != 0) |
---|
41 | | - return -EINVAL; |
---|
| 36 | + int ret; |
---|
42 | 37 | |
---|
43 | | - return 0; |
---|
44 | | -} |
---|
45 | | - |
---|
46 | | -static atomic_t host_sleep_count = ATOMIC_INIT(0); |
---|
47 | | - |
---|
48 | | -static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size, |
---|
49 | | - loff_t *offp) |
---|
50 | | -{ |
---|
51 | | - u32 data; |
---|
52 | | - int n, ret = 0, have_data; |
---|
53 | | - |
---|
54 | | - while (size) { |
---|
55 | | - n = os_read_file(random_fd, &data, sizeof(data)); |
---|
56 | | - if (n > 0) { |
---|
57 | | - have_data = n; |
---|
58 | | - while (have_data && size) { |
---|
59 | | - if (put_user((u8) data, buf++)) { |
---|
60 | | - ret = ret ? : -EFAULT; |
---|
61 | | - break; |
---|
62 | | - } |
---|
63 | | - size--; |
---|
64 | | - ret++; |
---|
65 | | - have_data--; |
---|
66 | | - data >>= 8; |
---|
67 | | - } |
---|
68 | | - } |
---|
69 | | - else if (n == -EAGAIN) { |
---|
70 | | - DECLARE_WAITQUEUE(wait, current); |
---|
71 | | - |
---|
72 | | - if (filp->f_flags & O_NONBLOCK) |
---|
73 | | - return ret ? : -EAGAIN; |
---|
74 | | - |
---|
75 | | - atomic_inc(&host_sleep_count); |
---|
76 | | - reactivate_fd(random_fd, RANDOM_IRQ); |
---|
| 38 | + for (;;) { |
---|
| 39 | + ret = os_read_file(random_fd, buf, max); |
---|
| 40 | + if (block && ret == -EAGAIN) { |
---|
77 | 41 | add_sigio_fd(random_fd); |
---|
78 | 42 | |
---|
79 | | - add_wait_queue(&host_read_wait, &wait); |
---|
80 | | - set_current_state(TASK_INTERRUPTIBLE); |
---|
| 43 | + ret = wait_for_completion_killable(&have_data); |
---|
81 | 44 | |
---|
82 | | - schedule(); |
---|
83 | | - remove_wait_queue(&host_read_wait, &wait); |
---|
| 45 | + ignore_sigio_fd(random_fd); |
---|
| 46 | + deactivate_fd(random_fd, RANDOM_IRQ); |
---|
84 | 47 | |
---|
85 | | - if (atomic_dec_and_test(&host_sleep_count)) { |
---|
86 | | - ignore_sigio_fd(random_fd); |
---|
87 | | - deactivate_fd(random_fd, RANDOM_IRQ); |
---|
88 | | - } |
---|
| 48 | + if (ret < 0) |
---|
| 49 | + break; |
---|
| 50 | + } else { |
---|
| 51 | + break; |
---|
89 | 52 | } |
---|
90 | | - else |
---|
91 | | - return n; |
---|
92 | | - |
---|
93 | | - if (signal_pending (current)) |
---|
94 | | - return ret ? : -ERESTARTSYS; |
---|
95 | 53 | } |
---|
96 | | - return ret; |
---|
| 54 | + |
---|
| 55 | + return ret != -EAGAIN ? ret : 0; |
---|
97 | 56 | } |
---|
98 | | - |
---|
99 | | -static const struct file_operations rng_chrdev_ops = { |
---|
100 | | - .owner = THIS_MODULE, |
---|
101 | | - .open = rng_dev_open, |
---|
102 | | - .read = rng_dev_read, |
---|
103 | | - .llseek = noop_llseek, |
---|
104 | | -}; |
---|
105 | | - |
---|
106 | | -/* rng_init shouldn't be called more than once at boot time */ |
---|
107 | | -static struct miscdevice rng_miscdev = { |
---|
108 | | - RNG_MISCDEV_MINOR, |
---|
109 | | - RNG_MODULE_NAME, |
---|
110 | | - &rng_chrdev_ops, |
---|
111 | | -}; |
---|
112 | 57 | |
---|
113 | 58 | static irqreturn_t random_interrupt(int irq, void *data) |
---|
114 | 59 | { |
---|
115 | | - wake_up(&host_read_wait); |
---|
| 60 | + complete(&have_data); |
---|
116 | 61 | |
---|
117 | 62 | return IRQ_HANDLED; |
---|
118 | 63 | } |
---|
.. | .. |
---|
129 | 74 | goto out; |
---|
130 | 75 | |
---|
131 | 76 | random_fd = err; |
---|
132 | | - |
---|
133 | 77 | err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt, |
---|
134 | 78 | 0, "random", NULL); |
---|
135 | 79 | if (err) |
---|
136 | 80 | goto err_out_cleanup_hw; |
---|
137 | 81 | |
---|
138 | 82 | sigio_broken(random_fd, 1); |
---|
| 83 | + hwrng.name = RNG_MODULE_NAME; |
---|
| 84 | + hwrng.read = rng_dev_read; |
---|
| 85 | + hwrng.quality = 1024; |
---|
139 | 86 | |
---|
140 | | - err = misc_register (&rng_miscdev); |
---|
| 87 | + err = hwrng_register(&hwrng); |
---|
141 | 88 | if (err) { |
---|
142 | | - printk (KERN_ERR RNG_MODULE_NAME ": misc device register " |
---|
143 | | - "failed\n"); |
---|
| 89 | + pr_err(RNG_MODULE_NAME " registering failed (%d)\n", err); |
---|
144 | 90 | goto err_out_cleanup_hw; |
---|
145 | 91 | } |
---|
146 | 92 | out: |
---|
.. | .. |
---|
164 | 110 | |
---|
165 | 111 | static void __exit rng_cleanup(void) |
---|
166 | 112 | { |
---|
| 113 | + hwrng_unregister(&hwrng); |
---|
167 | 114 | os_close_file(random_fd); |
---|
168 | | - misc_deregister (&rng_miscdev); |
---|
169 | 115 | } |
---|
170 | 116 | |
---|
171 | 117 | module_init (rng_init); |
---|