]> sigrok.org Git - libsigrok.git/blob - hardware/common/misc.c
43b158ae742c32356aff8b3d80b6ecb1adb9e46d
[libsigrok.git] / hardware / common / misc.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21 #include <stdint.h>
22 #include <glib.h>
23 #ifdef HAVE_LIBUSB_1_0
24 #include <libusb.h>
25 #endif
26 #include <sigrok.h>
27
28 #ifdef HAVE_LIBUSB_1_0
29
30 int opendev2(int device_index, struct sr_device_instance **sdi,
31              libusb_device *dev, struct libusb_device_descriptor *des,
32              int *skip, uint16_t vid, uint16_t pid, int interface)
33 {
34         int err;
35
36         if ((err = libusb_get_device_descriptor(dev, des))) {
37                 g_warning("failed to get device descriptor: %d", err);
38                 return -1;
39         }
40
41         if (des->idVendor != vid || des->idProduct != pid)
42                 return 0;
43
44         if (*skip != device_index) {
45                 /* Skip devices of this type that aren't the one we want. */
46                 *skip += 1;
47                 return 0;
48         }
49
50         /*
51          * Should check the bus here, since we know that already. But what are
52          * we going to do if it doesn't match after the right number of skips?
53          */
54         if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
55                 (*sdi)->usb->address = libusb_get_device_address(dev);
56                 (*sdi)->status = SR_ST_ACTIVE;
57                 g_message("opened device %d on %d.%d interface %d",
58                           (*sdi)->index, (*sdi)->usb->bus,
59                           (*sdi)->usb->address, interface);
60         } else {
61                 g_warning("failed to open device: %d", err);
62                 *sdi = NULL;
63         }
64
65         return 0;
66 }
67
68 int opendev3(struct sr_device_instance **sdi, libusb_device *dev,
69              struct libusb_device_descriptor *des,
70              uint16_t vid, uint16_t pid, int interface)
71 {
72         int err;
73
74         if ((err = libusb_get_device_descriptor(dev, des))) {
75                 g_warning("failed to get device descriptor: %d", err);
76                 return -1;
77         }
78
79         if (des->idVendor != vid || des->idProduct != pid)
80                 return 0;
81
82         if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
83             && libusb_get_device_address(dev) == (*sdi)->usb->address) {
84                 /* Found it. */
85                 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
86                         (*sdi)->status = SR_ST_ACTIVE;
87                         g_message("opened device %d on %d.%d interface %d",
88                                   (*sdi)->index, (*sdi)->usb->bus,
89                                   (*sdi)->usb->address, interface);
90                 } else {
91                         g_warning("failed to open device: %d", err);
92                         *sdi = NULL;
93                 }
94         }
95
96         return 0;
97 }
98
99 #endif