]> sigrok.org Git - libsigrok.git/blame - src/hardware/fx2lafw/protocol.c
sr_dev_open(): Factor out SR_ST_ACTIVE check.
[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
UH
24#include "protocol.h"
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{
a54edb1d
ML
63 struct sr_usb_dev_inst *usb = sdi->conn;
64 libusb_device_handle *devhdl = usb->devhdl;
adcb9951 65 int ret;
2f937611
UH
66
67 ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
adcb9951
JH
68 LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000,
69 revid, 1, USB_TIMEOUT);
2f937611
UH
70
71 if (ret < 0) {
72 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
73 return SR_ERR;
74 }
75
76 return SR_OK;
77}
78
b0acb693 79static int command_start_acquisition(const struct sr_dev_inst *sdi)
2f937611 80{
81a34976
BV
81 struct dev_context *devc;
82 struct sr_usb_dev_inst *usb;
83 uint64_t samplerate;
84 struct cmd_start_acquisition cmd;
85 int delay, ret;
86
87 devc = sdi->priv;
88 usb = sdi->conn;
89 samplerate = devc->cur_samplerate;
2f937611
UH
90
91 /* Compute the sample rate. */
81a34976 92 if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
2f937611
UH
93 sr_err("Unable to sample at %" PRIu64 "Hz "
94 "when collecting 16-bit samples.", samplerate);
95 return SR_ERR;
96 }
97
81a34976
BV
98 delay = 0;
99 cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
2f937611
UH
100 if ((SR_MHZ(48) % samplerate) == 0) {
101 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
102 delay = SR_MHZ(48) / samplerate - 1;
103 if (delay > MAX_SAMPLE_DELAY)
104 delay = 0;
105 }
106
107 if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
108 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
109 delay = SR_MHZ(30) / samplerate - 1;
110 }
111
b9d53092 112 sr_dbg("GPIF delay = %d, clocksource = %sMHz.", delay,
2f937611
UH
113 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
114
115 if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
116 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
117 return SR_ERR;
118 }
119
120 cmd.sample_delay_h = (delay >> 8) & 0xff;
121 cmd.sample_delay_l = delay & 0xff;
122
123 /* Select the sampling width. */
81a34976 124 cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
2f937611 125 CMD_START_FLAGS_SAMPLE_8BIT;
7fb90f94 126 /* Enable CTL2 clock. */
f9592d65 127 cmd.flags |= (g_slist_length(devc->enabled_analog_channels) > 0) ? CMD_START_FLAGS_CLK_CTL2 : 0;
2f937611
UH
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
2f937611
UH
142SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
143{
144 libusb_device **devlist;
145 struct sr_usb_dev_inst *usb;
146 struct libusb_device_descriptor des;
147 struct dev_context *devc;
148 struct drv_context *drvc;
149 struct version_info vi;
5e2c86eb 150 int ret, i, device_count;
2f937611 151 uint8_t revid;
5e2c86eb 152 char connection_id[64];
2f937611 153
41812aca 154 drvc = di->context;
2f937611
UH
155 devc = sdi->priv;
156 usb = sdi->conn;
157
2f937611
UH
158 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
159 if (device_count < 0) {
160 sr_err("Failed to get device list: %s.",
161 libusb_error_name(device_count));
162 return SR_ERR;
163 }
164
165 for (i = 0; i < device_count; i++) {
2a8f2d41 166 libusb_get_device_descriptor(devlist[i], &des);
2f937611
UH
167
168 if (des.idVendor != devc->profile->vid
169 || des.idProduct != devc->profile->pid)
170 continue;
171
5e2c86eb
SA
172 if ((sdi->status == SR_ST_INITIALIZING) ||
173 (sdi->status == SR_ST_INACTIVE)) {
2f937611 174 /*
5e2c86eb 175 * Check device by its physical USB bus/port address.
2f937611 176 */
5e2c86eb
SA
177 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
178 if (strcmp(sdi->connection_id, connection_id))
2f937611
UH
179 /* This is not the one. */
180 continue;
181 }
182
183 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
184 if (usb->address == 0xff)
185 /*
186 * First time we touch this device after FW
187 * upload, so we don't know the address yet.
188 */
189 usb->address = libusb_get_device_address(devlist[i]);
190 } else {
191 sr_err("Failed to open device: %s.",
192 libusb_error_name(ret));
193 break;
194 }
195
dc2903bb 196 if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) {
c11a1e61
UH
197 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
198 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
dc2903bb
UH
199 sr_err("Failed to detach kernel driver: %s.",
200 libusb_error_name(ret));
201 return SR_ERR;
202 }
203 }
204 }
205
2f937611
UH
206 ret = command_get_fw_version(usb->devhdl, &vi);
207 if (ret != SR_OK) {
208 sr_err("Failed to get firmware version.");
209 break;
210 }
211
a54edb1d 212 ret = command_get_revid_version(sdi, &revid);
2f937611
UH
213 if (ret != SR_OK) {
214 sr_err("Failed to get REVID.");
215 break;
216 }
217
218 /*
219 * Changes in major version mean incompatible/API changes, so
220 * bail out if we encounter an incompatible version.
221 * Different minor versions are OK, they should be compatible.
222 */
223 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
224 sr_err("Expected firmware version %d.x, "
225 "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
226 vi.major, vi.minor);
227 break;
228 }
229
230 sdi->status = SR_ST_ACTIVE;
5e2c86eb 231 sr_info("Opened device on %d.%d (logical) / %s (physical), "
2f937611 232 "interface %d, firmware %d.%d.",
5e2c86eb 233 usb->bus, usb->address, connection_id,
2f937611
UH
234 USB_INTERFACE, vi.major, vi.minor);
235
236 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
237 revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
238
239 break;
240 }
241 libusb_free_device_list(devlist, 1);
242
243 if (sdi->status != SR_ST_ACTIVE)
244 return SR_ERR;
245
246 return SR_OK;
247}
248
2f937611
UH
249SR_PRIV struct dev_context *fx2lafw_dev_new(void)
250{
251 struct dev_context *devc;
252
f57d8ffe 253 devc = g_malloc0(sizeof(struct dev_context));
2f937611
UH
254 devc->profile = NULL;
255 devc->fw_updated = 0;
256 devc->cur_samplerate = 0;
257 devc->limit_samples = 0;
7bfcb25c 258 devc->capture_ratio = 0;
87b545fb 259 devc->sample_wide = FALSE;
335122f0 260 devc->stl = NULL;
2f937611
UH
261
262 return devc;
263}
264
265SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
266{
267 int i;
268
b0ccd64d 269 devc->acq_aborted = TRUE;
2f937611
UH
270
271 for (i = devc->num_transfers - 1; i >= 0; i--) {
272 if (devc->transfers[i])
273 libusb_cancel_transfer(devc->transfers[i]);
274 }
275}
276
102f1239 277static void finish_acquisition(struct sr_dev_inst *sdi)
2f937611 278{
102f1239
BV
279 struct dev_context *devc;
280
281 devc = sdi->priv;
2f937611 282
bee2b016 283 std_session_send_df_end(sdi);
2f937611 284
102f1239 285 usb_source_remove(sdi->session, devc->ctx);
2f937611
UH
286
287 devc->num_transfers = 0;
288 g_free(devc->transfers);
335122f0 289
f9592d65
UH
290 /* Free the deinterlace buffers if we had them. */
291 if (g_slist_length(devc->enabled_analog_channels) > 0) {
292 g_free(devc->logic_buffer);
293 g_free(devc->analog_buffer);
294 }
7e5ccff2 295
335122f0
BV
296 if (devc->stl) {
297 soft_trigger_logic_free(devc->stl);
298 devc->stl = NULL;
299 }
2f937611
UH
300}
301
302static void free_transfer(struct libusb_transfer *transfer)
303{
9615eeb5 304 struct sr_dev_inst *sdi;
2f937611
UH
305 struct dev_context *devc;
306 unsigned int i;
307
9615eeb5
BV
308 sdi = transfer->user_data;
309 devc = sdi->priv;
2f937611
UH
310
311 g_free(transfer->buffer);
312 transfer->buffer = NULL;
313 libusb_free_transfer(transfer);
314
315 for (i = 0; i < devc->num_transfers; i++) {
316 if (devc->transfers[i] == transfer) {
317 devc->transfers[i] = NULL;
318 break;
319 }
320 }
321
322 devc->submitted_transfers--;
323 if (devc->submitted_transfers == 0)
102f1239 324 finish_acquisition(sdi);
2f937611
UH
325}
326
327static void resubmit_transfer(struct libusb_transfer *transfer)
328{
329 int ret;
330
331 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
332 return;
333
9615eeb5 334 sr_err("%s: %s", __func__, libusb_error_name(ret));
2f937611 335 free_transfer(transfer);
2f937611 336
9615eeb5
BV
337}
338
b0acb693 339static void mso_send_data_proc(struct sr_dev_inst *sdi,
7e5ccff2
JH
340 uint8_t *data, size_t length, size_t sample_width)
341{
342 size_t i;
695dc859 343 struct dev_context *devc;
a6ad49b3
UH
344 struct sr_datafeed_analog analog;
345 struct sr_analog_encoding encoding;
346 struct sr_analog_meaning meaning;
347 struct sr_analog_spec spec;
695dc859 348
e91d4ce2
UH
349 (void)sample_width;
350
695dc859
UH
351 devc = sdi->priv;
352
7e5ccff2
JH
353 length /= 2;
354
7e5ccff2 355 /* Send the logic */
695dc859 356 for (i = 0; i < length; i++) {
d9251a2c 357 devc->logic_buffer[i] = data[i * 2];
7fb90f94 358 /* Rescale to -10V - +10V from 0-255. */
2ab0679a 359 devc->analog_buffer[i] = (data[i * 2 + 1] - 128.0f) / 12.8f;
7e5ccff2
JH
360 };
361
362 const struct sr_datafeed_logic logic = {
363 .length = length,
364 .unitsize = 1,
365 .data = devc->logic_buffer
366 };
367
368 const struct sr_datafeed_packet logic_packet = {
369 .type = SR_DF_LOGIC,
370 .payload = &logic
371 };
695dc859
UH
372
373 sr_session_send(sdi, &logic_packet);
7e5ccff2 374
0cce2383 375 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
a6ad49b3
UH
376 analog.meaning->channels = devc->enabled_analog_channels;
377 analog.meaning->mq = SR_MQ_VOLTAGE;
378 analog.meaning->unit = SR_UNIT_VOLT;
379 analog.meaning->mqflags = 0 /* SR_MQFLAG_DC */;
380 analog.num_samples = length;
381 analog.data = devc->analog_buffer;
7e5ccff2
JH
382
383 const struct sr_datafeed_packet analog_packet = {
a6ad49b3 384 .type = SR_DF_ANALOG,
7e5ccff2
JH
385 .payload = &analog
386 };
7e5ccff2 387
695dc859 388 sr_session_send(sdi, &analog_packet);
7e5ccff2
JH
389}
390
b0acb693 391static void la_send_data_proc(struct sr_dev_inst *sdi,
7b5d1c64
JH
392 uint8_t *data, size_t length, size_t sample_width)
393{
394 const struct sr_datafeed_logic logic = {
395 .length = length,
396 .unitsize = sample_width,
397 .data = data
398 };
399
400 const struct sr_datafeed_packet packet = {
401 .type = SR_DF_LOGIC,
402 .payload = &logic
403 };
404
695dc859 405 sr_session_send(sdi, &packet);
7b5d1c64
JH
406}
407
b0acb693 408static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
2f937611 409{
9615eeb5
BV
410 struct sr_dev_inst *sdi;
411 struct dev_context *devc;
2f937611 412 gboolean packet_has_error = FALSE;
9615eeb5
BV
413 unsigned int num_samples;
414 int trigger_offset, cur_sample_count, unitsize;
7bfcb25c 415 int pre_trigger_samples;
2f937611 416
9615eeb5
BV
417 sdi = transfer->user_data;
418 devc = sdi->priv;
2f937611
UH
419
420 /*
421 * If acquisition has already ended, just free any queued up
422 * transfer that come in.
423 */
b0ccd64d 424 if (devc->acq_aborted) {
2f937611
UH
425 free_transfer(transfer);
426 return;
427 }
428
0f262763
UH
429 sr_dbg("receive_transfer(): status %s received %d bytes.",
430 libusb_error_name(transfer->status), transfer->actual_length);
2f937611
UH
431
432 /* Save incoming transfer before reusing the transfer struct. */
9615eeb5
BV
433 unitsize = devc->sample_wide ? 2 : 1;
434 cur_sample_count = transfer->actual_length / unitsize;
2f937611
UH
435
436 switch (transfer->status) {
437 case LIBUSB_TRANSFER_NO_DEVICE:
438 fx2lafw_abort_acquisition(devc);
439 free_transfer(transfer);
440 return;
441 case LIBUSB_TRANSFER_COMPLETED:
442 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
443 break;
444 default:
445 packet_has_error = TRUE;
446 break;
447 }
448
449 if (transfer->actual_length == 0 || packet_has_error) {
450 devc->empty_transfer_count++;
451 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
452 /*
453 * The FX2 gave up. End the acquisition, the frontend
454 * will work out that the samplecount is short.
455 */
456 fx2lafw_abort_acquisition(devc);
457 free_transfer(transfer);
458 } else {
459 resubmit_transfer(transfer);
460 }
461 return;
462 } else {
463 devc->empty_transfer_count = 0;
464 }
9615eeb5 465 if (devc->trigger_fired) {
2f663c82 466 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
9615eeb5 467 /* Send the incoming transfer to the session bus. */
2f663c82 468 if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
9615eeb5
BV
469 num_samples = devc->limit_samples - devc->sent_samples;
470 else
471 num_samples = cur_sample_count;
7b5d1c64 472
adcb9951
JH
473 devc->send_data_proc(sdi, (uint8_t *)transfer->buffer,
474 num_samples * unitsize, unitsize);
475 devc->sent_samples += num_samples;
9615eeb5
BV
476 }
477 } else {
335122f0 478 trigger_offset = soft_trigger_logic_check(devc->stl,
7bfcb25c 479 transfer->buffer, transfer->actual_length, &pre_trigger_samples);
9615eeb5 480 if (trigger_offset > -1) {
7bfcb25c 481 devc->sent_samples += pre_trigger_samples;
9615eeb5
BV
482 num_samples = cur_sample_count - trigger_offset;
483 if (devc->limit_samples &&
484 num_samples > devc->limit_samples - devc->sent_samples)
485 num_samples = devc->limit_samples - devc->sent_samples;
7b5d1c64 486
695dc859
UH
487 devc->send_data_proc(sdi, (uint8_t *)transfer->buffer
488 + trigger_offset * unitsize,
489 num_samples * unitsize, unitsize);
9615eeb5
BV
490 devc->sent_samples += num_samples;
491
492 devc->trigger_fired = TRUE;
2f937611 493 }
06b5d7f7
BV
494 }
495
496 if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
497 fx2lafw_abort_acquisition(devc);
498 free_transfer(transfer);
9615eeb5
BV
499 } else
500 resubmit_transfer(transfer);
2f937611
UH
501}
502
b0acb693
JH
503static int configure_channels(const struct sr_dev_inst *sdi)
504{
505 struct dev_context *devc;
506 const GSList *l;
507 int p;
508 struct sr_channel *ch;
509 uint32_t channel_mask = 0, num_analog = 0;
510
511 devc = sdi->priv;
512
513 g_slist_free(devc->enabled_analog_channels);
514 devc->enabled_analog_channels = NULL;
515
516 for (l = sdi->channels, p = 0; l; l = l->next, p++) {
517 ch = l->data;
518 if ((p <= NUM_CHANNELS) && (ch->type == SR_CHANNEL_ANALOG)
519 && (ch->enabled)) {
520 num_analog++;
521 devc->enabled_analog_channels =
522 g_slist_append(devc->enabled_analog_channels, ch);
523 } else {
524 channel_mask |= ch->enabled << p;
525 }
526 }
527
528 /*
529 * Use wide sampling if either any of the LA channels 8..15 is enabled,
530 * and/or at least one analog channel is enabled.
531 */
532 devc->sample_wide = channel_mask > 0xff || num_analog > 0;
533
534 return SR_OK;
535}
536
2f937611
UH
537static unsigned int to_bytes_per_ms(unsigned int samplerate)
538{
539 return samplerate / 1000;
540}
541
b0acb693 542static size_t get_buffer_size(struct dev_context *devc)
2f937611
UH
543{
544 size_t s;
545
546 /*
547 * The buffer should be large enough to hold 10ms of data and
548 * a multiple of 512.
549 */
550 s = 10 * to_bytes_per_ms(devc->cur_samplerate);
551 return (s + 511) & ~511;
552}
553
b0acb693 554static unsigned int get_number_of_transfers(struct dev_context *devc)
2f937611
UH
555{
556 unsigned int n;
557
558 /* Total buffer size should be able to hold about 500ms of data. */
559 n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
b0acb693 560 get_buffer_size(devc));
2f937611
UH
561
562 if (n > NUM_SIMUL_TRANSFERS)
563 return NUM_SIMUL_TRANSFERS;
564
565 return n;
566}
567
b0acb693 568static unsigned int get_timeout(struct dev_context *devc)
2f937611
UH
569{
570 size_t total_size;
571 unsigned int timeout;
572
b0acb693
JH
573 total_size = get_buffer_size(devc) *
574 get_number_of_transfers(devc);
2f937611
UH
575 timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
576 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
577}
b0acb693
JH
578
579static int receive_data(int fd, int revents, void *cb_data)
580{
581 struct timeval tv;
582 struct drv_context *drvc;
583
584 (void)fd;
585 (void)revents;
586
587 drvc = (struct drv_context *)cb_data;
588
589 tv.tv_sec = tv.tv_usec = 0;
590 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
591
592 return TRUE;
593}
594
595static int start_transfers(const struct sr_dev_inst *sdi)
596{
597 struct dev_context *devc;
598 struct sr_usb_dev_inst *usb;
599 struct sr_trigger *trigger;
600 struct libusb_transfer *transfer;
601 unsigned int i, num_transfers;
602 int timeout, ret;
603 unsigned char *buf;
604 size_t size;
605
606 devc = sdi->priv;
607 usb = sdi->conn;
608
609 devc->sent_samples = 0;
610 devc->acq_aborted = FALSE;
611 devc->empty_transfer_count = 0;
612
613 if ((trigger = sr_session_trigger_get(sdi->session))) {
614 int pre_trigger_samples = 0;
615 if (devc->limit_samples > 0)
616 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
617 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
618 if (!devc->stl)
619 return SR_ERR_MALLOC;
620 devc->trigger_fired = FALSE;
621 } else
622 devc->trigger_fired = TRUE;
623
624 num_transfers = get_number_of_transfers(devc);
625
626 size = get_buffer_size(devc);
627 devc->submitted_transfers = 0;
628
629 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
630 if (!devc->transfers) {
631 sr_err("USB transfers malloc failed.");
632 return SR_ERR_MALLOC;
633 }
634
635 timeout = get_timeout(devc);
636 devc->num_transfers = num_transfers;
637 for (i = 0; i < num_transfers; i++) {
638 if (!(buf = g_try_malloc(size))) {
639 sr_err("USB transfer buffer malloc failed.");
640 return SR_ERR_MALLOC;
641 }
642 transfer = libusb_alloc_transfer(0);
643 libusb_fill_bulk_transfer(transfer, usb->devhdl,
644 2 | LIBUSB_ENDPOINT_IN, buf, size,
645 receive_transfer, (void *)sdi, timeout);
646 sr_info("submitting transfer: %d", i);
647 if ((ret = libusb_submit_transfer(transfer)) != 0) {
648 sr_err("Failed to submit transfer: %s.",
649 libusb_error_name(ret));
650 libusb_free_transfer(transfer);
651 g_free(buf);
652 fx2lafw_abort_acquisition(devc);
653 return SR_ERR;
654 }
655 devc->transfers[i] = transfer;
656 devc->submitted_transfers++;
657 }
658
659 /*
660 * If this device has analog channels and at least one of them is
661 * enabled, use mso_send_data_proc() to properly handle the analog
662 * data. Otherwise use la_send_data_proc().
663 */
664 if (g_slist_length(devc->enabled_analog_channels) > 0)
665 devc->send_data_proc = mso_send_data_proc;
666 else
667 devc->send_data_proc = la_send_data_proc;
668
669 std_session_send_df_header(sdi);
670
671 return SR_OK;
672}
673
674SR_PRIV int fx2lafw_start_acquisition(const struct sr_dev_inst *sdi)
675{
676 struct sr_dev_driver *di;
677 struct drv_context *drvc;
678 struct dev_context *devc;
679 int timeout, ret;
680 size_t size;
681
b0acb693
JH
682 di = sdi->driver;
683 drvc = di->context;
684 devc = sdi->priv;
685
686 devc->ctx = drvc->sr_ctx;
687 devc->sent_samples = 0;
688 devc->empty_transfer_count = 0;
689 devc->acq_aborted = FALSE;
690
691 if (configure_channels(sdi) != SR_OK) {
692 sr_err("Failed to configure channels.");
693 return SR_ERR;
694 }
695
696 timeout = get_timeout(devc);
697 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
698
699 size = get_buffer_size(devc);
700 /* Prepare for analog sampling. */
701 if (g_slist_length(devc->enabled_analog_channels) > 0) {
702 /* We need a buffer half the size of a transfer. */
703 devc->logic_buffer = g_try_malloc(size / 2);
704 devc->analog_buffer = g_try_malloc(
705 sizeof(float) * size / 2);
706 }
707 start_transfers(sdi);
708 if ((ret = command_start_acquisition(sdi)) != SR_OK) {
709 fx2lafw_abort_acquisition(devc);
710 return ret;
711 }
712
713 return SR_OK;
714}