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;
46 * libusb 0.12 / 1.0 compatibility
49 struct libusb_device *
50 _get_usb_device (struct libusb_device_handle *udh)
52 return libusb_get_device (udh);
55 struct libusb_device_descriptor
56 _get_usb_device_descriptor(struct libusb_device *q)
59 struct libusb_device_descriptor desc;
61 if ((ret = libusb_get_device_descriptor(q, &desc)) < 0)
62 fprintf (stderr, "usrp: libusb_get_device_descriptor failed %d\n", ret);
68 _get_usb_string_descriptor (struct libusb_device_handle *udh, int index,
69 unsigned char* data, int length)
71 return libusb_get_string_descriptor_ascii (udh, (uint8_t) index, data, length);
75 _usb_control_transfer (struct libusb_device_handle *udh, int request_type,
76 int request, int value, int index,
77 unsigned char *data, int length, unsigned int timeout)
79 return libusb_control_transfer (udh, request_type, request, value, index,
80 data, length, timeout);
84 // ----------------------------------------------------------------
88 usrp_one_time_init (libusb_context **ctx)
92 if ((ret = libusb_init (ctx)) < 0)
93 fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
103 struct libusb_device *
104 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
106 libusb_device **list;
108 struct libusb_device *q;
111 // Make sure not operating on default context. There are cases where operating
112 // with a single global (NULL) context may be preferable, so this check can be
113 // skipped if you know what you're doing.
114 assert (ctx != NULL);
116 size_t cnt = libusb_get_device_list(ctx, &list);
120 fprintf(stderr, "usrp: libusb_get_device_list failed %d\n", cnt);
122 for (i = 0; i < cnt; i++) {
124 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
125 if (n_found == nth) // return this one
127 n_found++; // keep looking
131 // The list needs to be freed. Right now just release it if nothing is found.
132 libusb_free_device_list(list, 1);
134 return 0; // not found
137 struct libusb_device_handle *
138 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
140 libusb_device_handle *udh;
143 if (libusb_open (dev, &udh) < 0)
146 if (dev != libusb_get_device (udh)){
147 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
151 if ((ret = libusb_claim_interface (udh, interface)) < 0) {
152 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
153 fprintf (stderr, "%d\n", ret);
158 if ((ret = libusb_set_interface_alt_setting (udh, interface,
159 altinterface)) < 0) {
160 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
161 fprintf (stderr, "%d\n", ret);
162 libusb_release_interface (udh, interface);
171 usrp_close_interface (libusb_device_handle *udh)
179 // ----------------------------------------------------------------
180 // write vendor extension command to USRP
184 write_cmd (struct libusb_device_handle *udh,
185 int request, int value, int index,
186 unsigned char *bytes, int len)
188 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
190 int r = libusb_control_transfer(udh, requesttype, request, value, index,
191 (unsigned char *) bytes, len, 1000);
194 // we get EPIPE if the firmware stalls the endpoint.
195 if (r != LIBUSB_ERROR_PIPE) {
196 fprintf (stderr, "libusb_control_transfer failed: %i\n", r);