1 /* 2 * Generic Reed Solomon encoder / decoder library 3 * 4 * Copyright (C) 2004 Thomas Gleixner ([email protected]) 5 * 6 * RS code lifted from reed solomon library written by Phil Karn 7 * Copyright 2002 Phil Karn, KA9Q 8 * 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> 18 #include <linux/types.h> /* for gfp_t */ 19 #include <linux/gfp.h> /* for GFP_KERNEL */ 20 21 /** 22 * struct rs_control - rs control structure 23 * 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 28 * @genpoly: Generator polynomial 29 * @nroots: Number of generator roots = number of parity symbols 30 * @fcr: First consecutive root, index form 31 * @prim: Primitive element, index form 32 * @iprim: prim-th root of 1, index form 33 * @gfpoly: The primitive generator polynominal 34 * @gffunc: Function to generate the field, if non-canonical representation 35 * @users: Users of this structure 36 * @list: List entry for the rs control list 37 */ 38 struct rs_control { 39 int mm; 40 int nn; 41 uint16_t *alpha_to; 42 uint16_t *index_of; 43 uint16_t *genpoly; 44 int nroots; 45 int fcr; 46 int prim; 47 int iprim; 48 int gfpoly; 49 int (*gffunc)(int); 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 56 int 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 60 int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len, 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 67 int 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 71 int 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 */ 77 struct 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 */ 93 static 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 99 struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int), 100 int fcr, int prim, int nroots); 101 102 /* Release a rs control structure */ 103 void 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 111 * rs->mm = number of bits per symbol 112 * rs->nn = (2^rs->mm) - 1 113 * 114 * Simple arithmetic modulo would return a wrong result for values 115 * >= 3 * rs->nn 116 */ 117 static 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 127