| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * include/linux/rslib.h | 
 | 3 |  * | 
 | 4 |  * Overview: | 
 | 5 |  *   Generic Reed Solomon encoder / decoder library | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 6 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  * 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 Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 12 |  * $Id: rslib.h,v 1.4 2005/11/07 11:14:52 gleixner Exp $ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  * | 
 | 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 Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 24 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 |  * struct rs_control - rs control structure | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 26 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 |  * @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 Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 31 |  * @genpoly:	Generator polynomial | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 |  * @nroots:	Number of generator roots = number of parity symbols | 
 | 33 |  * @fcr:	First consecutive root, index form | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 34 |  * @prim:	Primitive element, index form | 
 | 35 |  * @iprim:	prim-th root of 1, index form | 
 | 36 |  * @gfpoly:	The primitive generator polynominal | 
| Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 37 |  * @gffunc:	Function to generate the field, if non-canonical representation | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 38 |  * @users:	Users of this structure | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  * @list:	List entry for the rs control list | 
 | 40 | */ | 
 | 41 | struct rs_control { | 
 | 42 | 	int 		mm; | 
 | 43 | 	int 		nn; | 
 | 44 | 	uint16_t	*alpha_to; | 
 | 45 | 	uint16_t	*index_of; | 
 | 46 | 	uint16_t	*genpoly; | 
 | 47 | 	int 		nroots; | 
 | 48 | 	int 		fcr; | 
 | 49 | 	int 		prim; | 
 | 50 | 	int 		iprim; | 
 | 51 | 	int		gfpoly; | 
| Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 52 | 	int		(*gffunc)(int); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | 	int		users; | 
 | 54 | 	struct list_head list; | 
 | 55 | }; | 
 | 56 |  | 
 | 57 | /* General purpose RS codec, 8-bit data width, symbol width 1-15 bit  */ | 
 | 58 | #ifdef CONFIG_REED_SOLOMON_ENC8 | 
 | 59 | int encode_rs8(struct rs_control *rs, uint8_t *data, int len, uint16_t *par, | 
 | 60 | 	       uint16_t invmsk); | 
 | 61 | #endif | 
 | 62 | #ifdef CONFIG_REED_SOLOMON_DEC8 | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 63 | int decode_rs8(struct rs_control *rs, uint8_t *data, uint16_t *par, int len, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | 		uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, | 
 | 65 | 	       uint16_t *corr); | 
 | 66 | #endif | 
 | 67 |  | 
 | 68 | /* General purpose RS codec, 16-bit data width, symbol width 1-15 bit  */ | 
 | 69 | #ifdef CONFIG_REED_SOLOMON_ENC16 | 
 | 70 | int encode_rs16(struct rs_control *rs, uint16_t *data, int len, uint16_t *par, | 
 | 71 | 		uint16_t invmsk); | 
 | 72 | #endif | 
 | 73 | #ifdef CONFIG_REED_SOLOMON_DEC16 | 
 | 74 | int decode_rs16(struct rs_control *rs, uint16_t *data, uint16_t *par, int len, | 
 | 75 | 		uint16_t *s, int no_eras, int *eras_pos, uint16_t invmsk, | 
 | 76 | 		uint16_t *corr); | 
 | 77 | #endif | 
 | 78 |  | 
 | 79 | /* Create or get a matching rs control structure */ | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 80 | struct rs_control *init_rs(int symsize, int gfpoly, int fcr, int prim, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | 			   int nroots); | 
| Segher Boessenkool | d7e5a54 | 2007-05-02 12:18:41 +0200 | [diff] [blame] | 82 | struct rs_control *init_rs_non_canonical(int symsize, int (*func)(int), | 
 | 83 |                                          int fcr, int prim, int nroots); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  | 
 | 85 | /* Release a rs control structure */ | 
 | 86 | void free_rs(struct rs_control *rs); | 
 | 87 |  | 
 | 88 | /** modulo replacement for galois field arithmetics | 
 | 89 |  * | 
 | 90 |  *  @rs:	the rs control structure | 
 | 91 |  *  @x:		the value to reduce | 
 | 92 |  * | 
 | 93 |  *  where | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 94 |  *  rs->mm = number of bits per symbol | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 |  *  rs->nn = (2^rs->mm) - 1 | 
| Thomas Gleixner | 03ead84 | 2005-11-07 11:15:37 +0000 | [diff] [blame] | 96 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 |  *  Simple arithmetic modulo would return a wrong result for values | 
 | 98 |  *  >= 3 * rs->nn | 
 | 99 | */ | 
 | 100 | static inline int rs_modnn(struct rs_control *rs, int x) | 
 | 101 | { | 
 | 102 | 	while (x >= rs->nn) { | 
 | 103 | 		x -= rs->nn; | 
 | 104 | 		x = (x >> rs->mm) + (x & rs->nn); | 
 | 105 | 	} | 
 | 106 | 	return x; | 
 | 107 | } | 
 | 108 |  | 
 | 109 | #endif |