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/klist.h>
18 #include <linux/list.h>
19 #include <linux/types.h>
20 #include <linux/module.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
52 struct subsystem subsys;
55 struct klist klist_devices;
56 struct klist klist_drivers;
58 struct bus_attribute * bus_attrs;
59 struct device_attribute * dev_attrs;
60 struct driver_attribute * drv_attrs;
62 int (*match)(struct device * dev, struct device_driver * drv);
63 int (*hotplug) (struct device *dev, char **envp,
64 int num_envp, char *buffer, int buffer_size);
65 int (*suspend)(struct device * dev, pm_message_t state);
66 int (*resume)(struct device * dev);
69 extern int bus_register(struct bus_type * bus);
70 extern void bus_unregister(struct bus_type * bus);
72 extern void bus_rescan_devices(struct bus_type * bus);
74 extern struct bus_type * get_bus(struct bus_type * bus);
75 extern void put_bus(struct bus_type * bus);
77 extern struct bus_type * find_bus(char * name);
79 /* iterator helpers for buses */
81 int bus_for_each_dev(struct bus_type * bus, struct device * start, void * data,
82 int (*fn)(struct device *, void *));
83 struct device * bus_find_device(struct bus_type *bus, struct device *start,
84 void *data, int (*match)(struct device *, void *));
86 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
87 void * data, int (*fn)(struct device_driver *, void *));
90 /* driverfs interface for exporting bus attributes */
92 struct bus_attribute {
93 struct attribute attr;
94 ssize_t (*show)(struct bus_type *, char * buf);
95 ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
98 #define BUS_ATTR(_name,_mode,_show,_store) \
99 struct bus_attribute bus_attr_##_name = __ATTR(_name,_mode,_show,_store)
101 extern int bus_create_file(struct bus_type *, struct bus_attribute *);
102 extern void bus_remove_file(struct bus_type *, struct bus_attribute *);
104 struct device_driver {
106 struct bus_type * bus;
108 struct completion unloaded;
110 struct klist klist_devices;
111 struct klist_node knode_bus;
113 struct module * owner;
115 int (*probe) (struct device * dev);
116 int (*remove) (struct device * dev);
117 void (*shutdown) (struct device * dev);
118 int (*suspend) (struct device * dev, pm_message_t state, u32 level);
119 int (*resume) (struct device * dev, u32 level);
123 extern int driver_register(struct device_driver * drv);
124 extern void driver_unregister(struct device_driver * drv);
126 extern struct device_driver * get_driver(struct device_driver * drv);
127 extern void put_driver(struct device_driver * drv);
128 extern struct device_driver *driver_find(const char *name, struct bus_type *bus);
131 /* driverfs interface for exporting driver attributes */
133 struct driver_attribute {
134 struct attribute attr;
135 ssize_t (*show)(struct device_driver *, char * buf);
136 ssize_t (*store)(struct device_driver *, const char * buf, size_t count);
139 #define DRIVER_ATTR(_name,_mode,_show,_store) \
140 struct driver_attribute driver_attr_##_name = __ATTR(_name,_mode,_show,_store)
142 extern int driver_create_file(struct device_driver *, struct driver_attribute *);
143 extern void driver_remove_file(struct device_driver *, struct driver_attribute *);
145 extern int driver_for_each_device(struct device_driver * drv, struct device * start,
146 void * data, int (*fn)(struct device *, void *));
147 struct device * driver_find_device(struct device_driver *drv,
148 struct device *start, void *data,
149 int (*match)(struct device *, void *));
157 struct module * owner;
159 struct subsystem subsys;
160 struct list_head children;
161 struct list_head interfaces;
162 struct semaphore sem; /* locks both the children and interfaces lists */
164 struct class_attribute * class_attrs;
165 struct class_device_attribute * class_dev_attrs;
167 int (*hotplug)(struct class_device *dev, char **envp,
168 int num_envp, char *buffer, int buffer_size);
170 void (*release)(struct class_device *dev);
171 void (*class_release)(struct class *class);
174 extern int class_register(struct class *);
175 extern void class_unregister(struct class *);
177 extern struct class * class_get(struct class *);
178 extern void class_put(struct class *);
181 struct class_attribute {
182 struct attribute attr;
183 ssize_t (*show)(struct class *, char * buf);
184 ssize_t (*store)(struct class *, const char * buf, size_t count);
187 #define CLASS_ATTR(_name,_mode,_show,_store) \
188 struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
190 extern int class_create_file(struct class *, const struct class_attribute *);
191 extern void class_remove_file(struct class *, const struct class_attribute *);
194 struct class_device {
195 struct list_head node;
198 struct class * class; /* required */
199 dev_t devt; /* dev_t, creates the sysfs "dev" */
200 struct class_device_attribute *devt_attr;
201 struct device * dev; /* not necessary, but nice to have */
202 void * class_data; /* class-specific data */
204 char class_id[BUS_ID_SIZE]; /* unique to this class */
208 class_get_devdata (struct class_device *dev)
210 return dev->class_data;
214 class_set_devdata (struct class_device *dev, void *data)
216 dev->class_data = data;
220 extern int class_device_register(struct class_device *);
221 extern void class_device_unregister(struct class_device *);
222 extern void class_device_initialize(struct class_device *);
223 extern int class_device_add(struct class_device *);
224 extern void class_device_del(struct class_device *);
226 extern int class_device_rename(struct class_device *, char *);
228 extern struct class_device * class_device_get(struct class_device *);
229 extern void class_device_put(struct class_device *);
231 struct class_device_attribute {
232 struct attribute attr;
233 ssize_t (*show)(struct class_device *, char * buf);
234 ssize_t (*store)(struct class_device *, const char * buf, size_t count);
237 #define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
238 struct class_device_attribute class_device_attr_##_name = \
239 __ATTR(_name,_mode,_show,_store)
241 extern int class_device_create_file(struct class_device *,
242 const struct class_device_attribute *);
243 extern void class_device_remove_file(struct class_device *,
244 const struct class_device_attribute *);
245 extern int class_device_create_bin_file(struct class_device *,
246 struct bin_attribute *);
247 extern void class_device_remove_bin_file(struct class_device *,
248 struct bin_attribute *);
250 struct class_interface {
251 struct list_head node;
254 int (*add) (struct class_device *);
255 void (*remove) (struct class_device *);
258 extern int class_interface_register(struct class_interface *);
259 extern void class_interface_unregister(struct class_interface *);
261 extern struct class *class_create(struct module *owner, char *name);
262 extern void class_destroy(struct class *cls);
263 extern struct class_device *class_device_create(struct class *cls, dev_t devt,
264 struct device *device, char *fmt, ...)
265 __attribute__((format(printf,4,5)));
266 extern void class_device_destroy(struct class *cls, dev_t devt);
270 struct klist klist_children;
271 struct klist_node knode_parent; /* node in sibling list */
272 struct klist_node knode_driver;
273 struct klist_node knode_bus;
274 struct device * parent;
277 char bus_id[BUS_ID_SIZE]; /* position on parent bus */
279 struct semaphore sem; /* semaphore to synchronize calls to
283 struct bus_type * bus; /* type of bus device is on */
284 struct device_driver *driver; /* which driver has allocated this
286 void *driver_data; /* data private to the driver */
287 void *platform_data; /* Platform specific data, device
288 core doesn't touch it */
289 void *firmware_data; /* Firmware specific data (e.g. ACPI,
290 BIOS data),reserved for device core*/
291 struct dev_pm_info power;
293 u64 *dma_mask; /* dma mask (if dma'able device) */
294 u64 coherent_dma_mask;/* Like dma_mask, but for
295 alloc_coherent mappings as
296 not all hardware supports
297 64 bit addresses for consistent
298 allocations such descriptors. */
300 struct list_head dma_pools; /* dma pools (if dma'ble) */
302 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
305 void (*release)(struct device * dev);
309 dev_get_drvdata (struct device *dev)
311 return dev->driver_data;
315 dev_set_drvdata (struct device *dev, void *data)
317 dev->driver_data = data;
321 * High level routines for use by the bus drivers
323 extern int device_register(struct device * dev);
324 extern void device_unregister(struct device * dev);
325 extern void device_initialize(struct device * dev);
326 extern int device_add(struct device * dev);
327 extern void device_del(struct device * dev);
328 extern int device_for_each_child(struct device *, void *,
329 int (*fn)(struct device *, void *));
332 * Manual binding of a device to driver. See drivers/base/bus.c
333 * for information on use.
335 extern void device_bind_driver(struct device * dev);
336 extern void device_release_driver(struct device * dev);
337 extern int device_attach(struct device * dev);
338 extern void driver_attach(struct device_driver * drv);
341 /* driverfs interface for exporting device attributes */
343 struct device_attribute {
344 struct attribute attr;
345 ssize_t (*show)(struct device *dev, struct device_attribute *attr,
347 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
348 const char *buf, size_t count);
351 #define DEVICE_ATTR(_name,_mode,_show,_store) \
352 struct device_attribute dev_attr_##_name = __ATTR(_name,_mode,_show,_store)
355 extern int device_create_file(struct device *device, struct device_attribute * entry);
356 extern void device_remove_file(struct device * dev, struct device_attribute * attr);
359 * Platform "fixup" functions - allow the platform to have their say
360 * about devices and actions that the general device layer doesn't
363 /* Notify platform of device discovery */
364 extern int (*platform_notify)(struct device * dev);
366 extern int (*platform_notify_remove)(struct device * dev);
370 * get_device - atomically increment the reference count for the device.
373 extern struct device * get_device(struct device * dev);
374 extern void put_device(struct device * dev);
377 /* drivers/base/platform.c */
379 struct platform_device {
384 struct resource * resource;
387 #define to_platform_device(x) container_of((x), struct platform_device, dev)
389 extern int platform_device_register(struct platform_device *);
390 extern void platform_device_unregister(struct platform_device *);
392 extern struct bus_type platform_bus_type;
393 extern struct device platform_bus;
395 extern struct resource *platform_get_resource(struct platform_device *, unsigned int, unsigned int);
396 extern int platform_get_irq(struct platform_device *, unsigned int);
397 extern struct resource *platform_get_resource_byname(struct platform_device *, unsigned int, char *);
398 extern int platform_get_irq_byname(struct platform_device *, char *);
399 extern int platform_add_devices(struct platform_device **, int);
401 extern struct platform_device *platform_device_register_simple(char *, unsigned int, struct resource *, unsigned int);
403 /* drivers/base/power.c */
404 extern void device_shutdown(void);
407 /* drivers/base/firmware.c */
408 extern int firmware_register(struct subsystem *);
409 extern void firmware_unregister(struct subsystem *);
411 /* debugging and troubleshooting/diagnostic helpers. */
412 #define dev_printk(level, dev, format, arg...) \
413 printk(level "%s %s: " format , (dev)->driver ? (dev)->driver->name : "" , (dev)->bus_id , ## arg)
416 #define dev_dbg(dev, format, arg...) \
417 dev_printk(KERN_DEBUG , dev , format , ## arg)
419 #define dev_dbg(dev, format, arg...) do { (void)(dev); } while (0)
422 #define dev_err(dev, format, arg...) \
423 dev_printk(KERN_ERR , dev , format , ## arg)
424 #define dev_info(dev, format, arg...) \
425 dev_printk(KERN_INFO , dev , format , ## arg)
426 #define dev_warn(dev, format, arg...) \
427 dev_printk(KERN_WARNING , dev , format , ## arg)
429 /* Create alias, so I can be autoloaded. */
430 #define MODULE_ALIAS_CHARDEV(major,minor) \
431 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor))
432 #define MODULE_ALIAS_CHARDEV_MAJOR(major) \
433 MODULE_ALIAS("char-major-" __stringify(major) "-*")
434 #endif /* _DEVICE_H_ */