]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/ata/libata-zpodd.c
libata: check zero power ready status for ZPODD
[mirror_ubuntu-artful-kernel.git] / drivers / ata / libata-zpodd.c
CommitLineData
afe75951
AL
1#include <linux/libata.h>
2#include <linux/cdrom.h>
f064a20d 3#include <linux/pm_runtime.h>
3dc67440 4#include <linux/module.h>
f064a20d 5#include <scsi/scsi_device.h>
afe75951
AL
6
7#include "libata.h"
8
3dc67440
AL
9static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */
10module_param(zpodd_poweroff_delay, int, 0644);
11MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds");
12
afe75951
AL
13enum odd_mech_type {
14 ODD_MECH_TYPE_SLOT,
15 ODD_MECH_TYPE_DRAWER,
16 ODD_MECH_TYPE_UNSUPPORTED,
17};
18
19struct zpodd {
20 enum odd_mech_type mech_type; /* init during probe, RO afterwards */
21 struct ata_device *dev;
f064a20d
AL
22
23 /* The following fields are synchronized by PM core. */
24 bool from_notify; /* resumed as a result of
25 * acpi wake notification */
3dc67440
AL
26 bool zp_ready; /* ZP ready state */
27 unsigned long last_ready; /* last ZP ready timestamp */
28 bool zp_sampled; /* ZP ready state sampled */
afe75951
AL
29};
30
31/* Per the spec, only slot type and drawer type ODD can be supported */
32static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
33{
34 char buf[16];
35 unsigned int ret;
36 struct rm_feature_desc *desc = (void *)(buf + 8);
37 struct ata_taskfile tf = {};
38
39 char cdb[] = { GPCMD_GET_CONFIGURATION,
40 2, /* only 1 feature descriptor requested */
41 0, 3, /* 3, removable medium feature */
42 0, 0, 0,/* reserved */
43 0, sizeof(buf),
44 0, 0, 0,
45 };
46
47 tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
48 tf.command = ATA_CMD_PACKET;
49 tf.protocol = ATAPI_PROT_PIO;
50 tf.lbam = sizeof(buf);
51
52 ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
53 buf, sizeof(buf), 0);
54 if (ret)
55 return ODD_MECH_TYPE_UNSUPPORTED;
56
57 if (be16_to_cpu(desc->feature_code) != 3)
58 return ODD_MECH_TYPE_UNSUPPORTED;
59
60 if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1)
61 return ODD_MECH_TYPE_SLOT;
62 else if (desc->mech_type == 1 && desc->load == 0 && desc->eject == 1)
63 return ODD_MECH_TYPE_DRAWER;
64 else
65 return ODD_MECH_TYPE_UNSUPPORTED;
66}
67
68static bool odd_can_poweroff(struct ata_device *ata_dev)
69{
70 acpi_handle handle;
71 acpi_status status;
72 struct acpi_device *acpi_dev;
73
74 handle = ata_dev_acpi_handle(ata_dev);
75 if (!handle)
76 return false;
77
78 status = acpi_bus_get_device(handle, &acpi_dev);
79 if (ACPI_FAILURE(status))
80 return false;
81
82 return acpi_device_can_poweroff(acpi_dev);
83}
84
3dc67440
AL
85/* Test if ODD is zero power ready by sense code */
86static bool zpready(struct ata_device *dev)
87{
88 u8 sense_key, *sense_buf;
89 unsigned int ret, asc, ascq, add_len;
90 struct zpodd *zpodd = dev->zpodd;
91
92 ret = atapi_eh_tur(dev, &sense_key);
93
94 if (!ret || sense_key != NOT_READY)
95 return false;
96
97 sense_buf = dev->link->ap->sector_buf;
98 ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
99 if (ret)
100 return false;
101
102 /* sense valid */
103 if ((sense_buf[0] & 0x7f) != 0x70)
104 return false;
105
106 add_len = sense_buf[7];
107 /* has asc and ascq */
108 if (add_len < 6)
109 return false;
110
111 asc = sense_buf[12];
112 ascq = sense_buf[13];
113
114 if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
115 /* no media inside */
116 return asc == 0x3a;
117 else
118 /* no media inside and door closed */
119 return asc == 0x3a && ascq == 0x01;
120}
121
122/*
123 * Update the zpodd->zp_ready field. This field will only be set
124 * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
125 * time, and will be used to decide if power off is allowed. If it
126 * is set, it will be cleared during resume from powered off state.
127 */
128void zpodd_on_suspend(struct ata_device *dev)
129{
130 struct zpodd *zpodd = dev->zpodd;
131 unsigned long expires;
132
133 if (!zpready(dev)) {
134 zpodd->zp_sampled = false;
135 return;
136 }
137
138 if (!zpodd->zp_sampled) {
139 zpodd->zp_sampled = true;
140 zpodd->last_ready = jiffies;
141 return;
142 }
143
144 expires = zpodd->last_ready +
145 msecs_to_jiffies(zpodd_poweroff_delay * 1000);
146 if (time_before(jiffies, expires))
147 return;
148
149 zpodd->zp_ready = true;
150}
151
f064a20d
AL
152static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
153{
154 struct ata_device *ata_dev = context;
155 struct zpodd *zpodd = ata_dev->zpodd;
156 struct device *dev = &ata_dev->sdev->sdev_gendev;
157
158 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
159 pm_runtime_suspended(dev)) {
160 zpodd->from_notify = true;
161 pm_runtime_resume(dev);
162 }
163}
164
165static void ata_acpi_add_pm_notifier(struct ata_device *dev)
166{
167 acpi_handle handle = ata_dev_acpi_handle(dev);
168 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
169 zpodd_wake_dev, dev);
170}
171
172static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
173{
174 acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->sdev->sdev_gendev);
175 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
176}
177
afe75951
AL
178void zpodd_init(struct ata_device *dev)
179{
180 enum odd_mech_type mech_type;
181 struct zpodd *zpodd;
182
183 if (dev->zpodd)
184 return;
185
186 if (!odd_can_poweroff(dev))
187 return;
188
189 mech_type = zpodd_get_mech_type(dev);
190 if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
191 return;
192
193 zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
194 if (!zpodd)
195 return;
196
197 zpodd->mech_type = mech_type;
198
f064a20d 199 ata_acpi_add_pm_notifier(dev);
afe75951
AL
200 zpodd->dev = dev;
201 dev->zpodd = zpodd;
202}
203
204void zpodd_exit(struct ata_device *dev)
205{
f064a20d 206 ata_acpi_remove_pm_notifier(dev);
afe75951
AL
207 kfree(dev->zpodd);
208 dev->zpodd = NULL;
209}