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