1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
#![allow(non_camel_case_types)]

pub mod constants;

use self::constants::*;
use libc::{c_char, c_int, c_short, c_uchar, c_uint, c_void, ssize_t, timeval};

#[repr(C)]
pub struct libusb_context {
    __private: c_void,
}

#[repr(C)]
pub struct libusb_device {
    __private: c_void,
}

#[repr(C)]
pub struct libusb_device_handle {
    __private: c_void,
}

#[repr(C)]
pub struct libusb_version {
    pub major: u16,
    pub minor: u16,
    pub micro: u16,
    pub nano: u16,
    pub rc: *const c_char,
    pub describe: *const c_char,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_device_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bcdUSB: u16,
    pub bDeviceClass: u8,
    pub bDeviceSubClass: u8,
    pub bDeviceProtocol: u8,
    pub bMaxPacketSize0: u8,
    pub idVendor: u16,
    pub idProduct: u16,
    pub bcdDevice: u16,
    pub iManufacturer: u8,
    pub iProduct: u8,
    pub iSerialNumber: u8,
    pub bNumConfigurations: u8,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_config_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub wTotalLength: u16,
    pub bNumInterfaces: u8,
    pub bConfigurationValue: u8,
    pub iConfiguration: u8,
    pub bmAttributes: u8,
    pub bMaxPower: u8,
    pub interface: *const libusb_interface,
    pub extra: *const c_uchar,
    pub extra_length: c_int,
}

#[repr(C)]
pub struct libusb_interface {
    pub altsetting: *const libusb_interface_descriptor,
    pub num_altsetting: c_int,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_interface_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bInterfaceNumber: u8,
    pub bAlternateSetting: u8,
    pub bNumEndpoints: u8,
    pub bInterfaceClass: u8,
    pub bInterfaceSubClass: u8,
    pub bInterfaceProtocol: u8,
    pub iInterface: u8,
    pub endpoint: *const libusb_endpoint_descriptor,
    pub extra: *const c_uchar,
    pub extra_length: c_int,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_endpoint_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bEndpointAddress: u8,
    pub bmAttributes: u8,
    pub wMaxPacketSize: u16,
    pub bInterval: u8,
    pub bRefresh: u8,
    pub bSynchAddress: u8,
    pub extra: *const c_uchar,
    pub extra_length: c_int,
}

#[repr(C)]
pub struct libusb_iso_packet_descriptor {
    pub length: c_uint,
    pub actual_length: c_uint,
    pub status: c_int,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_ss_endpoint_companion_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bMaxBurst: u8,
    pub bmAttributes: u8,
    pub wBytesPerInterval: u16,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_bos_dev_capability_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bDevCapabilityType: u8,
    pub dev_capability_data: [u8; 0],
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_bos_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub wTotalLength: u16,
    pub bNumDeviceCaps: u8,
    pub dev_capability: [libusb_bos_dev_capability_descriptor; 0],
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_usb_2_0_extension_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bDevCapabilityType: u8,
    pub bmAttributes: u32,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_ss_usb_device_capability_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bDevCapabilityType: u8,
    pub bmAttributes: u8,
    pub wSpeedSupported: u16,
    pub bFunctionalitySupport: u8,
    pub bU1DevExitLat: u8,
    pub bU2DevExitLat: u8,
}

#[allow(non_snake_case)]
#[repr(C)]
pub struct libusb_container_id_descriptor {
    pub bLength: u8,
    pub bDescriptorType: u8,
    pub bDevCapabilityType: u8,
    pub bReserved: u8,
    pub ContainerId: [u8; 16],
}

#[allow(non_snake_case)]
#[repr(C, packed)]
pub struct libusb_control_setup {
    pub bmRequestType: u8,
    pub bRequest: u8,
    pub wValue: u16,
    pub wIndex: u16,
    pub wLength: u16,
}

