aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/iicbus/iiconf.c
diff options
context:
space:
mode:
authorIan Lepore <ian@FreeBSD.org>2019-09-14 19:33:36 +0000
committerIan Lepore <ian@FreeBSD.org>2019-09-14 19:33:36 +0000
commitb897b02c6c27d60a836759d2e918cf0e0bf0a6fd (patch)
tree429e4c0f054b2e3841e77c625b5119ecae2e22c6 /sys/dev/iicbus/iiconf.c
parentfad4b12b9007f59d78a92b35669ffb13bd71b82f (diff)
downloadsrc-b897b02c6c27d60a836759d2e918cf0e0bf0a6fd.tar.gz
src-b897b02c6c27d60a836759d2e918cf0e0bf0a6fd.zip
Create a mechanism for encoding a system errno into the IIC_Exxxxx space.
Errors are communicated between the i2c controller layer and upper layers (iicbus and slave device drivers) using a set of IIC_Exxxxxx constants which effectively define a private number space separate from (and having values that conflict with) the system errno number space. Sometimes it is necessary to report a plain old system error (especially EINTR) from the controller or bus layer and have that value make it back across the syscall interface intact. I initially considered replicating a few "crucial" errno values with similar names and new numbers, e.g., IIC_EINTR, IIC_ERESTART, etc. It seemed like that had the potential to grow over time until many of the errno names were duplicated into the IIC_Exxxxx space. So instead, this defines a mechanism to "encode" an errno into the IIC_Exxxx space by setting the high bit and putting the errno into the lower-order bits; a new errno2iic() function does this. The existing iic2errno() recognizes the encoded values and extracts the original errno out of the encoded value. An interesting wrinkle occurs with the pseudo-error values such as ERESTART -- they aleady have the high bit set, and turning it off would be the wrong thing to do. Instead, iic2errno() recognizes that lots of high bits are on (i.e., it's a negative number near to zero) and just returns that value as-is. Thus, existing drivers continue to work without needing any changes, and there is now a way to return errno values from the lower layers. The first use of that is in iicbus_poll() which does mtx_sleep() with the PCATCH flag, and needs to return the errno from that up the call chain. Differential Revision: https://reviews.freebsd.org/D20975
Notes
Notes: svn path=/head/; revision=352338
Diffstat (limited to 'sys/dev/iicbus/iiconf.c')
-rw-r--r--sys/dev/iicbus/iiconf.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/sys/dev/iicbus/iiconf.c b/sys/dev/iicbus/iiconf.c
index 264e648448c3..50edd06a77af 100644
--- a/sys/dev/iicbus/iiconf.c
+++ b/sys/dev/iicbus/iiconf.c
@@ -42,6 +42,18 @@ __FBSDID("$FreeBSD$");
#include "iicbus_if.h"
/*
+ * Encode a system errno value into the IIC_Exxxxx space by setting the
+ * IIC_ERRNO marker bit, so that iic2errno() can turn it back into a plain
+ * system errno value later. This lets controller- and bus-layer code get
+ * important system errno values (such as EINTR/ERESTART) back to the caller.
+ */
+int
+errno2iic(int errno)
+{
+ return ((errno == 0) ? 0 : errno | IIC_ERRNO);
+}
+
+/*
* Translate IIC_Exxxxx status values to vaguely-equivelent errno values.
*/
int
@@ -59,7 +71,22 @@ iic2errno(int iic_status)
case IIC_ENOTSUPP: return (EOPNOTSUPP);
case IIC_ENOADDR: return (EADDRNOTAVAIL);
case IIC_ERESOURCE: return (ENOMEM);
- default: return (EIO);
+ default:
+ /*
+ * If the high bit is set, that means it's a system errno value
+ * that was encoded into the IIC_Exxxxxx space by setting the
+ * IIC_ERRNO marker bit. If lots of high-order bits are set,
+ * then it's one of the negative pseudo-errors such as ERESTART
+ * and we return it as-is. Otherwise it's a plain "small
+ * positive integer" errno, so just remove the IIC_ERRNO marker
+ * bit. If it's some unknown number without the high bit set,
+ * there isn't much we can do except call it an I/O error.
+ */
+ if ((iic_status & IIC_ERRNO) == 0)
+ return (EIO);
+ if ((iic_status & 0xFFFF0000) != 0)
+ return (iic_status);
+ return (iic_status & ~IIC_ERRNO);
}
}
@@ -97,7 +124,7 @@ iicbus_poll(struct iicbus_softc *sc, int how)
return (IIC_EBUSBSY);
}
- return (error);
+ return (errno2iic(error));
}
/*