staging/keucr: fix keucr scsiglue coding style
[deliverable/linux.git] / drivers / staging / keucr / smilecc.c
CommitLineData
126bb03b
AC
1#include "usb.h"
2#include "scsiglue.h"
3#include "transport.h"
4//#include "stdlib.h"
5//#include "EUCR6SK.h"
6#include "smcommon.h"
7#include "smil.h"
8
9//#include <stdio.h>
10//#include <stdlib.h>
11//#include <string.h>
12//#include <dos.h>
13//
14//#include "EMCRIOS.h"
15
16// CP0-CP5 code table
17static BYTE ecctable[256] = {
180x00,0x55,0x56,0x03,0x59,0x0C,0x0F,0x5A,0x5A,0x0F,0x0C,0x59,0x03,0x56,0x55,0x00,
190x65,0x30,0x33,0x66,0x3C,0x69,0x6A,0x3F,0x3F,0x6A,0x69,0x3C,0x66,0x33,0x30,0x65,
200x66,0x33,0x30,0x65,0x3F,0x6A,0x69,0x3C,0x3C,0x69,0x6A,0x3F,0x65,0x30,0x33,0x66,
210x03,0x56,0x55,0x00,0x5A,0x0F,0x0C,0x59,0x59,0x0C,0x0F,0x5A,0x00,0x55,0x56,0x03,
220x69,0x3C,0x3F,0x6A,0x30,0x65,0x66,0x33,0x33,0x66,0x65,0x30,0x6A,0x3F,0x3C,0x69,
230x0C,0x59,0x5A,0x0F,0x55,0x00,0x03,0x56,0x56,0x03,0x00,0x55,0x0F,0x5A,0x59,0x0C,
240x0F,0x5A,0x59,0x0C,0x56,0x03,0x00,0x55,0x55,0x00,0x03,0x56,0x0C,0x59,0x5A,0x0F,
250x6A,0x3F,0x3C,0x69,0x33,0x66,0x65,0x30,0x30,0x65,0x66,0x33,0x69,0x3C,0x3F,0x6A,
260x6A,0x3F,0x3C,0x69,0x33,0x66,0x65,0x30,0x30,0x65,0x66,0x33,0x69,0x3C,0x3F,0x6A,
270x0F,0x5A,0x59,0x0C,0x56,0x03,0x00,0x55,0x55,0x00,0x03,0x56,0x0C,0x59,0x5A,0x0F,
280x0C,0x59,0x5A,0x0F,0x55,0x00,0x03,0x56,0x56,0x03,0x00,0x55,0x0F,0x5A,0x59,0x0C,
290x69,0x3C,0x3F,0x6A,0x30,0x65,0x66,0x33,0x33,0x66,0x65,0x30,0x6A,0x3F,0x3C,0x69,
300x03,0x56,0x55,0x00,0x5A,0x0F,0x0C,0x59,0x59,0x0C,0x0F,0x5A,0x00,0x55,0x56,0x03,
310x66,0x33,0x30,0x65,0x3F,0x6A,0x69,0x3C,0x3C,0x69,0x6A,0x3F,0x65,0x30,0x33,0x66,
320x65,0x30,0x33,0x66,0x3C,0x69,0x6A,0x3F,0x3F,0x6A,0x69,0x3C,0x66,0x33,0x30,0x65,
330x00,0x55,0x56,0x03,0x59,0x0C,0x0F,0x5A,0x5A,0x0F,0x0C,0x59,0x03,0x56,0x55,0x00
34};
35
36static void trans_result (BYTE, BYTE, BYTE *, BYTE *);
37
38#define BIT7 0x80
39#define BIT6 0x40
40#define BIT5 0x20
41#define BIT4 0x10
42#define BIT3 0x08
43#define BIT2 0x04
44#define BIT1 0x02
45#define BIT0 0x01
46#define BIT1BIT0 0x03
47#define BIT23 0x00800000L
48#define MASK_CPS 0x3f
49#define CORRECTABLE 0x00555554L
50
d8aba9d5
RD
51/*
52 * reg2; // LP14,LP12,LP10,...
53 * reg3; // LP15,LP13,LP11,...
54 * *ecc1; // LP15,LP14,LP13,...
55 * *ecc2; // LP07,LP06,LP05,...
56 */
57static void trans_result(BYTE reg2, BYTE reg3, BYTE *ecc1, BYTE *ecc2)
126bb03b
AC
58{
59 BYTE a; // Working for reg2,reg3
60 BYTE b; // Working for ecc1,ecc2
61 BYTE i; // For counting
62
63 a=BIT7; b=BIT7; // 80h=10000000b
64 *ecc1=*ecc2=0; // Clear ecc1,ecc2
65 for(i=0; i<4; ++i) {
66 if ((reg3&a)!=0)
67 *ecc1|=b; // LP15,13,11,9 -> ecc1
68 b=b>>1; // Right shift
69 if ((reg2&a)!=0)
70 *ecc1|=b; // LP14,12,10,8 -> ecc1
71 b=b>>1; // Right shift
72 a=a>>1; // Right shift
73 }
74
75 b=BIT7; // 80h=10000000b
76 for(i=0; i<4; ++i) {
77 if ((reg3&a)!=0)
78 *ecc2|=b; // LP7,5,3,1 -> ecc2
79 b=b>>1; // Right shift
80 if ((reg2&a)!=0)
81 *ecc2|=b; // LP6,4,2,0 -> ecc2
82 b=b>>1; // Right shift
83 a=a>>1; // Right shift
84 }
85}
86
87//static void calculate_ecc(table,data,ecc1,ecc2,ecc3)
d8aba9d5
RD
88/*
89 * *table; // CP0-CP5 code table
90 * *data; // DATA
91 * *ecc1; // LP15,LP14,LP13,...
92 * *ecc2; // LP07,LP06,LP05,...
93 * *ecc3; // CP5,CP4,CP3,...,"1","1"
94 */
95void calculate_ecc(BYTE *table, BYTE *data, BYTE *ecc1, BYTE *ecc2, BYTE *ecc3)
126bb03b
AC
96{
97 DWORD i; // For counting
98 BYTE a; // Working for table
99 BYTE reg1; // D-all,CP5,CP4,CP3,...
100 BYTE reg2; // LP14,LP12,L10,...
101 BYTE reg3; // LP15,LP13,L11,...
102
103 reg1=reg2=reg3=0; // Clear parameter
104 for(i=0; i<256; ++i) {
105 a=table[data[i]]; // Get CP0-CP5 code from table
106 reg1^=(a&MASK_CPS); // XOR with a
107 if ((a&BIT6)!=0)
108 { // If D_all(all bit XOR) = 1
109 reg3^=(BYTE)i; // XOR with counter
110 reg2^=~((BYTE)i); // XOR with inv. of counter
111 }
112 }
113
114 // Trans LP14,12,10,... & LP15,13,11,... -> LP15,14,13,... & LP7,6,5,..
115 trans_result(reg2,reg3,ecc1,ecc2);
116 *ecc1=~(*ecc1); *ecc2=~(*ecc2); // Inv. ecc2 & ecc3
117 *ecc3=((~reg1)<<2)|BIT1BIT0; // Make TEL format
118}
119
d8aba9d5
RD
120/*
121 * *data; // DATA
122 * *eccdata; // ECC DATA
123 * ecc1; // LP15,LP14,LP13,...
124 * ecc2; // LP07,LP06,LP05,...
125 * ecc3; // CP5,CP4,CP3,...,"1","1"
126 */
127BYTE correct_data(BYTE *data, BYTE *eccdata, BYTE ecc1, BYTE ecc2, BYTE ecc3)
126bb03b
AC
128{
129 DWORD l; // Working to check d
130 DWORD d; // Result of comparison
131 DWORD i; // For counting
132 BYTE d1,d2,d3; // Result of comparison
133 BYTE a; // Working for add
134 BYTE add; // Byte address of cor. DATA
135 BYTE b; // Working for bit
136 BYTE bit; // Bit address of cor. DATA
137
138 d1=ecc1^eccdata[1]; d2=ecc2^eccdata[0]; // Compare LP's
139 d3=ecc3^eccdata[2]; // Comapre CP's
140 d=((DWORD)d1<<16) // Result of comparison
141 +((DWORD)d2<<8)
142 +(DWORD)d3;
143
144 if (d==0) return(0); // If No error, return
145
146 if (((d^(d>>1))&CORRECTABLE)==CORRECTABLE)
147 { // If correctable
148 l=BIT23;
149 add=0; // Clear parameter
150 a=BIT7;
151
152 for(i=0; i<8; ++i) { // Checking 8 bit
153 if ((d&l)!=0) add|=a; // Make byte address from LP's
154 l>>=2; a>>=1; // Right Shift
155 }
156
157 bit=0; // Clear parameter
158 b=BIT2;
159 for(i=0; i<3; ++i) { // Checking 3 bit
160 if ((d&l)!=0) bit|=b; // Make bit address from CP's
161 l>>=2; b>>=1; // Right shift
162 }
163
164 b=BIT0;
165 data[add]^=(b<<bit); // Put corrected data
166 return(1);
167 }
168
169 i=0; // Clear count
170 d&=0x00ffffffL; // Masking
171
172 while(d) { // If d=0 finish counting
173 if (d&BIT0) ++i; // Count number of 1 bit
174 d>>=1; // Right shift
175 }
176
177 if (i==1)
178 { // If ECC error
179 eccdata[1]=ecc1; eccdata[0]=ecc2; // Put right ECC code
180 eccdata[2]=ecc3;
181 return(2);
182 }
183 return(3); // Uncorrectable error
184}
185
d8aba9d5 186int _Correct_D_SwECC(BYTE *buf, BYTE *redundant_ecc, BYTE *calculate_ecc)
126bb03b 187{
f3d5049c 188 DWORD err;
126bb03b 189
f3d5049c
JMC
190 err = correct_data(buf, redundant_ecc, *(calculate_ecc + 1),
191 *(calculate_ecc), *(calculate_ecc + 2));
192 if (err == 1)
193 memcpy(calculate_ecc, redundant_ecc, 3);
194
195 if (err == 0 || err == 1 || err == 2)
196 return 0;
197
198 return -1;
126bb03b
AC
199}
200
d8aba9d5 201void _Calculate_D_SwECC(BYTE *buf, BYTE *ecc)
126bb03b
AC
202{
203 calculate_ecc(ecctable,buf,ecc+1,ecc+0,ecc+2);
204}
205
206
This page took 0.101621 seconds and 5 git commands to generate.