#[repr(C)]
pub struct libusb_transfer {
    pub dev_handle: *mut libusb_device_handle,
    pub flags: u8,
    pub endpoint: c_uchar,
    pub transfer_type: c_uchar,
    pub timeout: c_uint,
    pub status: c_int,
    pub length: c_int,
    pub actual_length: c_int,
    pub callback: libusb_transfer_cb_fn,
    pub user_data: *mut c_void,
    pub buffer: *mut c_uchar,
    pub num_iso_packets: c_int,
    pub iso_packet_desc: [libusb_iso_packet_descriptor; 0],
}

#[repr(C)]
pub struct libusb_pollfd {
    pub fd: c_int,
    pub events: c_short,
}

pub type libusb_hotplug_callback_handle = c_int;
pub type libusb_hotplug_flag = c_int;
pub type libusb_hotplug_event = c_int;

pub type libusb_log_cb_mode = c_int;

pub type libusb_transfer_cb_fn = extern "system" fn(*mut libusb_transfer);
pub type libusb_pollfd_added_cb = extern "system" fn(c_int, c_short, *mut c_void);
pub type libusb_pollfd_removed_cb = extern "system" fn(c_int, *mut c_void);
pub type libusb_hotplug_callback_fn = extern "system" fn(
    ctx: *mut libusb_context,
    device: *mut libusb_device,
    event: libusb_hotplug_event,
    user_data: *mut c_void,
) -> c_int;

pub type libusb_log_cb = extern "system" fn(context: *mut libusb_context, c_int, *mut c_void);

