Skip to contentSkip to conversation

lib: crc8: add new library module providing crc8 algorithm

RFC Superseded Arend van Spriel 2 participants 5 messages May 24, 2011 – May 25, 2011

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC 1/1 lib: crc8: add new library module providing crc8 algorithm

The brcm80211 driver in staging tree uses a crc8 function. Based on feedback from John Linville to move this to lib directory, the linux source has been searched. Although there is currently only one other kernel driver using this algorithm (ie. drivers/ssb) we are providing this as a library function for others to use.

V1:
- functionality taken from brcm80211 utility function as is.
- documented the crc8 header file using kernel-doc syntax.
- compilation verified in-kernel and as module for x86, x86_64, arm, and mips.

V2:
- ran checkpatch after sending V1 (sorry for that mortal sin) so fixed that.

V3:
- added function crc8_create().
- crc8_create() takes polynomial and bit direction as parameters.

---
 include/linux/crc8.h |   92 +++++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig          |    7 +++
 lib/Makefile         |    1 +
 lib/crc8.c           |  105 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 205 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/crc8.h
 create mode 100644 lib/crc8.c
include/linux/crc8.h new file +92 -0
new file mode 100644
index 0000000..2525ca0
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2011 Broadcom Corporation
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#ifndef __CRC8_H_
+#define __CRC8_H_
+
+#include <linux/types.h>
+
+/* see usage of this value in crc8() description */
+#define CRC8_INIT_VALUE		0xFF
+
+/* this return value of crc8() upon validating data indicates success */
+#define CRC8_GOOD_VALUE(_table)	(_table[0xFF])
+
+/* required table size for crc8 algorithm */
+#define CRC8_TABLE_SIZE			256
+
+/* helper macro assuring right table size is used */
+#define DECLARE_CRC8_TABLE(_table) \
+	static u8 _table[CRC8_TABLE_SIZE]
+
+/**
+ * enum - determine bit direction of the crc algorithm.
+ *
+ * @crc8_dir_msb_first: direction is in reverse order.
+ * @crc8_dir_lsb_first: direction is in regular order.
+ */
+enum crc8_direction {
+	crc8_dir_msb_first,
+	crc8_dir_lsb_first
+};
+
+/**
+ * crc8_create - fill crc table for given polynomial and bit direction.
+ *
+ * @table:	table to be filled.
+ * @polynomial:	polynomial for which table is to be filled.
+ * @direction:	bit order for which the table is to be filled.
+ *
+ * This function fills the provided table according the polynomial and
+ * the direction provided. Polynomials in CRC algorithms are typically
+ * represented as shown below.
+ *
+ *	poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1
+ *
+ * For lsb_first direction x^7 maps to the lsb and for msb_first
+ * direction x^7 maps to the msb. So the polynomials are as below.
+ *
+ * - lsb first: poly = 10101011(1) = 0xAB
+ *
+ * - msb first: poly = (1)11010101 = 0xD5
+ *
+ * Reference:
+ * "A Painless Guide to CRC Error Detection Algorithms", ver 3, Aug 1993
+ * Williams, Ross N., ross<at>ross.net
+ * (see URL http://www.ross.net/crc/download/crc_v3.txt).
+ */
+void crc8_create(u8 *table, u8 polynomial, enum crc8_direction direction);
+
+/**
+ * crc8() - calculate a crc8 over the given input data.
+ *
+ * @table:	crc table used for calculation.
+ * @pdata:	pointer to data buffer.
+ * @nbytes:	number of bytes in data buffer.
+ * @crc:	previous returned crc8 value.
+ *
+ * The CRC8 is calculated using the polynomial given in crc8_create().
+ *
+ * The caller provides the initial value (either %CRC8_INIT_VALUE
+ * or the previous returned value) to allow for processing of
+ * discontiguous blocks of data.  When generating the CRC the
+ * caller is responsible for complementing the final return value
+ * and inserting it into the byte stream.  When validating a byte
+ * stream (including CRC8), a final return value of %CRC8_GOOD_VALUE
+ * indicates the byte stream data can be considered valid.
+ */
+u8 crc8(u8 *table, u8 *pdata, uint nbytes, u8 crc);
+
+#endif /* __CRC8_H_ */
lib/Kconfig +7 -0
index 9c10e38..ff9e5a3 100644
@@ -89,6 +89,13 @@ config LIBCRC32C
 	  require M here.  See Castagnoli93.
 	  Module will be libcrc32c.
 
+config CRC8
+	tristate "CRC8 function"
+	help
+	  This option provides CRC8 function. Drivers may select this
+	  when they need to do cyclic redundancy check according CRC8
+	  algorithm. Module will be called crc8.
+
 config AUDIT_GENERIC
 	bool
 	depends on AUDIT && !AUDIT_ARCH
lib/Makefile +1 -0
index 4b49a24..704959d 100644
@@ -65,6 +65,7 @@ obj-$(CONFIG_CRC_ITU_T)	+= crc-itu-t.o
 obj-$(CONFIG_CRC32)	+= crc32.o
 obj-$(CONFIG_CRC7)	+= crc7.o
 obj-$(CONFIG_LIBCRC32C)	+= libcrc32c.o
