]> sigrok.org Git - libsigrok.git/blob - hardware/common/misc.c
SR_ prefix for all public enums.
[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 <stdint.h>
21 #include <glib.h>
22 #include <libusb.h>
23 #include <sigrok.h>
24
25 int opendev2(int device_index, struct sr_device_instance **sdi,
26              libusb_device *dev, struct libusb_device_descriptor *des,
27              int *skip, uint16_t vid, uint16_t pid, int interface)
28 {
29         int err;
30
31         if ((err = libusb_get_device_descriptor(dev, des))) {
32                 g_warning("failed to get device descriptor: %d", err);
33                 return -1;
34         }
35
36         if (des->idVendor != vid || des->idProduct != pid)
37                 return 0;
38
39         if (*skip != device_index) {
40                 /* Skip devices of this type that aren't the one we want. */
41                 *skip += 1;
42                 return 0;
43         }
44
45         /*
46          * Should check the bus here, since we know that already. But what are
47          * we going to do if it doesn't match after the right number of skips?
48          */
49         if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
50                 (*sdi)->usb->address = libusb_get_device_address(dev);
51                 (*sdi)->status = SR_ST_ACTIVE;
52                 g_message("opened device %d on %d.%d interface %d",
53                           (*sdi)->index, (*sdi)->usb->bus,
54                           (*sdi)->usb->address, interface);
55         } else {
56                 g_warning("failed to open device: %d", err);
57                 *sdi = NULL;
58         }
59
60         return 0;
61 }
62
63 int opendev3(struct sr_device_instance **sdi, libusb_device *dev,
64              struct libusb_device_descriptor *des,
65              uint16_t vid, uint16_t pid, int interface)
66 {
67         int err;
68
69         if ((err = libusb_get_device_descriptor(dev, des))) {
70                 g_warning("failed to get device descriptor: %d", err);
71                 return -1;
72         }
73
74         if (des->idVendor != vid || des->idProduct != pid)
75                 return 0;
76
77         if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
78             && libusb_get_device_address(dev) == (*sdi)->usb->address) {
79                 /* Found it. */
80                 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
81                         (*sdi)->status = SR_ST_ACTIVE;
82                         g_message("opened device %d on %d.%d interface %d",
83                                   (*sdi)->index, (*sdi)->usb->bus,
84                                   (*sdi)->usb->address, interface);
85                 } else {
86                         g_warning("failed to open device: %d", err);
87                         *sdi = NULL;
88                 }
89         }
90
91         return 0;
92 }