libata-zpodd.c 7.1 KB

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