~ubuntu-branches/ubuntu/feisty/gnupg2/feisty

« back to all changes in this revision

Viewing changes to scd/ccid-driver.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-07-11 11:38:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060711113813-zaw7unlbuh7gyxtl
Tags: 1.9.21-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* ccid-driver.c - USB ChipCardInterfaceDevices driver
2
 
 *      Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
3
3
 *      Written by Werner Koch.
4
4
 *
5
5
 * This file is part of GnuPG.
53
53
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54
54
 * OF THE POSSIBILITY OF SUCH DAMAGE.
55
55
 *
56
 
 * $Date: 2005/06/16 08:11:59 $
 
56
 * $Date: 2006-03-01 12:05:47 +0100 (Wed, 01 Mar 2006) $
57
57
 */
58
58
 
59
59
 
67
67
   portable access to USB.
68
68
 
69
69
   This driver has been tested with the SCM SCR335 and SPR532
70
 
   smartcard readers and requires that a reader implements the TPDU
71
 
   level exchange and does fully automatic initialization.
 
70
   smartcard readers and requires that a reader implements APDU or
 
71
   TPDU level exchange and does fully automatic initialization.
72
72
*/
73
73
 
74
74
#ifdef HAVE_CONFIG_H
82
82
#include <stdlib.h>
83
83
#include <string.h>
84
84
#include <assert.h>
 
85
#include <sys/types.h>
 
86
#include <sys/stat.h>
 
87
#include <fcntl.h>
85
88
 
86
89
#include <usb.h>
87
90
 
194
197
 
195
198
/* We need to know the vendor to do some hacks. */
196
199
enum {
 
200
  VENDOR_CHERRY = 0x046a,
197
201
  VENDOR_SCM    = 0x04e6,
198
 
  VENDOR_CHERRY = 0x046a,
 
202
  VENDOR_OMNIKEY= 0x076b,
199
203
  VENDOR_GEMPC  = 0x08e6
200
204
};
201
205
 
 
206
/* A list and a table with special transport descriptions. */
 
207
enum {
 
208
  TRANSPORT_USB    = 0, /* Standard USB transport. */
 
209
  TRANSPORT_CM4040 = 1  /* As used by the Cardman 4040. */
 
210
};
 
211
 
 
212
static struct
 
213
 
214
  char *name;  /* Device name. */
 
215
  int  type;
 
216
 
 
217
} transports[] = {
 
218
  { "/dev/cmx0", TRANSPORT_CM4040 },  
 
219
  { "/dev/cmx1", TRANSPORT_CM4040 },  
 
220
  { NULL },
 
221
};
 
222
 
202
223
 
203
224
/* Store information on the driver's state.  A pointer to such a
204
225
   structure is used as handle for most functions. */
206
227
{
207
228
  usb_dev_handle *idev;
208
229
  char *rid;
 
230
  int dev_fd;  /* -1 for USB transport or file descriptor of the
 
231
                   transport device. */
209
232
  unsigned short id_vendor;
210
233
  unsigned short id_product;
211
234
  unsigned short bcd_device;
306
329
}
307
330
  
308
331
 
309
 
 
 
332
/* Given a handle used for special transport prepare it for use.  In
 
333
   particular setup all information in way that resembles what
 
334
   parse_cccid_descriptor does. */
 
335
static void
 
336
prepare_special_transport (ccid_driver_t handle)
 
337
{
 
338
  assert (!handle->id_vendor);
 
339
 
 
340
  handle->nonnull_nad = 0;
 
341
  handle->auto_ifsd = 0;
 
342
  handle->max_ifsd = 32;
 
343
  handle->ifsd = 0;
 
344
  handle->has_pinpad = 0;
 
345
  handle->apdu_level = 0;
 
346
  switch (handle->id_product)
 
347
    {
 
348
    case TRANSPORT_CM4040:
 
349
      DEBUGOUT ("setting up transport for CardMan 4040\n");
 
350
      handle->apdu_level = 1;
 
351
      break;
 
352
 
 
353
    default: assert (!"transport not defined");
 
354
    }
 
355
}
310
356
 
311
357
/* Parse a CCID descriptor, optionally print all available features
312
358
   and test whether this reader is usable by this driver.  Returns 0
615
661
  char *rid;
616
662
  char prefix[20];
617
663
 
618
 
  sprintf (prefix, "%04X:%04X:", (vendor & 0xfff), (product & 0xffff));
 
664
  sprintf (prefix, "%04X:%04X:", (vendor & 0xffff), (product & 0xffff));
619
665
  rid = get_escaped_usb_string (idev, serialno_index, prefix, ":0");
620
666
  if (!rid)
621
667
    {
658
704
}
659
705
 
660
706
 
 
707
/* Helper for scan_or_find_devices. This function returns true if a
 
708
   requested device has been found or the caller should stop scanning
 
709
   for other reasons. */
 
710
static int
 
711
scan_or_find_usb_device (int scan_mode,
 
712
                         int *readerno, int *count, char **rid_list,
 
713
                         const char *readerid,
 
714
                         struct usb_device *dev,
 
715
                         char **r_rid,
 
716
                         struct usb_device **r_dev,
 
717
                         usb_dev_handle **r_idev,
 
718
                         unsigned char **ifcdesc_extra,
 
719
                         size_t *ifcdesc_extra_len,
 
720
                         int *interface_number,
 
721
                         int *ep_bulk_out, int *ep_bulk_in, int *ep_intr)
 
