olpc-ec.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Generic driver for the OLPC Embedded Controller.
  3. *
  4. * Copyright (C) 2011-2012 One Laptop per Child Foundation.
  5. *
  6. * Licensed under the GPL v2 or later.
  7. */
  8. #include <linux/completion.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/mutex.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/module.h>
  15. #include <linux/list.h>
  16. #include <linux/olpc-ec.h>
  17. #include <asm/olpc.h>
  18. struct ec_cmd_desc {
  19. u8 cmd;
  20. u8 *inbuf, *outbuf;
  21. size_t inlen, outlen;
  22. int err;
  23. struct completion finished;
  24. struct list_head node;
  25. void *priv;
  26. };
  27. struct olpc_ec_priv {
  28. struct olpc_ec_driver *drv;
  29. /*
  30. * Running an EC command while suspending means we don't always finish
  31. * the command before the machine suspends. This means that the EC
  32. * is expecting the command protocol to finish, but we after a period
  33. * of time (while the OS is asleep) the EC times out and restarts its
  34. * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
  35. * middle of the command protocol, starts throwing random things at
  36. * the EC... and everyone's uphappy.
  37. */
  38. bool suspended;
  39. };
  40. static void olpc_ec_worker(struct work_struct *w);
  41. static DECLARE_WORK(ec_worker, olpc_ec_worker);
  42. static LIST_HEAD(ec_cmd_q);
  43. static DEFINE_SPINLOCK(ec_cmd_q_lock);
  44. static struct olpc_ec_driver *ec_driver;
  45. static struct olpc_ec_priv *ec_priv;
  46. static void *ec_cb_arg;
  47. static DEFINE_MUTEX(ec_cb_lock);
  48. void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
  49. {
  50. ec_driver = drv;
  51. ec_cb_arg = arg;
  52. }
  53. EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
  54. static void olpc_ec_worker(struct work_struct *w)
  55. {
  56. struct ec_cmd_desc *desc = NULL;
  57. unsigned long flags;
  58. /* Grab the first pending command from the queue */
  59. spin_lock_irqsave(&ec_cmd_q_lock, flags);
  60. if (!list_empty(&ec_cmd_q)) {
  61. desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node);
  62. list_del(&desc->node);
  63. }
  64. spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
  65. /* Do we actually have anything to do? */
  66. if (!desc)
  67. return;
  68. /* Protect the EC hw with a mutex; only run one cmd at a time */
  69. mutex_lock(&ec_cb_lock);
  70. desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
  71. desc->outbuf, desc->outlen, ec_cb_arg);
  72. mutex_unlock(&ec_cb_lock);
  73. /* Finished, wake up olpc_ec_cmd() */
  74. complete(&desc->finished);
  75. /* Run the worker thread again in case there are more cmds pending */
  76. schedule_work(&ec_worker);
  77. }
  78. /*
  79. * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
  80. * locking is pretty critical.
  81. */
  82. static void queue_ec_descriptor(struct ec_cmd_desc *desc)
  83. {
  84. unsigned long flags;
  85. INIT_LIST_HEAD(&desc->node);
  86. spin_lock_irqsave(&ec_cmd_q_lock, flags);
  87. list_add_tail(&desc->node, &ec_cmd_q);
  88. spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
  89. schedule_work(&ec_worker);
  90. }
  91. int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
  92. {
  93. struct olpc_ec_priv *ec = ec_priv;
  94. struct ec_cmd_desc desc;
  95. /* XXX: this will be removed in later patches */
  96. /* Are we using old-style callers? */
  97. if (!ec_driver || !ec_driver->ec_cmd)
  98. return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen);
  99. /* Ensure a driver and ec hook have been registered */
  100. if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
  101. return -ENODEV;
  102. if (!ec)
  103. return -ENOMEM;
  104. /* Suspending in the middle of a command hoses things really badly */
  105. if (WARN_ON(ec->suspended))
  106. return -EBUSY;
  107. might_sleep();
  108. desc.cmd = cmd;
  109. desc.inbuf = inbuf;
  110. desc.outbuf = outbuf;
  111. desc.inlen = inlen;
  112. desc.outlen = outlen;
  113. desc.err = 0;
  114. init_completion(&desc.finished);
  115. queue_ec_descriptor(&desc);
  116. /* Timeouts must be handled in the platform-specific EC hook */
  117. wait_for_completion(&desc.finished);
  118. /* The worker thread dequeues the cmd; no need to do anything here */
  119. return desc.err;
  120. }
  121. EXPORT_SYMBOL_GPL(olpc_ec_cmd);
  122. static int olpc_ec_probe(struct platform_device *pdev)
  123. {
  124. struct olpc_ec_priv *ec;
  125. int err;
  126. if (!ec_driver)
  127. return -ENODEV;
  128. ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  129. if (!ec)
  130. return -ENOMEM;
  131. ec->drv = ec_driver;
  132. ec_priv = ec;
  133. platform_set_drvdata(pdev, ec);
  134. err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
  135. return err;
  136. }
  137. static int olpc_ec_suspend(struct device *dev)
  138. {
  139. struct platform_device *pdev = to_platform_device(dev);
  140. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  141. int err = 0;
  142. if (ec_driver->suspend)
  143. err = ec_driver->suspend(pdev);
  144. if (!err)
  145. ec->suspended = true;
  146. return err;
  147. }
  148. static int olpc_ec_resume(struct device *dev)
  149. {
  150. struct platform_device *pdev = to_platform_device(dev);
  151. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  152. ec->suspended = false;
  153. return ec_driver->resume ? ec_driver->resume(pdev) : 0;
  154. }
  155. static const struct dev_pm_ops olpc_ec_pm_ops = {
  156. .suspend_late = olpc_ec_suspend,
  157. .resume_early = olpc_ec_resume,
  158. };
  159. static struct platform_driver olpc_ec_plat_driver = {
  160. .probe = olpc_ec_probe,
  161. .driver = {
  162. .name = "olpc-ec",
  163. .pm = &olpc_ec_pm_ops,
  164. },
  165. };
  166. static int __init olpc_ec_init_module(void)
  167. {
  168. return platform_driver_register(&olpc_ec_plat_driver);
  169. }
  170. module_init(olpc_ec_init_module);
  171. MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
  172. MODULE_LICENSE("GPL");