2 * lib/reed_solomon/rslib.c
5 * Generic Reed Solomon encoder / decoder library
7 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
9 * Reed Solomon code lifted from reed solomon library written by Phil Karn
10 * Copyright 2002 Phil Karn, KA9Q
12 * $Id: rslib.c,v 1.5 2004/10/22 15:41:47 gleixner Exp $
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.
20 * The generic Reed Solomon library provides runtime configurable
21 * encoding / decoding of RS codes.
22 * Each user must call init_rs to get a pointer to a rs_control
23 * structure for the given rs parameters. This structure is either
24 * generated or a already available matching control structure is used.
25 * If a structure is generated then the polynomial arrays for
26 * fast encoding / decoding are built. This can take some time so
27 * make sure not to call this function from a time critical path.
28 * Usually a module / driver should initialize the necessary
29 * rs_control structure on module / driver init and release it
31 * The encoding puts the calculated syndrome into a given syndrome
33 * The decoding is a two step process. The first step calculates
34 * the syndrome over the received (data + syndrome) and calls the
35 * second stage, which does the decoding / error correction itself.
36 * Many hw encoders provide a syndrome calculation over the received
37 * data + syndrome and can call the second stage directly.
41 #include <linux/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/rslib.h>
46 #include <linux/slab.h>
47 #include <asm/semaphore.h>
49 /* This list holds all currently allocated rs control structures */
50 static LIST_HEAD (rslist
);
51 /* Protection for the list */
52 static DECLARE_MUTEX(rslistlock
);
55 * rs_init - Initialize a Reed-Solomon codec
57 * @symsize: symbol size, bits (1-8)
58 * @gfpoly: Field generator polynomial coefficients
59 * @fcr: first root of RS code generator polynomial, index form
60 * @prim: primitive element to generate polynomial roots
61 * @nroots: RS code generator polynomial degree (number of roots)
63 * Allocate a control structure and the polynom arrays for faster
64 * en/decoding. Fill the arrays according to the given parameters
66 static struct rs_control
*rs_init(int symsize
, int gfpoly
, int fcr
,
69 struct rs_control
*rs
;
70 int i
, j
, sr
, root
, iprim
;
72 /* Allocate the control structure */
73 rs
= kmalloc(sizeof (struct rs_control
), GFP_KERNEL
);
77 INIT_LIST_HEAD(&rs
->list
);
80 rs
->nn
= (1 << symsize
) - 1;
86 /* Allocate the arrays */
87 rs
->alpha_to
= kmalloc(sizeof(uint16_t) * (rs
->nn
+ 1), GFP_KERNEL
);
88 if (rs
->alpha_to
== NULL
)
91 rs
->index_of
= kmalloc(sizeof(uint16_t) * (rs
->nn
+ 1), GFP_KERNEL
);
92 if (rs
->index_of
== NULL
)
95 rs
->genpoly
= kmalloc(sizeof(uint16_t) * (rs
->nroots
+ 1), GFP_KERNEL
);
96 if(rs
->genpoly
== NULL
)
99 /* Generate Galois field lookup tables */
100 rs
->index_of
[0] = rs
->nn
; /* log(zero) = -inf */
101 rs
->alpha_to
[rs
->nn
] = 0; /* alpha**-inf = 0 */
103 for (i
= 0; i
< rs
->nn
; i
++) {
104 rs
->index_of
[sr
] = i
;
105 rs
->alpha_to
[i
] = sr
;
107 if (sr
& (1 << symsize
))
111 /* If it's not primitive, exit */
115 /* Find prim-th root of 1, used in decoding */
116 for(iprim
= 1; (iprim
% prim
) != 0; iprim
+= rs
->nn
);
117 /* prim-th root of 1, index form */
118 rs
->iprim
= iprim
/ prim
;
120 /* Form RS code generator polynomial from its roots */
122 for (i
= 0, root
= fcr
* prim
; i
< nroots
; i
++, root
+= prim
) {
123 rs
->genpoly
[i
+ 1] = 1;
124 /* Multiply rs->genpoly[] by @**(root + x) */
125 for (j
= i
; j
> 0; j
--) {
126 if (rs
->genpoly
[j
] != 0) {
127 rs
->genpoly
[j
] = rs
->genpoly
[j
-1] ^
128 rs
->alpha_to
[rs_modnn(rs
,
129 rs
->index_of
[rs
->genpoly
[j
]] + root
)];
131 rs
->genpoly
[j
] = rs
->genpoly
[j
- 1];
133 /* rs->genpoly[0] can never be zero */
135 rs
->alpha_to
[rs_modnn(rs
,
136 rs
->index_of
[rs
->genpoly
[0]] + root
)];
138 /* convert rs->genpoly[] to index form for quicker encoding */
139 for (i
= 0; i
<= nroots
; i
++)
140 rs
->genpoly
[i
] = rs
->index_of
[rs
->genpoly
[i
]];
157 * free_rs - Free the rs control structure, if its not longer used
159 * @rs: the control structure which is not longer used by the
162 void free_rs(struct rs_control
*rs
)
177 * init_rs - Find a matching or allocate a new rs control structure
179 * @symsize: the symbol size (number of bits)
180 * @gfpoly: the extended Galois field generator polynomial coefficients,
181 * with the 0th coefficient in the low order bit. The polynomial
183 * @fcr: the first consecutive root of the rs code generator polynomial
185 * @prim: primitive element to generate polynomial roots
186 * @nroots: RS code generator polynomial degree (number of roots)
188 struct rs_control
*init_rs(int symsize
, int gfpoly
, int fcr
, int prim
,
191 struct list_head
*tmp
;
192 struct rs_control
*rs
;
197 if (fcr
< 0 || fcr
>= (1<<symsize
))
199 if (prim
<= 0 || prim
>= (1<<symsize
))
201 if (nroots
< 0 || nroots
>= (1<<symsize
) || nroots
> 8)
206 /* Walk through the list and look for a matching entry */
207 list_for_each(tmp
, &rslist
) {
208 rs
= list_entry(tmp
, struct rs_control
, list
);
209 if (symsize
!= rs
->mm
)
211 if (gfpoly
!= rs
->gfpoly
)
215 if (prim
!= rs
->prim
)
217 if (nroots
!= rs
->nroots
)
219 /* We have a matching one already */
224 /* Create a new one */
225 rs
= rs_init(symsize
, gfpoly
, fcr
, prim
, nroots
);
228 list_add(&rs
->list
, &rslist
);
235 #ifdef CONFIG_REED_SOLOMON_ENC8
237 * encode_rs8 - Calculate the parity for data values (8bit data width)
239 * @rs: the rs control structure
240 * @data: data field of a given type
242 * @par: parity data, must be initialized by caller (usually all 0)
243 * @invmsk: invert data mask (will be xored on data)
245 * The parity uses a uint16_t data type to enable
246 * symbol size > 8. The calling code must take care of encoding of the
247 * syndrome result for storage itself.
249 int encode_rs8(struct rs_control
*rs
, uint8_t *data
, int len
, uint16_t *par
,
252 #include "encode_rs.c"
254 EXPORT_SYMBOL_GPL(encode_rs8
);
257 #ifdef CONFIG_REED_SOLOMON_DEC8
259 * decode_rs8 - Decode codeword (8bit data width)
261 * @rs: the rs control structure
262 * @data: data field of a given type
263 * @par: received parity data field
265 * @s: syndrome data field (if NULL, syndrome is calculated)
266 * @no_eras: number of erasures
267 * @eras_pos: position of erasures, can be NULL
268 * @invmsk: invert data mask (will be xored on data, not on parity!)
269 * @corr: buffer to store correction bitmask on eras_pos
271 * The syndrome and parity uses a uint16_t data type to enable
272 * symbol size > 8. The calling code must take care of decoding of the
273 * syndrome result and the received parity before calling this code.
275 int decode_rs8(struct rs_control
*rs
, uint8_t *data
, uint16_t *par
, int len
,
276 uint16_t *s
, int no_eras
, int *eras_pos
, uint16_t invmsk
,
279 #include "decode_rs.c"
281 EXPORT_SYMBOL_GPL(decode_rs8
);
284 #ifdef CONFIG_REED_SOLOMON_ENC16
286 * encode_rs16 - Calculate the parity for data values (16bit data width)
288 * @rs: the rs control structure
289 * @data: data field of a given type
291 * @par: parity data, must be initialized by caller (usually all 0)
292 * @invmsk: invert data mask (will be xored on data, not on parity!)
294 * Each field in the data array contains up to symbol size bits of valid data.
296 int encode_rs16(struct rs_control
*rs
, uint16_t *data
, int len
, uint16_t *par
,
299 #include "encode_rs.c"
301 EXPORT_SYMBOL_GPL(encode_rs16
);
304 #ifdef CONFIG_REED_SOLOMON_DEC16
306 * decode_rs16 - Decode codeword (16bit data width)
308 * @rs: the rs control structure
309 * @data: data field of a given type
310 * @par: received parity data field
312 * @s: syndrome data field (if NULL, syndrome is calculated)
313 * @no_eras: number of erasures
314 * @eras_pos: position of erasures, can be NULL
315 * @invmsk: invert data mask (will be xored on data, not on parity!)
316 * @corr: buffer to store correction bitmask on eras_pos
318 * Each field in the data array contains up to symbol size bits of valid data.
320 int decode_rs16(struct rs_control
*rs
, uint16_t *data
, uint16_t *par
, int len
,
321 uint16_t *s
, int no_eras
, int *eras_pos
, uint16_t invmsk
,
324 #include "decode_rs.c"
326 EXPORT_SYMBOL_GPL(decode_rs16
);
329 EXPORT_SYMBOL_GPL(init_rs
);
330 EXPORT_SYMBOL_GPL(free_rs
);
332 MODULE_LICENSE("GPL");
333 MODULE_DESCRIPTION("Reed Solomon encoder/decoder");
334 MODULE_AUTHOR("Phil Karn, Thomas Gleixner");