722
{
 
723
  int cfg_no;
 
724
  int ifc_no;
 
725
  int set_no;
 
726
  struct usb_config_descriptor *config;
 
727
  struct usb_interface *interface;          
 
728
  struct usb_interface_descriptor *ifcdesc;
 
729
  char *rid;
 
730
  usb_dev_handle *idev;
 
731
 
 
732
  *r_idev = NULL;
 
733
 
 
734
  for (cfg_no=0; cfg_no < dev->descriptor.bNumConfigurations; cfg_no++)
 
735
    {
 
736
      config = dev->config + cfg_no;
 
737
      if(!config)
 
738
        continue;
 
739
 
 
740
      for (ifc_no=0; ifc_no < config->bNumInterfaces; ifc_no++)
 
741
        {
 
742
          interface = config->interface + ifc_no;
 
743
          if (!interface)
 
744
            continue;
 
745
                  
 
746
          for (set_no=0; set_no < interface->num_altsetting; set_no++)
 
747
            {
 
748
              ifcdesc = (interface->altsetting + set_no);
 
749
              /* The second condition is for older SCM SPR 532 who did
 
750
                 not know about the assigned CCID class.  Instead of
 
751
                 trying to interpret the strings we simply check the
 
752
                 product ID. */
 
753
              if (ifcdesc && ifcdesc->extra
 
754
                  && ((ifcdesc->bInterfaceClass == 11
 
755
                       && ifcdesc->bInterfaceSubClass == 0
 
756
                       && ifcdesc->bInterfaceProtocol == 0)
 
757
                      || (ifcdesc->bInterfaceClass == 255
 
758
                          && dev->descriptor.idVendor == VENDOR_SCM
 
759
                          && dev->descriptor.idProduct == 0xe003)))
 
760
                {
 
761
                  idev = usb_open (dev);
 
762
                  if (!idev)
 
763
                    {
 
764
                      DEBUGOUT_1 ("usb_open failed: %s\n",
 
765
                                  strerror (errno));
 
766
                      continue; /* with next setting. */
 
767
                    }
 
768
                  
 
769
                  rid = make_reader_id (idev,
 
770
                                        dev->descriptor.idVendor,
 
771
                                        dev->descriptor.idProduct,
 
772
                                        dev->descriptor.iSerialNumber);
 
773
                  if (rid)
 
774
                    {
 
775
                      if (scan_mode)
 
776
                        {
 
777
                          char *p;
 
778
                          
 
779
                          /* We are collecting infos about all
 
780
                             available CCID readers.  Store them and
 
781
                             continue. */
 
782
                          DEBUGOUT_2 ("found CCID reader %d (ID=%s)\n",
 
783
                                      *count, rid );
 
784
                          p = malloc ((*rid_list? strlen (*rid_list):0) + 1
 
785
                                      + strlen (rid) + 1);
 
786
                          if (p)
 
787
                            {
 
788
                              *p = 0;
 
789
                              if (*rid_list)
 
790
                                {
 
791
                                  strcat (p, *rid_list);
 
792
                                  free (*rid_list);
 
793
                                }
 
794
                              strcat (p, rid);
 
795
                              strcat (p, "\n");
 
796
                              *rid_list = p;
 
797
                            }
 
798
                          else /* Out of memory. */
 
799
                            free (rid);
 
800
                          
 
801
                          rid = NULL;
 
802
                          ++*count;
 
803
                        }
 
804
                      else if (!*readerno
 
805
                               || (*readerno < 0
 
806
                                   && readerid
 
807
                                   && !strcmp (readerid, rid)))
 
808
                        {
 
809
                          /* We found the requested reader. */
 
810
                          if (ifcdesc_extra && ifcdesc_extra_len)
 
811
                            {
 
812
                              *ifcdesc_extra = malloc (ifcdesc
 
813
                                                       ->extralen);
 
814
                              if (!*ifcdesc_extra)
 
815
                                {
 
816
                                  usb_close (idev);
 
817
                                  free (rid);
 
818
                                  return 1; /* Out of core. */
 
819
                                }
 
820
                              memcpy (*ifcdesc_extra, ifcdesc->extra,
 
821
                                      ifcdesc->extralen);
 
822
                              *ifcdesc_extra_len = ifcdesc->extralen;
 
823
                            }
 
824
 
 
825
                          if (interface_number)
 
826
                            *interface_number = (ifcdesc->bInterfaceNumber);
 
827
 
 
828
                          if (ep_bulk_out)
 
829
                            *ep_bulk_out = find_endpoint (ifcdesc, 0);
 
830
                          if (ep_bulk_in)
 
831
                            *ep_bulk_in = find_endpoint (ifcdesc, 1);
 
832
                          if (ep_intr)
 
833
                            *ep_intr = find_endpoint (ifcdesc, 2);
 
834
 
 
835
                          if (r_dev)
 
836
                            *r_dev = dev;
 
837
                          if (r_rid)
 
838
                            {
 
839
                              *r_rid = rid;
 
840
                              rid = NULL;
 
841
                            }
 
842
                          else
 
843
                            free (rid);
 
844
                          
 
845
                          *r_idev = idev;
 
846
                          return 1; /* Found requested device. */
 
847
                        }
 
848
                      else
 
849
                        {
 
850
                          /* This is not yet the reader we want.
 
851
                             fixme: We should avoid the extra usb_open
 
852
                             in this case. */
 
853
                          if (*readerno >= 0)
 
854
                            --*readerno;
 
855
                        }
 
856
                      free (rid);
 
857
                    }
 
858
                  
 
859
                  usb_close (idev);
 
860
                  idev = NULL;
 
861
                  return 0;
 
862
                }
 
863
            }
 
864
        }
 
865
    }
 
866
 
 
867
  return 0;
 
868
}
661
869
 
