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));
139 // ----------------------------------------------------------------
143 usrp_one_time_init (libusb_context **ctx)
147 if ((ret = libusb_init (ctx)) < 0)
148 fprintf (stderr, "usrp: libusb_init failed: %s\n", _get_usb_error_str(ret));
150 // Enable debug verbosity if requested. This will only work if the debug
151 // option is compiled into libusb and may produce a generous amount of output
152 // on stdout. If debug output is not compiled into libusb, this call does
155 libusb_set_debug(*ctx, 3);
159 usrp_deinit (struct libusb_context *ctx)
161 // Each object _should_ be running in its own context. If running in default
162 // context then leave the instance open as it may be shared.
173 struct libusb_device *
174 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
176 libusb_device **list;
178 struct libusb_device *q;
181 // Make sure not operating on default context. There are cases where operating
182 // with a single global (NULL) context may be preferable, so this check can be
183 // skipped if you know what you're doing.
184 assert (ctx != NULL);
186 size_t cnt = libusb_get_device_list(ctx, &list);
190 fprintf(stderr, "usrp: libusb_get_device_list failed: %s\n",
191 _get_usb_error_str(cnt));
193 for (i = 0; i < cnt; i++) {
195 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
196 if (n_found == nth) // return this one
198 n_found++; // keep looking
202 // The list needs to be freed. Right now just release it if nothing is found.
203 libusb_free_device_list(list, 1);
205 return 0; // not found
208 struct libusb_device_handle *
209 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
211 libusb_device_handle *udh;
214 if (libusb_open (dev, &udh) < 0)
217 if (dev != libusb_get_device (udh)){
218 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
222 if ((ret = libusb_claim_interface (udh, interface)) < 0) {
223 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n",
224 __FUNCTION__, interface);
225 fprintf (stderr, "%s\n", _get_usb_error_str(ret));
230 if ((ret = libusb_set_interface_alt_setting (udh, interface,
231 altinterface)) < 0) {
232 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
233 fprintf (stderr, "%s\n", _get_usb_error_str(ret));
234 libusb_release_interface (udh, interface);
243 usrp_close_interface (libusb_device_handle *udh)
251 // ----------------------------------------------------------------
252 // write vendor extension command to USRP
256 write_cmd (struct libusb_device_handle *udh,
257 int request, int value, int index,
258 unsigned char *bytes, int len)
260 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
262 int ret = libusb_control_transfer(udh, requesttype, request, value, index,
266 // we get EPIPE if the firmware stalls the endpoint.
267 if (ret != LIBUSB_ERROR_PIPE) {
268 fprintf (stderr, "usrp: libusb_control_transfer failed: %s\n",
269 _get_usb_error_str(ret));
270 fprintf (stderr, "usrp: write_cmd failed\n");