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 <asm/semaphore.h>
24 #include <asm/atomic.h>
25 #include <asm/device.h>
27 #define DEVICE_NAME_SIZE 50
28 #define DEVICE_NAME_HALF __stringify(20) /* Less than half to accommodate slop */
29 #define DEVICE_ID_SIZE 32
30 #define BUS_ID_SIZE KOBJ_NAME_LEN
39 struct bus_attribute {
40 struct attribute attr;
41 ssize_t (*show)(struct bus_type *, char * buf);
42 ssize_t (*store)(struct bus_type *, 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 *);
56 struct kset *drivers_kset;
57 struct kset *devices_kset;
58 struct klist klist_devices;
59 struct klist klist_drivers;
61 struct blocking_notifier_head bus_notifier;
63 struct bus_attribute * bus_attrs;
64 struct device_attribute * dev_attrs;
65 struct driver_attribute * drv_attrs;
67 int (*match)(struct device * dev, struct device_driver * drv);
68 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
69 int (*probe)(struct device * dev);
70 int (*remove)(struct device * dev);
71 void (*shutdown)(struct device * dev);
73 int (*suspend)(struct device * dev, pm_message_t state);
74 int (*suspend_late)(struct device * dev, pm_message_t state);
75 int (*resume_early)(struct device * dev);
76 int (*resume)(struct device * dev);
78 unsigned int drivers_autoprobe:1;
81 extern int __must_check bus_register(struct bus_type * bus);
82 extern void bus_unregister(struct bus_type * bus);
84 extern int __must_check bus_rescan_devices(struct bus_type * bus);
86 /* iterator helpers for buses */
88 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
89 int (*fn)(struct device *, void *));
90 struct device * bus_find_device(struct bus_type *bus, struct device *start,
91 void *data, int (*match)(struct device *, void *));
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 struct device_driver {
122 struct bus_type * bus;
125 struct klist klist_devices;
126 struct klist_node knode_bus;
128 struct module * owner;
129 const char * mod_name; /* used for built-in modules */
130 struct module_kobject * mkobj;
132 int (*probe) (struct device * dev);
133 int (*remove) (struct device * dev);
134 void (*shutdown) (struct device * dev);
135 int (*suspend) (struct device * dev, pm_message_t state);
136 int (*resume) (struct device * dev);
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, struct bus_type *bus);
146 extern int driver_probe_done(void);
148 /* sysfs interface for exporting driver attributes */
150 struct driver_attribute {
151 struct attribute attr;
152 ssize_t (*show)(struct device_driver *, char * buf);
153 ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
156 #define DRIVER_ATTR(_name,_mode,_show,_store) \
157 struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
159 extern int __must_check driver_create_file(struct device_driver *,
160 struct driver_attribute *);
161 extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
163 extern int __must_check driver_for_each_device(struct device_driver * drv,
164 struct device *start, void *data,
165 int (*fn)(struct device *, void *));
166 struct device * driver_find_device(struct device_driver *drv,
167 struct device *start, void *data,
168 int (*match)(struct device *, void *));
175 struct module * owner;
178 struct list_head children;
179 struct list_head devices;
180 struct list_head interfaces;
181 struct kset class_dirs;
182 struct semaphore sem; /* locks both the children and interfaces lists */
184 struct class_attribute * class_attrs;
185 struct class_device_attribute * class_dev_attrs;
186 struct device_attribute * dev_attrs;
188 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
189 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
191 void (*release)(struct class_device *dev);
192 void (*class_release)(struct class *class);
193 void (*dev_release)(struct device *dev);
195 int (*suspend)(struct device *, pm_message_t state);
196 int (*resume)(struct device *);
199 extern int __must_check class_register(struct class *);
200 extern void class_unregister(struct class *);
203 struct class_attribute {
204 struct attribute attr;
205 ssize_t (*show)(struct class *, char * buf);
206 ssize_t (*store)(struct class *, const char * buf, size_t count);
209 #define CLASS_ATTR(_name,_mode,_show,_store) \
210 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
212 extern int __must_check class_create_file(struct class *,
213 const struct class_attribute *);
214 extern void class_remove_file(struct class *, const struct class_attribute *);
216 struct class_device_attribute {
217 struct attribute attr;
218 ssize_t (*show)(struct class_device *, char * buf);
219 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
222 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
223 struct class_device_attribute class_device_attr_##_name = \
224 __ATTR(_name,_mode,_show,_store)
226 extern int __must_check class_device_create_file(struct class_device *,
227 const struct class_device_attribute *);
230 * struct class_device - class devices
231 * @class: pointer to the parent class for this class device. This is required.
232 * @devt: for internal use by the driver core only.
233 * @node: for internal use by the driver core only.
234 * @kobj: for internal use by the driver core only.
235 * @groups: optional additional groups to be created
236 * @dev: if set, a symlink to the struct device is created in the sysfs
237 * directory for this struct class device.
238 * @class_data: pointer to whatever you want to store here for this struct
239 * class_device. Use class_get_devdata() and class_set_devdata() to get and
241 * @parent: pointer to a struct class_device that is the parent of this struct
242 * class_device. If NULL, this class_device will show up at the root of the
243 * struct class in sysfs (which is probably what you want to have happen.)
244 * @release: pointer to a release function for this struct class_device. If
245 * set, this will be called instead of the class specific release function.
246 * Only use this if you want to override the default release function, like
247 * when you are nesting class_device structures.
248 * @uevent: pointer to a uevent function for this struct class_device. If
249 * set, this will be called instead of the class specific uevent function.
250 * Only use this if you want to override the default uevent function, like
251 * when you are nesting class_device structures.
253 struct class_device {
254 struct list_head node;
257 struct class * class; /* required */
258 dev_t devt; /* dev_t, creates the sysfs "dev" */
259 struct device * dev; /* not necessary, but nice to have */
260 void * class_data; /* class-specific data */
261 struct class_device *parent; /* parent of this child device, if there is one */
262 struct attribute_group ** groups; /* optional groups */
264 void (*release)(struct class_device *dev);
265 int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
266 char class_id[BUS_ID_SIZE]; /* unique to this class */
270 class_get_devdata (struct class_device *dev)
272 return dev->class_data;
276 class_set_devdata (struct class_device *dev, void *data)
278 dev->class_data = data;
282 extern int __must_check class_device_register(struct class_device *);
283 extern void class_device_unregister(struct class_device *);
284 extern void class_device_initialize(struct class_device *);
285 extern int __must_check class_device_add(struct class_device *);
286 extern void class_device_del(struct class_device *);
288 extern struct class_device * class_device_get(struct class_device *);
289 extern void class_device_put(struct class_device *);
291 extern void class_device_remove_file(struct class_device *,
292 const struct class_device_attribute *);
293 extern int __must_check class_device_create_bin_file(struct class_device *,
294 struct bin_attribute *);
295 extern void class_device_remove_bin_file(struct class_device *,
296 struct bin_attribute *);
298 struct class_interface {
299 struct list_head node;
302 int (*add) (struct class_device *, struct class_interface *);
303 void (*remove) (struct class_device *, struct class_interface *);
304 int (*add_dev) (struct device *, struct class_interface *);
305 void (*remove_dev) (struct device *, struct class_interface *);
308 extern int __must_check class_interface_register(struct class_interface *);
309 extern void class_interface_unregister(struct class_interface *);
311 extern struct class *class_create(struct module *owner, const char *name);
312 extern void class_destroy(struct class *cls);
313 extern struct class_device *class_device_create(struct class *cls,
314 struct class_device *parent,
316 struct device *device,
317 const char *fmt, ...)
318 __attribute__((format(printf,5,6)));
319 extern void class_device_destroy(struct class *cls, dev_t devt);
322 * The type of device, "struct device" is embedded in. A class
323 * or bus can contain devices of different types
324 * like "partitions" and "disks", "mouse" and "event".
325 * This identifies the device type and carries type-specific
326 * information, equivalent to the kobj_type of a kobject.
327 * If "name" is specified, the uevent will contain it in
328 * the DEVTYPE variable.
332 struct attribute_group **groups;
333 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
334 void (*release)(struct device *dev);
335 int (*suspend)(struct device * dev, pm_message_t state);
336 int (*resume)(struct device * dev);
339 /* interface for exporting device attributes */
340 struct device_attribute {
341 struct attribute attr;
342 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
344 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
345 const char *buf, size_t count);
348 #define DEVICE_ATTR(_name,_mode,_show,_store) \
349 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
351 extern int __must_check device_create_file(struct device *device,
352 struct device_attribute * entry);
353 extern void device_remove_file(struct device * dev, struct device_attribute * attr);
354 extern int __must_check device_create_bin_file(struct device *dev,
355 struct bin_attribute *attr);
356 extern void device_remove_bin_file(struct device *dev,
357 struct bin_attribute *attr);
358 extern int device_schedule_callback_owner(struct device *dev,
359 void (*func)(struct device *), struct module *owner);
361 /* This is a macro to avoid include problems with THIS_MODULE */
362 #define device_schedule_callback(dev, func) \
363 device_schedule_callback_owner(dev, func, THIS_MODULE)
365 /* device resource management */
366 typedef void (*dr_release_t)(struct device *dev, void *res);
367 typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
369 #ifdef CONFIG_DEBUG_DEVRES
370 extern void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
372 #define devres_alloc(release, size, gfp) \
373 __devres_alloc(release, size, gfp, #release)
375 extern void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
377 extern void devres_free(void *res);
378 extern void devres_add(struct device *dev, void *res);
379 extern void * devres_find(struct device *dev, dr_release_t release,
380 dr_match_t match, void *match_data);
381 extern void * devres_get(struct device *dev, void *new_res,
382 dr_match_t match, void *match_data);
383 extern void * devres_remove(struct device *dev, dr_release_t release,
384 dr_match_t match, void *match_data);
385 extern int devres_destroy(struct device *dev, dr_release_t release,
386 dr_match_t match, void *match_data);
389 extern void * __must_check devres_open_group(struct device *dev, void *id,
391 extern void devres_close_group(struct device *dev, void *id);
392 extern void devres_remove_group(struct device *dev, void *id);
393 extern int devres_release_group(struct device *dev, void *id);
395 /* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
396 extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
397 extern void devm_kfree(struct device *dev, void *p);
400 struct klist klist_children;
401 struct klist_node knode_parent; /* node in sibling list */
402 struct klist_node knode_driver;
403 struct klist_node knode_bus;
404 struct device *parent;
407 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
408 struct device_type *type;
409 unsigned is_registered:1;
410 unsigned uevent_suppress:1;
412 struct semaphore sem; /* semaphore to synchronize calls to
416 struct bus_type * bus; /* type of bus device is on */
417 struct device_driver *driver; /* which driver has allocated this
419 void *driver_data; /* data private to the driver */
420 void *platform_data; /* Platform specific data, device
421 core doesn't touch it */
422 struct dev_pm_info power;
425 int numa_node; /* NUMA node this device is close to */
427 u64 *dma_mask; /* dma mask (if dma'able device) */
428 u64 coherent_dma_mask;/* Like dma_mask, but for
429 alloc_coherent mappings as
430 not all hardware supports
431 64 bit addresses for consistent
432 allocations such descriptors. */
434 struct list_head dma_pools; /* dma pools (if dma'ble) */
436 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
438 /* arch specific additions */
439 struct dev_archdata archdata;
441 spinlock_t devres_lock;
442 struct list_head devres_head;
444 /* class_device migration path */
445 struct list_head node;
447 dev_t devt; /* dev_t, creates the sysfs "dev" */
448 struct attribute_group **groups; /* optional groups */
450 void (*release)(struct device * dev);
454 static inline int dev_to_node(struct device *dev)
456 return dev->numa_node;
458 static inline void set_dev_node(struct device *dev, int node)
460 dev->numa_node = node;
463 static inline int dev_to_node(struct device *dev)
467 static inline void set_dev_node(struct device *dev, int node)
473 dev_get_drvdata (struct device *dev)
475 return dev->driver_data;
479 dev_set_drvdata (struct device *dev, void *data)
481 dev->driver_data = data;
484 static inline int device_is_registered(struct device *dev)
486 return dev->is_registered;
489 void driver_init(void);
492 * High level routines for use by the bus drivers
494 extern int __must_check device_register(struct device * dev);
495 extern void device_unregister(struct device * dev);
496 extern void device_initialize(struct device * dev);
497 extern int __must_check device_add(struct device * dev);
498 extern void device_del(struct device * dev);
499 extern int device_for_each_child(struct device *, void *,
500 int (*fn)(struct device *, void *));
501 extern struct device *device_find_child(struct device *, void *data,
502 int (*match)(struct device *, void *));
503 extern int device_rename(struct device *dev, char *new_name);
504 extern int device_move(struct device *dev, struct device *new_parent);
507 * Manual binding of a device to driver. See drivers/base/bus.c
508 * for information on use.
510 extern int __must_check device_bind_driver(struct device *dev);
511 extern void device_release_driver(struct device * dev);
512 extern int __must_check device_attach(struct device * dev);
513 extern int __must_check driver_attach(struct device_driver *drv);
514 extern int __must_check device_reprobe(struct device *dev);
517 * Easy functions for dynamically creating devices on the fly
519 extern struct device *device_create(struct class *cls, struct device *parent,
520 dev_t devt, const char *fmt, ...)
521 __attribute__((format(printf,4,5)));
522 extern void device_destroy(struct class *cls, dev_t devt);
523 #ifdef CONFIG_PM_SLEEP
524 extern void destroy_suspended_device(struct class *cls, dev_t devt);
525 #else /* !CONFIG_PM_SLEEP */
526 static inline void destroy_suspended_device(struct class *cls, dev_t devt)
528 device_destroy(cls, devt);
530 #endif /* !CONFIG_PM_SLEEP */
533 * Platform "fixup" functions - allow the platform to have their say
534 * about devices and actions that the general device layer doesn't
537 /* Notify platform of device discovery */
538 extern int (*platform_notify)(struct device * dev);
540 extern int (*platform_notify_remove)(struct device * dev);
544 * get_device - atomically increment the reference count for the device.
547 extern struct device * get_device(struct device * dev);
548 extern void put_device(struct device * dev);
551 /* drivers/base/power/shutdown.c */
552 extern void device_shutdown(void);
554 /* drivers/base/sys.c */
555 extern void sysdev_shutdown(void);
557 /* debugging and troubleshooting/diagnostic helpers. */
558 extern const char *dev_driver_string(struct device *dev);
559 #define dev_printk(level, dev, format, arg...) \
560 printk(level "%s %s: " format , dev_driver_string(dev) , (dev)->bus_id , ## arg)
562 #define dev_emerg(dev, format, arg...) \
563 dev_printk(KERN_EMERG , dev , format , ## arg)
564 #define dev_alert(dev, format, arg...) \
565 dev_printk(KERN_ALERT , dev , format , ## arg)
566 #define dev_crit(dev, format, arg...) \
567 dev_printk(KERN_CRIT , dev , format , ## arg)
568 #define dev_err(dev, format, arg...) \
569 dev_printk(KERN_ERR , dev , format , ## arg)
570 #define dev_warn(dev, format, arg...) \
571 dev_printk(KERN_WARNING , dev , format , ## arg)
572 #define dev_notice(dev, format, arg...) \
573 dev_printk(KERN_NOTICE , dev , format , ## arg)
574 #define dev_info(dev, format, arg...) \
575 dev_printk(KERN_INFO , dev , format , ## arg)
578 #define dev_dbg(dev, format, arg...) \
579 dev_printk(KERN_DEBUG , dev , format , ## arg)
581 static inline int __attribute__ ((format (printf, 2, 3)))
582 dev_dbg(struct device * dev, const char * fmt, ...)
589 #define dev_vdbg dev_dbg
591 static inline int __attribute__ ((format (printf, 2, 3)))
592 dev_vdbg(struct device * dev, const char * fmt, ...)
598 /* Create alias, so I can be autoloaded. */
599 #define MODULE_ALIAS_CHARDEV(major,minor) \
600 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
601 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
602 MODULE_ALIAS("char-major-" __stringify(major) "-*")
603 #endif /* _DEVICE_H_ */