blob: d7bc66931032538ebb0e46b405767575d595a031 [file] [log] [blame]
Thomas Gleixner03ead842005-11-07 11:15:37 +00001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * include/linux/rslib.h
3 *
4 * Overview:
5 * Generic Reed Solomon encoder / decoder library
Thomas Gleixner03ead842005-11-07 11:15:37 +00006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 2004 Thomas Gleixner (tglx@linutronix.de)
8 *
9 * RS code lifted from reed solomon library written by Phil Karn
10 * Copyright 2002 Phil Karn, KA9Q
11 *
Thomas Gleixner03ead842005-11-07 11:15:37 +000012 * $Id: rslib.h,v 1.4 2005/11/07 11:14:52 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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
19#ifndef _RSLIB_H_
20#define _RSLIB_H_
21
22#include <linux/list.h>
Thomas Gleixner83a530e2018-04-22 18:23:46 +020023#include <linux/types.h> /* for gfp_t */
24#include <linux/gfp.h> /* for GFP_KERNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Thomas Gleixner03ead842005-11-07 11:15:37 +000026/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * struct rs_control - rs control structure
Thomas Gleixner03ead842005-11-07 11:15:37 +000028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * @mm: Bits per symbol
30 * @nn: Symbols per block (= (1<<mm)-1)
31 * @alpha_to: log lookup table
32 * @index_of: Antilog lookup table
Thomas Gleixner03ead842005-11-07 11:15:37 +000033 * @genpoly: Generator polynomial
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * @nroots: Number of generator roots = number of parity symbols
35 * @fcr: First consecutive root, index form
Thomas Gleixner03ead842005-11-07 11:15:37 +000036 * @prim: Primitive element, index form
37 * @iprim: prim-th root of 1, index form
38 * @gfpoly: The primitive generator polynominal
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020039 * @gffunc: Function to generate the field, if non-canonical representation
Thomas Gleixner03ead842005-11-07 11:15:37 +000040 * @users: Users of this structure
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * @list: List entry for the rs control list
42*/
43struct rs_control {
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020044 int mm;
45 int nn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 uint16_t *alpha_to;
47 uint16_t *index_of;
48 uint16_t *genpoly;
Thomas Gleixnercc4b86e42018-04-22 18:23:48 +020049 int nroots;
50 int fcr;
51 int prim;
52 int iprim;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int gfpoly;
Segher Boessenkoold7e5a542007-05-02 12:18:41 +020054 int (*gffunc)(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 int users;
56 struct list_head list;
57};
58
59/* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
60#ifdef CONFIG_REED_SOLOMON_ENC8
61int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
62 uint16_t invmsk);
63#endif
64#ifdef CONFIG_REED_SOLOMON_DEC8
Thomas Gleixner03ead842005-11-07 11:15:37 +000065int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
67 uint16_t *corr);
68#endif
69
70/* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
71#ifdef CONFIG_REED_SOLOMON_ENC16
72int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
73 uint16_t invmsk);
74#endif
75#ifdef CONFIG_REED_SOLOMON_DEC16
76int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
77 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
78 uint16_t *corr);
79#endif
80
81/* Create or get a matching rs control structure */
Thomas Gleixner83a530e2018-04-22 18:23:46 +020082struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim,
83 int nroots, gfp_t gfp);
84
85/**
86 * init_rs - Create a RS control struct and initialize it
87 * @symsize: the symbol size (number of bits)
88 * @gfpoly: the extended Galois field generator polynomial coefficients,
89 * with the 0th coefficient in the low order bit. The polynomial
90 * must be primitive;
91 * @fcr: the first consecutive root of the rs code generator polynomial
92 * in index form
93 * @prim: primitive element to generate polynomial roots
94 * @nroots: RS code generator polynomial degree (number of roots)
95 *
96 * Allocations use GFP_KERNEL.
97 */
98static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr,
99 int prim, int nroots)
100{
101 return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL);
102}
103
Segher Boessenkoold7e5a542007-05-02 12:18:41 +0200104struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
Thomas Gleixner83a530e2018-04-22 18:23:46 +0200105 int fcr, int prim, int nroots);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* Release a rs control structure */
108void free_rs(struct rs_control *rs);
109
110/** modulo replacement for galois field arithmetics
111 *
112 * @rs: the rs control structure
113 * @x: the value to reduce
114 *
115 * where
Thomas Gleixner03ead842005-11-07 11:15:37 +0000116 * rs->mm = number of bits per symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * rs->nn = (2^rs->mm) - 1
Thomas Gleixner03ead842005-11-07 11:15:37 +0000118 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * Simple arithmetic modulo would return a wrong result for values
120 * >= 3 * rs->nn
121*/
122static inline int rs_modnn(struct rs_control *rs, int x)
123{
124 while (x >= rs->nn) {
125 x -= rs->nn;
126 x = (x >> rs->mm) + (x & rs->nn);
127 }
128 return x;
129}
130
131#endif