662
870
/* Combination function to either scan all CCID devices or to find and
663
871
   open one specific device. 
664
872
 
 
873
   The function returns 0 if a reader has been found or when a scan
 
874
   returned without error.
 
875
 
665
876
   With READERNO = -1 and READERID is NULL, scan mode is used and
666
877
   R_RID should be the address where to store the list of reader_ids
667
878
   we found.  If on return this list is empty, no CCID device has been
671
882
   With READERNO >= 0 or READERID is not NULL find mode is used.  This
672
883
   uses the same algorithm as the scan mode but stops and returns at
673
884
   the entry number READERNO and return the handle for the the opened
674
 
   USB device. If R_ID is not NULL it will receive the reader ID of
 
885
   USB device. If R_RID is not NULL it will receive the reader ID of
675
886
   that device.  If R_DEV is not NULL it will the device pointer of
676
887
   that device.  If IFCDESC_EXTRA is NOT NULL it will receive a
677
888
   malloced copy of the interfaces "extra: data filed;
678
 
   IFCDESC_EXTRA_LEN receive the lengtyh of this field.  If there is
 
889
   IFCDESC_EXTRA_LEN receive the length of this field.  If there is
679
890
   no reader with number READERNO or that reader is not usable by our
680
891
   implementation NULL will be returned.  The caller must close a
681
892
   returned USB device handle and free (if not passed as NULL) the
684
895
   IFCDESC_EXTRA_LEN.  With READERID being -1 the function stops if
685
896
   the READERID was found.
686
897
 
 
898
   If R_FD is not -1 on return the device is not using USB for
 
899
   transport but the device associated with that file descriptor.  In
 
900
   this case INTERFACE will receive the transport type and the other
 
901
   USB specific return values are not used; the return value is
 
902
   (void*)(1).
 
903
 
687
904
   Note that the first entry of the returned reader ID list in scan mode
688
905
   corresponds with a READERNO of 0 in find mode.
689
906
*/
690
 
static usb_dev_handle *
 
907
static int
691
908
scan_or_find_devices (int readerno, const char *readerid,
692
909
                      char **r_rid,
693
910
                      struct usb_device **r_dev,
694
911
                      unsigned char **ifcdesc_extra,
695
912
                      size_t *ifcdesc_extra_len,
696
913
                      int *interface_number,
697
 
                      int *ep_bulk_out, int *ep_bulk_in, int *ep_intr)
 
914
                      int *ep_bulk_out, int *ep_bulk_in, int *ep_intr,
 
915
                      usb_dev_handle **r_idev,
 
916
                      int *r_fd)
