Combined additiona usrp_prims code
[debian/gnuradio] / usrp / host / lib / usrp_prims_libusb.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/usrp_prims.h"
28 #include "usrp_commands.h"
29 //#include "usrp_ids.h"
30 #include <usb.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <ad9862.h>
38 #include <assert.h>
39
40 extern "C" {
41 #include "md5.h"
42 };
43
44 using namespace ad9862;
45
46 struct usb_device_descriptor
47 get_usb_device_descriptor (struct usb_device *q)
48 {
49   return q->descriptor;
50 }
51
52 struct usb_device *
53 get_usb_device (struct usb_dev_handle *udh)
54 {
55   return usb_device (udh);
56 }
57
58 int
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)
62 {
63   return usb_control_msg (udh, request_type, request, value, index,
64                           (char*) *data, length, (int) timeout);
65
66 }
67
68
69 // ----------------------------------------------------------------
70
71
72 void
73 usrp_one_time_init (libusb_context **ctx)
74 {
75   static bool first = true;
76
77   if (first) {
78     first = false;
79     usb_init ();                        // usb library init
80     usb_find_busses ();
81     usb_find_devices ();
82   }
83 }
84
85 void
86 usrp_rescan ()
87 {
88   usb_find_busses ();
89   usb_find_devices ();
90 }
91
92
93 // ----------------------------------------------------------------
94
95
96 struct usb_device *
97 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
98 {
99   struct usb_bus *p;
100   struct usb_device *q;
101   int    n_found = 0;
102
103   usrp_one_time_init ();
104   
105   p = usb_get_busses();
106   while (p != NULL){
107     q = p->devices;
108     while (q != NULL){
109       if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
110         if (n_found == nth)     // return this one
111           return q;
112         n_found++;              // keep looking
113       }
114       q = q->next;
115     }
116     p = p->next;
117   }
118   return 0;     // not found
119 }
120
121 struct usb_dev_handle *
122 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
123 {
124   struct usb_dev_handle *udh = usb_open (dev);
125   if (udh == 0)
126     return 0;
127
128   if (dev != usb_device (udh)){
129     fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
130     abort ();
131   }
132
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...
138
139   // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
140   if (usb_set_configuration (udh, 1) < 0){
141     /*
142      * Ignore this error.  
143      *
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.
148      */
149   }
150 #endif
151
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());
155     usb_close (udh);
156     return 0;
157   }
158
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);
163     usb_close (udh);
164     return 0;
165   }
166
167   return udh;
168 }
169
170 bool
171 usrp_close_interface (struct usb_dev_handle *udh)
172 {
173   // we're assuming that closing an interface automatically releases it.
174   return usb_close (udh) == 0;
175 }
176
177
178 // ----------------------------------------------------------------
179 // write vendor extension command to USRP
180
181
182 int
183 write_cmd (struct usb_dev_handle *udh,
184            int request, int value, int index,
185            unsigned char *bytes, int len)
186 {
187   int   requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
188
189   int r = usb_control_msg (udh, requesttype, request, value, index,
190                            (char *) bytes, len, 1000);
191   if (r < 0){
192     // we get EPIPE if the firmware stalls the endpoint.
193     if (errno != EPIPE)
194       fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
195   }
196
197   return r;
198 }
199
200
201 // ----------------------------------------------------------------
202
203
204 std::string
205 usrp_serial_number(struct usb_dev_handle *udh)
206 {
207   unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
208   if (iserial == 0)
209     return "";
210
211   char buf[1024];
212   if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)
213     return "";
214
215   return buf;
216 }