crc8.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #ifndef __CRC8_H_
  17. #define __CRC8_H_
  18. #include <linux/types.h>
  19. /* see usage of this value in crc8() description */
  20. #define CRC8_INIT_VALUE 0xFF
  21. /*
  22. * Return value of crc8() indicating valid message+crc. This is true
  23. * if a CRC is inverted before transmission. The CRC computed over the
  24. * whole received bitstream is _table[x], where x is the bit pattern
  25. * of the modification (almost always 0xff).
  26. */
  27. #define CRC8_GOOD_VALUE(_table) (_table[0xFF])
  28. /* required table size for crc8 algorithm */
  29. #define CRC8_TABLE_SIZE 256
  30. /* helper macro assuring right table size is used */
  31. #define DECLARE_CRC8_TABLE(_table) \
  32. static u8 _table[CRC8_TABLE_SIZE]
  33. /**
  34. * crc8_populate_lsb - fill crc table for given polynomial in regular bit order.
  35. *
  36. * @table: table to be filled.
  37. * @polynomial: polynomial for which table is to be filled.
  38. *
  39. * This function fills the provided table according the polynomial provided for
  40. * regular bit order (lsb first). Polynomials in CRC algorithms are typically
  41. * represented as shown below.
  42. *
  43. * poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1
  44. *
  45. * For lsb first direction x^7 maps to the lsb. So the polynomial is as below.
  46. *
  47. * - lsb first: poly = 10101011(1) = 0xAB
  48. */
  49. void crc8_populate_lsb(u8 table[CRC8_TABLE_SIZE], u8 polynomial);
  50. /**
  51. * crc8_populate_msb - fill crc table for given polynomial in reverse bit order.
  52. *
  53. * @table: table to be filled.
  54. * @polynomial: polynomial for which table is to be filled.
  55. *
  56. * This function fills the provided table according the polynomial provided for
  57. * reverse bit order (msb first). Polynomials in CRC algorithms are typically
  58. * represented as shown below.
  59. *
  60. * poly = x^8 + x^7 + x^6 + x^4 + x^2 + 1
  61. *
  62. * For msb first direction x^7 maps to the msb. So the polynomial is as below.
  63. *
  64. * - msb first: poly = (1)11010101 = 0xD5
  65. */
  66. void crc8_populate_msb(u8 table[CRC8_TABLE_SIZE], u8 polynomial);
  67. /**
  68. * crc8() - calculate a crc8 over the given input data.
  69. *
  70. * @table: crc table used for calculation.
  71. * @pdata: pointer to data buffer.
  72. * @nbytes: number of bytes in data buffer.
  73. * @crc: previous returned crc8 value.
  74. *
  75. * The CRC8 is calculated using the polynomial given in crc8_populate_msb()
  76. * or crc8_populate_lsb().
  77. *
  78. * The caller provides the initial value (either %CRC8_INIT_VALUE
  79. * or the previous returned value) to allow for processing of
  80. * discontiguous blocks of data. When generating the CRC the
  81. * caller is responsible for complementing the final return value
  82. * and inserting it into the byte stream. When validating a byte
  83. * stream (including CRC8), a final return value of %CRC8_GOOD_VALUE
  84. * indicates the byte stream data can be considered valid.
  85. *
  86. * Reference:
  87. * "A Painless Guide to CRC Error Detection Algorithms", ver 3, Aug 1993
  88. * Williams, Ross N., ross<at>ross.net
  89. * (see URL http://www.ross.net/crc/download/crc_v3.txt).
  90. */
  91. u8 crc8(const u8 table[CRC8_TABLE_SIZE], u8 *pdata, size_t nbytes, u8 crc);
  92. #endif /* __CRC8_H_ */