Changeset 4015 for trunk/csctapi
- Timestamp:
- 11/29/10 10:36:44 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/csctapi/ifd_smartreader.c
r4009 r4015 25 25 #define NUM_TXFERS 2 26 26 27 typedef struct s_reader S_READER; 27 static int number_of_smartreaders_running=0; //counts how many smartreaders are running, if 0 no libusbeventhandler needs to be called 28 static pthread_t libusb_thread; 28 29 29 30 static bool smartreader_check_endpoint(libusb_device *usb_dev, uint8_t out_endpoint); 30 31 static struct libusb_device *find_smartreader(const char*busname,const char *devname, uint8_t out_endpoint); 31 static void smartreader_init( S_READER*reader, uint8_t out_endpoint);32 static unsigned int smartreader_determine_max_packet_size( S_READER*reader);33 static int smartreader_usb_close_internal ( S_READER*reader);34 static int smartreader_usb_reset( S_READER*reader);35 static int smartreader_usb_open_dev( S_READER*reader);36 static int smartreader_usb_purge_rx_buffer( S_READER*reader);37 static int smartreader_usb_purge_tx_buffer( S_READER*reader);38 static int smartreader_usb_purge_buffers( S_READER*reader);39 static int smartreader_convert_baudrate(int baudrate, S_READER*reader, unsigned short *value, unsigned short *index);40 static int smartreader_set_baudrate( S_READER*reader, int baudrate);41 static int smartreader_setdtr_rts( S_READER*reader, int dtr, int rts);42 static int smartreader_setflowctrl( S_READER*reader, int flowctrl);43 static int smartreader_set_line_property2( S_READER*reader, enum smartreader_bits_type bits,32 static void smartreader_init(struct s_reader *reader, uint8_t out_endpoint); 33 static unsigned int smartreader_determine_max_packet_size(struct s_reader *reader); 34 static int smartreader_usb_close_internal (struct s_reader *reader); 35 static int smartreader_usb_reset(struct s_reader *reader); 36 static int smartreader_usb_open_dev(struct s_reader *reader); 37 static int smartreader_usb_purge_rx_buffer(struct s_reader *reader); 38 static int smartreader_usb_purge_tx_buffer(struct s_reader *reader); 39 static int smartreader_usb_purge_buffers(struct s_reader *reader); 40 static int smartreader_convert_baudrate(int baudrate, struct s_reader *reader, unsigned short *value, unsigned short *index); 41 static int smartreader_set_baudrate(struct s_reader *reader, int baudrate); 42 static int smartreader_setdtr_rts(struct s_reader *reader, int dtr, int rts); 43 static int smartreader_setflowctrl(struct s_reader *reader, int flowctrl); 44 static int smartreader_set_line_property2(struct s_reader *reader, enum smartreader_bits_type bits, 44 45 enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity, 45 46 enum smartreader_break_type break_type); 46 static int smartreader_set_line_property( S_READER*reader, enum smartreader_bits_type bits,47 static int smartreader_set_line_property(struct s_reader *reader, enum smartreader_bits_type bits, 47 48 enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity); 48 static void smart_flush( S_READER*reader);49 static int smartreader_set_latency_timer( S_READER*reader, unsigned short latency);50 static void EnableSmartReader( S_READER*reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T,unsigned char inv, int parity);49 static void smart_flush(struct s_reader *reader); 50 static int smartreader_set_latency_timer(struct s_reader *reader, unsigned short latency); 51 static void EnableSmartReader(struct s_reader *reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T,unsigned char inv, int parity); 51 52 static void *ReaderThread(void *p); 52 53 … … 74 75 cs_debug_mask (D_IFD,"IO:SR: Looking for device %s on bus %s",devname,busname); 75 76 76 ret = libusb_init(NULL); 77 77 ret = libusb_init(NULL); //FIXME should we really init for every smartreader or only for first one? 78 if (ret < 0) { 78 79 cs_log("Libusb init error : %d",ret); 79 80 return ret; … … 127 128 pthread_mutex_init(&reader->sr_config->g_read_mutex,NULL); 128 129 pthread_mutex_init(&reader->sr_config->g_usb_mutex,NULL); 129 ret = pthread_create(&reader->sr_config->rt, NULL, ReaderThread, (void *)(reader)); 130 if (ret) { 130 if (number_of_smartreaders_running++ <= 1) { //first smartreader starting needs to start readerthread 131 ret = pthread_create(&libusb_thread, NULL, ReaderThread, (void *)(reader)); 132 if (ret) { 131 133 cs_log("ERROR; return code from pthread_create() is %d", ret); 132 134 return ERROR; 133 }134 135 135 } 136 } 137 return OK; 136 138 } 137 139 … … 155 157 } 156 158 157 static int smart_read( S_READER*reader, unsigned char* buff, unsigned int size, int timeout_sec)159 static int smart_read(struct s_reader *reader, unsigned char* buff, unsigned int size, int timeout_sec) 158 160 { 159 161 … … 283 285 } 284 286 285 static int smart_write( S_READER*reader, unsigned char* buff, unsigned int size)287 static int smart_write(struct s_reader *reader, unsigned char* buff, unsigned int size) 286 288 { 287 289 … … 390 392 { 391 393 392 cs_debug_mask(D_IFD,"IO:SR: Closing smarteader\n"); 393 394 reader->sr_config->running=FALSE; 395 pthread_join(reader->sr_config->rt,NULL); 394 cs_debug_mask(D_IFD,"IO:SR: Closing smarteader\n"); 395 396 396 libusb_close(reader->sr_config->usb_dev_handle); 397 libusb_exit(NULL); 397 if (number_of_smartreaders_running-- <= 0) {//last reader is closed, thread must be closed also 398 pthread_join(libusb_thread,NULL); 399 libusb_exit(NULL); 400 } 398 401 free(reader->sr_config); 399 402 return OK; … … 422 425 } 423 426 424 static void EnableSmartReader( S_READER*reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T, unsigned char inv,int parity) {427 static void EnableSmartReader(struct s_reader *reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T, unsigned char inv,int parity) { 425 428 426 429 int ret = 0; … … 615 618 } 616 619 617 void smartreader_init( S_READER*reader,uint8_t out_endpoint)620 void smartreader_init(struct s_reader *reader,uint8_t out_endpoint) 618 621 { 619 622 reader->sr_config->usb_dev = NULL; … … 636 639 637 640 638 static unsigned int smartreader_determine_max_packet_size( S_READER*reader)641 static unsigned int smartreader_determine_max_packet_size(struct s_reader *reader) 639 642 { 640 643 unsigned int packet_size; … … 684 687 685 688 686 static int smartreader_usb_close_internal ( S_READER*reader)689 static int smartreader_usb_close_internal (struct s_reader *reader) 687 690 { 688 691 int ret = 0; … … 698 701 699 702 700 int smartreader_usb_reset( S_READER*reader)703 int smartreader_usb_reset(struct s_reader *reader) 701 704 { 702 705 if (libusb_control_transfer(reader->sr_config->usb_dev_handle, … … 717 720 718 721 719 int smartreader_usb_open_dev( S_READER*reader)722 int smartreader_usb_open_dev(struct s_reader *reader) 720 723 { 721 724 int detach_errno = 0; … … 851 854 852 855 853 int smartreader_usb_purge_rx_buffer( S_READER*reader)856 int smartreader_usb_purge_rx_buffer(struct s_reader *reader) 854 857 { 855 858 if (libusb_control_transfer(reader->sr_config->usb_dev_handle, … … 869 872 } 870 873 871 int smartreader_usb_purge_tx_buffer( S_READER*reader)874 int smartreader_usb_purge_tx_buffer(struct s_reader *reader) 872 875 { 873 876 if (libusb_control_transfer(reader->sr_config->usb_dev_handle, … … 886 889 } 887 890 888 int smartreader_usb_purge_buffers( S_READER*reader)891 int smartreader_usb_purge_buffers(struct s_reader *reader) 889 892 { 890 893 int result; … … 901 904 } 902 905 903 static int smartreader_convert_baudrate(int baudrate, S_READER*reader, unsigned short *value, unsigned short *index)906 static int smartreader_convert_baudrate(int baudrate, struct s_reader *reader, unsigned short *value, unsigned short *index) 904 907 { 905 908 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1}; … … 1021 1024 } 1022 1025 1023 int smartreader_set_baudrate( S_READER*reader, int baudrate)1026 int smartreader_set_baudrate(struct s_reader *reader, int baudrate) 1024 1027 { 1025 1028 unsigned short value, index; … … 1062 1065 } 1063 1066 1064 int smartreader_setdtr_rts( S_READER*reader, int dtr, int rts)1067 int smartreader_setdtr_rts(struct s_reader *reader, int dtr, int rts) 1065 1068 { 1066 1069 unsigned short usb_val; … … 1091 1094 } 1092 1095 1093 int smartreader_setflowctrl( S_READER*reader, int flowctrl)1096 int smartreader_setflowctrl(struct s_reader *reader, int flowctrl) 1094 1097 { 1095 1098 if (libusb_control_transfer(reader->sr_config->usb_dev_handle, … … 1108 1111 } 1109 1112 1110 int smartreader_set_line_property2( S_READER*reader, enum smartreader_bits_type bits,1113 int smartreader_set_line_property2(struct s_reader *reader, enum smartreader_bits_type bits, 1111 1114 enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity, 1112 1115 enum smartreader_break_type break_type) … … 1172 1175 1173 1176 1174 int smartreader_set_line_property( S_READER*reader, enum smartreader_bits_type bits,1177 int smartreader_set_line_property(struct s_reader *reader, enum smartreader_bits_type bits, 1175 1178 enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity) 1176 1179 { … … 1180 1183 1181 1184 1182 void smart_flush( S_READER*reader)1185 void smart_flush(struct s_reader *reader) 1183 1186 { 1184 1187 … … 1191 1194 } 1192 1195 1193 static int smartreader_set_latency_timer( S_READER*reader, unsigned short latency)1196 static int smartreader_set_latency_timer(struct s_reader *reader, unsigned short latency) 1194 1197 { 1195 1198 unsigned short usb_val; … … 1277 1280 1278 1281 reader = (struct s_reader *)p; 1279 reader->sr_config->running=TRUE;1280 1282 1281 1283 for(idx=0; idx<NUM_TXFERS; idx++) { … … 1294 1296 } 1295 1297 1296 while( reader->sr_config->running) {1298 while(number_of_smartreaders_running > 0) { 1297 1299 ret = libusb_handle_events(NULL); 1298 1300 if(ret!=0)
Note:
See TracChangeset
for help on using the changeset viewer.