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