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