libata-zpodd.c 7.3 KB

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