olpc-ec.c 7.8 KB

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