patch-2_6_7-vs1_9_1_12
[linux-2.6.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  *
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/list.h>
18 #include <linux/spinlock.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 struct class_simple;
49
50 struct bus_type {
51         char                    * name;
52
53         struct subsystem        subsys;
54         struct kset             drivers;
55         struct kset             devices;
56
57         int             (*match)(struct device * dev, struct device_driver * drv);
58         struct device * (*add)  (struct device * parent, char * bus_id);
59         int             (*hotplug) (struct device *dev, char **envp, 
60                                     int num_envp, char *buffer, int buffer_size);
61         int             (*suspend)(struct device * dev, u32 state);
62         int             (*resume)(struct device * dev);
63 };
64
65 extern int bus_register(struct bus_type * bus);
66 extern void bus_unregister(struct bus_type * bus);
67
68 extern int bus_rescan_devices(struct bus_type * bus);
69
70 extern struct bus_type * get_bus(struct bus_type * bus);
71 extern void put_bus(struct bus_type * bus);
72
73 extern struct bus_type * find_bus(char * name);
74
75 /* iterator helpers for buses */
76
77 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
78                      int (*fn)(struct device *, void *));
79
80 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, 
81                      void * data, int (*fn)(struct device_driver *, void *));
82
83
84 /* driverfs interface for exporting bus attributes */
85
86 struct bus_attribute {
87         struct attribute        attr;
88         ssize_t (*show)(struct bus_type *, char * buf);
89         ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
90 };
91
92 #define BUS_ATTR(_name,_mode,_show,_store)      \
93 struct bus_attribute bus_attr_##_name = {               \
94         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },     \
95         .show   = _show,                                \
96         .store  = _store,                               \
97 };
98
99 extern int bus_create_file(struct bus_type *, struct bus_attribute *);
100 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
101
102 struct device_driver {
103         char                    * name;
104         struct bus_type         * bus;
105
106         struct semaphore        unload_sem;
107         struct kobject          kobj;
108         struct list_head        devices;
109
110         int     (*probe)        (struct device * dev);
111         int     (*remove)       (struct device * dev);
112         void    (*shutdown)     (struct device * dev);
113         int     (*suspend)      (struct device * dev, u32 state, u32 level);
114         int     (*resume)       (struct device * dev, u32 level);
115 };
116
117
118 extern int driver_register(struct device_driver * drv);
119 extern void driver_unregister(struct device_driver * drv);
120
121 extern struct device_driver * get_driver(struct device_driver * drv);
122 extern void put_driver(struct device_driver * drv);
123
124
125 /* driverfs interface for exporting driver attributes */
126
127 struct driver_attribute {
128         struct attribute        attr;
129         ssize_t (*show)(struct device_driver *, char * buf);
130         ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
131 };
132
133 #define DRIVER_ATTR(_name,_mode,_show,_store)   \
134 struct driver_attribute driver_attr_##_name = {                 \
135         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },     \
136         .show   = _show,                                \
137         .store  = _store,                               \
138 };
139
140 extern int driver_create_file(struct device_driver *, struct driver_attribute *);
141 extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
142
143
144 /*
145  * device classes
146  */
147 struct class {
148         char                    * name;
149
150         struct subsystem        subsys;
151         struct list_head        children;
152         struct list_head        interfaces;
153
154         int     (*hotplug)(struct class_device *dev, char **envp, 
155                            int num_envp, char *buffer, int buffer_size);
156
157         void    (*release)(struct class_device *dev);
158         void    (*class_release)(struct class *class);
159 };
160
161 extern int class_register(struct class *);
162 extern void class_unregister(struct class *);
163
164 extern struct class * class_get(struct class *);
165 extern void class_put(struct class *);
166
167
168 struct class_attribute {
169         struct attribute        attr;
170         ssize_t (*show)(struct class *, char * buf);
171         ssize_t (*store)(struct class *, const char * buf, size_t count);
172 };
173
174 #define CLASS_ATTR(_name,_mode,_show,_store)                    \
175 struct class_attribute class_attr_##_name = {                   \
176         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },     \
177         .show   = _show,                                        \
178         .store  = _store,                                       \
179 };
180
181 extern int class_create_file(struct class *, const struct class_attribute *);
182 extern void class_remove_file(struct class *, const struct class_attribute *);
183
184
185 struct class_device {
186         struct list_head        node;
187
188         struct kobject          kobj;
189         struct class            * class;        /* required */
190         struct device           * dev;          /* not necessary, but nice to have */
191         void                    * class_data;   /* class-specific data */
192
193         char    class_id[BUS_ID_SIZE];  /* unique to this class */
194 };
195
196 static inline void *
197 class_get_devdata (struct class_device *dev)
198 {
199         return dev->class_data;
200 }
201
202 static inline void
203 class_set_devdata (struct class_device *dev, void *data)
204 {
205         dev->class_data = data;
206 }
207
208
209 extern int class_device_register(struct class_device *);
210 extern void class_device_unregister(struct class_device *);
211 extern void class_device_initialize(struct class_device *);
212 extern int class_device_add(struct class_device *);
213 extern void class_device_del(struct class_device *);
214
215 extern int class_device_rename(struct class_device *, char *);
216
217 extern struct class_device * class_device_get(struct class_device *);
218 extern void class_device_put(struct class_device *);
219
220 struct class_device_attribute {
221         struct attribute        attr;
222         ssize_t (*show)(struct class_device *, char * buf);
223         ssize_t (*store)(struct class_device *, const char * buf, size_t count);
224 };
225
226 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store)             \
227 struct class_device_attribute class_device_attr_##_name = {     \
228         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },     \
229         .show   = _show,                                        \
230         .store  = _store,                                       \
231 };
232
233 extern int class_device_create_file(struct class_device *, 
234                                     const struct class_device_attribute *);
235 extern void class_device_remove_file(struct class_device *, 
236                                      const struct class_device_attribute *);
237
238
239 struct class_interface {
240         struct list_head        node;
241         struct class            *class;
242
243         int (*add)      (struct class_device *);
244         void (*remove)  (struct class_device *);
245 };
246
247 extern int class_interface_register(struct class_interface *);
248 extern void class_interface_unregister(struct class_interface *);
249
250 /* interface for class simple stuff */
251 extern struct class_simple *class_simple_create(struct module *owner, char *name);
252 extern void class_simple_destroy(struct class_simple *cs);
253 extern struct class_device *class_simple_device_add(struct class_simple *cs, dev_t dev, struct device *device, const char *fmt, ...)
254         __attribute__((format(printf,4,5)));
255 extern int class_simple_set_hotplug(struct class_simple *, 
256         int (*hotplug)(struct class_device *dev, char **envp, int num_envp, char *buffer, int buffer_size));
257 extern void class_simple_device_remove(dev_t dev);
258
259
260 struct device {
261         struct list_head node;          /* node in sibling list */
262         struct list_head bus_list;      /* node in bus's list */
263         struct list_head driver_list;
264         struct list_head children;
265         struct device   * parent;
266
267         struct kobject kobj;
268         char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
269
270         struct bus_type * bus;          /* type of bus device is on */
271         struct device_driver *driver;   /* which driver has allocated this
272                                            device */
273         void            *driver_data;   /* data private to the driver */
274         void            *platform_data; /* Platform specific data (e.g. ACPI,
275                                            BIOS data relevant to device) */
276         struct dev_pm_info      power;
277         u32             power_state;    /* Current operating state. In
278                                            ACPI-speak, this is D0-D3, D0
279                                            being fully functional, and D3
280                                            being off. */
281
282         unsigned char *saved_state;     /* saved device state */
283         u32             detach_state;   /* State to enter when device is
284                                            detached from its driver. */
285
286         u64             *dma_mask;      /* dma mask (if dma'able device) */
287         u64             coherent_dma_mask;/* Like dma_mask, but for
288                                              alloc_coherent mappings as
289                                              not all hardware supports
290                                              64 bit addresses for consistent
291                                              allocations such descriptors. */
292
293         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
294
295         void    (*release)(struct device * dev);
296 };
297
298 static inline struct device *
299 list_to_dev(struct list_head *node)
300 {
301         return list_entry(node, struct device, node);
302 }
303
304 static inline void *
305 dev_get_drvdata (struct device *dev)
306 {
307         return dev->driver_data;
308 }
309
310 static inline void
311 dev_set_drvdata (struct device *dev, void *data)
312 {
313         dev->driver_data = data;
314 }
315
316 /*
317  * High level routines for use by the bus drivers
318  */
319 extern int device_register(struct device * dev);
320 extern void device_unregister(struct device * dev);
321 extern void device_initialize(struct device * dev);
322 extern int device_add(struct device * dev);
323 extern void device_del(struct device * dev);
324 extern int device_for_each_child(struct device *, void *,
325                      int (*fn)(struct device *, void *));
326
327 /*
328  * Manual binding of a device to driver. See drivers/base/bus.c 
329  * for information on use.
330  */
331 extern void device_bind_driver(struct device * dev);
332 extern void device_release_driver(struct device * dev);
333 extern void driver_attach(struct device_driver * drv);
334
335
336 /* driverfs interface for exporting device attributes */
337
338 struct device_attribute {
339         struct attribute        attr;
340         ssize_t (*show)(struct device * dev, char * buf);
341         ssize_t (*store)(struct device * dev, const char * buf, size_t count);
342 };
343
344 #define DEVICE_ATTR(_name,_mode,_show,_store) \
345 struct device_attribute dev_attr_##_name = {            \
346         .attr = {.name = __stringify(_name), .mode = _mode, .owner = THIS_MODULE },     \
347         .show   = _show,                                \
348         .store  = _store,                               \
349 };
350
351
352 extern int device_create_file(struct device *device, struct device_attribute * entry);
353 extern void device_remove_file(struct device * dev, struct device_attribute * attr);
354
355 /*
356  * Platform "fixup" functions - allow the platform to have their say
357  * about devices and actions that the general device layer doesn't
358  * know about.
359  */
360 /* Notify platform of device discovery */
361 extern int (*platform_notify)(struct device * dev);
362
363 extern int (*platform_notify_remove)(struct device * dev);
364
365
366 /**
367  * get_device - atomically increment the reference count for the device.
368  *
369  */
370 extern struct device * get_device(struct device * dev);
371 extern void put_device(struct device * dev);
372 extern struct device *device_find(const char *name, struct bus_type *bus);
373
374
375 /* drivers/base/platform.c */
376
377 struct platform_device {
378         char            * name;
379         u32             id;
380         struct device   dev;
381         u32             num_resources;
382         struct resource * resource;
383 };
384
385 #define to_platform_device(x) container_of((x), struct platform_device, dev)
386
387 extern int platform_device_register(struct platform_device *);
388 extern void platform_device_unregister(struct platform_device *);
389
390 extern struct bus_type platform_bus_type;
391 extern struct device platform_bus;
392
393 /* drivers/base/power.c */
394 extern void device_shutdown(void);
395
396
397 /* drivers/base/firmware.c */
398 extern int firmware_register(struct subsystem *);
399 extern void firmware_unregister(struct subsystem *);
400
401 /* debugging and troubleshooting/diagnostic helpers. */
402 #define dev_printk(level, dev, format, arg...)  \
403         printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
404
405 #ifdef DEBUG
406 #define dev_dbg(dev, format, arg...)            \
407         dev_printk(KERN_DEBUG , dev , format , ## arg)
408 #else
409 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
410 #endif
411
412 #define dev_err(dev, format, arg...)            \
413         dev_printk(KERN_ERR , dev , format , ## arg)
414 #define dev_info(dev, format, arg...)           \
415         dev_printk(KERN_INFO , dev , format , ## arg)
416 #define dev_warn(dev, format, arg...)           \
417         dev_printk(KERN_WARNING , dev , format , ## arg)
418
419 /* Create alias, so I can be autoloaded. */
420 #define MODULE_ALIAS_CHARDEV(major,minor) \
421         MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
422 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
423         MODULE_ALIAS("char-major-" __stringify(major) "-*")
424 #endif /* _DEVICE_H_ */