a99adafb3ecd3b7b617fada34d5bc3693b3ab35d
[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/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 <libusb-1.0/libusb.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 libusb_context *
68 usrp_one_time_init (bool new_context)
69 {
70
71   static bool first = true;
72   libusb_context *ctx = NULL;
73   int ret;
74
75   // On first call create default context in addition to any new requested
76   // context. The default context is probably useless in this form, but keep
77   // it for now due to compatibility reasons.
78
79   if (first) {
80     first = false;
81     if ((ret = libusb_init (NULL)) < 0)
82       fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
83   }
84
85   if (new_context) {
86     if ((ret = libusb_init (&ctx)) < 0)
87       fprintf (stderr, "usrp: libusb_init failed %i\n", ret);
88   }
89
90   return ctx;
91 }
92
93 void
94 usrp_rescan ()
95 {
96   // nop
97 }
98
99 // ----------------------------------------------------------------
100
101 /*
102  * q must be a real USRP, not an FX2.  Return its hardware rev number.
103  */
104 int
105 usrp_hw_rev (struct libusb_device *q)
106 {
107   struct libusb_device_descriptor desc;
108   if (libusb_get_device_descriptor(q, &desc) < 0)
109     fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
110
111   return desc.bcdDevice & 0x00FF;
112 }
113
114 /*
115  * q must be a real USRP, not an FX2.  Return true if it's configured.
116  */
117 bool
118 _usrp_configured_p (struct libusb_device *q)
119 {
120   struct libusb_device_descriptor desc;
121   if (libusb_get_device_descriptor(q, &desc) < 0)
122     fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
123
124   return (desc.bcdDevice & 0xFF00) != 0;
125 }
126
127 bool
128 usrp_usrp_p (struct libusb_device *q)
129 {
130   struct libusb_device_descriptor desc;
131   if (libusb_get_device_descriptor(q, &desc) < 0)
132     fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
133
134   return (desc.idVendor == USB_VID_FSF
135           && desc.idProduct == USB_PID_FSF_USRP);
136 }
137
138 bool
139 usrp_fx2_p (struct libusb_device *q)
140 {
141   struct libusb_device_descriptor desc;
142   if (libusb_get_device_descriptor(q, &desc) < 0)
143     fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
144
145   return (desc.idVendor == USB_VID_CYPRESS
146           && desc.idProduct == USB_PID_CYPRESS_FX2);
147 }
148
149
150 // ----------------------------------------------------------------
151
152 struct libusb_device *
153 usrp_find_device (int nth, bool fx2_ok_p, libusb_context *ctx)
154 {
155   libusb_device **list;
156
157   struct libusb_device *q;
158   int    n_found = 0;
159
160 //usrp_one_time_init (false);
161   assert (ctx != NULL);
162
163   size_t cnt = libusb_get_device_list(ctx, &list);
164   size_t i = 0;
165
166   if (cnt < 0)
167     fprintf(stderr, "usrp: libusb_get_device_list failed %d\n", cnt);
168
169   for (i = 0; i < cnt; i++) {
170     q = list[i];
171     if (usrp_usrp_p (q) || (fx2_ok_p && usrp_fx2_p (q))) {
172         if (n_found == nth)     // return this one
173           return q;
174         n_found++;              // keep looking
175     }
176   }
177
178 /*
179  * The list needs to be freed. Right now just release it if nothing is found.
180  */
181
182   libusb_free_device_list(list, 1);
183
184   return 0;     // not found
185 }
186
187 struct libusb_device_handle *
188 usrp_open_interface (libusb_device *dev, int interface, int altinterface)
189 {
190   struct libusb_device_handle *udh;
191   int ret;
192
193   if (libusb_open (dev, &udh) < 0)
194     return 0;
195
196   if (dev != libusb_get_device (udh)){
197     fprintf (stderr, "%s:%d: internal error!\n", __FILE__, __LINE__);
198     abort ();
199   }
200
201   if ((ret = libusb_claim_interface (udh, interface)) < 0) {
202     fprintf (stderr, "%s:usb_claim_interface: failed interface %d\n", __FUNCTION__,interface);
203     fprintf (stderr, "%d\n", ret);
204     libusb_close (udh);
205     return 0;
206   }
207
208   if ((ret = libusb_set_interface_alt_setting (udh, interface,
209                                                    altinterface)) < 0) {
210     fprintf (stderr, "%s:usb_set_alt_interface: failed\n", __FUNCTION__);
211     fprintf (stderr, "%d\n", ret);
212     libusb_release_interface (udh, interface);
213     libusb_close (udh);
214     return 0;
215   }
216
217   return udh;
218 }
219
220 bool
221 usrp_close_interface (libusb_device_handle *udh)
222 {
223   // returns void
224   libusb_close(udh);
225   return 0;
226 }
227
228 // ----------------------------------------------------------------
229 // write internal ram using Cypress vendor extension
230
231 bool
232 write_internal_ram (struct libusb_device_handle *udh, unsigned char *buf,
233                     int start_addr, size_t len)
234 {
235   int addr;
236   int n;
237   int a;
238   int quanta = MAX_EP0_PKTSIZE;
239
240   for (addr = start_addr; addr < start_addr + (int) len; addr += quanta){
241     n = len + start_addr - addr;
242     if (n > quanta)
243       n = quanta;
244
245     a = libusb_control_transfer (udh, 0x40, 0xA0,
246                          addr, 0, (unsigned char *)(buf + (addr - start_addr)), n, 1000);
247
248     if (a < 0){
249       fprintf(stderr,"write_internal_ram failed: %u\n", a);
250       return false;
251     }
252   }
253   return true;
254 }
255
256 // ----------------------------------------------------------------
257 // write vendor extension command to USRP
258
259 int
260 write_cmd (struct libusb_device_handle *udh,
261            int request, int value, int index,
262            unsigned char *bytes, int len)
263 {
264   int   requesttype = (request & 0x80) ? VRT_VENDOR_IN : VRT_VENDOR_OUT;
265
266   int r = libusb_control_transfer(udh, requesttype, request, value, index,
267                                   (unsigned char *) bytes, len, 1000);
268
269   if (r < 0){
270     // we get EPIPE if the firmware stalls the endpoint.
271     if (r != LIBUSB_ERROR_PIPE) {
272       fprintf (stderr, "libusb_control_transfer failed: %i\n", r);
273     }
274   }
275
276   return r;
277 }
278
279 bool
280 usrp_set_hash (struct libusb_device_handle *udh, int which,
281                const unsigned char hash[USRP_HASH_SIZE])
282 {
283   which &= 1;
284
285   // we use the Cypress firmware down load command to jam it in.
286   int r = libusb_control_transfer (udh, 0x40, 0xa0, hash_slot_addr[which], 0,
287                            (unsigned char *) hash, USRP_HASH_SIZE, 1000);
288   return r == USRP_HASH_SIZE;
289 }
290   
291 bool
292 usrp_get_hash (struct libusb_device_handle *udh, int which,
293                unsigned char hash[USRP_HASH_SIZE])
294 {
295   which &= 1;
296
297   // we use the Cypress firmware upload command to fetch it.
298   int r = libusb_control_transfer (udh, 0xc0, 0xa0, hash_slot_addr[which], 0,
299                            (unsigned char *) hash, USRP_HASH_SIZE, 1000);
300   return r == USRP_HASH_SIZE;
301 }
302
303 bool
304 usrp_write_fpga_reg (struct libusb_device_handle *udh, int reg, int value)
305 {
306   switch (usrp_hw_rev (libusb_get_device (udh))){
307   case 0:                       // not supported ;)
308     abort();
309
310   default:
311     return usrp1_fpga_write (udh, reg, value);
312   }
313 }
314
315 bool
316 usrp_read_fpga_reg (struct libusb_device_handle *udh, int reg, int *value)
317 {
318   switch (usrp_hw_rev (libusb_get_device (udh))){
319   case 0:               // not supported ;)
320     abort();
321
322   default:
323     return usrp1_fpga_read (udh, reg, value);
324   }
325 }
326
327
328 static libusb_device_handle *
329 open_nth_cmd_interface (int nth, libusb_context *ctx)
330 {
331
332   struct libusb_device *udev = usrp_find_device (nth, false, ctx);
333   if (udev == 0){
334     fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
335     return 0;
336   }
337
338   struct libusb_device_handle *udh;
339
340   udh = usrp_open_cmd_interface (udev);
341   if (udh == 0){
342     // FIXME this could be because somebody else has it open.
343     // We should delay and retry...
344     fprintf (stderr, "open_nth_cmd_interface: open_cmd_interface failed\n");
345     return 0;
346   }
347
348   return udh;
349 }
350
351 usrp_load_status_t
352 usrp_load_firmware_nth (int nth, const char *filename, bool force, libusb_context *ctx)
353 {
354   struct libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
355   if (udh == 0)
356     return ULS_ERROR;
357
358   usrp_load_status_t s = usrp_load_firmware (udh, filename, force);
359   usrp_close_interface (udh);
360
361   switch (s){
362
363   case ULS_ALREADY_LOADED:              // nothing changed...
364     return ULS_ALREADY_LOADED;
365     break;
366
367   case ULS_OK:
368     // we loaded firmware successfully.
369  
370     // It's highly likely that the board will renumerate (simulate a
371     // disconnect/reconnect sequence), invalidating our current
372     // handle.
373
374     // FIXME.  Turn this into a loop that rescans until we refind ourselves
375    
376     struct timespec     t;      // delay for 1 second
377     t.tv_sec = 2;
378     t.tv_nsec = 0;
379     our_nanosleep (&t);
380
381     return ULS_OK;
382
383   default:
384   case ULS_ERROR:               // some kind of problem
385     return ULS_ERROR;
386   }
387 }
388
389 bool
390 usrp_load_standard_bits (int nth, bool force,
391                          const std::string fpga_filename,
392                          const std::string firmware_filename,
393                          libusb_context *ctx)
394 {
395   usrp_load_status_t    s;
396   const char            *filename;
397   const char            *proto_filename;
398   int hw_rev;
399
400   assert (ctx != NULL);
401
402   // first, figure out what hardware rev we're dealing with
403   {
404     struct libusb_device *udev = usrp_find_device (nth, false, ctx);
405     if (udev == 0){
406       fprintf (stderr, "usrp: failed to find usrp[%d]\n", nth);
407       return false;
408     }
409     hw_rev = usrp_hw_rev (udev);
410   }
411
412   // start by loading the firmware
413
414   proto_filename = get_proto_filename(firmware_filename, "USRP_FIRMWARE",
415                                       default_firmware_filename);
416   filename = find_file(proto_filename, hw_rev);
417   if (filename == 0){
418     fprintf (stderr, "Can't find firmware: %s\n", proto_filename);
419     return false;
420   }
421   s = usrp_load_firmware_nth (nth, filename, force, ctx);
422   load_status_msg (s, "firmware", filename);
423
424   if (s == ULS_ERROR)
425     return false;
426
427   // if we actually loaded firmware, we must reload fpga ...
428   if (s == ULS_OK)
429     force = true;
430
431   // now move on to the fpga configuration bitstream
432
433   proto_filename = get_proto_filename(fpga_filename, "USRP_FPGA",
434                                       default_fpga_filename);
435   filename = find_file (proto_filename, hw_rev);
436   if (filename == 0){
437     fprintf (stderr, "Can't find fpga bitstream: %s\n", proto_filename);
438     return false;
439   }
440   struct libusb_device_handle *udh = open_nth_cmd_interface (nth, ctx);
441   if (udh == 0)
442     return false;
443   
444   s = usrp_load_fpga (udh, filename, force);
445   usrp_close_interface (udh);
446   load_status_msg (s, "fpga bitstream", filename);
447
448   if (s == ULS_ERROR)
449     return false;
450
451   return true;
452 }
453
454 void
455 power_down_9862s (struct libusb_device_handle *udh)
456 {
457   static const unsigned char regs[] = {
458     REG_RX_PWR_DN,      0x01,                   // everything
459     REG_TX_PWR_DN,      0x0f,                   // pwr dn digital and analog_both
460     REG_TX_MODULATOR,   0x00                    // coarse & fine modulators disabled
461   };
462
463   switch (usrp_hw_rev (libusb_get_device (udh))){
464   case 0:
465     break;
466
467   default:
468     usrp_9862_write_many_all (udh, regs, sizeof (regs));
469     break;
470   }
471 }
472
473
474 // ----------------------------------------------------------------
475
476 std::string
477 usrp_serial_number(struct libusb_device_handle *udh)
478 {
479   struct libusb_device_descriptor desc;
480   if (libusb_get_device_descriptor(libusb_get_device(udh), &desc) < 0)
481     fprintf (stderr, "usrp: libusb_get_device_descriptor failed\n");
482
483   unsigned char iserial = desc.iSerialNumber;
484   if (iserial == 0)
485     return "";
486
487   unsigned char buf[1024];
488   if (libusb_get_string_descriptor_ascii(udh, iserial, buf, sizeof(buf)) < 0)
489     return "";
490
491   return (char*) buf;
492 }