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