]> sigrok.org Git - libsigrok.git/blob - hardware/common/usb.c
usb: strip overly verbose debugging
[libsigrok.git] / hardware / common / usb.c
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
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  */
52 SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn)
53 {
54         struct sr_usb_dev_inst *usb;
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                 if ((mstr = g_match_info_fetch(match, 1)))
67                         vid = strtoul(mstr, NULL, 16);
68                 g_free(mstr);
69
70                 if ((mstr = g_match_info_fetch(match, 2)))
71                         pid = strtoul(mstr, NULL, 16);
72                 g_free(mstr);
73                 sr_dbg("Trying to find USB device with VID:PID = %04x:%04x.",
74                        vid, pid);
75         } else {
76                 g_match_info_unref(match);
77                 g_regex_unref(reg);
78                 reg = g_regex_new(CONN_USB_BUSADDR, 0, 0, NULL);
79                 if (g_regex_match(reg, conn, 0, &match)) {
80                         if ((mstr = g_match_info_fetch(match, 1)))
81                                 bus = strtoul(mstr, NULL, 16);
82                         g_free(mstr);
83
84                         if ((mstr = g_match_info_fetch(match, 2)))
85                                 addr = strtoul(mstr, NULL, 16);
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 specified.");
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: %s.",
115                                libusb_error_name(ret));
116                         continue;
117                 }
118
119                 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
120                         continue;
121
122                 b = libusb_get_bus_number(devlist[i]);
123                 a = libusb_get_device_address(devlist[i]);
124                 if (bus + addr && (b != bus || a != addr))
125                         continue;
126
127                 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
128                        "%d.%d).", des.idVendor, des.idProduct, b, a);
129
130                 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
131                                 libusb_get_device_address(devlist[i]), NULL);
132                 devices = g_slist_append(devices, usb);
133         }
134         libusb_free_device_list(devlist, 1);
135
136         sr_dbg("Found %d device(s).", g_slist_length(devices));
137         
138         return devices;
139 }
140
141 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
142 {
143         struct libusb_device **devlist;
144         struct libusb_device_descriptor des;
145         int ret, r, cnt, i, a, b;
146
147         sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address);
148
149         if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
150                 sr_err("Failed to retrieve device list: %s.",
151                        libusb_error_name(cnt));
152                 return SR_ERR;
153         }
154
155         ret = SR_ERR;
156         for (i = 0; i < cnt; i++) {
157                 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
158                         sr_err("Failed to get device descriptor: %s.",
159                                libusb_error_name(r));
160                         continue;
161                 }
162
163                 b = libusb_get_bus_number(devlist[i]);
164                 a = libusb_get_device_address(devlist[i]);
165                 if (b != usb->bus || a != usb->address)
166                         continue;
167
168                 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
169                         sr_err("Failed to open device: %s.",
170                                libusb_error_name(r));
171                         break;
172                 }
173
174                 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
175                        "%d.%d).", des.idVendor, des.idProduct, b, a);
176
177                 ret = SR_OK;
178                 break;
179         }
180
181         libusb_free_device_list(devlist, 1);
182
183         return ret;
184 }