Skip to contentSkip to conversation

lib/string_helpers: Add str_read_write() helper

PATCH Superseded Andy Shevchenko 3 participants 5 messages Jul 2, 2022 – Jul 4, 2022

Subsystems (8)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 lib/string_helpers: Add str_read_write() helper

Add str_read_write() helper to retun 'read' or 'write' string literal.

---
 include/linux/string_helpers.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/string_helpers.h +5 -0
index 4d72258d42fd..9e22cd78f3b8 100644
@@ -126,4 +126,9 @@ static inline const char *str_enabled_disabled(bool v)
 	return v ? "enabled" : "disabled";
 }
 
+static inline const char *str_read_write(bool v)
+{
+	return v ? "read" : "write";
+}
+
 #endif
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 i2c: Introduce i2c_str_read_write() and make use of it

str_read_write() returns a string literal "read" or "write" based on the value. It also allows to unify usage of a such in the kernel.

For i2c case introduce a wrapper that takes struct i2c_msg as parameter.

---
 drivers/i2c/algos/i2c-algo-pca.c     | 3 +--
 drivers/i2c/algos/i2c-algo-pcf.c     | 3 +--
 drivers/i2c/busses/i2c-at91-master.c | 3 +--
 drivers/i2c/busses/i2c-exynos5.c     | 3 +--
 drivers/i2c/busses/i2c-hix5hd2.c     | 3 +--
 drivers/i2c/busses/i2c-img-scb.c     | 5 ++---
 drivers/i2c/busses/i2c-sh_mobile.c   | 3 +--
 drivers/i2c/busses/i2c-tiny-usb.c    | 6 ++----
 drivers/i2c/busses/i2c-viperboard.c  | 6 ++----
 drivers/i2c/i2c-core-base.c          | 3 +--
 include/linux/i2c.h                  | 7 +++++++
 11 files changed, 20 insertions(+), 25 deletions(-)
drivers/i2c/algos/i2c-algo-pca.c +1 -2
index 384af88e58ad..e5ac3eee7a99 100644
@@ -119,8 +119,7 @@ static int pca_address(struct i2c_algo_pca_data *adap,
 	int sta = pca_get_con(adap);
 	int addr = i2c_8bit_addr_from_msg(msg);
 
-	DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
-	     msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
+	DEB2("=== SLAVE ADDRESS %#04x+%s=%#04x\n", msg->addr, i2c_str_read_write(msg), addr);
 
 	pca_outw(adap, I2C_PCA_DAT, addr);
 
drivers/i2c/algos/i2c-algo-pcf.c +1 -2
index 7a01f2687b4c..232224bbd670 100644
@@ -316,8 +316,7 @@ static int pcf_xfer(struct i2c_adapter *i2c_adap,
 		pmsg = &msgs[i];
 
 		DEB2(printk(KERN_DEBUG "i2c-algo-pcf.o: Doing %s %d bytes to 0x%02x - %d of %d messages\n",
-		     pmsg->flags & I2C_M_RD ? "read" : "write",
-		     pmsg->len, pmsg->addr, i + 1, num);)
+		     i2c_str_read_write(pmsg), pmsg->len, pmsg->addr, i + 1, num);)
 
 		ret = pcf_doAddress(adap, pmsg);
 
drivers/i2c/busses/i2c-at91-master.c +1 -2
index c0c35785a0dc..3761a6cb320f 100644
@@ -523,8 +523,7 @@ static int at91_do_twi_transfer(struct at91_twi_dev *dev)
 	 * writing the corresponding bit into the Control Register.
 	 */
 
-	dev_dbg(dev->dev, "transfer: %s %zu bytes.\n",
-		(dev->msg->flags & I2C_M_RD) ? "read" : "write", dev->buf_len);
+	dev_dbg(dev->dev, "transfer: %s %zu bytes.\n", i2c_str_read_write(dev->msg), dev->buf_len);
 
 	reinit_completion(&dev->cmd_complete);
 	dev->transfer_status = 0;
