xref: /aosp_15_r20/external/crosvm/usb_sys/src/lib.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
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 
14 use std::os::raw::c_char;
15 use std::os::raw::c_int;
16 use std::os::raw::c_uchar;
17 use std::os::raw::c_uint;
18 use std::os::raw::c_void;
19 
20 use base::ioctl_io_nr;
21 use base::ioctl_ior_nr;
22 use base::ioctl_iow_nr;
23 use base::ioctl_iowr_nr;
24 
25 #[repr(C)]
26 #[derive(Default)]
27 pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>);
28 impl<T> __IncompleteArrayField<T> {
29     #[inline]
new() -> Self30     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]
as_ptr(&self) -> *const T38     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]
as_mut_ptr(&mut self) -> *mut T46     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]
as_slice(&self, len: usize) -> &[T]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]
as_mut_slice(&mut self, len: usize) -> &mut [T]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 }
66 impl<T> ::std::clone::Clone for __IncompleteArrayField<T> {
67     #[inline]
clone(&self) -> Self68     fn clone(&self) -> Self {
69         Self::new()
70     }
71 }
72 
73 #[repr(C)]
74 #[derive(Copy, Clone)]
75 pub 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)]
87 pub 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)]
96 pub struct usbdevfs_setinterface {
97     pub interface: c_uint,
98     pub altsetting: c_uint,
99 }
100 
101 #[repr(C)]
102 #[derive(Default, Copy, Clone)]
103 struct usbdevfs_disconnectsignal {
104     pub signr: c_uint,
105     pub context: usize,
106 }
107 
108 pub const USBDEVFS_MAXDRIVERNAME: usize = 255;
109 
110 #[repr(C)]
111 #[derive(Copy, Clone)]
112 pub 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)]
119 pub struct usbdevfs_connectinfo {
120     pub devnum: c_uint,
121     pub slow: c_char,
122 }
123 
124 pub const USBDEVFS_URB_SHORT_NOT_OK: c_uint = 0x01;
125 pub const USBDEVFS_URB_ISO_ASAP: c_uint = 0x02;
126 pub const USBDEVFS_URB_BULK_CONTINUATION: c_uint = 0x04;
127 pub const USBDEVFS_URB_NO_FSBR: c_uint = 0x20;
128 pub const USBDEVFS_URB_ZERO_PACKET: c_uint = 0x40;
129 pub const USBDEVFS_URB_NO_INTERRUPT: c_uint = 0x80;
130 
131 pub const USBDEVFS_URB_TYPE_ISO: c_uchar = 0;
132 pub const USBDEVFS_URB_TYPE_INTERRUPT: c_uchar = 1;
133 pub const USBDEVFS_URB_TYPE_CONTROL: c_uchar = 2;
134 pub const USBDEVFS_URB_TYPE_BULK: c_uchar = 3;
135 
136 #[repr(C)]
137 #[derive(Default, Copy, Clone)]
138 pub 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)]
146 pub 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 
162 impl Default for usbdevfs_urb {
default() -> Self163     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.
171 unsafe impl Send for usbdevfs_urb {}
172 // SAFETY:
173 // The structure that embeds this should ensure that this is safe.
174 unsafe impl Sync for usbdevfs_urb {}
175 
176 #[repr(C)]
177 #[derive(Copy, Clone)]
178 pub 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)]
186 pub struct usbdevfs_hub_portinfo {
187     pub nports: c_char,
188     pub port: [u8; 127],
189 }
190 
191 pub const USBDEVFS_CAP_ZERO_PACKET: u32 = 0x01;
192 pub const USBDEVFS_CAP_BULK_CONTINUATION: u32 = 0x02;
193 pub const USBDEVFS_CAP_NO_PACKET_SIZE_LIM: u32 = 0x04;
194 pub const USBDEVFS_CAP_BULK_SCATTER_GATHER: u32 = 0x08;
195 pub const USBDEVFS_CAP_REAP_AFTER_DISCONNECT: u32 = 0x10;
196 pub const USBDEVFS_CAP_MMAP: u32 = 0x20;
197 pub const USBDEVFS_CAP_DROP_PRIVILEGES: u32 = 0x40;
198 
199 pub const USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER: c_uint = 0x01;
200 pub const USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER: c_uint = 0x02;
201 
202 #[repr(C)]
203 #[derive(Copy, Clone)]
204 pub 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)]
211 pub struct usbdevfs_streams {
212     pub num_streams: c_uint,
213     pub num_eps: c_uint,
214     pub eps: __IncompleteArrayField<c_uchar>,
215 }
216 
217 impl Default for usbdevfs_streams {
default() -> Self218     fn default() -> Self {
219         // SAFETY: trivially safe
220         unsafe { ::std::mem::zeroed() }
221     }
222 }
223 
224 const U: u32 = 'U' as u32;
225 
226 ioctl_iowr_nr!(USBDEVFS_CONTROL, U, 0, usbdevfs_ctrltransfer);
227 ioctl_iowr_nr!(USBDEVFS_BULK, U, 2, usbdevfs_bulktransfer);
228 ioctl_ior_nr!(USBDEVFS_RESETEP, U, 3, c_uint);
229 ioctl_ior_nr!(USBDEVFS_SETINTERFACE, U, 4, usbdevfs_setinterface);
230 ioctl_ior_nr!(USBDEVFS_SETCONFIGURATION, U, 5, c_uint);
231 ioctl_ior_nr!(USBDEVFS_GETDRIVER, U, 8, usbdevfs_getdriver);
232 ioctl_ior_nr!(USBDEVFS_SUBMITURB, U, 10, usbdevfs_urb);
233 ioctl_io_nr!(USBDEVFS_DISCARDURB, U, 11);
234 ioctl_iow_nr!(USBDEVFS_REAPURB, U, 12, *mut *mut usbdevfs_urb);
235 ioctl_iow_nr!(USBDEVFS_REAPURBNDELAY, U, 13, *mut *mut usbdevfs_urb);
236 ioctl_ior_nr!(USBDEVFS_DISCSIGNAL, U, 14, usbdevfs_disconnectsignal);
237 ioctl_ior_nr!(USBDEVFS_CLAIMINTERFACE, U, 15, c_uint);
238 ioctl_ior_nr!(USBDEVFS_RELEASEINTERFACE, U, 16, c_uint);
239 ioctl_iow_nr!(USBDEVFS_CONNECTINFO, U, 17, usbdevfs_connectinfo);
240 ioctl_iowr_nr!(USBDEVFS_IOCTL, U, 18, usbdevfs_ioctl);
241 ioctl_ior_nr!(USBDEVFS_HUB_PORTINFO, U, 19, usbdevfs_hub_portinfo);
242 ioctl_io_nr!(USBDEVFS_RESET, U, 20);
243 ioctl_ior_nr!(USBDEVFS_CLEAR_HALT, U, 21, c_uint);
244 ioctl_io_nr!(USBDEVFS_DISCONNECT, U, 22);
245 ioctl_io_nr!(USBDEVFS_CONNECT, U, 23);
246 ioctl_ior_nr!(USBDEVFS_CLAIM_PORT, U, 24, c_uint);
247 ioctl_ior_nr!(USBDEVFS_RELEASE_PORT, U, 25, c_uint);
248 ioctl_ior_nr!(USBDEVFS_GET_CAPABILITIES, U, 26, u32);
249 ioctl_ior_nr!(USBDEVFS_DISCONNECT_CLAIM, U, 27, usbdevfs_disconnect_claim);
250 ioctl_ior_nr!(USBDEVFS_ALLOC_STREAMS, U, 28, usbdevfs_streams);
251 ioctl_ior_nr!(USBDEVFS_FREE_STREAMS, U, 29, usbdevfs_streams);
252 ioctl_iow_nr!(USBDEVFS_DROP_PRIVILEGES, U, 30, u32);
253 ioctl_io_nr!(USBDEVFS_GET_SPEED, U, 31);
254