Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 1 | #include <linux/libata.h> |
| 2 | #include <linux/cdrom.h> |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 3 | #include <linux/pm_runtime.h> |
Aaron Lu | 3dc6744 | 2013-01-15 17:21:00 +0800 | [diff] [blame] | 4 | #include <linux/module.h> |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 5 | #include <linux/pm_qos.h> |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 6 | #include <scsi/scsi_device.h> |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 7 | |
| 8 | #include "libata.h" |
| 9 | |
Aaron Lu | 3dc6744 | 2013-01-15 17:21:00 +0800 | [diff] [blame] | 10 | static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */ |
| 11 | module_param(zpodd_poweroff_delay, int, 0644); |
| 12 | MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds"); |
| 13 | |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 14 | enum odd_mech_type { |
| 15 | ODD_MECH_TYPE_SLOT, |
| 16 | ODD_MECH_TYPE_DRAWER, |
| 17 | ODD_MECH_TYPE_UNSUPPORTED, |
| 18 | }; |
| 19 | |
| 20 | struct zpodd { |
| 21 | enum odd_mech_type mech_type; /* init during probe, RO afterwards */ |
| 22 | struct ata_device *dev; |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 23 | |
| 24 | /* The following fields are synchronized by PM core. */ |
| 25 | bool from_notify; /* resumed as a result of |
| 26 | * acpi wake notification */ |
Aaron Lu | 3dc6744 | 2013-01-15 17:21:00 +0800 | [diff] [blame] | 27 | bool zp_ready; /* ZP ready state */ |
| 28 | unsigned long last_ready; /* last ZP ready timestamp */ |
| 29 | bool zp_sampled; /* ZP ready state sampled */ |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 30 | bool powered_off; /* ODD is powered off |
| 31 | * during suspend */ |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 34 | static int eject_tray(struct ata_device *dev) |
| 35 | { |
Sergei Shtylyov | d0887c4 | 2013-06-23 23:25:04 +0400 | [diff] [blame] | 36 | struct ata_taskfile tf; |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 37 | const char cdb[] = { GPCMD_START_STOP_UNIT, |
| 38 | 0, 0, 0, |
| 39 | 0x02, /* LoEj */ |
| 40 | 0, 0, 0, 0, 0, 0, 0, |
| 41 | }; |
| 42 | |
Sergei Shtylyov | d0887c4 | 2013-06-23 23:25:04 +0400 | [diff] [blame] | 43 | ata_tf_init(dev, &tf); |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 44 | tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 45 | tf.command = ATA_CMD_PACKET; |
| 46 | tf.protocol = ATAPI_PROT_NODATA; |
| 47 | |
| 48 | return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0); |
| 49 | } |
| 50 | |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 51 | /* Per the spec, only slot type and drawer type ODD can be supported */ |
| 52 | static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev) |
| 53 | { |
| 54 | char buf[16]; |
| 55 | unsigned int ret; |
| 56 | struct rm_feature_desc *desc = (void *)(buf + 8); |
Sergei Shtylyov | d0887c4 | 2013-06-23 23:25:04 +0400 | [diff] [blame] | 57 | struct ata_taskfile tf; |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 58 | char cdb[] = { GPCMD_GET_CONFIGURATION, |
| 59 | 2, /* only 1 feature descriptor requested */ |
| 60 | 0, 3, /* 3, removable medium feature */ |
| 61 | 0, 0, 0,/* reserved */ |
| 62 | 0, sizeof(buf), |
| 63 | 0, 0, 0, |
| 64 | }; |
| 65 | |
Sergei Shtylyov | d0887c4 | 2013-06-23 23:25:04 +0400 | [diff] [blame] | 66 | ata_tf_init(dev, &tf); |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 67 | tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 68 | tf.command = ATA_CMD_PACKET; |
| 69 | tf.protocol = ATAPI_PROT_PIO; |
| 70 | tf.lbam = sizeof(buf); |
| 71 | |
| 72 | ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, |
| 73 | buf, sizeof(buf), 0); |
| 74 | if (ret) |
| 75 | return ODD_MECH_TYPE_UNSUPPORTED; |
| 76 | |
| 77 | if (be16_to_cpu(desc->feature_code) != 3) |
| 78 | return ODD_MECH_TYPE_UNSUPPORTED; |
| 79 | |
| 80 | if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1) |
| 81 | return ODD_MECH_TYPE_SLOT; |
| 82 | else if (desc->mech_type == 1 && desc->load == 0 && desc->eject == 1) |
| 83 | return ODD_MECH_TYPE_DRAWER; |
| 84 | else |
| 85 | return ODD_MECH_TYPE_UNSUPPORTED; |
| 86 | } |
| 87 | |
| 88 | static bool odd_can_poweroff(struct ata_device *ata_dev) |
| 89 | { |
| 90 | acpi_handle handle; |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 91 | struct acpi_device *acpi_dev; |
| 92 | |
| 93 | handle = ata_dev_acpi_handle(ata_dev); |
| 94 | if (!handle) |
| 95 | return false; |
| 96 | |
Yijing Wang | e2fc623 | 2013-11-20 17:28:25 +0800 | [diff] [blame] | 97 | if (acpi_bus_get_device(handle, &acpi_dev)) |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 98 | return false; |
| 99 | |
| 100 | return acpi_device_can_poweroff(acpi_dev); |
| 101 | } |
| 102 | |
Aaron Lu | 3dc6744 | 2013-01-15 17:21:00 +0800 | [diff] [blame] | 103 | /* Test if ODD is zero power ready by sense code */ |
| 104 | static bool zpready(struct ata_device *dev) |
| 105 | { |
| 106 | u8 sense_key, *sense_buf; |
| 107 | unsigned int ret, asc, ascq, add_len; |
| 108 | struct zpodd *zpodd = dev->zpodd; |
| 109 | |
| 110 | ret = atapi_eh_tur(dev, &sense_key); |
| 111 | |
| 112 | if (!ret || sense_key != NOT_READY) |
| 113 | return false; |
| 114 | |
| 115 | sense_buf = dev->link->ap->sector_buf; |
| 116 | ret = atapi_eh_request_sense(dev, sense_buf, sense_key); |
| 117 | if (ret) |
| 118 | return false; |
| 119 | |
| 120 | /* sense valid */ |
| 121 | if ((sense_buf[0] & 0x7f) != 0x70) |
| 122 | return false; |
| 123 | |
| 124 | add_len = sense_buf[7]; |
| 125 | /* has asc and ascq */ |
| 126 | if (add_len < 6) |
| 127 | return false; |
| 128 | |
| 129 | asc = sense_buf[12]; |
| 130 | ascq = sense_buf[13]; |
| 131 | |
| 132 | if (zpodd->mech_type == ODD_MECH_TYPE_SLOT) |
| 133 | /* no media inside */ |
| 134 | return asc == 0x3a; |
| 135 | else |
| 136 | /* no media inside and door closed */ |
| 137 | return asc == 0x3a && ascq == 0x01; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Update the zpodd->zp_ready field. This field will only be set |
| 142 | * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay |
| 143 | * time, and will be used to decide if power off is allowed. If it |
| 144 | * is set, it will be cleared during resume from powered off state. |
| 145 | */ |
| 146 | void zpodd_on_suspend(struct ata_device *dev) |
| 147 | { |
| 148 | struct zpodd *zpodd = dev->zpodd; |
| 149 | unsigned long expires; |
| 150 | |
| 151 | if (!zpready(dev)) { |
| 152 | zpodd->zp_sampled = false; |
Aaron Lu | a59b9aa | 2013-01-15 17:21:02 +0800 | [diff] [blame] | 153 | zpodd->zp_ready = false; |
Aaron Lu | 3dc6744 | 2013-01-15 17:21:00 +0800 | [diff] [blame] | 154 | return; |
| 155 | } |
| 156 | |
| 157 | if (!zpodd->zp_sampled) { |
| 158 | zpodd->zp_sampled = true; |
| 159 | zpodd->last_ready = jiffies; |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | expires = zpodd->last_ready + |
| 164 | msecs_to_jiffies(zpodd_poweroff_delay * 1000); |
| 165 | if (time_before(jiffies, expires)) |
| 166 | return; |
| 167 | |
| 168 | zpodd->zp_ready = true; |
| 169 | } |
| 170 | |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 171 | bool zpodd_zpready(struct ata_device *dev) |
| 172 | { |
| 173 | struct zpodd *zpodd = dev->zpodd; |
| 174 | return zpodd->zp_ready; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Enable runtime wake capability through ACPI and set the powered_off flag, |
| 179 | * this flag will be used during resume to decide what operations are needed |
| 180 | * to take. |
Aaron Lu | 6f4c827 | 2013-01-23 15:09:32 +0800 | [diff] [blame] | 181 | * |
| 182 | * Also, media poll needs to be silenced, so that it doesn't bring the ODD |
| 183 | * back to full power state every few seconds. |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 184 | */ |
| 185 | void zpodd_enable_run_wake(struct ata_device *dev) |
| 186 | { |
| 187 | struct zpodd *zpodd = dev->zpodd; |
| 188 | |
Aaron Lu | 6f4c827 | 2013-01-23 15:09:32 +0800 | [diff] [blame] | 189 | sdev_disable_disk_events(dev->sdev); |
| 190 | |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 191 | zpodd->powered_off = true; |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 192 | device_set_run_wake(&dev->tdev, true); |
| 193 | acpi_pm_device_run_wake(&dev->tdev, true); |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Disable runtime wake capability if it is enabled */ |
| 197 | void zpodd_disable_run_wake(struct ata_device *dev) |
| 198 | { |
| 199 | struct zpodd *zpodd = dev->zpodd; |
| 200 | |
| 201 | if (zpodd->powered_off) { |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 202 | acpi_pm_device_run_wake(&dev->tdev, false); |
| 203 | device_set_run_wake(&dev->tdev, false); |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Post power on processing after the ODD has been recovered. If the |
| 209 | * ODD wasn't powered off during suspend, it doesn't do anything. |
| 210 | * |
| 211 | * For drawer type ODD, if it is powered on due to user pressed the |
| 212 | * eject button, the tray needs to be ejected. This can only be done |
| 213 | * after the ODD has been recovered, i.e. link is initialized and |
| 214 | * device is able to process NON_DATA PIO command, as eject needs to |
| 215 | * send command for the ODD to process. |
| 216 | * |
| 217 | * The from_notify flag set in wake notification handler function |
| 218 | * zpodd_wake_dev represents if power on is due to user's action. |
| 219 | * |
| 220 | * For both types of ODD, several fields need to be reset. |
| 221 | */ |
| 222 | void zpodd_post_poweron(struct ata_device *dev) |
| 223 | { |
| 224 | struct zpodd *zpodd = dev->zpodd; |
| 225 | |
| 226 | if (!zpodd->powered_off) |
| 227 | return; |
| 228 | |
| 229 | zpodd->powered_off = false; |
| 230 | |
| 231 | if (zpodd->from_notify) { |
| 232 | zpodd->from_notify = false; |
| 233 | if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER) |
| 234 | eject_tray(dev); |
| 235 | } |
| 236 | |
| 237 | zpodd->zp_sampled = false; |
| 238 | zpodd->zp_ready = false; |
Aaron Lu | 6f4c827 | 2013-01-23 15:09:32 +0800 | [diff] [blame] | 239 | |
| 240 | sdev_enable_disk_events(dev->sdev); |
Aaron Lu | 2133420 | 2013-01-15 17:21:01 +0800 | [diff] [blame] | 241 | } |
| 242 | |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 243 | static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context) |
| 244 | { |
| 245 | struct ata_device *ata_dev = context; |
| 246 | struct zpodd *zpodd = ata_dev->zpodd; |
| 247 | struct device *dev = &ata_dev->sdev->sdev_gendev; |
| 248 | |
Aaron Lu | 53637e0 | 2013-01-28 13:08:02 +0800 | [diff] [blame] | 249 | if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) { |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 250 | zpodd->from_notify = true; |
| 251 | pm_runtime_resume(dev); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | static void ata_acpi_add_pm_notifier(struct ata_device *dev) |
| 256 | { |
| 257 | acpi_handle handle = ata_dev_acpi_handle(dev); |
| 258 | acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 259 | zpodd_wake_dev, dev); |
| 260 | } |
| 261 | |
| 262 | static void ata_acpi_remove_pm_notifier(struct ata_device *dev) |
| 263 | { |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 264 | acpi_handle handle = ata_dev_acpi_handle(dev); |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 265 | acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev); |
| 266 | } |
| 267 | |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 268 | void zpodd_init(struct ata_device *dev) |
| 269 | { |
| 270 | enum odd_mech_type mech_type; |
| 271 | struct zpodd *zpodd; |
| 272 | |
| 273 | if (dev->zpodd) |
| 274 | return; |
| 275 | |
| 276 | if (!odd_can_poweroff(dev)) |
| 277 | return; |
| 278 | |
| 279 | mech_type = zpodd_get_mech_type(dev); |
| 280 | if (mech_type == ODD_MECH_TYPE_UNSUPPORTED) |
| 281 | return; |
| 282 | |
| 283 | zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL); |
| 284 | if (!zpodd) |
| 285 | return; |
| 286 | |
| 287 | zpodd->mech_type = mech_type; |
| 288 | |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 289 | ata_acpi_add_pm_notifier(dev); |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 290 | zpodd->dev = dev; |
| 291 | dev->zpodd = zpodd; |
Aaron Lu | f1bc1e4 | 2013-08-23 10:17:54 +0800 | [diff] [blame] | 292 | dev_pm_qos_expose_flags(&dev->tdev, 0); |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void zpodd_exit(struct ata_device *dev) |
| 296 | { |
Aaron Lu | f064a20 | 2013-01-15 17:20:59 +0800 | [diff] [blame] | 297 | ata_acpi_remove_pm_notifier(dev); |
Aaron Lu | afe7595 | 2013-01-15 17:20:58 +0800 | [diff] [blame] | 298 | kfree(dev->zpodd); |
| 299 | dev->zpodd = NULL; |
| 300 | } |