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