apdu_connect (int slot)
{
  int sw;
  unsigned int status;

  if (DBG_READER)
    log_debug ("enter: apdu_connect: slot=%d\n", slot);

  if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
    {
      if (DBG_READER)
        log_debug ("leave: apdu_connect => SW_HOST_NO_DRIVER\n");
      return SW_HOST_NO_DRIVER;
    }

  /* Only if the access method provides a connect function we use it.
     If not, we expect that the card has been implicitly connected by
     apdu_open_reader.  */
  if (reader_table[slot].connect_card)
    {
      sw = lock_slot (slot);
      if (!sw)
        {
          sw = reader_table[slot].connect_card (slot);
          unlock_slot (slot);
        }
    }
  else
    sw = 0;

  /* We need to call apdu_get_status_internal, so that the last-status
     machinery gets setup properly even if a card is inserted while
     scdaemon is fired up and apdu_get_status has not yet been called.
     Without that we would force a reset of the card with the next
     call to apdu_get_status.  */
  apdu_get_status_internal (slot, 1, 1, &status, NULL);
  if (sw)
    ;
  else if (!(status & APDU_CARD_PRESENT))
    sw = SW_HOST_NO_CARD;
  else if ((status & APDU_CARD_PRESENT) && !(status & APDU_CARD_ACTIVE))
    sw = SW_HOST_CARD_INACTIVE;

  if (DBG_READER)
    log_debug ("leave: apdu_connect => sw=0x%x\n", sw);

  return sw;
}
apdu_connect (int slot)
{
  int sw;
  unsigned int status;

  if (DBG_READER)
    log_debug ("enter: apdu_connect: slot=%d\n", slot);

  if (slot < 0 || slot >= MAX_READER || !reader_table[slot].used )
    {
      if (DBG_READER)
        log_debug ("leave: apdu_connect => SW_HOST_NO_DRIVER\n");
      return SW_HOST_NO_DRIVER;
    }

   
  if (reader_table[slot].connect_card)
    {
      sw = lock_slot (slot);
      if (!sw)
        {
          sw = reader_table[slot].connect_card (slot);
          unlock_slot (slot);
        }
    }
  else
    sw = 0;

   
  apdu_get_status_internal (slot, 1, 1, &status, NULL);
  if (sw)
    ;
  else if (!(status & APDU_CARD_PRESENT))
    sw = SW_HOST_NO_CARD;
  else if ((status & APDU_CARD_PRESENT) && !(status & APDU_CARD_ACTIVE))
    sw = SW_HOST_CARD_INACTIVE;

  if (DBG_READER)
    log_debug ("leave: apdu_connect => sw=0x%x\n", sw);

  return sw;
}