new_reader_slot (void)
{
  int i, reader = -1;
  int err;

  for (i=0; i < MAX_READER; i++)
    {
      if (!reader_table[i].used && reader == -1)
        reader = i;
    }
  if (reader == -1)
    {
      log_error ("new_reader_slot: out of slots\n");
      return -1;
    }
#ifdef USE_NPTH
  if (!reader_table[reader].lock_initialized)
    {
      err = npth_mutex_init (&reader_table[reader].lock, NULL);
      if (err)
        {
          log_error ("error initializing mutex: %s\n", strerror (err));
          return -1;
        }
      reader_table[reader].lock_initialized = 1;
    }
#endif /*USE_NPTH*/
  if (lock_slot (reader))
    {
      log_error ("error locking mutex: %s\n", strerror (errno));
      return -1;
    }
  reader_table[reader].connect_card = NULL;
  reader_table[reader].disconnect_card = NULL;
  reader_table[reader].close_reader = NULL;
  reader_table[reader].shutdown_reader = NULL;
  reader_table[reader].reset_reader = NULL;
  reader_table[reader].get_status_reader = NULL;
  reader_table[reader].send_apdu_reader = NULL;
  reader_table[reader].check_pinpad = check_pcsc_pinpad;
  reader_table[reader].dump_status_reader = NULL;
  reader_table[reader].set_progress_cb = NULL;
  reader_table[reader].pinpad_verify = pcsc_pinpad_verify;
  reader_table[reader].pinpad_modify = pcsc_pinpad_modify;

  reader_table[reader].used = 1;
  reader_table[reader].any_status = 0;
  reader_table[reader].last_status = 0;
  reader_table[reader].is_t0 = 1;
  reader_table[reader].is_spr532 = 0;
  reader_table[reader].pinpad_varlen_supported = 0;
#ifdef NEED_PCSC_WRAPPER
  reader_table[reader].pcsc.req_fd = -1;
  reader_table[reader].pcsc.rsp_fd = -1;
  reader_table[reader].pcsc.pid = (pid_t)(-1);
#endif
  reader_table[reader].pcsc.verify_ioctl = 0;
  reader_table[reader].pcsc.modify_ioctl = 0;
  reader_table[reader].pcsc.pinmin = -1;
  reader_table[reader].pcsc.pinmax = -1;

  return reader;
}
new_reader_slot (void)
{
  int i, reader = -1;
  int err;

  for (i=0; i < MAX_READER; i++)
    {
      if (!reader_table[i].used && reader == -1)
        reader = i;
    }
  if (reader == -1)
    {
      log_error ("new_reader_slot: out of slots\n");
      return -1;
    }
#ifdef USE_NPTH
  if (!reader_table[reader].lock_initialized)
    {
      err = npth_mutex_init (&reader_table[reader].lock, NULL);
      if (err)
        {
          log_error ("error initializing mutex: %s\n", strerror (err));
          return -1;
        }
      reader_table[reader].lock_initialized = 1;
    }
#endif  
  if (lock_slot (reader))
    {
      log_error ("error locking mutex: %s\n", strerror (errno));
      return -1;
    }
  reader_table[reader].connect_card = NULL;
  reader_table[reader].disconnect_card = NULL;
  reader_table[reader].close_reader = NULL;
  reader_table[reader].shutdown_reader = NULL;
  reader_table[reader].reset_reader = NULL;
  reader_table[reader].get_status_reader = NULL;
  reader_table[reader].send_apdu_reader = NULL;
  reader_table[reader].check_pinpad = check_pcsc_pinpad;
  reader_table[reader].dump_status_reader = NULL;
  reader_table[reader].set_progress_cb = NULL;
  reader_table[reader].pinpad_verify = pcsc_pinpad_verify;
  reader_table[reader].pinpad_modify = pcsc_pinpad_modify;

  reader_table[reader].used = 1;
  reader_table[reader].any_status = 0;
  reader_table[reader].last_status = 0;
  reader_table[reader].is_t0 = 1;
  reader_table[reader].is_spr532 = 0;
  reader_table[reader].pinpad_varlen_supported = 0;
#ifdef NEED_PCSC_WRAPPER
  reader_table[reader].pcsc.req_fd = -1;
  reader_table[reader].pcsc.rsp_fd = -1;
  reader_table[reader].pcsc.pid = (pid_t)(-1);
#endif
  reader_table[reader].pcsc.verify_ioctl = 0;
  reader_table[reader].pcsc.modify_ioctl = 0;
  reader_table[reader].pcsc.pinmin = -1;
  reader_table[reader].pcsc.pinmax = -1;

  return reader;
}