Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwrng: timeriomem - update to support more than one device

timeriomem_rng only supports a single device instance. This patch
enables multiple timeriomem_rng devices to coexist as well as adds
some additional error checking.

Signed-off-by: Alexander Clouter <alex@digriz.org.uk>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Alexander Clouter and committed by
Herbert Xu
1907da78 57ae1b05

+119 -62
+119 -57
drivers/char/hw_random/timeriomem-rng.c
··· 25 25 #include <linux/platform_device.h> 26 26 #include <linux/hw_random.h> 27 27 #include <linux/io.h> 28 + #include <linux/slab.h> 28 29 #include <linux/timeriomem-rng.h> 29 30 #include <linux/jiffies.h> 30 31 #include <linux/sched.h> 31 32 #include <linux/timer.h> 32 33 #include <linux/completion.h> 33 34 34 - static struct timeriomem_rng_data *timeriomem_rng_data; 35 + struct timeriomem_rng_private_data { 36 + void __iomem *io_base; 37 + unsigned int expires; 38 + unsigned int period; 39 + unsigned int present:1; 35 40 36 - static void timeriomem_rng_trigger(unsigned long); 37 - static DEFINE_TIMER(timeriomem_rng_timer, timeriomem_rng_trigger, 0, 0); 41 + struct timer_list timer; 42 + struct completion completion; 43 + 44 + struct hwrng timeriomem_rng_ops; 45 + }; 46 + 47 + #define to_rng_priv(rng) \ 48 + ((struct timeriomem_rng_private_data *)rng->priv) 38 49 39 50 /* 40 51 * have data return 1, however return 0 if we have nothing 41 52 */ 42 53 static int timeriomem_rng_data_present(struct hwrng *rng, int wait) 43 54 { 44 - if (rng->priv == 0) 45 - return 1; 55 + struct timeriomem_rng_private_data *priv = to_rng_priv(rng); 46 56 47 - if (!wait || timeriomem_rng_data->present) 48 - return timeriomem_rng_data->present; 57 + if (!wait || priv->present) 58 + return priv->present; 49 59 50 - wait_for_completion(&timeriomem_rng_data->completion); 60 + wait_for_completion(&priv->completion); 51 61 52 62 return 1; 53 63 } 54 64 55 65 static int timeriomem_rng_data_read(struct hwrng *rng, u32 *data) 56 66 { 67 + struct timeriomem_rng_private_data *priv = to_rng_priv(rng); 57 68 unsigned long cur; 58 69 s32 delay; 59 70 60 - *data = readl(timeriomem_rng_data->address); 71 + *data = readl(priv->io_base); 61 72 62 - if (rng->priv != 0) { 63 - cur = jiffies; 73 + cur = jiffies; 64 74 65 - delay = cur - timeriomem_rng_timer.expires; 66 - delay = rng->priv - (delay % rng->priv); 75 + delay = cur - priv->expires; 76 + delay = priv->period - (delay % priv->period); 67 77 68 - timeriomem_rng_timer.expires = cur + delay; 69 - timeriomem_rng_data->present = 0; 78 + priv->expires = cur + delay; 79 + priv->present = 0; 70 80 71 - init_completion(&timeriomem_rng_data->completion); 72 - add_timer(&timeriomem_rng_timer); 73 - } 81 + INIT_COMPLETION(priv->completion); 82 + mod_timer(&priv->timer, priv->expires); 74 83 75 84 return 4; 76 85 } 77 86 78 - static void timeriomem_rng_trigger(unsigned long dummy) 87 + static void timeriomem_rng_trigger(unsigned long data) 79 88 { 80 - timeriomem_rng_data->present = 1; 81 - complete(&timeriomem_rng_data->completion); 82 - } 89 + struct timeriomem_rng_private_data *priv 90 + = (struct timeriomem_rng_private_data *)data; 83 91 84 - static struct hwrng timeriomem_rng_ops = { 85 - .name = "timeriomem", 86 - .data_present = timeriomem_rng_data_present, 87 - .data_read = timeriomem_rng_data_read, 88 - .priv = 0, 89 - }; 92 + priv->present = 1; 93 + complete(&priv->completion); 94 + } 90 95 91 96 static int timeriomem_rng_probe(struct platform_device *pdev) 92 97 { 98 + struct timeriomem_rng_data *pdata = pdev->dev.platform_data; 99 + struct timeriomem_rng_private_data *priv; 93 100 struct resource *res; 94 - int ret; 101 + int err = 0; 102 + int period; 103 + 104 + if (!pdata) { 105 + dev_err(&pdev->dev, "timeriomem_rng_data is missing\n"); 106 + return -EINVAL; 107 + } 95 108 96 109 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 97 - 98 110 if (!res) 99 - return -ENOENT; 111 + return -ENXIO; 100 112 101 - timeriomem_rng_data = pdev->dev.platform_data; 102 - 103 - timeriomem_rng_data->address = ioremap(res->start, resource_size(res)); 104 - if (!timeriomem_rng_data->address) 105 - return -EIO; 106 - 107 - if (timeriomem_rng_data->period != 0 108 - && usecs_to_jiffies(timeriomem_rng_data->period) > 0) { 109 - timeriomem_rng_timer.expires = jiffies; 110 - 111 - timeriomem_rng_ops.priv = usecs_to_jiffies( 112 - timeriomem_rng_data->period); 113 + if (res->start % 4 != 0 || resource_size(res) != 4) { 114 + dev_err(&pdev->dev, 115 + "address must be four bytes wide and aligned\n"); 116 + return -EINVAL; 113 117 } 114 - timeriomem_rng_data->present = 1; 115 118 116 - ret = hwrng_register(&timeriomem_rng_ops); 117 - if (ret) 118 - goto failed; 119 + /* Allocate memory for the device structure (and zero it) */ 120 + priv = kzalloc(sizeof(struct timeriomem_rng_private_data), GFP_KERNEL); 121 + if (!priv) { 122 + dev_err(&pdev->dev, "failed to allocate device structure.\n"); 123 + return -ENOMEM; 124 + } 125 + 126 + platform_set_drvdata(pdev, priv); 127 + 128 + period = pdata->period; 129 + 130 + priv->period = usecs_to_jiffies(period); 131 + if (priv->period < 1) { 132 + dev_err(&pdev->dev, "period is less than one jiffy\n"); 133 + err = -EINVAL; 134 + goto out_free; 135 + } 136 + 137 + priv->expires = jiffies; 138 + priv->present = 1; 139 + 140 + init_completion(&priv->completion); 141 + complete(&priv->completion); 142 + 143 + setup_timer(&priv->timer, timeriomem_rng_trigger, (unsigned long)priv); 144 + 145 + priv->timeriomem_rng_ops.name = dev_name(&pdev->dev); 146 + priv->timeriomem_rng_ops.data_present = timeriomem_rng_data_present; 147 + priv->timeriomem_rng_ops.data_read = timeriomem_rng_data_read; 148 + priv->timeriomem_rng_ops.priv = (unsigned long)priv; 149 + 150 + if (!request_mem_region(res->start, resource_size(res), 151 + dev_name(&pdev->dev))) { 152 + dev_err(&pdev->dev, "request_mem_region failed\n"); 153 + err = -EBUSY; 154 + goto out_timer; 155 + } 156 + 157 + priv->io_base = ioremap(res->start, resource_size(res)); 158 + if (priv->io_base == NULL) { 159 + dev_err(&pdev->dev, "ioremap failed\n"); 160 + err = -EIO; 161 + goto out_release_io; 162 + } 163 + 164 + err = hwrng_register(&priv->timeriomem_rng_ops); 165 + if (err) { 166 + dev_err(&pdev->dev, "problem registering\n"); 167 + goto out; 168 + } 119 169 120 170 dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n", 121 - timeriomem_rng_data->address, 122 - timeriomem_rng_data->period); 171 + priv->io_base, period); 123 172 124 173 return 0; 125 174 126 - failed: 127 - dev_err(&pdev->dev, "problem registering\n"); 128 - iounmap(timeriomem_rng_data->address); 129 - 130 - return ret; 175 + out: 176 + iounmap(priv->io_base); 177 + out_release_io: 178 + release_mem_region(res->start, resource_size(res)); 179 + out_timer: 180 + del_timer_sync(&priv->timer); 181 + out_free: 182 + platform_set_drvdata(pdev, NULL); 183 + kfree(priv); 184 + return err; 131 185 } 132 186 133 187 static int timeriomem_rng_remove(struct platform_device *pdev) 134 188 { 135 - del_timer_sync(&timeriomem_rng_timer); 136 - hwrng_unregister(&timeriomem_rng_ops); 189 + struct timeriomem_rng_private_data *priv = platform_get_drvdata(pdev); 190 + struct resource *res; 137 191 138 - iounmap(timeriomem_rng_data->address); 192 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 193 + 194 + hwrng_unregister(&priv->timeriomem_rng_ops); 195 + 196 + del_timer_sync(&priv->timer); 197 + iounmap(priv->io_base); 198 + release_mem_region(res->start, resource_size(res)); 199 + platform_set_drvdata(pdev, NULL); 200 + kfree(priv); 139 201 140 202 return 0; 141 203 }
-5
include/linux/timeriomem-rng.h
··· 8 8 * published by the Free Software Foundation. 9 9 */ 10 10 11 - #include <linux/completion.h> 12 - 13 11 struct timeriomem_rng_data { 14 - struct completion completion; 15 - unsigned int present:1; 16 - 17 12 void __iomem *address; 18 13 19 14 /* measures in usecs */