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