aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@g5.osdl.org>2006-09-26 14:49:46 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2006-09-26 14:49:46 -0400
commitdd77a4ee0f3981693d4229aa1d57cea9e526ff47 (patch)
treecb486be20b950201103a03636cbb1e1d180f0098 /drivers/base
parente8216dee838c09776680a6f1a2e54d81f3cdfa14 (diff)
parent7e9f4b2d3e21e87c26025810413ef1592834e63b (diff)
Merge master.kernel.org:/pub/scm/linux/kernel/git/gregkh/driver-2.6
* master.kernel.org:/pub/scm/linux/kernel/git/gregkh/driver-2.6: (47 commits) Driver core: Don't call put methods while holding a spinlock Driver core: Remove unneeded routines from driver core Driver core: Fix potential deadlock in driver core PCI: enable driver multi-threaded probe Driver Core: add ability for drivers to do a threaded probe sysfs: add proper sysfs_init() prototype drivers/base: check errors drivers/base: Platform notify needs to occur before drivers attach to the device v4l-dev2: handle __must_check add CONFIG_ENABLE_MUST_CHECK add __must_check to device management code Driver core: fixed add_bind_files() definition Driver core: fix comments in drivers/base/power/resume.c sysfs_remove_bin_file: no return value, dump_stack on error kobject: must_check fixes Driver core: add ability for devices to create and remove bin files Class: add support for class interfaces for devices Driver core: create devices/virtual/ tree Driver core: add device_rename function Driver core: add ability for classes to handle devices properly ...
Diffstat (limited to 'drivers/base')
-rw-r--r--drivers/base/base.h2
-rw-r--r--drivers/base/bus.c133
-rw-r--r--drivers/base/class.c34
-rw-r--r--drivers/base/core.c230
-rw-r--r--drivers/base/dd.c147
-rw-r--r--drivers/base/driver.c16
-rw-r--r--drivers/base/platform.c30
-rw-r--r--drivers/base/power/resume.c37
-rw-r--r--drivers/base/power/suspend.c92
-rw-r--r--drivers/base/power/sysfs.c35
10 files changed, 577 insertions, 179 deletions
diff --git a/drivers/base/base.h b/drivers/base/base.h
index c3b8dc98b8a7..d26644a59537 100644
--- a/drivers/base/base.h
+++ b/drivers/base/base.h
@@ -16,7 +16,7 @@ extern int cpu_dev_init(void);
16extern int attribute_container_init(void); 16extern int attribute_container_init(void);
17 17
18extern int bus_add_device(struct device * dev); 18extern int bus_add_device(struct device * dev);
19extern void bus_attach_device(struct device * dev); 19extern int bus_attach_device(struct device * dev);
20extern void bus_remove_device(struct device * dev); 20extern void bus_remove_device(struct device * dev);
21extern struct bus_type *get_bus(struct bus_type * bus); 21extern struct bus_type *get_bus(struct bus_type * bus);
22extern void put_bus(struct bus_type * bus); 22extern void put_bus(struct bus_type * bus);
diff --git a/drivers/base/bus.c b/drivers/base/bus.c
index 2e954d07175a..12173d16bea7 100644
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -371,12 +371,20 @@ int bus_add_device(struct device * dev)
371 if (bus) { 371 if (bus) {
372 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); 372 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
373 error = device_add_attrs(bus, dev); 373 error = device_add_attrs(bus, dev);
374 if (!error) { 374 if (error)
375 sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id); 375 goto out;
376 sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "subsystem"); 376 error = sysfs_create_link(&bus->devices.kobj,
377 sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus"); 377 &dev->kobj, dev->bus_id);
378 } 378 if (error)
379 goto out;
380 error = sysfs_create_link(&dev->kobj,
381 &dev->bus->subsys.kset.kobj, "subsystem");
382 if (error)
383 goto out;
384 error = sysfs_create_link(&dev->kobj,
385 &dev->bus->subsys.kset.kobj, "bus");
379 } 386 }
387out:
380 return error; 388 return error;
381} 389}
382 390
@@ -384,16 +392,24 @@ int bus_add_device(struct device * dev)
384 * bus_attach_device - add device to bus 392 * bus_attach_device - add device to bus
385 * @dev: device tried to attach to a driver 393 * @dev: device tried to attach to a driver
386 * 394 *
395 * - Add device to bus's list of devices.
387 * - Try to attach to driver. 396 * - Try to attach to driver.
388 */ 397 */
389void bus_attach_device(struct device * dev) 398int bus_attach_device(struct device * dev)
390{ 399{
391 struct bus_type * bus = dev->bus; 400 struct bus_type *bus = dev->bus;
401 int ret = 0;
392 402
393 if (bus) { 403 if (bus) {
394 device_attach(dev); 404 dev->is_registered = 1;
395 klist_add_tail(&dev->knode_bus, &bus->klist_devices); 405 ret = device_attach(dev);
406 if (ret >= 0) {
407 klist_add_tail(&dev->knode_bus, &bus->klist_devices);
408 ret = 0;
409 } else
410 dev->is_registered = 0;
396 } 411 }
412 return ret;
397} 413}
398 414
399/** 415/**
@@ -412,7 +428,8 @@ void bus_remove_device(struct device * dev)
412 sysfs_remove_link(&dev->kobj, "bus"); 428 sysfs_remove_link(&dev->kobj, "bus");
413 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id); 429 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
414 device_remove_attrs(dev->bus, dev); 430 device_remove_attrs(dev->bus, dev);
415 klist_remove(&dev->knode_bus); 431 dev->is_registered = 0;
432 klist_del(&dev->knode_bus);
416 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); 433 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
417 device_release_driver(dev); 434 device_release_driver(dev);
418 put_bus(dev->bus); 435 put_bus(dev->bus);
@@ -455,10 +472,17 @@ static void driver_remove_attrs(struct bus_type * bus, struct device_driver * dr
455 * Thanks to drivers making their tables __devinit, we can't allow manual 472 * Thanks to drivers making their tables __devinit, we can't allow manual
456 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled. 473 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
457 */ 474 */
458static void add_bind_files(struct device_driver *drv) 475static int __must_check add_bind_files(struct device_driver *drv)
459{ 476{
460 driver_create_file(drv, &driver_attr_unbind); 477 int ret;
461 driver_create_file(drv, &driver_attr_bind); 478
479 ret = driver_create_file(drv, &driver_attr_unbind);
480 if (ret == 0) {
481 ret = driver_create_file(drv, &driver_attr_bind);
482 if (ret)
483 driver_remove_file(drv, &driver_attr_unbind);
484 }
485 return ret;
462} 486}
463 487
464static void remove_bind_files(struct device_driver *drv) 488static void remove_bind_files(struct device_driver *drv)
@@ -467,7 +491,7 @@ static void remove_bind_files(struct device_driver *drv)
467 driver_remove_file(drv, &driver_attr_unbind); 491 driver_remove_file(drv, &driver_attr_unbind);
468} 492}
469#else 493#else
470static inline void add_bind_files(struct device_driver *drv) {} 494static inline int add_bind_files(struct device_driver *drv) { return 0; }
471static inline void remove_bind_files(struct device_driver *drv) {} 495static inline void remove_bind_files(struct device_driver *drv) {}
472#endif 496#endif
473 497
@@ -476,7 +500,7 @@ static inline void remove_bind_files(struct device_driver *drv) {}
476 * @drv: driver. 500 * @drv: driver.
477 * 501 *
478 */ 502 */
479int bus_add_driver(struct device_driver * drv) 503int bus_add_driver(struct device_driver *drv)
480{ 504{
481 struct bus_type * bus = get_bus(drv->bus); 505 struct bus_type * bus = get_bus(drv->bus);
482 int error = 0; 506 int error = 0;
@@ -484,27 +508,39 @@ int bus_add_driver(struct device_driver * drv)
484 if (bus) { 508 if (bus) {
485 pr_debug("bus %s: add driver %s\n", bus->name, drv->name); 509 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
486 error = kobject_set_name(&drv->kobj, "%s", drv->name); 510 error = kobject_set_name(&drv->kobj, "%s", drv->name);
487 if (error) { 511 if (error)
488 put_bus(bus); 512 goto out_put_bus;
489 return error;
490 }
491 drv->kobj.kset = &bus->drivers; 513 drv->kobj.kset = &bus->drivers;
492 if ((error = kobject_register(&drv->kobj))) { 514 if ((error = kobject_register(&drv->kobj)))
493 put_bus(bus); 515 goto out_put_bus;
494 return error;
495 }
496 516
497 driver_attach(drv); 517 error = driver_attach(drv);
518 if (error)
519 goto out_unregister;
498 klist_add_tail(&drv->knode_bus, &bus->klist_drivers); 520 klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
499 module_add_driver(drv->owner, drv); 521 module_add_driver(drv->owner, drv);
500 522
501 driver_add_attrs(bus, drv); 523 error = driver_add_attrs(bus, drv);
502 add_bind_files(drv); 524 if (error) {
525 /* How the hell do we get out of this pickle? Give up */
526 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
527 __FUNCTION__, drv->name);
528 }
529 error = add_bind_files(drv);
530 if (error) {
531 /* Ditto */
532 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
533 __FUNCTION__, drv->name);
534 }
503 } 535 }
504 return error; 536 return error;
537out_unregister:
538 kobject_unregister(&drv->kobj);
539out_put_bus:
540 put_bus(bus);
541 return error;
505} 542}
506 543
507
508/** 544/**
509 * bus_remove_driver - delete driver from bus's knowledge. 545 * bus_remove_driver - delete driver from bus's knowledge.
510 * @drv: driver. 546 * @drv: driver.
@@ -530,16 +566,21 @@ void bus_remove_driver(struct device_driver * drv)
530 566
531 567
532/* Helper for bus_rescan_devices's iter */ 568/* Helper for bus_rescan_devices's iter */
533static int bus_rescan_devices_helper(struct device *dev, void *data) 569static int __must_check bus_rescan_devices_helper(struct device *dev,
570 void *data)
534{ 571{
572 int ret = 0;
573
535 if (!dev->driver) { 574 if (!dev->driver) {
536 if (dev->parent) /* Needed for USB */ 575 if (dev->parent) /* Needed for USB */
537 down(&dev->parent->sem); 576 down(&dev->parent->sem);
538 device_attach(dev); 577 ret = device_attach(dev);
539 if (dev->parent) 578 if (dev->parent)
540 up(&dev->parent->sem); 579 up(&dev->parent->sem);
580 if (ret > 0)
581 ret = 0;
541 } 582 }
542 return 0; 583 return ret < 0 ? ret : 0;
543} 584}
544 585
545/** 586/**
@@ -550,9 +591,9 @@ static int bus_rescan_devices_helper(struct device *dev, void *data)
550 * attached and rescan it against existing drivers to see if it matches 591 * attached and rescan it against existing drivers to see if it matches
551 * any by calling device_attach() for the unbound devices. 592 * any by calling device_attach() for the unbound devices.
552 */ 593 */
553void bus_rescan_devices(struct bus_type * bus) 594int bus_rescan_devices(struct bus_type * bus)
554{ 595{
555 bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); 596 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
556} 597}
557 598
558/** 599/**
@@ -564,7 +605,7 @@ void bus_rescan_devices(struct bus_type * bus)
564 * to use if probing criteria changed during a devices lifetime and 605 * to use if probing criteria changed during a devices lifetime and
565 * driver attachment should change accordingly. 606 * driver attachment should change accordingly.
566 */ 607 */
567void device_reprobe(struct device *dev) 608int device_reprobe(struct device *dev)
568{ 609{
569 if (dev->driver) { 610 if (dev->driver) {
570 if (dev->parent) /* Needed for USB */ 611 if (dev->parent) /* Needed for USB */
@@ -573,14 +614,14 @@ void device_reprobe(struct device *dev)
573 if (dev->parent) 614 if (dev->parent)
574 up(&dev->parent->sem); 615 up(&dev->parent->sem);
575 } 616 }
576 617 return bus_rescan_devices_helper(dev, NULL);
577 bus_rescan_devices_helper(dev, NULL);
578} 618}
579EXPORT_SYMBOL_GPL(device_reprobe); 619EXPORT_SYMBOL_GPL(device_reprobe);
580 620
581struct bus_type * get_bus(struct bus_type * bus) 621struct bus_type *get_bus(struct bus_type *bus)
582{ 622{
583 return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL; 623 return bus ? container_of(subsys_get(&bus->subsys),
624 struct bus_type, subsys) : NULL;
584} 625}
585 626
586void put_bus(struct bus_type * bus) 627void put_bus(struct bus_type * bus)
@@ -655,22 +696,6 @@ static void klist_devices_put(struct klist_node *n)
655 put_device(dev); 696 put_device(dev);
656} 697}
657 698
658static void klist_drivers_get(struct klist_node *n)
659{
660 struct device_driver *drv = container_of(n, struct device_driver,
661 knode_bus);
662
663 get_driver(drv);
664}
665
666static void klist_drivers_put(struct klist_node *n)
667{
668 struct device_driver *drv = container_of(n, struct device_driver,
669 knode_bus);
670
671 put_driver(drv);
672}
673
674/** 699/**
675 * bus_register - register a bus with the system. 700 * bus_register - register a bus with the system.
676 * @bus: bus. 701 * @bus: bus.
@@ -706,7 +731,7 @@ int bus_register(struct bus_type * bus)
706 goto bus_drivers_fail; 731 goto bus_drivers_fail;
707 732
708 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put); 733 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
709 klist_init(&bus->klist_drivers, klist_drivers_get, klist_drivers_put); 734 klist_init(&bus->klist_drivers, NULL, NULL);
710 bus_add_attrs(bus); 735 bus_add_attrs(bus);
711 736
712 pr_debug("bus type '%s' registered\n", bus->name); 737 pr_debug("bus type '%s' registered\n", bus->name);
diff --git a/drivers/base/class.c b/drivers/base/class.c
index de8908320f23..b06b0e2b9c62 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -19,6 +19,8 @@
19#include <linux/slab.h> 19#include <linux/slab.h>
20#include "base.h" 20#include "base.h"
21 21
22extern struct subsystem devices_subsys;
23
22#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) 24#define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
23#define to_class(obj) container_of(obj, struct class, subsys.kset.kobj) 25#define to_class(obj) container_of(obj, struct class, subsys.kset.kobj)
24 26
@@ -197,7 +199,7 @@ static int class_device_create_uevent(struct class_device *class_dev,
197 * Note, the pointer created here is to be destroyed when finished by 199 * Note, the pointer created here is to be destroyed when finished by
198 * making a call to class_destroy(). 200 * making a call to class_destroy().
199 */ 201 */
200struct class *class_create(struct module *owner, char *name) 202struct class *class_create(struct module *owner, const char *name)
201{ 203{
202 struct class *cls; 204 struct class *cls;
203 int retval; 205 int retval;
@@ -361,7 +363,7 @@ static int class_uevent(struct kset *kset, struct kobject *kobj, char **envp,
361 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id); 363 pr_debug("%s - name = %s\n", __FUNCTION__, class_dev->class_id);
362 364
363 if (class_dev->dev) { 365 if (class_dev->dev) {
364 /* add physical device, backing this device */ 366 /* add device, backing this class device (deprecated) */
365 struct device *dev = class_dev->dev; 367 struct device *dev = class_dev->dev;
366 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL); 368 char *path = kobject_get_path(&dev->kobj, GFP_KERNEL);
367 369
@@ -679,7 +681,8 @@ int class_device_register(struct class_device *class_dev)
679struct class_device *class_device_create(struct class *cls, 681struct class_device *class_device_create(struct class *cls,
680 struct class_device *parent, 682 struct class_device *parent,
681 dev_t devt, 683 dev_t devt,
682 struct device *device, char *fmt, ...) 684 struct device *device,
685 const char *fmt, ...)
683{ 686{
684 va_list args; 687 va_list args;
685 struct class_device *class_dev = NULL; 688 struct class_device *class_dev = NULL;
@@ -839,6 +842,7 @@ int class_interface_register(struct class_interface *class_intf)
839{ 842{
840 struct class *parent; 843 struct class *parent;
841 struct class_device *class_dev; 844 struct class_device *class_dev;
845 struct device *dev;
842 846
843 if (!class_intf || !class_intf->class) 847 if (!class_intf || !class_intf->class)
844 return -ENODEV; 848 return -ENODEV;
@@ -853,6 +857,10 @@ int class_interface_register(struct class_interface *class_intf)
853 list_for_each_entry(class_dev, &parent->children, node) 857 list_for_each_entry(class_dev, &parent->children, node)
854 class_intf->add(class_dev, class_intf); 858 class_intf->add(class_dev, class_intf);
855 } 859 }
860 if (class_intf->add_dev) {
861 list_for_each_entry(dev, &parent->devices, node)
862 class_intf->add_dev(dev, class_intf);
863 }
856 up(&parent->sem); 864 up(&parent->sem);
857 865
858 return 0; 866 return 0;
@@ -862,6 +870,7 @@ void class_interface_unregister(struct class_interface *class_intf)
862{ 870{
863 struct class * parent = class_intf->class; 871 struct class * parent = class_intf->class;
864 struct class_device *class_dev; 872 struct class_device *class_dev;
873 struct device *dev;
865 874
866 if (!parent) 875 if (!parent)
867 return; 876 return;
@@ -872,12 +881,31 @@ void class_interface_unregister(struct class_interface *class_intf)
872 list_for_each_entry(class_dev, &parent->children, node) 881 list_for_each_entry(class_dev, &parent->children, node)
873 class_intf->remove(class_dev, class_intf); 882 class_intf->remove(class_dev, class_intf);
874 } 883 }
884 if (class_intf->remove_dev) {
885 list_for_each_entry(dev, &parent->devices, node)
886 class_intf->remove_dev(dev, class_intf);
887 }
875 up(&parent->sem); 888 up(&parent->sem);
876 889
877 class_put(parent); 890 class_put(parent);
878} 891}
879 892
893int virtual_device_parent(struct device *dev)
894{
895 if (!dev->class)
896 return -ENODEV;
897
898 if (!dev->class->virtual_dir) {
899 static struct kobject *virtual_dir = NULL;
900
901 if (!virtual_dir)
902 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
903 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
904 }
880 905
906 dev->kobj.parent = dev->class->virtual_dir;
907 return 0;
908}
881 909
882int __init classes_init(void) 910int __init classes_init(void)
883{ 911{
diff --git a/drivers/base/core.c b/drivers/base/core.c
index be6b5bc0677d..b224bb43ff63 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -3,6 +3,8 @@
3 * 3 *
4 * Copyright (c) 2002-3 Patrick Mochel 4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs 5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
6 * 8 *
7 * This file is released under the GPLv2 9 * This file is released under the GPLv2
8 * 10 *
@@ -92,6 +94,8 @@ static void device_release(struct kobject * kobj)
92 94
93 if (dev->release) 95 if (dev->release)
94 dev->release(dev); 96 dev->release(dev);
97 else if (dev->class && dev->class->dev_release)
98 dev->class->dev_release(dev);
95 else { 99 else {
96 printk(KERN_ERR "Device '%s' does not have a release() function, " 100 printk(KERN_ERR "Device '%s' does not have a release() function, "
97 "it is broken and must be fixed.\n", 101 "it is broken and must be fixed.\n",
@@ -149,17 +153,21 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
149 "MINOR=%u", MINOR(dev->devt)); 153 "MINOR=%u", MINOR(dev->devt));
150 } 154 }
151 155
152 /* add bus name of physical device */ 156 /* add bus name (same as SUBSYSTEM, deprecated) */
153 if (dev->bus) 157 if (dev->bus)
154 add_uevent_var(envp, num_envp, &i, 158 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length, 159 buffer, buffer_size, &length,
156 "PHYSDEVBUS=%s", dev->bus->name); 160 "PHYSDEVBUS=%s", dev->bus->name);
157 161
158 /* add driver name of physical device */ 162 /* add driver name (PHYSDEV* values are deprecated)*/
159 if (dev->driver) 163 if (dev->driver) {
164 add_uevent_var(envp, num_envp, &i,
165 buffer, buffer_size, &length,
166 "DRIVER=%s", dev->driver->name);
160 add_uevent_var(envp, num_envp, &i, 167 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length, 168 buffer, buffer_size, &length,
162 "PHYSDEVDRIVER=%s", dev->driver->name); 169 "PHYSDEVDRIVER=%s", dev->driver->name);
170 }
163 171
164 /* terminate, set to next free slot, shrink available space */ 172 /* terminate, set to next free slot, shrink available space */
165 envp[i] = NULL; 173 envp[i] = NULL;
@@ -177,6 +185,15 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
177 } 185 }
178 } 186 }
179 187
188 if (dev->class && dev->class->dev_uevent) {
189 /* have the class specific function add its stuff */
190 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
191 if (retval) {
192 pr_debug("%s - dev_uevent() returned %d\n",
193 __FUNCTION__, retval);
194 }
195 }
196
180 return retval; 197 return retval;
181} 198}
182 199
@@ -193,6 +210,72 @@ static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
193 return count; 210 return count;
194} 211}
195 212
213static int device_add_groups(struct device *dev)
214{
215 int i;
216 int error = 0;
217
218 if (dev->groups) {
219 for (i = 0; dev->groups[i]; i++) {
220 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
221 if (error) {
222 while (--i >= 0)
223 sysfs_remove_group(&dev->kobj, dev->groups[i]);
224 goto out;
225 }
226 }
227 }
228out:
229 return error;
230}
231
232static void device_remove_groups(struct device *dev)
233{
234 int i;
235 if (dev->groups) {
236 for (i = 0; dev->groups[i]; i++) {
237 sysfs_remove_group(&dev->kobj, dev->groups[i]);
238 }
239 }
240}
241
242static int device_add_attrs(struct device *dev)
243{
244 struct class *class = dev->class;
245 int error = 0;
246 int i;
247
248 if (!class)
249 return 0;
250
251 if (class->dev_attrs) {
252 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
253 error = device_create_file(dev, &class->dev_attrs[i]);
254 if (error)
255 break;
256 }
257 }
258 if (error)
259 while (--i >= 0)
260 device_remove_file(dev, &class->dev_attrs[i]);
261 return error;
262}
263
264static void device_remove_attrs(struct device *dev)
265{
266 struct class *class = dev->class;
267 int i;
268
269 if (!class)
270 return;
271
272 if (class->dev_attrs) {
273 for (i = 0; attr_name(class->dev_attrs[i]); i++)
274 device_remove_file(dev, &class->dev_attrs[i]);
275 }
276}
277
278
196static ssize_t show_dev(struct device *dev, struct device_attribute *attr, 279static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
197 char *buf) 280 char *buf)
198{ 281{
@@ -236,6 +319,32 @@ void device_remove_file(struct device * dev, struct device_attribute * attr)
236 } 319 }
237} 320}
238 321
322/**
323 * device_create_bin_file - create sysfs binary attribute file for device.
324 * @dev: device.
325 * @attr: device binary attribute descriptor.
326 */
327int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
328{
329 int error = -EINVAL;
330 if (dev)
331 error = sysfs_create_bin_file(&dev->kobj, attr);
332 return error;
333}
334EXPORT_SYMBOL_GPL(device_create_bin_file);
335
336/**
337 * device_remove_bin_file - remove sysfs binary attribute file
338 * @dev: device.
339 * @attr: device binary attribute descriptor.
340 */
341void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
342{
343 if (dev)
344 sysfs_remove_bin_file(&dev->kobj, attr);
345}
346EXPORT_SYMBOL_GPL(device_remove_bin_file);
347
239static void klist_children_get(struct klist_node *n) 348static void klist_children_get(struct klist_node *n)
240{ 349{
241 struct device *dev = container_of(n, struct device, knode_parent); 350 struct device *dev = container_of(n, struct device, knode_parent);
@@ -289,12 +398,20 @@ int device_add(struct device *dev)
289{ 398{
290 struct device *parent = NULL; 399 struct device *parent = NULL;
291 char *class_name = NULL; 400 char *class_name = NULL;
401 struct class_interface *class_intf;
292 int error = -EINVAL; 402 int error = -EINVAL;
293 403
294 dev = get_device(dev); 404 dev = get_device(dev);
295 if (!dev || !strlen(dev->bus_id)) 405 if (!dev || !strlen(dev->bus_id))
296 goto Error; 406 goto Error;
297 407
408 /* if this is a class device, and has no parent, create one */
409 if ((dev->class) && (dev->parent == NULL)) {
410 error = virtual_device_parent(dev);
411 if (error)
412 goto Error;
413 }
414
298 parent = get_device(dev->parent); 415 parent = get_device(dev->parent);
299 416
300 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); 417 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
@@ -307,6 +424,10 @@ int device_add(struct device *dev)
307 if ((error = kobject_add(&dev->kobj))) 424 if ((error = kobject_add(&dev->kobj)))
308 goto Error; 425 goto Error;
309 426
427 /* notify platform of device entry */
428 if (platform_notify)
429 platform_notify(dev);
430
310 dev->uevent_attr.attr.name = "uevent"; 431 dev->uevent_attr.attr.name = "uevent";
311 dev->uevent_attr.attr.mode = S_IWUSR; 432 dev->uevent_attr.attr.mode = S_IWUSR;
312 if (dev->driver) 433 if (dev->driver)
@@ -340,12 +461,17 @@ int device_add(struct device *dev)
340 "subsystem"); 461 "subsystem");
341 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, 462 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
342 dev->bus_id); 463 dev->bus_id);
343 464 if (parent) {
344 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); 465 sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
345 class_name = make_class_name(dev->class->name, &dev->kobj); 466 class_name = make_class_name(dev->class->name, &dev->kobj);
346 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); 467 sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
468 }
347 } 469 }
348 470
471 if ((error = device_add_attrs(dev)))
472 goto AttrsError;
473 if ((error = device_add_groups(dev)))
474 goto GroupError;
349 if ((error = device_pm_add(dev))) 475 if ((error = device_pm_add(dev)))
350 goto PMError; 476 goto PMError;
351 if ((error = bus_add_device(dev))) 477 if ((error = bus_add_device(dev)))
@@ -356,15 +482,16 @@ int device_add(struct device *dev)
356 klist_add_tail(&dev->knode_parent, &parent->klist_children); 482 klist_add_tail(&dev->knode_parent, &parent->klist_children);
357 483
358 if (dev->class) { 484 if (dev->class) {
359 /* tie the class to the device */
360 down(&dev->class->sem); 485 down(&dev->class->sem);
486 /* tie the class to the device */
361 list_add_tail(&dev->node, &dev->class->devices); 487 list_add_tail(&dev->node, &dev->class->devices);
488
489 /* notify any interfaces that the device is here */
490 list_for_each_entry(class_intf, &dev->class->interfaces, node)
491 if (class_intf->add_dev)
492 class_intf->add_dev(dev, class_intf);
362 up(&dev->class->sem); 493 up(&dev->class->sem);
363 } 494 }
364
365 /* notify platform of device entry */
366 if (platform_notify)
367 platform_notify(dev);
368 Done: 495 Done:
369 kfree(class_name); 496 kfree(class_name);
370 put_device(dev); 497 put_device(dev);
@@ -372,6 +499,10 @@ int device_add(struct device *dev)
372 BusError: 499 BusError:
373 device_pm_remove(dev); 500 device_pm_remove(dev);
374 PMError: 501 PMError:
502 device_remove_groups(dev);
503 GroupError:
504 device_remove_attrs(dev);
505 AttrsError:
375 if (dev->devt_attr) { 506 if (dev->devt_attr) {
376 device_remove_file(dev, dev->devt_attr); 507 device_remove_file(dev, dev->devt_attr);
377 kfree(dev->devt_attr); 508 kfree(dev->devt_attr);
@@ -449,6 +580,7 @@ void device_del(struct device * dev)
449{ 580{
450 struct device * parent = dev->parent; 581 struct device * parent = dev->parent;
451 char *class_name = NULL; 582 char *class_name = NULL;
583 struct class_interface *class_intf;
452 584
453 if (parent) 585 if (parent)
454 klist_del(&dev->knode_parent); 586 klist_del(&dev->knode_parent);
@@ -458,14 +590,23 @@ void device_del(struct device * dev)
458 sysfs_remove_link(&dev->kobj, "subsystem"); 590 sysfs_remove_link(&dev->kobj, "subsystem");
459 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id); 591 sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
460 class_name = make_class_name(dev->class->name, &dev->kobj); 592 class_name = make_class_name(dev->class->name, &dev->kobj);
461 sysfs_remove_link(&dev->kobj, "device"); 593 if (parent) {
462 sysfs_remove_link(&dev->parent->kobj, class_name); 594 sysfs_remove_link(&dev->kobj, "device");
595 sysfs_remove_link(&dev->parent->kobj, class_name);
596 }
463 kfree(class_name); 597 kfree(class_name);
464 down(&dev->class->sem); 598 down(&dev->class->sem);
599 /* notify any interfaces that the device is now gone */
600 list_for_each_entry(class_intf, &dev->class->interfaces, node)
601 if (class_intf->remove_dev)
602 class_intf->remove_dev(dev, class_intf);
603 /* remove the device from the class list */
465 list_del_init(&dev->node); 604 list_del_init(&dev->node);
466 up(&dev->class->sem); 605 up(&dev->class->sem);
467 } 606 }
468 device_remove_file(dev, &dev->uevent_attr); 607 device_remove_file(dev, &dev->uevent_attr);
608 device_remove_groups(dev);
609 device_remove_attrs(dev);
469 610
470 /* Notify the platform of the removal, in case they 611 /* Notify the platform of the removal, in case they
471 * need to do anything... 612 * need to do anything...
@@ -579,7 +720,7 @@ static void device_create_release(struct device *dev)
579 * been created with a call to class_create(). 720 * been created with a call to class_create().
580 */ 721 */
581struct device *device_create(struct class *class, struct device *parent, 722struct device *device_create(struct class *class, struct device *parent,
582 dev_t devt, char *fmt, ...) 723 dev_t devt, const char *fmt, ...)
583{ 724{
584 va_list args; 725 va_list args;
585 struct device *dev = NULL; 726 struct device *dev = NULL;
@@ -587,10 +728,6 @@ struct device *device_create(struct class *class, struct device *parent,
587 728
588 if (class == NULL || IS_ERR(class)) 729 if (class == NULL || IS_ERR(class))
589 goto error; 730 goto error;
590 if (parent == NULL) {
591 printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
592 goto error;
593 }
594 731
595 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 732 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
596 if (!dev) { 733 if (!dev) {
@@ -644,3 +781,58 @@ void device_destroy(struct class *class, dev_t devt)
644 device_unregister(dev); 781 device_unregister(dev);
645} 782}
646EXPORT_SYMBOL_GPL(device_destroy); 783EXPORT_SYMBOL_GPL(device_destroy);
784
785/**
786 * device_rename - renames a device
787 * @dev: the pointer to the struct device to be renamed
788 * @new_name: the new name of the device
789 */
790int device_rename(struct device *dev, char *new_name)
791{
792 char *old_class_name = NULL;
793 char *new_class_name = NULL;
794 char *old_symlink_name = NULL;
795 int error;
796
797 dev = get_device(dev);
798 if (!dev)
799 return -EINVAL;
800
801 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
802
803 if ((dev->class) && (dev->parent))
804 old_class_name = make_class_name(dev->class->name, &dev->kobj);
805
806 if (dev->class) {
807 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
808 if (!old_symlink_name)
809 return -ENOMEM;
810 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
811 }
812
813 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
814
815 error = kobject_rename(&dev->kobj, new_name);
816
817 if (old_class_name) {
818 new_class_name = make_class_name(dev->class->name, &dev->kobj);
819 if (new_class_name) {
820 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
821 new_class_name);
822 sysfs_remove_link(&dev->parent->kobj, old_class_name);
823 }
824 }
825 if (dev->class) {
826 sysfs_remove_link(&dev->class->subsys.kset.kobj,
827 old_symlink_name);
828 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
829 dev->bus_id);
830 }
831 put_device(dev);
832
833 kfree(old_class_name);
834 kfree(new_class_name);
835 kfree(old_symlink_name);
836
837 return error;
838}
diff --git a/drivers/base/dd.c b/drivers/base/dd.c
index 889c71111239..b5f43c3e44fa 100644
--- a/drivers/base/dd.c
+++ b/drivers/base/dd.c
@@ -17,6 +17,7 @@
17 17
18#include <linux/device.h> 18#include <linux/device.h>
19#include <linux/module.h> 19#include <linux/module.h>
20#include <linux/kthread.h>
20 21
21#include "base.h" 22#include "base.h"
22#include "power/power.h" 23#include "power/power.h"
@@ -38,66 +39,73 @@
38 * 39 *
39 * This function must be called with @dev->sem held. 40 * This function must be called with @dev->sem held.
40 */ 41 */
41void device_bind_driver(struct device * dev) 42int device_bind_driver(struct device *dev)
42{ 43{
43 if (klist_node_attached(&dev->knode_driver)) 44 int ret;
44 return; 45
46 if (klist_node_attached(&dev->knode_driver)) {
47 printk(KERN_WARNING "%s: device %s already bound\n",
48 __FUNCTION__, kobject_name(&dev->kobj));
49 return 0;
50 }
45 51
46 pr_debug("bound device '%s' to driver '%s'\n", 52 pr_debug("bound device '%s' to driver '%s'\n",
47 dev->bus_id, dev->driver->name); 53 dev->bus_id, dev->driver->name);
48 klist_add_tail(&dev->knode_driver, &dev->driver->klist_devices); 54 klist_add_tail(&dev->knode_driver, &dev->driver->klist_devices);
49 sysfs_create_link(&dev->driver->kobj, &dev->kobj, 55 ret = sysfs_create_link(&dev->driver->kobj, &dev->kobj,
50 kobject_name(&dev->kobj)); 56 kobject_name(&dev->kobj));
51 sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver"); 57 if (ret == 0) {
58 ret = sysfs_create_link(&dev->kobj, &dev->driver->kobj,
59 "driver");
60 if (ret)
61 sysfs_remove_link(&dev->driver->kobj,
62 kobject_name(&dev->kobj));
63 }
64 return ret;
52} 65}
53 66
54/** 67struct stupid_thread_structure {
55 * driver_probe_device - attempt to bind device & driver. 68 struct device_driver *drv;
56 * @drv: driver. 69 struct device *dev;
57 * @dev: device. 70};
58 * 71
59 * First, we call the bus's match function, if one present, which 72static atomic_t probe_count = ATOMIC_INIT(0);
60 * should compare the device IDs the driver supports with the 73static int really_probe(void *void_data)
61 * device IDs of the device. Note we don't do this ourselves
62 * because we don't know the format of the ID structures, nor what
63 * is to be considered a match and what is not.
64 *
65 * This function returns 1 if a match is found, an error if one
66 * occurs (that is not -ENODEV or -ENXIO), and 0 otherwise.
67 *
68 * This function must be called with @dev->sem held. When called
69 * for a USB interface, @dev->parent->sem must be held as well.
70 */
71int driver_probe_device(struct device_driver * drv, struct device * dev)
72{ 74{
75 struct stupid_thread_structure *data = void_data;
76 struct device_driver *drv = data->drv;
77 struct device *dev = data->dev;
73 int ret = 0; 78 int ret = 0;
74 79
75 if (drv->bus->match && !drv->bus->match(dev, drv)) 80 atomic_inc(&probe_count);
76 goto Done; 81 pr_debug("%s: Probing driver %s with device %s\n",
82 drv->bus->name, drv->name, dev->bus_id);
77 83
78 pr_debug("%s: Matched Device %s with Driver %s\n",
79 drv->bus->name, dev->bus_id, drv->name);
80 dev->driver = drv; 84 dev->driver = drv;
81 if (dev->bus->probe) { 85 if (dev->bus->probe) {
82 ret = dev->bus->probe(dev); 86 ret = dev->bus->probe(dev);
83 if (ret) { 87 if (ret) {
84 dev->driver = NULL; 88 dev->driver = NULL;
85 goto ProbeFailed; 89 goto probe_failed;
86 } 90 }
87 } else if (drv->probe) { 91 } else if (drv->probe) {
88 ret = drv->probe(dev); 92 ret = drv->probe(dev);
89 if (ret) { 93 if (ret) {
90 dev->driver = NULL; 94 dev->driver = NULL;
91 goto ProbeFailed; 95 goto probe_failed;
92 } 96 }
93 } 97 }
94 device_bind_driver(dev); 98 if (device_bind_driver(dev)) {
99 printk(KERN_ERR "%s: device_bind_driver(%s) failed\n",
100 __FUNCTION__, dev->bus_id);
101 /* How does undo a ->probe? We're screwed. */
102 }
95 ret = 1; 103 ret = 1;
96 pr_debug("%s: Bound Device %s to Driver %s\n", 104 pr_debug("%s: Bound Device %s to Driver %s\n",
97 drv->bus->name, dev->bus_id, drv->name); 105 drv->bus->name, dev->bus_id, drv->name);
98 goto Done; 106 goto done;
99 107
100 ProbeFailed: 108probe_failed:
101 if (ret == -ENODEV || ret == -ENXIO) { 109 if (ret == -ENODEV || ret == -ENXIO) {
102 /* Driver matched, but didn't support device 110 /* Driver matched, but didn't support device
103 * or device not found. 111 * or device not found.
@@ -110,7 +118,71 @@ int driver_probe_device(struct device_driver * drv, struct device * dev)
110 "%s: probe of %s failed with error %d\n", 118 "%s: probe of %s failed with error %d\n",
111 drv->name, dev->bus_id, ret); 119 drv->name, dev->bus_id, ret);
112 } 120 }
113 Done: 121done:
122 kfree(data);
123 atomic_dec(&probe_count);
124 return ret;
125}
126
127/**
128 * driver_probe_done
129 * Determine if the probe sequence is finished or not.
130 *
131 * Should somehow figure out how to use a semaphore, not an atomic variable...
132 */
133int driver_probe_done(void)
134{
135 pr_debug("%s: probe_count = %d\n", __FUNCTION__,
136 atomic_read(&probe_count));
137 if (atomic_read(&probe_count))
138 return -EBUSY;
139 return 0;
140}
141
142/**
143 * driver_probe_device - attempt to bind device & driver together
144 * @drv: driver to bind a device to
145 * @dev: device to try to bind to the driver
146 *
147 * First, we call the bus's match function, if one present, which should
148 * compare the device IDs the driver supports with the device IDs of the
149 * device. Note we don't do this ourselves because we don't know the
150 * format of the ID structures, nor what is to be considered a match and
151 * what is not.
152 *
153 * This function returns 1 if a match is found, an error if one occurs
154 * (that is not -ENODEV or -ENXIO), and 0 otherwise.
155 *
156 * This function must be called with @dev->sem held. When called for a
157 * USB interface, @dev->parent->sem must be held as well.
158 */
159int driver_probe_device(struct device_driver * drv, struct device * dev)
160{
161 struct stupid_thread_structure *data;
162 struct task_struct *probe_task;
163 int ret = 0;
164
165 if (!device_is_registered(dev))
166 return -ENODEV;
167 if (drv->bus->match && !drv->bus->match(dev, drv))
168 goto done;
169
170 pr_debug("%s: Matched Device %s with Driver %s\n",
171 drv->bus->name, dev->bus_id, drv->name);
172
173 data = kmalloc(sizeof(*data), GFP_KERNEL);
174 data->drv = drv;
175 data->dev = dev;
176
177 if (drv->multithread_probe) {
178 probe_task = kthread_run(really_probe, data,
179 "probe-%s", dev->bus_id);
180 if (IS_ERR(probe_task))
181 ret = PTR_ERR(probe_task);
182 } else
183 ret = really_probe(data);
184
185done:
114 return ret; 186 return ret;
115} 187}
116 188
@@ -139,8 +211,9 @@ int device_attach(struct device * dev)
139 211
140 down(&dev->sem); 212 down(&dev->sem);
141 if (dev->driver) { 213 if (dev->driver) {
142 device_bind_driver(dev); 214 ret = device_bind_driver(dev);
143 ret = 1; 215 if (ret == 0)
216 ret = 1;
144 } else 217 } else
145 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach); 218 ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
146 up(&dev->sem); 219 up(&dev->sem);
@@ -182,9 +255,9 @@ static int __driver_attach(struct device * dev, void * data)
182 * returns 0 and the @dev->driver is set, we've found a 255 * returns 0 and the @dev->driver is set, we've found a
183 * compatible pair. 256 * compatible pair.
184 */ 257 */
185void driver_attach(struct device_driver * drv) 258int driver_attach(struct device_driver * drv)
186{ 259{
187 bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); 260 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
188} 261}
189 262
190/** 263/**
diff --git a/drivers/base/driver.c b/drivers/base/driver.c
index 562600dd540a..1214cbd17d86 100644
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -142,20 +142,6 @@ void put_driver(struct device_driver * drv)
142 kobject_put(&drv->kobj); 142 kobject_put(&drv->kobj);
143} 143}
144 144
145static void klist_devices_get(struct klist_node *n)
146{
147 struct device *dev = container_of(n, struct device, knode_driver);
148
149 get_device(dev);
150}
151
152static void klist_devices_put(struct klist_node *n)
153{
154 struct device *dev = container_of(n, struct device, knode_driver);
155
156 put_device(dev);
157}
158
159/** 145/**
160 * driver_register - register driver with bus 146 * driver_register - register driver with bus
161 * @drv: driver to register 147 * @drv: driver to register
@@ -175,7 +161,7 @@ int driver_register(struct device_driver * drv)
175 (drv->bus->shutdown && drv->shutdown)) { 161 (drv->bus->shutdown && drv->shutdown)) {
176 printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name); 162 printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
177 } 163 }
178 klist_init(&drv->klist_devices, klist_devices_get, klist_devices_put); 164 klist_init(&drv->klist_devices, NULL, NULL);
179 init_completion(&drv->unloaded); 165 init_completion(&drv->unloaded);
180 return bus_add_driver(drv); 166 return bus_add_driver(drv);
181} 167}
diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 2b8755db76c6..940ce41f1887 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -505,12 +505,36 @@ static int platform_match(struct device * dev, struct device_driver * drv)
505 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0); 505 return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
506} 506}
507 507
508static int platform_suspend(struct device * dev, pm_message_t state) 508static int platform_suspend(struct device *dev, pm_message_t mesg)
509{ 509{
510 int ret = 0; 510 int ret = 0;
511 511
512 if (dev->driver && dev->driver->suspend) 512 if (dev->driver && dev->driver->suspend)
513 ret = dev->driver->suspend(dev, state); 513 ret = dev->driver->suspend(dev, mesg);
514
515 return ret;
516}
517
518static int platform_suspend_late(struct device *dev, pm_message_t mesg)
519{
520 struct platform_driver *drv = to_platform_driver(dev->driver);
521 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
522 int ret = 0;
523
524 if (dev->driver && drv->suspend_late)
525 ret = drv->suspend_late(pdev, mesg);
526
527 return ret;
528}
529
530static int platform_resume_early(struct device *dev)
531{
532 struct platform_driver *drv = to_platform_driver(dev->driver);
533 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
534 int ret = 0;
535
536 if (dev->driver && drv->resume_early)
537 ret = drv->resume_early(pdev);
514 538
515 return ret; 539 return ret;
516} 540}
@@ -531,6 +555,8 @@ struct bus_type platform_bus_type = {
531 .match = platform_match, 555 .match = platform_match,
532 .uevent = platform_uevent, 556 .uevent = platform_uevent,
533 .suspend = platform_suspend, 557 .suspend = platform_suspend,
558 .suspend_late = platform_suspend_late,
559 .resume_early = platform_resume_early,
534 .resume = platform_resume, 560 .resume = platform_resume,
535}; 561};
536EXPORT_SYMBOL_GPL(platform_bus_type); 562EXPORT_SYMBOL_GPL(platform_bus_type);
diff --git a/drivers/base/power/resume.c b/drivers/base/power/resume.c
index 826093ef4c7e..020be36705a6 100644
--- a/drivers/base/power/resume.c
+++ b/drivers/base/power/resume.c
@@ -38,13 +38,35 @@ int resume_device(struct device * dev)
38 dev_dbg(dev,"resuming\n"); 38 dev_dbg(dev,"resuming\n");
39 error = dev->bus->resume(dev); 39 error = dev->bus->resume(dev);
40 } 40 }
41 if (dev->class && dev->class->resume) {
42 dev_dbg(dev,"class resume\n");
43 error = dev->class->resume(dev);
44 }
41 up(&dev->sem); 45 up(&dev->sem);
42 TRACE_RESUME(error); 46 TRACE_RESUME(error);
43 return error; 47 return error;
44} 48}
45 49
46 50
51static int resume_device_early(struct device * dev)
52{
53 int error = 0;
47 54
55 TRACE_DEVICE(dev);
56 TRACE_RESUME(0);
57 if (dev->bus && dev->bus->resume_early) {
58 dev_dbg(dev,"EARLY resume\n");
59 error = dev->bus->resume_early(dev);
60 }
61 TRACE_RESUME(error);
62 return error;
63}
64
65/*
66 * Resume the devices that have either not gone through
67 * the late suspend, or that did go through it but also
68 * went through the early resume
69 */
48void dpm_resume(void) 70void dpm_resume(void)
49{ 71{
50 down(&dpm_list_sem); 72 down(&dpm_list_sem);
@@ -74,6 +96,7 @@ void dpm_resume(void)
74 96
75void device_resume(void) 97void device_resume(void)
76{ 98{
99 might_sleep();
77 down(&dpm_sem); 100 down(&dpm_sem);
78 dpm_resume(); 101 dpm_resume();
79 up(&dpm_sem); 102 up(&dpm_sem);
@@ -83,12 +106,12 @@ EXPORT_SYMBOL_GPL(device_resume);
83 106
84 107
85/** 108/**
86 * device_power_up_irq - Power on some devices. 109 * dpm_power_up - Power on some devices.
87 * 110 *
88 * Walk the dpm_off_irq list and power each device up. This 111 * Walk the dpm_off_irq list and power each device up. This
89 * is used for devices that required they be powered down with 112 * is used for devices that required they be powered down with
90 * interrupts disabled. As devices are powered on, they are moved to 113 * interrupts disabled. As devices are powered on, they are moved
91 * the dpm_suspended list. 114 * to the dpm_active list.
92 * 115 *
93 * Interrupts must be disabled when calling this. 116 * Interrupts must be disabled when calling this.
94 */ 117 */
@@ -99,16 +122,14 @@ void dpm_power_up(void)
99 struct list_head * entry = dpm_off_irq.next; 122 struct list_head * entry = dpm_off_irq.next;
100 struct device * dev = to_device(entry); 123 struct device * dev = to_device(entry);
101 124
102 get_device(dev); 125 list_move_tail(entry, &dpm_off);
103 list_move_tail(entry, &dpm_active); 126 resume_device_early(dev);
104 resume_device(dev);
105 put_device(dev);
106 } 127 }
107} 128}
108 129
109 130
110/** 131/**
111 * device_pm_power_up - Turn on all devices that need special attention. 132 * device_power_up - Turn on all devices that need special attention.
112 * 133 *
113 * Power on system devices then devices that required we shut them down 134 * Power on system devices then devices that required we shut them down
114 * with interrupts disabled. 135 * with interrupts disabled.
diff --git a/drivers/base/power/suspend.c b/drivers/base/power/suspend.c
index 69509e02f703..ece136bf97e3 100644
--- a/drivers/base/power/suspend.c
+++ b/drivers/base/power/suspend.c
@@ -34,6 +34,7 @@ static inline char *suspend_verb(u32 event)
34 switch (event) { 34 switch (event) {
35 case PM_EVENT_SUSPEND: return "suspend"; 35 case PM_EVENT_SUSPEND: return "suspend";
36 case PM_EVENT_FREEZE: return "freeze"; 36 case PM_EVENT_FREEZE: return "freeze";
37 case PM_EVENT_PRETHAW: return "prethaw";
37 default: return "(unknown suspend event)"; 38 default: return "(unknown suspend event)";
38 } 39 }
39} 40}
@@ -65,7 +66,19 @@ int suspend_device(struct device * dev, pm_message_t state)
65 66
66 dev->power.prev_state = dev->power.power_state; 67 dev->power.prev_state = dev->power.power_state;
67 68
68 if (dev->bus && dev->bus->suspend && !dev->power.power_state.event) { 69 if (dev->class && dev->class->suspend && !dev->power.power_state.event) {
70 dev_dbg(dev, "class %s%s\n",
71 suspend_verb(state.event),
72 ((state.event == PM_EVENT_SUSPEND)
73 && device_may_wakeup(dev))
74 ? ", may wakeup"
75 : ""
76 );
77 error = dev->class->suspend(dev, state);
78 suspend_report_result(dev->class->suspend, error);
79 }
80
81 if (!error && dev->bus && dev->bus->suspend && !dev->power.power_state.event) {
69 dev_dbg(dev, "%s%s\n", 82 dev_dbg(dev, "%s%s\n",
70 suspend_verb(state.event), 83 suspend_verb(state.event),
71 ((state.event == PM_EVENT_SUSPEND) 84 ((state.event == PM_EVENT_SUSPEND)
@@ -81,15 +94,42 @@ int suspend_device(struct device * dev, pm_message_t state)
81} 94}
82 95
83 96
97/*
98 * This is called with interrupts off, only a single CPU
99 * running. We can't do down() on a semaphore (and we don't
100 * need the protection)
101 */
102static int suspend_device_late(struct device *dev, pm_message_t state)
103{
104 int error = 0;
105
106 if (dev->bus && dev->bus->suspend_late && !dev->power.power_state.event) {
107 dev_dbg(dev, "LATE %s%s\n",
108 suspend_verb(state.event),
109 ((state.event == PM_EVENT_SUSPEND)
110 && device_may_wakeup(dev))
111 ? ", may wakeup"
112 : ""
113 );
114 error = dev->bus->suspend_late(dev, state);
115 suspend_report_result(dev->bus->suspend_late, error);
116 }
117 return error;
118}
119
84/** 120/**
85 * device_suspend - Save state and stop all devices in system. 121 * device_suspend - Save state and stop all devices in system.
86 * @state: Power state to put each device in. 122 * @state: Power state to put each device in.
87 * 123 *
88 * Walk the dpm_active list, call ->suspend() for each device, and move 124 * Walk the dpm_active list, call ->suspend() for each device, and move
89 * it to dpm_off. 125 * it to the dpm_off list.
90 * Check the return value for each. If it returns 0, then we move the 126 *
91 * the device to the dpm_off list. If it returns -EAGAIN, we move it to 127 * (For historical reasons, if it returns -EAGAIN, that used to mean
92 * the dpm_off_irq list. If we get a different error, try and back out. 128 * that the device would be called again with interrupts disabled.
129 * These days, we use the "suspend_late()" callback for that, so we
130 * print a warning and consider it an error).
131 *
132 * If we get a different error, try and back out.
93 * 133 *
94 * If we hit a failure with any of the devices, call device_resume() 134 * If we hit a failure with any of the devices, call device_resume()
95 * above to bring the suspended devices back to life. 135 * above to bring the suspended devices back to life.
@@ -100,6 +140,7 @@ int device_suspend(pm_message_t state)
100{ 140{
101 int error = 0; 141 int error = 0;
102 142
143 might_sleep();
103 down(&dpm_sem); 144 down(&dpm_sem);
104 down(&dpm_list_sem); 145 down(&dpm_list_sem);
105 while (!list_empty(&dpm_active) && error == 0) { 146 while (!list_empty(&dpm_active) && error == 0) {
@@ -115,39 +156,27 @@ int device_suspend(pm_message_t state)
115 156
116 /* Check if the device got removed */ 157 /* Check if the device got removed */
117 if (!list_empty(&dev->power.entry)) { 158 if (!list_empty(&dev->power.entry)) {
118 /* Move it to the dpm_off or dpm_off_irq list */ 159 /* Move it to the dpm_off list */
119 if (!error) 160 if (!error)
120 list_move(&dev->power.entry, &dpm_off); 161 list_move(&dev->power.entry, &dpm_off);
121 else if (error == -EAGAIN) {
122 list_move(&dev->power.entry, &dpm_off_irq);
123 error = 0;
124 }
125 } 162 }
126 if (error) 163 if (error)
127 printk(KERN_ERR "Could not suspend device %s: " 164 printk(KERN_ERR "Could not suspend device %s: "
128 "error %d\n", kobject_name(&dev->kobj), error); 165 "error %d%s\n",
166 kobject_name(&dev->kobj), error,
167 error == -EAGAIN ? " (please convert to suspend_late)" : "");
129 put_device(dev); 168 put_device(dev);
130 } 169 }
131 up(&dpm_list_sem); 170 up(&dpm_list_sem);
132 if (error) { 171 if (error)
133 /* we failed... before resuming, bring back devices from
134 * dpm_off_irq list back to main dpm_off list, we do want
135 * to call resume() on them, in case they partially suspended
136 * despite returning -EAGAIN
137 */
138 while (!list_empty(&dpm_off_irq)) {
139 struct list_head * entry = dpm_off_irq.next;
140 list_move(entry, &dpm_off);
141 }
142 dpm_resume(); 172 dpm_resume();
143 } 173
144 up(&dpm_sem); 174 up(&dpm_sem);
145 return error; 175 return error;
146} 176}
147 177
148EXPORT_SYMBOL_GPL(device_suspend); 178EXPORT_SYMBOL_GPL(device_suspend);
149 179
150
151/** 180/**
152 * device_power_down - Shut down special devices. 181 * device_power_down - Shut down special devices.
153 * @state: Power state to enter. 182 * @state: Power state to enter.
@@ -162,14 +191,17 @@ int device_power_down(pm_message_t state)
162 int error = 0; 191 int error = 0;
163 struct device * dev; 192 struct device * dev;
164 193
165 list_for_each_entry_reverse(dev, &dpm_off_irq, power.entry) { 194 while (!list_empty(&dpm_off)) {
166 if ((error = suspend_device(dev, state))) 195 struct list_head * entry = dpm_off.prev;
167 break; 196
197 dev = to_device(entry);
198 error = suspend_device_late(dev, state);
199 if (error)
200 goto Error;
201 list_move(&dev->power.entry, &dpm_off_irq);
168 } 202 }
169 if (error) 203
170 goto Error; 204 error = sysdev_suspend(state);
171 if ((error = sysdev_suspend(state)))
172 goto Error;
173 Done: 205 Done:
174 return error; 206 return error;
175 Error: 207 Error:
diff --git a/drivers/base/power/sysfs.c b/drivers/base/power/sysfs.c
index 40d7242a07c1..2d47517dbe32 100644
--- a/drivers/base/power/sysfs.c
+++ b/drivers/base/power/sysfs.c
@@ -7,22 +7,29 @@
7#include "power.h" 7#include "power.h"
8 8
9 9
10#ifdef CONFIG_PM_SYSFS_DEPRECATED
11
10/** 12/**
11 * state - Control current power state of device 13 * state - Control current power state of device
12 * 14 *
13 * show() returns the current power state of the device. '0' indicates 15 * show() returns the current power state of the device. '0' indicates
14 * the device is on. Other values (1-3) indicate the device is in a low 16 * the device is on. Other values (2) indicate the device is in some low
15 * power state. 17 * power state.
16 * 18 *
17 * store() sets the current power state, which is an integer value 19 * store() sets the current power state, which is an integer valued
18 * between 0-3. If the device is on ('0'), and the value written is 20 * 0, 2, or 3. Devices with bus.suspend_late(), or bus.resume_early()
19 * greater than 0, then the device is placed directly into the low-power 21 * methods fail this operation; those methods couldn't be called.
20 * state (via its driver's ->suspend() method). 22 * Otherwise,
21 * If the device is currently in a low-power state, and the value is 0, 23 *
22 * the device is powered back on (via the ->resume() method). 24 * - If the recorded dev->power.power_state.event matches the
23 * If the device is in a low-power state, and a different low-power state 25 * target value, nothing is done.
24 * is requested, the device is first resumed, then suspended into the new 26 * - If the recorded event code is nonzero, the device is reactivated
25 * low-power state. 27 * by calling bus.resume() and/or class.resume().
28 * - If the target value is nonzero, the device is suspended by
29 * calling class.suspend() and/or bus.suspend() with event code
30 * PM_EVENT_SUSPEND.
31 *
32 * This mechanism is DEPRECATED and should only be used for testing.
26 */ 33 */
27 34
28static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf) 35static ssize_t state_show(struct device * dev, struct device_attribute *attr, char * buf)
@@ -38,6 +45,10 @@ static ssize_t state_store(struct device * dev, struct device_attribute *attr, c
38 pm_message_t state; 45 pm_message_t state;
39 int error = -EINVAL; 46 int error = -EINVAL;
40 47
48 /* disallow incomplete suspend sequences */
49 if (dev->bus && (dev->bus->suspend_late || dev->bus->resume_early))
50 return error;
51
41 state.event = PM_EVENT_SUSPEND; 52 state.event = PM_EVENT_SUSPEND;
42 /* Older apps expected to write "3" here - confused with PCI D3 */ 53 /* Older apps expected to write "3" here - confused with PCI D3 */
43 if ((n == 1) && !strcmp(buf, "3")) 54 if ((n == 1) && !strcmp(buf, "3"))
@@ -57,6 +68,8 @@ static ssize_t state_store(struct device * dev, struct device_attribute *attr, c
57static DEVICE_ATTR(state, 0644, state_show, state_store); 68static DEVICE_ATTR(state, 0644, state_show, state_store);
58 69
59 70
71#endif /* CONFIG_PM_SYSFS_DEPRECATED */
72
60/* 73/*
61 * wakeup - Report/change current wakeup option for device 74 * wakeup - Report/change current wakeup option for device
62 * 75 *
@@ -130,7 +143,9 @@ static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
130 143
131 144
132static struct attribute * power_attrs[] = { 145static struct attribute * power_attrs[] = {
146#ifdef CONFIG_PM_SYSFS_DEPRECATED
133 &dev_attr_state.attr, 147 &dev_attr_state.attr,
148#endif
134 &dev_attr_wakeup.attr, 149 &dev_attr_wakeup.attr,
135 NULL, 150 NULL,
136}; 151};