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