]>
Commit | Line | Data |
---|---|---|
16e76bae MS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include "protocol.h" | |
21 | ||
e52e712d MS |
22 | extern struct sr_dev_driver ikalogic_scanalogic2_driver_info; |
23 | static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info; | |
24 | ||
25 | extern uint64_t ikalogic_scanalogic2_samplerates[NUM_SAMPLERATES]; | |
26 | ||
27 | static void stop_acquisition(struct sr_dev_inst *sdi) | |
16e76bae | 28 | { |
e52e712d MS |
29 | struct dev_context *devc; |
30 | struct sr_datafeed_packet packet; | |
31 | unsigned int i; | |
32 | ||
33 | devc = sdi->priv; | |
34 | ||
35 | /* Remove USB file descriptors from polling. */ | |
36 | for (i = 0; i < devc->num_usbfd; i++) | |
37 | sr_source_remove(devc->usbfd[i]); | |
38 | ||
39 | g_free(devc->usbfd); | |
16e76bae | 40 | |
e52e712d MS |
41 | packet.type = SR_DF_END; |
42 | sr_session_send(devc->cb_data, &packet); | |
43 | ||
44 | sdi->status = SR_ST_ACTIVE; | |
45 | } | |
46 | ||
47 | static void abort_acquisition(struct sr_dev_inst *sdi) | |
48 | { | |
16e76bae | 49 | struct dev_context *devc; |
e52e712d MS |
50 | struct sr_datafeed_packet packet; |
51 | unsigned int i; | |
52 | ||
53 | devc = sdi->priv; | |
54 | ||
55 | /* Remove USB file descriptors from polling. */ | |
56 | for (i = 0; i < devc->num_usbfd; i++) | |
57 | sr_source_remove(devc->usbfd[i]); | |
58 | ||
59 | g_free(devc->usbfd); | |
60 | ||
61 | packet.type = SR_DF_END; | |
62 | sr_session_send(devc->cb_data, &packet); | |
63 | ||
64 | sdi->driver->dev_close(sdi); | |
65 | } | |
66 | ||
67 | static void buffer_sample_data(const struct sr_dev_inst *sdi) | |
68 | { | |
69 | struct dev_context *devc; | |
70 | unsigned int offset, packet_length; | |
71 | ||
72 | devc = sdi->priv; | |
73 | ||
74 | if (devc->probes[devc->channel]->enabled) { | |
75 | offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES; | |
76 | ||
77 | /* | |
78 | * Determine the packet length to ensure that the last packet | |
79 | * will not exceed the buffer size. | |
80 | */ | |
81 | packet_length = MIN(PACKET_NUM_SAMPLE_BYTES, | |
82 | MAX_DEV_SAMPLE_BYTES - offset); | |
83 | ||
84 | /* | |
85 | * Skip the first 4 bytes of the source buffer because they | |
86 | * contain channel and packet information only. | |
87 | */ | |
88 | memcpy(devc->sample_buffer[devc->channel] + offset, | |
89 | devc->xfer_data_in + 4, packet_length); | |
90 | } | |
91 | } | |
92 | ||
93 | static void process_sample_data(const struct sr_dev_inst *sdi) | |
94 | { | |
95 | struct dev_context *devc; | |
96 | struct sr_datafeed_packet packet; | |
97 | struct sr_datafeed_logic logic; | |
98 | uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_PROBES]; | |
99 | uint16_t offset, n = 0; | |
100 | int8_t k; | |
101 | ||
102 | devc = sdi->priv; | |
103 | offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES; | |
104 | ||
105 | /* | |
106 | * Array of pointers to the sample data of all channels up to the last | |
107 | * enabled one for an uniform access to them. Note that the currently | |
108 | * received samples always belong to the last enabled channel. | |
109 | */ | |
110 | for (i = 0; i < devc->num_enabled_probes - 1; i++) | |
111 | ptr[i] = devc->sample_buffer[devc->probe_map[i]] + offset; | |
112 | ||
113 | /* | |
c824eb63 UH |
114 | * Skip the first 4 bytes of the buffer because they contain channel |
115 | * and packet information only. | |
e52e712d MS |
116 | */ |
117 | ptr[i] = devc->xfer_data_in + 4; | |
118 | ||
119 | for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) { | |
120 | /* Stop processing if all requested samples are processed. */ | |
121 | if (devc->samples_processed == devc->limit_samples) | |
122 | break; | |
123 | ||
124 | k = 7; | |
125 | ||
126 | if (devc->samples_processed == 0) { | |
127 | /* | |
128 | * Adjust the position of the first sample to be | |
129 | * processed because possibly more samples than | |
130 | * necessary might have been aquired. This is because | |
c824eb63 UH |
131 | * the number of aquired samples is always rounded up |
132 | * to a multiple of 8. | |
e52e712d MS |
133 | */ |
134 | k = k - (devc->pre_trigger_bytes * 8) + | |
135 | devc->pre_trigger_samples; | |
136 | ||
c824eb63 | 137 | sr_dbg("Start processing at sample: %d.", 7 - k); |
e52e712d MS |
138 | |
139 | /* | |
c824eb63 UH |
140 | * Send the trigger before the first sample is |
141 | * processed if no pre trigger samples were calculated | |
142 | * through the capture ratio. | |
e52e712d MS |
143 | */ |
144 | if (devc->trigger_type != TRIGGER_TYPE_NONE && | |
145 | devc->pre_trigger_samples == 0) { | |
146 | packet.type = SR_DF_TRIGGER; | |
147 | sr_session_send(devc->cb_data, &packet); | |
148 | } | |
149 | } | |
150 | ||
151 | for (; k >= 0; k--) { | |
152 | /* | |
153 | * Stop processing if all requested samples are | |
154 | * processed. | |
155 | */ | |
156 | if (devc->samples_processed == devc->limit_samples) | |
157 | break; | |
158 | ||
159 | buffer[n] = 0; | |
160 | ||
161 | /* | |
162 | * Extract the current sample for each enabled channel | |
163 | * and store them in the buffer. | |
164 | */ | |
165 | for (j = 0; j < devc->num_enabled_probes; j++) { | |
166 | tmp = (ptr[j][i] & (1 << k)) >> k; | |
167 | buffer[n] |= tmp << devc->probe_map[j]; | |
168 | } | |
169 | ||
170 | n++; | |
171 | devc->samples_processed++; | |
172 | ||
173 | /* | |
174 | * Send all processed samples and the trigger if the | |
175 | * number of processed samples reaches the calculated | |
176 | * number of pre trigger samples. | |
177 | */ | |
178 | if (devc->samples_processed == devc->pre_trigger_samples && | |
179 | devc->trigger_type != TRIGGER_TYPE_NONE) { | |
180 | packet.type = SR_DF_LOGIC; | |
181 | packet.payload = &logic; | |
182 | logic.length = n; | |
183 | logic.unitsize = 1; | |
184 | logic.data = buffer; | |
185 | sr_session_send(devc->cb_data, &packet); | |
186 | ||
187 | packet.type = SR_DF_TRIGGER; | |
188 | sr_session_send(devc->cb_data, &packet); | |
189 | ||
190 | n = 0; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | if (n > 0) { | |
196 | packet.type = SR_DF_LOGIC; | |
197 | packet.payload = &logic; | |
198 | logic.length = n; | |
199 | logic.unitsize = 1; | |
200 | logic.data = buffer; | |
201 | sr_session_send(devc->cb_data, &packet); | |
202 | } | |
203 | } | |
204 | ||
205 | SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, | |
206 | void *cb_data) | |
207 | { | |
208 | struct sr_dev_inst *sdi; | |
209 | struct dev_context *devc; | |
210 | struct drv_context *drvc; | |
211 | struct timeval tv; | |
212 | int64_t current_time, time_elapsed; | |
213 | int ret = 0; | |
214 | ||
215 | (void)fd; | |
216 | (void)revents; | |
16e76bae MS |
217 | |
218 | if (!(sdi = cb_data)) | |
219 | return TRUE; | |
220 | ||
221 | if (!(devc = sdi->priv)) | |
222 | return TRUE; | |
223 | ||
e52e712d MS |
224 | drvc = di->priv; |
225 | current_time = g_get_monotonic_time(); | |
226 | ||
227 | if (devc->state == STATE_WAIT_DATA_READY && | |
228 | !devc->wait_data_ready_locked) { | |
229 | time_elapsed = current_time - devc->wait_data_ready_time; | |
230 | ||
231 | /* | |
c824eb63 UH |
232 | * Check here for stopping in addition to the transfer |
233 | * callback functions to avoid waiting until the | |
234 | * WAIT_DATA_READY_INTERVAL has expired. | |
e52e712d MS |
235 | */ |
236 | if (sdi->status == SR_ST_STOPPING) { | |
237 | if (!devc->stopping_in_progress) { | |
238 | devc->next_state = STATE_RESET_AND_IDLE; | |
239 | devc->stopping_in_progress = TRUE; | |
240 | ret = libusb_submit_transfer(devc->xfer_in); | |
241 | } | |
242 | } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) { | |
243 | devc->wait_data_ready_locked = TRUE; | |
244 | ret = libusb_submit_transfer(devc->xfer_in); | |
245 | } | |
246 | } | |
247 | ||
248 | if (ret != 0) { | |
c824eb63 | 249 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); |
e52e712d MS |
250 | abort_acquisition(sdi); |
251 | return TRUE; | |
252 | } | |
253 | ||
254 | tv.tv_sec = 0; | |
255 | tv.tv_usec = 0; | |
256 | ||
257 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, | |
258 | NULL); | |
259 | ||
260 | /* Check if an error occurred on a transfer. */ | |
c824eb63 | 261 | if (devc->transfer_error) |
e52e712d | 262 | abort_acquisition(sdi); |
16e76bae MS |
263 | |
264 | return TRUE; | |
265 | } | |
e52e712d MS |
266 | |
267 | SR_PRIV void ikalogic_scanalogic2_receive_transfer_in( | |
268 | struct libusb_transfer *transfer) | |
269 | { | |
270 | struct sr_dev_inst *sdi; | |
271 | struct dev_context *devc; | |
272 | uint8_t last_channel; | |
273 | int ret = 0; | |
274 | ||
275 | sdi = transfer->user_data; | |
276 | devc = sdi->priv; | |
277 | ||
278 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { | |
c824eb63 | 279 | sr_err("Transfer to device failed: %i.", transfer->status); |
e52e712d MS |
280 | devc->transfer_error = TRUE; |
281 | return; | |
282 | } | |
283 | ||
284 | if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) { | |
285 | devc->next_state = STATE_RESET_AND_IDLE; | |
286 | devc->stopping_in_progress = TRUE; | |
287 | ||
288 | if (libusb_submit_transfer(devc->xfer_in) != 0) { | |
c824eb63 | 289 | sr_err("Submit transfer failed: %s.", |
e52e712d MS |
290 | libusb_error_name(ret)); |
291 | devc->transfer_error = TRUE; | |
292 | } | |
293 | ||
294 | return; | |
295 | } | |
296 | ||
c824eb63 | 297 | sr_spew("State changed from %i to %i.", devc->state, devc->next_state); |
e52e712d MS |
298 | devc->state = devc->next_state; |
299 | ||
300 | if (devc->state == STATE_WAIT_DATA_READY) { | |
301 | /* Check if the received data are a valid device status. */ | |
302 | if (devc->xfer_data_in[0] == 0x05) { | |
303 | if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER) | |
304 | sr_dbg("Waiting for trigger."); | |
305 | else if (devc->xfer_data_in[1] == STATUS_SAMPLING) | |
306 | sr_dbg("Sampling in progress."); | |
307 | } | |
308 | ||
309 | /* | |
310 | * Check if the received data are a valid device status and the | |
311 | * sample data are ready. | |
312 | */ | |
313 | if (devc->xfer_data_in[0] == 0x05 && | |
314 | devc->xfer_data_in[1] == STATUS_DATA_READY) { | |
315 | devc->next_state = STATE_RECEIVE_DATA; | |
316 | ret = libusb_submit_transfer(transfer); | |
317 | } else { | |
318 | devc->wait_data_ready_locked = FALSE; | |
319 | devc->wait_data_ready_time = g_get_monotonic_time(); | |
320 | } | |
321 | } else if (devc->state == STATE_RECEIVE_DATA) { | |
322 | last_channel = devc->probe_map[devc->num_enabled_probes - 1]; | |
323 | ||
324 | if (devc->channel < last_channel) { | |
325 | buffer_sample_data(sdi); | |
326 | } else if (devc->channel == last_channel) { | |
327 | process_sample_data(sdi); | |
328 | } else { | |
329 | /* | |
330 | * Stop acquisition because all samples of enabled | |
331 | * probes are processed. | |
332 | */ | |
333 | devc->next_state = STATE_RESET_AND_IDLE; | |
334 | } | |
335 | ||
336 | devc->sample_packet++; | |
337 | devc->sample_packet %= devc->num_sample_packets; | |
338 | ||
339 | if (devc->sample_packet == 0) | |
340 | devc->channel++; | |
341 | ||
342 | ret = libusb_submit_transfer(transfer); | |
343 | } else if (devc->state == STATE_RESET_AND_IDLE) { | |
344 | /* Check if the received data are a valid device status. */ | |
345 | if (devc->xfer_data_in[0] == 0x05) { | |
346 | if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) { | |
347 | devc->next_state = STATE_IDLE; | |
348 | devc->xfer_data_out[0] = CMD_IDLE; | |
349 | } else { | |
350 | devc->next_state = STATE_WAIT_DEVICE_READY; | |
351 | devc->xfer_data_out[0] = CMD_RESET; | |
352 | } | |
353 | ||
354 | ret = libusb_submit_transfer(devc->xfer_out); | |
355 | } else { | |
356 | /* | |
c824eb63 UH |
357 | * The received device status is invalid which |
358 | * indicates that the device is not ready to accept | |
359 | * commands. Request a new device status until a valid | |
360 | * device status is received. | |
e52e712d MS |
361 | */ |
362 | ret = libusb_submit_transfer(transfer); | |
363 | } | |
364 | } else if (devc->state == STATE_WAIT_DEVICE_READY) { | |
365 | /* Check if the received data are a valid device status. */ | |
366 | if (devc->xfer_data_in[0] == 0x05) { | |
367 | if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) { | |
368 | devc->next_state = STATE_IDLE; | |
369 | devc->xfer_data_out[0] = CMD_IDLE; | |
370 | } else { | |
371 | /* | |
372 | * The received device status is valid but the | |
373 | * device is not ready. Probably the device did | |
374 | * not recognize the last reset. Reset the | |
375 | * device again. | |
376 | */ | |
377 | devc->xfer_data_out[0] = CMD_RESET; | |
378 | } | |
379 | ||
380 | ret = libusb_submit_transfer(devc->xfer_out); | |
381 | } else { | |
382 | /* | |
383 | * The device is not ready and therefore not able to | |
c824eb63 UH |
384 | * change to the idle state. Request a new device |
385 | * status until the device is ready. | |
e52e712d MS |
386 | */ |
387 | ret = libusb_submit_transfer(transfer); | |
388 | } | |
389 | } | |
390 | ||
391 | if (ret != 0) { | |
c824eb63 | 392 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); |
e52e712d MS |
393 | devc->transfer_error = TRUE; |
394 | } | |
395 | } | |
396 | ||
397 | SR_PRIV void ikalogic_scanalogic2_receive_transfer_out( | |
398 | struct libusb_transfer *transfer) | |
399 | { | |
400 | struct sr_dev_inst *sdi; | |
401 | struct dev_context *devc; | |
402 | int ret = 0; | |
403 | ||
404 | sdi = transfer->user_data; | |
405 | devc = sdi->priv; | |
406 | ||
407 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { | |
c824eb63 | 408 | sr_err("Transfer to device failed: %i.", transfer->status); |
e52e712d MS |
409 | devc->transfer_error = TRUE; |
410 | return; | |
411 | } | |
412 | ||
413 | if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) { | |
414 | devc->next_state = STATE_RESET_AND_IDLE; | |
415 | devc->stopping_in_progress = TRUE; | |
416 | ||
417 | if (libusb_submit_transfer(devc->xfer_in) != 0) { | |
c824eb63 | 418 | sr_err("Submit transfer failed: %s.", |
e52e712d MS |
419 | libusb_error_name(ret)); |
420 | ||
421 | devc->transfer_error = TRUE; | |
422 | } | |
423 | ||
424 | return; | |
425 | } | |
426 | ||
c824eb63 | 427 | sr_spew("State changed from %i to %i.", devc->state, devc->next_state); |
e52e712d MS |
428 | devc->state = devc->next_state; |
429 | ||
430 | if (devc->state == STATE_IDLE) { | |
431 | stop_acquisition(sdi); | |
432 | } else if (devc->state == STATE_SAMPLE) { | |
433 | devc->next_state = STATE_WAIT_DATA_READY; | |
434 | ret = libusb_submit_transfer(devc->xfer_in); | |
435 | } else if (devc->state == STATE_WAIT_DEVICE_READY) { | |
436 | ret = libusb_submit_transfer(devc->xfer_in); | |
437 | } | |
438 | ||
439 | if (ret != 0) { | |
c824eb63 | 440 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); |
e52e712d MS |
441 | devc->transfer_error = TRUE; |
442 | } | |
443 | } | |
444 | ||
445 | SR_PRIV int ikalogic_scanalogic2_set_samplerate(const struct sr_dev_inst *sdi, | |
446 | uint64_t samplerate) | |
447 | { | |
448 | struct dev_context *devc; | |
449 | unsigned int i; | |
450 | ||
451 | devc = sdi->priv; | |
452 | ||
453 | for (i = 0; i < NUM_SAMPLERATES; i++) { | |
454 | if (ikalogic_scanalogic2_samplerates[i] == samplerate) { | |
455 | devc->samplerate = samplerate; | |
456 | devc->samplerate_id = NUM_SAMPLERATES - i - 1; | |
457 | return SR_OK; | |
458 | } | |
459 | } | |
460 | ||
461 | return SR_ERR_ARG; | |
462 | } | |
463 | ||
464 | SR_PRIV int ikalogic_scanalogic2_set_limit_samples( | |
465 | const struct sr_dev_inst *sdi, uint64_t limit_samples) | |
466 | { | |
467 | struct dev_context *devc; | |
468 | ||
469 | devc = sdi->priv; | |
470 | ||
471 | if (limit_samples == 0) { | |
472 | sr_err("Invalid number of limit samples: %" PRIu64 ".", | |
473 | limit_samples); | |
474 | return SR_ERR_ARG; | |
475 | } | |
476 | ||
477 | if (limit_samples > MAX_SAMPLES) | |
478 | limit_samples = MAX_SAMPLES; | |
479 | ||
c824eb63 | 480 | sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples); |
e52e712d MS |
481 | |
482 | devc->limit_samples = limit_samples; | |
483 | ||
484 | return SR_OK; | |
485 | } | |
486 | ||
487 | SR_PRIV void ikalogic_scanalogic2_configure_trigger( | |
488 | const struct sr_dev_inst *sdi) | |
489 | { | |
490 | struct dev_context *devc; | |
491 | struct sr_probe *probe; | |
492 | uint8_t trigger_type; | |
493 | int probe_index, num_triggers_anyedge; | |
494 | char *trigger; | |
495 | GSList *l; | |
496 | ||
497 | devc = sdi->priv; | |
498 | ||
499 | /* Disable the trigger by default. */ | |
500 | devc->trigger_channel = TRIGGER_CHANNEL_0; | |
501 | devc->trigger_type = TRIGGER_TYPE_NONE; | |
502 | ||
503 | num_triggers_anyedge = 0; | |
504 | ||
505 | for (l = sdi->probes, probe_index = 0; l; l = l->next, probe_index++) { | |
506 | probe = l->data; | |
507 | trigger = probe->trigger; | |
508 | ||
509 | if (!trigger || !probe->enabled) | |
510 | continue; | |
511 | ||
512 | switch (*trigger) { | |
513 | case 'r': | |
514 | trigger_type = TRIGGER_TYPE_POSEDGE; | |
515 | break; | |
516 | case 'f': | |
517 | trigger_type = TRIGGER_TYPE_NEGEDGE; | |
518 | break; | |
519 | case 'c': | |
520 | trigger_type = TRIGGER_TYPE_ANYEDGE; | |
521 | num_triggers_anyedge++; | |
522 | break; | |
523 | default: | |
524 | continue; | |
525 | } | |
526 | ||
527 | devc->trigger_channel = probe_index + 1; | |
528 | devc->trigger_type = trigger_type; | |
529 | } | |
530 | ||
531 | /* | |
532 | * Set trigger to any edge on all channels if the trigger for each | |
533 | * channel is set to any edge. | |
534 | */ | |
535 | if (num_triggers_anyedge == NUM_PROBES) { | |
536 | devc->trigger_channel = TRIGGER_CHANNEL_ALL; | |
537 | devc->trigger_type = TRIGGER_TYPE_ANYEDGE; | |
538 | } | |
539 | ||
c824eb63 | 540 | sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.", |
e52e712d MS |
541 | devc->trigger_channel, devc->trigger_type); |
542 | } | |
543 | ||
544 | SR_PRIV int ikalogic_scanalogic2_set_capture_ratio( | |
545 | const struct sr_dev_inst *sdi, uint64_t capture_ratio) | |
546 | { | |
547 | struct dev_context *devc; | |
548 | ||
549 | devc = sdi->priv; | |
550 | ||
551 | if (capture_ratio > 100) { | |
552 | sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio); | |
553 | return SR_ERR_ARG; | |
554 | } | |
555 | ||
556 | sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio); | |
557 | ||
558 | devc->capture_ratio = capture_ratio; | |
559 | ||
560 | return SR_OK; | |
561 | } | |
562 | ||
c824eb63 | 563 | SR_PRIV int ikalogic_scanalogic2_set_after_trigger_delay( |
e52e712d MS |
564 | const struct sr_dev_inst *sdi, uint64_t after_trigger_delay) |
565 | { | |
566 | struct dev_context *devc; | |
567 | ||
568 | devc = sdi->priv; | |
569 | ||
570 | if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) { | |
571 | sr_err("Invalid after trigger delay: %" PRIu64 " ms.", | |
572 | after_trigger_delay); | |
573 | return SR_ERR_ARG; | |
574 | } | |
575 | ||
576 | sr_info("After trigger delay set to %" PRIu64 " ms.", | |
577 | after_trigger_delay); | |
578 | ||
579 | devc->after_trigger_delay = after_trigger_delay; | |
580 | ||
581 | return SR_OK; | |
582 | } | |
583 | ||
584 | SR_PRIV void ikalogic_scanalogic2_calculate_trigger_samples( | |
585 | const struct sr_dev_inst *sdi) | |
586 | { | |
587 | struct dev_context *devc; | |
588 | uint64_t pre_trigger_samples, post_trigger_samples; | |
589 | uint16_t pre_trigger_bytes, post_trigger_bytes; | |
590 | uint8_t cr; | |
591 | ||
592 | devc = sdi->priv; | |
593 | cr = devc->capture_ratio; | |
594 | ||
595 | /* Ignore the capture ratio if no trigger is enabled. */ | |
596 | if (devc->trigger_type == TRIGGER_TYPE_NONE) | |
597 | cr = 0; | |
598 | ||
599 | pre_trigger_samples = (devc->limit_samples * cr) / 100; | |
600 | post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100; | |
601 | ||
602 | /* | |
603 | * Increase the number of post trigger samples by one to compensate the | |
604 | * possible loss of a sample through integer rounding. | |
605 | */ | |
606 | if (pre_trigger_samples + post_trigger_samples != devc->limit_samples) | |
607 | post_trigger_samples++; | |
608 | ||
609 | /* | |
610 | * The device requires the number of samples in multiples of 8 which | |
611 | * will also be called sample bytes in the following. | |
612 | */ | |
613 | pre_trigger_bytes = pre_trigger_samples / 8; | |
614 | post_trigger_bytes = post_trigger_samples / 8; | |
615 | ||
616 | /* | |
617 | * Round up the number of sample bytes to ensure that at least the | |
618 | * requested number of samples will be acquired. Note that due to this | |
619 | * rounding the buffer to store these sample bytes needs to be at least | |
c824eb63 UH |
620 | * one sample byte larger than the minimal number of sample bytes |
621 | * needed to store the requested samples. | |
e52e712d MS |
622 | */ |
623 | if (pre_trigger_samples % 8 != 0) | |
624 | pre_trigger_bytes++; | |
625 | ||
626 | if (post_trigger_samples % 8 != 0) | |
627 | post_trigger_bytes++; | |
628 | ||
629 | sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples); | |
630 | sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples); | |
631 | sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes); | |
632 | sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes); | |
633 | ||
634 | devc->pre_trigger_samples = pre_trigger_samples; | |
635 | devc->pre_trigger_bytes = pre_trigger_bytes; | |
636 | devc->post_trigger_bytes = post_trigger_bytes; | |
637 | } | |
638 | ||
639 | SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb, | |
640 | struct device_info *dev_info) | |
641 | { | |
642 | struct drv_context *drvc; | |
643 | uint8_t buffer[PACKET_LENGTH]; | |
644 | int ret; | |
645 | ||
646 | drvc = di->priv; | |
647 | ||
648 | if (!dev_info) | |
649 | return SR_ERR_ARG; | |
650 | ||
651 | if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK) | |
652 | return SR_ERR; | |
653 | ||
654 | /* | |
655 | * Determine if a kernel driver is active on this interface and, if so, | |
656 | * detach it. | |
657 | */ | |
658 | if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) { | |
659 | ret = libusb_detach_kernel_driver(usb.devhdl, | |
660 | USB_INTERFACE); | |
661 | ||
662 | if (ret < 0) { | |
663 | sr_err("Failed to detach kernel driver: %i.", | |
664 | libusb_error_name(ret)); | |
665 | libusb_close(usb.devhdl); | |
666 | return SR_ERR; | |
667 | } | |
668 | } | |
669 | ||
670 | ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE); | |
671 | ||
672 | if (ret) { | |
673 | sr_err("Failed to claim interface: %s.", | |
674 | libusb_error_name(ret)); | |
675 | libusb_close(usb.devhdl); | |
676 | return SR_ERR; | |
677 | } | |
678 | ||
679 | memset(buffer, 0, sizeof(buffer)); | |
680 | ||
681 | /* | |
682 | * Reset the device to ensure it is in a proper state to request the | |
683 | * device information. | |
684 | */ | |
685 | buffer[0] = CMD_RESET; | |
686 | ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer); | |
687 | ||
688 | if (ret != PACKET_LENGTH) { | |
689 | sr_err("Resetting of device failed: %s.", | |
690 | libusb_error_name(ret)); | |
691 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
692 | libusb_close(usb.devhdl); | |
693 | return SR_ERR; | |
694 | } | |
695 | ||
696 | buffer[0] = CMD_INFO; | |
697 | ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer); | |
698 | ||
699 | if (ret != PACKET_LENGTH) { | |
700 | sr_err("Requesting of device information failed: %s.", | |
701 | libusb_error_name(ret)); | |
702 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
703 | libusb_close(usb.devhdl); | |
704 | return SR_ERR; | |
705 | } | |
706 | ||
707 | ret = ikalogic_scanalogic2_transfer_in(usb.devhdl, buffer); | |
708 | ||
709 | if (ret != PACKET_LENGTH) { | |
710 | sr_err("Receiving of device information failed: %s.", | |
711 | libusb_error_name(ret)); | |
712 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
713 | libusb_close(usb.devhdl); | |
714 | return SR_ERR; | |
715 | } | |
716 | ||
717 | memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t)); | |
718 | dev_info->serial = GUINT32_FROM_LE(dev_info->serial); | |
719 | ||
720 | dev_info->fw_ver_major = buffer[5]; | |
721 | dev_info->fw_ver_minor = buffer[6]; | |
722 | ||
723 | buffer[0] = CMD_RESET; | |
724 | ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer); | |
725 | ||
726 | if (ret != PACKET_LENGTH) { | |
727 | sr_err("Device reset failed: %s.", libusb_error_name(ret)); | |
728 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
729 | libusb_close(usb.devhdl); | |
730 | return SR_ERR; | |
731 | } | |
732 | ||
733 | /* | |
734 | * Set the device to idle state. If the device is not in idle state it | |
c824eb63 UH |
735 | * possibly will reset itself after a few seconds without being used |
736 | * and thereby close the connection. | |
e52e712d MS |
737 | */ |
738 | buffer[0] = CMD_IDLE; | |
739 | ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer); | |
740 | ||
741 | if (ret != PACKET_LENGTH) { | |
742 | sr_err("Failed to set device in idle state: %s.", | |
743 | libusb_error_name(ret)); | |
744 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
745 | libusb_close(usb.devhdl); | |
746 | return SR_ERR; | |
747 | } | |
748 | ||
749 | ret = libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
750 | ||
751 | if (ret < 0) { | |
752 | sr_err("Failed to release interface: %i.", | |
753 | libusb_error_name(ret)); | |
754 | libusb_close(usb.devhdl); | |
755 | return SR_ERR; | |
756 | } | |
757 | ||
758 | libusb_close(usb.devhdl); | |
759 | ||
760 | return SR_OK; | |
761 | } | |
762 | ||
763 | SR_PRIV int ikalogic_scanalogic2_transfer_in(libusb_device_handle *dev_handle, | |
c824eb63 | 764 | uint8_t *data) |
e52e712d MS |
765 | { |
766 | return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN, | |
767 | USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
c824eb63 | 768 | (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT); |
e52e712d MS |
769 | } |
770 | ||
771 | SR_PRIV int ikalogic_scanalogic2_transfer_out(libusb_device_handle *dev_handle, | |
c824eb63 | 772 | uint8_t *data) |
e52e712d MS |
773 | { |
774 | return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT, | |
775 | USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
c824eb63 | 776 | (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT); |
e52e712d | 777 | } |