Changeset 601
- Timestamp:
- 12/01/09 23:28:46 (14 years ago)
- Location:
- branches/smartreader/csctapi
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/smartreader/csctapi/io_serial.c
r577 r601 51 51 #endif 52 52 53 #define IO_SERIAL_FILENAME_LENGTH 3254 53 55 54 /* … … 57 56 */ 58 57 59 static int IO_Serial_Bitrate(int bitrate); 58 static int IO_Serial_Bitrate_to_Speed(int bitrate); 59 60 static int IO_Serial_Bitrate_from_Speed(int speed); 60 61 61 62 static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms); … … 68 69 69 70 static void IO_Serial_Clear (IO_Serial * io); 71 72 static void IO_SR_Clear (SR_Config * srConfig); 73 74 static bool IO_Serial_Set_Smartreader_Config (IO_Serial * io); 70 75 71 76 static int _in_echo_read = 0; … … 169 174 io->reader_type=reader_type; 170 175 io->mhz=mhz; 176 177 io->SmartReaderConf = (SR_Config *) malloc(sizeof (SR_Config)); 178 if (io != NULL) 179 IO_SR_Clear (io->SmartReaderConf); 171 180 172 181 return io; … … 178 187 179 188 IO_Serial_DeviceName (com, usbserial, filename, IO_SERIAL_FILENAME_LENGTH); 189 190 strncpy(io->filename,filename,IO_SERIAL_FILENAME_LENGTH); 180 191 181 192 #ifdef DEBUG_IO … … 245 256 246 257 o_speed = cfgetospeed (&currtio); 247 248 switch (o_speed) 249 { 250 #ifdef B0 251 case B0: 252 io->output_bitrate = 0; 253 break; 254 #endif 255 #ifdef B50 256 case B50: 257 io->output_bitrate = 50; 258 break; 259 #endif 260 #ifdef B75 261 case B75: 262 io->output_bitrate = 75; 263 break; 264 #endif 265 #ifdef B110 266 case B110: 267 io->output_bitrate = 110; 268 break; 269 #endif 270 #ifdef B134 271 case B134: 272 io->output_bitrate = 134; 273 break; 274 #endif 275 #ifdef B150 276 case B150: 277 io->output_bitrate = 150; 278 break; 279 #endif 280 #ifdef B200 281 case B200: 282 io->output_bitrate = 200; 283 break; 284 #endif 285 #ifdef B300 286 case B300: 287 io->output_bitrate = 300; 288 break; 289 #endif 290 #ifdef B600 291 case B600: 292 io->output_bitrate = 600; 293 break; 294 #endif 295 #ifdef B1200 296 case B1200: 297 io->output_bitrate = 1200; 298 break; 299 #endif 300 #ifdef B1800 301 case B1800: 302 io->output_bitrate = 1800; 303 break; 304 #endif 305 #ifdef B2400 306 case B2400: 307 io->output_bitrate = 2400; 308 break; 309 #endif 310 #ifdef B4800 311 case B4800: 312 io->output_bitrate = 4800; 313 break; 314 #endif 315 #ifdef B9600 316 case B9600: 317 io->output_bitrate = 9600; 318 break; 319 #endif 320 #ifdef B19200 321 case B19200: 322 io->output_bitrate = 19200; 323 break; 324 #endif 325 #ifdef B38400 326 case B38400: 327 io->output_bitrate = 38400; 328 break; 329 #endif 330 #ifdef B57600 331 case B57600: 332 io->output_bitrate = 57600; 333 break; 334 #endif 335 #ifdef B115200 336 case B115200: 337 io->output_bitrate = 115200; 338 break; 339 #endif 340 #ifdef B230400 341 case B230400: 342 io->output_bitrate = 230400; 343 break; 344 #endif 345 default: 346 io->output_bitrate = 1200; 347 break; 348 } 258 io->output_bitrate = IO_Serial_Bitrate_from_Speed(o_speed); 349 259 350 260 i_speed = cfgetispeed (&currtio); 351 352 switch (i_speed) 353 { 354 #ifdef B0 355 case B0: 356 io->input_bitrate = 0; 357 break; 358 #endif 359 #ifdef B50 360 case B50: 361 io->input_bitrate = 50; 362 break; 363 #endif 364 #ifdef B75 365 case B75: 366 io->input_bitrate = 75; 367 break; 368 #endif 369 #ifdef B110 370 case B110: 371 io->input_bitrate = 110; 372 break; 373 #endif 374 #ifdef B134 375 case B134: 376 io->input_bitrate = 134; 377 break; 378 #endif 379 #ifdef B150 380 case B150: 381 io->input_bitrate = 150; 382 break; 383 #endif 384 #ifdef B200 385 case B200: 386 io->input_bitrate = 200; 387 break; 388 #endif 389 #ifdef B300 390 case B300: 391 io->input_bitrate = 300; 392 break; 393 #endif 394 #ifdef B600 395 case B600: 396 io->input_bitrate = 600; 397 break; 398 #endif 399 #ifdef B1200 400 case B1200: 401 io->input_bitrate = 1200; 402 break; 403 #endif 404 #ifdef B1800 405 case B1800: 406 io->input_bitrate = 1800; 407 break; 408 #endif 409 #ifdef B2400 410 case B2400: 411 io->input_bitrate = 2400; 412 break; 413 #endif 414 #ifdef B4800 415 case B4800: 416 io->input_bitrate = 4800; 417 break; 418 #endif 419 #ifdef B9600 420 case B9600: 421 io->input_bitrate = 9600; 422 break; 423 #endif 424 #ifdef B19200 425 case B19200: 426 io->input_bitrate = 19200; 427 break; 428 #endif 429 #ifdef B38400 430 case B38400: 431 io->input_bitrate = 38400; 432 break; 433 #endif 434 #ifdef B57600 435 case B57600: 436 io->input_bitrate = 57600; 437 break; 438 #endif 439 #ifdef B115200 440 case B115200: 441 io->input_bitrate = 115200; 442 break; 443 #endif 444 #ifdef B230400 445 case B230400: 446 io->input_bitrate = 230400; 447 break; 448 #endif 449 default: 450 io->input_bitrate = 1200; 451 break; 452 } 261 io->input_bitrate = IO_Serial_Bitrate_from_Speed(i_speed); 262 453 263 454 264 switch (currtio.c_cflag & CSIZE) … … 513 323 514 324 int mhz = io->mhz; 515 extern int reader_irdeto_mode;516 517 325 if(io->reader_type==RTYP_SMART) 518 326 { … … 520 328 printf("IO: SMARTREADER .. switching to frequency to %2.2fMHz\n", (float)mhz/100.0); 521 329 #endif 522 if(!IO_Serial_Set_Smartreader_ Freq(io,mhz,reader_irdeto_mode))330 if(!IO_Serial_Set_Smartreader_Config(io)) 523 331 { 524 332 #ifdef DEBUG_IO … … 539 347 #endif 540 348 { //no overclocking 541 cfsetospeed(&newtio, IO_Serial_Bitrate (io->output_bitrate));542 cfsetispeed(&newtio, IO_Serial_Bitrate (io->input_bitrate));349 cfsetospeed(&newtio, IO_Serial_Bitrate_to_Speed(io->output_bitrate)); 350 cfsetispeed(&newtio, IO_Serial_Bitrate_to_Speed(io->input_bitrate)); 543 351 } 544 352 #ifdef OS_LINUX … … 554 362 nuts.flags |= ASYNC_SPD_CUST; 555 363 ioctl(io->fd, TIOCSSERIAL, &nuts); 556 cfsetospeed(&newtio, IO_Serial_Bitrate (38400));557 cfsetispeed(&newtio, IO_Serial_Bitrate (38400));364 cfsetospeed(&newtio, IO_Serial_Bitrate_to_Speed(38400)); 365 cfsetispeed(&newtio, IO_Serial_Bitrate_to_Speed(38400)); 558 366 } 559 367 #endif … … 844 652 */ 845 653 846 static int IO_Serial_Bitrate(int bitrate) 847 { 654 static int IO_Serial_Bitrate_to_Speed(int bitrate) 655 { 656 static const struct BaudRates { int real; speed_t apival; } BaudRateTab[] = { 657 { 200, B200 }, { 300, B300 }, { 600, B600 }, 658 { 1200, B1200 }, { 2400, B2400 }, { 4800, B4800 }, 659 { 9600, B9600 }, { 19200, B19200 }, { 38400, B38400 }, 660 { 57600, B57600 }, { 115200, B115200 }, { 230400, B230400 } 661 }; 662 663 int i; 664 665 for(i=0; i<(int)(sizeof(BaudRateTab)/sizeof(struct BaudRates)); i++) 666 { 667 int b=BaudRateTab[i].real; 668 int d=((b-bitrate)*10000)/b; 669 if(abs(d)<=300) 670 { 671 return BaudRateTab[i].apival; 672 } 673 } 674 return B0; 675 } 676 677 static int IO_Serial_Bitrate_from_Speed(int speed) 678 { 679 680 switch (speed) 681 { 682 #ifdef B0 683 case B0: 684 return 0; 685 #endif 686 #ifdef B50 687 case B50: 688 return 50; 689 #endif 690 #ifdef B75 691 case B75: 692 return 75; 693 #endif 694 #ifdef B110 695 case B110: 696 return 110; 697 #endif 698 #ifdef B134 699 case B134: 700 return 134; 701 #endif 702 #ifdef B150 703 case B150: 704 return 150; 705 #endif 706 #ifdef B200 707 case B200: 708 return 200; 709 #endif 710 #ifdef B300 711 case B300: 712 return 300; 713 #endif 714 #ifdef B600 715 case B600: 716 return 600; 717 #endif 718 #ifdef B1200 719 case B1200: 720 return 1200; 721 #endif 722 #ifdef B1800 723 case B1800: 724 return 1800; 725 #endif 726 #ifdef B2400 727 case B2400: 728 return 2400; 729 #endif 730 #ifdef B4800 731 case B4800: 732 return 4800; 733 #endif 734 #ifdef B9600 735 case B9600: 736 return 9600; 737 #endif 738 #ifdef B19200 739 case B19200: 740 return 19200; 741 #endif 742 #ifdef B38400 743 case B38400: 744 return 38400; 745 #endif 746 #ifdef B57600 747 case B57600: 748 return 57600; 749 #endif 750 #ifdef B115200 751 case B115200: 752 return 115200; 753 #endif 848 754 #ifdef B230400 849 if ((bitrate)>=230400) return B230400; 850 #endif 851 #ifdef B115200 852 if ((bitrate)>=115200) return B115200; 853 #endif 854 #ifdef B57600 855 if ((bitrate)>=57600) return B57600; 856 #endif 857 #ifdef B38400 858 if ((bitrate)>=38400) return B38400; 859 #endif 860 #ifdef B19200 861 if ((bitrate)>=19200) return B19200; 862 #endif 863 #ifdef B9600 864 if ((bitrate)>=9600) return B9600; 865 #endif 866 #ifdef B4800 867 if ((bitrate)>=4800) return B4800; 868 #endif 869 #ifdef B2400 870 if ((bitrate)>=2400) return B2400; 871 #endif 872 #ifdef B1800 873 if ((bitrate)>=1800) return B1800; 874 #endif 875 #ifdef B1200 876 if ((bitrate)>=1200) return B1200; 877 #endif 878 #ifdef B600 879 if ((bitrate)>=600) return B600; 880 #endif 881 #ifdef B300 882 if ((bitrate)>=300) return B300; 883 #endif 884 #ifdef B200 885 if ((bitrate)>=200) return B200; 886 #endif 887 #ifdef B150 888 if ((bitrate)>=150) return B150; 889 #endif 890 #ifdef B134 891 if ((bitrate)>=134) return B134; 892 #endif 893 #ifdef B110 894 if ((bitrate)>=110) return B110; 895 #endif 896 #ifdef B75 897 if ((bitrate)>=75) return B75; 898 #endif 899 #ifdef B50 900 if ((bitrate)>=50) return B50; 901 #endif 902 #ifdef B0 903 if ((bitrate)>=0) return B0; 904 #endif 755 case B230400: 756 return 230400; 757 #endif 758 default: 759 return 1200; 760 } 761 905 762 return 0; /* Should never get here */ 906 763 } 764 765 907 766 908 767 static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms) … … 1034 893 } 1035 894 895 static void IO_SR_Clear (SR_Config * srConfig) 896 { 897 // set smartreader+ default values 898 srConfig->F=372; 899 srConfig->D=1.0; 900 srConfig->fs=3571200; 901 srConfig->N=0; 902 srConfig->T=0; 903 srConfig->inv=0; 904 } 905 1036 906 static void IO_Serial_DeviceName (unsigned com, bool usbserial, char * filename, unsigned length) 1037 907 { … … 1055 925 // io->rts = IO_SERIAL_HIGH; 1056 926 io->rts = IO_SERIAL_LOW; 927 928 // set smartreader+ default values 929 // use the frequency to get F for the smartreader 930 // to make sure the ATR is sent at 9600. 931 if(io->SmartReaderConf) 932 { 933 io->SmartReaderConf->F=(io->mhz)*1000/9600; 934 io->SmartReaderConf->D=1.0; 935 io->SmartReaderConf->fs=(io->mhz)*1000; // freq in Hz 936 io->SmartReaderConf->N=0; 937 io->SmartReaderConf->T=0; 938 io->SmartReaderConf->inv=0; 939 } 1057 940 1058 941 if (!IO_Serial_SetProperties (io)) … … 1067 950 1068 951 1069 bool IO_Serial_Set_Smartreader_Freq(IO_Serial * io, int freq, int irdeto_mode) 1070 { 1071 struct termios term; 1072 struct termios orig; 1073 unsigned char fi_di[4]={0x01, 0x01, 0x74, 0x01}; 1074 unsigned char fr[3]={0x02, 0x00, 0x00}; 1075 unsigned char nn[2]={0x03, 0x00}; 1076 unsigned char pr[2]={0x04, 0x00}; 1077 unsigned char in[2]={0x05, 0x00}; 1078 1079 orig=term; 1080 1081 /* set smartreader in CMD mode */ 1082 tcgetattr(io->fd, &term); 1083 1084 term.c_cflag &= ~CSIZE; 1085 term.c_cflag |= CS5; 1086 cfsetospeed(&term, 9600); 1087 cfsetispeed(&term, 9600); 1088 tcsetattr(io->fd, TCSANOW, &term); 1089 1090 // our freq comes in as 358, 357 or 600 so it needs this to be in KHz for the FR command 1091 freq*=10; 1092 fr[1]=(unsigned char)((freq & 0xff00)>>8); 1093 fr[2]=(unsigned char)(freq & 0x00ff); 1094 1095 // Irdeto card supposedly need NN set to 1 .. to be confirmed 1096 if(irdeto_mode) 1097 nn[1]=0x01; 1098 1099 // send the commands 1100 IO_Serial_Write (io, 0, 4, fi_di); 1101 IO_Serial_Flush(io); 1102 1103 IO_Serial_Write (io, 0, 3, fr); 1104 IO_Serial_Flush(io); 1105 1106 IO_Serial_Write (io, 0, 2, nn); 1107 IO_Serial_Flush(io); 1108 1109 IO_Serial_Write (io, 0, 2, pr); 1110 IO_Serial_Flush(io); 1111 1112 IO_Serial_Write (io, 0, 2, in); 1113 IO_Serial_Flush(io); 1114 1115 /* set smartreader in DATA mode */ 1116 tcgetattr(io->fd, &orig); 1117 orig.c_cflag &= ~CSIZE; 1118 orig.c_cflag |= CS8; 1119 tcsetattr(io->fd, TCSANOW, &orig); 1120 1121 return TRUE; 1122 } 1123 952 static bool IO_Serial_Set_Smartreader_Config(IO_Serial * io) 953 { 954 float D; 955 int F,fs,N,T,inv; 956 SR_Config *sr_config; 957 958 sr_config=io->SmartReaderConf; 959 if(sr_config==NULL) 960 return FALSE; 961 962 # ifdef DEBUG_IO 963 printf("IO: Setting SmartReader+ config : F=%d; D=%f; fs=%d; N=%d; T=%d; inv=%d \n", 964 sr_config->F, 965 sr_config->D, 966 sr_config->fs, 967 sr_config->N, 968 sr_config->T, 969 sr_config->inv); 970 # endif 971 972 F=sr_config->F; 973 D=sr_config->D; 974 fs=sr_config->fs; 975 N=sr_config->N; 976 T=sr_config->T; 977 inv=sr_config->inv; 978 979 fs/=1000; // convert to kHz. 980 # ifdef DEBUG_IO 981 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); 982 # endif 983 984 // Set SmartReader+ in CMD mode. 985 struct termios term; 986 if(tcgetattr(io->fd,&term)==-1) 987 { 988 # ifdef DEBUG_IO 989 printf("%s: tcgetattr failed: %s",io->filename,strerror(errno)); 990 #endif 991 return FALSE; 992 } 993 994 term.c_cflag&=~CSIZE; 995 term.c_cflag|=CS5; 996 if(tcsetattr(io->fd,TCSADRAIN,&term)==-1) 997 { 998 # ifdef DEBUG_IO 999 printf("%s: tcsetattr failed: %s",io->filename,strerror(errno)); 1000 #endif 1001 return FALSE; 1002 } 1003 // Write SmartReader+ configuration commands. 1004 unsigned char cmd[16]; 1005 1006 //XXX how is (int)D supposed to work for fractional values e.g. 0.125 ?? 1007 cmd[0]=1; cmd[1]=F>>8; cmd[2]=F; cmd[3]=(int)D; 1008 if(!IO_Serial_Write(io, 0, 4, cmd)) 1009 return FALSE; 1010 1011 cmd[0]=2; cmd[1]=fs>>8; cmd[2]=fs; 1012 if(!IO_Serial_Write(io, 0, 3, cmd)) 1013 return FALSE; 1014 1015 cmd[0]=3; cmd[1]=N; 1016 if(!IO_Serial_Write(io, 0, 2, cmd)) 1017 return FALSE; 1018 1019 cmd[0]=4; cmd[1]=T; 1020 if(!IO_Serial_Write(io, 0, 2, cmd)) 1021 return FALSE; 1022 1023 cmd[0]=5; cmd[1]=inv; 1024 if(!IO_Serial_Write(io, 0, 2, cmd)) 1025 return FALSE; 1026 1027 // Send zero bits for 0.25 - 0.5 seconds. 1028 if(tcsendbreak(io->fd,0)==-1) 1029 { 1030 # ifdef DEBUG_IO 1031 printf("%s: tcsendbreak failed: %s",io->filename,strerror(errno)); 1032 #endif 1033 return FALSE; 1034 } 1035 // We're entering SmartReader+ mode; speed up serial communication. 1036 // B230400 is the highest setting that sticks. 1037 cfsetispeed(&term,B230400); 1038 cfsetospeed(&term,B230400); 1039 // Set SmartReader+ in DATA mode. 1040 term.c_cflag&=~CSIZE; 1041 term.c_cflag|=CS8; 1042 if(tcsetattr(io->fd,TCSADRAIN,&term)==-1) 1043 { 1044 # ifdef DEBUG_IO 1045 printf("%s: tcsetattr failed: %s",io->filename,strerror(errno)); 1046 #endif 1047 return FALSE; 1048 } 1049 return TRUE; 1050 } 1051 -
branches/smartreader/csctapi/io_serial.h
r577 r601 48 48 #define IO_SERIAL_PNPID_SIZE 256 49 49 50 #define IO_SERIAL_FILENAME_LENGTH 32 50 51 /* 51 52 * Exported datatypes definition 52 53 */ 54 /* structure holding ATR value to be used for the smargo smartreader+ */ 55 typedef struct { 56 int F; 57 float D; 58 int fs; 59 int N; 60 int T; 61 int inv; 62 } SR_Config; 53 63 54 64 /* IO_Serial exported datatype */ 55 65 typedef struct 56 66 { 67 char filename[IO_SERIAL_FILENAME_LENGTH]; 57 68 int fd; /* Handle of the serial device */ 58 69 /* Settings that can be modified */ … … 72 83 int reader_type; 73 84 int mhz; /* mhz specified in config = actual reader clock speed */ 85 SR_Config *SmartReaderConf; 86 74 87 } 75 88 IO_Serial; … … 107 120 extern void IO_Serial_GetPnPId (IO_Serial * io, BYTE * pnp_id, unsigned *length); 108 121 109 /* smartreader Frequency set */110 extern bool IO_Serial_Set_Smartreader_Freq(IO_Serial * io, int freq, int irdeto_mode);111 112 122 #endif /* IO_SERIAL */
Note:
See TracChangeset
for help on using the changeset viewer.