]> Git Repo - linux.git/blame - include/linux/device.h
Merge branch 'x86-rdrand-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / include / linux / device.h
CommitLineData
1da177e4
LT
1/*
2 * device.h - generic, centralized driver model
3 *
4 * Copyright (c) 2001-2003 Patrick Mochel <[email protected]>
b4028437
GKH
5 * Copyright (c) 2004-2009 Greg Kroah-Hartman <[email protected]>
6 * Copyright (c) 2008-2009 Novell Inc.
1da177e4
LT
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
1da177e4
LT
16#include <linux/ioport.h>
17#include <linux/kobject.h>
465c7a3a 18#include <linux/klist.h>
1da177e4 19#include <linux/list.h>
d2a3b914 20#include <linux/lockdep.h>
4a7fb636 21#include <linux/compiler.h>
1da177e4
LT
22#include <linux/types.h>
23#include <linux/module.h>
24#include <linux/pm.h>
60063497 25#include <linux/atomic.h>
c6dbaef2 26#include <asm/device.h>
1da177e4 27
1da177e4 28struct device;
fb069a5d 29struct device_private;
1da177e4 30struct device_driver;
e5dd1278 31struct driver_private;
1da177e4 32struct class;
6b6e39a6 33struct subsys_private;
b8c5cec2 34struct bus_type;
d706c1b0 35struct device_node;
b8c5cec2
KS
36
37struct bus_attribute {
38 struct attribute attr;
d462943a
GKH
39 ssize_t (*show)(struct bus_type *bus, char *buf);
40 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
b8c5cec2
KS
41};
42
d462943a
GKH
43#define BUS_ATTR(_name, _mode, _show, _store) \
44struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
b8c5cec2
KS
45
46extern int __must_check bus_create_file(struct bus_type *,
47 struct bus_attribute *);
48extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
1da177e4 49
880ffb5c
WG
50/**
51 * struct bus_type - The bus type of the device
52 *
53 * @name: The name of the bus.
54 * @bus_attrs: Default attributes of the bus.
55 * @dev_attrs: Default attributes of the devices on the bus.
56 * @drv_attrs: Default attributes of the device drivers on the bus.
57 * @match: Called, perhaps multiple times, whenever a new device or driver
58 * is added for this bus. It should return a nonzero value if the
59 * given device can be handled by the given driver.
60 * @uevent: Called when a device is added, removed, or a few other things
61 * that generate uevents to add the environment variables.
62 * @probe: Called when a new device or driver add to this bus, and callback
63 * the specific driver's probe to initial the matched device.
64 * @remove: Called when a device removed from this bus.
65 * @shutdown: Called at shut-down time to quiesce the device.
66 * @suspend: Called when a device on this bus wants to go to sleep mode.
67 * @resume: Called to bring a device on this bus out of sleep mode.
68 * @pm: Power management operations of this bus, callback the specific
69 * device driver's pm-ops.
70 * @p: The private data of the driver core, only the driver core can
71 * touch this.
72 *
73 * A bus is a channel between the processor and one or more devices. For the
74 * purposes of the device model, all devices are connected via a bus, even if
75 * it is an internal, virtual, "platform" bus. Buses can plug into each other.
76 * A USB controller is usually a PCI device, for example. The device model
77 * represents the actual connections between buses and the devices they control.
78 * A bus is represented by the bus_type structure. It contains the name, the
79 * default attributes, the bus' methods, PM operations, and the driver core's
80 * private data.
81 */
1da177e4 82struct bus_type {
d462943a
GKH
83 const char *name;
84 struct bus_attribute *bus_attrs;
85 struct device_attribute *dev_attrs;
86 struct driver_attribute *drv_attrs;
87
88 int (*match)(struct device *dev, struct device_driver *drv);
89 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
90 int (*probe)(struct device *dev);
91 int (*remove)(struct device *dev);
92 void (*shutdown)(struct device *dev);
93
94 int (*suspend)(struct device *dev, pm_message_t state);
d462943a 95 int (*resume)(struct device *dev);
b8c5cec2 96
8150f32b 97 const struct dev_pm_ops *pm;
1eede070 98
6b6e39a6 99 struct subsys_private *p;
1da177e4
LT
100};
101
d462943a
GKH
102extern int __must_check bus_register(struct bus_type *bus);
103extern void bus_unregister(struct bus_type *bus);
1da177e4 104
d462943a 105extern int __must_check bus_rescan_devices(struct bus_type *bus);
1da177e4 106
1da177e4
LT
107/* iterator helpers for buses */
108
d462943a
GKH
109int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
110 int (*fn)(struct device *dev, void *data));
111struct device *bus_find_device(struct bus_type *bus, struct device *start,
112 void *data,
113 int (*match)(struct device *dev, void *data));
1f9ffc04
GKH
114struct device *bus_find_device_by_name(struct bus_type *bus,
115 struct device *start,
116 const char *name);
1da177e4 117
cc7447a5
JD
118int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
119 void *data, int (*fn)(struct device_driver *, void *));
1da177e4 120
99178b03
GKH
121void bus_sort_breadthfirst(struct bus_type *bus,
122 int (*compare)(const struct device *a,
123 const struct device *b));
116af378
BH
124/*
125 * Bus notifiers: Get notified of addition/removal of devices
126 * and binding/unbinding of drivers to devices.
127 * In the long run, it should be a replacement for the platform
128 * notify hooks.
129 */
130struct notifier_block;
131
132extern int bus_register_notifier(struct bus_type *bus,
133 struct notifier_block *nb);
134extern int bus_unregister_notifier(struct bus_type *bus,
135 struct notifier_block *nb);
136
137/* All 4 notifers below get called with the target struct device *
138 * as an argument. Note that those functions are likely to be called
8e9394ce 139 * with the device lock held in the core, so be careful.
116af378
BH
140 */
141#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
142#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
45daef0f
MD
143#define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be
144 bound */
145#define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */
146#define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be
116af378 147 unbound */
45daef0f 148#define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound
309b7d60 149 from the device */
116af378 150
0fed80f7 151extern struct kset *bus_get_kset(struct bus_type *bus);
b249072e 152extern struct klist *bus_get_device_klist(struct bus_type *bus);
0fed80f7 153
880ffb5c
WG
154/**
155 * struct device_driver - The basic device driver structure
156 * @name: Name of the device driver.
157 * @bus: The bus which the device of this driver belongs to.
158 * @owner: The module owner.
159 * @mod_name: Used for built-in modules.
160 * @suppress_bind_attrs: Disables bind/unbind via sysfs.
161 * @of_match_table: The open firmware table.
162 * @probe: Called to query the existence of a specific device,
163 * whether this driver can work with it, and bind the driver
164 * to a specific device.
165 * @remove: Called when the device is removed from the system to
166 * unbind a device from this driver.
167 * @shutdown: Called at shut-down time to quiesce the device.
168 * @suspend: Called to put the device to sleep mode. Usually to a
169 * low power state.
170 * @resume: Called to bring a device from sleep mode.
171 * @groups: Default attributes that get created by the driver core
172 * automatically.
173 * @pm: Power management operations of the device which matched
174 * this driver.
175 * @p: Driver core's private data, no one other than the driver
176 * core can touch this.
177 *
178 * The device driver-model tracks all of the drivers known to the system.
179 * The main reason for this tracking is to enable the driver core to match
180 * up drivers with new devices. Once drivers are known objects within the
181 * system, however, a number of other things become possible. Device drivers
182 * can export information and configuration variables that are independent
183 * of any specific device.
184 */
1da177e4 185struct device_driver {
e5dd1278
GKH
186 const char *name;
187 struct bus_type *bus;
1da177e4 188
e5dd1278 189 struct module *owner;
1a6f2a75
DT
190 const char *mod_name; /* used for built-in modules */
191
192 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
1da177e4 193
597b9d1e 194 const struct of_device_id *of_match_table;
597b9d1e 195
d462943a
GKH
196 int (*probe) (struct device *dev);
197 int (*remove) (struct device *dev);
198 void (*shutdown) (struct device *dev);
199 int (*suspend) (struct device *dev, pm_message_t state);
200 int (*resume) (struct device *dev);
a4dbd674 201 const struct attribute_group **groups;
e5dd1278 202
8150f32b 203 const struct dev_pm_ops *pm;
1eede070 204
e5dd1278 205 struct driver_private *p;
1da177e4
LT
206};
207
208
d462943a
GKH
209extern int __must_check driver_register(struct device_driver *drv);
210extern void driver_unregister(struct device_driver *drv);
1da177e4 211
d462943a
GKH
212extern struct device_driver *get_driver(struct device_driver *drv);
213extern void put_driver(struct device_driver *drv);
214extern struct device_driver *driver_find(const char *name,
215 struct bus_type *bus);
d779249e 216extern int driver_probe_done(void);
b23530eb 217extern void wait_for_device_probe(void);
216773a7 218
1da177e4 219
405ae7d3 220/* sysfs interface for exporting driver attributes */
1da177e4
LT
221
222struct driver_attribute {
d462943a
GKH
223 struct attribute attr;
224 ssize_t (*show)(struct device_driver *driver, char *buf);
225 ssize_t (*store)(struct device_driver *driver, const char *buf,
226 size_t count);
1da177e4
LT
227};
228
d462943a
GKH
229#define DRIVER_ATTR(_name, _mode, _show, _store) \
230struct driver_attribute driver_attr_##_name = \
231 __ATTR(_name, _mode, _show, _store)
1da177e4 232
d462943a 233extern int __must_check driver_create_file(struct device_driver *driver,
099c2f21 234 const struct driver_attribute *attr);
d462943a 235extern void driver_remove_file(struct device_driver *driver,
099c2f21 236 const struct driver_attribute *attr);
1da177e4 237
cbe9c595
GKH
238extern int __must_check driver_add_kobj(struct device_driver *drv,
239 struct kobject *kobj,
240 const char *fmt, ...);
241
d462943a
GKH
242extern int __must_check driver_for_each_device(struct device_driver *drv,
243 struct device *start,
244 void *data,
245 int (*fn)(struct device *dev,
246 void *));
247struct device *driver_find_device(struct device_driver *drv,
248 struct device *start, void *data,
249 int (*match)(struct device *dev, void *data));
fae3cd00 250
880ffb5c
WG
251/**
252 * struct class - device classes
253 * @name: Name of the class.
254 * @owner: The module owner.
255 * @class_attrs: Default attributes of this class.
256 * @dev_attrs: Default attributes of the devices belong to the class.
257 * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
258 * @dev_kobj: The kobject that represents this class and links it into the hierarchy.
259 * @dev_uevent: Called when a device is added, removed from this class, or a
260 * few other things that generate uevents to add the environment
261 * variables.
262 * @devnode: Callback to provide the devtmpfs.
263 * @class_release: Called to release this class.
264 * @dev_release: Called to release the device.
265 * @suspend: Used to put the device to sleep mode, usually to a low power
266 * state.
267 * @resume: Used to bring the device from the sleep mode.
268 * @ns_type: Callbacks so sysfs can detemine namespaces.
269 * @namespace: Namespace of the device belongs to this class.
270 * @pm: The default device power management operations of this class.
271 * @p: The private data of the driver core, no one other than the
272 * driver core can touch this.
273 *
274 * A class is a higher-level view of a device that abstracts out low-level
275 * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
276 * at the class level, they are all simply disks. Classes allow user space
277 * to work with devices based on what they do, rather than how they are
278 * connected or how they work.
1da177e4
LT
279 */
280struct class {
d462943a
GKH
281 const char *name;
282 struct module *owner;
1da177e4 283
d462943a 284 struct class_attribute *class_attrs;
d462943a 285 struct device_attribute *dev_attrs;
c97415a7 286 struct bin_attribute *dev_bin_attrs;
e105b8bf 287 struct kobject *dev_kobj;
1da177e4 288
d462943a 289 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
e454cea2 290 char *(*devnode)(struct device *dev, mode_t *mode);
1da177e4 291
d462943a
GKH
292 void (*class_release)(struct class *class);
293 void (*dev_release)(struct device *dev);
7c8265f5 294
d462943a
GKH
295 int (*suspend)(struct device *dev, pm_message_t state);
296 int (*resume)(struct device *dev);
1eede070 297
bc451f20
EB
298 const struct kobj_ns_type_operations *ns_type;
299 const void *(*namespace)(struct device *dev);
300
8150f32b
DT
301 const struct dev_pm_ops *pm;
302
6b6e39a6 303 struct subsys_private *p;
1da177e4
LT
304};
305
5a3ceb86
TH
306struct class_dev_iter {
307 struct klist_iter ki;
308 const struct device_type *type;
309};
310
e105b8bf
DW
311extern struct kobject *sysfs_dev_block_kobj;
312extern struct kobject *sysfs_dev_char_kobj;
d2a3b914
MW
313extern int __must_check __class_register(struct class *class,
314 struct lock_class_key *key);
d462943a 315extern void class_unregister(struct class *class);
d2a3b914
MW
316
317/* This is a #define to keep the compiler from merging different
318 * instances of the __key variable */
319#define class_register(class) \
320({ \
321 static struct lock_class_key __key; \
322 __class_register(class, &__key); \
323})
324
46227094
JD
325struct class_compat;
326struct class_compat *class_compat_register(const char *name);
327void class_compat_unregister(struct class_compat *cls);
328int class_compat_create_link(struct class_compat *cls, struct device *dev,
329 struct device *device_link);
330void class_compat_remove_link(struct class_compat *cls, struct device *dev,
331 struct device *device_link);
332
5a3ceb86
TH
333extern void class_dev_iter_init(struct class_dev_iter *iter,
334 struct class *class,
335 struct device *start,
336 const struct device_type *type);
337extern struct device *class_dev_iter_next(struct class_dev_iter *iter);
338extern void class_dev_iter_exit(struct class_dev_iter *iter);
339
93562b53
GKH
340extern int class_for_each_device(struct class *class, struct device *start,
341 void *data,
fd04897b 342 int (*fn)(struct device *dev, void *data));
695794ae
GKH
343extern struct device *class_find_device(struct class *class,
344 struct device *start, void *data,
fd04897b 345 int (*match)(struct device *, void *));
1da177e4
LT
346
347struct class_attribute {
d462943a 348 struct attribute attr;
28812fe1
AK
349 ssize_t (*show)(struct class *class, struct class_attribute *attr,
350 char *buf);
351 ssize_t (*store)(struct class *class, struct class_attribute *attr,
352 const char *buf, size_t count);
672d82c1
EB
353 const void *(*namespace)(struct class *class,
354 const struct class_attribute *attr);
1da177e4
LT
355};
356
d462943a
GKH
357#define CLASS_ATTR(_name, _mode, _show, _store) \
358struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
1da177e4 359
d462943a
GKH
360extern int __must_check class_create_file(struct class *class,
361 const struct class_attribute *attr);
362extern void class_remove_file(struct class *class,
363 const struct class_attribute *attr);
1da177e4 364
869dfc87
AK
365/* Simple class attribute that is just a static string */
366
367struct class_attribute_string {
368 struct class_attribute attr;
369 char *str;
370};
371
372/* Currently read-only only */
373#define _CLASS_ATTR_STRING(_name, _mode, _str) \
374 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str }
375#define CLASS_ATTR_STRING(_name, _mode, _str) \
376 struct class_attribute_string class_attr_##_name = \
377 _CLASS_ATTR_STRING(_name, _mode, _str)
378
379extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr,
380 char *buf);
381
1da177e4
LT
382struct class_interface {
383 struct list_head node;
384 struct class *class;
385
c47ed219
GKH
386 int (*add_dev) (struct device *, struct class_interface *);
387 void (*remove_dev) (struct device *, struct class_interface *);
1da177e4
LT
388};
389
4a7fb636 390extern int __must_check class_interface_register(struct class_interface *);
1da177e4
LT
391extern void class_interface_unregister(struct class_interface *);
392
d2a3b914
MW
393extern struct class * __must_check __class_create(struct module *owner,
394 const char *name,
395 struct lock_class_key *key);
e9ba6365 396extern void class_destroy(struct class *cls);
e9ba6365 397
d2a3b914
MW
398/* This is a #define to keep the compiler from merging different
399 * instances of the __key variable */
400#define class_create(owner, name) \
401({ \
402 static struct lock_class_key __key; \
403 __class_create(owner, name, &__key); \
404})
405
414264f9
KS
406/*
407 * The type of device, "struct device" is embedded in. A class
408 * or bus can contain devices of different types
409 * like "partitions" and "disks", "mouse" and "event".
410 * This identifies the device type and carries type-specific
411 * information, equivalent to the kobj_type of a kobject.
412 * If "name" is specified, the uevent will contain it in
413 * the DEVTYPE variable.
414 */
f9f852df 415struct device_type {
414264f9 416 const char *name;
a4dbd674 417 const struct attribute_group **groups;
7eff2e7a 418 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
e454cea2 419 char *(*devnode)(struct device *dev, mode_t *mode);
f9f852df 420 void (*release)(struct device *dev);
1eede070 421
8150f32b 422 const struct dev_pm_ops *pm;
f9f852df
KS
423};
424
a7fd6706
KS
425/* interface for exporting device attributes */
426struct device_attribute {
427 struct attribute attr;
428 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
429 char *buf);
430 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
431 const char *buf, size_t count);
432};
433
d462943a
GKH
434#define DEVICE_ATTR(_name, _mode, _show, _store) \
435struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
a7fd6706 436
4a7fb636 437extern int __must_check device_create_file(struct device *device,
66ecb92b 438 const struct device_attribute *entry);
d462943a 439extern void device_remove_file(struct device *dev,
26579ab7 440 const struct device_attribute *attr);
2589f188 441extern int __must_check device_create_bin_file(struct device *dev,
66ecb92b 442 const struct bin_attribute *attr);
2589f188 443extern void device_remove_bin_file(struct device *dev,
66ecb92b 444 const struct bin_attribute *attr);
523ded71 445extern int device_schedule_callback_owner(struct device *dev,
d462943a 446 void (*func)(struct device *dev), struct module *owner);
523ded71
AS
447
448/* This is a macro to avoid include problems with THIS_MODULE */
449#define device_schedule_callback(dev, func) \
450 device_schedule_callback_owner(dev, func, THIS_MODULE)
9ac7849e
TH
451
452/* device resource management */
453typedef void (*dr_release_t)(struct device *dev, void *res);
454typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
455
456#ifdef CONFIG_DEBUG_DEVRES
d462943a 457extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
9ac7849e
TH
458 const char *name);
459#define devres_alloc(release, size, gfp) \
460 __devres_alloc(release, size, gfp, #release)
461#else
d462943a 462extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
9ac7849e
TH
463#endif
464extern void devres_free(void *res);
465extern void devres_add(struct device *dev, void *res);
d462943a 466extern void *devres_find(struct device *dev, dr_release_t release,
9ac7849e 467 dr_match_t match, void *match_data);
d462943a
GKH
468extern void *devres_get(struct device *dev, void *new_res,
469 dr_match_t match, void *match_data);
470extern void *devres_remove(struct device *dev, dr_release_t release,
471 dr_match_t match, void *match_data);
9ac7849e
TH
472extern int devres_destroy(struct device *dev, dr_release_t release,
473 dr_match_t match, void *match_data);
474
475/* devres group */
476extern void * __must_check devres_open_group(struct device *dev, void *id,
477 gfp_t gfp);
478extern void devres_close_group(struct device *dev, void *id);
479extern void devres_remove_group(struct device *dev, void *id);
480extern int devres_release_group(struct device *dev, void *id);
481
482/* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
483extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
484extern void devm_kfree(struct device *dev, void *p);
485
6b7b6510
FT
486struct device_dma_parameters {
487 /*
488 * a low level driver may set these to teach IOMMU code about
489 * sg limitations.
490 */
491 unsigned int max_segment_size;
492 unsigned long segment_boundary_mask;
493};
494
880ffb5c
WG
495/**
496 * struct device - The basic device structure
497 * @parent: The device's "parent" device, the device to which it is attached.
498 * In most cases, a parent device is some sort of bus or host
499 * controller. If parent is NULL, the device, is a top-level device,
500 * which is not usually what you want.
501 * @p: Holds the private data of the driver core portions of the device.
502 * See the comment of the struct device_private for detail.
503 * @kobj: A top-level, abstract class from which other classes are derived.
504 * @init_name: Initial name of the device.
505 * @type: The type of device.
506 * This identifies the device type and carries type-specific
507 * information.
508 * @mutex: Mutex to synchronize calls to its driver.
509 * @bus: Type of bus device is on.
510 * @driver: Which driver has allocated this
511 * @platform_data: Platform data specific to the device.
512 * Example: For devices on custom boards, as typical of embedded
513 * and SOC based hardware, Linux often uses platform_data to point
514 * to board-specific structures describing devices and how they
515 * are wired. That can include what ports are available, chip
516 * variants, which GPIO pins act in what additional roles, and so
517 * on. This shrinks the "Board Support Packages" (BSPs) and
518 * minimizes board-specific #ifdefs in drivers.
519 * @power: For device power management.
520 * See Documentation/power/devices.txt for details.
564b905a 521 * @pm_domain: Provide callbacks that are executed during system suspend,
880ffb5c
WG
522 * hibernation, system resume and during runtime PM transitions
523 * along with subsystem-level and driver-level callbacks.
524 * @numa_node: NUMA node this device is close to.
525 * @dma_mask: Dma mask (if dma'ble device).
526 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
527 * hardware supports 64-bit addresses for consistent allocations
528 * such descriptors.
529 * @dma_parms: A low level driver may set these to teach IOMMU code about
530 * segment limitations.
531 * @dma_pools: Dma pools (if dma'ble device).
532 * @dma_mem: Internal for coherent mem override.
533 * @archdata: For arch-specific additions.
534 * @of_node: Associated device tree node.
880ffb5c
WG
535 * @devt: For creating the sysfs "dev".
536 * @devres_lock: Spinlock to protect the resource of the device.
537 * @devres_head: The resources list of the device.
538 * @knode_class: The node used to add the device to the class list.
539 * @class: The class of the device.
540 * @groups: Optional attribute groups.
541 * @release: Callback to free the device after all references have
542 * gone away. This should be set by the allocator of the
543 * device (i.e. the bus driver that discovered the device).
544 *
545 * At the lowest level, every device in a Linux system is represented by an
546 * instance of struct device. The device structure contains the information
547 * that the device model core needs to model the system. Most subsystems,
548 * however, track additional information about the devices they host. As a
549 * result, it is rare for devices to be represented by bare device structures;
550 * instead, that structure, like kobject structures, is usually embedded within
551 * a higher-level representation of the device.
552 */
1da177e4 553struct device {
49a4ec18 554 struct device *parent;
1da177e4 555
fb069a5d
GKH
556 struct device_private *p;
557
1da177e4 558 struct kobject kobj;
c906a48a 559 const char *init_name; /* initial name of the device */
aed65af1 560 const struct device_type *type;
1da177e4 561
3142788b 562 struct mutex mutex; /* mutex to synchronize calls to
af70316a
PM
563 * its driver.
564 */
565
d462943a 566 struct bus_type *bus; /* type of bus device is on */
1da177e4
LT
567 struct device_driver *driver; /* which driver has allocated this
568 device */
e67c8562
GKH
569 void *platform_data; /* Platform specific data, device
570 core doesn't touch it */
1da177e4 571 struct dev_pm_info power;
564b905a 572 struct dev_pm_domain *pm_domain;
1da177e4 573
87348136
CH
574#ifdef CONFIG_NUMA
575 int numa_node; /* NUMA node this device is close to */
576#endif
1da177e4
LT
577 u64 *dma_mask; /* dma mask (if dma'able device) */
578 u64 coherent_dma_mask;/* Like dma_mask, but for
579 alloc_coherent mappings as
580 not all hardware supports
581 64 bit addresses for consistent
582 allocations such descriptors. */
583
6b7b6510
FT
584 struct device_dma_parameters *dma_parms;
585
1da177e4
LT
586 struct list_head dma_pools; /* dma pools (if dma'ble) */
587
588 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
589 override */
c6dbaef2
BH
590 /* arch specific additions */
591 struct dev_archdata archdata;
c9e358df
GL
592
593 struct device_node *of_node; /* associated device tree node */
1da177e4 594
929d2fa5
MW
595 dev_t devt; /* dev_t, creates the sysfs "dev" */
596
9ac7849e
TH
597 spinlock_t devres_lock;
598 struct list_head devres_head;
599
5a3ceb86 600 struct klist_node knode_class;
b7a3e813 601 struct class *class;
a4dbd674 602 const struct attribute_group **groups; /* optional groups */
23681e47 603
d462943a 604 void (*release)(struct device *dev);
1da177e4
LT
605};
606
9a3df1f7
AS
607/* Get the wakeup routines, which depend on struct device */
608#include <linux/pm_wakeup.h>
609
bf9ca69f 610static inline const char *dev_name(const struct device *dev)
06916639 611{
a636ee7f
PM
612 /* Use the init name until the kobject becomes available */
613 if (dev->init_name)
614 return dev->init_name;
615
1fa5ae85 616 return kobject_name(&dev->kobj);
06916639
KS
617}
618
413c239f
SR
619extern int dev_set_name(struct device *dev, const char *name, ...)
620 __attribute__((format(printf, 2, 3)));
621
87348136
CH
622#ifdef CONFIG_NUMA
623static inline int dev_to_node(struct device *dev)
624{
625 return dev->numa_node;
626}
627static inline void set_dev_node(struct device *dev, int node)
628{
629 dev->numa_node = node;
630}
631#else
632static inline int dev_to_node(struct device *dev)
633{
634 return -1;
635}
636static inline void set_dev_node(struct device *dev, int node)
637{
638}
639#endif
640
5c095a0e
RW
641static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
642{
643 return dev ? dev->power.subsys_data : NULL;
644}
645
f67f129e
ML
646static inline unsigned int dev_get_uevent_suppress(const struct device *dev)
647{
648 return dev->kobj.uevent_suppress;
649}
650
651static inline void dev_set_uevent_suppress(struct device *dev, int val)
652{
653 dev->kobj.uevent_suppress = val;
654}
655
d305ef5d
DR
656static inline int device_is_registered(struct device *dev)
657{
3f62e570 658 return dev->kobj.state_in_sysfs;
d305ef5d
DR
659}
660
5af84b82
RW
661static inline void device_enable_async_suspend(struct device *dev)
662{
f76b168b 663 if (!dev->power.is_prepared)
5af84b82
RW
664 dev->power.async_suspend = true;
665}
666
5a2eb858
RW
667static inline void device_disable_async_suspend(struct device *dev)
668{
f76b168b 669 if (!dev->power.is_prepared)
5a2eb858
RW
670 dev->power.async_suspend = false;
671}
672
673static inline bool device_async_suspend_enabled(struct device *dev)
674{
675 return !!dev->power.async_suspend;
676}
677
8e9394ce
GKH
678static inline void device_lock(struct device *dev)
679{
3142788b 680 mutex_lock(&dev->mutex);
8e9394ce
GKH
681}
682
683static inline int device_trylock(struct device *dev)
684{
3142788b 685 return mutex_trylock(&dev->mutex);
8e9394ce
GKH
686}
687
688static inline void device_unlock(struct device *dev)
689{
3142788b 690 mutex_unlock(&dev->mutex);
8e9394ce
GKH
691}
692
1f21782e
AB
693void driver_init(void);
694
1da177e4
LT
695/*
696 * High level routines for use by the bus drivers
697 */
d462943a
GKH
698extern int __must_check device_register(struct device *dev);
699extern void device_unregister(struct device *dev);
700extern void device_initialize(struct device *dev);
701extern int __must_check device_add(struct device *dev);
702extern void device_del(struct device *dev);
703extern int device_for_each_child(struct device *dev, void *data,
704 int (*fn)(struct device *dev, void *data));
705extern struct device *device_find_child(struct device *dev, void *data,
706 int (*match)(struct device *dev, void *data));
6937e8f8 707extern int device_rename(struct device *dev, const char *new_name);
ffa6a705
CH
708extern int device_move(struct device *dev, struct device *new_parent,
709 enum dpm_order dpm_order);
e454cea2
KS
710extern const char *device_get_devnode(struct device *dev,
711 mode_t *mode, const char **tmp);
b4028437 712extern void *dev_get_drvdata(const struct device *dev);
c8705082 713extern int dev_set_drvdata(struct device *dev, void *data);
1da177e4 714
0aa0dc41
MM
715/*
716 * Root device objects for grouping under /sys/devices
717 */
718extern struct device *__root_device_register(const char *name,
719 struct module *owner);
720static inline struct device *root_device_register(const char *name)
721{
722 return __root_device_register(name, THIS_MODULE);
723}
724extern void root_device_unregister(struct device *root);
725
a5b8b1ad
MB
726static inline void *dev_get_platdata(const struct device *dev)
727{
728 return dev->platform_data;
729}
730
1da177e4
LT
731/*
732 * Manual binding of a device to driver. See drivers/base/bus.c
733 * for information on use.
734 */
f86db396 735extern int __must_check device_bind_driver(struct device *dev);
d462943a
GKH
736extern void device_release_driver(struct device *dev);
737extern int __must_check device_attach(struct device *dev);
f86db396
AM
738extern int __must_check driver_attach(struct device_driver *drv);
739extern int __must_check device_reprobe(struct device *dev);
1da177e4 740
23681e47
GKH
741/*
742 * Easy functions for dynamically creating devices on the fly
743 */
8882b394
GKH
744extern struct device *device_create_vargs(struct class *cls,
745 struct device *parent,
746 dev_t devt,
747 void *drvdata,
748 const char *fmt,
749 va_list vargs);
4e106739
GKH
750extern struct device *device_create(struct class *cls, struct device *parent,
751 dev_t devt, void *drvdata,
752 const char *fmt, ...)
8882b394 753 __attribute__((format(printf, 5, 6)));
23681e47 754extern void device_destroy(struct class *cls, dev_t devt);
1da177e4 755
1da177e4
LT
756/*
757 * Platform "fixup" functions - allow the platform to have their say
758 * about devices and actions that the general device layer doesn't
759 * know about.
760 */
761/* Notify platform of device discovery */
d462943a 762extern int (*platform_notify)(struct device *dev);
1da177e4 763
d462943a 764extern int (*platform_notify_remove)(struct device *dev);
1da177e4
LT
765
766
880ffb5c 767/*
1da177e4
LT
768 * get_device - atomically increment the reference count for the device.
769 *
770 */
d462943a
GKH
771extern struct device *get_device(struct device *dev);
772extern void put_device(struct device *dev);
1da177e4 773
d4d5291c 774extern void wait_for_device_probe(void);
1da177e4 775
2b2af54a
KS
776#ifdef CONFIG_DEVTMPFS
777extern int devtmpfs_create_node(struct device *dev);
778extern int devtmpfs_delete_node(struct device *dev);
073120cc 779extern int devtmpfs_mount(const char *mntdir);
2b2af54a
KS
780#else
781static inline int devtmpfs_create_node(struct device *dev) { return 0; }
782static inline int devtmpfs_delete_node(struct device *dev) { return 0; }
783static inline int devtmpfs_mount(const char *mountpoint) { return 0; }
784#endif
785
116f232b 786/* drivers/base/power/shutdown.c */
1da177e4
LT
787extern void device_shutdown(void);
788
1da177e4 789/* debugging and troubleshooting/diagnostic helpers. */
bf9ca69f 790extern const char *dev_driver_string(const struct device *dev);
99bcf217
JP
791
792
793#ifdef CONFIG_PRINTK
794
cbc46635
JP
795extern int __dev_printk(const char *level, const struct device *dev,
796 struct va_format *vaf);
99bcf217
JP
797extern int dev_printk(const char *level, const struct device *dev,
798 const char *fmt, ...)
799 __attribute__ ((format (printf, 3, 4)));
800extern int dev_emerg(const struct device *dev, const char *fmt, ...)
801 __attribute__ ((format (printf, 2, 3)));
802extern int dev_alert(const struct device *dev, const char *fmt, ...)
803 __attribute__ ((format (printf, 2, 3)));
804extern int dev_crit(const struct device *dev, const char *fmt, ...)
805 __attribute__ ((format (printf, 2, 3)));
806extern int dev_err(const struct device *dev, const char *fmt, ...)
807 __attribute__ ((format (printf, 2, 3)));
808extern int dev_warn(const struct device *dev, const char *fmt, ...)
809 __attribute__ ((format (printf, 2, 3)));
810extern int dev_notice(const struct device *dev, const char *fmt, ...)
811 __attribute__ ((format (printf, 2, 3)));
812extern int _dev_info(const struct device *dev, const char *fmt, ...)
813 __attribute__ ((format (printf, 2, 3)));
814
815#else
816
cbc46635
JP
817static inline int __dev_printk(const char *level, const struct device *dev,
818 struct va_format *vaf)
819 { return 0; }
99bcf217
JP
820static inline int dev_printk(const char *level, const struct device *dev,
821 const char *fmt, ...)
822 __attribute__ ((format (printf, 3, 4)));
823static inline int dev_printk(const char *level, const struct device *dev,
824 const char *fmt, ...)
825 { return 0; }
826
827static inline int dev_emerg(const struct device *dev, const char *fmt, ...)
828 __attribute__ ((format (printf, 2, 3)));
829static inline int dev_emerg(const struct device *dev, const char *fmt, ...)
830 { return 0; }
831static inline int dev_crit(const struct device *dev, const char *fmt, ...)
832 __attribute__ ((format (printf, 2, 3)));
833static inline int dev_crit(const struct device *dev, const char *fmt, ...)
834 { return 0; }
835static inline int dev_alert(const struct device *dev, const char *fmt, ...)
836 __attribute__ ((format (printf, 2, 3)));
837static inline int dev_alert(const struct device *dev, const char *fmt, ...)
838 { return 0; }
839static inline int dev_err(const struct device *dev, const char *fmt, ...)
840 __attribute__ ((format (printf, 2, 3)));
841static inline int dev_err(const struct device *dev, const char *fmt, ...)
842 { return 0; }
843static inline int dev_warn(const struct device *dev, const char *fmt, ...)
844 __attribute__ ((format (printf, 2, 3)));
845static inline int dev_warn(const struct device *dev, const char *fmt, ...)
846 { return 0; }
847static inline int dev_notice(const struct device *dev, const char *fmt, ...)
848 __attribute__ ((format (printf, 2, 3)));
849static inline int dev_notice(const struct device *dev, const char *fmt, ...)
850 { return 0; }
851static inline int _dev_info(const struct device *dev, const char *fmt, ...)
852 __attribute__ ((format (printf, 2, 3)));
853static inline int _dev_info(const struct device *dev, const char *fmt, ...)
854 { return 0; }
855
856#endif
857
858/*
859 * Stupid hackaround for existing uses of non-printk uses dev_info
860 *
861 * Note that the definition of dev_info below is actually _dev_info
862 * and a macro is used to avoid redefining dev_info
863 */
864
865#define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg)
7b8712e5 866
d0d85ff9
CH
867#if defined(DEBUG)
868#define dev_dbg(dev, format, arg...) \
99bcf217 869 dev_printk(KERN_DEBUG, dev, format, ##arg)
e9d376f0 870#elif defined(CONFIG_DYNAMIC_DEBUG)
99bcf217
JP
871#define dev_dbg(dev, format, ...) \
872do { \
346e15be 873 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \
99bcf217 874} while (0)
1da177e4 875#else
99bcf217
JP
876#define dev_dbg(dev, format, arg...) \
877({ \
878 if (0) \
879 dev_printk(KERN_DEBUG, dev, format, ##arg); \
880 0; \
881})
1da177e4
LT
882#endif
883
aebdc3b4
DB
884#ifdef VERBOSE_DEBUG
885#define dev_vdbg dev_dbg
886#else
99bcf217
JP
887#define dev_vdbg(dev, format, arg...) \
888({ \
889 if (0) \
890 dev_printk(KERN_DEBUG, dev, format, ##arg); \
891 0; \
892})
aebdc3b4
DB
893#endif
894
e6139662 895/*
bcdd323b 896 * dev_WARN*() acts like dev_printk(), but with the key difference
e6139662
AV
897 * of using a WARN/WARN_ON to get the message out, including the
898 * file/line information and a backtrace.
899 */
900#define dev_WARN(dev, format, arg...) \
901 WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg);
902
bcdd323b
FB
903#define dev_WARN_ONCE(dev, condition, format, arg...) \
904 WARN_ONCE(condition, "Device %s\n" format, \
905 dev_driver_string(dev), ## arg)
906
1da177e4
LT
907/* Create alias, so I can be autoloaded. */
908#define MODULE_ALIAS_CHARDEV(major,minor) \
909 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
910#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
911 MODULE_ALIAS("char-major-" __stringify(major) "-*")
e52eec13
AK
912
913#ifdef CONFIG_SYSFS_DEPRECATED
914extern long sysfs_deprecated;
915#else
916#define sysfs_deprecated 0
917#endif
918
1da177e4 919#endif /* _DEVICE_H_ */
This page took 1.178439 seconds and 4 git commands to generate.