drivers/i2c/busses/i2c-exynos5.c +1 -2
index b812d1090c0f..cbac64042760 100644
@@ -744,8 +744,7 @@ static int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
 	if (ret < 0) {
 		exynos5_i2c_reset(i2c);
 		if (ret == -ETIMEDOUT)
-			dev_warn(i2c->dev, "%s timeout\n",
-				 (msgs->flags & I2C_M_RD) ? "rx" : "tx");
+			dev_warn(i2c->dev, "%s timeout\n", i2c_str_read_write(msgs);
 	}
 
 	/* Return the state as in interrupt routine */
drivers/i2c/busses/i2c-hix5hd2.c +1 -2
index 61ae58f57047..fefa5db52138 100644
@@ -332,8 +332,7 @@ static int hix5hd2_i2c_xfer_msg(struct hix5hd2_i2c_priv *priv,
 	if (timeout == 0) {
 		priv->state = HIX5I2C_STAT_RW_ERR;
 		priv->err = -ETIMEDOUT;
-		dev_warn(priv->dev, "%s timeout=%d\n",
-			 msgs->flags & I2C_M_RD ? "rx" : "tx",
+		dev_warn(priv->dev, "%s timeout=%d\n", i2c_str_read_write(msgs),
 			 priv->adap.timeout);
 	}
 	ret = priv->state;
drivers/i2c/busses/i2c-img-scb.c +2 -3
index 8e987945ed45..772443059f49 100644
@@ -950,9 +950,8 @@ static irqreturn_t img_i2c_isr(int irq, void *dev_id)
 			    INT_FIFO_EMPTY |
 			    INT_FIFO_FULL))) {
 		dev_crit(i2c->adap.dev.parent,
-			 "fatal: clock low timeout occurred %s addr 0x%02x\n",
-			 (i2c->msg.flags & I2C_M_RD) ? "reading" : "writing",
-			 i2c->msg.addr);
+			 "fatal: clock low timeout occurred when %s addr 0x%02x\n",
+			 i2c_str_read_write(&i2c->msg), i2c->msg.addr);
 		hret = ISR_FATAL(EIO);
 		goto out;
 	}
drivers/i2c/busses/i2c-sh_mobile.c +1 -2
index 72f024a0c363..2b569403ce71 100644
@@ -409,8 +409,7 @@ static irqreturn_t sh_mobile_i2c_isr(int irq, void *dev_id)
 	pd->sr |= sr; /* remember state */
 
 	dev_dbg(pd->dev, "i2c_isr 0x%02x 0x%02x %s %d %d!\n", sr, pd->sr,
-	       (pd->msg->flags & I2C_M_RD) ? "read" : "write",
-	       pd->pos, pd->msg->len);
+		i2c_str_read_write(pd->msg), pd->pos, pd->msg->len);
 
 	/* Kick off TxDMA after preface was done */
 	if (pd->dma_direction == DMA_TO_DEVICE && pd->pos == 0)
drivers/i2c/busses/i2c-tiny-usb.c +2 -4
index 7279ca0eaa2d..74a435d7c308 100644
@@ -71,10 +71,8 @@ static int usb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
 
 		pmsg = &msgs[i];
 
