]> sigrok.org Git - libsigrok.git/blame - hardware/common/usb.c
lascar-el-usb: add scanning functionality
[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
7ae6a758
BV
41/**
42 * Find USB devices according to a connection string.
43 *
44 * @param usb_ctx libusb context to use while scanning.
45 * @param conn Connection string specifying the device(s) to match. This
46 * can be of the form "<bus>.<address>", or "<vendorid>.<productid>".
47 *
48 * @return A GSList of struct sr_usb_dev_inst, with bus and address fields
49 * matching the device that matched the connection string. The GSList and
50 * its contents must be freed by the caller.
51 */
52SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
0c632d36 53{
7ae6a758 54 struct sr_usb_dev_inst *usb;
0c632d36
UH
55 struct libusb_device **devlist;
56 struct libusb_device_descriptor des;
57 GSList *devices;
58 GRegex *reg;
59 GMatchInfo *match;
60 int vid, pid, bus, addr, b, a, ret, i;
61 char *mstr;
62
63 vid = pid = bus = addr = 0;
64 reg = g_regex_new(CONN_USB_VIDPID, 0, 0, NULL);
65 if (g_regex_match(reg, conn, 0, &match)) {
66 /* Extract VID. */
67 if ((mstr = g_match_info_fetch(match, 1))) {
68 vid = strtoul(mstr, NULL, 16);
69 sr_spew("Extracted VID 0x%04x.", vid);
70 }
71 g_free(mstr);
72
73 /* Extract PID. */
74 if ((mstr = g_match_info_fetch(match, 2))) {
75 pid = strtoul(mstr, NULL, 16);
76 sr_spew("Extracted PID 0x%04x.", pid);
77 }
78 g_free(mstr);
79 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
80 vid, pid);
81 } else {
82 g_match_info_unref(match);
83 g_regex_unref(reg);
84 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
85 if (g_regex_match(reg, conn, 0, &match)) {
86 /* Extract bus. */
87 if ((mstr = g_match_info_fetch(match, 1))) {
88 bus = strtoul(mstr, NULL, 16);
89 sr_spew("Extracted bus %d.", bus);
90 }
91 g_free(mstr);
92
93 /* Extract address. */
94 if ((mstr = g_match_info_fetch(match, 2))) {
95 addr = strtoul(mstr, NULL, 16);
96 sr_spew("Extracted address %d.", addr);
97 }
98 g_free(mstr);
99 sr_dbg("Trying to find USB device with bus.address = "
100 "%d.%d.", bus, addr);
101 }
102 }
103 g_match_info_unref(match);
104 g_regex_unref(reg);
105
106 if (vid + pid + bus + addr == 0) {
107 sr_err("Neither VID:PID nor bus.address was found.");
108 return NULL;
109 }
110
111 if (bus > 64) {
112 sr_err("Invalid bus specified: %d.", bus);
113 return NULL;
114 }
115
116 if (addr > 127) {
117 sr_err("Invalid address specified: %d.", addr);
118 return NULL;
119 }
120
121 /* Looks like a valid USB device specification, but is it connected? */
122 devices = NULL;
123 libusb_get_device_list(usb_ctx, &devlist);
124 for (i = 0; devlist[i]; i++) {
125 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
126 sr_err("Failed to get device descriptor: %d.", ret);
127 continue;
128 }
129
7ae6a758 130 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
0c632d36 131 continue;
0c632d36 132
7ae6a758
BV
133 b = libusb_get_bus_number(devlist[i]);
134 a = libusb_get_device_address(devlist[i]);
135 if (bus + addr && (b != bus || a != addr))
0c632d36 136 continue;
0c632d36
UH
137
138 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
139 "%d.%d).", des.idVendor, des.idProduct, b, a);
140
7ae6a758
BV
141 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
142 libusb_get_device_address(devlist[i]), NULL);
143 devices = g_slist_append(devices, usb);
0c632d36
UH
144 }
145 libusb_free_device_list(devlist, 1);
146
147 sr_dbg("Found %d device(s).", g_slist_length(devices));
148
149 return devices;
150}
151
152SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
153{
154 struct libusb_device **devlist;
155 struct libusb_device_descriptor des;
156 int ret, r, cnt, i, a, b;
157
158 sr_dbg("Trying to open USB device.");
159
160 if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
161 sr_err("Failed to retrieve device list: %s.",
162 libusb_error_name(cnt));
163 return SR_ERR;
164 }
165
166 ret = SR_ERR;
167 for (i = 0; i < cnt; i++) {
168 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
169 sr_err("Failed to get device descriptor: %s.",
170 libusb_error_name(r));
171 continue;
172 }
173
174 b = libusb_get_bus_number(devlist[i]);
175 a = libusb_get_device_address(devlist[i]);
176
177 if (b != usb->bus || a != usb->address) {
178 sr_spew("bus.address = %d.%d does not match.", b, a);
179 continue;
180 }
181
182 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
183 sr_err("Failed to open device: %s.",
184 libusb_error_name(r));
185 break;
186 }
187
188 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
189 "%d.%d).", des.idVendor, des.idProduct, b, a);
190
191 ret = SR_OK;
192 break;
193 }
194
195 libusb_free_device_list(devlist, 1);
196
197 return ret;
198}