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