-		dev_dbg(&adapter->dev,
-			"  %d: %s (flags %d) %d bytes to 0x%02x\n",
-			i, pmsg->flags & I2C_M_RD ? "read" : "write",
-			pmsg->flags, pmsg->len, pmsg->addr);
+		dev_dbg(&adapter->dev, "  %d: %s (flags %d) %d bytes to 0x%02x\n",
+			i, i2c_str_read_write(pmsg), pmsg->flags, pmsg->len, pmsg->addr);
 
 		/* and directly send the message */
 		if (pmsg->flags & I2C_M_RD) {
drivers/i2c/busses/i2c-viperboard.c +2 -4
index 8b5322c3bce0..dec12e904aca 100644
@@ -278,10 +278,8 @@ static int vprbrd_i2c_xfer(struct i2c_adapter *i2c, struct i2c_msg *msgs,
 	for (i = 0 ; i < num ; i++) {
 		pmsg = &msgs[i];
 
-		dev_dbg(&i2c->dev,
-			"  %d: %s (flags %d) %d bytes to 0x%02x\n",
-			i, pmsg->flags & I2C_M_RD ? "read" : "write",
-			pmsg->flags, pmsg->len, pmsg->addr);
+		dev_dbg(&i2c->dev, "  %d: %s (flags %d) %d bytes to 0x%02x\n",
+			i, i2c_str_read_write(pmsg), pmsg->flags, pmsg->len, pmsg->addr);
 
 		mutex_lock(&vb->lock);
 		/* directly send the message */
drivers/i2c/i2c-core-base.c +1 -2
index 94c0663a39a6..9811bb44a2e0 100644
@@ -2004,8 +2004,7 @@ module_exit(i2c_exit);
 static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
 {
 	dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
-			    err_msg, msg->addr, msg->len,
-			    msg->flags & I2C_M_RD ? "read" : "write");
+			    err_msg, msg->addr, msg->len, i2c_str_read_write(msg));
 	return -EOPNOTSUPP;
 }
 
include/linux/i2c.h +7 -0
index fbda5ada2afc..3cfaad39cc24 100644
@@ -20,7 +20,9 @@
 #include <linux/rtmutex.h>
 #include <linux/irqdomain.h>		/* for Host Notify IRQ */
 #include <linux/of.h>		/* for struct device_node */
+#include <linux/string_helpers.h>	/* for str_read_write() */
 #include <linux/swab.h>		/* for swab16 */
+
 #include <uapi/linux/i2c.h>
 
 extern struct bus_type i2c_bus_type;
@@ -934,6 +936,11 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
 	return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
 }
 
+static inline const char *i2c_str_read_write(const struct i2c_msg *msg)
+{
+	return str_read_write(msg->flags & I2C_M_RD);
+}
+
 u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);
 void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred);
 
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH v1 2/2] i2c: Introduce i2c_str_read_write() and make use of it

I love your patch! Yet something to improve:

Hi Andy,

I love your patch! Yet something to improve:

