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/lockdep.h>
20 #include <linux/compiler.h>
21 #include <linux/types.h>
22 #include <linux/module.h>
24 #include <linux/semaphore.h>
25 #include <asm/atomic.h>
26 #include <asm/device.h>
28 #define DEVICE_NAME_SIZE 50
29 /* DEVICE_NAME_HALF is really less than half to accommodate slop */
30 #define DEVICE_NAME_HALF __stringify(20)
31 #define DEVICE_ID_SIZE 32
32 #define BUS_ID_SIZE KOBJ_NAME_LEN
37 struct driver_private;
41 struct bus_type_private;
43 struct bus_attribute {
44 struct attribute attr;
45 ssize_t (*show)(struct bus_type *bus, char *buf);
46 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
49 #define BUS_ATTR(_name, _mode, _show, _store) \
50 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
52 extern int __must_check bus_create_file(struct bus_type *,
53 struct bus_attribute *);
54 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
58 struct bus_attribute *bus_attrs;
59 struct device_attribute *dev_attrs;
60 struct driver_attribute *drv_attrs;
62 int (*match)(struct device *dev, struct device_driver *drv);
63 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
64 int (*probe)(struct device *dev);
65 int (*remove)(struct device *dev);
66 void (*shutdown)(struct device *dev);
68 int (*suspend)(struct device *dev, pm_message_t state);
69 int (*suspend_late)(struct device *dev, pm_message_t state);
70 int (*resume_early)(struct device *dev);
71 int (*resume)(struct device *dev);
73 struct pm_ext_ops *pm;
75 struct bus_type_private *p;
78 extern int __must_check bus_register(struct bus_type *bus);
79 extern void bus_unregister(struct bus_type *bus);
81 extern int __must_check bus_rescan_devices(struct bus_type *bus);
83 /* iterator helpers for buses */
85 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
86 int (*fn)(struct device *dev, void *data));
87 struct device *bus_find_device(struct bus_type *bus, struct device *start,
89 int (*match)(struct device *dev, void *data));
90 struct device *bus_find_device_by_name(struct bus_type *bus,
94 int __must_check bus_for_each_drv(struct bus_type *bus,
95 struct device_driver *start, void *data,
96 int (*fn)(struct device_driver *, void *));
99 * Bus notifiers: Get notified of addition/removal of devices
100 * and binding/unbinding of drivers to devices.
101 * In the long run, it should be a replacement for the platform
104 struct notifier_block;
106 extern int bus_register_notifier(struct bus_type *bus,
107 struct notifier_block *nb);
108 extern int bus_unregister_notifier(struct bus_type *bus,
109 struct notifier_block *nb);
111 /* All 4 notifers below get called with the target struct device *
112 * as an argument. Note that those functions are likely to be called
113 * with the device semaphore held in the core, so be careful.
115 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
116 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
117 #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
118 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
121 extern struct kset *bus_get_kset(struct bus_type *bus);
122 extern struct klist *bus_get_device_klist(struct bus_type *bus);
124 struct device_driver {
126 struct bus_type *bus;
128 struct module *owner;
129 const char *mod_name; /* used for built-in modules */
131 int (*probe) (struct device *dev);
132 int (*remove) (struct device *dev);
133 void (*shutdown) (struct device *dev);
134 int (*suspend) (struct device *dev, pm_message_t state);
135 int (*resume) (struct device *dev);
136 struct attribute_group **groups;
140 struct driver_private *p;
144 extern int __must_check driver_register(struct device_driver *drv);
145 extern void driver_unregister(struct device_driver *drv);
147 extern struct device_driver *get_driver(struct device_driver *drv);
148 extern void put_driver(struct device_driver *drv);
149 extern struct device_driver *driver_find(const char *name,
150 struct bus_type *bus);
151 extern int driver_probe_done(void);
153 /* sysfs interface for exporting driver attributes */
155 struct driver_attribute {
156 struct attribute attr;
157 ssize_t (*show)(struct device_driver *driver, char *buf);
158 ssize_t (*store)(struct device_driver *driver, const char *buf,
162 #define DRIVER_ATTR(_name, _mode, _show, _store) \
163 struct driver_attribute driver_attr_##_name = \
164 __ATTR(_name, _mode, _show, _store)
166 extern int __must_check driver_create_file(struct device_driver *driver,
167 struct driver_attribute *attr);
168 extern void driver_remove_file(struct device_driver *driver,
169 struct driver_attribute *attr);
171 extern int __must_check driver_add_kobj(struct device_driver *drv,
172 struct kobject *kobj,
173 const char *fmt, ...);
175 extern int __must_check driver_for_each_device(struct device_driver *drv,
176 struct device *start,
178 int (*fn)(struct device *dev,
180 struct device *driver_find_device(struct device_driver *drv,
181 struct device *start, void *data,
182 int (*match)(struct device *dev, void *data));
189 struct module *owner;
191 struct class_attribute *class_attrs;
192 struct device_attribute *dev_attrs;
193 struct kobject *dev_kobj;
195 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
197 void (*class_release)(struct class *class);
198 void (*dev_release)(struct device *dev);
200 int (*suspend)(struct device *dev, pm_message_t state);
201 int (*resume)(struct device *dev);
204 struct class_private *p;
207 extern struct kobject *sysfs_dev_block_kobj;
208 extern struct kobject *sysfs_dev_char_kobj;
209 extern int __must_check __class_register(struct class *class,
210 struct lock_class_key *key);
211 extern void class_unregister(struct class *class);
213 /* This is a #define to keep the compiler from merging different
214 * instances of the __key variable */
215 #define class_register(class) \
217 static struct lock_class_key __key; \
218 __class_register(class, &__key); \
221 extern int class_for_each_device(struct class *class, struct device *start,
223 int (*fn)(struct device *dev, void *data));
224 extern struct device *class_find_device(struct class *class,
225 struct device *start, void *data,
226 int (*match)(struct device *, void *));
228 struct class_attribute {
229 struct attribute attr;
230 ssize_t (*show)(struct class *class, char *buf);
231 ssize_t (*store)(struct class *class, const char *buf, size_t count);
234 #define CLASS_ATTR(_name, _mode, _show, _store) \
235 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
237 extern int __must_check class_create_file(struct class *class,
238 const struct class_attribute *attr);
239 extern void class_remove_file(struct class *class,
240 const struct class_attribute *attr);
242 struct class_interface {
243 struct list_head node;
246 int (*add_dev) (struct device *, struct class_interface *);
247 void (*remove_dev) (struct device *, struct class_interface *);
250 extern int __must_check class_interface_register(struct class_interface *);
251 extern void class_interface_unregister(struct class_interface *);
253 extern struct class * __must_check __class_create(struct module *owner,
255 struct lock_class_key *key);
256 extern void class_destroy(struct class *cls);
258 /* This is a #define to keep the compiler from merging different
259 * instances of the __key variable */
260 #define class_create(owner, name) \
262 static struct lock_class_key __key; \
263 __class_create(owner, name, &__key); \
267 * The type of device, "struct device" is embedded in. A class
268 * or bus can contain devices of different types
269 * like "partitions" and "disks", "mouse" and "event".
270 * This identifies the device type and carries type-specific
271 * information, equivalent to the kobj_type of a kobject.
272 * If "name" is specified, the uevent will contain it in
273 * the DEVTYPE variable.
277 struct attribute_group **groups;
278 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
279 void (*release)(struct device *dev);
281 int (*suspend)(struct device *dev, pm_message_t state);
282 int (*resume)(struct device *dev);
287 /* interface for exporting device attributes */
288 struct device_attribute {
289 struct attribute attr;
290 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
292 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count);
296 #define DEVICE_ATTR(_name, _mode, _show, _store) \
297 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
299 extern int __must_check device_create_file(struct device *device,
300 struct device_attribute *entry);
301 extern void device_remove_file(struct device *dev,
302 struct device_attribute *attr);
303 extern int __must_check device_create_bin_file(struct device *dev,
304 struct bin_attribute *attr);
305 extern void device_remove_bin_file(struct device *dev,
306 struct bin_attribute *attr);
307 extern int device_schedule_callback_owner(struct device *dev,
308 void (*func)(struct device *dev), struct module *owner);
310 /* This is a macro to avoid include problems with THIS_MODULE */
311 #define device_schedule_callback(dev, func) \
312 device_schedule_callback_owner(dev, func, THIS_MODULE)
314 /* device resource management */
315 typedef void (*dr_release_t)(struct device *dev, void *res);
316 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
318 #ifdef CONFIG_DEBUG_DEVRES
319 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
321 #define devres_alloc(release, size, gfp) \
322 __devres_alloc(release, size, gfp, #release)
324 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
326 extern void devres_free(void *res);
327 extern void devres_add(struct device *dev, void *res);
328 extern void *devres_find(struct device *dev, dr_release_t release,
329 dr_match_t match, void *match_data);
330 extern void *devres_get(struct device *dev, void *new_res,
331 dr_match_t match, void *match_data);
332 extern void *devres_remove(struct device *dev, dr_release_t release,
333 dr_match_t match, void *match_data);
334 extern int devres_destroy(struct device *dev, dr_release_t release,
335 dr_match_t match, void *match_data);
338 extern void * __must_check devres_open_group(struct device *dev, void *id,
340 extern void devres_close_group(struct device *dev, void *id);
341 extern void devres_remove_group(struct device *dev, void *id);
342 extern int devres_release_group(struct device *dev, void *id);
344 /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
345 extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
346 extern void devm_kfree(struct device *dev, void *p);
348 struct device_dma_parameters {
350 * a low level driver may set these to teach IOMMU code about
353 unsigned int max_segment_size;
354 unsigned long segment_boundary_mask;
358 struct klist klist_children;
359 struct klist_node knode_parent; /* node in sibling list */
360 struct klist_node knode_driver;
361 struct klist_node knode_bus;
362 struct device *parent;
365 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
366 struct device_type *type;
367 unsigned uevent_suppress:1;
369 struct semaphore sem; /* semaphore to synchronize calls to
373 struct bus_type *bus; /* type of bus device is on */
374 struct device_driver *driver; /* which driver has allocated this
376 void *driver_data; /* data private to the driver */
377 void *platform_data; /* Platform specific data, device
378 core doesn't touch it */
379 struct dev_pm_info power;
382 int numa_node; /* NUMA node this device is close to */
384 u64 *dma_mask; /* dma mask (if dma'able device) */
385 u64 coherent_dma_mask;/* Like dma_mask, but for
386 alloc_coherent mappings as
387 not all hardware supports
388 64 bit addresses for consistent
389 allocations such descriptors. */
391 struct device_dma_parameters *dma_parms;
393 struct list_head dma_pools; /* dma pools (if dma'ble) */
395 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
397 /* arch specific additions */
398 struct dev_archdata archdata;
400 spinlock_t devres_lock;
401 struct list_head devres_head;
403 struct list_head node;
405 dev_t devt; /* dev_t, creates the sysfs "dev" */
406 struct attribute_group **groups; /* optional groups */
408 void (*release)(struct device *dev);
411 /* Get the wakeup routines, which depend on struct device */
412 #include <linux/pm_wakeup.h>
414 static inline const char *dev_name(struct device *dev)
416 /* will be changed into kobject_name(&dev->kobj) in the near future */
420 extern int dev_set_name(struct device *dev, const char *name, ...)
421 __attribute__((format(printf, 2, 3)));
424 static inline int dev_to_node(struct device *dev)
426 return dev->numa_node;
428 static inline void set_dev_node(struct device *dev, int node)
430 dev->numa_node = node;
433 static inline int dev_to_node(struct device *dev)
437 static inline void set_dev_node(struct device *dev, int node)
442 static inline void *dev_get_drvdata(struct device *dev)
444 return dev->driver_data;
447 static inline void dev_set_drvdata(struct device *dev, void *data)
449 dev->driver_data = data;
452 static inline int device_is_registered(struct device *dev)
454 return dev->kobj.state_in_sysfs;
457 void driver_init(void);
460 * High level routines for use by the bus drivers
462 extern int __must_check device_register(struct device *dev);
463 extern void device_unregister(struct device *dev);
464 extern void device_initialize(struct device *dev);
465 extern int __must_check device_add(struct device *dev);
466 extern void device_del(struct device *dev);
467 extern int device_for_each_child(struct device *dev, void *data,
468 int (*fn)(struct device *dev, void *data));
469 extern struct device *device_find_child(struct device *dev, void *data,
470 int (*match)(struct device *dev, void *data));
471 extern int device_rename(struct device *dev, char *new_name);
472 extern int device_move(struct device *dev, struct device *new_parent);
475 * Manual binding of a device to driver. See drivers/base/bus.c
476 * for information on use.
478 extern int __must_check device_bind_driver(struct device *dev);
479 extern void device_release_driver(struct device *dev);
480 extern int __must_check device_attach(struct device *dev);
481 extern int __must_check driver_attach(struct device_driver *drv);
482 extern int __must_check device_reprobe(struct device *dev);
485 * Easy functions for dynamically creating devices on the fly
487 extern struct device *device_create_vargs(struct class *cls,
488 struct device *parent,
493 extern struct device *device_create(struct class *cls, struct device *parent,
494 dev_t devt, void *drvdata,
495 const char *fmt, ...)
496 __attribute__((format(printf, 5, 6)));
497 #define device_create_drvdata device_create
498 extern void device_destroy(struct class *cls, dev_t devt);
501 * Platform "fixup" functions - allow the platform to have their say
502 * about devices and actions that the general device layer doesn't
505 /* Notify platform of device discovery */
506 extern int (*platform_notify)(struct device *dev);
508 extern int (*platform_notify_remove)(struct device *dev);
512 * get_device - atomically increment the reference count for the device.
515 extern struct device *get_device(struct device *dev);
516 extern void put_device(struct device *dev);
519 /* drivers/base/power/shutdown.c */
520 extern void device_shutdown(void);
522 /* drivers/base/sys.c */
523 extern void sysdev_shutdown(void);
525 /* debugging and troubleshooting/diagnostic helpers. */
526 extern const char *dev_driver_string(struct device *dev);
527 #define dev_printk(level, dev, format, arg...) \
528 printk(level "%s %s: " format , dev_driver_string(dev) , \
529 dev_name(dev) , ## arg)
531 #define dev_emerg(dev, format, arg...) \
532 dev_printk(KERN_EMERG , dev , format , ## arg)
533 #define dev_alert(dev, format, arg...) \
534 dev_printk(KERN_ALERT , dev , format , ## arg)
535 #define dev_crit(dev, format, arg...) \
536 dev_printk(KERN_CRIT , dev , format , ## arg)
537 #define dev_err(dev, format, arg...) \
538 dev_printk(KERN_ERR , dev , format , ## arg)
539 #define dev_warn(dev, format, arg...) \
540 dev_printk(KERN_WARNING , dev , format , ## arg)
541 #define dev_notice(dev, format, arg...) \
542 dev_printk(KERN_NOTICE , dev , format , ## arg)
543 #define dev_info(dev, format, arg...) \
544 dev_printk(KERN_INFO , dev , format , ## arg)
547 #define dev_dbg(dev, format, arg...) \
548 dev_printk(KERN_DEBUG , dev , format , ## arg)
550 #define dev_dbg(dev, format, arg...) \
551 ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
555 #define dev_vdbg dev_dbg
558 #define dev_vdbg(dev, format, arg...) \
559 ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
562 /* Create alias, so I can be autoloaded. */
563 #define MODULE_ALIAS_CHARDEV(major,minor) \
564 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
565 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
566 MODULE_ALIAS("char-major-" __stringify(major) "-*")
567 #endif /* _DEVICE_H_ */