698
917
{
699
918
  char *rid_list = NULL;
700
919
  int count = 0;
702
921
  struct usb_device *dev = NULL;
703
922
  usb_dev_handle *idev = NULL;
704
923
  int scan_mode = (readerno == -1 && !readerid);
 
924
  int i;
705
925
 
706
 
   /* Set return values to a default. */
 
926
  /* Set return values to a default. */
707
927
  if (r_rid)
708
928
    *r_rid = NULL;
709
929
  if (r_dev)
714
934
    *ifcdesc_extra_len = 0;
715
935
  if (interface_number)
716
936
    *interface_number = 0;
 
937
  if (r_idev)
 
938
    *r_idev = NULL;
 
939
  if (r_fd)
 
940
    *r_fd = -1;
717
941
 
718
942
  /* See whether we want scan or find mode. */
719
943
  if (scan_mode) 
734
958
    {
735
959
      for (dev = bus->devices; dev; dev = dev->next)
736
960
        {
737
 
          int cfg_no;
738
 
          
739
 
          for (cfg_no=0; cfg_no < dev->descriptor.bNumConfigurations; cfg_no++)
 
961
          if (scan_or_find_usb_device (scan_mode, &readerno, &count, &rid_list,
 
962
                                       readerid,
 
963
                                       dev,
 
964
                                       r_rid,
 
965
                                       r_dev,
 
966
                                       &idev,
 
967
                                       ifcdesc_extra,
 
968
                                       ifcdesc_extra_len,
 
969
                                       interface_number,
 
970
                                       ep_bulk_out, ep_bulk_in, ep_intr))
740
971
            {
741
 
              struct usb_config_descriptor *config = dev->config + cfg_no;
742
 
              int ifc_no;
743
 
 
744
 
              if(!config)
745
 
                continue;
746
 
 
747
 
              for (ifc_no=0; ifc_no < config->bNumInterfaces; ifc_no++)
 
972
              /* Found requested device or out of core. */
 
973
              if (!idev)
748
974
                {
749
 
                  struct usb_interface *interface
750
 
                    = config->interface + ifc_no;
751
 
                  int set_no;
752
 
                  
753
 
                  if (!interface)
754
 
                    continue;
755
 
                  
756
 
                  for (set_no=0; set_no < interface->num_altsetting; set_no++)
757
 
                    {
758
 
                      struct usb_interface_descriptor *ifcdesc
759
 
                        = interface->altsetting + set_no;
760
 
                      char *rid;
761
 
                      
762
 
                      /* The second condition is for some SCM Micro
763
 
                         SPR 532 which does not know about the
764
 
                         assigned CCID class. Instead of trying to
765
 
                         interpret the strings we simply look at the
766
 
                         product ID. */
767
 
                      if (ifcdesc && ifcdesc->extra
768
 
                          && (   (ifcdesc->bInterfaceClass == 11
769
 
                                  && ifcdesc->bInterfaceSubClass == 0
770
 
                                  && ifcdesc->bInterfaceProtocol == 0)
771
 
                              || (ifcdesc->bInterfaceClass == 255
772
 
                                  && dev->descriptor.idVendor == 0x04e6
773
 
                                  && dev->descriptor.idProduct == 0xe003)))
774
 
                        {
775
 
                          idev = usb_open (dev);
776
 
                          if (!idev)
777
 
                            {
778
 
                              DEBUGOUT_1 ("usb_open failed: %s\n",
779
 
                                          strerror (errno));
780
 
                              continue;
781
 
                            }
782
 
                              
783
 
                          rid = make_reader_id (idev,
784
 
                                                dev->descriptor.idVendor,
785
 
                                                dev->descriptor.idProduct,
786
 
                                                dev->descriptor.iSerialNumber);
787
 
                          if (rid)
788
 
                            {
789
 
                              if (scan_mode)
790
 
                                {
791
 
                                  char *p;
792
 
 
793
 
                                  /* We are collecting infos about all
794
 
                                     available CCID readers.  Store
795
 
                                     them and continue. */
796
 
                                  DEBUGOUT_2 ("found CCID reader %d "
797
 
                                              "(ID=%s)\n",
798
 
                                              count, rid );
799
 
                                  if ((p = malloc ((rid_list?
800
 
                                                    strlen (rid_list):0)
801
 
                                                   + 1 + strlen (rid)
802
 
                                                   + 1)))
803
 
                                    {
804
 
                                      *p = 0;
805
 
                                      if (rid_list)
806
 
                                        {
807
 
                                          strcat (p, rid_list);
808
 
                                          free (rid_list);
809
 
                                        }
810
 
                                      strcat (p, rid);
811
 
                                      strcat (p, "\n");
812
 
                                      rid_list = p;
813
 
                                    }
814
 
                                  else /* Out of memory. */
815
 
                                    free (rid);
816
 
                                  rid = NULL;
817
 
                                  count++;
818
 
                                }
819
 
                              else if (!readerno
820
 
                                       || (readerno < 0
821
 
                                           && readerid
822
 
                                           && !strcmp (readerid, rid)))
823
 
                                {
824
 
                                  /* We found the requested reader. */
825
 
                                  if (ifcdesc_extra && ifcdesc_extra_len)
826
 
                                    {
827
 
                                      *ifcdesc_extra = malloc (ifcdesc
828
 
                                                               ->extralen);
829
 
                                      if (!*ifcdesc_extra)
830
 
                                        {
831
 
                                          usb_close (idev);
832
 
                                          free (rid);
833
 
                                          return NULL; /* Out of core. */
834
 
                                        }
835
 
                                      memcpy (*ifcdesc_extra, ifcdesc->extra,
836
 
                                              ifcdesc->extralen);
837
 
                                      *ifcdesc_extra_len = ifcdesc->extralen;
838
 
                                    }
839
 
                                  if (interface_number)
840
 
                                    *interface_number = (ifcdesc->
841
 
                                                         bInterfaceNumber);
842
 
                                  if (ep_bulk_out)
843
 
                                    *ep_bulk_out = find_endpoint (ifcdesc, 0);
844
 
                                  if (ep_bulk_in)
845
 
                                    *ep_bulk_in = find_endpoint (ifcdesc, 1);
846
 
                                  if (ep_intr)
847
 
                                    *ep_intr = find_endpoint (ifcdesc, 2);
848
 
 
849
 
 
850
 
                                  if (r_dev)
851
 
                                    *r_dev = dev;
852
 
                                  if (r_rid)
853
 
                                    {
854
 
                                      *r_rid = rid;
855
 
                                      rid = NULL;
856
 
                                    }
857
 
                                  else
858
 
                                    free (rid);
859
 
                                  return idev; /* READY. */
860
 
                                }
861
 
                              else
862
 
                                {
863
 
                                  /* This is not yet the reader we
864
 
                                     want.  fixme: We could avoid the
865
 
                                     extra usb_open in this case. */
866
 
                                  if (readerno >= 0)
867
 
                                    readerno--;
868
 
                                }
869
 
                              free (rid);
870
 
                            }
871
 
                          
872
 
                          usb_close (idev);
873
 
                          idev = NULL;
874
 
                          goto next_device;
875
 
                        }
876
 
                    }
 
975
                  free (rid_list);
 
976
                  return -1; /* error */
877
977
                }
878
 
            }
879
 
        next_device:
880
 
          ;
881
 
        }
 
978
              *r_idev = idev;
 
979
              return 0; 
 
980
            }
 
981
        }
 
982
    }
 
983
 
 
984
  /* Now check whether there are any devices with special transport types. */
 
985
  for (i=0; transports[i].name; i++)
 
