]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/device.h
[PATCH] Add a klist to struct bus_type for its devices.
[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  *
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
57         struct bus_attribute    * bus_attrs;
58         struct device_attribute * dev_attrs;
59         struct driver_attribute * drv_attrs;
60
61         int             (*match)(struct device * dev, struct device_driver * drv);
62         int             (*hotplug) (struct device *dev, char **envp, 
63                                     int num_envp, char *buffer, int buffer_size);
64         int             (*suspend)(struct device * dev, pm_message_t state);
65         int             (*resume)(struct device * dev);
66 };
67
68 extern int bus_register(struct bus_type * bus);
69 extern void bus_unregister(struct bus_type * bus);
70
71 extern int bus_rescan_devices(struct bus_type * bus);
72
73 extern struct bus_type * get_bus(struct bus_type * bus);
74 extern void put_bus(struct bus_type * bus);
75
76 extern struct bus_type * find_bus(char * name);
77
78 /* iterator helpers for buses */
79
80 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
81                      int (*fn)(struct device *, void *));
82
83 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, 
84                      void * data, int (*fn)(struct device_driver *, void *));
85
86
87 /* driverfs interface for exporting bus attributes */
88
89 struct bus_attribute {
90         struct attribute        attr;
91         ssize_t (*show)(struct bus_type *, char * buf);
92         ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
93 };
94
95 #define BUS_ATTR(_name,_mode,_show,_store)      \
96 struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
97
98 extern int bus_create_file(struct bus_type *, struct bus_attribute *);
99 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
100
101 struct device_driver {
102         const char              * name;
103         struct bus_type         * bus;
104
105         struct completion       unloaded;
106         struct kobject          kobj;
107         struct list_head        devices;
108
109         struct module           * owner;
110
111         int     (*probe)        (struct device * dev);
112         int     (*remove)       (struct device * dev);
113         void    (*shutdown)     (struct device * dev);
114         int     (*suspend)      (struct device * dev, pm_message_t state, u32 level);
115         int     (*resume)       (struct device * dev, u32 level);
116 };
117
118
119 extern int driver_register(struct device_driver * drv);
120 extern void driver_unregister(struct device_driver * drv);
121
122 extern struct device_driver * get_driver(struct device_driver * drv);
123 extern void put_driver(struct device_driver * drv);
124 extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
125
126
127 /* driverfs interface for exporting driver attributes */
128
129 struct driver_attribute {
130         struct attribute        attr;
131         ssize_t (*show)(struct device_driver *, char * buf);
132         ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
133 };
134
135 #define DRIVER_ATTR(_name,_mode,_show,_store)   \
136 struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
137
138 extern int driver_create_file(struct device_driver *, struct driver_attribute *);
139 extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
140
141 extern int driver_for_each_device(struct device_driver * drv, struct device * start,
142                                   void * data, int (*fn)(struct device *, void *));
143
144
145 /*
146  * device classes
147  */
148 struct class {
149         const char              * name;
150         struct module           * owner;
151
152         struct subsystem        subsys;
153         struct list_head        children;
154         struct list_head        interfaces;
155         struct semaphore        sem;    /* locks both the children and interfaces lists */
156
157         struct class_attribute          * class_attrs;
158         struct class_device_attribute   * class_dev_attrs;
159
160         int     (*hotplug)(struct class_device *dev, char **envp, 
161                            int num_envp, char *buffer, int buffer_size);
162
163         void    (*release)(struct class_device *dev);
164         void    (*class_release)(struct class *class);
165 };
166
167 extern int class_register(struct class *);
168 extern void class_unregister(struct class *);
169
170 extern struct class * class_get(struct class *);
171 extern void class_put(struct class *);
172
173
174 struct class_attribute {
175         struct attribute        attr;
176         ssize_t (*show)(struct class *, char * buf);
177         ssize_t (*store)(struct class *, const char * buf, size_t count);
178 };
179
180 #define CLASS_ATTR(_name,_mode,_show,_store)                    \
181 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store) 
182
183 extern int class_create_file(struct class *, const struct class_attribute *);
184 extern void class_remove_file(struct class *, const struct class_attribute *);
185
186
187 struct class_device {
188         struct list_head        node;
189
190         struct kobject          kobj;
191         struct class            * class;        /* required */
192         dev_t                   devt;           /* dev_t, creates the sysfs "dev" */
193         struct class_device_attribute *devt_attr;
194         struct device           * dev;          /* not necessary, but nice to have */
195         void                    * class_data;   /* class-specific data */
196
197         char    class_id[BUS_ID_SIZE];  /* unique to this class */
198 };
199
200 static inline void *
201 class_get_devdata (struct class_device *dev)
202 {
203         return dev->class_data;
204 }
205
206 static inline void
207 class_set_devdata (struct class_device *dev, void *data)
208 {
209         dev->class_data = data;
210 }
211
212
213 extern int class_device_register(struct class_device *);
214 extern void class_device_unregister(struct class_device *);
215 extern void class_device_initialize(struct class_device *);
216 extern int class_device_add(struct class_device *);
217 extern void class_device_del(struct class_device *);
218
219 extern int class_device_rename(struct class_device *, char *);
220
221 extern struct class_device * class_device_get(struct class_device *);
222 extern void class_device_put(struct class_device *);
223
224 struct class_device_attribute {
225         struct attribute        attr;
226         ssize_t (*show)(struct class_device *, char * buf);
227         ssize_t (*store)(struct class_device *, const char * buf, size_t count);
228 };
229
230 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store)             \
231 struct class_device_attribute class_device_attr_##_name =       \
232         __ATTR(_name,_mode,_show,_store)
233
234 extern int class_device_create_file(struct class_device *, 
235                                     const struct class_device_attribute *);
236 extern void class_device_remove_file(struct class_device *, 
237                                      const struct class_device_attribute *);
238 extern int class_device_create_bin_file(struct class_device *,
239                                         struct bin_attribute *);
240 extern void class_device_remove_bin_file(struct class_device *,
241                                          struct bin_attribute *);
242
243 struct class_interface {
244         struct list_head        node;
245         struct class            *class;
246
247         int (*add)      (struct class_device *);
248         void (*remove)  (struct class_device *);
249 };
250
251 extern int class_interface_register(struct class_interface *);
252 extern void class_interface_unregister(struct class_interface *);
253
254 extern struct class *class_create(struct module *owner, char *name);
255 extern void class_destroy(struct class *cls);
256 extern struct class_device *class_device_create(struct class *cls, dev_t devt,
257                                                 struct device *device, char *fmt, ...)
258                                         __attribute__((format(printf,4,5)));
259 extern void class_device_destroy(struct class *cls, dev_t devt);
260
261
262 struct device {
263         struct list_head node;          /* node in sibling list */
264         struct list_head bus_list;      /* node in bus's list */
265         struct list_head driver_list;
266         struct list_head children;
267         struct klist_node       knode_bus;
268         struct device   * parent;
269
270         struct kobject kobj;
271         char    bus_id[BUS_ID_SIZE];    /* position on parent bus */
272
273         struct semaphore        sem;    /* semaphore to synchronize calls to
274                                          * its driver.
275                                          */
276
277         struct bus_type * bus;          /* type of bus device is on */
278         struct device_driver *driver;   /* which driver has allocated this
279                                            device */
280         void            *driver_data;   /* data private to the driver */
281         void            *platform_data; /* Platform specific data (e.g. ACPI,
282                                            BIOS data relevant to device) */
283         struct dev_pm_info      power;
284
285         u64             *dma_mask;      /* dma mask (if dma'able device) */
286         u64             coherent_dma_mask;/* Like dma_mask, but for
287                                              alloc_coherent mappings as
288                                              not all hardware supports
289                                              64 bit addresses for consistent
290                                              allocations such descriptors. */
291
292         struct list_head        dma_pools;      /* dma pools (if dma'ble) */
293
294         struct dma_coherent_mem *dma_mem; /* internal for coherent mem
295                                              override */
296
297         void    (*release)(struct device * dev);
298 };
299
300 static inline struct device *
301 list_to_dev(struct list_head *node)
302 {
303         return list_entry(node, struct device, node);
304 }
305
306 static inline void *
307 dev_get_drvdata (struct device *dev)
308 {
309         return dev->driver_data;
310 }
311
312 static inline void
313 dev_set_drvdata (struct device *dev, void *data)
314 {
315         dev->driver_data = data;
316 }
317
318 /*
319  * High level routines for use by the bus drivers
320  */
321 extern int device_register(struct device * dev);
322 extern void device_unregister(struct device * dev);
323 extern void device_initialize(struct device * dev);
324 extern int device_add(struct device * dev);
325 extern void device_del(struct device * dev);
326 extern int device_for_each_child(struct device *, void *,
327                      int (*fn)(struct device *, void *));
328
329 /*
330  * Manual binding of a device to driver. See drivers/base/bus.c
331  * for information on use.
332  */
333 extern int  driver_probe_device(struct device_driver * drv, struct device * dev);
334 extern void device_bind_driver(struct device * dev);
335 extern void device_release_driver(struct device * dev);
336 extern int  device_attach(struct device * dev);
337 extern void driver_attach(struct device_driver * drv);
338
339
340 /* driverfs interface for exporting device attributes */
341
342 struct device_attribute {
343         struct attribute        attr;
344         ssize_t (*show)(struct device * dev, char * buf);
345         ssize_t (*store)(struct device * dev, const char * buf, size_t count);
346 };
347
348 #define DEVICE_ATTR(_name,_mode,_show,_store) \
349 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
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         const 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 extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
394 extern int platform_get_irq(struct platform_device *, unsigned int);
395 extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
396 extern int platform_get_irq_byname(struct platform_device *, char *);
397 extern int platform_add_devices(struct platform_device **, int);
398
399 extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
400
401 /* drivers/base/power.c */
402 extern void device_shutdown(void);
403
404
405 /* drivers/base/firmware.c */
406 extern int firmware_register(struct subsystem *);
407 extern void firmware_unregister(struct subsystem *);
408
409 /* debugging and troubleshooting/diagnostic helpers. */
410 #define dev_printk(level, dev, format, arg...)  \
411         printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
412
413 #ifdef DEBUG
414 #define dev_dbg(dev, format, arg...)            \
415         dev_printk(KERN_DEBUG , dev , format , ## arg)
416 #else
417 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
418 #endif
419
420 #define dev_err(dev, format, arg...)            \
421         dev_printk(KERN_ERR , dev , format , ## arg)
422 #define dev_info(dev, format, arg...)           \
423         dev_printk(KERN_INFO , dev , format , ## arg)
424 #define dev_warn(dev, format, arg...)           \
425         dev_printk(KERN_WARNING , dev , format , ## arg)
426
427 /* Create alias, so I can be autoloaded. */
428 #define MODULE_ALIAS_CHARDEV(major,minor) \
429         MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
430 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
431         MODULE_ALIAS("char-major-" __stringify(major) "-*")
432 #endif /* _DEVICE_H_ */