rslib.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.4 2005/11/07 11:14:52 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. * @gffunc: Function to generate the field, if non-canonical representation
  35. * @users: Users of this structure
  36. * @list: List entry for the rs control list
  37. */
  38. struct rs_control {
  39. int mm;
  40. int nn;
  41. uint16_t *alpha_to;
  42. uint16_t *index_of;
  43. uint16_t *genpoly;
  44. int nroots;
  45. int fcr;
  46. int prim;
  47. int iprim;
  48. int gfpoly;
  49. int (*gffunc)(int);
  50. int users;
  51. struct list_head list;
  52. };
  53. /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
  54. #ifdef CONFIG_REED_SOLOMON_ENC8
  55. int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
  56. uint16_t invmsk);
  57. #endif
  58. #ifdef CONFIG_REED_SOLOMON_DEC8
  59. int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
  60. uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
  61. uint16_t *corr);
  62. #endif
  63. /* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
  64. #ifdef CONFIG_REED_SOLOMON_ENC16
  65. int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
  66. uint16_t invmsk);
  67. #endif
  68. #ifdef CONFIG_REED_SOLOMON_DEC16
  69. int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
  70. uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
  71. uint16_t *corr);
  72. #endif
  73. /* Create or get a matching rs control structure */
  74. struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
  75. int nroots);
  76. struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
  77. int fcr, int prim, int nroots);
  78. /* Release a rs control structure */
  79. void free_rs(struct rs_control *rs);
  80. /** modulo replacement for galois field arithmetics
  81. *
  82. * @rs: the rs control structure
  83. * @x: the value to reduce
  84. *
  85. * where
  86. * rs->mm = number of bits per symbol
  87. * rs->nn = (2^rs->mm) - 1
  88. *
  89. * Simple arithmetic modulo would return a wrong result for values
  90. * >= 3 * rs->nn
  91. */
  92. static inline int rs_modnn(struct rs_control *rs, int x)
  93. {
  94. while (x >= rs->nn) {
  95. x -= rs->nn;
  96. x = (x >> rs->mm) + (x & rs->nn);
  97. }
  98. return x;
  99. }
  100. #endif