Integrated more 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 "usrp_i2c_addr.h"
31 #include "fpga_regs_common.h"
32 #include "fpga_regs_standard.h"
33 #include <usb.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <time.h>               // FIXME should check with autoconf (nanosleep)
41 #include <algorithm>
42 #include <ad9862.h>
43 #include <assert.h>
44
45 extern "C" {
46 #include "md5.h"
47 };
48
49 #define VERBOSE 0
50
51 using namespace ad9862;
52
53 static const int FIRMWARE_HASH_SLOT     = 0;
54 static const int FPGA_HASH_SLOT         = 1;
55
56 static const int hash_slot_addr[2] = {
57   USRP_HASH_SLOT_0_ADDR,
58   USRP_HASH_SLOT_1_ADDR
59 };
60
61 static const char *default_firmware_filename = "std.ihx";
62 static const char *default_fpga_filename     = "std_2rxhb_2tx.rbf";
63
64 #include "std_paths.h"
65 #include <stdio.h>
66
67 void
68 usrp_one_time_init (libusb_context **ctx)
69 {
70   static bool first = true;
71
72   if (first) {
73     first = false;
74     usb_init ();                        // usb library init
75     usb_find_busses ();
76     usb_find_devices ();
77   }
78 }
79
80 void
81 usrp_rescan ()
82 {
83   usb_find_busses ();
84   usb_find_devices ();
85 }
86
87 // ----------------------------------------------------------------
88
89 /*
90  * q must be a real USRP, not an FX2.  Return its hardware rev number.
91  */
92 int
93 usrp_hw_rev (struct usb_device *q)
94 {
95   return q->descriptor.bcdDevice & 0x00FF;
96 }
97
98 /*
99  * q must be a real USRP, not an FX2.  Return true if it's configured.
100  */
101 bool
102 _usrp_configured_p (struct usb_device *q)
103 {
104   return (q->descriptor.bcdDevice & 0xFF00) != 0;
105 }
106
107 bool
108 usrp_usrp_p (struct usb_device *q)
109 {
110   return (q->descriptor.idVendor == USB_VID_FSF
111           && q->descriptor.idProduct == USB_PID_FSF_USRP);
112 }
113
114 bool
115 usrp_fx2_p (struct usb_device *q)
116 {
117   return (q->descriptor.idVendor == USB_VID_CYPRESS
118           && q->descriptor.idProduct == USB_PID_CYPRESS_FX2);
119 }
120
121 // ----------------------------------------------------------------
122
123 struct usb_device *
124 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
125 {
126   struct usb_bus *p;
127   struct usb_device *q;
128   int    n_found = 0;
129
130   usrp_one_time_init ();
131   
132   p = usb_get_busses();
133   while (p != NULL){
134     q = p->devices;
135     while (q != NULL){
136       if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))){
137         if (n_found == nth)     // return this one
138           return q;
139         n_found++;              // keep looking
140       }
141       q = q->next;
142     }
143     p = p->next;
144   }
145   return 0;     // not found
146 }
147
148 struct usb_dev_handle *
149 usrp_open_interface (struct usb_device *dev, int interface, int altinterface)
150 {
151   struct usb_dev_handle *udh = usb_open (dev);
152   if (udh == 0)
153     return 0;
154
155   if (dev != usb_device (udh)){
156     fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
157     abort ();
158   }
159
160 #if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
161   // There's no get get_configuration function, and with some of the newer kernels
162   // setting the configuration, even if to the same value, hoses any other processes
163   // that have it open.  Hence we opt to not set it at all (We've only
164   // got a single configuration anyway).  This may hose the win32 stuff...
165
166   // Appears to be required for libusb-win32 and Cygwin -- dew 09/20/06
167   if (usb_set_configuration (udh, 1) < 0){
168     /*
169      * Ignore this error.  
170      *
171      * Seems that something changed in drivers/usb/core/devio.c:proc_setconfig such that
172      * it returns -EBUSY if _any_ of the interfaces of a device are open.
173      * We've only got a single configuration, so setting it doesn't even seem
174      * like it should be required.
175      */
176   }
177 #endif
178
179   if (usb_claim_interface (udh, interface) < 0){
180     fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
181     fprintf (stderr, "%s\n", usb_strerror());
182     usb_close (udh);
183     return 0;
184   }
185
186   if (usb_set_altinterface (udh, altinterface) < 0){
187     fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
188     fprintf (stderr, "%s\n", usb_strerror());
189     usb_release_interface (udh, interface);
190     usb_close (udh);
191     return 0;
192   }
193
194   return udh;
195 }
196
197 bool
198 usrp_close_interface (struct usb_dev_handle *udh)
199 {
200   // we're assuming that closing an interface automatically releases it.
201   return usb_close (udh) == 0;
202 }
203
204 // ----------------------------------------------------------------
205 // write internal ram using Cypress vendor extension
206
207 bool
208 write_internal_ram (struct usb_dev_handle *udh, unsigned char *buf,
209                     int start_addr, size_t len)
210 {
211   int addr;
212   int n;
213   int a;
214   int quanta = MAX_EP0_PKTSIZE;
215
216   for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
217     n = len + start_addr - addr;
218     if (n > quanta)
219       n = quanta;
220
221     a = usb_control_msg (udh, 0x40, 0xA0,
222                          addr, 0, (char *)(buf + (addr - start_addr)), n, 1000);
223
224     if (a < 0){
225       fprintf(stderr,"write_internal_ram failed: %s\n", usb_strerror());
226       return false;
227     }
228   }
229   return true;
230 }
231
232
233 // ----------------------------------------------------------------
234 // write vendor extension command to USRP
235
236 int
237 write_cmd (struct usb_dev_handle *udh,
238            int request, int value, int index,
239            unsigned char *bytes, int len)
240 {
241   int   requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
242
243   int r = usb_control_msg (udh, requesttype, request, value, index,
244                            (char *) bytes, len, 1000);
245   if (r < 0){
246     // we get EPIPE if the firmware stalls the endpoint.
247     if (errno != EPIPE)
248       fprintf (stderr, "usb_control_msg failed: %s\n", usb_strerror ());
249   }
250
251   return r;
252 }
253
254
255 bool
256 usrp_set_hash (struct usb_dev_handle *udh, int which,
257                const unsigned char hash[USRP_HASH_SIZE])
258 {
259   which &= 1;
260   
261   // we use the Cypress firmware down load command to jam it in.
262   int r = usb_control_msg (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
263                            (char *) hash, USRP_HASH_SIZE, 1000);
264   return r == USRP_HASH_SIZE;
265 }
266
267 bool
268 usrp_get_hash (struct usb_dev_handle *udh, int which, 
269                unsigned char hash[USRP_HASH_SIZE])
270 {
271   which &= 1;
272   
273   // we use the Cypress firmware upload command to fetch it.
274   int r = usb_control_msg (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
275                            (char *) hash, USRP_HASH_SIZE, 1000);
276   return r == USRP_HASH_SIZE;
277 }
278
279 bool
280 usrp_write_fpga_reg (struct usb_dev_handle *udh, int reg, int value)
281 {
282   switch (usrp_hw_rev (usb_device (udh))){
283   case 0:                       // not supported ;)
284     abort();    
285
286   default:
287     return usrp1_fpga_write (udh, reg, value);
288   }
289 }
290
291 bool
292 usrp_read_fpga_reg (struct usb_dev_handle *udh, int reg, int *value)
293 {
294   switch (usrp_hw_rev (usb_device (udh))){
295   case 0:               // not supported ;)
296     abort();
297     
298   default:
299     return usrp1_fpga_read (udh, reg, value);
300   }
301 }
302
303
304 void
305 power_down_9862s (struct usb_dev_handle *udh)
306 {
307   static const unsigned char regs[] = {
308     REG_RX_PWR_DN,      0x01,              // everything
309     REG_TX_PWR_DN,      0x0f,              // pwr dn digital and analog_both
310     REG_TX_MODULATOR,   0x00               // coarse & fine modulators disabled
311   };
312
313   switch (usrp_hw_rev (usb_device (udh))){
314   case 0:
315     break;
316
317   default:
318     usrp_9862_write_many_all (udh, regs, sizeof (regs));
319     break;
320   }
321 }
322
323 // ----------------------------------------------------------------
324
325 std::string
326 usrp_serial_number(struct usb_dev_handle *udh)
327 {
328   unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
329   if (iserial == 0)
330     return "";
331
332   char buf[1024];
333   if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)
334     return "";
335
336   return buf;
337 }