extern "system" {
    pub fn libusb_get_version() -> *const libusb_version;
    pub fn libusb_has_capability(capability: u32) -> c_int;
    pub fn libusb_error_name(errcode: c_int) -> *const c_char;
    pub fn libusb_setlocale(locale: *const c_char) -> c_int;
    pub fn libusb_strerror(errcode: c_int) -> *const c_char;

    pub fn libusb_init(context: *mut *mut libusb_context) -> c_int;
    pub fn libusb_exit(context: *mut libusb_context);
    pub fn libusb_set_debug(context: *mut libusb_context, level: c_int);
    pub fn libusb_set_log_cb(context: *mut libusb_context, cb: Option<libusb_log_cb>, mode: c_int);

    pub fn libusb_get_device_list(
        context: *mut libusb_context,
        list: *mut *const *mut libusb_device,
    ) -> ssize_t;
    pub fn libusb_free_device_list(list: *const *mut libusb_device, unref_devices: c_int);
    pub fn libusb_get_parent(dev: *mut libusb_device) -> *mut libusb_device;
    pub fn libusb_get_device(dev_handle: *mut libusb_device_handle) -> *mut libusb_device;

    pub fn libusb_ref_device(dev: *mut libusb_device) -> *mut libusb_device;
    pub fn libusb_unref_device(dev: *mut libusb_device);

    pub fn libusb_get_device_descriptor(
        dev: *const libusb_device,
        desc: *mut libusb_device_descriptor,
    ) -> c_int;
    pub fn libusb_get_config_descriptor(
        dev: *const libusb_device,
        index: u8,
        config: *mut *const libusb_config_descriptor,
    ) -> c_int;
    pub fn libusb_get_active_config_descriptor(
        dev: *const libusb_device,
        config: *mut *const libusb_config_descriptor,
    ) -> c_int;
    pub fn libusb_get_config_descriptor_by_value(
        dev: *const libusb_device,
        bConfigurationValue: u8,
        config: *mut *const libusb_config_descriptor,
    ) -> c_int;
    pub fn libusb_free_config_descriptor(config: *const libusb_config_descriptor);

    pub fn libusb_get_bus_number(dev: *const libusb_device) -> u8;
    pub fn libusb_get_port_number(dev: *mut libusb_device) -> u8;
    pub fn libusb_get_port_numbers(
        dev: *mut libusb_device,
        port_numbers: *mut u8,
        port_numbers_len: c_int,
    ) -> c_int;
    pub fn libusb_get_device_address(dev: *const libusb_device) -> u8;
    pub fn libusb_get_device_speed(dev: *const libusb_device) -> c_int;
    pub fn libusb_get_max_packet_size(dev: *const libusb_device, endpoint: c_uchar) -> c_int;
    pub fn libusb_get_max_iso_packet_size(dev: *const libusb_device, endpoint: c_uchar) -> c_int;

    pub fn libusb_wrap_sys_device(
        context: *mut libusb_context,
        sys_dev: *mut c_int,
        handle: *mut *mut libusb_device_handle,
    ) -> c_int;
    pub fn libusb_open(dev: *const libusb_device, handle: *mut *mut libusb_device_handle) -> c_int;
    pub fn libusb_close(dev_handle: *mut libusb_device_handle);
    pub fn libusb_open_device_with_vid_pid(
        context: *mut libusb_context,
        vendor_id: u16,
        product_id: u16,
    ) -> *mut libusb_device_handle;
    pub fn libusb_reset_device(dev_handle: *mut libusb_device_handle) -> c_int;
    pub fn libusb_clear_halt(dev_handle: *mut libusb_device_handle, endpoint: c_uchar) -> c_int;
    pub fn libusb_alloc_streams(
        dev_handle: *mut libusb_device_handle,
        num_streams: u32,
        endpoints: *mut c_uchar,
        num_endpoints: c_int,
    ) -> c_int;
    pub fn libusb_free_streams(
        dev_handle: *mut libusb_device_handle,
        endpoints: *mut c_uchar,
        num_endpoints: c_int,
    ) -> c_int;
    pub fn libusb_get_string_descriptor_ascii(
        dev_handle: *mut libusb_device_handle,
        desc_index: u8,
        data: *mut c_uchar,
        length: c_int,
    ) -> c_int;

    pub fn libusb_get_configuration(
        dev_handle: *mut libusb_device_handle,
        config: *mut c_int,
    ) -> c_int;
    pub fn libusb_set_configuration(dev_handle: *mut libusb_device_handle, config: c_int) -> c_int;

    pub fn libusb_get_ss_endpoint_companion_descriptor(
        context: *mut libusb_context,
        endpoint: *const libusb_endpoint_descriptor,
        ep_comp: *mut *const libusb_ss_endpoint_companion_descriptor,
    ) -> c_int;
    pub fn libusb_free_ss_endpoint_companion_descriptor(
        ep_comp: *mut libusb_ss_endpoint_companion_descriptor,
    );
    pub fn libusb_get_bos_descriptor(
        dev_handle: *mut libusb_device_handle,
        bos: *mut *const libusb_bos_descriptor,
    ) -> c_int;
    pub fn libusb_free_bos_descriptor(bos: *mut libusb_bos_descriptor);
    pub fn libusb_get_usb_2_0_extension_descriptor(
        context: *mut libusb_context,
        dev_cap: *mut libusb_bos_dev_capability_descriptor,
        usb_2_0_extension: *mut *const libusb_usb_2_0_extension_descriptor,
    ) -> c_int;
    pub fn libusb_free_usb_2_0_extension_descriptor(
        usb_2_0_extension: *mut libusb_usb_2_0_extension_descriptor,
    );
    pub fn libusb_get_ss_usb_device_capability_descriptor(
        context: *mut libusb_context,
        dev_cap: *mut libusb_bos_dev_capability_descriptor,
        ss_usb_device_cap: *mut *const libusb_ss_usb_device_capability_descriptor,
    ) -> c_int;
    pub fn libusb_free_ss_usb_device_capability_descriptor(
        ss_usb_device_cap: *mut libusb_ss_usb_device_capability_descriptor,
    );
    pub fn libusb_get_container_id_descriptor(
        context: *mut libusb_context,
        dev_cap: *mut libusb_bos_dev_capability_descriptor,
        container_id: *mut *const libusb_container_id_descriptor,
    ) -> c_int;
    pub fn libusb_free_container_id_descriptor(container_id: *mut libusb_container_id_descriptor);

    pub fn libusb_set_auto_detach_kernel_driver(
        dev_handle: *mut libusb_device_handle,
        enable: c_int,
    ) -> c_int;
    pub fn libusb_kernel_driver_active(
        dev_handle: *mut libusb_device_handle,
        interface_number: c_int,
    ) -> c_int;
    pub fn libusb_detach_kernel_driver(
        dev_handle: *mut libusb_device_handle,
        interface_number: c_int,
    ) -> c_int;
    pub fn libusb_attach_kernel_driver(
        dev_handle: *mut libusb_device_handle,
        interface_number: c_int,
    ) -> c_int;

    pub fn libusb_claim_interface(
        dev_handle: *mut libusb_device_handle,
        interface_number: c_int,
    ) -> c_int;
    pub fn libusb_release_interface(
        dev_handle: *mut libusb_device_handle,
        interface_number: c_int,
    ) -> c_int;
    pub fn libusb_set_interface_alt_setting(
        dev_handle: *mut libusb_device_handle,
        interface_number: c_int,
        alternate_setting: c_int,
    ) -> c_int;

    pub fn libusb_interrupt_transfer(
        dev_handle: *mut libusb_device_handle,
        endpoint: c_uchar,
        data: *mut c_uchar,
        length: c_int,
        transferred: *mut c_int,
        timeout: c_uint,
    ) -> c_int;
    pub fn libusb_bulk_transfer(
        dev_handle: *mut libusb_device_handle,
        endpoint: c_uchar,
        data: *mut c_uchar,
        length: c_int,
        transferred: *mut c_int,
        timeout: c_uint,
    ) -> c_int;
    pub fn libusb_control_transfer(
        dev_handle: *mut libusb_device_handle,
        request_type: u8,
        request: u8,
        value: u16,
        index: u16,
        data: *mut c_uchar,
        length: u16,
        timeout: c_uint,
    ) -> c_int;

    pub fn libusb_alloc_transfer(iso_packets: c_int) -> *mut libusb_transfer;
    pub fn libusb_submit_transfer(transfer: *mut libusb_transfer) -> c_int;
    pub fn libusb_cancel_transfer(transfer: *mut libusb_transfer) -> c_int;
    pub fn libusb_free_transfer(transfer: *mut libusb_transfer);
    pub fn libusb_transfer_set_stream_id(transfer: *mut libusb_transfer, stream_id: u32);
    pub fn libusb_transfer_get_stream_id(transfer: *mut libusb_transfer) -> u32;

    pub fn libusb_handle_events(context: *mut libusb_context) -> c_int;
    pub fn libusb_handle_events_timeout(context: *mut libusb_context, tv: *const timeval) -> c_int;
    pub fn libusb_handle_events_completed(
        context: *mut libusb_context,
        completed: *mut c_int,
    ) -> c_int;
    pub fn libusb_handle_events_timeout_completed(
        context: *mut libusb_context,
        tv: *const timeval,
        completed: *mut c_int,
    ) -> c_int;
    pub fn libusb_handle_events_locked(context: *mut libusb_context, tv: *const timeval) -> c_int;
    pub fn libusb_interrupt_event_handler(context: *mut libusb_context);

    pub fn libusb_try_lock_events(context: *mut libusb_context) -> c_int;
    pub fn libusb_lock_events(context: *mut libusb_context);
    pub fn libusb_unlock_events(context: *mut libusb_context);
    pub fn libusb_event_handling_ok(context: *mut libusb_context) -> c_int;
    pub fn libusb_event_handler_active(context: *mut libusb_context) -> c_int;
    pub fn libusb_lock_event_waiters(context: *mut libusb_context);
    pub fn libusb_unlock_event_waiters(context: *mut libusb_context);
    pub fn libusb_wait_for_event(context: *mut libusb_context, tv: *const timeval) -> c_int;

    pub fn libusb_pollfds_handle_timeouts(context: *mut libusb_context) -> c_int;
    pub fn libusb_get_next_timeout(context: *mut libusb_context, tv: *mut timeval) -> c_int;
    pub fn libusb_get_pollfds(context: *mut libusb_context) -> *const *mut libusb_pollfd;
    pub fn libusb_set_pollfd_notifiers(
        context: *mut libusb_context,
        added_cb: Option<libusb_pollfd_added_cb>,
        removed_cb: Option<libusb_pollfd_removed_cb>,
        user_data: *mut c_void,
    );
    pub fn libusb_free_pollfds(pollfds: *const *mut libusb_pollfd);
    pub fn libusb_hotplug_register_callback(
        ctx: *mut libusb_context,
        events: c_int,
        flags: c_int,
        vendor_id: c_int,
        product_id: c_int,
        dev_class: c_int,
        cb_fn: libusb_hotplug_callback_fn,
        user_data: *mut c_void,
        callback_handle: *mut libusb_hotplug_callback_handle,
    ) -> c_int;
    pub fn libusb_hotplug_deregister_callback(
        ctx: *mut libusb_context,
        callback_handle: libusb_hotplug_callback_handle,
    );

    pub fn libusb_hotplug_get_user_data(
        ctx: *mut libusb_context,
        callback_handle: libusb_hotplug_callback_handle,
    ) -> *mut c_void;
}

