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.
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>
21 #include <asm/semaphore.h>
22 #include <asm/atomic.h>
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
51 struct subsystem subsys;
55 struct bus_attribute * bus_attrs;
56 struct device_attribute * dev_attrs;
57 struct driver_attribute * drv_attrs;
59 int (*match)(struct device * dev, struct device_driver * drv);
60 int (*hotplug) (struct device *dev, char **envp,
61 int num_envp, char *buffer, int buffer_size);
62 int (*suspend)(struct device * dev, pm_message_t state);
63 int (*resume)(struct device * dev);
66 extern int bus_register(struct bus_type * bus);
67 extern void bus_unregister(struct bus_type * bus);
69 extern int bus_rescan_devices(struct bus_type * bus);
71 extern struct bus_type * get_bus(struct bus_type * bus);
72 extern void put_bus(struct bus_type * bus);
74 extern struct bus_type * find_bus(char * name);
76 /* iterator helpers for buses */
78 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
79 int (*fn)(struct device *, void *));
81 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
82 void * data, int (*fn)(struct device_driver *, void *));
85 /* driverfs interface for exporting bus attributes */
87 struct bus_attribute {
88 struct attribute attr;
89 ssize_t (*show)(struct bus_type *, char * buf);
90 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
93 #define BUS_ATTR(_name,_mode,_show,_store) \
94 struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
96 extern int bus_create_file(struct bus_type *, struct bus_attribute *);
97 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
99 struct device_driver {
101 struct bus_type * bus;
103 struct completion unloaded;
105 struct list_head devices;
107 struct module * owner;
109 int (*probe) (struct device * dev);
110 int (*remove) (struct device * dev);
111 void (*shutdown) (struct device * dev);
112 int (*suspend) (struct device * dev, pm_message_t 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 *);
145 struct module * owner;
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 */
152 struct class_attribute * class_attrs;
153 struct class_device_attribute * class_dev_attrs;
155 int (*hotplug)(struct class_device *dev, char **envp,
156 int num_envp, char *buffer, int buffer_size);
158 void (*release)(struct class_device *dev);
159 void (*class_release)(struct class *class);
162 extern int class_register(struct class *);
163 extern void class_unregister(struct class *);
165 extern struct class * class_get(struct class *);
166 extern void class_put(struct class *);
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);
175 #define CLASS_ATTR(_name,_mode,_show,_store) \
176 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
178 extern int class_create_file(struct class *, const struct class_attribute *);
179 extern void class_remove_file(struct class *, const struct class_attribute *);
182 struct class_device {
183 struct list_head node;
186 struct class * class; /* required */
187 dev_t devt; /* dev_t, creates the sysfs "dev" */
188 struct class_device_attribute *devt_attr;
189 struct device * dev; /* not necessary, but nice to have */
190 void * class_data; /* class-specific data */
192 char class_id[BUS_ID_SIZE]; /* unique to this class */
196 class_get_devdata (struct class_device *dev)
198 return dev->class_data;
202 class_set_devdata (struct class_device *dev, void *data)
204 dev->class_data = data;
208 extern int class_device_register(struct class_device *);
209 extern void class_device_unregister(struct class_device *);
210 extern void class_device_initialize(struct class_device *);
211 extern int class_device_add(struct class_device *);
212 extern void class_device_del(struct class_device *);
214 extern int class_device_rename(struct class_device *, char *);
216 extern struct class_device * class_device_get(struct class_device *);
217 extern void class_device_put(struct class_device *);
219 struct class_device_attribute {
220 struct attribute attr;
221 ssize_t (*show)(struct class_device *, char * buf);
222 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
225 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
226 struct class_device_attribute class_device_attr_##_name = \
227 __ATTR(_name,_mode,_show,_store)
229 extern int class_device_create_file(struct class_device *,
230 const struct class_device_attribute *);
231 extern void class_device_remove_file(struct class_device *,
232 const struct class_device_attribute *);
233 extern int class_device_create_bin_file(struct class_device *,
234 struct bin_attribute *);
235 extern void class_device_remove_bin_file(struct class_device *,
236 struct bin_attribute *);
238 struct class_interface {
239 struct list_head node;
242 int (*add) (struct class_device *);
243 void (*remove) (struct class_device *);
246 extern int class_interface_register(struct class_interface *);
247 extern void class_interface_unregister(struct class_interface *);
249 extern struct class *class_create(struct module *owner, char *name);
250 extern void class_destroy(struct class *cls);
251 extern struct class_device *class_device_create(struct class *cls, dev_t devt,
252 struct device *device, char *fmt, ...)
253 __attribute__((format(printf,4,5)));
254 extern void class_device_destroy(struct class *cls, dev_t devt);
258 struct list_head node; /* node in sibling list */
259 struct list_head bus_list; /* node in bus's list */
260 struct list_head driver_list;
261 struct list_head children;
262 struct device * parent;
265 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
267 struct bus_type * bus; /* type of bus device is on */
268 struct device_driver *driver; /* which driver has allocated this
270 void *driver_data; /* data private to the driver */
271 void *platform_data; /* Platform specific data (e.g. ACPI,
272 BIOS data relevant to device) */
273 struct dev_pm_info power;
275 u64 *dma_mask; /* dma mask (if dma'able device) */
276 u64 coherent_dma_mask;/* Like dma_mask, but for
277 alloc_coherent mappings as
278 not all hardware supports
279 64 bit addresses for consistent
280 allocations such descriptors. */
282 struct list_head dma_pools; /* dma pools (if dma'ble) */
284 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
287 void (*release)(struct device * dev);
290 static inline struct device *
291 list_to_dev(struct list_head *node)
293 return list_entry(node, struct device, node);
297 dev_get_drvdata (struct device *dev)
299 return dev->driver_data;
303 dev_set_drvdata (struct device *dev, void *data)
305 dev->driver_data = data;
309 * High level routines for use by the bus drivers
311 extern int device_register(struct device * dev);
312 extern void device_unregister(struct device * dev);
313 extern void device_initialize(struct device * dev);
314 extern int device_add(struct device * dev);
315 extern void device_del(struct device * dev);
316 extern int device_for_each_child(struct device *, void *,
317 int (*fn)(struct device *, void *));
320 * Manual binding of a device to driver. See drivers/base/bus.c
321 * for information on use.
323 extern int driver_probe_device(struct device_driver * drv, struct device * dev);
324 extern void device_bind_driver(struct device * dev);
325 extern void device_release_driver(struct device * dev);
326 extern int device_attach(struct device * dev);
327 extern void driver_attach(struct device_driver * drv);
330 /* driverfs interface for exporting device attributes */
332 struct device_attribute {
333 struct attribute attr;
334 ssize_t (*show)(struct device * dev, char * buf);
335 ssize_t (*store)(struct device * dev, const char * buf, size_t count);
338 #define DEVICE_ATTR(_name,_mode,_show,_store) \
339 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
342 extern int device_create_file(struct device *device, struct device_attribute * entry);
343 extern void device_remove_file(struct device * dev, struct device_attribute * attr);
346 * Platform "fixup" functions - allow the platform to have their say
347 * about devices and actions that the general device layer doesn't
350 /* Notify platform of device discovery */
351 extern int (*platform_notify)(struct device * dev);
353 extern int (*platform_notify_remove)(struct device * dev);
357 * get_device - atomically increment the reference count for the device.
360 extern struct device * get_device(struct device * dev);
361 extern void put_device(struct device * dev);
362 extern struct device *device_find(const char *name, struct bus_type *bus);
365 /* drivers/base/platform.c */
367 struct platform_device {
372 struct resource * resource;
375 #define to_platform_device(x) container_of((x), struct platform_device, dev)
377 extern int platform_device_register(struct platform_device *);
378 extern void platform_device_unregister(struct platform_device *);
380 extern struct bus_type platform_bus_type;
381 extern struct device platform_bus;
383 extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
384 extern int platform_get_irq(struct platform_device *, unsigned int);
385 extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
386 extern int platform_get_irq_byname(struct platform_device *, char *);
387 extern int platform_add_devices(struct platform_device **, int);
389 extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
391 /* drivers/base/power.c */
392 extern void device_shutdown(void);
395 /* drivers/base/firmware.c */
396 extern int firmware_register(struct subsystem *);
397 extern void firmware_unregister(struct subsystem *);
399 /* debugging and troubleshooting/diagnostic helpers. */
400 #define dev_printk(level, dev, format, arg...) \
401 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
404 #define dev_dbg(dev, format, arg...) \
405 dev_printk(KERN_DEBUG , dev , format , ## arg)
407 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
410 #define dev_err(dev, format, arg...) \
411 dev_printk(KERN_ERR , dev , format , ## arg)
412 #define dev_info(dev, format, arg...) \
413 dev_printk(KERN_INFO , dev , format , ## arg)
414 #define dev_warn(dev, format, arg...) \
415 dev_printk(KERN_WARNING , dev , format , ## arg)
417 /* Create alias, so I can be autoloaded. */
418 #define MODULE_ALIAS_CHARDEV(major,minor) \
419 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
420 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
421 MODULE_ALIAS("char-major-" __stringify(major) "-*")
422 #endif /* _DEVICE_H_ */