2 * libusb example program to list devices on the bus
3 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <sys/types.h>
25 #include "../globals.h"
26 #include "../csctapi/ifd_smartreader_types.h"
28 #if defined(__FreeBSD__)
31 #include <libusb-1.0/libusb.h>
34 static void smartreader_check_endpoint(libusb_device
*usb_dev
, libusb_device_handle
*handle
)
36 struct libusb_device_descriptor usbdesc
;
37 struct libusb_config_descriptor
*configDesc
;
41 uint8_t tmpEndpointAddress
;
42 int32_t nb_endpoint_ok
= 0;
44 unsigned char iserialbuffer
[128], iproductbuffer
[128];
45 char *productptr
= (char *)iproductbuffer
;
46 static const char *const typename_str
[6] = {"SR", "Infinity", "SRv2", "TripleP1", "TripleP2", "TripleP3"};
48 ret
= libusb_get_device_descriptor(usb_dev
, &usbdesc
);
51 printf("Smartreader : couldn't read device descriptor, assuming this is not a smartreader");
54 if(usbdesc
.bNumConfigurations
)
56 ret
= libusb_get_active_config_descriptor(usb_dev
, &configDesc
);
59 printf("Smartreader : couldn't read config descriptor , assuming this is not a smartreader");
62 for(m
= 0; m
< sizeof(reader_types
) / sizeof(struct s_reader_types
); ++m
)
65 for(j
= 0; j
< (configDesc
->bNumInterfaces
); j
++)
67 for(k
= 0; k
< configDesc
->interface
[j
].num_altsetting
; k
++)
69 for(l
= 0; l
< configDesc
->interface
[j
].altsetting
[k
].bNumEndpoints
; l
++)
71 tmpEndpointAddress
= configDesc
->interface
[j
].altsetting
[k
].endpoint
[l
].bEndpointAddress
;
72 if((tmpEndpointAddress
== reader_types
[m
].in_ep
|| tmpEndpointAddress
== reader_types
[m
].out_ep
))
80 if(nb_endpoint_ok
== 2)
82 busid
= libusb_get_bus_number(usb_dev
);
83 devid
= libusb_get_device_address(usb_dev
);
84 memset(iserialbuffer
, 0, sizeof(iserialbuffer
));
85 memset(iproductbuffer
, 0, sizeof(iproductbuffer
));
86 libusb_get_string_descriptor_ascii(handle
, usbdesc
.iSerialNumber
, iserialbuffer
, sizeof(iserialbuffer
));
87 libusb_get_string_descriptor_ascii(handle
, usbdesc
.iProduct
, iproductbuffer
, sizeof(iproductbuffer
));
88 if ((!((!strcasecmp(productptr
, "Triple Reader+")) && (m
== 2))) && (!((!strcasecmp(productptr
, "Smartreader2 plus")) && (m
== 3)))) {
89 printf("bus %03d, device %03d : %04x:%04x %s (type=%s, in_ep=%02x, out_ep=%02x; insert in oscam.server 'device = %s%sSerial:%s')\n",
91 usbdesc
.idVendor
, usbdesc
.idProduct
, strlen(productptr
) > 0 ? productptr
: "Smartreader",
92 typename_str
[reader_types
[m
].rdrtypename
], reader_types
[m
].in_ep
, reader_types
[m
].out_ep
,
93 reader_types
[m
].rdrtypename
== 0 ? "" : typename_str
[reader_types
[m
].rdrtypename
] , reader_types
[m
].rdrtypename
== 0 ? "" : ";", iserialbuffer
100 static void print_devs(libusb_device
**devs
)
103 libusb_device_handle
*handle
;
107 while((dev
= devs
[i
++]) != NULL
)
109 struct libusb_device_descriptor usbdesc
;
110 int32_t r
= libusb_get_device_descriptor(dev
, &usbdesc
);
113 fprintf(stderr
, "failed to get device descriptor");
116 if(usbdesc
.idVendor
== 0x0403 && (usbdesc
.idProduct
== 0x6001 || usbdesc
.idProduct
== 0x6011))
118 ret
= libusb_open(dev
, &handle
);
121 printf("couldn't open device %03d:%03d\n", libusb_get_bus_number(dev
), libusb_get_device_address(dev
));
124 // check for smargo endpoints.
125 smartreader_check_endpoint(dev
, handle
);
127 libusb_close(handle
);
134 libusb_device
**devs
;
138 r
= libusb_init(NULL
);
142 printf("Looking for smartreader compatible devices...\n");
144 cnt
= libusb_get_device_list(NULL
, &devs
);
146 { return (int32_t) cnt
; }
149 libusb_free_device_list(devs
, 1);