1 #include <linux/libata.h>
2 #include <linux/cdrom.h>
3 #include <linux/pm_runtime.h>
4 #include <linux/module.h>
5 #include <linux/pm_qos.h>
6 #include <scsi/scsi_device.h>
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");
17 ODD_MECH_TYPE_UNSUPPORTED
,
21 enum odd_mech_type mech_type
; /* init during probe, RO afterwards */
22 struct ata_device
*dev
;
24 /* The following fields are synchronized by PM core. */
25 bool from_notify
; /* resumed as a result of
26 * acpi wake notification */
27 bool zp_ready
; /* ZP ready state */
28 unsigned long last_ready
; /* last ZP ready timestamp */
29 bool zp_sampled
; /* ZP ready state sampled */
30 bool powered_off
; /* ODD is powered off
34 static int eject_tray(struct ata_device
*dev
)
36 struct ata_taskfile tf
;
37 const char cdb
[] = { GPCMD_START_STOP_UNIT
,
43 ata_tf_init(dev
, &tf
);
44 tf
.flags
= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
45 tf
.command
= ATA_CMD_PACKET
;
46 tf
.protocol
= ATAPI_PROT_NODATA
;
48 return ata_exec_internal(dev
, &tf
, cdb
, DMA_NONE
, NULL
, 0, 0);
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
)
56 struct rm_feature_desc
*desc
= (void *)(buf
+ 8);
57 struct ata_taskfile tf
;
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 */
66 ata_tf_init(dev
, &tf
);
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
);
72 ret
= ata_exec_internal(dev
, &tf
, cdb
, DMA_FROM_DEVICE
,
75 return ODD_MECH_TYPE_UNSUPPORTED
;
77 if (be16_to_cpu(desc
->feature_code
) != 3)
78 return ODD_MECH_TYPE_UNSUPPORTED
;
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
;
85 return ODD_MECH_TYPE_UNSUPPORTED
;
88 /* Test if ODD is zero power ready by sense code */
89 static bool zpready(struct ata_device
*dev
)
91 u8 sense_key
, *sense_buf
;
92 unsigned int ret
, asc
, ascq
, add_len
;
93 struct zpodd
*zpodd
= dev
->zpodd
;
95 ret
= atapi_eh_tur(dev
, &sense_key
);
97 if (!ret
|| sense_key
!= NOT_READY
)
100 sense_buf
= dev
->link
->ap
->sector_buf
;
101 ret
= atapi_eh_request_sense(dev
, sense_buf
, sense_key
);
106 if ((sense_buf
[0] & 0x7f) != 0x70)
109 add_len
= sense_buf
[7];
110 /* has asc and ascq */
115 ascq
= sense_buf
[13];
117 if (zpodd
->mech_type
== ODD_MECH_TYPE_SLOT
)
118 /* no media inside */
121 /* no media inside and door closed */
122 return asc
== 0x3a && ascq
== 0x01;
126 * Update the zpodd->zp_ready field. This field will only be set
127 * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
128 * time, and will be used to decide if power off is allowed. If it
129 * is set, it will be cleared during resume from powered off state.
131 void zpodd_on_suspend(struct ata_device
*dev
)
133 struct zpodd
*zpodd
= dev
->zpodd
;
134 unsigned long expires
;
137 zpodd
->zp_sampled
= false;
138 zpodd
->zp_ready
= false;
142 if (!zpodd
->zp_sampled
) {
143 zpodd
->zp_sampled
= true;
144 zpodd
->last_ready
= jiffies
;
148 expires
= zpodd
->last_ready
+
149 msecs_to_jiffies(zpodd_poweroff_delay
* 1000);
150 if (time_before(jiffies
, expires
))
153 zpodd
->zp_ready
= true;
156 bool zpodd_zpready(struct ata_device
*dev
)
158 struct zpodd
*zpodd
= dev
->zpodd
;
159 return zpodd
->zp_ready
;
163 * Enable runtime wake capability through ACPI and set the powered_off flag,
164 * this flag will be used during resume to decide what operations are needed
167 * Also, media poll needs to be silenced, so that it doesn't bring the ODD
168 * back to full power state every few seconds.
170 void zpodd_enable_run_wake(struct ata_device
*dev
)
172 struct zpodd
*zpodd
= dev
->zpodd
;
174 sdev_disable_disk_events(dev
->sdev
);
176 zpodd
->powered_off
= true;
177 acpi_pm_set_device_wakeup(&dev
->tdev
, true);
180 /* Disable runtime wake capability if it is enabled */
181 void zpodd_disable_run_wake(struct ata_device
*dev
)
183 struct zpodd
*zpodd
= dev
->zpodd
;
185 if (zpodd
->powered_off
)
186 acpi_pm_set_device_wakeup(&dev
->tdev
, false);
190 * Post power on processing after the ODD has been recovered. If the
191 * ODD wasn't powered off during suspend, it doesn't do anything.
193 * For drawer type ODD, if it is powered on due to user pressed the
194 * eject button, the tray needs to be ejected. This can only be done
195 * after the ODD has been recovered, i.e. link is initialized and
196 * device is able to process NON_DATA PIO command, as eject needs to
197 * send command for the ODD to process.
199 * The from_notify flag set in wake notification handler function
200 * zpodd_wake_dev represents if power on is due to user's action.
202 * For both types of ODD, several fields need to be reset.
204 void zpodd_post_poweron(struct ata_device
*dev
)
206 struct zpodd
*zpodd
= dev
->zpodd
;
208 if (!zpodd
->powered_off
)
211 zpodd
->powered_off
= false;
213 if (zpodd
->from_notify
) {
214 zpodd
->from_notify
= false;
215 if (zpodd
->mech_type
== ODD_MECH_TYPE_DRAWER
)
219 zpodd
->zp_sampled
= false;
220 zpodd
->zp_ready
= false;
222 sdev_enable_disk_events(dev
->sdev
);
225 static void zpodd_wake_dev(acpi_handle handle
, u32 event
, void *context
)
227 struct ata_device
*ata_dev
= context
;
228 struct zpodd
*zpodd
= ata_dev
->zpodd
;
229 struct device
*dev
= &ata_dev
->sdev
->sdev_gendev
;
231 if (event
== ACPI_NOTIFY_DEVICE_WAKE
&& pm_runtime_suspended(dev
)) {
232 zpodd
->from_notify
= true;
233 pm_runtime_resume(dev
);
237 static void ata_acpi_add_pm_notifier(struct ata_device
*dev
)
239 acpi_handle handle
= ata_dev_acpi_handle(dev
);
240 acpi_install_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
,
241 zpodd_wake_dev
, dev
);
244 static void ata_acpi_remove_pm_notifier(struct ata_device
*dev
)
246 acpi_handle handle
= ata_dev_acpi_handle(dev
);
247 acpi_remove_notify_handler(handle
, ACPI_SYSTEM_NOTIFY
, zpodd_wake_dev
);
250 void zpodd_init(struct ata_device
*dev
)
252 struct acpi_device
*adev
= ACPI_COMPANION(&dev
->tdev
);
253 enum odd_mech_type mech_type
;
256 if (dev
->zpodd
|| !adev
|| !acpi_device_can_poweroff(adev
))
259 mech_type
= zpodd_get_mech_type(dev
);
260 if (mech_type
== ODD_MECH_TYPE_UNSUPPORTED
)
263 zpodd
= kzalloc(sizeof(struct zpodd
), GFP_KERNEL
);
267 zpodd
->mech_type
= mech_type
;
269 ata_acpi_add_pm_notifier(dev
);
272 dev_pm_qos_expose_flags(&dev
->tdev
, 0);
275 void zpodd_exit(struct ata_device
*dev
)
277 ata_acpi_remove_pm_notifier(dev
);