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