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