Commit ac18eb62 authored by Atsushi Nemoto's avatar Atsushi Nemoto Committed by Linus Torvalds

rtc-ds1742: fix races around device registration

* Call dev_set_drvdata before rtc device creation
* Use devres APIs and simplify error/remove path
Signed-off-by: default avatarAtsushi Nemoto <anemo@mba.ocn.ne.jp>
Acked-by: default avatarAlessandro Zummo <a.zummo@towertech.it>
Cc: David Brownell <david-b@pacbell.net>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 618161f7
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/io.h> #include <linux/io.h>
#define DRV_VERSION "0.3" #define DRV_VERSION "0.4"
#define RTC_SIZE 8 #define RTC_SIZE 8
...@@ -55,7 +55,6 @@ struct rtc_plat_data { ...@@ -55,7 +55,6 @@ struct rtc_plat_data {
void __iomem *ioaddr_rtc; void __iomem *ioaddr_rtc;
size_t size_nvram; size_t size_nvram;
size_t size; size_t size;
resource_size_t baseaddr;
unsigned long last_jiffies; unsigned long last_jiffies;
struct bin_attribute nvram_attr; struct bin_attribute nvram_attr;
}; };
...@@ -163,27 +162,24 @@ static int __devinit ds1742_rtc_probe(struct platform_device *pdev) ...@@ -163,27 +162,24 @@ static int __devinit ds1742_rtc_probe(struct platform_device *pdev)
struct rtc_device *rtc; struct rtc_device *rtc;
struct resource *res; struct resource *res;
unsigned int cen, sec; unsigned int cen, sec;
struct rtc_plat_data *pdata = NULL; struct rtc_plat_data *pdata;
void __iomem *ioaddr = NULL; void __iomem *ioaddr;
int ret = 0; int ret = 0;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) if (!res)
return -ENODEV; return -ENODEV;
pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
if (!pdata) if (!pdata)
return -ENOMEM; return -ENOMEM;
pdata->size = res->end - res->start + 1; pdata->size = res->end - res->start + 1;
if (!request_mem_region(res->start, pdata->size, pdev->name)) { if (!devm_request_mem_region(&pdev->dev, res->start, pdata->size,
ret = -EBUSY; pdev->name))
goto out; return -EBUSY;
} ioaddr = devm_ioremap(&pdev->dev, res->start, pdata->size);
pdata->baseaddr = res->start; if (!ioaddr)
ioaddr = ioremap(pdata->baseaddr, pdata->size); return -ENOMEM;
if (!ioaddr) {
ret = -ENOMEM;
goto out;
}
pdata->ioaddr_nvram = ioaddr; pdata->ioaddr_nvram = ioaddr;
pdata->size_nvram = pdata->size - RTC_SIZE; pdata->size_nvram = pdata->size - RTC_SIZE;
pdata->ioaddr_rtc = ioaddr + pdata->size_nvram; pdata->ioaddr_rtc = ioaddr + pdata->size_nvram;
...@@ -207,31 +203,19 @@ static int __devinit ds1742_rtc_probe(struct platform_device *pdev) ...@@ -207,31 +203,19 @@ static int __devinit ds1742_rtc_probe(struct platform_device *pdev)
if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG)) if (!(readb(ioaddr + RTC_DAY) & RTC_BATT_FLAG))
dev_warn(&pdev->dev, "voltage-low detected.\n"); dev_warn(&pdev->dev, "voltage-low detected.\n");
pdata->last_jiffies = jiffies;
platform_set_drvdata(pdev, pdata);
rtc = rtc_device_register(pdev->name, &pdev->dev, rtc = rtc_device_register(pdev->name, &pdev->dev,
&ds1742_rtc_ops, THIS_MODULE); &ds1742_rtc_ops, THIS_MODULE);
if (IS_ERR(rtc)) { if (IS_ERR(rtc))
ret = PTR_ERR(rtc); return PTR_ERR(rtc);
goto out;
}
pdata->rtc = rtc; pdata->rtc = rtc;
pdata->last_jiffies = jiffies;
platform_set_drvdata(pdev, pdata);
ret = sysfs_create_bin_file(&pdev->dev.kobj, &pdata->nvram_attr); ret = sysfs_create_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
if (ret) { if (ret) {
dev_err(&pdev->dev, "creating nvram file in sysfs failed\n"); dev_err(&pdev->dev, "creating nvram file in sysfs failed\n");
goto out; rtc_device_unregister(rtc);
} }
return 0;
out:
if (pdata->rtc)
rtc_device_unregister(pdata->rtc);
if (pdata->ioaddr_nvram)
iounmap(pdata->ioaddr_nvram);
if (pdata->baseaddr)
release_mem_region(pdata->baseaddr, pdata->size);
kfree(pdata);
return ret; return ret;
} }
...@@ -241,9 +225,6 @@ static int __devexit ds1742_rtc_remove(struct platform_device *pdev) ...@@ -241,9 +225,6 @@ static int __devexit ds1742_rtc_remove(struct platform_device *pdev)
sysfs_remove_bin_file(&pdev->dev.kobj, &pdata->nvram_attr); sysfs_remove_bin_file(&pdev->dev.kobj, &pdata->nvram_attr);
rtc_device_unregister(pdata->rtc); rtc_device_unregister(pdata->rtc);
iounmap(pdata->ioaddr_nvram);
release_mem_region(pdata->baseaddr, pdata->size);
kfree(pdata);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment