]> sigrok.org Git - libsigrok.git/blame - src/hardware/ikalogic-scanalogic2/protocol.c
dev_acquisition_{start,stop}(): Drop duplicate 'cb_data' parameter.
[libsigrok.git] / src / hardware / ikalogic-scanalogic2 / protocol.c
CommitLineData
16e76bae
MS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
16e76bae
MS
21#include "protocol.h"
22
e52e712d 23extern struct sr_dev_driver ikalogic_scanalogic2_driver_info;
e52e712d 24
79914b3a 25extern uint64_t sl2_samplerates[NUM_SAMPLERATES];
e52e712d
MS
26
27static void stop_acquisition(struct sr_dev_inst *sdi)
16e76bae 28{
338143ea 29 struct drv_context *drvc = sdi->driver->context;
e52e712d
MS
30
31 /* Remove USB file descriptors from polling. */
102f1239 32 usb_source_remove(sdi->session, drvc->sr_ctx);
16e76bae 33
695dc859 34 std_session_send_df_end(sdi, LOG_PREFIX);
e52e712d
MS
35
36 sdi->status = SR_ST_ACTIVE;
37}
38
39static void abort_acquisition(struct sr_dev_inst *sdi)
40{
338143ea 41 struct drv_context *drvc = sdi->driver->context;
e52e712d
MS
42
43 /* Remove USB file descriptors from polling. */
102f1239 44 usb_source_remove(sdi->session, drvc->sr_ctx);
e52e712d 45
695dc859 46 std_session_send_df_end(sdi, LOG_PREFIX);
e52e712d
MS
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
ba7dd8bb 58 if (devc->channels[devc->channel]->enabled) {
e52e712d
MS
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;
3f239f08 82 uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS];
e52e712d
MS
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 */
ba7dd8bb
UH
94 for (i = 0; i < devc->num_enabled_channels - 1; i++)
95 ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset;
e52e712d
MS
96
97 /*
c824eb63
UH
98 * Skip the first 4 bytes of the buffer because they contain channel
99 * and packet information only.
e52e712d
MS
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
b07b42f3
UH
114 * necessary might have been acquired. This is because
115 * the number of acquired samples is always rounded up
c824eb63 116 * to a multiple of 8.
e52e712d
MS
117 */
118 k = k - (devc->pre_trigger_bytes * 8) +
119 devc->pre_trigger_samples;
120
c824eb63 121 sr_dbg("Start processing at sample: %d.", 7 - k);
e52e712d
MS
122
123 /*
c824eb63
UH
124 * Send the trigger before the first sample is
125 * processed if no pre trigger samples were calculated
126 * through the capture ratio.
e52e712d
MS
127 */
128 if (devc->trigger_type != TRIGGER_TYPE_NONE &&
129 devc->pre_trigger_samples == 0) {
130 packet.type = SR_DF_TRIGGER;
695dc859 131 sr_session_send(sdi, &packet);
e52e712d
MS
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 */
ba7dd8bb 149 for (j = 0; j < devc->num_enabled_channels; j++) {
e52e712d 150 tmp = (ptr[j][i] & (1 << k)) >> k;
ba7dd8bb 151 buffer[n] |= tmp << devc->channel_map[j];
e52e712d
MS
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;
695dc859 169 sr_session_send(sdi, &packet);
e52e712d
MS
170
171 packet.type = SR_DF_TRIGGER;
695dc859 172 sr_session_send(sdi, &packet);
e52e712d
MS
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;
695dc859 185 sr_session_send(sdi, &packet);
e52e712d
MS
186 }
187}
188
79914b3a 189SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
e52e712d
MS
190{
191 struct sr_dev_inst *sdi;
4f840ce9 192 struct sr_dev_driver *di;
e52e712d
MS
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;
16e76bae
MS
201
202 if (!(sdi = cb_data))
203 return TRUE;
204
205 if (!(devc = sdi->priv))
206 return TRUE;
207
4f840ce9 208 di = sdi->driver;
41812aca 209 drvc = di->context;
e52e712d
MS
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 /*
c824eb63
UH
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.
e52e712d
MS
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) {
c824eb63 234 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
e52e712d
MS
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. */
c824eb63 246 if (devc->transfer_error)
e52e712d 247 abort_acquisition(sdi);
16e76bae
MS
248
249 return TRUE;
250}
e52e712d 251
55462b8b 252SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer)
e52e712d
MS
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) {
eb8e6cd2
UH
263 sr_err("Transfer to device failed: %s.",
264 libusb_error_name(transfer->status));
e52e712d
MS
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) {
c824eb63 274 sr_err("Submit transfer failed: %s.",
e52e712d
MS
275 libusb_error_name(ret));
276 devc->transfer_error = TRUE;
277 }
278
279 return;
280 }
281
28083961
UH
282 if (devc->state != devc->next_state)
283 sr_spew("State changed from %i to %i.",
284 devc->state, devc->next_state);
e52e712d
MS
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) {
ba7dd8bb 309 last_channel = devc->channel_map[devc->num_enabled_channels - 1];
e52e712d
MS
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
ba7dd8bb 318 * channels are processed.
e52e712d
MS
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 /*
c824eb63
UH
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.
e52e712d
MS
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
c824eb63
UH
371 * change to the idle state. Request a new device
372 * status until the device is ready.
e52e712d
MS
373 */
374 ret = libusb_submit_transfer(transfer);
375 }
376 }
377
378 if (ret != 0) {
c824eb63 379 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
e52e712d
MS
380 devc->transfer_error = TRUE;
381 }
382}
383
55462b8b 384SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer)
e52e712d
MS
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) {
eb8e6cd2
UH
394 sr_err("Transfer to device failed: %s.",
395 libusb_error_name(transfer->status));
e52e712d
MS
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) {
c824eb63 405 sr_err("Submit transfer failed: %s.",
e52e712d
MS
406 libusb_error_name(ret));
407
408 devc->transfer_error = TRUE;
409 }
410
411 return;
412 }
413
28083961
UH
414 if (devc->state != devc->next_state)
415 sr_spew("State changed from %i to %i.",
416 devc->state, devc->next_state);
e52e712d
MS
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) {
c824eb63 429 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
e52e712d
MS
430 devc->transfer_error = TRUE;
431 }
432}
433
79914b3a 434SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
e52e712d
MS
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++) {
79914b3a 443 if (sl2_samplerates[i] == samplerate) {
e52e712d
MS
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
79914b3a
UH
453SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
454 uint64_t limit_samples)
e52e712d
MS
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
c824eb63 469 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
e52e712d
MS
470
471 devc->limit_samples = limit_samples;
472
473 return SR_OK;
474}
475
02d5c0d8 476SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi)
e52e712d
MS
477{
478 struct dev_context *devc;
02d5c0d8
BV
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;
e52e712d
MS
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
0812c40e 491 if (!(trigger = sr_session_trigger_get(sdi->session)))
02d5c0d8 492 return SR_OK;
e52e712d 493
02d5c0d8
BV
494 if (g_slist_length(trigger->stages) > 1) {
495 sr_err("This device only supports 1 trigger stage.");
496 return SR_ERR;
497 }
e52e712d 498
02d5c0d8
BV
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 }
e52e712d 520 }
e52e712d
MS
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 */
3f239f08 527 if (num_triggers_anyedge == NUM_CHANNELS) {
e52e712d
MS
528 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
529 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
530 }
531
c824eb63 532 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
e52e712d 533 devc->trigger_channel, devc->trigger_type);
02d5c0d8
BV
534
535 return SR_OK;
e52e712d
MS
536}
537
79914b3a
UH
538SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
539 uint64_t capture_ratio)
e52e712d
MS
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
79914b3a
UH
557SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
558 uint64_t after_trigger_delay)
e52e712d
MS
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
79914b3a 578SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
e52e712d
MS
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
c824eb63
UH
613 * one sample byte larger than the minimal number of sample bytes
614 * needed to store the requested samples.
e52e712d
MS
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
79914b3a 632SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb,
e52e712d
MS
633 struct device_info *dev_info)
634{
635 struct drv_context *drvc;
636 uint8_t buffer[PACKET_LENGTH];
637 int ret;
638
41812aca 639 drvc = ikalogic_scanalogic2_driver_info.context;
e52e712d
MS
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) {
9526f2d4 656 sr_err("Failed to detach kernel driver: %s.",
e52e712d
MS
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;
79914b3a 679 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
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;
79914b3a 688 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
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
79914b3a 696 if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
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;
79914b3a 711 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
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
c824eb63
UH
720 * possibly will reset itself after a few seconds without being used
721 * and thereby close the connection.
e52e712d
MS
722 */
723 buffer[0] = CMD_IDLE;
79914b3a 724 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
e52e712d
MS
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) {
9526f2d4 735 sr_err("Failed to release interface: %s.",
e52e712d
MS
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
79914b3a 746SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
e52e712d
MS
747{
748 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
fb8d593c 749 USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
1a46cc62 750 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
e52e712d
MS
751}
752
79914b3a 753SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
e52e712d
MS
754{
755 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
756 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
1a46cc62 757 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
e52e712d 758}