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 struct class *class_create(struct module *owner, const char *name); 293 294 static inline int 295 class_register(struct class *class) 296 { 297 298 class->bsdclass = devclass_create(class->name); 299 kobject_init(&class->kobj, &linux_class_ktype); 300 kobject_set_name(&class->kobj, class->name); 301 kobject_add(&class->kobj, &linux_class_root, class->name); 302 303 return (0); 304 } 305 306 static inline void 307 class_unregister(struct class *class) 308 { 309 310 kobject_put(&class->kobj); 311 } 312 313 static inline struct device *kobj_to_dev(struct kobject *kobj) 314 { 315 return container_of(kobj, struct device, kobj); 316 } 317 318 struct device *device_create(struct class *class, struct device *parent, 319 dev_t devt, void *drvdata, const char *fmt, ...); 320 struct device *device_create_groups_vargs(struct class *class, struct device *parent, 321 dev_t devt, void *drvdata, const struct attribute_group **groups, 322 const char *fmt, va_list args); 323 324 /* 325 * Devices are registered and created for exporting to sysfs. Create 326 * implies register and register assumes the device fields have been 327 * setup appropriately before being called. 328 */ 329 static inline void 330 device_initialize(struct device *dev) 331 { 332 device_t bsddev = NULL; 333 int unit = -1; 334 335 if (dev->devt) { 336 unit = MINOR(dev->devt); 337 bsddev = devclass_get_device(dev->class->bsdclass, unit); 338 dev->bsddev_attached_here = false; 339 } else if (dev->parent == NULL) { 340 bsddev = devclass_get_device(dev->class->bsdclass, 0); 341 dev->bsddev_attached_here = false; 342 } else { 343 dev->bsddev_attached_here = true; 344 } 345 346 if (bsddev == NULL && dev->parent != NULL) { 347 bsddev = device_add_child(dev->parent->bsddev, 348 dev->class->kobj.name, unit); 349 } 350 351 if (bsddev != NULL) 352 device_set_softc(bsddev, dev); 353 354 dev->bsddev = bsddev; 355 MPASS(dev->bsddev != NULL); 356 kobject_init(&dev->kobj, &linux_dev_ktype); 357 358 spin_lock_init(&dev->devres_lock); 359 INIT_LIST_HEAD(&dev->devres_head); 360 } 361 362 static inline int 363 device_add(struct device *dev) 364 { 365 if (dev->bsddev != NULL) { 366 if (dev->devt == 0) 367 dev->devt = makedev(0, device_get_unit(dev->bsddev)); 368 } 369 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 370 371 if (dev->groups) 372 return (sysfs_create_groups(&dev->kobj, dev->groups)); 373 374 return (0); 375 } 376 377 static inline void 378 device_create_release(struct device *dev) 379 { 380 kfree(dev); 381 } 382 383 static inline struct device * 384 device_create_with_groups(struct class *class, 385 struct device *parent, dev_t devt, void *drvdata, 386 const struct attribute_group **groups, const char *fmt, ...) 387 { 388 va_list vargs; 389 struct device *dev; 390 391 va_start(vargs, fmt); 392 dev = device_create_groups_vargs(class, parent, devt, drvdata, 393 groups, fmt, vargs); 394 va_end(vargs); 395 return dev; 396 } 397 398 static inline bool 399 device_is_registered(struct device *dev) 400 { 401 402 return (dev->bsddev != NULL); 403 } 404 405 static inline int 406 device_register(struct device *dev) 407 { 408 device_t bsddev = NULL; 409 int unit = -1; 410 411 if (device_is_registered(dev)) 412 goto done; 413 414 if (dev->devt) { 415 unit = MINOR(dev->devt); 416 bsddev = devclass_get_device(dev->class->bsdclass, unit); 417 dev->bsddev_attached_here = false; 418 } else if (dev->parent == NULL) { 419 bsddev = devclass_get_device(dev->class->bsdclass, 0); 420 dev->bsddev_attached_here = false; 421 } else { 422 dev->bsddev_attached_here = true; 423 } 424 if (bsddev == NULL && dev->parent != NULL) { 425 bsddev = device_add_child(dev->parent->bsddev, 426 dev->class->kobj.name, unit); 427 } 428 if (bsddev != NULL) { 429 if (dev->devt == 0) 430 dev->devt = makedev(0, device_get_unit(bsddev)); 431 device_set_softc(bsddev, dev); 432 } 433 dev->bsddev = bsddev; 434 done: 435 kobject_init(&dev->kobj, &linux_dev_ktype); 436 kobject_add(&dev->kobj, &dev->class->kobj, dev_name(dev)); 437 438 sysfs_create_groups(&dev->kobj, dev->class->dev_groups); 439 440 return (0); 441 } 442 443 static inline void 444 device_unregister(struct device *dev) 445 { 446 device_t bsddev; 447 448 sysfs_remove_groups(&dev->kobj, dev->class->dev_groups); 449 450 bsddev = dev->bsddev; 451 dev->bsddev = NULL; 452 453 if (bsddev != NULL && dev->bsddev_attached_here) { 454 bus_topo_lock(); 455 device_delete_child(device_get_parent(bsddev), bsddev); 456 bus_topo_unlock(); 457 } 458 put_device(dev); 459 } 460 461 static inline void 462 device_del(struct device *dev) 463 { 464 device_t bsddev; 465 466 bsddev = dev->bsddev; 467 dev->bsddev = NULL; 468 469 if (bsddev != NULL && dev->bsddev_attached_here) { 470 bus_topo_lock(); 471 device_delete_child(device_get_parent(bsddev), bsddev); 472 bus_topo_unlock(); 473 } 474 } 475 476 static inline void 477 device_destroy(struct class *class, dev_t devt) 478 { 479 device_t bsddev; 480 int unit; 481 482 unit = MINOR(devt); 483 bsddev = devclass_get_device(class->bsdclass, unit); 484 if (bsddev != NULL) 485 device_unregister(device_get_softc(bsddev)); 486 } 487 488 static inline void 489 device_release_driver(struct device *dev) 490 { 491 492 #if 0 493 /* This leads to panics. Disable temporarily. Keep to rework. */ 494 495 /* We also need to cleanup LinuxKPI bits. What else? */ 496 lkpi_devres_release_free_list(dev); 497 dev_set_drvdata(dev, NULL); 498 /* Do not call dev->release! */ 499 500 bus_topo_lock(); 501 if (device_is_attached(dev->bsddev)) 502 device_detach(dev->bsddev); 503 bus_topo_unlock(); 504 #endif 505 } 506 507 static inline int 508 device_reprobe(struct device *dev) 509 { 510 int error; 511 512 device_release_driver(dev); 513 bus_topo_lock(); 514 error = device_probe_and_attach(dev->bsddev); 515 bus_topo_unlock(); 516 517 return (-error); 518 } 519 520 #define dev_pm_set_driver_flags(dev, flags) do { \ 521 } while (0) 522 523 static inline void 524 linux_class_kfree(struct class *class) 525 { 526 527 kfree(class); 528 } 529 530 static inline void 531 class_destroy(struct class *class) 532 { 533 534 if (class == NULL) 535 return; 536 class_unregister(class); 537 } 538 539 static inline int 540 device_create_file(struct device *dev, const struct device_attribute *attr) 541 { 542 543 if (dev) 544 return sysfs_create_file(&dev->kobj, &attr->attr); 545 return -EINVAL; 546 } 547 548 static inline void 549 device_remove_file(struct device *dev, const struct device_attribute *attr) 550 { 551 552 if (dev) 553 sysfs_remove_file(&dev->kobj, &attr->attr); 554 } 555 556 static inline int 557 class_create_file(struct class *class, const struct class_attribute *attr) 558 { 559 560 if (class) 561 return sysfs_create_file(&class->kobj, &attr->attr); 562 return -EINVAL; 563 } 564 565 static inline void 566 class_remove_file(struct class *class, const struct class_attribute *attr) 567 { 568 569 if (class) 570 sysfs_remove_file(&class->kobj, &attr->attr); 571 } 572 573 #define dev_to_node(dev) linux_dev_to_node(dev) 574 #define of_node_to_nid(node) -1 575 int linux_dev_to_node(struct device *); 576 577 char *kvasprintf(gfp_t, const char *, va_list); 578 char *kasprintf(gfp_t, const char *, ...); 579 char *lkpi_devm_kasprintf(struct device *, gfp_t, const char *, ...); 580 581 #define devm_kasprintf(_dev, _gfp, _fmt, ...) \ 582 lkpi_devm_kasprintf(_dev, _gfp, _fmt, ##__VA_ARGS__) 583 584 static __inline void * 585 devm_kmalloc(struct device *dev, size_t size, gfp_t gfp) 586 { 587 void *p; 588 589 p = lkpi_devres_alloc(lkpi_devm_kmalloc_release, size, gfp); 590 if (p != NULL) 591 lkpi_devres_add(dev, p); 592 593 return (p); 594 } 595 596 #define devm_kzalloc(_dev, _size, _gfp) \ 597 devm_kmalloc((_dev), (_size), (_gfp) | __GFP_ZERO) 598 599 #define devm_kcalloc(_dev, _sizen, _size, _gfp) \ 600 devm_kmalloc((_dev), ((_sizen) * (_size)), (_gfp) | __GFP_ZERO) 601 602 #endif /* _LINUXKPI_LINUX_DEVICE_H_ */ 603