crc8.c 2.4 KB

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