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_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 191 #define dev_crit(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 192 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 193 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 194 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 195 #define dev_emerg(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 196 #define dev_dbg(dev, fmt, ...) do { } while (0) 197 #define dev_printk(lvl, dev, fmt, ...) \ 198 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 199 200 #define dev_WARN(dev, fmt, ...) \ 201 device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__) 202 203 #define dev_WARN_ONCE(dev, condition, fmt, ...) do { \ 204 static bool __dev_WARN_ONCE; \ 205 bool __ret_warn_on = (condition); \ 206 if (unlikely(__ret_warn_on)) { \ 207 if (!__dev_WARN_ONCE) { \ 208 __dev_WARN_ONCE = true; \ 209 device_printf((dev)->bsddev, "%s:%d: " fmt, __func__, __LINE__, ##__VA_ARGS__); \ 210 } \ 211 } \ 212 } while (0) 213 214 #define dev_info_once(dev, ...) do { \ 215 static bool __dev_info_once; \ 216 if (!__dev_info_once) { \ 217 __dev_info_once = true; \ 218 dev_info(dev, __VA_ARGS__); \ 219 } \ 220 } while (0) 221 222 #define dev_warn_once(dev, ...) do { \ 223 static bool __dev_warn_once; \ 224 if (!__dev_warn_once) { \ 225 __dev_warn_once = 1; \ 226 dev_warn(dev, __VA_ARGS__); \ 227 } \ 228 } while (0) 229 230 #define dev_err_once(dev, ...) do { \ 231 static bool __dev_err_once; \ 232 if (!__dev_err_once) { \ 233 __dev_err_once = 1; \ 234 dev_err(dev, __VA_ARGS__); \ 235 } \ 236 } while (0) 237 238 #define dev_err_ratelimited(dev, ...) do { \ 239 static linux_ratelimit_t __ratelimited; \ 240 if (linux_ratelimited(&__ratelimited)) \ 241 dev_err(dev, __VA_ARGS__); \ 242 } while (0) 243 244 #define dev_warn_ratelimited(dev, ...) do { \ 245 static linux_ratelimit_t __ratelimited; \ 246 if (linux_ratelimited(&__ratelimited)) \ 247 dev_warn(dev, __VA_ARGS__); \ 248 } while (0) 249 250 #define dev_dbg_ratelimited(dev, ...) do { \ 251 static linux_ratelimit_t __ratelimited; \ 252 if (linux_ratelimited(&__ratelimited)) \ 253 dev_dbg(dev, __VA_ARGS__); \ 254 } while (0) 255 256 /* Public and LinuxKPI internal devres functions. */ 257 void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t); 258 void lkpi_devres_add(struct device *, void *); 259 void lkpi_devres_free(void *); 260 void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *), 261 int (*match)(struct device *, void *, void *), void *); 262 int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *), 263 int (*match)(struct device *, void *, void *), void *); 264 #define devres_alloc(_r, _s, _g) lkpi_devres_alloc(_r, _s, _g) 265 #define devres_add(_d, _p) lkpi_devres_add(_d, _p) 266 #define devres_free(_p) lkpi_devres_free(_p) 267 #define devres_find(_d, _rfn, _mfn, _mp) \ 268 lkpi_devres_find(_d, _rfn, _mfn, _mp) 269 #define devres_destroy(_d, _rfn, _mfn, _mp) \ 270 lkpi_devres_destroy(_d, _rfn, _mfn, _mp) 271 void lkpi_devres_release_free_list(struct device *); 272 void lkpi_devres_unlink(struct device *, void *); 273 void lkpi_devm_kmalloc_release(struct device *, void *); 274 275 static inline const char * 276 dev_driver_string(const struct device *dev) 277 { 278 driver_t *drv; 279 const char *str = ""; 280 281 if (dev->bsddev != NULL) { 282 drv = device_get_driver(dev->bsddev); 283 if (drv != NULL) 284 str = drv->name; 285 } 286 287 return (str); 288 } 289 290 static inline void * 291 dev_get_drvdata(const struct device *dev) 292 { 293 294 return dev->driver_data; 295 } 296 297 static inline void 298 dev_set_drvdata(struct device *dev, void *data) 299 { 300 301 dev->driver_data = data; 302 } 303 304 static inline struct device * 305 get_device(struct device *dev) 306 { 307 308 if (dev) 309 kobject_get(&dev->kobj); 310 311 return (dev); 312 } 313 314 static inline char * 315 dev_name(const struct device *dev) 316 { 317 318 return kobject_name(&dev->kobj); 319 } 320 321 #define dev_set_name(_dev, _fmt, ...) \ 322 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__) 323 324 static inline void 325 put_device(struct device *dev) 326 { 327 328 if (dev) 329 kobject_put(&dev->kobj); 330 } 331 332 struct class *class_create(struct module *owner, const char *name); 333 334 static inline int 335 class_register(struct class *class) 336 { 337 338 class->bsdclass = devclass_create(class->name); 339 kobject_init(&class->kobj, &linux_class_ktype); 340 kobject_set_name(&class->kobj, class->name); 341 kobject_add(&class->kobj, &linux_class_root, class->name); 342 343 return (0); 344 } 345 346 static inline void 347 class_unregister(struct class *class) 348 { 349 350 kobject_put(&class->kobj); 351 } 352 353 static inline struct device *kobj_to_dev(struct kobject *kobj) 354 { 355 return container_of(kobj, struct device, kobj); 356 } 357 358 struct device *device_create(struct class *class, struct device *parent, 359 dev_t devt, void *drvdata, const char *fmt, ...); 360 struct device *device_create_groups_vargs(struct class *class, struct device *parent, 361 dev_t devt, void *drvdata, const struct attribute_group **groups, 362 const char *fmt, va_list args); 363 364 /* 365 * Devices are registered and created for exporting to sysfs. Create 366 * implies register and register assumes the device fields have been 367 * setup appropriately before being called. 368 */ 369 static inline void 370 device_initialize(struct device *dev) 371 { 372 device_t bsddev = NULL; 373 int unit = -1; 374 375 if (dev->devt) { 376 unit = MINOR(dev->devt); 377 bsddev = devclass_get_device(dev->class->bsdclass, unit); 378 dev->bsddev_attached_here = false; 379 } else if (dev->parent == NULL) { 380 bsddev = devclass_get_device(dev->class->bsdclass, 0); 381 dev->bsddev_attached_here = false; 382 } else { 383 dev->bsddev_attached_here = true; 384 } 385 386 if (bsddev == NULL && dev->parent != NULL) { 387 bsddev = device_add_child(dev->parent->bsddev, 388 dev->class->kobj.name, unit); 389 } 390 391 if (bsddev != NULL) 392 device_set_softc(bsddev, dev); 393 394 dev->bsddev = bsddev; 395 MPASS(dev->bsddev != NULL); 396 kobject_init(&dev->kobj, &linux_dev_ktype); 397 398 spin_lock_init(&dev->devres_lock); 399 INIT_LIST_HEAD(&dev->devres_head); 400 } 401 402 static inline int 403 device_add(struct device *dev) 404 { 405 if (dev->bsddev != NULL) { 406 if (dev->devt == 0) 407 dev->devt = makedev(0, device_get_unit(dev->bsddev)); 408 } 409 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 410 411 if (dev->groups) 412 return (sysfs_create_groups(&dev->kobj, dev->groups)); 413 414 return (0); 415 } 416 417 static inline void 418 device_create_release(struct device *dev) 419 { 420 kfree(dev); 421 } 422 423 static inline struct device * 424 device_create_with_groups(struct class *class, 425 struct device *parent, dev_t devt, void *drvdata, 426 const struct attribute_group **groups, const char *fmt, ...) 427 { 428 va_list vargs; 429 struct device *dev; 430 431 va_start(vargs, fmt); 432 dev = device_create_groups_vargs(class, parent, devt, drvdata, 433 groups, fmt, vargs); 434 va_end(vargs); 435 return dev; 436 } 437 438 static inline bool 439 device_is_registered(struct device *dev) 440 { 441 442 return (dev->bsddev != NULL); 443 } 444 445 static inline int 446 device_register(struct device *dev) 447 { 448 device_t bsddev = NULL; 449 int unit = -1; 450 451 if (device_is_registered(dev)) 452 goto done; 453 454 if (dev->devt) { 455 unit = MINOR(dev->devt); 456 bsddev = devclass_get_device(dev->class->bsdclass, unit); 457 dev->bsddev_attached_here = false; 458 } else if (dev->parent == NULL) { 459 bsddev = devclass_get_device(dev->class->bsdclass, 0); 460 dev->bsddev_attached_here = false; 461 } else { 462 dev->bsddev_attached_here = true; 463 } 464 if (bsddev == NULL && dev->parent != NULL) { 465 bsddev = device_add_child(dev->parent->bsddev, 466 dev->class->kobj.name, unit); 467 } 468 if (bsddev != NULL) { 469 if (dev->devt == 0) 470 dev->devt = makedev(0, device_get_unit(bsddev)); 471 device_set_softc(bsddev, dev); 472 } 473 dev->bsddev = bsddev; 474 done: 475 kobject_init(&dev->kobj, &linux_dev_ktype); 476 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 477 478 sysfs_create_groups(&dev->kobj, dev->class->dev_groups); 479 480 return (0); 481 } 482 483 static inline void 484 device_unregister(struct device *dev) 485 { 486 device_t bsddev; 487 488 sysfs_remove_groups(&dev->kobj, dev->class->dev_groups); 489 490 bsddev = dev->bsddev; 491 dev->bsddev = NULL; 492 493 if (bsddev != NULL && dev->bsddev_attached_here) { 494 bus_topo_lock(); 495 device_delete_child(device_get_parent(bsddev), bsddev); 496 bus_topo_unlock(); 497 } 498 put_device(dev); 499 } 500 501 static inline void 502 device_del(struct device *dev) 503 { 504 device_t bsddev; 505 506 bsddev = dev->bsddev; 507 dev->bsddev = NULL; 508 509 if (bsddev != NULL && dev->bsddev_attached_here) { 510 bus_topo_lock(); 511 device_delete_child(device_get_parent(bsddev), bsddev); 512 bus_topo_unlock(); 513 } 514 } 515 516 static inline void 517 device_destroy(struct class *class, dev_t devt) 518 { 519 device_t bsddev; 520 int unit; 521 522 unit = MINOR(devt); 523 bsddev = devclass_get_device(class->bsdclass, unit); 524 if (bsddev != NULL) 525 device_unregister(device_get_softc(bsddev)); 526 } 527 528 static inline void 529 device_release_driver(struct device *dev) 530 { 531 532 #if 0 533 /* This leads to panics. Disable temporarily. Keep to rework. */ 534 535 /* We also need to cleanup LinuxKPI bits. What else? */ 536 lkpi_devres_release_free_list(dev); 537 dev_set_drvdata(dev, NULL); 538 /* Do not call dev->release! */ 539 540 bus_topo_lock(); 541 if (device_is_attached(dev->bsddev)) 542 device_detach(dev->bsddev); 543 bus_topo_unlock(); 544 #endif 545 } 546 547 static inline int 548 device_reprobe(struct device *dev) 549 { 550 int error; 551 552 device_release_driver(dev); 553 bus_topo_lock(); 554 error = device_probe_and_attach(dev->bsddev); 555 bus_topo_unlock(); 556 557 return (-error); 558 } 559 560 static inline void 561 device_set_wakeup_enable(struct device *dev __unused, bool enable __unused) 562 { 563 564 /* 565 * XXX-BZ TODO This is used by wireless drivers supporting WoWLAN which 566 * we currently do not support. 567 */ 568 } 569 570 static inline int 571 device_wakeup_enable(struct device *dev) 572 { 573 574 device_set_wakeup_enable(dev, true); 575 return (0); 576 } 577 578 static inline bool 579 device_iommu_mapped(struct device *dev __unused) 580 { 581 return (false); 582 } 583 584 #define dev_pm_set_driver_flags(dev, flags) do { \ 585 } while (0) 586 587 static inline void 588 linux_class_kfree(struct class *class) 589 { 590 591 kfree(class); 592 } 593 594 static inline void 595 class_destroy(struct class *class) 596 { 597 598 if (class == NULL) 599 return; 600 class_unregister(class); 601 } 602 603 static inline int 604 device_create_file(struct device *dev, const struct device_attribute *attr) 605 { 606 607 if (dev) 608 return sysfs_create_file(&dev->kobj, &attr->attr); 609 return -EINVAL; 610 } 611 612 static inline void 613 device_remove_file(struct device *dev, const struct device_attribute *attr) 614 { 615 616 if (dev) 617 sysfs_remove_file(&dev->kobj, &attr->attr); 618 } 619 620 static inline int 621 class_create_file(struct class *class, const struct class_attribute *attr) 622 { 623 624 if (class) 625 return sysfs_create_file(&class->kobj, &attr->attr); 626 return -EINVAL; 627 } 628 629 static inline void 630 class_remove_file(struct class *class, const struct class_attribute *attr) 631 { 632 633 if (class) 634 sysfs_remove_file(&class->kobj, &attr->attr); 635 } 636 637 #define dev_to_node(dev) linux_dev_to_node(dev) 638 #define of_node_to_nid(node) -1 639 int linux_dev_to_node(struct device *); 640 641 char *kvasprintf(gfp_t, const char *, va_list); 642 char *kasprintf(gfp_t, const char *, ...); 643 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...); 644 645 #define devm_kasprintf(_dev, _gfp, _fmt, ...) \ 646 lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__) 647 648 static __inline void * 649 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) 650 { 651 void *p; 652 653 p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp); 654 if (p != NULL) 655 lkpi_devres_add(dev, p); 656 657 return (p); 658 } 659 660 static inline void * 661 devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) 662 { 663 void *dst; 664 665 if (len == 0) 666 return (NULL); 667 668 dst = devm_kmalloc(dev, len, gfp); 669 if (dst != NULL) 670 memcpy(dst, src, len); 671 672 return (dst); 673 } 674 675 #define devm_kzalloc(_dev, _size, _gfp) \ 676 devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO) 677 678 #define devm_kcalloc(_dev, _sizen, _size, _gfp) \ 679 devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO) 680 681 int lkpi_devm_add_action(struct device *dev, void (*action)(void *), void *data); 682 #define devm_add_action(dev, action, data) \ 683 lkpi_devm_add_action(dev, action, data); 684 int lkpi_devm_add_action_or_reset(struct device *dev, void (*action)(void *), void *data); 685 #define devm_add_action_or_reset(dev, action, data) \ 686 lkpi_devm_add_action_or_reset(dev, action, data) 687 688 #endif /* _LINUXKPI_LINUX_DEVICE_H_ */ 689