716e9fd70cba1d4beb132a86d8dc2d3cf73b4c6b
[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 ()
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
305
306 static usb_dev_handle *
307 open_nth_cmd_interface (int nth)
308 {
309   struct usb_device *udev = usrp_find_device (nth);
310   if (udev == 0){
311     fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
312     return 0;
313   }
314
315   struct usb_dev_handle *udh;
316
317   udh = usrp_open_cmd_interface (udev);
318   if (udh == 0){
319     // FIXME this could be because somebody else has it open.
320     // We should delay and retry...
321     fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
322     usb_strerror ();
323     return 0;
324   }
325
326   return udh;
327 }
328
329
330 usrp_load_status_t
331 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx){
332   struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
333   if (udh == 0)
334     return ULS_ERROR;
335
336   usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
337   usrp_close_interface (udh);
338
339   switch (s){
340
341   case ULS_ALREADY_LOADED:              // nothing changed...
342     return ULS_ALREADY_LOADED;
343     break;
344
345   case ULS_OK:
346     // we loaded firmware successfully.
347
348     // It's highly likely that the board will renumerate (simulate a
349     // disconnect/reconnect sequence), invalidating our current
350     // handle.
351
352     // FIXME.  Turn this into a loop that rescans until we refind ourselves
353     
354     struct timespec     t;      // delay for 1 second
355     t.tv_sec = 2;
356     t.tv_nsec = 0;
357     our_nanosleep (&t);
358
359     usb_find_busses ();         // rescan busses and devices
360     usb_find_devices ();
361
362     return ULS_OK;
363
364   default:
365   case ULS_ERROR:               // some kind of problem
366     return ULS_ERROR;
367   }
368 }
369
370 bool
371 usrp_load_standard_bits (int nth, bool force,
372                          const std::string fpga_filename,
373                          const std::string firmware_filename,
374                          libusb_context *ctx)
375 {
376   usrp_load_status_t    s;
377   const char            *filename;
378   const char            *proto_filename;
379   int hw_rev;
380
381   // first, figure out what hardware rev we're dealing with
382   {
383     struct usb_device *udev = usrp_find_device (nth);
384     if (udev == 0){
385       fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
386       return false;
387     }
388     hw_rev = usrp_hw_rev (udev);
389   }
390
391   // start by loading the firmware
392
393   proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
394                                       default_firmware_filename);
395   filename = find_file(proto_filename, hw_rev);
396   if (filename == 0){
397     fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
398     return false;
399   }
400
401   s = usrp_load_firmware_nth (nth, filename, force);
402   load_status_msg (s, "firmware", filename);
403
404   if (s == ULS_ERROR)
405     return false;
406
407   // if we actually loaded firmware, we must reload fpga ...
408   if (s == ULS_OK)
409     force = true;
410
411   // now move on to the fpga configuration bitstream
412
413   proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
414                                       default_fpga_filename);
415   filename = find_file (proto_filename, hw_rev);
416   if (filename == 0){
417     fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
418     return false;
419   }
420
421   struct usb_dev_handle *udh = open_nth_cmd_interface (nth);
422   if (udh == 0)
423     return false;
424   
425   s = usrp_load_fpga (udh, filename, force);
426   usrp_close_interface (udh);
427   load_status_msg (s, "fpga bitstream", filename);
428
429   if (s == ULS_ERROR)
430     return false;
431
432   return true;
433 }
434
435 void
436 power_down_9862s (struct usb_dev_handle *udh)
437 {
438   static const unsigned char regs[] = {
439     REG_RX_PWR_DN,      0x01,              // everything
440     REG_TX_PWR_DN,      0x0f,              // pwr dn digital and analog_both
441     REG_TX_MODULATOR,   0x00               // coarse & fine modulators disabled
442   };
443
444   switch (usrp_hw_rev (usb_device (udh))){
445   case 0:
446     break;
447
448   default:
449     usrp_9862_write_many_all (udh, regs, sizeof (regs));
450     break;
451   }
452 }
453
454
455 std::string
456 usrp_serial_number(struct usb_dev_handle *udh)
457 {
458   unsigned char iserial = usb_device(udh)->descriptor.iSerialNumber;
459   if (iserial == 0)
460     return "";
461
462   char buf[1024];
463   if (usb_get_string_simple(udh, iserial, buf, sizeof(buf)) < 0)
464     return "";
465
466   return buf;
467 }