Cleanup in preparation for merge
[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 /*
46  * libusb 0.12 / 1.0 compatibility
47  */
48
49 struct libusb_device *
50 _get_usb_device (struct libusb_device_handle *udh)
51 {
52   return libusb_get_device (udh);
53 }
54
55 struct libusb_device_descriptor
56 _get_usb_device_descriptor(struct libusb_device *q)
57 {
58   int ret;
59   struct libusb_device_descriptor desc;
60
61   if ((ret = libusb_get_device_descriptor(q, &desc)) < 0)
62     fprintf (stderr, "usrp: libusb_get_device_descriptor failed %d\n", ret);
63
64   return desc;
65 }
66
67 int
68 _get_usb_string_descriptor (struct libusb_device_handle *udh, int index,
69                            unsigned char* data, int length)
70 {
71   return libusb_get_string_descriptor_ascii (udh, (uint8_t) index, data, length);
72 }
73
74 int
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)
78 {
79   return libusb_control_transfer (udh, request_type, request, value, index,
80                                   data, length, timeout);
81 }
82
83
84 // ----------------------------------------------------------------
85
86
87 void
88 usrp_one_time_init (libusb_context **ctx)
89 {
90   int ret;
91
92   if ((ret = libusb_init (ctx)) < 0)
93     fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
94 }
95
96 void
97 usrp_rescan ()
98 {
99   // nop
100 }
101
102
103 struct libusb_device *
104 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
105 {
106   libusb_device **list;
107
108   struct libusb_device *q;
109   int    n_found = 0;
110
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);
115
116   size_t cnt = libusb_get_device_list(ctx, &list);
117   size_t i = 0;
118
119   if (cnt < 0)
120     fprintf(stderr, "usrp: libusb_get_device_list failed %d\n", cnt);
121
122   for (i = 0; i < cnt; i++) {
123     q = list[i];
124     if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
125         if (n_found == nth)     // return this one
126           return q;
127         n_found++;              // keep looking
128     }
129   }
130
131   // The list needs to be freed. Right now just release it if nothing is found.
132   libusb_free_device_list(list, 1);
133
134   return 0;     // not found
135 }
136
137 struct libusb_device_handle *
138 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
139 {
140   libusb_device_handle *udh;
141   int ret;
142
143   if (libusb_open (dev, &udh) < 0)
144     return 0;
145
146   if (dev != libusb_get_device (udh)){
147     fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
148     abort ();
149   }
150
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);
154     libusb_close (udh);
155     return 0;
156   }
157
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);
163     libusb_close (udh);
164     return 0;
165   }
166
167   return udh;
168 }
169
170 bool
171 usrp_close_interface (libusb_device_handle *udh)
172 {
173   // returns void
174   libusb_close(udh);
175   return 0;
176 }
177
178
179 // ----------------------------------------------------------------
180 // write vendor extension command to USRP
181
182
183 int
184 write_cmd (struct libusb_device_handle *udh,
185            int request, int value, int index,
186            unsigned char *bytes, int len)
187 {
188   int   requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
189
190   int r = libusb_control_transfer(udh, requesttype, request, value, index,
191                                   (unsigned char *) bytes, len, 1000);
192
193   if (r < 0){
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);
197     }
198   }
199
200   return r;
201 }
202