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