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