Skip to contentSkip to conversation

driver model wakeup flags

PATCH New David Brownell 1 participant 4 messages Sep 13, 2005

Subsystems (6)
USB PCI Driver core Suspend to RAM +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover driver model wakeup flags

This is a refresh of the patch to add wakeup infrastructure to the driver model, in three parts. Comments? Merge?

pm-wake.patch ... adds the flags, and sysfs support pm-usb.patch ... usb devices use it pm-pci.patch ... pci devices use it

The default gives the current behavior: wakeup is enabled on all devices that support it. (Assuming ACPI etc don't get in the way...)

This goes back to having the sysfs "wakeup" file be tristate. Wakeup can be "enabled" or "disabled", and toggled from userspace. Or it can be unavailable, value "", if the hardware doesn't support wakeup or if no software has marked the device as supporting wakeup.

I attach a simple script I wrote to show what devices are capable of wakeup events. On one system, it reports wakeup allowed for several builtin devices and a USB keyboard/hub:

  lan        on  pci0000:00/0000:00:0a.0/0000:02:01.0
  modem      on  pci0000:00/0000:00:06.1
  hub        on  pci0000:00/0000:00:02.2/usb1
  usb_host   on  pci0000:00/0000:00:02.2
  input      on  pci0000:00/0000:00:02.1/usb3/3-1/3-1.1
  hub        on  pci0000:00/0000:00:02.1/usb3/3-1
  hub        on  pci0000:00/0000:00:02.1/usb3
  usb_host   on  pci0000:00/0000:00:02.1
  hub        on  pci0000:00/0000:00:02.0/usb2
  usb_host   on  pci0000:00/0000:00:02.0
  i2c        on  pci0000:00/0000:00:01.1

On this system /proc/acpi/wakeup says

  Device  Sleep state     Status
  USB0       3            disabled
  USB1       3            disabled
  USB2       3            disabled
  MAC0       3            disabled

I'm guessing those are the 00:02.* and 02:01.0 nodes. The hubs and input device (USB keyboard) use USB-internal wakeups and kick the usb_host nodes ... ACPI wouldn't know about them. (I understand that firewire could do the same sort of thing too.) The I2C device is actually SMBUS, waking up from D3hot or D3cold; I suspect that's handled by ACPI somehow.

- Dave

#1 lore raw

PATCH 1/3 driver model wakeup flags

I think we need "wakeup" infrastructure in the driver model core, so there's consistent support for userspace being able to

(a) tell if the device can be a wakeup source;
(b) change the default wakeup policy for any device.

For example, I have a USB mouse that lies about being able to issue wakeup events, so if Linux is ever going to suspend mice (for that save-2-Watts-with-usb-mouse-on-laptop scenario), it's got to prevent that from happening with this one.

So this is a fairly minimal patch doing just that.

- Dave

This is a refresh of an earlier patch to add "wakeup" support to the PM core model. This provides per-device bus-neutral control of the use of wakeup events.

* "struct device_pm_info" has two bits that are initialized as part of setting up the enclosing struct device:
- "can_wakeup", reflecting hardware capabilities
- "may_wakeup", the policy setting (when CONFIG_PM)

* There's a writeable sysfs "wakeup" file, with one of two values:
- "enabled", when the policy is to allow wakeup
- "disabled", when the policy is not to allow it
- "" if the device can't currently issue wakeups

By default, wakeup is enabled on all devices that support it. If its driver doesn't support it ... treat it as a bug. :)

include/linux/pm.h +25 -1
@@ -219,7 +219,9 @@ typedef struct pm_message {
 
 struct dev_pm_info {
 	pm_message_t		power_state;
+	unsigned		can_wakeup:1;
 #ifdef	CONFIG_PM
+	unsigned		should_wakeup:1;
 	pm_message_t		prev_state;
 	void			* saved_state;
 	atomic_t		pm_users;
@@ -236,13 +238,35 @@ extern void device_resume(void);
 
 #ifdef CONFIG_PM
 extern int device_suspend(pm_message_t state);
-#else
+
+#define device_set_wakeup_enable(dev,val) \
+	((dev)->power.should_wakeup = !!(val))
+#define device_may_wakeup(dev) \
+	(device_can_wakeup(dev) && (dev)->power.should_wakeup)
+
+#else /* !CONFIG_PM */
+
 static inline int device_suspend(pm_message_t state)
 {
 	return 0;
 }
+
+#define device_set_wakeup_enable(dev,val)	do{}while(0)
+#define device_may_wakeup(dev)			(0)
+
 #endif
 
+/* changes to device_may_wakeup take effect on the next pm state change.
+ * by default, devices should wakeup if they can.
+ */
+#define device_can_wakeup(dev) \
+	((dev)->power.can_wakeup)
+#define device_init_wakeup(dev,val) \
+	do { \
+		device_can_wakeup(dev) = !!(val); \
+		device_set_wakeup_enable(dev,val); \
+	} while(0)
+
 #endif /* __KERNEL__ */
 
 #endif /* _LINUX_PM_H */
drivers/base/core.c +1 -0
@@ -225,6 +225,7 @@ void device_initialize(struct device *de
 		   klist_children_put);
 	INIT_LIST_HEAD(&dev->dma_pools);
 	init_MUTEX(&dev->sem);
+	device_init_wakeup(dev, 0);
 }
 
 /**
drivers/base/power/sysfs.c +73 -0

81 lines not on this page · raw message

#2 lore raw

PATCH 2/3 usb device wakeup flags

In reply to David Brownell

Here's a patch showing how USB can detect wakeup-capable devices, such as hubs, keyboards, mice, and Ethernet adapters ... and use the policy setting to enable remote wakeup only when appropriate.

There's another USB patch on the way, switching the HCDs over to use the new driver model bits rather than the USB-only bits that do the same thing. I'm holding back on that till more of the HCD level PM updates get made.

- Dave

This patch teaches "usb_device" about the new driver model wakeup support:

- It updates device wakeup capabilities when entering a configuration with the WAKEUP attribute;

- During suspend processing it consults the policy bit to see whether it should enable wakeup for that device. (This resolves a FIXME to not assume the answer is always "yes"; some devices lie about supporting remote wakeup.)

Support for root hubs and the HCDs is separate (and more complex).

drivers/usb/core/hub.c +9 -7
@@ -1020,9 +1020,15 @@ void usb_set_device_state(struct usb_dev
 	spin_lock_irqsave(&device_state_lock, flags);
 	if (udev->state == USB_STATE_NOTATTACHED)
 		;	/* do nothing */
-	else if (new_state != USB_STATE_NOTATTACHED)
+	else if (new_state != USB_STATE_NOTATTACHED) {
 		udev->state = new_state;
-	else
+		if (new_state == USB_STATE_CONFIGURED)
+			device_init_wakeup(&udev->dev,
+				(udev->actconfig->desc.bmAttributes
+				 & USB_CONFIG_ATT_WAKEUP));
+		else if (new_state != USB_STATE_SUSPENDED)
+			device_init_wakeup(&udev->dev, 0);
+	} else
 		recursively_mark_NOTATTACHED(udev);
 	spin_unlock_irqrestore(&device_state_lock, flags);
 }
@@ -1546,11 +1552,7 @@ static int hub_port_suspend(struct usb_h
 	 * NOTE:  OTG devices may issue remote wakeup (or SRP) even when
 	 * we don't explicitly enable it here.
 	 */
-	if (udev->actconfig
-			// && FIXME (remote wakeup enabled on this bus)
-			// ... currently assuming it's always appropriate
-			&& (udev->actconfig->desc.bmAttributes
-				& USB_CONFIG_ATT_WAKEUP) != 0) {
+	if (device_may_wakeup(&udev->dev)) {
 		status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
 				USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
 				USB_DEVICE_REMOTE_WAKEUP, 0,
#3 lore raw

PATCH 3/3 pci device wakeup flags

In reply to David Brownell

And here's the PCI support, as driven by PCI PM capabilities. On systems using ACPI, most of these will be reported through ACPI. (But there will often also be PS/2 mouse and keyboard devices ...)

Two minor notes here. One, the PCI spec says we should init some PM stuff that we don't; that'll matter for wakeup from D3cold (but not now). Two, some oddness in the PCI device init sequence. Evidently some PPC patches are on the way, but nothing in the current tree will care about reverting that part of a recent patch.

- Dave

This patch teaches "pci_dev" about the new driver model wakeup support:

- It marks devices as supporting wakeup when "can issue PME#" is listed in its PCI PM capability.

- pci_enable_wake() refuses to enable wake if that's been disabled (e.g. through sysfs).

NOTE that a recent patch changed PCI probing, and this reverts part of that change ... so that driver model initialization is again done before the PCI setup.

(One issue is that the driver model "init + add == register" pattern isn't being used inside PCI ... and that probe change worsened the problem by making "add" do some "init" too. Maybe PCI should match the driver model more closely, and just grow a new "pci_dev_init" function.)

drivers/pci/probe.c +15 -1
@@ -571,6 +571,7 @@ static void pci_read_irq(struct pci_dev 
 static int pci_setup_device(struct pci_dev * dev)
 {
 	u32 class;
+	u16 pm;
 
 	sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
 		dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
@@ -598,6 +599,19 @@ static int pci_setup_device(struct pci_d
 		pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
 		pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
 		pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
+
+		/* PCI PM capable devices may be able to issue PME# (wakeup) */
+		pm = pci_find_capability(dev, PCI_CAP_ID_PM);
+		if (pm) {
+			pci_read_config_word(dev, pm + PCI_PM_PMC, &pm);
+			if (pm & PCI_PM_CAP_PME_MASK)
+				device_init_wakeup(&dev->dev, 1);
+
+			/* REVISIT: if (pm & PCI_PM_CAP_PME_D3cold) then
+			 * pci pm spec 1.2, section 3.2.4 says we should
+			 * init PCI_PM_CTRL_PME_{STATUS,ENABLE} ...
+			 */
+		}
 		break;
 
 	case PCI_HEADER_TYPE_BRIDGE:		    /* bridge header */
@@ -737,6 +751,7 @@ pci_scan_device(struct pci_bus *bus, int
 	memset(dev, 0, sizeof(struct pci_dev));
 	dev->bus = bus;
 	dev->sysdata = bus->sysdata;
+	device_initialize(&dev->dev);
 	dev->dev.parent = bus->bridge;
 	dev->dev.bus = &pci_bus_type;
 	dev->devfn = devfn;
@@ -759,7 +774,6 @@ pci_scan_device(struct pci_bus *bus, int
 
 void __devinit pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
 {
-	device_initialize(&dev->dev);
 	dev->dev.release = pci_release_dev;
 	pci_dev_get(dev);
 
drivers/pci/pci.c +4 -0
@@ -531,6 +531,10 @@ int pci_enable_wake(struct pci_dev *dev,
 	if (!pm) 
 		return enable ? -EIO : 0; 
 
+	/* don't enable unless policy set through driver core allows it */
+	if (!device_may_wakeup(&dev->dev) && enable)
+		return -EROFS;
+
 	/* Check device's ability to generate PME# */
 	pci_read_config_word(dev,pm+PCI_PM_PMC,&value);