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