986
    {
 
987
      int fd;
 
988
      char *rid, *p;
 
989
 
 
990
      fd = open (transports[i].name, O_RDWR);
 
991
      if (fd == -1)
 
992
        {
 
993
          log_debug ("failed to open `%s': %s\n",
 
994
                     transports[i].name, strerror (errno));
 
995
          continue;
 
996
        }
 
997
      log_debug ("opened `%s': fd=%d\n", transports[i].name, fd);
 
998
 
 
999
      rid = malloc (strlen (transports[i].name) + 30 + 10);
 
1000
      if (!rid)
 
1001
        {
 
1002
          close (fd);
 
1003
          free (rid_list);
 
1004
          return -1; /* Error. */
 
1005
        }
 
1006
      sprintf (rid, "0000:%04X:%s:0", transports[i].type, transports[i].name);
 
1007
      if (scan_mode)
 
1008
        {
 
1009
          DEBUGOUT_2 ("found CCID reader %d (ID=%s)\n", count, rid);
 
1010
          p = malloc ((rid_list? strlen (rid_list):0) + 1 + strlen (rid) + 1);
 
1011
          if (!p)
 
1012
            {
 
1013
              close (fd);
 
1014
              free (rid_list);
 
1015
              free (rid);
 
1016
              return -1; /* Error. */
 
1017
            }
 
1018
          *p = 0;
 
1019
          if (rid_list)
 
1020
            {
 
1021
              strcat (p, rid_list);
 
1022
              free (rid_list);
 
1023
            }
 
1024
          strcat (p, rid);
 
1025
          strcat (p, "\n");
 
1026
          rid_list = p;
 
1027
          ++count;
 
1028
        }
 
1029
      else if (!readerno ||
 
1030
               (readerno < 0 && readerid && !strcmp (readerid, rid)))
 
1031
        {
 
1032
          /* Found requested device. */
 
1033
          if (interface_number)
 
1034
            *interface_number = transports[i].type;
 
1035
          if (r_rid)
 
1036
            *r_rid = rid;
 
1037
          else
 
1038
            free (rid);
 
1039
          if (r_fd)
 
1040
            *r_fd = fd;
 
1041
          return 0; /* Okay, found device */
 
1042
        }
 
1043
      else /* This is not yet the reader we want. */
 
1044
        {
 
1045
          if (readerno >= 0)
 
1046
            --readerno;
 
1047
        }
 
1048
      free (rid);
 
1049
      close (fd);
 
1050
      log_debug ("closed fd %d\n", fd);
882
1051
    }
883
1052
 
884
1053
  if (scan_mode)
885
 
    *r_rid = rid_list;
886
 
 
887
 
  return NULL;
 
1054
    {
 
1055
      *r_rid = rid_list;
 
1056
      return 0;
 
1057
    }
 
1058
  else
 
1059
    return -1;
888
1060
}
889
1061
 
890
1062
 
891
 
/* Set the level of debugging to to usea dn return the old level.  -1
 
1063
/* Set the level of debugging to LEVEL and return the old level.  -1
892
1064
   just returns the old level.  A level of 0 disables debugging, 1
893
1065
   enables debugging, 2 enables additional tracing of the T=1
894
1066
   protocol, other values are not yet defined. */
913
1085
      initialized_usb = 1;
914
1086
    }
915
1087
 
916
 
  scan_or_find_devices (-1, NULL, &reader_list, NULL, NULL, NULL, NULL,
917
 
                        NULL, NULL, NULL);
 
1088
  if (scan_or_find_devices (-1, NULL, &reader_list, NULL, NULL, NULL, NULL,
 
1089
                            NULL, NULL, NULL, NULL, NULL))
 
1090
    return NULL; /* Error. */
918
1091
  return reader_list;
919
1092
}
920
1093
 
927
1100
  int rc = 0;
928
1101
  struct usb_device *dev = NULL;
929
1102
  usb_dev_handle *idev = NULL;
 
1103
  int dev_fd = -1;
930
1104
  char *rid = NULL;
931
1105
  unsigned char *ifcdesc_extra = NULL;
932
1106
  size_t ifcdesc_extra_len;
959
1133
  else
960
1134
    readerno = 0;  /* Default. */
961
1135
 
962
 
  idev = scan_or_find_devices (readerno, readerid, &rid, &dev,
963
 
                               &ifcdesc_extra, &ifcdesc_extra_len,
964
 
                               &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr);
965
 
  if (!idev)
 
1136
  if (scan_or_find_devices (readerno, readerid, &rid, &dev,
 
1137
                            &ifcdesc_extra, &ifcdesc_extra_len,
 
1138
                            &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr,
 
1139
                            &idev, &dev_fd) )
966
1140
    {
967
1141
      if (readerno == -1)
968
1142
        DEBUGOUT_1 ("no CCID reader with ID %s\n", readerid );
980
1154
      rc = CCID_DRIVER_ERR_OUT_OF_CORE;
981
1155
      goto leave;
982
1156
    }
983
 
  (*handle)->idev = idev;
984
1157
  (*handle)->rid = rid;
985
 
  (*handle)->id_vendor = dev->descriptor.idVendor;
986
 
  (*handle)->id_product = dev->descriptor.idProduct;
987
 
  (*handle)->bcd_device = dev->descriptor.bcdDevice;
988
 
  (*handle)->ifc_no = ifc_no;
989
 
  (*handle)->ep_bulk_out = ep_bulk_out;
990
 
  (*handle)->ep_bulk_in = ep_bulk_in;
991
 
  (*handle)->ep_intr = ep_intr;
 
1158
  if (idev) /* Regular USB transport. */
 
1159
    {
 
1160
      (*handle)->idev = idev;
 
1161
      (*handle)->dev_fd = -1;
 
1162
      (*handle)->id_vendor = dev->descriptor.idVendor;
 
1163
      (*handle)->id_product = dev->descriptor.idProduct;
 
1164
      (*handle)->bcd_device = dev->descriptor.bcdDevice;
 
1165
      (*handle)->ifc_no = ifc_no;
 
1166
      (*handle)->ep_bulk_out = ep_bulk_out;
 
1167
      (*handle)->ep_bulk_in = ep_bulk_in;
 
1168
      (*handle)->ep_intr = ep_intr;
 
1169
    }
 
1170
  else if (dev_fd != -1) /* Device transport. */
 
1171
    {
 
1172
      (*handle)->idev = NULL;
 
1173
      (*handle)->dev_fd = dev_fd;
 
1174
      (*handle)->id_vendor = 0;  /* Magic vendor for special transport. */
 
1175
      (*handle)->id_product = ifc_no; /* Transport type */
 
1176
      prepare_special_transport (*handle);
 
1177
    }
 
