2 * device.h - generic, centralized driver model
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5 * Copyright (c) 2004-2007 Greg Kroah-Hartman <gregkh@suse.de>
7 * This file is released under the GPLv2
9 * See Documentation/driver-model/ for more information.
15 #include <linux/ioport.h>
16 #include <linux/kobject.h>
17 #include <linux/klist.h>
18 #include <linux/list.h>
19 #include <linux/compiler.h>
20 #include <linux/types.h>
21 #include <linux/module.h>
23 #include <linux/semaphore.h>
24 #include <asm/atomic.h>
25 #include <asm/device.h>
27 #define DEVICE_NAME_SIZE 50
28 /* DEVICE_NAME_HALF is really less than half to accommodate slop */
29 #define DEVICE_NAME_HALF __stringify(20)
30 #define DEVICE_ID_SIZE 32
31 #define BUS_ID_SIZE KOBJ_NAME_LEN
36 struct driver_private
;
40 struct bus_type_private
;
42 struct bus_attribute
{
43 struct attribute attr
;
44 ssize_t (*show
)(struct bus_type
*bus
, char *buf
);
45 ssize_t (*store
)(struct bus_type
*bus
, const char *buf
, size_t count
);
48 #define BUS_ATTR(_name, _mode, _show, _store) \
49 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
51 extern int __must_check
bus_create_file(struct bus_type
*,
52 struct bus_attribute
*);
53 extern void bus_remove_file(struct bus_type
*, struct bus_attribute
*);
57 struct bus_attribute
*bus_attrs
;
58 struct device_attribute
*dev_attrs
;
59 struct driver_attribute
*drv_attrs
;
61 int (*match
)(struct device
*dev
, struct device_driver
*drv
);
62 int (*uevent
)(struct device
*dev
, struct kobj_uevent_env
*env
);
63 int (*probe
)(struct device
*dev
);
64 int (*remove
)(struct device
*dev
);
65 void (*shutdown
)(struct device
*dev
);
67 int (*suspend
)(struct device
*dev
, pm_message_t state
);
68 int (*suspend_late
)(struct device
*dev
, pm_message_t state
);
69 int (*resume_early
)(struct device
*dev
);
70 int (*resume
)(struct device
*dev
);
72 struct pm_ext_ops
*pm
;
74 struct bus_type_private
*p
;
77 extern int __must_check
bus_register(struct bus_type
*bus
);
78 extern void bus_unregister(struct bus_type
*bus
);
80 extern int __must_check
bus_rescan_devices(struct bus_type
*bus
);
82 /* iterator helpers for buses */
84 int bus_for_each_dev(struct bus_type
*bus
, struct device
*start
, void *data
,
85 int (*fn
)(struct device
*dev
, void *data
));
86 struct device
*bus_find_device(struct bus_type
*bus
, struct device
*start
,
88 int (*match
)(struct device
*dev
, void *data
));
89 struct device
*bus_find_device_by_name(struct bus_type
*bus
,
93 int __must_check
bus_for_each_drv(struct bus_type
*bus
,
94 struct device_driver
*start
, void *data
,
95 int (*fn
)(struct device_driver
*, void *));
98 * Bus notifiers: Get notified of addition/removal of devices
99 * and binding/unbinding of drivers to devices.
100 * In the long run, it should be a replacement for the platform
103 struct notifier_block
;
105 extern int bus_register_notifier(struct bus_type
*bus
,
106 struct notifier_block
*nb
);
107 extern int bus_unregister_notifier(struct bus_type
*bus
,
108 struct notifier_block
*nb
);
110 /* All 4 notifers below get called with the target struct device *
111 * as an argument. Note that those functions are likely to be called
112 * with the device semaphore held in the core, so be careful.
114 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
115 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
116 #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
117 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
120 extern struct kset
*bus_get_kset(struct bus_type
*bus
);
121 extern struct klist
*bus_get_device_klist(struct bus_type
*bus
);
123 struct device_driver
{
125 struct bus_type
*bus
;
127 struct module
*owner
;
128 const char *mod_name
; /* used for built-in modules */
130 int (*probe
) (struct device
*dev
);
131 int (*remove
) (struct device
*dev
);
132 void (*shutdown
) (struct device
*dev
);
133 int (*suspend
) (struct device
*dev
, pm_message_t state
);
134 int (*resume
) (struct device
*dev
);
135 struct attribute_group
**groups
;
139 struct driver_private
*p
;
143 extern int __must_check
driver_register(struct device_driver
*drv
);
144 extern void driver_unregister(struct device_driver
*drv
);
146 extern struct device_driver
*get_driver(struct device_driver
*drv
);
147 extern void put_driver(struct device_driver
*drv
);
148 extern struct device_driver
*driver_find(const char *name
,
149 struct bus_type
*bus
);
150 extern int driver_probe_done(void);
152 /* sysfs interface for exporting driver attributes */
154 struct driver_attribute
{
155 struct attribute attr
;
156 ssize_t (*show
)(struct device_driver
*driver
, char *buf
);
157 ssize_t (*store
)(struct device_driver
*driver
, const char *buf
,
161 #define DRIVER_ATTR(_name, _mode, _show, _store) \
162 struct driver_attribute driver_attr_##_name = \
163 __ATTR(_name, _mode, _show, _store)
165 extern int __must_check
driver_create_file(struct device_driver
*driver
,
166 struct driver_attribute
*attr
);
167 extern void driver_remove_file(struct device_driver
*driver
,
168 struct driver_attribute
*attr
);
170 extern int __must_check
driver_add_kobj(struct device_driver
*drv
,
171 struct kobject
*kobj
,
172 const char *fmt
, ...);
174 extern int __must_check
driver_for_each_device(struct device_driver
*drv
,
175 struct device
*start
,
177 int (*fn
)(struct device
*dev
,
179 struct device
*driver_find_device(struct device_driver
*drv
,
180 struct device
*start
, void *data
,
181 int (*match
)(struct device
*dev
, void *data
));
188 struct module
*owner
;
190 struct class_attribute
*class_attrs
;
191 struct device_attribute
*dev_attrs
;
192 struct kobject
*dev_kobj
;
194 int (*dev_uevent
)(struct device
*dev
, struct kobj_uevent_env
*env
);
196 void (*class_release
)(struct class *class);
197 void (*dev_release
)(struct device
*dev
);
199 int (*suspend
)(struct device
*dev
, pm_message_t state
);
200 int (*resume
)(struct device
*dev
);
203 struct class_private
*p
;
206 extern struct kobject
*sysfs_dev_block_kobj
;
207 extern struct kobject
*sysfs_dev_char_kobj
;
208 extern int __must_check
class_register(struct class *class);
209 extern void class_unregister(struct class *class);
210 extern int class_for_each_device(struct class *class, struct device
*start
,
212 int (*fn
)(struct device
*dev
, void *data
));
213 extern struct device
*class_find_device(struct class *class,
214 struct device
*start
, void *data
,
215 int (*match
)(struct device
*, void *));
217 struct class_attribute
{
218 struct attribute attr
;
219 ssize_t (*show
)(struct class *class, char *buf
);
220 ssize_t (*store
)(struct class *class, const char *buf
, size_t count
);
223 #define CLASS_ATTR(_name, _mode, _show, _store) \
224 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
226 extern int __must_check
class_create_file(struct class *class,
227 const struct class_attribute
*attr
);
228 extern void class_remove_file(struct class *class,
229 const struct class_attribute
*attr
);
231 struct class_interface
{
232 struct list_head node
;
235 int (*add_dev
) (struct device
*, struct class_interface
*);
236 void (*remove_dev
) (struct device
*, struct class_interface
*);
239 extern int __must_check
class_interface_register(struct class_interface
*);
240 extern void class_interface_unregister(struct class_interface
*);
242 extern struct class *class_create(struct module
*owner
, const char *name
);
243 extern void class_destroy(struct class *cls
);
246 * The type of device, "struct device" is embedded in. A class
247 * or bus can contain devices of different types
248 * like "partitions" and "disks", "mouse" and "event".
249 * This identifies the device type and carries type-specific
250 * information, equivalent to the kobj_type of a kobject.
251 * If "name" is specified, the uevent will contain it in
252 * the DEVTYPE variable.
256 struct attribute_group
**groups
;
257 int (*uevent
)(struct device
*dev
, struct kobj_uevent_env
*env
);
258 void (*release
)(struct device
*dev
);
260 int (*suspend
)(struct device
*dev
, pm_message_t state
);
261 int (*resume
)(struct device
*dev
);
266 /* interface for exporting device attributes */
267 struct device_attribute
{
268 struct attribute attr
;
269 ssize_t (*show
)(struct device
*dev
, struct device_attribute
*attr
,
271 ssize_t (*store
)(struct device
*dev
, struct device_attribute
*attr
,
272 const char *buf
, size_t count
);
275 #define DEVICE_ATTR(_name, _mode, _show, _store) \
276 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
278 extern int __must_check
device_create_file(struct device
*device
,
279 struct device_attribute
*entry
);
280 extern void device_remove_file(struct device
*dev
,
281 struct device_attribute
*attr
);
282 extern int __must_check
device_create_bin_file(struct device
*dev
,
283 struct bin_attribute
*attr
);
284 extern void device_remove_bin_file(struct device
*dev
,
285 struct bin_attribute
*attr
);
286 extern int device_schedule_callback_owner(struct device
*dev
,
287 void (*func
)(struct device
*dev
), struct module
*owner
);
289 /* This is a macro to avoid include problems with THIS_MODULE */
290 #define device_schedule_callback(dev, func) \
291 device_schedule_callback_owner(dev, func, THIS_MODULE)
293 /* device resource management */
294 typedef void (*dr_release_t
)(struct device
*dev
, void *res
);
295 typedef int (*dr_match_t
)(struct device
*dev
, void *res
, void *match_data
);
297 #ifdef CONFIG_DEBUG_DEVRES
298 extern void *__devres_alloc(dr_release_t release
, size_t size
, gfp_t gfp
,
300 #define devres_alloc(release, size, gfp) \
301 __devres_alloc(release, size, gfp, #release)
303 extern void *devres_alloc(dr_release_t release
, size_t size
, gfp_t gfp
);
305 extern void devres_free(void *res
);
306 extern void devres_add(struct device
*dev
, void *res
);
307 extern void *devres_find(struct device
*dev
, dr_release_t release
,
308 dr_match_t match
, void *match_data
);
309 extern void *devres_get(struct device
*dev
, void *new_res
,
310 dr_match_t match
, void *match_data
);
311 extern void *devres_remove(struct device
*dev
, dr_release_t release
,
312 dr_match_t match
, void *match_data
);
313 extern int devres_destroy(struct device
*dev
, dr_release_t release
,
314 dr_match_t match
, void *match_data
);
317 extern void * __must_check
devres_open_group(struct device
*dev
, void *id
,
319 extern void devres_close_group(struct device
*dev
, void *id
);
320 extern void devres_remove_group(struct device
*dev
, void *id
);
321 extern int devres_release_group(struct device
*dev
, void *id
);
323 /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
324 extern void *devm_kzalloc(struct device
*dev
, size_t size
, gfp_t gfp
);
325 extern void devm_kfree(struct device
*dev
, void *p
);
327 struct device_dma_parameters
{
329 * a low level driver may set these to teach IOMMU code about
332 unsigned int max_segment_size
;
333 unsigned long segment_boundary_mask
;
337 struct klist klist_children
;
338 struct klist_node knode_parent
; /* node in sibling list */
339 struct klist_node knode_driver
;
340 struct klist_node knode_bus
;
341 struct device
*parent
;
344 char bus_id
[BUS_ID_SIZE
]; /* position on parent bus */
345 struct device_type
*type
;
346 unsigned uevent_suppress
:1;
348 struct semaphore sem
; /* semaphore to synchronize calls to
352 struct bus_type
*bus
; /* type of bus device is on */
353 struct device_driver
*driver
; /* which driver has allocated this
355 void *driver_data
; /* data private to the driver */
356 void *platform_data
; /* Platform specific data, device
357 core doesn't touch it */
358 struct dev_pm_info power
;
361 int numa_node
; /* NUMA node this device is close to */
363 u64
*dma_mask
; /* dma mask (if dma'able device) */
364 u64 coherent_dma_mask
;/* Like dma_mask, but for
365 alloc_coherent mappings as
366 not all hardware supports
367 64 bit addresses for consistent
368 allocations such descriptors. */
370 struct device_dma_parameters
*dma_parms
;
372 struct list_head dma_pools
; /* dma pools (if dma'ble) */
374 struct dma_coherent_mem
*dma_mem
; /* internal for coherent mem
376 /* arch specific additions */
377 struct dev_archdata archdata
;
379 spinlock_t devres_lock
;
380 struct list_head devres_head
;
382 struct list_head node
;
384 dev_t devt
; /* dev_t, creates the sysfs "dev" */
385 struct attribute_group
**groups
; /* optional groups */
387 void (*release
)(struct device
*dev
);
390 /* Get the wakeup routines, which depend on struct device */
391 #include <linux/pm_wakeup.h>
393 static inline const char *dev_name(struct device
*dev
)
395 /* will be changed into kobject_name(&dev->kobj) in the near future */
399 extern int dev_set_name(struct device
*dev
, const char *name
, ...)
400 __attribute__((format(printf
, 2, 3)));
403 static inline int dev_to_node(struct device
*dev
)
405 return dev
->numa_node
;
407 static inline void set_dev_node(struct device
*dev
, int node
)
409 dev
->numa_node
= node
;
412 static inline int dev_to_node(struct device
*dev
)
416 static inline void set_dev_node(struct device
*dev
, int node
)
421 static inline void *dev_get_drvdata(struct device
*dev
)
423 return dev
->driver_data
;
426 static inline void dev_set_drvdata(struct device
*dev
, void *data
)
428 dev
->driver_data
= data
;
431 static inline int device_is_registered(struct device
*dev
)
433 return dev
->kobj
.state_in_sysfs
;
436 void driver_init(void);
439 * High level routines for use by the bus drivers
441 extern int __must_check
device_register(struct device
*dev
);
442 extern void device_unregister(struct device
*dev
);
443 extern void device_initialize(struct device
*dev
);
444 extern int __must_check
device_add(struct device
*dev
);
445 extern void device_del(struct device
*dev
);
446 extern int device_for_each_child(struct device
*dev
, void *data
,
447 int (*fn
)(struct device
*dev
, void *data
));
448 extern struct device
*device_find_child(struct device
*dev
, void *data
,
449 int (*match
)(struct device
*dev
, void *data
));
450 extern int device_rename(struct device
*dev
, char *new_name
);
451 extern int device_move(struct device
*dev
, struct device
*new_parent
);
454 * Manual binding of a device to driver. See drivers/base/bus.c
455 * for information on use.
457 extern int __must_check
device_bind_driver(struct device
*dev
);
458 extern void device_release_driver(struct device
*dev
);
459 extern int __must_check
device_attach(struct device
*dev
);
460 extern int __must_check
driver_attach(struct device_driver
*drv
);
461 extern int __must_check
device_reprobe(struct device
*dev
);
464 * Easy functions for dynamically creating devices on the fly
466 extern struct device
*device_create_vargs(struct class *cls
,
467 struct device
*parent
,
472 extern struct device
*device_create(struct class *cls
, struct device
*parent
,
473 dev_t devt
, void *drvdata
,
474 const char *fmt
, ...)
475 __attribute__((format(printf
, 5, 6)));
476 #define device_create_drvdata device_create
477 extern void device_destroy(struct class *cls
, dev_t devt
);
480 * Platform "fixup" functions - allow the platform to have their say
481 * about devices and actions that the general device layer doesn't
484 /* Notify platform of device discovery */
485 extern int (*platform_notify
)(struct device
*dev
);
487 extern int (*platform_notify_remove
)(struct device
*dev
);
491 * get_device - atomically increment the reference count for the device.
494 extern struct device
*get_device(struct device
*dev
);
495 extern void put_device(struct device
*dev
);
498 /* drivers/base/power/shutdown.c */
499 extern void device_shutdown(void);
501 /* drivers/base/sys.c */
502 extern void sysdev_shutdown(void);
504 /* debugging and troubleshooting/diagnostic helpers. */
505 extern const char *dev_driver_string(struct device
*dev
);
506 #define dev_printk(level, dev, format, arg...) \
507 printk(level "%s %s: " format , dev_driver_string(dev) , \
508 dev_name(dev) , ## arg)
510 #define dev_emerg(dev, format, arg...) \
511 dev_printk(KERN_EMERG , dev , format , ## arg)
512 #define dev_alert(dev, format, arg...) \
513 dev_printk(KERN_ALERT , dev , format , ## arg)
514 #define dev_crit(dev, format, arg...) \
515 dev_printk(KERN_CRIT , dev , format , ## arg)
516 #define dev_err(dev, format, arg...) \
517 dev_printk(KERN_ERR , dev , format , ## arg)
518 #define dev_warn(dev, format, arg...) \
519 dev_printk(KERN_WARNING , dev , format , ## arg)
520 #define dev_notice(dev, format, arg...) \
521 dev_printk(KERN_NOTICE , dev , format , ## arg)
522 #define dev_info(dev, format, arg...) \
523 dev_printk(KERN_INFO , dev , format , ## arg)
526 #define dev_dbg(dev, format, arg...) \
527 dev_printk(KERN_DEBUG , dev , format , ## arg)
529 #define dev_dbg(dev, format, arg...) \
530 ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
534 #define dev_vdbg dev_dbg
537 #define dev_vdbg(dev, format, arg...) \
538 ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
541 /* Create alias, so I can be autoloaded. */
542 #define MODULE_ALIAS_CHARDEV(major,minor) \
543 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
544 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
545 MODULE_ALIAS("char-major-" __stringify(major) "-*")
546 #endif /* _DEVICE_H_ */