162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2011 Broadcom Corporation 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any 562306a36Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above 662306a36Sopenharmony_ci * copyright notice and this permission notice appear in all copies. 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 962306a36Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1062306a36Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 1162306a36Sopenharmony_ci * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1262306a36Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 1362306a36Sopenharmony_ci * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 1462306a36Sopenharmony_ci * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1562306a36Sopenharmony_ci */ 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include <linux/module.h> 2062306a36Sopenharmony_ci#include <linux/crc8.h> 2162306a36Sopenharmony_ci#include <linux/printk.h> 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci/** 2462306a36Sopenharmony_ci * crc8_populate_msb - fill crc table for given polynomial in reverse bit order. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * @table: table to be filled. 2762306a36Sopenharmony_ci * @polynomial: polynomial for which table is to be filled. 2862306a36Sopenharmony_ci */ 2962306a36Sopenharmony_civoid crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial) 3062306a36Sopenharmony_ci{ 3162306a36Sopenharmony_ci int i, j; 3262306a36Sopenharmony_ci const u8 msbit = 0x80; 3362306a36Sopenharmony_ci u8 t = msbit; 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci table[0] = 0; 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci for (i = 1; i < CRC8_TABLE_SIZE; i *= 2) { 3862306a36Sopenharmony_ci t = (t << 1) ^ (t & msbit ? polynomial : 0); 3962306a36Sopenharmony_ci for (j = 0; j < i; j++) 4062306a36Sopenharmony_ci table[i+j] = table[j] ^ t; 4162306a36Sopenharmony_ci } 4262306a36Sopenharmony_ci} 4362306a36Sopenharmony_ciEXPORT_SYMBOL(crc8_populate_msb); 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci/** 4662306a36Sopenharmony_ci * crc8_populate_lsb - fill crc table for given polynomial in regular bit order. 4762306a36Sopenharmony_ci * 4862306a36Sopenharmony_ci * @table: table to be filled. 4962306a36Sopenharmony_ci * @polynomial: polynomial for which table is to be filled. 5062306a36Sopenharmony_ci */ 5162306a36Sopenharmony_civoid crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial) 5262306a36Sopenharmony_ci{ 5362306a36Sopenharmony_ci int i, j; 5462306a36Sopenharmony_ci u8 t = 1; 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci table[0] = 0; 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci for (i = (CRC8_TABLE_SIZE >> 1); i; i >>= 1) { 5962306a36Sopenharmony_ci t = (t >> 1) ^ (t & 1 ? polynomial : 0); 6062306a36Sopenharmony_ci for (j = 0; j < CRC8_TABLE_SIZE; j += 2*i) 6162306a36Sopenharmony_ci table[i+j] = table[j] ^ t; 6262306a36Sopenharmony_ci } 6362306a36Sopenharmony_ci} 6462306a36Sopenharmony_ciEXPORT_SYMBOL(crc8_populate_lsb); 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci/** 6762306a36Sopenharmony_ci * crc8 - calculate a crc8 over the given input data. 6862306a36Sopenharmony_ci * 6962306a36Sopenharmony_ci * @table: crc table used for calculation. 7062306a36Sopenharmony_ci * @pdata: pointer to data buffer. 7162306a36Sopenharmony_ci * @nbytes: number of bytes in data buffer. 7262306a36Sopenharmony_ci * @crc: previous returned crc8 value. 7362306a36Sopenharmony_ci */ 7462306a36Sopenharmony_ciu8 crc8(const u8 table[CRC8_TABLE_SIZE], const u8 *pdata, size_t nbytes, u8 crc) 7562306a36Sopenharmony_ci{ 7662306a36Sopenharmony_ci /* loop over the buffer data */ 7762306a36Sopenharmony_ci while (nbytes-- > 0) 7862306a36Sopenharmony_ci crc = table[(crc ^ *pdata++) & 0xff]; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci return crc; 8162306a36Sopenharmony_ci} 8262306a36Sopenharmony_ciEXPORT_SYMBOL(crc8); 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ciMODULE_DESCRIPTION("CRC8 (by Williams, Ross N.) function"); 8562306a36Sopenharmony_ciMODULE_AUTHOR("Broadcom Corporation"); 8662306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL"); 87