2 * device.h - generic, centralized driver model
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.
8 * This file is released under the GPLv2
10 * See Documentation/driver-model/ for more information.
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>
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>
33 struct device_private;
35 struct driver_private;
38 struct subsys_private;
44 struct bus_attribute {
45 struct attribute attr;
46 ssize_t (*show)(struct bus_type *bus, char *buf);
47 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
50 #define BUS_ATTR(_name, _mode, _show, _store) \
51 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
52 #define BUS_ATTR_RW(_name) \
53 struct bus_attribute bus_attr_##_name = __ATTR_RW(_name)
54 #define BUS_ATTR_RO(_name) \
55 struct bus_attribute bus_attr_##_name = __ATTR_RO(_name)
57 extern int __must_check bus_create_file(struct bus_type *,
58 struct bus_attribute *);
59 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
62 * struct bus_type - The bus type of the device
64 * @name: The name of the bus.
65 * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
66 * @dev_root: Default device to use as the parent.
67 * @bus_attrs: Default attributes of the bus.
68 * @dev_attrs: Default attributes of the devices on the bus.
69 * @drv_attrs: Default attributes of the device drivers on the bus.
70 * @match: Called, perhaps multiple times, whenever a new device or driver
71 * is added for this bus. It should return a nonzero value if the
72 * given device can be handled by the given driver.
73 * @uevent: Called when a device is added, removed, or a few other things
74 * that generate uevents to add the environment variables.
75 * @probe: Called when a new device or driver add to this bus, and callback
76 * the specific driver's probe to initial the matched device.
77 * @remove: Called when a device removed from this bus.
78 * @shutdown: Called at shut-down time to quiesce the device.
79 * @suspend: Called when a device on this bus wants to go to sleep mode.
80 * @resume: Called to bring a device on this bus out of sleep mode.
81 * @pm: Power management operations of this bus, callback the specific
82 * device driver's pm-ops.
83 * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU
84 * driver implementations to a bus and allow the driver to do
86 * @p: The private data of the driver core, only the driver core can
89 * A bus is a channel between the processor and one or more devices. For the
90 * purposes of the device model, all devices are connected via a bus, even if
91 * it is an internal, virtual, "platform" bus. Buses can plug into each other.
92 * A USB controller is usually a PCI device, for example. The device model
93 * represents the actual connections between buses and the devices they control.
94 * A bus is represented by the bus_type structure. It contains the name, the
95 * default attributes, the bus' methods, PM operations, and the driver core's
100 const char *dev_name;
101 struct device *dev_root;
102 struct bus_attribute *bus_attrs;
103 struct device_attribute *dev_attrs;
104 struct driver_attribute *drv_attrs;
106 int (*match)(struct device *dev, struct device_driver *drv);
107 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
108 int (*probe)(struct device *dev);
109 int (*remove)(struct device *dev);
110 void (*shutdown)(struct device *dev);
112 int (*suspend)(struct device *dev, pm_message_t state);
113 int (*resume)(struct device *dev);
115 const struct dev_pm_ops *pm;
117 struct iommu_ops *iommu_ops;
119 struct subsys_private *p;
120 struct lock_class_key lock_key;
123 extern int __must_check bus_register(struct bus_type *bus);
125 extern void bus_unregister(struct bus_type *bus);
127 extern int __must_check bus_rescan_devices(struct bus_type *bus);
129 /* iterator helpers for buses */
130 struct subsys_dev_iter {
131 struct klist_iter ki;
132 const struct device_type *type;
134 void subsys_dev_iter_init(struct subsys_dev_iter *iter,
135 struct bus_type *subsys,
136 struct device *start,
137 const struct device_type *type);
138 struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter);
139 void subsys_dev_iter_exit(struct subsys_dev_iter *iter);
141 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
142 int (*fn)(struct device *dev, void *data));
143 struct device *bus_find_device(struct bus_type *bus, struct device *start,
145 int (*match)(struct device *dev, void *data));
146 struct device *bus_find_device_by_name(struct bus_type *bus,
147 struct device *start,
149 struct device *subsys_find_device_by_id(struct bus_type *bus, unsigned int id,
150 struct device *hint);
151 int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
152 void *data, int (*fn)(struct device_driver *, void *));
153 void bus_sort_breadthfirst(struct bus_type *bus,
154 int (*compare)(const struct device *a,
155 const struct device *b));
157 * Bus notifiers: Get notified of addition/removal of devices
158 * and binding/unbinding of drivers to devices.
159 * In the long run, it should be a replacement for the platform
162 struct notifier_block;
164 extern int bus_register_notifier(struct bus_type *bus,
165 struct notifier_block *nb);
166 extern int bus_unregister_notifier(struct bus_type *bus,
167 struct notifier_block *nb);
169 /* All 4 notifers below get called with the target struct device *
170 * as an argument. Note that those functions are likely to be called
171 * with the device lock held in the core, so be careful.
173 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
174 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
175 #define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be
177 #define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */
178 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be
180 #define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound
183 extern struct kset *bus_get_kset(struct bus_type *bus);
184 extern struct klist *bus_get_device_klist(struct bus_type *bus);
187 * struct device_driver - The basic device driver structure
188 * @name: Name of the device driver.
189 * @bus: The bus which the device of this driver belongs to.
190 * @owner: The module owner.
191 * @mod_name: Used for built-in modules.
192 * @suppress_bind_attrs: Disables bind/unbind via sysfs.
193 * @of_match_table: The open firmware table.
194 * @acpi_match_table: The ACPI match table.
195 * @probe: Called to query the existence of a specific device,
196 * whether this driver can work with it, and bind the driver
197 * to a specific device.
198 * @remove: Called when the device is removed from the system to
199 * unbind a device from this driver.
200 * @shutdown: Called at shut-down time to quiesce the device.
201 * @suspend: Called to put the device to sleep mode. Usually to a
203 * @resume: Called to bring a device from sleep mode.
204 * @groups: Default attributes that get created by the driver core
206 * @pm: Power management operations of the device which matched
208 * @p: Driver core's private data, no one other than the driver
209 * core can touch this.
211 * The device driver-model tracks all of the drivers known to the system.
212 * The main reason for this tracking is to enable the driver core to match
213 * up drivers with new devices. Once drivers are known objects within the
214 * system, however, a number of other things become possible. Device drivers
215 * can export information and configuration variables that are independent
216 * of any specific device.
218 struct device_driver {
220 struct bus_type *bus;
222 struct module *owner;
223 const char *mod_name; /* used for built-in modules */
225 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
227 const struct of_device_id *of_match_table;
228 const struct acpi_device_id *acpi_match_table;
230 int (*probe) (struct device *dev);
231 int (*remove) (struct device *dev);
232 void (*shutdown) (struct device *dev);
233 int (*suspend) (struct device *dev, pm_message_t state);
234 int (*resume) (struct device *dev);
235 const struct attribute_group **groups;
237 const struct dev_pm_ops *pm;
239 struct driver_private *p;
243 extern int __must_check driver_register(struct device_driver *drv);
244 extern void driver_unregister(struct device_driver *drv);
246 extern struct device_driver *driver_find(const char *name,
247 struct bus_type *bus);
248 extern int driver_probe_done(void);
249 extern void wait_for_device_probe(void);
252 /* sysfs interface for exporting driver attributes */
254 struct driver_attribute {
255 struct attribute attr;
256 ssize_t (*show)(struct device_driver *driver, char *buf);
257 ssize_t (*store)(struct device_driver *driver, const char *buf,
261 #define DRIVER_ATTR(_name, _mode, _show, _store) \
262 struct driver_attribute driver_attr_##_name = __ATTR(_name, _mode, _show, _store)
263 #define DRIVER_ATTR_RW(_name) \
264 struct driver_attribute driver_attr_##_name = __ATTR_RW(_name)
265 #define DRIVER_ATTR_RO(_name) \
266 struct driver_attribute driver_attr_##_name = __ATTR_RO(_name)
267 #define DRIVER_ATTR_WO(_name) \
268 struct driver_attribute driver_attr_##_name = __ATTR_WO(_name)
270 extern int __must_check driver_create_file(struct device_driver *driver,
271 const struct driver_attribute *attr);
272 extern void driver_remove_file(struct device_driver *driver,
273 const struct driver_attribute *attr);
275 extern int __must_check driver_for_each_device(struct device_driver *drv,
276 struct device *start,
278 int (*fn)(struct device *dev,
280 struct device *driver_find_device(struct device_driver *drv,
281 struct device *start, void *data,
282 int (*match)(struct device *dev, void *data));
285 * struct subsys_interface - interfaces to device functions
286 * @name: name of the device function
287 * @subsys: subsytem of the devices to attach to
288 * @node: the list of functions registered at the subsystem
289 * @add_dev: device hookup to device function handler
290 * @remove_dev: device hookup to device function handler
292 * Simple interfaces attached to a subsystem. Multiple interfaces can
293 * attach to a subsystem and its devices. Unlike drivers, they do not
294 * exclusively claim or control devices. Interfaces usually represent
295 * a specific functionality of a subsystem/class of devices.
297 struct subsys_interface {
299 struct bus_type *subsys;
300 struct list_head node;
301 int (*add_dev)(struct device *dev, struct subsys_interface *sif);
302 int (*remove_dev)(struct device *dev, struct subsys_interface *sif);
305 int subsys_interface_register(struct subsys_interface *sif);
306 void subsys_interface_unregister(struct subsys_interface *sif);
308 int subsys_system_register(struct bus_type *subsys,
309 const struct attribute_group **groups);
310 int subsys_virtual_register(struct bus_type *subsys,
311 const struct attribute_group **groups);
314 * struct class - device classes
315 * @name: Name of the class.
316 * @owner: The module owner.
317 * @class_attrs: Default attributes of this class.
318 * @dev_attrs: Default attributes of the devices belong to the class.
319 * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
320 * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
321 * @dev_uevent: Called when a device is added, removed from this class, or a
322 * few other things that generate uevents to add the environment
324 * @devnode: Callback to provide the devtmpfs.
325 * @class_release: Called to release this class.
326 * @dev_release: Called to release the device.
327 * @suspend: Used to put the device to sleep mode, usually to a low power
329 * @resume: Used to bring the device from the sleep mode.
330 * @ns_type: Callbacks so sysfs can detemine namespaces.
331 * @namespace: Namespace of the device belongs to this class.
332 * @pm: The default device power management operations of this class.
333 * @p: The private data of the driver core, no one other than the
334 * driver core can touch this.
336 * A class is a higher-level view of a device that abstracts out low-level
337 * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
338 * at the class level, they are all simply disks. Classes allow user space
339 * to work with devices based on what they do, rather than how they are
340 * connected or how they work.
344 struct module *owner;
346 struct class_attribute *class_attrs;
347 struct device_attribute *dev_attrs;
348 struct bin_attribute *dev_bin_attrs;
349 struct kobject *dev_kobj;
351 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
352 char *(*devnode)(struct device *dev, umode_t *mode);
354 void (*class_release)(struct class *class);
355 void (*dev_release)(struct device *dev);
357 int (*suspend)(struct device *dev, pm_message_t state);
358 int (*resume)(struct device *dev);
360 const struct kobj_ns_type_operations *ns_type;
361 const void *(*namespace)(struct device *dev);
363 const struct dev_pm_ops *pm;
365 struct subsys_private *p;
368 struct class_dev_iter {
369 struct klist_iter ki;
370 const struct device_type *type;
373 extern struct kobject *sysfs_dev_block_kobj;
374 extern struct kobject *sysfs_dev_char_kobj;
375 extern int __must_check __class_register(struct class *class,
376 struct lock_class_key *key);
377 extern void class_unregister(struct class *class);
379 /* This is a #define to keep the compiler from merging different
380 * instances of the __key variable */
381 #define class_register(class) \
383 static struct lock_class_key __key; \
384 __class_register(class, &__key); \
388 struct class_compat *class_compat_register(const char *name);
389 void class_compat_unregister(struct class_compat *cls);
390 int class_compat_create_link(struct class_compat *cls, struct device *dev,
391 struct device *device_link);
392 void class_compat_remove_link(struct class_compat *cls, struct device *dev,
393 struct device *device_link);
395 extern void class_dev_iter_init(struct class_dev_iter *iter,
397 struct device *start,
398 const struct device_type *type);
399 extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
400 extern void class_dev_iter_exit(struct class_dev_iter *iter);
402 extern int class_for_each_device(struct class *class, struct device *start,
404 int (*fn)(struct device *dev, void *data));
405 extern struct device *class_find_device(struct class *class,
406 struct device *start, const void *data,
407 int (*match)(struct device *, const void *));
409 struct class_attribute {
410 struct attribute attr;
411 ssize_t (*show)(struct class *class, struct class_attribute *attr,
413 ssize_t (*store)(struct class *class, struct class_attribute *attr,
414 const char *buf, size_t count);
415 const void *(*namespace)(struct class *class,
416 const struct class_attribute *attr);
419 #define CLASS_ATTR(_name, _mode, _show, _store) \
420 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
421 #define CLASS_ATTR_RW(_name) \
422 struct class_attribute class_attr_##_name = __ATTR_RW(_name)
423 #define CLASS_ATTR_RO(_name) \
424 struct class_attribute class_attr_##_name = __ATTR_RO(_name)
426 extern int __must_check class_create_file(struct class *class,
427 const struct class_attribute *attr);
428 extern void class_remove_file(struct class *class,
429 const struct class_attribute *attr);
431 /* Simple class attribute that is just a static string */
432 struct class_attribute_string {
433 struct class_attribute attr;
437 /* Currently read-only only */
438 #define _CLASS_ATTR_STRING(_name, _mode, _str) \
439 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
440 #define CLASS_ATTR_STRING(_name, _mode, _str) \
441 struct class_attribute_string class_attr_##_name = \
442 _CLASS_ATTR_STRING(_name, _mode, _str)
444 extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
447 struct class_interface {
448 struct list_head node;
451 int (*add_dev) (struct device *, struct class_interface *);
452 void (*remove_dev) (struct device *, struct class_interface *);
455 extern int __must_check class_interface_register(struct class_interface *);
456 extern void class_interface_unregister(struct class_interface *);
458 extern struct class * __must_check __class_create(struct module *owner,
460 struct lock_class_key *key);
461 extern void class_destroy(struct class *cls);
463 /* This is a #define to keep the compiler from merging different
464 * instances of the __key variable */
465 #define class_create(owner, name) \
467 static struct lock_class_key __key; \
468 __class_create(owner, name, &__key); \
472 * The type of device, "struct device" is embedded in. A class
473 * or bus can contain devices of different types
474 * like "partitions" and "disks", "mouse" and "event".
475 * This identifies the device type and carries type-specific
476 * information, equivalent to the kobj_type of a kobject.
477 * If "name" is specified, the uevent will contain it in
478 * the DEVTYPE variable.
482 const struct attribute_group **groups;
483 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
484 char *(*devnode)(struct device *dev, umode_t *mode,
485 kuid_t *uid, kgid_t *gid);
486 void (*release)(struct device *dev);
488 const struct dev_pm_ops *pm;
491 /* interface for exporting device attributes */
492 struct device_attribute {
493 struct attribute attr;
494 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
496 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
497 const char *buf, size_t count);
500 struct dev_ext_attribute {
501 struct device_attribute attr;
505 ssize_t device_show_ulong(struct device *dev, struct device_attribute *attr,
507 ssize_t device_store_ulong(struct device *dev, struct device_attribute *attr,
508 const char *buf, size_t count);
509 ssize_t device_show_int(struct device *dev, struct device_attribute *attr,
511 ssize_t device_store_int(struct device *dev, struct device_attribute *attr,
512 const char *buf, size_t count);
513 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
515 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
516 const char *buf, size_t count);
518 #define DEVICE_ATTR(_name, _mode, _show, _store) \
519 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
520 #define DEVICE_ATTR_RW(_name) \
521 struct device_attribute dev_attr_##_name = __ATTR_RW(_name)
522 #define DEVICE_ATTR_RO(_name) \
523 struct device_attribute dev_attr_##_name = __ATTR_RO(_name)
524 #define DEVICE_ATTR_WO(_name) \
525 struct device_attribute dev_attr_##_name = __ATTR_WO(_name)
526 #define DEVICE_ULONG_ATTR(_name, _mode, _var) \
527 struct dev_ext_attribute dev_attr_##_name = \
528 { __ATTR(_name, _mode, device_show_ulong, device_store_ulong), &(_var) }
529 #define DEVICE_INT_ATTR(_name, _mode, _var) \
530 struct dev_ext_attribute dev_attr_##_name = \
531 { __ATTR(_name, _mode, device_show_int, device_store_int), &(_var) }
532 #define DEVICE_BOOL_ATTR(_name, _mode, _var) \
533 struct dev_ext_attribute dev_attr_##_name = \
534 { __ATTR(_name, _mode, device_show_bool, device_store_bool), &(_var) }
535 #define DEVICE_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
536 struct device_attribute dev_attr_##_name = \
537 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
539 extern int device_create_file(struct device *device,
540 const struct device_attribute *entry);
541 extern void device_remove_file(struct device *dev,
542 const struct device_attribute *attr);
543 extern int __must_check device_create_bin_file(struct device *dev,
544 const struct bin_attribute *attr);
545 extern void device_remove_bin_file(struct device *dev,
546 const struct bin_attribute *attr);
547 extern int device_schedule_callback_owner(struct device *dev,
548 void (*func)(struct device *dev), struct module *owner);
550 /* This is a macro to avoid include problems with THIS_MODULE */
551 #define device_schedule_callback(dev, func) \
552 device_schedule_callback_owner(dev, func, THIS_MODULE)
554 /* device resource management */
555 typedef void (*dr_release_t)(struct device *dev, void *res);
556 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
558 #ifdef CONFIG_DEBUG_DEVRES
559 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
561 #define devres_alloc(release, size, gfp) \
562 __devres_alloc(release, size, gfp, #release)
564 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
566 extern void devres_for_each_res(struct device *dev, dr_release_t release,
567 dr_match_t match, void *match_data,
568 void (*fn)(struct device *, void *, void *),
570 extern void devres_free(void *res);
571 extern void devres_add(struct device *dev, void *res);
572 extern void *devres_find(struct device *dev, dr_release_t release,
573 dr_match_t match, void *match_data);
574 extern void *devres_get(struct device *dev, void *new_res,
575 dr_match_t match, void *match_data);
576 extern void *devres_remove(struct device *dev, dr_release_t release,
577 dr_match_t match, void *match_data);
578 extern int devres_destroy(struct device *dev, dr_release_t release,
579 dr_match_t match, void *match_data);
580 extern int devres_release(struct device *dev, dr_release_t release,
581 dr_match_t match, void *match_data);
584 extern void * __must_check devres_open_group(struct device *dev, void *id,
586 extern void devres_close_group(struct device *dev, void *id);
587 extern void devres_remove_group(struct device *dev, void *id);
588 extern int devres_release_group(struct device *dev, void *id);
590 /* managed devm_k.alloc/kfree for device drivers */
591 extern void *devm_kmalloc(struct device *dev, size_t size, gfp_t gfp);
592 static inline void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp)
594 return devm_kmalloc(dev, size, gfp | __GFP_ZERO);
596 static inline void *devm_kmalloc_array(struct device *dev,
597 size_t n, size_t size, gfp_t flags)
599 if (size != 0 && n > SIZE_MAX / size)
601 return devm_kmalloc(dev, n * size, flags);
603 static inline void *devm_kcalloc(struct device *dev,
604 size_t n, size_t size, gfp_t flags)
606 return devm_kmalloc_array(dev, n, size, flags | __GFP_ZERO);
608 extern void devm_kfree(struct device *dev, void *p);
610 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
611 void __iomem *devm_request_and_ioremap(struct device *dev,
612 struct resource *res);
614 void __iomem *devm_ioremap_exec_resource(struct device *dev,
615 struct resource *res);
616 void __iomem *devm_request_and_ioremap_exec(struct device *dev,
617 struct resource *res);
619 /* allows to add/remove a custom action to devres stack */
620 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
621 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
623 struct device_dma_parameters {
625 * a low level driver may set these to teach IOMMU code about
628 unsigned int max_segment_size;
629 unsigned long segment_boundary_mask;
632 struct acpi_dev_node {
639 * struct device - The basic device structure
640 * @parent: The device's "parent" device, the device to which it is attached.
641 * In most cases, a parent device is some sort of bus or host
642 * controller. If parent is NULL, the device, is a top-level device,
643 * which is not usually what you want.
644 * @p: Holds the private data of the driver core portions of the device.
645 * See the comment of the struct device_private for detail.
646 * @kobj: A top-level, abstract class from which other classes are derived.
647 * @init_name: Initial name of the device.
648 * @type: The type of device.
649 * This identifies the device type and carries type-specific
651 * @mutex: Mutex to synchronize calls to its driver.
652 * @bus: Type of bus device is on.
653 * @driver: Which driver has allocated this
654 * @platform_data: Platform data specific to the device.
655 * Example: For devices on custom boards, as typical of embedded
656 * and SOC based hardware, Linux often uses platform_data to point
657 * to board-specific structures describing devices and how they
658 * are wired. That can include what ports are available, chip
659 * variants, which GPIO pins act in what additional roles, and so
660 * on. This shrinks the "Board Support Packages" (BSPs) and
661 * minimizes board-specific #ifdefs in drivers.
662 * @power: For device power management.
663 * See Documentation/power/devices.txt for details.
664 * @pm_domain: Provide callbacks that are executed during system suspend,
665 * hibernation, system resume and during runtime PM transitions
666 * along with subsystem-level and driver-level callbacks.
667 * @pins: For device pin management.
668 * See Documentation/pinctrl.txt for details.
669 * @numa_node: NUMA node this device is close to.
670 * @dma_mask: Dma mask (if dma'ble device).
671 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
672 * hardware supports 64-bit addresses for consistent allocations
674 * @dma_parms: A low level driver may set these to teach IOMMU code about
675 * segment limitations.
676 * @dma_pools: Dma pools (if dma'ble device).
677 * @dma_mem: Internal for coherent mem override.
678 * @archdata: For arch-specific additions.
679 * @of_node: Associated device tree node.
680 * @acpi_node: Associated ACPI device node.
681 * @devt: For creating the sysfs "dev".
682 * @id: device instance
683 * @devres_lock: Spinlock to protect the resource of the device.
684 * @devres_head: The resources list of the device.
685 * @knode_class: The node used to add the device to the class list.
686 * @class: The class of the device.
687 * @groups: Optional attribute groups.
688 * @release: Callback to free the device after all references have
689 * gone away. This should be set by the allocator of the
690 * device (i.e. the bus driver that discovered the device).
692 * At the lowest level, every device in a Linux system is represented by an
693 * instance of struct device. The device structure contains the information
694 * that the device model core needs to model the system. Most subsystems,
695 * however, track additional information about the devices they host. As a
696 * result, it is rare for devices to be represented by bare device structures;
697 * instead, that structure, like kobject structures, is usually embedded within
698 * a higher-level representation of the device.
701 struct device *parent;
703 struct device_private *p;
706 const char *init_name; /* initial name of the device */
707 const struct device_type *type;
709 struct mutex mutex; /* mutex to synchronize calls to
713 struct bus_type *bus; /* type of bus device is on */
714 struct device_driver *driver; /* which driver has allocated this
716 void *platform_data; /* Platform specific data, device
717 core doesn't touch it */
718 struct dev_pm_info power;
719 struct dev_pm_domain *pm_domain;
721 #ifdef CONFIG_PINCTRL
722 struct dev_pin_info *pins;
726 int numa_node; /* NUMA node this device is close to */
728 u64 *dma_mask; /* dma mask (if dma'able device) */
729 u64 coherent_dma_mask;/* Like dma_mask, but for
730 alloc_coherent mappings as
731 not all hardware supports
732 64 bit addresses for consistent
733 allocations such descriptors. */
735 struct device_dma_parameters *dma_parms;
737 struct list_head dma_pools; /* dma pools (if dma'ble) */
739 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
741 #ifdef CONFIG_DMA_CMA
742 struct cma *cma_area; /* contiguous memory area for dma
745 /* arch specific additions */
746 struct dev_archdata archdata;
748 struct device_node *of_node; /* associated device tree node */
749 struct acpi_dev_node acpi_node; /* associated ACPI device node */
751 dev_t devt; /* dev_t, creates the sysfs "dev" */
752 u32 id; /* device instance */
754 spinlock_t devres_lock;
755 struct list_head devres_head;
757 struct klist_node knode_class;
759 const struct attribute_group **groups; /* optional groups */
761 void (*release)(struct device *dev);
762 struct iommu_group *iommu_group;
765 static inline struct device *kobj_to_dev(struct kobject *kobj)
767 return container_of(kobj, struct device, kobj);
771 #define ACPI_HANDLE(dev) ((dev)->acpi_node.handle)
772 #define ACPI_HANDLE_SET(dev, _handle_) (dev)->acpi_node.handle = (_handle_)
774 #define ACPI_HANDLE(dev) (NULL)
775 #define ACPI_HANDLE_SET(dev, _handle_) do { } while (0)
778 /* Get the wakeup routines, which depend on struct device */
779 #include <linux/pm_wakeup.h>
781 static inline const char *dev_name(const struct device *dev)
783 /* Use the init name until the kobject becomes available */
785 return dev->init_name;
787 return kobject_name(&dev->kobj);
790 extern __printf(2, 3)
791 int dev_set_name(struct device *dev, const char *name, ...);
794 static inline int dev_to_node(struct device *dev)
796 return dev->numa_node;
798 static inline void set_dev_node(struct device *dev, int node)
800 dev->numa_node = node;
803 static inline int dev_to_node(struct device *dev)
807 static inline void set_dev_node(struct device *dev, int node)
812 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
814 return dev ? dev->power.subsys_data : NULL;
817 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
819 return dev->kobj.uevent_suppress;
822 static inline void dev_set_uevent_suppress(struct device *dev, int val)
824 dev->kobj.uevent_suppress = val;
827 static inline int device_is_registered(struct device *dev)
829 return dev->kobj.state_in_sysfs;
832 static inline void device_enable_async_suspend(struct device *dev)
834 if (!dev->power.is_prepared)
835 dev->power.async_suspend = true;
838 static inline void device_disable_async_suspend(struct device *dev)
840 if (!dev->power.is_prepared)
841 dev->power.async_suspend = false;
844 static inline bool device_async_suspend_enabled(struct device *dev)
846 return !!dev->power.async_suspend;
849 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
851 dev->power.ignore_children = enable;
854 static inline void dev_pm_syscore_device(struct device *dev, bool val)
856 #ifdef CONFIG_PM_SLEEP
857 dev->power.syscore = val;
861 static inline void device_lock(struct device *dev)
863 mutex_lock(&dev->mutex);
866 static inline int device_trylock(struct device *dev)
868 return mutex_trylock(&dev->mutex);
871 static inline void device_unlock(struct device *dev)
873 mutex_unlock(&dev->mutex);
876 void driver_init(void);
879 * High level routines for use by the bus drivers
881 extern int __must_check device_register(struct device *dev);
882 extern void device_unregister(struct device *dev);
883 extern void device_initialize(struct device *dev);
884 extern int __must_check device_add(struct device *dev);
885 extern void device_del(struct device *dev);
886 extern int device_for_each_child(struct device *dev, void *data,
887 int (*fn)(struct device *dev, void *data));
888 extern struct device *device_find_child(struct device *dev, void *data,
889 int (*match)(struct device *dev, void *data));
890 extern int device_rename(struct device *dev, const char *new_name);
891 extern int device_move(struct device *dev, struct device *new_parent,
892 enum dpm_order dpm_order);
893 extern const char *device_get_devnode(struct device *dev,
894 umode_t *mode, kuid_t *uid, kgid_t *gid,
896 extern void *dev_get_drvdata(const struct device *dev);
897 extern int dev_set_drvdata(struct device *dev, void *data);
900 * Root device objects for grouping under /sys/devices
902 extern struct device *__root_device_register(const char *name,
903 struct module *owner);
906 * This is a macro to avoid include problems with THIS_MODULE,
907 * just as per what is done for device_schedule_callback() above.
909 #define root_device_register(name) \
910 __root_device_register(name, THIS_MODULE)
912 extern void root_device_unregister(struct device *root);
914 static inline void *dev_get_platdata(const struct device *dev)
916 return dev->platform_data;
920 * Manual binding of a device to driver. See drivers/base/bus.c
921 * for information on use.
923 extern int __must_check device_bind_driver(struct device *dev);
924 extern void device_release_driver(struct device *dev);
925 extern int __must_check device_attach(struct device *dev);
926 extern int __must_check driver_attach(struct device_driver *drv);
927 extern int __must_check device_reprobe(struct device *dev);
930 * Easy functions for dynamically creating devices on the fly
932 extern struct device *device_create_vargs(struct class *cls,
933 struct device *parent,
938 extern __printf(5, 6)
939 struct device *device_create(struct class *cls, struct device *parent,
940 dev_t devt, void *drvdata,
941 const char *fmt, ...);
942 extern void device_destroy(struct class *cls, dev_t devt);
945 * Platform "fixup" functions - allow the platform to have their say
946 * about devices and actions that the general device layer doesn't
949 /* Notify platform of device discovery */
950 extern int (*platform_notify)(struct device *dev);
952 extern int (*platform_notify_remove)(struct device *dev);
956 * get_device - atomically increment the reference count for the device.
959 extern struct device *get_device(struct device *dev);
960 extern void put_device(struct device *dev);
962 #ifdef CONFIG_DEVTMPFS
963 extern int devtmpfs_create_node(struct device *dev);
964 extern int devtmpfs_delete_node(struct device *dev);
965 extern int devtmpfs_mount(const char *mntdir);
967 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
968 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
969 static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
972 /* drivers/base/power/shutdown.c */
973 extern void device_shutdown(void);
975 /* debugging and troubleshooting/diagnostic helpers. */
976 extern const char *dev_driver_string(const struct device *dev);
981 extern __printf(3, 0)
982 int dev_vprintk_emit(int level, const struct device *dev,
983 const char *fmt, va_list args);
984 extern __printf(3, 4)
985 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
987 extern __printf(3, 4)
988 int dev_printk(const char *level, const struct device *dev,
989 const char *fmt, ...);
990 extern __printf(2, 3)
991 int dev_emerg(const struct device *dev, const char *fmt, ...);
992 extern __printf(2, 3)
993 int dev_alert(const struct device *dev, const char *fmt, ...);
994 extern __printf(2, 3)
995 int dev_crit(const struct device *dev, const char *fmt, ...);
996 extern __printf(2, 3)
997 int dev_err(const struct device *dev, const char *fmt, ...);
998 extern __printf(2, 3)
999 int dev_warn(const struct device *dev, const char *fmt, ...);
1000 extern __printf(2, 3)
1001 int dev_notice(const struct device *dev, const char *fmt, ...);
1002 extern __printf(2, 3)
1003 int _dev_info(const struct device *dev, const char *fmt, ...);
1007 static inline __printf(3, 0)
1008 int dev_vprintk_emit(int level, const struct device *dev,
1009 const char *fmt, va_list args)
1011 static inline __printf(3, 4)
1012 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1015 static inline int __dev_printk(const char *level, const struct device *dev,
1016 struct va_format *vaf)
1018 static inline __printf(3, 4)
1019 int dev_printk(const char *level, const struct device *dev,
1020 const char *fmt, ...)
1023 static inline __printf(2, 3)
1024 int dev_emerg(const struct device *dev, const char *fmt, ...)
1026 static inline __printf(2, 3)
1027 int dev_crit(const struct device *dev, const char *fmt, ...)
1029 static inline __printf(2, 3)
1030 int dev_alert(const struct device *dev, const char *fmt, ...)
1032 static inline __printf(2, 3)
1033 int dev_err(const struct device *dev, const char *fmt, ...)
1035 static inline __printf(2, 3)
1036 int dev_warn(const struct device *dev, const char *fmt, ...)
1038 static inline __printf(2, 3)
1039 int dev_notice(const struct device *dev, const char *fmt, ...)
1041 static inline __printf(2, 3)
1042 int _dev_info(const struct device *dev, const char *fmt, ...)
1048 * Stupid hackaround for existing uses of non-printk uses dev_info
1050 * Note that the definition of dev_info below is actually _dev_info
1051 * and a macro is used to avoid redefining dev_info
1054 #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
1056 #if defined(CONFIG_DYNAMIC_DEBUG)
1057 #define dev_dbg(dev, format, ...) \
1059 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
1061 #elif defined(DEBUG)
1062 #define dev_dbg(dev, format, arg...) \
1063 dev_printk(KERN_DEBUG, dev, format, ##arg)
1065 #define dev_dbg(dev, format, arg...) \
1068 dev_printk(KERN_DEBUG, dev, format, ##arg); \
1073 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
1075 static DEFINE_RATELIMIT_STATE(_rs, \
1076 DEFAULT_RATELIMIT_INTERVAL, \
1077 DEFAULT_RATELIMIT_BURST); \
1078 if (__ratelimit(&_rs)) \
1079 dev_level(dev, fmt, ##__VA_ARGS__); \
1082 #define dev_emerg_ratelimited(dev, fmt, ...) \
1083 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1084 #define dev_alert_ratelimited(dev, fmt, ...) \
1085 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1086 #define dev_crit_ratelimited(dev, fmt, ...) \
1087 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1088 #define dev_err_ratelimited(dev, fmt, ...) \
1089 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1090 #define dev_warn_ratelimited(dev, fmt, ...) \
1091 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1092 #define dev_notice_ratelimited(dev, fmt, ...) \
1093 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1094 #define dev_info_ratelimited(dev, fmt, ...) \
1095 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1096 #if defined(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
1097 #define dev_dbg_ratelimited(dev, fmt, ...) \
1099 static DEFINE_RATELIMIT_STATE(_rs, \
1100 DEFAULT_RATELIMIT_INTERVAL, \
1101 DEFAULT_RATELIMIT_BURST); \
1102 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
1103 if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
1104 __ratelimit(&_rs)) \
1105 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
1109 #define dev_dbg_ratelimited(dev, fmt, ...) \
1110 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
1113 #ifdef VERBOSE_DEBUG
1114 #define dev_vdbg dev_dbg
1116 #define dev_vdbg(dev, format, arg...) \
1119 dev_printk(KERN_DEBUG, dev, format, ##arg); \
1125 * dev_WARN*() acts like dev_printk(), but with the key difference
1126 * of using a WARN/WARN_ON to get the message out, including the
1127 * file/line information and a backtrace.
1129 #define dev_WARN(dev, format, arg...) \
1130 WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
1132 #define dev_WARN_ONCE(dev, condition, format, arg...) \
1133 WARN_ONCE(condition, "Device %s\n" format, \
1134 dev_driver_string(dev), ## arg)
1136 /* Create alias, so I can be autoloaded. */
1137 #define MODULE_ALIAS_CHARDEV(major,minor) \
1138 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1139 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1140 MODULE_ALIAS("char-major-" __stringify(major) "-*")
1142 #ifdef CONFIG_SYSFS_DEPRECATED
1143 extern long sysfs_deprecated;
1145 #define sysfs_deprecated 0
1149 * module_driver() - Helper macro for drivers that don't do anything
1150 * special in module init/exit. This eliminates a lot of boilerplate.
1151 * Each module may only use this macro once, and calling it replaces
1152 * module_init() and module_exit().
1154 * @__driver: driver name
1155 * @__register: register function for this driver type
1156 * @__unregister: unregister function for this driver type
1157 * @...: Additional arguments to be passed to __register and __unregister.
1159 * Use this macro to construct bus specific macros for registering
1160 * drivers, and do not use it on its own.
1162 #define module_driver(__driver, __register, __unregister, ...) \
1163 static int __init __driver##_init(void) \
1165 return __register(&(__driver) , ##__VA_ARGS__); \
1167 module_init(__driver##_init); \
1168 static void __exit __driver##_exit(void) \
1170 __unregister(&(__driver) , ##__VA_ARGS__); \
1172 module_exit(__driver##_exit);
1174 #endif /* _DEVICE_H_ */