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