libata-zpodd.c 5.0 KB

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