[auto build test ERROR on wsa/i2c/for-next]
[also build test ERROR on linus/master v5.19-rc4 next-20220701] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/lib-string_helpers-Add-str_read_write-helper/20220702-215944
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next
config: mips-buildonly-randconfig-r002-20220702 (https://download.01.org/0day-ci/archive/20220702/[email protected]/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project bcd153485ebf07fe79e2b843ed5f1cb74997df1b)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mipsel-linux-gnu
# https://github.com/intel-lab-lkp/linux/commit/c9ef15ef6b2b2b51d33d68a8b92beb05771cc8c2
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Andy-Shevchenko/lib-string_helpers-Add-str_read_write-helper/20220702-215944 git checkout c9ef15ef6b2b2b51d33d68a8b92beb05771cc8c2
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash drivers/i2c/busses/

If you fix the issue, kindly add following tag where applicable

All error/warnings (new ones prefixed by >>):

>> drivers/i2c/busses/i2c-exynos5.c:747:4: error: unterminated function-like macro invocation
                           dev_warn(i2c->dev, "%s timeout\n", i2c_str_read_write(msgs);
                           ^
   include/linux/dev_printk.h:145:9: note: macro 'dev_warn' defined here
   #define dev_warn(dev, fmt, ...) \
           ^
>> drivers/i2c/busses/i2c-exynos5.c:960:26: error: expected expression
   MODULE_LICENSE("GPL v2");
                            ^
>> drivers/i2c/busses/i2c-exynos5.c:960:26: warning: misleading indentation; statement is not part of the previous 'if' [-Wmisleading-indentation]
   drivers/i2c/busses/i2c-exynos5.c:746:3: note: previous statement is here
                   if (ret == -ETIMEDOUT)
                   ^
>> drivers/i2c/busses/i2c-exynos5.c:960:26: error: expected '}'
   MODULE_LICENSE("GPL v2");
                            ^
   drivers/i2c/busses/i2c-exynos5.c:744:15: note: to match this '{'
           if (ret < 0) {
                        ^
>> drivers/i2c/busses/i2c-exynos5.c:960:26: error: expected '}'
   MODULE_LICENSE("GPL v2");
                            ^

drivers/i2c/busses/i2c-exynos5.c:718:1: note: to match this '{' {
^
1 warning and 4 errors generated.

vim +747 drivers/i2c/busses/i2c-exynos5.c

715
716 static int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
717 struct i2c_msg *msgs, int stop)
718 {
719 unsigned long timeout;
720 int ret;
721
722 i2c->msg = msgs;
723 i2c->msg_ptr = 0;
724 i2c->trans_done = 0;
725
726 reinit_completion(&i2c->msg_complete);
727
728 exynos5_i2c_message_start(i2c, stop);
729
730 timeout = wait_for_completion_timeout(&i2c->msg_complete,
731 EXYNOS5_I2C_TIMEOUT);
732 if (timeout == 0)
733 ret = -ETIMEDOUT;
734 else
735 ret = i2c->state;
736
737 /*
738 * If this is the last message to be transfered (stop == 1)
739 * Then check if the bus can be brought back to idle.
740 */
741 if (ret == 0 && stop)
742 ret = exynos5_i2c_wait_bus_idle(i2c);
743
744 if (ret < 0) {
745 exynos5_i2c_reset(i2c);
746 if (ret == -ETIMEDOUT)

 > 747				dev_warn(i2c->dev, "%s timeout\n", i2c_str_read_write(msgs);

748 }
749
750 /* Return the state as in interrupt routine */
751 return ret;
752 }
753

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#3 lore raw

Re: [PATCH v1 2/2] i2c: Introduce i2c_str_read_write() and make use of it

In reply to Andy Shevchenko
I love your patch! Yet something to improve:

Hi Andy,

I love your patch! Yet something to improve:

[auto build test ERROR on wsa/i2c/for-next]
[also build test ERROR on linus/master v5.19-rc4 next-20220701] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/lib-string_helpers-Add-str_read_write-helper/20220702-215944
base:   https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next
config: arc-randconfig-r043-20220702 (https://download.01.org/0day-ci/archive/20220703/[email protected]/config)
compiler: arc-elf-gcc (GCC) 11.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/intel-lab-lkp/linux/commit/c9ef15ef6b2b2b51d33d68a8b92beb05771cc8c2
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Andy-Shevchenko/lib-string_helpers-Add-str_read_write-helper/20220702-215944 git checkout c9ef15ef6b2b2b51d33d68a8b92beb05771cc8c2
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash drivers/i2c/busses/

If you fix the issue, kindly add following tag where applicable

All errors (new ones prefixed by >>):

drivers/i2c/busses/i2c-exynos5.c: In function 'exynos5_i2c_xfer_msg':

>> drivers/i2c/busses/i2c-exynos5.c:960:26: error: unterminated argument list invoking macro "dev_warn"
     960 | MODULE_LICENSE("GPL v2");
         |                          ^
>> drivers/i2c/busses/i2c-exynos5.c:747:25: error: 'dev_warn' undeclared (first use in this function); did you mean '_dev_warn'?
     747 |                         dev_warn(i2c->dev, "%s timeout\n", i2c_str_read_write(msgs);
         |                         ^~~~~~~~
         |                         _dev_warn
   drivers/i2c/busses/i2c-exynos5.c:747:25: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/i2c/busses/i2c-exynos5.c:747:33: error: expected ';' at end of input
     747 |                         dev_warn(i2c->dev, "%s timeout\n", i2c_str_read_write(msgs);
         |                                 ^
         |                                 ;
   ......
   drivers/i2c/busses/i2c-exynos5.c:746:17: note: '-Wmisleading-indentation' is disabled from this point onwards, since column-tracking was disabled due to the size of the code/headers
     746 |                 if (ret == -ETIMEDOUT)
         |                 ^~
   drivers/i2c/busses/i2c-exynos5.c:746:17: note: adding '-flarge-source-files' will allow for more column-tracking support, at the expense of compilation time and memory
>> drivers/i2c/busses/i2c-exynos5.c:747:25: error: expected declaration or statement at end of input
     747 |                         dev_warn(i2c->dev, "%s timeout\n", i2c_str_read_write(msgs);
         |                         ^~~~~~~~
>> drivers/i2c/busses/i2c-exynos5.c:747:25: error: expected declaration or statement at end of input
   drivers/i2c/busses/i2c-exynos5.c:747:25: error: no return statement in function returning non-void [-Werror=return-type]
   At top level:
   drivers/i2c/busses/i2c-exynos5.c:716:12: warning: 'exynos5_i2c_xfer_msg' defined but not used [-Wunused-function]
     716 | static int exynos5_i2c_xfer_msg(struct exynos5_i2c *i2c,
         |            ^~~~~~~~~~~~~~~~~~~~
   drivers/i2c/busses/i2c-exynos5.c:445:20: warning: 'exynos5_i2c_irq' defined but not used [-Wunused-function]
     445 | static irqreturn_t exynos5_i2c_irq(int irqno, void *dev_id)
         |                    ^~~~~~~~~~~~~~~
   drivers/i2c/busses/i2c-exynos5.c:240:34: warning: 'exynos5_i2c_match' defined but not used [-Wunused-const-variable=]
     240 | static const struct of_device_id exynos5_i2c_match[] = {
         |                                  ^~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

vim +/dev_warn +960 drivers/i2c/busses/i2c-exynos5.c

8a73cd4cfa1599 Naveen Krishna Ch 2013-10-16  956  
8a73cd4cfa1599 Naveen Krishna Ch 2013-10-16  957  MODULE_DESCRIPTION("Exynos5 HS-I2C Bus driver");
d790eeb3db6aef Jean Delvare      2020-06-11  958  MODULE_AUTHOR("Naveen Krishna Chatradhi <[email protected]>");
d790eeb3db6aef Jean Delvare      2020-06-11  959  MODULE_AUTHOR("Taekgyun Ko <[email protected]>");

8a73cd4cfa1599 Naveen Krishna Ch 2013-10-16 @960 MODULE_LICENSE("GPL v2");

Signature · 2 lines
0-DAY CI Kernel Test Service
https://01.org/lkp
#4 lore raw

RE: [PATCH v1 2/2] i2c: Introduce i2c_str_read_write() and make use of it

In reply to Andy Shevchenko

From: Andy Shevchenko

Quoted 7 lines

Sent: 02 July 2022 14:59

str_read_write() returns a string literal "read" or "write" based on the value. It also allows to unify usage of a such in the kernel.

For i2c case introduce a wrapper that takes struct i2c_msg as parameter.

...

drivers/i2c/algos/i2c-algo-pca.c static int pca_address(struct i2c_algo_pca_data *adap, · line 122

Quoted 4 lines
drivers/i2c/algos/i2c-algo-pca.c +1 -2
-	DEB2("=== SLAVE ADDRESS %#04x+%c=%#04x\n",
-	     msg->addr, msg->flags & I2C_M_RD ? 'R' : 'W', addr);
+	DEB2("=== SLAVE ADDRESS %#04x+%s=%#04x\n", msg->addr, i2c_str_read_write(msg), addr);

You should ask yourself whether this actually makes the code more readable. Imagine someone who is scan-reading the code to see what it does. They're not going to be impressed when they've chased through two searches to find out the code does.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)