]>
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 <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | extern uint64_t sl2_samplerates[NUM_SAMPLERATES]; | |
24 | ||
25 | static void stop_acquisition(struct sr_dev_inst *sdi) | |
26 | { | |
27 | struct drv_context *drvc = sdi->driver->context; | |
28 | ||
29 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
30 | ||
31 | std_session_send_df_end(sdi); | |
32 | ||
33 | sdi->status = SR_ST_ACTIVE; | |
34 | } | |
35 | ||
36 | static void abort_acquisition(struct sr_dev_inst *sdi) | |
37 | { | |
38 | struct drv_context *drvc = sdi->driver->context; | |
39 | ||
40 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
41 | ||
42 | std_session_send_df_end(sdi); | |
43 | ||
44 | sdi->driver->dev_close(sdi); | |
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 | ||
54 | if (devc->channels[devc->channel]->enabled) { | |
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; | |
78 | uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS]; | |
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 | */ | |
90 | for (i = 0; i < devc->num_enabled_channels - 1; i++) | |
91 | ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset; | |
92 | ||
93 | /* | |
94 | * Skip the first 4 bytes of the buffer because they contain channel | |
95 | * and packet information only. | |
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 | |
110 | * necessary might have been acquired. This is because | |
111 | * the number of acquired samples is always rounded up | |
112 | * to a multiple of 8. | |
113 | */ | |
114 | k = k - (devc->pre_trigger_bytes * 8) + | |
115 | devc->pre_trigger_samples; | |
116 | ||
117 | sr_dbg("Start processing at sample: %d.", 7 - k); | |
118 | ||
119 | /* | |
120 | * Send the trigger before the first sample is | |
121 | * processed if no pre trigger samples were calculated | |
122 | * through the capture ratio. | |
123 | */ | |
124 | if (devc->trigger_type != TRIGGER_TYPE_NONE && | |
125 | devc->pre_trigger_samples == 0) { | |
126 | packet.type = SR_DF_TRIGGER; | |
127 | sr_session_send(sdi, &packet); | |
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 | */ | |
145 | for (j = 0; j < devc->num_enabled_channels; j++) { | |
146 | tmp = (ptr[j][i] & (1 << k)) >> k; | |
147 | buffer[n] |= tmp << devc->channel_map[j]; | |
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; | |
165 | sr_session_send(sdi, &packet); | |
166 | ||
167 | packet.type = SR_DF_TRIGGER; | |
168 | sr_session_send(sdi, &packet); | |
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; | |
181 | sr_session_send(sdi, &packet); | |
182 | } | |
183 | } | |
184 | ||
185 | SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data) | |
186 | { | |
187 | struct sr_dev_inst *sdi; | |
188 | struct sr_dev_driver *di; | |
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; | |
197 | ||
198 | if (!(sdi = cb_data)) | |
199 | return TRUE; | |
200 | ||
201 | if (!(devc = sdi->priv)) | |
202 | return TRUE; | |
203 | ||
204 | di = sdi->driver; | |
205 | drvc = di->context; | |
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 | /* | |
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. | |
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) { | |
230 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); | |
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. */ | |
242 | if (devc->transfer_error) | |
243 | abort_acquisition(sdi); | |
244 | ||
245 | return TRUE; | |
246 | } | |
247 | ||
248 | SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer) | |
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) { | |
259 | sr_err("Transfer to device failed: %s.", | |
260 | libusb_error_name(transfer->status)); | |
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) { | |
270 | sr_err("Submit transfer failed: %s.", | |
271 | libusb_error_name(ret)); | |
272 | devc->transfer_error = TRUE; | |
273 | } | |
274 | ||
275 | return; | |
276 | } | |
277 | ||
278 | if (devc->state != devc->next_state) | |
279 | sr_spew("State changed from %i to %i.", | |
280 | devc->state, devc->next_state); | |
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) { | |
305 | last_channel = devc->channel_map[devc->num_enabled_channels - 1]; | |
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 | |
314 | * channels are processed. | |
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 | /* | |
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. | |
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 | |
367 | * change to the idle state. Request a new device | |
368 | * status until the device is ready. | |
369 | */ | |
370 | ret = libusb_submit_transfer(transfer); | |
371 | } | |
372 | } | |
373 | ||
374 | if (ret != 0) { | |
375 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); | |
376 | devc->transfer_error = TRUE; | |
377 | } | |
378 | } | |
379 | ||
380 | SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer) | |
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) { | |
390 | sr_err("Transfer to device failed: %s.", | |
391 | libusb_error_name(transfer->status)); | |
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) { | |
401 | sr_err("Submit transfer failed: %s.", | |
402 | libusb_error_name(ret)); | |
403 | ||
404 | devc->transfer_error = TRUE; | |
405 | } | |
406 | ||
407 | return; | |
408 | } | |
409 | ||
410 | if (devc->state != devc->next_state) | |
411 | sr_spew("State changed from %i to %i.", | |
412 | devc->state, devc->next_state); | |
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) { | |
425 | sr_err("Submit transfer failed: %s.", libusb_error_name(ret)); | |
426 | devc->transfer_error = TRUE; | |
427 | } | |
428 | } | |
429 | ||
430 | SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi, | |
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++) { | |
439 | if (sl2_samplerates[i] == samplerate) { | |
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 | ||
449 | SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi, | |
450 | uint64_t limit_samples) | |
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 | ||
465 | sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples); | |
466 | ||
467 | devc->limit_samples = limit_samples; | |
468 | ||
469 | return SR_OK; | |
470 | } | |
471 | ||
472 | SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi) | |
473 | { | |
474 | struct dev_context *devc; | |
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; | |
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 | ||
487 | if (!(trigger = sr_session_trigger_get(sdi->session))) | |
488 | return SR_OK; | |
489 | ||
490 | if (g_slist_length(trigger->stages) > 1) { | |
491 | sr_err("This device only supports 1 trigger stage."); | |
492 | return SR_ERR; | |
493 | } | |
494 | ||
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 | } | |
516 | } | |
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 | */ | |
523 | if (num_triggers_anyedge == NUM_CHANNELS) { | |
524 | devc->trigger_channel = TRIGGER_CHANNEL_ALL; | |
525 | devc->trigger_type = TRIGGER_TYPE_ANYEDGE; | |
526 | } | |
527 | ||
528 | sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.", | |
529 | devc->trigger_channel, devc->trigger_type); | |
530 | ||
531 | return SR_OK; | |
532 | } | |
533 | ||
534 | SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi, | |
535 | uint64_t capture_ratio) | |
536 | { | |
537 | struct dev_context *devc; | |
538 | ||
539 | devc = sdi->priv; | |
540 | ||
541 | if (capture_ratio > 100) { | |
542 | sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio); | |
543 | return SR_ERR_ARG; | |
544 | } | |
545 | ||
546 | sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio); | |
547 | ||
548 | devc->capture_ratio = capture_ratio; | |
549 | ||
550 | return SR_OK; | |
551 | } | |
552 | ||
553 | SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi, | |
554 | uint64_t after_trigger_delay) | |
555 | { | |
556 | struct dev_context *devc; | |
557 | ||
558 | devc = sdi->priv; | |
559 | ||
560 | if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) { | |
561 | sr_err("Invalid after trigger delay: %" PRIu64 " ms.", | |
562 | after_trigger_delay); | |
563 | return SR_ERR_ARG; | |
564 | } | |
565 | ||
566 | sr_info("After trigger delay set to %" PRIu64 " ms.", | |
567 | after_trigger_delay); | |
568 | ||
569 | devc->after_trigger_delay = after_trigger_delay; | |
570 | ||
571 | return SR_OK; | |
572 | } | |
573 | ||
574 | SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi) | |
575 | { | |
576 | struct dev_context *devc; | |
577 | uint64_t pre_trigger_samples, post_trigger_samples; | |
578 | uint16_t pre_trigger_bytes, post_trigger_bytes; | |
579 | uint8_t cr; | |
580 | ||
581 | devc = sdi->priv; | |
582 | cr = devc->capture_ratio; | |
583 | ||
584 | /* Ignore the capture ratio if no trigger is enabled. */ | |
585 | if (devc->trigger_type == TRIGGER_TYPE_NONE) | |
586 | cr = 0; | |
587 | ||
588 | pre_trigger_samples = (devc->limit_samples * cr) / 100; | |
589 | post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100; | |
590 | ||
591 | /* | |
592 | * Increase the number of post trigger samples by one to compensate the | |
593 | * possible loss of a sample through integer rounding. | |
594 | */ | |
595 | if (pre_trigger_samples + post_trigger_samples != devc->limit_samples) | |
596 | post_trigger_samples++; | |
597 | ||
598 | /* | |
599 | * The device requires the number of samples in multiples of 8 which | |
600 | * will also be called sample bytes in the following. | |
601 | */ | |
602 | pre_trigger_bytes = pre_trigger_samples / 8; | |
603 | post_trigger_bytes = post_trigger_samples / 8; | |
604 | ||
605 | /* | |
606 | * Round up the number of sample bytes to ensure that at least the | |
607 | * requested number of samples will be acquired. Note that due to this | |
608 | * rounding the buffer to store these sample bytes needs to be at least | |
609 | * one sample byte larger than the minimal number of sample bytes | |
610 | * needed to store the requested samples. | |
611 | */ | |
612 | if (pre_trigger_samples % 8 != 0) | |
613 | pre_trigger_bytes++; | |
614 | ||
615 | if (post_trigger_samples % 8 != 0) | |
616 | post_trigger_bytes++; | |
617 | ||
618 | sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples); | |
619 | sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples); | |
620 | sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes); | |
621 | sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes); | |
622 | ||
623 | devc->pre_trigger_samples = pre_trigger_samples; | |
624 | devc->pre_trigger_bytes = pre_trigger_bytes; | |
625 | devc->post_trigger_bytes = post_trigger_bytes; | |
626 | } | |
627 | ||
628 | SR_PRIV int sl2_get_device_info(struct sr_dev_driver *di, | |
629 | struct sr_usb_dev_inst usb, struct device_info *dev_info) | |
630 | { | |
631 | struct drv_context *drvc; | |
632 | uint8_t buffer[PACKET_LENGTH]; | |
633 | int ret; | |
634 | ||
635 | drvc = di->context; | |
636 | ||
637 | if (!dev_info) | |
638 | return SR_ERR_ARG; | |
639 | ||
640 | if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK) | |
641 | return SR_ERR; | |
642 | ||
643 | /* | |
644 | * Determine if a kernel driver is active on this interface and, if so, | |
645 | * detach it. | |
646 | */ | |
647 | if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) { | |
648 | ret = libusb_detach_kernel_driver(usb.devhdl, | |
649 | USB_INTERFACE); | |
650 | ||
651 | if (ret < 0) { | |
652 | sr_err("Failed to detach kernel driver: %s.", | |
653 | libusb_error_name(ret)); | |
654 | libusb_close(usb.devhdl); | |
655 | return SR_ERR; | |
656 | } | |
657 | } | |
658 | ||
659 | ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE); | |
660 | ||
661 | if (ret) { | |
662 | sr_err("Failed to claim interface: %s.", | |
663 | libusb_error_name(ret)); | |
664 | libusb_close(usb.devhdl); | |
665 | return SR_ERR; | |
666 | } | |
667 | ||
668 | memset(buffer, 0, sizeof(buffer)); | |
669 | ||
670 | /* | |
671 | * Reset the device to ensure it is in a proper state to request the | |
672 | * device information. | |
673 | */ | |
674 | buffer[0] = CMD_RESET; | |
675 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
676 | sr_err("Resetting of device failed: %s.", | |
677 | libusb_error_name(ret)); | |
678 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
679 | libusb_close(usb.devhdl); | |
680 | return SR_ERR; | |
681 | } | |
682 | ||
683 | buffer[0] = CMD_INFO; | |
684 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
685 | sr_err("Requesting of device information failed: %s.", | |
686 | libusb_error_name(ret)); | |
687 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
688 | libusb_close(usb.devhdl); | |
689 | return SR_ERR; | |
690 | } | |
691 | ||
692 | if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
693 | sr_err("Receiving of device information failed: %s.", | |
694 | libusb_error_name(ret)); | |
695 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
696 | libusb_close(usb.devhdl); | |
697 | return SR_ERR; | |
698 | } | |
699 | ||
700 | memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t)); | |
701 | dev_info->serial = GUINT32_FROM_LE(dev_info->serial); | |
702 | ||
703 | dev_info->fw_ver_major = buffer[5]; | |
704 | dev_info->fw_ver_minor = buffer[6]; | |
705 | ||
706 | buffer[0] = CMD_RESET; | |
707 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
708 | sr_err("Device reset failed: %s.", libusb_error_name(ret)); | |
709 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
710 | libusb_close(usb.devhdl); | |
711 | return SR_ERR; | |
712 | } | |
713 | ||
714 | /* | |
715 | * Set the device to idle state. If the device is not in idle state it | |
716 | * possibly will reset itself after a few seconds without being used | |
717 | * and thereby close the connection. | |
718 | */ | |
719 | buffer[0] = CMD_IDLE; | |
720 | if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) { | |
721 | sr_err("Failed to set device in idle state: %s.", | |
722 | libusb_error_name(ret)); | |
723 | libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
724 | libusb_close(usb.devhdl); | |
725 | return SR_ERR; | |
726 | } | |
727 | ||
728 | ret = libusb_release_interface(usb.devhdl, USB_INTERFACE); | |
729 | ||
730 | if (ret < 0) { | |
731 | sr_err("Failed to release interface: %s.", | |
732 | libusb_error_name(ret)); | |
733 | libusb_close(usb.devhdl); | |
734 | return SR_ERR; | |
735 | } | |
736 | ||
737 | libusb_close(usb.devhdl); | |
738 | ||
739 | return SR_OK; | |
740 | } | |
741 | ||
742 | SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data) | |
743 | { | |
744 | return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN, | |
745 | USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
746 | (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS); | |
747 | } | |
748 | ||
749 | SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data) | |
750 | { | |
751 | return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT, | |
752 | USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE, | |
753 | (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS); | |
754 | } |