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