]>
Commit | Line | Data |
---|---|---|
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 | ||
21 | #include <config.h> | |
22 | #include <glib.h> | |
23 | #include <glib/gstdio.h> | |
24 | #include "protocol.h" | |
25 | ||
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 | ||
41 | #define USB_TIMEOUT 100 | |
42 | ||
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, | |
50 | (unsigned char *)vi, sizeof(struct version_info), USB_TIMEOUT); | |
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 | ||
61 | static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid) | |
62 | { | |
63 | struct sr_usb_dev_inst *usb = sdi->conn; | |
64 | libusb_device_handle *devhdl = usb->devhdl; | |
65 | int ret; | |
66 | ||
67 | ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
68 | LIBUSB_ENDPOINT_IN, CMD_GET_REVID_VERSION, 0x0000, 0x0000, | |
69 | revid, 1, USB_TIMEOUT); | |
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 | ||
79 | static int command_start_acquisition(const struct sr_dev_inst *sdi) | |
80 | { | |
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; | |
90 | ||
91 | /* Compute the sample rate. */ | |
92 | if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) { | |
93 | sr_err("Unable to sample at %" PRIu64 "Hz " | |
94 | "when collecting 16-bit samples.", samplerate); | |
95 | return SR_ERR; | |
96 | } | |
97 | ||
98 | delay = 0; | |
99 | cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0; | |
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 | ||
112 | sr_dbg("GPIF delay = %d, clocksource = %sMHz.", delay, | |
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. */ | |
124 | cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT : | |
125 | CMD_START_FLAGS_SAMPLE_8BIT; | |
126 | /* Enable CTL2 clock. */ | |
127 | cmd.flags |= (g_slist_length(devc->enabled_analog_channels) > 0) ? CMD_START_FLAGS_CLK_CTL2 : 0; | |
128 | ||
129 | /* Send the control message. */ | |
130 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR | | |
131 | LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000, | |
132 | (unsigned char *)&cmd, sizeof(cmd), USB_TIMEOUT); | |
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 | 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; | |
150 | int ret = SR_ERR, i, device_count; | |
151 | uint8_t revid; | |
152 | char connection_id[64]; | |
153 | ||
154 | drvc = di->context; | |
155 | devc = sdi->priv; | |
156 | usb = sdi->conn; | |
157 | ||
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++) { | |
166 | libusb_get_device_descriptor(devlist[i], &des); | |
167 | ||
168 | if (des.idVendor != devc->profile->vid | |
169 | || des.idProduct != devc->profile->pid) | |
170 | continue; | |
171 | ||
172 | if ((sdi->status == SR_ST_INITIALIZING) || | |
173 | (sdi->status == SR_ST_INACTIVE)) { | |
174 | /* | |
175 | * Check device by its physical USB bus/port address. | |
176 | */ | |
177 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) | |
178 | continue; | |
179 | ||
180 | if (strcmp(sdi->connection_id, connection_id)) | |
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)); | |
195 | ret = SR_ERR; | |
196 | break; | |
197 | } | |
198 | ||
199 | if (libusb_has_capability(LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) { | |
200 | if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) { | |
201 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) { | |
202 | sr_err("Failed to detach kernel driver: %s.", | |
203 | libusb_error_name(ret)); | |
204 | ret = SR_ERR; | |
205 | break; | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
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 | ||
216 | ret = command_get_revid_version(sdi, &revid); | |
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 | ||
234 | sr_info("Opened device on %d.%d (logical) / %s (physical), " | |
235 | "interface %d, firmware %d.%d.", | |
236 | usb->bus, usb->address, connection_id, | |
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 | ||
242 | ret = SR_OK; | |
243 | ||
244 | break; | |
245 | } | |
246 | ||
247 | libusb_free_device_list(devlist, 1); | |
248 | ||
249 | return ret; | |
250 | } | |
251 | ||
252 | SR_PRIV struct dev_context *fx2lafw_dev_new(void) | |
253 | { | |
254 | struct dev_context *devc; | |
255 | ||
256 | devc = g_malloc0(sizeof(struct dev_context)); | |
257 | devc->profile = NULL; | |
258 | devc->channel_names = NULL; | |
259 | devc->fw_updated = 0; | |
260 | devc->cur_samplerate = 0; | |
261 | devc->limit_frames = 1; | |
262 | devc->limit_samples = 0; | |
263 | devc->capture_ratio = 0; | |
264 | devc->sample_wide = FALSE; | |
265 | devc->num_frames = 0; | |
266 | devc->stl = NULL; | |
267 | ||
268 | return devc; | |
269 | } | |
270 | ||
271 | SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc) | |
272 | { | |
273 | int i; | |
274 | ||
275 | devc->acq_aborted = TRUE; | |
276 | ||
277 | for (i = devc->num_transfers - 1; i >= 0; i--) { | |
278 | if (devc->transfers[i]) | |
279 | libusb_cancel_transfer(devc->transfers[i]); | |
280 | } | |
281 | } | |
282 | ||
283 | static void finish_acquisition(struct sr_dev_inst *sdi) | |
284 | { | |
285 | struct dev_context *devc; | |
286 | ||
287 | devc = sdi->priv; | |
288 | ||
289 | std_session_send_df_end(sdi); | |
290 | ||
291 | usb_source_remove(sdi->session, devc->ctx); | |
292 | ||
293 | devc->num_transfers = 0; | |
294 | g_free(devc->transfers); | |
295 | ||
296 | /* Free the deinterlace buffers if we had them. */ | |
297 | if (g_slist_length(devc->enabled_analog_channels) > 0) { | |
298 | g_free(devc->logic_buffer); | |
299 | g_free(devc->analog_buffer); | |
300 | } | |
301 | ||
302 | if (devc->stl) { | |
303 | soft_trigger_logic_free(devc->stl); | |
304 | devc->stl = NULL; | |
305 | } | |
306 | } | |
307 | ||
308 | static void free_transfer(struct libusb_transfer *transfer) | |
309 | { | |
310 | struct sr_dev_inst *sdi; | |
311 | struct dev_context *devc; | |
312 | unsigned int i; | |
313 | ||
314 | sdi = transfer->user_data; | |
315 | devc = sdi->priv; | |
316 | ||
317 | g_free(transfer->buffer); | |
318 | transfer->buffer = NULL; | |
319 | libusb_free_transfer(transfer); | |
320 | ||
321 | for (i = 0; i < devc->num_transfers; i++) { | |
322 | if (devc->transfers[i] == transfer) { | |
323 | devc->transfers[i] = NULL; | |
324 | break; | |
325 | } | |
326 | } | |
327 | ||
328 | devc->submitted_transfers--; | |
329 | if (devc->submitted_transfers == 0) | |
330 | finish_acquisition(sdi); | |
331 | } | |
332 | ||
333 | static void resubmit_transfer(struct libusb_transfer *transfer) | |
334 | { | |
335 | int ret; | |
336 | ||
337 | if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS) | |
338 | return; | |
339 | ||
340 | sr_err("%s: %s", __func__, libusb_error_name(ret)); | |
341 | free_transfer(transfer); | |
342 | ||
343 | } | |
344 | ||
345 | static void mso_send_data_proc(struct sr_dev_inst *sdi, | |
346 | uint8_t *data, size_t length, size_t sample_width) | |
347 | { | |
348 | size_t i; | |
349 | struct dev_context *devc; | |
350 | struct sr_datafeed_analog analog; | |
351 | struct sr_analog_encoding encoding; | |
352 | struct sr_analog_meaning meaning; | |
353 | struct sr_analog_spec spec; | |
354 | ||
355 | (void)sample_width; | |
356 | ||
357 | devc = sdi->priv; | |
358 | ||
359 | length /= 2; | |
360 | ||
361 | /* Send the logic */ | |
362 | for (i = 0; i < length; i++) { | |
363 | devc->logic_buffer[i] = data[i * 2]; | |
364 | /* Rescale to -10V - +10V from 0-255. */ | |
365 | devc->analog_buffer[i] = (data[i * 2 + 1] - 128.0f) / 12.8f; | |
366 | }; | |
367 | ||
368 | const struct sr_datafeed_logic logic = { | |
369 | .length = length, | |
370 | .unitsize = 1, | |
371 | .data = devc->logic_buffer | |
372 | }; | |
373 | ||
374 | const struct sr_datafeed_packet logic_packet = { | |
375 | .type = SR_DF_LOGIC, | |
376 | .payload = &logic | |
377 | }; | |
378 | ||
379 | sr_session_send(sdi, &logic_packet); | |
380 | ||
381 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); | |
382 | analog.meaning->channels = devc->enabled_analog_channels; | |
383 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
384 | analog.meaning->unit = SR_UNIT_VOLT; | |
385 | analog.meaning->mqflags = 0 /* SR_MQFLAG_DC */; | |
386 | analog.num_samples = length; | |
387 | analog.data = devc->analog_buffer; | |
388 | ||
389 | const struct sr_datafeed_packet analog_packet = { | |
390 | .type = SR_DF_ANALOG, | |
391 | .payload = &analog | |
392 | }; | |
393 | ||
394 | sr_session_send(sdi, &analog_packet); | |
395 | } | |
396 | ||
397 | static void la_send_data_proc(struct sr_dev_inst *sdi, | |
398 | uint8_t *data, size_t length, size_t sample_width) | |
399 | { | |
400 | const struct sr_datafeed_logic logic = { | |
401 | .length = length, | |
402 | .unitsize = sample_width, | |
403 | .data = data | |
404 | }; | |
405 | ||
406 | const struct sr_datafeed_packet packet = { | |
407 | .type = SR_DF_LOGIC, | |
408 | .payload = &logic | |
409 | }; | |
410 | ||
411 | sr_session_send(sdi, &packet); | |
412 | } | |
413 | ||
414 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) | |
415 | { | |
416 | struct sr_dev_inst *sdi; | |
417 | struct dev_context *devc; | |
418 | gboolean packet_has_error = FALSE; | |
419 | unsigned int num_samples; | |
420 | int trigger_offset, cur_sample_count, unitsize, processed_samples; | |
421 | int pre_trigger_samples; | |
422 | ||
423 | sdi = transfer->user_data; | |
424 | devc = sdi->priv; | |
425 | ||
426 | /* | |
427 | * If acquisition has already ended, just free any queued up | |
428 | * transfer that come in. | |
429 | */ | |
430 | if (devc->acq_aborted) { | |
431 | free_transfer(transfer); | |
432 | return; | |
433 | } | |
434 | ||
435 | sr_dbg("receive_transfer(): status %s received %d bytes.", | |
436 | libusb_error_name(transfer->status), transfer->actual_length); | |
437 | ||
438 | /* Save incoming transfer before reusing the transfer struct. */ | |
439 | unitsize = devc->sample_wide ? 2 : 1; | |
440 | cur_sample_count = transfer->actual_length / unitsize; | |
441 | processed_samples = 0; | |
442 | ||
443 | switch (transfer->status) { | |
444 | case LIBUSB_TRANSFER_NO_DEVICE: | |
445 | fx2lafw_abort_acquisition(devc); | |
446 | free_transfer(transfer); | |
447 | return; | |
448 | case LIBUSB_TRANSFER_COMPLETED: | |
449 | case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */ | |
450 | break; | |
451 | default: | |
452 | packet_has_error = TRUE; | |
453 | break; | |
454 | } | |
455 | ||
456 | if (transfer->actual_length == 0 || packet_has_error) { | |
457 | devc->empty_transfer_count++; | |
458 | if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) { | |
459 | /* | |
460 | * The FX2 gave up. End the acquisition, the frontend | |
461 | * will work out that the samplecount is short. | |
462 | */ | |
463 | fx2lafw_abort_acquisition(devc); | |
464 | free_transfer(transfer); | |
465 | } else { | |
466 | resubmit_transfer(transfer); | |
467 | } | |
468 | return; | |
469 | } else { | |
470 | devc->empty_transfer_count = 0; | |
471 | } | |
472 | ||
473 | check_trigger: | |
474 | if (devc->trigger_fired) { | |
475 | if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) { | |
476 | /* Send the incoming transfer to the session bus. */ | |
477 | num_samples = cur_sample_count - processed_samples; | |
478 | if (devc->limit_samples && devc->sent_samples + num_samples > devc->limit_samples) | |
479 | num_samples = devc->limit_samples - devc->sent_samples; | |
480 | ||
481 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer + processed_samples * unitsize, | |
482 | num_samples * unitsize, unitsize); | |
483 | devc->sent_samples += num_samples; | |
484 | processed_samples += num_samples; | |
485 | } | |
486 | } else { | |
487 | trigger_offset = soft_trigger_logic_check(devc->stl, | |
488 | transfer->buffer + processed_samples * unitsize, | |
489 | transfer->actual_length - processed_samples * unitsize, | |
490 | &pre_trigger_samples); | |
491 | if (trigger_offset > -1) { | |
492 | std_session_send_df_frame_begin(sdi); | |
493 | devc->sent_samples += pre_trigger_samples; | |
494 | num_samples = cur_sample_count - processed_samples - trigger_offset; | |
495 | if (devc->limit_samples && | |
496 | devc->sent_samples + num_samples > devc->limit_samples) | |
497 | num_samples = devc->limit_samples - devc->sent_samples; | |
498 | ||
499 | devc->send_data_proc(sdi, (uint8_t *)transfer->buffer | |
500 | + processed_samples * unitsize | |
501 | + trigger_offset * unitsize, | |
502 | num_samples * unitsize, unitsize); | |
503 | devc->sent_samples += num_samples; | |
504 | processed_samples += trigger_offset + num_samples; | |
505 | ||
506 | devc->trigger_fired = TRUE; | |
507 | } | |
508 | } | |
509 | ||
510 | const int frame_ended = devc->limit_samples && (devc->sent_samples >= devc->limit_samples); | |
511 | const int final_frame = devc->limit_frames && (devc->num_frames >= (devc->limit_frames - 1)); | |
512 | ||
513 | if (frame_ended) { | |
514 | devc->num_frames++; | |
515 | devc->sent_samples = 0; | |
516 | devc->trigger_fired = FALSE; | |
517 | std_session_send_df_frame_end(sdi); | |
518 | ||
519 | /* There may be another trigger in the remaining data, go back and check for it */ | |
520 | if (processed_samples < cur_sample_count) { | |
521 | /* Reset the trigger stage */ | |
522 | if (devc->stl) | |
523 | devc->stl->cur_stage = 0; | |
524 | else { | |
525 | std_session_send_df_frame_begin(sdi); | |
526 | devc->trigger_fired = TRUE; | |
527 | } | |
528 | if (!final_frame) | |
529 | goto check_trigger; | |
530 | } | |
531 | } | |
532 | if (frame_ended && final_frame) { | |
533 | fx2lafw_abort_acquisition(devc); | |
534 | free_transfer(transfer); | |
535 | } else | |
536 | resubmit_transfer(transfer); | |
537 | } | |
538 | ||
539 | static int configure_channels(const struct sr_dev_inst *sdi) | |
540 | { | |
541 | struct dev_context *devc; | |
542 | const GSList *l; | |
543 | int p; | |
544 | struct sr_channel *ch; | |
545 | uint32_t channel_mask = 0, num_analog = 0; | |
546 | ||
547 | devc = sdi->priv; | |
548 | ||
549 | g_slist_free(devc->enabled_analog_channels); | |
550 | devc->enabled_analog_channels = NULL; | |
551 | ||
552 | for (l = sdi->channels, p = 0; l; l = l->next, p++) { | |
553 | ch = l->data; | |
554 | if ((p <= NUM_CHANNELS) && (ch->type == SR_CHANNEL_ANALOG) | |
555 | && (ch->enabled)) { | |
556 | num_analog++; | |
557 | devc->enabled_analog_channels = | |
558 | g_slist_append(devc->enabled_analog_channels, ch); | |
559 | } else { | |
560 | channel_mask |= ch->enabled << p; | |
561 | } | |
562 | } | |
563 | ||
564 | /* | |
565 | * Use wide sampling if either any of the LA channels 8..15 is enabled, | |
566 | * and/or at least one analog channel is enabled. | |
567 | */ | |
568 | devc->sample_wide = channel_mask > 0xff || num_analog > 0; | |
569 | ||
570 | return SR_OK; | |
571 | } | |
572 | ||
573 | static unsigned int to_bytes_per_ms(unsigned int samplerate) | |
574 | { | |
575 | return samplerate / 1000; | |
576 | } | |
577 | ||
578 | static size_t get_buffer_size(struct dev_context *devc) | |
579 | { | |
580 | size_t s; | |
581 | ||
582 | /* | |
583 | * The buffer should be large enough to hold 10ms of data and | |
584 | * a multiple of 512. | |
585 | */ | |
586 | s = 10 * to_bytes_per_ms(devc->cur_samplerate); | |
587 | return (s + 511) & ~511; | |
588 | } | |
589 | ||
590 | static unsigned int get_number_of_transfers(struct dev_context *devc) | |
591 | { | |
592 | unsigned int n; | |
593 | ||
594 | /* Total buffer size should be able to hold about 500ms of data. */ | |
595 | n = (500 * to_bytes_per_ms(devc->cur_samplerate) / | |
596 | get_buffer_size(devc)); | |
597 | ||
598 | if (n > NUM_SIMUL_TRANSFERS) | |
599 | return NUM_SIMUL_TRANSFERS; | |
600 | ||
601 | return n; | |
602 | } | |
603 | ||
604 | static unsigned int get_timeout(struct dev_context *devc) | |
605 | { | |
606 | size_t total_size; | |
607 | unsigned int timeout; | |
608 | ||
609 | total_size = get_buffer_size(devc) * | |
610 | get_number_of_transfers(devc); | |
611 | timeout = total_size / to_bytes_per_ms(devc->cur_samplerate); | |
612 | return timeout + timeout / 4; /* Leave a headroom of 25% percent. */ | |
613 | } | |
614 | ||
615 | static int receive_data(int fd, int revents, void *cb_data) | |
616 | { | |
617 | struct timeval tv; | |
618 | struct drv_context *drvc; | |
619 | ||
620 | (void)fd; | |
621 | (void)revents; | |
622 | ||
623 | drvc = (struct drv_context *)cb_data; | |
624 | ||
625 | tv.tv_sec = tv.tv_usec = 0; | |
626 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
627 | ||
628 | return TRUE; | |
629 | } | |
630 | ||
631 | static int start_transfers(const struct sr_dev_inst *sdi) | |
632 | { | |
633 | struct dev_context *devc; | |
634 | struct sr_usb_dev_inst *usb; | |
635 | struct sr_trigger *trigger; | |
636 | struct libusb_transfer *transfer; | |
637 | unsigned int i, num_transfers; | |
638 | int timeout, ret; | |
639 | unsigned char *buf; | |
640 | size_t size; | |
641 | ||
642 | devc = sdi->priv; | |
643 | usb = sdi->conn; | |
644 | ||
645 | devc->sent_samples = 0; | |
646 | devc->acq_aborted = FALSE; | |
647 | devc->empty_transfer_count = 0; | |
648 | ||
649 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
650 | int pre_trigger_samples = 0; | |
651 | if (devc->limit_samples > 0) | |
652 | pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100; | |
653 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); | |
654 | if (!devc->stl) | |
655 | return SR_ERR_MALLOC; | |
656 | devc->trigger_fired = FALSE; | |
657 | } else { | |
658 | std_session_send_df_frame_begin(sdi); | |
659 | devc->trigger_fired = TRUE; | |
660 | } | |
661 | ||
662 | num_transfers = get_number_of_transfers(devc); | |
663 | ||
664 | size = get_buffer_size(devc); | |
665 | devc->submitted_transfers = 0; | |
666 | ||
667 | devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers); | |
668 | if (!devc->transfers) { | |
669 | sr_err("USB transfers malloc failed."); | |
670 | return SR_ERR_MALLOC; | |
671 | } | |
672 | ||
673 | timeout = get_timeout(devc); | |
674 | devc->num_transfers = num_transfers; | |
675 | for (i = 0; i < num_transfers; i++) { | |
676 | if (!(buf = g_try_malloc(size))) { | |
677 | sr_err("USB transfer buffer malloc failed."); | |
678 | return SR_ERR_MALLOC; | |
679 | } | |
680 | transfer = libusb_alloc_transfer(0); | |
681 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
682 | 2 | LIBUSB_ENDPOINT_IN, buf, size, | |
683 | receive_transfer, (void *)sdi, timeout); | |
684 | sr_info("submitting transfer: %d", i); | |
685 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
686 | sr_err("Failed to submit transfer: %s.", | |
687 | libusb_error_name(ret)); | |
688 | libusb_free_transfer(transfer); | |
689 | g_free(buf); | |
690 | fx2lafw_abort_acquisition(devc); | |
691 | return SR_ERR; | |
692 | } | |
693 | devc->transfers[i] = transfer; | |
694 | devc->submitted_transfers++; | |
695 | } | |
696 | ||
697 | /* | |
698 | * If this device has analog channels and at least one of them is | |
699 | * enabled, use mso_send_data_proc() to properly handle the analog | |
700 | * data. Otherwise use la_send_data_proc(). | |
701 | */ | |
702 | if (g_slist_length(devc->enabled_analog_channels) > 0) | |
703 | devc->send_data_proc = mso_send_data_proc; | |
704 | else | |
705 | devc->send_data_proc = la_send_data_proc; | |
706 | ||
707 | std_session_send_df_header(sdi); | |
708 | ||
709 | return SR_OK; | |
710 | } | |
711 | ||
712 | SR_PRIV int fx2lafw_start_acquisition(const struct sr_dev_inst *sdi) | |
713 | { | |
714 | struct sr_dev_driver *di; | |
715 | struct drv_context *drvc; | |
716 | struct dev_context *devc; | |
717 | int timeout, ret; | |
718 | size_t size; | |
719 | ||
720 | di = sdi->driver; | |
721 | drvc = di->context; | |
722 | devc = sdi->priv; | |
723 | ||
724 | devc->ctx = drvc->sr_ctx; | |
725 | devc->num_frames = 0; | |
726 | devc->sent_samples = 0; | |
727 | devc->empty_transfer_count = 0; | |
728 | devc->acq_aborted = FALSE; | |
729 | ||
730 | if (configure_channels(sdi) != SR_OK) { | |
731 | sr_err("Failed to configure channels."); | |
732 | return SR_ERR; | |
733 | } | |
734 | ||
735 | timeout = get_timeout(devc); | |
736 | usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc); | |
737 | ||
738 | size = get_buffer_size(devc); | |
739 | /* Prepare for analog sampling. */ | |
740 | if (g_slist_length(devc->enabled_analog_channels) > 0) { | |
741 | /* We need a buffer half the size of a transfer. */ | |
742 | devc->logic_buffer = g_try_malloc(size / 2); | |
743 | devc->analog_buffer = g_try_malloc( | |
744 | sizeof(float) * size / 2); | |
745 | } | |
746 | start_transfers(sdi); | |
747 | if ((ret = command_start_acquisition(sdi)) != SR_OK) { | |
748 | fx2lafw_abort_acquisition(devc); | |
749 | return ret; | |
750 | } | |
751 | ||
752 | return SR_OK; | |
753 | } |