diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /lib/reed_solomon |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'lib/reed_solomon')
-rw-r--r-- | lib/reed_solomon/Makefile | 6 | ||||
-rw-r--r-- | lib/reed_solomon/decode_rs.c | 272 | ||||
-rw-r--r-- | lib/reed_solomon/encode_rs.c | 54 | ||||
-rw-r--r-- | lib/reed_solomon/reed_solomon.c | 335 |
4 files changed, 667 insertions, 0 deletions
diff --git a/lib/reed_solomon/Makefile b/lib/reed_solomon/Makefile new file mode 100644 index 000000000000..747a2de29346 --- /dev/null +++ b/lib/reed_solomon/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | # | ||
2 | # This is a modified version of reed solomon lib, | ||
3 | # | ||
4 | |||
5 | obj-$(CONFIG_REED_SOLOMON) += reed_solomon.o | ||
6 | |||
diff --git a/lib/reed_solomon/decode_rs.c b/lib/reed_solomon/decode_rs.c new file mode 100644 index 000000000000..d401decd6289 --- /dev/null +++ b/lib/reed_solomon/decode_rs.c | |||
@@ -0,0 +1,272 @@ | |||
1 | /* | ||
2 | * lib/reed_solomon/decode_rs.c | ||
3 | * | ||
4 | * Overview: | ||
5 | * Generic Reed Solomon encoder / decoder library | ||
6 | * | ||
7 | * Copyright 2002, Phil Karn, KA9Q | ||
8 | * May be used under the terms of the GNU General Public License (GPL) | ||
9 | * | ||
10 | * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de) | ||
11 | * | ||
12 | * $Id: decode_rs.c,v 1.6 2004/10/22 15:41:47 gleixner Exp $ | ||
13 | * | ||
14 | */ | ||
15 | |||
16 | /* Generic data width independent code which is included by the | ||
17 | * wrappers. | ||
18 | */ | ||
19 | { | ||
20 | int deg_lambda, el, deg_omega; | ||
21 | int i, j, r, k, pad; | ||
22 | int nn = rs->nn; | ||
23 | int nroots = rs->nroots; | ||
24 | int fcr = rs->fcr; | ||
25 | int prim = rs->prim; | ||
26 | int iprim = rs->iprim; | ||
27 | uint16_t *alpha_to = rs->alpha_to; | ||
28 | uint16_t *index_of = rs->index_of; | ||
29 | uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error; | ||
30 | /* Err+Eras Locator poly and syndrome poly The maximum value | ||
31 | * of nroots is 8. So the necessary stack size will be about | ||
32 | * 220 bytes max. | ||
33 | */ | ||
34 | uint16_t lambda[nroots + 1], syn[nroots]; | ||
35 | uint16_t b[nroots + 1], t[nroots + 1], omega[nroots + 1]; | ||
36 | uint16_t root[nroots], reg[nroots + 1], loc[nroots]; | ||
37 | int count = 0; | ||
38 | uint16_t msk = (uint16_t) rs->nn; | ||
39 | |||
40 | /* Check length parameter for validity */ | ||
41 | pad = nn - nroots - len; | ||
42 | if (pad < 0 || pad >= nn) | ||
43 | return -ERANGE; | ||
44 | |||
45 | /* Does the caller provide the syndrome ? */ | ||
46 | if (s != NULL) | ||
47 | goto decode; | ||
48 | |||
49 | /* form the syndromes; i.e., evaluate data(x) at roots of | ||
50 | * g(x) */ | ||
51 | for (i = 0; i < nroots; i++) | ||
52 | syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk; | ||
53 | |||
54 | for (j = 1; j < len; j++) { | ||
55 | for (i = 0; i < nroots; i++) { | ||
56 | if (syn[i] == 0) { | ||
57 | syn[i] = (((uint16_t) data[j]) ^ | ||
58 | invmsk) & msk; | ||
59 | } else { | ||
60 | syn[i] = ((((uint16_t) data[j]) ^ | ||
61 | invmsk) & msk) ^ | ||
62 | alpha_to[rs_modnn(rs, index_of[syn[i]] + | ||
63 | (fcr + i) * prim)]; | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | for (j = 0; j < nroots; j++) { | ||
69 | for (i = 0; i < nroots; i++) { | ||
70 | if (syn[i] == 0) { | ||
71 | syn[i] = ((uint16_t) par[j]) & msk; | ||
72 | } else { | ||
73 | syn[i] = (((uint16_t) par[j]) & msk) ^ | ||
74 | alpha_to[rs_modnn(rs, index_of[syn[i]] + | ||
75 | (fcr+i)*prim)]; | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | s = syn; | ||
80 | |||
81 | /* Convert syndromes to index form, checking for nonzero condition */ | ||
82 | syn_error = 0; | ||
83 | for (i = 0; i < nroots; i++) { | ||
84 | syn_error |= s[i]; | ||
85 | s[i] = index_of[s[i]]; | ||
86 | } | ||
87 | |||
88 | if (!syn_error) { | ||
89 | /* if syndrome is zero, data[] is a codeword and there are no | ||
90 | * errors to correct. So return data[] unmodified | ||
91 | */ | ||
92 | count = 0; | ||
93 | goto finish; | ||
94 | } | ||
95 | |||
96 | decode: | ||
97 | memset(&lambda[1], 0, nroots * sizeof(lambda[0])); | ||
98 | lambda[0] = 1; | ||
99 | |||
100 | if (no_eras > 0) { | ||
101 | /* Init lambda to be the erasure locator polynomial */ | ||
102 | lambda[1] = alpha_to[rs_modnn(rs, | ||
103 | prim * (nn - 1 - eras_pos[0]))]; | ||
104 | for (i = 1; i < no_eras; i++) { | ||
105 | u = rs_modnn(rs, prim * (nn - 1 - eras_pos[i])); | ||
106 | for (j = i + 1; j > 0; j--) { | ||
107 | tmp = index_of[lambda[j - 1]]; | ||
108 | if (tmp != nn) { | ||
109 | lambda[j] ^= | ||
110 | alpha_to[rs_modnn(rs, u + tmp)]; | ||
111 | } | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | |||
116 | for (i = 0; i < nroots + 1; i++) | ||
117 | b[i] = index_of[lambda[i]]; | ||
118 | |||
119 | /* | ||
120 | * Begin Berlekamp-Massey algorithm to determine error+erasure | ||
121 | * locator polynomial | ||
122 | */ | ||
123 | r = no_eras; | ||
124 | el = no_eras; | ||
125 | while (++r <= nroots) { /* r is the step number */ | ||
126 | /* Compute discrepancy at the r-th step in poly-form */ | ||
127 | discr_r = 0; | ||
128 | for (i = 0; i < r; i++) { | ||
129 | if ((lambda[i] != 0) && (s[r - i - 1] != nn)) { | ||
130 | discr_r ^= | ||
131 | alpha_to[rs_modnn(rs, | ||
132 | index_of[lambda[i]] + | ||
133 | s[r - i - 1])]; | ||
134 | } | ||
135 | } | ||
136 | discr_r = index_of[discr_r]; /* Index form */ | ||
137 | if (discr_r == nn) { | ||
138 | /* 2 lines below: B(x) <-- x*B(x) */ | ||
139 | memmove (&b[1], b, nroots * sizeof (b[0])); | ||
140 | b[0] = nn; | ||
141 | } else { | ||
142 | /* 7 lines below: T(x) <-- lambda(x)-discr_r*x*b(x) */ | ||
143 | t[0] = lambda[0]; | ||
144 | for (i = 0; i < nroots; i++) { | ||
145 | if (b[i] != nn) { | ||
146 | t[i + 1] = lambda[i + 1] ^ | ||
147 | alpha_to[rs_modnn(rs, discr_r + | ||
148 | b[i])]; | ||
149 | } else | ||
150 | t[i + 1] = lambda[i + 1]; | ||
151 | } | ||
152 | if (2 * el <= r + no_eras - 1) { | ||
153 | el = r + no_eras - el; | ||
154 | /* | ||
155 | * 2 lines below: B(x) <-- inv(discr_r) * | ||
156 | * lambda(x) | ||
157 | */ | ||
158 | for (i = 0; i <= nroots; i++) { | ||
159 | b[i] = (lambda[i] == 0) ? nn : | ||
160 | rs_modnn(rs, index_of[lambda[i]] | ||
161 | - discr_r + nn); | ||
162 | } | ||
163 | } else { | ||
164 | /* 2 lines below: B(x) <-- x*B(x) */ | ||
165 | memmove(&b[1], b, nroots * sizeof(b[0])); | ||
166 | b[0] = nn; | ||
167 | } | ||
168 | memcpy(lambda, t, (nroots + 1) * sizeof(t[0])); | ||
169 | } | ||
170 | } | ||
171 | |||
172 | /* Convert lambda to index form and compute deg(lambda(x)) */ | ||
173 | deg_lambda = 0; | ||
174 | for (i = 0; i < nroots + 1; i++) { | ||
175 | lambda[i] = index_of[lambda[i]]; | ||
176 | if (lambda[i] != nn) | ||
177 | deg_lambda = i; | ||
178 | } | ||
179 | /* Find roots of error+erasure locator polynomial by Chien search */ | ||
180 | memcpy(®[1], &lambda[1], nroots * sizeof(reg[0])); | ||
181 | count = 0; /* Number of roots of lambda(x) */ | ||
182 | for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) { | ||
183 | q = 1; /* lambda[0] is always 0 */ | ||
184 | for (j = deg_lambda; j > 0; j--) { | ||
185 | if (reg[j] != nn) { | ||
186 | reg[j] = rs_modnn(rs, reg[j] + j); | ||
187 | q ^= alpha_to[reg[j]]; | ||
188 | } | ||
189 | } | ||
190 | if (q != 0) | ||
191 | continue; /* Not a root */ | ||
192 | /* store root (index-form) and error location number */ | ||
193 | root[count] = i; | ||
194 | loc[count] = k; | ||
195 | /* If we've already found max possible roots, | ||
196 | * abort the search to save time | ||
197 | */ | ||
198 | if (++count == deg_lambda) | ||
199 | break; | ||
200 | } | ||
201 | if (deg_lambda != count) { | ||
202 | /* | ||
203 | * deg(lambda) unequal to number of roots => uncorrectable | ||
204 | * error detected | ||
205 | */ | ||
206 | count = -1; | ||
207 | goto finish; | ||
208 | } | ||
209 | /* | ||
210 | * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo | ||
211 | * x**nroots). in index form. Also find deg(omega). | ||
212 | */ | ||
213 | deg_omega = deg_lambda - 1; | ||
214 | for (i = 0; i <= deg_omega; i++) { | ||
215 | tmp = 0; | ||
216 | for (j = i; j >= 0; j--) { | ||
217 | if ((s[i - j] != nn) && (lambda[j] != nn)) | ||
218 | tmp ^= | ||
219 | alpha_to[rs_modnn(rs, s[i - j] + lambda[j])]; | ||
220 | } | ||
221 | omega[i] = index_of[tmp]; | ||
222 | } | ||
223 | |||
224 | /* | ||
225 | * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 = | ||
226 | * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form | ||
227 | */ | ||
228 | for (j = count - 1; j >= 0; j--) { | ||
229 | num1 = 0; | ||
230 | for (i = deg_omega; i >= 0; i--) { | ||
231 | if (omega[i] != nn) | ||
232 | num1 ^= alpha_to[rs_modnn(rs, omega[i] + | ||
233 | i * root[j])]; | ||
234 | } | ||
235 | num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)]; | ||
236 | den = 0; | ||
237 | |||
238 | /* lambda[i+1] for i even is the formal derivative | ||
239 | * lambda_pr of lambda[i] */ | ||
240 | for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) { | ||
241 | if (lambda[i + 1] != nn) { | ||
242 | den ^= alpha_to[rs_modnn(rs, lambda[i + 1] + | ||
243 | i * root[j])]; | ||
244 | } | ||
245 | } | ||
246 | /* Apply error to data */ | ||
247 | if (num1 != 0 && loc[j] >= pad) { | ||
248 | uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] + | ||
249 | index_of[num2] + | ||
250 | nn - index_of[den])]; | ||
251 | /* Store the error correction pattern, if a | ||
252 | * correction buffer is available */ | ||
253 | if (corr) { | ||
254 | corr[j] = cor; | ||
255 | } else { | ||
256 | /* If a data buffer is given and the | ||
257 | * error is inside the message, | ||
258 | * correct it */ | ||
259 | if (data && (loc[j] < (nn - nroots))) | ||
260 | data[loc[j] - pad] ^= cor; | ||
261 | } | ||
262 | } | ||
263 | } | ||
264 | |||
265 | finish: | ||
266 | if (eras_pos != NULL) { | ||
267 | for (i = 0; i < count; i++) | ||
268 | eras_pos[i] = loc[i] - pad; | ||
269 | } | ||
270 | return count; | ||
271 | |||
272 | } | ||
diff --git a/lib/reed_solomon/encode_rs.c b/lib/reed_solomon/encode_rs.c new file mode 100644 index 000000000000..237bf65ae886 --- /dev/null +++ b/lib/reed_solomon/encode_rs.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * lib/reed_solomon/encode_rs.c | ||
3 | * | ||
4 | * Overview: | ||
5 | * Generic Reed Solomon encoder / decoder library | ||
6 | * | ||
7 | * Copyright 2002, Phil Karn, KA9Q | ||
8 | * May be used under the terms of the GNU General Public License (GPL) | ||
9 | * | ||
10 | * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de) | ||
11 | * | ||
12 | * $Id: encode_rs.c,v 1.4 2004/10/22 15:41:47 gleixner Exp $ | ||
13 | * | ||
14 | */ | ||
15 | |||
16 | /* Generic data width independent code which is included by the | ||
17 | * wrappers. | ||
18 | * int encode_rsX (struct rs_control *rs, uintX_t *data, int len, uintY_t *par) | ||
19 | */ | ||
20 | { | ||
21 | int i, j, pad; | ||
22 | int nn = rs->nn; | ||
23 | int nroots = rs->nroots; | ||
24 | uint16_t *alpha_to = rs->alpha_to; | ||
25 | uint16_t *index_of = rs->index_of; | ||
26 | uint16_t *genpoly = rs->genpoly; | ||
27 | uint16_t fb; | ||
28 | uint16_t msk = (uint16_t) rs->nn; | ||
29 | |||
30 | /* Check length parameter for validity */ | ||
31 | pad = nn - nroots - len; | ||
32 | if (pad < 0 || pad >= nn) | ||
33 | return -ERANGE; | ||
34 | |||
35 | for (i = 0; i < len; i++) { | ||
36 | fb = index_of[((((uint16_t) data[i])^invmsk) & msk) ^ par[0]]; | ||
37 | /* feedback term is non-zero */ | ||
38 | if (fb != nn) { | ||
39 | for (j = 1; j < nroots; j++) { | ||
40 | par[j] ^= alpha_to[rs_modnn(rs, fb + | ||
41 | genpoly[nroots - j])]; | ||
42 | } | ||
43 | } | ||
44 | /* Shift */ | ||
45 | memmove(&par[0], &par[1], sizeof(uint16_t) * (nroots - 1)); | ||
46 | if (fb != nn) { | ||
47 | par[nroots - 1] = alpha_to[rs_modnn(rs, | ||
48 | fb + genpoly[0])]; | ||
49 | } else { | ||
50 | par[nroots - 1] = 0; | ||
51 | } | ||
52 | } | ||
53 | return 0; | ||
54 | } | ||
diff --git a/lib/reed_solomon/reed_solomon.c b/lib/reed_solomon/reed_solomon.c new file mode 100644 index 000000000000..6604e3b1940c --- /dev/null +++ b/lib/reed_solomon/reed_solomon.c | |||
@@ -0,0 +1,335 @@ | |||
1 | /* | ||
2 | * lib/reed_solomon/rslib.c | ||
3 | * | ||
4 | * Overview: | ||
5 | * Generic Reed Solomon encoder / decoder library | ||
6 | * | ||
7 | * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de) | ||
8 | * | ||
9 | * Reed Solomon code lifted from reed solomon library written by Phil Karn | ||
10 | * Copyright 2002 Phil Karn, KA9Q | ||
11 | * | ||
12 | * $Id: rslib.c,v 1.5 2004/10/22 15:41:47 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 | * Description: | ||
19 | * | ||
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 | ||
30 | * on exit. | ||
31 | * The encoding puts the calculated syndrome into a given syndrome | ||
32 | * buffer. | ||
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. | ||
38 | * | ||
39 | */ | ||
40 | |||
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> | ||
48 | |||
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); | ||
53 | |||
54 | /** | ||
55 | * rs_init - Initialize a Reed-Solomon codec | ||
56 | * | ||
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) | ||
62 | * | ||
63 | * Allocate a control structure and the polynom arrays for faster | ||
64 | * en/decoding. Fill the arrays according to the given parameters | ||
65 | */ | ||
66 | static struct rs_control *rs_init(int symsize, int gfpoly, int fcr, | ||
67 | int prim, int nroots) | ||
68 | { | ||
69 | struct rs_control *rs; | ||
70 | int i, j, sr, root, iprim; | ||
71 | |||
72 | /* Allocate the control structure */ | ||
73 | rs = kmalloc(sizeof (struct rs_control), GFP_KERNEL); | ||
74 | if (rs == NULL) | ||
75 | return NULL; | ||
76 | |||
77 | INIT_LIST_HEAD(&rs->list); | ||
78 | |||
79 | rs->mm = symsize; | ||
80 | rs->nn = (1 << symsize) - 1; | ||
81 | rs->fcr = fcr; | ||
82 | rs->prim = prim; | ||
83 | rs->nroots = nroots; | ||
84 | rs->gfpoly = gfpoly; | ||
85 | |||
86 | /* Allocate the arrays */ | ||
87 | rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); | ||
88 | if (rs->alpha_to == NULL) | ||
89 | goto errrs; | ||
90 | |||
91 | rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), GFP_KERNEL); | ||
92 | if (rs->index_of == NULL) | ||
93 | goto erralp; | ||
94 | |||
95 | rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), GFP_KERNEL); | ||
96 | if(rs->genpoly == NULL) | ||
97 | goto erridx; | ||
98 | |||
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 */ | ||
102 | sr = 1; | ||
103 | for (i = 0; i < rs->nn; i++) { | ||
104 | rs->index_of[sr] = i; | ||
105 | rs->alpha_to[i] = sr; | ||
106 | sr <<= 1; | ||
107 | if (sr & (1 << symsize)) | ||
108 | sr ^= gfpoly; | ||
109 | sr &= rs->nn; | ||
110 | } | ||
111 | /* If it's not primitive, exit */ | ||
112 | if(sr != 1) | ||
113 | goto errpol; | ||
114 | |||
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; | ||
119 | |||
120 | /* Form RS code generator polynomial from its roots */ | ||
121 | rs->genpoly[0] = 1; | ||
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)]; | ||
130 | } else | ||
131 | rs->genpoly[j] = rs->genpoly[j - 1]; | ||
132 | } | ||
133 | /* rs->genpoly[0] can never be zero */ | ||
134 | rs->genpoly[0] = | ||
135 | rs->alpha_to[rs_modnn(rs, | ||
136 | rs->index_of[rs->genpoly[0]] + root)]; | ||
137 | } | ||
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]]; | ||
141 | return rs; | ||
142 | |||
143 | /* Error exit */ | ||
144 | errpol: | ||
145 | kfree(rs->genpoly); | ||
146 | erridx: | ||
147 | kfree(rs->index_of); | ||
148 | erralp: | ||
149 | kfree(rs->alpha_to); | ||
150 | errrs: | ||
151 | kfree(rs); | ||
152 | return NULL; | ||
153 | } | ||
154 | |||
155 | |||
156 | /** | ||
157 | * free_rs - Free the rs control structure, if its not longer used | ||
158 | * | ||
159 | * @rs: the control structure which is not longer used by the | ||
160 | * caller | ||
161 | */ | ||
162 | void free_rs(struct rs_control *rs) | ||
163 | { | ||
164 | down(&rslistlock); | ||
165 | rs->users--; | ||
166 | if(!rs->users) { | ||
167 | list_del(&rs->list); | ||
168 | kfree(rs->alpha_to); | ||
169 | kfree(rs->index_of); | ||
170 | kfree(rs->genpoly); | ||
171 | kfree(rs); | ||
172 | } | ||
173 | up(&rslistlock); | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * init_rs - Find a matching or allocate a new rs control structure | ||
178 | * | ||
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 | ||
182 | * must be primitive; | ||
183 | * @fcr: the first consecutive root of the rs code generator polynomial | ||
184 | * in index form | ||
185 | * @prim: primitive element to generate polynomial roots | ||
186 | * @nroots: RS code generator polynomial degree (number of roots) | ||
187 | */ | ||
188 | struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, | ||
189 | int nroots) | ||
190 | { | ||
191 | struct list_head *tmp; | ||
192 | struct rs_control *rs; | ||
193 | |||
194 | /* Sanity checks */ | ||
195 | if (symsize < 1) | ||
196 | return NULL; | ||
197 | if (fcr < 0 || fcr >= (1<<symsize)) | ||
198 | return NULL; | ||
199 | if (prim <= 0 || prim >= (1<<symsize)) | ||
200 | return NULL; | ||
201 | if (nroots < 0 || nroots >= (1<<symsize) || nroots > 8) | ||
202 | return NULL; | ||
203 | |||
204 | down(&rslistlock); | ||
205 | |||
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) | ||
210 | continue; | ||
211 | if (gfpoly != rs->gfpoly) | ||
212 | continue; | ||
213 | if (fcr != rs->fcr) | ||
214 | continue; | ||
215 | if (prim != rs->prim) | ||
216 | continue; | ||
217 | if (nroots != rs->nroots) | ||
218 | continue; | ||
219 | /* We have a matching one already */ | ||
220 | rs->users++; | ||
221 | goto out; | ||
222 | } | ||
223 | |||
224 | /* Create a new one */ | ||
225 | rs = rs_init(symsize, gfpoly, fcr, prim, nroots); | ||
226 | if (rs) { | ||
227 | rs->users = 1; | ||
228 | list_add(&rs->list, &rslist); | ||
229 | } | ||
230 | out: | ||
231 | up(&rslistlock); | ||
232 | return rs; | ||
233 | } | ||
234 | |||
235 | #ifdef CONFIG_REED_SOLOMON_ENC8 | ||
236 | /** | ||
237 | * encode_rs8 - Calculate the parity for data values (8bit data width) | ||
238 | * | ||
239 | * @rs: the rs control structure | ||
240 | * @data: data field of a given type | ||
241 | * @len: data length | ||
242 | * @par: parity data, must be initialized by caller (usually all 0) | ||
243 | * @invmsk: invert data mask (will be xored on data) | ||
244 | * | ||
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. | ||
248 | */ | ||
249 | int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par, | ||
250 | uint16_t invmsk) | ||
251 | { | ||
252 | #include "encode_rs.c" | ||
253 | } | ||
254 | EXPORT_SYMBOL_GPL(encode_rs8); | ||
255 | #endif | ||
256 | |||
257 | #ifdef CONFIG_REED_SOLOMON_DEC8 | ||
258 | /** | ||
259 | * decode_rs8 - Decode codeword (8bit data width) | ||
260 | * | ||
261 | * @rs: the rs control structure | ||
262 | * @data: data field of a given type | ||
263 | * @par: received parity data field | ||
264 | * @len: data length | ||
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 | ||
270 | * | ||
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. | ||
274 | */ | ||
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, | ||
277 | uint16_t *corr) | ||
278 | { | ||
279 | #include "decode_rs.c" | ||
280 | } | ||
281 | EXPORT_SYMBOL_GPL(decode_rs8); | ||
282 | #endif | ||
283 | |||
284 | #ifdef CONFIG_REED_SOLOMON_ENC16 | ||
285 | /** | ||
286 | * encode_rs16 - Calculate the parity for data values (16bit data width) | ||
287 | * | ||
288 | * @rs: the rs control structure | ||
289 | * @data: data field of a given type | ||
290 | * @len: data length | ||
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!) | ||
293 | * | ||
294 | * Each field in the data array contains up to symbol size bits of valid data. | ||
295 | */ | ||
296 | int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par, | ||
297 | uint16_t invmsk) | ||
298 | { | ||
299 | #include "encode_rs.c" | ||
300 | } | ||
301 | EXPORT_SYMBOL_GPL(encode_rs16); | ||
302 | #endif | ||
303 | |||
304 | #ifdef CONFIG_REED_SOLOMON_DEC16 | ||
305 | /** | ||
306 | * decode_rs16 - Decode codeword (16bit data width) | ||
307 | * | ||
308 | * @rs: the rs control structure | ||
309 | * @data: data field of a given type | ||
310 | * @par: received parity data field | ||
311 | * @len: data length | ||
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 | ||
317 | * | ||
318 | * Each field in the data array contains up to symbol size bits of valid data. | ||
319 | */ | ||
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, | ||
322 | uint16_t *corr) | ||
323 | { | ||
324 | #include "decode_rs.c" | ||
325 | } | ||
326 | EXPORT_SYMBOL_GPL(decode_rs16); | ||
327 | #endif | ||
328 | |||
329 | EXPORT_SYMBOL_GPL(init_rs); | ||
330 | EXPORT_SYMBOL_GPL(free_rs); | ||
331 | |||
332 | MODULE_LICENSE("GPL"); | ||
333 | MODULE_DESCRIPTION("Reed Solomon encoder/decoder"); | ||
334 | MODULE_AUTHOR("Phil Karn, Thomas Gleixner"); | ||
335 | |||