1178
  else
 
1179
    {
 
1180
      assert (!"no transport"); /* Bug. */
 
1181
    }
992
1182
 
993
1183
  DEBUGOUT_2 ("using CCID reader %d (ID=%s)\n",  readerno, rid );
994
1184
 
 
1185
  if (idev)
 
1186
    {
 
1187
      if (parse_ccid_descriptor (*handle, ifcdesc_extra, ifcdesc_extra_len))
 
1188
        {
 
1189
          DEBUGOUT ("device not supported\n");
 
1190
          rc = CCID_DRIVER_ERR_NO_READER;
 
1191
          goto leave;
 
1192
        }
 
1193
      
 
1194
      rc = usb_claim_interface (idev, ifc_no);
 
1195
      if (rc)
 
1196
        {
 
1197
          DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
 
1198
          rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1199
          goto leave;
 
1200
        }
 
1201
    }
995
1202
 
996
 
  if (parse_ccid_descriptor (*handle, ifcdesc_extra, ifcdesc_extra_len))
997
 
    {
998
 
      DEBUGOUT ("device not supported\n");
999
 
      rc = CCID_DRIVER_ERR_NO_READER;
1000
 
      goto leave;
1001
 
    }
1002
 
  
1003
 
  rc = usb_claim_interface (idev, ifc_no);
1004
 
  if (rc)
1005
 
    {
1006
 
      DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1007
 
      rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
1008
 
      goto leave;
1009
 
    }
1010
 
  
1011
1203
 leave:
1012
1204
  free (ifcdesc_extra);
1013
1205
  if (rc)
1015
1207
      free (rid);
1016
1208
      if (idev)
1017
1209
        usb_close (idev);
 
1210
      if (dev_fd != -1)
 
1211
        {
 
1212
          close (dev_fd);
 
1213
          log_debug ("closed fd %d\n", dev_fd);
 
1214
        }
1018
1215
      free (*handle);
1019
1216
      *handle = NULL;
1020
1217
    }
1054
1251
      usb_close (handle->idev);
1055
1252
      handle->idev = NULL;
1056
1253
    }
 
1254
  if (handle->dev_fd != -1)
 
1255
    {
 
1256
      close (handle->dev_fd);
 
1257
      log_debug ("closed fd %d\n", handle->dev_fd);
 
1258
      handle->dev_fd = -1;
 
1259
    }
1057
1260
}
1058
1261
 
1059
1262
 
1080
1283
 
1081
1284
  do_close_reader (handle);
1082
1285
 
1083
 
  idev = scan_or_find_devices (-1, handle->rid, NULL, &dev,
1084
 
                               &ifcdesc_extra, &ifcdesc_extra_len,
1085
 
                               &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr);
1086
 
  if (!idev)
 
1286
  if (scan_or_find_devices (-1, handle->rid, NULL, &dev,
 
1287
                            &ifcdesc_extra, &ifcdesc_extra_len,
 
1288
                            &ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr,
 
1289
                            &idev, NULL) || !idev)
1087
1290
    {
1088
1291
      DEBUGOUT_1 ("no CCID reader with ID %s\n", handle->rid);
1089
1292
      return CCID_DRIVER_ERR_NO_READER;
1090
1293
    }
1091
1294
 
1092
 
 
1093
 
  handle->idev = idev;
1094
 
  handle->ifc_no = ifc_no;
1095
 
  handle->ep_bulk_out = ep_bulk_out;
1096
 
  handle->ep_bulk_in = ep_bulk_in;
1097
 
  handle->ep_intr = ep_intr;
1098
 
 
1099
 
  if (parse_ccid_descriptor (handle, ifcdesc_extra, ifcdesc_extra_len))
1100
 
    {
1101
 
      DEBUGOUT ("device not supported\n");
1102
 
      rc = CCID_DRIVER_ERR_NO_READER;
1103
 
      goto leave;
1104
 
    }
1105
 
  
1106
 
  rc = usb_claim_interface (idev, ifc_no);
1107
 
  if (rc)
1108
 
    {
1109
 
      DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1110
 
      rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
1111
 
      goto leave;
1112
 
    }
1113
 
  
 
1295
  if (idev)
 
1296
    {
 
1297
      handle->idev = idev;
 
1298
      handle->ifc_no = ifc_no;
 
1299
      handle->ep_bulk_out = ep_bulk_out;
 
1300
      handle->ep_bulk_in = ep_bulk_in;
 
1301
      handle->ep_intr = ep_intr;
 
1302
 
 
1303
      if (parse_ccid_descriptor (handle, ifcdesc_extra, ifcdesc_extra_len))
 
1304
        {
 
1305
          DEBUGOUT ("device not supported\n");
 
1306
          rc = CCID_DRIVER_ERR_NO_READER;
 
1307
          goto leave;
 
1308
        }
 
1309
      
 
1310
      rc = usb_claim_interface (idev, ifc_no);
 
1311
      if (rc)
 
1312
        {
 
1313
          DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
 
1314
          rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1315
          goto leave;
 
1316
        }
 
1317
    }
 
1318
 
1114
1319
 leave:
1115
1320
  free (ifcdesc_extra);
1116
1321
  if (rc)
1117
1322
    {
1118
 
      usb_close (handle->idev);
 
1323
      if (handle->idev)
 
1324
        usb_close (handle->idev);
1119
1325
      handle->idev = NULL;
 
1326
      if (handle->dev_fd != -1)
 
1327
        {
 
1328
          close (handle->dev_fd);
 
1329
          log_debug ("closed fd %d\n", handle->dev_fd);
 
1330
        }
 
1331
      handle->dev_fd = -1;
1120
1332
    }
