rslib.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * include/linux/rslib.h
  3. *
  4. * Overview:
  5. * Generic Reed Solomon encoder / decoder library
  6. *
  7. * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
  8. *
  9. * RS code lifted from reed solomon library written by Phil Karn
  10. * Copyright 2002 Phil Karn, KA9Q
  11. *
  12. * $Id: rslib.h,v 1.3 2004/10/05 22:08:22 gleixner Exp $
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #ifndef _RSLIB_H_
  19. #define _RSLIB_H_
  20. #include <linux/list.h>
  21. /**
  22. * struct rs_control - rs control structure
  23. *
  24. * @mm: Bits per symbol
  25. * @nn: Symbols per block (= (1<<mm)-1)
  26. * @alpha_to: log lookup table
  27. * @index_of: Antilog lookup table
  28. * @genpoly: Generator polynomial
  29. * @nroots: Number of generator roots = number of parity symbols
  30. * @fcr: First consecutive root, index form
  31. * @prim: Primitive element, index form
  32. * @iprim: prim-th root of 1, index form
  33. * @gfpoly: The primitive generator polynominal
  34. * @users: Users of this structure
  35. * @list: List entry for the rs control list
  36. */
  37. struct rs_control {
  38. int mm;
  39. int nn;
  40. uint16_t *alpha_to;
  41. uint16_t *index_of;
  42. uint16_t *genpoly;
  43. int nroots;
  44. int fcr;
  45. int prim;
  46. int iprim;
  47. int gfpoly;
  48. int users;
  49. struct list_head list;
  50. };
  51. /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
  52. #ifdef CONFIG_REED_SOLOMON_ENC8
  53. int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
  54. uint16_t invmsk);
  55. #endif
  56. #ifdef CONFIG_REED_SOLOMON_DEC8
  57. int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
  58. uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
  59. uint16_t *corr);
  60. #endif
  61. /* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
  62. #ifdef CONFIG_REED_SOLOMON_ENC16
  63. int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
  64. uint16_t invmsk);
  65. #endif
  66. #ifdef CONFIG_REED_SOLOMON_DEC16
  67. int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
  68. uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
  69. uint16_t *corr);
  70. #endif
  71. /* Create or get a matching rs control structure */
  72. struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
  73. int nroots);
  74. /* Release a rs control structure */
  75. void free_rs(struct rs_control *rs);
  76. /** modulo replacement for galois field arithmetics
  77. *
  78. * @rs: the rs control structure
  79. * @x: the value to reduce
  80. *
  81. * where
  82. * rs->mm = number of bits per symbol
  83. * rs->nn = (2^rs->mm) - 1
  84. *
  85. * Simple arithmetic modulo would return a wrong result for values
  86. * >= 3 * rs->nn
  87. */
  88. static inline int rs_modnn(struct rs_control *rs, int x)
  89. {
  90. while (x >= rs->nn) {
  91. x -= rs->nn;
  92. x = (x >> rs->mm) + (x & rs->nn);
  93. }
  94. return x;
  95. }
  96. #endif