at v3.0-rc4 32 kB view raw
1/* 2 * device.h - generic, centralized driver model 3 * 4 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org> 5 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de> 6 * Copyright (c) 2008-2009 Novell Inc. 7 * 8 * This file is released under the GPLv2 9 * 10 * See Documentation/driver-model/ for more information. 11 */ 12 13#ifndef _DEVICE_H_ 14#define _DEVICE_H_ 15 16#include <linux/ioport.h> 17#include <linux/kobject.h> 18#include <linux/klist.h> 19#include <linux/list.h> 20#include <linux/lockdep.h> 21#include <linux/compiler.h> 22#include <linux/types.h> 23#include <linux/module.h> 24#include <linux/pm.h> 25#include <asm/atomic.h> 26#include <asm/device.h> 27 28struct device; 29struct device_private; 30struct device_driver; 31struct driver_private; 32struct class; 33struct subsys_private; 34struct bus_type; 35struct device_node; 36 37struct bus_attribute { 38 struct attribute attr; 39 ssize_t (*show)(struct bus_type *bus, char *buf); 40 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count); 41}; 42 43#define BUS_ATTR(_name, _mode, _show, _store) \ 44struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store) 45 46extern int __must_check bus_create_file(struct bus_type *, 47 struct bus_attribute *); 48extern void bus_remove_file(struct bus_type *, struct bus_attribute *); 49 50/** 51 * struct bus_type - The bus type of the device 52 * 53 * @name: The name of the bus. 54 * @bus_attrs: Default attributes of the bus. 55 * @dev_attrs: Default attributes of the devices on the bus. 56 * @drv_attrs: Default attributes of the device drivers on the bus. 57 * @match: Called, perhaps multiple times, whenever a new device or driver 58 * is added for this bus. It should return a nonzero value if the 59 * given device can be handled by the given driver. 60 * @uevent: Called when a device is added, removed, or a few other things 61 * that generate uevents to add the environment variables. 62 * @probe: Called when a new device or driver add to this bus, and callback 63 * the specific driver's probe to initial the matched device. 64 * @remove: Called when a device removed from this bus. 65 * @shutdown: Called at shut-down time to quiesce the device. 66 * @suspend: Called when a device on this bus wants to go to sleep mode. 67 * @resume: Called to bring a device on this bus out of sleep mode. 68 * @pm: Power management operations of this bus, callback the specific 69 * device driver's pm-ops. 70 * @p: The private data of the driver core, only the driver core can 71 * touch this. 72 * 73 * A bus is a channel between the processor and one or more devices. For the 74 * purposes of the device model, all devices are connected via a bus, even if 75 * it is an internal, virtual, "platform" bus. Buses can plug into each other. 76 * A USB controller is usually a PCI device, for example. The device model 77 * represents the actual connections between buses and the devices they control. 78 * A bus is represented by the bus_type structure. It contains the name, the 79 * default attributes, the bus' methods, PM operations, and the driver core's 80 * private data. 81 */ 82struct bus_type { 83 const char *name; 84 struct bus_attribute *bus_attrs; 85 struct device_attribute *dev_attrs; 86 struct driver_attribute *drv_attrs; 87 88 int (*match)(struct device *dev, struct device_driver *drv); 89 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 90 int (*probe)(struct device *dev); 91 int (*remove)(struct device *dev); 92 void (*shutdown)(struct device *dev); 93 94 int (*suspend)(struct device *dev, pm_message_t state); 95 int (*resume)(struct device *dev); 96 97 const struct dev_pm_ops *pm; 98 99 struct subsys_private *p; 100}; 101 102extern int __must_check bus_register(struct bus_type *bus); 103extern void bus_unregister(struct bus_type *bus); 104 105extern int __must_check bus_rescan_devices(struct bus_type *bus); 106 107/* iterator helpers for buses */ 108 109int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, 110 int (*fn)(struct device *dev, void *data)); 111struct device *bus_find_device(struct bus_type *bus, struct device *start, 112 void *data, 113 int (*match)(struct device *dev, void *data)); 114struct device *bus_find_device_by_name(struct bus_type *bus, 115 struct device *start, 116 const char *name); 117 118int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, 119 void *data, int (*fn)(struct device_driver *, void *)); 120 121void bus_sort_breadthfirst(struct bus_type *bus, 122 int (*compare)(const struct device *a, 123 const struct device *b)); 124/* 125 * Bus notifiers: Get notified of addition/removal of devices 126 * and binding/unbinding of drivers to devices. 127 * In the long run, it should be a replacement for the platform 128 * notify hooks. 129 */ 130struct notifier_block; 131 132extern int bus_register_notifier(struct bus_type *bus, 133 struct notifier_block *nb); 134extern int bus_unregister_notifier(struct bus_type *bus, 135 struct notifier_block *nb); 136 137/* All 4 notifers below get called with the target struct device * 138 * as an argument. Note that those functions are likely to be called 139 * with the device lock held in the core, so be careful. 140 */ 141#define BUS_NOTIFY_ADD_DEVICE 0x00000001 /* device added */ 142#define BUS_NOTIFY_DEL_DEVICE 0x00000002 /* device removed */ 143#define BUS_NOTIFY_BIND_DRIVER 0x00000003 /* driver about to be 144 bound */ 145#define BUS_NOTIFY_BOUND_DRIVER 0x00000004 /* driver bound to device */ 146#define BUS_NOTIFY_UNBIND_DRIVER 0x00000005 /* driver about to be 147 unbound */ 148#define BUS_NOTIFY_UNBOUND_DRIVER 0x00000006 /* driver is unbound 149 from the device */ 150 151extern struct kset *bus_get_kset(struct bus_type *bus); 152extern struct klist *bus_get_device_klist(struct bus_type *bus); 153 154/** 155 * struct device_driver - The basic device driver structure 156 * @name: Name of the device driver. 157 * @bus: The bus which the device of this driver belongs to. 158 * @owner: The module owner. 159 * @mod_name: Used for built-in modules. 160 * @suppress_bind_attrs: Disables bind/unbind via sysfs. 161 * @of_match_table: The open firmware table. 162 * @probe: Called to query the existence of a specific device, 163 * whether this driver can work with it, and bind the driver 164 * to a specific device. 165 * @remove: Called when the device is removed from the system to 166 * unbind a device from this driver. 167 * @shutdown: Called at shut-down time to quiesce the device. 168 * @suspend: Called to put the device to sleep mode. Usually to a 169 * low power state. 170 * @resume: Called to bring a device from sleep mode. 171 * @groups: Default attributes that get created by the driver core 172 * automatically. 173 * @pm: Power management operations of the device which matched 174 * this driver. 175 * @p: Driver core's private data, no one other than the driver 176 * core can touch this. 177 * 178 * The device driver-model tracks all of the drivers known to the system. 179 * The main reason for this tracking is to enable the driver core to match 180 * up drivers with new devices. Once drivers are known objects within the 181 * system, however, a number of other things become possible. Device drivers 182 * can export information and configuration variables that are independent 183 * of any specific device. 184 */ 185struct device_driver { 186 const char *name; 187 struct bus_type *bus; 188 189 struct module *owner; 190 const char *mod_name; /* used for built-in modules */ 191 192 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */ 193 194 const struct of_device_id *of_match_table; 195 196 int (*probe) (struct device *dev); 197 int (*remove) (struct device *dev); 198 void (*shutdown) (struct device *dev); 199 int (*suspend) (struct device *dev, pm_message_t state); 200 int (*resume) (struct device *dev); 201 const struct attribute_group **groups; 202 203 const struct dev_pm_ops *pm; 204 205 struct driver_private *p; 206}; 207 208 209extern int __must_check driver_register(struct device_driver *drv); 210extern void driver_unregister(struct device_driver *drv); 211 212extern struct device_driver *get_driver(struct device_driver *drv); 213extern void put_driver(struct device_driver *drv); 214extern struct device_driver *driver_find(const char *name, 215 struct bus_type *bus); 216extern int driver_probe_done(void); 217extern void wait_for_device_probe(void); 218 219 220/* sysfs interface for exporting driver attributes */ 221 222struct driver_attribute { 223 struct attribute attr; 224 ssize_t (*show)(struct device_driver *driver, char *buf); 225 ssize_t (*store)(struct device_driver *driver, const char *buf, 226 size_t count); 227}; 228 229#define DRIVER_ATTR(_name, _mode, _show, _store) \ 230struct driver_attribute driver_attr_##_name = \ 231 __ATTR(_name, _mode, _show, _store) 232 233extern int __must_check driver_create_file(struct device_driver *driver, 234 const struct driver_attribute *attr); 235extern void driver_remove_file(struct device_driver *driver, 236 const struct driver_attribute *attr); 237 238extern int __must_check driver_add_kobj(struct device_driver *drv, 239 struct kobject *kobj, 240 const char *fmt, ...); 241 242extern int __must_check driver_for_each_device(struct device_driver *drv, 243 struct device *start, 244 void *data, 245 int (*fn)(struct device *dev, 246 void *)); 247struct device *driver_find_device(struct device_driver *drv, 248 struct device *start, void *data, 249 int (*match)(struct device *dev, void *data)); 250 251/** 252 * struct class - device classes 253 * @name: Name of the class. 254 * @owner: The module owner. 255 * @class_attrs: Default attributes of this class. 256 * @dev_attrs: Default attributes of the devices belong to the class. 257 * @dev_bin_attrs: Default binary attributes of the devices belong to the class. 258 * @dev_kobj: The kobject that represents this class and links it into the hierarchy. 259 * @dev_uevent: Called when a device is added, removed from this class, or a 260 * few other things that generate uevents to add the environment 261 * variables. 262 * @devnode: Callback to provide the devtmpfs. 263 * @class_release: Called to release this class. 264 * @dev_release: Called to release the device. 265 * @suspend: Used to put the device to sleep mode, usually to a low power 266 * state. 267 * @resume: Used to bring the device from the sleep mode. 268 * @ns_type: Callbacks so sysfs can detemine namespaces. 269 * @namespace: Namespace of the device belongs to this class. 270 * @pm: The default device power management operations of this class. 271 * @p: The private data of the driver core, no one other than the 272 * driver core can touch this. 273 * 274 * A class is a higher-level view of a device that abstracts out low-level 275 * implementation details. Drivers may see a SCSI disk or an ATA disk, but, 276 * at the class level, they are all simply disks. Classes allow user space 277 * to work with devices based on what they do, rather than how they are 278 * connected or how they work. 279 */ 280struct class { 281 const char *name; 282 struct module *owner; 283 284 struct class_attribute *class_attrs; 285 struct device_attribute *dev_attrs; 286 struct bin_attribute *dev_bin_attrs; 287 struct kobject *dev_kobj; 288 289 int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); 290 char *(*devnode)(struct device *dev, mode_t *mode); 291 292 void (*class_release)(struct class *class); 293 void (*dev_release)(struct device *dev); 294 295 int (*suspend)(struct device *dev, pm_message_t state); 296 int (*resume)(struct device *dev); 297 298 const struct kobj_ns_type_operations *ns_type; 299 const void *(*namespace)(struct device *dev); 300 301 const struct dev_pm_ops *pm; 302 303 struct subsys_private *p; 304}; 305 306struct class_dev_iter { 307 struct klist_iter ki; 308 const struct device_type *type; 309}; 310 311extern struct kobject *sysfs_dev_block_kobj; 312extern struct kobject *sysfs_dev_char_kobj; 313extern int __must_check __class_register(struct class *class, 314 struct lock_class_key *key); 315extern void class_unregister(struct class *class); 316 317/* This is a #define to keep the compiler from merging different 318 * instances of the __key variable */ 319#define class_register(class) \ 320({ \ 321 static struct lock_class_key __key; \ 322 __class_register(class, &__key); \ 323}) 324 325struct class_compat; 326struct class_compat *class_compat_register(const char *name); 327void class_compat_unregister(struct class_compat *cls); 328int class_compat_create_link(struct class_compat *cls, struct device *dev, 329 struct device *device_link); 330void class_compat_remove_link(struct class_compat *cls, struct device *dev, 331 struct device *device_link); 332 333extern void class_dev_iter_init(struct class_dev_iter *iter, 334 struct class *class, 335 struct device *start, 336 const struct device_type *type); 337extern struct device *class_dev_iter_next(struct class_dev_iter *iter); 338extern void class_dev_iter_exit(struct class_dev_iter *iter); 339 340extern int class_for_each_device(struct class *class, struct device *start, 341 void *data, 342 int (*fn)(struct device *dev, void *data)); 343extern struct device *class_find_device(struct class *class, 344 struct device *start, void *data, 345 int (*match)(struct device *, void *)); 346 347struct class_attribute { 348 struct attribute attr; 349 ssize_t (*show)(struct class *class, struct class_attribute *attr, 350 char *buf); 351 ssize_t (*store)(struct class *class, struct class_attribute *attr, 352 const char *buf, size_t count); 353}; 354 355#define CLASS_ATTR(_name, _mode, _show, _store) \ 356struct class_attribute class_attr_##_name = __ATTR(_name, _mode, _show, _store) 357 358extern int __must_check class_create_file(struct class *class, 359 const struct class_attribute *attr); 360extern void class_remove_file(struct class *class, 361 const struct class_attribute *attr); 362 363/* Simple class attribute that is just a static string */ 364 365struct class_attribute_string { 366 struct class_attribute attr; 367 char *str; 368}; 369 370/* Currently read-only only */ 371#define _CLASS_ATTR_STRING(_name, _mode, _str) \ 372 { __ATTR(_name, _mode, show_class_attr_string, NULL), _str } 373#define CLASS_ATTR_STRING(_name, _mode, _str) \ 374 struct class_attribute_string class_attr_##_name = \ 375 _CLASS_ATTR_STRING(_name, _mode, _str) 376 377extern ssize_t show_class_attr_string(struct class *class, struct class_attribute *attr, 378 char *buf); 379 380struct class_interface { 381 struct list_head node; 382 struct class *class; 383 384 int (*add_dev) (struct device *, struct class_interface *); 385 void (*remove_dev) (struct device *, struct class_interface *); 386}; 387 388extern int __must_check class_interface_register(struct class_interface *); 389extern void class_interface_unregister(struct class_interface *); 390 391extern struct class * __must_check __class_create(struct module *owner, 392 const char *name, 393 struct lock_class_key *key); 394extern void class_destroy(struct class *cls); 395 396/* This is a #define to keep the compiler from merging different 397 * instances of the __key variable */ 398#define class_create(owner, name) \ 399({ \ 400 static struct lock_class_key __key; \ 401 __class_create(owner, name, &__key); \ 402}) 403 404/* 405 * The type of device, "struct device" is embedded in. A class 406 * or bus can contain devices of different types 407 * like "partitions" and "disks", "mouse" and "event". 408 * This identifies the device type and carries type-specific 409 * information, equivalent to the kobj_type of a kobject. 410 * If "name" is specified, the uevent will contain it in 411 * the DEVTYPE variable. 412 */ 413struct device_type { 414 const char *name; 415 const struct attribute_group **groups; 416 int (*uevent)(struct device *dev, struct kobj_uevent_env *env); 417 char *(*devnode)(struct device *dev, mode_t *mode); 418 void (*release)(struct device *dev); 419 420 const struct dev_pm_ops *pm; 421}; 422 423/* interface for exporting device attributes */ 424struct device_attribute { 425 struct attribute attr; 426 ssize_t (*show)(struct device *dev, struct device_attribute *attr, 427 char *buf); 428 ssize_t (*store)(struct device *dev, struct device_attribute *attr, 429 const char *buf, size_t count); 430}; 431 432#define DEVICE_ATTR(_name, _mode, _show, _store) \ 433struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 434 435extern int __must_check device_create_file(struct device *device, 436 const struct device_attribute *entry); 437extern void device_remove_file(struct device *dev, 438 const struct device_attribute *attr); 439extern int __must_check device_create_bin_file(struct device *dev, 440 const struct bin_attribute *attr); 441extern void device_remove_bin_file(struct device *dev, 442 const struct bin_attribute *attr); 443extern int device_schedule_callback_owner(struct device *dev, 444 void (*func)(struct device *dev), struct module *owner); 445 446/* This is a macro to avoid include problems with THIS_MODULE */ 447#define device_schedule_callback(dev, func) \ 448 device_schedule_callback_owner(dev, func, THIS_MODULE) 449 450/* device resource management */ 451typedef void (*dr_release_t)(struct device *dev, void *res); 452typedef int (*dr_match_t)(struct device *dev, void *res, void *match_data); 453 454#ifdef CONFIG_DEBUG_DEVRES 455extern void *__devres_alloc(dr_release_t release, size_t size, gfp_t gfp, 456 const char *name); 457#define devres_alloc(release, size, gfp) \ 458 __devres_alloc(release, size, gfp, #release) 459#else 460extern void *devres_alloc(dr_release_t release, size_t size, gfp_t gfp); 461#endif 462extern void devres_free(void *res); 463extern void devres_add(struct device *dev, void *res); 464extern void *devres_find(struct device *dev, dr_release_t release, 465 dr_match_t match, void *match_data); 466extern void *devres_get(struct device *dev, void *new_res, 467 dr_match_t match, void *match_data); 468extern void *devres_remove(struct device *dev, dr_release_t release, 469 dr_match_t match, void *match_data); 470extern int devres_destroy(struct device *dev, dr_release_t release, 471 dr_match_t match, void *match_data); 472 473/* devres group */ 474extern void * __must_check devres_open_group(struct device *dev, void *id, 475 gfp_t gfp); 476extern void devres_close_group(struct device *dev, void *id); 477extern void devres_remove_group(struct device *dev, void *id); 478extern int devres_release_group(struct device *dev, void *id); 479 480/* managed kzalloc/kfree for device drivers, no kmalloc, always use kzalloc */ 481extern void *devm_kzalloc(struct device *dev, size_t size, gfp_t gfp); 482extern void devm_kfree(struct device *dev, void *p); 483 484struct device_dma_parameters { 485 /* 486 * a low level driver may set these to teach IOMMU code about 487 * sg limitations. 488 */ 489 unsigned int max_segment_size; 490 unsigned long segment_boundary_mask; 491}; 492 493/** 494 * struct device - The basic device structure 495 * @parent: The device's "parent" device, the device to which it is attached. 496 * In most cases, a parent device is some sort of bus or host 497 * controller. If parent is NULL, the device, is a top-level device, 498 * which is not usually what you want. 499 * @p: Holds the private data of the driver core portions of the device. 500 * See the comment of the struct device_private for detail. 501 * @kobj: A top-level, abstract class from which other classes are derived. 502 * @init_name: Initial name of the device. 503 * @type: The type of device. 504 * This identifies the device type and carries type-specific 505 * information. 506 * @mutex: Mutex to synchronize calls to its driver. 507 * @bus: Type of bus device is on. 508 * @driver: Which driver has allocated this 509 * @platform_data: Platform data specific to the device. 510 * Example: For devices on custom boards, as typical of embedded 511 * and SOC based hardware, Linux often uses platform_data to point 512 * to board-specific structures describing devices and how they 513 * are wired. That can include what ports are available, chip 514 * variants, which GPIO pins act in what additional roles, and so 515 * on. This shrinks the "Board Support Packages" (BSPs) and 516 * minimizes board-specific #ifdefs in drivers. 517 * @power: For device power management. 518 * See Documentation/power/devices.txt for details. 519 * @pwr_domain: Provide callbacks that are executed during system suspend, 520 * hibernation, system resume and during runtime PM transitions 521 * along with subsystem-level and driver-level callbacks. 522 * @numa_node: NUMA node this device is close to. 523 * @dma_mask: Dma mask (if dma'ble device). 524 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 525 * hardware supports 64-bit addresses for consistent allocations 526 * such descriptors. 527 * @dma_parms: A low level driver may set these to teach IOMMU code about 528 * segment limitations. 529 * @dma_pools: Dma pools (if dma'ble device). 530 * @dma_mem: Internal for coherent mem override. 531 * @archdata: For arch-specific additions. 532 * @of_node: Associated device tree node. 533 * @of_match: Matching of_device_id from driver. 534 * @devt: For creating the sysfs "dev". 535 * @devres_lock: Spinlock to protect the resource of the device. 536 * @devres_head: The resources list of the device. 537 * @knode_class: The node used to add the device to the class list. 538 * @class: The class of the device. 539 * @groups: Optional attribute groups. 540 * @release: Callback to free the device after all references have 541 * gone away. This should be set by the allocator of the 542 * device (i.e. the bus driver that discovered the device). 543 * 544 * At the lowest level, every device in a Linux system is represented by an 545 * instance of struct device. The device structure contains the information 546 * that the device model core needs to model the system. Most subsystems, 547 * however, track additional information about the devices they host. As a 548 * result, it is rare for devices to be represented by bare device structures; 549 * instead, that structure, like kobject structures, is usually embedded within 550 * a higher-level representation of the device. 551 */ 552struct device { 553 struct device *parent; 554 555 struct device_private *p; 556 557 struct kobject kobj; 558 const char *init_name; /* initial name of the device */ 559 const struct device_type *type; 560 561 struct mutex mutex; /* mutex to synchronize calls to 562 * its driver. 563 */ 564 565 struct bus_type *bus; /* type of bus device is on */ 566 struct device_driver *driver; /* which driver has allocated this 567 device */ 568 void *platform_data; /* Platform specific data, device 569 core doesn't touch it */ 570 struct dev_pm_info power; 571 struct dev_power_domain *pwr_domain; 572 573#ifdef CONFIG_NUMA 574 int numa_node; /* NUMA node this device is close to */ 575#endif 576 u64 *dma_mask; /* dma mask (if dma'able device) */ 577 u64 coherent_dma_mask;/* Like dma_mask, but for 578 alloc_coherent mappings as 579 not all hardware supports 580 64 bit addresses for consistent 581 allocations such descriptors. */ 582 583 struct device_dma_parameters *dma_parms; 584 585 struct list_head dma_pools; /* dma pools (if dma'ble) */ 586 587 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 588 override */ 589 /* arch specific additions */ 590 struct dev_archdata archdata; 591 592 struct device_node *of_node; /* associated device tree node */ 593 594 dev_t devt; /* dev_t, creates the sysfs "dev" */ 595 596 spinlock_t devres_lock; 597 struct list_head devres_head; 598 599 struct klist_node knode_class; 600 struct class *class; 601 const struct attribute_group **groups; /* optional groups */ 602 603 void (*release)(struct device *dev); 604}; 605 606/* Get the wakeup routines, which depend on struct device */ 607#include <linux/pm_wakeup.h> 608 609static inline const char *dev_name(const struct device *dev) 610{ 611 /* Use the init name until the kobject becomes available */ 612 if (dev->init_name) 613 return dev->init_name; 614 615 return kobject_name(&dev->kobj); 616} 617 618extern int dev_set_name(struct device *dev, const char *name, ...) 619 __attribute__((format(printf, 2, 3))); 620 621#ifdef CONFIG_NUMA 622static inline int dev_to_node(struct device *dev) 623{ 624 return dev->numa_node; 625} 626static inline void set_dev_node(struct device *dev, int node) 627{ 628 dev->numa_node = node; 629} 630#else 631static inline int dev_to_node(struct device *dev) 632{ 633 return -1; 634} 635static inline void set_dev_node(struct device *dev, int node) 636{ 637} 638#endif 639 640static inline unsigned int dev_get_uevent_suppress(const struct device *dev) 641{ 642 return dev->kobj.uevent_suppress; 643} 644 645static inline void dev_set_uevent_suppress(struct device *dev, int val) 646{ 647 dev->kobj.uevent_suppress = val; 648} 649 650static inline int device_is_registered(struct device *dev) 651{ 652 return dev->kobj.state_in_sysfs; 653} 654 655static inline void device_enable_async_suspend(struct device *dev) 656{ 657 if (!dev->power.in_suspend) 658 dev->power.async_suspend = true; 659} 660 661static inline void device_disable_async_suspend(struct device *dev) 662{ 663 if (!dev->power.in_suspend) 664 dev->power.async_suspend = false; 665} 666 667static inline bool device_async_suspend_enabled(struct device *dev) 668{ 669 return !!dev->power.async_suspend; 670} 671 672static inline void device_lock(struct device *dev) 673{ 674 mutex_lock(&dev->mutex); 675} 676 677static inline int device_trylock(struct device *dev) 678{ 679 return mutex_trylock(&dev->mutex); 680} 681 682static inline void device_unlock(struct device *dev) 683{ 684 mutex_unlock(&dev->mutex); 685} 686 687void driver_init(void); 688 689/* 690 * High level routines for use by the bus drivers 691 */ 692extern int __must_check device_register(struct device *dev); 693extern void device_unregister(struct device *dev); 694extern void device_initialize(struct device *dev); 695extern int __must_check device_add(struct device *dev); 696extern void device_del(struct device *dev); 697extern int device_for_each_child(struct device *dev, void *data, 698 int (*fn)(struct device *dev, void *data)); 699extern struct device *device_find_child(struct device *dev, void *data, 700 int (*match)(struct device *dev, void *data)); 701extern int device_rename(struct device *dev, const char *new_name); 702extern int device_move(struct device *dev, struct device *new_parent, 703 enum dpm_order dpm_order); 704extern const char *device_get_devnode(struct device *dev, 705 mode_t *mode, const char **tmp); 706extern void *dev_get_drvdata(const struct device *dev); 707extern int dev_set_drvdata(struct device *dev, void *data); 708 709/* 710 * Root device objects for grouping under /sys/devices 711 */ 712extern struct device *__root_device_register(const char *name, 713 struct module *owner); 714static inline struct device *root_device_register(const char *name) 715{ 716 return __root_device_register(name, THIS_MODULE); 717} 718extern void root_device_unregister(struct device *root); 719 720static inline void *dev_get_platdata(const struct device *dev) 721{ 722 return dev->platform_data; 723} 724 725/* 726 * Manual binding of a device to driver. See drivers/base/bus.c 727 * for information on use. 728 */ 729extern int __must_check device_bind_driver(struct device *dev); 730extern void device_release_driver(struct device *dev); 731extern int __must_check device_attach(struct device *dev); 732extern int __must_check driver_attach(struct device_driver *drv); 733extern int __must_check device_reprobe(struct device *dev); 734 735/* 736 * Easy functions for dynamically creating devices on the fly 737 */ 738extern struct device *device_create_vargs(struct class *cls, 739 struct device *parent, 740 dev_t devt, 741 void *drvdata, 742 const char *fmt, 743 va_list vargs); 744extern struct device *device_create(struct class *cls, struct device *parent, 745 dev_t devt, void *drvdata, 746 const char *fmt, ...) 747 __attribute__((format(printf, 5, 6))); 748extern void device_destroy(struct class *cls, dev_t devt); 749 750/* 751 * Platform "fixup" functions - allow the platform to have their say 752 * about devices and actions that the general device layer doesn't 753 * know about. 754 */ 755/* Notify platform of device discovery */ 756extern int (*platform_notify)(struct device *dev); 757 758extern int (*platform_notify_remove)(struct device *dev); 759 760 761/* 762 * get_device - atomically increment the reference count for the device. 763 * 764 */ 765extern struct device *get_device(struct device *dev); 766extern void put_device(struct device *dev); 767 768extern void wait_for_device_probe(void); 769 770#ifdef CONFIG_DEVTMPFS 771extern int devtmpfs_create_node(struct device *dev); 772extern int devtmpfs_delete_node(struct device *dev); 773extern int devtmpfs_mount(const char *mntdir); 774#else 775static inline int devtmpfs_create_node(struct device *dev) { return 0; } 776static inline int devtmpfs_delete_node(struct device *dev) { return 0; } 777static inline int devtmpfs_mount(const char *mountpoint) { return 0; } 778#endif 779 780/* drivers/base/power/shutdown.c */ 781extern void device_shutdown(void); 782 783/* debugging and troubleshooting/diagnostic helpers. */ 784extern const char *dev_driver_string(const struct device *dev); 785 786 787#ifdef CONFIG_PRINTK 788 789extern int dev_printk(const char *level, const struct device *dev, 790 const char *fmt, ...) 791 __attribute__ ((format (printf, 3, 4))); 792extern int dev_emerg(const struct device *dev, const char *fmt, ...) 793 __attribute__ ((format (printf, 2, 3))); 794extern int dev_alert(const struct device *dev, const char *fmt, ...) 795 __attribute__ ((format (printf, 2, 3))); 796extern int dev_crit(const struct device *dev, const char *fmt, ...) 797 __attribute__ ((format (printf, 2, 3))); 798extern int dev_err(const struct device *dev, const char *fmt, ...) 799 __attribute__ ((format (printf, 2, 3))); 800extern int dev_warn(const struct device *dev, const char *fmt, ...) 801 __attribute__ ((format (printf, 2, 3))); 802extern int dev_notice(const struct device *dev, const char *fmt, ...) 803 __attribute__ ((format (printf, 2, 3))); 804extern int _dev_info(const struct device *dev, const char *fmt, ...) 805 __attribute__ ((format (printf, 2, 3))); 806 807#else 808 809static inline int dev_printk(const char *level, const struct device *dev, 810 const char *fmt, ...) 811 __attribute__ ((format (printf, 3, 4))); 812static inline int dev_printk(const char *level, const struct device *dev, 813 const char *fmt, ...) 814 { return 0; } 815 816static inline int dev_emerg(const struct device *dev, const char *fmt, ...) 817 __attribute__ ((format (printf, 2, 3))); 818static inline int dev_emerg(const struct device *dev, const char *fmt, ...) 819 { return 0; } 820static inline int dev_crit(const struct device *dev, const char *fmt, ...) 821 __attribute__ ((format (printf, 2, 3))); 822static inline int dev_crit(const struct device *dev, const char *fmt, ...) 823 { return 0; } 824static inline int dev_alert(const struct device *dev, const char *fmt, ...) 825 __attribute__ ((format (printf, 2, 3))); 826static inline int dev_alert(const struct device *dev, const char *fmt, ...) 827 { return 0; } 828static inline int dev_err(const struct device *dev, const char *fmt, ...) 829 __attribute__ ((format (printf, 2, 3))); 830static inline int dev_err(const struct device *dev, const char *fmt, ...) 831 { return 0; } 832static inline int dev_warn(const struct device *dev, const char *fmt, ...) 833 __attribute__ ((format (printf, 2, 3))); 834static inline int dev_warn(const struct device *dev, const char *fmt, ...) 835 { return 0; } 836static inline int dev_notice(const struct device *dev, const char *fmt, ...) 837 __attribute__ ((format (printf, 2, 3))); 838static inline int dev_notice(const struct device *dev, const char *fmt, ...) 839 { return 0; } 840static inline int _dev_info(const struct device *dev, const char *fmt, ...) 841 __attribute__ ((format (printf, 2, 3))); 842static inline int _dev_info(const struct device *dev, const char *fmt, ...) 843 { return 0; } 844 845#endif 846 847/* 848 * Stupid hackaround for existing uses of non-printk uses dev_info 849 * 850 * Note that the definition of dev_info below is actually _dev_info 851 * and a macro is used to avoid redefining dev_info 852 */ 853 854#define dev_info(dev, fmt, arg...) _dev_info(dev, fmt, ##arg) 855 856#if defined(DEBUG) 857#define dev_dbg(dev, format, arg...) \ 858 dev_printk(KERN_DEBUG, dev, format, ##arg) 859#elif defined(CONFIG_DYNAMIC_DEBUG) 860#define dev_dbg(dev, format, ...) \ 861do { \ 862 dynamic_dev_dbg(dev, format, ##__VA_ARGS__); \ 863} while (0) 864#else 865#define dev_dbg(dev, format, arg...) \ 866({ \ 867 if (0) \ 868 dev_printk(KERN_DEBUG, dev, format, ##arg); \ 869 0; \ 870}) 871#endif 872 873#ifdef VERBOSE_DEBUG 874#define dev_vdbg dev_dbg 875#else 876#define dev_vdbg(dev, format, arg...) \ 877({ \ 878 if (0) \ 879 dev_printk(KERN_DEBUG, dev, format, ##arg); \ 880 0; \ 881}) 882#endif 883 884/* 885 * dev_WARN*() acts like dev_printk(), but with the key difference 886 * of using a WARN/WARN_ON to get the message out, including the 887 * file/line information and a backtrace. 888 */ 889#define dev_WARN(dev, format, arg...) \ 890 WARN(1, "Device: %s\n" format, dev_driver_string(dev), ## arg); 891 892#define dev_WARN_ONCE(dev, condition, format, arg...) \ 893 WARN_ONCE(condition, "Device %s\n" format, \ 894 dev_driver_string(dev), ## arg) 895 896/* Create alias, so I can be autoloaded. */ 897#define MODULE_ALIAS_CHARDEV(major,minor) \ 898 MODULE_ALIAS("char-major-" __stringify(major) "-" __stringify(minor)) 899#define MODULE_ALIAS_CHARDEV_MAJOR(major) \ 900 MODULE_ALIAS("char-major-" __stringify(major) "-*") 901 902#ifdef CONFIG_SYSFS_DEPRECATED 903extern long sysfs_deprecated; 904#else 905#define sysfs_deprecated 0 906#endif 907 908#endif /* _DEVICE_H_ */