Module Name:    src
Committed By:   thorpej
Date:           Thu Jan  2 17:40:27 UTC 2020

Modified Files:
        src/sys/dev/i2c: x1226.c

Log Message:
- No need to use I2C_F_POLL here.
- Correctly propagate errors up the stack.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/dev/i2c/x1226.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/i2c/x1226.c
diff -u src/sys/dev/i2c/x1226.c:1.21 src/sys/dev/i2c/x1226.c:1.22
--- src/sys/dev/i2c/x1226.c:1.21	Sat Jun 16 21:22:13 2018
+++ src/sys/dev/i2c/x1226.c	Thu Jan  2 17:40:27 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: x1226.c,v 1.21 2018/06/16 21:22:13 thorpej Exp $	*/
+/*	$NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $	*/
 
 /*
  * Copyright (c) 2003 Shigeyuki Fukushima.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.21 2018/06/16 21:22:13 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x1226.c,v 1.22 2020/01/02 17:40:27 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -252,14 +252,17 @@ xrtc_gettime(struct todr_chip_handle *ch
 	struct xrtc_softc *sc = ch->cookie;
 	struct clock_ymdhms dt, check;
 	int retries;
+	int error;
 
 	memset(&dt, 0, sizeof(dt));
 	memset(&check, 0, sizeof(check));
 
 	retries = 5;
 	do {
-		xrtc_clock_read(sc, &dt);
-		xrtc_clock_read(sc, &check);
+		if ((error = xrtc_clock_read(sc, &dt)) == 0)
+			error = xrtc_clock_read(sc, &check);
+		if (error)
+			return error;
 	} while (memcmp(&dt, &check, sizeof(check)) != 0 && --retries);
 
 	tv->tv_sec = clock_ymdhms_to_secs(&dt);
@@ -276,10 +279,7 @@ xrtc_settime(struct todr_chip_handle *ch
 
 	clock_secs_to_ymdhms(tv->tv_sec, &dt);
 
-	if (xrtc_clock_write(sc, &dt) == 0)
-		return (-1);
-
-	return (0);
+	return xrtc_clock_write(sc, &dt);
 }
 
 static int
@@ -287,11 +287,12 @@ xrtc_clock_read(struct xrtc_softc *sc, s
 {
 	int i = 0;
 	u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[2];
+	int error;
 
-	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
+	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
 		aprint_error_dev(sc->sc_dev,
 		    "xrtc_clock_read: failed to acquire I2C bus\n");
-		return (0);
+		return (error);
 	}
 
 	/* Read each RTC register in order */
