Skip to contentSkip to conversation

lib/crc16: unexport crc16_table and crc16_byte()

PATCH In review Eric Biggers 3 participants 5 messages May 13, 2025

Subsystems (4)
lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

cover lib/crc16: unexport crc16_table and crc16_byte()

This series removes the only caller of crc16_byte() outside lib/crc16.c, then folds crc16_table and crc16_byte() into lib/crc16.c. I'm planning to apply this to the crc tree.

Eric Biggers (2):
w1: ds2406: use crc16() instead of crc16_byte() loop lib/crc16: unexport crc16_table and crc16_byte()

 drivers/w1/slaves/w1_ds2406.c | 12 ++----------
 include/linux/crc16.h         |  9 +--------
 lib/crc16.c                   |  9 ++++-----
 3 files changed, 7 insertions(+), 23 deletions(-)

base-commit: e8d72b766adcde14188e68968f3cd05f4321691d

Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 1/2 w1: ds2406: use crc16() instead of crc16_byte() loop

From: Eric Biggers <[email protected]>

Instead of looping through each byte and calling crc16_byte(), instead just call crc16() on the whole buffer. No functional change.

---
 drivers/w1/slaves/w1_ds2406.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)
drivers/w1/slaves/w1_ds2406.c +2 -10
index 1cae9b243ff8..76026d615111 100644
@@ -27,12 +27,10 @@ static ssize_t w1_f12_read_state(
 	const struct bin_attribute *bin_attr,
 	char *buf, loff_t off, size_t count)
 {
 	u8 w1_buf[6] = {W1_F12_FUNC_READ_STATUS, 7, 0, 0, 0, 0};
 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
-	u16 crc = 0;
-	int i;
 	ssize_t rtnval = 1;
 
 	if (off != 0)
 		return 0;
 	if (!buf)
@@ -45,13 +43,11 @@ static ssize_t w1_f12_read_state(
 		return -EIO;
 	}
 
 	w1_write_block(sl->master, w1_buf, 3);
 	w1_read_block(sl->master, w1_buf+3, 3);
-	for (i = 0; i < 6; i++)
-		crc = crc16_byte(crc, w1_buf[i]);
-	if (crc == 0xb001) /* good read? */
+	if (crc16(0, w1_buf, sizeof(w1_buf)) == 0xb001) /* good read? */
 		*buf = ((w1_buf[3]>>5)&3)|0x30;
 	else
 		rtnval = -EIO;
 
 	mutex_unlock(&sl->master->bus_mutex);
@@ -64,12 +60,10 @@ static ssize_t w1_f12_write_output(
 	const struct bin_attribute *bin_attr,
 	char *buf, loff_t off, size_t count)
 {
 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
 	u8 w1_buf[6] = {W1_F12_FUNC_WRITE_STATUS, 7, 0, 0, 0, 0};
-	u16 crc = 0;
-	int i;
 	ssize_t rtnval = 1;
 
 	if (count != 1 || off != 0)
 		return -EFAULT;
 
@@ -81,13 +75,11 @@ static ssize_t w1_f12_write_output(
 	}
 
 	w1_buf[3] = (((*buf)&3)<<5)|0x1F;
 	w1_write_block(sl->master, w1_buf, 4);
 	w1_read_block(sl->master, w1_buf+4, 2);
-	for (i = 0; i < 6; i++)
-		crc = crc16_byte(crc, w1_buf[i]);
-	if (crc == 0xb001) /* good read? */
+	if (crc16(0, w1_buf, sizeof(w1_buf)) == 0xb001) /* good read? */
 		w1_write_8(sl->master, 0xFF);
 	else
 		rtnval = -EIO;
 
 	mutex_unlock(&sl->master->bus_mutex);
Signature · 1 line
2.49.0
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] w1: ds2406: use crc16() instead of crc16_byte() loop

Quoted 9 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Instead of looping through each byte and calling crc16_byte(), instead just call crc16() on the whole buffer. No functional change.

---
 drivers/w1/slaves/w1_ds2406.c | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

Best regards,
Krzysztof

author maintainer
#3 lore raw

PATCH 2/2 lib/crc16: unexport crc16_table and crc16_byte()

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Now that neither crc16_table nor crc16_byte() is used outside lib/crc16.c, fold them into lib/crc16.c.

---
 include/linux/crc16.h | 9 +--------
 lib/crc16.c           | 9 ++++-----
 2 files changed, 5 insertions(+), 13 deletions(-)
include/linux/crc16.h +1 -8
index 9fa74529b317..b861d969b161 100644
@@ -13,16 +13,9 @@
 #ifndef __CRC16_H
 #define __CRC16_H
 
 #include <linux/types.h>
 
-extern u16 const crc16_table[256];
-
-extern u16 crc16(u16 crc, const u8 *buffer, size_t len);
-
-static inline u16 crc16_byte(u16 crc, const u8 data)
-{
-	return (crc >> 8) ^ crc16_table[(crc ^ data) & 0xff];
-}
+u16 crc16(u16 crc, const u8 *p, size_t len);
 
 #endif /* __CRC16_H */
 
lib/crc16.c +4 -5
index 5c3a803c01e0..9c71eda9bf4b 100644
@@ -6,11 +6,11 @@
 #include <linux/types.h>
 #include <linux/module.h>
 #include <linux/crc16.h>
 
 /** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */
-u16 const crc16_table[256] = {
+static const u16 crc16_table[256] = {
 	0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
 	0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
 	0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
 	0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
 	0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
@@ -40,24 +40,23 @@ u16 const crc16_table[256] = {
 	0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
 	0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
 	0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
 	0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
 };
-EXPORT_SYMBOL(crc16_table);
 
 /**
  * crc16 - compute the CRC-16 for the data buffer
  * @crc:	previous CRC value
- * @buffer:	data pointer
+ * @p:		data pointer
  * @len:	number of bytes in the buffer
  *
  * Returns the updated CRC value.
  */
-u16 crc16(u16 crc, u8 const *buffer, size_t len)
+u16 crc16(u16 crc, const u8 *p, size_t len)
 {
 	while (len--)
-		crc = crc16_byte(crc, *buffer++);
+		crc = (crc >> 8) ^ crc16_table[(crc & 0xff) ^ *p++];
 	return crc;
 }
 EXPORT_SYMBOL(crc16);
 
 MODULE_DESCRIPTION("CRC16 calculations");
Signature · 1 line
2.49.0
reviewer Acked-by on the v1 series
#4 lore raw
In reply to Eric Biggers
Quoted 5 lines · Eric Biggers

This series removes the only caller of crc16_byte() outside lib/crc16.c, then folds crc16_table and crc16_byte() into lib/crc16.c. I'm planning to apply this to the crc tree.

Quoted 9 lines

Eric Biggers (2):
w1: ds2406: use crc16() instead of crc16_byte() loop lib/crc16: unexport crc16_table and crc16_byte()

 drivers/w1/slaves/w1_ds2406.c | 12 ++----------
 include/linux/crc16.h         |  9 +--------
 lib/crc16.c                   |  9 ++++-----
 3 files changed, 7 insertions(+), 23 deletions(-)