Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 39 | #include <linux/sched.h> |
Arnd Bergmann | a9c4e8f | 2008-05-20 19:16:00 +0200 | [diff] [blame] | 40 | #include <linux/smp_lock.h> |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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 | |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 70 | static inline int hwrng_data_present(struct hwrng *rng, int wait) |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 71 | { |
| 72 | if (!rng->data_present) |
| 73 | return 1; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 74 | return rng->data_present(rng, wait); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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 | return 0; |
| 91 | } |
| 92 | |
| 93 | static ssize_t rng_dev_read(struct file *filp, char __user *buf, |
| 94 | size_t size, loff_t *offp) |
| 95 | { |
| 96 | u32 data; |
| 97 | ssize_t ret = 0; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 98 | int err = 0; |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 99 | int bytes_read; |
| 100 | |
| 101 | while (size) { |
| 102 | err = -ERESTARTSYS; |
| 103 | if (mutex_lock_interruptible(&rng_mutex)) |
| 104 | goto out; |
| 105 | if (!current_rng) { |
| 106 | mutex_unlock(&rng_mutex); |
| 107 | err = -ENODEV; |
| 108 | goto out; |
| 109 | } |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 110 | |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 111 | bytes_read = 0; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 112 | if (hwrng_data_present(current_rng, |
| 113 | !(filp->f_flags & O_NONBLOCK))) |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 114 | bytes_read = hwrng_data_read(current_rng, &data); |
| 115 | mutex_unlock(&rng_mutex); |
| 116 | |
| 117 | err = -EAGAIN; |
| 118 | if (!bytes_read && (filp->f_flags & O_NONBLOCK)) |
| 119 | goto out; |
Ralph Wuerthner | 893f112 | 2008-04-17 07:46:14 +0200 | [diff] [blame] | 120 | if (bytes_read < 0) { |
| 121 | err = bytes_read; |
| 122 | goto out; |
| 123 | } |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 124 | |
| 125 | err = -EFAULT; |
| 126 | while (bytes_read && size) { |
| 127 | if (put_user((u8)data, buf++)) |
| 128 | goto out; |
| 129 | size--; |
| 130 | ret++; |
| 131 | bytes_read--; |
| 132 | data >>= 8; |
| 133 | } |
| 134 | |
| 135 | if (need_resched()) |
| 136 | schedule_timeout_interruptible(1); |
| 137 | err = -ERESTARTSYS; |
| 138 | if (signal_pending(current)) |
| 139 | goto out; |
| 140 | } |
| 141 | out: |
| 142 | return ret ? : err; |
| 143 | } |
| 144 | |
| 145 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 146 | static const struct file_operations rng_chrdev_ops = { |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 147 | .owner = THIS_MODULE, |
| 148 | .open = rng_dev_open, |
| 149 | .read = rng_dev_read, |
| 150 | }; |
| 151 | |
| 152 | static struct miscdevice rng_miscdev = { |
| 153 | .minor = RNG_MISCDEV_MINOR, |
| 154 | .name = RNG_MODULE_NAME, |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame] | 155 | .nodename = "hwrng", |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 156 | .fops = &rng_chrdev_ops, |
| 157 | }; |
| 158 | |
| 159 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 160 | static ssize_t hwrng_attr_current_store(struct device *dev, |
| 161 | struct device_attribute *attr, |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 191 | static ssize_t hwrng_attr_current_show(struct device *dev, |
| 192 | struct device_attribute *attr, |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 210 | static ssize_t hwrng_attr_available_show(struct device *dev, |
| 211 | struct device_attribute *attr, |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 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); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 241 | |
| 242 | |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 243 | static void unregister_miscdev(void) |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 244 | { |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 245 | device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available); |
| 246 | device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current); |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 247 | misc_deregister(&rng_miscdev); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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; |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 257 | err = device_create_file(rng_miscdev.this_device, |
| 258 | &dev_attr_rng_current); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 259 | if (err) |
| 260 | goto err_misc_dereg; |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 261 | err = device_create_file(rng_miscdev.this_device, |
| 262 | &dev_attr_rng_available); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 263 | if (err) |
| 264 | goto err_remove_current; |
| 265 | out: |
| 266 | return err; |
| 267 | |
| 268 | err_remove_current: |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 269 | device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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 | |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 322 | void hwrng_unregister(struct hwrng *rng) |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 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)) |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 341 | unregister_miscdev(); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 342 | |
| 343 | mutex_unlock(&rng_mutex); |
| 344 | } |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 345 | EXPORT_SYMBOL_GPL(hwrng_unregister); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 346 | |
| 347 | |
| 348 | MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver"); |
| 349 | MODULE_LICENSE("GPL"); |