source: trunk/reader-common.c@ 109

Last change on this file since 109 was 94, checked in by smurzch2, 11 years ago

Wait for local cards on oscam startup before oening network ports

Thanks to okmikel for the patch.
This is controlled with the global config:
WaitForCards = 1

File size: 8.3 KB
Line 
1#include "globals.h"
2#include "reader-common.h"
3
4char oscam_device[128];
5int oscam_card_detect;
6int mhz;
7int reader_irdeto_mode;
8
9uchar cta_cmd[272], cta_res[260], atr[64];
10ushort cta_lr, atr_size=0;
11static int cs_ptyp_orig; //reinit=1,
12
13#define SC_IRDETO 1
14#define SC_CRYPTOWORKS 2
15#define SC_VIACCESS 3
16#define SC_CONAX 4
17#define SC_SECA 5
18#define SC_VIDEOGUARD2 6
19
20static int reader_device_type(char *device, int typ)
21{
22 int rc=PORT_STD;
23#ifdef TUXBOX
24 struct stat sb;
25#endif
26
27 switch(reader[ridx].typ)
28 {
29 case R_MOUSE:
30 case R_SMART:
31 rc=PORT_STD;
32#ifdef TUXBOX
33 if (!stat(device, &sb))
34 {
35 if (S_ISCHR(sb.st_mode))
36 {
37 int dev_major, dev_minor;
38 dev_major=major(sb.st_rdev);
39 dev_minor=minor(sb.st_rdev);
40 if ((cs_hw==CS_HW_DBOX2) && ((dev_major==4) || (dev_major==5)))
41 switch(dev_minor & 0x3F)
42 {
43 case 0: rc=PORT_DB2COM1; break;
44 case 1: rc=PORT_DB2COM2; break;
45 }
46 cs_debug("device is major: %d, minor: %d, typ=%d", dev_major, dev_minor, rc);
47 }
48 }
49#endif
50 break;
51 case R_INTERN:
52 rc=PORT_SCI;
53 break;
54 }
55 return(rc);
56}
57
58static void reader_nullcard(void)
59{
60 reader[ridx].card_system=0;
61 memset(reader[ridx].hexserial, 0 , sizeof(reader[ridx].hexserial));
62 memset(reader[ridx].prid , 0xFF, sizeof(reader[ridx].prid ));
63 memset(reader[ridx].caid , 0 , sizeof(reader[ridx].caid ));
64 memset(reader[ridx].availkeys, 0 , sizeof(reader[ridx].availkeys));
65 reader[ridx].acs=0;
66 reader[ridx].nprov=0;
67}
68
69int reader_doapi(uchar dad, uchar *buf, int l, int dbg)
70{
71 int rc;
72 uchar sad;
73
74// oscam_card_inserted=4;
75 sad=2;
76 cta_lr=sizeof(cta_res)-1;
77 cs_ptyp_orig=cs_ptyp;
78 cs_ptyp=dbg;
79 //cs_ddump(buf, l, "send %d bytes to ctapi", l);
80 rc=CT_data(1, &dad, &sad, l, buf, &cta_lr, cta_res);
81 //cs_ddump(cta_res, cta_lr, "received %d bytes from ctapi with rc=%d", cta_lr, rc);
82 cs_ptyp=cs_ptyp_orig;
83 return(rc);
84}
85
86int reader_chkicc(uchar *buf, int l)
87{
88 return(reader_doapi(1, buf, l, D_WATCHDOG));
89}
90
91int reader_cmd2api(uchar *buf, int l)
92{
93 return(reader_doapi(1, buf, l, D_DEVICE));
94}
95
96int reader_cmd2icc(uchar *buf, int l)
97{
98// int rc;
99// if ((rc=reader_doapi(0, buf, l, D_DEVICE))<0)
100 return(reader_doapi(0, buf, l, D_DEVICE));
101// else
102// return(rc);
103}
104
105static int reader_activate_card()
106{
107 int i;
108 char ret;
109
110 cta_cmd[0] = CTBCS_INS_RESET;
111 cta_cmd[1] = CTBCS_P2_RESET_GET_ATR;
112 cta_cmd[2] = 0x00;
113
114 ret = reader_cmd2api(cta_cmd, 3);
115 if (ret!=OK)
116 {
117 cs_log("Error reset terminal: %d", ret);
118 return(0);
119 }
120
121 cta_cmd[0] = CTBCS_CLA;
122 cta_cmd[1] = CTBCS_INS_STATUS;
123 cta_cmd[2] = CTBCS_P1_CT_KERNEL;
124 cta_cmd[3] = CTBCS_P2_STATUS_ICC;
125 cta_cmd[4] = 0x00;
126
127// ret=reader_cmd2api(cmd, 11); warum 11 ??????
128 ret=reader_cmd2api(cta_cmd, 5);
129 if (ret!=OK)
130 {
131 cs_log("Error getting status of terminal: %d", ret);
132 return(0);
133 }
134 if (cta_res[0]!=CTBCS_DATA_STATUS_CARD_CONNECT)
135 return(0);
136
137 /* Activate card */
138// for (i=0; (i<5) && ((ret!=OK)||(cta_res[cta_lr-2]!=0x90)); i++)
139 for (i=0; i<5; i++)
140 {
141 reader_irdeto_mode = i%2 == 1;
142 cta_cmd[0] = CTBCS_CLA;
143 cta_cmd[1] = CTBCS_INS_REQUEST;
144 cta_cmd[2] = CTBCS_P1_INTERFACE1;
145 cta_cmd[3] = CTBCS_P2_REQUEST_GET_ATR;
146 cta_cmd[4] = 0x00;
147
148 ret=reader_cmd2api(cta_cmd, 5);
149 if ((ret==OK)||(cta_res[cta_lr-2]==0x90))
150 {
151 i=100;
152 break;
153 }
154 cs_log("Error activating card: %d", ret);
155 cs_sleepms(500);
156 }
157 if (i<100) return(0);
158
159 /* Store ATR */
160 atr_size=cta_lr-2;
161 memcpy(atr, cta_res, atr_size);
162#ifdef CS_RDR_INIT_HIST
163 reader[ridx].init_history_pos=0;
164 memset(reader[ridx].init_history, 0, sizeof(reader[ridx].init_history));
165#endif
166 cs_ri_log("ATR: %s", cs_hexdump(1, atr, atr_size));
167 sleep(1);
168 return(1);
169}
170
171void reader_card_info()
172{
173 int rc=-1;
174 if (rc=reader_checkhealth())
175 {
176 client[cs_idx].last=time((time_t)0);
177 cs_ri_brk(0);
178 switch(reader[ridx].card_system)
179 {
180 case SC_IRDETO:
181 rc=irdeto_card_info(); break;
182 case SC_CRYPTOWORKS:
183 rc=cryptoworks_card_info(); break;
184 case SC_VIACCESS:
185 rc=viaccess_card_info(); break;
186 case SC_CONAX:
187 rc=conax_card_info(); break;
188 case SC_VIDEOGUARD2:
189 rc=videoguard_card_info(); break;
190 case SC_SECA:
191 rc=seca_card_info(); break;
192 default: rc=0;
193 }
194 }
195// return(rc);
196}
197
198static int reader_get_cardsystem(void)
199{
200 if (irdeto_card_init(atr, atr_size)) reader[ridx].card_system=SC_IRDETO;
201 if (conax_card_init(atr, atr_size)) reader[ridx].card_system=SC_CONAX;
202 if (cryptoworks_card_init(atr, atr_size)) reader[ridx].card_system=SC_CRYPTOWORKS;
203 if (seca_card_init(atr, atr_size)) reader[ridx].card_system=SC_SECA;
204 if (viaccess_card_init(atr, atr_size)) reader[ridx].card_system=SC_VIACCESS;
205 if (videoguard_card_init(atr, atr_size)) reader[ridx].card_system=SC_VIDEOGUARD2;
206 if (!reader[ridx].card_system) cs_ri_log("card system not supported");
207 cs_ri_brk(1);
208 return(reader[ridx].card_system);
209}
210
211static int reader_reset(void)
212{
213 reader_nullcard();
214 if (!reader_activate_card()) return(0);
215 return(reader_get_cardsystem());
216}
217
218static int reader_card_inserted(void)
219{
220 cta_cmd[0]=CTBCS_CLA;
221 cta_cmd[1]=CTBCS_INS_STATUS;
222 cta_cmd[2]=CTBCS_P1_INTERFACE1;
223 cta_cmd[3]=CTBCS_P2_STATUS_ICC;
224 cta_cmd[4]=0x00;
225
226 return(reader_chkicc(cta_cmd, 5) ? 0 : cta_res[0]);
227}
228
229int reader_device_init(char *device, int typ)
230{
231 int rc;
232 oscam_card_detect=reader[ridx].detect;
233 mhz=reader[ridx].mhz;
234 cs_ptyp_orig=cs_ptyp;
235 cs_ptyp=D_DEVICE;
236 snprintf(oscam_device, sizeof(oscam_device), "%s", device);
237 if ((rc=CT_init(1, reader_device_type(device, typ),reader[ridx].typ))!=OK)
238 cs_log("Cannot open device: %s", device);
239 cs_debug("ct_init on %s: %d", device, rc);
240 cs_ptyp=cs_ptyp_orig;
241 return((rc!=OK) ? 2 : 0);
242}
243
244int reader_checkhealth(void)
245{
246 if (reader_card_inserted())
247 {
248 if (!(reader[ridx].card_status & CARD_INSERTED))
249 {
250 cs_log("card detected");
251 reader[ridx].card_status = CARD_NEED_INIT;
252 reader[ridx].card_status = CARD_INSERTED | (reader_reset() ? 0 : CARD_FAILURE);
253 if (reader[ridx].card_status & CARD_FAILURE)
254 {
255 cs_log("card initializing error");
256 }
257 else
258 {
259 client[cs_idx].au=ridx;
260 reader_card_info();
261 }
262
263 int i;
264 for( i=1; i<CS_MAXPID; i++ ) {
265 if( client[i].pid && client[i].typ=='c' && client[i].usr[0] ) {
266 kill(client[i].pid, SIGQUIT);
267 }
268 }
269 }
270 }
271 else
272 {
273 if (reader[ridx].card_status & CARD_INSERTED)
274 {
275 reader_nullcard();
276 client[cs_idx].lastemm=0;
277 client[cs_idx].lastecm=0;
278 client[cs_idx].au=-1;
279 extern int io_serial_need_dummy_char;
280 io_serial_need_dummy_char=0;
281 cs_log("card ejected");
282 }
283 reader[ridx].card_status=0;
284 reader[ridx].online=0;
285 }
286 return reader[ridx].card_status==CARD_INSERTED;
287}
288
289int reader_ecm(ECM_REQUEST *er)
290{
291 int rc=-1;
292 if( (rc=reader_checkhealth()) )
293 {
294 if( (reader[ridx].caid[0]>>8)==((er->caid>>8)&0xFF) )
295 {
296 client[cs_idx].last_srvid=er->srvid;
297 client[cs_idx].last_caid=er->caid;
298 client[cs_idx].last=time((time_t)0);
299 switch(reader[ridx].card_system)
300 {
301 case SC_IRDETO:
302 rc=(irdeto_do_ecm(er)) ? 1 : 0; break;
303 case SC_CRYPTOWORKS:
304 rc=(cryptoworks_do_ecm(er)) ? 1 : 0; break;
305 case SC_VIACCESS:
306 rc=(viaccess_do_ecm(er)) ? 1 : 0; break;
307 case SC_CONAX:
308 rc=(conax_do_ecm(er)) ? 1 : 0; break;
309 case SC_SECA:
310 rc=(seca_do_ecm(er)) ? 1 : 0; break;
311 case SC_VIDEOGUARD2:
312 rc=(videoguard_do_ecm(er)) ? 1 : 0; break;
313 default: rc=0;
314 }
315 }
316 else
317 rc=0;
318 }
319 return(rc);
320}
321
322int reader_emm(EMM_PACKET *ep)
323{
324 int rc=-1;
325 if (rc=reader_checkhealth())
326 {
327 client[cs_idx].last=time((time_t)0);
328 switch(reader[ridx].card_system)
329 {
330 case SC_IRDETO:
331 rc=irdeto_do_emm(ep); break;
332 case SC_CRYPTOWORKS:
333 rc=cryptoworks_do_emm(ep); break;
334 case SC_VIACCESS:
335 rc=viaccess_do_emm(ep); break;
336 case SC_CONAX:
337 rc=conax_do_emm(ep); break;
338 case SC_SECA:
339 rc=seca_do_emm(ep); break;
340 case SC_VIDEOGUARD2:
341 rc=videoguard_do_emm(ep); break;
342 default: rc=0;
343 }
344 }
345 return(rc);
346}
347
Note: See TracBrowser for help on using the repository browser.