blob: ace25acfdc97375d0d403afb4bf12c08443e6309 [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>
23
Thomas Gleixner03ead842005-11-07 11:15:37 +000024/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * struct rs_control - rs control structure
Thomas Gleixner03ead842005-11-07 11:15:37 +000026 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * @mm: Bits per symbol
28 * @nn: Symbols per block (= (1<<mm)-1)
29 * @alpha_to: log lookup table
30 * @index_of: Antilog lookup table
Thomas Gleixner03ead842005-11-07 11:15:37 +000031 * @genpoly: Generator polynomial
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * @nroots: Number of generator roots = number of parity symbols
33 * @fcr: First consecutive root, index form
Thomas Gleixner03ead842005-11-07 11:15:37 +000034 * @prim: Primitive element, index form
35 * @iprim: prim-th root of 1, index form
36 * @gfpoly: The primitive generator polynominal
37 * @users: Users of this structure
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * @list: List entry for the rs control list
39*/
40struct rs_control {
41 int mm;
42 int nn;
43 uint16_t *alpha_to;
44 uint16_t *index_of;
45 uint16_t *genpoly;
46 int nroots;
47 int fcr;
48 int prim;
49 int iprim;
50 int gfpoly;
51 int users;
52 struct list_head list;
53};
54
55/* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */
56#ifdef CONFIG_REED_SOLOMON_ENC8
57int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par,
58 uint16_t invmsk);
59#endif
60#ifdef CONFIG_REED_SOLOMON_DEC8
Thomas Gleixner03ead842005-11-07 11:15:37 +000061int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
63 uint16_t *corr);
64#endif
65
66/* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */
67#ifdef CONFIG_REED_SOLOMON_ENC16
68int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par,
69 uint16_t invmsk);
70#endif
71#ifdef CONFIG_REED_SOLOMON_DEC16
72int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len,
73 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk,
74 uint16_t *corr);
75#endif
76
77/* Create or get a matching rs control structure */
Thomas Gleixner03ead842005-11-07 11:15:37 +000078struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int nroots);
80
81/* Release a rs control structure */
82void free_rs(struct rs_control *rs);
83
84/** modulo replacement for galois field arithmetics
85 *
86 * @rs: the rs control structure
87 * @x: the value to reduce
88 *
89 * where
Thomas Gleixner03ead842005-11-07 11:15:37 +000090 * rs->mm = number of bits per symbol
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * rs->nn = (2^rs->mm) - 1
Thomas Gleixner03ead842005-11-07 11:15:37 +000092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Simple arithmetic modulo would return a wrong result for values
94 * >= 3 * rs->nn
95*/
96static inline int rs_modnn(struct rs_control *rs, int x)
97{
98 while (x >= rs->nn) {
99 x -= rs->nn;
100 x = (x >> rs->mm) + (x & rs->nn);
101 }
102 return x;
103}
104
105#endif