Lines Matching full:rng

34 /* the current rng has been explicitly chosen by user via sysfs */
41 /* Protects rng read functions, data_avail, rng_buffer and rng_fillbuf */
56 static int hwrng_init(struct hwrng *rng);
59 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
67 static void add_early_randomness(struct hwrng *rng) in add_early_randomness() argument
72 bytes_read = rng_get_data(rng, rng_fillbuf, 32, 0); in add_early_randomness()
75 size_t entropy = bytes_read * 8 * rng->quality / 1024; in add_early_randomness()
82 struct hwrng *rng = container_of(kref, struct hwrng, ref); in cleanup_rng() local
84 if (rng->cleanup) in cleanup_rng()
85 rng->cleanup(rng); in cleanup_rng()
87 complete(&rng->cleanup_done); in cleanup_rng()
90 static int set_current_rng(struct hwrng *rng) in set_current_rng() argument
96 err = hwrng_init(rng); in set_current_rng()
101 current_rng = rng; in set_current_rng()
137 struct hwrng *rng; in get_current_rng() local
142 rng = get_current_rng_nolock(); in get_current_rng()
145 return rng; in get_current_rng()
148 static void put_rng(struct hwrng *rng) in put_rng() argument
152 * on rng again immediately. in put_rng()
155 if (rng) in put_rng()
156 kref_put(&rng->ref, cleanup_rng); in put_rng()
160 static int hwrng_init(struct hwrng *rng) in hwrng_init() argument
162 if (kref_get_unless_zero(&rng->ref)) in hwrng_init()
165 if (rng->init) { in hwrng_init()
168 ret = rng->init(rng); in hwrng_init()
173 kref_init(&rng->ref); in hwrng_init()
174 reinit_completion(&rng->cleanup_done); 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()
218 struct hwrng *rng; in rng_dev_read() local
221 rng = get_current_rng(); in rng_dev_read()
222 if (IS_ERR(rng)) { in rng_dev_read()
223 err = PTR_ERR(rng); in rng_dev_read()
226 if (!rng) { in rng_dev_read()
236 bytes_read = rng_get_data(rng, rng_buffer, in rng_dev_read()
261 put_rng(rng); in rng_dev_read()
289 put_rng(rng); in rng_dev_read()
312 struct hwrng *rng, *new_rng = NULL; in enable_best_rng() local
317 /* no rng to use? */ in enable_best_rng()
324 /* use the rng which offers the best quality */ in enable_best_rng()
325 list_for_each_entry(rng, &rng_list, list) { in enable_best_rng()
326 if (!new_rng || rng->quality > new_rng->quality) in enable_best_rng()
327 new_rng = rng; in enable_best_rng()
342 struct hwrng *rng, *old_rng, *new_rng; in rng_current_store() local
352 list_for_each_entry(rng, &rng_list, list) { in rng_current_store()
353 if (sysfs_streq(rng->name, buf)) { in rng_current_store()
354 err = set_current_rng(rng); in rng_current_store()
378 struct hwrng *rng; in rng_current_show() local
380 rng = get_current_rng(); in rng_current_show()
381 if (IS_ERR(rng)) in rng_current_show()
382 return PTR_ERR(rng); in rng_current_show()
384 ret = snprintf(buf, PAGE_SIZE, "%s\n", rng ? rng->name : "none"); in rng_current_show()
385 put_rng(rng); in rng_current_show()
395 struct hwrng *rng; in rng_available_show() local
401 list_for_each_entry(rng, &rng_list, list) { in rng_available_show()
402 strlcat(buf, rng->name, PAGE_SIZE); in rng_available_show()
423 struct hwrng *rng; in rng_quality_show() local
425 rng = get_current_rng(); in rng_quality_show()
426 if (IS_ERR(rng)) in rng_quality_show()
427 return PTR_ERR(rng); in rng_quality_show()
429 if (!rng) /* no need to put_rng */ in rng_quality_show()
432 ret = sysfs_emit(buf, "%hu\n", rng->quality); in rng_quality_show()
433 put_rng(rng); in rng_quality_show()
466 /* the best available RNG may have changed */ in rng_quality_store()
506 struct hwrng *rng; in hwrng_fillfn() local
508 rng = get_current_rng(); in hwrng_fillfn()
509 if (IS_ERR(rng) || !rng) in hwrng_fillfn()
512 rc = rng_get_data(rng, rng_fillbuf, in hwrng_fillfn()
514 if (current_quality != rng->quality) in hwrng_fillfn()
515 rng->quality = current_quality; /* obsolete */ in hwrng_fillfn()
516 quality = rng->quality; in hwrng_fillfn()
520 hwrng_msleep(rng, 10000); in hwrng_fillfn()
522 put_rng(rng); in hwrng_fillfn()
542 int hwrng_register(struct hwrng *rng) in hwrng_register() argument
548 if (!rng->name || (!rng->data_read && !rng->read)) in hwrng_register()
556 if (strcmp(tmp->name, rng->name) == 0) in hwrng_register()
559 list_add_tail(&rng->list, &rng_list); in hwrng_register()
561 init_completion(&rng->cleanup_done); in hwrng_register()
562 complete(&rng->cleanup_done); in hwrng_register()
563 init_completion(&rng->dying); in hwrng_register()
566 rng->quality = min_t(u16, min_t(u16, default_quality, 1024), rng->quality ?: 1024); in hwrng_register()
569 (!cur_rng_set_by_user && rng->quality > current_rng->quality)) { in hwrng_register()
571 * Set new rng as current as the new rng source in hwrng_register()
575 err = set_current_rng(rng); in hwrng_register()
582 kref_get(&rng->ref); in hwrng_register()
585 if (is_new_current || !rng->init) { in hwrng_register()
588 * the system. If this rng device isn't going to be in hwrng_register()
593 add_early_randomness(rng); in hwrng_register()
596 put_rng(rng); in hwrng_register()
605 void hwrng_unregister(struct hwrng *rng) in hwrng_unregister() argument
613 list_del(&rng->list); in hwrng_unregister()
614 complete_all(&rng->dying); in hwrng_unregister()
615 if (current_rng == rng) { in hwrng_unregister()
637 wait_for_completion(&rng->cleanup_done); in hwrng_unregister()
656 int devm_hwrng_register(struct device *dev, struct hwrng *rng) in devm_hwrng_register() argument
665 error = hwrng_register(rng); in devm_hwrng_register()
671 *ptr = rng; in devm_hwrng_register()
677 void devm_hwrng_unregister(struct device *dev, struct hwrng *rng) in devm_hwrng_unregister() argument
679 devres_release(dev, devm_hwrng_release, devm_hwrng_match, rng); in devm_hwrng_unregister()
683 long hwrng_msleep(struct hwrng *rng, unsigned int msecs) in hwrng_msleep() argument
687 return wait_for_completion_interruptible_timeout(&rng->dying, timeout); in hwrng_msleep()
691 long hwrng_yield(struct hwrng *rng) in hwrng_yield() argument
693 return wait_for_completion_interruptible_timeout(&rng->dying, 1); in hwrng_yield()
735 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");