File: usbdev_libusb1.c

package info (click to toggle)
pcsc-cyberjack 3.99.5final.sp03-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,992 kB
  • sloc: cpp: 21,766; sh: 10,196; ansic: 5,486; xml: 1,681; objc: 680; makefile: 440
file content (141 lines) | stat: -rw-r--r-- 3,275 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifdef USE_USB1

#include <libusb.h>

static struct libusb_context *ausb_libusb1_context=NULL;

int rsct_usbdev_init(void){
  if (ausb_libusb1_context==NULL) {
    int rv;
    rv=libusb_init(&ausb_libusb1_context);
    if (rv) {
      fprintf(stderr, "RSCT: Error on libusb_init(): %d\n", rv);
      ausb_libusb1_context=NULL;
      return -1;
    }
  }
  return 0;
}

void rsct_usbdev_fini(void) {
  libusb_exit(ausb_libusb1_context);
  ausb_libusb1_context=NULL;
}


int rsct_usbdev_scan(rsct_usbdev_t **usbdev_list) {
  if (rsct_usbdev_init())
    return -1;
  else {
    libusb_device **list;
    size_t cnt=libusb_get_device_list(ausb_libusb1_context, &list);
    size_t i;
  
    for (i=0; i<cnt; i++) {
      libusb_device *dev;
      struct libusb_device_descriptor descr;
      int rv;
  
      dev=list[i];
      rv=libusb_get_device_descriptor(dev, &descr);
      if (rv==0) {
	if (descr.idVendor==0xc4b) {
	  rsct_usbdev_t *d;
	  char pbuff[256];
	  struct stat st;
	  int havePath=0;
  
	  d=rsct_usbdev_new();
	  d->busId=libusb_get_bus_number(dev);
	  d->busPos=libusb_get_device_address(dev);
	  d->vendorId=descr.idVendor;
	  d->productId=descr.idProduct;
  
	  /* determine path for LibUSB */
	  snprintf(pbuff, sizeof(pbuff)-1,
		   "/dev/bus/usb/%03d/%03d",
		   d->busId, d->busPos);
	  pbuff[sizeof(pbuff)-1]=0;
	  if (stat(pbuff, &st)==0) {
	    havePath=1;
	  }
	  else {
	    snprintf(pbuff, sizeof(pbuff)-1,
		     "/proc/bus/usb/%03d/%03d",
		     d->busId, d->busPos);
	    pbuff[sizeof(pbuff)-1]=0;
	    if (stat(pbuff, &st)==0) {
	      havePath=1;
	    }
	  }
  
	  if (havePath) {
	    strncpy(d->usbPath, pbuff, sizeof(d->usbPath)-1);
	    d->usbPath[sizeof(d->usbPath)-1]=0;
  
	    strncpy(d->deviceNodePath, pbuff, sizeof(d->deviceNodePath)-1);
	    d->deviceNodePath[sizeof(d->deviceNodePath)-1]=0;
	  }
  
	  /* generate path for CTAPI/IFD */
	  snprintf(d->path, sizeof(d->path)-1,
		   "usb:%04x/%04x:libusb:%03d:%03d",
		   d->vendorId,
		   d->productId,
		   d->busId,
		   d->busPos);
  
	  if (1) {
	    libusb_device_handle *dh;
  
	    rv=libusb_open(dev, &dh);
	    if (rv) {
	      fprintf(stderr, "RSCT: Error on libusb_open: %d\n", rv);
	    }
	    else {
	      /* get product string */
	      rv=libusb_get_string_descriptor_ascii(dh, descr.iProduct,
						    (unsigned char*) (d->productName),
						    sizeof(d->productName)-1);
	      if (rv<0) {
		fprintf(stderr, "RSCT: Error on libusb_get_string_descriptor_ascii: %d\n", rv);
		d->productName[0]=0;
	      }
	      else {
		d->productName[rv]=0;
	      }
  
	      if (descr.idProduct>=0x300) {
		/* get serial number for newer devices */
		rv=libusb_get_string_descriptor_ascii(dh, descr.iSerialNumber,
						      (unsigned char*) (d->serial),
						      sizeof(d->serial)-1);
		if (rv<0) {
		  fprintf(stderr, "RSCT: Error on libusb_get_string_descriptor_ascii: %d\n", rv);
		  d->serial[0]=0;
		}
		else {
		  d->serial[rv]=0;
		}
	      }
  
	      libusb_close(dh);
	    }
	  }
  
	  /* all set, add device */
	  rsct_usbdev_list_add(usbdev_list, d);
	}
      }
      else {
	fprintf(stderr, "RSCT: Error on libusb_get_device_descriptor: %d\n", rv);
      }
    }
  
    libusb_free_device_list(list, 1);
    rsct_usbdev_fini();
    return 0;
  }
}

#endif