]> sigrok.org Git - libsigrok.git/blame - src/hardware/fx2lafw/protocol.c
windows: Fix various compiler warnings.
[libsigrok.git] / src / hardware / fx2lafw / protocol.c
CommitLineData
2f937611
UH
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
a7d7f93c
BV
21#include <glib.h>
22#include <glib/gstdio.h>
2f937611 23#include "protocol.h"
b9d53092 24#include "dslogic.h"
2f937611 25
2f937611
UH
26#pragma pack(push, 1)
27
28struct version_info {
29 uint8_t major;
30 uint8_t minor;
31};
32
33struct cmd_start_acquisition {
34 uint8_t flags;
35 uint8_t sample_delay_h;
36 uint8_t sample_delay_l;
37};
38
39#pragma pack(pop)
40
4df5739a
UH
41#define USB_TIMEOUT 100
42
2f937611
UH
43static int command_get_fw_version(libusb_device_handle *devhdl,
44 struct version_info *vi)
45{
46 int ret;
47
48 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
49 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
4df5739a 50 (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
2f937611
UH
51
52 if (ret < 0) {
53 sr_err("Unable to get version info: %s.",
54 libusb_error_name(ret));
55 return SR_ERR;
56 }
57
58 return SR_OK;
59}
60
a54edb1d 61static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
2f937611 62{
a7d7f93c 63 struct dev_context *devc = sdi->priv;
a54edb1d
ML
64 struct sr_usb_dev_inst *usb = sdi->conn;
65 libusb_device_handle *devhdl = usb->devhdl;
a7d7f93c 66 int cmd, ret;
2f937611 67
b9d53092 68 cmd = devc->dslogic ? DS_CMD_GET_REVID_VERSION : CMD_GET_REVID_VERSION;
2f937611 69 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
4df5739a 70 LIBUSB_ENDPOINT_IN, cmd, 0x0000, 0x0000, revid, 1, USB_TIMEOUT);
2f937611
UH
71
72 if (ret < 0) {
73 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
74 return SR_ERR;
75 }
76
77 return SR_OK;
78}
79
a54edb1d 80SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
2f937611 81{
81a34976
BV
82 struct dev_context *devc;
83 struct sr_usb_dev_inst *usb;
84 uint64_t samplerate;
85 struct cmd_start_acquisition cmd;
86 int delay, ret;
87
88 devc = sdi->priv;
89 usb = sdi->conn;
90 samplerate = devc->cur_samplerate;
2f937611
UH
91
92 /* Compute the sample rate. */
81a34976 93 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
2f937611
UH
94 sr_err("Unable to sample at %" PRIu64 "Hz "
95 "when collecting 16-bit samples.", samplerate);
96 return SR_ERR;
97 }
98
81a34976
BV
99 delay = 0;
100 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
2f937611
UH
101 if ((SR_MHZ(48) % samplerate) == 0) {
102 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
103 delay = SR_MHZ(48) / samplerate - 1;
104 if (delay > MAX_SAMPLE_DELAY)
105 delay = 0;
106 }
107
108 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
109 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
110 delay = SR_MHZ(30) / samplerate - 1;
111 }
112
b9d53092 113 sr_dbg("GPIF delay = %d, clocksource = %sMHz.", delay,
2f937611
UH
114 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
115
116 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
117 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
118 return SR_ERR;
119 }
120
121 cmd.sample_delay_h = (delay >> 8) & 0xff;
122 cmd.sample_delay_l = delay & 0xff;
123
124 /* Select the sampling width. */
81a34976 125 cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
2f937611
UH
126 CMD_START_FLAGS_SAMPLE_8BIT;
127
128 /* Send the control message. */
81a34976 129 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
2f937611 130 LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
4df5739a 131 (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT);
2f937611
UH
132 if (ret < 0) {
133 sr_err("Unable to send start command: %s.",
134 libusb_error_name(ret));
135 return SR_ERR;
136 }
137
138 return SR_OK;
139}
140
141/**
142 * Check the USB configuration to determine if this is an fx2lafw device.
143 *
a7d7f93c 144 * @return TRUE if the device's configuration profile matches fx2lafw
6fcf3f0a 145 * configuration, FALSE otherwise.
2f937611 146 */
a7d7f93c
BV
147SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer,
148 const char *product)
2f937611
UH
149{
150 struct libusb_device_descriptor des;
151 struct libusb_device_handle *hdl;
152 gboolean ret;
153 unsigned char strdesc[64];
154
155 hdl = NULL;
156 ret = FALSE;
157 while (!ret) {
158 /* Assume the FW has not been loaded, unless proven wrong. */
159 if (libusb_get_device_descriptor(dev, &des) != 0)
160 break;
161
162 if (libusb_open(dev, &hdl) != 0)
163 break;
164
165 if (libusb_get_string_descriptor_ascii(hdl,
a7d7f93c 166 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
2f937611 167 break;
bc497772 168 if (strcmp((const char *)strdesc, manufacturer))
2f937611
UH
169 break;
170
171 if (libusb_get_string_descriptor_ascii(hdl,
172 des.iProduct, strdesc, sizeof(strdesc)) < 0)
173 break;
bc497772 174 if (strcmp((const char *)strdesc, product))
2f937611
UH
175 break;
176
2f937611
UH
177 ret = TRUE;
178 }
179 if (hdl)
180 libusb_close(hdl);
181
182 return ret;
183}
184
185SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
186{
187 libusb_device **devlist;
188 struct sr_usb_dev_inst *usb;
189 struct libusb_device_descriptor des;
190 struct dev_context *devc;
191 struct drv_context *drvc;
192 struct version_info vi;
5e2c86eb 193 int ret, i, device_count;
2f937611 194 uint8_t revid;
5e2c86eb 195 char connection_id[64];
2f937611
UH
196
197 drvc = di->priv;
198 devc = sdi->priv;
199 usb = sdi->conn;
200
201 if (sdi->status == SR_ST_ACTIVE)
202 /* Device is already in use. */
203 return SR_ERR;
204
2f937611
UH
205 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
206 if (device_count < 0) {
207 sr_err("Failed to get device list: %s.",
208 libusb_error_name(device_count));
209 return SR_ERR;
210 }
211
212 for (i = 0; i < device_count; i++) {
213 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
214 sr_err("Failed to get device descriptor: %s.",
215 libusb_error_name(ret));
216 continue;
217 }
218
219 if (des.idVendor != devc->profile->vid
220 || des.idProduct != devc->profile->pid)
221 continue;
222
5e2c86eb
SA
223 if ((sdi->status == SR_ST_INITIALIZING) ||
224 (sdi->status == SR_ST_INACTIVE)) {
2f937611 225 /*
5e2c86eb 226 * Check device by its physical USB bus/port address.
2f937611 227 */
5e2c86eb
SA
228 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
229 if (strcmp(sdi->connection_id, connection_id))
2f937611
UH
230 /* This is not the one. */
231 continue;
232 }
233
234 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
235 if (usb->address == 0xff)
236 /*
237 * First time we touch this device after FW
238 * upload, so we don't know the address yet.
239 */
240 usb->address = libusb_get_device_address(devlist[i]);
241 } else {
242 sr_err("Failed to open device: %s.",
243 libusb_error_name(ret));
244 break;
245 }
246
247 ret = command_get_fw_version(usb->devhdl, &vi);
248 if (ret != SR_OK) {
249 sr_err("Failed to get firmware version.");
250 break;
251 }
252
a54edb1d 253 ret = command_get_revid_version(sdi, &revid);
2f937611
UH
254 if (ret != SR_OK) {
255 sr_err("Failed to get REVID.");
256 break;
257 }
258
259 /*
260 * Changes in major version mean incompatible/API changes, so
261 * bail out if we encounter an incompatible version.
262 * Different minor versions are OK, they should be compatible.
263 */
264 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
265 sr_err("Expected firmware version %d.x, "
266 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
267 vi.major, vi.minor);
268 break;
269 }
270
271 sdi->status = SR_ST_ACTIVE;
5e2c86eb 272 sr_info("Opened device on %d.%d (logical) / %s (physical), "
2f937611 273 "interface %d, firmware %d.%d.",
5e2c86eb 274 usb->bus, usb->address, connection_id,
2f937611
UH
275 USB_INTERFACE, vi.major, vi.minor);
276
277 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
278 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
279
280 break;
281 }
282 libusb_free_device_list(devlist, 1);
283
284 if (sdi->status != SR_ST_ACTIVE)
285 return SR_ERR;
286
287 return SR_OK;
288}
289
2f937611
UH
290SR_PRIV struct dev_context *fx2lafw_dev_new(void)
291{
292 struct dev_context *devc;
293
f57d8ffe 294 devc = g_malloc0(sizeof(struct dev_context));
2f937611
UH
295 devc->profile = NULL;
296 devc->fw_updated = 0;
297 devc->cur_samplerate = 0;
298 devc->limit_samples = 0;
7bfcb25c 299 devc->capture_ratio = 0;
87b545fb 300 devc->sample_wide = FALSE;
335122f0 301 devc->stl = NULL;
2f937611
UH
302
303 return devc;
304}
305
306SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
307{
308 int i;
309
b0ccd64d 310 devc->acq_aborted = TRUE;
2f937611
UH
311
312 for (i = devc->num_transfers - 1; i >= 0; i--) {
313 if (devc->transfers[i])
314 libusb_cancel_transfer(devc->transfers[i]);
315 }
316}
317
102f1239 318static void finish_acquisition(struct sr_dev_inst *sdi)
2f937611
UH
319{
320 struct sr_datafeed_packet packet;
102f1239
BV
321 struct dev_context *devc;
322
323 devc = sdi->priv;
2f937611
UH
324
325 /* Terminate session. */
326 packet.type = SR_DF_END;
102f1239 327 sr_session_send(sdi, &packet);
2f937611
UH
328
329 /* Remove fds from polling. */
102f1239 330 usb_source_remove(sdi->session, devc->ctx);
2f937611
UH
331
332 devc->num_transfers = 0;
333 g_free(devc->transfers);
335122f0
BV
334
335 if (devc->stl) {
336 soft_trigger_logic_free(devc->stl);
337 devc->stl = NULL;
338 }
2f937611
UH
339}
340
341static void free_transfer(struct libusb_transfer *transfer)
342{
9615eeb5 343 struct sr_dev_inst *sdi;
2f937611
UH
344 struct dev_context *devc;
345 unsigned int i;
346
9615eeb5
BV
347 sdi = transfer->user_data;
348 devc = sdi->priv;
2f937611
UH
349
350 g_free(transfer->buffer);
351 transfer->buffer = NULL;
352 libusb_free_transfer(transfer);
353
354 for (i = 0; i < devc->num_transfers; i++) {
355 if (devc->transfers[i] == transfer) {
356 devc->transfers[i] = NULL;
357 break;
358 }
359 }
360
361 devc->submitted_transfers--;
362 if (devc->submitted_transfers == 0)
102f1239 363 finish_acquisition(sdi);
2f937611
UH
364}
365
366static void resubmit_transfer(struct libusb_transfer *transfer)
367{
368 int ret;
369
370 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
371 return;
372
9615eeb5 373 sr_err("%s: %s", __func__, libusb_error_name(ret));
2f937611 374 free_transfer(transfer);
2f937611 375
9615eeb5
BV
376}
377
55462b8b 378SR_PRIV void LIBUSB_CALL fx2lafw_receive_transfer(struct libusb_transfer *transfer)
2f937611 379{
9615eeb5
BV
380 struct sr_dev_inst *sdi;
381 struct dev_context *devc;
2f937611
UH
382 gboolean packet_has_error = FALSE;
383 struct sr_datafeed_packet packet;
384 struct sr_datafeed_logic logic;
9615eeb5
BV
385 unsigned int num_samples;
386 int trigger_offset, cur_sample_count, unitsize;
7bfcb25c 387 int pre_trigger_samples;
2f937611 388
9615eeb5
BV
389 sdi = transfer->user_data;
390 devc = sdi->priv;
2f937611
UH
391
392 /*
393 * If acquisition has already ended, just free any queued up
394 * transfer that come in.
395 */
b0ccd64d 396 if (devc->acq_aborted) {
2f937611
UH
397 free_transfer(transfer);
398 return;
399 }
400
0f262763
UH
401 sr_dbg("receive_transfer(): status %s received %d bytes.",
402 libusb_error_name(transfer->status), transfer->actual_length);
2f937611
UH
403
404 /* Save incoming transfer before reusing the transfer struct. */
9615eeb5
BV
405 unitsize = devc->sample_wide ? 2 : 1;
406 cur_sample_count = transfer->actual_length / unitsize;
2f937611
UH
407
408 switch (transfer->status) {
409 case LIBUSB_TRANSFER_NO_DEVICE:
410 fx2lafw_abort_acquisition(devc);
411 free_transfer(transfer);
412 return;
413 case LIBUSB_TRANSFER_COMPLETED:
414 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
415 break;
416 default:
417 packet_has_error = TRUE;
418 break;
419 }
420
421 if (transfer->actual_length == 0 || packet_has_error) {
422 devc->empty_transfer_count++;
423 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
424 /*
425 * The FX2 gave up. End the acquisition, the frontend
426 * will work out that the samplecount is short.
427 */
428 fx2lafw_abort_acquisition(devc);
429 free_transfer(transfer);
430 } else {
431 resubmit_transfer(transfer);
432 }
433 return;
434 } else {
435 devc->empty_transfer_count = 0;
436 }
437
9615eeb5 438 if (devc->trigger_fired) {
2f663c82 439 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
9615eeb5
BV
440 /* Send the incoming transfer to the session bus. */
441 packet.type = SR_DF_LOGIC;
442 packet.payload = &logic;
2f663c82 443 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
9615eeb5
BV
444 num_samples = devc->limit_samples - devc->sent_samples;
445 else
446 num_samples = cur_sample_count;
447 logic.length = num_samples * unitsize;
448 logic.unitsize = unitsize;
449 logic.data = transfer->buffer;
450 sr_session_send(devc->cb_data, &packet);
649e9e16 451 devc->sent_samples += num_samples;
9615eeb5
BV
452 }
453 } else {
335122f0 454 trigger_offset = soft_trigger_logic_check(devc->stl,
7bfcb25c 455 transfer->buffer, transfer->actual_length, &pre_trigger_samples);
9615eeb5 456 if (trigger_offset > -1) {
7bfcb25c 457 devc->sent_samples += pre_trigger_samples;
9615eeb5
BV
458 packet.type = SR_DF_LOGIC;
459 packet.payload = &logic;
460 num_samples = cur_sample_count - trigger_offset;
461 if (devc->limit_samples &&
462 num_samples > devc->limit_samples - devc->sent_samples)
463 num_samples = devc->limit_samples - devc->sent_samples;
464 logic.length = num_samples * unitsize;
465 logic.unitsize = unitsize;
466 logic.data = transfer->buffer + trigger_offset * unitsize;
467 sr_session_send(devc->cb_data, &packet);
468 devc->sent_samples += num_samples;
469
470 devc->trigger_fired = TRUE;
2f937611 471 }
06b5d7f7
BV
472 }
473
474 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
475 fx2lafw_abort_acquisition(devc);
476 free_transfer(transfer);
9615eeb5
BV
477 } else
478 resubmit_transfer(transfer);
2f937611
UH
479}
480
481static unsigned int to_bytes_per_ms(unsigned int samplerate)
482{
483 return samplerate / 1000;
484}
485
486SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
487{
488 size_t s;
489
490 /*
491 * The buffer should be large enough to hold 10ms of data and
492 * a multiple of 512.
493 */
494 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
495 return (s + 511) & ~511;
496}
497
498SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
499{
500 unsigned int n;
501
502 /* Total buffer size should be able to hold about 500ms of data. */
503 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
504 fx2lafw_get_buffer_size(devc));
505
506 if (n > NUM_SIMUL_TRANSFERS)
507 return NUM_SIMUL_TRANSFERS;
508
509 return n;
510}
511
512SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
513{
514 size_t total_size;
515 unsigned int timeout;
516
517 total_size = fx2lafw_get_buffer_size(devc) *
518 fx2lafw_get_number_of_transfers(devc);
519 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
520 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
521}