+obj-$(CONFIG_CRC8)	+= crc8.o
 obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o
 
 obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate/
lib/crc8.c new file +105 -0

105 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#1 lore raw
Quoted 3 lines

V3:
- added function crc8_create().
- crc8_create() takes polynomial and bit direction as parameters.

May I suggest that crc8_create is A Stupid Idea. Since the bit order (and polynomial) will always be compile-time constants, just let the call sites call crc8_create_[lm]sb_first() directly.

That way there's no need for the enum, and specifying something other than lsb_first and msb_first is a compile-time error rather than a run-time one.

It might be nice to add some const declarations, and use size_t for the buffer length:
u8 crc8(u8 const *table, u8 const *pdata, size_t nbytes, u8 crc) or
u8 crc8(u8 const table[256], u8 const *pdata, size_t nbytes, u8 crc)

Style points you might consider, but I do not consider essential:

Personally, when a function parameter is a pointer to a fixed-size array, I prefer to declare it as

	void crc8_create_lsb_first(u8 table[256], u8 poly)

for better documentation, but that's your choice.

The CRC8_GOOD_VALUE could be explained if you like. "If a CRC is inverted before transmission, the CRC computed over the while (message+crc) is _table[x], when x is the bit pattern of the modification (almost always 0xff)."

Thanks!

#2 lore raw
Quoted 6 lines · George Spelvin
Quoted 3 lines

V3:
- added function crc8_create().
- crc8_create() takes polynomial and bit direction as parameters.

May I suggest that crc8_create is A Stupid Idea. Since the bit order (and polynomial) will always be compile-time constants, just let the call sites call crc8_create_[lm]sb_first() directly.

Thanks for be subtle in your suggestion. The crc8 module can have multiple callers, which have their own bit order and polynomial. From caller perspective they are likely compile-time constants. You are right that the extra layer crc8_create is not adding much and callers should the bit order specific create functions directly.

Quoted 3 lines

That way there's no need for the enum, and specifying something other than lsb_first and msb_first is a compile-time error rather than a run-time one

If callers use the specific function they can not specify a bit order so no compile-time error.

Quoted 11 lines

It might be nice to add some const declarations, and use size_t for the buffer length:
u8 crc8(u8 const *table, u8 const *pdata, size_t nbytes, u8 crc) or
u8 crc8(u8 const table[256], u8 const *pdata, size_t nbytes, u8 crc)

Style points you might consider, but I do not consider essential:

Personally, when a function parameter is a pointer to a fixed-size array, I prefer to declare it as

Quoted 7 lines
	void crc8_create_lsb_first(u8 table[256], u8 poly)

for better documentation, but that's your choice.

The CRC8_GOOD_VALUE could be explained if you like. "If a CRC is inverted before transmission, the CRC computed over the while (message+crc) is _table[x], when x is the bit pattern of the modification (almost always 0xff)."

Great remarks. Will update the code.

Quoted 1 line

Thanks!

Thanks.

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --
#3 lore raw
Quoted 6 lines · George Spelvin
Quoted 3 lines

May I suggest that crc8_create is A Stupid Idea. Since the bit order (and polynomial) will always be compile-time constants, just let the call sites call crc8_create_[lm]sb_first() directly.

Thanks for be subtle in your suggestion.

Since you are obviously being ironic in your phrasing, I hope I did not give serious offense. What I wrote is a fairly accurate description of my reflex reaction, but I could have phrased it more diplomatically.

Quoted 2 lines

If callers use the specific function they can not specify a bit order so no compile-time error.

What I meant was, the bit order is specified by the function name. An invalid bit order translates to an invalid function name, which the linker will complain about.

If you feel ambitious, you can fold the crc7 code into yours. Its an msbit-first CRC. The resultant table will be left-justified rather than the current right-justified, but if you look at all the call sites, you'll notice that they all shift the result left 1 bit!

I have a pending bug report about some of the calling code which I noticed when trying to update it myself, but it doesn't actally prevent updating the crc7() call sites:
http://marc.info/?l=linux-wireless&m=130616050311698

#4 lore raw
Quoted 3 lines · George Spelvin

Since you are obviously being ironic in your phrasing, I hope I did not give serious offense. What I wrote is a fairly accurate description of my reflex reaction, but I could have phrased it more diplomatically.

No offense. I am not keen on politics anyway so blunt in fine with me ;-)

Quoted 5 lines
Quoted 2 lines

If callers use the specific function they can not specify a bit order so no compile-time error.

What I meant was, the bit order is specified by the function name. An invalid bit order translates to an invalid function name, which the linker will complain about.

Than we mean the same thing.

Quoted 4 lines

If you feel ambitious, you can fold the crc7 code into yours. Its an msbit-first CRC. The resultant table will be left-justified rather than the current right-justified, but if you look at all the call sites, you'll notice that they all shift the result left 1 bit!

We are ambitious, but the focus is on getting our wireless driver in shape. The crc8 library function is related to that. So folding the crc7 will get a low priority for now.

I noticed the whole table changed in the patch you posted in the bug report. So that is because of the shift, right?

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --