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