blob: a596abaf2e56153b2138b763a0418ec438881cc4 [file] [log] [blame]
Thomas Gleixner03ead842005-11-07 11:15:37 +00001/*
Thomas Gleixner3413e182018-04-22 18:23:49 +02002 * Generic Reed Solomon encoder / decoder library
Thomas Gleixner03ead842005-11-07 11:15:37 +00003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
5 *
6 * RS code lifted from reed solomon library written by Phil Karn
7 * Copyright 2002 Phil Karn, KA9Q
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#ifndef _RSLIB_H_
15#define _RSLIB_H_
16
17#include <linux/list.h>
Thomas Gleixner83a530e2018-04-22 18:23:46 +020018#include <linux/types.h> /* for gfp_t */
19#include <linux/gfp.h> /* for GFP_KERNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Thomas Gleixner03ead842005-11-07 11:15:37 +000021/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * struct rs_control - rs control structure
Thomas Gleixner03ead842005-11-07 11:15:37 +000023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * @mm: Bits per symbol
25 * @nn: Symbols per block (= (1<<mm)-1)
26 * @alpha_to: log lookup table
27 * @index_of: Antilog lookup table
Thomas Gleixner03ead842005-11-07 11:15:37 +000028 * @genpoly: Generator polynomial
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * @nroots: Number of generator roots = number of parity symbols
30 * @fcr: First consecutive root, index form
Thomas Gleixner03ead842005-11-07 11:15:37 +000031 * @prim: Primitive element, index form
32 * @iprim: prim-th root of 1, index form
33 * @gfpoly: The primitive generator polynominal
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020034 * @gffunc: Function to generate the field, if non-canonical representation
Thomas Gleixner03ead842005-11-07 11:15:37 +000035 * @users: Users of this structure
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * @list: List entry for the rs control list
37*/
38struct rs_control {
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020039 int mm;
40 int nn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 uint16_t *alpha_to;
42 uint16_t *index_of;
43 uint16_t *genpoly;
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020044 int nroots;
45 int fcr;
46 int prim;
47 int iprim;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int gfpoly;
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020049 int (*gffunc)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int users;
51 struct list_head list;
52};
53
54/* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
55#ifdef CONFIG_REED_SOLOMON_ENC8
56int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
57 uint16_t invmsk);
58#endif
59#ifdef CONFIG_REED_SOLOMON_DEC8
Thomas Gleixner03ead842005-11-07 11:15:37 +000060int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
62 uint16_t *corr);
63#endif
64
65/* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
66#ifdef CONFIG_REED_SOLOMON_ENC16
67int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
68 uint16_t invmsk);
69#endif
70#ifdef CONFIG_REED_SOLOMON_DEC16
71int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
72 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
73 uint16_t *corr);
74#endif
75
76/* Create or get a matching rs control structure */
Thomas Gleixner83a530e2018-04-22 18:23:46 +020077struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
78 int nroots, gfp_t gfp);
79
80/**
81 * init_rs - Create a RS control struct and initialize it
82 * @symsize: the symbol size (number of bits)
83 * @gfpoly: the extended Galois field generator polynomial coefficients,
84 * with the 0th coefficient in the low order bit. The polynomial
85 * must be primitive;
86 * @fcr: the first consecutive root of the rs code generator polynomial
87 * in index form
88 * @prim: primitive element to generate polynomial roots
89 * @nroots: RS code generator polynomial degree (number of roots)
90 *
91 * Allocations use GFP_KERNEL.
92 */
93static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr,
94 int prim, int nroots)
95{
96 return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL);
97}
98
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020099struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
Thomas Gleixner83a530e2018-04-22 18:23:46 +0200100 int fcr, int prim, int nroots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* Release a rs control structure */
103void free_rs(struct rs_control *rs);
104
105/** modulo replacement for galois field arithmetics
106 *
107 * @rs: the rs control structure
108 * @x: the value to reduce
109 *
110 * where
Thomas Gleixner03ead842005-11-07 11:15:37 +0000111 * rs->mm = number of bits per symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * rs->nn = (2^rs->mm) - 1
Thomas Gleixner03ead842005-11-07 11:15:37 +0000113 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Simple arithmetic modulo would return a wrong result for values
115 * >= 3 * rs->nn
116*/
117static inline int rs_modnn(struct rs_control *rs, int x)
118{
119 while (x >= rs->nn) {
120 x -= rs->nn;
121 x = (x >> rs->mm) + (x & rs->nn);
122 }
123 return x;
124}
125
126#endif