]> sigrok.org Git - libsigrok.git/blob - hardware/sysclk-lwla/lwla.c
sysclk-lwla: Limit use of SR_ERR_ARG to user-supplied arguments.
[libsigrok.git] / hardware / sysclk-lwla / lwla.c
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
20 #include "lwla.h"
21 #include "protocol.h"
22 #include "libsigrok-internal.h"
23 #include <errno.h>
24 #include <glib/gstdio.h>
25
26 #define BITSTREAM_MAX_SIZE      262144  /* bitstream size limit for safety */
27 #define BITSTREAM_HEADER_SIZE   4       /* transfer header size in bytes */
28
29 /* Load a bitstream file into memory.  Returns a newly allocated array
30  * consisting of a 32-bit length field followed by the bitstream data.
31  */
32 static unsigned char *load_bitstream_file(const char *filename, int *length_p)
33 {
34         GStatBuf statbuf;
35         FILE *file;
36         unsigned char *stream;
37         size_t length, count;
38
39         /* Retrieve and validate the file size. */
40         if (g_stat(filename, &statbuf) < 0) {
41                 sr_err("Failed to access bitstream file: %s.",
42                        g_strerror(errno));
43                 return NULL;
44         }
45         if (!S_ISREG(statbuf.st_mode)) {
46                 sr_err("Bitstream is not a regular file.");
47                 return NULL;
48         }
49         if (statbuf.st_size <= 0 || statbuf.st_size > BITSTREAM_MAX_SIZE) {
50                 sr_err("Refusing to load bitstream of unreasonable size "
51                        "(%" PRIu64 " bytes).", (uint64_t)statbuf.st_size);
52                 return NULL;
53         }
54
55         /* The message length includes the 4-byte header. */
56         length = BITSTREAM_HEADER_SIZE + statbuf.st_size;
57         stream = g_try_malloc(length);
58         if (!stream) {
59                 sr_err("Failed to allocate bitstream buffer.");
60                 return NULL;
61         }
62
63         file = g_fopen(filename, "rb");
64         if (!file) {
65                 sr_err("Failed to open bitstream file: %s.", g_strerror(errno));
66                 g_free(stream);
67                 return NULL;
68         }
69
70         /* Write the message length header. */
71         *(uint32_t *)stream = GUINT32_TO_BE(length);
72
73         count = fread(stream + BITSTREAM_HEADER_SIZE,
74                       length - BITSTREAM_HEADER_SIZE, 1, file);
75         if (count != 1) {
76                 sr_err("Failed to read bitstream file: %s.", g_strerror(errno));
77                 fclose(file);
78                 g_free(stream);
79                 return NULL;
80         }
81         fclose(file);
82
83         *length_p = length;
84         return stream;
85 }
86
87 /* Load a Raw Binary File (.rbf) from the firmware directory and transfer
88  * it to the device.
89  */
90 SR_PRIV int lwla_send_bitstream(const struct sr_usb_dev_inst *usb,
91                                 const char *basename)
92 {
93         char *filename;
94         unsigned char *stream;
95         int ret;
96         int length;
97         int xfer_len;
98
99         if (!usb || !basename)
100                 return SR_ERR_BUG;
101
102         filename = g_build_filename(FIRMWARE_DIR, basename, NULL);
103         sr_info("Downloading FPGA bitstream at '%s'.", filename);
104
105         stream = load_bitstream_file(filename, &length);
106         g_free(filename);
107
108         if (!stream)
109                 return SR_ERR;
110
111         /* Transfer the entire bitstream in one URB. */
112         ret = libusb_bulk_transfer(usb->devhdl, EP_BITSTREAM,
113                                    stream, length, &xfer_len, USB_TIMEOUT);
114         g_free(stream);
115
116         if (ret != 0) {
117                 sr_err("Failed to transfer bitstream: %s.",
118                        libusb_error_name(ret));
119                 return SR_ERR;
120         }
121         if (xfer_len != length) {
122                 sr_err("Failed to transfer bitstream: incorrect length "
123                        "%d != %d.", xfer_len, length);
124                 return SR_ERR;
125         }
126         sr_info("FPGA bitstream download of %d bytes done.", xfer_len);
127
128         /* This delay appears to be necessary for reliable operation. */
129         g_usleep(30000);
130
131         return SR_OK;
132 }
133
134 SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
135                               const uint16_t *command, int cmd_len)
136 {
137         int ret;
138         int xfer_len;
139
140         if (!usb || !command || cmd_len <= 0)
141                 return SR_ERR_BUG;
142
143         xfer_len = 0;
144         ret = libusb_bulk_transfer(usb->devhdl, EP_COMMAND,
145                                    (unsigned char *)command, cmd_len * 2,
146                                    &xfer_len, USB_TIMEOUT);
147         if (ret != 0) {
148                 sr_dbg("Failed to send command %u: %s.",
149                        LWLA_READ16(command), libusb_error_name(ret));
150                 return SR_ERR;
151         }
152         if (xfer_len != cmd_len * 2) {
153                 sr_dbg("Failed to send command %u: incorrect length %d != %d.",
154                        LWLA_READ16(command), xfer_len, cmd_len * 2);
155                 return SR_ERR;
156         }
157         return SR_OK;
158 }
159
160 SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb,
161                                uint16_t *reply, int reply_len, int expect_len)
162 {
163         int ret;
164         int xfer_len;
165
166         if (!usb || !reply || reply_len <= 0)
167                 return SR_ERR_BUG;
168
169         xfer_len = 0;
170         ret = libusb_bulk_transfer(usb->devhdl, EP_REPLY,
171                                    (unsigned char *)reply, reply_len * 2,
172                                    &xfer_len, USB_TIMEOUT);
173         if (ret != 0) {
174                 sr_dbg("Failed to receive reply: %s.", libusb_error_name(ret));
175                 return SR_ERR;
176         }
177         if (xfer_len != expect_len * 2) {
178                 sr_dbg("Failed to receive reply: incorrect length %d != %d.",
179                        xfer_len, expect_len * 2);
180                 return SR_ERR;
181         }
182         return SR_OK;
183 }
184
185 SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
186                           uint16_t reg, uint32_t *value)
187 {
188         int ret;
189         uint16_t command[2];
190         uint16_t reply[256]; /* full EP buffer to avoid overflows */
191
192         command[0] = LWLA_WORD(CMD_READ_REG);
193         command[1] = LWLA_WORD(reg);
194
195         ret = lwla_send_command(usb, command, G_N_ELEMENTS(command));
196
197         if (ret != SR_OK)
198                 return ret;
199
200         ret = lwla_receive_reply(usb, reply, G_N_ELEMENTS(reply), 2);
201
202         if (ret == SR_OK)
203                 *value = LWLA_READ32(reply);
204
205         return ret;
206 }
207
208 SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
209                            uint16_t reg, uint32_t value)
210 {
211         uint16_t command[4];
212
213         command[0] = LWLA_WORD(CMD_WRITE_REG);
214         command[1] = LWLA_WORD(reg);
215         command[2] = LWLA_WORD_0(value);
216         command[3] = LWLA_WORD_1(value);
217
218         return lwla_send_command(usb, command, G_N_ELEMENTS(command));
219 }
220
221 SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
222                             const struct regval_pair *regvals, int count)
223 {
224         int i;
225         int ret;
226
227         ret = SR_OK;
228
229         for (i = 0; i < count; ++i) {
230                 ret = lwla_write_reg(usb, regvals[i].reg, regvals[i].val);
231
232                 if (ret != SR_OK)
233                         break;
234         }
235
236         return ret;
237 }