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