blob: 8a01d09ac4db7f7d0f4437bbcd5e83a07b9ff21d [file] [log] [blame]
Aaron Luafe75952013-01-15 17:20:58 +08001#include <linux/libata.h>
2#include <linux/cdrom.h>
Aaron Luf064a202013-01-15 17:20:59 +08003#include <linux/pm_runtime.h>
Aaron Lu3dc67442013-01-15 17:21:00 +08004#include <linux/module.h>
Aaron Luf1bc1e42013-08-23 10:17:54 +08005#include <linux/pm_qos.h>
Aaron Luf064a202013-01-15 17:20:59 +08006#include <scsi/scsi_device.h>
Aaron Luafe75952013-01-15 17:20:58 +08007
8#include "libata.h"
9
Aaron Lu3dc67442013-01-15 17:21:00 +080010static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */
11module_param(zpodd_poweroff_delay, int, 0644);
12MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds");
13
Aaron Luafe75952013-01-15 17:20:58 +080014enum odd_mech_type {
15 ODD_MECH_TYPE_SLOT,
16 ODD_MECH_TYPE_DRAWER,
17 ODD_MECH_TYPE_UNSUPPORTED,
18};
19
20struct zpodd {
21 enum odd_mech_type mech_type; /* init during probe, RO afterwards */
22 struct ata_device *dev;
Aaron Luf064a202013-01-15 17:20:59 +080023
24 /* The following fields are synchronized by PM core. */
25 bool from_notify; /* resumed as a result of
26 * acpi wake notification */
Aaron Lu3dc67442013-01-15 17:21:00 +080027 bool zp_ready; /* ZP ready state */
28 unsigned long last_ready; /* last ZP ready timestamp */
29 bool zp_sampled; /* ZP ready state sampled */
Aaron Lu21334202013-01-15 17:21:01 +080030 bool powered_off; /* ODD is powered off
31 * during suspend */
Aaron Luafe75952013-01-15 17:20:58 +080032};
33
Aaron Lu21334202013-01-15 17:21:01 +080034static int eject_tray(struct ata_device *dev)
35{
Sergei Shtylyovd0887c42013-06-23 23:25:04 +040036 struct ata_taskfile tf;
Aaron Lu21334202013-01-15 17:21:01 +080037 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 Shtylyovd0887c42013-06-23 23:25:04 +040043 ata_tf_init(dev, &tf);
Aaron Lu21334202013-01-15 17:21:01 +080044 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 Luafe75952013-01-15 17:20:58 +080051/* Per the spec, only slot type and drawer type ODD can be supported */
52static 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 Shtylyovd0887c42013-06-23 23:25:04 +040057 struct ata_taskfile tf;
Aaron Luafe75952013-01-15 17:20:58 +080058 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 Shtylyovd0887c42013-06-23 23:25:04 +040066 ata_tf_init(dev, &tf);
Aaron Luafe75952013-01-15 17:20:58 +080067 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
Aaron Lu3dc67442013-01-15 17:21:00 +080088/* Test if ODD is zero power ready by sense code */
89static bool zpready(struct ata_device *dev)
90{
91 u8 sense_key, *sense_buf;
92 unsigned int ret, asc, ascq, add_len;
93 struct zpodd *zpodd = dev->zpodd;
94
95 ret = atapi_eh_tur(dev, &sense_key);
96
97 if (!ret || sense_key != NOT_READY)
98 return false;
99
100 sense_buf = dev->link->ap->sector_buf;
101 ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
102 if (ret)
103 return false;
104
105 /* sense valid */
106 if ((sense_buf[0] & 0x7f) != 0x70)
107 return false;
108
109 add_len = sense_buf[7];
110 /* has asc and ascq */
111 if (add_len < 6)
112 return false;
113
114 asc = sense_buf[12];
115 ascq = sense_buf[13];
116
117 if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
118 /* no media inside */
119 return asc == 0x3a;
120 else
121 /* no media inside and door closed */
122 return asc == 0x3a && ascq == 0x01;
123}
124
125/*
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.
130 */
131void zpodd_on_suspend(struct ata_device *dev)
132{
133 struct zpodd *zpodd = dev->zpodd;
134 unsigned long expires;
135
136 if (!zpready(dev)) {
137 zpodd->zp_sampled = false;
Aaron Lua59b9aa2013-01-15 17:21:02 +0800138 zpodd->zp_ready = false;
Aaron Lu3dc67442013-01-15 17:21:00 +0800139 return;
140 }
141
142 if (!zpodd->zp_sampled) {
143 zpodd->zp_sampled = true;
144 zpodd->last_ready = jiffies;
145 return;
146 }
147
148 expires = zpodd->last_ready +
149 msecs_to_jiffies(zpodd_poweroff_delay * 1000);
150 if (time_before(jiffies, expires))
151 return;
152
153 zpodd->zp_ready = true;
154}
155
Aaron Lu21334202013-01-15 17:21:01 +0800156bool zpodd_zpready(struct ata_device *dev)
157{
158 struct zpodd *zpodd = dev->zpodd;
159 return zpodd->zp_ready;
160}
161
162/*
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
165 * to take.
Aaron Lu6f4c8272013-01-23 15:09:32 +0800166 *
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.
Aaron Lu21334202013-01-15 17:21:01 +0800169 */
170void zpodd_enable_run_wake(struct ata_device *dev)
171{
172 struct zpodd *zpodd = dev->zpodd;
173
Aaron Lu6f4c8272013-01-23 15:09:32 +0800174 sdev_disable_disk_events(dev->sdev);
175
Aaron Lu21334202013-01-15 17:21:01 +0800176 zpodd->powered_off = true;
Rafael J. Wysocki4d183d02017-06-24 01:54:39 +0200177 acpi_pm_set_device_wakeup(&dev->tdev, true);
Aaron Lu21334202013-01-15 17:21:01 +0800178}
179
180/* Disable runtime wake capability if it is enabled */
181void zpodd_disable_run_wake(struct ata_device *dev)
182{
183 struct zpodd *zpodd = dev->zpodd;
184
Rafael J. Wysocki4d183d02017-06-24 01:54:39 +0200185 if (zpodd->powered_off)
186 acpi_pm_set_device_wakeup(&dev->tdev, false);
Aaron Lu21334202013-01-15 17:21:01 +0800187}
188
189/*
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.
192 *
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.
198 *
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.
201 *
202 * For both types of ODD, several fields need to be reset.
203 */
204void zpodd_post_poweron(struct ata_device *dev)
205{
206 struct zpodd *zpodd = dev->zpodd;
207
208 if (!zpodd->powered_off)
209 return;
210
211 zpodd->powered_off = false;
212
213 if (zpodd->from_notify) {
214 zpodd->from_notify = false;
215 if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
216 eject_tray(dev);
217 }
218
219 zpodd->zp_sampled = false;
220 zpodd->zp_ready = false;
Aaron Lu6f4c8272013-01-23 15:09:32 +0800221
222 sdev_enable_disk_events(dev->sdev);
Aaron Lu21334202013-01-15 17:21:01 +0800223}
224
Aaron Luf064a202013-01-15 17:20:59 +0800225static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
226{
227 struct ata_device *ata_dev = context;
228 struct zpodd *zpodd = ata_dev->zpodd;
229 struct device *dev = &ata_dev->sdev->sdev_gendev;
230
Aaron Lu53637e02013-01-28 13:08:02 +0800231 if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
Aaron Luf064a202013-01-15 17:20:59 +0800232 zpodd->from_notify = true;
233 pm_runtime_resume(dev);
234 }
235}
236
237static void ata_acpi_add_pm_notifier(struct ata_device *dev)
238{
239 acpi_handle handle = ata_dev_acpi_handle(dev);
240 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
241 zpodd_wake_dev, dev);
242}
243
244static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
245{
Aaron Luf1bc1e42013-08-23 10:17:54 +0800246 acpi_handle handle = ata_dev_acpi_handle(dev);
Aaron Luf064a202013-01-15 17:20:59 +0800247 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
248}
249
Aaron Luafe75952013-01-15 17:20:58 +0800250void zpodd_init(struct ata_device *dev)
251{
Aaron Lud9202032014-03-14 13:46:10 +0800252 struct acpi_device *adev = ACPI_COMPANION(&dev->tdev);
Aaron Luafe75952013-01-15 17:20:58 +0800253 enum odd_mech_type mech_type;
254 struct zpodd *zpodd;
255
Aaron Lud9202032014-03-14 13:46:10 +0800256 if (dev->zpodd || !adev || !acpi_device_can_poweroff(adev))
Aaron Luafe75952013-01-15 17:20:58 +0800257 return;
258
259 mech_type = zpodd_get_mech_type(dev);
260 if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
261 return;
262
263 zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
264 if (!zpodd)
265 return;
266
267 zpodd->mech_type = mech_type;
268
Aaron Luf064a202013-01-15 17:20:59 +0800269 ata_acpi_add_pm_notifier(dev);
Aaron Luafe75952013-01-15 17:20:58 +0800270 zpodd->dev = dev;
271 dev->zpodd = zpodd;
Aaron Luf1bc1e42013-08-23 10:17:54 +0800272 dev_pm_qos_expose_flags(&dev->tdev, 0);
Aaron Luafe75952013-01-15 17:20:58 +0800273}
274
275void zpodd_exit(struct ata_device *dev)
276{
Aaron Luf064a202013-01-15 17:20:59 +0800277 ata_acpi_remove_pm_notifier(dev);
Aaron Luafe75952013-01-15 17:20:58 +0800278 kfree(dev->zpodd);
279 dev->zpodd = NULL;
280}