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