diff mbox

[3/3] scsi: sd: add may_power_off sysfs entry

Message ID 1346309040-27112-4-git-send-email-aaron.lu@intel.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Aaron Lu Aug. 30, 2012, 6:44 a.m. UTC
If the platform is able to power off the scsi device, add a sysfs entry
may_power_off to let user control the may_power_off flag of the device.

This flag will control when the scsi device is runtime suspended, can we
remove power from it(e.g. let it enter D3 cold ACPI device state).

This flag is used in libata-acpi.c to decide which ACPI D-State it will
enter when runtime suspended.

Signed-off-by: Aaron Lu <aaron.lu@intel.com>
---
 drivers/scsi/sd.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
diff mbox

Patch

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 4df73e5..db25569 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -2584,6 +2584,38 @@  static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
 	return 0;
 }
 
+static ssize_t
+may_power_off_show(struct device *dev, struct device_attribute *attr,
+		   char *buf)
+{
+	struct scsi_device *sdev = to_scsi_device(dev);
+	return snprintf(buf, 10, "%d\n", sdev->may_power_off);
+}
+
+static ssize_t
+may_power_off_store(struct device *dev, struct device_attribute *attr,
+		    const char *buf, size_t count)
+{
+	int value = -EINVAL;
+	struct scsi_device *sdev = to_scsi_device(dev);
+
+	if (buf[1] == '\0' || (buf[1] == '\n' && buf[2] == '\0')) {
+		if (buf[0] == '1')
+			value = 1;
+		else if (buf[0] == '0')
+			value = 0;
+	}
+
+	if (value >= 0) {
+		sdev->may_power_off = value;
+		value = count;
+	}
+
+	return value;
+}
+DEVICE_ATTR(may_power_off, S_IRUGO | S_IWUSR,
+		may_power_off_show, may_power_off_store);
+
 /*
  * The asynchronous part of sd_probe
  */
@@ -2638,6 +2670,8 @@  static void sd_probe_async(void *data, async_cookie_t cookie)
 
 	sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
 		  sdp->removable ? "removable " : "");
+	if (sdp->can_power_off)
+		device_create_file(&sdp->sdev_gendev, &dev_attr_may_power_off);
 	scsi_autopm_put_device(sdp);
 	put_device(&sdkp->dev);
 }