ibBoard.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /***************************************************************************
  2. lib/ibBoard.c
  3. -------------------
  4. copyright : (C) 2001,2002 by Frank Mori Hess
  5. email : fmhess@users.sourceforge.net
  6. ***************************************************************************/
  7. /***************************************************************************
  8. * *
  9. * This program is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2 of the License, or *
  12. * (at your option) any later version. *
  13. * *
  14. ***************************************************************************/
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include "ib_internal.h"
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <stdarg.h>
  21. #include <sys/ioctl.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <signal.h>
  25. #include <pthread.h>
  26. #include <stdlib.h>
  27. ibBoard_t ibBoard[ GPIB_MAX_NUM_BOARDS ];
  28. void init_ibboard(ibBoard_t *board)
  29. {
  30. strcpy(board->board_type, "");
  31. board->base = 0;
  32. board->irq = 0;
  33. board->dma = 0;
  34. board->pci_bus = -1;
  35. board->pci_slot = -1;
  36. board->fileno = -1;
  37. strcpy(board->device, "");
  38. board->open_count = 0;
  39. board->is_system_controller = 0;
  40. board->use_event_queue = 0;
  41. board->autospoll = 0;
  42. }
  43. int configure_autospoll(ibConf_t *conf, int enable)
  44. {
  45. autospoll_ioctl_t spoll_enable = enable != 0;
  46. int retval = 0;
  47. ibBoard_t *board = interfaceBoard(conf);
  48. if((spoll_enable && board->autospoll == 0) ||
  49. (spoll_enable == 0 && board->autospoll))
  50. {
  51. retval = ioctl(interfaceBoard(conf)->fileno, IBAUTOSPOLL, &spoll_enable);
  52. if(retval)
  53. {
  54. fprintf(stderr, "libgpib: autospoll ioctl returned error %i\n", retval);
  55. }else
  56. {
  57. board->autospoll = enable != 0;
  58. }
  59. }
  60. return retval;
  61. }
  62. int ibBoardOpen( ibBoard_t *board )
  63. {
  64. int fd;
  65. int flags = 0;
  66. if( board->fileno >= 0 ) return 0;
  67. if( ( fd = open( board->device, O_RDWR | flags ) ) < 0 )
  68. {
  69. setIberr( EDVR );
  70. setIbcnt( errno );
  71. fprintf( stderr, "libgpib: ibBoardOpen failed to open device file %s\n", board->device);
  72. perror( "libgpib" );
  73. return -1;
  74. }
  75. board->fileno = fd;
  76. board->open_count++;
  77. return 0;
  78. }
  79. int ibBoardClose( ibBoard_t *board )
  80. {
  81. if( board->open_count == 0 )
  82. {
  83. fprintf( stderr, "libgpib: bug! board->open_count is zero on close\n");
  84. return -1;
  85. }
  86. board->open_count--;
  87. if( board->open_count > 0 )
  88. return 0;
  89. if( board->fileno >= 0 )
  90. {
  91. close( board->fileno );
  92. board->fileno = -1;
  93. }
  94. return 0;
  95. }
  96. int InternalResetSys( ibConf_t *conf, const Addr4882_t addressList[] )
  97. {
  98. ibBoard_t *board;
  99. int retval;
  100. board = interfaceBoard( conf );
  101. if( addressListIsValid( addressList ) == 0 )
  102. {
  103. setIberr( EARG );
  104. return -1;
  105. }
  106. if( conf->is_interface == 0 )
  107. {
  108. setIberr( EDVR );
  109. return -1;
  110. }
  111. if( is_system_controller( board ) == 0 )
  112. {
  113. setIberr( ESAC );
  114. return -1;
  115. }
  116. if( is_cic( board ) == 0 )
  117. {
  118. setIberr( ECIC );
  119. return -1;
  120. }
  121. retval = remote_enable( board, 1 );
  122. if( retval < 0 ) return retval;
  123. retval = internal_ibsic( conf );
  124. if( retval < 0 ) return retval;
  125. retval = InternalDevClearList( conf, NULL );
  126. if( retval < 0 ) return retval;
  127. retval = InternalSendList( conf, addressList, "*RST", 4, NLend );
  128. if( retval < 0 ) return retval;
  129. return 0;
  130. }
  131. void ResetSys( int boardID, const Addr4882_t addressList[] )
  132. {
  133. ibConf_t *conf;
  134. int retval;
  135. conf = enter_library( boardID );
  136. if( conf == NULL )
  137. {
  138. exit_library( boardID, 1 );
  139. return;
  140. }
  141. retval = InternalResetSys( conf, addressList );
  142. if( retval < 0 )
  143. {
  144. exit_library( boardID, 1 );
  145. return;
  146. }
  147. exit_library( boardID, 0 );
  148. }