// As libusb_set_option is a variatic function, it must use "C"
// calling conventions
extern "C" {
    pub fn libusb_set_option(ctx: *mut libusb_context, option: u32, ...) -> c_int;
}

// defined as static inline in libusb.h
#[inline]
pub unsafe fn libusb_get_string_descriptor(
    dev_handle: *mut libusb_device_handle,
    desc_index: u8,
    langid: u16,
    data: *mut c_uchar,
    length: c_int,
) -> c_int {
    libusb_control_transfer(
        dev_handle,
        LIBUSB_ENDPOINT_IN,
        LIBUSB_REQUEST_GET_DESCRIPTOR,
        u16::from(LIBUSB_DT_STRING) << 8 | u16::from(desc_index),
        langid,
        data,
        length as u16,
        1000,
    )
}

// defined as static inline in libusb.h
#[inline]
pub unsafe fn libusb_get_descriptor(
    dev_handle: *mut libusb_device_handle,
    desc_type: u8,
    desc_index: u8,
    langid: u16,
    data: *mut c_uchar,
    length: c_int,
) -> c_int {
    libusb_control_transfer(
        dev_handle,
        LIBUSB_ENDPOINT_IN,
        LIBUSB_REQUEST_GET_DESCRIPTOR,
        u16::from(desc_type) << 8 | u16::from(desc_index),
        langid,
        data,
        length as u16,
        1000,
    )
}

