* [PATCHv2] hw_random: add timeriomem-rng driver
@ 2009-02-07 9:53 Alexander Clouter
2009-02-10 4:57 ` Herbert Xu
0 siblings, 1 reply; 5+ messages in thread
From: Alexander Clouter @ 2009-02-07 9:53 UTC (permalink / raw)
To: linux-kernel; +Cc: akpm
Some hardware platforms, the TS-7800[1] is one for example, can
supply the kernel with an entropy source, albeit a slow one for
TS-7800 users, by just reading a particular IO address. This
source must not be read above a certain rate otherwise the quality
suffers.
The driver is then hooked into by calling
platform_device_(register|add|del) passing a structure similar to:
------
static struct timeriomem_rng_data ts78xx_ts_rng_data = {
.address = (u32 *__iomem) TS_RNG,
.period = 1000000, /* one second */
};
static struct platform_device ts78xx_ts_rng_device = {
.name = "timeriomem_rng",
.id = -1,
.dev = {
.platform_data = &ts78xx_ts_rng_data,
},
.num_resources = 0,
};
------
[1] http://www.embeddedarm.com/products/board-detail.php?product=TS-7800
Signed-off-by: Alexander Clouter <alex@digriz.org.uk>
---
drivers/char/hw_random/Kconfig | 14 +++
drivers/char/hw_random/Makefile | 1 +
drivers/char/hw_random/timeriomem-rng.c | 151 +++++++++++++++++++++++++++++++
include/linux/timeriomem-rng.h | 16 ++++
4 files changed, 182 insertions(+), 0 deletions(-)
create mode 100644 drivers/char/hw_random/timeriomem-rng.c
create mode 100644 include/linux/timeriomem-rng.h
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 8822eca..e86dd42 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -20,6 +20,20 @@ config HW_RANDOM
If unsure, say Y.
+config HW_RANDOM_TIMERIOMEM
+ tristate "Timer IOMEM HW Random Number Generator support"
+ depends on HW_RANDOM
+ ---help---
+ This driver provides kernel-side support for a generic Random
+ Number Generator used by reading a 'dumb' iomem address that
+ is to be read no faster than, for example, once a second;
+ the default FPGA bitstream on the TS-7800 has such functionality.
+
+ To compile this driver as a module, choose M here: the
+ module will be called timeriomem-rng.
+
+ If unsure, say Y.
+
config HW_RANDOM_INTEL
tristate "Intel HW Random Number Generator support"
depends on HW_RANDOM && (X86 || IA64) && PCI
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index b6effb7..e81d21a 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -4,6 +4,7 @@
obj-$(CONFIG_HW_RANDOM) += rng-core.o
rng-core-y := core.o
+obj-$(CONFIG_HW_RANDOM_TIMERIOMEM) += timeriomem-rng.o
obj-$(CONFIG_HW_RANDOM_INTEL) += intel-rng.o
obj-$(CONFIG_HW_RANDOM_AMD) += amd-rng.o
obj-$(CONFIG_HW_RANDOM_GEODE) += geode-rng.o
diff --git a/drivers/char/hw_random/timeriomem-rng.c b/drivers/char/hw_random/timeriomem-rng.c
new file mode 100644
index 0000000..06131fa
--- /dev/null
+++ b/drivers/char/hw_random/timeriomem-rng.c
@@ -0,0 +1,151 @@
+/*
+ * drivers/char/hw_random/timeriomem-rng.c
+ *
+ * Copyright (C) 2008 Alexander Clouter <alex@digriz.org.uk>
+ *
+ * Derived from drivers/char/hw_random/omap-rng.c
+ * Copyright 2005 (c) MontaVista Software, Inc.
+ * Author: Deepak Saxena <dsaxena@plexity.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Overview:
+ * This driver is useful for platforms that have an IO range that provides
+ * periodic random data from a single IO memory address. All the platform
+ * has to do is provide the address and 'wait time' that new data becomes
+ * available.
+ *
+ * TODO: add support for reading sizes other than 32bits and masking
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/platform_device.h>
+#include <linux/hw_random.h>
+#include <linux/io.h>
+#include <linux/timeriomem-rng.h>
+#include <linux/jiffies.h>
+#include <linux/sched.h>
+#include <linux/timer.h>
+
+static struct timeriomem_rng_data *timeriomem_rng_data;
+
+static void timeriomem_rng_trigger(unsigned long);
+static DEFINE_TIMER(timeriomem_rng_timer, &timeriomem_rng_trigger, 0, 0);
+
+/*
+ * have data return 1, however return 0 if we have nothing
+ */
+static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
+{
+ s32 delay;
+
+ if (rng->priv == 0)
+ return 1;
+
+ if (del_timer_sync(&timeriomem_rng_timer)) {
+ if (!wait)
+ return 0;
+
+ delay = timeriomem_rng_timer.expires - jiffies;
+
+ schedule_timeout_uninterruptible(delay);
+ }
+
+ return 1;
+}
+
+static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
+{
+ unsigned long cur;
+ s32 delay;
+
+ *data = readl(timeriomem_rng_data->address);
+
+ if (rng->priv != 0) {
+ cur = jiffies;
+
+ delay = cur - timeriomem_rng_timer.expires;
+ delay = rng->priv - (delay % rng->priv);
+
+ timeriomem_rng_timer.expires = cur + delay;
+ add_timer(&timeriomem_rng_timer);
+ }
+
+ return 4;
+}
+
+static void timeriomem_rng_trigger(unsigned long dummy)
+{
+ del_timer_sync(&timeriomem_rng_timer);
+}
+
+static struct hwrng timeriomem_rng_ops = {
+ .name = "timeriomem",
+ .data_present = timeriomem_rng_data_present,
+ .data_read = timeriomem_rng_data_read,
+ .priv = 0,
+};
+
+static int __init timeriomem_rng_probe(struct platform_device *pdev)
+{
+ int ret;
+
+ timeriomem_rng_data = pdev->dev.platform_data;
+
+ if (timeriomem_rng_data->period != 0
+ && usecs_to_jiffies(timeriomem_rng_data->period) > 0) {
+ timeriomem_rng_timer.expires = jiffies;
+
+ timeriomem_rng_ops.priv = usecs_to_jiffies(
+ timeriomem_rng_data->period);
+ }
+
+ ret = hwrng_register(&timeriomem_rng_ops);
+ if (ret) {
+ dev_err(&pdev->dev, "problem registering\n");
+ return ret;
+ }
+
+ dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
+ timeriomem_rng_data->address,
+ timeriomem_rng_data->period);
+
+ return 0;
+}
+
+static int __devexit timeriomem_rng_remove(struct platform_device *pdev)
+{
+ del_timer_sync(&timeriomem_rng_timer);
+ hwrng_unregister(&timeriomem_rng_ops);
+
+ return 0;
+}
+
+static struct platform_driver timeriomem_rng_driver = {
+ .driver = {
+ .name = "timeriomem_rng",
+ .owner = THIS_MODULE,
+ },
+ .probe = timeriomem_rng_probe,
+ .remove = __devexit_p(timeriomem_rng_remove),
+};
+
+static int __init timeriomem_rng_init(void)
+{
+ return platform_driver_register(&timeriomem_rng_driver);
+}
+
+static void __exit timeriomem_rng_exit(void)
+{
+ platform_driver_unregister(&timeriomem_rng_driver);
+}
+
+module_init(timeriomem_rng_init);
+module_exit(timeriomem_rng_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
+MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");
diff --git a/include/linux/timeriomem-rng.h b/include/linux/timeriomem-rng.h
new file mode 100644
index 0000000..16dd9e4
--- /dev/null
+++ b/include/linux/timeriomem-rng.h
@@ -0,0 +1,16 @@
+/*
+ * linux/include/linux/timeriomem-rng.h
+ *
+ * Copyright (c) 2008 Alexander Clouter <alex@digriz.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+struct timeriomem_rng_data {
+ u32 __iomem *address;
+
+ /* measures in usecs */
+ unsigned int period;
+};
--
1.5.6.5
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCHv2] hw_random: add timeriomem-rng driver
2009-02-07 9:53 [PATCHv2] hw_random: add timeriomem-rng driver Alexander Clouter
@ 2009-02-10 4:57 ` Herbert Xu
2009-02-10 19:39 ` Alexander Clouter
0 siblings, 1 reply; 5+ messages in thread
From: Herbert Xu @ 2009-02-10 4:57 UTC (permalink / raw)
To: Alexander Clouter; +Cc: linux-kernel, akpm
On Sat, Feb 07, 2009 at 09:53:17AM +0000, Alexander Clouter wrote:
>
> +/*
> + * have data return 1, however return 0 if we have nothing
> + */
> +static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
> +{
> + s32 delay;
> +
> + if (rng->priv == 0)
> + return 1;
> +
> + if (del_timer_sync(&timeriomem_rng_timer)) {
> + if (!wait)
> + return 0;
> +
> + delay = timeriomem_rng_timer.expires - jiffies;
> +
> + schedule_timeout_uninterruptible(delay);
> + }
Sorry, but this just doesn't work for O_NONBLOCK reads. What'll
happen is that the first failed read will return -EAGAIN, and when
we're called again immediately (because hwrng polling support is
non-existant), it'll just read the data right away because now
there is no timer.
> +static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
> +{
> + unsigned long cur;
> + s32 delay;
> +
> + *data = readl(timeriomem_rng_data->address);
> +
> + if (rng->priv != 0) {
> + cur = jiffies;
> +
> + delay = cur - timeriomem_rng_timer.expires;
> + delay = rng->priv - (delay % rng->priv);
> +
> + timeriomem_rng_timer.expires = cur + delay;
> + add_timer(&timeriomem_rng_timer);
> + }
Instead of deleting the timer above, why not create a rng->present
variable which you set to zero here, and the timer sets it to
non-zero. Then you just need to return rng->present in your
data_present function.
> +static void timeriomem_rng_trigger(unsigned long dummy)
> +{
> + del_timer_sync(&timeriomem_rng_timer);
> +}
This is going to create an infinite loop. There is no point in
deleting yourself.
Cheers,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCHv2] hw_random: add timeriomem-rng driver
2009-02-10 4:57 ` Herbert Xu
@ 2009-02-10 19:39 ` Alexander Clouter
2009-02-10 19:49 ` Alexander Clouter
2009-02-11 13:27 ` Herbert Xu
0 siblings, 2 replies; 5+ messages in thread
From: Alexander Clouter @ 2009-02-10 19:39 UTC (permalink / raw)
To: Herbert Xu; +Cc: linux-kernel, akpm
Hi,
* Herbert Xu <herbert@gondor.apana.org.au> [2009-02-10 15:57:49+1100]:
>
> On Sat, Feb 07, 2009 at 09:53:17AM +0000, Alexander Clouter wrote:
> >
> > +/*
> > + * have data return 1, however return 0 if we have nothing
> > + */
> > +static int timeriomem_rng_data_present(struct hwrng *rng, int wait)
> > +{
> > + s32 delay;
> > +
> > + if (rng->priv == 0)
> > + return 1;
> > +
> > + if (del_timer_sync(&timeriomem_rng_timer)) {
> > + if (!wait)
> > + return 0;
> > +
> > + delay = timeriomem_rng_timer.expires - jiffies;
> > +
> > + schedule_timeout_uninterruptible(delay);
> > + }
>
> Sorry, but this just doesn't work for O_NONBLOCK reads. What'll
> happen is that the first failed read will return -EAGAIN, and when
> we're called again immediately (because hwrng polling support is
> non-existant), it'll just read the data right away because now
> there is no timer.
>
My original code[1] approach, with timer_pending() in there, would have
been okay then, right?
----
if (timer_pending(&timeriomem_rng_timer)) {
if (!wait)
return 0;
del_timer_sync(&timeriomem_rng_timer);
delay = timeriomem_rng_timer.expires - jiffies;
schedule_timeout_uninterruptible(delay);
}
----
> > +static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data)
> > +{
> > + unsigned long cur;
> > + s32 delay;
> > +
> > + *data = readl(timeriomem_rng_data->address);
> > +
> > + if (rng->priv != 0) {
> > + cur = jiffies;
> > +
> > + delay = cur - timeriomem_rng_timer.expires;
> > + delay = rng->priv - (delay % rng->priv);
> > +
> > + timeriomem_rng_timer.expires = cur + delay;
> > + add_timer(&timeriomem_rng_timer);
> > + }
>
> Instead of deleting the timer above, why not create a rng->present
> variable which you set to zero here, and the timer sets it to
> non-zero. Then you just need to return rng->present in your
> data_present function.
>
Sounds fair, better than the timer_pending approach and easier to
understand.
> > +static void timeriomem_rng_trigger(unsigned long dummy)
> > +{
> > + del_timer_sync(&timeriomem_rng_timer);
> > +}
>
> This is going to create an infinite loop. There is no point in
> deleting yourself.
>
Noted.
Cheers
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCHv2] hw_random: add timeriomem-rng driver
2009-02-10 19:39 ` Alexander Clouter
@ 2009-02-10 19:49 ` Alexander Clouter
2009-02-11 13:27 ` Herbert Xu
1 sibling, 0 replies; 5+ messages in thread
From: Alexander Clouter @ 2009-02-10 19:49 UTC (permalink / raw)
To: Herbert Xu; +Cc: linux-kernel, akpm
Hi,
* Alexander Clouter <alex@digriz.org.uk> [2009-02-10 19:39:04+0000]:
>
> * Herbert Xu <herbert@gondor.apana.org.au> [2009-02-10 15:57:49+1100]:
> >
> > Sorry, but this just doesn't work for O_NONBLOCK reads. What'll
> > happen is that the first failed read will return -EAGAIN, and when
> > we're called again immediately (because hwrng polling support is
> > non-existant), it'll just read the data right away because now
> > there is no timer.
> >
> My original code[1] approach, with timer_pending() in there, would have
> been okay then, right?
> ----
> if (timer_pending(&timeriomem_rng_timer)) {
> if (!wait)
> return 0;
>
> del_timer_sync(&timeriomem_rng_timer);
> delay = timeriomem_rng_timer.expires - jiffies;
>
> schedule_timeout_uninterruptible(delay);
> }
> ----
>
> [snipped]
> >
> > Instead of deleting the timer above, why not create a rng->present
> > variable which you set to zero here, and the timer sets it to
> > non-zero. Then you just need to return rng->present in your
> > data_present function.
> >
> Sounds fair, better than the timer_pending approach and easier to
> understand.
>
Actually, as timer_pending() just reads a field in timer_list (or so
says the 'manual'), would it not be a waste to have a second effectively
duplicate variable? In a blocking read of data_presnt you would have to
have del_timer_sync() anyway...the two approaches would be identical
except for the rng->present variable approach would force us to waste a
whole extra bit of ram?
Should I keep with my old original timer_pending() approach?
Of course I could be talking rubbish...
Cheers
--
Alexander Clouter
.sigmonster says: So many men; so little time.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCHv2] hw_random: add timeriomem-rng driver
2009-02-10 19:39 ` Alexander Clouter
2009-02-10 19:49 ` Alexander Clouter
@ 2009-02-11 13:27 ` Herbert Xu
1 sibling, 0 replies; 5+ messages in thread
From: Herbert Xu @ 2009-02-11 13:27 UTC (permalink / raw)
To: Alexander Clouter; +Cc: linux-kernel, akpm
On Tue, Feb 10, 2009 at 07:39:04PM +0000, Alexander Clouter wrote:
>
> My original code[1] approach, with timer_pending() in there, would have
> been okay then, right?
> ----
> if (timer_pending(&timeriomem_rng_timer)) {
> if (!wait)
> return 0;
>
> del_timer_sync(&timeriomem_rng_timer);
> delay = timeriomem_rng_timer.expires - jiffies;
>
> schedule_timeout_uninterruptible(delay);
> }
Yes probably but it is racy. Also it's better if we can avoid
the expires - jiffies calculation if only because then we won't
have to tell Andrew why it can't be negative :)
> > Instead of deleting the timer above, why not create a rng->present
> > variable which you set to zero here, and the timer sets it to
> > non-zero. Then you just need to return rng->present in your
> > data_present function.
>
> Sounds fair, better than the timer_pending approach and easier to
> understand.
Oh I forgot about the blocking case. We can probably add a
completion object to handle that. So something like
if (!wait || rng->present)
return rng->present;
wait_for_completion(&rng->completion);
return 1;
The timer would do
rng->present = 1;
complete(&rng->completion);
And the reader would do
rng->present = 0;
INIT_COMPLETION(rng->completion);
add_timer(...)
Note that everything but the timer is completely serialised by
the hwrng mutex so we only need to worry about racing against
the timer.
Cheers,
--
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-02-11 13:27 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-07 9:53 [PATCHv2] hw_random: add timeriomem-rng driver Alexander Clouter
2009-02-10 4:57 ` Herbert Xu
2009-02-10 19:39 ` Alexander Clouter
2009-02-10 19:49 ` Alexander Clouter
2009-02-11 13:27 ` Herbert Xu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).