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"
43 using namespace ad9862;
46 * libusb 0.12 / 1.0 compatibility
50 _get_usb_device (struct usb_dev_handle *udh)
52 return usb_device (udh);
55 struct usb_device_descriptor
56 _get_usb_device_descriptor (struct usb_device *q)
62 _get_usb_string_descriptor (struct usb_dev_handle *udh, int index,
63 unsigned char* data, int length)
66 ret = usb_get_string_simple (udh, index, (char*) data, length);
69 fprintf (stderr, "usrp: usb_get_string_descriptor failed: %s\n",
77 _usb_control_transfer (struct usb_dev_handle *udh, int request_type,
78 int request, int value, int index,
79 unsigned char *data, int length, unsigned int timeout)
83 ret = usb_control_msg (udh, request_type,request, value, index,
84 (char*) data, length, (int) timeout);
86 fprintf (stderr, "usrp: usb_control_msg failed: %s\n", usb_strerror());
92 // ----------------------------------------------------------------
96 usrp_one_time_init (libusb_context **ctx)
98 static bool first = true;
102 usb_init (); // usb library init
109 usrp_deinit (libusb_context *ctx)
122 // ----------------------------------------------------------------
126 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
129 struct usb_device *q;
132 usrp_one_time_init ();
134 p = usb_get_busses();
138 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
139 if (n_found == nth) // return this one
141 n_found++; // keep looking
147 return 0; // not found
150 struct usb_dev_handle *
151 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
153 struct usb_dev_handle *udh = usb_open (dev);
157 if (dev != usb_device (udh)){
158 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
162 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
163 // There's no get get_configuration function, and with some of the newer kernels
164 // setting the configuration, even if to the same value, hoses any other processes
165 // that have it open. Hence we opt to not set it at all (We've only
166 // got a single configuration anyway). This may hose the win32 stuff...
168 // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
169 if (usb_set_configuration (udh, 1) < 0){
173 * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
174 * it returns -EBUSY if _any_ of the interfaces of a device are open.
175 * We've only got a single configuration, so setting it doesn't even seem
176 * like it should be required.
181 if (usb_claim_interface (udh, interface) < 0){
182 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
183 fprintf (stderr, "%s\n", usb_strerror());
188 if (usb_set_altinterface (udh, altinterface) < 0){
189 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
190 fprintf (stderr, "%s\n", usb_strerror());
191 usb_release_interface (udh, interface);
200 usrp_close_interface (struct usb_dev_handle *udh)
202 // we're assuming that closing an interface automatically releases it.
203 return usb_close (udh) == 0;
207 // ----------------------------------------------------------------
208 // write vendor extension command to USRP
212 write_cmd (struct usb_dev_handle *udh,
213 int request, int value, int index,
214 unsigned char *bytes, int len)
216 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
218 int r = usb_control_msg (udh, requesttype, request, value, index,
219 (char *) bytes, len, 1000);
221 // we get EPIPE if the firmware stalls the endpoint.
222 if (errno != EPIPE) {
223 fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
224 fprintf (stderr, "write_cmd failed\n");