olpc-ec.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 %02x %02x %02x %02x %02x, want %d returns\n",
  157. ec_cmd[0], ec_cmd_bytes, ec_cmd[1], ec_cmd[2],
  158. ec_cmd[3], ec_cmd[4], ec_cmd[5], 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 %02x %02x %02x %02x %02x %02x %02x %02x (%d bytes expected)\n",
  162. ec_dbgfs_resp[0], ec_dbgfs_resp[1], ec_dbgfs_resp[2],
  163. ec_dbgfs_resp[3], ec_dbgfs_resp[4], ec_dbgfs_resp[5],
  164. ec_dbgfs_resp[6], ec_dbgfs_resp[7],
  165. ec_dbgfs_resp_bytes);
  166. out:
  167. mutex_unlock(&ec_dbgfs_lock);
  168. return size;
  169. }
  170. static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
  171. size_t size, loff_t *ppos)
  172. {
  173. unsigned int i, r;
  174. char *rp;
  175. char respbuf[64];
  176. mutex_lock(&ec_dbgfs_lock);
  177. rp = respbuf;
  178. rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
  179. for (i = 1; i < ec_dbgfs_resp_bytes; i++)
  180. rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
  181. mutex_unlock(&ec_dbgfs_lock);
  182. rp += sprintf(rp, "\n");
  183. r = rp - respbuf;
  184. return simple_read_from_buffer(buf, size, ppos, respbuf, r);
  185. }
  186. static const struct file_operations ec_dbgfs_ops = {
  187. .write = ec_dbgfs_cmd_write,
  188. .read = ec_dbgfs_cmd_read,
  189. };
  190. static struct dentry *olpc_ec_setup_debugfs(void)
  191. {
  192. struct dentry *dbgfs_dir;
  193. dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
  194. if (IS_ERR_OR_NULL(dbgfs_dir))
  195. return NULL;
  196. debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
  197. return dbgfs_dir;
  198. }
  199. #else
  200. static struct dentry *olpc_ec_setup_debugfs(void)
  201. {
  202. return NULL;
  203. }
  204. #endif /* CONFIG_DEBUG_FS */
  205. static int olpc_ec_probe(struct platform_device *pdev)
  206. {
  207. struct olpc_ec_priv *ec;
  208. int err;
  209. if (!ec_driver)
  210. return -ENODEV;
  211. ec = kzalloc(sizeof(*ec), GFP_KERNEL);
  212. if (!ec)
  213. return -ENOMEM;
  214. ec->drv = ec_driver;
  215. INIT_WORK(&ec->worker, olpc_ec_worker);
  216. mutex_init(&ec->cmd_lock);
  217. INIT_LIST_HEAD(&ec->cmd_q);
  218. spin_lock_init(&ec->cmd_q_lock);
  219. ec_priv = ec;
  220. platform_set_drvdata(pdev, ec);
  221. err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
  222. if (err) {
  223. ec_priv = NULL;
  224. kfree(ec);
  225. } else {
  226. ec->dbgfs_dir = olpc_ec_setup_debugfs();
  227. }
  228. return err;
  229. }
  230. static int olpc_ec_suspend(struct device *dev)
  231. {
  232. struct platform_device *pdev = to_platform_device(dev);
  233. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  234. int err = 0;
  235. if (ec_driver->suspend)
  236. err = ec_driver->suspend(pdev);
  237. if (!err)
  238. ec->suspended = true;
  239. return err;
  240. }
  241. static int olpc_ec_resume(struct device *dev)
  242. {
  243. struct platform_device *pdev = to_platform_device(dev);
  244. struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
  245. ec->suspended = false;
  246. return ec_driver->resume ? ec_driver->resume(pdev) : 0;
  247. }
  248. static const struct dev_pm_ops olpc_ec_pm_ops = {
  249. .suspend_late = olpc_ec_suspend,
  250. .resume_early = olpc_ec_resume,
  251. };
  252. static struct platform_driver olpc_ec_plat_driver = {
  253. .probe = olpc_ec_probe,
  254. .driver = {
  255. .name = "olpc-ec",
  256. .pm = &olpc_ec_pm_ops,
  257. },
  258. };
  259. static int __init olpc_ec_init_module(void)
  260. {
  261. return platform_driver_register(&olpc_ec_plat_driver);
  262. }
  263. module_init(olpc_ec_init_module);
  264. MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
  265. MODULE_LICENSE("GPL");