#[inline]
pub unsafe fn libusb_control_transfer_get_data(transfer: *mut libusb_transfer) -> *mut c_uchar {
    (*transfer).buffer.add(constants::LIBUSB_CONTROL_SETUP_SIZE)
}

#[inline]
pub unsafe fn libusb_control_transfer_get_setup(
    transfer: *mut libusb_transfer,
) -> *mut libusb_control_setup {
    (*transfer).buffer as *mut _
}

#[allow(non_snake_case)]
#[inline]
pub unsafe fn libusb_fill_control_setup(
    buffer: *mut c_uchar,
    bmRequestType: u8,
    bRequest: u8,
    wValue: u16,
    wIndex: u16,
    wLength: u16,
) {
    let setup: *mut libusb_control_setup = buffer as *mut _;
    (*setup).bmRequestType = bmRequestType;
    (*setup).bRequest = bRequest;
    (*setup).wValue = wValue.to_le();
    (*setup).wIndex = wIndex.to_le();
    (*setup).wLength = wLength.to_le();
}

#[inline]
pub unsafe fn libusb_fill_control_transfer(
    transfer: *mut libusb_transfer,
    dev_handle: *mut libusb_device_handle,
    buffer: *mut u8,
    callback: libusb_transfer_cb_fn,
    user_data: *mut c_void,
    timeout: c_uint,
) {
    let setup: *mut libusb_control_setup = buffer as *mut c_void as *mut libusb_control_setup;

    (*transfer).dev_handle = dev_handle;
    (*transfer).endpoint = 0;
    (*transfer).transfer_type = LIBUSB_TRANSFER_TYPE_CONTROL;
    (*transfer).timeout = timeout;
    (*transfer).buffer = buffer;
    if !buffer.is_null() {
        (*transfer).length =
            (constants::LIBUSB_CONTROL_SETUP_SIZE as u16 + u16::from_le((*setup).wLength)).into();
    }
    (*transfer).user_data = user_data;
    (*transfer).callback = callback;
}

