librs.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ==========================================
  2. Reed-Solomon Library Programming Interface
  3. ==========================================
  4. :Author: Thomas Gleixner
  5. Introduction
  6. ============
  7. The generic Reed-Solomon Library provides encoding, decoding and error
  8. correction functions.
  9. Reed-Solomon codes are used in communication and storage applications to
  10. ensure data integrity.
  11. This documentation is provided for developers who want to utilize the
  12. functions provided by the library.
  13. Known Bugs And Assumptions
  14. ==========================
  15. None.
  16. Usage
  17. =====
  18. This chapter provides examples of how to use the library.
  19. Initializing
  20. ------------
  21. The init function init_rs returns a pointer to an rs decoder structure,
  22. which holds the necessary information for encoding, decoding and error
  23. correction with the given polynomial. It either uses an existing
  24. matching decoder or creates a new one. On creation all the lookup tables
  25. for fast en/decoding are created. The function may take a while, so make
  26. sure not to call it in critical code paths.
  27. ::
  28. /* the Reed Solomon control structure */
  29. static struct rs_control *rs_decoder;
  30. /* Symbolsize is 10 (bits)
  31. * Primitive polynomial is x^10+x^3+1
  32. * first consecutive root is 0
  33. * primitive element to generate roots = 1
  34. * generator polynomial degree (number of roots) = 6
  35. */
  36. rs_decoder = init_rs (10, 0x409, 0, 1, 6);
  37. Encoding
  38. --------
  39. The encoder calculates the Reed-Solomon code over the given data length
  40. and stores the result in the parity buffer. Note that the parity buffer
  41. must be initialized before calling the encoder.
  42. The expanded data can be inverted on the fly by providing a non-zero
  43. inversion mask. The expanded data is XOR'ed with the mask. This is used
  44. e.g. for FLASH ECC, where the all 0xFF is inverted to an all 0x00. The
  45. Reed-Solomon code for all 0x00 is all 0x00. The code is inverted before
  46. storing to FLASH so it is 0xFF too. This prevents that reading from an
  47. erased FLASH results in ECC errors.
  48. The databytes are expanded to the given symbol size on the fly. There is
  49. no support for encoding continuous bitstreams with a symbol size != 8 at
  50. the moment. If it is necessary it should be not a big deal to implement
  51. such functionality.
  52. ::
  53. /* Parity buffer. Size = number of roots */
  54. uint16_t par[6];
  55. /* Initialize the parity buffer */
  56. memset(par, 0, sizeof(par));
  57. /* Encode 512 byte in data8. Store parity in buffer par */
  58. encode_rs8 (rs_decoder, data8, 512, par, 0);
  59. Decoding
  60. --------
  61. The decoder calculates the syndrome over the given data length and the
  62. received parity symbols and corrects errors in the data.
  63. If a syndrome is available from a hardware decoder then the syndrome
  64. calculation is skipped.
  65. The correction of the data buffer can be suppressed by providing a
  66. correction pattern buffer and an error location buffer to the decoder.
  67. The decoder stores the calculated error location and the correction
  68. bitmask in the given buffers. This is useful for hardware decoders which
  69. use a weird bit ordering scheme.
  70. The databytes are expanded to the given symbol size on the fly. There is
  71. no support for decoding continuous bitstreams with a symbolsize != 8 at
  72. the moment. If it is necessary it should be not a big deal to implement
  73. such functionality.
  74. Decoding with syndrome calculation, direct data correction
  75. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. ::
  77. /* Parity buffer. Size = number of roots */
  78. uint16_t par[6];
  79. uint8_t data[512];
  80. int numerr;
  81. /* Receive data */
  82. .....
  83. /* Receive parity */
  84. .....
  85. /* Decode 512 byte in data8.*/
  86. numerr = decode_rs8 (rs_decoder, data8, par, 512, NULL, 0, NULL, 0, NULL);
  87. Decoding with syndrome given by hardware decoder, direct data correction
  88. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  89. ::
  90. /* Parity buffer. Size = number of roots */
  91. uint16_t par[6], syn[6];
  92. uint8_t data[512];
  93. int numerr;
  94. /* Receive data */
  95. .....
  96. /* Receive parity */
  97. .....
  98. /* Get syndrome from hardware decoder */
  99. .....
  100. /* Decode 512 byte in data8.*/
  101. numerr = decode_rs8 (rs_decoder, data8, par, 512, syn, 0, NULL, 0, NULL);
  102. Decoding with syndrome given by hardware decoder, no direct data correction.
  103. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  104. Note: It's not necessary to give data and received parity to the
  105. decoder.
  106. ::
  107. /* Parity buffer. Size = number of roots */
  108. uint16_t par[6], syn[6], corr[8];
  109. uint8_t data[512];
  110. int numerr, errpos[8];
  111. /* Receive data */
  112. .....
  113. /* Receive parity */
  114. .....
  115. /* Get syndrome from hardware decoder */
  116. .....
  117. /* Decode 512 byte in data8.*/
  118. numerr = decode_rs8 (rs_decoder, NULL, NULL, 512, syn, 0, errpos, 0, corr);
  119. for (i = 0; i < numerr; i++) {
  120. do_error_correction_in_your_buffer(errpos[i], corr[i]);
  121. }
  122. Cleanup
  123. -------
  124. The function free_rs frees the allocated resources, if the caller is
  125. the last user of the decoder.
  126. ::
  127. /* Release resources */
  128. free_rs(rs_decoder);
  129. Structures
  130. ==========
  131. This chapter contains the autogenerated documentation of the structures
  132. which are used in the Reed-Solomon Library and are relevant for a
  133. developer.
  134. .. kernel-doc:: include/linux/rslib.h
  135. :internal:
  136. Public Functions Provided
  137. =========================
  138. This chapter contains the autogenerated documentation of the
  139. Reed-Solomon functions which are exported.
  140. .. kernel-doc:: lib/reed_solomon/reed_solomon.c
  141. :export:
  142. Credits
  143. =======
  144. The library code for encoding and decoding was written by Phil Karn.
  145. ::
  146. Copyright 2002, Phil Karn, KA9Q
  147. May be used under the terms of the GNU General Public License (GPL)
  148. The wrapper functions and interfaces are written by Thomas Gleixner.
  149. Many users have provided bugfixes, improvements and helping hands for
  150. testing. Thanks a lot.
  151. The following people have contributed to this document:
  152. Thomas Gleixner\ [email protected]