Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2011 Broadcom Corporation
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
0011  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
0013  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
0014  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 #ifndef __CRC8_H_
0017 #define __CRC8_H_
0018 
0019 #include <linux/types.h>
0020 
0021 /* see usage of this value in crc8() description */
0022 #define CRC8_INIT_VALUE     0xFF
0023 
0024 /*
0025  * Return value of crc8() indicating valid message+crc. This is true
0026  * if a CRC is inverted before transmission. The CRC computed over the
0027  * whole received bitstream is _table[x], where x is the bit pattern
0028  * of the modification (almost always 0xff).
0029  */
0030 #define CRC8_GOOD_VALUE(_table) (_table[0xFF])
0031 
0032 /* required table size for crc8 algorithm */
0033 #define CRC8_TABLE_SIZE         256
0034 
0035 /* helper macro assuring right table size is used */
0036 #define DECLARE_CRC8_TABLE(_table) \
0037     static u8 _table[CRC8_TABLE_SIZE]
0038 
0039 /**
0040  * crc8_populate_lsb - fill crc table for given polynomial in regular bit order.
0041  *
0042  * @table:  table to be filled.
0043  * @polynomial: polynomial for which table is to be filled.
0044  *
0045  * This function fills the provided table according the polynomial provided for
0046  * regular bit order (lsb first). Polynomials in CRC algorithms are typically
0047  * represented as shown below.
0048  *
0049  *  poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1
0050  *
0051  * For lsb first direction x^7 maps to the lsb. So the polynomial is as below.
0052  *
0053  * - lsb first: poly = 10101011(1) = 0xAB
0054  */
0055 void crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial);
0056 
0057 /**
0058  * crc8_populate_msb - fill crc table for given polynomial in reverse bit order.
0059  *
0060  * @table:  table to be filled.
0061  * @polynomial: polynomial for which table is to be filled.
0062  *
0063  * This function fills the provided table according the polynomial provided for
0064  * reverse bit order (msb first). Polynomials in CRC algorithms are typically
0065  * represented as shown below.
0066  *
0067  *  poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1
0068  *
0069  * For msb first direction x^7 maps to the msb. So the polynomial is as below.
0070  *
0071  * - msb first: poly = (1)11010101 = 0xD5
0072  */
0073 void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial);
0074 
0075 /**
0076  * crc8() - calculate a crc8 over the given input data.
0077  *
0078  * @table:  crc table used for calculation.
0079  * @pdata:  pointer to data buffer.
0080  * @nbytes: number of bytes in data buffer.
0081  * @crc:    previous returned crc8 value.
0082  *
0083  * The CRC8 is calculated using the polynomial given in crc8_populate_msb()
0084  * or crc8_populate_lsb().
0085  *
0086  * The caller provides the initial value (either %CRC8_INIT_VALUE
0087  * or the previous returned value) to allow for processing of
0088  * discontiguous blocks of data.  When generating the CRC the
0089  * caller is responsible for complementing the final return value
0090  * and inserting it into the byte stream.  When validating a byte
0091  * stream (including CRC8), a final return value of %CRC8_GOOD_VALUE
0092  * indicates the byte stream data can be considered valid.
0093  *
0094  * Reference:
0095  * "A Painless Guide to CRC Error Detection Algorithms", ver 3, Aug 1993
0096  * Williams, Ross N., ross<at>ross.net
0097  * (see URL http://www.ross.net/crc/download/crc_v3.txt).
0098  */
0099 u8 crc8(const u8 table[CRC8_TABLE_SIZE], const u8 *pdata, size_t nbytes, u8 crc);
0100 
0101 #endif /* __CRC8_H_ */