1 // SPDX-License-Identifier: GPL-2.0
3 * Generic Reed Solomon encoder / decoder library
5 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
7 * RS code lifted from reed solomon library written by Phil Karn
8 * Copyright 2002 Phil Karn, KA9Q
13 #include <linux/list.h>
14 #include <linux/types.h> /* for gfp_t */
15 #include <linux/gfp.h> /* for GFP_KERNEL */
18 * struct rs_codec - rs codec data
20 * @mm: Bits per symbol
21 * @nn: Symbols per block (= (1<<mm)-1)
22 * @alpha_to: log lookup table
23 * @index_of: Antilog lookup table
24 * @genpoly: Generator polynomial
25 * @nroots: Number of generator roots = number of parity symbols
26 * @fcr: First consecutive root, index form
27 * @prim: Primitive element, index form
28 * @iprim: prim-th root of 1, index form
29 * @gfpoly: The primitive generator polynominal
30 * @gffunc: Function to generate the field, if non-canonical representation
31 * @users: Users of this structure
32 * @list: List entry for the rs codec list
47 struct list_head list;
51 * struct rs_control - rs control structure per instance
52 * @codec: The codec used for this instance
53 * @buffers: Internal scratch buffers used in calls to decode_rs()
56 struct rs_codec *codec;
60 /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
61 #ifdef CONFIG_REED_SOLOMON_ENC8
62 int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
65 #ifdef CONFIG_REED_SOLOMON_DEC8
66 int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
67 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
71 /* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
72 #ifdef CONFIG_REED_SOLOMON_ENC16
73 int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
76 #ifdef CONFIG_REED_SOLOMON_DEC16
77 int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
78 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
82 struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
83 int nroots, gfp_t gfp);
86 * init_rs - Create a RS control struct and initialize it
87 * @symsize: the symbol size (number of bits)
88 * @gfpoly: the extended Galois field generator polynomial coefficients,
89 * with the 0th coefficient in the low order bit. The polynomial
91 * @fcr: the first consecutive root of the rs code generator polynomial
93 * @prim: primitive element to generate polynomial roots
94 * @nroots: RS code generator polynomial degree (number of roots)
96 * Allocations use GFP_KERNEL.
98 static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr,
101 return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL);
104 struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
105 int fcr, int prim, int nroots);
107 /* Release a rs control structure */
108 void free_rs(struct rs_control *rs);
110 /** modulo replacement for galois field arithmetics
112 * @rs: Pointer to the RS codec
113 * @x: the value to reduce
116 * rs->mm = number of bits per symbol
117 * rs->nn = (2^rs->mm) - 1
119 * Simple arithmetic modulo would return a wrong result for values
122 static inline int rs_modnn(struct rs_codec *rs, int x)
124 while (x >= rs->nn) {
126 x = (x >> rs->mm) + (x & rs->nn);