Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Functions
bch.c File Reference
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/bitops.h>
#include <asm/byteorder.h>
#include <linux/bch.h>

Go to the source code of this file.

Data Structures

struct  gf_poly
 
struct  gf_poly_deg1
 

Macros

#define GF_M(_p)   ((_p)->m)
 
#define GF_T(_p)   ((_p)->t)
 
#define GF_N(_p)   ((_p)->n)
 
#define BCH_ECC_WORDS(_p)   DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32)
 
#define BCH_ECC_BYTES(_p)   DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 8)
 
#define dbg(_fmt, args...)   do {} while (0)
 
#define GF_POLY_SZ(_d)   (sizeof(struct gf_poly)+((_d)+1)*sizeof(unsigned int))
 

Functions

void encode_bch (struct bch_control *bch, const uint8_t *data, unsigned int len, uint8_t *ecc)
 
 EXPORT_SYMBOL_GPL (encode_bch)
 
int decode_bch (struct bch_control *bch, const uint8_t *data, unsigned int len, const uint8_t *recv_ecc, const uint8_t *calc_ecc, const unsigned int *syn, unsigned int *errloc)
 
 EXPORT_SYMBOL_GPL (decode_bch)
 
struct bch_controlinit_bch (int m, int t, unsigned int prim_poly)
 
 EXPORT_SYMBOL_GPL (init_bch)
 
void free_bch (struct bch_control *bch)
 
 EXPORT_SYMBOL_GPL (free_bch)
 
 MODULE_LICENSE ("GPL")
 
 MODULE_AUTHOR ("Ivan Djelic <[email protected]>")
 
 MODULE_DESCRIPTION ("Binary BCH encoder/decoder")
 

Macro Definition Documentation

#define BCH_ECC_BYTES (   _p)    DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 8)

Definition at line 88 of file bch.c.

#define BCH_ECC_WORDS (   _p)    DIV_ROUND_UP(GF_M(_p)*GF_T(_p), 32)

Definition at line 87 of file bch.c.

#define dbg (   _fmt,
  args... 
)    do {} while (0)

Definition at line 91 of file bch.c.

#define GF_M (   _p)    ((_p)->m)

Definition at line 82 of file bch.c.

#define GF_N (   _p)    ((_p)->n)

Definition at line 84 of file bch.c.

#define GF_POLY_SZ (   _d)    (sizeof(struct gf_poly)+((_d)+1)*sizeof(unsigned int))

Definition at line 103 of file bch.c.

#define GF_T (   _p)    ((_p)->t)

Definition at line 83 of file bch.c.

Function Documentation

int decode_bch ( struct bch_control bch,
const uint8_t data,
unsigned int  len,
const uint8_t recv_ecc,
const uint8_t calc_ecc,
const unsigned int syn,
unsigned int errloc 
)

decode_bch - decode received codeword and find bit error locations : BCH control structure : received data, ignored if is provided : data length in bytes, must always be provided : received ecc, if NULL then assume it was XORed in : calculated ecc, if NULL then calc_ecc is computed from : hw computed syndrome data (if NULL, syndrome is calculated) : output array of error locations

Returns: The number of errors found, or -EBADMSG if decoding failed, or -EINVAL if invalid parameters were provided

Depending on the available hw BCH support and the need to compute separately (using encode_bch()), this function should be called with one of the following parameter configurations -

by providing and only: decode_bch(, , , , NULL, NULL, )

by providing and : decode_bch(, NULL, , , , NULL, )

by providing ecc = recv_ecc XOR calc_ecc: decode_bch(, NULL, , NULL, ecc, NULL, )

by providing syndrome results : decode_bch(, NULL, , NULL, NULL, , )

Once decode_bch() has successfully returned with a positive value, error locations returned in array should be interpreted as follows -

if (errloc[n] >= 8*len), then n-th error is located in ecc (no need for data correction)

if (errloc[n] < 8*len), then n-th error is located in data and can be corrected with statement data[errloc[n]/8] ^= 1 << (errloc[n] % 8);

Note that this function does not perform any data correction by itself, it merely indicates error locations.

Definition at line 986 of file bch.c.

void encode_bch ( struct bch_control bch,
const uint8_t data,
unsigned int  len,
uint8_t ecc 
)

encode_bch - calculate BCH ecc parity of data : BCH control structure : data to encode : data length in bytes : ecc parity data, must be initialized by caller

The parity array is used both as input and output parameter, in order to allow incremental computations. It should be of the size indicated by member of , and should be initialized to 0 before the first call.

The exact number of computed ecc parity bits is given by member of ; it may be less than m*t for large values of t.

Definition at line 184 of file bch.c.

EXPORT_SYMBOL_GPL ( encode_bch  )
EXPORT_SYMBOL_GPL ( decode_bch  )
EXPORT_SYMBOL_GPL ( init_bch  )
EXPORT_SYMBOL_GPL ( free_bch  )
void free_bch ( struct bch_control bch)

free_bch - free the BCH control structure : BCH control structure to release

Definition at line 1343 of file bch.c.

struct bch_control* init_bch ( int  m,
int  t,
unsigned int  prim_poly 
)
read

init_bch - initialize a BCH encoder/decoder : Galois field order, should be in the range 5-15 : maximum error correction capability, in bits : user-provided primitive polynomial (or 0 to use default)

Returns: a newly allocated BCH control structure if successful, NULL otherwise

This initialization can take some time, as lookup tables are built for fast encoding/decoding; make sure not to call this function from a time critical path. Usually, init_bch() should be called on module/driver init and free_bch() should be called to release memory on exit.

You may provide your own primitive polynomial of degree in argument , or let init_bch() use its default polynomial.

Once init_bch() has successfully returned a pointer to a newly allocated BCH control structure, ecc length in bytes is given by member of the structure.

Definition at line 1249 of file bch.c.

MODULE_AUTHOR ( "Ivan Djelic <[email protected]>"  )
MODULE_DESCRIPTION ( "Binary BCH encoder/decoder"  )
MODULE_LICENSE ( "GPL"  )