| 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::{ |
| 8 |
ffi::{c_int, c_uchar, c_uint, c_void}, |
| 9 |
marker::PhantomData, |
| 10 |
}; |
| 11 |
|
| 12 |
use rustix::{ |
| 13 |
fd::AsFd, |
| 14 |
io, |
| 15 |
ioctl::{self, CompileTimeOpcode, Ioctl, IoctlOutput}, |
| 16 |
}; |
| 17 |
|
| 18 |
pub fn set_configuration<Fd: AsFd>(fd: Fd, configuration: u8) -> io::Result<()> { |
| 19 |
unsafe { |
| 20 |
let ctl = |
| 21 |
ioctl::Setter::<ioctl::ReadOpcode<b'U', 5, c_uint>, c_uint>::new(configuration.into()); |
| 22 |
ioctl::ioctl(fd, ctl) |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
pub fn claim_interface<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> { |
| 27 |
unsafe { |
| 28 |
let ctl = |
| 29 |
ioctl::Setter::<ioctl::ReadOpcode<b'U', 15, c_uint>, c_uint>::new(interface.into()); |
| 30 |
ioctl::ioctl(fd, ctl) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
pub fn release_interface<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> { |
| 35 |
unsafe { |
| 36 |
let ctl = |
| 37 |
ioctl::Setter::<ioctl::ReadOpcode<b'U', 16, c_uint>, c_uint>::new(interface.into()); |
| 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 super::*; |
| 82 |
|
| 83 |
pub type USBDEVFS_IOCTL = ioctl::ReadWriteOpcode<b'U', 18, UsbFsIoctl>; |
| 84 |
pub type USBDEVFS_DISCONNECT_CLAIM = ioctl::ReadOpcode<b'U', 27, DetachAndClaim>; |
| 85 |
|
| 86 |
/// These opcodes are nested inside a [`USBDEVFS_IOCTL`] operation. |
| 87 |
pub mod nested { |
| 88 |
use super::*; |
| 89 |
|
| 90 |
pub type USBDEVFS_DISCONNECT = ioctl::NoneOpcode<b'U', 22, ()>; |
| 91 |
pub type USBDEVFS_CONNECT = ioctl::NoneOpcode<b'U', 23, ()>; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
pub fn detach_kernel_driver<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> { |
| 96 |
let command = UsbFsIoctl { |
| 97 |
interface: interface.into(), |
| 98 |
// NOTE: Cast needed since on android this type is i32 vs u32 on linux |
| 99 |
ioctl_code: opcodes::nested::USBDEVFS_DISCONNECT::OPCODE.raw() as _, |
| 100 |
data: std::ptr::null_mut(), |
| 101 |
}; |
| 102 |
unsafe { |
| 103 |
let ctl = ioctl::Setter::<opcodes::USBDEVFS_IOCTL, UsbFsIoctl>::new(command); |
| 104 |
ioctl::ioctl(fd, ctl) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
pub fn attach_kernel_driver<Fd: AsFd>(fd: Fd, interface: u8) -> io::Result<()> { |
| 109 |
let command = UsbFsIoctl { |
| 110 |
interface: interface.into(), |
| 111 |
ioctl_code: opcodes::nested::USBDEVFS_CONNECT::OPCODE.raw() as _, |
| 112 |
data: std::ptr::null_mut(), |
| 113 |
}; |
| 114 |
unsafe { |
| 115 |
let ctl = ioctl::Setter::<opcodes::USBDEVFS_IOCTL, UsbFsIoctl>::new(command); |
| 116 |
ioctl::ioctl(fd, ctl) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[repr(C)] |
| 121 |
struct SetAltSetting { |
| 122 |
interface: c_int, |
| 123 |
alt_setting: c_int, |
| 124 |
} |
| 125 |
|
| 126 |
pub fn set_interface<Fd: AsFd>(fd: Fd, interface: u8, alt_setting: u8) -> io::Result<()> { |
| 127 |
unsafe { |
| 128 |
let ctl = ioctl::Setter::<ioctl::ReadOpcode<b'U', 4, SetAltSetting>, SetAltSetting>::new( |
| 129 |
SetAltSetting { |
| 130 |
interface: interface.into(), |
| 131 |
alt_setting: alt_setting.into(), |
| 132 |
}, |
| 133 |
); |
| 134 |
ioctl::ioctl(fd, ctl) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
pub struct PassPtr<Opcode, Input> { |
| 139 |
input: *mut Input, |
| 140 |
_opcode: PhantomData<Opcode>, |
| 141 |
} |
| 142 |
|
| 143 |
impl<Opcode: CompileTimeOpcode, Input> PassPtr<Opcode, Input> { |
| 144 |
/// Create a new pointer setter-style `ioctl` object. |
| 145 |
/// |
| 146 |
/// # Safety |
| 147 |
/// |
| 148 |
/// - `Opcode` must provide a valid opcode. |
| 149 |
/// - For this opcode, `Input` must be the type that the kernel expects to |
| 150 |
/// get. |
| 151 |
#[inline] |
| 152 |
pub unsafe fn new(input: *mut Input) -> Self { |
| 153 |
Self { |
| 154 |
input, |
| 155 |
_opcode: PhantomData, |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
unsafe impl<Opcode: CompileTimeOpcode, Input> Ioctl for PassPtr<Opcode, Input> { |
| 161 |
type Output = (); |
| 162 |
|
| 163 |
const IS_MUTATING: bool = false; |
| 164 |
const OPCODE: rustix::ioctl::Opcode = Opcode::OPCODE; |
| 165 |
|
| 166 |
fn as_ptr(&mut self) -> *mut c_void { |
| 167 |
self.input as *mut c_void |
| 168 |
} |
| 169 |
|
| 170 |
unsafe fn output_from_ptr(_: IoctlOutput, _: *mut c_void) -> rustix::io::Result<Self::Output> { |
| 171 |
Ok(()) |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
pub unsafe fn submit_urb<Fd: AsFd>(fd: Fd, urb: *mut Urb) -> io::Result<()> { |
| 176 |
unsafe { |
| 177 |
let ctl = PassPtr::<ioctl::ReadOpcode<b'U', 10, Urb>, Urb>::new(urb); |
| 178 |
ioctl::ioctl(fd, ctl) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
pub fn reap_urb_ndelay<Fd: AsFd>(fd: Fd) -> io::Result<*mut Urb> { |
| 183 |
unsafe { |
| 184 |
let ctl = ioctl::Getter::<ioctl::WriteOpcode<b'U', 13, *mut Urb>, *mut Urb>::new(); |
| 185 |
ioctl::ioctl(fd, ctl) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
pub unsafe fn discard_urb<Fd: AsFd>(fd: Fd, urb: *mut Urb) -> io::Result<()> { |
| 190 |
unsafe { |
| 191 |
let ctl = PassPtr::<ioctl::NoneOpcode<b'U', 11, ()>, Urb>::new(urb); |
| 192 |
ioctl::ioctl(fd, ctl) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
pub fn reset<Fd: AsFd>(fd: Fd) -> io::Result<()> { |
| 197 |
unsafe { |
| 198 |
let ctl = ioctl::NoArg::<ioctl::NoneOpcode<b'U', 20, ()>>::new(); |
| 199 |
ioctl::ioctl(fd, ctl) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
const USBDEVFS_URB_SHORT_NOT_OK: c_uint = 0x01; |
| 204 |
const USBDEVFS_URB_ISO_ASAP: c_uint = 0x02; |
| 205 |
const USBDEVFS_URB_BULK_CONTINUATION: c_uint = 0x04; |
| 206 |
const USBDEVFS_URB_ZERO_PACKET: c_uint = 0x40; |
| 207 |
const USBDEVFS_URB_NO_INTERRUPT: c_uint = 0x80; |
| 208 |
|
| 209 |
pub const USBDEVFS_URB_TYPE_ISO: c_uchar = 0; |
| 210 |
pub const USBDEVFS_URB_TYPE_INTERRUPT: c_uchar = 1; |
| 211 |
pub const USBDEVFS_URB_TYPE_CONTROL: c_uchar = 2; |
| 212 |
pub const USBDEVFS_URB_TYPE_BULK: c_uchar = 3; |
| 213 |
|
| 214 |
#[repr(C)] |
| 215 |
#[derive(Debug)] |
| 216 |
pub struct Urb { |
| 217 |
pub ep_type: c_uchar, |
| 218 |
pub endpoint: c_uchar, |
| 219 |
pub status: c_int, |
| 220 |
pub flags: c_uint, |
| 221 |
pub buffer: *mut u8, |
| 222 |
pub buffer_length: c_int, |
| 223 |
pub actual_length: c_int, |
| 224 |
pub start_frame: c_int, |
| 225 |
pub number_of_packets_or_stream_id: c_uint, // a union in C |
| 226 |
pub error_count: c_int, |
| 227 |
pub signr: c_uint, |
| 228 |
pub usercontext: *mut c_void, |
| 229 |
// + variable size array of iso_packet_desc |
| 230 |
} |
| 231 |
|
| 232 |
pub struct Transfer<Opcode, Input> { |
| 233 |
input: Input, |
| 234 |
_opcode: PhantomData<Opcode>, |
| 235 |
} |
| 236 |
|
| 237 |
impl<Opcode: CompileTimeOpcode, Input> Transfer<Opcode, Input> { |
| 238 |
#[inline] |
| 239 |
pub unsafe fn new(input: Input) -> Self { |
| 240 |
Self { |
| 241 |
input, |
| 242 |
_opcode: PhantomData, |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
unsafe impl<Opcode: CompileTimeOpcode, Input> Ioctl for Transfer<Opcode, Input> { |
| 248 |
type Output = usize; |
| 249 |
|
| 250 |
const IS_MUTATING: bool = true; |
| 251 |
const OPCODE: rustix::ioctl::Opcode = Opcode::OPCODE; |
| 252 |
|
| 253 |
fn as_ptr(&mut self) -> *mut c_void { |
| 254 |
&mut self.input as *mut Input as *mut c_void |
| 255 |
} |
| 256 |
|
| 257 |
unsafe fn output_from_ptr(r: IoctlOutput, _: *mut c_void) -> io::Result<usize> { |
| 258 |
Ok(r as usize) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
#[repr(C)] |
| 263 |
#[allow(non_snake_case)] |
| 264 |
pub struct CtrlTransfer { |
| 265 |
pub bRequestType: u8, |
| 266 |
pub bRequest: u8, |
| 267 |
pub wValue: u16, |
| 268 |
pub wIndex: u16, |
| 269 |
pub wLength: u16, |
| 270 |
pub timeout: u32, /* in milliseconds */ |
| 271 |
pub data: *mut c_void, |
| 272 |
} |
| 273 |
|
| 274 |
pub fn control<Fd: AsFd>(fd: Fd, transfer: CtrlTransfer) -> io::Result<usize> { |
| 275 |
unsafe { |
| 276 |
let ctl = |
| 277 |
Transfer::<ioctl::ReadWriteOpcode<b'U', 0, CtrlTransfer>, CtrlTransfer>::new(transfer); |
| 278 |
ioctl::ioctl(fd, ctl) |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
pub fn clear_halt<Fd: AsFd>(fd: Fd, endpoint: u8) -> io::Result<()> { |
| 283 |
unsafe { |
| 284 |
let ctl = |
| 285 |
ioctl::Setter::<ioctl::ReadOpcode<b'U', 21, c_uint>, c_uint>::new(endpoint.into()); |
| 286 |
ioctl::ioctl(fd, ctl) |
| 287 |
} |
| 288 |
} |