| 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 | , |
| 10 | ; |
| 11 | use ; |
| 12 | |
| 13 | |
| 14 | use cratec_str; |
| 15 | |
| 16 | /// A reference-counted device. |
| 17 | /// |
| 18 | /// This structure represents the Rust abstraction for a C `struct device`. This implementation |
| 19 | /// abstracts the usage of an already existing C `struct device` within Rust code that we get |
| 20 | /// passed from the C side. |
| 21 | /// |
| 22 | /// An instance of this abstraction can be obtained temporarily or permanent. |
| 23 | /// |
| 24 | /// A temporary one is bound to the lifetime of the C `struct device` pointer used for creation. |
| 25 | /// A permanent instance is always reference-counted and hence not restricted by any lifetime |
| 26 | /// boundaries. |
| 27 | /// |
| 28 | /// For subsystems it is recommended to create a permanent instance to wrap into a subsystem |
| 29 | /// specific device structure (e.g. `pci::Device`). This is useful for passing it to drivers in |
| 30 | /// `T::probe()`, such that a driver can store the `ARef<Device>` (equivalent to storing a |
| 31 | /// `struct device` pointer in a C driver) for arbitrary purposes, e.g. allocating DMA coherent |
| 32 | /// memory. |
| 33 | /// |
| 34 | /// # Invariants |
| 35 | /// |
| 36 | /// A `Device` instance represents a valid `struct device` created by the C portion of the kernel. |
| 37 | /// |
| 38 | /// Instances of this type are always reference-counted, that is, a call to `get_device` ensures |
| 39 | /// that the allocation remains valid at least until the matching call to `put_device`. |
| 40 | /// |
| 41 | /// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be |
| 42 | /// dropped from any thread. |
| 43 | |
| 44 | ; |
| 45 | |
| 46 | |
| 47 | /// Creates a new reference-counted abstraction instance of an existing `struct device` pointer. |
| 48 | /// |
| 49 | /// # Safety |
| 50 | /// |
| 51 | /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count, |
| 52 | /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to |
| 53 | /// can't drop to zero, for the duration of this function call. |
| 54 | /// |
| 55 | /// It must also be ensured that `bindings::device::release` can be called from any thread. |
| 56 | /// While not officially documented, this should be the case for any `struct device`. |
| 57 | pub unsafe |
| 58 | // SAFETY: By the safety requirements ptr is valid |
| 59 | unsafe .into |
| 60 | |
| 61 | |
| 62 | /// Obtain the raw `struct device *`. |
| 63 | pub *mut device |
| 64 | self.0.get |
| 65 | |
| 66 | |
| 67 | /// Convert a raw C `struct device` pointer to a `&'a Device`. |
| 68 | /// |
| 69 | /// # Safety |
| 70 | /// |
| 71 | /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count, |
| 72 | /// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to |
| 73 | /// can't drop to zero, for the duration of this function call and the entire duration when the |
| 74 | /// returned reference exists. |
| 75 | pub unsafe |
| 76 | // SAFETY: Guaranteed by the safety requirements of the function. |
| 77 | unsafe |
| 78 | |
| 79 | |
| 80 | /// Prints an emergency-level message (level 0) prefixed with device information. |
| 81 | /// |
| 82 | /// More details are available from [`dev_emerg`]. |
| 83 | /// |
| 84 | /// [`dev_emerg`]: crate::dev_emerg |
| 85 | |
| 86 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 87 | unsafe ; |
| 88 | |
| 89 | |
| 90 | /// Prints an alert-level message (level 1) prefixed with device information. |
| 91 | /// |
| 92 | /// More details are available from [`dev_alert`]. |
| 93 | /// |
| 94 | /// [`dev_alert`]: crate::dev_alert |
| 95 | |
| 96 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 97 | unsafe ; |
| 98 | |
| 99 | |
| 100 | /// Prints a critical-level message (level 2) prefixed with device information. |
| 101 | /// |
| 102 | /// More details are available from [`dev_crit`]. |
| 103 | /// |
| 104 | /// [`dev_crit`]: crate::dev_crit |
| 105 | |
| 106 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 107 | unsafe ; |
| 108 | |
| 109 | |
| 110 | /// Prints an error-level message (level 3) prefixed with device information. |
| 111 | /// |
| 112 | /// More details are available from [`dev_err`]. |
| 113 | /// |
| 114 | /// [`dev_err`]: crate::dev_err |
| 115 | |
| 116 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 117 | unsafe ; |
| 118 | |
| 119 | |
| 120 | /// Prints a warning-level message (level 4) prefixed with device information. |
| 121 | /// |
| 122 | /// More details are available from [`dev_warn`]. |
| 123 | /// |
| 124 | /// [`dev_warn`]: crate::dev_warn |
| 125 | |
| 126 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 127 | unsafe ; |
| 128 | |
| 129 | |
| 130 | /// Prints a notice-level message (level 5) prefixed with device information. |
| 131 | /// |
| 132 | /// More details are available from [`dev_notice`]. |
| 133 | /// |
| 134 | /// [`dev_notice`]: crate::dev_notice |
| 135 | |
| 136 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 137 | unsafe ; |
| 138 | |
| 139 | |
| 140 | /// Prints an info-level message (level 6) prefixed with device information. |
| 141 | /// |
| 142 | /// More details are available from [`dev_info`]. |
| 143 | /// |
| 144 | /// [`dev_info`]: crate::dev_info |
| 145 | |
| 146 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 147 | unsafe ; |
| 148 | |
| 149 | |
| 150 | /// Prints a debug-level message (level 7) prefixed with device information. |
| 151 | /// |
| 152 | /// More details are available from [`dev_dbg`]. |
| 153 | /// |
| 154 | /// [`dev_dbg`]: crate::dev_dbg |
| 155 | |
| 156 | if cfg! |
| 157 | // SAFETY: `klevel` is null-terminated, uses one of the kernel constants. |
| 158 | unsafe ; |
| 159 | |
| 160 | |
| 161 | |
| 162 | /// Prints the provided message to the console. |
| 163 | /// |
| 164 | /// # Safety |
| 165 | /// |
| 166 | /// Callers must ensure that `klevel` is null-terminated; in particular, one of the |
| 167 | /// `KERN_*`constants, for example, `KERN_CRIT`, `KERN_ALERT`, etc. |
| 168 | |
| 169 | unsafe |
| 170 | // SAFETY: `klevel` is null-terminated and one of the kernel constants. `self.as_raw` |
| 171 | // is valid because `self` is valid. The "%pA" format string expects a pointer to |
| 172 | // `fmt::Arguments`, which is what we're passing as the last argument. |
| 173 | |
| 174 | unsafe |
| 175 | _dev_printk |
| 176 | klevel as *const _ as *const c_char, |
| 177 | self.as_raw, |
| 178 | c_str!.as_char_ptr, |
| 179 | &msg as *const _ as *const c_void, |
| 180 | |
| 181 | ; |
| 182 | |
| 183 | |
| 184 | |
| 185 | // SAFETY: Instances of `Device` are always reference-counted. |
| 186 | unsafe |
| 187 | |
| 188 | // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. |
| 189 | unsafe ; |
| 190 | |
| 191 | |
| 192 | unsafe |
| 193 | // SAFETY: The safety requirements guarantee that the refcount is non-zero. |
| 194 | unsafe |
| 195 | |
| 196 | |
| 197 | |
| 198 | // SAFETY: As by the type invariant `Device` can be sent to any thread. |
| 199 | unsafe |
| 200 | |
| 201 | // SAFETY: `Device` can be shared among threads because all immutable methods are protected by the |
| 202 | // synchronization in `struct device`. |
| 203 | unsafe |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | => |
| 209 | |
| 210 | .$method; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /// Prints an emergency-level message (level 0) prefixed with device information. |
| 216 | /// |
| 217 | /// This level should be used if the system is unusable. |
| 218 | /// |
| 219 | /// Equivalent to the kernel's `dev_emerg` macro. |
| 220 | /// |
| 221 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 222 | /// [`core::fmt`] and `alloc::format!`. |
| 223 | /// |
| 224 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 225 | /// |
| 226 | /// # Examples |
| 227 | /// |
| 228 | /// ``` |
| 229 | /// # use kernel::device::Device; |
| 230 | /// |
| 231 | /// fn example(dev: &Device) { |
| 232 | /// dev_emerg!(dev, "hello {}\n", "there"); |
| 233 | /// } |
| 234 | /// ``` |
| 235 | |
| 236 | |
| 237 | => |
| 238 | |
| 239 | |
| 240 | /// Prints an alert-level message (level 1) prefixed with device information. |
| 241 | /// |
| 242 | /// This level should be used if action must be taken immediately. |
| 243 | /// |
| 244 | /// Equivalent to the kernel's `dev_alert` macro. |
| 245 | /// |
| 246 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 247 | /// [`core::fmt`] and `alloc::format!`. |
| 248 | /// |
| 249 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 250 | /// |
| 251 | /// # Examples |
| 252 | /// |
| 253 | /// ``` |
| 254 | /// # use kernel::device::Device; |
| 255 | /// |
| 256 | /// fn example(dev: &Device) { |
| 257 | /// dev_alert!(dev, "hello {}\n", "there"); |
| 258 | /// } |
| 259 | /// ``` |
| 260 | |
| 261 | |
| 262 | => |
| 263 | |
| 264 | |
| 265 | /// Prints a critical-level message (level 2) prefixed with device information. |
| 266 | /// |
| 267 | /// This level should be used in critical conditions. |
| 268 | /// |
| 269 | /// Equivalent to the kernel's `dev_crit` macro. |
| 270 | /// |
| 271 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 272 | /// [`core::fmt`] and `alloc::format!`. |
| 273 | /// |
| 274 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 275 | /// |
| 276 | /// # Examples |
| 277 | /// |
| 278 | /// ``` |
| 279 | /// # use kernel::device::Device; |
| 280 | /// |
| 281 | /// fn example(dev: &Device) { |
| 282 | /// dev_crit!(dev, "hello {}\n", "there"); |
| 283 | /// } |
| 284 | /// ``` |
| 285 | |
| 286 | |
| 287 | => |
| 288 | |
| 289 | |
| 290 | /// Prints an error-level message (level 3) prefixed with device information. |
| 291 | /// |
| 292 | /// This level should be used in error conditions. |
| 293 | /// |
| 294 | /// Equivalent to the kernel's `dev_err` macro. |
| 295 | /// |
| 296 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 297 | /// [`core::fmt`] and `alloc::format!`. |
| 298 | /// |
| 299 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 300 | /// |
| 301 | /// # Examples |
| 302 | /// |
| 303 | /// ``` |
| 304 | /// # use kernel::device::Device; |
| 305 | /// |
| 306 | /// fn example(dev: &Device) { |
| 307 | /// dev_err!(dev, "hello {}\n", "there"); |
| 308 | /// } |
| 309 | /// ``` |
| 310 | |
| 311 | |
| 312 | => |
| 313 | |
| 314 | |
| 315 | /// Prints a warning-level message (level 4) prefixed with device information. |
| 316 | /// |
| 317 | /// This level should be used in warning conditions. |
| 318 | /// |
| 319 | /// Equivalent to the kernel's `dev_warn` macro. |
| 320 | /// |
| 321 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 322 | /// [`core::fmt`] and `alloc::format!`. |
| 323 | /// |
| 324 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 325 | /// |
| 326 | /// # Examples |
| 327 | /// |
| 328 | /// ``` |
| 329 | /// # use kernel::device::Device; |
| 330 | /// |
| 331 | /// fn example(dev: &Device) { |
| 332 | /// dev_warn!(dev, "hello {}\n", "there"); |
| 333 | /// } |
| 334 | /// ``` |
| 335 | |
| 336 | |
| 337 | => |
| 338 | |
| 339 | |
| 340 | /// Prints a notice-level message (level 5) prefixed with device information. |
| 341 | /// |
| 342 | /// This level should be used in normal but significant conditions. |
| 343 | /// |
| 344 | /// Equivalent to the kernel's `dev_notice` macro. |
| 345 | /// |
| 346 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 347 | /// [`core::fmt`] and `alloc::format!`. |
| 348 | /// |
| 349 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 350 | /// |
| 351 | /// # Examples |
| 352 | /// |
| 353 | /// ``` |
| 354 | /// # use kernel::device::Device; |
| 355 | /// |
| 356 | /// fn example(dev: &Device) { |
| 357 | /// dev_notice!(dev, "hello {}\n", "there"); |
| 358 | /// } |
| 359 | /// ``` |
| 360 | |
| 361 | |
| 362 | => |
| 363 | |
| 364 | |
| 365 | /// Prints an info-level message (level 6) prefixed with device information. |
| 366 | /// |
| 367 | /// This level should be used for informational messages. |
| 368 | /// |
| 369 | /// Equivalent to the kernel's `dev_info` macro. |
| 370 | /// |
| 371 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 372 | /// [`core::fmt`] and `alloc::format!`. |
| 373 | /// |
| 374 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 375 | /// |
| 376 | /// # Examples |
| 377 | /// |
| 378 | /// ``` |
| 379 | /// # use kernel::device::Device; |
| 380 | /// |
| 381 | /// fn example(dev: &Device) { |
| 382 | /// dev_info!(dev, "hello {}\n", "there"); |
| 383 | /// } |
| 384 | /// ``` |
| 385 | |
| 386 | |
| 387 | => |
| 388 | |
| 389 | |
| 390 | /// Prints a debug-level message (level 7) prefixed with device information. |
| 391 | /// |
| 392 | /// This level should be used for debug messages. |
| 393 | /// |
| 394 | /// Equivalent to the kernel's `dev_dbg` macro, except that it doesn't support dynamic debug yet. |
| 395 | /// |
| 396 | /// Mimics the interface of [`std::print!`]. More information about the syntax is available from |
| 397 | /// [`core::fmt`] and `alloc::format!`. |
| 398 | /// |
| 399 | /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html |
| 400 | /// |
| 401 | /// # Examples |
| 402 | /// |
| 403 | /// ``` |
| 404 | /// # use kernel::device::Device; |
| 405 | /// |
| 406 | /// fn example(dev: &Device) { |
| 407 | /// dev_dbg!(dev, "hello {}\n", "there"); |
| 408 | /// } |
| 409 | /// ``` |
| 410 | |
| 411 | |
| 412 | => |
| 413 |