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