1121
1333
 
1122
1334
  return rc;
1128
1340
int 
1129
1341
ccid_close_reader (ccid_driver_t handle)
1130
1342
{
1131
 
  if (!handle || !handle->idev)
 
1343
  if (!handle || (!handle->idev && handle->dev_fd == -1))
1132
1344
    return 0;
1133
1345
 
1134
1346
  do_close_reader (handle);
1147
1359
}
1148
1360
 
1149
1361
 
 
1362
/* Write NBYTES of BUF to file descriptor FD. */
 
1363
static int
 
1364
writen (int fd, const void *buf, size_t nbytes)
 
1365
{
 
1366
  size_t nleft = nbytes;
 
1367
  int nwritten;
 
1368
  
 
1369
  while (nleft > 0)
 
1370
    {
 
1371
      nwritten = write (fd, buf, nleft);
 
1372
      if (nwritten < 0)
 
1373
        {
 
1374
          if (errno == EINTR)
 
1375
            nwritten = 0;
 
1376
          else
 
1377
            return -1;
 
1378
        }
 
1379
      nleft -= nwritten;
 
1380
      buf = (const char*)buf + nwritten;
 
1381
    }
 
1382
    
 
1383
  return 0;
 
1384
}
 
1385
 
 
1386
 
1150
1387
/* Write a MSG of length MSGLEN to the designated bulk out endpoint.
1151
1388
   Returns 0 on success. */
1152
1389
static int
1154
1391
{
1155
1392
  int rc;
1156
1393
 
1157
 
  rc = usb_bulk_write (handle->idev, 
1158
 
                       handle->ep_bulk_out,
1159
 
                       (char*)msg, msglen,
1160
 
                       1000 /* ms timeout */);
1161
 
  if (rc == msglen)
1162
 
    return 0;
1163
 
 
1164
 
  if (rc == -1)
1165
 
    DEBUGOUT_1 ("usb_bulk_write error: %s\n", strerror (errno));
 
1394
  if (handle->idev)
 
1395
    {
 
1396
      rc = usb_bulk_write (handle->idev, 
 
1397
                           handle->ep_bulk_out,
 
1398
                           (char*)msg, msglen,
 
1399
                           1000 /* ms timeout */);
 
1400
      if (rc == msglen)
 
1401
        return 0;
 
1402
      if (rc == -1)
 
1403
        DEBUGOUT_1 ("usb_bulk_write error: %s\n", strerror (errno));
 
1404
      else
 
1405
        DEBUGOUT_1 ("usb_bulk_write failed: %d\n", rc);
 
1406
    }
1166
1407
  else
1167
 
    DEBUGOUT_1 ("usb_bulk_write failed: %d\n", rc);
 
1408
    {
 
1409
      rc = writen (handle->dev_fd, msg, msglen);
 
1410
      if (!rc)
 
1411
        return 0;
 
1412
      DEBUGOUT_2 ("writen to %d failed: %s\n",
 
1413
                  handle->dev_fd, strerror (errno));
 
1414
      
 
1415
    }
1168
1416
  return CCID_DRIVER_ERR_CARD_IO_ERROR;
1169
1417
}
1170
1418
 
1187
1435
     for USB IOCTLs. */
1188
1436
  memset (buffer, 0, length);
1189
1437
 retry:
1190
 
  rc = usb_bulk_read (handle->idev, 
1191
 
                      handle->ep_bulk_in,
1192
 
                      (char*)buffer, length,
1193
 
                      timeout);
1194
 
  if (rc < 0)
1195
 
    {
1196
 
      DEBUGOUT_1 ("usb_bulk_read error: %s\n", strerror (errno));
1197
 
      return CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1438
  if (handle->idev)
 
1439
    {
 
1440
      rc = usb_bulk_read (handle->idev, 
 
1441
                          handle->ep_bulk_in,
 
1442
                          (char*)buffer, length,
 
1443
                          timeout);
 
1444
      if (rc < 0)
 
1445
        {
 
1446
          DEBUGOUT_1 ("usb_bulk_read error: %s\n", strerror (errno));
 
1447
          return CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1448
        }
 
1449
      *nread = msglen = rc;
 
1450
    }
 
1451
  else
 
1452
    {
 
1453
      rc = read (handle->dev_fd, buffer, length);
 
1454
      if (rc < 0)
 
1455
        {
 
1456
          DEBUGOUT_2 ("read from %d failed: %s\n",
 
1457
                      handle->dev_fd, strerror (errno));
 
1458
          return CCID_DRIVER_ERR_CARD_IO_ERROR;
 
1459
        }
 
1460
      *nread = msglen = rc;
1198
1461
    }
1199
1462
 
1200
 
  *nread = msglen = rc;
1201
1463
 
1202
1464
  if (msglen < 10)
1203
1465
    {
1339
1601
  size_t msglen;
1340
1602
  int i, j;
1341
1603
 
1342
 
  rc = usb_bulk_read (handle->idev, 
1343
 
                      handle->ep_intr,
1344
 
                      (char*)msg, sizeof msg,
1345
 
                      0 /* ms timeout */ );
1346
 
  if (rc < 0 && errno == ETIMEDOUT)
 
1604
  if (handle->idev)
 
1605
    {
 
1606
      rc = usb_bulk_read (handle->idev, 
 
1607
                          handle->ep_intr,
 
1608
                          (char*)msg, sizeof msg,
 
1609
                          0 /* ms timeout */ );
 
1610
      if (rc < 0 && errno == ETIMEDOUT)
 
1611
        return 0;
 
1612
    }
 
1613
  else 
1347
1614
    return 0;
1348
1615
 
1349
1616
  if (rc < 0)
1449
1716
  unsigned int edc;
1450
1717
  int i;
1451
1718
  int tried_iso = 0;
 
1719
  int got_param;
1452
1720
 
1453
1721
  /* First check whether a card is available.  */
1454
1722
  rc = ccid_slot_status (handle, &statusbits);
1457
1725
  if (statusbits == 2)
1458
1726
    return CCID_DRIVER_ERR_NO_CARD;
1459
1727
 
 
1728
    
1460
1729
  /* For an inactive and also for an active card, issue the PowerOn
1461
1730
     command to get the ATR.  */
1462
1731
 again:
1505
1774
      *atrlen = n;
1506
1775
    }
1507
1776
 
 
1777
  got_param = 0;
 
1778
  msg[0] = PC_to_RDR_GetParameters;
 
1779
  msg[5] = 0; /* slot */
 
1780
  msg[6] = seqno = handle->seqno++;
 
1781
  msg[7] = 0; /* RFU */
 
1782
  msg[8] = 0; /* RFU */
 
1783
  msg[9] = 0; /* RFU */
 
1784
  set_msg_len (msg, 0);
 
1785
  msglen = 10;
 
1786
  rc = bulk_out (handle, msg, msglen);
 
1787
  if (!rc)
 
1788
    rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
 
1789
                  seqno, 2000, 0);
 
