]> sigrok.org Git - libsigrok.git/blame - src/hardware/dreamsourcelab-dslogic/protocol.c
dreamsourcelab-dslogic: Fixed trigger-stages field in trig_glb
[libsigrok.git] / src / hardware / dreamsourcelab-dslogic / protocol.c
CommitLineData
adcb9951
JH
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 <config.h>
4bd770f5 22#include <math.h>
adcb9951
JH
23#include <glib.h>
24#include <glib/gstdio.h>
25#include "protocol.h"
4bd770f5
JH
26
27#define DS_CMD_GET_FW_VERSION 0xb0
28#define DS_CMD_GET_REVID_VERSION 0xb1
29#define DS_CMD_START 0xb2
30#define DS_CMD_CONFIG 0xb3
31#define DS_CMD_SETTING 0xb4
32#define DS_CMD_CONTROL 0xb5
33#define DS_CMD_STATUS 0xb6
34#define DS_CMD_STATUS_INFO 0xb7
35#define DS_CMD_WR_REG 0xb8
36#define DS_CMD_WR_NVM 0xb9
37#define DS_CMD_RD_NVM 0xba
38#define DS_CMD_RD_NVM_PRE 0xbb
39#define DS_CMD_GET_HW_INFO 0xbc
40
41#define DS_START_FLAGS_STOP (1 << 7)
42#define DS_START_FLAGS_CLK_48MHZ (1 << 6)
43#define DS_START_FLAGS_SAMPLE_WIDE (1 << 5)
44#define DS_START_FLAGS_MODE_LA (1 << 4)
45
46#define DS_ADDR_COMB 0x68
47#define DS_ADDR_EEWP 0x70
48#define DS_ADDR_VTH 0x78
49
50#define DS_MAX_LOGIC_DEPTH SR_MHZ(16)
51#define DS_MAX_LOGIC_SAMPLERATE SR_MHZ(100)
52#define DS_MAX_TRIG_PERCENT 90
53
54#define DS_MODE_TRIG_EN (1 << 0)
55#define DS_MODE_CLK_TYPE (1 << 1)
56#define DS_MODE_CLK_EDGE (1 << 2)
57#define DS_MODE_RLE_MODE (1 << 3)
58#define DS_MODE_DSO_MODE (1 << 4)
59#define DS_MODE_HALF_MODE (1 << 5)
60#define DS_MODE_QUAR_MODE (1 << 6)
61#define DS_MODE_ANALOG_MODE (1 << 7)
62#define DS_MODE_FILTER (1 << 8)
63#define DS_MODE_INSTANT (1 << 9)
64#define DS_MODE_STRIG_MODE (1 << 11)
65#define DS_MODE_STREAM_MODE (1 << 12)
66#define DS_MODE_LPB_TEST (1 << 13)
67#define DS_MODE_EXT_TEST (1 << 14)
68#define DS_MODE_INT_TEST (1 << 15)
69
03a0002e
JH
70#define DSLOGIC_ATOMIC_SAMPLES (sizeof(uint64_t) * 8)
71#define DSLOGIC_ATOMIC_BYTES sizeof(uint64_t)
4bd770f5
JH
72
73/*
74 * The FPGA is configured with TLV tuples. Length is specified as the
75 * number of 16-bit words.
76 */
77#define _DS_CFG(variable, wordcnt) ((variable << 8) | wordcnt)
44b46d70
UH
78#define DS_CFG_START 0xf5a5f5a5
79#define DS_CFG_MODE _DS_CFG(0, 1)
80#define DS_CFG_DIVIDER _DS_CFG(1, 2)
81#define DS_CFG_COUNT _DS_CFG(3, 2)
82#define DS_CFG_TRIG_POS _DS_CFG(5, 2)
83#define DS_CFG_TRIG_GLB _DS_CFG(7, 1)
84#define DS_CFG_CH_EN _DS_CFG(8, 1)
85#define DS_CFG_TRIG _DS_CFG(64, 160)
86#define DS_CFG_END 0xfa5afa5a
adcb9951
JH
87
88#pragma pack(push, 1)
89
90struct version_info {
91 uint8_t major;
92 uint8_t minor;
93};
94
95struct cmd_start_acquisition {
96 uint8_t flags;
97 uint8_t sample_delay_h;
98 uint8_t sample_delay_l;
99};
100
4b25cbff 101struct fpga_config {
4bd770f5
JH
102 uint32_t sync;
103
104 uint16_t mode_header;
105 uint16_t mode;
106 uint16_t divider_header;
107 uint32_t divider;
108 uint16_t count_header;
109 uint32_t count;
110 uint16_t trig_pos_header;
111 uint32_t trig_pos;
112 uint16_t trig_glb_header;
113 uint16_t trig_glb;
114 uint16_t ch_en_header;
115 uint16_t ch_en;
116
117 uint16_t trig_header;
118 uint16_t trig_mask0[NUM_TRIGGER_STAGES];
119 uint16_t trig_mask1[NUM_TRIGGER_STAGES];
120 uint16_t trig_value0[NUM_TRIGGER_STAGES];
121 uint16_t trig_value1[NUM_TRIGGER_STAGES];
122 uint16_t trig_edge0[NUM_TRIGGER_STAGES];
123 uint16_t trig_edge1[NUM_TRIGGER_STAGES];
124 uint16_t trig_logic0[NUM_TRIGGER_STAGES];
125 uint16_t trig_logic1[NUM_TRIGGER_STAGES];
126 uint32_t trig_count[NUM_TRIGGER_STAGES];
127
128 uint32_t end_sync;
129};
130
adcb9951
JH
131#pragma pack(pop)
132
4bd770f5
JH
133/*
134 * This should be larger than the FPGA bitstream image so that it'll get
135 * uploaded in one big operation. There seem to be issues when uploading
136 * it in chunks.
137 */
138#define FW_BUFSIZE (1024 * 1024)
139
140#define FPGA_UPLOAD_DELAY (10 * 1000)
141
142#define USB_TIMEOUT (3 * 1000)
adcb9951
JH
143
144static int command_get_fw_version(libusb_device_handle *devhdl,
145 struct version_info *vi)
146{
147 int ret;
148
149 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
150 LIBUSB_ENDPOINT_IN, DS_CMD_GET_FW_VERSION, 0x0000, 0x0000,
151 (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT);
152
153 if (ret < 0) {
154 sr_err("Unable to get version info: %s.",
155 libusb_error_name(ret));
156 return SR_ERR;
157 }
158
159 return SR_OK;
160}
161
162static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
163{
164 struct sr_usb_dev_inst *usb = sdi->conn;
165 libusb_device_handle *devhdl = usb->devhdl;
166 int ret;
167
168 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
169 LIBUSB_ENDPOINT_IN, DS_CMD_GET_REVID_VERSION, 0x0000, 0x0000,
170 revid, 1, USB_TIMEOUT);
171
172 if (ret < 0) {
173 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
174 return SR_ERR;
175 }
176
177 return SR_OK;
178}
179
4bd770f5
JH
180static int command_start_acquisition(const struct sr_dev_inst *sdi)
181{
182 struct sr_usb_dev_inst *usb;
183 struct dslogic_mode mode;
184 int ret;
185
186 mode.flags = DS_START_FLAGS_MODE_LA | DS_START_FLAGS_SAMPLE_WIDE;
187 mode.sample_delay_h = mode.sample_delay_l = 0;
188
189 usb = sdi->conn;
190 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
191 LIBUSB_ENDPOINT_OUT, DS_CMD_START, 0x0000, 0x0000,
192 (unsigned char *)&mode, sizeof(mode), USB_TIMEOUT);
193 if (ret < 0) {
194 sr_err("Failed to send start command: %s.", libusb_error_name(ret));
195 return SR_ERR;
196 }
197
198 return SR_OK;
199}
200
201static int command_stop_acquisition(const struct sr_dev_inst *sdi)
202{
203 struct sr_usb_dev_inst *usb;
204 struct dslogic_mode mode;
205 int ret;
206
207 mode.flags = DS_START_FLAGS_STOP;
208 mode.sample_delay_h = mode.sample_delay_l = 0;
209
210 usb = sdi->conn;
211 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
212 LIBUSB_ENDPOINT_OUT, DS_CMD_START, 0x0000, 0x0000,
213 (unsigned char *)&mode, sizeof(struct dslogic_mode), USB_TIMEOUT);
214 if (ret < 0) {
215 sr_err("Failed to send stop command: %s.", libusb_error_name(ret));
216 return SR_ERR;
217 }
218
219 return SR_OK;
220}
221
222SR_PRIV int dslogic_fpga_firmware_upload(const struct sr_dev_inst *sdi)
223{
224 const char *name = NULL;
225 uint64_t sum;
226 struct sr_resource bitstream;
227 struct drv_context *drvc;
228 struct dev_context *devc;
229 struct sr_usb_dev_inst *usb;
230 unsigned char *buf;
231 ssize_t chunksize;
232 int transferred;
233 int result, ret;
234 const uint8_t cmd[3] = {0, 0, 0};
235
236 drvc = sdi->driver->context;
237 devc = sdi->priv;
238 usb = sdi->conn;
239
240 if (!strcmp(devc->profile->model, "DSLogic")) {
241 if (devc->cur_threshold < 1.40)
242 name = DSLOGIC_FPGA_FIRMWARE_3V3;
243 else
244 name = DSLOGIC_FPGA_FIRMWARE_5V;
245 } else if (!strcmp(devc->profile->model, "DSLogic Pro")){
246 name = DSLOGIC_PRO_FPGA_FIRMWARE;
247 } else if (!strcmp(devc->profile->model, "DSLogic Plus")){
248 name = DSLOGIC_PLUS_FPGA_FIRMWARE;
249 } else if (!strcmp(devc->profile->model, "DSLogic Basic")){
250 name = DSLOGIC_BASIC_FPGA_FIRMWARE;
251 } else if (!strcmp(devc->profile->model, "DSCope")) {
252 name = DSCOPE_FPGA_FIRMWARE;
253 } else {
254 sr_err("Failed to select FPGA firmware.");
255 return SR_ERR;
256 }
257
258 sr_dbg("Uploading FPGA firmware '%s'.", name);
259
260 result = sr_resource_open(drvc->sr_ctx, &bitstream,
261 SR_RESOURCE_FIRMWARE, name);
262 if (result != SR_OK)
263 return result;
264
265 /* Tell the device firmware is coming. */
266 if ((ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
267 LIBUSB_ENDPOINT_OUT, DS_CMD_CONFIG, 0x0000, 0x0000,
268 (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT)) < 0) {
269 sr_err("Failed to upload FPGA firmware: %s.", libusb_error_name(ret));
270 sr_resource_close(drvc->sr_ctx, &bitstream);
271 return SR_ERR;
272 }
273
274 /* Give the FX2 time to get ready for FPGA firmware upload. */
275 g_usleep(FPGA_UPLOAD_DELAY);
276
277 buf = g_malloc(FW_BUFSIZE);
278 sum = 0;
279 result = SR_OK;
280 while (1) {
281 chunksize = sr_resource_read(drvc->sr_ctx, &bitstream,
282 buf, FW_BUFSIZE);
283 if (chunksize < 0)
284 result = SR_ERR;
285 if (chunksize <= 0)
286 break;
287
288 if ((ret = libusb_bulk_transfer(usb->devhdl, 2 | LIBUSB_ENDPOINT_OUT,
289 buf, chunksize, &transferred, USB_TIMEOUT)) < 0) {
290 sr_err("Unable to configure FPGA firmware: %s.",
291 libusb_error_name(ret));
292 result = SR_ERR;
293 break;
294 }
295 sum += transferred;
296 sr_spew("Uploaded %" PRIu64 "/%" PRIu64 " bytes.",
297 sum, bitstream.size);
298
299 if (transferred != chunksize) {
300 sr_err("Short transfer while uploading FPGA firmware.");
301 result = SR_ERR;
302 break;
303 }
304 }
305 g_free(buf);
306 sr_resource_close(drvc->sr_ctx, &bitstream);
307
308 if (result == SR_OK)
309 sr_dbg("FPGA firmware upload done.");
310
311 return result;
312}
313
6dfa2c39
JH
314static unsigned int enabled_channel_count(const struct sr_dev_inst *sdi)
315{
316 unsigned int count = 0;
317 for (const GSList *l = sdi->channels; l; l = l->next) {
318 const struct sr_channel *const probe = (struct sr_channel *)l->data;
319 if (probe->enabled)
320 count++;
321 }
322 return count;
323}
324
325static uint16_t enabled_channel_mask(const struct sr_dev_inst *sdi)
326{
327 unsigned int mask = 0;
328 for (const GSList *l = sdi->channels; l; l = l->next) {
329 const struct sr_channel *const probe = (struct sr_channel *)l->data;
330 if (probe->enabled)
331 mask |= 1 << probe->index;
332 }
333 return mask;
334}
335
4bd770f5
JH
336/*
337 * Get the session trigger and configure the FPGA structure
338 * accordingly.
339 */
4b25cbff 340static void set_trigger(const struct sr_dev_inst *sdi, struct fpga_config *cfg)
4bd770f5
JH
341{
342 struct sr_trigger *trigger;
343 struct sr_trigger_stage *stage;
344 struct sr_trigger_match *match;
345 struct dev_context *devc;
346 const GSList *l, *m;
6dfa2c39
JH
347 const unsigned int num_enabled_channels = enabled_channel_count(sdi);
348 int num_trigger_stages = 0;
349
4bd770f5
JH
350 int channelbit, i = 0;
351 uint32_t trigger_point;
352
353 devc = sdi->priv;
354
6dfa2c39 355 cfg->ch_en = enabled_channel_mask(sdi);
4bd770f5 356
b23ecd6c 357 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
4bd770f5
JH
358 cfg->trig_mask0[i] = 0xffff;
359 cfg->trig_mask1[i] = 0xffff;
360 cfg->trig_value0[i] = 0;
361 cfg->trig_value1[i] = 0;
362 cfg->trig_edge0[i] = 0;
363 cfg->trig_edge1[i] = 0;
364 cfg->trig_logic0[i] = 2;
365 cfg->trig_logic1[i] = 2;
366 cfg->trig_count[i] = 0;
367 }
368
369 trigger_point = (devc->capture_ratio * devc->limit_samples) / 100;
370 if (trigger_point < DSLOGIC_ATOMIC_SAMPLES)
371 trigger_point = DSLOGIC_ATOMIC_SAMPLES;
372 const uint32_t mem_depth = devc->profile->mem_depth;
373 const uint32_t max_trigger_point = devc->continuous_mode ? ((mem_depth * 10) / 100) :
374 ((mem_depth * DS_MAX_TRIG_PERCENT) / 100);
375 if (trigger_point > max_trigger_point)
376 trigger_point = max_trigger_point;
377 cfg->trig_pos = trigger_point & ~(DSLOGIC_ATOMIC_SAMPLES - 1);
378
379 if (!(trigger = sr_session_trigger_get(sdi->session))) {
380 sr_dbg("No session trigger found");
381 return;
382 }
383
384 for (l = trigger->stages; l; l = l->next) {
385 stage = l->data;
386 num_trigger_stages++;
387 for (m = stage->matches; m; m = m->next) {
388 match = m->data;
389 if (!match->channel->enabled)
390 /* Ignore disabled channels with a trigger. */
391 continue;
392 channelbit = 1 << (match->channel->index);
393 /* Simple trigger support (event). */
394 if (match->match == SR_TRIGGER_ONE) {
395 cfg->trig_mask0[0] &= ~channelbit;
396 cfg->trig_mask1[0] &= ~channelbit;
397 cfg->trig_value0[0] |= channelbit;
398 cfg->trig_value1[0] |= channelbit;
399 } else if (match->match == SR_TRIGGER_ZERO) {
400 cfg->trig_mask0[0] &= ~channelbit;
401 cfg->trig_mask1[0] &= ~channelbit;
402 } else if (match->match == SR_TRIGGER_FALLING) {
403 cfg->trig_mask0[0] &= ~channelbit;
404 cfg->trig_mask1[0] &= ~channelbit;
405 cfg->trig_edge0[0] |= channelbit;
406 cfg->trig_edge1[0] |= channelbit;
407 } else if (match->match == SR_TRIGGER_RISING) {
408 cfg->trig_mask0[0] &= ~channelbit;
409 cfg->trig_mask1[0] &= ~channelbit;
410 cfg->trig_value0[0] |= channelbit;
411 cfg->trig_value1[0] |= channelbit;
412 cfg->trig_edge0[0] |= channelbit;
413 cfg->trig_edge1[0] |= channelbit;
414 } else if (match->match == SR_TRIGGER_EDGE) {
415 cfg->trig_edge0[0] |= channelbit;
416 cfg->trig_edge1[0] |= channelbit;
417 }
418 }
419 }
420
b7a3d79e 421 cfg->trig_glb = (num_enabled_channels << 4) | (num_trigger_stages - 1);
4bd770f5
JH
422}
423
424static int fpga_configure(const struct sr_dev_inst *sdi)
425{
426 struct dev_context *devc;
427 struct sr_usb_dev_inst *usb;
428 uint8_t c[3];
4b25cbff 429 struct fpga_config cfg;
4bd770f5
JH
430 uint16_t v16;
431 uint32_t v32;
432 int transferred, len, ret;
433
434 sr_dbg("Configuring FPGA.");
435
436 usb = sdi->conn;
437 devc = sdi->priv;
438
439 WL32(&cfg.sync, DS_CFG_START);
440 WL16(&cfg.mode_header, DS_CFG_MODE);
441 WL16(&cfg.divider_header, DS_CFG_DIVIDER);
442 WL16(&cfg.count_header, DS_CFG_COUNT);
443 WL16(&cfg.trig_pos_header, DS_CFG_TRIG_POS);
444 WL16(&cfg.trig_glb_header, DS_CFG_TRIG_GLB);
445 WL16(&cfg.ch_en_header, DS_CFG_CH_EN);
446 WL16(&cfg.trig_header, DS_CFG_TRIG);
447 WL32(&cfg.end_sync, DS_CFG_END);
448
449 /* Pass in the length of a fixed-size struct. Really. */
4b25cbff 450 len = sizeof(struct fpga_config) / 2;
4bd770f5
JH
451 c[0] = len & 0xff;
452 c[1] = (len >> 8) & 0xff;
453 c[2] = (len >> 16) & 0xff;
454
455 ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
456 LIBUSB_ENDPOINT_OUT, DS_CMD_SETTING, 0x0000, 0x0000,
457 c, sizeof(c), USB_TIMEOUT);
458 if (ret < 0) {
459 sr_err("Failed to send FPGA configure command: %s.",
460 libusb_error_name(ret));
461 return SR_ERR;
462 }
463
464 v16 = 0x0000;
465
466 if (devc->mode == DS_OP_INTERNAL_TEST)
467 v16 = DS_MODE_INT_TEST;
468 else if (devc->mode == DS_OP_EXTERNAL_TEST)
469 v16 = DS_MODE_EXT_TEST;
470 else if (devc->mode == DS_OP_LOOPBACK_TEST)
471 v16 = DS_MODE_LPB_TEST;
472
473 if (devc->cur_samplerate == DS_MAX_LOGIC_SAMPLERATE * 2)
474 v16 |= DS_MODE_HALF_MODE;
475 else if (devc->cur_samplerate == DS_MAX_LOGIC_SAMPLERATE * 4)
476 v16 |= DS_MODE_QUAR_MODE;
477
478 if (devc->continuous_mode)
479 v16 |= DS_MODE_STREAM_MODE;
480 if (devc->external_clock) {
481 v16 |= DS_MODE_CLK_TYPE;
482 if (devc->clock_edge == DS_EDGE_FALLING)
483 v16 |= DS_MODE_CLK_EDGE;
484 }
485 if (devc->limit_samples > DS_MAX_LOGIC_DEPTH *
486 ceil(devc->cur_samplerate * 1.0 / DS_MAX_LOGIC_SAMPLERATE)
487 && !devc->continuous_mode) {
488 /* Enable RLE for long captures.
489 * Without this, captured data present errors.
490 */
491 v16 |= DS_MODE_RLE_MODE;
492 }
493
494 WL16(&cfg.mode, v16);
495 v32 = ceil(DS_MAX_LOGIC_SAMPLERATE * 1.0 / devc->cur_samplerate);
496 WL32(&cfg.divider, v32);
497
498 /* Number of 16-sample units. */
499 WL32(&cfg.count, devc->limit_samples / 16);
500
501 set_trigger(sdi, &cfg);
502
4b25cbff 503 len = sizeof(struct fpga_config);
4bd770f5
JH
504 ret = libusb_bulk_transfer(usb->devhdl, 2 | LIBUSB_ENDPOINT_OUT,
505 (unsigned char *)&cfg, len, &transferred, USB_TIMEOUT);
506 if (ret < 0 || transferred != len) {
507 sr_err("Failed to send FPGA configuration: %s.", libusb_error_name(ret));
508 return SR_ERR;
509 }
510
511 return SR_OK;
512}
513
1ee70746
JH
514SR_PRIV int dslogic_set_voltage_threshold(const struct sr_dev_inst *sdi, double threshold)
515{
516 int ret;
517 struct dev_context *const devc = sdi->priv;
518 const struct sr_usb_dev_inst *const usb = sdi->conn;
519 const uint8_t value = (threshold / 5.0) * 255;
520 const uint16_t cmd = value | (DS_ADDR_VTH << 8);
521
522 /* Send the control command. */
523 ret = libusb_control_transfer(usb->devhdl,
524 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT,
525 DS_CMD_WR_REG, 0x0000, 0x0000,
526 (unsigned char *)&cmd, sizeof(cmd), 3000);
527 if (ret < 0) {
528 sr_err("Unable to set voltage-threshold register: %s.",
529 libusb_error_name(ret));
530 return SR_ERR;
531 }
532
533 devc->cur_threshold = threshold;
534
535 return SR_OK;
536}
537
adcb9951
JH
538SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
539{
540 libusb_device **devlist;
541 struct sr_usb_dev_inst *usb;
542 struct libusb_device_descriptor des;
543 struct dev_context *devc;
544 struct drv_context *drvc;
545 struct version_info vi;
7e463623 546 int ret = SR_ERR, i, device_count;
adcb9951
JH
547 uint8_t revid;
548 char connection_id[64];
549
550 drvc = di->context;
551 devc = sdi->priv;
552 usb = sdi->conn;
553
adcb9951
JH
554 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
555 if (device_count < 0) {
556 sr_err("Failed to get device list: %s.",
557 libusb_error_name(device_count));
558 return SR_ERR;
559 }
560
561 for (i = 0; i < device_count; i++) {
562 libusb_get_device_descriptor(devlist[i], &des);
563
564 if (des.idVendor != devc->profile->vid
565 || des.idProduct != devc->profile->pid)
566 continue;
567
568 if ((sdi->status == SR_ST_INITIALIZING) ||
569 (sdi->status == SR_ST_INACTIVE)) {
7e463623 570 /* Check device by its physical USB bus/port address. */
adcb9951
JH
571 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
572 if (strcmp(sdi->connection_id, connection_id))
573 /* This is not the one. */
574 continue;
575 }
576
577 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
578 if (usb->address == 0xff)
579 /*
580 * First time we touch this device after FW
581 * upload, so we don't know the address yet.
582 */
583 usb->address = libusb_get_device_address(devlist[i]);
584 } else {
585 sr_err("Failed to open device: %s.",
586 libusb_error_name(ret));
7e463623 587 ret = SR_ERR;
adcb9951
JH
588 break;
589 }
590
591 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
592 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
593 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
594 sr_err("Failed to detach kernel driver: %s.",
595 libusb_error_name(ret));
7e463623
UH
596 ret = SR_ERR;
597 break;
adcb9951
JH
598 }
599 }
600 }
601
602 ret = command_get_fw_version(usb->devhdl, &vi);
603 if (ret != SR_OK) {
604 sr_err("Failed to get firmware version.");
605 break;
606 }
607
608 ret = command_get_revid_version(sdi, &revid);
609 if (ret != SR_OK) {
610 sr_err("Failed to get REVID.");
611 break;
612 }
613
614 /*
615 * Changes in major version mean incompatible/API changes, so
616 * bail out if we encounter an incompatible version.
617 * Different minor versions are OK, they should be compatible.
618 */
619 if (vi.major != DSLOGIC_REQUIRED_VERSION_MAJOR) {
620 sr_err("Expected firmware version %d.x, "
621 "got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
622 vi.major, vi.minor);
7e463623 623 ret = SR_ERR;
adcb9951
JH
624 break;
625 }
626
adcb9951
JH
627 sr_info("Opened device on %d.%d (logical) / %s (physical), "
628 "interface %d, firmware %d.%d.",
629 usb->bus, usb->address, connection_id,
630 USB_INTERFACE, vi.major, vi.minor);
631
632 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
633 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
634
7e463623
UH
635 ret = SR_OK;
636
adcb9951
JH
637 break;
638 }
adcb9951 639
7e463623 640 libusb_free_device_list(devlist, 1);
adcb9951 641
7e463623 642 return ret;
adcb9951
JH
643}
644
645SR_PRIV struct dev_context *dslogic_dev_new(void)
646{
647 struct dev_context *devc;
648
649 devc = g_malloc0(sizeof(struct dev_context));
650 devc->profile = NULL;
651 devc->fw_updated = 0;
652 devc->cur_samplerate = 0;
653 devc->limit_samples = 0;
654 devc->capture_ratio = 0;
655 devc->continuous_mode = FALSE;
656 devc->clock_edge = DS_EDGE_RISING;
657
658 return devc;
659}
660
4bd770f5 661static void abort_acquisition(struct dev_context *devc)
adcb9951
JH
662{
663 int i;
664
665 devc->acq_aborted = TRUE;
666
667 for (i = devc->num_transfers - 1; i >= 0; i--) {
668 if (devc->transfers[i])
669 libusb_cancel_transfer(devc->transfers[i]);
670 }
671}
672
673static void finish_acquisition(struct sr_dev_inst *sdi)
674{
675 struct dev_context *devc;
676
677 devc = sdi->priv;
678
679 std_session_send_df_end(sdi);
680
681 usb_source_remove(sdi->session, devc->ctx);
682
683 devc->num_transfers = 0;
684 g_free(devc->transfers);
f74485b6 685 g_free(devc->deinterleave_buffer);
adcb9951
JH
686}
687
688static void free_transfer(struct libusb_transfer *transfer)
689{
690 struct sr_dev_inst *sdi;
691 struct dev_context *devc;
692 unsigned int i;
693
694 sdi = transfer->user_data;
695 devc = sdi->priv;
696
697 g_free(transfer->buffer);
698 transfer->buffer = NULL;
699 libusb_free_transfer(transfer);
700
701 for (i = 0; i < devc->num_transfers; i++) {
702 if (devc->transfers[i] == transfer) {
703 devc->transfers[i] = NULL;
704 break;
705 }
706 }
707
708 devc->submitted_transfers--;
709 if (devc->submitted_transfers == 0)
710 finish_acquisition(sdi);
711}
712
713static void resubmit_transfer(struct libusb_transfer *transfer)
714{
715 int ret;
716
717 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
718 return;
719
720 sr_err("%s: %s", __func__, libusb_error_name(ret));
721 free_transfer(transfer);
722
723}
724
f74485b6
JH
725static void deinterleave_buffer(const uint8_t *src, size_t length,
726 uint16_t *dst_ptr, size_t channel_count, uint16_t channel_mask)
727{
728 uint16_t sample;
729
730 for (const uint64_t *src_ptr = (uint64_t*)src;
731 src_ptr < (uint64_t*)(src + length);
732 src_ptr += channel_count) {
733 for (int bit = 0; bit != 64; bit++) {
734 const uint64_t *word_ptr = src_ptr;
735 sample = 0;
736 for (size_t channel = 0; channel != channel_count;
737 channel++) {
738 if ((channel_mask & (1 << channel)) &&
739 (*word_ptr++ & (1ULL << bit)))
740 sample |= 1 << channel;
741 }
742 *dst_ptr++ = sample;
743 }
744 }
745}
746
4bd770f5 747static void send_data(struct sr_dev_inst *sdi,
f74485b6 748 uint16_t *data, size_t sample_count)
adcb9951
JH
749{
750 const struct sr_datafeed_logic logic = {
f74485b6
JH
751 .length = sample_count * sizeof(uint16_t),
752 .unitsize = sizeof(uint16_t),
adcb9951
JH
753 .data = data
754 };
755
756 const struct sr_datafeed_packet packet = {
757 .type = SR_DF_LOGIC,
758 .payload = &logic
759 };
760
761 sr_session_send(sdi, &packet);
762}
763
4bd770f5 764static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
adcb9951 765{
f74485b6
JH
766 struct sr_dev_inst *const sdi = transfer->user_data;
767 struct dev_context *const devc = sdi->priv;
768 const size_t channel_count = enabled_channel_count(sdi);
769 const uint16_t channel_mask = enabled_channel_mask(sdi);
770 const unsigned int cur_sample_count = DSLOGIC_ATOMIC_SAMPLES *
771 transfer->actual_length /
772 (DSLOGIC_ATOMIC_BYTES * channel_count);
773
adcb9951
JH
774 gboolean packet_has_error = FALSE;
775 struct sr_datafeed_packet packet;
776 unsigned int num_samples;
f74485b6 777 int trigger_offset;
adcb9951
JH
778
779 /*
780 * If acquisition has already ended, just free any queued up
781 * transfer that come in.
782 */
783 if (devc->acq_aborted) {
784 free_transfer(transfer);
785 return;
786 }
787
788 sr_dbg("receive_transfer(): status %s received %d bytes.",
789 libusb_error_name(transfer->status), transfer->actual_length);
790
791 /* Save incoming transfer before reusing the transfer struct. */
adcb9951
JH
792
793 switch (transfer->status) {
794 case LIBUSB_TRANSFER_NO_DEVICE:
4bd770f5 795 abort_acquisition(devc);
adcb9951
JH
796 free_transfer(transfer);
797 return;
798 case LIBUSB_TRANSFER_COMPLETED:
799 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
800 break;
801 default:
802 packet_has_error = TRUE;
803 break;
804 }
805
806 if (transfer->actual_length == 0 || packet_has_error) {
807 devc->empty_transfer_count++;
808 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
809 /*
810 * The FX2 gave up. End the acquisition, the frontend
811 * will work out that the samplecount is short.
812 */
4bd770f5 813 abort_acquisition(devc);
adcb9951
JH
814 free_transfer(transfer);
815 } else {
816 resubmit_transfer(transfer);
817 }
818 return;
819 } else {
820 devc->empty_transfer_count = 0;
821 }
5e7e327a
JH
822
823 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
5e7e327a
JH
824 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
825 num_samples = devc->limit_samples - devc->sent_samples;
826 else
827 num_samples = cur_sample_count;
828
f74485b6
JH
829 /**
830 * The DSLogic emits sample data as sequences of 64-bit sample words
831 * in a round-robin i.e. 64-bits from channel 0, 64-bits from channel 1
832 * etc. for each of the enabled channels, then looping back to the
833 * channel.
834 *
835 * Because sigrok's internal representation is bit-interleaved channels
836 * we must recast the data.
837 *
838 * Hopefully in future it will be possible to pass the data on as-is.
839 */
ecadb118
UH
840 if (transfer->actual_length % (DSLOGIC_ATOMIC_BYTES * channel_count) != 0)
841 sr_err("Invalid transfer length!");
f74485b6
JH
842 deinterleave_buffer(transfer->buffer, transfer->actual_length,
843 devc->deinterleave_buffer, channel_count, channel_mask);
844
845 /* Send the incoming transfer to the session bus. */
5e7e327a
JH
846 if (devc->trigger_pos > devc->sent_samples
847 && devc->trigger_pos <= devc->sent_samples + num_samples) {
848 /* DSLogic trigger in this block. Send trigger position. */
849 trigger_offset = devc->trigger_pos - devc->sent_samples;
850 /* Pre-trigger samples. */
f74485b6 851 send_data(sdi, devc->deinterleave_buffer, trigger_offset);
5e7e327a
JH
852 devc->sent_samples += trigger_offset;
853 /* Trigger position. */
854 devc->trigger_pos = 0;
855 packet.type = SR_DF_TRIGGER;
856 packet.payload = NULL;
857 sr_session_send(sdi, &packet);
858 /* Post trigger samples. */
859 num_samples -= trigger_offset;
f74485b6
JH
860 send_data(sdi, devc->deinterleave_buffer
861 + trigger_offset, num_samples);
5e7e327a
JH
862 devc->sent_samples += num_samples;
863 } else {
f74485b6 864 send_data(sdi, devc->deinterleave_buffer, num_samples);
5e7e327a 865 devc->sent_samples += num_samples;
adcb9951
JH
866 }
867 }
868
869 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
4bd770f5 870 abort_acquisition(devc);
adcb9951
JH
871 free_transfer(transfer);
872 } else
873 resubmit_transfer(transfer);
874}
875
4bd770f5 876static int receive_data(int fd, int revents, void *cb_data)
adcb9951 877{
4bd770f5
JH
878 struct timeval tv;
879 struct drv_context *drvc;
880
881 (void)fd;
882 (void)revents;
883
884 drvc = (struct drv_context *)cb_data;
885
886 tv.tv_sec = tv.tv_usec = 0;
887 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
888
889 return TRUE;
adcb9951
JH
890}
891
03a0002e 892static size_t to_bytes_per_ms(const struct sr_dev_inst *sdi)
adcb9951 893{
03a0002e
JH
894 const struct dev_context *const devc = sdi->priv;
895 const size_t ch_count = enabled_channel_count(sdi);
896
897 if (devc->continuous_mode)
898 return (devc->cur_samplerate * ch_count) / (1000 * 8);
899
900
901 /* If we're in buffered mode, the transfer rate is not so important,
902 * but we expect to get at least 10% of the high-speed USB bandwidth.
903 */
904 return 35000000 / (1000 * 10);
4bd770f5 905}
adcb9951 906
03a0002e 907static size_t get_buffer_size(const struct sr_dev_inst *sdi)
4bd770f5 908{
adcb9951
JH
909 /*
910 * The buffer should be large enough to hold 10ms of data and
03a0002e 911 * a multiple of the size of a data atom.
adcb9951 912 */
03a0002e
JH
913 const size_t block_size = enabled_channel_count(sdi) * 512;
914 const size_t s = 10 * to_bytes_per_ms(sdi);
915 return ((s + block_size - 1) / block_size) * block_size;
adcb9951
JH
916}
917
03a0002e 918static unsigned int get_number_of_transfers(const struct sr_dev_inst *sdi)
adcb9951 919{
4bd770f5 920 /* Total buffer size should be able to hold about 100ms of data. */
03a0002e
JH
921 const unsigned int s = get_buffer_size(sdi);
922 const unsigned int n = (100 * to_bytes_per_ms(sdi) + s - 1) / s;
923 return (n > NUM_SIMUL_TRANSFERS) ? NUM_SIMUL_TRANSFERS : n;
4bd770f5 924}
adcb9951 925
03a0002e 926static unsigned int get_timeout(const struct sr_dev_inst *sdi)
4bd770f5 927{
03a0002e
JH
928 const size_t total_size = get_buffer_size(sdi) *
929 get_number_of_transfers(sdi);
930 const unsigned int timeout = total_size / to_bytes_per_ms(sdi);
adcb9951
JH
931 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
932}
4bd770f5
JH
933
934static int start_transfers(const struct sr_dev_inst *sdi)
935{
f74485b6 936 const size_t channel_count = enabled_channel_count(sdi);
03a0002e
JH
937 const size_t size = get_buffer_size(sdi);
938 const unsigned int num_transfers = get_number_of_transfers(sdi);
939 const unsigned int timeout = get_timeout(sdi);
940
4bd770f5
JH
941 struct dev_context *devc;
942 struct sr_usb_dev_inst *usb;
943 struct libusb_transfer *transfer;
03a0002e
JH
944 unsigned int i;
945 int ret;
4bd770f5 946 unsigned char *buf;
4bd770f5
JH
947
948 devc = sdi->priv;
949 usb = sdi->conn;
950
951 devc->sent_samples = 0;
952 devc->acq_aborted = FALSE;
953 devc->empty_transfer_count = 0;
4bd770f5
JH
954 devc->submitted_transfers = 0;
955
5e23d42f 956 g_free(devc->transfers);
4bd770f5
JH
957 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
958 if (!devc->transfers) {
959 sr_err("USB transfers malloc failed.");
960 return SR_ERR_MALLOC;
961 }
962
f74485b6
JH
963 devc->deinterleave_buffer = g_try_malloc(DSLOGIC_ATOMIC_SAMPLES *
964 (size / (channel_count * DSLOGIC_ATOMIC_BYTES)) * sizeof(uint16_t));
965 if (!devc->deinterleave_buffer) {
966 sr_err("Deinterleave buffer malloc failed.");
967 g_free(devc->deinterleave_buffer);
968 return SR_ERR_MALLOC;
969 }
970
4bd770f5
JH
971 devc->num_transfers = num_transfers;
972 for (i = 0; i < num_transfers; i++) {
973 if (!(buf = g_try_malloc(size))) {
974 sr_err("USB transfer buffer malloc failed.");
975 return SR_ERR_MALLOC;
976 }
977 transfer = libusb_alloc_transfer(0);
978 libusb_fill_bulk_transfer(transfer, usb->devhdl,
979 6 | LIBUSB_ENDPOINT_IN, buf, size,
980 receive_transfer, (void *)sdi, timeout);
981 sr_info("submitting transfer: %d", i);
982 if ((ret = libusb_submit_transfer(transfer)) != 0) {
983 sr_err("Failed to submit transfer: %s.",
984 libusb_error_name(ret));
985 libusb_free_transfer(transfer);
986 g_free(buf);
987 abort_acquisition(devc);
988 return SR_ERR;
989 }
990 devc->transfers[i] = transfer;
991 devc->submitted_transfers++;
992 }
993
994 std_session_send_df_header(sdi);
995
996 return SR_OK;
997}
998
999static void LIBUSB_CALL trigger_receive(struct libusb_transfer *transfer)
1000{
1001 const struct sr_dev_inst *sdi;
1002 struct dslogic_trigger_pos *tpos;
1003 struct dev_context *devc;
1004
1005 sdi = transfer->user_data;
1006 devc = sdi->priv;
1007 if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
1008 sr_dbg("Trigger transfer canceled.");
1009 /* Terminate session. */
1010 std_session_send_df_end(sdi);
1011 usb_source_remove(sdi->session, devc->ctx);
1012 devc->num_transfers = 0;
1013 g_free(devc->transfers);
1014 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED
1015 && transfer->actual_length == sizeof(struct dslogic_trigger_pos)) {
1016 tpos = (struct dslogic_trigger_pos *)transfer->buffer;
1017 sr_info("tpos real_pos %d ram_saddr %d cnt %d", tpos->real_pos,
1018 tpos->ram_saddr, tpos->remain_cnt);
1019 devc->trigger_pos = tpos->real_pos;
1020 g_free(tpos);
1021 start_transfers(sdi);
1022 }
1023 libusb_free_transfer(transfer);
1024}
1025
658caaf0 1026SR_PRIV int dslogic_acquisition_start(const struct sr_dev_inst *sdi)
4bd770f5 1027{
03a0002e
JH
1028 const unsigned int timeout = get_timeout(sdi);
1029
658caaf0
JH
1030 struct sr_dev_driver *di;
1031 struct drv_context *drvc;
1032 struct dev_context *devc;
4bd770f5 1033 struct sr_usb_dev_inst *usb;
4bd770f5 1034 struct dslogic_trigger_pos *tpos;
658caaf0 1035 struct libusb_transfer *transfer;
4bd770f5
JH
1036 int ret;
1037
658caaf0
JH
1038 di = sdi->driver;
1039 drvc = di->context;
4bd770f5 1040 devc = sdi->priv;
658caaf0
JH
1041 usb = sdi->conn;
1042
1043 devc->ctx = drvc->sr_ctx;
1044 devc->sent_samples = 0;
1045 devc->empty_transfer_count = 0;
1046 devc->acq_aborted = FALSE;
1047
658caaf0 1048 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
4bd770f5
JH
1049
1050 if ((ret = command_stop_acquisition(sdi)) != SR_OK)
1051 return ret;
1052
1053 if ((ret = fpga_configure(sdi)) != SR_OK)
1054 return ret;
1055
1056 if ((ret = command_start_acquisition(sdi)) != SR_OK)
1057 return ret;
1058
1059 sr_dbg("Getting trigger.");
1060 tpos = g_malloc(sizeof(struct dslogic_trigger_pos));
1061 transfer = libusb_alloc_transfer(0);
1062 libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN,
1063 (unsigned char *)tpos, sizeof(struct dslogic_trigger_pos),
1064 trigger_receive, (void *)sdi, 0);
1065 if ((ret = libusb_submit_transfer(transfer)) < 0) {
1066 sr_err("Failed to request trigger: %s.", libusb_error_name(ret));
1067 libusb_free_transfer(transfer);
1068 g_free(tpos);
1069 return SR_ERR;
1070 }
1071
1072 devc->transfers = g_try_malloc0(sizeof(*devc->transfers));
1073 if (!devc->transfers) {
1074 sr_err("USB trigger_pos transfer malloc failed.");
1075 return SR_ERR_MALLOC;
1076 }
1077 devc->num_transfers = 1;
1078 devc->submitted_transfers++;
1079 devc->transfers[0] = transfer;
1080
1081 return ret;
1082}
1083
4bd770f5
JH
1084SR_PRIV int dslogic_acquisition_stop(struct sr_dev_inst *sdi)
1085{
1086 command_stop_acquisition(sdi);
1087 abort_acquisition(sdi->priv);
1088 return SR_OK;
1089}