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