libata-zpodd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. bool powered_off; /* ODD is powered off
  25. * during suspend */
  26. };
  27. static int eject_tray(struct ata_device *dev)
  28. {
  29. struct ata_taskfile tf = {};
  30. const char cdb[] = { GPCMD_START_STOP_UNIT,
  31. 0, 0, 0,
  32. 0x02, /* LoEj */
  33. 0, 0, 0, 0, 0, 0, 0,
  34. };
  35. tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  36. tf.command = ATA_CMD_PACKET;
  37. tf.protocol = ATAPI_PROT_NODATA;
  38. return ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
  39. }
  40. /* Per the spec, only slot type and drawer type ODD can be supported */
  41. static enum odd_mech_type zpodd_get_mech_type(struct ata_device *dev)
  42. {
  43. char buf[16];
  44. unsigned int ret;
  45. struct rm_feature_desc *desc = (void *)(buf + 8);
  46. struct ata_taskfile tf = {};
  47. char cdb[] = { GPCMD_GET_CONFIGURATION,
  48. 2, /* only 1 feature descriptor requested */
  49. 0, 3, /* 3, removable medium feature */
  50. 0, 0, 0,/* reserved */
  51. 0, sizeof(buf),
  52. 0, 0, 0,
  53. };
  54. tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  55. tf.command = ATA_CMD_PACKET;
  56. tf.protocol = ATAPI_PROT_PIO;
  57. tf.lbam = sizeof(buf);
  58. ret = ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  59. buf, sizeof(buf), 0);
  60. if (ret)
  61. return ODD_MECH_TYPE_UNSUPPORTED;
  62. if (be16_to_cpu(desc->feature_code) != 3)
  63. return ODD_MECH_TYPE_UNSUPPORTED;
  64. if (desc->mech_type == 0 && desc->load == 0 && desc->eject == 1)
  65. return ODD_MECH_TYPE_SLOT;
  66. else if (desc->mech_type == 1 && desc->load == 0 && desc->eject == 1)
  67. return ODD_MECH_TYPE_DRAWER;
  68. else
  69. return ODD_MECH_TYPE_UNSUPPORTED;
  70. }
  71. static bool odd_can_poweroff(struct ata_device *ata_dev)
  72. {
  73. acpi_handle handle;
  74. acpi_status status;
  75. struct acpi_device *acpi_dev;
  76. handle = ata_dev_acpi_handle(ata_dev);
  77. if (!handle)
  78. return false;
  79. status = acpi_bus_get_device(handle, &acpi_dev);
  80. if (ACPI_FAILURE(status))
  81. return false;
  82. return acpi_device_can_poweroff(acpi_dev);
  83. }
  84. /* Test if ODD is zero power ready by sense code */
  85. static bool zpready(struct ata_device *dev)
  86. {
  87. u8 sense_key, *sense_buf;
  88. unsigned int ret, asc, ascq, add_len;
  89. struct zpodd *zpodd = dev->zpodd;
  90. ret = atapi_eh_tur(dev, &sense_key);
  91. if (!ret || sense_key != NOT_READY)
  92. return false;
  93. sense_buf = dev->link->ap->sector_buf;
  94. ret = atapi_eh_request_sense(dev, sense_buf, sense_key);
  95. if (ret)
  96. return false;
  97. /* sense valid */
  98. if ((sense_buf[0] & 0x7f) != 0x70)
  99. return false;
  100. add_len = sense_buf[7];
  101. /* has asc and ascq */
  102. if (add_len < 6)
  103. return false;
  104. asc = sense_buf[12];
  105. ascq = sense_buf[13];
  106. if (zpodd->mech_type == ODD_MECH_TYPE_SLOT)
  107. /* no media inside */
  108. return asc == 0x3a;
  109. else
  110. /* no media inside and door closed */
  111. return asc == 0x3a && ascq == 0x01;
  112. }
  113. /*
  114. * Update the zpodd->zp_ready field. This field will only be set
  115. * if the ODD has stayed in ZP ready state for zpodd_poweroff_delay
  116. * time, and will be used to decide if power off is allowed. If it
  117. * is set, it will be cleared during resume from powered off state.
  118. */
  119. void zpodd_on_suspend(struct ata_device *dev)
  120. {
  121. struct zpodd *zpodd = dev->zpodd;
  122. unsigned long expires;
  123. if (!zpready(dev)) {
  124. zpodd->zp_sampled = false;
  125. zpodd->zp_ready = false;
  126. return;
  127. }
  128. if (!zpodd->zp_sampled) {
  129. zpodd->zp_sampled = true;
  130. zpodd->last_ready = jiffies;
  131. return;
  132. }
  133. expires = zpodd->last_ready +
  134. msecs_to_jiffies(zpodd_poweroff_delay * 1000);
  135. if (time_before(jiffies, expires))
  136. return;
  137. zpodd->zp_ready = true;
  138. }
  139. bool zpodd_zpready(struct ata_device *dev)
  140. {
  141. struct zpodd *zpodd = dev->zpodd;
  142. return zpodd->zp_ready;
  143. }
  144. /*
  145. * Enable runtime wake capability through ACPI and set the powered_off flag,
  146. * this flag will be used during resume to decide what operations are needed
  147. * to take.
  148. *
  149. * Also, media poll needs to be silenced, so that it doesn't bring the ODD
  150. * back to full power state every few seconds.
  151. */
  152. void zpodd_enable_run_wake(struct ata_device *dev)
  153. {
  154. struct zpodd *zpodd = dev->zpodd;
  155. sdev_disable_disk_events(dev->sdev);
  156. zpodd->powered_off = true;
  157. device_set_run_wake(&dev->sdev->sdev_gendev, true);
  158. acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, true);
  159. }
  160. /* Disable runtime wake capability if it is enabled */
  161. void zpodd_disable_run_wake(struct ata_device *dev)
  162. {
  163. struct zpodd *zpodd = dev->zpodd;
  164. if (zpodd->powered_off) {
  165. acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, false);
  166. device_set_run_wake(&dev->sdev->sdev_gendev, false);
  167. }
  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 = DEVICE_ACPI_HANDLE(&dev->sdev->sdev_gendev);
  218. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
  219. }
  220. void zpodd_init(struct ata_device *dev)
  221. {
  222. enum odd_mech_type mech_type;
  223. struct zpodd *zpodd;
  224. if (dev->zpodd)
  225. return;
  226. if (!odd_can_poweroff(dev))
  227. return;
  228. mech_type = zpodd_get_mech_type(dev);
  229. if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
  230. return;
  231. zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
  232. if (!zpodd)
  233. return;
  234. zpodd->mech_type = mech_type;
  235. ata_acpi_add_pm_notifier(dev);
  236. zpodd->dev = dev;
  237. dev->zpodd = zpodd;
  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. }