]> err.no Git - linux-2.6/blob - include/linux/device.h
10ab7807f8ea3c202106ecb3bf8ac5da1b84b156
[linux-2.6] / include / linux / device.h
1 /*
2  * device.h - generic, centralized driver model
3  *
4  * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
5  *
6  * This file is released under the GPLv2
7  *
8  * See Documentation/driver-model/ for more information.
9  */
10
11 #ifndef _DEVICE_H_
12 #define _DEVICE_H_
13
14 #include <linux/config.h>
15 #include <linux/ioport.h>
16 #include <linux/kobject.h>
17 #include <linux/klist.h>
18 #include <linux/list.h>
19 #include <linux/types.h>
20 #include <linux/module.h>
21 #include <linux/pm.h>
22 #include <asm/semaphore.h>
23 #include <asm/atomic.h>
24
25 #define DEVICE_NAME_SIZE        50
26 #define DEVICE_NAME_HALF        __stringify(20) /* Less than half to accommodate slop */
27 #define DEVICE_ID_SIZE          32
28 #define BUS_ID_SIZE             KOBJ_NAME_LEN
29
30
31 enum {
32         SUSPEND_NOTIFY,
33         SUSPEND_SAVE_STATE,
34         SUSPEND_DISABLE,
35         SUSPEND_POWER_DOWN,
36 };
37
38 enum {
39         RESUME_POWER_ON,
40         RESUME_RESTORE_STATE,
41         RESUME_ENABLE,
42 };
43
44 struct device;
45 struct device_driver;
46 struct class;
47 struct class_device;
48
49 struct bus_type {
50         const char              * name;
51
52         struct subsystem        subsys;
53         struct kset             drivers;
54         struct kset             devices;
55         struct klist            klist_devices;
56         struct klist            klist_drivers;
57
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             (*hotplug) (struct device *dev, char **envp, 
64                                     int num_envp, char *buffer, int buffer_size);
65         int             (*suspend)(struct device * dev, pm_message_t state);
66         int             (*resume)(struct device * dev);
67 };
68
69 extern int bus_register(struct bus_type * bus);
70 extern void bus_unregister(struct bus_type * bus);
71
72 extern void bus_rescan_devices(struct bus_type * bus);
73
74 extern struct bus_type * get_bus(struct bus_type * bus);
75 extern void put_bus(struct bus_type * bus);
76
77 extern struct bus_type * find_bus(char * name);
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 *, void *));
83 struct device * bus_find_device(struct bus_type *bus, struct device *start,
84                                 void *data, int (*match)(struct device *, void *));
85
86 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, 
87                      void * data, int (*fn)(struct device_driver *, void *));
88
89
90 /* driverfs interface for exporting bus attributes */
91
92 struct bus_attribute {
93         struct attribute        attr;
94         ssize_t (*show)(struct bus_type *, char * buf);
95         ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
96 };
97
98 #define BUS_ATTR(_name,_mode,_show,_store)      \
99 struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
100
101 extern int bus_create_file(struct bus_type *, struct bus_attribute *);
102 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
103
104 struct device_driver {
105         const char              * name;
106         struct bus_type         * bus;
107
108         struct completion       unloaded;
109         struct kobject          kobj;
110         struct klist            klist_devices;
111         struct klist_node       knode_bus;
112
113         struct module           * owner;
114
115         int     (*probe)        (struct device * dev);
116         int     (*remove)       (struct device * dev);
117         void    (*shutdown)     (struct device * dev);
118         int     (*suspend)      (struct device * dev, pm_message_t state, u32 level);
119         int     (*resume)       (struct device * dev, u32 level);
120 };
121
122
123 extern int driver_register(struct device_driver * drv);
124 extern void driver_unregister(struct device_driver * drv);
125
126 extern struct device_driver * get_driver(struct device_driver * drv);
127 extern void put_driver(struct device_driver * drv);
128 extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
129
130
131 /* driverfs interface for exporting driver attributes */
132
133 struct driver_attribute {
134         struct attribute        attr;
135         ssize_t (*show)(struct device_driver *, char * buf);
136         ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
137 };
138
139 #define DRIVER_ATTR(_name,_mode,_show,_store)   \
140 struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
141
142 extern int driver_create_file(struct device_driver *, struct driver_attribute *);
143 extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
144
145 extern int driver_for_each_device(struct device_driver * drv, struct device * start,
146                                   void * data, int (*fn)(struct device *, void *));
147 struct device * driver_find_device(struct device_driver *drv,
148                                    struct device *start, void *data,
149                                    int (*match)(struct device *, void *));
150
151
152 /*
153  * device classes
154  */
155 struct class {
156         const char              * name;
157         struct module           * owner;
158
159         struct subsystem        subsys;
160         struct list_head        children;
161         struct list_head        interfaces;
162         struct semaphore        sem;    /* locks both the children and interfaces lists */
163
164         struct class_attribute          * class_attrs;
165         struct class_device_attribute   * class_dev_attrs;
166
167         int     (*hotplug)(struct class_device *dev, char **envp, 
168                            int num_envp, char *buffer, int buffer_size);
169
170         void    (*release)(struct class_device *dev);
171         void    (*class_release)(struct class *class);
172 };
173
174 extern int class_register(struct class *);
175 extern void class_unregister(struct class *);
176
177 extern struct class * class_get(struct class *);
178 extern void class_put(struct class *);
179
180
181 struct class_attribute {
182         struct attribute        attr;
183         ssize_t (*show)(struct class *, char * buf);
184         ssize_t (*store)(struct class *, const char * buf, size_t count);
185 };
186
187 #define CLASS_ATTR(_name,_mode,_show,_store)                    \
188 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store) 
189
190 extern int class_create_file(struct class *, const struct class_attribute *);
191 extern void class_remove_file(struct class *, const struct class_attribute *);
192
193 struct class_device_attribute {
194         struct attribute        attr;
195         ssize_t (*show)(struct class_device *, char * buf);
196         ssize_t (*store)(struct class_device *, const char * buf, size_t count);
197 };
198
199 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store)             \
200 struct class_device_attribute class_device_attr_##_name =       \
201         __ATTR(_name,_mode,_show,_store)
202
203 extern int class_device_create_file(struct class_device *,
204                                     const struct class_device_attribute *);
205
206 /**
207  * struct class_device - class devices
208  * @class: pointer to the parent class for this class device.  This is required.
209  * @devt: for internal use by the driver core only.
210  * @node: for internal use by the driver core only.
211  * @kobj: for internal use by the driver core only.
212  * @devt_attr: for internal use by the driver core only.
213  * @dev: if set, a symlink to the struct device is created in the sysfs
214  * directory for this struct class device.
215  * @class_data: pointer to whatever you want to store here for this struct
216  * class_device.  Use class_get_devdata() and class_set_devdata() to get and
217  * set this pointer.
218  * @parent: pointer to a struct class_device that is the parent of this struct
219  * class_device.  If NULL, this class_device will show up at the root of the
220  * struct class in sysfs (which is probably what you want to have happen.)
221  * @release: pointer to a release function for this struct class_device.  If
222  * set, this will be called instead of the class specific release function.
223  * Only use this if you want to override the default release function, like
224  * when you are nesting class_device structures.
225  * @hotplug: pointer to a hotplug function for this struct class_device.  If
226  * set, this will be called instead of the class specific hotplug function.
227  * Only use this if you want to override the default hotplug function, like
228  * when you are nesting class_device structures.
229  */
230 struct class_device {
231         struct list_head        node;
232
233         struct kobject          kobj;
234         struct class            * class;        /* required */
235         dev_t                   devt;           /* dev_t, creates the sysfs "dev" */
236         struct class_device_attribute *devt_attr;
237         struct class_device_attribute uevent_attr;
238         struct device           * dev;          /* not necessary, but nice to have */
239         void                    * class_data;   /* class-specific data */
240         struct class_device     *parent;        /* parent of this child device, if there is one */
241
242         void    (*release)(struct class_device *dev);
243         int     (*hotplug)(struct class_device *dev, char **envp,
244                            int num_envp, char *buffer, int buffer_size);
245         char    class_id[BUS_ID_SIZE];  /* unique to this class */
246 };
247
248 static inline void *
249 class_get_devdata (struct class_device *dev)
250 {
251         return dev->class_data;
252 }
253
254 static inline void
255 class_set_devdata (struct class_device *dev, void *data)
256 {
257         dev->class_data = data;
258 }
259
260
261 extern int class_device_register(struct class_device *);
262 extern void class_device_unregister(struct class_device *);
263 extern void class_device_initialize(struct class_device *);
264 extern int class_device_add(struct class_device *);
265 extern void class_device_del(struct class_device *);
266
267 extern int class_device_rename(struct class_device *, char *);
268
269 extern struct class_device * class_device_get(struct class_device *);
270 extern void class_device_put(struct class_device *);
271
272 extern void class_device_remove_file(struct class_device *, 
273                                      const struct class_device_attribute *);
274 extern int class_device_create_bin_file(struct class_device *,
275                                         struct bin_attribute *);
276 extern void class_device_remove_bin_file(struct class_device *,
277                                          struct bin_attribute *);
278
279 struct class_interface {
280         struct list_head        node;
281         struct class            *class;
282
283         int (*add)      (struct class_device *, struct class_interface *);
284         void (*remove)  (struct class_device *, struct class_interface *);
285 };
286
287 extern int class_interface_register(struct class_interface *);
288 extern void class_interface_unregister(struct class_interface *);
289
290 extern struct class *class_create(struct module *owner, char *name);
291 extern void class_destroy(struct class *cls);
292 extern struct class_device *class_device_create(struct class *cls,
293                                                 struct class_device *parent,
294                                                 dev_t devt,
295                                                 struct device *device,
296                                                 char *fmt, ...)
297                                         __attribute__((format(printf,5,6)));
298 extern void class_device_destroy(struct class *cls, dev_t devt);
299
300
301 /* interface for exporting device attributes */
302 struct device_attribute {
303         struct attribute        attr;
304         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
305                         char *buf);
306         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
307                          const char *buf, size_t count);
308 };
309
310 #define DEVICE_ATTR(_name,_mode,_show,_store) \
311 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
312
313 extern int device_create_file(struct device *device, struct device_attribute * entry);
314 extern void device_remove_file(struct device * dev, struct device_attribute * attr);
315 struct device {
316         struct klist            klist_children;
317         struct klist_node       knode_parent;           /* node in sibling list */
318         struct klist_node       knode_driver;
319         struct klist_node       knode_bus;
320         struct device   * parent;
321
322         struct kobject kobj;
323         char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
324         struct device_attribute uevent_attr;
325
326         struct semaphore        sem;    /* semaphore to synchronize calls to
327                                          * its driver.
328                                          */
329
330         struct bus_type * bus;          /* type of bus device is on */
331         struct device_driver *driver;   /* which driver has allocated this
332                                            device */
333         void            *driver_data;   /* data private to the driver */
334         void            *platform_data; /* Platform specific data, device
335                                            core doesn't touch it */
336         void            *firmware_data; /* Firmware specific data (e.g. ACPI,
337                                            BIOS data),reserved for device core*/
338         struct dev_pm_info      power;
339
340         u64             *dma_mask;      /* dma mask (if dma'able device) */
341         u64             coherent_dma_mask;/* Like dma_mask, but for
342                                              alloc_coherent mappings as
343                                              not all hardware supports
344                                              64 bit addresses for consistent
345                                              allocations such descriptors. */
346
347         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
348
349         struct dma_coherent_mem *dma_mem; /* internal for coherent mem
350                                              override */
351
352         void    (*release)(struct device * dev);
353 };
354
355 static inline void *
356 dev_get_drvdata (struct device *dev)
357 {
358         return dev->driver_data;
359 }
360
361 static inline void
362 dev_set_drvdata (struct device *dev, void *data)
363 {
364         dev->driver_data = data;
365 }
366
367 static inline int device_is_registered(struct device *dev)
368 {
369         return klist_node_attached(&dev->knode_bus);
370 }
371
372 /*
373  * High level routines for use by the bus drivers
374  */
375 extern int device_register(struct device * dev);
376 extern void device_unregister(struct device * dev);
377 extern void device_initialize(struct device * dev);
378 extern int device_add(struct device * dev);
379 extern void device_del(struct device * dev);
380 extern int device_for_each_child(struct device *, void *,
381                      int (*fn)(struct device *, void *));
382
383 /*
384  * Manual binding of a device to driver. See drivers/base/bus.c
385  * for information on use.
386  */
387 extern void device_bind_driver(struct device * dev);
388 extern void device_release_driver(struct device * dev);
389 extern int  device_attach(struct device * dev);
390 extern void driver_attach(struct device_driver * drv);
391
392
393 /*
394  * Platform "fixup" functions - allow the platform to have their say
395  * about devices and actions that the general device layer doesn't
396  * know about.
397  */
398 /* Notify platform of device discovery */
399 extern int (*platform_notify)(struct device * dev);
400
401 extern int (*platform_notify_remove)(struct device * dev);
402
403
404 /**
405  * get_device - atomically increment the reference count for the device.
406  *
407  */
408 extern struct device * get_device(struct device * dev);
409 extern void put_device(struct device * dev);
410
411
412 /* drivers/base/platform.c */
413
414 struct platform_device {
415         const char      * name;
416         u32             id;
417         struct device   dev;
418         u32             num_resources;
419         struct resource * resource;
420 };
421
422 #define to_platform_device(x) container_of((x), struct platform_device, dev)
423
424 extern int platform_device_register(struct platform_device *);
425 extern void platform_device_unregister(struct platform_device *);
426
427 extern struct bus_type platform_bus_type;
428 extern struct device platform_bus;
429
430 extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
431 extern int platform_get_irq(struct platform_device *, unsigned int);
432 extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
433 extern int platform_get_irq_byname(struct platform_device *, char *);
434 extern int platform_add_devices(struct platform_device **, int);
435
436 extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
437
438 /* drivers/base/power.c */
439 extern void device_shutdown(void);
440
441
442 /* drivers/base/firmware.c */
443 extern int firmware_register(struct subsystem *);
444 extern void firmware_unregister(struct subsystem *);
445
446 /* debugging and troubleshooting/diagnostic helpers. */
447 #define dev_printk(level, dev, format, arg...)  \
448         printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
449
450 #ifdef DEBUG
451 #define dev_dbg(dev, format, arg...)            \
452         dev_printk(KERN_DEBUG , dev , format , ## arg)
453 #else
454 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
455 #endif
456
457 #define dev_err(dev, format, arg...)            \
458         dev_printk(KERN_ERR , dev , format , ## arg)
459 #define dev_info(dev, format, arg...)           \
460         dev_printk(KERN_INFO , dev , format , ## arg)
461 #define dev_warn(dev, format, arg...)           \
462         dev_printk(KERN_WARNING , dev , format , ## arg)
463
464 /* Create alias, so I can be autoloaded. */
465 #define MODULE_ALIAS_CHARDEV(major,minor) \
466         MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
467 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
468         MODULE_ALIAS("char-major-" __stringify(major) "-*")
469 #endif /* _DEVICE_H_ */