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