usb_sys/
lib.rs

1// Copyright 2019 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Linux USB device filesystem ioctl bindings.
6
7// Translated from include/uapi/linux/usbdevice_fs.h
8
9#![allow(non_upper_case_globals)]
10#![allow(non_camel_case_types)]
11#![allow(non_snake_case)]
12#![allow(dead_code)]
13
14use std::os::raw::c_char;
15use std::os::raw::c_int;
16use std::os::raw::c_uchar;
17use std::os::raw::c_uint;
18use std::os::raw::c_void;
19
20use base::ioctl_io_nr;
21use base::ioctl_ior_nr;
22use base::ioctl_iow_nr;
23use base::ioctl_iowr_nr;
24
25#[repr(C)]
26#[derive(Default)]
27pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
28impl<T> __IncompleteArrayField<T> {
29    #[inline]
30    pub fn new() -> Self {
31        __IncompleteArrayField(::std::marker::PhantomData)
32    }
33    /// # Safety
34    ///
35    /// Caller must ensure that Self's size and alignment requirements matches
36    /// those of `T`s.
37    #[inline]
38    pub unsafe fn as_ptr(&self) -> *const T {
39        ::std::mem::transmute(self)
40    }
41    /// # Safety
42    ///
43    /// Caller must ensure that Self's size and alignment requirements matches
44    /// those of `T`s.
45    #[inline]
46    pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
47        ::std::mem::transmute(self)
48    }
49    /// # Safety
50    ///
51    /// Caller must ensure that Self's size and alignment requirements matches
52    /// those of `T`s.
53    #[inline]
54    pub unsafe fn as_slice(&self, len: usize) -> &[T] {
55        ::std::slice::from_raw_parts(self.as_ptr(), len)
56    }
57    /// # Safety
58    ///
59    /// Caller must ensure that Self's size and alignment requirements matches
60    /// those of `T`s.
61    #[inline]
62    pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
63        ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
64    }
65}
66impl<T> ::std::clone::Clone for __IncompleteArrayField<T> {
67    #[inline]
68    fn clone(&self) -> Self {
69        Self::new()
70    }
71}
72
73#[repr(C)]
74#[derive(Copy, Clone)]
75pub struct usbdevfs_ctrltransfer {
76    pub bRequestType: u8,
77    pub bRequest: u8,
78    pub wValue: u16,
79    pub wIndex: u16,
80    pub wLength: u16,
81    pub timeout: u32,
82    pub data: *mut c_void,
83}
84
85#[repr(C)]
86#[derive(Copy, Clone)]
87pub struct usbdevfs_bulktransfer {
88    pub ep: c_uint,
89    pub len: c_uint,
90    pub timeout: c_uint,
91    pub data: *mut c_void,
92}
93
94#[repr(C)]
95#[derive(Default, Copy, Clone)]
96pub struct usbdevfs_setinterface {
97    pub interface: c_uint,
98    pub altsetting: c_uint,
99}
100
101#[repr(C)]
102#[derive(Default, Copy, Clone)]
103struct usbdevfs_disconnectsignal {
104    pub signr: c_uint,
105    pub context: usize,
106}
107
108pub const USBDEVFS_MAXDRIVERNAME: usize = 255;
109
110#[repr(C)]
111#[derive(Copy, Clone)]
112pub struct usbdevfs_getdriver {
113    pub interface: c_uint,
114    pub driver: [u8; USBDEVFS_MAXDRIVERNAME + 1],
115}
116
117#[repr(C)]
118#[derive(Default, Copy, Clone)]
119pub struct usbdevfs_connectinfo {
120    pub devnum: c_uint,
121    pub slow: c_char,
122}
123
124pub const USBDEVFS_URB_SHORT_NOT_OK: c_uint = 0x01;
125pub const USBDEVFS_URB_ISO_ASAP: c_uint = 0x02;
126pub const USBDEVFS_URB_BULK_CONTINUATION: c_uint = 0x04;
127pub const USBDEVFS_URB_NO_FSBR: c_uint = 0x20;
128pub const USBDEVFS_URB_ZERO_PACKET: c_uint = 0x40;
129pub const USBDEVFS_URB_NO_INTERRUPT: c_uint = 0x80;
130
131pub const USBDEVFS_URB_TYPE_ISO: c_uchar = 0;
132pub const USBDEVFS_URB_TYPE_INTERRUPT: c_uchar = 1;
133pub const USBDEVFS_URB_TYPE_CONTROL: c_uchar = 2;
134pub const USBDEVFS_URB_TYPE_BULK: c_uchar = 3;
135
136#[repr(C)]
137#[derive(Default, Copy, Clone)]
138pub struct usbdevfs_iso_packet_desc {
139    pub length: c_uint,
140    pub actual_length: c_uint,
141    pub status: c_uint,
142}
143
144#[repr(C)]
145#[derive(Clone)]
146pub struct usbdevfs_urb {
147    pub urb_type: c_uchar,
148    pub endpoint: c_uchar,
149    pub status: c_int,
150    pub flags: c_uint,
151    pub buffer: *mut c_void,
152    pub buffer_length: c_int,
153    pub actual_length: c_int,
154    pub start_frame: c_int,
155    pub number_of_packets_or_stream_id: c_uint,
156    pub error_count: c_int,
157    pub signr: c_uint,
158    pub usercontext: usize,
159    pub iso_frame_desc: __IncompleteArrayField<usbdevfs_iso_packet_desc>,
160}
161
162impl Default for usbdevfs_urb {
163    fn default() -> Self {
164        // SAFETY: trivially safe
165        unsafe { ::std::mem::zeroed() }
166    }
167}
168
169// SAFETY:
170// The structure that embeds this should ensure that this is safe.
171unsafe impl Send for usbdevfs_urb {}
172// SAFETY:
173// The structure that embeds this should ensure that this is safe.
174unsafe impl Sync for usbdevfs_urb {}
175
176#[repr(C)]
177#[derive(Copy, Clone)]
178pub struct usbdevfs_ioctl {
179    pub ifno: c_int,
180    pub ioctl_code: c_int,
181    pub data: *mut c_void,
182}
183
184#[repr(C)]
185#[derive(Copy, Clone)]
186pub struct usbdevfs_hub_portinfo {
187    pub nports: c_char,
188    pub port: [u8; 127],
189}
190
191pub const USBDEVFS_CAP_ZERO_PACKET: u32 = 0x01;
192pub const USBDEVFS_CAP_BULK_CONTINUATION: u32 = 0x02;
193pub const USBDEVFS_CAP_NO_PACKET_SIZE_LIM: u32 = 0x04;
194pub const USBDEVFS_CAP_BULK_SCATTER_GATHER: u32 = 0x08;
195pub const USBDEVFS_CAP_REAP_AFTER_DISCONNECT: u32 = 0x10;
196pub const USBDEVFS_CAP_MMAP: u32 = 0x20;
197pub const USBDEVFS_CAP_DROP_PRIVILEGES: u32 = 0x40;
198
199pub const USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER: c_uint = 0x01;
200pub const USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER: c_uint = 0x02;
201
202#[repr(C)]
203#[derive(Copy, Clone)]
204pub struct usbdevfs_disconnect_claim {
205    pub interface: c_uint,
206    pub flags: c_uint,
207    pub driver: [u8; USBDEVFS_MAXDRIVERNAME + 1],
208}
209
210#[repr(C)]
211pub struct usbdevfs_streams {
212    pub num_streams: c_uint,
213    pub num_eps: c_uint,
214    pub eps: __IncompleteArrayField<c_uchar>,
215}
216
217impl Default for usbdevfs_streams {
218    fn default() -> Self {
219        // SAFETY: trivially safe
220        unsafe { ::std::mem::zeroed() }
221    }
222}
223
224const U: u32 = 'U' as u32;
225
226ioctl_iowr_nr!(USBDEVFS_CONTROL, U, 0, usbdevfs_ctrltransfer);
227ioctl_iowr_nr!(USBDEVFS_BULK, U, 2, usbdevfs_bulktransfer);
228ioctl_ior_nr!(USBDEVFS_RESETEP, U, 3, c_uint);
229ioctl_ior_nr!(USBDEVFS_SETINTERFACE, U, 4, usbdevfs_setinterface);
230ioctl_ior_nr!(USBDEVFS_SETCONFIGURATION, U, 5, c_uint);
231ioctl_ior_nr!(USBDEVFS_GETDRIVER, U, 8, usbdevfs_getdriver);
232ioctl_ior_nr!(USBDEVFS_SUBMITURB, U, 10, usbdevfs_urb);
233ioctl_io_nr!(USBDEVFS_DISCARDURB, U, 11);
234ioctl_iow_nr!(USBDEVFS_REAPURB, U, 12, *mut *mut usbdevfs_urb);
235ioctl_iow_nr!(USBDEVFS_REAPURBNDELAY, U, 13, *mut *mut usbdevfs_urb);
236ioctl_ior_nr!(USBDEVFS_DISCSIGNAL, U, 14, usbdevfs_disconnectsignal);
237ioctl_ior_nr!(USBDEVFS_CLAIMINTERFACE, U, 15, c_uint);
238ioctl_ior_nr!(USBDEVFS_RELEASEINTERFACE, U, 16, c_uint);
239ioctl_iow_nr!(USBDEVFS_CONNECTINFO, U, 17, usbdevfs_connectinfo);
240ioctl_iowr_nr!(USBDEVFS_IOCTL, U, 18, usbdevfs_ioctl);
241ioctl_ior_nr!(USBDEVFS_HUB_PORTINFO, U, 19, usbdevfs_hub_portinfo);
242ioctl_io_nr!(USBDEVFS_RESET, U, 20);
243ioctl_ior_nr!(USBDEVFS_CLEAR_HALT, U, 21, c_uint);
244ioctl_io_nr!(USBDEVFS_DISCONNECT, U, 22);
245ioctl_io_nr!(USBDEVFS_CONNECT, U, 23);
246ioctl_ior_nr!(USBDEVFS_CLAIM_PORT, U, 24, c_uint);
247ioctl_ior_nr!(USBDEVFS_RELEASE_PORT, U, 25, c_uint);
248ioctl_ior_nr!(USBDEVFS_GET_CAPABILITIES, U, 26, u32);
249ioctl_ior_nr!(USBDEVFS_DISCONNECT_CLAIM, U, 27, usbdevfs_disconnect_claim);
250ioctl_ior_nr!(USBDEVFS_ALLOC_STREAMS, U, 28, usbdevfs_streams);
251ioctl_ior_nr!(USBDEVFS_FREE_STREAMS, U, 29, usbdevfs_streams);
252ioctl_iow_nr!(USBDEVFS_DROP_PRIVILEGES, U, 30, u32);
253ioctl_io_nr!(USBDEVFS_GET_SPEED, U, 31);