#[inline]
pub unsafe fn libusb_fill_bulk_transfer(
    transfer: *mut libusb_transfer,
    dev_handle: *mut libusb_device_handle,
    endpoint: u8,
    buffer: *mut u8,
    length: c_int,
    callback: libusb_transfer_cb_fn,
    user_data: *mut c_void,
    timeout: c_uint,
) {
    (*transfer).dev_handle = dev_handle;
    (*transfer).endpoint = endpoint;
    (*transfer).transfer_type = LIBUSB_TRANSFER_TYPE_BULK;
    (*transfer).timeout = timeout;
    (*transfer).buffer = buffer;
    (*transfer).length = length;
    (*transfer).user_data = user_data;
    (*transfer).callback = callback;
}

#[inline]
pub unsafe fn libusb_fill_bulk_stream_transfer(
    transfer: *mut libusb_transfer,
    dev_handle: *mut libusb_device_handle,
    endpoint: u8,
    stream_id: u32,
    buffer: *mut u8,
    length: c_int,
    callback: libusb_transfer_cb_fn,
    user_data: *mut c_void,
    timeout: c_uint,
) {
    libusb_fill_bulk_transfer(
        transfer, dev_handle, endpoint, buffer, length, callback, user_data, timeout,
    );
    (*transfer).transfer_type = LIBUSB_TRANSFER_TYPE_BULK_STREAM;
    libusb_transfer_set_stream_id(transfer, stream_id);
}

#[inline]
pub unsafe fn libusb_fill_interrupt_transfer(
    transfer: *mut libusb_transfer,
    dev_handle: *mut libusb_device_handle,
    endpoint: u8,
    buffer: *mut u8,
    length: c_int,
    callback: libusb_transfer_cb_fn,
    user_data: *mut c_void,
    timeout: c_uint,
) {
    (*transfer).dev_handle = dev_handle;
    (*transfer).endpoint = endpoint;
    (*transfer).transfer_type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
    (*transfer).timeout = timeout;
    (*transfer).buffer = buffer;
    (*transfer).length = length;
    (*transfer).user_data = user_data;
    (*transfer).callback = callback;
}

#[inline]
pub unsafe fn libusb_fill_iso_transfer(
    transfer: *mut libusb_transfer,
    dev_handle: *mut libusb_device_handle,
    endpoint: u8,
    buffer: *mut u8,
    length: c_int,
    num_iso_packets: c_int,
    callback: libusb_transfer_cb_fn,
    user_data: *mut c_void,
    timeout: c_uint,
) {
    (*transfer).dev_handle = dev_handle;
    (*transfer).endpoint = endpoint;
    (*transfer).transfer_type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
    (*transfer).timeout = timeout;
    (*transfer).buffer = buffer;
    (*transfer).length = length;
    (*transfer).num_iso_packets = num_iso_packets;
    (*transfer).user_data = user_data;
    (*transfer).callback = callback;
}

#[inline]
pub unsafe fn libusb_set_iso_packet_lengths(transfer: *mut libusb_transfer, length: c_uint) {
    for i in 0..(*transfer).num_iso_packets {
        (*(*transfer).iso_packet_desc.as_mut_ptr().add(i as usize)).length = length;
    }
}

#[inline]
pub unsafe fn libusb_get_iso_packet_buffer(
    transfer: *mut libusb_transfer,
    packet: c_uint,
) -> *mut c_uchar {
    if packet as c_int >= (*transfer).num_iso_packets {
        return std::ptr::null_mut();
    }
    let mut offset = 0;
    for i in 0..packet {
        offset += (*(*transfer).iso_packet_desc.as_mut_ptr().add(i as usize)).length;
    }

    (*transfer).buffer.add(offset as usize)
}

#[inline]
pub unsafe fn libusb_get_iso_packet_buffer_simple(
    transfer: *mut libusb_transfer,
    packet: c_uint,
) -> *mut c_uchar {
    if packet as c_int >= (*transfer).num_iso_packets {
        return std::ptr::null_mut();
    }

    (*transfer)
        .buffer
        .add(((*(*transfer).iso_packet_desc.as_mut_ptr().add(0)).length * packet) as usize)
}