1790
  if (rc)
 
1791
    DEBUGOUT ("GetParameters failed\n");
 
1792
  else
 
1793
    {
 
1794
      DEBUGOUT ("GetParametes returned");
 
1795
      for (i=0; i < msglen; i++)
 
1796
        DEBUGOUT_CONT_1 (" %02X", msg[i]);
 
1797
      DEBUGOUT_LF ();
 
1798
      if (msglen >= 10)
 
1799
        {
 
1800
          DEBUGOUT_1 ("  protocol ..........: T=%d\n", msg[9]);
 
1801
          if (msglen == 17 && msg[9] == 1)
 
1802
            {
 
1803
              DEBUGOUT_1 ("  bmFindexDindex ....: %02X\n", msg[10]);
 
1804
              DEBUGOUT_1 ("  bmTCCKST1 .........: %02X\n", msg[11]);
 
1805
              DEBUGOUT_1 ("  bGuardTimeT1 ......: %02X\n", msg[12]);
 
1806
              DEBUGOUT_1 ("  bmWaitingIntegersT1: %02X\n", msg[13]);
 
1807
              DEBUGOUT_1 ("  bClockStop ........: %02X\n", msg[14]);
 
1808
              DEBUGOUT_1 ("  bIFSC .............: %d\n", msg[15]);
 
1809
              DEBUGOUT_1 ("  bNadValue .........: %d\n", msg[16]);
 
1810
              got_param = 1;
 
1811
            }
 
1812
        }
 
1813
    }
 
1814
 
1508
1815
  /* Setup parameters to select T=1. */
1509
1816
  msg[0] = PC_to_RDR_SetParameters;
1510
1817
  msg[5] = 0; /* slot */
1513
1820
  msg[8] = 0; /* RFU */
1514
1821
  msg[9] = 0; /* RFU */
1515
1822
 
1516
 
  /* FIXME: Get those values from the ATR. */
1517
 
  msg[10]= 0x01; /* Fi/Di */
1518
 
  msg[11]= 0x10; /* LRC, direct convention. */
1519
 
  msg[12]= 0;    /* Extra guardtime. */
1520
 
  msg[13]= 0x41; /* BWI/CWI */
1521
 
  msg[14]= 0;    /* No clock stoppping. */
1522
 
  msg[15]= 254;  /* IFSC */
1523
 
  msg[16]= 0;    /* Does not support non default NAD values. */
 
1823
  if (!got_param)
 
1824
    {
 
1825
      /* FIXME: Get those values from the ATR. */
 
1826
      msg[10]= 0x01; /* Fi/Di */
 
1827
      msg[11]= 0x10; /* LRC, direct convention. */
 
1828
      msg[12]= 0;    /* Extra guardtime. */
 
1829
      msg[13]= 0x41; /* BWI/CWI */
 
1830
      msg[14]= 0;    /* No clock stoppping. */
 
1831
      msg[15]= 254;  /* IFSC */
 
1832
      msg[16]= 0;    /* Does not support non default NAD values. */
 
1833
    }
1524
1834
  set_msg_len (msg, 7);
1525
1835
  msglen = 10 + 7;
1526
1836
 
1532
1842
  rc = bulk_out (handle, msg, msglen);
1533
1843
  if (rc)
1534
1844
    return rc;
1535
 
  /* Note that we ignore the error code on purpose. */
1536
 
  bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
1537
 
           seqno, 5000, 0);
 
1845
  rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
 
1846
                seqno, 5000, 0);
 
1847
  if (rc)
 
1848
    DEBUGOUT ("SetParameters failed (ignored)\n");
1538
1849
 
1539
1850
  handle->t1_ns = 0;
1540
1851
  handle->t1_nr = 0;
1541
1852
 
1542
 
  /* Send an S-Block with our maximun IFSD to the CCID.  */
1543
 
  if (!handle->auto_ifsd)
 
1853
  /* Send an S-Block with our maximum IFSD to the CCID.  */
 
1854
  if (!handle->apdu_level && !handle->auto_ifsd)
1544
1855
    {
1545
1856
      tpdu = msg+10;
1546
1857
      /* NAD: DAD=1, SAD=0 */