rslib: Cleanup top level comments
[linux-block.git] / include / linux / rslib.h
CommitLineData
03ead842 1/*
3413e189 2 * Generic Reed Solomon encoder / decoder library
03ead842 3 *
1da177e4
LT
4 * 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 *
1da177e4
LT
9 * 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>
83a530e1
TG
18#include <linux/types.h> /* for gfp_t */
19#include <linux/gfp.h> /* for GFP_KERNEL */
1da177e4 20
03ead842 21/**
1da177e4 22 * struct rs_control - rs control structure
03ead842 23 *
1da177e4
LT
24 * @mm: Bits per symbol
25 * @nn: Symbols per block (= (1<<mm)-1)
26 * @alpha_to: log lookup table
27 * @index_of: Antilog lookup table
03ead842 28 * @genpoly: Generator polynomial
1da177e4
LT
29 * @nroots: Number of generator roots = number of parity symbols
30 * @fcr: First consecutive root, index form
03ead842
TG
31 * @prim: Primitive element, index form
32 * @iprim: prim-th root of 1, index form
33 * @gfpoly: The primitive generator polynominal
d7e5a546 34 * @gffunc: Function to generate the field, if non-canonical representation
03ead842 35 * @users: Users of this structure
1da177e4
LT
36 * @list: List entry for the rs control list
37*/
38struct rs_control {
cc4b86e4
TG
39 int mm;
40 int nn;
1da177e4
LT
41 uint16_t *alpha_to;
42 uint16_t *index_of;
43 uint16_t *genpoly;
cc4b86e4
TG
44 int nroots;
45 int fcr;
46 int prim;
47 int iprim;
1da177e4 48 int gfpoly;
d7e5a546 49 int (*gffunc)(int);
1da177e4
LT
50 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
03ead842 60int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
1da177e4
LT
61 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 */
83a530e1
TG
77struct 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
d7e5a546 99struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int),
83a530e1 100 int fcr, int prim, int nroots);
1da177e4
LT
101
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
03ead842 111 * rs->mm = number of bits per symbol
1da177e4 112 * rs->nn = (2^rs->mm) - 1
03ead842 113 *
1da177e4
LT
114 * 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