17150962dSArend van Spriel /* 27150962dSArend van Spriel * Copyright (c) 2011 Broadcom Corporation 37150962dSArend van Spriel * 47150962dSArend van Spriel * Permission to use, copy, modify, and/or distribute this software for any 57150962dSArend van Spriel * purpose with or without fee is hereby granted, provided that the above 67150962dSArend van Spriel * copyright notice and this permission notice appear in all copies. 77150962dSArend van Spriel * 87150962dSArend van Spriel * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 97150962dSArend van Spriel * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 107150962dSArend van Spriel * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 117150962dSArend van Spriel * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 127150962dSArend van Spriel * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 137150962dSArend van Spriel * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 147150962dSArend van Spriel * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 157150962dSArend van Spriel */ 167150962dSArend van Spriel #ifndef __CRC8_H_ 177150962dSArend van Spriel #define __CRC8_H_ 187150962dSArend van Spriel 197150962dSArend van Spriel #include <linux/types.h> 207150962dSArend van Spriel 217150962dSArend van Spriel /* see usage of this value in crc8() description */ 227150962dSArend van Spriel #define CRC8_INIT_VALUE 0xFF 237150962dSArend van Spriel 247150962dSArend van Spriel /* 257150962dSArend van Spriel * Return value of crc8() indicating valid message+crc. This is true 267150962dSArend van Spriel * if a CRC is inverted before transmission. The CRC computed over the 277150962dSArend van Spriel * whole received bitstream is _table[x], where x is the bit pattern 287150962dSArend van Spriel * of the modification (almost always 0xff). 297150962dSArend van Spriel */ 307150962dSArend van Spriel #define CRC8_GOOD_VALUE(_table) (_table[0xFF]) 317150962dSArend van Spriel 327150962dSArend van Spriel /* required table size for crc8 algorithm */ 337150962dSArend van Spriel #define CRC8_TABLE_SIZE 256 347150962dSArend van Spriel 357150962dSArend van Spriel /* helper macro assuring right table size is used */ 367150962dSArend van Spriel #define DECLARE_CRC8_TABLE(_table) \ 377150962dSArend van Spriel static u8 _table[CRC8_TABLE_SIZE] 387150962dSArend van Spriel 397150962dSArend van Spriel /** 407150962dSArend van Spriel * crc8_populate_lsb - fill crc table for given polynomial in regular bit order. 417150962dSArend van Spriel * 427150962dSArend van Spriel * @table: table to be filled. 437150962dSArend van Spriel * @polynomial: polynomial for which table is to be filled. 447150962dSArend van Spriel * 457150962dSArend van Spriel * This function fills the provided table according the polynomial provided for 467150962dSArend van Spriel * regular bit order (lsb first). Polynomials in CRC algorithms are typically 477150962dSArend van Spriel * represented as shown below. 487150962dSArend van Spriel * 497150962dSArend van Spriel * poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1 507150962dSArend van Spriel * 517150962dSArend van Spriel * For lsb first direction x^7 maps to the lsb. So the polynomial is as below. 527150962dSArend van Spriel * 537150962dSArend van Spriel * - lsb first: poly = 10101011(1) = 0xAB 547150962dSArend van Spriel */ 557150962dSArend van Spriel void crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial); 567150962dSArend van Spriel 577150962dSArend van Spriel /** 587150962dSArend van Spriel * crc8_populate_msb - fill crc table for given polynomial in reverse bit order. 597150962dSArend van Spriel * 607150962dSArend van Spriel * @table: table to be filled. 617150962dSArend van Spriel * @polynomial: polynomial for which table is to be filled. 627150962dSArend van Spriel * 637150962dSArend van Spriel * This function fills the provided table according the polynomial provided for 647150962dSArend van Spriel * reverse bit order (msb first). Polynomials in CRC algorithms are typically 657150962dSArend van Spriel * represented as shown below. 667150962dSArend van Spriel * 677150962dSArend van Spriel * poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1 687150962dSArend van Spriel * 697150962dSArend van Spriel * For msb first direction x^7 maps to the msb. So the polynomial is as below. 707150962dSArend van Spriel * 717150962dSArend van Spriel * - msb first: poly = (1)11010101 = 0xD5 727150962dSArend van Spriel */ 737150962dSArend van Spriel void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial); 747150962dSArend van Spriel 757150962dSArend van Spriel /** 767150962dSArend van Spriel * crc8() - calculate a crc8 over the given input data. 777150962dSArend van Spriel * 787150962dSArend van Spriel * @table: crc table used for calculation. 797150962dSArend van Spriel * @pdata: pointer to data buffer. 807150962dSArend van Spriel * @nbytes: number of bytes in data buffer. 817150962dSArend van Spriel * @crc: previous returned crc8 value. 827150962dSArend van Spriel * 837150962dSArend van Spriel * The CRC8 is calculated using the polynomial given in crc8_populate_msb() 847150962dSArend van Spriel * or crc8_populate_lsb(). 857150962dSArend van Spriel * 867150962dSArend van Spriel * The caller provides the initial value (either %CRC8_INIT_VALUE 877150962dSArend van Spriel * or the previous returned value) to allow for processing of 887150962dSArend van Spriel * discontiguous blocks of data. When generating the CRC the 897150962dSArend van Spriel * caller is responsible for complementing the final return value 907150962dSArend van Spriel * and inserting it into the byte stream. When validating a byte 917150962dSArend van Spriel * stream (including CRC8), a final return value of %CRC8_GOOD_VALUE 927150962dSArend van Spriel * indicates the byte stream data can be considered valid. 937150962dSArend van Spriel * 947150962dSArend van Spriel * Reference: 957150962dSArend van Spriel * "A Painless Guide to CRC Error Detection Algorithms", ver 3, Aug 1993 967150962dSArend van Spriel * Williams, Ross N., ross<at>ross.net 977150962dSArend van Spriel * (see URL http://www.ross.net/crc/download/crc_v3.txt). 987150962dSArend van Spriel */ 99*e18baa7cSRichard Fitzgerald u8 crc8(const u8 table[CRC8_TABLE_SIZE], const u8 *pdata, size_t nbytes, u8 crc); 1007150962dSArend van Spriel 1017150962dSArend van Spriel #endif /* __CRC8_H_ */ 102