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