libata-zpodd.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include "libata.h"
  9. static int zpodd_poweroff_delay = 30; /* 30 seconds for power off delay */
  10. module_param(zpodd_poweroff_delay, int, 0644);
  11. MODULE_PARM_DESC(zpodd_poweroff_delay, "Poweroff delay for ZPODD in seconds");
  12. enum odd_mech_type {
  13. ODD_MECH_TYPE_SLOT,
  14. ODD_MECH_TYPE_DRAWER,
  15. ODD_MECH_TYPE_UNSUPPORTED,
  16. };
  17. struct zpodd {
  18. enum odd_mech_type mech_type; /* init during probe, RO afterwards */
  19. struct ata_device *dev;
  20. /* The following fields are synchronized by PM core. */
  21. bool from_notify; /* resumed as a result of
  22. * acpi wake notification */
  23. bool zp_ready; /* ZP ready state */
  24. unsigned long last_ready; /* last ZP ready timestamp */
  25. bool zp_sampled; /* ZP ready state sampled */
  26. bool powered_off; /* ODD is powered off
  27. * during suspend */
  28. };
  29. static int eject_tray(struct ata_device *dev)
  30. {
  31. struct ata_taskfile tf;
  32. static const char cdb[ATAPI_CDB_LEN] = { GPCMD_START_STOP_UNIT,
  33. 0, 0, 0,
  34. 0x02, /* LoEj */
  35. 0, 0, 0, 0, 0, 0, 0,
  36. };
  37. ata_tf_init(dev, &tf);
  38. tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  39. tf.command = ATA_CMD_PACKET;
  40. tf.protocol = ATAPI_PROT_NODATA;
  41. return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
  42. }
  43. /* Per the spec, only slot type and drawer type ODD can be supported */
  44. static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
  45. {
  46. char *buf;
  47. unsigned int ret;
  48. struct rm_feature_desc *desc;
  49. struct ata_taskfile tf;
  50. static const char cdb[ATAPI_CDB_LEN] = { GPCMD_GET_CONFIGURATION,
  51. 2, /* only 1 feature descriptor requested */
  52. 0, 3, /* 3, removable medium feature */
  53. 0, 0, 0,/* reserved */
  54. 0, 16,
  55. 0, 0, 0,
  56. };
  57. buf = kzalloc(16, GFP_KERNEL);
  58. if (!buf)
  59. return ODD_MECH_TYPE_UNSUPPORTED;
  60. desc = (void *)(buf + 8);
  61. ata_tf_init(dev, &tf);
  62. tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  63. tf.command = ATA_CMD_PACKET;
  64. tf.protocol = ATAPI_PROT_PIO;
  65. tf.lbam = 16;
  66. ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  67. buf, 16, 0);
  68. if (ret) {
  69. kfree(buf);
  70. return ODD_MECH_TYPE_UNSUPPORTED;
  71. }
  72. if (be16_to_cpu(desc->feature_code) != 3) {
  73. kfree(buf);
  74. return ODD_MECH_TYPE_UNSUPPORTED;
  75. }
  76. if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1) {
  77. kfree(buf);
  78. return ODD_MECH_TYPE_SLOT;
  79. } else if (desc->mech_type == 1 && desc->load == 0 &&
  80. desc->eject == 1) {
  81. kfree(buf);
  82. return ODD_MECH_TYPE_DRAWER;
  83. } else {
  84. kfree(buf);
  85. return ODD_MECH_TYPE_UNSUPPORTED;
  86. }
  87. }
  88. /* Test if ODD is zero power ready by sense code */
  89. static 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. ret = atapi_eh_tur(dev, &sense_key);
  95. if (!ret || sense_key != NOT_READY)
  96. return false;
  97. sense_buf = dev->sector_buf;
  98. ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
  99. if (ret)
  100. return false;
  101. /* sense valid */
  102. if ((sense_buf[0] & 0x7f) != 0x70)
  103. return false;
  104. add_len = sense_buf[7];
  105. /* has asc and ascq */
  106. if (add_len < 6)
  107. return false;
  108. asc = sense_buf[12];
  109. ascq = sense_buf[13];
  110. if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
  111. /* no media inside */
  112. return asc == 0x3a;
  113. else
  114. /* no media inside and door closed */
  115. return asc == 0x3a && ascq == 0x01;
  116. }
  117. /*
  118. * Update the zpodd->zp_ready field. This field will only be set
  119. * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
  120. * time, and will be used to decide if power off is allowed. If it
  121. * is set, it will be cleared during resume from powered off state.
  122. */
  123. void zpodd_on_suspend(struct ata_device *dev)
  124. {
  125. struct zpodd *zpodd = dev->zpodd;
  126. unsigned long expires;
  127. if (!zpready(dev)) {
  128. zpodd->zp_sampled = false;
  129. zpodd->zp_ready = false;
  130. return;
  131. }
  132. if (!zpodd->zp_sampled) {
  133. zpodd->zp_sampled = true;
  134. zpodd->last_ready = jiffies;
  135. return;
  136. }
  137. expires = zpodd->last_ready + secs_to_jiffies(zpodd_poweroff_delay);
  138. if (time_before(jiffies, expires))
  139. return;
  140. zpodd->zp_ready = true;
  141. }
  142. bool zpodd_zpready(struct ata_device *dev)
  143. {
  144. struct zpodd *zpodd = dev->zpodd;
  145. return zpodd->zp_ready;
  146. }
  147. /*
  148. * Enable runtime wake capability through ACPI and set the powered_off flag,
  149. * this flag will be used during resume to decide what operations are needed
  150. * to take.
  151. *
  152. * Also, media poll needs to be silenced, so that it doesn't bring the ODD
  153. * back to full power state every few seconds.
  154. */
  155. void zpodd_enable_run_wake(struct ata_device *dev)
  156. {
  157. struct zpodd *zpodd = dev->zpodd;
  158. sdev_disable_disk_events(dev->sdev);
  159. zpodd->powered_off = true;
  160. acpi_pm_set_device_wakeup(&dev->tdev, true);
  161. }
  162. /* Disable runtime wake capability if it is enabled */
  163. void zpodd_disable_run_wake(struct ata_device *dev)
  164. {
  165. struct zpodd *zpodd = dev->zpodd;
  166. if (zpodd->powered_off)
  167. acpi_pm_set_device_wakeup(&dev->tdev, false);
  168. }
  169. /*
  170. * Post power on processing after the ODD has been recovered. If the
  171. * ODD wasn't powered off during suspend, it doesn't do anything.
  172. *
  173. * For drawer type ODD, if it is powered on due to user pressed the
  174. * eject button, the tray needs to be ejected. This can only be done
  175. * after the ODD has been recovered, i.e. link is initialized and
  176. * device is able to process NON_DATA PIO command, as eject needs to
  177. * send command for the ODD to process.
  178. *
  179. * The from_notify flag set in wake notification handler function
  180. * zpodd_wake_dev represents if power on is due to user's action.
  181. *
  182. * For both types of ODD, several fields need to be reset.
  183. */
  184. void zpodd_post_poweron(struct ata_device *dev)
  185. {
  186. struct zpodd *zpodd = dev->zpodd;
  187. if (!zpodd->powered_off)
  188. return;
  189. zpodd->powered_off = false;
  190. if (zpodd->from_notify) {
  191. zpodd->from_notify = false;
  192. if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
  193. eject_tray(dev);
  194. }
  195. zpodd->zp_sampled = false;
  196. zpodd->zp_ready = false;
  197. sdev_enable_disk_events(dev->sdev);
  198. }
  199. static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
  200. {
  201. struct ata_device *ata_dev = context;
  202. struct zpodd *zpodd = ata_dev->zpodd;
  203. struct device *dev = &ata_dev->sdev->sdev_gendev;
  204. if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
  205. zpodd->from_notify = true;
  206. pm_runtime_resume(dev);
  207. }
  208. }
  209. static void ata_acpi_add_pm_notifier(struct ata_device *dev)
  210. {
  211. acpi_handle handle = ata_dev_acpi_handle(dev);
  212. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  213. zpodd_wake_dev, dev);
  214. }
  215. static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
  216. {
  217. acpi_handle handle = ata_dev_acpi_handle(dev);
  218. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
  219. }
  220. void zpodd_init(struct ata_device *dev)
  221. {
  222. struct acpi_device *adev = ACPI_COMPANION(&dev->tdev);
  223. enum odd_mech_type mech_type;
  224. struct zpodd *zpodd;
  225. if (dev->zpodd || !adev || !acpi_device_can_poweroff(adev))
  226. return;
  227. mech_type = zpodd_get_mech_type(dev);
  228. if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
  229. return;
  230. zpodd = kzalloc_obj(struct zpodd);
  231. if (!zpodd)
  232. return;
  233. zpodd->mech_type = mech_type;
  234. ata_acpi_add_pm_notifier(dev);
  235. zpodd->dev = dev;
  236. dev->zpodd = zpodd;
  237. dev_pm_qos_expose_flags(&dev->tdev, 0);
  238. }
  239. void zpodd_exit(struct ata_device *dev)
  240. {
  241. ata_acpi_remove_pm_notifier(dev);
  242. kfree(dev->zpodd);
  243. dev->zpodd = NULL;
  244. }