Driver core: udev triggered device-<>driver binding
[deliverable/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 <mochel@osdl.org>
89790fd7 5 * Copyright (c) 2004-2007 Greg Kroah-Hartman <gregkh@suse.de>
1da177e4
LT
6 *
7 * This file is released under the GPLv2
8 *
9 * See Documentation/driver-model/ for more information.
10 */
11
12#ifndef _DEVICE_H_
13#define _DEVICE_H_
14
1da177e4
LT
15#include <linux/ioport.h>
16#include <linux/kobject.h>
465c7a3a 17#include <linux/klist.h>
1da177e4 18#include <linux/list.h>
4a7fb636 19#include <linux/compiler.h>
1da177e4
LT
20#include <linux/types.h>
21#include <linux/module.h>
22#include <linux/pm.h>
23#include <asm/semaphore.h>
24#include <asm/atomic.h>
c6dbaef2 25#include <asm/device.h>
1da177e4
LT
26
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
31
32
1da177e4
LT
33struct device;
34struct device_driver;
35struct class;
36struct class_device;
b8c5cec2
KS
37struct bus_type;
38
39struct 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);
43};
44
45#define BUS_ATTR(_name,_mode,_show,_store) \
46struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
47
48extern int __must_check bus_create_file(struct bus_type *,
49 struct bus_attribute *);
50extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
1da177e4
LT
51
52struct bus_type {
8d790d74 53 const char * name;
b8c5cec2 54 struct module * owner;
1da177e4
LT
55
56 struct subsystem subsys;
57 struct kset drivers;
58 struct kset devices;
465c7a3a 59 struct klist klist_devices;
38fdac3c 60 struct klist klist_drivers;
1da177e4 61
116af378
BH
62 struct blocking_notifier_head bus_notifier;
63
1da177e4
LT
64 struct bus_attribute * bus_attrs;
65 struct device_attribute * dev_attrs;
66 struct driver_attribute * drv_attrs;
b8c5cec2
KS
67 struct bus_attribute drivers_autoprobe_attr;
68 struct bus_attribute drivers_probe_attr;
1da177e4
LT
69
70 int (*match)(struct device * dev, struct device_driver * drv);
312c004d
KS
71 int (*uevent)(struct device *dev, char **envp,
72 int num_envp, char *buffer, int buffer_size);
594c8281
RK
73 int (*probe)(struct device * dev);
74 int (*remove)(struct device * dev);
75 void (*shutdown)(struct device * dev);
7c8265f5 76
7c8265f5
LT
77 int (*suspend)(struct device * dev, pm_message_t state);
78 int (*suspend_late)(struct device * dev, pm_message_t state);
79 int (*resume_early)(struct device * dev);
80 int (*resume)(struct device * dev);
b8c5cec2
KS
81
82 unsigned int drivers_autoprobe:1;
1da177e4
LT
83};
84
4a7fb636 85extern int __must_check bus_register(struct bus_type * bus);
1da177e4
LT
86extern void bus_unregister(struct bus_type * bus);
87
f86db396 88extern int __must_check bus_rescan_devices(struct bus_type * bus);
1da177e4 89
1da177e4
LT
90/* iterator helpers for buses */
91
92int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
93 int (*fn)(struct device *, void *));
0edb5860
CH
94struct device * bus_find_device(struct bus_type *bus, struct device *start,
95 void *data, int (*match)(struct device *, void *));
1da177e4 96
4a7fb636
AM
97int __must_check bus_for_each_drv(struct bus_type *bus,
98 struct device_driver *start, void *data,
99 int (*fn)(struct device_driver *, void *));
1da177e4 100
116af378
BH
101/*
102 * Bus notifiers: Get notified of addition/removal of devices
103 * and binding/unbinding of drivers to devices.
104 * In the long run, it should be a replacement for the platform
105 * notify hooks.
106 */
107struct notifier_block;
108
109extern int bus_register_notifier(struct bus_type *bus,
110 struct notifier_block *nb);
111extern int bus_unregister_notifier(struct bus_type *bus,
112 struct notifier_block *nb);
113
114/* All 4 notifers below get called with the target struct device *
115 * as an argument. Note that those functions are likely to be called
116 * with the device semaphore held in the core, so be careful.
117 */
118#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */
119#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */
120#define BUS_NOTIFY_BOUND_DRIVER 0x00000003 /* driver bound to device */
121#define BUS_NOTIFY_UNBIND_DRIVER 0x00000004 /* driver about to be
122 unbound */
123
1da177e4 124struct device_driver {
8d790d74 125 const char * name;
1da177e4
LT
126 struct bus_type * bus;
127
128 struct completion unloaded;
129 struct kobject kobj;
94e7b1c5 130 struct klist klist_devices;
38fdac3c 131 struct klist_node knode_bus;
1da177e4 132
8d790d74 133 struct module * owner;
f30c53a8 134 const char * mod_name; /* used for built-in modules */
0c84ce26 135 struct module_kobject * mkobj;
1da177e4
LT
136
137 int (*probe) (struct device * dev);
8d790d74 138 int (*remove) (struct device * dev);
1da177e4 139 void (*shutdown) (struct device * dev);
9480e307
RK
140 int (*suspend) (struct device * dev, pm_message_t state);
141 int (*resume) (struct device * dev);
d779249e
GKH
142
143 unsigned int multithread_probe:1;
1da177e4
LT
144};
145
146
4a7fb636 147extern int __must_check driver_register(struct device_driver * drv);
1da177e4
LT
148extern void driver_unregister(struct device_driver * drv);
149
150extern struct device_driver * get_driver(struct device_driver * drv);
151extern void put_driver(struct device_driver * drv);
152extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
d779249e 153extern int driver_probe_done(void);
1da177e4 154
405ae7d3 155/* sysfs interface for exporting driver attributes */
1da177e4
LT
156
157struct driver_attribute {
158 struct attribute attr;
159 ssize_t (*show)(struct device_driver *, char * buf);
160 ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
161};
162
163#define DRIVER_ATTR(_name,_mode,_show,_store) \
164struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
165
4a7fb636
AM
166extern int __must_check driver_create_file(struct device_driver *,
167 struct driver_attribute *);
1da177e4
LT
168extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
169
4a7fb636
AM
170extern int __must_check driver_for_each_device(struct device_driver * drv,
171 struct device *start, void *data,
172 int (*fn)(struct device *, void *));
0edb5860
CH
173struct device * driver_find_device(struct device_driver *drv,
174 struct device *start, void *data,
175 int (*match)(struct device *, void *));
fae3cd00 176
1da177e4
LT
177/*
178 * device classes
179 */
180struct class {
8d790d74 181 const char * name;
e9ba6365 182 struct module * owner;
1da177e4
LT
183
184 struct subsystem subsys;
185 struct list_head children;
23681e47 186 struct list_head devices;
1da177e4 187 struct list_head interfaces;
86406245 188 struct kset class_dirs;
1da177e4
LT
189 struct semaphore sem; /* locks both the children and interfaces lists */
190
191 struct class_attribute * class_attrs;
192 struct class_device_attribute * class_dev_attrs;
2620efef 193 struct device_attribute * dev_attrs;
1da177e4 194
312c004d 195 int (*uevent)(struct class_device *dev, char **envp,
1da177e4 196 int num_envp, char *buffer, int buffer_size);
2620efef
GKH
197 int (*dev_uevent)(struct device *dev, char **envp, int num_envp,
198 char *buffer, int buffer_size);
1da177e4
LT
199
200 void (*release)(struct class_device *dev);
201 void (*class_release)(struct class *class);
2620efef 202 void (*dev_release)(struct device *dev);
7c8265f5
LT
203
204 int (*suspend)(struct device *, pm_message_t state);
205 int (*resume)(struct device *);
1da177e4
LT
206};
207
4a7fb636 208extern int __must_check class_register(struct class *);
1da177e4
LT
209extern void class_unregister(struct class *);
210
1da177e4
LT
211
212struct class_attribute {
213 struct attribute attr;
214 ssize_t (*show)(struct class *, char * buf);
215 ssize_t (*store)(struct class *, const char * buf, size_t count);
216};
217
218#define CLASS_ATTR(_name,_mode,_show,_store) \
219struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
220
4a7fb636
AM
221extern int __must_check class_create_file(struct class *,
222 const struct class_attribute *);
1da177e4
LT
223extern void class_remove_file(struct class *, const struct class_attribute *);
224
a7fd6706
KS
225struct class_device_attribute {
226 struct attribute attr;
227 ssize_t (*show)(struct class_device *, char * buf);
228 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
229};
230
231#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
232struct class_device_attribute class_device_attr_##_name = \
233 __ATTR(_name,_mode,_show,_store)
234
4a7fb636 235extern int __must_check class_device_create_file(struct class_device *,
a7fd6706 236 const struct class_device_attribute *);
1da177e4 237
74be227f
GKH
238/**
239 * struct class_device - class devices
240 * @class: pointer to the parent class for this class device. This is required.
241 * @devt: for internal use by the driver core only.
242 * @node: for internal use by the driver core only.
243 * @kobj: for internal use by the driver core only.
244 * @devt_attr: for internal use by the driver core only.
1498221d 245 * @groups: optional additional groups to be created
74be227f
GKH
246 * @dev: if set, a symlink to the struct device is created in the sysfs
247 * directory for this struct class device.
248 * @class_data: pointer to whatever you want to store here for this struct
249 * class_device. Use class_get_devdata() and class_set_devdata() to get and
250 * set this pointer.
251 * @parent: pointer to a struct class_device that is the parent of this struct
252 * class_device. If NULL, this class_device will show up at the root of the
253 * struct class in sysfs (which is probably what you want to have happen.)
254 * @release: pointer to a release function for this struct class_device. If
255 * set, this will be called instead of the class specific release function.
256 * Only use this if you want to override the default release function, like
257 * when you are nesting class_device structures.
312c004d
KS
258 * @uevent: pointer to a uevent function for this struct class_device. If
259 * set, this will be called instead of the class specific uevent function.
260 * Only use this if you want to override the default uevent function, like
74be227f
GKH
261 * when you are nesting class_device structures.
262 */
1da177e4
LT
263struct class_device {
264 struct list_head node;
265
266 struct kobject kobj;
267 struct class * class; /* required */
268 dev_t devt; /* dev_t, creates the sysfs "dev" */
e9ba6365 269 struct class_device_attribute *devt_attr;
a7fd6706 270 struct class_device_attribute uevent_attr;
1da177e4
LT
271 struct device * dev; /* not necessary, but nice to have */
272 void * class_data; /* class-specific data */
51d172d5 273 struct class_device *parent; /* parent of this child device, if there is one */
1498221d 274 struct attribute_group ** groups; /* optional groups */
1da177e4 275
51d172d5 276 void (*release)(struct class_device *dev);
312c004d 277 int (*uevent)(struct class_device *dev, char **envp,
51d172d5 278 int num_envp, char *buffer, int buffer_size);
1da177e4
LT
279 char class_id[BUS_ID_SIZE]; /* unique to this class */
280};
281
282static inline void *
283class_get_devdata (struct class_device *dev)
284{
285 return dev->class_data;
286}
287
288static inline void
289class_set_devdata (struct class_device *dev, void *data)
290{
291 dev->class_data = data;
292}
293
294
4a7fb636 295extern int __must_check class_device_register(struct class_device *);
1da177e4
LT
296extern void class_device_unregister(struct class_device *);
297extern void class_device_initialize(struct class_device *);
4a7fb636 298extern int __must_check class_device_add(struct class_device *);
1da177e4
LT
299extern void class_device_del(struct class_device *);
300
1da177e4
LT
301extern struct class_device * class_device_get(struct class_device *);
302extern void class_device_put(struct class_device *);
303
1da177e4
LT
304extern void class_device_remove_file(struct class_device *,
305 const struct class_device_attribute *);
4a7fb636 306extern int __must_check class_device_create_bin_file(struct class_device *,
1da177e4
LT
307 struct bin_attribute *);
308extern void class_device_remove_bin_file(struct class_device *,
309 struct bin_attribute *);
310
311struct class_interface {
312 struct list_head node;
313 struct class *class;
314
d8539d81
DT
315 int (*add) (struct class_device *, struct class_interface *);
316 void (*remove) (struct class_device *, struct class_interface *);
c47ed219
GKH
317 int (*add_dev) (struct device *, struct class_interface *);
318 void (*remove_dev) (struct device *, struct class_interface *);
1da177e4
LT
319};
320
4a7fb636 321extern int __must_check class_interface_register(struct class_interface *);
1da177e4
LT
322extern void class_interface_unregister(struct class_interface *);
323
ab7d7371 324extern struct class *class_create(struct module *owner, const char *name);
e9ba6365 325extern void class_destroy(struct class *cls);
51d172d5
GKH
326extern struct class_device *class_device_create(struct class *cls,
327 struct class_device *parent,
328 dev_t devt,
329 struct device *device,
ddd5d35a 330 const char *fmt, ...)
51d172d5 331 __attribute__((format(printf,5,6)));
e9ba6365 332extern void class_device_destroy(struct class *cls, dev_t devt);
333
f9f852df
KS
334struct device_type {
335 struct device_attribute *attrs;
336 int (*uevent)(struct device *dev, char **envp, int num_envp,
337 char *buffer, int buffer_size);
338 void (*release)(struct device *dev);
339};
340
a7fd6706
KS
341/* interface for exporting device attributes */
342struct device_attribute {
343 struct attribute attr;
344 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
345 char *buf);
346 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
347 const char *buf, size_t count);
348};
349
350#define DEVICE_ATTR(_name,_mode,_show,_store) \
351struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
352
4a7fb636
AM
353extern int __must_check device_create_file(struct device *device,
354 struct device_attribute * entry);
a7fd6706 355extern void device_remove_file(struct device * dev, struct device_attribute * attr);
2589f188
GKH
356extern int __must_check device_create_bin_file(struct device *dev,
357 struct bin_attribute *attr);
358extern void device_remove_bin_file(struct device *dev,
359 struct bin_attribute *attr);
d9a9cdfb
AS
360extern int device_schedule_callback(struct device *dev,
361 void (*func)(struct device *));
9ac7849e
TH
362
363/* device resource management */
364typedef void (*dr_release_t)(struct device *dev, void *res);
365typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data);
366
367#ifdef CONFIG_DEBUG_DEVRES
368extern void * __devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
369 const char *name);
370#define devres_alloc(release, size, gfp) \
371 __devres_alloc(release, size, gfp, #release)
372#else
373extern void * devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
374#endif
375extern void devres_free(void *res);
376extern void devres_add(struct device *dev, void *res);
377extern void * devres_find(struct device *dev, dr_release_t release,
378 dr_match_t match, void *match_data);
379extern void * devres_get(struct device *dev, void *new_res,
380 dr_match_t match, void *match_data);
381extern void * devres_remove(struct device *dev, dr_release_t release,
382 dr_match_t match, void *match_data);
383extern int devres_destroy(struct device *dev, dr_release_t release,
384 dr_match_t match, void *match_data);
385
386/* devres group */
387extern void * __must_check devres_open_group(struct device *dev, void *id,
388 gfp_t gfp);
389extern void devres_close_group(struct device *dev, void *id);
390extern void devres_remove_group(struct device *dev, void *id);
391extern int devres_release_group(struct device *dev, void *id);
392
393/* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */
394extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp);
395extern void devm_kfree(struct device *dev, void *p);
396
1da177e4 397struct device {
36239577 398 struct klist klist_children;
399 struct klist_node knode_parent; /* node in sibling list */
94e7b1c5 400 struct klist_node knode_driver;
465c7a3a 401 struct klist_node knode_bus;
1da177e4
LT
402 struct device * parent;
403
404 struct kobject kobj;
405 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
f9f852df 406 struct device_type *type;
f2eaae19 407 unsigned is_registered:1;
a7fd6706 408 struct device_attribute uevent_attr;
23681e47 409 struct device_attribute *devt_attr;
1da177e4 410
af70316a 411 struct semaphore sem; /* semaphore to synchronize calls to
412 * its driver.
413 */
414
1da177e4
LT
415 struct bus_type * bus; /* type of bus device is on */
416 struct device_driver *driver; /* which driver has allocated this
417 device */
418 void *driver_data; /* data private to the driver */
4e10d12a
DSL
419 void *platform_data; /* Platform specific data, device
420 core doesn't touch it */
1da177e4
LT
421 struct dev_pm_info power;
422
87348136
CH
423#ifdef CONFIG_NUMA
424 int numa_node; /* NUMA node this device is close to */
425#endif
1da177e4
LT
426 u64 *dma_mask; /* dma mask (if dma'able device) */
427 u64 coherent_dma_mask;/* Like dma_mask, but for
428 alloc_coherent mappings as
429 not all hardware supports
430 64 bit addresses for consistent
431 allocations such descriptors. */
432
433 struct list_head dma_pools; /* dma pools (if dma'ble) */
434
435 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
436 override */
c6dbaef2
BH
437 /* arch specific additions */
438 struct dev_archdata archdata;
1da177e4 439
9ac7849e
TH
440 spinlock_t devres_lock;
441 struct list_head devres_head;
442
23681e47
GKH
443 /* class_device migration path */
444 struct list_head node;
b7a3e813 445 struct class *class;
23681e47 446 dev_t devt; /* dev_t, creates the sysfs "dev" */
de0ff00d 447 struct attribute_group **groups; /* optional groups */
b7a3e813 448 int uevent_suppress;
23681e47 449
1da177e4
LT
450 void (*release)(struct device * dev);
451};
452
87348136
CH
453#ifdef CONFIG_NUMA
454static inline int dev_to_node(struct device *dev)
455{
456 return dev->numa_node;
457}
458static inline void set_dev_node(struct device *dev, int node)
459{
460 dev->numa_node = node;
461}
462#else
463static inline int dev_to_node(struct device *dev)
464{
465 return -1;
466}
467static inline void set_dev_node(struct device *dev, int node)
468{
469}
470#endif
471
1da177e4
LT
472static inline void *
473dev_get_drvdata (struct device *dev)
474{
475 return dev->driver_data;
476}
477
478static inline void
479dev_set_drvdata (struct device *dev, void *data)
480{
481 dev->driver_data = data;
482}
483
d305ef5d
DR
484static inline int device_is_registered(struct device *dev)
485{
f2eaae19 486 return dev->is_registered;
d305ef5d
DR
487}
488
1f21782e
AB
489void driver_init(void);
490
1da177e4
LT
491/*
492 * High level routines for use by the bus drivers
493 */
4a7fb636 494extern int __must_check device_register(struct device * dev);
1da177e4
LT
495extern void device_unregister(struct device * dev);
496extern void device_initialize(struct device * dev);
4a7fb636 497extern int __must_check device_add(struct device * dev);
1da177e4 498extern void device_del(struct device * dev);
04fed361 499extern int device_for_each_child(struct device *, void *,
1da177e4 500 int (*fn)(struct device *, void *));
5ab69981
CH
501extern struct device *device_find_child(struct device *, void *data,
502 int (*match)(struct device *, void *));
a2de48ca 503extern int device_rename(struct device *dev, char *new_name);
8a82472f 504extern int device_move(struct device *dev, struct device *new_parent);
1da177e4
LT
505
506/*
507 * Manual binding of a device to driver. See drivers/base/bus.c
508 * for information on use.
509 */
f86db396 510extern int __must_check device_bind_driver(struct device *dev);
1da177e4 511extern void device_release_driver(struct device * dev);
4a7fb636 512extern int __must_check device_attach(struct device * dev);
f86db396
AM
513extern int __must_check driver_attach(struct device_driver *drv);
514extern int __must_check device_reprobe(struct device *dev);
1da177e4 515
23681e47
GKH
516/*
517 * Easy functions for dynamically creating devices on the fly
518 */
519extern struct device *device_create(struct class *cls, struct device *parent,
5cbe5f8a 520 dev_t devt, const char *fmt, ...)
23681e47
GKH
521 __attribute__((format(printf,4,5)));
522extern void device_destroy(struct class *cls, dev_t devt);
1da177e4 523
1da177e4
LT
524/*
525 * Platform "fixup" functions - allow the platform to have their say
526 * about devices and actions that the general device layer doesn't
527 * know about.
528 */
529/* Notify platform of device discovery */
530extern int (*platform_notify)(struct device * dev);
531
532extern int (*platform_notify_remove)(struct device * dev);
533
534
535/**
536 * get_device - atomically increment the reference count for the device.
537 *
538 */
539extern struct device * get_device(struct device * dev);
540extern void put_device(struct device * dev);
1da177e4
LT
541
542
116f232b 543/* drivers/base/power/shutdown.c */
1da177e4
LT
544extern void device_shutdown(void);
545
546
547/* drivers/base/firmware.c */
4a7fb636 548extern int __must_check firmware_register(struct subsystem *);
1da177e4
LT
549extern void firmware_unregister(struct subsystem *);
550
551/* debugging and troubleshooting/diagnostic helpers. */
3e95637a 552extern const char *dev_driver_string(struct device *dev);
1da177e4 553#define dev_printk(level, dev, format, arg...) \
3e95637a 554 printk(level "%s %s: " format , dev_driver_string(dev) , (dev)->bus_id , ## arg)
1da177e4
LT
555
556#ifdef DEBUG
557#define dev_dbg(dev, format, arg...) \
558 dev_printk(KERN_DEBUG , dev , format , ## arg)
559#else
560#define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
561#endif
562
563#define dev_err(dev, format, arg...) \
564 dev_printk(KERN_ERR , dev , format , ## arg)
565#define dev_info(dev, format, arg...) \
566 dev_printk(KERN_INFO , dev , format , ## arg)
567#define dev_warn(dev, format, arg...) \
568 dev_printk(KERN_WARNING , dev , format , ## arg)
4f2928d0
TS
569#define dev_notice(dev, format, arg...) \
570 dev_printk(KERN_NOTICE , dev , format , ## arg)
1da177e4
LT
571
572/* Create alias, so I can be autoloaded. */
573#define MODULE_ALIAS_CHARDEV(major,minor) \
574 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
575#define MODULE_ALIAS_CHARDEV_MAJOR(major) \
576 MODULE_ALIAS("char-major-" __stringify(major) "-*")
577#endif /* _DEVICE_H_ */
This page took 0.331143 seconds and 5 git commands to generate.