| 1 | |
| 2 | |
| 3 |
|
| 4 | |
| 5 | |
| 6 | |
| 7 |
|
| 8 | use crate::{
|
| 9 | bindings, device,
|
| 10 | device_id::{RawDeviceId, RawDeviceIdIndex},
|
| 11 | driver,
|
| 12 | error::{from_result, to_result, Result},
|
| 13 | prelude::*,
|
| 14 | str::CStr,
|
| 15 | types::{AlwaysRefCounted, Opaque},
|
| 16 | ThisModule,
|
| 17 | };
|
| 18 | use core::{marker::PhantomData, mem::MaybeUninit, ptr::NonNull};
|
| 19 |
|
| 20 | |
| 21 | pub struct Adapter<T: Driver>(T);
|
| 22 |
|
| 23 | |
| 24 | |
| 25 | unsafe impl<T: Driver + 'static> driver::RegistrationOps for Adapter<T> {
|
| 26 | type RegType = bindings::usb_driver;
|
| 27 |
|
| 28 | unsafe fn register(
|
| 29 | udrv: &Opaque<Self::RegType>,
|
| 30 | name: &'static CStr,
|
| 31 | module: &'static ThisModule,
|
| 32 | ) -> Result {
|
| 33 | |
| 34 | unsafe {
|
| 35 | (*udrv.get()).name = name.as_char_ptr();
|
| 36 | (*udrv.get()).probe = Some(Self::probe_callback);
|
| 37 | (*udrv.get()).disconnect = Some(Self::disconnect_callback);
|
| 38 | (*udrv.get()).id_table = T::ID_TABLE.as_ptr();
|
| 39 | }
|
| 40 |
|
| 41 | |
| 42 | to_result(unsafe {
|
| 43 | bindings::usb_register_driver(udrv.get(), module.0, name.as_char_ptr())
|
| 44 | })
|
| 45 | }
|
| 46 |
|
| 47 | unsafe fn unregister(udrv: &Opaque<Self::RegType>) {
|
| 48 | |
| 49 | unsafe { bindings::usb_deregister(udrv.get()) };
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | impl<T: Driver + 'static> Adapter<T> {
|
| 54 | extern "C" fn probe_callback(
|
| 55 | intf: *mut bindings::usb_interface,
|
| 56 | id: *const bindings::usb_device_id,
|
| 57 | ) -> kernel::ffi::c_int {
|
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal>>() };
|
| 63 |
|
| 64 | from_result(|| {
|
| 65 | |
| 66 | |
| 67 | let id = unsafe { &*id.cast::<DeviceId>() };
|
| 68 |
|
| 69 | let info = T::ID_TABLE.info(id.index());
|
| 70 | let data = T::probe(intf, id, info)?;
|
| 71 |
|
| 72 | let dev: &device::Device<device::CoreInternal> = intf.as_ref();
|
| 73 | dev.set_drvdata(data);
|
| 74 | Ok(0)
|
| 75 | })
|
| 76 | }
|
| 77 |
|
| 78 | extern "C" fn disconnect_callback(intf: *mut bindings::usb_interface) {
|
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal>>() };
|
| 84 |
|
| 85 | let dev: &device::Device<device::CoreInternal> = intf.as_ref();
|
| 86 |
|
| 87 | |
| 88 | |
| 89 | |
| 90 | let data = unsafe { dev.drvdata_obtain::<Pin<KBox<T>>>() };
|
| 91 |
|
| 92 | T::disconnect(intf, data.as_ref());
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | |
| 97 | |
| 98 | |
| 99 | #[repr(transparent)]
|
| 100 | #[derive(Clone, Copy)]
|
| 101 | pub struct DeviceId(bindings::usb_device_id);
|
| 102 |
|
| 103 | impl DeviceId {
|
| 104 | |
| 105 | pub const fn from_id(vendor: u16, product: u16) -> Self {
|
| 106 | Self(bindings::usb_device_id {
|
| 107 | match_flags: bindings::USB_DEVICE_ID_MATCH_DEVICE as u16,
|
| 108 | idVendor: vendor,
|
| 109 | idProduct: product,
|
| 110 | |
| 111 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 112 | })
|
| 113 | }
|
| 114 |
|
| 115 | |
| 116 | pub const fn from_device_ver(vendor: u16, product: u16, bcd_lo: u16, bcd_hi: u16) -> Self {
|
| 117 | Self(bindings::usb_device_id {
|
| 118 | match_flags: bindings::USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION as u16,
|
| 119 | idVendor: vendor,
|
| 120 | idProduct: product,
|
| 121 | bcdDevice_lo: bcd_lo,
|
| 122 | bcdDevice_hi: bcd_hi,
|
| 123 | |
| 124 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 125 | })
|
| 126 | }
|
| 127 |
|
| 128 | |
| 129 | pub const fn from_device_info(class: u8, subclass: u8, protocol: u8) -> Self {
|
| 130 | Self(bindings::usb_device_id {
|
| 131 | match_flags: bindings::USB_DEVICE_ID_MATCH_DEV_INFO as u16,
|
| 132 | bDeviceClass: class,
|
| 133 | bDeviceSubClass: subclass,
|
| 134 | bDeviceProtocol: protocol,
|
| 135 | |
| 136 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 137 | })
|
| 138 | }
|
| 139 |
|
| 140 | |
| 141 | pub const fn from_interface_info(class: u8, subclass: u8, protocol: u8) -> Self {
|
| 142 | Self(bindings::usb_device_id {
|
| 143 | match_flags: bindings::USB_DEVICE_ID_MATCH_INT_INFO as u16,
|
| 144 | bInterfaceClass: class,
|
| 145 | bInterfaceSubClass: subclass,
|
| 146 | bInterfaceProtocol: protocol,
|
| 147 | |
| 148 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 149 | })
|
| 150 | }
|
| 151 |
|
| 152 | |
| 153 | pub const fn from_device_interface_class(vendor: u16, product: u16, class: u8) -> Self {
|
| 154 | Self(bindings::usb_device_id {
|
| 155 | match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
|
| 156 | | bindings::USB_DEVICE_ID_MATCH_INT_CLASS) as u16,
|
| 157 | idVendor: vendor,
|
| 158 | idProduct: product,
|
| 159 | bInterfaceClass: class,
|
| 160 | |
| 161 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 162 | })
|
| 163 | }
|
| 164 |
|
| 165 | |
| 166 | pub const fn from_device_interface_protocol(vendor: u16, product: u16, protocol: u8) -> Self {
|
| 167 | Self(bindings::usb_device_id {
|
| 168 | match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
|
| 169 | | bindings::USB_DEVICE_ID_MATCH_INT_PROTOCOL) as u16,
|
| 170 | idVendor: vendor,
|
| 171 | idProduct: product,
|
| 172 | bInterfaceProtocol: protocol,
|
| 173 | |
| 174 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 175 | })
|
| 176 | }
|
| 177 |
|
| 178 | |
| 179 | pub const fn from_device_interface_number(vendor: u16, product: u16, number: u8) -> Self {
|
| 180 | Self(bindings::usb_device_id {
|
| 181 | match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
|
| 182 | | bindings::USB_DEVICE_ID_MATCH_INT_NUMBER) as u16,
|
| 183 | idVendor: vendor,
|
| 184 | idProduct: product,
|
| 185 | bInterfaceNumber: number,
|
| 186 | |
| 187 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 188 | })
|
| 189 | }
|
| 190 |
|
| 191 | |
| 192 | pub const fn from_device_and_interface_info(
|
| 193 | vendor: u16,
|
| 194 | product: u16,
|
| 195 | class: u8,
|
| 196 | subclass: u8,
|
| 197 | protocol: u8,
|
| 198 | ) -> Self {
|
| 199 | Self(bindings::usb_device_id {
|
| 200 | match_flags: (bindings::USB_DEVICE_ID_MATCH_INT_INFO
|
| 201 | | bindings::USB_DEVICE_ID_MATCH_DEVICE) as u16,
|
| 202 | idVendor: vendor,
|
| 203 | idProduct: product,
|
| 204 | bInterfaceClass: class,
|
| 205 | bInterfaceSubClass: subclass,
|
| 206 | bInterfaceProtocol: protocol,
|
| 207 | |
| 208 | ..unsafe { MaybeUninit::zeroed().assume_init() }
|
| 209 | })
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | |
| 214 | |
| 215 | unsafe impl RawDeviceId for DeviceId {
|
| 216 | type RawType = bindings::usb_device_id;
|
| 217 | }
|
| 218 |
|
| 219 | |
| 220 | unsafe impl RawDeviceIdIndex for DeviceId {
|
| 221 | const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::usb_device_id, driver_info);
|
| 222 |
|
| 223 | fn index(&self) -> usize {
|
| 224 | self.0.driver_info
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | |
| 229 | pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
|
| 230 |
|
| 231 | |
| 232 | #[macro_export]
|
| 233 | macro_rules! usb_device_table {
|
| 234 | ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => {
|
| 235 | const $table_name: $crate::device_id::IdArray<
|
| 236 | $crate::usb::DeviceId,
|
| 237 | $id_info_type,
|
| 238 | { $table_data.len() },
|
| 239 | > = $crate::device_id::IdArray::new($table_data);
|
| 240 |
|
| 241 | $crate::module_device_table!("usb", $module_table_name, $table_name);
|
| 242 | };
|
| 243 | }
|
| 244 |
|
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | pub trait Driver {
|
| 281 | |
| 282 | type IdInfo: 'static;
|
| 283 |
|
| 284 | |
| 285 | const ID_TABLE: IdTable<Self::IdInfo>;
|
| 286 |
|
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | fn probe(
|
| 292 | interface: &Interface<device::Core>,
|
| 293 | id: &DeviceId,
|
| 294 | id_info: &Self::IdInfo,
|
| 295 | ) -> Result<Pin<KBox<Self>>>;
|
| 296 |
|
| 297 | |
| 298 | |
| 299 | |
| 300 | fn disconnect(interface: &Interface<device::Core>, data: Pin<&Self>);
|
| 301 | }
|
| 302 |
|
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | #[repr(transparent)]
|
| 316 | pub struct Interface<Ctx: device::DeviceContext = device::Normal>(
|
| 317 | Opaque<bindings::usb_interface>,
|
| 318 | PhantomData<Ctx>,
|
| 319 | );
|
| 320 |
|
| 321 | impl<Ctx: device::DeviceContext> Interface<Ctx> {
|
| 322 | fn as_raw(&self) -> *mut bindings::usb_interface {
|
| 323 | self.0.get()
|
| 324 | }
|
| 325 | }
|
| 326 |
|
| 327 | |
| 328 | |
| 329 | kernel::impl_device_context_deref!(unsafe { Interface });
|
| 330 | kernel::impl_device_context_into_aref!(Interface);
|
| 331 |
|
| 332 | impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Interface<Ctx> {
|
| 333 | fn as_ref(&self) -> &device::Device<Ctx> {
|
| 334 | |
| 335 | |
| 336 | let dev = unsafe { &raw mut ((*self.as_raw()).dev) };
|
| 337 |
|
| 338 | |
| 339 | unsafe { device::Device::from_raw(dev) }
|
| 340 | }
|
| 341 | }
|
| 342 |
|
| 343 | impl<Ctx: device::DeviceContext> AsRef<Device> for Interface<Ctx> {
|
| 344 | fn as_ref(&self) -> &Device {
|
| 345 | |
| 346 | let usb_dev = unsafe { bindings::interface_to_usbdev(self.as_raw()) };
|
| 347 |
|
| 348 | |
| 349 | |
| 350 | unsafe { &*(usb_dev.cast()) }
|
| 351 | }
|
| 352 | }
|
| 353 |
|
| 354 | |
| 355 | unsafe impl AlwaysRefCounted for Interface {
|
| 356 | fn inc_ref(&self) {
|
| 357 | |
| 358 | |
| 359 | |
| 360 | unsafe { bindings::usb_get_intf(self.as_raw()) };
|
| 361 | }
|
| 362 |
|
| 363 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 364 | |
| 365 | unsafe { bindings::usb_put_intf(obj.cast().as_ptr()) }
|
| 366 | }
|
| 367 | }
|
| 368 |
|
| 369 | |
| 370 | unsafe impl Send for Interface {}
|
| 371 |
|
| 372 | |
| 373 | |
| 374 | unsafe impl Sync for Interface {}
|
| 375 |
|
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | #[repr(transparent)]
|
| 389 | struct Device<Ctx: device::DeviceContext = device::Normal>(
|
| 390 | Opaque<bindings::usb_device>,
|
| 391 | PhantomData<Ctx>,
|
| 392 | );
|
| 393 |
|
| 394 | impl<Ctx: device::DeviceContext> Device<Ctx> {
|
| 395 | fn as_raw(&self) -> *mut bindings::usb_device {
|
| 396 | self.0.get()
|
| 397 | }
|
| 398 | }
|
| 399 |
|
| 400 | |
| 401 | |
| 402 | kernel::impl_device_context_deref!(unsafe { Device });
|
| 403 | kernel::impl_device_context_into_aref!(Device);
|
| 404 |
|
| 405 | |
| 406 | unsafe impl AlwaysRefCounted for Device {
|
| 407 | fn inc_ref(&self) {
|
| 408 | |
| 409 | |
| 410 | |
| 411 | unsafe { bindings::usb_get_dev(self.as_raw()) };
|
| 412 | }
|
| 413 |
|
| 414 | unsafe fn dec_ref(obj: NonNull<Self>) {
|
| 415 | |
| 416 | unsafe { bindings::usb_put_dev(obj.cast().as_ptr()) }
|
| 417 | }
|
| 418 | }
|
| 419 |
|
| 420 | impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
|
| 421 | fn as_ref(&self) -> &device::Device<Ctx> {
|
| 422 | |
| 423 | |
| 424 | let dev = unsafe { &raw mut ((*self.as_raw()).dev) };
|
| 425 |
|
| 426 | |
| 427 | unsafe { device::Device::from_raw(dev) }
|
| 428 | }
|
| 429 | }
|
| 430 |
|
| 431 | |
| 432 | unsafe impl Send for Device {}
|
| 433 |
|
| 434 | |
| 435 | |
| 436 | unsafe impl Sync for Device {}
|
| 437 |
|
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | #[macro_export]
|
| 452 | macro_rules! module_usb_driver {
|
| 453 | ($($f:tt)*) => {
|
| 454 | $crate::module_driver!(<T>, $crate::usb::Adapter<T>, { $($f)* });
|
| 455 | }
|
| 456 | }
|