1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Generic Reed Solomon encoder / decoder library 4 * 5 * Copyright (C) 2004 Thomas Gleixner ([email protected]) 6 * 7 * RS code lifted from reed solomon library written by Phil Karn 8 * Copyright 2002 Phil Karn, KA9Q 9 */ 10 #ifndef _RSLIB_H_ 11 #define _RSLIB_H_ 12 13 #include <linux/list.h> 14 #include <linux/types.h> /* for gfp_t */ 15 #include <linux/gfp.h> /* for GFP_KERNEL */ 16 17 /** 18 * struct rs_control - rs control structure 19 * 20 * @mm: Bits per symbol 21 * @nn: Symbols per block (= (1<<mm)-1) 22 * @alpha_to: log lookup table 23 * @index_of: Antilog lookup table 24 * @genpoly: Generator polynomial 25 * @nroots: Number of generator roots = number of parity symbols 26 * @fcr: First consecutive root, index form 27 * @prim: Primitive element, index form 28 * @iprim: prim-th root of 1, index form 29 * @gfpoly: The primitive generator polynominal 30 * @gffunc: Function to generate the field, if non-canonical representation 31 * @users: Users of this structure 32 * @list: List entry for the rs control list 33 */ 34 struct rs_control { 35 int mm; 36 int nn; 37 uint16_t *alpha_to; 38 uint16_t *index_of; 39 uint16_t *genpoly; 40 int nroots; 41 int fcr; 42 int prim; 43 int iprim; 44 int gfpoly; 45 int (*gffunc)(int); 46 int users; 47 struct list_head list; 48 }; 49 50 /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit */ 51 #ifdef CONFIG_REED_SOLOMON_ENC8 52 int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par, 53 uint16_t invmsk); 54 #endif 55 #ifdef CONFIG_REED_SOLOMON_DEC8 56 int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len, 57 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, 58 uint16_t *corr); 59 #endif 60 61 /* General purpose RS codec, 16-bit data width, symbol width 1-15 bit */ 62 #ifdef CONFIG_REED_SOLOMON_ENC16 63 int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par, 64 uint16_t invmsk); 65 #endif 66 #ifdef CONFIG_REED_SOLOMON_DEC16 67 int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len, 68 uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, 69 uint16_t *corr); 70 #endif 71 72 /* Create or get a matching rs control structure */ 73 struct rs_control *init_rs_gfp(int symsize, int gfpoly, int fcr, int prim, 74 int nroots, gfp_t gfp); 75 76 /** 77 * init_rs - Create a RS control struct and initialize it 78 * @symsize: the symbol size (number of bits) 79 * @gfpoly: the extended Galois field generator polynomial coefficients, 80 * with the 0th coefficient in the low order bit. The polynomial 81 * must be primitive; 82 * @fcr: the first consecutive root of the rs code generator polynomial 83 * in index form 84 * @prim: primitive element to generate polynomial roots 85 * @nroots: RS code generator polynomial degree (number of roots) 86 * 87 * Allocations use GFP_KERNEL. 88 */ 89 static inline struct rs_control *init_rs(int symsize, int gfpoly, int fcr, 90 int prim, int nroots) 91 { 92 return init_rs_gfp(symsize, gfpoly, fcr, prim, nroots, GFP_KERNEL); 93 } 94 95 struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int), 96 int fcr, int prim, int nroots); 97 98 /* Release a rs control structure */ 99 void free_rs(struct rs_control *rs); 100 101 /** modulo replacement for galois field arithmetics 102 * 103 * @rs: the rs control structure 104 * @x: the value to reduce 105 * 106 * where 107 * rs->mm = number of bits per symbol 108 * rs->nn = (2^rs->mm) - 1 109 * 110 * Simple arithmetic modulo would return a wrong result for values 111 * >= 3 * rs->nn 112 */ 113 static inline int rs_modnn(struct rs_control *rs, int x) 114 { 115 while (x >= rs->nn) { 116 x -= rs->nn; 117 x = (x >> rs->mm) + (x & rs->nn); 118 } 119 return x; 120 } 121 122 #endif 123