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_ID_SIZE 32
29 #define BUS_ID_SIZE 20
33 struct driver_private;
37 struct bus_type_private;
39 struct bus_attribute {
40 struct attribute attr;
41 ssize_t (*show)(struct bus_type *bus, char *buf);
42 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
45 #define BUS_ATTR(_name, _mode, _show, _store) \
46 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
48 extern int __must_check bus_create_file(struct bus_type *,
49 struct bus_attribute *);
50 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
54 struct bus_attribute *bus_attrs;
55 struct device_attribute *dev_attrs;
56 struct driver_attribute *drv_attrs;
58 int (*match)(struct device *dev, struct device_driver *drv);
59 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
60 int (*probe)(struct device *dev);
61 int (*remove)(struct device *dev);
62 void (*shutdown)(struct device *dev);
64 int (*suspend)(struct device *dev, pm_message_t state);
65 int (*suspend_late)(struct device *dev, pm_message_t state);
66 int (*resume_early)(struct device *dev);
67 int (*resume)(struct device *dev);
69 struct pm_ext_ops *pm;
71 struct bus_type_private *p;
74 extern int __must_check bus_register(struct bus_type *bus);
75 extern void bus_unregister(struct bus_type *bus);
77 extern int __must_check bus_rescan_devices(struct bus_type *bus);
79 /* iterator helpers for buses */
81 int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data,
82 int (*fn)(struct device *dev, void *data));
83 struct device *bus_find_device(struct bus_type *bus, struct device *start,
85 int (*match)(struct device *dev, void *data));
86 struct device *bus_find_device_by_name(struct bus_type *bus,
90 int __must_check bus_for_each_drv(struct bus_type *bus,
91 struct device_driver *start, void *data,
92 int (*fn)(struct device_driver *, void *));
95 * Bus notifiers: Get notified of addition/removal of devices
96 * and binding/unbinding of drivers to devices.
97 * In the long run, it should be a replacement for the platform
100 struct notifier_block;
102 extern int bus_register_notifier(struct bus_type *bus,
103 struct notifier_block *nb);
104 extern int bus_unregister_notifier(struct bus_type *bus,
105 struct notifier_block *nb);
107 /* All 4 notifers below get called with the target struct device *
108 * as an argument. Note that those functions are likely to be called
109 * with the device semaphore held in the core, so be careful.
111 #define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
112 #define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
113 #define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
114 #define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
117 extern struct kset *bus_get_kset(struct bus_type *bus);
118 extern struct klist *bus_get_device_klist(struct bus_type *bus);
120 struct device_driver {
122 struct bus_type *bus;
124 struct module *owner;
125 const char *mod_name; /* used for built-in modules */
127 int (*probe) (struct device *dev);
128 int (*remove) (struct device *dev);
129 void (*shutdown) (struct device *dev);
130 int (*suspend) (struct device *dev, pm_message_t state);
131 int (*resume) (struct device *dev);
132 struct attribute_group **groups;
136 struct driver_private *p;
140 extern int __must_check driver_register(struct device_driver *drv);
141 extern void driver_unregister(struct device_driver *drv);
143 extern struct device_driver *get_driver(struct device_driver *drv);
144 extern void put_driver(struct device_driver *drv);
145 extern struct device_driver *driver_find(const char *name,
146 struct bus_type *bus);
147 extern int driver_probe_done(void);
149 /* sysfs interface for exporting driver attributes */
151 struct driver_attribute {
152 struct attribute attr;
153 ssize_t (*show)(struct device_driver *driver, char *buf);
154 ssize_t (*store)(struct device_driver *driver, const char *buf,
158 #define DRIVER_ATTR(_name, _mode, _show, _store) \
159 struct driver_attribute driver_attr_##_name = \
160 __ATTR(_name, _mode, _show, _store)
162 extern int __must_check driver_create_file(struct device_driver *driver,
163 struct driver_attribute *attr);
164 extern void driver_remove_file(struct device_driver *driver,
165 struct driver_attribute *attr);
167 extern int __must_check driver_add_kobj(struct device_driver *drv,
168 struct kobject *kobj,
169 const char *fmt, ...);
171 extern int __must_check driver_for_each_device(struct device_driver *drv,
172 struct device *start,
174 int (*fn)(struct device *dev,
176 struct device *driver_find_device(struct device_driver *drv,
177 struct device *start, void *data,
178 int (*match)(struct device *dev, void *data));
185 struct module *owner;
187 struct class_attribute *class_attrs;
188 struct device_attribute *dev_attrs;
189 struct kobject *dev_kobj;
191 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
193 void (*class_release)(struct class *class);
194 void (*dev_release)(struct device *dev);
196 int (*suspend)(struct device *dev, pm_message_t state);
197 int (*resume)(struct device *dev);
200 struct class_private *p;
203 extern struct kobject *sysfs_dev_block_kobj;
204 extern struct kobject *sysfs_dev_char_kobj;
205 extern int __must_check __class_register(struct class *class,
206 struct lock_class_key *key);
207 extern void class_unregister(struct class *class);
209 /* This is a #define to keep the compiler from merging different
210 * instances of the __key variable */
211 #define class_register(class) \
213 static struct lock_class_key __key; \
214 __class_register(class, &__key); \
217 extern int class_for_each_device(struct class *class, struct device *start,
219 int (*fn)(struct device *dev, void *data));
220 extern struct device *class_find_device(struct class *class,
221 struct device *start, void *data,
222 int (*match)(struct device *, void *));
224 struct class_attribute {
225 struct attribute attr;
226 ssize_t (*show)(struct class *class, char *buf);
227 ssize_t (*store)(struct class *class, const char *buf, size_t count);
230 #define CLASS_ATTR(_name, _mode, _show, _store) \
231 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
233 extern int __must_check class_create_file(struct class *class,
234 const struct class_attribute *attr);
235 extern void class_remove_file(struct class *class,
236 const struct class_attribute *attr);
238 struct class_interface {
239 struct list_head node;
242 int (*add_dev) (struct device *, struct class_interface *);
243 void (*remove_dev) (struct device *, struct class_interface *);
246 extern int __must_check class_interface_register(struct class_interface *);
247 extern void class_interface_unregister(struct class_interface *);
249 extern struct class * __must_check __class_create(struct module *owner,
251 struct lock_class_key *key);
252 extern void class_destroy(struct class *cls);
254 /* This is a #define to keep the compiler from merging different
255 * instances of the __key variable */
256 #define class_create(owner, name) \
258 static struct lock_class_key __key; \
259 __class_create(owner, name, &__key); \
263 * The type of device, "struct device" is embedded in. A class
264 * or bus can contain devices of different types
265 * like "partitions" and "disks", "mouse" and "event".
266 * This identifies the device type and carries type-specific
267 * information, equivalent to the kobj_type of a kobject.
268 * If "name" is specified, the uevent will contain it in
269 * the DEVTYPE variable.
273 struct attribute_group **groups;
274 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
275 void (*release)(struct device *dev);
277 int (*suspend)(struct device *dev, pm_message_t state);
278 int (*resume)(struct device *dev);
283 /* interface for exporting device attributes */
284 struct device_attribute {
285 struct attribute attr;
286 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
288 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
289 const char *buf, size_t count);
292 #define DEVICE_ATTR(_name, _mode, _show, _store) \
293 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
295 extern int __must_check device_create_file(struct device *device,
296 struct device_attribute *entry);
297 extern void device_remove_file(struct device *dev,
298 struct device_attribute *attr);
299 extern int __must_check device_create_bin_file(struct device *dev,
300 struct bin_attribute *attr);
301 extern void device_remove_bin_file(struct device *dev,
302 struct bin_attribute *attr);
303 extern int device_schedule_callback_owner(struct device *dev,
304 void (*func)(struct device *dev), struct module *owner);
306 /* This is a macro to avoid include problems with THIS_MODULE */
307 #define device_schedule_callback(dev, func) \
308 device_schedule_callback_owner(dev, func, THIS_MODULE)
310 /* device resource management */
311 typedef void (*dr_release_t)(struct device *dev, void *res);
312 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
314 #ifdef CONFIG_DEBUG_DEVRES
315 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
317 #define devres_alloc(release, size, gfp) \
318 __devres_alloc(release, size, gfp, #release)
320 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
322 extern void devres_free(void *res);
323 extern void devres_add(struct device *dev, void *res);
324 extern void *devres_find(struct device *dev, dr_release_t release,
325 dr_match_t match, void *match_data);
326 extern void *devres_get(struct device *dev, void *new_res,
327 dr_match_t match, void *match_data);
328 extern void *devres_remove(struct device *dev, dr_release_t release,
329 dr_match_t match, void *match_data);
330 extern int devres_destroy(struct device *dev, dr_release_t release,
331 dr_match_t match, void *match_data);
334 extern void * __must_check devres_open_group(struct device *dev, void *id,
336 extern void devres_close_group(struct device *dev, void *id);
337 extern void devres_remove_group(struct device *dev, void *id);
338 extern int devres_release_group(struct device *dev, void *id);
340 /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
341 extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
342 extern void devm_kfree(struct device *dev, void *p);
344 struct device_dma_parameters {
346 * a low level driver may set these to teach IOMMU code about
349 unsigned int max_segment_size;
350 unsigned long segment_boundary_mask;
354 struct klist klist_children;
355 struct klist_node knode_parent; /* node in sibling list */
356 struct klist_node knode_driver;
357 struct klist_node knode_bus;
358 struct device *parent;
361 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
362 struct device_type *type;
363 unsigned uevent_suppress:1;
365 struct semaphore sem; /* semaphore to synchronize calls to
369 struct bus_type *bus; /* type of bus device is on */
370 struct device_driver *driver; /* which driver has allocated this
372 void *driver_data; /* data private to the driver */
373 void *platform_data; /* Platform specific data, device
374 core doesn't touch it */
375 struct dev_pm_info power;
378 int numa_node; /* NUMA node this device is close to */
380 u64 *dma_mask; /* dma mask (if dma'able device) */
381 u64 coherent_dma_mask;/* Like dma_mask, but for
382 alloc_coherent mappings as
383 not all hardware supports
384 64 bit addresses for consistent
385 allocations such descriptors. */
387 struct device_dma_parameters *dma_parms;
389 struct list_head dma_pools; /* dma pools (if dma'ble) */
391 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
393 /* arch specific additions */
394 struct dev_archdata archdata;
396 spinlock_t devres_lock;
397 struct list_head devres_head;
399 struct list_head node;
401 dev_t devt; /* dev_t, creates the sysfs "dev" */
402 struct attribute_group **groups; /* optional groups */
404 void (*release)(struct device *dev);
407 /* Get the wakeup routines, which depend on struct device */
408 #include <linux/pm_wakeup.h>
410 static inline const char *dev_name(struct device *dev)
412 /* will be changed into kobject_name(&dev->kobj) in the near future */
416 extern int dev_set_name(struct device *dev, const char *name, ...)
417 __attribute__((format(printf, 2, 3)));
420 static inline int dev_to_node(struct device *dev)
422 return dev->numa_node;
424 static inline void set_dev_node(struct device *dev, int node)
426 dev->numa_node = node;
429 static inline int dev_to_node(struct device *dev)
433 static inline void set_dev_node(struct device *dev, int node)
438 static inline void *dev_get_drvdata(struct device *dev)
440 return dev->driver_data;
443 static inline void dev_set_drvdata(struct device *dev, void *data)
445 dev->driver_data = data;
448 static inline int device_is_registered(struct device *dev)
450 return dev->kobj.state_in_sysfs;
453 void driver_init(void);
456 * High level routines for use by the bus drivers
458 extern int __must_check device_register(struct device *dev);
459 extern void device_unregister(struct device *dev);
460 extern void device_initialize(struct device *dev);
461 extern int __must_check device_add(struct device *dev);
462 extern void device_del(struct device *dev);
463 extern int device_for_each_child(struct device *dev, void *data,
464 int (*fn)(struct device *dev, void *data));
465 extern struct device *device_find_child(struct device *dev, void *data,
466 int (*match)(struct device *dev, void *data));
467 extern int device_rename(struct device *dev, char *new_name);
468 extern int device_move(struct device *dev, struct device *new_parent);
471 * Manual binding of a device to driver. See drivers/base/bus.c
472 * for information on use.
474 extern int __must_check device_bind_driver(struct device *dev);
475 extern void device_release_driver(struct device *dev);
476 extern int __must_check device_attach(struct device *dev);
477 extern int __must_check driver_attach(struct device_driver *drv);
478 extern int __must_check device_reprobe(struct device *dev);
481 * Easy functions for dynamically creating devices on the fly
483 extern struct device *device_create_vargs(struct class *cls,
484 struct device *parent,
489 extern struct device *device_create(struct class *cls, struct device *parent,
490 dev_t devt, void *drvdata,
491 const char *fmt, ...)
492 __attribute__((format(printf, 5, 6)));
493 #define device_create_drvdata device_create
494 extern void device_destroy(struct class *cls, dev_t devt);
497 * Platform "fixup" functions - allow the platform to have their say
498 * about devices and actions that the general device layer doesn't
501 /* Notify platform of device discovery */
502 extern int (*platform_notify)(struct device *dev);
504 extern int (*platform_notify_remove)(struct device *dev);
508 * get_device - atomically increment the reference count for the device.
511 extern struct device *get_device(struct device *dev);
512 extern void put_device(struct device *dev);
515 /* drivers/base/power/shutdown.c */
516 extern void device_shutdown(void);
518 /* drivers/base/sys.c */
519 extern void sysdev_shutdown(void);
521 /* debugging and troubleshooting/diagnostic helpers. */
522 extern const char *dev_driver_string(struct device *dev);
523 #define dev_printk(level, dev, format, arg...) \
524 printk(level "%s %s: " format , dev_driver_string(dev) , \
525 dev_name(dev) , ## arg)
527 #define dev_emerg(dev, format, arg...) \
528 dev_printk(KERN_EMERG , dev , format , ## arg)
529 #define dev_alert(dev, format, arg...) \
530 dev_printk(KERN_ALERT , dev , format , ## arg)
531 #define dev_crit(dev, format, arg...) \
532 dev_printk(KERN_CRIT , dev , format , ## arg)
533 #define dev_err(dev, format, arg...) \
534 dev_printk(KERN_ERR , dev , format , ## arg)
535 #define dev_warn(dev, format, arg...) \
536 dev_printk(KERN_WARNING , dev , format , ## arg)
537 #define dev_notice(dev, format, arg...) \
538 dev_printk(KERN_NOTICE , dev , format , ## arg)
539 #define dev_info(dev, format, arg...) \
540 dev_printk(KERN_INFO , dev , format , ## arg)
543 #define dev_dbg(dev, format, arg...) \
544 dev_printk(KERN_DEBUG , dev , format , ## arg)
546 #define dev_dbg(dev, format, arg...) \
547 ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
551 #define dev_vdbg dev_dbg
554 #define dev_vdbg(dev, format, arg...) \
555 ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
558 /* Create alias, so I can be autoloaded. */
559 #define MODULE_ALIAS_CHARDEV(major,minor) \
560 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
561 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
562 MODULE_ALIAS("char-major-" __stringify(major) "-*")
563 #endif /* _DEVICE_H_ */