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