]> sigrok.org Git - libsigrok.git/blob - hardware/fx2lafw/dslogic.c
fx2lafw: scan/firmware support for DSLogic.
[libsigrok.git] / hardware / fx2lafw / dslogic.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6  *
7  * This program 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 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <glib.h>
26 #include <glib/gstdio.h>
27 #include "protocol.h"
28
29 #define FW_BUFSIZE 4096
30 int dslogic_fpga_firmware_upload(struct libusb_device_handle *hdl,
31                 const char *filename)
32 {
33         FILE *fw;
34         struct stat st;
35         int chunksize, result, ret;
36         unsigned char *buf;
37         int sum, transferred;
38
39         sr_info("Uploading FPGA firmware at %s.", filename);
40
41         if (stat(filename, &st) < 0) {
42                 sr_err("Unable to upload FPGA firmware: %s", strerror(errno));
43                 return SR_ERR;
44         }
45
46         /* Tell the device firmware is coming. */
47         if ((ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
48                         LIBUSB_ENDPOINT_OUT, CMD_DSLOGIC_CONFIG, 0x0000, 0x0000,
49                         NULL, 0, 3000)) < 0) {
50                 sr_err("Failed to upload FPGA firmware: %s.", libusb_error_name(ret));
51                 return SR_ERR;
52         }
53         buf = g_malloc(FW_BUFSIZE);
54
55         if ((fw = g_fopen(filename, "rb")) == NULL) {
56                 sr_err("Unable to open %s for reading: %s.", filename, strerror(errno));
57                 return SR_ERR;
58         }
59
60         /* Give the FX2 time to get ready for FPGA firmware upload. */
61         g_usleep(10 * 1000);
62
63         sum = 0;
64         result = SR_OK;
65         while (1) {
66                 if ((chunksize = fread(buf, 1, FW_BUFSIZE, fw)) == 0)
67                         break;
68
69                 if ((ret = libusb_bulk_transfer(hdl, 2 | LIBUSB_ENDPOINT_OUT,
70                                 buf, chunksize, &transferred, 1000)) < 0) {
71                         sr_err("Unable to configure FPGA firmware: %s.",
72                                         libusb_error_name(ret));
73                         result = SR_ERR;
74                         break;
75                 }
76                 sum += transferred;
77                 sr_info("Uploaded %d/%d bytes.", sum, st.st_size);
78
79                 if (transferred != chunksize) {
80                         sr_err("Short transfer while uploading FPGA firmware.");
81                         result = SR_ERR;
82                         break;
83                 }
84         }
85         fclose(fw);
86         if (result == SR_OK)
87                 sr_info("FPGA firmware upload done.");
88
89         return result;
90 }
91