MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / include / linux / device.h
blob4bb694c6737a794a5ff5679ca9242a38be369a98
1 /*
2 * device.h - generic, centralized driver model
4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6 * This file is released under the GPLv2
8 * See Documentation/driver-model/ for more information.
9 */
11 #ifndef _DEVICE_H_
12 #define _DEVICE_H_
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>
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
31 enum {
32 SUSPEND_NOTIFY,
33 SUSPEND_SAVE_STATE,
34 SUSPEND_DISABLE,
35 SUSPEND_POWER_DOWN,
38 enum {
39 RESUME_POWER_ON,
40 RESUME_RESTORE_STATE,
41 RESUME_ENABLE,
44 struct device;
45 struct device_driver;
46 struct class;
47 struct class_device;
48 struct class_simple;
50 struct bus_type {
51 char * name;
53 struct subsystem subsys;
54 struct kset drivers;
55 struct kset devices;
57 struct bus_attribute * bus_attrs;
58 struct device_attribute * dev_attrs;
59 struct driver_attribute * drv_attrs;
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, u32 state);
65 int (*resume)(struct device * dev);
68 extern int bus_register(struct bus_type * bus);
69 extern void bus_unregister(struct bus_type * bus);
71 extern int bus_rescan_devices(struct bus_type * bus);
73 extern struct bus_type * get_bus(struct bus_type * bus);
74 extern void put_bus(struct bus_type * bus);
76 extern struct bus_type * find_bus(char * name);
78 /* iterator helpers for buses */
80 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
81 int (*fn)(struct device *, void *));
83 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
84 void * data, int (*fn)(struct device_driver *, void *));
87 /* driverfs interface for exporting bus attributes */
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);
95 #define BUS_ATTR(_name,_mode,_show,_store) \
96 struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
98 extern int bus_create_file(struct bus_type *, struct bus_attribute *);
99 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
101 struct device_driver {
102 char * name;
103 struct bus_type * bus;
105 struct semaphore unload_sem;
106 struct kobject kobj;
107 struct list_head devices;
109 int (*probe) (struct device * dev);
110 int (*remove) (struct device * dev);
111 void (*shutdown) (struct device * dev);
112 int (*suspend) (struct device * dev, u32 state, u32 level);
113 int (*resume) (struct device * dev, u32 level);
117 extern int driver_register(struct device_driver * drv);
118 extern void driver_unregister(struct device_driver * drv);
120 extern struct device_driver * get_driver(struct device_driver * drv);
121 extern void put_driver(struct device_driver * drv);
122 extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
125 /* driverfs interface for exporting driver attributes */
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);
133 #define DRIVER_ATTR(_name,_mode,_show,_store) \
134 struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
136 extern int driver_create_file(struct device_driver *, struct driver_attribute *);
137 extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
141 * device classes
143 struct class {
144 char * name;
146 struct subsystem subsys;
147 struct list_head children;
148 struct list_head interfaces;
150 struct class_attribute * class_attrs;
151 struct class_device_attribute * class_dev_attrs;
153 int (*hotplug)(struct class_device *dev, char **envp,
154 int num_envp, char *buffer, int buffer_size);
156 void (*release)(struct class_device *dev);
157 void (*class_release)(struct class *class);
160 extern int class_register(struct class *);
161 extern void class_unregister(struct class *);
163 extern struct class * class_get(struct class *);
164 extern void class_put(struct class *);
167 struct class_attribute {
168 struct attribute attr;
169 ssize_t (*show)(struct class *, char * buf);
170 ssize_t (*store)(struct class *, const char * buf, size_t count);
173 #define CLASS_ATTR(_name,_mode,_show,_store) \
174 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
176 extern int class_create_file(struct class *, const struct class_attribute *);
177 extern void class_remove_file(struct class *, const struct class_attribute *);
180 struct class_device {
181 struct list_head node;
183 struct kobject kobj;
184 struct class * class; /* required */
185 struct device * dev; /* not necessary, but nice to have */
186 void * class_data; /* class-specific data */
188 char class_id[BUS_ID_SIZE]; /* unique to this class */
191 static inline void *
192 class_get_devdata (struct class_device *dev)
194 return dev->class_data;
197 static inline void
198 class_set_devdata (struct class_device *dev, void *data)
200 dev->class_data = data;
204 extern int class_device_register(struct class_device *);
205 extern void class_device_unregister(struct class_device *);
206 extern void class_device_initialize(struct class_device *);
207 extern int class_device_add(struct class_device *);
208 extern void class_device_del(struct class_device *);
210 extern int class_device_rename(struct class_device *, char *);
212 extern struct class_device * class_device_get(struct class_device *);
213 extern void class_device_put(struct class_device *);
215 struct class_device_attribute {
216 struct attribute attr;
217 ssize_t (*show)(struct class_device *, char * buf);
218 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
221 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
222 struct class_device_attribute class_device_attr_##_name = \
223 __ATTR(_name,_mode,_show,_store)
225 extern int class_device_create_file(struct class_device *,
226 const struct class_device_attribute *);
227 extern void class_device_remove_file(struct class_device *,
228 const struct class_device_attribute *);
231 struct class_interface {
232 struct list_head node;
233 struct class *class;
235 int (*add) (struct class_device *);
236 void (*remove) (struct class_device *);
239 extern int class_interface_register(struct class_interface *);
240 extern void class_interface_unregister(struct class_interface *);
242 /* interface for class simple stuff */
243 extern struct class_simple *class_simple_create(struct module *owner, char *name);
244 extern void class_simple_destroy(struct class_simple *cs);
245 extern struct class_device *class_simple_device_add(struct class_simple *cs, dev_t dev, struct device *device, const char *fmt, ...)
246 __attribute__((format(printf,4,5)));
247 extern int class_simple_set_hotplug(struct class_simple *,
248 int (*hotplug)(struct class_device *dev, char **envp, int num_envp, char *buffer, int buffer_size));
249 extern void class_simple_device_remove(dev_t dev);
252 struct device {
253 struct list_head node; /* node in sibling list */
254 struct list_head bus_list; /* node in bus's list */
255 struct list_head driver_list;
256 struct list_head children;
257 struct device * parent;
259 struct kobject kobj;
260 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
262 struct bus_type * bus; /* type of bus device is on */
263 struct device_driver *driver; /* which driver has allocated this
264 device */
265 void *driver_data; /* data private to the driver */
266 void *platform_data; /* Platform specific data (e.g. ACPI,
267 BIOS data relevant to device) */
268 struct dev_pm_info power;
269 u32 power_state; /* Current operating state. In
270 ACPI-speak, this is D0-D3, D0
271 being fully functional, and D3
272 being off. */
274 unsigned char *saved_state; /* saved device state */
275 u32 detach_state; /* State to enter when device is
276 detached from its driver. */
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. */
285 struct list_head dma_pools; /* dma pools (if dma'ble) */
287 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
288 override */
290 void (*release)(struct device * dev);
293 static inline struct device *
294 list_to_dev(struct list_head *node)
296 return list_entry(node, struct device, node);
299 static inline void *
300 dev_get_drvdata (struct device *dev)
302 return dev->driver_data;
305 static inline void
306 dev_set_drvdata (struct device *dev, void *data)
308 dev->driver_data = data;
312 * High level routines for use by the bus drivers
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 *));
323 * Manual binding of a device to driver. See drivers/base/bus.c
324 * for information on use.
326 extern void device_bind_driver(struct device * dev);
327 extern void device_release_driver(struct device * dev);
328 extern void driver_attach(struct device_driver * drv);
331 /* driverfs interface for exporting device attributes */
333 struct device_attribute {
334 struct attribute attr;
335 ssize_t (*show)(struct device * dev, char * buf);
336 ssize_t (*store)(struct device * dev, const char * buf, size_t count);
339 #define DEVICE_ATTR(_name,_mode,_show,_store) \
340 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
343 extern int device_create_file(struct device *device, struct device_attribute * entry);
344 extern void device_remove_file(struct device * dev, struct device_attribute * attr);
347 * Platform "fixup" functions - allow the platform to have their say
348 * about devices and actions that the general device layer doesn't
349 * know about.
351 /* Notify platform of device discovery */
352 extern int (*platform_notify)(struct device * dev);
354 extern int (*platform_notify_remove)(struct device * dev);
358 * get_device - atomically increment the reference count for the device.
361 extern struct device * get_device(struct device * dev);
362 extern void put_device(struct device * dev);
363 extern struct device *device_find(const char *name, struct bus_type *bus);
366 /* drivers/base/platform.c */
368 struct platform_device {
369 char * name;
370 u32 id;
371 struct device dev;
372 u32 num_resources;
373 struct resource * resource;
376 #define to_platform_device(x) container_of((x), struct platform_device, dev)
378 extern int platform_device_register(struct platform_device *);
379 extern void platform_device_unregister(struct platform_device *);
381 extern struct bus_type platform_bus_type;
382 extern struct device platform_bus;
384 extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
385 extern int platform_get_irq(struct platform_device *, unsigned int);
386 extern int platform_add_devices(struct platform_device **, int);
388 extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
390 /* drivers/base/power.c */
391 extern void device_shutdown(void);
394 /* drivers/base/firmware.c */
395 extern int firmware_register(struct subsystem *);
396 extern void firmware_unregister(struct subsystem *);
398 /* debugging and troubleshooting/diagnostic helpers. */
399 #define dev_printk(level, dev, format, arg...) \
400 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
402 #ifdef DEBUG
403 #define dev_dbg(dev, format, arg...) \
404 dev_printk(KERN_DEBUG , dev , format , ## arg)
405 #else
406 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
407 #endif
409 #define dev_err(dev, format, arg...) \
410 dev_printk(KERN_ERR , dev , format , ## arg)
411 #define dev_info(dev, format, arg...) \
412 dev_printk(KERN_INFO , dev , format , ## arg)
413 #define dev_warn(dev, format, arg...) \
414 dev_printk(KERN_WARNING , dev , format , ## arg)
416 /* Create alias, so I can be autoloaded. */
417 #define MODULE_ALIAS_CHARDEV(major,minor) \
418 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
419 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
420 MODULE_ALIAS("char-major-" __stringify(major) "-*")
421 #endif /* _DEVICE_H_ */