]> sigrok.org Git - libsigrok.git/blame - hardware/common/usb.c
add VID:PID for generic SILabs F32x USBXpress chips
[libsigrok.git] / hardware / common / usb.c
CommitLineData
0c632d36
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdlib.h>
23#include <glib.h>
24#include <libusb.h>
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27
28/* SR_HWCAP_CONN takes one of these: */
29#define CONN_USB_VIDPID "^([0-9a-z]{1,4})\\.([0-9a-z]{1,4})$"
30#define CONN_USB_BUSADDR "^(\\d+)\\.(\\d+)$"
31
32/* Message logging helpers with driver-specific prefix string. */
33#define DRIVER_LOG_DOMAIN "usb: "
34#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
35#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
36#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
37#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
38#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
39#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
40
41SR_PRIV GSList *sr_usb_connect(libusb_context *usb_ctx, const char *conn)
42{
43 struct libusb_device **devlist;
44 struct libusb_device_descriptor des;
45 GSList *devices;
46 GRegex *reg;
47 GMatchInfo *match;
48 int vid, pid, bus, addr, b, a, ret, i;
49 char *mstr;
50
51 vid = pid = bus = addr = 0;
52 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
53 if (g_regex_match(reg, conn, 0, &match)) {
54 /* Extract VID. */
55 if ((mstr = g_match_info_fetch(match, 1))) {
56 vid = strtoul(mstr, NULL, 16);
57 sr_spew("Extracted VID 0x%04x.", vid);
58 }
59 g_free(mstr);
60
61 /* Extract PID. */
62 if ((mstr = g_match_info_fetch(match, 2))) {
63 pid = strtoul(mstr, NULL, 16);
64 sr_spew("Extracted PID 0x%04x.", pid);
65 }
66 g_free(mstr);
67 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
68 vid, pid);
69 } else {
70 g_match_info_unref(match);
71 g_regex_unref(reg);
72 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
73 if (g_regex_match(reg, conn, 0, &match)) {
74 /* Extract bus. */
75 if ((mstr = g_match_info_fetch(match, 1))) {
76 bus = strtoul(mstr, NULL, 16);
77 sr_spew("Extracted bus %d.", bus);
78 }
79 g_free(mstr);
80
81 /* Extract address. */
82 if ((mstr = g_match_info_fetch(match, 2))) {
83 addr = strtoul(mstr, NULL, 16);
84 sr_spew("Extracted address %d.", addr);
85 }
86 g_free(mstr);
87 sr_dbg("Trying to find USB device with bus.address = "
88 "%d.%d.", bus, addr);
89 }
90 }
91 g_match_info_unref(match);
92 g_regex_unref(reg);
93
94 if (vid + pid + bus + addr == 0) {
95 sr_err("Neither VID:PID nor bus.address was found.");
96 return NULL;
97 }
98
99 if (bus > 64) {
100 sr_err("Invalid bus specified: %d.", bus);
101 return NULL;
102 }
103
104 if (addr > 127) {
105 sr_err("Invalid address specified: %d.", addr);
106 return NULL;
107 }
108
109 /* Looks like a valid USB device specification, but is it connected? */
110 devices = NULL;
111 libusb_get_device_list(usb_ctx, &devlist);
112 for (i = 0; devlist[i]; i++) {
113 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
114 sr_err("Failed to get device descriptor: %d.", ret);
115 continue;
116 }
117
118 b = libusb_get_bus_number(devlist[i]);
119 a = libusb_get_device_address(devlist[i]);
120
121 if (vid + pid &&
122 (des.idVendor != vid || des.idProduct != pid)) {
123 sr_spew("VID:PID = %04x:%04x (%d.%d) doesn't match.",
124 des.idVendor, des.idProduct, b, a);
125 continue;
126 }
127
128 if (bus + addr && (b != bus || a != addr)) {
129 sr_spew("bus.address = %d.%d does not match.", b, a);
130 continue;
131 }
132
133 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
134 "%d.%d).", des.idVendor, des.idProduct, b, a);
135
136 devices = g_slist_append(devices, devlist[i]);
137 }
138 libusb_free_device_list(devlist, 1);
139
140 sr_dbg("Found %d device(s).", g_slist_length(devices));
141
142 return devices;
143}
144
145SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
146{
147 struct libusb_device **devlist;
148 struct libusb_device_descriptor des;
149 int ret, r, cnt, i, a, b;
150
151 sr_dbg("Trying to open USB device.");
152
153 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
154 sr_err("Failed to retrieve device list: %s.",
155 libusb_error_name(cnt));
156 return SR_ERR;
157 }
158
159 ret = SR_ERR;
160 for (i = 0; i < cnt; i++) {
161 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
162 sr_err("Failed to get device descriptor: %s.",
163 libusb_error_name(r));
164 continue;
165 }
166
167 b = libusb_get_bus_number(devlist[i]);
168 a = libusb_get_device_address(devlist[i]);
169
170 if (b != usb->bus || a != usb->address) {
171 sr_spew("bus.address = %d.%d does not match.", b, a);
172 continue;
173 }
174
175 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
176 sr_err("Failed to open device: %s.",
177 libusb_error_name(r));
178 break;
179 }
180
181 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
182 "%d.%d).", des.idVendor, des.idProduct, b, a);
183
184 ret = SR_OK;
185 break;
186 }
187
188 libusb_free_device_list(devlist, 1);
189
190 return ret;
191}