Switch from GPLv2 to GPLv2+
[fw/altos] / src / kernel / ao_fec_tx.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao_fec.h>
20 #include <stdio.h>
21
22 #if AO_FEC_DEBUG
23 void
24 ao_fec_dump_bytes(const uint8_t *bytes, uint16_t len, const char *name)
25 {
26         uint16_t        i;
27
28         printf ("%s (%d):", name, len);
29         for (i = 0; i < len; i++) {
30                 if ((i & 7) == 0)
31                         printf ("\n\t%02x:", i);
32                 printf(" %02x", bytes[i]);
33         }
34         printf ("\n");
35 }
36 #endif
37
38 uint16_t
39 ao_fec_crc(const uint8_t *bytes, uint8_t len)
40 {
41         uint16_t        crc = AO_FEC_CRC_INIT;
42
43         while (len--)
44                 crc = ao_fec_crc_byte(*bytes++, crc);
45         return crc;
46 }
47
48 /*
49  * len is the length of the data; the crc will be
50  * the fist two bytes after that
51  */
52
53 uint8_t
54 ao_fec_check_crc(const uint8_t *bytes, uint8_t len)
55 {
56         uint16_t        computed_crc = ao_fec_crc(bytes, len);
57         uint16_t        received_crc = (bytes[len] << 8) | (bytes[len+1]);
58
59         return computed_crc == received_crc;
60 }
61
62 /*
63  * Compute CRC and trellis-terminator/interleave-pad bytes
64  */
65 static uint8_t
66 ao_fec_prepare(const uint8_t *in, uint8_t len, uint8_t *extra)
67 {
68         uint16_t        crc = ao_fec_crc (in, len);
69         uint8_t         i = 0;
70         uint8_t         num_fec;
71
72         /* Append CRC */
73         extra[i++] = crc >> 8;
74         extra[i++] = crc;
75
76         /* Append FEC -- 1 byte if odd, two bytes if even */
77         num_fec = 2 - (i & 1);
78         while (num_fec--)
79                 extra[i++] = AO_FEC_TRELLIS_TERMINATOR;
80         return i;
81 }
82
83 const uint8_t ao_fec_whiten_table[] = {
84 #include "ao_whiten.h"
85 };
86
87 static const uint8_t ao_fec_encode_table[16] = {
88 /* next 0  1      state */
89         0, 3,   /* 000 */
90         1, 2,   /* 001 */
91         3, 0,   /* 010 */
92         2, 1,   /* 011 */
93         3, 0,   /* 100 */
94         2, 1,   /* 101 */
95         0, 3,   /* 110 */
96         1, 2    /* 111 */
97 };
98
99 uint8_t
100 ao_fec_encode(const uint8_t *in, uint8_t len, uint8_t *out)
101 {
102         uint8_t         extra[AO_FEC_PREPARE_EXTRA];
103         uint8_t         extra_len;
104         uint32_t        encode, interleave;
105         uint8_t         pair, byte, bit;
106         uint16_t        fec = 0;
107         const uint8_t   *whiten = ao_fec_whiten_table;
108
109         extra_len = ao_fec_prepare(in, len, extra);
110         for (pair = 0; pair < len + extra_len; pair += 2) {
111                 encode = 0;
112                 for (byte = 0; byte < 2; byte++) {
113                         if (pair + byte == len)
114                                 in = extra;
115                         fec |= *in++ ^ *whiten++;
116                         for (bit = 0; bit < 8; bit++) {
117                                 encode = encode << 2 | ao_fec_encode_table[fec >> 7];
118                                 fec = (fec << 1) & 0x7ff;
119                         }
120                 }
121
122                 interleave = 0;
123                 for (bit = 0; bit < 4 * 4; bit++) {
124                         uint8_t byte_shift = (bit & 0x3) << 3;
125                         uint8_t bit_shift = (bit & 0xc) >> 1;
126
127                         interleave = (interleave << 2) | ((encode >> (byte_shift + bit_shift)) & 0x3);
128                 }
129                 *out++ = interleave >> 24;
130                 *out++ = interleave >> 16;
131                 *out++ = interleave >> 8;
132                 *out++ = interleave >> 0;
133         }
134         return (len + extra_len) * 2;
135 }