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