1/*******************************************************************************
2 * $Id: crc.h,v 8.9 2003/10/16 21:55:54 Exp $
3 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 * crc.h - a function to compute crc for iLine10 headers
17 ******************************************************************************/
18
19#ifndef _RTS_CRC_H_
20#define _RTS_CRC_H_ 1
21
22#include "typedefs.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28
29#define CRC8_INIT_VALUE  0xff       /* Initial CRC8 checksum value */
30#define CRC8_GOOD_VALUE  0x9f       /* Good final CRC8 checksum value */
31#define HCS_GOOD_VALUE   0x39       /* Good final header checksum value */
32
33#define CRC16_INIT_VALUE 0xffff     /* Initial CRC16 checksum value */
34#define CRC16_GOOD_VALUE 0xf0b8     /* Good final CRC16 checksum value */
35
36#define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */
37#define CRC32_GOOD_VALUE 0xdebb20e3 /* Good final CRC32 checksum value */
38
39void   hcs(uint8 *, uint);
40uint8  crc8(uint8 *, uint, uint8);
41uint16 crc16(uint8 *, uint, uint16);
42uint32 crc32(uint8 *, uint, uint32);
43
44/* macros for common usage */
45
46#define APPEND_CRC8(pbytes, nbytes)                           \
47do {                                                          \
48    uint8 tmp = crc8(pbytes, nbytes, CRC8_INIT_VALUE) ^ 0xff; \
49    (pbytes)[(nbytes)] = tmp;                                 \
50    (nbytes) += 1;                                            \
51} while (0)
52
53#define APPEND_CRC16(pbytes, nbytes)                               \
54do {                                                               \
55    uint16 tmp = crc16(pbytes, nbytes, CRC16_INIT_VALUE) ^ 0xffff; \
56    (pbytes)[(nbytes) + 0] = (tmp >> 0) & 0xff;                    \
57    (pbytes)[(nbytes) + 1] = (tmp >> 8) & 0xff;                    \
58    (nbytes) += 2;                                                 \
59} while (0)
60
61#define APPEND_CRC32(pbytes, nbytes)                                   \
62do {                                                                   \
63    uint32 tmp = crc32(pbytes, nbytes, CRC32_INIT_VALUE) ^ 0xffffffff; \
64    (pbytes)[(nbytes) + 0] = (tmp >>  0) & 0xff;                       \
65    (pbytes)[(nbytes) + 1] = (tmp >>  8) & 0xff;                       \
66    (pbytes)[(nbytes) + 2] = (tmp >> 16) & 0xff;                       \
67    (pbytes)[(nbytes) + 3] = (tmp >> 24) & 0xff;                       \
68    (nbytes) += 4;                                                     \
69} while (0)
70
71#ifdef __cplusplus
72}
73#endif
74
75#endif /* _RTS_CRC_H_ */
76