forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/arch/um/drivers/random.c
....@@ -11,6 +11,7 @@
1111 #include <linux/fs.h>
1212 #include <linux/interrupt.h>
1313 #include <linux/miscdevice.h>
14
+#include <linux/hw_random.h>
1415 #include <linux/delay.h>
1516 #include <linux/uaccess.h>
1617 #include <init.h>
....@@ -18,101 +19,45 @@
1819 #include <os.h>
1920
2021 /*
21
- * core module and version information
22
+ * core module information
2223 */
23
-#define RNG_VERSION "1.0.0"
2424 #define RNG_MODULE_NAME "hw_random"
25
-
26
-#define RNG_MISCDEV_MINOR 183 /* official */
2725
2826 /* Changed at init time, in the non-modular case, and at module load
2927 * time, in the module case. Presumably, the module subsystem
3028 * protects against a module being loaded twice at the same time.
3129 */
3230 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);
3433
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)
3635 {
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;
4237
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) {
7741 add_sigio_fd(random_fd);
7842
79
- add_wait_queue(&host_read_wait, &wait);
80
- set_current_state(TASK_INTERRUPTIBLE);
43
+ ret = wait_for_completion_killable(&have_data);
8144
82
- schedule();
83
- remove_wait_queue(&host_read_wait, &wait);
45
+ ignore_sigio_fd(random_fd);
46
+ deactivate_fd(random_fd, RANDOM_IRQ);
8447
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;
8952 }
90
- else
91
- return n;
92
-
93
- if (signal_pending (current))
94
- return ret ? : -ERESTARTSYS;
9553 }
96
- return ret;
54
+
55
+ return ret != -EAGAIN ? ret : 0;
9756 }
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
-};
11257
11358 static irqreturn_t random_interrupt(int irq, void *data)
11459 {
115
- wake_up(&host_read_wait);
60
+ complete(&have_data);
11661
11762 return IRQ_HANDLED;
11863 }
....@@ -129,18 +74,19 @@
12974 goto out;
13075
13176 random_fd = err;
132
-
13377 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
13478 0, "random", NULL);
13579 if (err)
13680 goto err_out_cleanup_hw;
13781
13882 sigio_broken(random_fd, 1);
83
+ hwrng.name = RNG_MODULE_NAME;
84
+ hwrng.read = rng_dev_read;
85
+ hwrng.quality = 1024;
13986
140
- err = misc_register (&rng_miscdev);
87
+ err = hwrng_register(&hwrng);
14188 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);
14490 goto err_out_cleanup_hw;
14591 }
14692 out:
....@@ -164,8 +110,8 @@
164110
165111 static void __exit rng_cleanup(void)
166112 {
113
+ hwrng_unregister(&hwrng);
167114 os_close_file(random_fd);
168
- misc_deregister (&rng_miscdev);
169115 }
170116
171117 module_init (rng_init);