1 /*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 6 * All rights reserved. 7 * Copyright (c) 2021-2025 The FreeBSD Foundation 8 * 9 * Portions of this software were developed by Björn Zeeb 10 * under sponsorship from the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice unmodified, this list of conditions, and the following 17 * disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 #ifndef _LINUXKPI_LINUX_DEVICE_H_ 34 #define _LINUXKPI_LINUX_DEVICE_H_ 35 36 #include <linux/err.h> 37 #include <linux/types.h> 38 #include <linux/kobject.h> 39 #include <linux/sysfs.h> 40 #include <linux/list.h> 41 #include <linux/compiler.h> 42 #include <linux/module.h> 43 #include <linux/workqueue.h> 44 #include <linux/kdev_t.h> 45 #include <linux/backlight.h> 46 #include <linux/pm.h> 47 #include <linux/idr.h> 48 #include <linux/overflow.h> 49 #include <linux/ratelimit.h> /* via linux/dev_printk.h */ 50 #include <linux/fwnode.h> 51 #include <linux/device/devres.h> 52 #include <asm/atomic.h> 53 54 #include <sys/bus.h> 55 #include <sys/backlight.h> 56 57 struct device; 58 59 struct class { 60 const char *name; 61 struct kobject kobj; 62 devclass_t bsdclass; 63 const struct dev_pm_ops *pm; 64 const struct attribute_group **dev_groups; 65 void (*class_release)(struct class *class); 66 void (*dev_release)(struct device *dev); 67 char * (*devnode)(struct device *dev, umode_t *mode); 68 }; 69 70 struct dev_pm_ops { 71 int (*prepare)(struct device *dev); 72 void (*complete)(struct device *dev); 73 int (*suspend)(struct device *dev); 74 int (*suspend_late)(struct device *dev); 75 int (*resume)(struct device *dev); 76 int (*resume_early)(struct device *dev); 77 int (*freeze)(struct device *dev); 78 int (*freeze_late)(struct device *dev); 79 int (*thaw)(struct device *dev); 80 int (*thaw_early)(struct device *dev); 81 int (*poweroff)(struct device *dev); 82 int (*poweroff_late)(struct device *dev); 83 int (*restore)(struct device *dev); 84 int (*restore_early)(struct device *dev); 85 int (*suspend_noirq)(struct device *dev); 86 int (*runtime_suspend)(struct device *dev); 87 int (*runtime_resume)(struct device *dev); 88 int (*runtime_idle)(struct device *dev); 89 }; 90 91 struct device_driver { 92 const char *name; 93 const struct dev_pm_ops *pm; 94 95 void (*shutdown) (struct device *); 96 void (*coredump) (struct device *); 97 }; 98 99 struct device_type { 100 const char *name; 101 }; 102 103 struct device { 104 struct device *parent; 105 struct list_head irqents; 106 device_t bsddev; 107 /* 108 * The following flag is used to determine if the LinuxKPI is 109 * responsible for detaching the BSD device or not. If the 110 * LinuxKPI got the BSD device using devclass_get_device(), it 111 * must not try to detach or delete it, because it's already 112 * done somewhere else. 113 */ 114 bool bsddev_attached_here; 115 struct device_driver *driver; 116 struct device_type *type; 117 dev_t devt; 118 struct class *class; 119 void (*release)(struct device *dev); 120 struct kobject kobj; 121 void *dma_priv; 122 void *driver_data; 123 unsigned int irq; 124 #define LINUX_IRQ_INVALID 65535 125 unsigned int irq_start; 126 unsigned int irq_end; 127 const struct attribute_group **groups; 128 struct fwnode_handle *fwnode; 129 struct cdev *backlight_dev; 130 struct backlight_device *bd; 131 132 spinlock_t devres_lock; 133 struct list_head devres_head; 134 135 struct dev_pm_info power; 136 }; 137 138 extern struct device linux_root_device; 139 extern struct kobject linux_class_root; 140 extern const struct kobj_type linux_dev_ktype; 141 extern const struct kobj_type linux_class_ktype; 142 143 struct class_attribute { 144 struct attribute attr; 145 ssize_t (*show)(struct class *, struct class_attribute *, char *); 146 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t); 147 const void *(*namespace)(struct class *, const struct class_attribute *); 148 }; 149 150 #define CLASS_ATTR(_name, _mode, _show, _store) \ 151 struct class_attribute class_attr_##_name = \ 152 { { #_name, NULL, _mode }, _show, _store } 153 154 struct device_attribute { 155 struct attribute attr; 156 ssize_t (*show)(struct device *, 157 struct device_attribute *, char *); 158 ssize_t (*store)(struct device *, 159 struct device_attribute *, const char *, 160 size_t); 161 }; 162 163 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 164 struct device_attribute dev_attr_##_name = \ 165 __ATTR(_name, _mode, _show, _store) 166 #define DEVICE_ATTR_RO(_name) \ 167 struct device_attribute dev_attr_##_name = __ATTR_RO(_name) 168 #define DEVICE_ATTR_WO(_name) \ 169 struct device_attribute dev_attr_##_name = __ATTR_WO(_name) 170 #define DEVICE_ATTR_RW(_name) \ 171 struct device_attribute dev_attr_##_name = __ATTR_RW(_name) 172 173 /* Simple class attribute that is just a static string */ 174 struct class_attribute_string { 175 struct class_attribute attr; 176 char *str; 177 }; 178 179 static inline ssize_t 180 show_class_attr_string(struct class *class, 181 struct class_attribute *attr, char *buf) 182 { 183 struct class_attribute_string *cs; 184 cs = container_of(attr, struct class_attribute_string, attr); 185 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str); 186 } 187 188 /* Currently read-only only */ 189 #define _CLASS_ATTR_STRING(_name, _mode, _str) \ 190 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 191 #define CLASS_ATTR_STRING(_name, _mode, _str) \ 192 struct class_attribute_string class_attr_##_name = \ 193 _CLASS_ATTR_STRING(_name, _mode, _str) 194 195 #define dev_printk(lvl, dev, fmt, ...) \ 196 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 197 198 #define dev_emerg(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 199 #define dev_alert(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 200 #define dev_crit(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 201 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 202 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 203 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 204 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 205 #define dev_dbg(dev, fmt, ...) do { } while (0) 206 207 #define dev_WARN(dev, fmt, ...) \ 208 device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__) 209 210 #define dev_WARN_ONCE(dev, condition, fmt, ...) do { \ 211 static bool __dev_WARN_ONCE; \ 212 bool __ret_warn_on = (condition); \ 213 if (unlikely(__ret_warn_on)) { \ 214 if (!__dev_WARN_ONCE) { \ 215 __dev_WARN_ONCE = true; \ 216 device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); \ 217 } \ 218 } \ 219 } while (0) 220 221 #define dev_info_once(dev, ...) do { \ 222 static bool __dev_info_once; \ 223 if (!__dev_info_once) { \ 224 __dev_info_once = true; \ 225 dev_info(dev, __VA_ARGS__); \ 226 } \ 227 } while (0) 228 229 #define dev_warn_once(dev, ...) do { \ 230 static bool __dev_warn_once; \ 231 if (!__dev_warn_once) { \ 232 __dev_warn_once = 1; \ 233 dev_warn(dev, __VA_ARGS__); \ 234 } \ 235 } while (0) 236 237 #define dev_err_once(dev, ...) do { \ 238 static bool __dev_err_once; \ 239 if (!__dev_err_once) { \ 240 __dev_err_once = 1; \ 241 dev_err(dev, __VA_ARGS__); \ 242 } \ 243 } while (0) 244 245 #define dev_dbg_once(dev, ...) do { \ 246 static bool __dev_dbg_once; \ 247 if (!__dev_dbg_once) { \ 248 __dev_dbg_once = 1; \ 249 dev_dbg(dev, __VA_ARGS__); \ 250 } \ 251 } while (0) 252 253 #define dev_err_ratelimited(dev, ...) do { \ 254 static linux_ratelimit_t __ratelimited; \ 255 if (linux_ratelimited(&__ratelimited)) \ 256 dev_err(dev, __VA_ARGS__); \ 257 } while (0) 258 259 #define dev_warn_ratelimited(dev, ...) do { \ 260 static linux_ratelimit_t __ratelimited; \ 261 if (linux_ratelimited(&__ratelimited)) \ 262 dev_warn(dev, __VA_ARGS__); \ 263 } while (0) 264 265 #define dev_dbg_ratelimited(dev, ...) do { \ 266 static linux_ratelimit_t __ratelimited; \ 267 if (linux_ratelimited(&__ratelimited)) \ 268 dev_dbg(dev, __VA_ARGS__); \ 269 } while (0) 270 271 static inline int 272 dev_err_probe(const struct device *dev, int err, const char *fmt, ...) 273 { 274 va_list args; 275 276 va_start(args, fmt); 277 278 /* 279 * On Linux, they look at the error code to determine if the message 280 * should be logged (not logged if -ENOMEM) and at which log level. 281 */ 282 device_printf(dev->bsddev, fmt, args); 283 284 va_end(args); 285 286 return (err); 287 } 288 289 #define dev_err_ptr_probe(dev, err, fmt, ...) \ 290 ERR_PTR(dev_err_probe((dev), (err), fmt, ##__VA_ARGS__) 291 292 #define dev_err_cast_probe(dev, err, fmt, ...) \ 293 ERR_PTR(dev_err_probe((dev), PTR_ERR(err), fmt, ##__VA_ARGS__) 294 295 void lkpi_devres_release_free_list(struct device *); 296 void lkpi_devres_unlink(struct device *, void *); 297 298 static inline const char * 299 dev_driver_string(const struct device *dev) 300 { 301 driver_t *drv; 302 const char *str = ""; 303 304 if (dev->bsddev != NULL) { 305 drv = device_get_driver(dev->bsddev); 306 if (drv != NULL) 307 str = drv->name; 308 } 309 310 return (str); 311 } 312 313 static inline void * 314 dev_get_drvdata(const struct device *dev) 315 { 316 317 return dev->driver_data; 318 } 319 320 static inline void 321 dev_set_drvdata(struct device *dev, void *data) 322 { 323 324 dev->driver_data = data; 325 } 326 327 static inline struct device * 328 get_device(struct device *dev) 329 { 330 331 if (dev) 332 kobject_get(&dev->kobj); 333 334 return (dev); 335 } 336 337 static inline char * 338 dev_name(const struct device *dev) 339 { 340 341 return kobject_name(&dev->kobj); 342 } 343 344 static inline bool 345 dev_is_removable(struct device *dev) 346 { 347 348 return (false); 349 } 350 351 #define dev_set_name(_dev, _fmt, ...) \ 352 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__) 353 354 static inline void 355 put_device(struct device *dev) 356 { 357 358 if (dev) 359 kobject_put(&dev->kobj); 360 } 361 362 struct class *lkpi_class_create(const char *name); 363 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 60400 364 #define class_create(name) lkpi_class_create(name) 365 #else 366 #define class_create(owner, name) lkpi_class_create(name) 367 #endif 368 369 static inline int 370 class_register(struct class *class) 371 { 372 373 class->bsdclass = devclass_create(class->name); 374 kobject_init(&class->kobj, &linux_class_ktype); 375 kobject_set_name(&class->kobj, class->name); 376 kobject_add(&class->kobj, &linux_class_root, class->name); 377 378 return (0); 379 } 380 381 static inline void 382 class_unregister(struct class *class) 383 { 384 385 kobject_put(&class->kobj); 386 } 387 388 static inline struct device *kobj_to_dev(struct kobject *kobj) 389 { 390 return container_of(kobj, struct device, kobj); 391 } 392 393 struct device *device_create(struct class *class, struct device *parent, 394 dev_t devt, void *drvdata, const char *fmt, ...); 395 struct device *device_create_groups_vargs(struct class *class, struct device *parent, 396 dev_t devt, void *drvdata, const struct attribute_group **groups, 397 const char *fmt, va_list args); 398 399 /* 400 * Devices are registered and created for exporting to sysfs. Create 401 * implies register and register assumes the device fields have been 402 * setup appropriately before being called. 403 */ 404 static inline void 405 device_initialize(struct device *dev) 406 { 407 device_t bsddev = NULL; 408 int unit = -1; 409 410 if (dev->devt) { 411 unit = MINOR(dev->devt); 412 bsddev = devclass_get_device(dev->class->bsdclass, unit); 413 dev->bsddev_attached_here = false; 414 } else if (dev->parent == NULL) { 415 bsddev = devclass_get_device(dev->class->bsdclass, 0); 416 dev->bsddev_attached_here = false; 417 } else { 418 dev->bsddev_attached_here = true; 419 } 420 421 if (bsddev == NULL && dev->parent != NULL) { 422 bsddev = device_add_child(dev->parent->bsddev, 423 dev->class->kobj.name, unit); 424 } 425 426 if (bsddev != NULL) 427 device_set_softc(bsddev, dev); 428 429 dev->bsddev = bsddev; 430 MPASS(dev->bsddev != NULL); 431 kobject_init(&dev->kobj, &linux_dev_ktype); 432 433 spin_lock_init(&dev->devres_lock); 434 INIT_LIST_HEAD(&dev->devres_head); 435 } 436 437 static inline int 438 device_add(struct device *dev) 439 { 440 if (dev->bsddev != NULL) { 441 if (dev->devt == 0) 442 dev->devt = makedev(0, device_get_unit(dev->bsddev)); 443 } 444 445 /* 446 * Do not change: 447 * the specific location is used by IB user space tools (PR298485). 448 */ 449 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 450 451 if (dev->groups) 452 return (sysfs_create_groups(&dev->kobj, dev->groups)); 453 454 return (0); 455 } 456 457 static inline void 458 device_create_release(struct device *dev) 459 { 460 kfree(dev); 461 } 462 463 static inline struct device * 464 device_create_with_groups(struct class *class, 465 struct device *parent, dev_t devt, void *drvdata, 466 const struct attribute_group **groups, const char *fmt, ...) 467 { 468 va_list vargs; 469 struct device *dev; 470 471 va_start(vargs, fmt); 472 dev = device_create_groups_vargs(class, parent, devt, drvdata, 473 groups, fmt, vargs); 474 va_end(vargs); 475 return dev; 476 } 477 478 static inline bool 479 device_is_registered(struct device *dev) 480 { 481 482 return (dev->bsddev != NULL); 483 } 484 485 static inline int 486 device_register(struct device *dev) 487 { 488 device_t bsddev = NULL; 489 int unit = -1; 490 491 if (device_is_registered(dev)) 492 goto done; 493 494 if (dev->devt) { 495 unit = MINOR(dev->devt); 496 bsddev = devclass_get_device(dev->class->bsdclass, unit); 497 dev->bsddev_attached_here = false; 498 } else if (dev->parent == NULL) { 499 bsddev = devclass_get_device(dev->class->bsdclass, 0); 500 dev->bsddev_attached_here = false; 501 } else { 502 dev->bsddev_attached_here = true; 503 } 504 if (bsddev == NULL && dev->parent != NULL) { 505 bsddev = device_add_child(dev->parent->bsddev, 506 dev->class->kobj.name, unit); 507 } 508 if (bsddev != NULL) { 509 if (dev->devt == 0) 510 dev->devt = makedev(0, device_get_unit(bsddev)); 511 device_set_softc(bsddev, dev); 512 } 513 dev->bsddev = bsddev; 514 done: 515 kobject_init(&dev->kobj, &linux_dev_ktype); 516 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 517 518 sysfs_create_groups(&dev->kobj, dev->class->dev_groups); 519 520 return (0); 521 } 522 523 static inline void 524 device_unregister(struct device *dev) 525 { 526 device_t bsddev; 527 528 sysfs_remove_groups(&dev->kobj, dev->class->dev_groups); 529 530 bsddev = dev->bsddev; 531 dev->bsddev = NULL; 532 533 if (bsddev != NULL && dev->bsddev_attached_here) { 534 bus_topo_lock(); 535 device_delete_child(device_get_parent(bsddev), bsddev); 536 bus_topo_unlock(); 537 } 538 put_device(dev); 539 } 540 541 static inline void 542 device_del(struct device *dev) 543 { 544 device_t bsddev; 545 546 bsddev = dev->bsddev; 547 dev->bsddev = NULL; 548 549 if (bsddev != NULL && dev->bsddev_attached_here) { 550 bus_topo_lock(); 551 device_delete_child(device_get_parent(bsddev), bsddev); 552 bus_topo_unlock(); 553 } 554 } 555 556 static inline void 557 device_destroy(struct class *class, dev_t devt) 558 { 559 device_t bsddev; 560 int unit; 561 562 unit = MINOR(devt); 563 bsddev = devclass_get_device(class->bsdclass, unit); 564 if (bsddev != NULL) 565 device_unregister(device_get_softc(bsddev)); 566 } 567 568 static inline void 569 device_release_driver(struct device *dev) 570 { 571 572 pr_debug("%s: TODO\n", __func__); 573 #if 0 574 /* This leads to panics. Disable temporarily. Keep to rework. */ 575 576 /* We also need to cleanup LinuxKPI bits. What else? */ 577 lkpi_devres_release_free_list(dev); 578 dev_set_drvdata(dev, NULL); 579 /* Do not call dev->release! */ 580 581 bus_topo_lock(); 582 if (device_is_attached(dev->bsddev)) 583 device_detach(dev->bsddev); 584 bus_topo_unlock(); 585 #endif 586 } 587 588 static inline int 589 device_reprobe(struct device *dev) 590 { 591 int error; 592 593 device_release_driver(dev); 594 bus_topo_lock(); 595 error = device_probe_and_attach(dev->bsddev); 596 bus_topo_unlock(); 597 598 return (-error); 599 } 600 601 static inline void 602 device_set_wakeup_enable(struct device *dev __unused, bool enable __unused) 603 { 604 605 /* 606 * XXX-BZ TODO This is used by wireless drivers supporting WoWLAN which 607 * we currently do not support. 608 */ 609 } 610 611 static inline int 612 device_wakeup_enable(struct device *dev) 613 { 614 615 device_set_wakeup_enable(dev, true); 616 return (0); 617 } 618 619 static inline bool 620 device_iommu_mapped(struct device *dev __unused) 621 { 622 return (false); 623 } 624 625 #define dev_pm_set_driver_flags(dev, flags) do { \ 626 } while (0) 627 628 static inline void 629 linux_class_kfree(struct class *class) 630 { 631 632 kfree(class); 633 } 634 635 static inline void 636 class_destroy(struct class *class) 637 { 638 639 if (class == NULL) 640 return; 641 class_unregister(class); 642 } 643 644 static inline int 645 device_create_file(struct device *dev, const struct device_attribute *attr) 646 { 647 648 if (dev) 649 return sysfs_create_file(&dev->kobj, &attr->attr); 650 return -EINVAL; 651 } 652 653 static inline void 654 device_remove_file(struct device *dev, const struct device_attribute *attr) 655 { 656 657 if (dev) 658 sysfs_remove_file(&dev->kobj, &attr->attr); 659 } 660 661 static inline int 662 class_create_file(struct class *class, const struct class_attribute *attr) 663 { 664 665 if (class) 666 return sysfs_create_file(&class->kobj, &attr->attr); 667 return -EINVAL; 668 } 669 670 static inline void 671 class_remove_file(struct class *class, const struct class_attribute *attr) 672 { 673 674 if (class) 675 sysfs_remove_file(&class->kobj, &attr->attr); 676 } 677 678 #define dev_to_node(dev) linux_dev_to_node(dev) 679 #define of_node_to_nid(node) -1 680 int linux_dev_to_node(struct device *); 681 682 char *kvasprintf(gfp_t, const char *, va_list); 683 char *kasprintf(gfp_t, const char *, ...); 684 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...); 685 686 #define devm_kasprintf(_dev, _gfp, _fmt, ...) \ 687 lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__) 688 689 #define devm_kzalloc(_dev, _size, _gfp) \ 690 devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO) 691 692 #define devm_kcalloc(_dev, _sizen, _size, _gfp) \ 693 devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO) 694 695 int lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data); 696 #define devm_add_action(dev, action, data) \ 697 lkpi_devm_add_action(dev, action, data); 698 int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data); 699 #define devm_add_action_or_reset(dev, action, data) \ 700 lkpi_devm_add_action_or_reset(dev, action, data) 701 702 int lkpi_devm_device_add_group(struct device *dev, const struct attribute_group *group); 703 #define devm_device_add_group(dev, group) \ 704 lkpi_devm_device_add_group(dev, group) 705 706 #endif /* _LINUXKPI_LINUX_DEVICE_H_ */ 707