xref: /OK3568_Linux_fs/kernel/include/linux/crc8.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2011 Broadcom Corporation
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Permission to use, copy, modify, and/or distribute this software for any
5*4882a593Smuzhiyun  * purpose with or without fee is hereby granted, provided that the above
6*4882a593Smuzhiyun  * copyright notice and this permission notice appear in all copies.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9*4882a593Smuzhiyun  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10*4882a593Smuzhiyun  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11*4882a593Smuzhiyun  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12*4882a593Smuzhiyun  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13*4882a593Smuzhiyun  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14*4882a593Smuzhiyun  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun #ifndef __CRC8_H_
17*4882a593Smuzhiyun #define __CRC8_H_
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/types.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* see usage of this value in crc8() description */
22*4882a593Smuzhiyun #define CRC8_INIT_VALUE		0xFF
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Return value of crc8() indicating valid message+crc. This is true
26*4882a593Smuzhiyun  * if a CRC is inverted before transmission. The CRC computed over the
27*4882a593Smuzhiyun  * whole received bitstream is _table[x], where x is the bit pattern
28*4882a593Smuzhiyun  * of the modification (almost always 0xff).
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun #define CRC8_GOOD_VALUE(_table)	(_table[0xFF])
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /* required table size for crc8 algorithm */
33*4882a593Smuzhiyun #define CRC8_TABLE_SIZE			256
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* helper macro assuring right table size is used */
36*4882a593Smuzhiyun #define DECLARE_CRC8_TABLE(_table) \
37*4882a593Smuzhiyun 	static u8 _table[CRC8_TABLE_SIZE]
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /**
40*4882a593Smuzhiyun  * crc8_populate_lsb - fill crc table for given polynomial in regular bit order.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * @table:	table to be filled.
43*4882a593Smuzhiyun  * @polynomial:	polynomial for which table is to be filled.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * This function fills the provided table according the polynomial provided for
46*4882a593Smuzhiyun  * regular bit order (lsb first). Polynomials in CRC algorithms are typically
47*4882a593Smuzhiyun  * represented as shown below.
48*4882a593Smuzhiyun  *
49*4882a593Smuzhiyun  *	poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  * For lsb first direction x^7 maps to the lsb. So the polynomial is as below.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * - lsb first: poly = 10101011(1) = 0xAB
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun void crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /**
58*4882a593Smuzhiyun  * crc8_populate_msb - fill crc table for given polynomial in reverse bit order.
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * @table:	table to be filled.
61*4882a593Smuzhiyun  * @polynomial:	polynomial for which table is to be filled.
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * This function fills the provided table according the polynomial provided for
64*4882a593Smuzhiyun  * reverse bit order (msb first). Polynomials in CRC algorithms are typically
65*4882a593Smuzhiyun  * represented as shown below.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  *	poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * For msb first direction x^7 maps to the msb. So the polynomial is as below.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * - msb first: poly = (1)11010101 = 0xD5
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun /**
76*4882a593Smuzhiyun  * crc8() - calculate a crc8 over the given input data.
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * @table:	crc table used for calculation.
79*4882a593Smuzhiyun  * @pdata:	pointer to data buffer.
80*4882a593Smuzhiyun  * @nbytes:	number of bytes in data buffer.
81*4882a593Smuzhiyun  * @crc:	previous returned crc8 value.
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * The CRC8 is calculated using the polynomial given in crc8_populate_msb()
84*4882a593Smuzhiyun  * or crc8_populate_lsb().
85*4882a593Smuzhiyun  *
86*4882a593Smuzhiyun  * The caller provides the initial value (either %CRC8_INIT_VALUE
87*4882a593Smuzhiyun  * or the previous returned value) to allow for processing of
88*4882a593Smuzhiyun  * discontiguous blocks of data.  When generating the CRC the
89*4882a593Smuzhiyun  * caller is responsible for complementing the final return value
90*4882a593Smuzhiyun  * and inserting it into the byte stream.  When validating a byte
91*4882a593Smuzhiyun  * stream (including CRC8), a final return value of %CRC8_GOOD_VALUE
92*4882a593Smuzhiyun  * indicates the byte stream data can be considered valid.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * Reference:
95*4882a593Smuzhiyun  * "A Painless Guide to CRC Error Detection Algorithms", ver 3, Aug 1993
96*4882a593Smuzhiyun  * Williams, Ross N., ross<at>ross.net
97*4882a593Smuzhiyun  * (see URL http://www.ross.net/crc/download/crc_v3.txt).
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun u8 crc8(const u8 table[CRC8_TABLE_SIZE], u8 *pdata, size_t nbytes, u8 crc);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun #endif /* __CRC8_H_ */
102