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-2022 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 <asm/atomic.h> 52 53 #include <sys/bus.h> 54 #include <sys/backlight.h> 55 56 struct device; 57 58 struct class { 59 const char *name; 60 struct module *owner; 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 96 struct device_type { 97 const char *name; 98 }; 99 100 struct device { 101 struct device *parent; 102 struct list_head irqents; 103 device_t bsddev; 104 /* 105 * The following flag is used to determine if the LinuxKPI is 106 * responsible for detaching the BSD device or not. If the 107 * LinuxKPI got the BSD device using devclass_get_device(), it 108 * must not try to detach or delete it, because it's already 109 * done somewhere else. 110 */ 111 bool bsddev_attached_here; 112 struct device_driver *driver; 113 struct device_type *type; 114 dev_t devt; 115 struct class *class; 116 void (*release)(struct device *dev); 117 struct kobject kobj; 118 void *dma_priv; 119 void *driver_data; 120 unsigned int irq; 121 #define LINUX_IRQ_INVALID 65535 122 unsigned int irq_start; 123 unsigned int irq_end; 124 const struct attribute_group **groups; 125 struct fwnode_handle *fwnode; 126 struct cdev *backlight_dev; 127 struct backlight_device *bd; 128 129 spinlock_t devres_lock; 130 struct list_head devres_head; 131 132 struct dev_pm_info power; 133 }; 134 135 extern struct device linux_root_device; 136 extern struct kobject linux_class_root; 137 extern const struct kobj_type linux_dev_ktype; 138 extern const struct kobj_type linux_class_ktype; 139 140 struct class_attribute { 141 struct attribute attr; 142 ssize_t (*show)(struct class *, struct class_attribute *, char *); 143 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t); 144 const void *(*namespace)(struct class *, const struct class_attribute *); 145 }; 146 147 #define CLASS_ATTR(_name, _mode, _show, _store) \ 148 struct class_attribute class_attr_##_name = \ 149 { { #_name, NULL, _mode }, _show, _store } 150 151 struct device_attribute { 152 struct attribute attr; 153 ssize_t (*show)(struct device *, 154 struct device_attribute *, char *); 155 ssize_t (*store)(struct device *, 156 struct device_attribute *, const char *, 157 size_t); 158 }; 159 160 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 161 struct device_attribute dev_attr_##_name = \ 162 __ATTR(_name, _mode, _show, _store) 163 #define DEVICE_ATTR_RO(_name) \ 164 struct device_attribute dev_attr_##_name = __ATTR_RO(_name) 165 #define DEVICE_ATTR_WO(_name) \ 166 struct device_attribute dev_attr_##_name = __ATTR_WO(_name) 167 #define DEVICE_ATTR_RW(_name) \ 168 struct device_attribute dev_attr_##_name = __ATTR_RW(_name) 169 170 /* Simple class attribute that is just a static string */ 171 struct class_attribute_string { 172 struct class_attribute attr; 173 char *str; 174 }; 175 176 static inline ssize_t 177 show_class_attr_string(struct class *class, 178 struct class_attribute *attr, char *buf) 179 { 180 struct class_attribute_string *cs; 181 cs = container_of(attr, struct class_attribute_string, attr); 182 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str); 183 } 184 185 /* Currently read-only only */ 186 #define _CLASS_ATTR_STRING(_name, _mode, _str) \ 187 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 188 #define CLASS_ATTR_STRING(_name, _mode, _str) \ 189 struct class_attribute_string class_attr_##_name = \ 190 _CLASS_ATTR_STRING(_name, _mode, _str) 191 192 #define dev_printk(lvl, dev, fmt, ...) \ 193 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 194 195 #define dev_emerg(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 196 #define dev_alert(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 197 #define dev_crit(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 198 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 199 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 200 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 201 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 202 #define dev_dbg(dev, fmt, ...) do { } while (0) 203 204 #define dev_WARN(dev, fmt, ...) \ 205 device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__) 206 207 #define dev_WARN_ONCE(dev, condition, fmt, ...) do { \ 208 static bool __dev_WARN_ONCE; \ 209 bool __ret_warn_on = (condition); \ 210 if (unlikely(__ret_warn_on)) { \ 211 if (!__dev_WARN_ONCE) { \ 212 __dev_WARN_ONCE = true; \ 213 device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); \ 214 } \ 215 } \ 216 } while (0) 217 218 #define dev_info_once(dev, ...) do { \ 219 static bool __dev_info_once; \ 220 if (!__dev_info_once) { \ 221 __dev_info_once = true; \ 222 dev_info(dev, __VA_ARGS__); \ 223 } \ 224 } while (0) 225 226 #define dev_warn_once(dev, ...) do { \ 227 static bool __dev_warn_once; \ 228 if (!__dev_warn_once) { \ 229 __dev_warn_once = 1; \ 230 dev_warn(dev, __VA_ARGS__); \ 231 } \ 232 } while (0) 233 234 #define dev_err_once(dev, ...) do { \ 235 static bool __dev_err_once; \ 236 if (!__dev_err_once) { \ 237 __dev_err_once = 1; \ 238 dev_err(dev, __VA_ARGS__); \ 239 } \ 240 } while (0) 241 242 #define dev_dbg_once(dev, ...) do { \ 243 static bool __dev_dbg_once; \ 244 if (!__dev_dbg_once) { \ 245 __dev_dbg_once = 1; \ 246 dev_dbg(dev, __VA_ARGS__); \ 247 } \ 248 } while (0) 249 250 #define dev_err_ratelimited(dev, ...) do { \ 251 static linux_ratelimit_t __ratelimited; \ 252 if (linux_ratelimited(&__ratelimited)) \ 253 dev_err(dev, __VA_ARGS__); \ 254 } while (0) 255 256 #define dev_warn_ratelimited(dev, ...) do { \ 257 static linux_ratelimit_t __ratelimited; \ 258 if (linux_ratelimited(&__ratelimited)) \ 259 dev_warn(dev, __VA_ARGS__); \ 260 } while (0) 261 262 #define dev_dbg_ratelimited(dev, ...) do { \ 263 static linux_ratelimit_t __ratelimited; \ 264 if (linux_ratelimited(&__ratelimited)) \ 265 dev_dbg(dev, __VA_ARGS__); \ 266 } while (0) 267 268 /* Public and LinuxKPI internal devres functions. */ 269 void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t); 270 void lkpi_devres_add(struct device *, void *); 271 void lkpi_devres_free(void *); 272 void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *), 273 int (*match)(struct device *, void *, void *), void *); 274 int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *), 275 int (*match)(struct device *, void *, void *), void *); 276 #define devres_alloc(_r, _s, _g) lkpi_devres_alloc(_r, _s, _g) 277 #define devres_add(_d, _p) lkpi_devres_add(_d, _p) 278 #define devres_free(_p) lkpi_devres_free(_p) 279 #define devres_find(_d, _rfn, _mfn, _mp) \ 280 lkpi_devres_find(_d, _rfn, _mfn, _mp) 281 #define devres_destroy(_d, _rfn, _mfn, _mp) \ 282 lkpi_devres_destroy(_d, _rfn, _mfn, _mp) 283 void lkpi_devres_release_free_list(struct device *); 284 void lkpi_devres_unlink(struct device *, void *); 285 void lkpi_devm_kmalloc_release(struct device *, void *); 286 287 static inline const char * 288 dev_driver_string(const struct device *dev) 289 { 290 driver_t *drv; 291 const char *str = ""; 292 293 if (dev->bsddev != NULL) { 294 drv = device_get_driver(dev->bsddev); 295 if (drv != NULL) 296 str = drv->name; 297 } 298 299 return (str); 300 } 301 302 static inline void * 303 dev_get_drvdata(const struct device *dev) 304 { 305 306 return dev->driver_data; 307 } 308 309 static inline void 310 dev_set_drvdata(struct device *dev, void *data) 311 { 312 313 dev->driver_data = data; 314 } 315 316 static inline struct device * 317 get_device(struct device *dev) 318 { 319 320 if (dev) 321 kobject_get(&dev->kobj); 322 323 return (dev); 324 } 325 326 static inline char * 327 dev_name(const struct device *dev) 328 { 329 330 return kobject_name(&dev->kobj); 331 } 332 333 #define dev_set_name(_dev, _fmt, ...) \ 334 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__) 335 336 static inline void 337 put_device(struct device *dev) 338 { 339 340 if (dev) 341 kobject_put(&dev->kobj); 342 } 343 344 struct class *class_create(struct module *owner, const char *name); 345 346 static inline int 347 class_register(struct class *class) 348 { 349 350 class->bsdclass = devclass_create(class->name); 351 kobject_init(&class->kobj, &linux_class_ktype); 352 kobject_set_name(&class->kobj, class->name); 353 kobject_add(&class->kobj, &linux_class_root, class->name); 354 355 return (0); 356 } 357 358 static inline void 359 class_unregister(struct class *class) 360 { 361 362 kobject_put(&class->kobj); 363 } 364 365 static inline struct device *kobj_to_dev(struct kobject *kobj) 366 { 367 return container_of(kobj, struct device, kobj); 368 } 369 370 struct device *device_create(struct class *class, struct device *parent, 371 dev_t devt, void *drvdata, const char *fmt, ...); 372 struct device *device_create_groups_vargs(struct class *class, struct device *parent, 373 dev_t devt, void *drvdata, const struct attribute_group **groups, 374 const char *fmt, va_list args); 375 376 /* 377 * Devices are registered and created for exporting to sysfs. Create 378 * implies register and register assumes the device fields have been 379 * setup appropriately before being called. 380 */ 381 static inline void 382 device_initialize(struct device *dev) 383 { 384 device_t bsddev = NULL; 385 int unit = -1; 386 387 if (dev->devt) { 388 unit = MINOR(dev->devt); 389 bsddev = devclass_get_device(dev->class->bsdclass, unit); 390 dev->bsddev_attached_here = false; 391 } else if (dev->parent == NULL) { 392 bsddev = devclass_get_device(dev->class->bsdclass, 0); 393 dev->bsddev_attached_here = false; 394 } else { 395 dev->bsddev_attached_here = true; 396 } 397 398 if (bsddev == NULL && dev->parent != NULL) { 399 bsddev = device_add_child(dev->parent->bsddev, 400 dev->class->kobj.name, unit); 401 } 402 403 if (bsddev != NULL) 404 device_set_softc(bsddev, dev); 405 406 dev->bsddev = bsddev; 407 MPASS(dev->bsddev != NULL); 408 kobject_init(&dev->kobj, &linux_dev_ktype); 409 410 spin_lock_init(&dev->devres_lock); 411 INIT_LIST_HEAD(&dev->devres_head); 412 } 413 414 static inline int 415 device_add(struct device *dev) 416 { 417 if (dev->bsddev != NULL) { 418 if (dev->devt == 0) 419 dev->devt = makedev(0, device_get_unit(dev->bsddev)); 420 } 421 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 422 423 if (dev->groups) 424 return (sysfs_create_groups(&dev->kobj, dev->groups)); 425 426 return (0); 427 } 428 429 static inline void 430 device_create_release(struct device *dev) 431 { 432 kfree(dev); 433 } 434 435 static inline struct device * 436 device_create_with_groups(struct class *class, 437 struct device *parent, dev_t devt, void *drvdata, 438 const struct attribute_group **groups, const char *fmt, ...) 439 { 440 va_list vargs; 441 struct device *dev; 442 443 va_start(vargs, fmt); 444 dev = device_create_groups_vargs(class, parent, devt, drvdata, 445 groups, fmt, vargs); 446 va_end(vargs); 447 return dev; 448 } 449 450 static inline bool 451 device_is_registered(struct device *dev) 452 { 453 454 return (dev->bsddev != NULL); 455 } 456 457 static inline int 458 device_register(struct device *dev) 459 { 460 device_t bsddev = NULL; 461 int unit = -1; 462 463 if (device_is_registered(dev)) 464 goto done; 465 466 if (dev->devt) { 467 unit = MINOR(dev->devt); 468 bsddev = devclass_get_device(dev->class->bsdclass, unit); 469 dev->bsddev_attached_here = false; 470 } else if (dev->parent == NULL) { 471 bsddev = devclass_get_device(dev->class->bsdclass, 0); 472 dev->bsddev_attached_here = false; 473 } else { 474 dev->bsddev_attached_here = true; 475 } 476 if (bsddev == NULL && dev->parent != NULL) { 477 bsddev = device_add_child(dev->parent->bsddev, 478 dev->class->kobj.name, unit); 479 } 480 if (bsddev != NULL) { 481 if (dev->devt == 0) 482 dev->devt = makedev(0, device_get_unit(bsddev)); 483 device_set_softc(bsddev, dev); 484 } 485 dev->bsddev = bsddev; 486 done: 487 kobject_init(&dev->kobj, &linux_dev_ktype); 488 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 489 490 sysfs_create_groups(&dev->kobj, dev->class->dev_groups); 491 492 return (0); 493 } 494 495 static inline void 496 device_unregister(struct device *dev) 497 { 498 device_t bsddev; 499 500 sysfs_remove_groups(&dev->kobj, dev->class->dev_groups); 501 502 bsddev = dev->bsddev; 503 dev->bsddev = NULL; 504 505 if (bsddev != NULL && dev->bsddev_attached_here) { 506 bus_topo_lock(); 507 device_delete_child(device_get_parent(bsddev), bsddev); 508 bus_topo_unlock(); 509 } 510 put_device(dev); 511 } 512 513 static inline void 514 device_del(struct device *dev) 515 { 516 device_t bsddev; 517 518 bsddev = dev->bsddev; 519 dev->bsddev = NULL; 520 521 if (bsddev != NULL && dev->bsddev_attached_here) { 522 bus_topo_lock(); 523 device_delete_child(device_get_parent(bsddev), bsddev); 524 bus_topo_unlock(); 525 } 526 } 527 528 static inline void 529 device_destroy(struct class *class, dev_t devt) 530 { 531 device_t bsddev; 532 int unit; 533 534 unit = MINOR(devt); 535 bsddev = devclass_get_device(class->bsdclass, unit); 536 if (bsddev != NULL) 537 device_unregister(device_get_softc(bsddev)); 538 } 539 540 static inline void 541 device_release_driver(struct device *dev) 542 { 543 544 #if 0 545 /* This leads to panics. Disable temporarily. Keep to rework. */ 546 547 /* We also need to cleanup LinuxKPI bits. What else? */ 548 lkpi_devres_release_free_list(dev); 549 dev_set_drvdata(dev, NULL); 550 /* Do not call dev->release! */ 551 552 bus_topo_lock(); 553 if (device_is_attached(dev->bsddev)) 554 device_detach(dev->bsddev); 555 bus_topo_unlock(); 556 #endif 557 } 558 559 static inline int 560 device_reprobe(struct device *dev) 561 { 562 int error; 563 564 device_release_driver(dev); 565 bus_topo_lock(); 566 error = device_probe_and_attach(dev->bsddev); 567 bus_topo_unlock(); 568 569 return (-error); 570 } 571 572 static inline void 573 device_set_wakeup_enable(struct device *dev __unused, bool enable __unused) 574 { 575 576 /* 577 * XXX-BZ TODO This is used by wireless drivers supporting WoWLAN which 578 * we currently do not support. 579 */ 580 } 581 582 static inline int 583 device_wakeup_enable(struct device *dev) 584 { 585 586 device_set_wakeup_enable(dev, true); 587 return (0); 588 } 589 590 static inline bool 591 device_iommu_mapped(struct device *dev __unused) 592 { 593 return (false); 594 } 595 596 #define dev_pm_set_driver_flags(dev, flags) do { \ 597 } while (0) 598 599 static inline void 600 linux_class_kfree(struct class *class) 601 { 602 603 kfree(class); 604 } 605 606 static inline void 607 class_destroy(struct class *class) 608 { 609 610 if (class == NULL) 611 return; 612 class_unregister(class); 613 } 614 615 static inline int 616 device_create_file(struct device *dev, const struct device_attribute *attr) 617 { 618 619 if (dev) 620 return sysfs_create_file(&dev->kobj, &attr->attr); 621 return -EINVAL; 622 } 623 624 static inline void 625 device_remove_file(struct device *dev, const struct device_attribute *attr) 626 { 627 628 if (dev) 629 sysfs_remove_file(&dev->kobj, &attr->attr); 630 } 631 632 static inline int 633 class_create_file(struct class *class, const struct class_attribute *attr) 634 { 635 636 if (class) 637 return sysfs_create_file(&class->kobj, &attr->attr); 638 return -EINVAL; 639 } 640 641 static inline void 642 class_remove_file(struct class *class, const struct class_attribute *attr) 643 { 644 645 if (class) 646 sysfs_remove_file(&class->kobj, &attr->attr); 647 } 648 649 #define dev_to_node(dev) linux_dev_to_node(dev) 650 #define of_node_to_nid(node) -1 651 int linux_dev_to_node(struct device *); 652 653 char *kvasprintf(gfp_t, const char *, va_list); 654 char *kasprintf(gfp_t, const char *, ...); 655 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...); 656 657 #define devm_kasprintf(_dev, _gfp, _fmt, ...) \ 658 lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__) 659 660 static __inline void * 661 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) 662 { 663 void *p; 664 665 p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp); 666 if (p != NULL) 667 lkpi_devres_add(dev, p); 668 669 return (p); 670 } 671 672 static inline void * 673 devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) 674 { 675 void *dst; 676 677 if (len == 0) 678 return (NULL); 679 680 dst = devm_kmalloc(dev, len, gfp); 681 if (dst != NULL) 682 memcpy(dst, src, len); 683 684 return (dst); 685 } 686 687 #define devm_kzalloc(_dev, _size, _gfp) \ 688 devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO) 689 690 #define devm_kcalloc(_dev, _sizen, _size, _gfp) \ 691 devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO) 692 693 int lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data); 694 #define devm_add_action(dev, action, data) \ 695 lkpi_devm_add_action(dev, action, data); 696 int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data); 697 #define devm_add_action_or_reset(dev, action, data) \ 698 lkpi_devm_add_action_or_reset(dev, action, data) 699 700 #endif /* _LINUXKPI_LINUX_DEVICE_H_ */ 701