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