diff mbox

rtc: r9701: fix crash in r9701_remove()

Message ID 1330447522-7076-1-git-send-email-agust@denx.de
State Accepted
Headers show

Commit Message

Anatolij Gustschin Feb. 28, 2012, 4:45 p.m. UTC
If probing the RTC didn't succeed due to failed RTC register
access, the RTC device will be unregistered. Then, when
removing the module r9701_remove() causes kernel crash
while trying to unregister not registered RTC device.
Fix this by doing RTC register access test before RTC
device registration.

Signed-off-by: Anatolij Gustschin <agust@denx.de>
Cc: Alessandro Zummo <a.zummo@towertech.it>
---
 drivers/rtc/rtc-r9701.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
diff mbox

Patch

diff --git a/drivers/rtc/rtc-r9701.c b/drivers/rtc/rtc-r9701.c
index 931f95c..7f8e6c2 100644
--- a/drivers/rtc/rtc-r9701.c
+++ b/drivers/rtc/rtc-r9701.c
@@ -125,6 +125,13 @@  static int __devinit r9701_probe(struct spi_device *spi)
 	unsigned char tmp;
 	int res;
 
+	tmp = R100CNT;
+	res = read_regs(&spi->dev, &tmp, 1);
+	if (res || tmp != 0x20) {
+		dev_err(&spi->dev, "cannot read RTC register\n");
+		return -ENODEV;
+	}
+
 	rtc = rtc_device_register("r9701",
 				&spi->dev, &r9701_rtc_ops, THIS_MODULE);
 	if (IS_ERR(rtc))
@@ -132,13 +139,6 @@  static int __devinit r9701_probe(struct spi_device *spi)
 
 	dev_set_drvdata(&spi->dev, rtc);
 
-	tmp = R100CNT;
-	res = read_regs(&spi->dev, &tmp, 1);
-	if (res || tmp != 0x20) {
-		rtc_device_unregister(rtc);
-		return res;
-	}
-
 	return 0;
 }