*     @dev: device to examine; its driver model wakeup flags control
  *             whether it should be able to wake up the system
  *     @d_min_p: used to store the upper limit of allowed states range
- *     Return value: preferred power state of the device on success, -ENODEV on
- *             failure (ie. if there's no 'struct acpi_device' for @dev)
+ *     @d_max_in: specify the lowest allowed states
+ *     Return value: preferred power state of the device on success, -ENODEV
+ *     (ie. if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  *
  *     Find the lowest power (highest number) ACPI device power state that
  *     device @dev can be in while the system is in the sleep state represented
  *     via @wake.
  */
 
-int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p)
+int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
 {
        acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
        struct acpi_device *adev;
        char acpi_method[] = "_SxD";
        unsigned long long d_min, d_max;
 
+       if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
+               return -EINVAL;
        if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
                printk(KERN_DEBUG "ACPI handle has no context!\n");
                return -ENODEV;
 
        acpi_method[2] = '0' + acpi_target_sleep_state;
        /*
-        * If the sleep state is S0, we will return D3, but if the device has
-        * _S0W, we will use the value from _S0W
+        * If the sleep state is S0, the lowest limit from ACPI is D3,
+        * but if the device has _S0W, we will use the value from _S0W
+        * as the lowest limit from ACPI.  Finally, we will constrain
+        * the lowest limit with the specified one.
         */
        d_min = ACPI_STATE_D0;
        d_max = ACPI_STATE_D3;
                }
        }
 
+       if (d_max_in < d_min)
+               return -EINVAL;
        if (d_min_p)
                *d_min_p = d_min;
+       /* constrain d_max with specified lowest limit (max number) */
+       if (d_max > d_max_in) {
+               for (d_max = d_max_in; d_max > d_min; d_max--) {
+                       if (adev->power.states[d_max].flags.valid)
+                               break;
+               }
+       }
        return d_max;
 }
 #endif /* CONFIG_PM */
 
 int acpi_disable_wakeup_device_power(struct acpi_device *dev);
 
 #ifdef CONFIG_PM
-int acpi_pm_device_sleep_state(struct device *, int *);
+int acpi_pm_device_sleep_state(struct device *, int *, int);
 #else
-static inline int acpi_pm_device_sleep_state(struct device *d, int *p)
+static inline int acpi_pm_device_sleep_state(struct device *d, int *p, int m)
 {
        if (p)
                *p = ACPI_STATE_D0;
-       return ACPI_STATE_D3;
+       return (m >= ACPI_STATE_D0 && m <= ACPI_STATE_D3) ? m : ACPI_STATE_D0;
 }
 #endif