Changeset 565
- Timestamp:
- 11/29/09 21:07:37 (14 years ago)
- Location:
- trunk
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/csctapi/atr.c
r552 r565 32 32 */ 33 33 34 static unsigned long atr_fs_table[16] = { 3571200L, 5000000L, 6000000L, 8000000L, 12000000L, 16000000L, 20000000L , 0L, 0L, 5000000L, 7500000L, 10000000L, 15000000L, 20000000L , 0L, 0L};34 static unsigned long atr_fs_table[16] = {0, 5000000L, 6000000L, 8000000L, 12000000L, 16000000L, 20000000L, 0, 0, 5000000L, 7500000L, 10000000L, 15000000L, 20000000L, 0, 0}; 35 35 36 36 static unsigned atr_num_ib_table[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; … … 42 42 unsigned atr_f_table[16] = {0, 372, 558, 744, 1116, 1488, 1860, 0, 0, 512, 768, 1024, 1536, 2048, 0, 0}; 43 43 44 double atr_d_table[16] = { 0, 1, 2, 4, 8, 16, 32, 0, 12, 20, 0.5, 0.25, 125, 0.0625, 0.03125, 0.015625};44 double atr_d_table[16] = {0, 1, 2, 4, 8, 16, 0, 0, 0, 0, 0.5, 0.25, 125, 0.0625, 0.03125, 0.015625}; 45 45 46 46 unsigned atr_i_table[4] = {25, 50, 100, 0}; -
trunk/csctapi/cardterminal.c
r552 r565 71 71 } 72 72 73 char CardTerminal_Init (CardTerminal * ct, unsigned short pn, unsigned long frequency,int reader_type)73 char CardTerminal_Init (CardTerminal * ct, unsigned short pn, int reader_type) 74 74 { 75 75 char ret; … … 99 99 100 100 /* Initialise serial port */ 101 if (!IO_Serial_Init (ct->io, pn + 1, usbserial, frequency, reader_type,FALSE))101 if (!IO_Serial_Init (ct->io, pn + 1, usbserial, FALSE)) 102 102 { 103 103 IO_Serial_Delete (ct->io); -
trunk/csctapi/cardterminal.h
r552 r565 68 68 /* Intialice a CardTerminal in a given port */ 69 69 extern char 70 CardTerminal_Init (CardTerminal * ct, unsigned short pn, unsigned long frequency,int reader_type);70 CardTerminal_Init (CardTerminal * ct, unsigned short pn, int reader_type); 71 71 72 72 /* Send a CT-BCS command to a CardTerminal */ -
trunk/csctapi/config.h
r557 r565 6 6 /* Debug CT-API */ 7 7 /* #undef DEBUG_CTAPI */ 8 // #define DEBUG_CTAPI 18 // #define DEBUG_CTAPI 1 9 9 10 10 /* Debug Integrated Circuit Card */ 11 11 /* #undef DEBUG_ICC */ 12 // #define DEBUG_ICC 112 // #define DEBUG_ICC 1 13 13 14 14 /* Debug Interface Device */ … … 22 22 /* Debug Input/Output */ 23 23 /* #undef DEBUG_IO */ 24 // #define DEBUG_IO 124 // #define DEBUG_IO 1 25 25 26 26 /* Debug Protocol */ -
trunk/csctapi/ctapi.c
r552 r565 50 50 */ 51 51 52 char CT_init (unsigned short ctn, unsigned short pn, unsigned long frequency,int reader_type)52 char CT_init (unsigned short ctn, unsigned short pn, int reader_type) 53 53 { 54 54 CardTerminal *ct; … … 69 69 { 70 70 /* Initialize CardTerminal */ 71 ret = CardTerminal_Init (ct, pn, frequency, reader_type);71 ret = CardTerminal_Init (ct, pn, reader_type); 72 72 73 73 /* Add CardTerminal to list */ -
trunk/csctapi/ctapi.h
r552 r565 22 22 unsigned short Ctn, /* Terminal Number */ 23 23 unsigned short pn, /* Port Number */ 24 unsigned long frequency, /* smartreader+ freq */25 24 int reader_type /* reader type (mouse, smartreader) 26 25 ); -
trunk/csctapi/ifd_towitoko.c
r559 r565 147 147 { 148 148 IO_Serial_Properties props; 149 SR_Config sr_config;150 149 int ret; 151 150 … … 187 186 props.parity = IO_SERIAL_PARITY_EVEN; 188 187 props.dtr = IO_SERIAL_HIGH; 189 if (io->reader_type == RTYP_SMART) 190 { 191 props.rts = IO_SERIAL_HIGH; 192 } 193 else 194 { 195 props.rts = IO_SERIAL_LOW; 196 } 197 198 199 // set smartreader+ default values 200 // for Irdeto card, the Frequency is 6.00MHz and the F parameter need to be set to 558 201 // test have shown that an irdeto card still reply to an ATR at 9600 with a Freq of 3.5712 MHz 202 // we need to do more test to see if this work with irdeto cards so I'm reverting my changes 203 // to use the original default smartreader+ values 204 sr_config.F=372; 205 sr_config.D=1.0; 206 sr_config.fs=3571200; 207 sr_config.N=0; 208 sr_config.T=0; 209 sr_config.inv=0; 210 211 if (!IO_Serial_SetProperties (io, &props,&sr_config)) 188 // props.dtr = IO_SERIAL_LOW; 189 // props.rts = IO_SERIAL_HIGH; 190 props.rts = IO_SERIAL_LOW; 191 192 193 if (!IO_Serial_SetProperties (io, &props)) 212 194 return IFD_TOWITOKO_IO_ERROR; 213 195 … … 278 260 { 279 261 IO_Serial_Properties props; 280 SR_Config sr_config;281 262 282 263 if(ifd->io->com==RTYP_SCI) … … 298 279 299 280 /* Get current settings */ 300 if (!IO_Serial_GetProperties (ifd->io, &props , &sr_config))281 if (!IO_Serial_GetProperties (ifd->io, &props)) 301 282 return IFD_TOWITOKO_IO_ERROR; 302 283 … … 309 290 props.input_bitrate = baudrate; 310 291 311 if (!IO_Serial_SetProperties (ifd->io, &props , &sr_config))292 if (!IO_Serial_SetProperties (ifd->io, &props)) 312 293 return IFD_TOWITOKO_IO_ERROR; 313 294 … … 318 299 { 319 300 IO_Serial_Properties props; 320 SR_Config sr_config; 321 301 322 302 if(ifd->io->com==RTYP_SCI) 323 303 { … … 326 306 327 307 /* Get current settings */ 328 if (!IO_Serial_GetProperties (ifd->io, &props , &sr_config))308 if (!IO_Serial_GetProperties (ifd->io, &props)) 329 309 return IFD_TOWITOKO_IO_ERROR; 330 310 … … 337 317 { 338 318 IO_Serial_Properties props; 339 SR_Config sr_config; 340 319 341 320 if(ifd->io->com==RTYP_SCI) 342 321 { … … 354 333 355 334 /* Get current settings */ 356 if (!IO_Serial_GetProperties (ifd->io, &props , &sr_config))335 if (!IO_Serial_GetProperties (ifd->io, &props)) 357 336 return IFD_TOWITOKO_IO_ERROR; 358 337 … … 361 340 props.parity = parity; 362 341 363 if (!IO_Serial_SetProperties (ifd->io, &props , &sr_config))342 if (!IO_Serial_SetProperties (ifd->io, &props)) 364 343 return IFD_TOWITOKO_IO_ERROR; 365 344 } … … 807 786 IFD_Towitoko_SetParity (ifd, IFD_TOWITOKO_PARITY_NONE); 808 787 #endif 809 if(atr!=NULL && ret == IFD_TOWITOKO_OK && ifd->io!=NULL)810 {811 double tmp_param;812 BYTE tmp;813 if((ifd->io)->SmartReaderConf!=NULL)814 {815 // if we get here we have a valid ATR and can init the IO->SmartReaderConf structure.816 ATR_GetParameter((*atr), ATR_PARAMETER_F, &tmp_param);817 ((ifd->io)->SmartReaderConf)->F=(int)tmp_param;818 819 ATR_GetParameter((*atr), ATR_PARAMETER_D, &tmp_param);820 ((ifd->io)->SmartReaderConf)->D=(float)tmp_param;821 822 ((ifd->io)->SmartReaderConf)->fs = (ifd->io)->frequency;823 824 ATR_GetParameter((*atr), ATR_PARAMETER_N, &tmp_param);825 ((ifd->io)->SmartReaderConf)->N=(int)tmp_param;826 827 ATR_GetProtocolType((*atr), 2, &tmp);828 ((ifd->io)->SmartReaderConf)->T = (int)tmp_param && 0xFF;829 ATR_GetConvention((*atr), &((ifd->io)->SmartReaderConf)->inv);830 }831 }832 788 return ret; 833 789 } -
trunk/csctapi/io_serial.c
r563 r565 51 51 #endif 52 52 53 #define IO_SERIAL_FILENAME_LENGTH 32 53 54 54 55 /* … … 56 57 */ 57 58 58 static int IO_Serial_Bitrate_to_Speed(int bitrate); 59 60 static int IO_Serial_Bitrate_from_Speed(int speed); 59 static int IO_Serial_Bitrate(int bitrate); 61 60 62 61 static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms); … … 70 69 static void IO_Serial_Clear (IO_Serial * io); 71 70 72 static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props , SR_Config *sr_config);73 74 static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props , SR_Config *sr_config);71 static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props); 72 73 static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props); 75 74 76 75 static void IO_Serial_ClearPropertiesCache (IO_Serial * io); 77 78 static bool IO_Serial_Set_Smartreader_Config(IO_Serial * io, SR_Config *sr_config);79 80 76 81 77 static int _in_echo_read = 0; … … 182 178 } 183 179 184 bool IO_Serial_Init (IO_Serial * io, unsigned com, bool usbserial, unsigned long frequency, unsigned short reader_type,bool pnp)180 bool IO_Serial_Init (IO_Serial * io, unsigned com, bool usbserial, bool pnp) 185 181 { 186 182 char filename[IO_SERIAL_FILENAME_LENGTH]; 187 188 io->frequency = frequency;189 io->reader_type = reader_type;190 183 191 184 IO_Serial_DeviceName (com, usbserial, filename, IO_SERIAL_FILENAME_LENGTH); 192 185 193 strncpy(io->filename,filename,IO_SERIAL_FILENAME_LENGTH); 194 195 #ifdef DEBUG_IO 196 printf ("IO: Opening serial port %s of type %d at freq %2.2fMHz\n", filename,io->reader_type,(float)(io->frequency)/1000000); 186 #ifdef DEBUG_IO 187 printf ("IO: Opening serial port %s\n", filename); 197 188 #endif 198 189 … … 241 232 } 242 233 243 bool IO_Serial_GetProperties (IO_Serial * io, IO_Serial_Properties * props, SR_Config *sr_config)234 bool IO_Serial_GetProperties (IO_Serial * io, IO_Serial_Properties * props) 244 235 { 245 236 struct termios currtio; … … 251 242 return FALSE; 252 243 #endif 253 if (IO_Serial_GetPropertiesCache(io, props,sr_config)) 244 245 if (IO_Serial_GetPropertiesCache(io, props)) 254 246 return TRUE; 255 247 … … 257 249 return FALSE; 258 250 259 o_speed = cfgetospeed(&currtio); 260 props->output_bitrate = IO_Serial_Bitrate_from_Speed(o_speed); 261 262 i_speed = cfgetispeed(&currtio); 263 props->input_bitrate = IO_Serial_Bitrate_from_Speed(i_speed); 251 o_speed = cfgetospeed (&currtio); 252 253 switch (o_speed) 254 { 255 #ifdef B0 256 case B0: 257 props->output_bitrate = 0; 258 break; 259 #endif 260 #ifdef B50 261 case B50: 262 props->output_bitrate = 50; 263 break; 264 #endif 265 #ifdef B75 266 case B75: 267 props->output_bitrate = 75; 268 break; 269 #endif 270 #ifdef B110 271 case B110: 272 props->output_bitrate = 110; 273 break; 274 #endif 275 #ifdef B134 276 case B134: 277 props->output_bitrate = 134; 278 break; 279 #endif 280 #ifdef B150 281 case B150: 282 props->output_bitrate = 150; 283 break; 284 #endif 285 #ifdef B200 286 case B200: 287 props->output_bitrate = 200; 288 break; 289 #endif 290 #ifdef B300 291 case B300: 292 props->output_bitrate = 300; 293 break; 294 #endif 295 #ifdef B600 296 case B600: 297 props->output_bitrate = 600; 298 break; 299 #endif 300 #ifdef B1200 301 case B1200: 302 props->output_bitrate = 1200; 303 break; 304 #endif 305 #ifdef B1800 306 case B1800: 307 props->output_bitrate = 1800; 308 break; 309 #endif 310 #ifdef B2400 311 case B2400: 312 props->output_bitrate = 2400; 313 break; 314 #endif 315 #ifdef B4800 316 case B4800: 317 props->output_bitrate = 4800; 318 break; 319 #endif 320 #ifdef B9600 321 case B9600: 322 props->output_bitrate = 9600; 323 break; 324 #endif 325 #ifdef B19200 326 case B19200: 327 props->output_bitrate = 19200; 328 break; 329 #endif 330 #ifdef B38400 331 case B38400: 332 props->output_bitrate = 38400; 333 break; 334 #endif 335 #ifdef B57600 336 case B57600: 337 props->output_bitrate = 57600; 338 break; 339 #endif 340 #ifdef B115200 341 case B115200: 342 props->output_bitrate = 115200; 343 break; 344 #endif 345 #ifdef B230400 346 case B230400: 347 props->output_bitrate = 230400; 348 break; 349 #endif 350 default: 351 props->output_bitrate = 1200; 352 break; 353 } 354 355 i_speed = cfgetispeed (&currtio); 356 357 switch (i_speed) 358 { 359 #ifdef B0 360 case B0: 361 props->input_bitrate = 0; 362 break; 363 #endif 364 #ifdef B50 365 case B50: 366 props->input_bitrate = 50; 367 break; 368 #endif 369 #ifdef B75 370 case B75: 371 props->input_bitrate = 75; 372 break; 373 #endif 374 #ifdef B110 375 case B110: 376 props->input_bitrate = 110; 377 break; 378 #endif 379 #ifdef B134 380 case B134: 381 props->input_bitrate = 134; 382 break; 383 #endif 384 #ifdef B150 385 case B150: 386 props->input_bitrate = 150; 387 break; 388 #endif 389 #ifdef B200 390 case B200: 391 props->input_bitrate = 200; 392 break; 393 #endif 394 #ifdef B300 395 case B300: 396 props->input_bitrate = 300; 397 break; 398 #endif 399 #ifdef B600 400 case B600: 401 props->input_bitrate = 600; 402 break; 403 #endif 404 #ifdef B1200 405 case B1200: 406 props->input_bitrate = 1200; 407 break; 408 #endif 409 #ifdef B1800 410 case B1800: 411 props->input_bitrate = 1800; 412 break; 413 #endif 414 #ifdef B2400 415 case B2400: 416 props->input_bitrate = 2400; 417 break; 418 #endif 419 #ifdef B4800 420 case B4800: 421 props->input_bitrate = 4800; 422 break; 423 #endif 424 #ifdef B9600 425 case B9600: 426 props->input_bitrate = 9600; 427 break; 428 #endif 429 #ifdef B19200 430 case B19200: 431 props->input_bitrate = 19200; 432 break; 433 #endif 434 #ifdef B38400 435 case B38400: 436 props->input_bitrate = 38400; 437 break; 438 #endif 439 #ifdef B57600 440 case B57600: 441 props->input_bitrate = 57600; 442 break; 443 #endif 444 #ifdef B115200 445 case B115200: 446 props->input_bitrate = 115200; 447 break; 448 #endif 449 #ifdef B230400 450 case B230400: 451 props->input_bitrate = 230400; 452 break; 453 #endif 454 default: 455 props->input_bitrate = 1200; 456 break; 457 } 264 458 265 459 switch (currtio.c_cflag & CSIZE) … … 302 496 props->rts = ((mctl & TIOCM_RTS) ? IO_SERIAL_HIGH : IO_SERIAL_LOW); 303 497 304 // set smartreader+ default values 305 // for Irdeto card, the Frequency is 6.00MHz and the F parameter need to be set to 558 306 // test have shown that an irdeto card still reply to an ATR at 9600 with a Freq of 3.5712 MHz 307 // we need to do more test to see if this work with irdeto cards so I'm reverting my changes 308 // to use the original default smartreader+ values 309 sr_config->F=372; 310 sr_config->D=1.0; 311 sr_config->fs=3571200; 312 sr_config->N=0; 313 sr_config->T=0; 314 sr_config->inv=0; 315 316 IO_Serial_SetPropertiesCache(io, props, sr_config); 498 IO_Serial_SetPropertiesCache (io, props); 317 499 318 500 #ifdef DEBUG_IO … … 323 505 } 324 506 325 bool IO_Serial_SetProperties (IO_Serial * io, IO_Serial_Properties * props, SR_Config *sr_config)507 bool IO_Serial_SetProperties (IO_Serial * io, IO_Serial_Properties * props) 326 508 { 327 509 struct termios newtio; … … 338 520 339 521 extern int mhz; 340 extern int reader_has_irdeto_card; 341 522 extern int reader_irdeto_mode; 342 523 343 524 if(io->reader_type==RTYP_SMART) 344 525 { 345 526 #ifdef DEBUG_IO 346 printf("IO: Smartreader+ .. setting config with a frequency of %2.2fMHz\n", (float) io->frequency / 1000000);347 #endif 348 if (!IO_Serial_Set_Smartreader_Config(io,sr_config))349 350 #ifdef DEBUG_IO 351 printf("IO: Smartreader+ .. ERROR setting configuration\n");352 #endif 353 354 527 printf("IO: SMARTREADER .. switching to frequency to %2.2fMHz\n", (float)mhz/100.0); 528 #endif 529 if(!IO_Serial_Set_Smartreader_Freq(io,mhz,reader_irdeto_mode)) 530 { 531 #ifdef DEBUG_IO 532 printf("IO: SMARTREADER .. ERROR switching to 6MHz\n"); 533 #endif 534 return FALSE; 535 } 355 536 } 356 537 538 if (mhz == 600) 539 { 540 541 /* for 6MHz */ 542 if (reader_irdeto_mode) 543 { 544 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 545 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 546 } 547 else 548 { 357 549 #ifdef OS_LINUX 358 int standard_card_clock; //contains non-overclocked, standard clockrate of the card in 10kHz steps 359 if (reader_has_irdeto_card) 360 standard_card_clock = 600; 550 /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */ 551 struct serial_struct nuts; 552 ioctl(io->fd, TIOCGSERIAL, &nuts); 553 nuts.custom_divisor = nuts.baud_base / 9600 * 3.57 / 6; 554 nuts.flags &= ~ASYNC_SPD_MASK; 555 nuts.flags |= ASYNC_SPD_CUST; 556 ioctl(io->fd, TIOCSSERIAL, &nuts); 557 cfsetospeed(&newtio, IO_Serial_Bitrate(38400)); 558 cfsetispeed(&newtio, IO_Serial_Bitrate(38400)); 559 #else 560 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 561 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 562 #endif 563 } 564 } 565 else if (mhz == 357 || mhz == 358) 566 { 567 /* for 3.57 MHz */ 568 if (reader_irdeto_mode) 569 { 570 #ifdef OS_LINUX 571 /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */ 572 struct serial_struct nuts; 573 ioctl(io->fd, TIOCGSERIAL, &nuts); 574 nuts.custom_divisor = nuts.baud_base / 5713; 575 nuts.flags &= ~ASYNC_SPD_MASK; 576 nuts.flags |= ASYNC_SPD_CUST; 577 ioctl(io->fd, TIOCSSERIAL, &nuts); 578 cfsetospeed(&newtio, IO_Serial_Bitrate(38400)); 579 cfsetispeed(&newtio, IO_Serial_Bitrate(38400)); 580 #else 581 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 582 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 583 #endif 584 } 585 else 586 { 587 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 588 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 589 } 590 } 591 else if (mhz == 800) 592 { 593 594 /* for 8MHz */ 595 if (reader_irdeto_mode) 596 { 597 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 598 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 599 } 600 else 601 { 602 #ifdef OS_LINUX 603 /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */ 604 struct serial_struct nuts; 605 ioctl(io->fd, TIOCGSERIAL, &nuts); 606 nuts.custom_divisor = nuts.baud_base / 9600 * 6 / 8; 607 nuts.flags &= ~ASYNC_SPD_MASK; 608 nuts.flags |= ASYNC_SPD_CUST; 609 ioctl(io->fd, TIOCSSERIAL, &nuts); 610 cfsetospeed(&newtio, IO_Serial_Bitrate(38400)); 611 cfsetispeed(&newtio, IO_Serial_Bitrate(38400)); 612 #else 613 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 614 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 615 #endif 616 } 617 } 618 else if (mhz == 1000) 619 { 620 621 /* for 10MHz */ 622 if (reader_irdeto_mode) 623 { 624 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 625 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 626 } 627 else 628 { 629 #ifdef OS_LINUX 630 /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */ 631 struct serial_struct nuts; 632 ioctl(io->fd, TIOCGSERIAL, &nuts); 633 nuts.custom_divisor = nuts.baud_base / 9600 * 6 / 10; 634 nuts.flags &= ~ASYNC_SPD_MASK; 635 nuts.flags |= ASYNC_SPD_CUST; 636 ioctl(io->fd, TIOCSSERIAL, &nuts); 637 cfsetospeed(&newtio, IO_Serial_Bitrate(38400)); 638 cfsetispeed(&newtio, IO_Serial_Bitrate(38400)); 639 #else 640 cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate)); 641 cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate)); 642 #endif 643 } 644 } 361 645 else 362 standard_card_clock = 357; 363 if (mhz == standard_card_clock) 364 #endif 365 { //no overclocking 366 cfsetospeed(&newtio, IO_Serial_Bitrate_to_Speed(props->output_bitrate)); 367 cfsetispeed(&newtio, IO_Serial_Bitrate_to_Speed(props->input_bitrate)); 646 { 647 /* invalid */ 648 return FALSE; 368 649 } 369 #ifdef OS_LINUX370 else { //over or underclocking371 /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */372 struct serial_struct nuts;373 ioctl(io->fd, TIOCGSERIAL, &nuts);374 int custom_baud = 9600 * mhz / standard_card_clock;375 nuts.custom_divisor = (nuts.baud_base + (custom_baud/2))/ custom_baud;376 cs_debug("customspeed: standardclock=%d mhz=%d custom_baud=%d baud_base=%d divisor=%d -> effective baudrate %d",377 standard_card_clock, mhz, custom_baud, nuts.baud_base, nuts.custom_divisor, nuts.baud_base/nuts.custom_divisor);378 nuts.flags &= ~ASYNC_SPD_MASK;379 nuts.flags |= ASYNC_SPD_CUST;380 ioctl(io->fd, TIOCSSERIAL, &nuts);381 cfsetospeed(&newtio, IO_Serial_Bitrate_to_Speed(38400));382 cfsetispeed(&newtio, IO_Serial_Bitrate_to_Speed(38400));383 }384 #endif385 650 386 651 /* Set the character size */ … … 459 724 IO_Serial_Ioctl_Lock(io, 0); 460 725 461 IO_Serial_SetPropertiesCache (io, props, sr_config);726 IO_Serial_SetPropertiesCache (io, props); 462 727 463 728 #ifdef DEBUG_IO … … 680 945 */ 681 946 682 static int IO_Serial_Bitrate_to_Speed(int bitrate) 683 { 684 static const struct BaudRates { int real; speed_t apival; } BaudRateTab[] = { 685 { 200, B200 }, { 300, B300 }, { 600, B600 }, 686 { 1200, B1200 }, { 2400, B2400 }, { 4800, B4800 }, 687 { 9600, B9600 }, { 19200, B19200 }, { 38400, B38400 }, 688 { 57600, B57600 }, { 115200, B115200 }, { 230400, B230400 } 689 }; 690 691 int i; 692 693 for(i=0; i<(int)(sizeof(BaudRateTab)/sizeof(struct BaudRates)); i++) 694 { 695 int b=BaudRateTab[i].real; 696 int d=((b-bitrate)*10000)/b; 697 if(abs(d)<=300) 698 { 699 return BaudRateTab[i].apival; 700 } 701 } 702 return B0; 703 } 704 705 static int IO_Serial_Bitrate_from_Speed(int speed) 706 { 707 708 switch (speed) 709 { 947 static int IO_Serial_Bitrate(int bitrate) 948 { 949 #ifdef B230400 950 if ((bitrate)>=230400) return B230400; 951 #endif 952 #ifdef B115200 953 if ((bitrate)>=115200) return B115200; 954 #endif 955 #ifdef B57600 956 if ((bitrate)>=57600) return B57600; 957 #endif 958 #ifdef B38400 959 if ((bitrate)>=38400) return B38400; 960 #endif 961 #ifdef B19200 962 if ((bitrate)>=19200) return B19200; 963 #endif 964 #ifdef B9600 965 if ((bitrate)>=9600) return B9600; 966 #endif 967 #ifdef B4800 968 if ((bitrate)>=4800) return B4800; 969 #endif 970 #ifdef B2400 971 if ((bitrate)>=2400) return B2400; 972 #endif 973 #ifdef B1800 974 if ((bitrate)>=1800) return B1800; 975 #endif 976 #ifdef B1200 977 if ((bitrate)>=1200) return B1200; 978 #endif 979 #ifdef B600 980 if ((bitrate)>=600) return B600; 981 #endif 982 #ifdef B300 983 if ((bitrate)>=300) return B300; 984 #endif 985 #ifdef B200 986 if ((bitrate)>=200) return B200; 987 #endif 988 #ifdef B150 989 if ((bitrate)>=150) return B150; 990 #endif 991 #ifdef B134 992 if ((bitrate)>=134) return B134; 993 #endif 994 #ifdef B110 995 if ((bitrate)>=110) return B110; 996 #endif 997 #ifdef B75 998 if ((bitrate)>=75) return B75; 999 #endif 1000 #ifdef B50 1001 if ((bitrate)>=50) return B50; 1002 #endif 710 1003 #ifdef B0 711 case B0: 712 return 0; 713 #endif 714 #ifdef B50 715 case B50: 716 return 50; 717 #endif 718 #ifdef B75 719 case B75: 720 return 75; 721 #endif 722 #ifdef B110 723 case B110: 724 return 110; 725 #endif 726 #ifdef B134 727 case B134: 728 return 134; 729 #endif 730 #ifdef B150 731 case B150: 732 return 150; 733 #endif 734 #ifdef B200 735 case B200: 736 return 200; 737 #endif 738 #ifdef B300 739 case B300: 740 return 300; 741 #endif 742 #ifdef B600 743 case B600: 744 return 600; 745 #endif 746 #ifdef B1200 747 case B1200: 748 return 1200; 749 #endif 750 #ifdef B1800 751 case B1800: 752 return 1800; 753 #endif 754 #ifdef B2400 755 case B2400: 756 return 2400; 757 #endif 758 #ifdef B4800 759 case B4800: 760 return 4800; 761 #endif 762 #ifdef B9600 763 case B9600: 764 return 9600; 765 #endif 766 #ifdef B19200 767 case B19200: 768 return 19200; 769 #endif 770 #ifdef B38400 771 case B38400: 772 return 38400; 773 #endif 774 #ifdef B57600 775 case B57600: 776 return 57600; 777 #endif 778 #ifdef B115200 779 case B115200: 780 return 115200; 781 #endif 782 #ifdef B230400 783 case B230400: 784 return 230400; 785 #endif 786 default: 787 return 1200; 788 } 789 1004 if ((bitrate)>=0) return B0; 1005 #endif 790 1006 return 0; /* Should never get here */ 791 1007 } 792 793 1008 794 1009 static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms) … … 913 1128 } 914 1129 915 static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props , SR_Config *sr_config)1130 static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props) 916 1131 { 917 1132 #ifdef SCI_DEV … … 922 1137 if (io->props == NULL) 923 1138 io->props = (IO_Serial_Properties *) malloc (sizeof (IO_Serial_Properties)); 924 925 if (io->SmartReaderConf == NULL)926 io->SmartReaderConf = (SR_Config *) malloc(sizeof (SR_Config));927 928 1139 #ifdef DEBUG_IO 929 1140 printf ("IO: Catching properties\n"); 930 1141 #endif 931 if(props) 932 memcpy (io->props, props, sizeof (IO_Serial_Properties)); 933 if(sr_config) 934 memcpy(io->SmartReaderConf, sr_config, sizeof (SR_Config)); 935 } 936 937 static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props, SR_Config *sr_config) 938 { 939 bool ok_props, ok_sr; 940 941 ok_props=FALSE; 942 ok_sr=FALSE; 943 if (io->props != NULL && props != NULL) 944 { 945 memcpy(props, io->props, sizeof (IO_Serial_Properties)); 946 # ifdef DEBUG_IO 947 printf("IO: Getting properties (catched): %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", 948 props->input_bitrate, props->bits, 949 props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", 950 props->stopbits, 951 props->dtr, 952 props->rts); 953 # endif 954 ok_props=TRUE; 955 } 956 957 if (io->SmartReaderConf != NULL && sr_config != NULL) 958 { 959 memcpy(sr_config, io->SmartReaderConf, sizeof (SR_Config)); 960 # ifdef DEBUG_IO 961 printf("IO: Getting SmartReader+ config (catched): F=%d; D=%f; fs=%d; N=%d; T=%d; inv=%d \n", 962 sr_config->F, 963 sr_config->D, 964 sr_config->fs, 965 sr_config->N, 966 sr_config->T, 967 sr_config->inv); 968 # endif 969 ok_sr=TRUE; 970 } 971 else 972 { 973 // no props so they are ok by default 974 ok_sr=TRUE; 975 } 976 977 978 # ifdef DEBUG_IO 979 printf("IO: Getting properties (catched): done\n"); 980 # endif 981 return ok_props && ok_sr; 1142 1143 memcpy (io->props, props, sizeof (IO_Serial_Properties)); 1144 } 1145 1146 static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props) 1147 { 1148 if (io->props != NULL) 1149 { 1150 memcpy (props, io->props, sizeof (IO_Serial_Properties)); 1151 #if 0 1152 #ifdef DEBUG_IO 1153 printf("IO: Getting properties (catched): %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts); 1154 #endif 1155 #endif 1156 return TRUE; 1157 } 1158 1159 return FALSE; 982 1160 } 983 1161 … … 1007 1185 { 1008 1186 IO_Serial_Properties props; 1009 SR_Config srConfig;1010 1011 1187 int i = 0; 1012 1188 props.input_bitrate = 1200; … … 1019 1195 props.rts = IO_SERIAL_LOW; 1020 1196 1021 // set smartreader+ default values 1022 // use the frequency to get F for the smartreader 1023 // to make sure the ATR is sent at 9600. 1024 srConfig.F=io->frequency/9600; 1025 srConfig.D=1.0; 1026 srConfig.fs=io->frequency; 1027 srConfig.N=0; 1028 srConfig.T=0; 1029 srConfig.inv=0; 1030 1031 if (!IO_Serial_SetProperties(io, &props, &srConfig)) 1197 if (!IO_Serial_SetProperties (io, &props)) 1032 1198 return FALSE; 1033 1199 1034 while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read (io, 1000, 1, &(io->PnP_id[i]))) 1035 //while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read (io, 200, 1, &(io->PnP_id[i]))) 1200 while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read (io, 200, 1, &(io->PnP_id[i]))) 1036 1201 i++; 1037 1202 … … 1041 1206 1042 1207 1043 static bool IO_Serial_Set_Smartreader_Config(IO_Serial * io, SR_Config *sr_config) 1044 { 1045 int F=sr_config->F; 1046 float D=sr_config->D; 1047 int fs=sr_config->fs; 1048 int N=sr_config->N; 1049 int T=sr_config->T; 1050 int inv=sr_config->inv; 1051 1052 fs/=1000; // convert to kHz. 1053 # ifdef DEBUG_IO 1054 printf("IO: Smartreader+ on %s: SR+ options F=%d D=%f fs=%d N=%d T=%d inv=%d\n",io->filename,F,D,fs,N,T,inv); 1055 # endif 1056 1057 // Set SmartReader+ in CMD mode. 1058 struct termios term; 1059 if(tcgetattr(io->fd,&term)==-1) 1060 { 1061 # ifdef DEBUG_IO 1062 printf("%s: tcgetattr failed: %s",io->filename,strerror(errno)); 1063 #endif 1064 return FALSE; 1065 } 1066 1067 term.c_cflag&=~CSIZE; 1068 term.c_cflag|=CS5; 1069 if(tcsetattr(io->fd,TCSADRAIN,&term)==-1) 1070 { 1071 # ifdef DEBUG_IO 1072 printf("%s: tcsetattr failed: %s",io->filename,strerror(errno)); 1073 #endif 1074 return FALSE; 1075 } 1076 // Write SmartReader+ configuration commands. 1077 unsigned char cmd[16]; 1078 1079 //XXX how is (int)D supposed to work for fractional values e.g. 0.125 ?? 1080 cmd[0]=1; cmd[1]=F>>8; cmd[2]=F; cmd[3]=(int)D; 1081 if(!IO_Serial_Write(io, 0, 4, cmd)) 1082 return FALSE; 1083 1084 cmd[0]=2; cmd[1]=fs>>8; cmd[2]=fs; 1085 if(!IO_Serial_Write(io, 0, 3, cmd)) 1086 return FALSE; 1087 1088 cmd[0]=3; cmd[1]=N; 1089 if(!IO_Serial_Write(io, 0, 2, cmd)) 1090 return FALSE; 1091 1092 cmd[0]=4; cmd[1]=T; 1093 if(!IO_Serial_Write(io, 0, 2, cmd)) 1094 return FALSE; 1095 1096 cmd[0]=5; cmd[1]=inv; 1097 if(!IO_Serial_Write(io, 0, 2, cmd)) 1098 return FALSE; 1099 1100 // Send zero bits for 0.25 - 0.5 seconds. 1101 if(tcsendbreak(io->fd,0)==-1) 1102 { 1103 # ifdef DEBUG_IO 1104 printf("%s: tcsendbreak failed: %s",io->filename,strerror(errno)); 1105 #endif 1106 return FALSE; 1107 } 1108 // We're entering SmartReader+ mode; speed up serial communication. 1109 // B230400 is the highest setting that sticks. 1110 cfsetispeed(&term,B230400); 1111 cfsetospeed(&term,B230400); 1112 // Set SmartReader+ in DATA mode. 1113 term.c_cflag&=~CSIZE; 1114 term.c_cflag|=CS8; 1115 if(tcsetattr(io->fd,TCSADRAIN,&term)==-1) 1116 { 1117 # ifdef DEBUG_IO 1118 printf("%s: tcsetattr failed: %s",io->filename,strerror(errno)); 1119 #endif 1120 return FALSE; 1121 } 1122 return TRUE; 1123 } 1124 1208 bool IO_Serial_Set_Smartreader_Freq(IO_Serial * io, int freq, int irdeto_mode) 1209 { 1210 struct termios term; 1211 struct termios orig; 1212 unsigned int u; 1213 unsigned char fi_di[4]={0x01, 0x01, 0x74, 0x01}; 1214 unsigned char fr[3]={0x02, 0x00, 0x00}; 1215 unsigned char nn[2]={0x03, 0x00}; 1216 unsigned char pr[2]={0x04, 0x00}; 1217 unsigned char in[2]={0x05, 0x00}; 1218 1219 orig=term; 1220 1221 /* set smartreader in CMD mode */ 1222 tcgetattr(io->fd, &term); 1223 1224 term.c_cflag &= ~CSIZE; 1225 term.c_cflag |= CS5; 1226 cfsetospeed(&term, 9600); 1227 cfsetispeed(&term, 9600); 1228 tcsetattr(io->fd, TCSANOW, &term); 1229 1230 // our freq comes in as 358, 357 or 600 so it needs this to be in KHz for the FR command 1231 freq*=10; 1232 fr[1]=(unsigned char)((freq & 0xff00)>>8); 1233 fr[2]=(unsigned char)(freq & 0x00ff); 1234 1235 // Irdeto card supposedly need NN set to 1 .. to be confirmed 1236 if(irdeto_mode) 1237 nn[1]=0x01; 1238 1239 // send the commands 1240 IO_Serial_Write (io, 0, 4, fi_di); 1241 IO_Serial_Flush(io); 1242 1243 IO_Serial_Write (io, 0, 3, fr); 1244 IO_Serial_Flush(io); 1245 1246 IO_Serial_Write (io, 0, 2, nn); 1247 IO_Serial_Flush(io); 1248 1249 IO_Serial_Write (io, 0, 2, pr); 1250 IO_Serial_Flush(io); 1251 1252 IO_Serial_Write (io, 0, 2, in); 1253 IO_Serial_Flush(io); 1254 1255 /* set smartreader in DATA mode */ 1256 tcgetattr(io->fd, &orig); 1257 orig.c_cflag &= ~CSIZE; 1258 orig.c_cflag |= CS8; 1259 tcsetattr(io->fd, TCSANOW, &orig); 1260 1261 return TRUE; 1262 } 1263 -
trunk/csctapi/io_serial.h
r556 r565 48 48 #define IO_SERIAL_PNPID_SIZE 256 49 49 50 #define IO_SERIAL_FILENAME_LENGTH 3251 52 50 /* 53 51 * Exported datatypes definition … … 66 64 } 67 65 IO_Serial_Properties; 68 /* structure holding ATR value to be used for the smargo smartreader+ */69 typedef struct {70 int F;71 float D;72 int fs;73 int N;74 int T;75 int inv;76 } SR_Config;77 66 78 67 /* IO_Serial exported datatype */ 79 68 typedef struct 80 69 { 81 char filename[IO_SERIAL_FILENAME_LENGTH];82 70 int fd; /* Handle of the serial device */ 83 71 IO_Serial_Properties * props; … … 88 76 int wr; 89 77 int reader_type; 90 unsigned long frequency; /* Frequency (Hz) */91 SR_Config *SmartReaderConf;92 78 } 93 79 IO_Serial; … … 104 90 105 91 /* Initialization and closing */ 106 extern bool IO_Serial_Init (IO_Serial * io, unsigned com, bool usbserial, unsigned long frequency, unsigned short reader_type,bool pnp);92 extern bool IO_Serial_Init (IO_Serial * io, unsigned com, bool usbserial, bool pnp); 107 93 extern bool IO_Serial_Close (IO_Serial * io); 108 94 109 95 /* Transmission properties */ 110 extern bool IO_Serial_SetProperties (IO_Serial * io, IO_Serial_Properties * props, SR_Config *sr_config);111 extern bool IO_Serial_GetProperties (IO_Serial * io, IO_Serial_Properties * props, SR_Config *sr_config);96 extern bool IO_Serial_SetProperties (IO_Serial * io, IO_Serial_Properties * props); 97 extern bool IO_Serial_GetProperties (IO_Serial * io, IO_Serial_Properties * props); 112 98 extern bool IO_Serial_DTR_RTS(IO_Serial * io, int, int); 113 99 #if defined(TUXBOX) && defined(PPC) … … 125 111 extern void IO_Serial_GetPnPId (IO_Serial * io, BYTE * pnp_id, unsigned *length); 126 112 113 /* smartreader Frequency set */ 114 extern bool IO_Serial_Set_Smartreader_Freq(IO_Serial * io, int freq, int irdeto_mode); 115 127 116 #endif /* IO_SERIAL */ -
trunk/csctapi/pps.c
r556 r565 1 2 1 /* 3 2 pps.c … … 50 49 */ 51 50 52 static int PPS_Exchange (PPS * pps, BYTE * params, unsigned *length);53 54 static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply);55 56 static unsigned PPS_GetLength (BYTE * block);57 58 static int PPS_InitICC (PPS * pps);59 60 static int PPS_InitProtocol (PPS * pps);61 62 static void PPS_SelectFirstProtocol (PPS * pps);63 64 static BYTE PPS_GetPCK (BYTE * block, unsigned length);51 static int PPS_Exchange (PPS * pps, BYTE * params, unsigned *length); 52 53 static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * reply, unsigned len_reply); 54 55 static unsigned PPS_GetLength (BYTE * block); 56 57 static int PPS_InitICC (PPS * pps); 58 59 static int PPS_InitProtocol (PPS * pps); 60 61 static void PPS_SelectFirstProtocol (PPS * pps); 62 63 static BYTE PPS_GetPCK (BYTE * block, unsigned length); 65 64 66 65 /* … … 68 67 */ 69 68 70 PPS * PPS_New(ICC_Async * icc)69 PPS * PPS_New (ICC_Async * icc) 71 70 { 72 71 PPS *pps; 73 74 pps = (PPS *) malloc(sizeof (PPS)); 75 76 if (pps != NULL) { 72 73 pps = (PPS *) malloc (sizeof (PPS)); 74 75 if (pps != NULL) 76 { 77 77 pps->icc = icc; 78 78 pps->protocol = NULL; … … 82 82 pps->parameters.n = ATR_DEFAULT_N; 83 83 } 84 84 85 85 return pps; 86 86 } 87 87 88 int PPS_PreformPTS(PPS *pps) 89 { 88 int PPS_Perform (PPS * pps, BYTE * params, unsigned *length) 89 { 90 ATR *atr; 90 91 int ret; 91 unsigned long baudrate; 92 BYTE req[4]; 92 93 /* Perform PPS Exchange if requested */ 94 if ((*length) > 0) 95 { 96 ret = PPS_Exchange (pps, params, length); 97 98 /* Get parameters from PPS handsake */ 99 if (ret == PPS_OK) 100 { 101 pps->parameters.t = params[1] & 0x0F; 102 103 if (PPS_HAS_PPS1 (params)) 104 { 105 pps->parameters.f = atr_f_table[(params[2] >> 4)]; 106 pps->parameters.d = atr_d_table[(params[2] & 0x0F)]; 107 } 108 109 ret = PPS_InitICC(pps); 110 111 if (ret != PPS_OK) 112 return ret; 113 } 114 else 115 { 116 return ret; 117 } 118 } 119 else /* Get parameters from ATR */ 120 { 121 PPS_SelectFirstProtocol (pps); 122 123 #ifndef PTS_USE_DEFAULT_TIMINGS 124 atr = ICC_Async_GetAtr (pps->icc); 125 126 ATR_GetParameter (atr, ATR_PARAMETER_N, &(pps->parameters.n)); 127 ATR_GetParameter (atr, ATR_PARAMETER_D, &(pps->parameters.d)); 128 ATR_GetParameter (atr, ATR_PARAMETER_F, &(pps->parameters.f)); 129 #endif 130 } 131 132 #ifdef DEBUG_PROTOCOL 133 printf("PPS: T=%X, F=%.0f, D=%.6f, N=%.0f\n", 134 pps->parameters.t, 135 pps->parameters.f, 136 pps->parameters.d, 137 pps->parameters.n); 138 #endif 139 140 /* Initialize selected protocol with selected parameters */ 141 ret = PPS_InitProtocol (pps); 142 143 return ret; 144 } 145 146 void * PPS_GetProtocol (PPS * pps) 147 { 148 return pps->protocol; 149 } 150 151 PPS_ProtocolParameters *PPS_GetProtocolParameters (PPS * pps) 152 { 153 /* User must Remember not to reference this struct after removing PPS */ 154 return &(pps->parameters); 155 } 156 157 void PPS_Delete (PPS * pps) 158 { 159 free (pps); 160 } 161 162 /* 163 * Not exported funtions definition 164 */ 165 166 static int PPS_Exchange (PPS * pps, BYTE * params, unsigned *length) 167 { 93 168 BYTE confirm[PPS_MAX_LENGTH]; 94 ATR *atr; 95 unsigned len_confirm; 96 unsigned len_request=4; 97 98 99 atr = ICC_Async_GetAtr(pps->icc); 100 ATR_GetProtocolType(atr,0,&(pps->parameters.t)); 101 102 req[0]=0xFF; 103 req[1]=0x10 | pps->parameters.t; 104 req[2]=atr->ib[0][ATR_INTERFACE_BYTE_TA].value; 105 req[3]=PPS_GetPCK(req,len_request-1); 106 169 unsigned len_request, len_confirm; 170 int ret; 107 171 #ifdef DEBUG_PROTOCOL 108 172 int i; 109 printf("PTS: Sending request: "); 173 #endif 174 175 len_request = PPS_GetLength (params); 176 params[len_request - 1] = PPS_GetPCK(params, len_request - 1); 177 178 #ifdef DEBUG_PROTOCOL 179 printf ("PPS: Sending request: "); 110 180 for (i = 0; i < len_request; i++) 111 printf ("%X ", req[i]);112 printf ("\n");113 #endif 114 115 /* Send P TS request */116 if (ICC_Async_Transmit (pps->icc, len_request, req) != ICC_ASYNC_OK)181 printf ("%X ", params[i]); 182 printf ("\n"); 183 #endif 184 185 /* Send PPS request */ 186 if (ICC_Async_Transmit (pps->icc, len_request, params) != ICC_ASYNC_OK) 117 187 return PPS_ICC_ERROR; 118 188 119 if (ICC_Async_Receive(pps->icc, len_request, confirm) != ICC_ASYNC_OK) 120 { 121 #ifdef DEBUG_PROTOCOL 122 printf("PTS: error receiving PTS answer !!\n"); 123 #endif 189 /* Get PPS confirm */ 190 if (ICC_Async_Receive (pps->icc, 2, confirm) != ICC_ASYNC_OK) 124 191 return PPS_ICC_ERROR; 125 } 126 127 len_confirm=len_request; 128 129 #ifdef DEBUG_PROTOCOL 130 printf("PTS: Receiving confirm: "); 192 193 len_confirm = PPS_GetLength (confirm); 194 195 if (ICC_Async_Receive (pps->icc, len_confirm - 2, confirm + 2) != ICC_ASYNC_OK) 196 return PPS_ICC_ERROR; 197 198 #ifdef DEBUG_PROTOCOL 199 printf ("PPS: Receivig confirm: "); 131 200 for (i = 0; i < len_confirm; i++) 132 printf ("%X ", confirm[i]);133 printf ("\n");134 #endif 135 136 if (!PPS_Match (req, len_request, confirm, len_confirm))201 printf ("%X ", confirm[i]); 202 printf ("\n"); 203 #endif 204 205 if (!PPS_Match (params, len_request, confirm, len_confirm)) 137 206 ret = PPS_HANDSAKE_ERROR; 138 207 else … … 140 209 141 210 /* Copy PPS handsake */ 142 memcpy(req, confirm, len_confirm); 143 144 // compute baudrate to be use if PTS handshake was successfull. 145 baudrate=(long unsigned int)((float)ICC_Async_GetClockRate(pps->icc)*pps->parameters.d /pps->parameters.f); 146 147 if (ICC_Async_SetBaudrate(pps->icc, baudrate) != ICC_ASYNC_OK) 148 return PPS_ICC_ERROR; 149 211 memcpy (params, confirm, len_confirm); 212 (*length) = len_confirm; 213 150 214 return ret; 151 215 } 152 216 153 int PPS_Perform(PPS * pps, BYTE * params, unsigned *length) 154 { 155 ATR *atr; 156 int ret; 157 158 /* Perform PPS Exchange if requested */ 159 if ((*length) > 0) { 160 ret = PPS_Exchange(pps, params, length); 161 162 /* Get parameters from PPS handsake */ 163 if (ret == PPS_OK) { 164 pps->parameters.t = params[1] & 0x0F; 165 166 if (PPS_HAS_PPS1(params)) { 167 pps->parameters.f = atr_f_table[(params[2] >> 4)]; 168 pps->parameters.d = atr_d_table[(params[2] & 0x0F)]; 169 if(pps->parameters.d==0) 170 { 171 // set pps->parameters.d to 1 as 0 is not a valid value 172 pps->parameters.d=ATR_DEFAULT_D; 173 } 174 #ifdef DEBUG_PROTOCOL 175 printf("PPS: pps->parameters.n %f\n",pps->parameters.n); 176 printf("PPS: pps->parameters.d %f\n",pps->parameters.d); 177 printf("PPS: pps->parameters.f %f\n",pps->parameters.f); 178 printf("PPS: Calling PPP_InitICC to set PPS params\n"); 179 #endif 180 } 181 182 ret = PPS_InitICC(pps); 183 184 if (ret != PPS_OK) 185 return ret; 186 } else { 187 return ret; 188 } 189 } else { /* Get parameters from ATR */ 190 191 PPS_SelectFirstProtocol(pps); 192 193 #ifndef PPS_USE_DEFAULT_TIMINGS 194 atr = ICC_Async_GetAtr(pps->icc); 195 #ifdef DEBUG_PROTOCOL 196 printf("PPS: atr.lenght = %u\n",atr->length); 197 printf("PPS: atr.TS = %u\n",atr->TS); 198 printf("PPS: atr.T0 = %u\n",atr->T0); 199 printf("PPS: atr.TA = %u\n",atr->ib[0][ATR_INTERFACE_BYTE_TA].value); 200 printf("PPS: atr.FI = %u\n",(atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0xF0) >> 4); 201 printf("PPS: atr.DI = %u\n",(atr->ib[0][ATR_INTERFACE_BYTE_TA].value & 0x0F)); 202 #endif 203 ATR_GetParameter(atr, ATR_PARAMETER_N, &(pps->parameters.n)); 204 ATR_GetParameter(atr, ATR_PARAMETER_D, &(pps->parameters.d)); 205 ATR_GetParameter(atr, ATR_PARAMETER_F, &(pps->parameters.f)); 206 if(pps->parameters.d==0) 207 { 208 // set pps->parameters.d to 1 as 0 is not a valid value 209 pps->parameters.d=ATR_DEFAULT_D; 210 } 211 212 #ifdef DEBUG_PROTOCOL 213 printf("PPS: pps->parameters.n %f\n",pps->parameters.n); 214 printf("PPS: pps->parameters.d %f\n",pps->parameters.d); 215 printf("PPS: pps->parameters.f %f\n",pps->parameters.f); 216 printf("PPS: Calling PPP_InitICC to set PPS params\n"); 217 #endif 218 ret = PPS_InitICC(pps); 219 220 if (ret != PPS_OK) 221 return ret; 222 #endif 223 } 224 225 #ifdef DEBUG_PROTOCOL 226 printf("PPS: T=%X, F=%.0f, D=%.6f, N=%.0f\n", pps->parameters.t, pps->parameters.f, pps->parameters.d, pps->parameters.n); 227 #endif 228 229 /* Initialize selected protocol with selected parameters */ 230 ret = PPS_InitProtocol(pps); 231 /* 232 #ifdef DEBUG_PROTOCOL 233 printf("PPS: Attempting PTS\n"); 234 #endif 235 if(PPS_PreformPTS(pps)==PPS_OK) 236 { 237 printf("PTS handcheck succeded.\n"); 238 } 239 else 240 { 241 printf("PTS handcheck failed.\n"); 242 } 243 244 #ifdef DEBUG_PROTOCOL 245 printf("PPS: pps->parameters.n %f\n",pps->parameters.n); 246 printf("PPS: pps->parameters.d %f\n",pps->parameters.d); 247 printf("PPS: pps->parameters.f %f\n",pps->parameters.f); 248 #endif 249 */ 250 return ret; 251 } 252 253 void *PPS_GetProtocol(PPS * pps) 254 { 255 return pps->protocol; 256 } 257 258 PPS_ProtocolParameters *PPS_GetProtocolParameters(PPS * pps) 259 { 260 /* User must Remember not to reference this struct after removing PPS */ 261 return &(pps->parameters); 262 } 263 264 void PPS_Delete(PPS * pps) 265 { 266 free(pps); 267 } 268 269 /* 270 * Not exported funtions definition 271 */ 272 273 static int PPS_Exchange(PPS * pps, BYTE * params, unsigned *length) 274 { 275 BYTE confirm[PPS_MAX_LENGTH]; 276 unsigned len_request, len_confirm; 277 int ret; 278 279 #ifdef DEBUG_PROTOCOL 280 int i; 281 #endif 282 283 len_request = PPS_GetLength(params); 284 params[len_request - 1] = PPS_GetPCK(params, len_request - 1); 285 286 #ifdef DEBUG_PROTOCOL 287 printf("PPS: Sending request: "); 288 for (i = 0; i < len_request; i++) 289 printf("%X ", params[i]); 290 printf("\n"); 291 #endif 292 293 /* Send PPS request */ 294 if (ICC_Async_Transmit(pps->icc, len_request, params) != ICC_ASYNC_OK) 295 return PPS_ICC_ERROR; 296 297 /* Get PPS confirm */ 298 if (ICC_Async_Receive(pps->icc, 2, confirm) != ICC_ASYNC_OK) 299 { 300 #ifdef DEBUG_PROTOCOL 301 printf("PPS: error receiving confirm !!\n"); 302 #endif 303 return PPS_ICC_ERROR; 304 } 305 len_confirm = PPS_GetLength(confirm); 306 307 if (ICC_Async_Receive(pps->icc, len_confirm - 2, confirm + 2) != ICC_ASYNC_OK) 308 { 309 #ifdef DEBUG_PROTOCOL 310 printf("PPS: error receiving answer !!\n"); 311 #endif 312 return PPS_ICC_ERROR; 313 } 314 315 #ifdef DEBUG_PROTOCOL 316 printf("PPS: Receiving confirm: "); 317 for (i = 0; i < len_confirm; i++) 318 printf("%X ", confirm[i]); 319 printf("\n"); 320 #endif 321 322 if (!PPS_Match(params, len_request, confirm, len_confirm)) 323 ret = PPS_HANDSAKE_ERROR; 324 else 325 ret = PPS_OK; 326 327 /* Copy PPS handsake */ 328 memcpy(params, confirm, len_confirm); 329 (*length) = len_confirm; 330 331 return ret; 332 } 333 334 static bool PPS_Match(BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_confirm) 217 static bool PPS_Match (BYTE * request, unsigned len_request, BYTE * confirm, unsigned len_confirm) 335 218 { 336 219 /* See if the reply differs from request */ 337 if ((len_request != len_confirm) || (!memcmp(request, confirm, len_request))) { 220 if ((len_request != len_confirm) || (!memcmp (request, confirm, len_request))) 221 { 338 222 /* See if the card specifies other than default FI and D */ 339 if ((PPS_HAS_PPS1 (confirm)) && (confirm[2] != request[2]))223 if ((PPS_HAS_PPS1 (confirm)) && (confirm[2] != request[2])) 340 224 return FALSE; 341 225 } 342 226 343 227 return TRUE; 344 228 } 345 229 346 static unsigned PPS_GetLength (BYTE * block)230 static unsigned PPS_GetLength (BYTE * block) 347 231 { 348 232 unsigned length = 3; 349 350 if (PPS_HAS_PPS1 (block))351 352 353 if (PPS_HAS_PPS2 (block))354 355 356 if (PPS_HAS_PPS3 (block))357 358 233 234 if (PPS_HAS_PPS1 (block)) 235 length++; 236 237 if (PPS_HAS_PPS2 (block)) 238 length++; 239 240 if (PPS_HAS_PPS3 (block)) 241 length++; 242 359 243 return length; 360 244 } 361 245 362 static int PPS_InitICC (PPS * pps)246 static int PPS_InitICC (PPS * pps) 363 247 { 364 248 unsigned long baudrate; 365 249 long double work_etu; 366 250 367 251 /* Work etu = (1/D) * (F/fs) * 1000 milliseconds */ 368 work_etu = (1000 * pps->parameters.f) / (pps->parameters.d * ICC_Async_GetClockRate(pps->icc)); 369 370 // FIXME : 371 // initializing the baudrate here is plain wrong. 372 // the card inits MUST be done at 9600 bps 373 // and if , and only if, there is a PPS (or PTS) handshake 374 // then we use this value for the new baudrate 375 // The F and D parameter returned by the ATR are to be used ONLY 376 // if a PPS or PTS exchange is done, otherwize everything MUST 377 // be done at 9600 bps 378 // this mean we need to fix the logic around the card reset 379 // as for now it uses this new rate in the midle of the reset just after getting the ATR 380 // which doesn't work for example with a card returning 3F 77 18 .. as in this case 381 // F=372 and D=12 (see the commented atr_d_table in atr.c which has the right value 382 // as the curent one as 0 for the index 8 which is of course wrong as D=0 is an invalid value). 383 // I'm leaving it like this until we discuss what to do as a rework of the code is needed. 384 // 252 work_etu = (1000 * pps->parameters.f) / (pps->parameters.d * ICC_Async_GetClockRate (pps->icc)); 385 253 386 254 /* Baudrate = 1000 / etu bps */ 387 255 baudrate = (long unsigned int) (1000 / work_etu); 388 389 #ifdef DEBUG_PROTOCOL 390 printf ("PPS: Baudrate = %d\n", (int)baudrate);391 #endif 392 393 if (ICC_Async_SetBaudrate (pps->icc, baudrate) != ICC_ASYNC_OK)256 257 #ifdef DEBUG_PROTOCOL 258 printf ("PPS: Baudrate = %d\n", (int)baudrate); 259 #endif 260 261 if (ICC_Async_SetBaudrate (pps->icc, baudrate) != ICC_ASYNC_OK) 394 262 return PPS_ICC_ERROR; 395 263 396 264 return PPS_OK; 397 265 } 398 266 399 static int PPS_InitProtocol (PPS * pps)267 static int PPS_InitProtocol (PPS * pps) 400 268 { 401 269 int ret; 402 403 if (pps->parameters.t == ATR_PROTOCOL_TYPE_T0) { 404 pps->protocol = Protocol_T0_New(); 405 406 if ((pps->protocol) != NULL) { 407 ret = Protocol_T0_Init((Protocol_T0 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters)); 408 409 if (ret != PROTOCOL_T0_OK) { 410 Protocol_T0_Delete((Protocol_T0 *) pps->protocol); 270 271 if (pps->parameters.t == ATR_PROTOCOL_TYPE_T0) 272 { 273 pps->protocol = Protocol_T0_New (); 274 275 if ((pps->protocol) != NULL) 276 { 277 ret = Protocol_T0_Init ((Protocol_T0 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters)); 278 279 if (ret != PROTOCOL_T0_OK) 280 { 281 Protocol_T0_Delete ((Protocol_T0 *) pps->protocol); 411 282 pps->protocol = NULL; 412 283 return PPS_PROTOCOL_ERROR; 413 284 } 414 285 415 286 return PPS_OK; 416 } 417 } else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T1) { 418 pps->protocol = Protocol_T1_New(); 419 420 if (pps->protocol != NULL) { 421 ret = Protocol_T1_Init((Protocol_T1 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters)); 422 423 if (ret != PROTOCOL_T1_OK) { 424 Protocol_T1_Delete((Protocol_T1 *) pps->protocol); 287 } 288 } 289 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T1) 290 { 291 pps->protocol = Protocol_T1_New (); 292 293 if (pps->protocol != NULL) 294 { 295 ret = Protocol_T1_Init ((Protocol_T1 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters)); 296 297 if (ret != PROTOCOL_T1_OK) 298 { 299 Protocol_T1_Delete ((Protocol_T1 *) pps->protocol); 425 300 pps->protocol = NULL; 426 301 return PPS_PROTOCOL_ERROR; 427 302 } 428 303 429 304 return PPS_OK; 430 305 } 431 } else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T14) { 432 pps->protocol = Protocol_T14_New(); 433 434 if ((pps->protocol) != NULL) { 435 ret = Protocol_T14_Init((Protocol_T14 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters)); 436 437 if (ret != PROTOCOL_T14_OK) { 438 Protocol_T14_Delete((Protocol_T14 *) pps->protocol); 306 } 307 else if (pps->parameters.t == ATR_PROTOCOL_TYPE_T14) 308 { 309 pps->protocol = Protocol_T14_New (); 310 311 if ((pps->protocol) != NULL) 312 { 313 ret = Protocol_T14_Init ((Protocol_T14 *) pps->protocol, (ICC_Async *) pps->icc, &(pps->parameters)); 314 315 if (ret != PROTOCOL_T14_OK) 316 { 317 Protocol_T14_Delete ((Protocol_T14 *) pps->protocol); 439 318 pps->protocol = NULL; 440 319 return PPS_PROTOCOL_ERROR; 441 320 } 442 321 443 322 return PPS_OK; 444 } 445 } else { 323 } 324 } 325 else 326 { 446 327 pps->protocol = NULL; 447 328 } 448 329 449 330 return PPS_PROTOCOL_ERROR; 450 331 } 451 332 452 static void PPS_SelectFirstProtocol (PPS * pps)453 { 454 ATR *atr = ICC_Async_GetAtr (pps->icc);333 static void PPS_SelectFirstProtocol (PPS * pps) 334 { 335 ATR *atr = ICC_Async_GetAtr (pps->icc); 455 336 unsigned np; 456 337 457 338 pps->parameters.t = ATR_PROTOCOL_TYPE_T0; 458 459 ATR_GetNumberOfProtocols (atr, &np);460 339 340 ATR_GetNumberOfProtocols (atr, &np); 341 461 342 /* 462 463 464 465 if (np >1)466 ATR_GetProtocolType (atr, 2, &(pps->parameters.t));467 468 #ifdef DEBUG_PROTOCOL 469 printf ("PPS: Protocol T=%d selected\n", pps->parameters.t);470 #endif 471 } 472 473 static BYTE PPS_GetPCK (BYTE * block, unsigned length)343 * Get protocol offered by interface bytes T*2 if available, 344 * (that is, if TD1 is available), * otherwise use default T=0 345 */ 346 if (np>1) 347 ATR_GetProtocolType (atr, 2, &(pps->parameters.t)); 348 349 #ifdef DEBUG_PROTOCOL 350 printf ("PPS: Protocol T=%d selected\n", pps->parameters.t); 351 #endif 352 } 353 354 static BYTE PPS_GetPCK (BYTE * block, unsigned length) 474 355 { 475 356 BYTE pck; 476 357 unsigned i; 477 358 478 359 pck = block[0]; 479 360 for (i = 1; i < length; i++) 480 361 pck ^= block[i]; 481 362 482 363 return pck; 483 364 } -
trunk/reader-common.c
r562 r565 5 5 int oscam_card_detect; 6 6 int mhz; 7 int reader_ has_irdeto_card;7 int reader_irdeto_mode; 8 8 9 9 uchar cta_cmd[272], cta_res[260], atr[64]; … … 154 154 for (i=0; i<5; i++) 155 155 { 156 //reader_irdeto_mode = i%2 == 1; //only works when not overclocking156 reader_irdeto_mode = i%2 == 1; 157 157 cta_cmd[0] = CTBCS_CLA; 158 158 cta_cmd[1] = CTBCS_INS_REQUEST; … … 180 180 #endif 181 181 cs_ri_log("ATR: %s", cs_hexdump(1, atr, atr_size)); 182 if (!memcmp(atr+4, "IRDETO", 6))183 reader_has_irdeto_card = 1;184 else185 reader_has_irdeto_card = 0;186 182 sleep(1); 187 183 return(1); … … 297 293 cs_ptyp=D_DEVICE; 298 294 snprintf(oscam_device, sizeof(oscam_device), "%s", device); 299 300 if ((rc=CT_init(1, reader_device_type(device, typ),mhz*10000,reader[ridx].typ))!=OK) 295 if ((rc=CT_init(1, reader_device_type(device, typ),reader[ridx].typ))!=OK) 301 296 cs_log("Cannot open device: %s", device); 302 297 cs_debug("ct_init on %s: %d", device, rc);
Note:
See TracChangeset
for help on using the changeset viewer.