libata-zpodd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. const char cdb[] = { 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. 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. static bool odd_can_poweroff(struct ata_device *ata_dev)
  75. {
  76. acpi_handle handle;
  77. acpi_status status;
  78. struct acpi_device *acpi_dev;
  79. handle = ata_dev_acpi_handle(ata_dev);
  80. if (!handle)
  81. return false;
  82. status = acpi_bus_get_device(handle, &acpi_dev);
  83. if (ACPI_FAILURE(status))
  84. return false;
  85. return acpi_device_can_poweroff(acpi_dev);
  86. }
  87. /* Test if ODD is zero power ready by sense code */
  88. static bool zpready(struct ata_device *dev)
  89. {
  90. u8 sense_key, *sense_buf;
  91. unsigned int ret, asc, ascq, add_len;
  92. struct zpodd *zpodd = dev->zpodd;
  93. ret = atapi_eh_tur(dev, &sense_key);
  94. if (!ret || sense_key != NOT_READY)
  95. return false;
  96. sense_buf = dev->link->ap->sector_buf;
  97. ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
  98. if (ret)
  99. return false;
  100. /* sense valid */
  101. if ((sense_buf[0] & 0x7f) != 0x70)
  102. return false;
  103. add_len = sense_buf[7];
  104. /* has asc and ascq */
  105. if (add_len < 6)
  106. return false;
  107. asc = sense_buf[12];
  108. ascq = sense_buf[13];
  109. if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
  110. /* no media inside */
  111. return asc == 0x3a;
  112. else
  113. /* no media inside and door closed */
  114. return asc == 0x3a && ascq == 0x01;
  115. }
  116. /*
  117. * Update the zpodd->zp_ready field. This field will only be set
  118. * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
  119. * time, and will be used to decide if power off is allowed. If it
  120. * is set, it will be cleared during resume from powered off state.
  121. */
  122. void zpodd_on_suspend(struct ata_device *dev)
  123. {
  124. struct zpodd *zpodd = dev->zpodd;
  125. unsigned long expires;
  126. if (!zpready(dev)) {
  127. zpodd->zp_sampled = false;
  128. zpodd->zp_ready = false;
  129. return;
  130. }
  131. if (!zpodd->zp_sampled) {
  132. zpodd->zp_sampled = true;
  133. zpodd->last_ready = jiffies;
  134. return;
  135. }
  136. expires = zpodd->last_ready +
  137. msecs_to_jiffies(zpodd_poweroff_delay * 1000);
  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. device_set_run_wake(&dev->tdev, true);
  161. acpi_pm_device_run_wake(&dev->tdev, true);
  162. }
  163. /* Disable runtime wake capability if it is enabled */
  164. void zpodd_disable_run_wake(struct ata_device *dev)
  165. {
  166. struct zpodd *zpodd = dev->zpodd;
  167. if (zpodd->powered_off) {
  168. acpi_pm_device_run_wake(&dev->tdev, false);
  169. device_set_run_wake(&dev->tdev, false);
  170. }
  171. }
  172. /*
  173. * Post power on processing after the ODD has been recovered. If the
  174. * ODD wasn't powered off during suspend, it doesn't do anything.
  175. *
  176. * For drawer type ODD, if it is powered on due to user pressed the
  177. * eject button, the tray needs to be ejected. This can only be done
  178. * after the ODD has been recovered, i.e. link is initialized and
  179. * device is able to process NON_DATA PIO command, as eject needs to
  180. * send command for the ODD to process.
  181. *
  182. * The from_notify flag set in wake notification handler function
  183. * zpodd_wake_dev represents if power on is due to user's action.
  184. *
  185. * For both types of ODD, several fields need to be reset.
  186. */
  187. void zpodd_post_poweron(struct ata_device *dev)
  188. {
  189. struct zpodd *zpodd = dev->zpodd;
  190. if (!zpodd->powered_off)
  191. return;
  192. zpodd->powered_off = false;
  193. if (zpodd->from_notify) {
  194. zpodd->from_notify = false;
  195. if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
  196. eject_tray(dev);
  197. }
  198. zpodd->zp_sampled = false;
  199. zpodd->zp_ready = false;
  200. sdev_enable_disk_events(dev->sdev);
  201. }
  202. static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
  203. {
  204. struct ata_device *ata_dev = context;
  205. struct zpodd *zpodd = ata_dev->zpodd;
  206. struct device *dev = &ata_dev->sdev->sdev_gendev;
  207. if (event == ACPI_NOTIFY_DEVICE_WAKE && pm_runtime_suspended(dev)) {
  208. zpodd->from_notify = true;
  209. pm_runtime_resume(dev);
  210. }
  211. }
  212. static void ata_acpi_add_pm_notifier(struct ata_device *dev)
  213. {
  214. acpi_handle handle = ata_dev_acpi_handle(dev);
  215. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  216. zpodd_wake_dev, dev);
  217. }
  218. static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
  219. {
  220. acpi_handle handle = ata_dev_acpi_handle(dev);
  221. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
  222. }
  223. void zpodd_init(struct ata_device *dev)
  224. {
  225. enum odd_mech_type mech_type;
  226. struct zpodd *zpodd;
  227. if (dev->zpodd)
  228. return;
  229. if (!odd_can_poweroff(dev))
  230. return;
  231. mech_type = zpodd_get_mech_type(dev);
  232. if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
  233. return;
  234. zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
  235. if (!zpodd)
  236. return;
  237. zpodd->mech_type = mech_type;
  238. ata_acpi_add_pm_notifier(dev);
  239. zpodd->dev = dev;
  240. dev->zpodd = zpodd;
  241. dev_pm_qos_expose_flags(&dev->tdev, 0);
  242. }
  243. void zpodd_exit(struct ata_device *dev)
  244. {
  245. ata_acpi_remove_pm_notifier(dev);
  246. kfree(dev->zpodd);
  247. dev->zpodd = NULL;
  248. }