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