source: branches/smartreader/csctapi/ifd_sci.c@ 1239

Last change on this file since 1239 was 1239, checked in by rorothetroll, 13 years ago

resync with trunk

File size: 2.7 KB
Line 
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 <sys/ioctl.h>
13#include "sci_global.h"
14#include "sci_ioctl.h"
15#include "atr.h"
16#include "string.h"
17#ifdef SH4
18#include <fcntl.h>
19#endif
20#include "../globals.h"
21
22#include "ifd_towitoko.h"
23#define IFD_TOWITOKO_ATR_TIMEOUT 800
24
25#define OK 1
26#define ERROR 0
27
28int Sci_Init ()
29{
30}
31
32int Sci_GetStatus (int handle, int * status)
33{
34 int in;
35 if (ioctl(handle, IOCTL_GET_IS_CARD_PRESENT, status)<0)
36 return ERROR;
37 return OK;
38}
39
40int Sci_Reset (ATR ** atr)
41{
42 unsigned char buf[SCI_MAX_ATR_SIZE];
43 int n = 0;
44 SCI_PARAMETERS params;
45#ifdef SH4
46 struct timeval tv, tv_spent;
47 int atr_size = 2, TDi_exists = 0;
48#endif
49
50 (*atr) = NULL;
51
52 memset(&params,0,sizeof(SCI_PARAMETERS));
53
54 params.ETU = 372;
55 params.EGT = 3; //not sure why this value is chosen
56#ifdef SH4
57 params.fs = 9; //not sure why this value is chosen
58#else
59 params.fs = 5;
60#endif
61 params.T = 0;
62
63 if(ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
64 return ERROR;
65
66 if(ioctl(reader[ridx].handle, IOCTL_SET_RESET)<0)
67 return ERROR;
68
69#ifdef SH4
70 gettimeofday(&tv,0);
71 memcpy(&tv_spent,&tv,sizeof(struct timeval));
72
73 while(n<atr_size && (tv_spent.tv_sec-tv.tv_sec)<10)
74 {
75 if(IO_Serial_Read(IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
76 n++;
77 gettimeofday(&tv_spent,0);
78 if(n==2) // format character
79 {
80 // high nibble = TA1 , TB1 , TC1 , TD1
81 if(buf[n-1] & 0x10)
82 atr_size++;
83 if(buf[n-1] & 0x20)
84 atr_size++;
85 if(buf[n-1] & 0x40)
86 atr_size++;
87 if(buf[n-1] & 0x80)
88 {
89 atr_size++;
90 TDi_exists=atr_size;
91 }
92 atr_size+=(buf[n-1] & 0x0F); // historical bytes
93 }
94 if( (TDi_exists>0) && (n==TDi_exists) )
95 {
96 TDi_exists=0;
97 // high nibble = TA1 , TB1 , TC1 , TD1
98 if(buf[n-1] & 0x10)
99 atr_size++;
100 if(buf[n-1] & 0x20)
101 atr_size++;
102 if(buf[n-1] & 0x40)
103 atr_size++;
104 if(buf[n-1] & 0x80)
105 {
106 atr_size++;
107 TDi_exists=atr_size;
108 }
109 }
110 }
111#else
112 while(n<SCI_MAX_ATR_SIZE && IO_Serial_Read(IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
113 {
114 n++;
115 }
116
117 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
118 buf[0] = 0x3B;
119#endif
120
121 if(n==0)
122 return ERROR;
123
124
125 (*atr) = ATR_New ();
126 if(ATR_InitFromArray ((*atr), buf, n) == ATR_OK)
127 {
128 struct timespec req_ts;
129 req_ts.tv_sec = 0;
130 req_ts.tv_nsec = 50000000;
131 nanosleep (&req_ts, NULL);
132 if (ioctl(reader[ridx].handle, IOCTL_SET_ATR_READY)<0)
133 return ERROR;
134 return OK;
135 }
136 else
137 {
138 ATR_Delete (*atr);
139 (*atr) = NULL;
140 return ERROR;
141 }
142}
143
144#endif
Note: See TracBrowser for help on using the repository browser.