]>
Commit | Line | Data |
---|---|---|
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 | ||
28 | struct version_info { | |
29 | uint8_t major; | |
30 | uint8_t minor; | |
31 | }; | |
32 | ||
33 | struct 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 |
43 | static 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 | 61 | static 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 | 79 | static 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 | ||
1656cd4a | 115 | if (delay < 0 || delay > MAX_SAMPLE_DELAY) { |
2f937611 UH |
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 |
142 | SR_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 |
252 | SR_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; | |
5f8cf332 | 260 | devc->limit_frames = 1; |
2f937611 | 261 | devc->limit_samples = 0; |
7bfcb25c | 262 | devc->capture_ratio = 0; |
87b545fb | 263 | devc->sample_wide = FALSE; |
5f8cf332 | 264 | devc->num_frames = 0; |
335122f0 | 265 | devc->stl = NULL; |
2f937611 UH |
266 | |
267 | return devc; | |
268 | } | |
269 | ||
270 | SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc) | |
271 | { | |
272 | int i; | |
273 | ||
b0ccd64d | 274 | devc->acq_aborted = TRUE; |
2f937611 UH |
275 | |
276 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
277 | if (devc->transfers[i]) | |
278 | libusb_cancel_transfer(devc->transfers[i]); | |
279 | } | |
280 | } | |
281 | ||
102f1239 | 282 | static void finish_acquisition(struct sr_dev_inst *sdi) |
2f937611 | 283 | { |
102f1239 BV |
284 | struct dev_context *devc; |
285 | ||
286 | devc = sdi->priv; | |
2f937611 | 287 | |
bee2b016 | 288 | std_session_send_df_end(sdi); |
2f937611 | 289 | |
102f1239 | 290 | usb_source_remove(sdi->session, devc->ctx); |
2f937611 UH |
291 | |
292 | devc->num_transfers = 0; | |
293 | g_free(devc->transfers); | |
335122f0 | 294 | |
f9592d65 UH |
295 | /* Free the deinterlace buffers if we had them. */ |
296 | if (g_slist_length(devc->enabled_analog_channels) > 0) { | |
297 | g_free(devc->logic_buffer); | |
298 | g_free(devc->analog_buffer); | |
299 | } | |
7e5ccff2 | 300 | |
335122f0 BV |
301 | if (devc->stl) { |
302 | soft_trigger_logic_free(devc->stl); | |
303 | devc->stl = NULL; | |
304 | } | |
2f937611 UH |
305 | } |
306 | ||
307 | static void free_transfer(struct libusb_transfer *transfer) | |
308 | { | |
9615eeb5 | 309 | struct sr_dev_inst *sdi; |
2f937611 UH |
310 | struct dev_context *devc; |
311 | unsigned int i; | |
312 | ||
9615eeb5 BV |
313 | sdi = transfer->user_data; |
314 | devc = sdi->priv; | |
2f937611 UH |
315 | |
316 | g_free(transfer->buffer); | |
317 | transfer->buffer = NULL; | |
318 | libusb_free_transfer(transfer); | |
319 | ||
320 | for (i = 0; i < devc->num_transfers; i++) { | |
321 | if (devc->transfers[i] == transfer) { | |
322 | devc->transfers[i] = NULL; | |
323 | break; | |
324 | } | |
325 | } | |
326 | ||
327 | devc->submitted_transfers--; | |
328 | if (devc->submitted_transfers == 0) | |
102f1239 | 329 | finish_acquisition(sdi); |
2f937611 UH |
330 | } |
331 | ||
332 | static void resubmit_transfer(struct libusb_transfer *transfer) | |
333 | { | |
334 | int ret; | |
335 | ||
336 | if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS) | |
337 | return; | |
338 | ||
9615eeb5 | 339 | sr_err("%s: %s", __func__, libusb_error_name(ret)); |
2f937611 | 340 | free_transfer(transfer); |
2f937611 | 341 | |
9615eeb5 BV |
342 | } |
343 | ||
b0acb693 | 344 | static void mso_send_data_proc(struct sr_dev_inst *sdi, |
7e5ccff2 JH |
345 | uint8_t *data, size_t length, size_t sample_width) |
346 | { | |
347 | size_t i; | |
695dc859 | 348 | struct dev_context *devc; |
a6ad49b3 UH |
349 | struct sr_datafeed_analog analog; |
350 | struct sr_analog_encoding encoding; | |
351 | struct sr_analog_meaning meaning; | |
352 | struct sr_analog_spec spec; | |
695dc859 | 353 | |
e91d4ce2 UH |
354 | (void)sample_width; |
355 | ||
695dc859 UH |
356 | devc = sdi->priv; |
357 | ||
7e5ccff2 JH |
358 | length /= 2; |
359 | ||
7e5ccff2 | 360 | /* Send the logic */ |
695dc859 | 361 | for (i = 0; i < length; i++) { |
d9251a2c | 362 | devc->logic_buffer[i] = data[i * 2]; |
7fb90f94 | 363 | /* Rescale to -10V - +10V from 0-255. */ |
2ab0679a | 364 | devc->analog_buffer[i] = (data[i * 2 + 1] - 128.0f) / 12.8f; |
7e5ccff2 JH |
365 | }; |
366 | ||
367 | const struct sr_datafeed_logic logic = { | |
368 | .length = length, | |
369 | .unitsize = 1, | |
370 | .data = devc->logic_buffer | |
371 | }; | |
372 | ||
373 | const struct sr_datafeed_packet logic_packet = { | |
374 | .type = SR_DF_LOGIC, | |
375 | .payload = &logic | |
376 | }; | |
695dc859 UH |
377 | |
378 | sr_session_send(sdi, &logic_packet); | |
7e5ccff2 | 379 | |
0cce2383 | 380 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); |
a6ad49b3 UH |
381 | analog.meaning->channels = devc->enabled_analog_channels; |
382 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
383 | analog.meaning->unit = SR_UNIT_VOLT; | |
384 | analog.meaning->mqflags = 0 /* SR_MQFLAG_DC */; | |
385 | analog.num_samples = length; | |
386 | analog.data = devc->analog_buffer; | |
7e5ccff2 JH |
387 | |
388 | const struct sr_datafeed_packet analog_packet = { | |
a6ad49b3 | 389 | .type = SR_DF_ANALOG, |
7e5ccff2 JH |
390 | .payload = &analog |
391 | }; | |
7e5ccff2 | 392 | |
695dc859 | 393 | sr_session_send(sdi, &analog_packet); |
7e5ccff2 JH |
394 | } |
395 | ||
b0acb693 | 396 | static void la_send_data_proc(struct sr_dev_inst *sdi, |
7b5d1c64 JH |
397 | uint8_t *data, size_t length, size_t sample_width) |
398 | { | |
399 | const struct sr_datafeed_logic logic = { | |
400 | .length = length, | |
401 | .unitsize = sample_width, | |
402 | .data = data | |
403 | }; | |
404 | ||
405 | const struct sr_datafeed_packet packet = { | |
406 | .type = SR_DF_LOGIC, | |
407 | .payload = &logic | |
408 | }; | |
409 | ||
695dc859 | 410 | sr_session_send(sdi, &packet); |
7b5d1c64 JH |
411 | } |
412 | ||
b0acb693 | 413 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) |
2f937611 | 414 | { |
9615eeb5 BV |
415 | struct sr_dev_inst *sdi; |
416 | struct dev_context *devc; | |
2f937611 | 417 | gboolean packet_has_error = FALSE; |
9615eeb5 | 418 | unsigned int num_samples; |
bfc34b9a | 419 | int trigger_offset, cur_sample_count, unitsize, processed_samples; |
7bfcb25c | 420 | int pre_trigger_samples; |
2f937611 | 421 | |
9615eeb5 BV |
422 | sdi = transfer->user_data; |
423 | devc = sdi->priv; | |
2f937611 UH |
424 | |
425 | /* | |
426 | * If acquisition has already ended, just free any queued up | |
427 | * transfer that come in. | |
428 | */ | |
b0ccd64d | 429 | if (devc->acq_aborted) { |
2f937611 UH |
430 | free_transfer(transfer); |
431 | return; | |
432 | } | |
433 | ||
0f262763 UH |
434 | sr_dbg("receive_transfer(): status %s received %d bytes.", |
435 | libusb_error_name(transfer->status), transfer->actual_length); | |
2f937611 UH |
436 | |
437 | /* Save incoming transfer before reusing the transfer struct. */ | |
9615eeb5 BV |
438 | unitsize = devc->sample_wide ? 2 : 1; |
439 | cur_sample_count = transfer->actual_length / unitsize; | |
bfc34b9a | 440 | processed_samples = 0; |
2f937611 UH |
441 | |
442 | switch (transfer->status) { | |
443 | case LIBUSB_TRANSFER_NO_DEVICE: | |
444 | fx2lafw_abort_acquisition(devc); | |
445 | free_transfer(transfer); | |
446 | return; | |
447 | case LIBUSB_TRANSFER_COMPLETED: | |
448 | case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */ | |
449 | break; | |
450 | default: | |
451 | packet_has_error = TRUE; | |
452 | break; | |
453 | } | |
454 | ||
455 | if (transfer->actual_length == 0 || packet_has_error) { | |
456 | devc->empty_transfer_count++; | |
457 | if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
458 | /* | |
459 | * The FX2 gave up. End the acquisition, the frontend | |
460 | * will work out that the samplecount is short. | |
461 | */ | |
462 | fx2lafw_abort_acquisition(devc); | |
463 | free_transfer(transfer); | |
464 | } else { | |
465 | resubmit_transfer(transfer); | |
466 | } | |
467 | return; | |
468 | } else { | |
469 | devc->empty_transfer_count = 0; | |
470 | } | |
bfc34b9a VO |
471 | |
472 | check_trigger: | |
9615eeb5 | 473 | if (devc->trigger_fired) { |
2f663c82 | 474 | if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) { |
9615eeb5 | 475 | /* Send the incoming transfer to the session bus. */ |
bfc34b9a VO |
476 | num_samples = cur_sample_count - processed_samples; |
477 | if (devc->limit_samples && devc->sent_samples + num_samples > devc->limit_samples) | |
9615eeb5 | 478 | num_samples = devc->limit_samples - devc->sent_samples; |
7b5d1c64 | 479 | |
bfc34b9a | 480 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer + processed_samples * unitsize, |
adcb9951 JH |
481 | num_samples * unitsize, unitsize); |
482 | devc->sent_samples += num_samples; | |
bfc34b9a | 483 | processed_samples += num_samples; |
9615eeb5 BV |
484 | } |
485 | } else { | |
335122f0 | 486 | trigger_offset = soft_trigger_logic_check(devc->stl, |
bfc34b9a VO |
487 | transfer->buffer + processed_samples * unitsize, |
488 | transfer->actual_length - processed_samples * unitsize, | |
489 | &pre_trigger_samples); | |
9615eeb5 | 490 | if (trigger_offset > -1) { |
bfc34b9a | 491 | std_session_send_df_frame_begin(sdi); |
7bfcb25c | 492 | devc->sent_samples += pre_trigger_samples; |
bfc34b9a | 493 | num_samples = cur_sample_count - processed_samples - trigger_offset; |
9615eeb5 | 494 | if (devc->limit_samples && |
bfc34b9a | 495 | devc->sent_samples + num_samples > devc->limit_samples) |
9615eeb5 | 496 | num_samples = devc->limit_samples - devc->sent_samples; |
7b5d1c64 | 497 | |
695dc859 | 498 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer |
bfc34b9a | 499 | + processed_samples * unitsize |
695dc859 UH |
500 | + trigger_offset * unitsize, |
501 | num_samples * unitsize, unitsize); | |
9615eeb5 | 502 | devc->sent_samples += num_samples; |
bfc34b9a | 503 | processed_samples += trigger_offset + num_samples; |
9615eeb5 BV |
504 | |
505 | devc->trigger_fired = TRUE; | |
2f937611 | 506 | } |
06b5d7f7 BV |
507 | } |
508 | ||
bfc34b9a VO |
509 | const int frame_ended = devc->limit_samples && (devc->sent_samples >= devc->limit_samples); |
510 | const int final_frame = devc->limit_frames && (devc->num_frames >= (devc->limit_frames - 1)); | |
5f8cf332 | 511 | |
bfc34b9a VO |
512 | if (frame_ended) { |
513 | devc->num_frames++; | |
514 | devc->sent_samples = 0; | |
515 | devc->trigger_fired = FALSE; | |
516 | std_session_send_df_frame_end(sdi); | |
517 | ||
518 | /* There may be another trigger in the remaining data, go back and check for it */ | |
519 | if (processed_samples < cur_sample_count) { | |
520 | /* Reset the trigger stage */ | |
521 | if (devc->stl) | |
522 | devc->stl->cur_stage = 0; | |
523 | else { | |
524 | std_session_send_df_frame_begin(sdi); | |
525 | devc->trigger_fired = TRUE; | |
526 | } | |
527 | if (!final_frame) | |
528 | goto check_trigger; | |
529 | } | |
530 | } | |
531 | if (frame_ended && final_frame) { | |
06b5d7f7 BV |
532 | fx2lafw_abort_acquisition(devc); |
533 | free_transfer(transfer); | |
9615eeb5 BV |
534 | } else |
535 | resubmit_transfer(transfer); | |
2f937611 UH |
536 | } |
537 | ||
b0acb693 JH |
538 | static int configure_channels(const struct sr_dev_inst *sdi) |
539 | { | |
540 | struct dev_context *devc; | |
541 | const GSList *l; | |
542 | int p; | |
543 | struct sr_channel *ch; | |
544 | uint32_t channel_mask = 0, num_analog = 0; | |
545 | ||
546 | devc = sdi->priv; | |
547 | ||
548 | g_slist_free(devc->enabled_analog_channels); | |
549 | devc->enabled_analog_channels = NULL; | |
550 | ||
551 | for (l = sdi->channels, p = 0; l; l = l->next, p++) { | |
552 | ch = l->data; | |
553 | if ((p <= NUM_CHANNELS) && (ch->type == SR_CHANNEL_ANALOG) | |
554 | && (ch->enabled)) { | |
555 | num_analog++; | |
556 | devc->enabled_analog_channels = | |
557 | g_slist_append(devc->enabled_analog_channels, ch); | |
558 | } else { | |
559 | channel_mask |= ch->enabled << p; | |
560 | } | |
561 | } | |
562 | ||
563 | /* | |
564 | * Use wide sampling if either any of the LA channels 8..15 is enabled, | |
565 | * and/or at least one analog channel is enabled. | |
566 | */ | |
567 | devc->sample_wide = channel_mask > 0xff || num_analog > 0; | |
568 | ||
569 | return SR_OK; | |
570 | } | |
571 | ||
2f937611 UH |
572 | static unsigned int to_bytes_per_ms(unsigned int samplerate) |
573 | { | |
574 | return samplerate / 1000; | |
575 | } | |
576 | ||
b0acb693 | 577 | static size_t get_buffer_size(struct dev_context *devc) |
2f937611 UH |
578 | { |
579 | size_t s; | |
580 | ||
581 | /* | |
582 | * The buffer should be large enough to hold 10ms of data and | |
583 | * a multiple of 512. | |
584 | */ | |
585 | s = 10 * to_bytes_per_ms(devc->cur_samplerate); | |
586 | return (s + 511) & ~511; | |
587 | } | |
588 | ||
b0acb693 | 589 | static unsigned int get_number_of_transfers(struct dev_context *devc) |
2f937611 UH |
590 | { |
591 | unsigned int n; | |
592 | ||
593 | /* Total buffer size should be able to hold about 500ms of data. */ | |
594 | n = (500 * to_bytes_per_ms(devc->cur_samplerate) / | |
b0acb693 | 595 | get_buffer_size(devc)); |
2f937611 UH |
596 | |
597 | if (n > NUM_SIMUL_TRANSFERS) | |
598 | return NUM_SIMUL_TRANSFERS; | |
599 | ||
600 | return n; | |
601 | } | |
602 | ||
b0acb693 | 603 | static unsigned int get_timeout(struct dev_context *devc) |
2f937611 UH |
604 | { |
605 | size_t total_size; | |
606 | unsigned int timeout; | |
607 | ||
b0acb693 JH |
608 | total_size = get_buffer_size(devc) * |
609 | get_number_of_transfers(devc); | |
2f937611 UH |
610 | timeout = total_size / to_bytes_per_ms(devc->cur_samplerate); |
611 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
612 | } | |
b0acb693 JH |
613 | |
614 | static int receive_data(int fd, int revents, void *cb_data) | |
615 | { | |
616 | struct timeval tv; | |
617 | struct drv_context *drvc; | |
618 | ||
619 | (void)fd; | |
620 | (void)revents; | |
621 | ||
622 | drvc = (struct drv_context *)cb_data; | |
623 | ||
624 | tv.tv_sec = tv.tv_usec = 0; | |
625 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
626 | ||
627 | return TRUE; | |
628 | } | |
629 | ||
630 | static int start_transfers(const struct sr_dev_inst *sdi) | |
631 | { | |
632 | struct dev_context *devc; | |
633 | struct sr_usb_dev_inst *usb; | |
634 | struct sr_trigger *trigger; | |
635 | struct libusb_transfer *transfer; | |
636 | unsigned int i, num_transfers; | |
637 | int timeout, ret; | |
638 | unsigned char *buf; | |
639 | size_t size; | |
640 | ||
641 | devc = sdi->priv; | |
642 | usb = sdi->conn; | |
643 | ||
644 | devc->sent_samples = 0; | |
645 | devc->acq_aborted = FALSE; | |
646 | devc->empty_transfer_count = 0; | |
647 | ||
648 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
649 | int pre_trigger_samples = 0; | |
650 | if (devc->limit_samples > 0) | |
efad7ccc | 651 | pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100; |
b0acb693 JH |
652 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); |
653 | if (!devc->stl) | |
654 | return SR_ERR_MALLOC; | |
655 | devc->trigger_fired = FALSE; | |
bfc34b9a VO |
656 | } else { |
657 | std_session_send_df_frame_begin(sdi); | |
b0acb693 | 658 | devc->trigger_fired = TRUE; |
bfc34b9a | 659 | } |
b0acb693 JH |
660 | |
661 | num_transfers = get_number_of_transfers(devc); | |
662 | ||
663 | size = get_buffer_size(devc); | |
664 | devc->submitted_transfers = 0; | |
665 | ||
666 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
667 | if (!devc->transfers) { | |
668 | sr_err("USB transfers malloc failed."); | |
669 | return SR_ERR_MALLOC; | |
670 | } | |
671 | ||
672 | timeout = get_timeout(devc); | |
673 | devc->num_transfers = num_transfers; | |
674 | for (i = 0; i < num_transfers; i++) { | |
675 | if (!(buf = g_try_malloc(size))) { | |
676 | sr_err("USB transfer buffer malloc failed."); | |
677 | return SR_ERR_MALLOC; | |
678 | } | |
679 | transfer = libusb_alloc_transfer(0); | |
680 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
681 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
682 | receive_transfer, (void *)sdi, timeout); | |
683 | sr_info("submitting transfer: %d", i); | |
684 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
685 | sr_err("Failed to submit transfer: %s.", | |
686 | libusb_error_name(ret)); | |
687 | libusb_free_transfer(transfer); | |
688 | g_free(buf); | |
689 | fx2lafw_abort_acquisition(devc); | |
690 | return SR_ERR; | |
691 | } | |
692 | devc->transfers[i] = transfer; | |
693 | devc->submitted_transfers++; | |
694 | } | |
695 | ||
696 | /* | |
697 | * If this device has analog channels and at least one of them is | |
698 | * enabled, use mso_send_data_proc() to properly handle the analog | |
699 | * data. Otherwise use la_send_data_proc(). | |
700 | */ | |
701 | if (g_slist_length(devc->enabled_analog_channels) > 0) | |
702 | devc->send_data_proc = mso_send_data_proc; | |
703 | else | |
704 | devc->send_data_proc = la_send_data_proc; | |
705 | ||
706 | std_session_send_df_header(sdi); | |
707 | ||
708 | return SR_OK; | |
709 | } | |
710 | ||
711 | SR_PRIV int fx2lafw_start_acquisition(const struct sr_dev_inst *sdi) | |
712 | { | |
713 | struct sr_dev_driver *di; | |
714 | struct drv_context *drvc; | |
715 | struct dev_context *devc; | |
716 | int timeout, ret; | |
717 | size_t size; | |
718 | ||
b0acb693 JH |
719 | di = sdi->driver; |
720 | drvc = di->context; | |
721 | devc = sdi->priv; | |
722 | ||
723 | devc->ctx = drvc->sr_ctx; | |
5f8cf332 | 724 | devc->num_frames = 0; |
b0acb693 JH |
725 | devc->sent_samples = 0; |
726 | devc->empty_transfer_count = 0; | |
727 | devc->acq_aborted = FALSE; | |
728 | ||
729 | if (configure_channels(sdi) != SR_OK) { | |
730 | sr_err("Failed to configure channels."); | |
731 | return SR_ERR; | |
732 | } | |
733 | ||
734 | timeout = get_timeout(devc); | |
735 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc); | |
736 | ||
737 | size = get_buffer_size(devc); | |
738 | /* Prepare for analog sampling. */ | |
739 | if (g_slist_length(devc->enabled_analog_channels) > 0) { | |
740 | /* We need a buffer half the size of a transfer. */ | |
741 | devc->logic_buffer = g_try_malloc(size / 2); | |
742 | devc->analog_buffer = g_try_malloc( | |
743 | sizeof(float) * size / 2); | |
744 | } | |
745 | start_transfers(sdi); | |
746 | if ((ret = command_start_acquisition(sdi)) != SR_OK) { | |
747 | fx2lafw_abort_acquisition(devc); | |
748 | return ret; | |
749 | } | |
750 | ||
751 | return SR_OK; | |
752 | } |