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; |
Arnd Bergmann | a9c4e8f | 2008-05-20 19:16:00 +0200 | [diff] [blame] | 90 | cycle_kernel_lock(); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static ssize_t rng_dev_read(struct file *filp, char __user *buf, |
| 95 | size_t size, loff_t *offp) |
| 96 | { |
| 97 | u32 data; |
| 98 | ssize_t ret = 0; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 99 | int err = 0; |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 100 | int bytes_read; |
| 101 | |
| 102 | while (size) { |
| 103 | err = -ERESTARTSYS; |
| 104 | if (mutex_lock_interruptible(&rng_mutex)) |
| 105 | goto out; |
| 106 | if (!current_rng) { |
| 107 | mutex_unlock(&rng_mutex); |
| 108 | err = -ENODEV; |
| 109 | goto out; |
| 110 | } |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 111 | |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 112 | bytes_read = 0; |
Patrick McHardy | 984e976 | 2007-11-21 12:24:45 +0800 | [diff] [blame] | 113 | if (hwrng_data_present(current_rng, |
| 114 | !(filp->f_flags & O_NONBLOCK))) |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 115 | bytes_read = hwrng_data_read(current_rng, &data); |
| 116 | mutex_unlock(&rng_mutex); |
| 117 | |
| 118 | err = -EAGAIN; |
| 119 | if (!bytes_read && (filp->f_flags & O_NONBLOCK)) |
| 120 | goto out; |
Ralph Wuerthner | 893f112 | 2008-04-17 07:46:14 +0200 | [diff] [blame] | 121 | if (bytes_read < 0) { |
| 122 | err = bytes_read; |
| 123 | goto out; |
| 124 | } |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 125 | |
| 126 | err = -EFAULT; |
| 127 | while (bytes_read && size) { |
| 128 | if (put_user((u8)data, buf++)) |
| 129 | goto out; |
| 130 | size--; |
| 131 | ret++; |
| 132 | bytes_read--; |
| 133 | data >>= 8; |
| 134 | } |
| 135 | |
| 136 | if (need_resched()) |
| 137 | schedule_timeout_interruptible(1); |
| 138 | err = -ERESTARTSYS; |
| 139 | if (signal_pending(current)) |
| 140 | goto out; |
| 141 | } |
| 142 | out: |
| 143 | return ret ? : err; |
| 144 | } |
| 145 | |
| 146 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 147 | static const struct file_operations rng_chrdev_ops = { |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 148 | .owner = THIS_MODULE, |
| 149 | .open = rng_dev_open, |
| 150 | .read = rng_dev_read, |
| 151 | }; |
| 152 | |
| 153 | static struct miscdevice rng_miscdev = { |
| 154 | .minor = RNG_MISCDEV_MINOR, |
| 155 | .name = RNG_MODULE_NAME, |
Kay Sievers | e454cea | 2009-09-18 23:01:12 +0200 | [diff] [blame^] | 156 | .nodename = "hwrng", |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 157 | .fops = &rng_chrdev_ops, |
| 158 | }; |
| 159 | |
| 160 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 161 | static ssize_t hwrng_attr_current_store(struct device *dev, |
| 162 | struct device_attribute *attr, |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 163 | const char *buf, size_t len) |
| 164 | { |
| 165 | int err; |
| 166 | struct hwrng *rng; |
| 167 | |
| 168 | err = mutex_lock_interruptible(&rng_mutex); |
| 169 | if (err) |
| 170 | return -ERESTARTSYS; |
| 171 | err = -ENODEV; |
| 172 | list_for_each_entry(rng, &rng_list, list) { |
| 173 | if (strcmp(rng->name, buf) == 0) { |
| 174 | if (rng == current_rng) { |
| 175 | err = 0; |
| 176 | break; |
| 177 | } |
| 178 | err = hwrng_init(rng); |
| 179 | if (err) |
| 180 | break; |
| 181 | hwrng_cleanup(current_rng); |
| 182 | current_rng = rng; |
| 183 | err = 0; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | mutex_unlock(&rng_mutex); |
| 188 | |
| 189 | return err ? : len; |
| 190 | } |
| 191 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 192 | static ssize_t hwrng_attr_current_show(struct device *dev, |
| 193 | struct device_attribute *attr, |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 194 | char *buf) |
| 195 | { |
| 196 | int err; |
| 197 | ssize_t ret; |
| 198 | const char *name = "none"; |
| 199 | |
| 200 | err = mutex_lock_interruptible(&rng_mutex); |
| 201 | if (err) |
| 202 | return -ERESTARTSYS; |
| 203 | if (current_rng) |
| 204 | name = current_rng->name; |
| 205 | ret = snprintf(buf, PAGE_SIZE, "%s\n", name); |
| 206 | mutex_unlock(&rng_mutex); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 211 | static ssize_t hwrng_attr_available_show(struct device *dev, |
| 212 | struct device_attribute *attr, |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 213 | char *buf) |
| 214 | { |
| 215 | int err; |
| 216 | ssize_t ret = 0; |
| 217 | struct hwrng *rng; |
| 218 | |
| 219 | err = mutex_lock_interruptible(&rng_mutex); |
| 220 | if (err) |
| 221 | return -ERESTARTSYS; |
| 222 | buf[0] = '\0'; |
| 223 | list_for_each_entry(rng, &rng_list, list) { |
| 224 | strncat(buf, rng->name, PAGE_SIZE - ret - 1); |
| 225 | ret += strlen(rng->name); |
| 226 | strncat(buf, " ", PAGE_SIZE - ret - 1); |
| 227 | ret++; |
| 228 | } |
| 229 | strncat(buf, "\n", PAGE_SIZE - ret - 1); |
| 230 | ret++; |
| 231 | mutex_unlock(&rng_mutex); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 236 | static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR, |
| 237 | hwrng_attr_current_show, |
| 238 | hwrng_attr_current_store); |
| 239 | static DEVICE_ATTR(rng_available, S_IRUGO, |
| 240 | hwrng_attr_available_show, |
| 241 | NULL); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 242 | |
| 243 | |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 244 | static void unregister_miscdev(void) |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 245 | { |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 246 | device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available); |
| 247 | device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current); |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 248 | misc_deregister(&rng_miscdev); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static int register_miscdev(void) |
| 252 | { |
| 253 | int err; |
| 254 | |
| 255 | err = misc_register(&rng_miscdev); |
| 256 | if (err) |
| 257 | goto out; |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 258 | err = device_create_file(rng_miscdev.this_device, |
| 259 | &dev_attr_rng_current); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 260 | if (err) |
| 261 | goto err_misc_dereg; |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 262 | err = device_create_file(rng_miscdev.this_device, |
| 263 | &dev_attr_rng_available); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 264 | if (err) |
| 265 | goto err_remove_current; |
| 266 | out: |
| 267 | return err; |
| 268 | |
| 269 | err_remove_current: |
Greg Kroah-Hartman | 94fbcde | 2006-07-27 16:16:04 -0700 | [diff] [blame] | 270 | device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 271 | err_misc_dereg: |
| 272 | misc_deregister(&rng_miscdev); |
| 273 | goto out; |
| 274 | } |
| 275 | |
| 276 | int hwrng_register(struct hwrng *rng) |
| 277 | { |
| 278 | int must_register_misc; |
| 279 | int err = -EINVAL; |
| 280 | struct hwrng *old_rng, *tmp; |
| 281 | |
| 282 | if (rng->name == NULL || |
| 283 | rng->data_read == NULL) |
| 284 | goto out; |
| 285 | |
| 286 | mutex_lock(&rng_mutex); |
| 287 | |
| 288 | /* Must not register two RNGs with the same name. */ |
| 289 | err = -EEXIST; |
| 290 | list_for_each_entry(tmp, &rng_list, list) { |
| 291 | if (strcmp(tmp->name, rng->name) == 0) |
| 292 | goto out_unlock; |
| 293 | } |
| 294 | |
| 295 | must_register_misc = (current_rng == NULL); |
| 296 | old_rng = current_rng; |
| 297 | if (!old_rng) { |
| 298 | err = hwrng_init(rng); |
| 299 | if (err) |
| 300 | goto out_unlock; |
| 301 | current_rng = rng; |
| 302 | } |
| 303 | err = 0; |
| 304 | if (must_register_misc) { |
| 305 | err = register_miscdev(); |
| 306 | if (err) { |
| 307 | if (!old_rng) { |
| 308 | hwrng_cleanup(rng); |
| 309 | current_rng = NULL; |
| 310 | } |
| 311 | goto out_unlock; |
| 312 | } |
| 313 | } |
| 314 | INIT_LIST_HEAD(&rng->list); |
| 315 | list_add_tail(&rng->list, &rng_list); |
| 316 | out_unlock: |
| 317 | mutex_unlock(&rng_mutex); |
| 318 | out: |
| 319 | return err; |
| 320 | } |
| 321 | EXPORT_SYMBOL_GPL(hwrng_register); |
| 322 | |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 323 | void hwrng_unregister(struct hwrng *rng) |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 324 | { |
| 325 | int err; |
| 326 | |
| 327 | mutex_lock(&rng_mutex); |
| 328 | |
| 329 | list_del(&rng->list); |
| 330 | if (current_rng == rng) { |
| 331 | hwrng_cleanup(rng); |
| 332 | if (list_empty(&rng_list)) { |
| 333 | current_rng = NULL; |
| 334 | } else { |
| 335 | current_rng = list_entry(rng_list.prev, struct hwrng, list); |
| 336 | err = hwrng_init(current_rng); |
| 337 | if (err) |
| 338 | current_rng = NULL; |
| 339 | } |
| 340 | } |
| 341 | if (list_empty(&rng_list)) |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 342 | unregister_miscdev(); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 343 | |
| 344 | mutex_unlock(&rng_mutex); |
| 345 | } |
Rafael J. Wysocki | b844eba | 2008-03-23 20:28:24 +0100 | [diff] [blame] | 346 | EXPORT_SYMBOL_GPL(hwrng_unregister); |
Michael Buesch | 844dd05 | 2006-06-26 00:24:59 -0700 | [diff] [blame] | 347 | |
| 348 | |
| 349 | MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver"); |
| 350 | MODULE_LICENSE("GPL"); |