olpc-ec.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/workqueue.h>
  12. #include <linux/module.h>
  13. #include <linux/list.h>
  14. #include <linux/olpc-ec.h>
  15. #include <asm/olpc.h>
  16. struct ec_cmd_desc {
  17. u8 cmd;
  18. u8 *inbuf, *outbuf;
  19. size_t inlen, outlen;
  20. int err;
  21. struct completion finished;
  22. struct list_head node;
  23. void *priv;
  24. };
  25. static void olpc_ec_worker(struct work_struct *w);
  26. static DECLARE_WORK(ec_worker, olpc_ec_worker);
  27. static LIST_HEAD(ec_cmd_q);
  28. static DEFINE_SPINLOCK(ec_cmd_q_lock);
  29. static struct olpc_ec_driver *ec_driver;
  30. static void *ec_cb_arg;
  31. static DEFINE_MUTEX(ec_cb_lock);
  32. void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
  33. {
  34. ec_driver = drv;
  35. ec_cb_arg = arg;
  36. }
  37. EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
  38. static void olpc_ec_worker(struct work_struct *w)
  39. {
  40. struct ec_cmd_desc *desc = NULL;
  41. unsigned long flags;
  42. /* Grab the first pending command from the queue */
  43. spin_lock_irqsave(&ec_cmd_q_lock, flags);
  44. if (!list_empty(&ec_cmd_q)) {
  45. desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node);
  46. list_del(&desc->node);
  47. }
  48. spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
  49. /* Do we actually have anything to do? */
  50. if (!desc)
  51. return;
  52. /* Protect the EC hw with a mutex; only run one cmd at a time */
  53. mutex_lock(&ec_cb_lock);
  54. desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
  55. desc->outbuf, desc->outlen, ec_cb_arg);
  56. mutex_unlock(&ec_cb_lock);
  57. /* Finished, wake up olpc_ec_cmd() */
  58. complete(&desc->finished);
  59. /* Run the worker thread again in case there are more cmds pending */
  60. schedule_work(&ec_worker);
  61. }
  62. /*
  63. * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
  64. * locking is pretty critical.
  65. */
  66. static void queue_ec_descriptor(struct ec_cmd_desc *desc)
  67. {
  68. unsigned long flags;
  69. INIT_LIST_HEAD(&desc->node);
  70. spin_lock_irqsave(&ec_cmd_q_lock, flags);
  71. list_add_tail(&desc->node, &ec_cmd_q);
  72. spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
  73. schedule_work(&ec_worker);
  74. }
  75. int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
  76. {
  77. struct ec_cmd_desc desc;
  78. /* XXX: this will be removed in later patches */
  79. /* Are we using old-style callers? */
  80. if (!ec_driver || !ec_driver->ec_cmd)
  81. return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen);
  82. /* Ensure a driver and ec hook have been registered */
  83. if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
  84. return -ENODEV;
  85. might_sleep();
  86. desc.cmd = cmd;
  87. desc.inbuf = inbuf;
  88. desc.outbuf = outbuf;
  89. desc.inlen = inlen;
  90. desc.outlen = outlen;
  91. desc.err = 0;
  92. init_completion(&desc.finished);
  93. queue_ec_descriptor(&desc);
  94. /* Timeouts must be handled in the platform-specific EC hook */
  95. wait_for_completion(&desc.finished);
  96. /* The worker thread dequeues the cmd; no need to do anything here */
  97. return desc.err;
  98. }
  99. EXPORT_SYMBOL_GPL(olpc_ec_cmd);