home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / usbd0906.zip / usb_20020906.zip / usbcalls / samples / Cardreader / usblinux.cpp < prev    next >
C/C++ Source or Header  |  2001-11-13  |  3KB  |  119 lines

  1. /*
  2.     Author : David Corcoran
  3.     Title  : usblinux.h
  4.     Purpose: To provide Linux abstraction to searaching the
  5.              USB layer.
  6. */
  7.  
  8. #include <unistd.h>
  9. #include <sys/stat.h>
  10. #include <sys/ioctl.h>
  11. #include <fcntl.h>
  12. #include <limits.h>
  13. #include <dirent.h>
  14. #include <stdlib.h>
  15. #include <usblinux.h>
  16. #include <stdio.h>
  17.  
  18. #define PCSCLITE_USB_PATH      "/proc/bus/usb"
  19.  
  20.  
  21. int open_linux_usb_dev ( unsigned int manuID, unsigned int prodID, 
  22.              unsigned int lunNum ) {
  23.  
  24.   DIR                         *dir, *dirB;
  25.   struct dirent               *entry, *entryB;
  26.   char                         dirpath[150];
  27.   struct usb_device_descriptor usbDescriptor;
  28.  
  29.   
  30.   dir = opendir(PCSCLITE_USB_PATH);
  31.   if (!dir) {
  32.     printf("Cannot Open USB Path Directory\n");
  33.   }
  34.   
  35.   
  36.   while ((entry = readdir(dir)) != NULL) {
  37.     
  38.     /* Skip anything starting with a . */
  39.     if (entry->d_name[0] == '.')
  40.       continue;
  41.     if (!strchr("0123456789", entry->d_name[strlen(entry->d_name) - 1])) {
  42.       continue;
  43.     }
  44.     
  45.     sprintf(dirpath, "%s/%s", PCSCLITE_USB_PATH, entry->d_name);
  46.     
  47.     dirB = opendir(dirpath);
  48.     
  49.     if (!dirB) {
  50.       printf("Path does not exist - do you have USB ?\n");
  51.     }
  52.     
  53.     while ((entryB = readdir(dirB)) != NULL) {
  54.       char filename[PATH_MAX + 1];
  55.       struct usb_device *dev;
  56.       int fd, ret;
  57.       
  58.       /* Skip anything starting with a . */
  59.       if (entryB->d_name[0] == '.')
  60.     continue;
  61.       
  62.       
  63.       sprintf(filename, "%s/%s", dirpath, entryB->d_name);
  64.       fd = open(filename, O_RDWR);
  65.       if (fd < 0) {
  66.     continue;
  67.       }
  68.       
  69.       ret = read(fd, (void *)&usbDescriptor, sizeof(usbDescriptor));
  70.  
  71.       if (ret < 0) {
  72.     continue;
  73.       }
  74.       
  75.       /* Device is found and we don't know about it */
  76.       if ( usbDescriptor.idVendor == manuID && 
  77.        usbDescriptor.idProduct == prodID ) {
  78.         closedir(dir); closedir(dirB);
  79.     return fd;
  80.       } else {
  81.     close(fd);
  82.       }      
  83.  
  84.     }
  85.   }
  86.  
  87.   closedir(dir); closedir(dirB);
  88.   return -1;
  89. }  
  90.       
  91.  
  92.  
  93. int close_linux_usb_dev( int fd ) {
  94.   return close( fd );
  95. }
  96.  
  97. int bulk_linux_usb_dev( int fd, int pipeNum, unsigned char *buffer, 
  98.             int *length, int timeout ) {
  99.   
  100.   struct usb_bulktransfer bulk;
  101.   int    ret, retrieved = 0;
  102.  
  103.   bulk.ep      = pipeNum;
  104.   bulk.len     = *length;
  105.   bulk.timeout = timeout;
  106.   bulk.data    = buffer;
  107.  
  108.   ret = ioctl( fd, IOCTL_USB_BULK, &bulk );
  109.  
  110.   if ( ret < 0 ) {
  111.     *length = 0;
  112.     return -1;
  113.   }
  114.  
  115.   *length = ret;
  116.  
  117.   return ret;  
  118. }
  119.