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 | |
| 49 | /* This list holds all currently allocated rs control structures */ |
| 50 | static LIST_HEAD (rslist); |
| 51 | /* Protection for the list */ |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 52 | static DEFINE_MUTEX(rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 54 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | * rs_init - Initialize a Reed-Solomon codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * @symsize: symbol size, bits (1-8) |
| 57 | * @gfpoly: Field generator polynomial coefficients |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 58 | * @gffunc: Field generator function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 62 | * @gfp: GFP_ flags for allocations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | * |
| 64 | * Allocate a control structure and the polynom arrays for faster |
Randy Dunlap | 9dc6557 | 2006-06-25 05:49:14 -0700 | [diff] [blame] | 65 | * en/decoding. Fill the arrays according to the given parameters. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | */ |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 67 | static struct rs_control *rs_init(int symsize, int gfpoly, int (*gffunc)(int), |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 68 | int fcr, int prim, int nroots, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | struct rs_control *rs; |
| 71 | int i, j, sr, root, iprim; |
| 72 | |
| 73 | /* Allocate the control structure */ |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 74 | rs = kmalloc(sizeof(*rs), gfp); |
| 75 | if (!rs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | return NULL; |
| 77 | |
| 78 | INIT_LIST_HEAD(&rs->list); |
| 79 | |
| 80 | rs->mm = symsize; |
| 81 | rs->nn = (1 << symsize) - 1; |
| 82 | rs->fcr = fcr; |
| 83 | rs->prim = prim; |
| 84 | rs->nroots = nroots; |
| 85 | rs->gfpoly = gfpoly; |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 86 | rs->gffunc = gffunc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | /* Allocate the arrays */ |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 89 | rs->alpha_to = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (rs->alpha_to == NULL) |
| 91 | goto errrs; |
| 92 | |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 93 | rs->index_of = kmalloc(sizeof(uint16_t) * (rs->nn + 1), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (rs->index_of == NULL) |
| 95 | goto erralp; |
| 96 | |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 97 | rs->genpoly = kmalloc(sizeof(uint16_t) * (rs->nroots + 1), gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | if(rs->genpoly == NULL) |
| 99 | goto erridx; |
| 100 | |
| 101 | /* Generate Galois field lookup tables */ |
| 102 | rs->index_of[0] = rs->nn; /* log(zero) = -inf */ |
| 103 | rs->alpha_to[rs->nn] = 0; /* alpha**-inf = 0 */ |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 104 | if (gfpoly) { |
| 105 | sr = 1; |
| 106 | for (i = 0; i < rs->nn; i++) { |
| 107 | rs->index_of[sr] = i; |
| 108 | rs->alpha_to[i] = sr; |
| 109 | sr <<= 1; |
| 110 | if (sr & (1 << symsize)) |
| 111 | sr ^= gfpoly; |
| 112 | sr &= rs->nn; |
| 113 | } |
| 114 | } else { |
| 115 | sr = gffunc(0); |
| 116 | for (i = 0; i < rs->nn; i++) { |
| 117 | rs->index_of[sr] = i; |
| 118 | rs->alpha_to[i] = sr; |
| 119 | sr = gffunc(sr); |
| 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | /* If it's not primitive, exit */ |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 123 | if(sr != rs->alpha_to[0]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | goto errpol; |
| 125 | |
| 126 | /* Find prim-th root of 1, used in decoding */ |
| 127 | for(iprim = 1; (iprim % prim) != 0; iprim += rs->nn); |
| 128 | /* prim-th root of 1, index form */ |
| 129 | rs->iprim = iprim / prim; |
| 130 | |
| 131 | /* Form RS code generator polynomial from its roots */ |
| 132 | rs->genpoly[0] = 1; |
| 133 | for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) { |
| 134 | rs->genpoly[i + 1] = 1; |
| 135 | /* Multiply rs->genpoly[] by @**(root + x) */ |
| 136 | for (j = i; j > 0; j--) { |
| 137 | if (rs->genpoly[j] != 0) { |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 138 | rs->genpoly[j] = rs->genpoly[j -1] ^ |
| 139 | rs->alpha_to[rs_modnn(rs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | rs->index_of[rs->genpoly[j]] + root)]; |
| 141 | } else |
| 142 | rs->genpoly[j] = rs->genpoly[j - 1]; |
| 143 | } |
| 144 | /* rs->genpoly[0] can never be zero */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 145 | rs->genpoly[0] = |
| 146 | rs->alpha_to[rs_modnn(rs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | rs->index_of[rs->genpoly[0]] + root)]; |
| 148 | } |
| 149 | /* convert rs->genpoly[] to index form for quicker encoding */ |
| 150 | for (i = 0; i <= nroots; i++) |
| 151 | rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; |
| 152 | return rs; |
| 153 | |
| 154 | /* Error exit */ |
| 155 | errpol: |
| 156 | kfree(rs->genpoly); |
| 157 | erridx: |
| 158 | kfree(rs->index_of); |
| 159 | erralp: |
| 160 | kfree(rs->alpha_to); |
| 161 | errrs: |
| 162 | kfree(rs); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 167 | /** |
Randy Dunlap | 9dc6557 | 2006-06-25 05:49:14 -0700 | [diff] [blame] | 168 | * 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] | 169 | * @rs: the control structure which is not longer used by the |
| 170 | * caller |
| 171 | */ |
| 172 | void free_rs(struct rs_control *rs) |
| 173 | { |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 174 | mutex_lock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | rs->users--; |
| 176 | if(!rs->users) { |
| 177 | list_del(&rs->list); |
| 178 | kfree(rs->alpha_to); |
| 179 | kfree(rs->index_of); |
| 180 | kfree(rs->genpoly); |
| 181 | kfree(rs); |
| 182 | } |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 183 | mutex_unlock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 185 | EXPORT_SYMBOL_GPL(free_rs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 187 | /** |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 188 | * init_rs_internal - Find a matching or allocate a new rs control structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * @symsize: the symbol size (number of bits) |
| 190 | * @gfpoly: the extended Galois field generator polynomial coefficients, |
| 191 | * with the 0th coefficient in the low order bit. The polynomial |
| 192 | * must be primitive; |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 193 | * @gffunc: pointer to function to generate the next field element, |
| 194 | * or the multiplicative identity element if given 0. Used |
| 195 | * instead of gfpoly if gfpoly is 0 |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame^] | 196 | * @fcr: the first consecutive root of the rs code generator polynomial |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | * in index form |
| 198 | * @prim: primitive element to generate polynomial roots |
| 199 | * @nroots: RS code generator polynomial degree (number of roots) |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 200 | * @gfp: GFP_ flags for allocations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | */ |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 202 | static struct rs_control *init_rs_internal(int symsize, int gfpoly, |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 203 | int (*gffunc)(int), int fcr, |
| 204 | int prim, int nroots, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 206 | struct list_head *tmp; |
| 207 | struct rs_control *rs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | /* Sanity checks */ |
| 210 | if (symsize < 1) |
| 211 | return NULL; |
| 212 | if (fcr < 0 || fcr >= (1<<symsize)) |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame^] | 213 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (prim <= 0 || prim >= (1<<symsize)) |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame^] | 215 | return NULL; |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 216 | if (nroots < 0 || nroots >= (1<<symsize)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return NULL; |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 218 | |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 219 | mutex_lock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | /* Walk through the list and look for a matching entry */ |
| 222 | list_for_each(tmp, &rslist) { |
| 223 | rs = list_entry(tmp, struct rs_control, list); |
| 224 | if (symsize != rs->mm) |
| 225 | continue; |
| 226 | if (gfpoly != rs->gfpoly) |
| 227 | continue; |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 228 | if (gffunc != rs->gffunc) |
| 229 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | if (fcr != rs->fcr) |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 231 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | if (prim != rs->prim) |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 233 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | if (nroots != rs->nroots) |
| 235 | continue; |
| 236 | /* We have a matching one already */ |
| 237 | rs->users++; |
| 238 | goto out; |
| 239 | } |
| 240 | |
| 241 | /* Create a new one */ |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 242 | rs = rs_init(symsize, gfpoly, gffunc, fcr, prim, nroots, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | if (rs) { |
| 244 | rs->users = 1; |
| 245 | list_add(&rs->list, &rslist); |
| 246 | } |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 247 | out: |
Arjan van de Ven | 97d1f15 | 2006-03-23 03:00:24 -0800 | [diff] [blame] | 248 | mutex_unlock(&rslistlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return rs; |
| 250 | } |
| 251 | |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 252 | /** |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 253 | * init_rs_gfp - Find a matching or allocate a new rs control structure |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 254 | * @symsize: the symbol size (number of bits) |
| 255 | * @gfpoly: the extended Galois field generator polynomial coefficients, |
| 256 | * with the 0th coefficient in the low order bit. The polynomial |
| 257 | * must be primitive; |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame^] | 258 | * @fcr: the first consecutive root of the rs code generator polynomial |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 259 | * in index form |
| 260 | * @prim: primitive element to generate polynomial roots |
| 261 | * @nroots: RS code generator polynomial degree (number of roots) |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 262 | * @gfp: GFP_ flags for allocations |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 263 | */ |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 264 | struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim, |
| 265 | int nroots, gfp_t gfp) |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 266 | { |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 267 | return init_rs_internal(symsize, gfpoly, NULL, fcr, prim, nroots, gfp); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 268 | } |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 269 | EXPORT_SYMBOL_GPL(init_rs_gfp); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 270 | |
| 271 | /** |
| 272 | * init_rs_non_canonical - Find a matching or allocate a new rs control |
| 273 | * structure, for fields with non-canonical |
| 274 | * representation |
| 275 | * @symsize: the symbol size (number of bits) |
| 276 | * @gffunc: pointer to function to generate the next field element, |
| 277 | * or the multiplicative identity element if given 0. Used |
| 278 | * instead of gfpoly if gfpoly is 0 |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame^] | 279 | * @fcr: the first consecutive root of the rs code generator polynomial |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 280 | * in index form |
| 281 | * @prim: primitive element to generate polynomial roots |
| 282 | * @nroots: RS code generator polynomial degree (number of roots) |
| 283 | */ |
| 284 | struct rs_control *init_rs_non_canonical(int symsize, int (*gffunc)(int), |
Thomas Gleixner | cc4b86e4 | 2018-04-22 18:23:48 +0200 | [diff] [blame^] | 285 | int fcr, int prim, int nroots) |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 286 | { |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 287 | return init_rs_internal(symsize, 0, gffunc, fcr, prim, nroots, |
| 288 | GFP_KERNEL); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 289 | } |
Thomas Gleixner | 83a530e | 2018-04-22 18:23:46 +0200 | [diff] [blame] | 290 | EXPORT_SYMBOL_GPL(init_rs_non_canonical); |
Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | #ifdef CONFIG_REED_SOLOMON_ENC8 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 293 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | * encode_rs8 - Calculate the parity for data values (8bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | * @rs: the rs control structure |
| 296 | * @data: data field of a given type |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 297 | * @len: data length |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | * @par: parity data, must be initialized by caller (usually all 0) |
| 299 | * @invmsk: invert data mask (will be xored on data) |
| 300 | * |
| 301 | * The parity uses a uint16_t data type to enable |
| 302 | * symbol size > 8. The calling code must take care of encoding of the |
| 303 | * syndrome result for storage itself. |
| 304 | */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 305 | 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] | 306 | uint16_t invmsk) |
| 307 | { |
| 308 | #include "encode_rs.c" |
| 309 | } |
| 310 | EXPORT_SYMBOL_GPL(encode_rs8); |
| 311 | #endif |
| 312 | |
| 313 | #ifdef CONFIG_REED_SOLOMON_DEC8 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 314 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | * decode_rs8 - Decode codeword (8bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | * @rs: the rs control structure |
| 317 | * @data: data field of a given type |
| 318 | * @par: received parity data field |
| 319 | * @len: data length |
| 320 | * @s: syndrome data field (if NULL, syndrome is calculated) |
| 321 | * @no_eras: number of erasures |
| 322 | * @eras_pos: position of erasures, can be NULL |
| 323 | * @invmsk: invert data mask (will be xored on data, not on parity!) |
| 324 | * @corr: buffer to store correction bitmask on eras_pos |
| 325 | * |
| 326 | * The syndrome and parity uses a uint16_t data type to enable |
| 327 | * symbol size > 8. The calling code must take care of decoding of the |
| 328 | * syndrome result and the received parity before calling this code. |
Jörn Engel | eb68450 | 2007-10-20 23:16:32 +0200 | [diff] [blame] | 329 | * Returns the number of corrected bits or -EBADMSG for uncorrectable errors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | */ |
| 331 | 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] | 332 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | uint16_t *corr) |
| 334 | { |
| 335 | #include "decode_rs.c" |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(decode_rs8); |
| 338 | #endif |
| 339 | |
| 340 | #ifdef CONFIG_REED_SOLOMON_ENC16 |
| 341 | /** |
| 342 | * encode_rs16 - Calculate the parity for data values (16bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | * @rs: the rs control structure |
| 344 | * @data: data field of a given type |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 345 | * @len: data length |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * @par: parity data, must be initialized by caller (usually all 0) |
| 347 | * @invmsk: invert data mask (will be xored on data, not on parity!) |
| 348 | * |
| 349 | * Each field in the data array contains up to symbol size bits of valid data. |
| 350 | */ |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 351 | 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] | 352 | uint16_t invmsk) |
| 353 | { |
| 354 | #include "encode_rs.c" |
| 355 | } |
| 356 | EXPORT_SYMBOL_GPL(encode_rs16); |
| 357 | #endif |
| 358 | |
| 359 | #ifdef CONFIG_REED_SOLOMON_DEC16 |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 360 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * decode_rs16 - Decode codeword (16bit data width) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | * @rs: the rs control structure |
| 363 | * @data: data field of a given type |
| 364 | * @par: received parity data field |
| 365 | * @len: data length |
| 366 | * @s: syndrome data field (if NULL, syndrome is calculated) |
| 367 | * @no_eras: number of erasures |
| 368 | * @eras_pos: position of erasures, can be NULL |
Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 369 | * @invmsk: invert data mask (will be xored on data, not on parity!) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | * @corr: buffer to store correction bitmask on eras_pos |
| 371 | * |
| 372 | * Each field in the data array contains up to symbol size bits of valid data. |
Jörn Engel | eb68450 | 2007-10-20 23:16:32 +0200 | [diff] [blame] | 373 | * Returns the number of corrected bits or -EBADMSG for uncorrectable errors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | */ |
| 375 | 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] | 376 | uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | uint16_t *corr) |
| 378 | { |
| 379 | #include "decode_rs.c" |
| 380 | } |
| 381 | EXPORT_SYMBOL_GPL(decode_rs16); |
| 382 | #endif |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | MODULE_LICENSE("GPL"); |
| 385 | MODULE_DESCRIPTION("Reed Solomon encoder/decoder"); |
| 386 | MODULE_AUTHOR("Phil Karn, Thomas Gleixner"); |
| 387 | |