olpc-ec.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Generic driver for the OLPC Embedded Controller.
  3. *
  4. * Author: Andres Salomon <dilinger@queued.net>
  5. *
  6. * Copyright (C) 2011-2012 One Laptop per Child Foundation.
  7. *
  8. * Licensed under the GPL v2 or later.
  9. */
  10. #include <linux/completion.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/mutex.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <linux/olpc-ec.h>
  20. #include <asm/olpc.h>
  21. struct ec_cmd_desc {
  22. u8 cmd;
  23. u8 *inbuf, *outbuf;
  24. size_t inlen, outlen;
  25. int err;
  26. struct completion finished;
  27. struct list_head node;
  28. void *priv;
  29. };
  30. struct olpc_ec_priv {
  31. struct olpc_ec_driver *drv;
  32. struct work_struct worker;
  33. struct mutex cmd_lock;
  34. /* Pending EC commands */
  35. struct list_head cmd_q;
  36. spinlock_t cmd_q_lock;
  37. struct dentry *dbgfs_dir;
  38. /*
  39. * Running an EC command while suspending means we don't always finish
  40. * the command before the machine suspends. This means that the EC
  41. * is expecting the command protocol to finish, but we after a period
  42. * of time (while the OS is asleep) the EC times out and restarts its
  43. * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
  44. * middle of the command protocol, starts throwing random things at
  45. * the EC... and everyone's uphappy.
  46. */
  47. bool suspended;
  48. };
  49. static struct olpc_ec_driver *ec_driver;
  50. static struct olpc_ec_priv *ec_priv;
  51. static void *ec_cb_arg;
  52. void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
  53. {
  54. ec_driver = drv;
  55. ec_cb_arg = arg;
  56. }
  57. EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
  58. static void olpc_ec_worker(struct work_struct *w)
  59. {
  60. struct olpc_ec_priv *ec = container_of(w, struct olpc_ec_priv, worker);
  61. struct ec_cmd_desc *desc = NULL;
  62. unsigned long flags;
  63. /* Grab the first pending command from the queue */
  64. spin_lock_irqsave(&ec->cmd_q_lock, flags);
  65. if (!list_empty(&ec->cmd_q)) {
  66. desc = list_first_entry(&ec->cmd_q, struct ec_cmd_desc, node);
  67. list_del(&desc->node);
  68. }
  69. spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
  70. /* Do we actually have anything to do? */
  71. if (!desc)
  72. return;
  73. /* Protect the EC hw with a mutex; only run one cmd at a time */
  74. mutex_lock(&ec->cmd_lock);
  75. desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
  76. desc->outbuf, desc->outlen, ec_cb_arg);
  77. mutex_unlock(&ec->cmd_lock);
  78. /* Finished, wake up olpc_ec_cmd() */
  79. complete(&desc->finished);
  80. /* Run the worker thread again in case there are more cmds pending */
  81. schedule_work(&ec->worker);
  82. }
  83. /*
  84. * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
  85. * locking is pretty critical.
  86. */
  87. static void queue_ec_descriptor(struct ec_cmd_desc *desc,
  88. struct olpc_ec_priv *ec)
  89. {
  90. unsigned long flags;
  91. INIT_LIST_HEAD(&desc->node);
  92. spin_lock_irqsave(&ec->cmd_q_lock, flags);
  93. list_add_tail(&desc->node, &ec->cmd_q);
  94. spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
  95. schedule_work(&ec->worker);
  96. }
  97. int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
  98. {
  99. struct olpc_ec_priv *ec = ec_priv;
  100. struct ec_cmd_desc desc;
  101. /* Ensure a driver and ec hook have been registered */
  102. if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
  103. return -ENODEV;
  104. if (!ec)
  105. return -ENOMEM;
  106. /* Suspending in the middle of a command hoses things really badly */
  107. if (WARN_ON(ec->suspended))
  108. return -EBUSY;
  109. might_sleep();
  110. desc.cmd = cmd;
  111. desc.inbuf = inbuf;
  112. desc.outbuf = outbuf;
  113. desc.inlen = inlen;
  114. desc.outlen = outlen;
  115. desc.err = 0;
  116. init_completion(&desc.finished);
  117. queue_ec_descriptor(&desc, ec);
  118. /* Timeouts must be handled in the platform-specific EC hook */
  119. wait_for_completion(&desc.finished);
  120. /* The worker thread dequeues the cmd; no need to do anything here */
  121. return desc.err;
  122. }
  123. EXPORT_SYMBOL_GPL(olpc_ec_cmd);
  124. #ifdef CONFIG_DEBUG_FS
  125. /*
  126. * debugfs support for "generic commands", to allow sending
  127. * arbitrary EC commands from userspace.
  128. */
  129. #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
  130. #define EC_MAX_CMD_REPLY (8)
  131. static DEFINE_MUTEX(ec_dbgfs_lock);
  132. static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY];
  133. static unsigned int ec_dbgfs_resp_bytes;
  134. static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
  135. size_t size, loff_t *ppos)
  136. {
  137. int i, m;
  138. unsigned char ec_cmd[EC_MAX_CMD_ARGS];
  139. unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
  140. char cmdbuf[64];
  141. int ec_cmd_bytes;
  142. mutex_lock(&ec_dbgfs_lock);
  143. size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
  144. m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
  145. &ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
  146. &ec_cmd_int[3], &ec_cmd_int[4], &ec_cmd_int[5]);
  147. if (m < 2 || ec_dbgfs_resp_bytes > EC_MAX_CMD_REPLY) {
  148. /* reset to prevent overflow on read */
  149. ec_dbgfs_resp_bytes = 0;
  150. pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
  151. size = -EINVAL;
  152. goto out;
  153. }
  154. /* convert scanf'd ints to char */
  155. ec_cmd_bytes = m - 2;
  156. for (i = 0; i <= ec_cmd_bytes; i++)
  157. ec_cmd[i] = ec_cmd_int[i];
  158. pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %5ph, want %d returns\n",
  159. ec_cmd[0], ec_cmd_bytes, ec_cmd + 1,
  160. ec_dbgfs_resp_bytes);
  161. olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
  162. ec_cmd_bytes, ec_dbgfs_resp, ec_dbgfs_resp_bytes);
  163. pr_debug("olpc-ec: response %8ph (%d bytes expected)\n",
  164. ec_dbgfs_resp, ec_dbgfs_resp_bytes);
  165. out:
  166. mutex_unlock(&ec_dbgfs_lock);
  167. return size;
  168. }
  169. static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
  170. size_t size, loff_t *ppos)
  171. {
  172. unsigned int i, r;
  173. char *rp;
  174. char respbuf[64];
  175. mutex_lock(&ec_dbgfs_lock);
  176. rp = respbuf;
  177. rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
  178. for (i = 1; i < ec_dbgfs_resp_bytes; i++)
  179. rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
  180. mutex_unlock(&ec_dbgfs_lock);
  181. rp += sprintf(rp, "\n");
  182. r = rp - respbuf;
  183. return simple_read_from_buffer(buf, size, ppos, respbuf, r);
  184. }
  185. static const struct file_operations ec_dbgfs_ops = {
  186. .write = ec_dbgfs_cmd_write,
  187. .read = ec_dbgfs_cmd_read,
  188. };
  189. static struct dentry *olpc_ec_setup_debugfs(void)
  190. {
  191. struct dentry *dbgfs_dir;
  192. dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
  193. if (IS_ERR_OR_NULL(dbgfs_dir))
  194. return NULL;
  195. debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
  196. return dbgfs_dir;
  197. }
  198. #else
  199. static struct dentry *olpc_ec_setup_debugfs(void)
  200. {
  201. return NULL;
  202. }
  203. #endif /* CONFIG_DEBUG_FS */
  204. static int olpc_ec_probe(struct platform_device *pdev)
  205. {
  206. struct olpc_ec_priv *ec;
  207. int err;
  208. if (!ec_driver)
  209. return -ENODEV;
  210. ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  211. if (!ec)
  212. return -ENOMEM;
  213. ec->drv = ec_driver;
  214. INIT_WORK(&ec->worker, olpc_ec_worker);
  215. mutex_init(&ec->cmd_lock);
  216. INIT_LIST_HEAD(&ec->cmd_q);
  217. spin_lock_init(&ec->cmd_q_lock);
  218. ec_priv = ec;
  219. platform_set_drvdata(pdev, ec);
  220. err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
  221. if (err) {
  222. ec_priv = NULL;
  223. kfree(ec);
  224. } else {
  225. ec->dbgfs_dir = olpc_ec_setup_debugfs();
  226. }
  227. return err;
  228. }
  229. static int olpc_ec_suspend(struct device *dev)
  230. {
  231. struct platform_device *pdev = to_platform_device(dev);
  232. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  233. int err = 0;
  234. if (ec_driver->suspend)
  235. err = ec_driver->suspend(pdev);
  236. if (!err)
  237. ec->suspended = true;
  238. return err;
  239. }
  240. static int olpc_ec_resume(struct device *dev)
  241. {
  242. struct platform_device *pdev = to_platform_device(dev);
  243. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  244. ec->suspended = false;
  245. return ec_driver->resume ? ec_driver->resume(pdev) : 0;
  246. }
  247. static const struct dev_pm_ops olpc_ec_pm_ops = {
  248. .suspend_late = olpc_ec_suspend,
  249. .resume_early = olpc_ec_resume,
  250. };
  251. static struct platform_driver olpc_ec_plat_driver = {
  252. .probe = olpc_ec_probe,
  253. .driver = {
  254. .name = "olpc-ec",
  255. .pm = &olpc_ec_pm_ops,
  256. },
  257. };
  258. static int __init olpc_ec_init_module(void)
  259. {
  260. return platform_driver_register(&olpc_ec_plat_driver);
  261. }
  262. arch_initcall(olpc_ec_init_module);