source: titan/titan/cardreader.h @ 15345

Last change on this file since 15345 was 11330, checked in by nit, 12 years ago

[titan] add unicable screen, fix message corrupt channel after tunerconfig, add first cardreader funktions

File size: 7.4 KB
Line 
1#ifndef CARDREADER_H
2#define CARDREADER_H
3
4//MAJOR NUM OF DEVICE DRVIER
5#define MAJOR_NUM                       169
6
7//constants
8#define DEVICE_NAME             "sci_dev"
9#define SCI_IOW_MAGIC   's'
10#define ATR_TIMEOUT   800
11
12typedef struct s_ATR
13{
14  unsigned length;
15  unsigned char TS;
16  unsigned char T0;
17  struct
18  {
19    unsigned char value;
20    int present;
21  } ib[ATR_MAX_PROTOCOLS][ATR_MAX_IB], TCK;
22  unsigned pn;
23  unsigned char hb[ATR_MAX_HISTORICAL];
24  unsigned hbn;
25} ATR;
26
27//ATR Paramenters
28#define ATR_MAX_SIZE            33      //Maximum size of ATR byte array/
29#define ATR_MAX_HISTORICAL      15      //Maximum number of historical bytes
30#define ATR_MAX_PROTOCOLS       7       //Maximun number of protocols
31#define ATR_MAX_IB              4       //MaxNr of interface bytes per protocol
32#define ATR_CONVENTION_DIRECT   0       //Direct convention
33#define ATR_CONVENTION_INVERSE  1       //Inverse convention
34#define ATR_PROTOCOL_TYPE_T0    0       //Protocol type T=0
35#define ATR_PROTOCOL_TYPE_T1    1       //Protocol type T=1
36#define ATR_PROTOCOL_TYPE_T2    2       //Protocol type T=2
37#define ATR_PROTOCOL_TYPE_T3    3       //Protocol type T=3
38#define ATR_PROTOCOL_TYPE_T14   14      //Protocol type T=14
39#define ATR_INTERFACE_BYTE_TA   0       //Interface byte TAi
40#define ATR_INTERFACE_BYTE_TB   1       //Interface byte TBi
41#define ATR_INTERFACE_BYTE_TC   2       //Interface byte TCi
42#define ATR_INTERFACE_BYTE_TD   3       //Interface byte TDi
43#define ATR_PARAMETER_F         0       //Parameter F
44#define ATR_PARAMETER_D         1       //Parameter D
45#define ATR_PARAMETER_I         2       //Parameter I
46#define ATR_PARAMETER_P         3       //Parameter P
47#define ATR_PARAMETER_N         4       //Parameter N
48#define ATR_INTEGER_VALUE_FI    0       //Integer value FI
49#define ATR_INTEGER_VALUE_DI    1       //Integer value DI
50#define ATR_INTEGER_VALUE_II    2       //Integer value II
51#define ATR_INTEGER_VALUE_PI1   3       //Integer value PI1
52#define ATR_INTEGER_VALUE_N     4       //Integer value N
53#define ATR_INTEGER_VALUE_PI2   5       //Integer value PI2
54
55//Default values for paramenters
56#define ATR_DEFAULT_FI  1
57#define ATR_DEFAULT_D   1
58#define ATR_DEFAULT_I   50
59#define ATR_DEFAULT_N   0
60#define ATR_DEFAULT_P   5
61
62#define INVERT_BYTE(a)  ((((a) << 7) & 0x80) | \
63                        (((a) << 5) & 0x40) | \
64                        (((a) << 3) & 0x20) | \
65                        (((a) << 1) & 0x10) | \
66                        (((a) >> 1) & 0x08) | \
67                        (((a) >> 3) & 0x04) | \
68                        (((a) >> 5) & 0x02) | \
69                        (((a) >> 7) & 0x01))
70
71
72const uint32_t atr_fs_table[16] = {4000000L, 5000000L, 6000000L, 8000000L, 12000000L, 16000000L, 20000000L, 0, 0, 5000000L, 7500000L, 10000000L, 15000000L, 20000000L, 0, 0};
73static const uint32_t atr_num_ib_table[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
74const uint32_t atr_f_table[16] = {372, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0};
75const double atr_d_table[16] = {0, 1, 2, 4, 8, 16, 32, 64, 12, 20, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625};
76const uint32_t atr_i_table[4] = {25, 50, 100, 0};
77
78void cs_sleepms(uint32_t msec)
79{
80        //does not interfere with signals like sleep and usleep do
81        struct timespec req_ts;
82        req_ts.tv_sec = msec / 1000;
83        req_ts.tv_nsec = (msec % 1000) * 1000000L;
84        int32_t olderrno = errno; //Some OS (especially MacOSX) seem to set errno to ETIMEDOUT when sleeping
85        nanosleep(&req_ts, NULL);
86        errno = olderrno;
87}
88
89int serialread(struct dvbdev* node, uint32_t timeout, uint32_t size, unsigned char* data)
90{
91        char c;
92        uint32_t count = 0;
93        int readed;
94        struct timeval tv, tv_spent;
95
96        if(node == NULL) return 1;
97       
98//TODO:
99/*      if((node->typ != R_INTERNAL) && (node->written > 0))
100        {
101                char buf[256];
102                int32_t n = node->written;
103                node->written = 0;
104       
105                if(serialread(node, timeout, n, buf))
106                        return 1;
107        }
108*/
109       
110        for(count = 0; count < size ; count++)
111        {
112                gettimeofday(&tv, 0);
113                memcpy(&tv_spent,&tv, sizeof(struct timeval));
114                readed = FALSE;
115                while((((tv_spent.tv_sec - tv.tv_sec) * 1000) + ((tv_spent.tv_usec - tv.tv_usec) / 1000L)) < timeout)
116                {
117                        if(read(node->fd, &c, 1) == 1)
118                        {
119                                readed = TRUE;
120                                break;
121                        }
122                        gettimeofday(&tv_spent, 0);
123                }
124                if(!readed)
125                {
126                        debug(630, "Receiving: count=%d", count);
127                        return 1;
128                }
129                data[count] = c;
130        }
131        debug(630, "Receiving: count=%d", count);
132        return 0;
133}
134
135int atrinitfromarray(ATR* atr, char atr_buffer[ATR_MAX_SIZE], uint32_t length)
136{
137        unsigned char TDi;
138        unsigned char buffer[ATR_MAX_SIZE];
139        uint32_t pointer = 0, pn = 0;
140       
141        /* Check size of buffer */
142        if(length < 2) return(2);
143       
144        /* Check if ATR is from a inverse convention card */
145        if(atr_buffer[0] == 0x03)
146        {
147                for(pointer = 0; pointer < length; pointer++)
148                        buffer[pointer] = ~(INVERT_BYTE(atr_buffer[pointer]));
149        }
150        else
151                memcpy(buffer, atr_buffer, length);
152       
153        /* Store T0 and TS */
154        atr->TS = buffer[0];
155        atr->T0 = TDi = buffer[1];
156        pointer = 1;
157       
158        /* Store number of historical bytes */
159        atr->hbn = TDi & 0x0F;
160       
161        /* TCK is not present by default */
162        (atr->TCK).present = 0;
163       
164        /* Extract interface bytes */
165        while(pointer < length)
166        {
167                /* Check buffer is long enought */
168                if(pointer + atr_num_ib_table[(0xF0 & TDi) >> 4] >= length)
169                        return(2);
170               
171                /* Check TAi is present */
172                if((TDi | 0xEF) == 0xFF)
173                {
174                        pointer++;
175                        atr->ib[pn][ATR_INTERFACE_BYTE_TA].value = buffer[pointer];
176                        atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = 1;
177                }
178                else
179                        atr->ib[pn][ATR_INTERFACE_BYTE_TA].present = 0;
180               
181                /* Check TBi is present */
182                if((TDi | 0xDF) == 0xFF)
183                {
184                        pointer++;
185                        atr->ib[pn][ATR_INTERFACE_BYTE_TB].value = buffer[pointer];
186                        atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = 1;
187                }
188                else
189                        atr->ib[pn][ATR_INTERFACE_BYTE_TB].present = 0;
190               
191                /* Check TCi is present */
192                if((TDi | 0xBF) == 0xFF)
193                {
194                        pointer++;
195                        atr->ib[pn][ATR_INTERFACE_BYTE_TC].value = buffer[pointer];
196                        atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = 1;
197                }
198                else
199                        atr->ib[pn][ATR_INTERFACE_BYTE_TC].present = 0;
200               
201                /* Read TDi if present */
202                if((TDi | 0x7F) == 0xFF)
203                {
204                        pointer++;
205                        TDi = atr->ib[pn][ATR_INTERFACE_BYTE_TD].value = buffer[pointer];
206                        atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = 1;
207                        (atr->TCK).present = ((TDi & 0x0F) != ATR_PROTOCOL_TYPE_T0);
208                        if(pn >= ATR_MAX_PROTOCOLS) return(2);
209                        pn++;
210                }
211                else
212                {
213                        atr->ib[pn][ATR_INTERFACE_BYTE_TD].present = 0;
214                        break;
215                }
216        }
217       
218        /* Store number of protocols */
219        atr->pn = pn + 1;
220       
221        /* Store historical bytes */
222        if (pointer + atr->hbn >= length)
223        {
224                err("ATR is malformed, it reports %i historical bytes but there are only %i", atr->hbn, length-pointer - 2);
225                if(length-pointer >= 2)
226                        atr->hbn = length-pointer - 2;
227                else
228                {
229                        atr->hbn = 0;
230                        atr->length = pointer + 1;
231                        return(2);
232                }
233
234        }
235       
236        memcpy(atr->hb, buffer + pointer + 1, atr->hbn);
237        pointer += (atr->hbn);
238       
239        /* Store TCK  */
240        if((atr->TCK).present)
241        {       
242                if(pointer + 1 >= length) return(2);
243                pointer++;
244                (atr->TCK).value = buffer[pointer];
245        }
246       
247        atr->length = pointer + 1;
248       
249        //check that TA1, if pn==1 , has a valid value for FI
250        if((atr->pn==1) && (atr->ib[pn][ATR_INTERFACE_BYTE_TA].present != 0))
251        {
252                unsigned char FI;
253                debug(630, "TA1 = %02x", atr->ib[pn][ATR_INTERFACE_BYTE_TA].value);
254                FI = (atr->ib[pn][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4;
255                debug(630, "FI = %02x", FI);
256                if(atr_fs_table[FI] == 0)
257                {
258                        err("Invalid ATR as FI is not returning a valid frequency value");
259                        return(2);
260                }
261        }
262   
263        //check that TB1 < 0x80
264        if((atr->pn==1) && (atr->ib[pn][ATR_INTERFACE_BYTE_TB].present != 0))
265        {
266                if(atr->ib[pn][ATR_INTERFACE_BYTE_TB].value > 0x80)
267                {
268                        err("Invalid ATR as TB1 has an invalid value");
269                        return(2);
270                }
271        }
272        return(0);
273}
274
275#endif
Note: See TracBrowser for help on using the repository browser.