]>
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 | ||
24 | extern uint64_t sl2_samplerates[NUM_SAMPLERATES]; | |
25 | ||
26 | static void stop_acquisition(struct sr_dev_inst *sdi) | |
27 | { | |
28 | struct drv_context *drvc = sdi->driver->priv; | |
29 | struct dev_context *devc; | |
30 | struct sr_datafeed_packet packet; | |
31 | ||
32 | devc = sdi->priv; | |
33 | ||
34 | /* Remove USB file descriptors from polling. */ | |
35 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
36 | ||
37 | packet.type = SR_DF_END; | |
38 | sr_session_send(devc->cb_data, &packet); | |
39 | ||
40 | sdi->status = SR_ST_ACTIVE; | |
41 | } | |
42 | ||
43 | static void abort_acquisition(struct sr_dev_inst *sdi) | |
44 | { | |
45 | struct drv_context *drvc = sdi->driver->priv; | |
46 | struct dev_context *devc; | |
47 | struct sr_datafeed_packet packet; | |
48 | ||
49 | devc = sdi->priv; | |
50 | ||
51 | /* Remove USB file descriptors from polling. */ | |
52 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
53 | ||
54 | packet.type = SR_DF_END; | |
55 | sr_session_send(devc->cb_data, &packet); | |
56 | ||
57 | sdi->driver->dev_close(sdi); | |
58 | } | |
59 | ||
60 | static void buffer_sample_data(const struct sr_dev_inst *sdi) | |
61 | { | |
62 | struct dev_context *devc; | |
63 | unsigned int offset, packet_length; | |
64 | ||
65 | devc = sdi->priv; | |
66 | ||
67 | if (devc->channels[devc->channel]->enabled) { | |
68 | offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES; | |
69 | ||
70 | /* | |
71 | * Determine the packet length to ensure that the last packet | |
72 | * will not exceed the buffer size. | |
73 | */ | |
74 | packet_length = MIN(PACKET_NUM_SAMPLE_BYTES, | |
75 | MAX_DEV_SAMPLE_BYTES - offset); | |
76 | ||
77 | /* | |
78 | * Skip the first 4 bytes of the source buffer because they | |
79 | * contain channel and packet information only. | |
80 | */ | |
81 | memcpy(devc->sample_buffer[devc->channel] + offset, | |
82 | devc->xfer_data_in + 4, packet_length); | |
83 | } | |
84 | } | |
85 | ||
86 | static void process_sample_data(const struct sr_dev_inst *sdi) | |
87 | { | |
88 | struct dev_context *devc; | |
89 | struct sr_datafeed_packet packet; | |
90 | struct sr_datafeed_logic logic; | |
91 | uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS]; | |
92 | uint16_t offset, n = 0; | |
93 | int8_t k; | |
94 | ||
95 | devc = sdi->priv; | |
96 | offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES; | |
97 | ||
98 | /* | |
99 | * Array of pointers to the sample data of all channels up to the last | |
100 | * enabled one for an uniform access to them. Note that the currently | |
101 | * received samples always belong to the last enabled channel. | |
102 | */ | |
103 | for (i = 0; i < devc->num_enabled_channels - 1; i++) | |
104 | ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset; | |
105 | ||
106 | /* | |
107 | * Skip the first 4 bytes of the buffer because they contain channel | |
108 | * and packet information only. | |
109 | */ | |
110 | ptr[i] = devc->xfer_data_in + 4; | |
111 | ||
112 | for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) { | |
113 | /* Stop processing if all requested samples are processed. */ | |
114 | if (devc->samples_processed == devc->limit_samples) | |
115 | break; | |
116 | ||
117 | k = 7; | |
118 | ||
119 | if (devc->samples_processed == 0) { | |
120 | /* | |
121 | * Adjust the position of the first sample to be | |
122 | * processed because possibly more samples than | |
123 | * necessary might have been acquired. This is because | |
124 | * the number of acquired samples is always rounded up | |
125 | * to a multiple of 8. | |
126 | */ | |
127 | k = k - (devc->pre_trigger_bytes * 8) + | |
128 | devc->pre_trigger_samples; | |
129 | ||
130 | sr_dbg("Start processing at sample: %d.", 7 - k); | |
131 | ||
132 | /* | |
133 | * Send the trigger before the first sample is | |
134 | * processed if no pre trigger samples were calculated | |
135 | * through the capture ratio. | |
136 | */ | |
137 | if (devc->trigger_type != TRIGGER_TYPE_NONE && | |
138 | devc->pre_trigger_samples == 0) { | |
139 | packet.type = SR_DF_TRIGGER; | |
140 | sr_session_send(devc->cb_data, &packet); | |
141 | } | |
142 | } | |
143 | ||
144 | for (; k >= 0; k--) { | |
145 | /* | |
146 | * Stop processing if all requested samples are | |
147 | * processed. | |
148 | */ | |
149 | if (devc->samples_processed == devc->limit_samples) | |
150 | break; | |
151 | ||
152 | buffer[n] = 0; | |
153 | ||
154 | /* | |
155 | * Extract the current sample for each enabled channel | |
156 | * and store them in the buffer. | |
157 | */ | |
158 | for (j = 0; j < devc->num_enabled_channels; j++) { | |
159 | tmp = (ptr[j][i] & (1 << k)) >> k; | |
160 | buffer[n] |= tmp << devc->channel_map[j]; | |
161 | } | |
162 | ||
163 | n++; | |
164 | devc->samples_processed++; | |
165 | ||
166 | /* | |
167 | * Send all processed samples and the trigger if the | |
168 | * number of processed samples reaches the calculated | |
169 | * number of pre trigger samples. | |
170 | */ | |
171 | if (devc->samples_processed == devc->pre_trigger_samples && | |
172 | devc->trigger_type != TRIGGER_TYPE_NONE) { | |
173 | packet.type = SR_DF_LOGIC; | |
174 | packet.payload = &logic; | |
175 | logic.length = n; | |
176 | logic.unitsize = 1; | |
177 | logic.data = buffer; | |
178 | sr_session_send(devc->cb_data, &packet); | |
179 | ||
180 | packet.type = SR_DF_TRIGGER; | |
181 | sr_session_send(devc->cb_data, &packet); | |
182 | ||
183 | n = 0; | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
188 | if (n > 0) { | |
189 | packet.type = SR_DF_LOGIC; | |
190 | packet.payload = &logic; | |
191 | logic.length = n; | |
192 | logic.unitsize = 1; | |
193 | logic.data = buffer; | |
194 | sr_session_send(devc->cb_data, &packet); | |
195 | } | |
196 | } | |
197 | ||
198 | SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data) | |
199 | { | |
200 | struct sr_dev_inst *sdi; | |
201 | struct sr_dev_driver *di; | |
202 | struct dev_context *devc; | |
203 | struct drv_context *drvc; | |
204 | struct timeval tv; | |
205 | int64_t current_time, time_elapsed; | |
206 | int ret = 0; | |
207 | ||
208 | (void)fd; | |
209 | (void)revents; | |
210 | ||
211 | if (!(sdi = cb_data)) | |
212 | return TRUE; | |
213 | ||
214 | if (!(devc = sdi->priv)) | |
215 | return TRUE; | |
216 | ||
217 | di = sdi->driver; | |
218 | drvc = di->priv; | |
219 | current_time = g_get_monotonic_time(); | |
220 | ||
221 | if (devc->state == STATE_WAIT_DATA_READY && | |
222 | !devc->wait_data_ready_locked) { | |
223 | time_elapsed = current_time - devc->wait_data_ready_time; | |
224 | ||
225 | /* | |
226 | * Check here for stopping in addition to the transfer | |
227 | * callback functions to avoid waiting until the | |
228 | * WAIT_DATA_READY_INTERVAL has expired. | |
229 | */ | |
230 | if (sdi->status == SR_ST_STOPPING) { | |
231 | if (!devc->stopping_in_progress) { | |
232 | devc->next_state = STATE_RESET_AND_IDLE; | |
233 | devc->stopping_in_progress = TRUE; | |
234 | ret = libusb_submit_transfer(devc->xfer_in); | |
235 | } | |
236 | } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) { | |
237 | devc->wait_data_ready_locked = TRUE; | |
238 | ret = libusb_submit_transfer(devc->xfer_in); | |
239 | } | |
240 | } | |
241 | ||
242 | if (ret != 0) { | |
243 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); | |
244 | abort_acquisition(sdi); | |
245 | return TRUE; | |
246 | } | |
247 | ||
248 | tv.tv_sec = 0; | |
249 | tv.tv_usec = 0; | |
250 | ||
251 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, | |
252 | NULL); | |
253 | ||
254 | /* Check if an error occurred on a transfer. */ | |
255 | if (devc->transfer_error) | |
256 | abort_acquisition(sdi); | |
257 | ||
258 | return TRUE; | |
259 | } | |
260 | ||
261 | SR_PRIV void sl2_receive_transfer_in( struct libusb_transfer *transfer) | |
262 | { | |
263 | struct sr_dev_inst *sdi; | |
264 | struct dev_context *devc; | |
265 | uint8_t last_channel; | |
266 | int ret = 0; | |
267 | ||
268 | sdi = transfer->user_data; | |
269 | devc = sdi->priv; | |
270 | ||
271 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { | |
272 | sr_err("Transfer to device failed: %i.", transfer->status); | |
273 | devc->transfer_error = TRUE; | |
274 | return; | |
275 | } | |
276 | ||
277 | if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) { | |
278 | devc->next_state = STATE_RESET_AND_IDLE; | |
279 | devc->stopping_in_progress = TRUE; | |
280 | ||
281 | if (libusb_submit_transfer(devc->xfer_in) != 0) { | |
282 | sr_err("Submit transfer failed: %s.", | |
283 | libusb_error_name(ret)); | |
284 | devc->transfer_error = TRUE; | |
285 | } | |
286 | ||
287 | return; | |
288 | } | |
289 | ||
290 | if (devc->state != devc->next_state) | |
291 | sr_spew("State changed from %i to %i.", | |
292 | devc->state, devc->next_state); | |
293 | devc->state = devc->next_state; | |
294 | ||
295 | if (devc->state == STATE_WAIT_DATA_READY) { | |
296 | /* Check if the received data are a valid device status. */ | |
297 | if (devc->xfer_data_in[0] == 0x05) { | |
298 | if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER) | |
299 | sr_dbg("Waiting for trigger."); | |
300 | else if (devc->xfer_data_in[1] == STATUS_SAMPLING) | |
301 | sr_dbg("Sampling in progress."); | |
302 | } | |
303 | ||
304 | /* | |
305 | * Check if the received data are a valid device status and the | |
306 | * sample data are ready. | |
307 | */ | |
308 | if (devc->xfer_data_in[0] == 0x05 && | |
309 | devc->xfer_data_in[1] == STATUS_DATA_READY) { | |
310 | devc->next_state = STATE_RECEIVE_DATA; | |
311 | ret = libusb_submit_transfer(transfer); | |
312 | } else { | |
313 | devc->wait_data_ready_locked = FALSE; | |
314 | devc->wait_data_ready_time = g_get_monotonic_time(); | |
315 | } | |
316 | } else if (devc->state == STATE_RECEIVE_DATA) { | |
317 | last_channel = devc->channel_map[devc->num_enabled_channels - 1]; | |
318 | ||
319 | if (devc->channel < last_channel) { | |
320 | buffer_sample_data(sdi); | |
321 | } else if (devc->channel == last_channel) { | |
322 | process_sample_data(sdi); | |
323 | } else { | |
324 | /* | |
325 | * Stop acquisition because all samples of enabled | |
326 | * channels are processed. | |
327 | */ | |
328 | devc->next_state = STATE_RESET_AND_IDLE; | |
329 | } | |
330 | ||
331 | devc->sample_packet++; | |
332 | devc->sample_packet %= devc->num_sample_packets; | |
333 | ||
334 | if (devc->sample_packet == 0) | |
335 | devc->channel++; | |
336 | ||
337 | ret = libusb_submit_transfer(transfer); | |
338 | } else if (devc->state == STATE_RESET_AND_IDLE) { | |
339 | /* Check if the received data are a valid device status. */ | |
340 | if (devc->xfer_data_in[0] == 0x05) { | |
341 | if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) { | |
342 | devc->next_state = STATE_IDLE; | |
343 | devc->xfer_data_out[0] = CMD_IDLE; | |
344 | } else { | |
345 | devc->next_state = STATE_WAIT_DEVICE_READY; | |
346 | devc->xfer_data_out[0] = CMD_RESET; | |
347 | } | |
348 | ||
349 | ret = libusb_submit_transfer(devc->xfer_out); | |
350 | } else { | |
351 | /* | |
352 | * The received device status is invalid which | |
353 | * indicates that the device is not ready to accept | |
354 | * commands. Request a new device status until a valid | |
355 | * device status is received. | |
356 | */ | |
357 | ret = libusb_submit_transfer(transfer); | |
358 | } | |
359 | } else if (devc->state == STATE_WAIT_DEVICE_READY) { | |
360 | /* Check if the received data are a valid device status. */ | |
361 | if (devc->xfer_data_in[0] == 0x05) { | |
362 | if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) { | |
363 | devc->next_state = STATE_IDLE; | |
364 | devc->xfer_data_out[0] = CMD_IDLE; | |
365 | } else { | |
366 | /* | |
367 | * The received device status is valid but the | |
368 | * device is not ready. Probably the device did | |
369 | * not recognize the last reset. Reset the | |
370 | * device again. | |
371 | */ | |
372 | devc->xfer_data_out[0] = CMD_RESET; | |
373 | } | |
374 | ||
375 | ret = libusb_submit_transfer(devc->xfer_out); | |
376 | } else { | |
377 | /* | |
378 | * The device is not ready and therefore not able to | |
379 | * change to the idle state. Request a new device | |
380 | * status until the device is ready. | |
381 | */ | |
382 | ret = libusb_submit_transfer(transfer); | |
383 | } | |
384 | } | |
385 | ||
386 | if (ret != 0) { | |
387 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); | |
388 | devc->transfer_error = TRUE; | |
389 | } | |
390 | } | |
391 | ||
392 | SR_PRIV void sl2_receive_transfer_out( struct libusb_transfer *transfer) | |
393 | { | |
394 | struct sr_dev_inst *sdi; | |
395 | struct dev_context *devc; | |
396 | int ret = 0; | |
397 | ||
398 | sdi = transfer->user_data; | |
399 | devc = sdi->priv; | |
400 | ||
401 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { | |
402 | sr_err("Transfer to device failed: %i.", transfer->status); | |
403 | devc->transfer_error = TRUE; | |
404 | return; | |
405 | } | |
406 | ||
407 | if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) { | |
408 | devc->next_state = STATE_RESET_AND_IDLE; | |
409 | devc->stopping_in_progress = TRUE; | |
410 | ||
411 | if (libusb_submit_transfer(devc->xfer_in) != 0) { | |
412 | sr_err("Submit transfer failed: %s.", | |
413 | libusb_error_name(ret)); | |
414 | ||
415 | devc->transfer_error = TRUE; | |
416 | } | |
417 | ||
418 | return; | |
419 | } | |
420 | ||
421 | if (devc->state != devc->next_state) | |
422 | sr_spew("State changed from %i to %i.", | |
423 | devc->state, devc->next_state); | |
424 | devc->state = devc->next_state; | |
425 | ||
426 | if (devc->state == STATE_IDLE) { | |
427 | stop_acquisition(sdi); | |
428 | } else if (devc->state == STATE_SAMPLE) { | |
429 | devc->next_state = STATE_WAIT_DATA_READY; | |
430 | ret = libusb_submit_transfer(devc->xfer_in); | |
431 | } else if (devc->state == STATE_WAIT_DEVICE_READY) { | |
432 | ret = libusb_submit_transfer(devc->xfer_in); | |
433 | } | |
434 | ||
435 | if (ret != 0) { | |
436 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); | |
437 | devc->transfer_error = TRUE; | |
438 | } | |
439 | } | |
440 | ||
441 | SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi, | |
442 | uint64_t samplerate) | |
443 | { | |
444 | struct dev_context *devc; | |
445 | unsigned int i; | |
446 | ||
447 | devc = sdi->priv; | |
448 | ||
449 | for (i = 0; i < NUM_SAMPLERATES; i++) { | |
450 | if (sl2_samplerates[i] == samplerate) { | |
451 | devc->samplerate = samplerate; | |
452 | devc->samplerate_id = NUM_SAMPLERATES - i - 1; | |
453 | return SR_OK; | |
454 | } | |
455 | } | |
456 | ||
457 | return SR_ERR_ARG; | |
458 | } | |
459 | ||
460 | SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi, | |
461 | uint64_t limit_samples) | |
462 | { | |
463 | struct dev_context *devc; | |
464 | ||
465 | devc = sdi->priv; | |
466 | ||
467 | if (limit_samples == 0) { | |
468 | sr_err("Invalid number of limit samples: %" PRIu64 ".", | |
469 | limit_samples); | |
470 | return SR_ERR_ARG; | |
471 | } | |
472 | ||
473 | if (limit_samples > MAX_SAMPLES) | |
474 | limit_samples = MAX_SAMPLES; | |
475 | ||
476 | sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples); | |
477 | ||
478 | devc->limit_samples = limit_samples; | |
479 | ||
480 | return SR_OK; | |
481 | } | |
482 | ||
483 | SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi) | |
484 | { | |
485 | struct dev_context *devc; | |
486 | struct sr_trigger *trigger; | |
487 | struct sr_trigger_stage *stage; | |
488 | struct sr_trigger_match *match; | |
489 | const GSList *l, *m; | |
490 | int num_triggers_anyedge; | |
491 | ||
492 | devc = sdi->priv; | |
493 | ||
494 | /* Disable the trigger by default. */ | |
495 | devc->trigger_channel = TRIGGER_CHANNEL_0; | |
496 | devc->trigger_type = TRIGGER_TYPE_NONE; | |
497 | ||
498 | if (!(trigger = sr_session_trigger_get(sdi->session))) | |
499 | return SR_OK; | |
500 | ||
501 | if (g_slist_length(trigger->stages) > 1) { | |
502 | sr_err("This device only supports 1 trigger stage."); | |
503 | return SR_ERR; | |
504 | } | |
505 | ||
506 | num_triggers_anyedge = 0; | |
507 | for (l = trigger->stages; l; l = l->next) { | |
508 | stage = l->data; | |
509 | for (m = stage->matches; m; m = m->next) { | |
510 | match = m->data; | |
511 | if (!match->channel->enabled) | |
512 | /* Ignore disabled channels with a trigger. */ | |
513 | continue; | |
514 | devc->trigger_channel = match->channel->index + 1; | |
515 | switch (match->match) { | |
516 | case SR_TRIGGER_RISING: | |
517 | devc->trigger_type = TRIGGER_TYPE_POSEDGE; | |
518 | break; | |
519 | case SR_TRIGGER_FALLING: | |
520 | devc->trigger_type = TRIGGER_TYPE_NEGEDGE; | |
521 | break; | |
522 | case SR_TRIGGER_EDGE: | |
523 | devc->trigger_type = TRIGGER_TYPE_ANYEDGE; | |
524 | num_triggers_anyedge++; | |
525 | break; | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
530 | /* | |
531 | * Set trigger to any edge on all channels if the trigger for each | |
532 | * channel is set to any edge. | |
533 | */ | |
534 | if (num_triggers_anyedge == NUM_CHANNELS) { | |
535 | devc->trigger_channel = TRIGGER_CHANNEL_ALL; | |
536 | devc->trigger_type = TRIGGER_TYPE_ANYEDGE; | |
537 | } | |
538 | ||
539 | sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.", | |
540 | devc->trigger_channel, devc->trigger_type); | |
541 | ||
542 | return SR_OK; | |
543 | } | |
544 | ||
545 | SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi, | |
546 | uint64_t capture_ratio) | |
547 | { | |
548 | struct dev_context *devc; | |
549 | ||
550 | devc = sdi->priv; | |
551 | ||
552 | if (capture_ratio > 100) { | |
553 | sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio); | |
554 | return SR_ERR_ARG; | |
555 | } | |
556 | ||
557 | sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio); | |
558 | ||
559 | devc->capture_ratio = capture_ratio; | |
560 | ||
561 | return SR_OK; | |
562 | } | |
563 | ||
564 | SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi, | |
565 | uint64_t after_trigger_delay) | |
566 | { | |
567 | struct dev_context *devc; | |
568 | ||
569 | devc = sdi->priv; | |
570 | ||
571 | if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) { | |
572 | sr_err("Invalid after trigger delay: %" PRIu64 " ms.", | |
573 | after_trigger_delay); | |
574 | return SR_ERR_ARG; | |
575 | } | |
576 | ||
577 | sr_info("After trigger delay set to %" PRIu64 " ms.", | |
578 | after_trigger_delay); | |
579 | ||
580 | devc->after_trigger_delay = after_trigger_delay; | |
581 | ||
582 | return SR_OK; | |
583 | } | |
584 | ||
585 | SR_PRIV void sl2_calculate_trigger_samples(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 | |
620 | * one sample byte larger than the minimal number of sample bytes | |
621 | * needed to store the requested samples. | |
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 sl2_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 = ikalogic_scanalogic2_driver_info.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: %s.", | |
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 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
687 | sr_err("Resetting of device failed: %s.", | |
688 | libusb_error_name(ret)); | |
689 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
690 | libusb_close(usb.devhdl); | |
691 | return SR_ERR; | |
692 | } | |
693 | ||
694 | buffer[0] = CMD_INFO; | |
695 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
696 | sr_err("Requesting of device information failed: %s.", | |
697 | libusb_error_name(ret)); | |
698 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
699 | libusb_close(usb.devhdl); | |
700 | return SR_ERR; | |
701 | } | |
702 | ||
703 | if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
704 | sr_err("Receiving of device information failed: %s.", | |
705 | libusb_error_name(ret)); | |
706 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
707 | libusb_close(usb.devhdl); | |
708 | return SR_ERR; | |
709 | } | |
710 | ||
711 | memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t)); | |
712 | dev_info->serial = GUINT32_FROM_LE(dev_info->serial); | |
713 | ||
714 | dev_info->fw_ver_major = buffer[5]; | |
715 | dev_info->fw_ver_minor = buffer[6]; | |
716 | ||
717 | buffer[0] = CMD_RESET; | |
718 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
719 | sr_err("Device reset failed: %s.", libusb_error_name(ret)); | |
720 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
721 | libusb_close(usb.devhdl); | |
722 | return SR_ERR; | |
723 | } | |
724 | ||
725 | /* | |
726 | * Set the device to idle state. If the device is not in idle state it | |
727 | * possibly will reset itself after a few seconds without being used | |
728 | * and thereby close the connection. | |
729 | */ | |
730 | buffer[0] = CMD_IDLE; | |
731 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
732 | sr_err("Failed to set device in idle state: %s.", | |
733 | libusb_error_name(ret)); | |
734 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
735 | libusb_close(usb.devhdl); | |
736 | return SR_ERR; | |
737 | } | |
738 | ||
739 | ret = libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
740 | ||
741 | if (ret < 0) { | |
742 | sr_err("Failed to release interface: %s.", | |
743 | libusb_error_name(ret)); | |
744 | libusb_close(usb.devhdl); | |
745 | return SR_ERR; | |
746 | } | |
747 | ||
748 | libusb_close(usb.devhdl); | |
749 | ||
750 | return SR_OK; | |
751 | } | |
752 | ||
753 | SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data) | |
754 | { | |
755 | return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN, | |
756 | USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
757 | (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT); | |
758 | } | |
759 | ||
760 | SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data) | |
761 | { | |
762 | return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT, | |
763 | USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
764 | (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT); | |
765 | } |