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