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 /* allows to add/remove a custom action to devres stack */
615 int devm_add_action(struct device *dev, void (*action)(void *), void *data);
616 void devm_remove_action(struct device *dev, void (*action)(void *), void *data);
618 struct device_dma_parameters {
620 * a low level driver may set these to teach IOMMU code about
623 unsigned int max_segment_size;
624 unsigned long segment_boundary_mask;
627 struct acpi_dev_node {
634 * struct device - The basic device structure
635 * @parent: The device's "parent" device, the device to which it is attached.
636 * In most cases, a parent device is some sort of bus or host
637 * controller. If parent is NULL, the device, is a top-level device,
638 * which is not usually what you want.
639 * @p: Holds the private data of the driver core portions of the device.
640 * See the comment of the struct device_private for detail.
641 * @kobj: A top-level, abstract class from which other classes are derived.
642 * @init_name: Initial name of the device.
643 * @type: The type of device.
644 * This identifies the device type and carries type-specific
646 * @mutex: Mutex to synchronize calls to its driver.
647 * @bus: Type of bus device is on.
648 * @driver: Which driver has allocated this
649 * @platform_data: Platform data specific to the device.
650 * Example: For devices on custom boards, as typical of embedded
651 * and SOC based hardware, Linux often uses platform_data to point
652 * to board-specific structures describing devices and how they
653 * are wired. That can include what ports are available, chip
654 * variants, which GPIO pins act in what additional roles, and so
655 * on. This shrinks the "Board Support Packages" (BSPs) and
656 * minimizes board-specific #ifdefs in drivers.
657 * @power: For device power management.
658 * See Documentation/power/devices.txt for details.
659 * @pm_domain: Provide callbacks that are executed during system suspend,
660 * hibernation, system resume and during runtime PM transitions
661 * along with subsystem-level and driver-level callbacks.
662 * @pins: For device pin management.
663 * See Documentation/pinctrl.txt for details.
664 * @numa_node: NUMA node this device is close to.
665 * @dma_mask: Dma mask (if dma'ble device).
666 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
667 * hardware supports 64-bit addresses for consistent allocations
669 * @dma_parms: A low level driver may set these to teach IOMMU code about
670 * segment limitations.
671 * @dma_pools: Dma pools (if dma'ble device).
672 * @dma_mem: Internal for coherent mem override.
673 * @archdata: For arch-specific additions.
674 * @of_node: Associated device tree node.
675 * @acpi_node: Associated ACPI device node.
676 * @devt: For creating the sysfs "dev".
677 * @id: device instance
678 * @devres_lock: Spinlock to protect the resource of the device.
679 * @devres_head: The resources list of the device.
680 * @knode_class: The node used to add the device to the class list.
681 * @class: The class of the device.
682 * @groups: Optional attribute groups.
683 * @release: Callback to free the device after all references have
684 * gone away. This should be set by the allocator of the
685 * device (i.e. the bus driver that discovered the device).
687 * At the lowest level, every device in a Linux system is represented by an
688 * instance of struct device. The device structure contains the information
689 * that the device model core needs to model the system. Most subsystems,
690 * however, track additional information about the devices they host. As a
691 * result, it is rare for devices to be represented by bare device structures;
692 * instead, that structure, like kobject structures, is usually embedded within
693 * a higher-level representation of the device.
696 struct device *parent;
698 struct device_private *p;
701 const char *init_name; /* initial name of the device */
702 const struct device_type *type;
704 struct mutex mutex; /* mutex to synchronize calls to
708 struct bus_type *bus; /* type of bus device is on */
709 struct device_driver *driver; /* which driver has allocated this
711 void *platform_data; /* Platform specific data, device
712 core doesn't touch it */
713 struct dev_pm_info power;
714 struct dev_pm_domain *pm_domain;
716 #ifdef CONFIG_PINCTRL
717 struct dev_pin_info *pins;
721 int numa_node; /* NUMA node this device is close to */
723 u64 *dma_mask; /* dma mask (if dma'able device) */
724 u64 coherent_dma_mask;/* Like dma_mask, but for
725 alloc_coherent mappings as
726 not all hardware supports
727 64 bit addresses for consistent
728 allocations such descriptors. */
730 struct device_dma_parameters *dma_parms;
732 struct list_head dma_pools; /* dma pools (if dma'ble) */
734 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
736 #ifdef CONFIG_DMA_CMA
737 struct cma *cma_area; /* contiguous memory area for dma
740 /* arch specific additions */
741 struct dev_archdata archdata;
743 struct device_node *of_node; /* associated device tree node */
744 struct acpi_dev_node acpi_node; /* associated ACPI device node */
746 dev_t devt; /* dev_t, creates the sysfs "dev" */
747 u32 id; /* device instance */
749 spinlock_t devres_lock;
750 struct list_head devres_head;
752 struct klist_node knode_class;
754 const struct attribute_group **groups; /* optional groups */
756 void (*release)(struct device *dev);
757 struct iommu_group *iommu_group;
760 static inline struct device *kobj_to_dev(struct kobject *kobj)
762 return container_of(kobj, struct device, kobj);
766 #define ACPI_HANDLE(dev) ((dev)->acpi_node.handle)
767 #define ACPI_HANDLE_SET(dev, _handle_) (dev)->acpi_node.handle = (_handle_)
769 #define ACPI_HANDLE(dev) (NULL)
770 #define ACPI_HANDLE_SET(dev, _handle_) do { } while (0)
773 /* Get the wakeup routines, which depend on struct device */
774 #include <linux/pm_wakeup.h>
776 static inline const char *dev_name(const struct device *dev)
778 /* Use the init name until the kobject becomes available */
780 return dev->init_name;
782 return kobject_name(&dev->kobj);
785 extern __printf(2, 3)
786 int dev_set_name(struct device *dev, const char *name, ...);
789 static inline int dev_to_node(struct device *dev)
791 return dev->numa_node;
793 static inline void set_dev_node(struct device *dev, int node)
795 dev->numa_node = node;
798 static inline int dev_to_node(struct device *dev)
802 static inline void set_dev_node(struct device *dev, int node)
807 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
809 return dev ? dev->power.subsys_data : NULL;
812 static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
814 return dev->kobj.uevent_suppress;
817 static inline void dev_set_uevent_suppress(struct device *dev, int val)
819 dev->kobj.uevent_suppress = val;
822 static inline int device_is_registered(struct device *dev)
824 return dev->kobj.state_in_sysfs;
827 static inline void device_enable_async_suspend(struct device *dev)
829 if (!dev->power.is_prepared)
830 dev->power.async_suspend = true;
833 static inline void device_disable_async_suspend(struct device *dev)
835 if (!dev->power.is_prepared)
836 dev->power.async_suspend = false;
839 static inline bool device_async_suspend_enabled(struct device *dev)
841 return !!dev->power.async_suspend;
844 static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
846 dev->power.ignore_children = enable;
849 static inline void dev_pm_syscore_device(struct device *dev, bool val)
851 #ifdef CONFIG_PM_SLEEP
852 dev->power.syscore = val;
856 static inline void device_lock(struct device *dev)
858 mutex_lock(&dev->mutex);
861 static inline int device_trylock(struct device *dev)
863 return mutex_trylock(&dev->mutex);
866 static inline void device_unlock(struct device *dev)
868 mutex_unlock(&dev->mutex);
871 void driver_init(void);
874 * High level routines for use by the bus drivers
876 extern int __must_check device_register(struct device *dev);
877 extern void device_unregister(struct device *dev);
878 extern void device_initialize(struct device *dev);
879 extern int __must_check device_add(struct device *dev);
880 extern void device_del(struct device *dev);
881 extern int device_for_each_child(struct device *dev, void *data,
882 int (*fn)(struct device *dev, void *data));
883 extern struct device *device_find_child(struct device *dev, void *data,
884 int (*match)(struct device *dev, void *data));
885 extern int device_rename(struct device *dev, const char *new_name);
886 extern int device_move(struct device *dev, struct device *new_parent,
887 enum dpm_order dpm_order);
888 extern const char *device_get_devnode(struct device *dev,
889 umode_t *mode, kuid_t *uid, kgid_t *gid,
891 extern void *dev_get_drvdata(const struct device *dev);
892 extern int dev_set_drvdata(struct device *dev, void *data);
895 * Root device objects for grouping under /sys/devices
897 extern struct device *__root_device_register(const char *name,
898 struct module *owner);
901 * This is a macro to avoid include problems with THIS_MODULE,
902 * just as per what is done for device_schedule_callback() above.
904 #define root_device_register(name) \
905 __root_device_register(name, THIS_MODULE)
907 extern void root_device_unregister(struct device *root);
909 static inline void *dev_get_platdata(const struct device *dev)
911 return dev->platform_data;
915 * Manual binding of a device to driver. See drivers/base/bus.c
916 * for information on use.
918 extern int __must_check device_bind_driver(struct device *dev);
919 extern void device_release_driver(struct device *dev);
920 extern int __must_check device_attach(struct device *dev);
921 extern int __must_check driver_attach(struct device_driver *drv);
922 extern int __must_check device_reprobe(struct device *dev);
925 * Easy functions for dynamically creating devices on the fly
927 extern struct device *device_create_vargs(struct class *cls,
928 struct device *parent,
933 extern __printf(5, 6)
934 struct device *device_create(struct class *cls, struct device *parent,
935 dev_t devt, void *drvdata,
936 const char *fmt, ...);
937 extern void device_destroy(struct class *cls, dev_t devt);
940 * Platform "fixup" functions - allow the platform to have their say
941 * about devices and actions that the general device layer doesn't
944 /* Notify platform of device discovery */
945 extern int (*platform_notify)(struct device *dev);
947 extern int (*platform_notify_remove)(struct device *dev);
951 * get_device - atomically increment the reference count for the device.
954 extern struct device *get_device(struct device *dev);
955 extern void put_device(struct device *dev);
957 #ifdef CONFIG_DEVTMPFS
958 extern int devtmpfs_create_node(struct device *dev);
959 extern int devtmpfs_delete_node(struct device *dev);
960 extern int devtmpfs_mount(const char *mntdir);
962 static inline int devtmpfs_create_node(struct device *dev) { return 0; }
963 static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
964 static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
967 /* drivers/base/power/shutdown.c */
968 extern void device_shutdown(void);
970 /* debugging and troubleshooting/diagnostic helpers. */
971 extern const char *dev_driver_string(const struct device *dev);
976 extern __printf(3, 0)
977 int dev_vprintk_emit(int level, const struct device *dev,
978 const char *fmt, va_list args);
979 extern __printf(3, 4)
980 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
982 extern __printf(3, 4)
983 int dev_printk(const char *level, const struct device *dev,
984 const char *fmt, ...);
985 extern __printf(2, 3)
986 int dev_emerg(const struct device *dev, const char *fmt, ...);
987 extern __printf(2, 3)
988 int dev_alert(const struct device *dev, const char *fmt, ...);
989 extern __printf(2, 3)
990 int dev_crit(const struct device *dev, const char *fmt, ...);
991 extern __printf(2, 3)
992 int dev_err(const struct device *dev, const char *fmt, ...);
993 extern __printf(2, 3)
994 int dev_warn(const struct device *dev, const char *fmt, ...);
995 extern __printf(2, 3)
996 int dev_notice(const struct device *dev, const char *fmt, ...);
997 extern __printf(2, 3)
998 int _dev_info(const struct device *dev, const char *fmt, ...);
1002 static inline __printf(3, 0)
1003 int dev_vprintk_emit(int level, const struct device *dev,
1004 const char *fmt, va_list args)
1006 static inline __printf(3, 4)
1007 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
1010 static inline int __dev_printk(const char *level, const struct device *dev,
1011 struct va_format *vaf)
1013 static inline __printf(3, 4)
1014 int dev_printk(const char *level, const struct device *dev,
1015 const char *fmt, ...)
1018 static inline __printf(2, 3)
1019 int dev_emerg(const struct device *dev, const char *fmt, ...)
1021 static inline __printf(2, 3)
1022 int dev_crit(const struct device *dev, const char *fmt, ...)
1024 static inline __printf(2, 3)
1025 int dev_alert(const struct device *dev, const char *fmt, ...)
1027 static inline __printf(2, 3)
1028 int dev_err(const struct device *dev, const char *fmt, ...)
1030 static inline __printf(2, 3)
1031 int dev_warn(const struct device *dev, const char *fmt, ...)
1033 static inline __printf(2, 3)
1034 int dev_notice(const struct device *dev, const char *fmt, ...)
1036 static inline __printf(2, 3)
1037 int _dev_info(const struct device *dev, const char *fmt, ...)
1043 * Stupid hackaround for existing uses of non-printk uses dev_info
1045 * Note that the definition of dev_info below is actually _dev_info
1046 * and a macro is used to avoid redefining dev_info
1049 #define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
1051 #if defined(CONFIG_DYNAMIC_DEBUG)
1052 #define dev_dbg(dev, format, ...) \
1054 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
1056 #elif defined(DEBUG)
1057 #define dev_dbg(dev, format, arg...) \
1058 dev_printk(KERN_DEBUG, dev, format, ##arg)
1060 #define dev_dbg(dev, format, arg...) \
1063 dev_printk(KERN_DEBUG, dev, format, ##arg); \
1068 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
1070 static DEFINE_RATELIMIT_STATE(_rs, \
1071 DEFAULT_RATELIMIT_INTERVAL, \
1072 DEFAULT_RATELIMIT_BURST); \
1073 if (__ratelimit(&_rs)) \
1074 dev_level(dev, fmt, ##__VA_ARGS__); \
1077 #define dev_emerg_ratelimited(dev, fmt, ...) \
1078 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
1079 #define dev_alert_ratelimited(dev, fmt, ...) \
1080 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
1081 #define dev_crit_ratelimited(dev, fmt, ...) \
1082 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
1083 #define dev_err_ratelimited(dev, fmt, ...) \
1084 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
1085 #define dev_warn_ratelimited(dev, fmt, ...) \
1086 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
1087 #define dev_notice_ratelimited(dev, fmt, ...) \
1088 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
1089 #define dev_info_ratelimited(dev, fmt, ...) \
1090 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
1091 #if defined(CONFIG_DYNAMIC_DEBUG) || defined(DEBUG)
1092 #define dev_dbg_ratelimited(dev, fmt, ...) \
1094 static DEFINE_RATELIMIT_STATE(_rs, \
1095 DEFAULT_RATELIMIT_INTERVAL, \
1096 DEFAULT_RATELIMIT_BURST); \
1097 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
1098 if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
1099 __ratelimit(&_rs)) \
1100 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
1104 #define dev_dbg_ratelimited(dev, fmt, ...) \
1105 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
1108 #ifdef VERBOSE_DEBUG
1109 #define dev_vdbg dev_dbg
1111 #define dev_vdbg(dev, format, arg...) \
1114 dev_printk(KERN_DEBUG, dev, format, ##arg); \
1120 * dev_WARN*() acts like dev_printk(), but with the key difference
1121 * of using a WARN/WARN_ON to get the message out, including the
1122 * file/line information and a backtrace.
1124 #define dev_WARN(dev, format, arg...) \
1125 WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
1127 #define dev_WARN_ONCE(dev, condition, format, arg...) \
1128 WARN_ONCE(condition, "Device %s\n" format, \
1129 dev_driver_string(dev), ## arg)
1131 /* Create alias, so I can be autoloaded. */
1132 #define MODULE_ALIAS_CHARDEV(major,minor) \
1133 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
1134 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
1135 MODULE_ALIAS("char-major-" __stringify(major) "-*")
1137 #ifdef CONFIG_SYSFS_DEPRECATED
1138 extern long sysfs_deprecated;
1140 #define sysfs_deprecated 0
1144 * module_driver() - Helper macro for drivers that don't do anything
1145 * special in module init/exit. This eliminates a lot of boilerplate.
1146 * Each module may only use this macro once, and calling it replaces
1147 * module_init() and module_exit().
1149 * @__driver: driver name
1150 * @__register: register function for this driver type
1151 * @__unregister: unregister function for this driver type
1152 * @...: Additional arguments to be passed to __register and __unregister.
1154 * Use this macro to construct bus specific macros for registering
1155 * drivers, and do not use it on its own.
1157 #define module_driver(__driver, __register, __unregister, ...) \
1158 static int __init __driver##_init(void) \
1160 return __register(&(__driver) , ##__VA_ARGS__); \
1162 module_init(__driver##_init); \
1163 static void __exit __driver##_exit(void) \
1165 __unregister(&(__driver) , ##__VA_ARGS__); \
1167 module_exit(__driver##_exit);
1169 #endif /* _DEVICE_H_ */