]> sigrok.org Git - libsigrok.git/blob - hardware/common/usb.c
hardware: Call libusb_error_name() in all USB-related error messages
[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                 /* 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: %s.",
127                                libusb_error_name(ret));
128                         continue;
129                 }
130
131                 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
132                         continue;
133
134                 b = libusb_get_bus_number(devlist[i]);
135                 a = libusb_get_device_address(devlist[i]);
136                 if (bus + addr && (b != bus || a != addr))
137                         continue;
138
139                 sr_dbg("Found USB device (VID:PID = %04x:%04x, bus.address = "
140                        "%d.%d).", des.idVendor, des.idProduct, b, a);
141
142                 usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
143                                 libusb_get_device_address(devlist[i]), NULL);
144                 devices = g_slist_append(devices, usb);
145         }
146         libusb_free_device_list(devlist, 1);
147
148         sr_dbg("Found %d device(s).", g_slist_length(devices));
149         
150         return devices;
151 }
152
153 SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb)
154 {
155         struct libusb_device **devlist;
156         struct libusb_device_descriptor des;
157         int ret, r, cnt, i, a, b;
158
159         sr_dbg("Trying to open USB device.");
160
161         if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) {
162                 sr_err("Failed to retrieve device list: %s.",
163                        libusb_error_name(cnt));
164                 return SR_ERR;
165         }
166
167         ret = SR_ERR;
168         for (i = 0; i < cnt; i++) {
169                 if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
170                         sr_err("Failed to get device descriptor: %s.",
171                                libusb_error_name(r));
172                         continue;
173                 }
174
175                 b = libusb_get_bus_number(devlist[i]);
176                 a = libusb_get_device_address(devlist[i]);
177
178                 if (b != usb->bus || a != usb->address) {
179                         sr_spew("bus.address = %d.%d does not match.", b, a);
180                         continue;
181                 }
182
183                 if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) {
184                         sr_err("Failed to open device: %s.",
185                                libusb_error_name(r));
186                         break;
187                 }
188
189                 sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = "
190                        "%d.%d).", des.idVendor, des.idProduct, b, a);
191
192                 ret = SR_OK;
193                 break;
194         }
195
196         libusb_free_device_list(devlist, 1);
197
198         return ret;
199 }