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