class: add lockdep infrastructure
[pandora-kernel.git] / include / linux / device.h
1 /*
2  * device.h - generic, centralized driver model
3  *
4  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5  * Copyright (c) 2004-2007 Greg Kroah-Hartman <gregkh@suse.de>
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
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>
23 #include <linux/pm.h>
24 #include <linux/semaphore.h>
25 #include <asm/atomic.h>
26 #include <asm/device.h>
27
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
33
34
35 struct device;
36 struct device_driver;
37 struct driver_private;
38 struct class;
39 struct class_private;
40 struct bus_type;
41 struct bus_type_private;
42
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);
47 };
48
49 #define BUS_ATTR(_name, _mode, _show, _store)   \
50 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
51
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 *);
55
56 struct bus_type {
57         const char              *name;
58         struct bus_attribute    *bus_attrs;
59         struct device_attribute *dev_attrs;
60         struct driver_attribute *drv_attrs;
61
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);
67
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);
72
73         struct pm_ext_ops *pm;
74
75         struct bus_type_private *p;
76 };
77
78 extern int __must_check bus_register(struct bus_type *bus);
79 extern void bus_unregister(struct bus_type *bus);
80
81 extern int __must_check bus_rescan_devices(struct bus_type *bus);
82
83 /* iterator helpers for buses */
84
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,
88                                void *data,
89                                int (*match)(struct device *dev, void *data));
90 struct device *bus_find_device_by_name(struct bus_type *bus,
91                                        struct device *start,
92                                        const char *name);
93
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 *));
97
98 /*
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
102  * notify hooks.
103  */
104 struct notifier_block;
105
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);
110
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.
114  */
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
119                                                       unbound */
120
121 extern struct kset *bus_get_kset(struct bus_type *bus);
122 extern struct klist *bus_get_device_klist(struct bus_type *bus);
123
124 struct device_driver {
125         const char              *name;
126         struct bus_type         *bus;
127
128         struct module           *owner;
129         const char              *mod_name;      /* used for built-in modules */
130
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;
137
138         struct pm_ops *pm;
139
140         struct driver_private *p;
141 };
142
143
144 extern int __must_check driver_register(struct device_driver *drv);
145 extern void driver_unregister(struct device_driver *drv);
146
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);
152
153 /* sysfs interface for exporting driver attributes */
154
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,
159                          size_t count);
160 };
161
162 #define DRIVER_ATTR(_name, _mode, _show, _store)        \
163 struct driver_attribute driver_attr_##_name =           \
164         __ATTR(_name, _mode, _show, _store)
165
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);
170
171 extern int __must_check driver_add_kobj(struct device_driver *drv,
172                                         struct kobject *kobj,
173                                         const char *fmt, ...);
174
175 extern int __must_check driver_for_each_device(struct device_driver *drv,
176                                                struct device *start,
177                                                void *data,
178                                                int (*fn)(struct device *dev,
179                                                          void *));
180 struct device *driver_find_device(struct device_driver *drv,
181                                   struct device *start, void *data,
182                                   int (*match)(struct device *dev, void *data));
183
184 /*
185  * device classes
186  */
187 struct class {
188         const char              *name;
189         struct module           *owner;
190
191         struct class_attribute          *class_attrs;
192         struct device_attribute         *dev_attrs;
193         struct kobject                  *dev_kobj;
194
195         int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
196
197         void (*class_release)(struct class *class);
198         void (*dev_release)(struct device *dev);
199
200         int (*suspend)(struct device *dev, pm_message_t state);
201         int (*resume)(struct device *dev);
202
203         struct pm_ops *pm;
204         struct class_private *p;
205 };
206
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);
212
213 /* This is a #define to keep the compiler from merging different
214  * instances of the __key variable */
215 #define class_register(class)                   \
216 ({                                              \
217         static struct lock_class_key __key;     \
218         __class_register(class, &__key);        \
219 })
220
221 extern int class_for_each_device(struct class *class, struct device *start,
222                                  void *data,
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 *));
227
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);
232 };
233
234 #define CLASS_ATTR(_name, _mode, _show, _store)                 \
235 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
236
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);
241
242 struct class_interface {
243         struct list_head        node;
244         struct class            *class;
245
246         int (*add_dev)          (struct device *, struct class_interface *);
247         void (*remove_dev)      (struct device *, struct class_interface *);
248 };
249
250 extern int __must_check class_interface_register(struct class_interface *);
251 extern void class_interface_unregister(struct class_interface *);
252
253 extern struct class * __must_check __class_create(struct module *owner,
254                                                   const char *name,
255                                                   struct lock_class_key *key);
256 extern void class_destroy(struct class *cls);
257
258 /* This is a #define to keep the compiler from merging different
259  * instances of the __key variable */
260 #define class_create(owner, name)               \
261 ({                                              \
262         static struct lock_class_key __key;     \
263         __class_create(owner, name, &__key);    \
264 })
265
266 /*
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.
274  */
275 struct device_type {
276         const char *name;
277         struct attribute_group **groups;
278         int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
279         void (*release)(struct device *dev);
280
281         int (*suspend)(struct device *dev, pm_message_t state);
282         int (*resume)(struct device *dev);
283
284         struct pm_ops *pm;
285 };
286
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,
291                         char *buf);
292         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
293                          const char *buf, size_t count);
294 };
295
296 #define DEVICE_ATTR(_name, _mode, _show, _store) \
297 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
298
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);
309
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)
313
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);
317
318 #ifdef CONFIG_DEBUG_DEVRES
319 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
320                              const char *name);
321 #define devres_alloc(release, size, gfp) \
322         __devres_alloc(release, size, gfp, #release)
323 #else
324 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
325 #endif
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);
336
337 /* devres group */
338 extern void * __must_check devres_open_group(struct device *dev, void *id,
339                                              gfp_t gfp);
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);
343
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);
347
348 struct device_dma_parameters {
349         /*
350          * a low level driver may set these to teach IOMMU code about
351          * sg limitations.
352          */
353         unsigned int max_segment_size;
354         unsigned long segment_boundary_mask;
355 };
356
357 struct device {
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;
363
364         struct kobject kobj;
365         char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
366         struct device_type      *type;
367         unsigned                uevent_suppress:1;
368
369         struct semaphore        sem;    /* semaphore to synchronize calls to
370                                          * its driver.
371                                          */
372
373         struct bus_type *bus;           /* type of bus device is on */
374         struct device_driver *driver;   /* which driver has allocated this
375                                            device */
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;
380
381 #ifdef CONFIG_NUMA
382         int             numa_node;      /* NUMA node this device is close to */
383 #endif
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. */
390
391         struct device_dma_parameters *dma_parms;
392
393         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
394
395         struct dma_coherent_mem *dma_mem; /* internal for coherent mem
396                                              override */
397         /* arch specific additions */
398         struct dev_archdata     archdata;
399
400         spinlock_t              devres_lock;
401         struct list_head        devres_head;
402
403         struct list_head        node;
404         struct class            *class;
405         dev_t                   devt;   /* dev_t, creates the sysfs "dev" */
406         struct attribute_group  **groups;       /* optional groups */
407
408         void    (*release)(struct device *dev);
409 };
410
411 /* Get the wakeup routines, which depend on struct device */
412 #include <linux/pm_wakeup.h>
413
414 static inline const char *dev_name(struct device *dev)
415 {
416         /* will be changed into kobject_name(&dev->kobj) in the near future */
417         return dev->bus_id;
418 }
419
420 extern int dev_set_name(struct device *dev, const char *name, ...)
421                         __attribute__((format(printf, 2, 3)));
422
423 #ifdef CONFIG_NUMA
424 static inline int dev_to_node(struct device *dev)
425 {
426         return dev->numa_node;
427 }
428 static inline void set_dev_node(struct device *dev, int node)
429 {
430         dev->numa_node = node;
431 }
432 #else
433 static inline int dev_to_node(struct device *dev)
434 {
435         return -1;
436 }
437 static inline void set_dev_node(struct device *dev, int node)
438 {
439 }
440 #endif
441
442 static inline void *dev_get_drvdata(struct device *dev)
443 {
444         return dev->driver_data;
445 }
446
447 static inline void dev_set_drvdata(struct device *dev, void *data)
448 {
449         dev->driver_data = data;
450 }
451
452 static inline int device_is_registered(struct device *dev)
453 {
454         return dev->kobj.state_in_sysfs;
455 }
456
457 void driver_init(void);
458
459 /*
460  * High level routines for use by the bus drivers
461  */
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);
473
474 /*
475  * Manual binding of a device to driver. See drivers/base/bus.c
476  * for information on use.
477  */
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);
483
484 /*
485  * Easy functions for dynamically creating devices on the fly
486  */
487 extern struct device *device_create_vargs(struct class *cls,
488                                           struct device *parent,
489                                           dev_t devt,
490                                           void *drvdata,
491                                           const char *fmt,
492                                           va_list vargs);
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);
499
500 /*
501  * Platform "fixup" functions - allow the platform to have their say
502  * about devices and actions that the general device layer doesn't
503  * know about.
504  */
505 /* Notify platform of device discovery */
506 extern int (*platform_notify)(struct device *dev);
507
508 extern int (*platform_notify_remove)(struct device *dev);
509
510
511 /**
512  * get_device - atomically increment the reference count for the device.
513  *
514  */
515 extern struct device *get_device(struct device *dev);
516 extern void put_device(struct device *dev);
517
518
519 /* drivers/base/power/shutdown.c */
520 extern void device_shutdown(void);
521
522 /* drivers/base/sys.c */
523 extern void sysdev_shutdown(void);
524
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)
530
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)
545
546 #ifdef DEBUG
547 #define dev_dbg(dev, format, arg...)            \
548         dev_printk(KERN_DEBUG , dev , format , ## arg)
549 #else
550 #define dev_dbg(dev, format, arg...)            \
551         ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
552 #endif
553
554 #ifdef VERBOSE_DEBUG
555 #define dev_vdbg        dev_dbg
556 #else
557
558 #define dev_vdbg(dev, format, arg...)           \
559         ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
560 #endif
561
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_ */