| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 |
|
| 112 | use crate::alloc::flags;
|
| 113 | use crate::container_of;
|
| 114 | use crate::page::PAGE_SIZE;
|
| 115 | use crate::prelude::*;
|
| 116 | use crate::str::CString;
|
| 117 | use crate::sync::Arc;
|
| 118 | use crate::sync::ArcBorrow;
|
| 119 | use crate::types::Opaque;
|
| 120 | use core::cell::UnsafeCell;
|
| 121 | use core::marker::PhantomData;
|
| 122 |
|
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | #[pin_data(PinnedDrop)]
|
| 128 | pub struct Subsystem<Data> {
|
| 129 | #[pin]
|
| 130 | subsystem: Opaque<bindings::configfs_subsystem>,
|
| 131 | #[pin]
|
| 132 | data: Data,
|
| 133 | }
|
| 134 |
|
| 135 | |
| 136 | unsafe impl<Data> Sync for Subsystem<Data> {}
|
| 137 |
|
| 138 | |
| 139 | unsafe impl<Data> Send for Subsystem<Data> {}
|
| 140 |
|
| 141 | impl<Data> Subsystem<Data> {
|
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | pub fn new(
|
| 148 | name: &'static CStr,
|
| 149 | item_type: &'static ItemType<Subsystem<Data>, Data>,
|
| 150 | data: impl PinInit<Data, Error>,
|
| 151 | ) -> impl PinInit<Self, Error> {
|
| 152 | try_pin_init!(Self {
|
| 153 | subsystem <- pin_init::init_zeroed().chain(
|
| 154 | |place: &mut Opaque<bindings::configfs_subsystem>| {
|
| 155 | |
| 156 | unsafe {
|
| 157 | bindings::config_group_init_type_name(
|
| 158 | &mut (*place.get()).su_group,
|
| 159 | name.as_char_ptr(),
|
| 160 | item_type.as_ptr(),
|
| 161 | )
|
| 162 | };
|
| 163 |
|
| 164 | |
| 165 | unsafe {
|
| 166 | bindings::__mutex_init(
|
| 167 | &mut (*place.get()).su_mutex,
|
| 168 | kernel::optional_name!().as_char_ptr(),
|
| 169 | kernel::static_lock_class!().as_ptr(),
|
| 170 | )
|
| 171 | }
|
| 172 | Ok(())
|
| 173 | }
|
| 174 | ),
|
| 175 | data <- data,
|
| 176 | })
|
| 177 | .pin_chain(|this| {
|
| 178 | crate::error::to_result(
|
| 179 | |
| 180 | unsafe { bindings::configfs_register_subsystem(this.subsystem.get()) },
|
| 181 | )
|
| 182 | })
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | #[pinned_drop]
|
| 187 | impl<Data> PinnedDrop for Subsystem<Data> {
|
| 188 | fn drop(self: Pin<&mut Self>) {
|
| 189 | |
| 190 | unsafe { bindings::configfs_unregister_subsystem(self.subsystem.get()) };
|
| 191 | |
| 192 | unsafe { bindings::mutex_destroy(&raw mut (*self.subsystem.get()).su_mutex) };
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | pub unsafe trait HasGroup<Data> {
|
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | unsafe fn group(this: *const Self) -> *const bindings::config_group;
|
| 212 |
|
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | unsafe fn container_of(group: *const bindings::config_group) -> *const Self;
|
| 220 | }
|
| 221 |
|
| 222 | |
| 223 | |
| 224 | unsafe impl<Data> HasGroup<Data> for Subsystem<Data> {
|
| 225 | unsafe fn group(this: *const Self) -> *const bindings::config_group {
|
| 226 | |
| 227 | unsafe { &raw const (*(*this).subsystem.get()).su_group }
|
| 228 | }
|
| 229 |
|
| 230 | unsafe fn container_of(group: *const bindings::config_group) -> *const Self {
|
| 231 | |
| 232 | let c_subsys_ptr = unsafe { container_of!(group, bindings::configfs_subsystem, su_group) };
|
| 233 | let opaque_ptr = c_subsys_ptr.cast::<Opaque<bindings::configfs_subsystem>>();
|
| 234 | |
| 235 | |
| 236 | unsafe { container_of!(opaque_ptr, Subsystem<Data>, subsystem) }
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | #[pin_data]
|
| 245 | pub struct Group<Data> {
|
| 246 | #[pin]
|
| 247 | group: Opaque<bindings::config_group>,
|
| 248 | #[pin]
|
| 249 | data: Data,
|
| 250 | }
|
| 251 |
|
| 252 | impl<Data> Group<Data> {
|
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | pub fn new(
|
| 258 | name: CString,
|
| 259 | item_type: &'static ItemType<Group<Data>, Data>,
|
| 260 | data: impl PinInit<Data, Error>,
|
| 261 | ) -> impl PinInit<Self, Error> {
|
| 262 | try_pin_init!(Self {
|
| 263 | group <- pin_init::init_zeroed().chain(|v: &mut Opaque<bindings::config_group>| {
|
| 264 | let place = v.get();
|
| 265 | let name = name.to_bytes_with_nul().as_ptr();
|
| 266 | |
| 267 | unsafe {
|
| 268 | bindings::config_group_init_type_name(place, name.cast(), item_type.as_ptr())
|
| 269 | };
|
| 270 | Ok(())
|
| 271 | }),
|
| 272 | data <- data,
|
| 273 | })
|
| 274 | }
|
| 275 | }
|
| 276 |
|
| 277 | |
| 278 | |
| 279 | unsafe impl<Data> HasGroup<Data> for Group<Data> {
|
| 280 | unsafe fn group(this: *const Self) -> *const bindings::config_group {
|
| 281 | Opaque::cast_into(
|
| 282 | |
| 283 | |
| 284 | unsafe { &raw const (*this).group },
|
| 285 | )
|
| 286 | }
|
| 287 |
|
| 288 | unsafe fn container_of(group: *const bindings::config_group) -> *const Self {
|
| 289 | let opaque_ptr = group.cast::<Opaque<bindings::config_group>>();
|
| 290 | |
| 291 | |
| 292 | unsafe { container_of!(opaque_ptr, Self, group) }
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | unsafe fn get_group_data<'a, Parent>(this: *mut bindings::config_group) -> &'a Parent {
|
| 308 | |
| 309 | let is_root = unsafe { (*this).cg_subsys.is_null() };
|
| 310 |
|
| 311 | if !is_root {
|
| 312 | |
| 313 | |
| 314 | |
| 315 | unsafe { &(*Group::<Parent>::container_of(this)).data }
|
| 316 | } else {
|
| 317 | |
| 318 | |
| 319 | unsafe { &(*Subsystem::container_of(this)).data }
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | struct GroupOperationsVTable<Parent, Child>(PhantomData<(Parent, Child)>);
|
| 324 |
|
| 325 | impl<Parent, Child> GroupOperationsVTable<Parent, Child>
|
| 326 | where
|
| 327 | Parent: GroupOperations<Child = Child>,
|
| 328 | Child: 'static,
|
| 329 | {
|
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | unsafe extern "C" fn make_group(
|
| 344 | this: *mut bindings::config_group,
|
| 345 | name: *const kernel::ffi::c_char,
|
| 346 | ) -> *mut bindings::config_group {
|
| 347 | |
| 348 | |
| 349 | let parent_data = unsafe { get_group_data(this) };
|
| 350 |
|
| 351 | let group_init = match Parent::make_group(
|
| 352 | parent_data,
|
| 353 | |
| 354 | |
| 355 | unsafe { CStr::from_char_ptr(name) },
|
| 356 | ) {
|
| 357 | Ok(init) => init,
|
| 358 | Err(e) => return e.to_ptr(),
|
| 359 | };
|
| 360 |
|
| 361 | let child_group = <Arc<Group<Child>> as InPlaceInit<Group<Child>>>::try_pin_init(
|
| 362 | group_init,
|
| 363 | flags::GFP_KERNEL,
|
| 364 | );
|
| 365 |
|
| 366 | match child_group {
|
| 367 | Ok(child_group) => {
|
| 368 | let child_group_ptr = child_group.into_raw();
|
| 369 | |
| 370 | |
| 371 | unsafe { Group::<Child>::group(child_group_ptr) }.cast_mut()
|
| 372 | }
|
| 373 | Err(e) => e.to_ptr(),
|
| 374 | }
|
| 375 | }
|
| 376 |
|
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | unsafe extern "C" fn drop_item(
|
| 390 | this: *mut bindings::config_group,
|
| 391 | item: *mut bindings::config_item,
|
| 392 | ) {
|
| 393 | |
| 394 | |
| 395 | let parent_data = unsafe { get_group_data(this) };
|
| 396 |
|
| 397 | |
| 398 | |
| 399 | let c_child_group_ptr = unsafe { container_of!(item, bindings::config_group, cg_item) };
|
| 400 | |
| 401 | |
| 402 | let r_child_group_ptr = unsafe { Group::<Child>::container_of(c_child_group_ptr) };
|
| 403 |
|
| 404 | if Parent::HAS_DROP_ITEM {
|
| 405 | |
| 406 | |
| 407 | let arc: Arc<Group<Child>> = unsafe { Arc::from_raw(r_child_group_ptr.cast_mut()) };
|
| 408 |
|
| 409 | Parent::drop_item(parent_data, arc.as_arc_borrow());
|
| 410 | arc.into_raw();
|
| 411 | }
|
| 412 |
|
| 413 | |
| 414 | |
| 415 | unsafe { bindings::config_item_put(item) };
|
| 416 | }
|
| 417 |
|
| 418 | const VTABLE: bindings::configfs_group_operations = bindings::configfs_group_operations {
|
| 419 | make_item: None,
|
| 420 | make_group: Some(Self::make_group),
|
| 421 | disconnect_notify: None,
|
| 422 | drop_item: Some(Self::drop_item),
|
| 423 | is_visible: None,
|
| 424 | is_bin_visible: None,
|
| 425 | };
|
| 426 |
|
| 427 | const fn vtable_ptr() -> *const bindings::configfs_group_operations {
|
| 428 | &Self::VTABLE
|
| 429 | }
|
| 430 | }
|
| 431 |
|
| 432 | struct ItemOperationsVTable<Container, Data>(PhantomData<(Container, Data)>);
|
| 433 |
|
| 434 | impl<Data> ItemOperationsVTable<Group<Data>, Data>
|
| 435 | where
|
| 436 | Data: 'static,
|
| 437 | {
|
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | unsafe extern "C" fn release(this: *mut bindings::config_item) {
|
| 446 | |
| 447 | |
| 448 | let c_group_ptr = unsafe { kernel::container_of!(this, bindings::config_group, cg_item) };
|
| 449 | |
| 450 | |
| 451 | let r_group_ptr = unsafe { Group::<Data>::container_of(c_group_ptr) };
|
| 452 |
|
| 453 | |
| 454 | |
| 455 | let pin_self: Arc<Group<Data>> = unsafe { Arc::from_raw(r_group_ptr.cast_mut()) };
|
| 456 | drop(pin_self);
|
| 457 | }
|
| 458 |
|
| 459 | const VTABLE: bindings::configfs_item_operations = bindings::configfs_item_operations {
|
| 460 | release: Some(Self::release),
|
| 461 | allow_link: None,
|
| 462 | drop_link: None,
|
| 463 | };
|
| 464 |
|
| 465 | const fn vtable_ptr() -> *const bindings::configfs_item_operations {
|
| 466 | &Self::VTABLE
|
| 467 | }
|
| 468 | }
|
| 469 |
|
| 470 | impl<Data> ItemOperationsVTable<Subsystem<Data>, Data> {
|
| 471 | const VTABLE: bindings::configfs_item_operations = bindings::configfs_item_operations {
|
| 472 | release: None,
|
| 473 | allow_link: None,
|
| 474 | drop_link: None,
|
| 475 | };
|
| 476 |
|
| 477 | const fn vtable_ptr() -> *const bindings::configfs_item_operations {
|
| 478 | &Self::VTABLE
|
| 479 | }
|
| 480 | }
|
| 481 |
|
| 482 | |
| 483 | |
| 484 | |
| 485 | #[vtable]
|
| 486 | pub trait GroupOperations {
|
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | type Child: 'static;
|
| 492 |
|
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | fn make_group(&self, name: &CStr) -> Result<impl PinInit<Group<Self::Child>, Error>>;
|
| 502 |
|
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | fn drop_item(&self, _child: ArcBorrow<'_, Group<Self::Child>>) {
|
| 514 | kernel::build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
|
| 515 | }
|
| 516 | }
|
| 517 |
|
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | #[repr(transparent)]
|
| 523 | pub struct Attribute<const ID: u64, O, Data> {
|
| 524 | attribute: Opaque<bindings::configfs_attribute>,
|
| 525 | _p: PhantomData<(O, Data)>,
|
| 526 | }
|
| 527 |
|
| 528 | |
| 529 | unsafe impl<const ID: u64, O, Data> Sync for Attribute<ID, O, Data> {}
|
| 530 |
|
| 531 | |
| 532 | unsafe impl<const ID: u64, O, Data> Send for Attribute<ID, O, Data> {}
|
| 533 |
|
| 534 | impl<const ID: u64, O, Data> Attribute<ID, O, Data>
|
| 535 | where
|
| 536 | O: AttributeOperations<ID, Data = Data>,
|
| 537 | {
|
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | unsafe extern "C" fn show(
|
| 550 | item: *mut bindings::config_item,
|
| 551 | page: *mut kernel::ffi::c_char,
|
| 552 | ) -> isize {
|
| 553 | let c_group: *mut bindings::config_group =
|
| 554 | |
| 555 | |
| 556 | unsafe { container_of!(item, bindings::config_group, cg_item) };
|
| 557 |
|
| 558 | |
| 559 | |
| 560 | let data: &Data = unsafe { get_group_data(c_group) };
|
| 561 |
|
| 562 | |
| 563 | let ret = O::show(data, unsafe { &mut *(page.cast::<[u8; PAGE_SIZE]>()) });
|
| 564 |
|
| 565 | match ret {
|
| 566 | Ok(size) => size as isize,
|
| 567 | Err(err) => err.to_errno() as isize,
|
| 568 | }
|
| 569 | }
|
| 570 |
|
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | unsafe extern "C" fn store(
|
| 583 | item: *mut bindings::config_item,
|
| 584 | page: *const kernel::ffi::c_char,
|
| 585 | size: usize,
|
| 586 | ) -> isize {
|
| 587 | let c_group: *mut bindings::config_group =
|
| 588 | |
| 589 | |
| 590 | unsafe { container_of!(item, bindings::config_group, cg_item) };
|
| 591 |
|
| 592 | |
| 593 | |
| 594 | let data: &Data = unsafe { get_group_data(c_group) };
|
| 595 |
|
| 596 | let ret = O::store(
|
| 597 | data,
|
| 598 | |
| 599 | |
| 600 | unsafe { core::slice::from_raw_parts(page.cast(), size) },
|
| 601 | );
|
| 602 |
|
| 603 | match ret {
|
| 604 | Ok(()) => size as isize,
|
| 605 | Err(err) => err.to_errno() as isize,
|
| 606 | }
|
| 607 | }
|
| 608 |
|
| 609 | |
| 610 | |
| 611 | |
| 612 | pub const fn new(name: &'static CStr) -> Self {
|
| 613 | Self {
|
| 614 | attribute: Opaque::new(bindings::configfs_attribute {
|
| 615 | ca_name: crate::str::as_char_ptr_in_const_context(name),
|
| 616 | ca_owner: core::ptr::null_mut(),
|
| 617 | ca_mode: 0o660,
|
| 618 | show: Some(Self::show),
|
| 619 | store: if O::HAS_STORE {
|
| 620 | Some(Self::store)
|
| 621 | } else {
|
| 622 | None
|
| 623 | },
|
| 624 | }),
|
| 625 | _p: PhantomData,
|
| 626 | }
|
| 627 | }
|
| 628 | }
|
| 629 |
|
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | #[vtable]
|
| 643 | pub trait AttributeOperations<const ID: u64 = 0> {
|
| 644 | |
| 645 | |
| 646 | type Data;
|
| 647 |
|
| 648 | |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | fn show(data: &Self::Data, page: &mut [u8; PAGE_SIZE]) -> Result<usize>;
|
| 655 |
|
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | fn store(_data: &Self::Data, _page: &[u8]) -> Result {
|
| 663 | kernel::build_error!(kernel::error::VTABLE_DEFAULT_ERROR)
|
| 664 | }
|
| 665 | }
|
| 666 |
|
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | #[repr(transparent)]
|
| 680 | pub struct AttributeList<const N: usize, Data>(
|
| 681 | |
| 682 | |
| 683 | UnsafeCell<[*mut kernel::ffi::c_void; N]>,
|
| 684 | PhantomData<Data>,
|
| 685 | );
|
| 686 |
|
| 687 | |
| 688 | unsafe impl<const N: usize, Data> Send for AttributeList<N, Data> {}
|
| 689 |
|
| 690 | |
| 691 | unsafe impl<const N: usize, Data> Sync for AttributeList<N, Data> {}
|
| 692 |
|
| 693 | impl<const N: usize, Data> AttributeList<N, Data> {
|
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | #[doc(hidden)]
|
| 699 | pub const unsafe fn new() -> Self {
|
| 700 | Self(UnsafeCell::new([core::ptr::null_mut(); N]), PhantomData)
|
| 701 | }
|
| 702 |
|
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | #[doc(hidden)]
|
| 708 | pub const unsafe fn add<const I: usize, const ID: u64, O>(
|
| 709 | &'static self,
|
| 710 | attribute: &'static Attribute<ID, O, Data>,
|
| 711 | ) where
|
| 712 | O: AttributeOperations<ID, Data = Data>,
|
| 713 | {
|
| 714 | |
| 715 | const { assert!(I < N - 1, "Invalid attribute index") };
|
| 716 |
|
| 717 | |
| 718 | |
| 719 | unsafe { (&mut *self.0.get())[I] = core::ptr::from_ref(attribute).cast_mut().cast() };
|
| 720 | }
|
| 721 | }
|
| 722 |
|
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | #[pin_data]
|
| 730 | pub struct ItemType<Container, Data> {
|
| 731 | #[pin]
|
| 732 | item_type: Opaque<bindings::config_item_type>,
|
| 733 | _p: PhantomData<(Container, Data)>,
|
| 734 | }
|
| 735 |
|
| 736 | |
| 737 | unsafe impl<Container, Data> Sync for ItemType<Container, Data> {}
|
| 738 |
|
| 739 | |
| 740 | unsafe impl<Container, Data> Send for ItemType<Container, Data> {}
|
| 741 |
|
| 742 | macro_rules! impl_item_type {
|
| 743 | ($tpe:ty) => {
|
| 744 | impl<Data> ItemType<$tpe, Data> {
|
| 745 | #[doc(hidden)]
|
| 746 | pub const fn new_with_child_ctor<const N: usize, Child>(
|
| 747 | owner: &'static ThisModule,
|
| 748 | attributes: &'static AttributeList<N, Data>,
|
| 749 | ) -> Self
|
| 750 | where
|
| 751 | Data: GroupOperations<Child = Child>,
|
| 752 | Child: 'static,
|
| 753 | {
|
| 754 | Self {
|
| 755 | item_type: Opaque::new(bindings::config_item_type {
|
| 756 | ct_owner: owner.as_ptr(),
|
| 757 | ct_group_ops: GroupOperationsVTable::<Data, Child>::vtable_ptr().cast_mut(),
|
| 758 | ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr().cast_mut(),
|
| 759 | ct_attrs: core::ptr::from_ref(attributes).cast_mut().cast(),
|
| 760 | ct_bin_attrs: core::ptr::null_mut(),
|
| 761 | }),
|
| 762 | _p: PhantomData,
|
| 763 | }
|
| 764 | }
|
| 765 |
|
| 766 | #[doc(hidden)]
|
| 767 | pub const fn new<const N: usize>(
|
| 768 | owner: &'static ThisModule,
|
| 769 | attributes: &'static AttributeList<N, Data>,
|
| 770 | ) -> Self {
|
| 771 | Self {
|
| 772 | item_type: Opaque::new(bindings::config_item_type {
|
| 773 | ct_owner: owner.as_ptr(),
|
| 774 | ct_group_ops: core::ptr::null_mut(),
|
| 775 | ct_item_ops: ItemOperationsVTable::<$tpe, Data>::vtable_ptr().cast_mut(),
|
| 776 | ct_attrs: core::ptr::from_ref(attributes).cast_mut().cast(),
|
| 777 | ct_bin_attrs: core::ptr::null_mut(),
|
| 778 | }),
|
| 779 | _p: PhantomData,
|
| 780 | }
|
| 781 | }
|
| 782 | }
|
| 783 | };
|
| 784 | }
|
| 785 |
|
| 786 | impl_item_type!(Subsystem<Data>);
|
| 787 | impl_item_type!(Group<Data>);
|
| 788 |
|
| 789 | impl<Container, Data> ItemType<Container, Data> {
|
| 790 | fn as_ptr(&self) -> *const bindings::config_item_type {
|
| 791 | self.item_type.get()
|
| 792 | }
|
| 793 | }
|
| 794 |
|
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | |
| 881 | |
| 882 | |
| 883 | |
| 884 | |
| 885 | #[allow(clippy::crate_in_macro_def)]
|
| 886 | #[macro_export]
|
| 887 | macro_rules! configfs_attrs {
|
| 888 | (
|
| 889 | container: $container:ty,
|
| 890 | data: $data:ty,
|
| 891 | attributes: [
|
| 892 | $($name:ident: $attr:literal),* $(,)?
|
| 893 | ] $(,)?
|
| 894 | ) => {
|
| 895 | $crate::configfs_attrs!(
|
| 896 | count:
|
| 897 | @container($container),
|
| 898 | @data($data),
|
| 899 | @child(),
|
| 900 | @no_child(x),
|
| 901 | @attrs($($name $attr)*),
|
| 902 | @eat($($name $attr,)*),
|
| 903 | @assign(),
|
| 904 | @cnt(0usize),
|
| 905 | )
|
| 906 | };
|
| 907 | (
|
| 908 | container: $container:ty,
|
| 909 | data: $data:ty,
|
| 910 | child: $child:ty,
|
| 911 | attributes: [
|
| 912 | $($name:ident: $attr:literal),* $(,)?
|
| 913 | ] $(,)?
|
| 914 | ) => {
|
| 915 | $crate::configfs_attrs!(
|
| 916 | count:
|
| 917 | @container($container),
|
| 918 | @data($data),
|
| 919 | @child($child),
|
| 920 | @no_child(),
|
| 921 | @attrs($($name $attr)*),
|
| 922 | @eat($($name $attr,)*),
|
| 923 | @assign(),
|
| 924 | @cnt(0usize),
|
| 925 | )
|
| 926 | };
|
| 927 | (count:
|
| 928 | @container($container:ty),
|
| 929 | @data($data:ty),
|
| 930 | @child($($child:ty)?),
|
| 931 | @no_child($($no_child:ident)?),
|
| 932 | @attrs($($aname:ident $aattr:literal)*),
|
| 933 | @eat($name:ident $attr:literal, $($rname:ident $rattr:literal,)*),
|
| 934 | @assign($($assign:block)*),
|
| 935 | @cnt($cnt:expr),
|
| 936 | ) => {
|
| 937 | $crate::configfs_attrs!(
|
| 938 | count:
|
| 939 | @container($container),
|
| 940 | @data($data),
|
| 941 | @child($($child)?),
|
| 942 | @no_child($($no_child)?),
|
| 943 | @attrs($($aname $aattr)*),
|
| 944 | @eat($($rname $rattr,)*),
|
| 945 | @assign($($assign)* {
|
| 946 | const N: usize = $cnt;
|
| 947 | |
| 948 |
|
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | unsafe {
|
| 955 | $crate::macros::paste!(
|
| 956 | [< $data:upper _ATTRS >]
|
| 957 | .add::<N, $attr, _>(&[< $data:upper _ $name:upper _ATTR >])
|
| 958 | )
|
| 959 | };
|
| 960 | }),
|
| 961 | @cnt(1usize + $cnt),
|
| 962 | )
|
| 963 | };
|
| 964 | (count:
|
| 965 | @container($container:ty),
|
| 966 | @data($data:ty),
|
| 967 | @child($($child:ty)?),
|
| 968 | @no_child($($no_child:ident)?),
|
| 969 | @attrs($($aname:ident $aattr:literal)*),
|
| 970 | @eat(),
|
| 971 | @assign($($assign:block)*),
|
| 972 | @cnt($cnt:expr),
|
| 973 | ) =>
|
| 974 | {
|
| 975 | $crate::configfs_attrs!(
|
| 976 | final:
|
| 977 | @container($container),
|
| 978 | @data($data),
|
| 979 | @child($($child)?),
|
| 980 | @no_child($($no_child)?),
|
| 981 | @attrs($($aname $aattr)*),
|
| 982 | @assign($($assign)*),
|
| 983 | @cnt($cnt),
|
| 984 | )
|
| 985 | };
|
| 986 | (final:
|
| 987 | @container($container:ty),
|
| 988 | @data($data:ty),
|
| 989 | @child($($child:ty)?),
|
| 990 | @no_child($($no_child:ident)?),
|
| 991 | @attrs($($name:ident $attr:literal)*),
|
| 992 | @assign($($assign:block)*),
|
| 993 | @cnt($cnt:expr),
|
| 994 | ) =>
|
| 995 | {
|
| 996 | $crate::macros::paste!{
|
| 997 | {
|
| 998 | $(
|
| 999 | |
| 1000 | static [< $data:upper _ $name:upper _ATTR >]:
|
| 1001 | $crate::configfs::Attribute<$attr, $data, $data> =
|
| 1002 | unsafe {
|
| 1003 | $crate::configfs::Attribute::new(
|
| 1004 | $crate::c_str!(::core::stringify!($name)),
|
| 1005 | )
|
| 1006 | };
|
| 1007 | )*
|
| 1008 |
|
| 1009 |
|
| 1010 | |
| 1011 | const N: usize = $cnt + 1usize;
|
| 1012 |
|
| 1013 | |
| 1014 | static [< $data:upper _ATTRS >]:
|
| 1015 | $crate::configfs::AttributeList<N, $data> =
|
| 1016 | unsafe { $crate::configfs::AttributeList::new() };
|
| 1017 |
|
| 1018 | $($assign)*
|
| 1019 |
|
| 1020 | $(
|
| 1021 | const [<$no_child:upper>]: bool = true;
|
| 1022 |
|
| 1023 | static [< $data:upper _TPE >] : $crate::configfs::ItemType<$container, $data> =
|
| 1024 | $crate::configfs::ItemType::<$container, $data>::new::<N>(
|
| 1025 | $crate::module::this_module::<crate::LocalModule>(),
|
| 1026 | &[<$ data:upper _ATTRS >]
|
| 1027 | );
|
| 1028 | )?
|
| 1029 |
|
| 1030 | $(
|
| 1031 | static [< $data:upper _TPE >]:
|
| 1032 | $crate::configfs::ItemType<$container, $data> =
|
| 1033 | $crate::configfs::ItemType::<$container, $data>::
|
| 1034 | new_with_child_ctor::<N, $child>(
|
| 1035 | $crate::module::this_module::<crate::LocalModule>(),
|
| 1036 | &[<$ data:upper _ATTRS >]
|
| 1037 | );
|
| 1038 | )?
|
| 1039 |
|
| 1040 | & [< $data:upper _TPE >]
|
| 1041 | }
|
| 1042 | }
|
| 1043 | };
|
| 1044 |
|
| 1045 | }
|
| 1046 |
|
| 1047 | pub use crate::configfs_attrs;
|