1*89a51591SEric Biggers /* 2*89a51591SEric Biggers * Copyright (c) 2011 Broadcom Corporation 3*89a51591SEric Biggers * 4*89a51591SEric Biggers * Permission to use, copy, modify, and/or distribute this software for any 5*89a51591SEric Biggers * purpose with or without fee is hereby granted, provided that the above 6*89a51591SEric Biggers * copyright notice and this permission notice appear in all copies. 7*89a51591SEric Biggers * 8*89a51591SEric Biggers * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9*89a51591SEric Biggers * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10*89a51591SEric Biggers * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 11*89a51591SEric Biggers * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12*89a51591SEric Biggers * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 13*89a51591SEric Biggers * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 14*89a51591SEric Biggers * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15*89a51591SEric Biggers */ 16*89a51591SEric Biggers 17*89a51591SEric Biggers #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 18*89a51591SEric Biggers 19*89a51591SEric Biggers #include <linux/module.h> 20*89a51591SEric Biggers #include <linux/crc8.h> 21*89a51591SEric Biggers #include <linux/printk.h> 22*89a51591SEric Biggers 23*89a51591SEric Biggers /** 24*89a51591SEric Biggers * crc8_populate_msb - fill crc table for given polynomial in reverse bit order. 25*89a51591SEric Biggers * 26*89a51591SEric Biggers * @table: table to be filled. 27*89a51591SEric Biggers * @polynomial: polynomial for which table is to be filled. 28*89a51591SEric Biggers */ 29*89a51591SEric Biggers void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial) 30*89a51591SEric Biggers { 31*89a51591SEric Biggers int i, j; 32*89a51591SEric Biggers const u8 msbit = 0x80; 33*89a51591SEric Biggers u8 t = msbit; 34*89a51591SEric Biggers 35*89a51591SEric Biggers table[0] = 0; 36*89a51591SEric Biggers 37*89a51591SEric Biggers for (i = 1; i < CRC8_TABLE_SIZE; i *= 2) { 38*89a51591SEric Biggers t = (t << 1) ^ (t & msbit ? polynomial : 0); 39*89a51591SEric Biggers for (j = 0; j < i; j++) 40*89a51591SEric Biggers table[i+j] = table[j] ^ t; 41*89a51591SEric Biggers } 42*89a51591SEric Biggers } 43*89a51591SEric Biggers EXPORT_SYMBOL(crc8_populate_msb); 44*89a51591SEric Biggers 45*89a51591SEric Biggers /** 46*89a51591SEric Biggers * crc8_populate_lsb - fill crc table for given polynomial in regular bit order. 47*89a51591SEric Biggers * 48*89a51591SEric Biggers * @table: table to be filled. 49*89a51591SEric Biggers * @polynomial: polynomial for which table is to be filled. 50*89a51591SEric Biggers */ 51*89a51591SEric Biggers void crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial) 52*89a51591SEric Biggers { 53*89a51591SEric Biggers int i, j; 54*89a51591SEric Biggers u8 t = 1; 55*89a51591SEric Biggers 56*89a51591SEric Biggers table[0] = 0; 57*89a51591SEric Biggers 58*89a51591SEric Biggers for (i = (CRC8_TABLE_SIZE >> 1); i; i >>= 1) { 59*89a51591SEric Biggers t = (t >> 1) ^ (t & 1 ? polynomial : 0); 60*89a51591SEric Biggers for (j = 0; j < CRC8_TABLE_SIZE; j += 2*i) 61*89a51591SEric Biggers table[i+j] = table[j] ^ t; 62*89a51591SEric Biggers } 63*89a51591SEric Biggers } 64*89a51591SEric Biggers EXPORT_SYMBOL(crc8_populate_lsb); 65*89a51591SEric Biggers 66*89a51591SEric Biggers /** 67*89a51591SEric Biggers * crc8 - calculate a crc8 over the given input data. 68*89a51591SEric Biggers * 69*89a51591SEric Biggers * @table: crc table used for calculation. 70*89a51591SEric Biggers * @pdata: pointer to data buffer. 71*89a51591SEric Biggers * @nbytes: number of bytes in data buffer. 72*89a51591SEric Biggers * @crc: previous returned crc8 value. 73*89a51591SEric Biggers */ 74*89a51591SEric Biggers u8 crc8(const u8 table[CRC8_TABLE_SIZE], const u8 *pdata, size_t nbytes, u8 crc) 75*89a51591SEric Biggers { 76*89a51591SEric Biggers /* loop over the buffer data */ 77*89a51591SEric Biggers while (nbytes-- > 0) 78*89a51591SEric Biggers crc = table[(crc ^ *pdata++) & 0xff]; 79*89a51591SEric Biggers 80*89a51591SEric Biggers return crc; 81*89a51591SEric Biggers } 82*89a51591SEric Biggers EXPORT_SYMBOL(crc8); 83*89a51591SEric Biggers 84*89a51591SEric Biggers MODULE_DESCRIPTION("CRC8 (by Williams, Ross N.) function"); 85*89a51591SEric Biggers MODULE_AUTHOR("Broadcom Corporation"); 86*89a51591SEric Biggers MODULE_LICENSE("Dual BSD/GPL"); 87