Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * lib/reed_solomon/reed_solomon.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Overview: |
| 5 | * Generic Reed Solomon encoder / decoder library |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 12 | * $Id: rslib.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 19 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 28 | * Usually a module / driver should initialize the necessary |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * rs_control structure on module / driver init and release it |
| 30 | * on exit. |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 31 | * The encoding puts the calculated syndrome into a given syndrome |
| 32 | * buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 47 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/semaphore.h> |
| 49 | |
| 50 | /* This list holds all currently allocated rs control structures */ |
| 51 | static LIST_HEAD (rslist); |
| 52 | /* Protection for the list */ |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 53 | static DEFINE_MUTEX(rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 55 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * rs_init - Initialize a Reed-Solomon codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Randy Dunlap | 9dc6557 | 2006-06-25 05:49:14 -0700 | [diff] [blame] | 64 | * en/decoding. Fill the arrays according to the given parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 66 | static struct rs_control *rs_init(int symsize, int gfpoly, int fcr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 127 | rs->genpoly[j] = rs->genpoly[j -1] ^ |
| 128 | rs->alpha_to[rs_modnn(rs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 134 | rs->genpoly[0] = |
| 135 | rs->alpha_to[rs_modnn(rs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 156 | /** |
Randy Dunlap | 9dc6557 | 2006-06-25 05:49:14 -0700 | [diff] [blame] | 157 | * free_rs - Free the rs control structure, if it is no longer used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | * @rs: the control structure which is not longer used by the |
| 159 | * caller |
| 160 | */ |
| 161 | void free_rs(struct rs_control *rs) |
| 162 | { |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 163 | mutex_lock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | rs->users--; |
| 165 | if(!rs->users) { |
| 166 | list_del(&rs->list); |
| 167 | kfree(rs->alpha_to); |
| 168 | kfree(rs->index_of); |
| 169 | kfree(rs->genpoly); |
| 170 | kfree(rs); |
| 171 | } |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 172 | mutex_unlock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 175 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * init_rs - Find a matching or allocate a new rs control structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | * @symsize: the symbol size (number of bits) |
| 178 | * @gfpoly: the extended Galois field generator polynomial coefficients, |
| 179 | * with the 0th coefficient in the low order bit. The polynomial |
| 180 | * must be primitive; |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 181 | * @fcr: the first consecutive root of the rs code generator polynomial |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | * in index form |
| 183 | * @prim: primitive element to generate polynomial roots |
| 184 | * @nroots: RS code generator polynomial degree (number of roots) |
| 185 | */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 186 | struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | int nroots) |
| 188 | { |
| 189 | struct list_head *tmp; |
| 190 | struct rs_control *rs; |
| 191 | |
| 192 | /* Sanity checks */ |
| 193 | if (symsize < 1) |
| 194 | return NULL; |
| 195 | if (fcr < 0 || fcr >= (1<<symsize)) |
| 196 | return NULL; |
| 197 | if (prim <= 0 || prim >= (1<<symsize)) |
| 198 | return NULL; |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 199 | if (nroots < 0 || nroots >= (1<<symsize)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return NULL; |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 201 | |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 202 | mutex_lock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | /* Walk through the list and look for a matching entry */ |
| 205 | list_for_each(tmp, &rslist) { |
| 206 | rs = list_entry(tmp, struct rs_control, list); |
| 207 | if (symsize != rs->mm) |
| 208 | continue; |
| 209 | if (gfpoly != rs->gfpoly) |
| 210 | continue; |
| 211 | if (fcr != rs->fcr) |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 212 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (prim != rs->prim) |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 214 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (nroots != rs->nroots) |
| 216 | continue; |
| 217 | /* We have a matching one already */ |
| 218 | rs->users++; |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | /* Create a new one */ |
| 223 | rs = rs_init(symsize, gfpoly, fcr, prim, nroots); |
| 224 | if (rs) { |
| 225 | rs->users = 1; |
| 226 | list_add(&rs->list, &rslist); |
| 227 | } |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 228 | out: |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 229 | mutex_unlock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return rs; |
| 231 | } |
| 232 | |
| 233 | #ifdef CONFIG_REED_SOLOMON_ENC8 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 234 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | * encode_rs8 - Calculate the parity for data values (8bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | * @rs: the rs control structure |
| 237 | * @data: data field of a given type |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 238 | * @len: data length |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | * @par: parity data, must be initialized by caller (usually all 0) |
| 240 | * @invmsk: invert data mask (will be xored on data) |
| 241 | * |
| 242 | * The parity uses a uint16_t data type to enable |
| 243 | * symbol size > 8. The calling code must take care of encoding of the |
| 244 | * syndrome result for storage itself. |
| 245 | */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 246 | int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | uint16_t invmsk) |
| 248 | { |
| 249 | #include "encode_rs.c" |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(encode_rs8); |
| 252 | #endif |
| 253 | |
| 254 | #ifdef CONFIG_REED_SOLOMON_DEC8 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 255 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | * decode_rs8 - Decode codeword (8bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | * @rs: the rs control structure |
| 258 | * @data: data field of a given type |
| 259 | * @par: received parity data field |
| 260 | * @len: data length |
| 261 | * @s: syndrome data field (if NULL, syndrome is calculated) |
| 262 | * @no_eras: number of erasures |
| 263 | * @eras_pos: position of erasures, can be NULL |
| 264 | * @invmsk: invert data mask (will be xored on data, not on parity!) |
| 265 | * @corr: buffer to store correction bitmask on eras_pos |
| 266 | * |
| 267 | * The syndrome and parity uses a uint16_t data type to enable |
| 268 | * symbol size > 8. The calling code must take care of decoding of the |
| 269 | * syndrome result and the received parity before calling this code. |
| 270 | */ |
| 271 | int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len, |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 272 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | uint16_t *corr) |
| 274 | { |
| 275 | #include "decode_rs.c" |
| 276 | } |
| 277 | EXPORT_SYMBOL_GPL(decode_rs8); |
| 278 | #endif |
| 279 | |
| 280 | #ifdef CONFIG_REED_SOLOMON_ENC16 |
| 281 | /** |
| 282 | * encode_rs16 - Calculate the parity for data values (16bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * @rs: the rs control structure |
| 284 | * @data: data field of a given type |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 285 | * @len: data length |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | * @par: parity data, must be initialized by caller (usually all 0) |
| 287 | * @invmsk: invert data mask (will be xored on data, not on parity!) |
| 288 | * |
| 289 | * Each field in the data array contains up to symbol size bits of valid data. |
| 290 | */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 291 | int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | uint16_t invmsk) |
| 293 | { |
| 294 | #include "encode_rs.c" |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(encode_rs16); |
| 297 | #endif |
| 298 | |
| 299 | #ifdef CONFIG_REED_SOLOMON_DEC16 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 300 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | * decode_rs16 - Decode codeword (16bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * @rs: the rs control structure |
| 303 | * @data: data field of a given type |
| 304 | * @par: received parity data field |
| 305 | * @len: data length |
| 306 | * @s: syndrome data field (if NULL, syndrome is calculated) |
| 307 | * @no_eras: number of erasures |
| 308 | * @eras_pos: position of erasures, can be NULL |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 309 | * @invmsk: invert data mask (will be xored on data, not on parity!) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | * @corr: buffer to store correction bitmask on eras_pos |
| 311 | * |
| 312 | * Each field in the data array contains up to symbol size bits of valid data. |
| 313 | */ |
| 314 | int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len, |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 315 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | uint16_t *corr) |
| 317 | { |
| 318 | #include "decode_rs.c" |
| 319 | } |
| 320 | EXPORT_SYMBOL_GPL(decode_rs16); |
| 321 | #endif |
| 322 | |
| 323 | EXPORT_SYMBOL_GPL(init_rs); |
| 324 | EXPORT_SYMBOL_GPL(free_rs); |
| 325 | |
| 326 | MODULE_LICENSE("GPL"); |
| 327 | MODULE_DESCRIPTION("Reed Solomon encoder/decoder"); |
| 328 | MODULE_AUTHOR("Phil Karn, Thomas Gleixner"); |
| 329 | |