1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/libata.h> 3 #include <linux/cdrom.h> 4 #include <linux/pm_runtime.h> 5 #include <linux/module.h> 6 #include <linux/pm_qos.h> 7 #include <scsi/scsi_device.h> 8 9 #include "libata.h" 10 11 static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */ 12 module_param(zpodd_poweroff_delay, int, 0644); 13 MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds"); 14 15 enum odd_mech_type { 16 ODD_MECH_TYPE_SLOT, 17 ODD_MECH_TYPE_DRAWER, 18 ODD_MECH_TYPE_UNSUPPORTED, 19 }; 20 21 struct zpodd { 22 enum odd_mech_type mech_type; /* init during probe, RO afterwards */ 23 struct ata_device *dev; 24 25 /* The following fields are synchronized by PM core. */ 26 bool from_notify; /* resumed as a result of 27 * acpi wake notification */ 28 bool zp_ready; /* ZP ready state */ 29 unsigned long last_ready; /* last ZP ready timestamp */ 30 bool zp_sampled; /* ZP ready state sampled */ 31 bool powered_off; /* ODD is powered off 32 * during suspend */ 33 }; 34 35 static int eject_tray(struct ata_device *dev) 36 { 37 struct ata_taskfile tf; 38 static const char cdb[ATAPI_CDB_LEN] = { GPCMD_START_STOP_UNIT, 39 0, 0, 0, 40 0x02, /* LoEj */ 41 0, 0, 0, 0, 0, 0, 0, 42 }; 43 44 ata_tf_init(dev, &tf); 45 tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 46 tf.command = ATA_CMD_PACKET; 47 tf.protocol = ATAPI_PROT_NODATA; 48 49 return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0); 50 } 51 52 /* Per the spec, only slot type and drawer type ODD can be supported */ 53 static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev) 54 { 55 char *buf; 56 unsigned int ret; 57 struct rm_feature_desc *desc; 58 struct ata_taskfile tf; 59 static const char cdb[ATAPI_CDB_LEN] = { GPCMD_GET_CONFIGURATION, 60 2, /* only 1 feature descriptor requested */ 61 0, 3, /* 3, removable medium feature */ 62 0, 0, 0,/* reserved */ 63 0, 16, 64 0, 0, 0, 65 }; 66 67 buf = kzalloc(16, GFP_KERNEL); 68 if (!buf) 69 return ODD_MECH_TYPE_UNSUPPORTED; 70 desc = (void *)(buf + 8); 71 72 ata_tf_init(dev, &tf); 73 tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 74 tf.command = ATA_CMD_PACKET; 75 tf.protocol = ATAPI_PROT_PIO; 76 tf.lbam = 16; 77 78 ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE, 79 buf, 16, 0); 80 if (ret) { 81 kfree(buf); 82 return ODD_MECH_TYPE_UNSUPPORTED; 83 } 84 85 if (be16_to_cpu(desc->feature_code) != 3) { 86 kfree(buf); 87 return ODD_MECH_TYPE_UNSUPPORTED; 88 } 89 90 if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1) { 91 kfree(buf); 92 return ODD_MECH_TYPE_SLOT; 93 } else if (desc->mech_type == 1 && desc->load == 0 && 94 desc->eject == 1) { 95 kfree(buf); 96 return ODD_MECH_TYPE_DRAWER; 97 } else { 98 kfree(buf); 99 return ODD_MECH_TYPE_UNSUPPORTED; 100 } 101 } 102 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->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; 153 zpodd->zp_ready = false; 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 + secs_to_jiffies(zpodd_poweroff_delay); 164 if (time_before(jiffies, expires)) 165 return; 166 167 zpodd->zp_ready = true; 168 } 169 170 bool zpodd_zpready(struct ata_device *dev) 171 { 172 struct zpodd *zpodd = dev->zpodd; 173 return zpodd->zp_ready; 174 } 175 176 /* 177 * Enable runtime wake capability through ACPI and set the powered_off flag, 178 * this flag will be used during resume to decide what operations are needed 179 * to take. 180 * 181 * Also, media poll needs to be silenced, so that it doesn't bring the ODD 182 * back to full power state every few seconds. 183 */ 184 void zpodd_enable_run_wake(struct ata_device *dev) 185 { 186 struct zpodd *zpodd = dev->zpodd; 187 188 sdev_disable_disk_events(dev->sdev); 189 190 zpodd->powered_off = true; 191 acpi_pm_set_device_wakeup(&dev->tdev, true); 192 } 193 194 /* Disable runtime wake capability if it is enabled */ 195 void zpodd_disable_run_wake(struct ata_device *dev) 196 { 197 struct zpodd *zpodd = dev->zpodd; 198 199 if (zpodd->powered_off) 200 acpi_pm_set_device_wakeup(&dev->tdev, false); 201 } 202 203 /* 204 * Post power on processing after the ODD has been recovered. If the 205 * ODD wasn't powered off during suspend, it doesn't do anything. 206 * 207 * For drawer type ODD, if it is powered on due to user pressed the 208 * eject button, the tray needs to be ejected. This can only be done 209 * after the ODD has been recovered, i.e. link is initialized and 210 * device is able to process NON_DATA PIO command, as eject needs to 211 * send command for the ODD to process. 212 * 213 * The from_notify flag set in wake notification handler function 214 * zpodd_wake_dev represents if power on is due to user's action. 215 * 216 * For both types of ODD, several fields need to be reset. 217 */ 218 void zpodd_post_poweron(struct ata_device *dev) 219 { 220 struct zpodd *zpodd = dev->zpodd; 221 222 if (!zpodd->powered_off) 223 return; 224 225 zpodd->powered_off = false; 226 227 if (zpodd->from_notify) { 228 zpodd->from_notify = false; 229 if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER) 230 eject_tray(dev); 231 } 232 233 zpodd->zp_sampled = false; 234 zpodd->zp_ready = false; 235 236 sdev_enable_disk_events(dev->sdev); 237 } 238 239 static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context) 240 { 241 struct ata_device *ata_dev = context; 242 struct zpodd *zpodd = ata_dev->zpodd; 243 struct device *dev = &ata_dev->sdev->sdev_gendev; 244 245 if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) { 246 zpodd->from_notify = true; 247 pm_runtime_resume(dev); 248 } 249 } 250 251 static void ata_acpi_add_pm_notifier(struct ata_device *dev) 252 { 253 acpi_handle handle = ata_dev_acpi_handle(dev); 254 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, 255 zpodd_wake_dev, dev); 256 } 257 258 static void ata_acpi_remove_pm_notifier(struct ata_device *dev) 259 { 260 acpi_handle handle = ata_dev_acpi_handle(dev); 261 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev); 262 } 263 264 void zpodd_init(struct ata_device *dev) 265 { 266 struct acpi_device *adev = ACPI_COMPANION(&dev->tdev); 267 enum odd_mech_type mech_type; 268 struct zpodd *zpodd; 269 270 if (dev->zpodd || !adev || !acpi_device_can_poweroff(adev)) 271 return; 272 273 mech_type = zpodd_get_mech_type(dev); 274 if (mech_type == ODD_MECH_TYPE_UNSUPPORTED) 275 return; 276 277 zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL); 278 if (!zpodd) 279 return; 280 281 zpodd->mech_type = mech_type; 282 283 ata_acpi_add_pm_notifier(dev); 284 zpodd->dev = dev; 285 dev->zpodd = zpodd; 286 dev_pm_qos_expose_flags(&dev->tdev, 0); 287 } 288 289 void zpodd_exit(struct ata_device *dev) 290 { 291 ata_acpi_remove_pm_notifier(dev); 292 kfree(dev->zpodd); 293 dev->zpodd = NULL; 294 } 295