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 
0017 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
0018 
0019 #include <linux/module.h>
0020 #include <linux/crc8.h>
0021 #include <linux/printk.h>
0022 
0023 /**
0024  * crc8_populate_msb - fill crc table for given polynomial in reverse bit order.
0025  *
0026  * @table:  table to be filled.
0027  * @polynomial: polynomial for which table is to be filled.
0028  */
0029 void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial)
0030 {
0031     int i, j;
0032     const u8 msbit = 0x80;
0033     u8 t = msbit;
0034 
0035     table[0] = 0;
0036 
0037     for (i = 1; i < CRC8_TABLE_SIZE; i *= 2) {
0038         t = (t << 1) ^ (t & msbit ? polynomial : 0);
0039         for (j = 0; j < i; j++)
0040             table[i+j] = table[j] ^ t;
0041     }
0042 }
0043 EXPORT_SYMBOL(crc8_populate_msb);
0044 
0045 /**
0046  * crc8_populate_lsb - fill crc table for given polynomial in regular bit order.
0047  *
0048  * @table:  table to be filled.
0049  * @polynomial: polynomial for which table is to be filled.
0050  */
0051 void crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial)
0052 {
0053     int i, j;
0054     u8 t = 1;
0055 
0056     table[0] = 0;
0057 
0058     for (i = (CRC8_TABLE_SIZE >> 1); i; i >>= 1) {
0059         t = (t >> 1) ^ (t & 1 ? polynomial : 0);
0060         for (j = 0; j < CRC8_TABLE_SIZE; j += 2*i)
0061             table[i+j] = table[j] ^ t;
0062     }
0063 }
0064 EXPORT_SYMBOL(crc8_populate_lsb);
0065 
0066 /**
0067  * crc8 - calculate a crc8 over the given input data.
0068  *
0069  * @table: crc table used for calculation.
0070  * @pdata: pointer to data buffer.
0071  * @nbytes: number of bytes in data buffer.
0072  * @crc: previous returned crc8 value.
0073  */
0074 u8 crc8(const u8 table[CRC8_TABLE_SIZE], const u8 *pdata, size_t nbytes, u8 crc)
0075 {
0076     /* loop over the buffer data */
0077     while (nbytes-- > 0)
0078         crc = table[(crc ^ *pdata++) & 0xff];
0079 
0080     return crc;
0081 }
0082 EXPORT_SYMBOL(crc8);
0083 
0084 MODULE_DESCRIPTION("CRC8 (by Williams, Ross N.) function");
0085 MODULE_AUTHOR("Broadcom Corporation");
0086 MODULE_LICENSE("Dual BSD/GPL");