Debian Codegraph @

src/platform/linux_usbfs/usbfs.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 //! Wrappers for the [usbfs] character device ioctls, translated from the
2 //! [C structures and ioctl definitions][uapi].
3 //!
4 //! [usbfs]: https://www.kernel.org/doc/html/latest/driver-api/usb/usb.html#the-usb-character-device-nodes
5 //! [uapi]: https://github.com/torvalds/linux/blob/master/tools/include/uapi/linux/usbdevice_fs.h
6 #![allow(dead_code)]
•7 use std::ffi::{c_int, c_uchar, c_uint, c_void};
8
9 use rustix::{
10 fd::AsFd,
11 io,
•12 ioctl::{self, Ioctl, IoctlOutput, Opcode},
13 };
14
15 pub fn set_configuration<Fd: AsFd>(fd: Fd, configuration: u8) -> io::Result<()> {
16 unsafe {
•17 let ctl = ioctl::Setter::<{ ioctl::opcode::read::<c_uint>(b'U', 5) }, c_uint>::new(
18 configuration.into(),
19 );
20 ioctl::ioctl(fd, ctl)
21 }
22 }
23
24 pub fn claim_interface<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> {
25 unsafe {
•26 let ctl = ioctl::Setter::<{ ioctl::opcode::read::<c_uint>(b'U', 15) }, c_uint>::new(
27 interface.into(),
28 );
29 ioctl::ioctl(fd, ctl)
30 }
31 }
32
33 pub fn release_interface<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> {
34 unsafe {
•35 let ctl = ioctl::Setter::<{ ioctl::opcode::read::<c_uint>(b'U', 16) }, c_uint>::new(
36 interface.into(),
37 );
38 ioctl::ioctl(fd, ctl)
39 }
40 }
41
42 #[repr(C)]
43 struct DetachAndClaim {
44 interface: c_uint,
45 flags: c_uint,
46 driver: [c_uchar; 255 + 1],
47 }
48
49 pub fn detach_and_claim_interface<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> {
50 const USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER: c_uint = 0x02;
51 unsafe {
52 let mut dc = DetachAndClaim {
53 interface: interface.into(),
54 flags: USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER,
55 driver: [0; 256],
56 };
57
58 dc.driver[0..6].copy_from_slice(b"usbfs\0");
59
•60 let ctl = ioctl::Setter::<{ opcodes::USBDEVFS_DISCONNECT_CLAIM }, DetachAndClaim>::new(dc);
61
62 ioctl::ioctl(&fd, ctl)
63 }
64 }
65
66 #[repr(C)]
67 struct UsbFsIoctl {
68 interface: c_uint,
69 ioctl_code: c_uint,
70 data: *mut c_void,
71 }
72
73 /// Opcodes used in ioctl with the usb device fs.
74 ///
75 /// Taken from https://github.com/torvalds/linux/blob/e9680017b2dc8686a908ea1b51941a91b6da9f1d/include/uapi/linux/usbdevice_fs.h#L187
76 // We repeat the USBDEVFS_ prefix to help keep the same names as what linux uses.
77 // This makes the code more searchable.
78 // TODO: Move the rest of the opcodes into here.
79 #[allow(non_camel_case_types)]
80 mod opcodes {
•81 use rustix::ioctl::Opcode;
82
83 use super::*;
84
•85 pub const USBDEVFS_IOCTL: Opcode = ioctl::opcode::read_write::<UsbFsIoctl>(b'U', 18);
86 pub const USBDEVFS_DISCONNECT_CLAIM: Opcode = ioctl::opcode::read::<DetachAndClaim>(b'U', 27);
87
88 /// These opcodes are nested inside a [`USBDEVFS_IOCTL`] operation.
89 pub mod nested {
90 use super::*;
91
•92 pub const USBDEVFS_DISCONNECT: Opcode = ioctl::opcode::none(b'U', 22);
93 pub const USBDEVFS_CONNECT: Opcode = ioctl::opcode::none(b'U', 23);
94 }
95 }
96
97 pub fn detach_kernel_driver<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> {
98 let command = UsbFsIoctl {
99 interface: interface.into(),
100 // NOTE: Cast needed since on android this type is i32 vs u32 on linux
•101 ioctl_code: opcodes::nested::USBDEVFS_DISCONNECT as _,
102 data: std::ptr::null_mut(),
103 };
104 unsafe {
•105 let ctl = ioctl::Setter::<{ opcodes::USBDEVFS_IOCTL }, UsbFsIoctl>::new(command);
106 ioctl::ioctl(fd, ctl)
107 }
108 }
109
110 pub fn attach_kernel_driver<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> {
111 let command = UsbFsIoctl {
112 interface: interface.into(),
•113 ioctl_code: opcodes::nested::USBDEVFS_CONNECT as _,
114 data: std::ptr::null_mut(),
115 };
116 unsafe {
•117 let ctl = ioctl::Setter::<{ opcodes::USBDEVFS_IOCTL }, UsbFsIoctl>::new(command);
118 ioctl::ioctl(fd, ctl)
119 }
120 }
121
122 #[repr(C)]
123 struct SetAltSetting {
124 interface: c_int,
125 alt_setting: c_int,
126 }
127
128 pub fn set_interface<Fd: AsFd>(fd: Fd, interface: u8, alt_setting: u8) -> io::Result<()> {
129 unsafe {
•130 let ctl =
131 ioctl::Setter::<{ ioctl::opcode::read::<SetAltSetting>(b'U', 4) }, SetAltSetting>::new(
132 SetAltSetting {
133 interface: interface.into(),
134 alt_setting: alt_setting.into(),
135 },
136 );
137 ioctl::ioctl(fd, ctl)
138 }
139 }
140
•141 pub struct PassPtr<const OPCODE: Opcode, Input> {
142 input: *mut Input,
143 }
144
•145 impl<const OPCODE: Opcode, Input> PassPtr<OPCODE, Input> {
146 /// Create a new pointer setter-style `ioctl` object.
147 ///
148 /// # Safety
149 ///
150 /// - `Opcode` must provide a valid opcode.
151 /// - For this opcode, `Input` must be the type that the kernel expects to
152 /// get.
153 #[inline]
154 pub unsafe fn new(input: *mut Input) -> Self {
•155 Self { input }
156 }
157 }
158
•159 unsafe impl<const OPCODE: Opcode, Input> Ioctl for PassPtr<OPCODE, Input> {
160 type Output = ();
161
162 const IS_MUTATING: bool = false;
•163
164 fn opcode(&self) -> ioctl::Opcode {
165 OPCODE
166 }
167
168 fn as_ptr(&mut self) -> *mut c_void {
169 self.input as *mut c_void
170 }
171
172 unsafe fn output_from_ptr(_: IoctlOutput, _: *mut c_void) -> rustix::io::Result<Self::Output> {
173 Ok(())
174 }
175 }
176
177 pub unsafe fn submit_urb<Fd: AsFd>(fd: Fd, urb: *mut Urb) -> io::Result<()> {
178 unsafe {
•179 let ctl = PassPtr::<{ ioctl::opcode::read::<Urb>(b'U', 10) }, Urb>::new(urb);
180 ioctl::ioctl(fd, ctl)
181 }
182 }
183
184 pub fn reap_urb_ndelay<Fd: AsFd>(fd: Fd) -> io::Result<*mut Urb> {
185 unsafe {
•186 let ctl = ioctl::Getter::<{ ioctl::opcode::write::<*mut Urb>(b'U', 13) }, *mut Urb>::new();
187 ioctl::ioctl(fd, ctl)
188 }
189 }
190
191 pub unsafe fn discard_urb<Fd: AsFd>(fd: Fd, urb: *mut Urb) -> io::Result<()> {
192 unsafe {
•193 let ctl = PassPtr::<{ ioctl::opcode::none(b'U', 11) }, Urb>::new(urb);
194 ioctl::ioctl(fd, ctl)
195 }
196 }
197
198 pub fn reset<Fd: AsFd>(fd: Fd) -> io::Result<()> {
199 unsafe {
•200 let ctl = ioctl::NoArg::<{ ioctl::opcode::none(b'U', 20) }>::new();
201 ioctl::ioctl(fd, ctl)
202 }
203 }
204
205 const USBDEVFS_URB_SHORT_NOT_OK: c_uint = 0x01;
206 const USBDEVFS_URB_ISO_ASAP: c_uint = 0x02;
207 const USBDEVFS_URB_BULK_CONTINUATION: c_uint = 0x04;
208 const USBDEVFS_URB_ZERO_PACKET: c_uint = 0x40;
209 const USBDEVFS_URB_NO_INTERRUPT: c_uint = 0x80;
210
211 pub const USBDEVFS_URB_TYPE_ISO: c_uchar = 0;
212 pub const USBDEVFS_URB_TYPE_INTERRUPT: c_uchar = 1;
213 pub const USBDEVFS_URB_TYPE_CONTROL: c_uchar = 2;
214 pub const USBDEVFS_URB_TYPE_BULK: c_uchar = 3;
215
216 #[repr(C)]
217 #[derive(Debug)]
218 pub struct Urb {
219 pub ep_type: c_uchar,
220 pub endpoint: c_uchar,
221 pub status: c_int,
222 pub flags: c_uint,
223 pub buffer: *mut u8,
224 pub buffer_length: c_int,
225 pub actual_length: c_int,
226 pub start_frame: c_int,
227 pub number_of_packets_or_stream_id: c_uint, // a union in C
228 pub error_count: c_int,
229 pub signr: c_uint,
230 pub usercontext: *mut c_void,
231 // + variable size array of iso_packet_desc
232 }
233
•234 pub struct Transfer<const OPCODE: Opcode, Input> {
235 input: Input,
236 }
237
•238 impl<const OPCODE: Opcode, Input> Transfer<OPCODE, Input> {
239 #[inline]
240 pub unsafe fn new(input: Input) -> Self {
•241 Self { input }
242 }
243 }
244
•245 unsafe impl<const OPCODE: Opcode, Input> Ioctl for Transfer<OPCODE, Input> {
246 type Output = usize;
247
248 const IS_MUTATING: bool = true;
•249
250 fn opcode(&self) -> ioctl::Opcode {
251 OPCODE
252 }
253
254 fn as_ptr(&mut self) -> *mut c_void {
255 &mut self.input as *mut Input as *mut c_void
256 }
257
258 unsafe fn output_from_ptr(r: IoctlOutput, _: *mut c_void) -> io::Result<usize> {
259 Ok(r as usize)
260 }
261 }
262
263 #[repr(C)]
264 #[allow(non_snake_case)]
265 pub struct CtrlTransfer {
266 pub bRequestType: u8,
267 pub bRequest: u8,
268 pub wValue: u16,
269 pub wIndex: u16,
270 pub wLength: u16,
271 pub timeout: u32, /* in milliseconds */
272 pub data: *mut c_void,
273 }
274
275 pub fn control<Fd: AsFd>(fd: Fd, transfer: CtrlTransfer) -> io::Result<usize> {
276 unsafe {
277 let ctl =
•278 Transfer::<{ ioctl::opcode::read_write::<CtrlTransfer>(b'U', 0) }, CtrlTransfer>::new(transfer);
279 ioctl::ioctl(fd, ctl)
280 }
281 }
282
283 pub fn clear_halt<Fd: AsFd>(fd: Fd, endpoint: u8) -> io::Result<()> {
284 unsafe {
285 let ctl =
•286 ioctl::Setter::<{ ioctl::opcode::read::<c_uint>(b'U', 21) }, c_uint>::new(endpoint.into());
287 ioctl::ioctl(fd, ctl)
288 }
289 }