olpc-ec.c 7.8 KB

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