Lines Matching full:rng
32 /* the current rng has been explicitly chosen by user via sysfs */
39 /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
54 static int hwrng_init(struct hwrng *rng);
57 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
65 static void add_early_randomness(struct hwrng *rng) in add_early_randomness() argument
70 bytes_read = rng_get_data(rng, rng_fillbuf, 32, 0); in add_early_randomness()
78 struct hwrng *rng = container_of(kref, struct hwrng, ref); in cleanup_rng() local
80 if (rng->cleanup) in cleanup_rng()
81 rng->cleanup(rng); in cleanup_rng()
83 complete(&rng->cleanup_done); in cleanup_rng()
86 static int set_current_rng(struct hwrng *rng) in set_current_rng() argument
92 err = hwrng_init(rng); in set_current_rng()
97 current_rng = rng; in set_current_rng()
133 struct hwrng *rng; in get_current_rng() local
138 rng = get_current_rng_nolock(); in get_current_rng()
141 return rng; in get_current_rng()
144 static void put_rng(struct hwrng *rng) in put_rng() argument
148 * on rng again immediately. in put_rng()
151 if (rng) in put_rng()
152 kref_put(&rng->ref, cleanup_rng); in put_rng()
156 static int hwrng_init(struct hwrng *rng) in hwrng_init() argument
158 if (kref_get_unless_zero(&rng->ref)) in hwrng_init()
161 if (rng->init) { in hwrng_init()
164 ret = rng->init(rng); in hwrng_init()
169 kref_init(&rng->ref); in hwrng_init()
170 reinit_completion(&rng->cleanup_done); in hwrng_init()
173 if (!rng->quality) in hwrng_init()
174 rng->quality = default_quality; in hwrng_init()
175 if (rng->quality > 1024) in hwrng_init()
176 rng->quality = 1024; in hwrng_init()
177 current_quality = rng->quality; /* obsolete */ in hwrng_init()
192 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size, in rng_get_data() argument
197 if (rng->read) in rng_get_data()
198 return rng->read(rng, (void *)buffer, size, wait); in rng_get_data()
200 if (rng->data_present) in rng_get_data()
201 present = rng->data_present(rng, wait); in rng_get_data()
206 return rng->data_read(rng, (u32 *)buffer); in rng_get_data()
217 struct hwrng *rng; in rng_dev_read() local
220 rng = get_current_rng(); in rng_dev_read()
221 if (IS_ERR(rng)) { in rng_dev_read()
222 err = PTR_ERR(rng); in rng_dev_read()
225 if (!rng) { in rng_dev_read()
235 bytes_read = rng_get_data(rng, rng_buffer, in rng_dev_read()
268 put_rng(rng); in rng_dev_read()
284 put_rng(rng); in rng_dev_read()
307 struct hwrng *rng, *new_rng = NULL; in enable_best_rng() local
312 /* no rng to use? */ in enable_best_rng()
319 /* use the rng which offers the best quality */ in enable_best_rng()
320 list_for_each_entry(rng, &rng_list, list) { in enable_best_rng()
321 if (!new_rng || rng->quality > new_rng->quality) in enable_best_rng()
322 new_rng = rng; in enable_best_rng()
337 struct hwrng *rng, *old_rng, *new_rng; in rng_current_store() local
347 list_for_each_entry(rng, &rng_list, list) { in rng_current_store()
348 if (sysfs_streq(rng->name, buf)) { in rng_current_store()
349 err = set_current_rng(rng); in rng_current_store()
373 struct hwrng *rng; in rng_current_show() local
375 rng = get_current_rng(); in rng_current_show()
376 if (IS_ERR(rng)) in rng_current_show()
377 return PTR_ERR(rng); in rng_current_show()
379 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none"); in rng_current_show()
380 put_rng(rng); in rng_current_show()
390 struct hwrng *rng; in rng_available_show() local
396 list_for_each_entry(rng, &rng_list, list) { in rng_available_show()
397 strlcat(buf, rng->name, PAGE_SIZE); in rng_available_show()
418 struct hwrng *rng; in rng_quality_show() local
420 rng = get_current_rng(); in rng_quality_show()
421 if (IS_ERR(rng)) in rng_quality_show()
422 return PTR_ERR(rng); in rng_quality_show()
424 if (!rng) /* no need to put_rng */ in rng_quality_show()
427 ret = sysfs_emit(buf, "%hu\n", rng->quality); in rng_quality_show()
428 put_rng(rng); in rng_quality_show()
461 /* the best available RNG may have changed */ in rng_quality_store()
501 struct hwrng *rng; in hwrng_fillfn() local
503 rng = get_current_rng(); in hwrng_fillfn()
504 if (IS_ERR(rng) || !rng) in hwrng_fillfn()
507 rc = rng_get_data(rng, rng_fillbuf, in hwrng_fillfn()
509 if (current_quality != rng->quality) in hwrng_fillfn()
510 rng->quality = current_quality; /* obsolete */ in hwrng_fillfn()
511 quality = rng->quality; in hwrng_fillfn()
515 hwrng_msleep(rng, 10000); in hwrng_fillfn()
517 put_rng(rng); in hwrng_fillfn()
537 int hwrng_register(struct hwrng *rng) in hwrng_register() argument
543 if (!rng->name || (!rng->data_read && !rng->read)) in hwrng_register()
551 if (strcmp(tmp->name, rng->name) == 0) in hwrng_register()
554 list_add_tail(&rng->list, &rng_list); in hwrng_register()
556 init_completion(&rng->cleanup_done); in hwrng_register()
557 complete(&rng->cleanup_done); in hwrng_register()
558 init_completion(&rng->dying); in hwrng_register()
561 (!cur_rng_set_by_user && rng->quality > current_rng->quality)) { in hwrng_register()
563 * Set new rng as current as the new rng source in hwrng_register()
567 err = set_current_rng(rng); in hwrng_register()
574 kref_get(&rng->ref); in hwrng_register()
577 if (is_new_current || !rng->init) { in hwrng_register()
580 * the system. If this rng device isn't going to be in hwrng_register()
585 add_early_randomness(rng); in hwrng_register()
588 put_rng(rng); in hwrng_register()
597 void hwrng_unregister(struct hwrng *rng) in hwrng_unregister() argument
605 list_del(&rng->list); in hwrng_unregister()
606 complete_all(&rng->dying); in hwrng_unregister()
607 if (current_rng == rng) { in hwrng_unregister()
629 wait_for_completion(&rng->cleanup_done); in hwrng_unregister()
648 int devm_hwrng_register(struct device *dev, struct hwrng *rng) in devm_hwrng_register() argument
657 error = hwrng_register(rng); in devm_hwrng_register()
663 *ptr = rng; in devm_hwrng_register()
669 void devm_hwrng_unregister(struct device *dev, struct hwrng *rng) in devm_hwrng_unregister() argument
671 devres_release(dev, devm_hwrng_release, devm_hwrng_match, rng); in devm_hwrng_unregister()
675 long hwrng_msleep(struct hwrng *rng, unsigned int msecs) in hwrng_msleep() argument
679 return wait_for_completion_interruptible_timeout(&rng->dying, timeout); in hwrng_msleep()
721 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");