18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2011 Broadcom Corporation 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any 58c2ecf20Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above 68c2ecf20Sopenharmony_ci * copyright notice and this permission notice appear in all copies. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 98c2ecf20Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 108c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 118c2ecf20Sopenharmony_ci * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 128c2ecf20Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 138c2ecf20Sopenharmony_ci * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 148c2ecf20Sopenharmony_ci * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/module.h> 208c2ecf20Sopenharmony_ci#include <linux/crc8.h> 218c2ecf20Sopenharmony_ci#include <linux/printk.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/** 248c2ecf20Sopenharmony_ci * crc8_populate_msb - fill crc table for given polynomial in reverse bit order. 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * @table: table to be filled. 278c2ecf20Sopenharmony_ci * @polynomial: polynomial for which table is to be filled. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_civoid crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci int i, j; 328c2ecf20Sopenharmony_ci const u8 msbit = 0x80; 338c2ecf20Sopenharmony_ci u8 t = msbit; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci table[0] = 0; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci for (i = 1; i < CRC8_TABLE_SIZE; i *= 2) { 388c2ecf20Sopenharmony_ci t = (t << 1) ^ (t & msbit ? polynomial : 0); 398c2ecf20Sopenharmony_ci for (j = 0; j < i; j++) 408c2ecf20Sopenharmony_ci table[i+j] = table[j] ^ t; 418c2ecf20Sopenharmony_ci } 428c2ecf20Sopenharmony_ci} 438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(crc8_populate_msb); 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/** 468c2ecf20Sopenharmony_ci * crc8_populate_lsb - fill crc table for given polynomial in regular bit order. 478c2ecf20Sopenharmony_ci * 488c2ecf20Sopenharmony_ci * @table: table to be filled. 498c2ecf20Sopenharmony_ci * @polynomial: polynomial for which table is to be filled. 508c2ecf20Sopenharmony_ci */ 518c2ecf20Sopenharmony_civoid crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci int i, j; 548c2ecf20Sopenharmony_ci u8 t = 1; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci table[0] = 0; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci for (i = (CRC8_TABLE_SIZE >> 1); i; i >>= 1) { 598c2ecf20Sopenharmony_ci t = (t >> 1) ^ (t & 1 ? polynomial : 0); 608c2ecf20Sopenharmony_ci for (j = 0; j < CRC8_TABLE_SIZE; j += 2*i) 618c2ecf20Sopenharmony_ci table[i+j] = table[j] ^ t; 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ciEXPORT_SYMBOL(crc8_populate_lsb); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/** 678c2ecf20Sopenharmony_ci * crc8 - calculate a crc8 over the given input data. 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * @table: crc table used for calculation. 708c2ecf20Sopenharmony_ci * @pdata: pointer to data buffer. 718c2ecf20Sopenharmony_ci * @nbytes: number of bytes in data buffer. 728c2ecf20Sopenharmony_ci * @crc: previous returned crc8 value. 738c2ecf20Sopenharmony_ci */ 748c2ecf20Sopenharmony_ciu8 crc8(const u8 table[CRC8_TABLE_SIZE], u8 *pdata, size_t nbytes, u8 crc) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci /* loop over the buffer data */ 778c2ecf20Sopenharmony_ci while (nbytes-- > 0) 788c2ecf20Sopenharmony_ci crc = table[(crc ^ *pdata++) & 0xff]; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci return crc; 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ciEXPORT_SYMBOL(crc8); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CRC8 (by Williams, Ross N.) function"); 858c2ecf20Sopenharmony_ciMODULE_AUTHOR("Broadcom Corporation"); 868c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL"); 87