1 | /*
|
---|
2 | ifd_sci.c
|
---|
3 | This module provides IFD handling functions for SCI internal reader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include "ifd_sci.h"
|
---|
7 |
|
---|
8 | #ifdef SCI_DEV
|
---|
9 |
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <time.h>
|
---|
12 | //#include <string.h>
|
---|
13 | #include <sys/ioctl.h>
|
---|
14 | #include "sci_global.h"
|
---|
15 | #include "sci_ioctl.h"
|
---|
16 | #include "atr.h"
|
---|
17 |
|
---|
18 | #include "ifd_towitoko.h"
|
---|
19 | #define IFD_TOWITOKO_ATR_TIMEOUT 800
|
---|
20 |
|
---|
21 | #define OK 1
|
---|
22 | #define ERROR 0
|
---|
23 |
|
---|
24 | int Sci_Init ()
|
---|
25 | {
|
---|
26 | }
|
---|
27 |
|
---|
28 | int Sci_GetStatus (int handle, int * status)
|
---|
29 | {
|
---|
30 | int in;
|
---|
31 | if (ioctl(handle, IOCTL_GET_IS_CARD_PRESENT, status)<0)
|
---|
32 | return ERROR;
|
---|
33 | return OK;
|
---|
34 | }
|
---|
35 |
|
---|
36 | int Sci_Reset (IFD * ifd, ATR ** atr)
|
---|
37 | {
|
---|
38 | unsigned char buf[SCI_MAX_ATR_SIZE];
|
---|
39 | int n = 0;
|
---|
40 | SCI_PARAMETERS params;
|
---|
41 | #ifdef SH4
|
---|
42 | struct timeval tv, tv_spent;
|
---|
43 | int atr_size = 2, TDi_exists = 0;
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | (*atr) = NULL;
|
---|
47 |
|
---|
48 | #ifdef SH4
|
---|
49 | memset(¶ms,0,sizeof(SCI_PARAMETERS));
|
---|
50 |
|
---|
51 | params.ETU = 372;
|
---|
52 | params.EGT = 3;
|
---|
53 | params.fs = 9;
|
---|
54 | params.T = 0;
|
---|
55 |
|
---|
56 | if(ioctl(ifd->io->fd, IOCTL_SET_PARAMETERS, ¶ms)!=0)
|
---|
57 | return ERROR;
|
---|
58 | #endif
|
---|
59 |
|
---|
60 | if(ioctl(ifd->io->fd, IOCTL_SET_RESET)<0)
|
---|
61 | return ERROR;
|
---|
62 |
|
---|
63 | #ifdef SH4
|
---|
64 | gettimeofday(&tv,0);
|
---|
65 | memcpy(&tv_spent,&tv,sizeof(struct timeval));
|
---|
66 |
|
---|
67 | while(n<atr_size && (tv_spent.tv_sec-tv.tv_sec)<10)
|
---|
68 | {
|
---|
69 | if(IO_Serial_Read(ifd->io, IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
|
---|
70 | n++;
|
---|
71 | gettimeofday(&tv_spent,0);
|
---|
72 | if(n==2) // format character
|
---|
73 | {
|
---|
74 | // high nibble = TA1 , TB1 , TC1 , TD1
|
---|
75 | if(buf[n-1] & 0x10)
|
---|
76 | atr_size++;
|
---|
77 | if(buf[n-1] & 0x20)
|
---|
78 | atr_size++;
|
---|
79 | if(buf[n-1] & 0x40)
|
---|
80 | atr_size++;
|
---|
81 | if(buf[n-1] & 0x80)
|
---|
82 | {
|
---|
83 | atr_size++;
|
---|
84 | TDi_exists=atr_size;
|
---|
85 | }
|
---|
86 | atr_size+=(buf[n-1] & 0x0F); // historical bytes
|
---|
87 | }
|
---|
88 | if( (TDi_exists>0) && (n==TDi_exists) )
|
---|
89 | {
|
---|
90 | TDi_exists=0;
|
---|
91 | // high nibble = TA1 , TB1 , TC1 , TD1
|
---|
92 | if(buf[n-1] & 0x10)
|
---|
93 | atr_size++;
|
---|
94 | if(buf[n-1] & 0x20)
|
---|
95 | atr_size++;
|
---|
96 | if(buf[n-1] & 0x40)
|
---|
97 | atr_size++;
|
---|
98 | if(buf[n-1] & 0x80)
|
---|
99 | {
|
---|
100 | atr_size++;
|
---|
101 | TDi_exists=atr_size;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | #else
|
---|
106 | while(n<SCI_MAX_ATR_SIZE && IO_Serial_Read(ifd->io, IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
|
---|
107 | {
|
---|
108 | n++;
|
---|
109 | }
|
---|
110 |
|
---|
111 | if ((buf[0] !=0x3B) && (buf[0] != 0x3F) && (n>9 && !memcmp(buf+4, "IRDETO", 6))) //irdeto S02 reports FD as first byte on dreambox SCI, not sure about SH4 or phoenix
|
---|
112 | buf[0] = 0x3B;
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | if(n==0)
|
---|
116 | return ERROR;
|
---|
117 |
|
---|
118 |
|
---|
119 | (*atr) = ATR_New ();
|
---|
120 | if(ATR_InitFromArray ((*atr), buf, n) == ATR_OK)
|
---|
121 | {
|
---|
122 | struct timespec req_ts;
|
---|
123 | req_ts.tv_sec = 0;
|
---|
124 | req_ts.tv_nsec = 50000000;
|
---|
125 | nanosleep (&req_ts, NULL);
|
---|
126 | if (ioctl(ifd->io->fd, IOCTL_SET_ATR_READY)<0)
|
---|
127 | return ERROR;
|
---|
128 | return OK;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | ATR_Delete (*atr);
|
---|
133 | (*atr) = NULL;
|
---|
134 | return ERROR;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | #endif
|
---|