Skip to content

Commit 7657ec1

Browse files
Evgeniy Polyakovgregkh
authored andcommitted
[PATCH] lib/crc16: added crc16 algorithm.
Add the crc16 routines, as used by w1 devices. Signed-off-by: Ben Gardner <bgardner@wabtec.com> Signed-off-by: Evgeniy Polyakov <johnpol@2ka.mipt.ru> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent a3d65f2 commit 7657ec1

4 files changed

Lines changed: 121 additions & 1 deletion

File tree

‎include/linux/crc16.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* crc16.h - CRC-16 routine
3+
*
4+
* Implements the standard CRC-16, as used with 1-wire devices:
5+
* Width 16
6+
* Poly 0x8005 (x^16 + x^15 + x^2 + 1)
7+
* Init 0
8+
*
9+
* For 1-wire devices, the CRC is stored inverted, LSB-first
10+
*
11+
* Example buffer with the CRC attached:
12+
* 31 32 33 34 35 36 37 38 39 C2 44
13+
*
14+
* The CRC over a buffer with the CRC attached is 0xB001.
15+
* So, if (crc16(0, buf, size) == 0xB001) then the buffer is valid.
16+
*
17+
* Refer to "Application Note 937: Book of iButton Standards" for details.
18+
* http://www.maxim-ic.com/appnotes.cfm/appnote_number/937
19+
*
20+
* Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
21+
*
22+
* This source code is licensed under the GNU General Public License,
23+
* Version 2. See the file COPYING for more details.
24+
*/
25+
26+
#ifndef __CRC16_H
27+
#define __CRC16_H
28+
29+
#include <linux/types.h>
30+
31+
#define CRC16_INIT 0
32+
#define CRC16_VALID 0xb001
33+
34+
extern u16 const crc16_table[256];
35+
36+
extern u16 crc16(u16 crc, const u8 *buffer, size_t len);
37+
38+
static inline u16 crc16_byte(u16 crc, const u8 data)
39+
{
40+
return (crc >> 8) ^ crc16_table[(crc ^ data) & 0xff];
41+
}
42+
43+
#endif /* __CRC16_H */
44+

‎lib/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ config CRC_CCITT
1212
the kernel tree does. Such modules that use library CRC-CCITT
1313
functions require M here.
1414

15+
config CRC16
16+
tristate "CRC16 functions"
17+
help
18+
This option is provided for the case where no in-kernel-tree
19+
modules require CRC16 functions, but a module built outside
20+
the kernel tree does. Such modules that use library CRC16
21+
functions require M here.
22+
1523
config CRC32
1624
tristate "CRC32 functions"
1725
default y

‎lib/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
2323
obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o
2424
obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
2525

26-
ifneq ($(CONFIG_HAVE_DEC_LOCK),y)
26+
ifneq ($(CONFIG_HAVE_DEC_LOCK),y)
2727
lib-y += dec_and_lock.o
2828
endif
2929

3030
obj-$(CONFIG_CRC_CCITT) += crc-ccitt.o
31+
obj-$(CONFIG_CRC16) += crc16.o
3132
obj-$(CONFIG_CRC32) += crc32.o
3233
obj-$(CONFIG_LIBCRC32C) += libcrc32c.o
3334
obj-$(CONFIG_GENERIC_IOMAP) += iomap.o

‎lib/crc16.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* crc16.c
3+
*
4+
* This source code is licensed under the GNU General Public License,
5+
* Version 2. See the file COPYING for more details.
6+
*/
7+
8+
#include <linux/types.h>
9+
#include <linux/module.h>
10+
#include <linux/crc16.h>
11+
12+
/** CRC table for the CRC-16. The poly is 0x8005 (x^16 + x^15 + x^2 + 1) */
13+
u16 const crc16_table[256] = {
14+
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
15+
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
16+
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
17+
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
18+
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
19+
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
20+
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
21+
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
22+
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
23+
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
24+
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
25+
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
26+
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
27+
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
28+
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
29+
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
30+
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
31+
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
32+
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
33+
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
34+
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
35+
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
36+
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
37+
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
38+
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
39+
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
40+
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
41+
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
42+
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
43+
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
44+
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
45+
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
46+
};
47+
EXPORT_SYMBOL(crc16_table);
48+
49+
/**
50+
* Compute the CRC-16 for the data buffer
51+
*
52+
* @param crc previous CRC value
53+
* @param buffer data pointer
54+
* @param len number of bytes in the buffer
55+
* @return the updated CRC value
56+
*/
57+
u16 crc16(u16 crc, u8 const *buffer, size_t len)
58+
{
59+
while (len--)
60+
crc = crc16_byte(crc, *buffer++);
61+
return crc;
62+
}
63+
EXPORT_SYMBOL(crc16);
64+
65+
MODULE_DESCRIPTION("CRC16 calculations");
66+
MODULE_LICENSE("GPL");
67+

0 commit comments

Comments
 (0)