Skip to contentSkip to conversation

lib: crc8: add new library module providing crc8 algorithm

RFC In review Arend van Spriel 4 participants 17 messages May 21, 2011 – May 24, 2011

Subsystems (2)
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.

---
 include/linux/crc8.h |   61 +++++++++++++++++++++++++++++++++++++++
 lib/Kconfig          |    7 ++++
 lib/Makefile         |    1 +
 lib/crc8.c           |   77 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 146 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/crc8.h
 create mode 100644 lib/crc8.c
include/linux/crc8.h new file +61 -0
new file mode 100644
index 0000000..4048a34
@@ -0,0 +1,61 @@
+/*
+ * 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>
+
+/**
+ * enum - CRC8 constants
+ *
+ * @CRC8_INIT_VALUE: Initial CRC8 checksum value
+ * @CRC8_GOOD_VALUE: Good final CRC8 checksum value
+ *
+ * Constants for the crc8() function. Refer to its
+ * documentation how to use these values.
+ */
+enum CRC8 {
+	CRC8_INIT_VALUE	= 0xff,
+	CRC8_GOOD_VALUE	= 0x9f
+};
+
+/**
+ * crc8() - calculate a crc8 over the given input data
+ *
+ * @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 below.
+ *
+ * x^8 + x^7 +x^6 + x^4 + x^2 + 1
+ *
+ * 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.
+ *
+ * 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).
+ */
+u8 crc8(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 +77 -0

77 lines not on this page · raw message

Signature · 1 line
1.7.4.1
#1 lore raw

include/linux/crc8.h line 33

Quoted 13 lines · Arend van Spriel
include/linux/crc8.h new file +13 -0
+/**
+ * enum - CRC8 constants
+ *
+ * @CRC8_INIT_VALUE: Initial CRC8 checksum value
+ * @CRC8_GOOD_VALUE: Good final CRC8 checksum value
+ *
+ * Constants for the crc8() function. Refer to its
+ * documentation how to use these values.
+ */
+enum CRC8 {
+	CRC8_INIT_VALUE	= 0xff,
+	CRC8_GOOD_VALUE	= 0x9f
+};

These seem a little out of place, wouldn't that be specific to how the algorithm is used?

johannes

#2 lore raw
Quoted 17 lines · Johannes Berg

include/linux/crc8.h line 33

Quoted 13 lines · Arend van Spriel
include/linux/crc8.h new file +13 -0
+/**
+ * enum - CRC8 constants
+ *
+ * @CRC8_INIT_VALUE: Initial CRC8 checksum value
+ * @CRC8_GOOD_VALUE: Good final CRC8 checksum value
+ *
+ * Constants for the crc8() function. Refer to its
+ * documentation how to use these values.
+ */
+enum CRC8 {
+	CRC8_INIT_VALUE	= 0xff,
+	CRC8_GOOD_VALUE	= 0x9f
+};

These seem a little out of place, wouldn't that be specific to how the algorithm is used?

True, a different init value likely results in a different good value. However, the documentation of the crc8() function provides one possible use which makes use of these values. So it is provided as a convenience. Removal is easy of course, but I tend to keep it for those who are content with the proposed usage (not necessarily Broadcom :-D ).

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --
#3 lore raw
In reply to Arend van Spriel
Quoted 5 lines · On Sat, 21 May 2011 22:49:33 +0200 "Arend van Spriel" <[email protected]>…

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.

Can we find a better name for it - there seem to be lots of CRC8 variants that are different - eg bit direction and it's going to be confusing to just call it 'crc8'

#4 lore raw
Quoted 11 lines · Alan Cox
Quoted 5 lines · On Sat, 21 May 2011 22:49:33 +0200 "Arend van Spriel"<[email protected]>…

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.

Can we find a better name for it - there seem to be lots of CRC8 variants that are different - eg bit direction and it's going to be confusing to just call it 'crc8'

No problem. I just had a look at the lib directory and noticed the crc7 function and module is called crc7. I can imagine there are enough variants of that as well. Another variation is probably the polynomial used. Not sure how to factor in bit direction and polynomial into the name.

suggestions from my thumb:
- rnw_crc8: refers to Ross N. Williams who wrote the paper used for implementing this function.
- painless_crc8: refers to title of the paper "A Painless Guide to CRC Error Detection Algorithms".
- brcm_crc8: indicates where the function came from.

Other suggestions are welcome.

Gr. AvS

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

Is it just used for wireless in this form ?

See the n_gsm and bluetooth code for what I think is the same crc algorithm but in reverse bit order .

#6 lore raw
Quoted 5 lines · Alan Cox

Is it just used for wireless in this form ?

See the n_gsm and bluetooth code for what I think is the same crc algorithm but in reverse bit order .

I only searched for term 'crc8' in the tree so you may be right. I will take a look. It may be worth to support both bit directions in some fashion.

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --
#7 lore raw
In reply to Alan Cox
Quoted 4 lines · Alan Cox

Is it just used for wireless in this form ?

See the n_gsm and bluetooth code for what I think is the same crc algorithm but in reverse bit order .

Hi Alan,

Look into the code in n_gsm and bluetooth code. The algorithm is the same, but except for the bit order it also is using a different polynomial. My current implementation has a fixed table. I could do the table generation runtime with the polynomial and the bit order as parameters. That would make it more general purpose.

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --
#8 lore raw
Quoted 8 lines · Arend van Spriel
Quoted 4 lines · Alan Cox

Is it just used for wireless in this form ?

See the n_gsm and bluetooth code for what I think is the same crc algorithm but in reverse bit order .

Hi Alan,

Look into the code in n_gsm and bluetooth code. The algorithm is the

Meant to say 'I looked into the code...'

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --
#9 lore raw
In reply to Arend van Spriel
Quoted 6 lines · On Tue, 24 May 2011 09:45:47 +0200 "Arend van Spriel" <[email protected]>…
Quoted 4 lines · Alan Cox

Is it just used for wireless in this form ?

See the n_gsm and bluetooth code for what I think is the same crc algorithm but in reverse bit order .

Quoted 7 lines

Hi Alan,

Look into the code in n_gsm and bluetooth code. The algorithm is the same, but except for the bit order it also is using a different polynomial. My current implementation has a fixed table. I could do the table generation runtime with the polynomial and the bit order as parameters. That would make it more general purpose.

Probably not worth it - its so tiny a piece of code anyway (< 500 bytes or so) that it's going to be bigger not smaller if you do that !

#10 lore raw
Quoted 10 lines · Alan Cox
Quoted 7 lines · On Tue, 24 May 2011 09:45:47 +0200 "Arend van Spriel"<[email protected]>…
Quoted 4 lines · Alan Cox

Is it just used for wireless in this form ?

See the n_gsm and bluetooth code for what I think is the same crc algorithm but in reverse bit order .

Hi Alan,

Quoted 7 lines
Quoted 5 lines

Look into the code in n_gsm and bluetooth code. The algorithm is the same, but except for the bit order it also is using a different polynomial. My current implementation has a fixed table. I could do the table generation runtime with the polynomial and the bit order as parameters. That would make it more general purpose.

Probably not worth it - its so tiny a piece of code anyway (< 500 bytes or so) that it's going to be bigger not smaller if you do that !

Hi Alan,

Not sure whether you mean the source code or the object code. I build kernel with debug symbols so the sizes are bit higher.

fixed table:        -rw-r--r-- 1 arend arend 1859 2011-05-24 11:53 crc8.o
custom table:    -rw-r--r-- 1 arend arend 2215 2011-05-24 11:58 crc8.o

The size increase is not that big.

Gr. AvS

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

I'm out of topic, but you can take a look at drivers/ssb/pci.c

We implement crc checksum here as well. The same will be soon needed for bcma (i've already done this locally).

Signature · 1 line
Rafał
#13 lore raw
In reply to Arend van Spriel
Quoted 2 lines

Not sure whether you mean the source code or the object code. I build kernel with debug symbols so the sizes are bit higher.

nm will tell you the actual sizes. I think the n_gsm code is about 500 bytes or so and chunks of it inline.

#14 lore raw
Quoted 4 lines · Alan Cox
Quoted 2 lines

Not sure whether you mean the source code or the object code. I build kernel with debug symbols so the sizes are bit higher.

nm will tell you the actual sizes. I think the n_gsm code is about 500 bytes or so and chunks of it inline.

Not counting the crc table I assume.

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --
#12 lore raw
In reply to Rafał Miłecki
Quoted 5 lines · Rafał Miłecki

I'm out of topic, but you can take a look at drivers/ssb/pci.c

We implement crc checksum here as well. The same will be soon needed for bcma (i've already done this locally).

Hi Rafał,

Couple of days ago I posted a patch for comments. In the commit message I referred to ssb having the same algorithm (identical crc table) as brcm80211. One of the reason for moving it to lib directory in the kernel tree. brcm80211, ssb, and bcma could use it.

Gr. AvS

Signature · 2 lines
Almost nobody dances sober, unless they happen to be insane.
-- H.P. Lovecraft --
#15 lore raw
In reply to Arend van Spriel
Quoted 7 lines · On Tue, 24 May 2011 12:38:45 +0200 "Arend van Spriel" <[email protected]>…
Quoted 4 lines · Alan Cox
Quoted 2 lines

Not sure whether you mean the source code or the object code. I build kernel with debug symbols so the sizes are bit higher.

nm will tell you the actual sizes. I think the n_gsm code is about 500 bytes or so and chunks of it inline.

Not counting the crc table I assume.

I forget but I think including - its a tiny tiny bit of code once inlined.

#16 lore raw
Quoted 10 lines · Alan Cox
Quoted 6 lines · On Tue, 24 May 2011 12:38:45 +0200 "Arend van Spriel"<[email protected]>…
Quoted 4 lines · Alan Cox
Quoted 2 lines

Not sure whether you mean the source code or the object code. I build kernel with debug symbols so the sizes are bit higher.

nm will tell you the actual sizes. I think the n_gsm code is about 500 bytes or so and chunks of it inline.

Not counting the crc table I assume.

I forget but I think including - its a tiny tiny bit of code once inlined.

I used nm on both flavours.

dynamic crc table based on polynomial and bit order
---------------------------------------------------
00000000 00000026 T crc8
00000030 000000dc T crc8_create

fixed crc table with table selection based on bit order
-------------------------------------------------------
00000000 00000026 T crc8
00000030 0000002b T crc8_create
00000100 00000100 r crc8_table_lsb
00000000 00000100 r crc8_table_msb

The first requires the user to provide buffer to hold the table. The difference is in the crc_create() function (maybe better named crc_populate_table() in first, and crc_select_table() in second). It is 177 bytes larger. The crc calculating function is 38 bytes. Still pretty small I would think.

Gr. AvS

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