usrp: Disabled libusb-1.0 debug output by default
[debian/gnuradio] / usrp / host / lib / usrp_prims_libusb1.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,2004,2006,2009 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "usrp_primsi.h"
28 #include "usrp_commands.h"
29 #include <libusb-1.0/libusb.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <ad9862.h>
37 #include <assert.h>
38
39 extern "C" {
40 #include "md5.h"
41 };
42
43 using namespace ad9862;
44
45 static const int LIBUSB1_DEBUG = 0;
46
47 /*
48  * libusb 0.12 / 1.0 compatibility
49  */
50
51 static const char *
52 _get_usb_error_str (int usb_err)
53 {
54   switch (usb_err) {
55   case LIBUSB_SUCCESS:
56     return "Success (no error)";
57   case LIBUSB_ERROR_IO:
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:
72     return "Overflow";
73   case LIBUSB_ERROR_PIPE:
74     return "Pipe error";
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";
83   }
84
85   return "Unknown error";
86 }
87
88 struct libusb_device *
89 _get_usb_device (struct libusb_device_handle *udh)
90 {
91   return libusb_get_device (udh);
92 }
93
94 struct libusb_device_descriptor
95 _get_usb_device_descriptor(struct libusb_device *q)
96 {
97   int ret;
98   struct libusb_device_descriptor desc;
99
100   ret = libusb_get_device_descriptor(q, &desc);
101
102   if (ret < 0) {
103     fprintf (stderr, "usrp: libusb_get_device_descriptor failed: %s\n",
104              _get_usb_error_str(ret));
105   }
106   return desc;
107 }
108
109 int
110 _get_usb_string_descriptor (struct libusb_device_handle *udh, int index,
111                             unsigned char* data, int length)
112 {
113   int ret;
114   ret = libusb_get_string_descriptor_ascii (udh, (uint8_t) index, data, length);
115
116   if (ret < 0) {
117     fprintf (stderr, "usrp: libusb_get_string_descriptor_ascii failed: %s\n",
118              _get_usb_error_str(ret));
119   }
120   return ret;
121 }
122
123 int
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)
127 {
128   int ret;
129   ret = libusb_control_transfer (udh, request_type, request, value, index,
130                                  data, length, timeout);
131   if (ret < 0) {
132     fprintf (stderr, "usrp: libusb_control_transfer failed: %s\n",
133              _get_usb_error_str(ret));
134   }
135   return ret;
136 }
137
138 // ----------------------------------------------------------------
139
140
141 void
142 usrp_one_time_init (libusb_context **ctx)
143 {
144   int ret;
145
146   if ((ret = libusb_init (ctx)) < 0)
147     fprintf (stderr, "usrp: libusb_init failed: %s\n", _get_usb_error_str(ret));
148
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
152   // nothing. 
153   if (LIBUSB1_DEBUG)
154     libusb_set_debug(*ctx, 3);
155 }
156
157 void
158 usrp_rescan ()
159 {
160   // nop
161 }
162
163
164 struct libusb_device *
165 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
166 {
167   libusb_device **list;
168
169   struct libusb_device *q;
170   int    n_found = 0;
171
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);
176
177   size_t cnt = libusb_get_device_list(ctx, &list);
178   size_t i = 0;
179
180   if (cnt < 0)
181     fprintf(stderr, "usrp: libusb_get_device_list failed: %s\n",
182             _get_usb_error_str(cnt));
183
184   for (i = 0; i < cnt; i++) {
185     q = list[i];
186     if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
187         if (n_found == nth)     // return this one
188           return q;
189         n_found++;              // keep looking
190     }
191   }
192
193   // The list needs to be freed. Right now just release it if nothing is found.
194   libusb_free_device_list(list, 1);
195
196   return 0;     // not found
197 }
198
199 struct libusb_device_handle *
200 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
201 {
202   libusb_device_handle *udh;
203   int ret;
204
205   if (libusb_open (dev, &udh) < 0)
206     return 0;
207
208   if (dev != libusb_get_device (udh)){
209     fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
210     abort ();
211   }
212
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));
217     libusb_close (udh);
218     return 0;
219   }
220
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);
226     libusb_close (udh);
227     return 0;
228   }
229
230   return udh;
231 }
232
233 bool
234 usrp_close_interface (libusb_device_handle *udh)
235 {
236   // returns void
237   libusb_close(udh);
238   return 0;
239 }
240
241
242 // ----------------------------------------------------------------
243 // write vendor extension command to USRP
244
245
246 int
247 write_cmd (struct libusb_device_handle *udh,
248            int request, int value, int index,
249            unsigned char *bytes, int len)
250 {
251   int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
252
253   int ret = libusb_control_transfer(udh, requesttype, request, value, index,
254                                     bytes, len, 1000);
255
256   if (ret < 0) {
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");
262     }
263   }
264
265   return ret;
266 }
267