]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-lwla/lwla.c
fx2lafw, sysclk-lwla: Avoid g_stat()
[libsigrok.git] / src / hardware / sysclk-lwla / lwla.c
CommitLineData
5874e88d
DE
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
c2066c21 21#include <errno.h>
2a09aac2 22#include <sys/stat.h>
c2066c21 23#include <glib/gstdio.h>
c1aae900 24#include <libsigrok/libsigrok.h>
515ab088
UH
25#include "libsigrok-internal.h"
26#include "protocol.h"
27#include "lwla.h"
5874e88d 28
1a46cc62
UH
29#define BITSTREAM_MAX_SIZE (256 * 1024) /* bitstream size limit for safety */
30#define BITSTREAM_HEADER_SIZE 4 /* transfer header size in bytes */
c2066c21
DE
31
32/* Load a bitstream file into memory. Returns a newly allocated array
33 * consisting of a 32-bit length field followed by the bitstream data.
34 */
35static unsigned char *load_bitstream_file(const char *filename, int *length_p)
36{
2a09aac2 37 struct stat statbuf;
c2066c21
DE
38 FILE *file;
39 unsigned char *stream;
40 size_t length, count;
41
42 /* Retrieve and validate the file size. */
2a09aac2 43 if (stat(filename, &statbuf) < 0) {
c2066c21
DE
44 sr_err("Failed to access bitstream file: %s.",
45 g_strerror(errno));
46 return NULL;
47 }
48 if (!S_ISREG(statbuf.st_mode)) {
49 sr_err("Bitstream is not a regular file.");
50 return NULL;
51 }
52 if (statbuf.st_size <= 0 || statbuf.st_size > BITSTREAM_MAX_SIZE) {
53 sr_err("Refusing to load bitstream of unreasonable size "
54 "(%" PRIu64 " bytes).", (uint64_t)statbuf.st_size);
55 return NULL;
56 }
57
58 /* The message length includes the 4-byte header. */
59 length = BITSTREAM_HEADER_SIZE + statbuf.st_size;
60 stream = g_try_malloc(length);
61 if (!stream) {
62 sr_err("Failed to allocate bitstream buffer.");
63 return NULL;
64 }
65
66 file = g_fopen(filename, "rb");
67 if (!file) {
68 sr_err("Failed to open bitstream file: %s.", g_strerror(errno));
69 g_free(stream);
70 return NULL;
71 }
72
73 /* Write the message length header. */
74 *(uint32_t *)stream = GUINT32_TO_BE(length);
75
76 count = fread(stream + BITSTREAM_HEADER_SIZE,
77 length - BITSTREAM_HEADER_SIZE, 1, file);
78 if (count != 1) {
79 sr_err("Failed to read bitstream file: %s.", g_strerror(errno));
80 fclose(file);
81 g_free(stream);
82 return NULL;
83 }
84 fclose(file);
85
86 *length_p = length;
87 return stream;
88}
89
90/* Load a Raw Binary File (.rbf) from the firmware directory and transfer
91 * it to the device.
92 */
5874e88d 93SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb,
c2066c21 94 const char *basename)
5874e88d 95{
c2066c21
DE
96 char *filename;
97 unsigned char *stream;
5874e88d 98 int ret;
c2066c21 99 int length;
5874e88d
DE
100 int xfer_len;
101
c2066c21
DE
102 if (!usb || !basename)
103 return SR_ERR_BUG;
5874e88d 104
c2066c21 105 filename = g_build_filename(FIRMWARE_DIR, basename, NULL);
5874e88d
DE
106 sr_info("Downloading FPGA bitstream at '%s'.", filename);
107
c2066c21
DE
108 stream = load_bitstream_file(filename, &length);
109 g_free(filename);
5874e88d 110
c2066c21 111 if (!stream)
5874e88d 112 return SR_ERR;
5874e88d
DE
113
114 /* Transfer the entire bitstream in one URB. */
115 ret = libusb_bulk_transfer(usb->devhdl, EP_BITSTREAM,
1a46cc62 116 stream, length, &xfer_len, USB_TIMEOUT_MS);
c2066c21 117 g_free(stream);
313c7a7d 118
5874e88d
DE
119 if (ret != 0) {
120 sr_err("Failed to transfer bitstream: %s.",
121 libusb_error_name(ret));
5874e88d
DE
122 return SR_ERR;
123 }
c2066c21 124 if (xfer_len != length) {
5874e88d 125 sr_err("Failed to transfer bitstream: incorrect length "
c2066c21 126 "%d != %d.", xfer_len, length);
5874e88d
DE
127 return SR_ERR;
128 }
5874e88d
DE
129 sr_info("FPGA bitstream download of %d bytes done.", xfer_len);
130
131 /* This delay appears to be necessary for reliable operation. */
1a46cc62 132 g_usleep(30 * 1000);
5874e88d
DE
133
134 return SR_OK;
135}
136
137SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
138 const uint16_t *command, int cmd_len)
139{
140 int ret;
313c7a7d 141 int xfer_len;
5874e88d 142
5413df19
DE
143 if (!usb || !command || cmd_len <= 0)
144 return SR_ERR_BUG;
5874e88d 145
313c7a7d 146 xfer_len = 0;
5874e88d
DE
147 ret = libusb_bulk_transfer(usb->devhdl, EP_COMMAND,
148 (unsigned char *)command, cmd_len * 2,
1a46cc62 149 &xfer_len, USB_TIMEOUT_MS);
5874e88d 150 if (ret != 0) {
e0df15d4
DE
151 sr_dbg("Failed to send command %d: %s.",
152 LWLA_TO_UINT16(command[0]), libusb_error_name(ret));
5874e88d
DE
153 return SR_ERR;
154 }
155 if (xfer_len != cmd_len * 2) {
e0df15d4
DE
156 sr_dbg("Failed to send command %d: incorrect length %d != %d.",
157 LWLA_TO_UINT16(command[0]), xfer_len, cmd_len * 2);
5874e88d
DE
158 return SR_ERR;
159 }
160 return SR_OK;
161}
162
163SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb,
e0df15d4 164 uint32_t *reply, int reply_len, int expect_len)
5874e88d
DE
165{
166 int ret;
313c7a7d 167 int xfer_len;
5874e88d 168
5413df19
DE
169 if (!usb || !reply || reply_len <= 0)
170 return SR_ERR_BUG;
5874e88d 171
313c7a7d 172 xfer_len = 0;
5874e88d 173 ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY,
e0df15d4 174 (unsigned char *)reply, reply_len * 4,
1a46cc62 175 &xfer_len, USB_TIMEOUT_MS);
5874e88d
DE
176 if (ret != 0) {
177 sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret));
178 return SR_ERR;
179 }
e0df15d4 180 if (xfer_len != expect_len * 4) {
5874e88d 181 sr_dbg("Failed to receive reply: incorrect length %d != %d.",
e0df15d4 182 xfer_len, expect_len * 4);
5874e88d
DE
183 return SR_ERR;
184 }
185 return SR_OK;
186}
187
188SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
189 uint16_t reg, uint32_t *value)
190{
191 int ret;
192 uint16_t command[2];
e0df15d4 193 uint32_t reply[128]; /* full EP buffer to avoid overflows */
5874e88d
DE
194
195 command[0] = LWLA_WORD(CMD_READ_REG);
196 command[1] = LWLA_WORD(reg);
197
ce3ecb70 198 ret = lwla_send_command(usb, command, ARRAY_SIZE(command));
5874e88d
DE
199
200 if (ret != SR_OK)
201 return ret;
202
ce3ecb70 203 ret = lwla_receive_reply(usb, reply, ARRAY_SIZE(reply), 1);
5874e88d
DE
204
205 if (ret == SR_OK)
e0df15d4 206 *value = LWLA_TO_UINT32(reply[0]);
5874e88d
DE
207
208 return ret;
209}
210
211SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
212 uint16_t reg, uint32_t value)
213{
214 uint16_t command[4];
215
216 command[0] = LWLA_WORD(CMD_WRITE_REG);
217 command[1] = LWLA_WORD(reg);
218 command[2] = LWLA_WORD_0(value);
219 command[3] = LWLA_WORD_1(value);
220
ce3ecb70 221 return lwla_send_command(usb, command, ARRAY_SIZE(command));
5874e88d
DE
222}
223
224SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
225 const struct regval_pair *regvals, int count)
226{
227 int i;
228 int ret;
229
230 ret = SR_OK;
231
232 for (i = 0; i < count; ++i) {
233 ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val);
234
235 if (ret != SR_OK)
236 break;
237 }
238
239 return ret;
240}