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