linux/include/linux/device.h
<<
>>
Prefs
   1/*
   2 * device.h - generic, centralized driver model
   3 *
   4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
   5 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
   6 * Copyright (c) 2008-2009 Novell Inc.
   7 *
   8 * This file is released under the GPLv2
   9 *
  10 * See Documentation/driver-model/ for more information.
  11 */
  12
  13#ifndef _DEVICE_H_
  14#define _DEVICE_H_
  15
  16#include <linux/ioport.h>
  17#include <linux/kobject.h>
  18#include <linux/klist.h>
  19#include <linux/list.h>
  20#include <linux/lockdep.h>
  21#include <linux/compiler.h>
  22#include <linux/types.h>
  23#include <linux/mutex.h>
  24#include <linux/pinctrl/devinfo.h>
  25#include <linux/pm.h>
  26#include <linux/atomic.h>
  27#include <linux/ratelimit.h>
  28#include <linux/uidgid.h>
  29#include <linux/gfp.h>
  30#include <asm/device.h>
  31
  32#include <linux/rh_kabi.h>
  33
  34struct device;
  35struct device_private;
  36struct device_driver;
  37struct driver_private;
  38struct module;
  39struct class;
  40struct subsys_private;
  41struct bus_type;
  42struct device_node;
  43struct fwnode_handle;
  44struct iommu_ops;
  45struct iommu_group;
  46
  47struct bus_attribute {
  48        struct attribute        attr;
  49        ssize_t (*show)(struct bus_type *bus, char *buf);
  50        ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
  51};
  52
  53#define BUS_ATTR(_name, _mode, _show, _store)   \
  54        struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
  55#define BUS_ATTR_RW(_name) \
  56        struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
  57#define BUS_ATTR_RO(_name) \
  58        struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
  59
  60extern int __must_check bus_create_file(struct bus_type *,
  61                                        struct bus_attribute *);
  62extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
  63
  64/**
  65 * struct bus_type - The bus type of the device
  66 *
  67 * @name:       The name of the bus.
  68 * @dev_name:   Used for subsystems to enumerate devices like ("foo%u", dev->id).
  69 * @dev_root:   Default device to use as the parent.
  70 * @bus_attrs:  Default attributes of the bus.
  71 * @dev_attrs:  Default attributes of the devices on the bus.
  72 * @drv_attrs:  Default attributes of the device drivers on the bus.
  73 * @bus_groups: Default attributes of the bus.
  74 * @dev_groups: Default attributes of the devices on the bus.
  75 * @drv_groups: Default attributes of the device drivers on the bus.
  76 * @match:      Called, perhaps multiple times, whenever a new device or driver
  77 *              is added for this bus. It should return a nonzero value if the
  78 *              given device can be handled by the given driver.
  79 * @uevent:     Called when a device is added, removed, or a few other things
  80 *              that generate uevents to add the environment variables.
  81 * @probe:      Called when a new device or driver add to this bus, and callback
  82 *              the specific driver's probe to initial the matched device.
  83 * @remove:     Called when a device removed from this bus.
  84 * @shutdown:   Called at shut-down time to quiesce the device.
  85 *
  86 * @online:     Called to put the device back online (after offlining it).
  87 * @offline:    Called to put the device offline for hot-removal. May fail.
  88 *
  89 * @suspend:    Called when a device on this bus wants to go to sleep mode.
  90 * @resume:     Called to bring a device on this bus out of sleep mode.
  91 * @pm:         Power management operations of this bus, callback the specific
  92 *              device driver's pm-ops.
  93 * @iommu_ops:  IOMMU specific operations for this bus, used to attach IOMMU
  94 *              driver implementations to a bus and allow the driver to do
  95 *              bus-specific setup
  96 * @p:          The private data of the driver core, only the driver core can
  97 *              touch this.
  98 *
  99 * A bus is a channel between the processor and one or more devices. For the
 100 * purposes of the device model, all devices are connected via a bus, even if
 101 * it is an internal, virtual, "platform" bus. Buses can plug into each other.
 102 * A USB controller is usually a PCI device, for example. The device model
 103 * represents the actual connections between buses and the devices they control.
 104 * A bus is represented by the bus_type structure. It contains the name, the
 105 * default attributes, the bus' methods, PM operations, and the driver core's
 106 * private data.
 107 */
 108struct bus_type {
 109        const char              *name;
 110        const char              *dev_name;
 111        struct device           *dev_root;
 112        struct bus_attribute    *bus_attrs;     /* use bus_groups instead */
 113        struct device_attribute *dev_attrs;     /* use dev_groups instead */
 114        struct driver_attribute *drv_attrs;     /* use drv_groups instead */
 115        const struct attribute_group **bus_groups;
 116        const struct attribute_group **dev_groups;
 117        const struct attribute_group **drv_groups;
 118
 119        int (*match)(struct device *dev, struct device_driver *drv);
 120        int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
 121        int (*probe)(struct device *dev);
 122        int (*remove)(struct device *dev);
 123        void (*shutdown)(struct device *dev);
 124
 125        int (*online)(struct device *dev);
 126        int (*offline)(struct device *dev);
 127
 128        int (*suspend)(struct device *dev, pm_message_t state);
 129        int (*resume)(struct device *dev);
 130
 131        const struct dev_pm_ops *pm;
 132
 133        struct iommu_ops *iommu_ops;
 134
 135        struct subsys_private *p;
 136        struct lock_class_key lock_key;
 137};
 138
 139extern int __must_check bus_register(struct bus_type *bus);
 140
 141extern void bus_unregister(struct bus_type *bus);
 142
 143extern int __must_check bus_rescan_devices(struct bus_type *bus);
 144
 145/* iterator helpers for buses */
 146struct subsys_dev_iter {
 147        struct klist_iter               ki;
 148        const struct device_type        *type;
 149};
 150void subsys_dev_iter_init(struct subsys_dev_iter *iter,
 151                         struct bus_type *subsys,
 152                         struct device *start,
 153                         const struct device_type *type);
 154struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
 155void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
 156
 157int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
 158                     int (*fn)(struct device *dev, void *data));
 159struct device *bus_find_device(struct bus_type *bus, struct device *start,
 160                               void *data,
 161                               int (*match)(struct device *dev, void *data));
 162struct device *bus_find_device_by_name(struct bus_type *bus,
 163                                       struct device *start,
 164                                       const char *name);
 165struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
 166                                        struct device *hint);
 167int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
 168                     void *data, int (*fn)(struct device_driver *, void *));
 169void bus_sort_breadthfirst(struct bus_type *bus,
 170                           int (*compare)(const struct device *a,
 171                                          const struct device *b));
 172/*
 173 * Bus notifiers: Get notified of addition/removal of devices
 174 * and binding/unbinding of drivers to devices.
 175 * In the long run, it should be a replacement for the platform
 176 * notify hooks.
 177 */
 178struct notifier_block;
 179
 180extern int bus_register_notifier(struct bus_type *bus,
 181                                 struct notifier_block *nb);
 182extern int bus_unregister_notifier(struct bus_type *bus,
 183                                   struct notifier_block *nb);
 184
 185/* All 4 notifers below get called with the target struct device *
 186 * as an argument. Note that those functions are likely to be called
 187 * with the device lock held in the core, so be careful.
 188 *
 189 * RHEL7 - Note that the following notifier values differ from upstream.
 190 * This was due to KABI and upstream commit 599bad38cf71.
 191 */
 192#define BUS_NOTIFY_ADD_DEVICE           0x00000001 /* device added */
 193#define BUS_NOTIFY_DEL_DEVICE           0x00000002 /* device to be removed */
 194#define BUS_NOTIFY_BIND_DRIVER          0x00000003 /* driver about to be
 195                                                      bound */
 196#define BUS_NOTIFY_BOUND_DRIVER         0x00000004 /* driver bound to device */
 197#define BUS_NOTIFY_UNBIND_DRIVER        0x00000005 /* driver about to be
 198                                                      unbound */
 199#define BUS_NOTIFY_UNBOUND_DRIVER       0x00000006 /* driver is unbound
 200                                                      from the device */
 201#define BUS_NOTIFY_REMOVED_DEVICE       0x00000007 /* device removed */
 202
 203
 204extern struct kset *bus_get_kset(struct bus_type *bus);
 205extern struct klist *bus_get_device_klist(struct bus_type *bus);
 206
 207/**
 208 * struct device_driver - The basic device driver structure
 209 * @name:       Name of the device driver.
 210 * @bus:        The bus which the device of this driver belongs to.
 211 * @owner:      The module owner.
 212 * @mod_name:   Used for built-in modules.
 213 * @suppress_bind_attrs: Disables bind/unbind via sysfs.
 214 * @of_match_table: The open firmware table.
 215 * @acpi_match_table: The ACPI match table.
 216 * @probe:      Called to query the existence of a specific device,
 217 *              whether this driver can work with it, and bind the driver
 218 *              to a specific device.
 219 * @remove:     Called when the device is removed from the system to
 220 *              unbind a device from this driver.
 221 * @shutdown:   Called at shut-down time to quiesce the device.
 222 * @suspend:    Called to put the device to sleep mode. Usually to a
 223 *              low power state.
 224 * @resume:     Called to bring a device from sleep mode.
 225 * @groups:     Default attributes that get created by the driver core
 226 *              automatically.
 227 * @pm:         Power management operations of the device which matched
 228 *              this driver.
 229 * @p:          Driver core's private data, no one other than the driver
 230 *              core can touch this.
 231 *
 232 * The device driver-model tracks all of the drivers known to the system.
 233 * The main reason for this tracking is to enable the driver core to match
 234 * up drivers with new devices. Once drivers are known objects within the
 235 * system, however, a number of other things become possible. Device drivers
 236 * can export information and configuration variables that are independent
 237 * of any specific device.
 238 */
 239struct device_driver {
 240        const char              *name;
 241        struct bus_type         *bus;
 242
 243        struct module           *owner;
 244        const char              *mod_name;      /* used for built-in modules */
 245
 246        bool suppress_bind_attrs;       /* disables bind/unbind via sysfs */
 247
 248        const struct of_device_id       *of_match_table;
 249        const struct acpi_device_id     *acpi_match_table;
 250
 251        int (*probe) (struct device *dev);
 252        int (*remove) (struct device *dev);
 253        void (*shutdown) (struct device *dev);
 254        int (*suspend) (struct device *dev, pm_message_t state);
 255        int (*resume) (struct device *dev);
 256        const struct attribute_group **groups;
 257
 258        const struct dev_pm_ops *pm;
 259
 260        struct driver_private *p;
 261
 262        /* Extension to accomodate future upstream changes to this structure
 263         * yet maintain RHEL7 KABI.  For Red Hat internal use only!
 264         */
 265        struct device_driver_rh *device_driver_rh;
 266};
 267
 268/* RHEL7 specific 'struct device_driver' shadow structure to help maintain
 269 * KABI going forward.  This structure will never be under KABI restrictions.
 270 */
 271struct device_driver_rh {
 272#ifndef __GENKSYMS__
 273#endif
 274};
 275
 276extern int __must_check driver_register(struct device_driver *drv);
 277extern void driver_unregister(struct device_driver *drv);
 278
 279extern struct device_driver *driver_find(const char *name,
 280                                         struct bus_type *bus);
 281extern int driver_probe_done(void);
 282extern void wait_for_device_probe(void);
 283
 284
 285/* sysfs interface for exporting driver attributes */
 286
 287struct driver_attribute {
 288        struct attribute attr;
 289        ssize_t (*show)(struct device_driver *driver, char *buf);
 290        ssize_t (*store)(struct device_driver *driver, const char *buf,
 291                         size_t count);
 292};
 293
 294#define DRIVER_ATTR(_name, _mode, _show, _store) \
 295        struct driver_attribute driver_attr_##_name = __ATTR(_name, _mode, _show, _store)
 296#define DRIVER_ATTR_RW(_name) \
 297        struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
 298#define DRIVER_ATTR_RO(_name) \
 299        struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
 300#define DRIVER_ATTR_WO(_name) \
 301        struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
 302
 303extern int __must_check driver_create_file(struct device_driver *driver,
 304                                        const struct driver_attribute *attr);
 305extern void driver_remove_file(struct device_driver *driver,
 306                               const struct driver_attribute *attr);
 307
 308extern int __must_check driver_for_each_device(struct device_driver *drv,
 309                                               struct device *start,
 310                                               void *data,
 311                                               int (*fn)(struct device *dev,
 312                                                         void *));
 313struct device *driver_find_device(struct device_driver *drv,
 314                                  struct device *start, void *data,
 315                                  int (*match)(struct device *dev, void *data));
 316
 317/**
 318 * struct subsys_interface - interfaces to device functions
 319 * @name:       name of the device function
 320 * @subsys:     subsytem of the devices to attach to
 321 * @node:       the list of functions registered at the subsystem
 322 * @add_dev:    device hookup to device function handler
 323 * @remove_dev: device hookup to device function handler
 324 *
 325 * Simple interfaces attached to a subsystem. Multiple interfaces can
 326 * attach to a subsystem and its devices. Unlike drivers, they do not
 327 * exclusively claim or control devices. Interfaces usually represent
 328 * a specific functionality of a subsystem/class of devices.
 329 */
 330struct subsys_interface {
 331        const char *name;
 332        struct bus_type *subsys;
 333        struct list_head node;
 334        int (*add_dev)(struct device *dev, struct subsys_interface *sif);
 335        void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
 336};
 337
 338int subsys_interface_register(struct subsys_interface *sif);
 339void subsys_interface_unregister(struct subsys_interface *sif);
 340
 341int subsys_system_register(struct bus_type *subsys,
 342                           const struct attribute_group **groups);
 343int subsys_virtual_register(struct bus_type *subsys,
 344                            const struct attribute_group **groups);
 345
 346/**
 347 * struct class - device classes
 348 * @name:       Name of the class.
 349 * @owner:      The module owner.
 350 * @class_attrs: Default attributes of this class.
 351 * @dev_groups: Default attributes of the devices that belong to the class.
 352 * @dev_attrs:  Default attributes of the devices belong to the class.
 353 * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
 354 * @dev_kobj:   The kobject that represents this class and links it into the hierarchy.
 355 * @dev_uevent: Called when a device is added, removed from this class, or a
 356 *              few other things that generate uevents to add the environment
 357 *              variables.
 358 * @devnode:    Callback to provide the devtmpfs.
 359 * @class_release: Called to release this class.
 360 * @dev_release: Called to release the device.
 361 * @suspend:    Used to put the device to sleep mode, usually to a low power
 362 *              state.
 363 * @resume:     Used to bring the device from the sleep mode.
 364 * @ns_type:    Callbacks so sysfs can detemine namespaces.
 365 * @namespace:  Namespace of the device belongs to this class.
 366 * @pm:         The default device power management operations of this class.
 367 * @p:          The private data of the driver core, no one other than the
 368 *              driver core can touch this.
 369 *
 370 * A class is a higher-level view of a device that abstracts out low-level
 371 * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
 372 * at the class level, they are all simply disks. Classes allow user space
 373 * to work with devices based on what they do, rather than how they are
 374 * connected or how they work.
 375 */
 376struct class {
 377        const char              *name;
 378        struct module           *owner;
 379
 380        struct class_attribute          *class_attrs;
 381        struct device_attribute         *dev_attrs;     /* use dev_groups instead */
 382        const struct attribute_group    **dev_groups;
 383        struct bin_attribute            *dev_bin_attrs;
 384        struct kobject                  *dev_kobj;
 385
 386        int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
 387        char *(*devnode)(struct device *dev, umode_t *mode);
 388
 389        void (*class_release)(struct class *class);
 390        void (*dev_release)(struct device *dev);
 391
 392        int (*suspend)(struct device *dev, pm_message_t state);
 393        int (*resume)(struct device *dev);
 394
 395        const struct kobj_ns_type_operations *ns_type;
 396        const void *(*namespace)(struct device *dev);
 397
 398        const struct dev_pm_ops *pm;
 399
 400        struct subsys_private *p;
 401};
 402
 403struct class_dev_iter {
 404        struct klist_iter               ki;
 405        const struct device_type        *type;
 406};
 407
 408extern struct kobject *sysfs_dev_block_kobj;
 409extern struct kobject *sysfs_dev_char_kobj;
 410extern int __must_check __class_register(struct class *class,
 411                                         struct lock_class_key *key);
 412extern void class_unregister(struct class *class);
 413
 414/* This is a #define to keep the compiler from merging different
 415 * instances of the __key variable */
 416#define class_register(class)                   \
 417({                                              \
 418        static struct lock_class_key __key;     \
 419        __class_register(class, &__key);        \
 420})
 421
 422struct class_compat;
 423struct class_compat *class_compat_register(const char *name);
 424void class_compat_unregister(struct class_compat *cls);
 425int class_compat_create_link(struct class_compat *cls, struct device *dev,
 426                             struct device *device_link);
 427void class_compat_remove_link(struct class_compat *cls, struct device *dev,
 428                              struct device *device_link);
 429
 430extern void class_dev_iter_init(struct class_dev_iter *iter,
 431                                struct class *class,
 432                                struct device *start,
 433                                const struct device_type *type);
 434extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
 435extern void class_dev_iter_exit(struct class_dev_iter *iter);
 436
 437extern int class_for_each_device(struct class *class, struct device *start,
 438                                 void *data,
 439                                 int (*fn)(struct device *dev, void *data));
 440extern struct device *class_find_device(struct class *class,
 441                                        struct device *start, const void *data,
 442                                        int (*match)(struct device *, const void *));
 443
 444struct class_attribute {
 445        struct attribute attr;
 446        ssize_t (*show)(struct class *class, struct class_attribute *attr,
 447                        char *buf);
 448        ssize_t (*store)(struct class *class, struct class_attribute *attr,
 449                        const char *buf, size_t count);
 450        RH_KABI_DEPRECATE_FN(const void *, namespace, struct class *, const struct class_attribute *)
 451};
 452
 453#define CLASS_ATTR(_name, _mode, _show, _store) \
 454        struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
 455#define CLASS_ATTR_RW(_name) \
 456        struct class_attribute class_attr_##_name = __ATTR_RW(_name)
 457#define CLASS_ATTR_RO(_name) \
 458        struct class_attribute class_attr_##_name = __ATTR_RO(_name)
 459
 460extern int __must_check class_create_file_ns(struct class *class,
 461                                             const struct class_attribute *attr,
 462                                             const void *ns);
 463extern void class_remove_file_ns(struct class *class,
 464                                 const struct class_attribute *attr,
 465                                 const void *ns);
 466
 467static inline int __must_check class_create_file(struct class *class,
 468                                        const struct class_attribute *attr)
 469{
 470        return class_create_file_ns(class, attr, NULL);
 471}
 472
 473static inline void class_remove_file(struct class *class,
 474                                     const struct class_attribute *attr)
 475{
 476        return class_remove_file_ns(class, attr, NULL);
 477}
 478
 479/* Simple class attribute that is just a static string */
 480struct class_attribute_string {
 481        struct class_attribute attr;
 482        char *str;
 483};
 484
 485/* Currently read-only only */
 486#define _CLASS_ATTR_STRING(_name, _mode, _str) \
 487        { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
 488#define CLASS_ATTR_STRING(_name, _mode, _str) \
 489        struct class_attribute_string class_attr_##_name = \
 490                _CLASS_ATTR_STRING(_name, _mode, _str)
 491
 492extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
 493                        char *buf);
 494
 495struct class_interface {
 496        struct list_head        node;
 497        struct class            *class;
 498
 499        int (*add_dev)          (struct device *, struct class_interface *);
 500        void (*remove_dev)      (struct device *, struct class_interface *);
 501};
 502
 503extern int __must_check class_interface_register(struct class_interface *);
 504extern void class_interface_unregister(struct class_interface *);
 505
 506extern struct class * __must_check __class_create(struct module *owner,
 507                                                  const char *name,
 508                                                  struct lock_class_key *key);
 509extern void class_destroy(struct class *cls);
 510
 511/* This is a #define to keep the compiler from merging different
 512 * instances of the __key variable */
 513#define class_create(owner, name)               \
 514({                                              \
 515        static struct lock_class_key __key;     \
 516        __class_create(owner, name, &__key);    \
 517})
 518
 519/*
 520 * The type of device, "struct device" is embedded in. A class
 521 * or bus can contain devices of different types
 522 * like "partitions" and "disks", "mouse" and "event".
 523 * This identifies the device type and carries type-specific
 524 * information, equivalent to the kobj_type of a kobject.
 525 * If "name" is specified, the uevent will contain it in
 526 * the DEVTYPE variable.
 527 */
 528struct device_type {
 529        const char *name;
 530        const struct attribute_group **groups;
 531        int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
 532        char *(*devnode)(struct device *dev, umode_t *mode,
 533                         kuid_t *uid, kgid_t *gid);
 534        void (*release)(struct device *dev);
 535
 536        const struct dev_pm_ops *pm;
 537};
 538
 539/* interface for exporting device attributes */
 540struct device_attribute {
 541        struct attribute        attr;
 542        ssize_t (*show)(struct device *dev, struct device_attribute *attr,
 543                        char *buf);
 544        ssize_t (*store)(struct device *dev, struct device_attribute *attr,
 545                         const char *buf, size_t count);
 546};
 547
 548struct dev_ext_attribute {
 549        struct device_attribute attr;
 550        void *var;
 551};
 552
 553ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
 554                          char *buf);
 555ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
 556                           const char *buf, size_t count);
 557ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
 558                        char *buf);
 559ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
 560                         const char *buf, size_t count);
 561ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
 562                        char *buf);
 563ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
 564                         const char *buf, size_t count);
 565
 566#define DEVICE_ATTR(_name, _mode, _show, _store) \
 567        struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
 568#define DEVICE_ATTR_RW(_name) \
 569        struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
 570#define DEVICE_ATTR_RO(_name) \
 571        struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
 572#define DEVICE_ATTR_WO(_name) \
 573        struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
 574#define DEVICE_ULONG_ATTR(_name, _mode, _var) \
 575        struct dev_ext_attribute dev_attr_##_name = \
 576                { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
 577#define DEVICE_INT_ATTR(_name, _mode, _var) \
 578        struct dev_ext_attribute dev_attr_##_name = \
 579                { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
 580#define DEVICE_BOOL_ATTR(_name, _mode, _var) \
 581        struct dev_ext_attribute dev_attr_##_name = \
 582                { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
 583#define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
 584        struct device_attribute dev_attr_##_name =              \
 585                __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
 586
 587extern int device_create_file(struct device *device,
 588                              const struct device_attribute *entry);
 589extern void device_remove_file(struct device *dev,
 590                               const struct device_attribute *attr);
 591extern bool device_remove_file_self(struct device *dev,
 592                                    const struct device_attribute *attr);
 593extern int __must_check device_create_bin_file(struct device *dev,
 594                                        const struct bin_attribute *attr);
 595extern void device_remove_bin_file(struct device *dev,
 596                                   const struct bin_attribute *attr);
 597extern int device_schedule_callback_owner(struct device *dev,
 598                void (*func)(struct device *dev), struct module *owner);
 599
 600/* This is a macro to avoid include problems with THIS_MODULE */
 601#define device_schedule_callback(dev, func)                     \
 602        device_schedule_callback_owner(dev, func, THIS_MODULE)
 603
 604/* device resource management */
 605typedef void (*dr_release_t)(struct device *dev, void *res);
 606typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
 607
 608#ifdef CONFIG_DEBUG_DEVRES
 609extern void *__devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
 610                                 int nid, const char *name);
 611#define devres_alloc(release, size, gfp) \
 612        __devres_alloc_node(release, size, gfp, NUMA_NO_NODE, #release)
 613#define devres_alloc_node(release, size, gfp, nid) \
 614        __devres_alloc_node(release, size, gfp, nid, #release)
 615#else
 616extern void *devres_alloc_node(dr_release_t release, size_t size, gfp_t gfp,
 617                               int nid);
 618static inline void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp)
 619{
 620        return devres_alloc_node(release, size, gfp, NUMA_NO_NODE);
 621}
 622#endif
 623
 624extern void devres_for_each_res(struct device *dev, dr_release_t release,
 625                                dr_match_t match, void *match_data,
 626                                void (*fn)(struct device *, void *, void *),
 627                                void *data);
 628extern void devres_free(void *res);
 629extern void devres_add(struct device *dev, void *res);
 630extern void *devres_find(struct device *dev, dr_release_t release,
 631                         dr_match_t match, void *match_data);
 632extern void *devres_get(struct device *dev, void *new_res,
 633                        dr_match_t match, void *match_data);
 634extern void *devres_remove(struct device *dev, dr_release_t release,
 635                           dr_match_t match, void *match_data);
 636extern int devres_destroy(struct device *dev, dr_release_t release,
 637                          dr_match_t match, void *match_data);
 638extern int devres_release(struct device *dev, dr_release_t release,
 639                          dr_match_t match, void *match_data);
 640
 641/* devres group */
 642extern void * __must_check devres_open_group(struct device *dev, void *id,
 643                                             gfp_t gfp);
 644extern void devres_close_group(struct device *dev, void *id);
 645extern void devres_remove_group(struct device *dev, void *id);
 646extern int devres_release_group(struct device *dev, void *id);
 647
 648/* managed devm_k.alloc/kfree for device drivers */
 649extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
 650extern char *devm_kvasprintf(struct device *dev, gfp_t gfp, const char *fmt,
 651                             va_list ap);
 652extern char *devm_kasprintf(struct device *dev, gfp_t gfp,
 653                            const char *fmt, ...);
 654static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
 655{
 656        return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
 657}
 658static inline void *devm_kmalloc_array(struct device *dev,
 659                                       size_t n, size_t size, gfp_t flags)
 660{
 661        if (size != 0 && n > SIZE_MAX / size)
 662                return NULL;
 663        return devm_kmalloc(dev, n * size, flags);
 664}
 665static inline void *devm_kcalloc(struct device *dev,
 666                                 size_t n, size_t size, gfp_t flags)
 667{
 668        return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
 669}
 670extern void devm_kfree(struct device *dev, void *p);
 671extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
 672extern void *devm_kmemdup(struct device *dev, const void *src, size_t len,
 673                          gfp_t gfp);
 674
 675void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
 676void __iomem *devm_request_and_ioremap(struct device *dev,
 677                        struct resource *res);
 678
 679/* allows to add/remove a custom action to devres stack */
 680int devm_add_action(struct device *dev, void (*action)(void *), void *data);
 681void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
 682
 683static inline int devm_add_action_or_reset(struct device *dev,
 684                                           void (*action)(void *), void *data)
 685{
 686        int ret;
 687
 688        ret = devm_add_action(dev, action, data);
 689        if (ret)
 690                action(data);
 691
 692        return ret;
 693}
 694
 695struct device_dma_parameters {
 696        /*
 697         * a low level driver may set these to teach IOMMU code about
 698         * sg limitations.
 699         */
 700        unsigned int max_segment_size;
 701        unsigned long segment_boundary_mask;
 702};
 703
 704/**
 705 * struct device_connection - Device Connection Descriptor
 706 * @endpoint: The names of the two devices connected together
 707 * @id: Unique identifier for the connection
 708 * @list: List head, private, for internal use only
 709 */
 710struct device_connection {
 711        const char              *endpoint[2];
 712        const char              *id;
 713        struct list_head        list;
 714};
 715
 716void *device_connection_find_match(struct device *dev, const char *con_id,
 717                                void *data,
 718                                void *(*match)(struct device_connection *con,
 719                                               int ep, void *data));
 720
 721struct device *device_connection_find(struct device *dev, const char *con_id);
 722
 723void device_connection_add(struct device_connection *con);
 724void device_connection_remove(struct device_connection *con);
 725
 726/* RHEL7: Do not use this struct.  It is only here for KABI purposes. */
 727struct acpi_dev_node {
 728#ifdef CONFIG_ACPI
 729        RH_KABI_REPLACE(void    *handle,
 730                          struct acpi_device *companion)
 731
 732#endif
 733};
 734
 735/**
 736 * struct device - The basic device structure
 737 * @parent:     The device's "parent" device, the device to which it is attached.
 738 *              In most cases, a parent device is some sort of bus or host
 739 *              controller. If parent is NULL, the device, is a top-level device,
 740 *              which is not usually what you want.
 741 * @p:          Holds the private data of the driver core portions of the device.
 742 *              See the comment of the struct device_private for detail.
 743 * @kobj:       A top-level, abstract class from which other classes are derived.
 744 * @init_name:  Initial name of the device.
 745 * @type:       The type of device.
 746 *              This identifies the device type and carries type-specific
 747 *              information.
 748 * @mutex:      Mutex to synchronize calls to its driver.
 749 * @bus:        Type of bus device is on.
 750 * @driver:     Which driver has allocated this
 751 * @platform_data: Platform data specific to the device.
 752 *              Example: For devices on custom boards, as typical of embedded
 753 *              and SOC based hardware, Linux often uses platform_data to point
 754 *              to board-specific structures describing devices and how they
 755 *              are wired.  That can include what ports are available, chip
 756 *              variants, which GPIO pins act in what additional roles, and so
 757 *              on.  This shrinks the "Board Support Packages" (BSPs) and
 758 *              minimizes board-specific #ifdefs in drivers.
 759 * @power:      For device power management.
 760 *              See Documentation/power/devices.txt for details.
 761 * @pm_domain:  Provide callbacks that are executed during system suspend,
 762 *              hibernation, system resume and during runtime PM transitions
 763 *              along with subsystem-level and driver-level callbacks.
 764 * @pins:       For device pin management.
 765 *              See Documentation/pinctrl.txt for details.
 766 * @numa_node:  NUMA node this device is close to.
 767 * @dma_mask:   Dma mask (if dma'ble device).
 768 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
 769 *              hardware supports 64-bit addresses for consistent allocations
 770 *              such descriptors.
 771 * @dma_pfn_offset: offset of DMA memory range relatively of RAM
 772 * @dma_parms:  A low level driver may set these to teach IOMMU code about
 773 *              segment limitations.
 774 * @dma_pools:  Dma pools (if dma'ble device).
 775 * @dma_mem:    Internal for coherent mem override.
 776 * @archdata:   For arch-specific additions.
 777 * @of_node:    Associated device tree node.
 778 * @fwnode:     Associated device node supplied by platform firmware.
 779 * @devt:       For creating the sysfs "dev".
 780 * @id:         device instance
 781 * @devres_lock: Spinlock to protect the resource of the device.
 782 * @devres_head: The resources list of the device.
 783 * @knode_class: The node used to add the device to the class list.
 784 * @class:      The class of the device.
 785 * @groups:     Optional attribute groups.
 786 * @release:    Callback to free the device after all references have
 787 *              gone away. This should be set by the allocator of the
 788 *              device (i.e. the bus driver that discovered the device).
 789 * @offline_disabled: If set, the device is permanently online.
 790 * @offline:    Set after successful invocation of bus type's .offline().
 791 * @of_node_reused: Set if the device-tree node is shared with an ancestor
 792 *              device.
 793 *
 794 * At the lowest level, every device in a Linux system is represented by an
 795 * instance of struct device. The device structure contains the information
 796 * that the device model core needs to model the system. Most subsystems,
 797 * however, track additional information about the devices they host. As a
 798 * result, it is rare for devices to be represented by bare device structures;
 799 * instead, that structure, like kobject structures, is usually embedded within
 800 * a higher-level representation of the device.
 801 */
 802struct device {
 803        struct device           *parent;
 804
 805        struct device_private   *p;
 806
 807        struct kobject kobj;
 808        const char              *init_name; /* initial name of the device */
 809        const struct device_type *type;
 810
 811        struct mutex            mutex;  /* mutex to synchronize calls to
 812                                         * its driver.
 813                                         */
 814
 815        struct bus_type *bus;           /* type of bus device is on */
 816        struct device_driver *driver;   /* which driver has allocated this
 817                                           device */
 818        void            *platform_data; /* Platform specific data, device
 819                                           core doesn't touch it */
 820        struct dev_pm_info      power;
 821        struct dev_pm_domain    *pm_domain;
 822
 823#ifdef CONFIG_NUMA
 824        int             numa_node;      /* NUMA node this device is close to */
 825#endif
 826        u64             *dma_mask;      /* dma mask (if dma'able device) */
 827        u64             coherent_dma_mask;/* Like dma_mask, but for
 828                                             alloc_coherent mappings as
 829                                             not all hardware supports
 830                                             64 bit addresses for consistent
 831                                             allocations such descriptors. */
 832
 833        struct device_dma_parameters *dma_parms;
 834
 835        struct list_head        dma_pools;      /* dma pools (if dma'ble) */
 836
 837        struct dma_coherent_mem *dma_mem; /* internal for coherent mem
 838                                             override */
 839#ifdef CONFIG_DMA_CMA
 840        struct cma *cma_area;           /* contiguous memory area for dma
 841                                           allocations */
 842#endif
 843        /* arch specific additions */
 844        struct dev_archdata     archdata;
 845
 846        struct device_node      *of_node; /* associated device tree node */
 847        /*
 848         * RHEL7: This appears safe to do.  struct acpi_dev_node was
 849         * previously safely modified with a KABI workaround so deprecating
 850         * it should also not be a problem.
 851         */
 852        RH_KABI_DEPRECATE(struct acpi_dev_node, acpi_node)
 853
 854        dev_t                   devt;   /* dev_t, creates the sysfs "dev" */
 855        u32                     id;     /* device instance */
 856
 857        spinlock_t              devres_lock;
 858        struct list_head        devres_head;
 859
 860        struct klist_node       knode_class;
 861        struct class            *class;
 862        const struct attribute_group **groups;  /* optional groups */
 863
 864        void    (*release)(struct device *dev);
 865        struct iommu_group      *iommu_group;
 866
 867        bool                    offline_disabled:1;
 868        bool                    offline:1;
 869
 870        /* Extension to accomodate future upstream changes to this structure
 871         * yet maintain RHEL7 KABI.  For Red Hat internal use only!
 872         */
 873        struct device_rh        *device_rh;
 874};
 875
 876/* RHEL7 specific 'struct device_rh' shadow structure to help maintain KABI
 877 * going forward.  This structure will never be under KABI restrictions.
 878 */
 879struct device_rh {
 880        RH_KABI_EXTEND(struct dev_pm_info_rh power)
 881#ifdef CONFIG_PINCTRL
 882        RH_KABI_EXTEND(struct dev_pin_info *pins)
 883#endif
 884        RH_KABI_EXTEND(struct fwnode_handle *fwnode)
 885        RH_KABI_EXTEND(struct dma_map_ops *dma_ops)
 886
 887        /* RHEL7: due to KABI this can't go into struct class */
 888        RH_KABI_EXTEND(int (*class_shutdown_pre)(struct device *dev))
 889
 890        /* RHEL7: due to KABI this can't go into struct class */
 891        RH_KABI_EXTEND(unsigned long  dma_pfn_offset)
 892        RH_KABI_EXTEND(bool of_node_reused:1)
 893
 894};
 895/* allocator for device_rh */
 896extern void device_rh_alloc(struct device *dev);
 897extern void device_rh_free(struct device *dev);
 898
 899static inline struct device *kobj_to_dev(struct kobject *kobj)
 900{
 901        return container_of(kobj, struct device, kobj);
 902}
 903
 904/* Get the wakeup routines, which depend on struct device */
 905#include <linux/pm_wakeup.h>
 906
 907static inline const char *dev_name(const struct device *dev)
 908{
 909        /* Use the init name until the kobject becomes available */
 910        if (dev->init_name)
 911                return dev->init_name;
 912
 913        return kobject_name(&dev->kobj);
 914}
 915
 916extern __printf(2, 3)
 917int dev_set_name(struct device *dev, const char *name, ...);
 918
 919#ifdef CONFIG_NUMA
 920static inline int dev_to_node(struct device *dev)
 921{
 922        return dev->numa_node;
 923}
 924static inline void set_dev_node(struct device *dev, int node)
 925{
 926        dev->numa_node = node;
 927}
 928#else
 929static inline int dev_to_node(struct device *dev)
 930{
 931        return -1;
 932}
 933static inline void set_dev_node(struct device *dev, int node)
 934{
 935}
 936#endif
 937
 938static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
 939{
 940        return dev ? dev->power.subsys_data : NULL;
 941}
 942
 943static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
 944{
 945        return dev->kobj.uevent_suppress;
 946}
 947
 948static inline void dev_set_uevent_suppress(struct device *dev, int val)
 949{
 950        dev->kobj.uevent_suppress = val;
 951}
 952
 953static inline int device_is_registered(struct device *dev)
 954{
 955        return dev->kobj.state_in_sysfs;
 956}
 957
 958static inline void device_enable_async_suspend(struct device *dev)
 959{
 960        if (!dev->power.is_prepared)
 961                dev->power.async_suspend = true;
 962}
 963
 964static inline void device_disable_async_suspend(struct device *dev)
 965{
 966        if (!dev->power.is_prepared)
 967                dev->power.async_suspend = false;
 968}
 969
 970static inline bool device_async_suspend_enabled(struct device *dev)
 971{
 972        return !!dev->power.async_suspend;
 973}
 974
 975static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
 976{
 977        dev->power.ignore_children = enable;
 978}
 979
 980static inline void dev_pm_syscore_device(struct device *dev, bool val)
 981{
 982#ifdef CONFIG_PM_SLEEP
 983        dev->power.syscore = val;
 984#endif
 985}
 986
 987static inline void device_lock(struct device *dev)
 988{
 989        mutex_lock(&dev->mutex);
 990}
 991
 992static inline int device_lock_interruptible(struct device *dev)
 993{
 994        return mutex_lock_interruptible(&dev->mutex);
 995}
 996
 997static inline int device_trylock(struct device *dev)
 998{
 999        return mutex_trylock(&dev->mutex);
1000}
1001
1002static inline void device_unlock(struct device *dev)
1003{
1004        mutex_unlock(&dev->mutex);
1005}
1006
1007static inline struct device_node *dev_of_node(struct device *dev)
1008{
1009        if (!IS_ENABLED(CONFIG_OF))
1010                return NULL;
1011        return dev->of_node;
1012}
1013
1014void driver_init(void);
1015
1016/*
1017 * High level routines for use by the bus drivers
1018 */
1019extern int __must_check device_register(struct device *dev);
1020extern void device_unregister(struct device *dev);
1021extern void device_initialize(struct device *dev);
1022extern int __must_check device_add(struct device *dev);
1023extern void device_del(struct device *dev);
1024extern int device_for_each_child(struct device *dev, void *data,
1025                     int (*fn)(struct device *dev, void *data));
1026extern int device_for_each_child_reverse(struct device *dev, void *data,
1027                     int (*fn)(struct device *dev, void *data));
1028extern struct device *device_find_child(struct device *dev, void *data,
1029                                int (*match)(struct device *dev, void *data));
1030extern int device_rename(struct device *dev, const char *new_name);
1031extern int device_move(struct device *dev, struct device *new_parent,
1032                       enum dpm_order dpm_order);
1033extern const char *device_get_devnode(struct device *dev,
1034                                      umode_t *mode, kuid_t *uid, kgid_t *gid,
1035                                      const char **tmp);
1036extern void *dev_get_drvdata(const struct device *dev);
1037extern int dev_set_drvdata(struct device *dev, void *data);
1038
1039static inline bool device_supports_offline(struct device *dev)
1040{
1041        return dev->bus && dev->bus->offline && dev->bus->online;
1042}
1043
1044extern void lock_device_hotplug(void);
1045extern void unlock_device_hotplug(void);
1046extern int lock_device_hotplug_sysfs(void);
1047void assert_held_device_hotplug(void);
1048extern int device_offline(struct device *dev);
1049extern int device_online(struct device *dev);
1050extern void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1051extern void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode);
1052void device_set_of_node_from_dev(struct device *dev, const struct device *dev2);
1053
1054/*
1055 * Root device objects for grouping under /sys/devices
1056 */
1057extern struct device *__root_device_register(const char *name,
1058                                             struct module *owner);
1059
1060/*
1061 * This is a macro to avoid include problems with THIS_MODULE,
1062 * just as per what is done for device_schedule_callback() above.
1063 */
1064#define root_device_register(name) \
1065        __root_device_register(name, THIS_MODULE)
1066
1067extern void root_device_unregister(struct device *root);
1068
1069static inline void *dev_get_platdata(const struct device *dev)
1070{
1071        return dev->platform_data;
1072}
1073
1074/*
1075 * Manual binding of a device to driver. See drivers/base/bus.c
1076 * for information on use.
1077 */
1078extern int __must_check device_bind_driver(struct device *dev);
1079extern void device_release_driver(struct device *dev);
1080extern int  __must_check device_attach(struct device *dev);
1081extern int __must_check driver_attach(struct device_driver *drv);
1082extern int __must_check device_reprobe(struct device *dev);
1083
1084extern bool device_is_bound(struct device *dev);
1085
1086/*
1087 * Easy functions for dynamically creating devices on the fly
1088 */
1089extern struct device *device_create_vargs(struct class *cls,
1090                                          struct device *parent,
1091                                          dev_t devt,
1092                                          void *drvdata,
1093                                          const char *fmt,
1094                                          va_list vargs);
1095extern __printf(5, 6)
1096struct device *device_create(struct class *cls, struct device *parent,
1097                             dev_t devt, void *drvdata,
1098                             const char *fmt, ...);
1099extern __printf(6, 7)
1100struct device *device_create_with_groups(struct class *cls,
1101                             struct device *parent, dev_t devt, void *drvdata,
1102                             const struct attribute_group **groups,
1103                             const char *fmt, ...);
1104extern void device_destroy(struct class *cls, dev_t devt);
1105
1106extern int __must_check device_add_groups(struct device *dev,
1107                                        const struct attribute_group **groups);
1108extern void device_remove_groups(struct device *dev,
1109                                 const struct attribute_group **groups);
1110
1111static inline int __must_check device_add_group(struct device *dev,
1112                                        const struct attribute_group *grp)
1113{
1114        const struct attribute_group *groups[] = { grp, NULL };
1115
1116        return device_add_groups(dev, groups);
1117}
1118
1119static inline void device_remove_group(struct device *dev,
1120                                       const struct attribute_group *grp)
1121{
1122        const struct attribute_group *groups[] = { grp, NULL };
1123
1124        return device_remove_groups(dev, groups);
1125}
1126
1127/*
1128 * Platform "fixup" functions - allow the platform to have their say
1129 * about devices and actions that the general device layer doesn't
1130 * know about.
1131 */
1132/* Notify platform of device discovery */
1133extern int (*platform_notify)(struct device *dev);
1134
1135extern int (*platform_notify_remove)(struct device *dev);
1136
1137
1138/*
1139 * get_device - atomically increment the reference count for the device.
1140 *
1141 */
1142extern struct device *get_device(struct device *dev);
1143extern void put_device(struct device *dev);
1144
1145#ifdef CONFIG_DEVTMPFS
1146extern int devtmpfs_create_node(struct device *dev);
1147extern int devtmpfs_delete_node(struct device *dev);
1148extern int devtmpfs_mount(const char *mntdir);
1149#else
1150static inline int devtmpfs_create_node(struct device *dev) { return 0; }
1151static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
1152static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
1153#endif
1154
1155/* drivers/base/power/shutdown.c */
1156extern void device_shutdown(void);
1157
1158/* debugging and troubleshooting/diagnostic helpers. */
1159extern const char *dev_driver_string(const struct device *dev);
1160
1161
1162#ifdef CONFIG_PRINTK
1163
1164extern __printf(3, 0)
1165int dev_vprintk_emit(int level, const struct device *dev,
1166                     const char *fmt, va_list args);
1167extern __printf(3, 4)
1168int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
1169
1170extern __printf(3, 4)
1171int dev_printk(const char *level, const struct device *dev,
1172               const char *fmt, ...);
1173extern __printf(2, 3)
1174int dev_emerg(const struct device *dev, const char *fmt, ...);
1175extern __printf(2, 3)
1176int dev_alert(const struct device *dev, const char *fmt, ...);
1177extern __printf(2, 3)
1178int dev_crit(const struct device *dev, const char *fmt, ...);
1179extern __printf(2, 3)
1180int dev_err(const struct device *dev, const char *fmt, ...);
1181extern __printf(2, 3)
1182int dev_warn(const struct device *dev, const char *fmt, ...);
1183extern __printf(2, 3)
1184int dev_notice(const struct device *dev, const char *fmt, ...);
1185extern __printf(2, 3)
1186int _dev_info(const struct device *dev, const char *fmt, ...);
1187
1188#else
1189
1190static inline __printf(3, 0)
1191int dev_vprintk_emit(int level, const struct device *dev,
1192                     const char *fmt, va_list args)
1193{ return 0; }
1194static inline __printf(3, 4)
1195int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1196{ return 0; }
1197
1198static inline int __dev_printk(const char *level, const struct device *dev,
1199                               struct va_format *vaf)
1200{ return 0; }
1201static inline __printf(3, 4)
1202int dev_printk(const char *level, const struct device *dev,
1203               const char *fmt, ...)
1204{ return 0; }
1205
1206static inline __printf(2, 3)
1207int dev_emerg(const struct device *dev, const char *fmt, ...)
1208{ return 0; }
1209static inline __printf(2, 3)
1210int dev_crit(const struct device *dev, const char *fmt, ...)
1211{ return 0; }
1212static inline __printf(2, 3)
1213int dev_alert(const struct device *dev, const char *fmt, ...)
1214{ return 0; }
1215static inline __printf(2, 3)
1216int dev_err(const struct device *dev, const char *fmt, ...)
1217{ return 0; }
1218static inline __printf(2, 3)
1219int dev_warn(const struct device *dev, const char *fmt, ...)
1220{ return 0; }
1221static inline __printf(2, 3)
1222int dev_notice(const struct device *dev, const char *fmt, ...)
1223{ return 0; }
1224static inline __printf(2, 3)
1225int _dev_info(const struct device *dev, const char *fmt, ...)
1226{ return 0; }
1227
1228#endif
1229
1230/*
1231 * Stupid hackaround for existing uses of non-printk uses dev_info
1232 *
1233 * Note that the definition of dev_info below is actually _dev_info
1234 * and a macro is used to avoid redefining dev_info
1235 */
1236
1237#define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
1238
1239#if defined(CONFIG_DYNAMIC_DEBUG)
1240#define dev_dbg(dev, format, ...)                    \
1241do {                                                 \
1242        dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
1243} while (0)
1244#elif defined(DEBUG)
1245#define dev_dbg(dev, format, arg...)            \
1246        dev_printk(KERN_DEBUG, dev, format, ##arg)
1247#else
1248#define dev_dbg(dev, format, arg...)                            \
1249({                                                              \
1250        if (0)                                                  \
1251                dev_printk(KERN_DEBUG, dev, format, ##arg);     \
1252        0;                                                      \
1253})
1254#endif
1255
1256#ifdef CONFIG_PRINTK
1257#define dev_level_once(dev_level, dev, fmt, ...)                        \
1258do {                                                                    \
1259        static bool __print_once __read_mostly;                         \
1260                                                                        \
1261        if (!__print_once) {                                            \
1262                __print_once = true;                                    \
1263                dev_level(dev, fmt, ##__VA_ARGS__);                     \
1264        }                                                               \
1265} while (0)
1266#else
1267#define dev_level_once(dev_level, dev, fmt, ...)                        \
1268do {                                                                    \
1269        if (0)                                                          \
1270                dev_level(dev, fmt, ##__VA_ARGS__);                     \
1271} while (0)
1272#endif
1273
1274#define dev_emerg_once(dev, fmt, ...)                                   \
1275        dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
1276#define dev_alert_once(dev, fmt, ...)                                   \
1277        dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
1278#define dev_crit_once(dev, fmt, ...)                                    \
1279        dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
1280#define dev_err_once(dev, fmt, ...)                                     \
1281        dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
1282#define dev_warn_once(dev, fmt, ...)                                    \
1283        dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
1284#define dev_notice_once(dev, fmt, ...)                                  \
1285        dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
1286#define dev_info_once(dev, fmt, ...)                                    \
1287        dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
1288#define dev_dbg_once(dev, fmt, ...)                                     \
1289        dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
1290
1291#define dev_level_ratelimited(dev_level, dev, fmt, ...)                 \
1292do {                                                                    \
1293        static DEFINE_RATELIMIT_STATE(_rs,                              \
1294                                      DEFAULT_RATELIMIT_INTERVAL,       \
1295                                      DEFAULT_RATELIMIT_BURST);         \
1296        if (__ratelimit(&_rs))                                          \
1297                dev_level(dev, fmt, ##__VA_ARGS__);                     \
1298} while (0)
1299
1300#define dev_emerg_ratelimited(dev, fmt, ...)                            \
1301        dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1302#define dev_alert_ratelimited(dev, fmt, ...)                            \
1303        dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1304#define dev_crit_ratelimited(dev, fmt, ...)                             \
1305        dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1306#define dev_err_ratelimited(dev, fmt, ...)                              \
1307        dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1308#define dev_warn_ratelimited(dev, fmt, ...)                             \
1309        dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1310#define dev_notice_ratelimited(dev, fmt, ...)                           \
1311        dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1312#define dev_info_ratelimited(dev, fmt, ...)                             \
1313        dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1314#if defined(CONFIG_DYNAMIC_DEBUG)
1315/* descriptor check is first to prevent flooding with "callbacks suppressed" */
1316#define dev_dbg_ratelimited(dev, fmt, ...)                              \
1317do {                                                                    \
1318        static DEFINE_RATELIMIT_STATE(_rs,                              \
1319                                      DEFAULT_RATELIMIT_INTERVAL,       \
1320                                      DEFAULT_RATELIMIT_BURST);         \
1321        DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);                 \
1322        if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) &&        \
1323            __ratelimit(&_rs))                                          \
1324                __dynamic_dev_dbg(&descriptor, dev, fmt,                \
1325                                  ##__VA_ARGS__);                       \
1326} while (0)
1327#elif defined(DEBUG)
1328#define dev_dbg_ratelimited(dev, fmt, ...)                              \
1329do {                                                                    \
1330        static DEFINE_RATELIMIT_STATE(_rs,                              \
1331                                      DEFAULT_RATELIMIT_INTERVAL,       \
1332                                      DEFAULT_RATELIMIT_BURST);         \
1333        if (__ratelimit(&_rs))                                          \
1334                dev_printk(KERN_DEBUG, dev, fmt, ##__VA_ARGS__);        \
1335} while (0)
1336#else
1337#define dev_dbg_ratelimited(dev, fmt, ...)                      \
1338        no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
1339#endif
1340
1341#ifdef VERBOSE_DEBUG
1342#define dev_vdbg        dev_dbg
1343#else
1344#define dev_vdbg(dev, format, arg...)                           \
1345({                                                              \
1346        if (0)                                                  \
1347                dev_printk(KERN_DEBUG, dev, format, ##arg);     \
1348        0;                                                      \
1349})
1350#endif
1351
1352/*
1353 * dev_WARN*() acts like dev_printk(), but with the key difference
1354 * of using a WARN/WARN_ON to get the message out, including the
1355 * file/line information and a backtrace.
1356 */
1357#define dev_WARN(dev, format, arg...) \
1358        WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
1359
1360#define dev_WARN_ONCE(dev, condition, format, arg...) \
1361        WARN_ONCE(condition, "Device %s\n" format, \
1362                        dev_driver_string(dev), ## arg)
1363
1364/* Create alias, so I can be autoloaded. */
1365#define MODULE_ALIAS_CHARDEV(major,minor) \
1366        MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1367#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1368        MODULE_ALIAS("char-major-" __stringify(major) "-*")
1369
1370#ifdef CONFIG_SYSFS_DEPRECATED
1371extern long sysfs_deprecated;
1372#else
1373#define sysfs_deprecated 0
1374#endif
1375
1376/**
1377 * module_driver() - Helper macro for drivers that don't do anything
1378 * special in module init/exit. This eliminates a lot of boilerplate.
1379 * Each module may only use this macro once, and calling it replaces
1380 * module_init() and module_exit().
1381 *
1382 * @__driver: driver name
1383 * @__register: register function for this driver type
1384 * @__unregister: unregister function for this driver type
1385 * @...: Additional arguments to be passed to __register and __unregister.
1386 *
1387 * Use this macro to construct bus specific macros for registering
1388 * drivers, and do not use it on its own.
1389 */
1390#define module_driver(__driver, __register, __unregister, ...) \
1391static int __init __driver##_init(void) \
1392{ \
1393        return __register(&(__driver) , ##__VA_ARGS__); \
1394} \
1395module_init(__driver##_init); \
1396static void __exit __driver##_exit(void) \
1397{ \
1398        __unregister(&(__driver) , ##__VA_ARGS__); \
1399} \
1400module_exit(__driver##_exit);
1401
1402#endif /* _DEVICE_H_ */
1403