1 // SPDX-License-Identifier: GPL-2.0 2 3 //! Generic devices that are part of the kernel's driver model. 4 //! 5 //! C header: [`include/linux/device.h`](srctree/include/linux/device.h) 6 7 use crate::{ 8 bindings, 9 sync::aref::ARef, 10 types::{ForeignOwnable, Opaque}, 11 }; 12 use core::{fmt, marker::PhantomData, ptr}; 13 14 #[cfg(CONFIG_PRINTK)] 15 use crate::c_str; 16 17 pub mod property; 18 19 /// A reference-counted device. 20 /// 21 /// This structure represents the Rust abstraction for a C `struct device`. This implementation 22 /// abstracts the usage of an already existing C `struct device` within Rust code that we get 23 /// passed from the C side. 24 /// 25 /// An instance of this abstraction can be obtained temporarily or permanent. 26 /// 27 /// A temporary one is bound to the lifetime of the C `struct device` pointer used for creation. 28 /// A permanent instance is always reference-counted and hence not restricted by any lifetime 29 /// boundaries. 30 /// 31 /// For subsystems it is recommended to create a permanent instance to wrap into a subsystem 32 /// specific device structure (e.g. `pci::Device`). This is useful for passing it to drivers in 33 /// `T::probe()`, such that a driver can store the `ARef<Device>` (equivalent to storing a 34 /// `struct device` pointer in a C driver) for arbitrary purposes, e.g. allocating DMA coherent 35 /// memory. 36 /// 37 /// # Invariants 38 /// 39 /// A `Device` instance represents a valid `struct device` created by the C portion of the kernel. 40 /// 41 /// Instances of this type are always reference-counted, that is, a call to `get_device` ensures 42 /// that the allocation remains valid at least until the matching call to `put_device`. 43 /// 44 /// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be 45 /// dropped from any thread. 46 #[repr(transparent)] 47 pub struct Device<Ctx: DeviceContext = Normal>(Opaque<bindings::device>, PhantomData<Ctx>); 48 49 impl Device { 50 /// Creates a new reference-counted abstraction instance of an existing `struct device` pointer. 51 /// 52 /// # Safety 53 /// 54 /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count, 55 /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to 56 /// can't drop to zero, for the duration of this function call. 57 /// 58 /// It must also be ensured that `bindings::device::release` can be called from any thread. 59 /// While not officially documented, this should be the case for any `struct device`. 60 pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> { 61 // SAFETY: By the safety requirements ptr is valid 62 unsafe { Self::from_raw(ptr) }.into() 63 } 64 65 /// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>). 66 /// 67 /// # Safety 68 /// 69 /// The caller is responsible to ensure that the returned [`&Device<Bound>`](Device<Bound>) 70 /// only lives as long as it can be guaranteed that the [`Device`] is actually bound. 71 pub unsafe fn as_bound(&self) -> &Device<Bound> { 72 let ptr = core::ptr::from_ref(self); 73 74 // CAST: By the safety requirements the caller is responsible to guarantee that the 75 // returned reference only lives as long as the device is actually bound. 76 let ptr = ptr.cast(); 77 78 // SAFETY: 79 // - `ptr` comes from `from_ref(self)` above, hence it's guaranteed to be valid. 80 // - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`. 81 unsafe { &*ptr } 82 } 83 } 84 85 impl Device<CoreInternal> { 86 /// Store a pointer to the bound driver's private data. 87 pub fn set_drvdata(&self, data: impl ForeignOwnable) { 88 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. 89 unsafe { bindings::dev_set_drvdata(self.as_raw(), data.into_foreign().cast()) } 90 } 91 92 /// Take ownership of the private data stored in this [`Device`]. 93 /// 94 /// # Safety 95 /// 96 /// - Must only be called once after a preceding call to [`Device::set_drvdata`]. 97 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by 98 /// [`Device::set_drvdata`]. 99 pub unsafe fn drvdata_obtain<T: ForeignOwnable>(&self) -> T { 100 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. 101 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) }; 102 103 // SAFETY: 104 // - By the safety requirements of this function, `ptr` comes from a previous call to 105 // `into_foreign()`. 106 // - `dev_get_drvdata()` guarantees to return the same pointer given to `dev_set_drvdata()` 107 // in `into_foreign()`. 108 unsafe { T::from_foreign(ptr.cast()) } 109 } 110 111 /// Borrow the driver's private data bound to this [`Device`]. 112 /// 113 /// # Safety 114 /// 115 /// - Must only be called after a preceding call to [`Device::set_drvdata`] and before 116 /// [`Device::drvdata_obtain`]. 117 /// - The type `T` must match the type of the `ForeignOwnable` previously stored by 118 /// [`Device::set_drvdata`]. 119 pub unsafe fn drvdata_borrow<T: ForeignOwnable>(&self) -> T::Borrowed<'_> { 120 // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`. 121 let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) }; 122 123 // SAFETY: 124 // - By the safety requirements of this function, `ptr` comes from a previous call to 125 // `into_foreign()`. 126 // - `dev_get_drvdata()` guarantees to return the same pointer given to `dev_set_drvdata()` 127 // in `into_foreign()`. 128 unsafe { T::borrow(ptr.cast()) } 129 } 130 } 131 132 impl<Ctx: DeviceContext> Device<Ctx> { 133 /// Obtain the raw `struct device *`. 134 pub(crate) fn as_raw(&self) -> *mut bindings::device { 135 self.0.get() 136 } 137 138 /// Returns a reference to the parent device, if any. 139 #[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))] 140 pub(crate) fn parent(&self) -> Option<&Self> { 141 // SAFETY: 142 // - By the type invariant `self.as_raw()` is always valid. 143 // - The parent device is only ever set at device creation. 144 let parent = unsafe { (*self.as_raw()).parent }; 145 146 if parent.is_null() { 147 None 148 } else { 149 // SAFETY: 150 // - Since `parent` is not NULL, it must be a valid pointer to a `struct device`. 151 // - `parent` is valid for the lifetime of `self`, since a `struct device` holds a 152 // reference count of its parent. 153 Some(unsafe { Self::from_raw(parent) }) 154 } 155 } 156 157 /// Convert a raw C `struct device` pointer to a `&'a Device`. 158 /// 159 /// # Safety 160 /// 161 /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count, 162 /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to 163 /// can't drop to zero, for the duration of this function call and the entire duration when the 164 /// returned reference exists. 165 pub unsafe fn from_raw<'a>(ptr: *mut bindings::device) -> &'a Self { 166 // SAFETY: Guaranteed by the safety requirements of the function. 167 unsafe { &*ptr.cast() } 168 } 169 170 /// Prints an emergency-level message (level 0) prefixed with device information. 171 /// 172 /// More details are available from [`dev_emerg`]. 173 /// 174 /// [`dev_emerg`]: crate::dev_emerg 175 pub fn pr_emerg(&self, args: fmt::Arguments<'_>) { 176 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 177 unsafe { self.printk(bindings::KERN_EMERG, args) }; 178 } 179 180 /// Prints an alert-level message (level 1) prefixed with device information. 181 /// 182 /// More details are available from [`dev_alert`]. 183 /// 184 /// [`dev_alert`]: crate::dev_alert 185 pub fn pr_alert(&self, args: fmt::Arguments<'_>) { 186 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 187 unsafe { self.printk(bindings::KERN_ALERT, args) }; 188 } 189 190 /// Prints a critical-level message (level 2) prefixed with device information. 191 /// 192 /// More details are available from [`dev_crit`]. 193 /// 194 /// [`dev_crit`]: crate::dev_crit 195 pub fn pr_crit(&self, args: fmt::Arguments<'_>) { 196 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 197 unsafe { self.printk(bindings::KERN_CRIT, args) }; 198 } 199 200 /// Prints an error-level message (level 3) prefixed with device information. 201 /// 202 /// More details are available from [`dev_err`]. 203 /// 204 /// [`dev_err`]: crate::dev_err 205 pub fn pr_err(&self, args: fmt::Arguments<'_>) { 206 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 207 unsafe { self.printk(bindings::KERN_ERR, args) }; 208 } 209 210 /// Prints a warning-level message (level 4) prefixed with device information. 211 /// 212 /// More details are available from [`dev_warn`]. 213 /// 214 /// [`dev_warn`]: crate::dev_warn 215 pub fn pr_warn(&self, args: fmt::Arguments<'_>) { 216 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 217 unsafe { self.printk(bindings::KERN_WARNING, args) }; 218 } 219 220 /// Prints a notice-level message (level 5) prefixed with device information. 221 /// 222 /// More details are available from [`dev_notice`]. 223 /// 224 /// [`dev_notice`]: crate::dev_notice 225 pub fn pr_notice(&self, args: fmt::Arguments<'_>) { 226 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 227 unsafe { self.printk(bindings::KERN_NOTICE, args) }; 228 } 229 230 /// Prints an info-level message (level 6) prefixed with device information. 231 /// 232 /// More details are available from [`dev_info`]. 233 /// 234 /// [`dev_info`]: crate::dev_info 235 pub fn pr_info(&self, args: fmt::Arguments<'_>) { 236 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 237 unsafe { self.printk(bindings::KERN_INFO, args) }; 238 } 239 240 /// Prints a debug-level message (level 7) prefixed with device information. 241 /// 242 /// More details are available from [`dev_dbg`]. 243 /// 244 /// [`dev_dbg`]: crate::dev_dbg 245 pub fn pr_dbg(&self, args: fmt::Arguments<'_>) { 246 if cfg!(debug_assertions) { 247 // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. 248 unsafe { self.printk(bindings::KERN_DEBUG, args) }; 249 } 250 } 251 252 /// Prints the provided message to the console. 253 /// 254 /// # Safety 255 /// 256 /// Callers must ensure that `klevel` is null-terminated; in particular, one of the 257 /// `KERN_*`constants, for example, `KERN_CRIT`, `KERN_ALERT`, etc. 258 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))] 259 unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) { 260 // SAFETY: `klevel` is null-terminated and one of the kernel constants. `self.as_raw` 261 // is valid because `self` is valid. The "%pA" format string expects a pointer to 262 // `fmt::Arguments`, which is what we're passing as the last argument. 263 #[cfg(CONFIG_PRINTK)] 264 unsafe { 265 bindings::_dev_printk( 266 klevel.as_ptr().cast::<crate::ffi::c_char>(), 267 self.as_raw(), 268 c_str!("%pA").as_char_ptr(), 269 core::ptr::from_ref(&msg).cast::<crate::ffi::c_void>(), 270 ) 271 }; 272 } 273 274 /// Obtain the [`FwNode`](property::FwNode) corresponding to this [`Device`]. 275 pub fn fwnode(&self) -> Option<&property::FwNode> { 276 // SAFETY: `self` is valid. 277 let fwnode_handle = unsafe { bindings::__dev_fwnode(self.as_raw()) }; 278 if fwnode_handle.is_null() { 279 return None; 280 } 281 // SAFETY: `fwnode_handle` is valid. Its lifetime is tied to `&self`. We 282 // return a reference instead of an `ARef<FwNode>` because `dev_fwnode()` 283 // doesn't increment the refcount. It is safe to cast from a 284 // `struct fwnode_handle*` to a `*const FwNode` because `FwNode` is 285 // defined as a `#[repr(transparent)]` wrapper around `fwnode_handle`. 286 Some(unsafe { &*fwnode_handle.cast() }) 287 } 288 } 289 290 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic 291 // argument. 292 kernel::impl_device_context_deref!(unsafe { Device }); 293 kernel::impl_device_context_into_aref!(Device); 294 295 // SAFETY: Instances of `Device` are always reference-counted. 296 unsafe impl crate::sync::aref::AlwaysRefCounted for Device { 297 fn inc_ref(&self) { 298 // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. 299 unsafe { bindings::get_device(self.as_raw()) }; 300 } 301 302 unsafe fn dec_ref(obj: ptr::NonNull<Self>) { 303 // SAFETY: The safety requirements guarantee that the refcount is non-zero. 304 unsafe { bindings::put_device(obj.cast().as_ptr()) } 305 } 306 } 307 308 // SAFETY: As by the type invariant `Device` can be sent to any thread. 309 unsafe impl Send for Device {} 310 311 // SAFETY: `Device` can be shared among threads because all immutable methods are protected by the 312 // synchronization in `struct device`. 313 unsafe impl Sync for Device {} 314 315 /// Marker trait for the context of a bus specific device. 316 /// 317 /// Some functions of a bus specific device should only be called from a certain context, i.e. bus 318 /// callbacks, such as `probe()`. 319 /// 320 /// This is the marker trait for structures representing the context of a bus specific device. 321 pub trait DeviceContext: private::Sealed {} 322 323 /// The [`Normal`] context is the context of a bus specific device when it is not an argument of 324 /// any bus callback. 325 pub struct Normal; 326 327 /// The [`Core`] context is the context of a bus specific device when it is supplied as argument of 328 /// any of the bus callbacks, such as `probe()`. 329 pub struct Core; 330 331 /// Semantically the same as [`Core`] but reserved for internal usage of the corresponding bus 332 /// abstraction. 333 pub struct CoreInternal; 334 335 /// The [`Bound`] context is the context of a bus specific device reference when it is guaranteed to 336 /// be bound for the duration of its lifetime. 337 pub struct Bound; 338 339 mod private { 340 pub trait Sealed {} 341 342 impl Sealed for super::Bound {} 343 impl Sealed for super::Core {} 344 impl Sealed for super::CoreInternal {} 345 impl Sealed for super::Normal {} 346 } 347 348 impl DeviceContext for Bound {} 349 impl DeviceContext for Core {} 350 impl DeviceContext for CoreInternal {} 351 impl DeviceContext for Normal {} 352 353 /// # Safety 354 /// 355 /// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the 356 /// generic argument of `$device`. 357 #[doc(hidden)] 358 #[macro_export] 359 macro_rules! __impl_device_context_deref { 360 (unsafe { $device:ident, $src:ty => $dst:ty }) => { 361 impl ::core::ops::Deref for $device<$src> { 362 type Target = $device<$dst>; 363 364 fn deref(&self) -> &Self::Target { 365 let ptr: *const Self = self; 366 367 // CAST: `$device<$src>` and `$device<$dst>` transparently wrap the same type by the 368 // safety requirement of the macro. 369 let ptr = ptr.cast::<Self::Target>(); 370 371 // SAFETY: `ptr` was derived from `&self`. 372 unsafe { &*ptr } 373 } 374 } 375 }; 376 } 377 378 /// Implement [`core::ops::Deref`] traits for allowed [`DeviceContext`] conversions of a (bus 379 /// specific) device. 380 /// 381 /// # Safety 382 /// 383 /// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the 384 /// generic argument of `$device`. 385 #[macro_export] 386 macro_rules! impl_device_context_deref { 387 (unsafe { $device:ident }) => { 388 // SAFETY: This macro has the exact same safety requirement as 389 // `__impl_device_context_deref!`. 390 ::kernel::__impl_device_context_deref!(unsafe { 391 $device, 392 $crate::device::CoreInternal => $crate::device::Core 393 }); 394 395 // SAFETY: This macro has the exact same safety requirement as 396 // `__impl_device_context_deref!`. 397 ::kernel::__impl_device_context_deref!(unsafe { 398 $device, 399 $crate::device::Core => $crate::device::Bound 400 }); 401 402 // SAFETY: This macro has the exact same safety requirement as 403 // `__impl_device_context_deref!`. 404 ::kernel::__impl_device_context_deref!(unsafe { 405 $device, 406 $crate::device::Bound => $crate::device::Normal 407 }); 408 }; 409 } 410 411 #[doc(hidden)] 412 #[macro_export] 413 macro_rules! __impl_device_context_into_aref { 414 ($src:ty, $device:tt) => { 415 impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> { 416 fn from(dev: &$device<$src>) -> Self { 417 (&**dev).into() 418 } 419 } 420 }; 421 } 422 423 /// Implement [`core::convert::From`], such that all `&Device<Ctx>` can be converted to an 424 /// `ARef<Device>`. 425 #[macro_export] 426 macro_rules! impl_device_context_into_aref { 427 ($device:tt) => { 428 ::kernel::__impl_device_context_into_aref!($crate::device::CoreInternal, $device); 429 ::kernel::__impl_device_context_into_aref!($crate::device::Core, $device); 430 ::kernel::__impl_device_context_into_aref!($crate::device::Bound, $device); 431 }; 432 } 433 434 #[doc(hidden)] 435 #[macro_export] 436 macro_rules! dev_printk { 437 ($method:ident, $dev:expr, $($f:tt)*) => { 438 { 439 ($dev).$method(::core::format_args!($($f)*)); 440 } 441 } 442 } 443 444 /// Prints an emergency-level message (level 0) prefixed with device information. 445 /// 446 /// This level should be used if the system is unusable. 447 /// 448 /// Equivalent to the kernel's `dev_emerg` macro. 449 /// 450 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 451 /// [`core::fmt`] and [`std::format!`]. 452 /// 453 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 454 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 455 /// 456 /// # Examples 457 /// 458 /// ``` 459 /// # use kernel::device::Device; 460 /// 461 /// fn example(dev: &Device) { 462 /// dev_emerg!(dev, "hello {}\n", "there"); 463 /// } 464 /// ``` 465 #[macro_export] 466 macro_rules! dev_emerg { 467 ($($f:tt)*) => { $crate::dev_printk!(pr_emerg, $($f)*); } 468 } 469 470 /// Prints an alert-level message (level 1) prefixed with device information. 471 /// 472 /// This level should be used if action must be taken immediately. 473 /// 474 /// Equivalent to the kernel's `dev_alert` macro. 475 /// 476 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 477 /// [`core::fmt`] and [`std::format!`]. 478 /// 479 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 480 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 481 /// 482 /// # Examples 483 /// 484 /// ``` 485 /// # use kernel::device::Device; 486 /// 487 /// fn example(dev: &Device) { 488 /// dev_alert!(dev, "hello {}\n", "there"); 489 /// } 490 /// ``` 491 #[macro_export] 492 macro_rules! dev_alert { 493 ($($f:tt)*) => { $crate::dev_printk!(pr_alert, $($f)*); } 494 } 495 496 /// Prints a critical-level message (level 2) prefixed with device information. 497 /// 498 /// This level should be used in critical conditions. 499 /// 500 /// Equivalent to the kernel's `dev_crit` macro. 501 /// 502 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 503 /// [`core::fmt`] and [`std::format!`]. 504 /// 505 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 506 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 507 /// 508 /// # Examples 509 /// 510 /// ``` 511 /// # use kernel::device::Device; 512 /// 513 /// fn example(dev: &Device) { 514 /// dev_crit!(dev, "hello {}\n", "there"); 515 /// } 516 /// ``` 517 #[macro_export] 518 macro_rules! dev_crit { 519 ($($f:tt)*) => { $crate::dev_printk!(pr_crit, $($f)*); } 520 } 521 522 /// Prints an error-level message (level 3) prefixed with device information. 523 /// 524 /// This level should be used in error conditions. 525 /// 526 /// Equivalent to the kernel's `dev_err` macro. 527 /// 528 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 529 /// [`core::fmt`] and [`std::format!`]. 530 /// 531 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 532 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 533 /// 534 /// # Examples 535 /// 536 /// ``` 537 /// # use kernel::device::Device; 538 /// 539 /// fn example(dev: &Device) { 540 /// dev_err!(dev, "hello {}\n", "there"); 541 /// } 542 /// ``` 543 #[macro_export] 544 macro_rules! dev_err { 545 ($($f:tt)*) => { $crate::dev_printk!(pr_err, $($f)*); } 546 } 547 548 /// Prints a warning-level message (level 4) prefixed with device information. 549 /// 550 /// This level should be used in warning conditions. 551 /// 552 /// Equivalent to the kernel's `dev_warn` macro. 553 /// 554 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 555 /// [`core::fmt`] and [`std::format!`]. 556 /// 557 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 558 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 559 /// 560 /// # Examples 561 /// 562 /// ``` 563 /// # use kernel::device::Device; 564 /// 565 /// fn example(dev: &Device) { 566 /// dev_warn!(dev, "hello {}\n", "there"); 567 /// } 568 /// ``` 569 #[macro_export] 570 macro_rules! dev_warn { 571 ($($f:tt)*) => { $crate::dev_printk!(pr_warn, $($f)*); } 572 } 573 574 /// Prints a notice-level message (level 5) prefixed with device information. 575 /// 576 /// This level should be used in normal but significant conditions. 577 /// 578 /// Equivalent to the kernel's `dev_notice` macro. 579 /// 580 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 581 /// [`core::fmt`] and [`std::format!`]. 582 /// 583 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 584 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 585 /// 586 /// # Examples 587 /// 588 /// ``` 589 /// # use kernel::device::Device; 590 /// 591 /// fn example(dev: &Device) { 592 /// dev_notice!(dev, "hello {}\n", "there"); 593 /// } 594 /// ``` 595 #[macro_export] 596 macro_rules! dev_notice { 597 ($($f:tt)*) => { $crate::dev_printk!(pr_notice, $($f)*); } 598 } 599 600 /// Prints an info-level message (level 6) prefixed with device information. 601 /// 602 /// This level should be used for informational messages. 603 /// 604 /// Equivalent to the kernel's `dev_info` macro. 605 /// 606 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 607 /// [`core::fmt`] and [`std::format!`]. 608 /// 609 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 610 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 611 /// 612 /// # Examples 613 /// 614 /// ``` 615 /// # use kernel::device::Device; 616 /// 617 /// fn example(dev: &Device) { 618 /// dev_info!(dev, "hello {}\n", "there"); 619 /// } 620 /// ``` 621 #[macro_export] 622 macro_rules! dev_info { 623 ($($f:tt)*) => { $crate::dev_printk!(pr_info, $($f)*); } 624 } 625 626 /// Prints a debug-level message (level 7) prefixed with device information. 627 /// 628 /// This level should be used for debug messages. 629 /// 630 /// Equivalent to the kernel's `dev_dbg` macro, except that it doesn't support dynamic debug yet. 631 /// 632 /// Mimics the interface of [`std::print!`]. More information about the syntax is available from 633 /// [`core::fmt`] and [`std::format!`]. 634 /// 635 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html 636 /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html 637 /// 638 /// # Examples 639 /// 640 /// ``` 641 /// # use kernel::device::Device; 642 /// 643 /// fn example(dev: &Device) { 644 /// dev_dbg!(dev, "hello {}\n", "there"); 645 /// } 646 /// ``` 647 #[macro_export] 648 macro_rules! dev_dbg { 649 ($($f:tt)*) => { $crate::dev_printk!(pr_dbg, $($f)*); } 650 } 651