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