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/usrp_prims.h"
28 #include "usrp_commands.h"
29 //#include "usrp_ids.h"
44 using namespace ad9862;
46 struct usb_device_descriptor
47 get_usb_device_descriptor (struct usb_device *q)
53 get_usb_device (struct usb_dev_handle *udh)
55 return usb_device (udh);
59 usb_control_transfer (struct usb_dev_handle *udh, int request_type,
60 int request, int value, int index,
61 unsigned char *data, int length, unsigned int timeout)
63 return usb_control_msg (udh, request_type, request, value, index,
64 (char*) *data, length, (int) timeout);
69 // ----------------------------------------------------------------
73 usrp_one_time_init (libusb_context **ctx)
75 static bool first = true;
79 usb_init (); // usb library init
93 // ----------------------------------------------------------------
97 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
100 struct usb_device *q;
103 usrp_one_time_init ();
105 p = usb_get_busses();
109 if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
110 if (n_found == nth) // return this one
112 n_found++; // keep looking
118 return 0; // not found
121 struct usb_dev_handle *
122 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
124 struct usb_dev_handle *udh = usb_open (dev);
128 if (dev != usb_device (udh)){
129 fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
133 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
134 // There's no get get_configuration function, and with some of the newer kernels
135 // setting the configuration, even if to the same value, hoses any other processes
136 // that have it open. Hence we opt to not set it at all (We've only
137 // got a single configuration anyway). This may hose the win32 stuff...
139 // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
140 if (usb_set_configuration (udh, 1) < 0){
144 * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
145 * it returns -EBUSY if _any_ of the interfaces of a device are open.
146 * We've only got a single configuration, so setting it doesn't even seem
147 * like it should be required.
152 if (usb_claim_interface (udh, interface) < 0){
153 fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
154 fprintf (stderr, "%s\n", usb_strerror());
159 if (usb_set_altinterface (udh, altinterface) < 0){
160 fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
161 fprintf (stderr, "%s\n", usb_strerror());
162 usb_release_interface (udh, interface);
171 usrp_close_interface (struct usb_dev_handle *udh)
173 // we're assuming that closing an interface automatically releases it.
174 return usb_close (udh) == 0;
178 // ----------------------------------------------------------------
179 // write vendor extension command to USRP
183 write_cmd (struct usb_dev_handle *udh,
184 int request, int value, int index,
185 unsigned char *bytes, int len)
187 int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
189 int r = usb_control_msg (udh, requesttype, request, value, index,
190 (char *) bytes, len, 1000);
192 // we get EPIPE if the firmware stalls the endpoint.
194 fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
201 // ----------------------------------------------------------------
205 usrp_serial_number(struct usb_dev_handle *udh)
207 unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
212 if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)