2018-04-23 00:23:50 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-11-07 19:15:37 +08:00
|
|
|
/*
|
2018-04-23 00:23:49 +08:00
|
|
|
* Generic Reed Solomon encoder / decoder library
|
2005-11-07 19:15:37 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
|
|
|
|
*
|
|
|
|
* RS code lifted from reed solomon library written by Phil Karn
|
|
|
|
* Copyright 2002 Phil Karn, KA9Q
|
|
|
|
*/
|
|
|
|
#ifndef _RSLIB_H_
|
|
|
|
#define _RSLIB_H_
|
|
|
|
|
|
|
|
#include <linux/list.h>
|
2018-04-23 00:23:46 +08:00
|
|
|
#include <linux/types.h> /* for gfp_t */
|
|
|
|
#include <linux/gfp.h> /* for GFP_KERNEL */
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-07 19:15:37 +08:00
|
|
|
/**
|
2018-04-23 00:23:53 +08:00
|
|
|
* struct rs_codec - rs codec data
|
2005-11-07 19:15:37 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* @mm: Bits per symbol
|
|
|
|
* @nn: Symbols per block (= (1<<mm)-1)
|
|
|
|
* @alpha_to: log lookup table
|
|
|
|
* @index_of: Antilog lookup table
|
2005-11-07 19:15:37 +08:00
|
|
|
* @genpoly: Generator polynomial
|
2005-04-17 06:20:36 +08:00
|
|
|
* @nroots: Number of generator roots = number of parity symbols
|
|
|
|
* @fcr: First consecutive root, index form
|
2005-11-07 19:15:37 +08:00
|
|
|
* @prim: Primitive element, index form
|
|
|
|
* @iprim: prim-th root of 1, index form
|
|
|
|
* @gfpoly: The primitive generator polynominal
|
2007-05-02 18:18:41 +08:00
|
|
|
* @gffunc: Function to generate the field, if non-canonical representation
|
2005-11-07 19:15:37 +08:00
|
|
|
* @users: Users of this structure
|
2018-04-23 00:23:53 +08:00
|
|
|
* @list: List entry for the rs codec list
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2018-04-23 00:23:53 +08:00
|
|
|
struct rs_codec {
|
2018-04-23 00:23:48 +08:00
|
|
|
int mm;
|
|
|
|
int nn;
|
2005-04-17 06:20:36 +08:00
|
|
|
uint16_t *alpha_to;
|
|
|
|
uint16_t *index_of;
|
|
|
|
uint16_t *genpoly;
|
2018-04-23 00:23:48 +08:00
|
|
|
int nroots;
|
|
|
|
int fcr;
|
|
|
|
int prim;
|
|
|
|
int iprim;
|
2005-04-17 06:20:36 +08:00
|
|
|
int gfpoly;
|
2007-05-02 18:18:41 +08:00
|
|
|
int (*gffunc)(int);
|
2005-04-17 06:20:36 +08:00
|
|
|
int users;
|
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
2018-04-23 00:23:53 +08:00
|
|
|
/**
|
|
|
|
* struct rs_control - rs control structure per instance
|
|
|
|
* @codec: The codec used for this instance
|
2018-04-23 00:23:55 +08:00
|
|
|
* @buffers: Internal scratch buffers used in calls to decode_rs()
|
2018-04-23 00:23:53 +08:00
|
|
|
*/
|
|
|
|
struct rs_control {
|
|
|
|
struct rs_codec *codec;
|
2018-04-23 00:23:55 +08:00
|
|
|
uint16_t buffers[0];
|
2018-04-23 00:23:53 +08:00
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
|
|
|
|
#ifdef CONFIG_REED_SOLOMON_ENC8
|
|
|
|
int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
|
|
|
|
uint16_t invmsk);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_REED_SOLOMON_DEC8
|
2005-11-07 19:15:37 +08:00
|
|
|
int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
|
2005-04-17 06:20:36 +08:00
|
|
|
uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
|
|
|
|
uint16_t *corr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
|
|
|
|
#ifdef CONFIG_REED_SOLOMON_ENC16
|
|
|
|
int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
|
|
|
|
uint16_t invmsk);
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_REED_SOLOMON_DEC16
|
|
|
|
int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
|
|
|
|
uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
|
|
|
|
uint16_t *corr);
|
|
|
|
#endif
|
|
|
|
|
2018-04-23 00:23:46 +08:00
|
|
|
struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
|
|
|
|
int nroots, gfp_t gfp);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* init_rs - Create a RS control struct and initialize it
|
|
|
|
* @symsize: the symbol size (number of bits)
|
|
|
|
* @gfpoly: the extended Galois field generator polynomial coefficients,
|
|
|
|
* with the 0th coefficient in the low order bit. The polynomial
|
|
|
|
* must be primitive;
|
|
|
|
* @fcr: the first consecutive root of the rs code generator polynomial
|
|
|
|
* in index form
|
|
|
|
* @prim: primitive element to generate polynomial roots
|
|
|
|
* @nroots: RS code generator polynomial degree (number of roots)
|
|
|
|
*
|
|
|
|
* Allocations use GFP_KERNEL.
|
|
|
|
*/
|
|
|
|
static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr,
|
|
|
|
int prim, int nroots)
|
|
|
|
{
|
|
|
|
return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL);
|
|
|
|
}
|
|
|
|
|
2007-05-02 18:18:41 +08:00
|
|
|
struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
|
2018-04-23 00:23:46 +08:00
|
|
|
int fcr, int prim, int nroots);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Release a rs control structure */
|
|
|
|
void free_rs(struct rs_control *rs);
|
|
|
|
|
|
|
|
/** modulo replacement for galois field arithmetics
|
|
|
|
*
|
2018-04-23 00:23:53 +08:00
|
|
|
* @rs: Pointer to the RS codec
|
2005-04-17 06:20:36 +08:00
|
|
|
* @x: the value to reduce
|
|
|
|
*
|
|
|
|
* where
|
2005-11-07 19:15:37 +08:00
|
|
|
* rs->mm = number of bits per symbol
|
2005-04-17 06:20:36 +08:00
|
|
|
* rs->nn = (2^rs->mm) - 1
|
2005-11-07 19:15:37 +08:00
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* Simple arithmetic modulo would return a wrong result for values
|
|
|
|
* >= 3 * rs->nn
|
|
|
|
*/
|
2018-04-23 00:23:53 +08:00
|
|
|
static inline int rs_modnn(struct rs_codec *rs, int x)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
while (x >= rs->nn) {
|
|
|
|
x -= rs->nn;
|
|
|
|
x = (x >> rs->mm) + (x & rs->nn);
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|