Merge branch 'osx_10.6_64_fixes' of http://gnuradio.org/git/michaelld
[debian/gnuradio] / usrp / host / lib / usrp_prims_libusb0.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 <usb.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 usb_device *
50 _get_usb_device (struct usb_dev_handle *udh)
51 {
52   return usb_device (udh);
53 }
54
55 struct usb_device_descriptor
56 _get_usb_device_descriptor (struct usb_device *q)
57 {
58   return q->descriptor;
59 }
60
61 int
62 _get_usb_string_descriptor (struct usb_dev_handle *udh, int index,
63                            unsigned char* data, int length)
64 {
65   int ret;
66   ret =  usb_get_string_simple (udh, index, (char*) data, length);
67
68   if (ret < 0) {
69     fprintf (stderr, "usrp: usb_get_string_descriptor failed: %s\n",
70              usb_strerror());
71   }
72
73   return ret;
74 }
75
76 int
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)
80 {
81   int ret;
82
83   ret = usb_control_msg (udh, request_type,request, value, index,
84                          (char*) data, length, (int) timeout);
85   if (ret < 0) 
86     fprintf (stderr, "usrp: usb_claim_interface failed: %s\n", usb_strerror());
87
88   return ret;
89 }
90
91
92 // ----------------------------------------------------------------
93
94
95 void
96 usrp_one_time_init (libusb_context **ctx)
97 {
98   static bool first = true;
99
100   if (first) {
101     first = false;
102     usb_init ();                        // usb library init
103     usb_find_busses ();
104     usb_find_devices ();
105   }
106 }
107
108 void
109 usrp_rescan ()
110 {
111   usb_find_busses ();
112   usb_find_devices ();
113 }
114
115
116 // ----------------------------------------------------------------
117
118
119 struct usb_device *
120 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
121 {
122   struct usb_bus *p;
123   struct usb_device *q;
124   int    n_found = 0;
125
126   usrp_one_time_init ();
127
128   p = usb_get_busses();
129   while (p != NULL){
130     q = p->devices;
131     while (q != NULL){
132       if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
133         if (n_found == nth)     // return this one
134           return q;
135         n_found++;              // keep looking
136       }
137       q = q->next;
138     }
139     p = p->next;
140   }
141   return 0;     // not found
142 }
143
144 struct usb_dev_handle *
145 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
146 {
147   struct usb_dev_handle *udh = usb_open (dev);
148   if (udh == 0)
149     return 0;
150
151   if (dev != usb_device (udh)){
152     fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
153     abort ();
154   }
155
156 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
157   // There's no get get_configuration function, and with some of the newer kernels
158   // setting the configuration, even if to the same value, hoses any other processes
159   // that have it open.  Hence we opt to not set it at all (We've only
160   // got a single configuration anyway).  This may hose the win32 stuff...
161
162   // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
163   if (usb_set_configuration (udh, 1) < 0){
164     /*
165      * Ignore this error.
166      *
167      * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
168      * it returns -EBUSY if _any_ of the interfaces of a device are open.
169      * We've only got a single configuration, so setting it doesn't even seem
170      * like it should be required.
171      */
172   }
173 #endif
174
175   if (usb_claim_interface (udh, interface) < 0){
176     fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
177     fprintf (stderr, "%s\n", usb_strerror());
178     usb_close (udh);
179     return 0;
180   }
181
182   if (usb_set_altinterface (udh, altinterface) < 0){
183     fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
184     fprintf (stderr, "%s\n", usb_strerror());
185     usb_release_interface (udh, interface);
186     usb_close (udh);
187     return 0;
188   }
189
190   return udh;
191 }
192
193 bool
194 usrp_close_interface (struct usb_dev_handle *udh)
195 {
196   // we're assuming that closing an interface automatically releases it.
197   return usb_close (udh) == 0;
198 }
199
200
201 // ----------------------------------------------------------------
202 // write vendor extension command to USRP
203
204
205 int
206 write_cmd (struct usb_dev_handle *udh,
207            int request, int value, int index,
208            unsigned char *bytes, int len)
209 {
210   int   requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
211
212   int r = usb_control_msg (udh, requesttype, request, value, index,
213                            (char *) bytes, len, 1000);
214   if (r < 0){
215     // we get EPIPE if the firmware stalls the endpoint.
216     if (errno != EPIPE) {
217       fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
218       fprintf (stderr, "write_cmd failed\n");
219     }
220   }
221
222   return r;
223 }
224