GNU Linux-libre 4.4.297-gnu1
[releases.git] / drivers / ata / libata-zpodd.c
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>
7
8 #include "libata.h"
9
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
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;
23
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
31                                               * during suspend */
32 };
33
34 static int eject_tray(struct ata_device *dev)
35 {
36         struct ata_taskfile tf;
37         static const char cdb[ATAPI_CDB_LEN] = {  GPCMD_START_STOP_UNIT,
38                 0, 0, 0,
39                 0x02,     /* LoEj */
40                 0, 0, 0, 0, 0, 0, 0,
41         };
42
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;
47
48         return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
49 }
50
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;
55         unsigned int ret;
56         struct rm_feature_desc *desc;
57         struct ata_taskfile tf;
58         static const char cdb[ATAPI_CDB_LEN] = {  GPCMD_GET_CONFIGURATION,
59                         2,      /* only 1 feature descriptor requested */
60                         0, 3,   /* 3, removable medium feature */
61                         0, 0, 0,/* reserved */
62                         0, 16,
63                         0, 0, 0,
64         };
65
66         buf = kzalloc(16, GFP_KERNEL);
67         if (!buf)
68                 return ODD_MECH_TYPE_UNSUPPORTED;
69         desc = (void *)(buf + 8);
70
71         ata_tf_init(dev, &tf);
72         tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
73         tf.command = ATA_CMD_PACKET;
74         tf.protocol = ATAPI_PROT_PIO;
75         tf.lbam = 16;
76
77         ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
78                                 buf, 16, 0);
79         if (ret) {
80                 kfree(buf);
81                 return ODD_MECH_TYPE_UNSUPPORTED;
82         }
83
84         if (be16_to_cpu(desc->feature_code) != 3) {
85                 kfree(buf);
86                 return ODD_MECH_TYPE_UNSUPPORTED;
87         }
88
89         if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1) {
90                 kfree(buf);
91                 return ODD_MECH_TYPE_SLOT;
92         } else if (desc->mech_type == 1 && desc->load == 0 &&
93                    desc->eject == 1) {
94                 kfree(buf);
95                 return ODD_MECH_TYPE_DRAWER;
96         } else {
97                 kfree(buf);
98                 return ODD_MECH_TYPE_UNSUPPORTED;
99         }
100 }
101
102 /* Test if ODD is zero power ready by sense code */
103 static bool zpready(struct ata_device *dev)
104 {
105         u8 sense_key, *sense_buf;
106         unsigned int ret, asc, ascq, add_len;
107         struct zpodd *zpodd = dev->zpodd;
108
109         ret = atapi_eh_tur(dev, &sense_key);
110
111         if (!ret || sense_key != NOT_READY)
112                 return false;
113
114         sense_buf = dev->link->ap->sector_buf;
115         ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
116         if (ret)
117                 return false;
118
119         /* sense valid */
120         if ((sense_buf[0] & 0x7f) != 0x70)
121                 return false;
122
123         add_len = sense_buf[7];
124         /* has asc and ascq */
125         if (add_len < 6)
126                 return false;
127
128         asc = sense_buf[12];
129         ascq = sense_buf[13];
130
131         if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
132                 /* no media inside */
133                 return asc == 0x3a;
134         else
135                 /* no media inside and door closed */
136                 return asc == 0x3a && ascq == 0x01;
137 }
138
139 /*
140  * Update the zpodd->zp_ready field. This field will only be set
141  * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
142  * time, and will be used to decide if power off is allowed. If it
143  * is set, it will be cleared during resume from powered off state.
144  */
145 void zpodd_on_suspend(struct ata_device *dev)
146 {
147         struct zpodd *zpodd = dev->zpodd;
148         unsigned long expires;
149
150         if (!zpready(dev)) {
151                 zpodd->zp_sampled = false;
152                 zpodd->zp_ready = false;
153                 return;
154         }
155
156         if (!zpodd->zp_sampled) {
157                 zpodd->zp_sampled = true;
158                 zpodd->last_ready = jiffies;
159                 return;
160         }
161
162         expires = zpodd->last_ready +
163                   msecs_to_jiffies(zpodd_poweroff_delay * 1000);
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         device_set_run_wake(&dev->tdev, true);
192         acpi_pm_device_run_wake(&dev->tdev, true);
193 }
194
195 /* Disable runtime wake capability if it is enabled */
196 void zpodd_disable_run_wake(struct ata_device *dev)
197 {
198         struct zpodd *zpodd = dev->zpodd;
199
200         if (zpodd->powered_off) {
201                 acpi_pm_device_run_wake(&dev->tdev, false);
202                 device_set_run_wake(&dev->tdev, false);
203         }
204 }
205
206 /*
207  * Post power on processing after the ODD has been recovered. If the
208  * ODD wasn't powered off during suspend, it doesn't do anything.
209  *
210  * For drawer type ODD, if it is powered on due to user pressed the
211  * eject button, the tray needs to be ejected. This can only be done
212  * after the ODD has been recovered, i.e. link is initialized and
213  * device is able to process NON_DATA PIO command, as eject needs to
214  * send command for the ODD to process.
215  *
216  * The from_notify flag set in wake notification handler function
217  * zpodd_wake_dev represents if power on is due to user's action.
218  *
219  * For both types of ODD, several fields need to be reset.
220  */
221 void zpodd_post_poweron(struct ata_device *dev)
222 {
223         struct zpodd *zpodd = dev->zpodd;
224
225         if (!zpodd->powered_off)
226                 return;
227
228         zpodd->powered_off = false;
229
230         if (zpodd->from_notify) {
231                 zpodd->from_notify = false;
232                 if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
233                         eject_tray(dev);
234         }
235
236         zpodd->zp_sampled = false;
237         zpodd->zp_ready = false;
238
239         sdev_enable_disk_events(dev->sdev);
240 }
241
242 static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
243 {
244         struct ata_device *ata_dev = context;
245         struct zpodd *zpodd = ata_dev->zpodd;
246         struct device *dev = &ata_dev->sdev->sdev_gendev;
247
248         if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
249                 zpodd->from_notify = true;
250                 pm_runtime_resume(dev);
251         }
252 }
253
254 static void ata_acpi_add_pm_notifier(struct ata_device *dev)
255 {
256         acpi_handle handle = ata_dev_acpi_handle(dev);
257         acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
258                                     zpodd_wake_dev, dev);
259 }
260
261 static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
262 {
263         acpi_handle handle = ata_dev_acpi_handle(dev);
264         acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
265 }
266
267 void zpodd_init(struct ata_device *dev)
268 {
269         struct acpi_device *adev = ACPI_COMPANION(&dev->tdev);
270         enum odd_mech_type mech_type;
271         struct zpodd *zpodd;
272
273         if (dev->zpodd || !adev || !acpi_device_can_poweroff(adev))
274                 return;
275
276         mech_type = zpodd_get_mech_type(dev);
277         if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
278                 return;
279
280         zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
281         if (!zpodd)
282                 return;
283
284         zpodd->mech_type = mech_type;
285
286         ata_acpi_add_pm_notifier(dev);
287         zpodd->dev = dev;
288         dev->zpodd = zpodd;
289         dev_pm_qos_expose_flags(&dev->tdev, 0);
290 }
291
292 void zpodd_exit(struct ata_device *dev)
293 {
294         ata_acpi_remove_pm_notifier(dev);
295         kfree(dev->zpodd);
296         dev->zpodd = NULL;
297 }