]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/device.h
894fdb2e483a168abc79283329793aba712269df
[linux-2.6-omap-h63xx.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_ID_SIZE          32
29 #define BUS_ID_SIZE             20
30
31 struct device;
32 struct device_driver;
33 struct driver_private;
34 struct class;
35 struct class_private;
36 struct bus_type;
37 struct bus_type_private;
38
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);
43 };
44
45 #define BUS_ATTR(_name, _mode, _show, _store)   \
46 struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)
47
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 *);
51
52 struct bus_type {
53         const char              *name;
54         struct bus_attribute    *bus_attrs;
55         struct device_attribute *dev_attrs;
56         struct driver_attribute *drv_attrs;
57
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);
63
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);
68
69         struct pm_ext_ops *pm;
70
71         struct bus_type_private *p;
72 };
73
74 extern int __must_check bus_register(struct bus_type *bus);
75 extern void bus_unregister(struct bus_type *bus);
76
77 extern int __must_check bus_rescan_devices(struct bus_type *bus);
78
79 /* iterator helpers for buses */
80
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,
84                                void *data,
85                                int (*match)(struct device *dev, void *data));
86 struct device *bus_find_device_by_name(struct bus_type *bus,
87                                        struct device *start,
88                                        const char *name);
89
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 *));
93
94 /*
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
98  * notify hooks.
99  */
100 struct notifier_block;
101
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);
106
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.
110  */
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
115                                                       unbound */
116
117 extern struct kset *bus_get_kset(struct bus_type *bus);
118 extern struct klist *bus_get_device_klist(struct bus_type *bus);
119
120 struct device_driver {
121         const char              *name;
122         struct bus_type         *bus;
123
124         struct module           *owner;
125         const char              *mod_name;      /* used for built-in modules */
126
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;
133
134         struct pm_ops *pm;
135
136         struct driver_private *p;
137 };
138
139
140 extern int __must_check driver_register(struct device_driver *drv);
141 extern void driver_unregister(struct device_driver *drv);
142
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);
148
149 /* sysfs interface for exporting driver attributes */
150
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,
155                          size_t count);
156 };
157
158 #define DRIVER_ATTR(_name, _mode, _show, _store)        \
159 struct driver_attribute driver_attr_##_name =           \
160         __ATTR(_name, _mode, _show, _store)
161
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);
166
167 extern int __must_check driver_add_kobj(struct device_driver *drv,
168                                         struct kobject *kobj,
169                                         const char *fmt, ...);
170
171 extern int __must_check driver_for_each_device(struct device_driver *drv,
172                                                struct device *start,
173                                                void *data,
174                                                int (*fn)(struct device *dev,
175                                                          void *));
176 struct device *driver_find_device(struct device_driver *drv,
177                                   struct device *start, void *data,
178                                   int (*match)(struct device *dev, void *data));
179
180 /*
181  * device classes
182  */
183 struct class {
184         const char              *name;
185         struct module           *owner;
186
187         struct class_attribute          *class_attrs;
188         struct device_attribute         *dev_attrs;
189         struct kobject                  *dev_kobj;
190
191         int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
192
193         void (*class_release)(struct class *class);
194         void (*dev_release)(struct device *dev);
195
196         int (*suspend)(struct device *dev, pm_message_t state);
197         int (*resume)(struct device *dev);
198
199         struct pm_ops *pm;
200         struct class_private *p;
201 };
202
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);
208
209 /* This is a #define to keep the compiler from merging different
210  * instances of the __key variable */
211 #define class_register(class)                   \
212 ({                                              \
213         static struct lock_class_key __key;     \
214         __class_register(class, &__key);        \
215 })
216
217 extern int class_for_each_device(struct class *class, struct device *start,
218                                  void *data,
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 *));
223
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);
228 };
229
230 #define CLASS_ATTR(_name, _mode, _show, _store)                 \
231 struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store)
232
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);
237
238 struct class_interface {
239         struct list_head        node;
240         struct class            *class;
241
242         int (*add_dev)          (struct device *, struct class_interface *);
243         void (*remove_dev)      (struct device *, struct class_interface *);
244 };
245
246 extern int __must_check class_interface_register(struct class_interface *);
247 extern void class_interface_unregister(struct class_interface *);
248
249 extern struct class * __must_check __class_create(struct module *owner,
250                                                   const char *name,
251                                                   struct lock_class_key *key);
252 extern void class_destroy(struct class *cls);
253
254 /* This is a #define to keep the compiler from merging different
255  * instances of the __key variable */
256 #define class_create(owner, name)               \
257 ({                                              \
258         static struct lock_class_key __key;     \
259         __class_create(owner, name, &__key);    \
260 })
261
262 /*
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.
270  */
271 struct device_type {
272         const char *name;
273         struct attribute_group **groups;
274         int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
275         void (*release)(struct device *dev);
276
277         int (*suspend)(struct device *dev, pm_message_t state);
278         int (*resume)(struct device *dev);
279
280         struct pm_ops *pm;
281 };
282
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,
287                         char *buf);
288         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
289                          const char *buf, size_t count);
290 };
291
292 #define DEVICE_ATTR(_name, _mode, _show, _store) \
293 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
294
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);
305
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)
309
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);
313
314 #ifdef CONFIG_DEBUG_DEVRES
315 extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp,
316                              const char *name);
317 #define devres_alloc(release, size, gfp) \
318         __devres_alloc(release, size, gfp, #release)
319 #else
320 extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp);
321 #endif
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);
332
333 /* devres group */
334 extern void * __must_check devres_open_group(struct device *dev, void *id,
335                                              gfp_t gfp);
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);
339
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);
343
344 struct device_dma_parameters {
345         /*
346          * a low level driver may set these to teach IOMMU code about
347          * sg limitations.
348          */
349         unsigned int max_segment_size;
350         unsigned long segment_boundary_mask;
351 };
352
353 struct device {
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;
359
360         struct kobject kobj;
361         char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
362         struct device_type      *type;
363         unsigned                uevent_suppress:1;
364
365         struct semaphore        sem;    /* semaphore to synchronize calls to
366                                          * its driver.
367                                          */
368
369         struct bus_type *bus;           /* type of bus device is on */
370         struct device_driver *driver;   /* which driver has allocated this
371                                            device */
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;
376
377 #ifdef CONFIG_NUMA
378         int             numa_node;      /* NUMA node this device is close to */
379 #endif
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. */
386
387         struct device_dma_parameters *dma_parms;
388
389         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
390
391         struct dma_coherent_mem *dma_mem; /* internal for coherent mem
392                                              override */
393         /* arch specific additions */
394         struct dev_archdata     archdata;
395
396         spinlock_t              devres_lock;
397         struct list_head        devres_head;
398
399         struct list_head        node;
400         struct class            *class;
401         dev_t                   devt;   /* dev_t, creates the sysfs "dev" */
402         struct attribute_group  **groups;       /* optional groups */
403
404         void    (*release)(struct device *dev);
405 };
406
407 /* Get the wakeup routines, which depend on struct device */
408 #include <linux/pm_wakeup.h>
409
410 static inline const char *dev_name(struct device *dev)
411 {
412         /* will be changed into kobject_name(&dev->kobj) in the near future */
413         return dev->bus_id;
414 }
415
416 extern int dev_set_name(struct device *dev, const char *name, ...)
417                         __attribute__((format(printf, 2, 3)));
418
419 #ifdef CONFIG_NUMA
420 static inline int dev_to_node(struct device *dev)
421 {
422         return dev->numa_node;
423 }
424 static inline void set_dev_node(struct device *dev, int node)
425 {
426         dev->numa_node = node;
427 }
428 #else
429 static inline int dev_to_node(struct device *dev)
430 {
431         return -1;
432 }
433 static inline void set_dev_node(struct device *dev, int node)
434 {
435 }
436 #endif
437
438 static inline void *dev_get_drvdata(struct device *dev)
439 {
440         return dev->driver_data;
441 }
442
443 static inline void dev_set_drvdata(struct device *dev, void *data)
444 {
445         dev->driver_data = data;
446 }
447
448 static inline int device_is_registered(struct device *dev)
449 {
450         return dev->kobj.state_in_sysfs;
451 }
452
453 void driver_init(void);
454
455 /*
456  * High level routines for use by the bus drivers
457  */
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);
469
470 /*
471  * Manual binding of a device to driver. See drivers/base/bus.c
472  * for information on use.
473  */
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);
479
480 /*
481  * Easy functions for dynamically creating devices on the fly
482  */
483 extern struct device *device_create_vargs(struct class *cls,
484                                           struct device *parent,
485                                           dev_t devt,
486                                           void *drvdata,
487                                           const char *fmt,
488                                           va_list vargs);
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);
495
496 /*
497  * Platform "fixup" functions - allow the platform to have their say
498  * about devices and actions that the general device layer doesn't
499  * know about.
500  */
501 /* Notify platform of device discovery */
502 extern int (*platform_notify)(struct device *dev);
503
504 extern int (*platform_notify_remove)(struct device *dev);
505
506
507 /**
508  * get_device - atomically increment the reference count for the device.
509  *
510  */
511 extern struct device *get_device(struct device *dev);
512 extern void put_device(struct device *dev);
513
514
515 /* drivers/base/power/shutdown.c */
516 extern void device_shutdown(void);
517
518 /* drivers/base/sys.c */
519 extern void sysdev_shutdown(void);
520
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)
526
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)
541
542 #ifdef DEBUG
543 #define dev_dbg(dev, format, arg...)            \
544         dev_printk(KERN_DEBUG , dev , format , ## arg)
545 #else
546 #define dev_dbg(dev, format, arg...)            \
547         ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
548 #endif
549
550 #ifdef VERBOSE_DEBUG
551 #define dev_vdbg        dev_dbg
552 #else
553
554 #define dev_vdbg(dev, format, arg...)           \
555         ({ if (0) dev_printk(KERN_DEBUG, dev, format, ##arg); 0; })
556 #endif
557
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_ */