3 * Copyright 2003,2004,2006,2009 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio 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 3, or (at your option)
12 * GNU Radio 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.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include "usrp_primsi.h"
28 #include "usrp_commands.h"
29 #include <libusb-1.0/libusb.h>
43 using namespace ad9862;
45 static const int LIBUSB1_DEBUG = 0;
48 * libusb 0.12 / 1.0 compatibility
52 _get_usb_error_str (int usb_err)
56 return "Success (no error)";
58 return "Input/output error";
59 case LIBUSB_ERROR_INVALID_PARAM:
60 return "Invalid parameter";
61 case LIBUSB_ERROR_ACCESS:
62 return "Access denied (insufficient permissions";
63 case LIBUSB_ERROR_NO_DEVICE:
64 return "No such device (it may have been disconnected)";
65 case LIBUSB_ERROR_NOT_FOUND:
66 return "Entity not found";
67 case LIBUSB_ERROR_BUSY:
68 return "Resource busy";
69 case LIBUSB_ERROR_TIMEOUT:
70 return "Operation timed out";
71 case LIBUSB_ERROR_OVERFLOW:
73 case LIBUSB_ERROR_PIPE:
75 case LIBUSB_ERROR_INTERRUPTED:
76 return "System call interrupted (perhaps due to signal)";
77 case LIBUSB_ERROR_NO_MEM:
78 return "Insufficient memory";
79 case LIBUSB_ERROR_NOT_SUPPORTED:
80 return "Operation not supported or unimplemented on this platform";
81 case LIBUSB_ERROR_OTHER:
82 return "Unknown error";
85 return "Unknown error";
88 struct libusb_device *
89 _get_usb_device (struct libusb_device_handle *udh)
91 return libusb_get_device (udh);
94 struct libusb_device_descriptor
95 _get_usb_device_descriptor(struct libusb_device *q)
98 struct libusb_device_descriptor desc;
100 ret = libusb_get_device_descriptor(q, &desc);
103 fprintf (stderr, "usrp: libusb_get_device_descriptor failed: %s\n",
104 _get_usb_error_str(ret));
110 _get_usb_string_descriptor (struct libusb_device_handle *udh, int index,
111 unsigned char* data, int length)
114 ret = libusb_get_string_descriptor_ascii (udh, (uint8_t) index, data, length);
117 fprintf (stderr, "usrp: libusb_get_string_descriptor_ascii failed: %s\n",
118 _get_usb_error_str(ret));
124 _usb_control_transfer (struct libusb_device_handle *udh, int request_type,
125 int request, int value, int index,
126 unsigned char *data, int length, unsigned int timeout)
129 ret = libusb_control_transfer (udh, request_type, request, value, index,
130 data, length, timeout);
132 fprintf (stderr, "usrp: libusb_control_transfer failed: %s\n",
133 _get_usb_error_str(ret));
138 // ----------------------------------------------------------------
142 usrp_one_time_init (libusb_context **ctx)
146 if ((ret = libusb_init (ctx)) < 0)
147 fprintf (stderr, "usrp: libusb_init failed: %s\n", _get_usb_error_str(ret));
149 // Enable debug verbosity if requested. This will only work if the debug
150 // option is compiled into libusb and may produce a generous amount of output
151 // on stdout. If debug output is not compiled into libusb, this call does
154 libusb_set_debug(*ctx, 3);
164 struct libusb_device *
165 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
167 libusb_device **list;
169 struct libusb_device *q;
172 // Make sure not operating on default context. There are cases where operating
173 // with a single global (NULL) context may be preferable, so this check can be
174 // skipped if you know what you're doing.
175 assert (ctx != NULL);
177 size_t cnt = libusb_get_device_list(ctx, &list);
181 fprintf(stderr, "usrp: libusb_get_device_list failed: %s\n",
182 _get_usb_error_str(cnt));
184 for (i = 0; i < cnt; i++) {
186 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
187 if (n_found == nth) // return this one
189 n_found++; // keep looking
193 // The list needs to be freed. Right now just release it if nothing is found.
194 libusb_free_device_list(list, 1);
196 return 0; // not found
199 struct libusb_device_handle *
200 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
202 libusb_device_handle *udh;
205 if (libusb_open (dev, &udh) < 0)
208 if (dev != libusb_get_device (udh)){
209 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
213 if ((ret = libusb_claim_interface (udh, interface)) < 0) {
214 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n",
215 __FUNCTION__, interface);
216 fprintf (stderr, "%s\n", _get_usb_error_str(ret));
221 if ((ret = libusb_set_interface_alt_setting (udh, interface,
222 altinterface)) < 0) {
223 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
224 fprintf (stderr, "%s\n", _get_usb_error_str(ret));
225 libusb_release_interface (udh, interface);
234 usrp_close_interface (libusb_device_handle *udh)
242 // ----------------------------------------------------------------
243 // write vendor extension command to USRP
247 write_cmd (struct libusb_device_handle *udh,
248 int request, int value, int index,
249 unsigned char *bytes, int len)
251 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
253 int ret = libusb_control_transfer(udh, requesttype, request, value, index,
257 // we get EPIPE if the firmware stalls the endpoint.
258 if (ret != LIBUSB_ERROR_PIPE) {
259 fprintf (stderr, "usrp: libusb_control_transfer failed: %s\n",
260 _get_usb_error_str(ret));
261 fprintf (stderr, "usrp: write_cmd failed\n");