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 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice unmodified, this list of conditions, and the following 13 * disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 #ifndef _LINUXKPI_LINUX_DEVICE_H_ 32 #define _LINUXKPI_LINUX_DEVICE_H_ 33 34 #include <linux/err.h> 35 #include <linux/types.h> 36 #include <linux/kobject.h> 37 #include <linux/sysfs.h> 38 #include <linux/list.h> 39 #include <linux/compiler.h> 40 #include <linux/types.h> 41 #include <linux/module.h> 42 #include <linux/workqueue.h> 43 #include <linux/kdev_t.h> 44 #include <linux/backlight.h> 45 #include <linux/pm.h> 46 #include <linux/idr.h> 47 #include <linux/ratelimit.h> /* via linux/dev_printk.h */ 48 #include <asm/atomic.h> 49 50 #include <sys/bus.h> 51 #include <sys/backlight.h> 52 53 struct device; 54 struct fwnode_handle; 55 56 struct class { 57 const char *name; 58 struct module *owner; 59 struct kobject kobj; 60 devclass_t bsdclass; 61 const struct dev_pm_ops *pm; 62 const struct attribute_group **dev_groups; 63 void (*class_release)(struct class *class); 64 void (*dev_release)(struct device *dev); 65 char * (*devnode)(struct device *dev, umode_t *mode); 66 }; 67 68 struct dev_pm_ops { 69 int (*prepare)(struct device *dev); 70 int (*suspend)(struct device *dev); 71 int (*suspend_late)(struct device *dev); 72 int (*resume)(struct device *dev); 73 int (*resume_early)(struct device *dev); 74 int (*freeze)(struct device *dev); 75 int (*freeze_late)(struct device *dev); 76 int (*thaw)(struct device *dev); 77 int (*thaw_early)(struct device *dev); 78 int (*poweroff)(struct device *dev); 79 int (*poweroff_late)(struct device *dev); 80 int (*restore)(struct device *dev); 81 int (*restore_early)(struct device *dev); 82 int (*runtime_suspend)(struct device *dev); 83 int (*runtime_resume)(struct device *dev); 84 int (*runtime_idle)(struct device *dev); 85 }; 86 87 struct device_driver { 88 const char *name; 89 const struct dev_pm_ops *pm; 90 }; 91 92 struct device_type { 93 const char *name; 94 }; 95 96 struct device { 97 struct device *parent; 98 struct list_head irqents; 99 device_t bsddev; 100 /* 101 * The following flag is used to determine if the LinuxKPI is 102 * responsible for detaching the BSD device or not. If the 103 * LinuxKPI got the BSD device using devclass_get_device(), it 104 * must not try to detach or delete it, because it's already 105 * done somewhere else. 106 */ 107 bool bsddev_attached_here; 108 struct device_driver *driver; 109 struct device_type *type; 110 dev_t devt; 111 struct class *class; 112 void (*release)(struct device *dev); 113 struct kobject kobj; 114 void *dma_priv; 115 void *driver_data; 116 unsigned int irq; 117 #define LINUX_IRQ_INVALID 65535 118 unsigned int irq_start; 119 unsigned int irq_end; 120 const struct attribute_group **groups; 121 struct fwnode_handle *fwnode; 122 struct cdev *backlight_dev; 123 struct backlight_device *bd; 124 125 spinlock_t devres_lock; 126 struct list_head devres_head; 127 }; 128 129 extern struct device linux_root_device; 130 extern struct kobject linux_class_root; 131 extern const struct kobj_type linux_dev_ktype; 132 extern const struct kobj_type linux_class_ktype; 133 134 struct class_attribute { 135 struct attribute attr; 136 ssize_t (*show)(struct class *, struct class_attribute *, char *); 137 ssize_t (*store)(struct class *, struct class_attribute *, const char *, size_t); 138 const void *(*namespace)(struct class *, const struct class_attribute *); 139 }; 140 141 #define CLASS_ATTR(_name, _mode, _show, _store) \ 142 struct class_attribute class_attr_##_name = \ 143 { { #_name, NULL, _mode }, _show, _store } 144 145 struct device_attribute { 146 struct attribute attr; 147 ssize_t (*show)(struct device *, 148 struct device_attribute *, char *); 149 ssize_t (*store)(struct device *, 150 struct device_attribute *, const char *, 151 size_t); 152 }; 153 154 #define DEVICE_ATTR(_name, _mode, _show, _store) \ 155 struct device_attribute dev_attr_##_name = \ 156 __ATTR(_name, _mode, _show, _store) 157 #define DEVICE_ATTR_RO(_name) \ 158 struct device_attribute dev_attr_##_name = __ATTR_RO(_name) 159 #define DEVICE_ATTR_WO(_name) \ 160 struct device_attribute dev_attr_##_name = __ATTR_WO(_name) 161 #define DEVICE_ATTR_RW(_name) \ 162 struct device_attribute dev_attr_##_name = __ATTR_RW(_name) 163 164 /* Simple class attribute that is just a static string */ 165 struct class_attribute_string { 166 struct class_attribute attr; 167 char *str; 168 }; 169 170 static inline ssize_t 171 show_class_attr_string(struct class *class, 172 struct class_attribute *attr, char *buf) 173 { 174 struct class_attribute_string *cs; 175 cs = container_of(attr, struct class_attribute_string, attr); 176 return snprintf(buf, PAGE_SIZE, "%s\n", cs->str); 177 } 178 179 /* Currently read-only only */ 180 #define _CLASS_ATTR_STRING(_name, _mode, _str) \ 181 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 182 #define CLASS_ATTR_STRING(_name, _mode, _str) \ 183 struct class_attribute_string class_attr_##_name = \ 184 _CLASS_ATTR_STRING(_name, _mode, _str) 185 186 #define dev_err(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 187 #define dev_crit(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 188 #define dev_warn(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 189 #define dev_info(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 190 #define dev_notice(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 191 #define dev_emerg(dev, fmt, ...) device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 192 #define dev_dbg(dev, fmt, ...) do { } while (0) 193 #define dev_printk(lvl, dev, fmt, ...) \ 194 device_printf((dev)->bsddev, fmt, ##__VA_ARGS__) 195 196 #define dev_err_once(dev, ...) do { \ 197 static bool __dev_err_once; \ 198 if (!__dev_err_once) { \ 199 __dev_err_once = 1; \ 200 dev_err(dev, __VA_ARGS__); \ 201 } \ 202 } while (0) 203 204 #define dev_err_ratelimited(dev, ...) do { \ 205 static linux_ratelimit_t __ratelimited; \ 206 if (linux_ratelimited(&__ratelimited)) \ 207 dev_err(dev, __VA_ARGS__); \ 208 } while (0) 209 210 #define dev_warn_ratelimited(dev, ...) do { \ 211 static linux_ratelimit_t __ratelimited; \ 212 if (linux_ratelimited(&__ratelimited)) \ 213 dev_warn(dev, __VA_ARGS__); \ 214 } while (0) 215 216 /* Public and LinuxKPI internal devres functions. */ 217 void *lkpi_devres_alloc(void(*release)(struct device *, void *), size_t, gfp_t); 218 void lkpi_devres_add(struct device *, void *); 219 void lkpi_devres_free(void *); 220 void *lkpi_devres_find(struct device *, void(*release)(struct device *, void *), 221 int (*match)(struct device *, void *, void *), void *); 222 int lkpi_devres_destroy(struct device *, void(*release)(struct device *, void *), 223 int (*match)(struct device *, void *, void *), void *); 224 #define devres_alloc(_r, _s, _g) lkpi_devres_alloc(_r, _s, _g) 225 #define devres_add(_d, _p) lkpi_devres_add(_d, _p) 226 #define devres_free(_p) lkpi_devres_free(_p) 227 #define devres_find(_d, _rfn, _mfn, _mp) \ 228 lkpi_devres_find(_d, _rfn, _mfn, _mp) 229 #define devres_destroy(_d, _rfn, _mfn, _mp) \ 230 lkpi_devres_destroy(_d, _rfn, _mfn, _mp) 231 void lkpi_devres_release_free_list(struct device *); 232 void lkpi_devres_unlink(struct device *, void *); 233 void lkpi_devm_kmalloc_release(struct device *, void *); 234 235 static inline const char * 236 dev_driver_string(const struct device *dev) 237 { 238 driver_t *drv; 239 const char *str = ""; 240 241 if (dev->bsddev != NULL) { 242 drv = device_get_driver(dev->bsddev); 243 if (drv != NULL) 244 str = drv->name; 245 } 246 247 return (str); 248 } 249 250 static inline void * 251 dev_get_drvdata(const struct device *dev) 252 { 253 254 return dev->driver_data; 255 } 256 257 static inline void 258 dev_set_drvdata(struct device *dev, void *data) 259 { 260 261 dev->driver_data = data; 262 } 263 264 static inline struct device * 265 get_device(struct device *dev) 266 { 267 268 if (dev) 269 kobject_get(&dev->kobj); 270 271 return (dev); 272 } 273 274 static inline char * 275 dev_name(const struct device *dev) 276 { 277 278 return kobject_name(&dev->kobj); 279 } 280 281 #define dev_set_name(_dev, _fmt, ...) \ 282 kobject_set_name(&(_dev)->kobj, (_fmt), ##__VA_ARGS__) 283 284 static inline void 285 put_device(struct device *dev) 286 { 287 288 if (dev) 289 kobject_put(&dev->kobj); 290 } 291 292 static inline int 293 class_register(struct class *class) 294 { 295 296 class->bsdclass = devclass_create(class->name); 297 kobject_init(&class->kobj, &linux_class_ktype); 298 kobject_set_name(&class->kobj, class->name); 299 kobject_add(&class->kobj, &linux_class_root, class->name); 300 301 return (0); 302 } 303 304 static inline void 305 class_unregister(struct class *class) 306 { 307 308 kobject_put(&class->kobj); 309 } 310 311 static inline struct device *kobj_to_dev(struct kobject *kobj) 312 { 313 return container_of(kobj, struct device, kobj); 314 } 315 316 struct device *device_create(struct class *class, struct device *parent, 317 dev_t devt, void *drvdata, const char *fmt, ...); 318 struct device *device_create_groups_vargs(struct class *class, struct device *parent, 319 dev_t devt, void *drvdata, const struct attribute_group **groups, 320 const char *fmt, va_list args); 321 322 /* 323 * Devices are registered and created for exporting to sysfs. Create 324 * implies register and register assumes the device fields have been 325 * setup appropriately before being called. 326 */ 327 static inline void 328 device_initialize(struct device *dev) 329 { 330 device_t bsddev = NULL; 331 int unit = -1; 332 333 if (dev->devt) { 334 unit = MINOR(dev->devt); 335 bsddev = devclass_get_device(dev->class->bsdclass, unit); 336 dev->bsddev_attached_here = false; 337 } else if (dev->parent == NULL) { 338 bsddev = devclass_get_device(dev->class->bsdclass, 0); 339 dev->bsddev_attached_here = false; 340 } else { 341 dev->bsddev_attached_here = true; 342 } 343 344 if (bsddev == NULL && dev->parent != NULL) { 345 bsddev = device_add_child(dev->parent->bsddev, 346 dev->class->kobj.name, unit); 347 } 348 349 if (bsddev != NULL) 350 device_set_softc(bsddev, dev); 351 352 dev->bsddev = bsddev; 353 MPASS(dev->bsddev != NULL); 354 kobject_init(&dev->kobj, &linux_dev_ktype); 355 356 spin_lock_init(&dev->devres_lock); 357 INIT_LIST_HEAD(&dev->devres_head); 358 } 359 360 static inline int 361 device_add(struct device *dev) 362 { 363 if (dev->bsddev != NULL) { 364 if (dev->devt == 0) 365 dev->devt = makedev(0, device_get_unit(dev->bsddev)); 366 } 367 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 368 369 if (dev->groups) 370 return (sysfs_create_groups(&dev->kobj, dev->groups)); 371 372 return (0); 373 } 374 375 static inline void 376 device_create_release(struct device *dev) 377 { 378 kfree(dev); 379 } 380 381 static inline struct device * 382 device_create_with_groups(struct class *class, 383 struct device *parent, dev_t devt, void *drvdata, 384 const struct attribute_group **groups, const char *fmt, ...) 385 { 386 va_list vargs; 387 struct device *dev; 388 389 va_start(vargs, fmt); 390 dev = device_create_groups_vargs(class, parent, devt, drvdata, 391 groups, fmt, vargs); 392 va_end(vargs); 393 return dev; 394 } 395 396 static inline bool 397 device_is_registered(struct device *dev) 398 { 399 400 return (dev->bsddev != NULL); 401 } 402 403 static inline int 404 device_register(struct device *dev) 405 { 406 device_t bsddev = NULL; 407 int unit = -1; 408 409 if (device_is_registered(dev)) 410 goto done; 411 412 if (dev->devt) { 413 unit = MINOR(dev->devt); 414 bsddev = devclass_get_device(dev->class->bsdclass, unit); 415 dev->bsddev_attached_here = false; 416 } else if (dev->parent == NULL) { 417 bsddev = devclass_get_device(dev->class->bsdclass, 0); 418 dev->bsddev_attached_here = false; 419 } else { 420 dev->bsddev_attached_here = true; 421 } 422 if (bsddev == NULL && dev->parent != NULL) { 423 bsddev = device_add_child(dev->parent->bsddev, 424 dev->class->kobj.name, unit); 425 } 426 if (bsddev != NULL) { 427 if (dev->devt == 0) 428 dev->devt = makedev(0, device_get_unit(bsddev)); 429 device_set_softc(bsddev, dev); 430 } 431 dev->bsddev = bsddev; 432 done: 433 kobject_init(&dev->kobj, &linux_dev_ktype); 434 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 435 436 sysfs_create_groups(&dev->kobj, dev->class->dev_groups); 437 438 return (0); 439 } 440 441 static inline void 442 device_unregister(struct device *dev) 443 { 444 device_t bsddev; 445 446 sysfs_remove_groups(&dev->kobj, dev->class->dev_groups); 447 448 bsddev = dev->bsddev; 449 dev->bsddev = NULL; 450 451 if (bsddev != NULL && dev->bsddev_attached_here) { 452 bus_topo_lock(); 453 device_delete_child(device_get_parent(bsddev), bsddev); 454 bus_topo_unlock(); 455 } 456 put_device(dev); 457 } 458 459 static inline void 460 device_del(struct device *dev) 461 { 462 device_t bsddev; 463 464 bsddev = dev->bsddev; 465 dev->bsddev = NULL; 466 467 if (bsddev != NULL && dev->bsddev_attached_here) { 468 bus_topo_lock(); 469 device_delete_child(device_get_parent(bsddev), bsddev); 470 bus_topo_unlock(); 471 } 472 } 473 474 static inline void 475 device_destroy(struct class *class, dev_t devt) 476 { 477 device_t bsddev; 478 int unit; 479 480 unit = MINOR(devt); 481 bsddev = devclass_get_device(class->bsdclass, unit); 482 if (bsddev != NULL) 483 device_unregister(device_get_softc(bsddev)); 484 } 485 486 static inline void 487 device_release_driver(struct device *dev) 488 { 489 490 #if 0 491 /* This leads to panics. Disable temporarily. Keep to rework. */ 492 493 /* We also need to cleanup LinuxKPI bits. What else? */ 494 lkpi_devres_release_free_list(dev); 495 dev_set_drvdata(dev, NULL); 496 /* Do not call dev->release! */ 497 498 bus_topo_lock(); 499 if (device_is_attached(dev->bsddev)) 500 device_detach(dev->bsddev); 501 bus_topo_unlock(); 502 #endif 503 } 504 505 static inline int 506 device_reprobe(struct device *dev) 507 { 508 int error; 509 510 device_release_driver(dev); 511 bus_topo_lock(); 512 error = device_probe_and_attach(dev->bsddev); 513 bus_topo_unlock(); 514 515 return (-error); 516 } 517 518 #define dev_pm_set_driver_flags(dev, flags) do { \ 519 } while (0) 520 521 static inline void 522 linux_class_kfree(struct class *class) 523 { 524 525 kfree(class); 526 } 527 528 static inline struct class * 529 class_create(struct module *owner, const char *name) 530 { 531 struct class *class; 532 int error; 533 534 class = kzalloc(sizeof(*class), M_WAITOK); 535 class->owner = owner; 536 class->name = name; 537 class->class_release = linux_class_kfree; 538 error = class_register(class); 539 if (error) { 540 kfree(class); 541 return (NULL); 542 } 543 544 return (class); 545 } 546 547 static inline void 548 class_destroy(struct class *class) 549 { 550 551 if (class == NULL) 552 return; 553 class_unregister(class); 554 } 555 556 static inline int 557 device_create_file(struct device *dev, const struct device_attribute *attr) 558 { 559 560 if (dev) 561 return sysfs_create_file(&dev->kobj, &attr->attr); 562 return -EINVAL; 563 } 564 565 static inline void 566 device_remove_file(struct device *dev, const struct device_attribute *attr) 567 { 568 569 if (dev) 570 sysfs_remove_file(&dev->kobj, &attr->attr); 571 } 572 573 static inline int 574 class_create_file(struct class *class, const struct class_attribute *attr) 575 { 576 577 if (class) 578 return sysfs_create_file(&class->kobj, &attr->attr); 579 return -EINVAL; 580 } 581 582 static inline void 583 class_remove_file(struct class *class, const struct class_attribute *attr) 584 { 585 586 if (class) 587 sysfs_remove_file(&class->kobj, &attr->attr); 588 } 589 590 #define dev_to_node(dev) linux_dev_to_node(dev) 591 #define of_node_to_nid(node) -1 592 int linux_dev_to_node(struct device *); 593 594 char *kvasprintf(gfp_t, const char *, va_list); 595 char *kasprintf(gfp_t, const char *, ...); 596 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...); 597 598 #define devm_kasprintf(_dev, _gfp, _fmt, ...) \ 599 lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__) 600 601 static __inline void * 602 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) 603 { 604 void *p; 605 606 p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp); 607 if (p != NULL) 608 lkpi_devres_add(dev, p); 609 610 return (p); 611 } 612 613 #define devm_kzalloc(_dev, _size, _gfp) \ 614 devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO) 615 616 #define devm_kcalloc(_dev, _sizen, _size, _gfp) \ 617 devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO) 618 619 #endif /* _LINUXKPI_LINUX_DEVICE_H_ */ 620