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