@@ -300,20 +301,20 @@ xrtc_clock_read(struct xrtc_softc *sc, s
 		cmdbuf[0] = (addr >> 8) & 0xff;
 		cmdbuf[1] = addr & 0xff;
 
-		if (iic_exec(sc->sc_tag,
+		if ((error = iic_exec(sc->sc_tag,
 			I2C_OP_READ_WITH_STOP,
 			sc->sc_address, cmdbuf, 2,
-			&bcd[i], 1, I2C_F_POLL)) {
-			iic_release_bus(sc->sc_tag, I2C_F_POLL);
+			&bcd[i], 1, 0)) != 0) {
+			iic_release_bus(sc->sc_tag, 0);
 			aprint_error_dev(sc->sc_dev,
 			    "xrtc_clock_read: failed to read rtc "
 				"at 0x%x\n", i);
-			return (0);
+			return (error);
 		}
 	}
 
 	/* Done with I2C */
-	iic_release_bus(sc->sc_tag, I2C_F_POLL);
+	iic_release_bus(sc->sc_tag, 0);
 
 	/*
 	 * Convert the X1226's register bcd values
@@ -343,7 +344,7 @@ xrtc_clock_read(struct xrtc_softc *sc, s
 	dt->dt_year += bcdtobin(bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE]
 			& X1226_REG_Y2K_MASK) * 100;
 
-	return (1);
+	return (0);
 }
 
 static int
@@ -351,6 +352,7 @@ xrtc_clock_write(struct xrtc_softc *sc, 
 {
 	int i = 0, addr;
 	u_int8_t bcd[X1226_REG_RTC_SIZE], cmdbuf[3];
+	int error, error2;
 
 	/*
 	 * Convert our time to bcd values
@@ -365,10 +367,10 @@ xrtc_clock_write(struct xrtc_softc *sc, 
 	bcd[X1226_REG_YR - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year % 100);
 	bcd[X1226_REG_Y2K - X1226_REG_RTC_BASE] = bintobcd(dt->dt_year / 100);
 
-	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
+	if ((error = iic_acquire_bus(sc->sc_tag, 0)) != 0) {
 		aprint_error_dev(sc->sc_dev,
 		    "xrtc_clock_write: failed to acquire I2C bus\n");
-		return (0);
+		return (error);
 	}
 
 	/* Unlock register: Write Enable Latch */
@@ -376,13 +378,13 @@ xrtc_clock_write(struct xrtc_softc *sc, 
 	cmdbuf[0] = ((addr >> 8) & 0xff);
 	cmdbuf[1] = (addr & 0xff);
 	cmdbuf[2] = X1226_FLAG_SR_WEL;
-	if (iic_exec(sc->sc_tag,
+	if ((error = iic_exec(sc->sc_tag,
 		I2C_OP_WRITE_WITH_STOP,
-		sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
-		iic_release_bus(sc->sc_tag, I2C_F_POLL);
+		sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
+		iic_release_bus(sc->sc_tag, 0);
 		aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
 			"failed to write-unlock status register(WEL=1)\n");
-		return (0);
+		return (error);
 	}
 
 	/* Unlock register: Register Write Enable Latch */
@@ -390,13 +392,13 @@ xrtc_clock_write(struct xrtc_softc *sc, 
 	cmdbuf[0] = ((addr >> 8) & 0xff);
 	cmdbuf[1] = (addr & 0xff);
 	cmdbuf[2] = X1226_FLAG_SR_WEL | X1226_FLAG_SR_RWEL;
-	if (iic_exec(sc->sc_tag,
+	if ((error = iic_exec(sc->sc_tag,
 		I2C_OP_WRITE_WITH_STOP,
-		sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
-		iic_release_bus(sc->sc_tag, I2C_F_POLL);
+		sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
+		iic_release_bus(sc->sc_tag, 0);
 		aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
 			"failed to write-unlock status register(RWEL=1)\n");
-		return (0);
+		return (error);
 	}
 
 	/* Write each RTC register in reverse order */
@@ -404,42 +406,34 @@ xrtc_clock_write(struct xrtc_softc *sc, 
 		addr = i + X1226_REG_RTC_BASE;
 		cmdbuf[0] = ((addr >> 8) & 0xff);
 		cmdbuf[1] = (addr & 0xff);
-		if (iic_exec(sc->sc_tag,
+		if ((error = iic_exec(sc->sc_tag,
 			I2C_OP_WRITE_WITH_STOP,
 			sc->sc_address, cmdbuf, 2,
-			&bcd[i], 1, I2C_F_POLL)) {
+			&bcd[i], 1, 0)) != 0) {
 
-			/* Lock register: WEL/RWEL off */
-			addr = X1226_REG_SR;
-			cmdbuf[0] = ((addr >> 8) & 0xff);
-			cmdbuf[1] = (addr & 0xff);
-			cmdbuf[2] = 0;
-			iic_exec(sc->sc_tag,
-				I2C_OP_WRITE_WITH_STOP,
-				sc->sc_address, cmdbuf, 2,
-				&cmdbuf[2], 1, 0);
+			aprint_error_dev(sc->sc_dev,
+			    "xrtc_clock_write: failed to write rtc at 0x%x\n",
+			    i);
 
-			iic_release_bus(sc->sc_tag, I2C_F_POLL);
-			aprint_error_dev(sc->sc_dev, "xrtc_clock_write: failed to write rtc "
-				"at 0x%x\n", i);
-			return (0);
+			goto write_lock_rtc;
 		}
 	}
 
+ write_lock_rtc:
 	/* Lock register: WEL/RWEL off */
 	addr = X1226_REG_SR;
 	cmdbuf[0] = ((addr >> 8) & 0xff);
 	cmdbuf[1] = (addr & 0xff);
 	cmdbuf[2] = 0;
-	if (iic_exec(sc->sc_tag,
-		I2C_OP_WRITE_WITH_STOP,
-		sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0) != 0) {
-		iic_release_bus(sc->sc_tag, I2C_F_POLL);
+	if ((error2 = iic_exec(sc->sc_tag,
+			I2C_OP_WRITE_WITH_STOP,
+			sc->sc_address, cmdbuf, 2, &cmdbuf[2], 1, 0)) != 0) {
+		iic_release_bus(sc->sc_tag, 0);
 		aprint_error_dev(sc->sc_dev, "xrtc_clock_write: "
 			"failed to write-lock status register\n");
-		return (0);
+		return (error ? error : error2);
 	}
 
-	iic_release_bus(sc->sc_tag, I2C_F_POLL);
-	return (1);
+	iic_release_bus(sc->sc_tag, 0);
+	return (error);
 }

Reply via email to