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