5dfe416e148231a3770781180b9f635fa823fd9c
[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
142 void
143 usrp_one_time_init (libusb_context **ctx)
144 {
145   int ret;
146
147   if ((ret = libusb_init (ctx)) < 0)
148     fprintf (stderr, "usrp: libusb_init failed: %s\n", _get_usb_error_str(ret));
149
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
153   // nothing. 
154   if (LIBUSB1_DEBUG)
155     libusb_set_debug(*ctx, 3);
156 }
157
158 void
159 usrp_deinit (struct libusb_context *ctx)
160 {
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.
163   if (ctx != NULL)
164     libusb_exit (ctx);
165 }
166
167 void
168 usrp_rescan ()
169 {
170   // nop
171 }
172
173 struct libusb_device *
174 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
175 {
176   libusb_device **list;
177
178   struct libusb_device *q;
179   int    n_found = 0;
180
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);
185
186   size_t cnt = libusb_get_device_list(ctx, &list);
187   size_t i = 0;
188
189   if (cnt < 0)
190     fprintf(stderr, "usrp: libusb_get_device_list failed: %s\n",
191             _get_usb_error_str(cnt));
192
193   for (i = 0; i < cnt; i++) {
194     q = list[i];
195     if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
196         if (n_found == nth)     // return this one
197           return q;
198         n_found++;              // keep looking
199     }
200   }
201
202   // The list needs to be freed. Right now just release it if nothing is found.
203   libusb_free_device_list(list, 1);
204
205   return 0;     // not found
206 }
207
208 struct libusb_device_handle *
209 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
210 {
211   libusb_device_handle *udh;
212   int ret;
213
214   if (libusb_open (dev, &udh) < 0)
215     return 0;
216
217   if (dev != libusb_get_device (udh)){
218     fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
219     abort ();
220   }
221
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));
226     libusb_close (udh);
227     return 0;
228   }
229
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);
235     libusb_close (udh);
236     return 0;
237   }
238
239   return udh;
240 }
241
242 bool
243 usrp_close_interface (libusb_device_handle *udh)
244 {
245   // returns void
246   libusb_close(udh);
247   return 0;
248 }
249
250
251 // ----------------------------------------------------------------
252 // write vendor extension command to USRP
253
254
255 int
256 write_cmd (struct libusb_device_handle *udh,
257            int request, int value, int index,
258            unsigned char *bytes, int len)
259 {
260   int requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
261
262   int ret = libusb_control_transfer(udh, requesttype, request, value, index,
263                                     bytes, len, 1000);
264
265   if (ret < 0) {
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");
271     }
272   }
273
274   return ret;
275 }
276