libata-zpodd.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. void zpodd_enable_run_wake(struct ata_device *dev)
  150. {
  151. struct zpodd *zpodd = dev->zpodd;
  152. zpodd->powered_off = true;
  153. device_set_run_wake(&dev->sdev->sdev_gendev, true);
  154. acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, true);
  155. }
  156. /* Disable runtime wake capability if it is enabled */
  157. void zpodd_disable_run_wake(struct ata_device *dev)
  158. {
  159. struct zpodd *zpodd = dev->zpodd;
  160. if (zpodd->powered_off) {
  161. acpi_pm_device_run_wake(&dev->sdev->sdev_gendev, false);
  162. device_set_run_wake(&dev->sdev->sdev_gendev, false);
  163. }
  164. }
  165. /*
  166. * Post power on processing after the ODD has been recovered. If the
  167. * ODD wasn't powered off during suspend, it doesn't do anything.
  168. *
  169. * For drawer type ODD, if it is powered on due to user pressed the
  170. * eject button, the tray needs to be ejected. This can only be done
  171. * after the ODD has been recovered, i.e. link is initialized and
  172. * device is able to process NON_DATA PIO command, as eject needs to
  173. * send command for the ODD to process.
  174. *
  175. * The from_notify flag set in wake notification handler function
  176. * zpodd_wake_dev represents if power on is due to user's action.
  177. *
  178. * For both types of ODD, several fields need to be reset.
  179. */
  180. void zpodd_post_poweron(struct ata_device *dev)
  181. {
  182. struct zpodd *zpodd = dev->zpodd;
  183. if (!zpodd->powered_off)
  184. return;
  185. zpodd->powered_off = false;
  186. if (zpodd->from_notify) {
  187. zpodd->from_notify = false;
  188. if (zpodd->mech_type == ODD_MECH_TYPE_DRAWER)
  189. eject_tray(dev);
  190. }
  191. zpodd->zp_sampled = false;
  192. zpodd->zp_ready = false;
  193. }
  194. static void zpodd_wake_dev(acpi_handle handle, u32 event, void *context)
  195. {
  196. struct ata_device *ata_dev = context;
  197. struct zpodd *zpodd = ata_dev->zpodd;
  198. struct device *dev = &ata_dev->sdev->sdev_gendev;
  199. if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
  200. pm_runtime_suspended(dev)) {
  201. zpodd->from_notify = true;
  202. pm_runtime_resume(dev);
  203. }
  204. }
  205. static void ata_acpi_add_pm_notifier(struct ata_device *dev)
  206. {
  207. acpi_handle handle = ata_dev_acpi_handle(dev);
  208. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  209. zpodd_wake_dev, dev);
  210. }
  211. static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
  212. {
  213. acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->sdev->sdev_gendev);
  214. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, zpodd_wake_dev);
  215. }
  216. void zpodd_init(struct ata_device *dev)
  217. {
  218. enum odd_mech_type mech_type;
  219. struct zpodd *zpodd;
  220. if (dev->zpodd)
  221. return;
  222. if (!odd_can_poweroff(dev))
  223. return;
  224. mech_type = zpodd_get_mech_type(dev);
  225. if (mech_type == ODD_MECH_TYPE_UNSUPPORTED)
  226. return;
  227. zpodd = kzalloc(sizeof(struct zpodd), GFP_KERNEL);
  228. if (!zpodd)
  229. return;
  230. zpodd->mech_type = mech_type;
  231. ata_acpi_add_pm_notifier(dev);
  232. zpodd->dev = dev;
  233. dev->zpodd = zpodd;
  234. }
  235. void zpodd_exit(struct ata_device *dev)
  236. {
  237. ata_acpi_remove_pm_notifier(dev);
  238. kfree(dev->zpodd);
  239. dev->zpodd = NULL;
  240. }