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