2 * Generic driver for the OLPC Embedded Controller.
4 * Copyright (C) 2011-2012 One Laptop per Child Foundation.
6 * Licensed under the GPL v2 or later.
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>
26 struct completion finished;
27 struct list_head node;
33 struct olpc_ec_driver *drv;
35 struct dentry *dbgfs_dir;
38 * Running an EC command while suspending means we don't always finish
39 * the command before the machine suspends. This means that the EC
40 * is expecting the command protocol to finish, but we after a period
41 * of time (while the OS is asleep) the EC times out and restarts its
42 * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
43 * middle of the command protocol, starts throwing random things at
44 * the EC... and everyone's uphappy.
49 static void olpc_ec_worker(struct work_struct *w);
51 static DECLARE_WORK(ec_worker, olpc_ec_worker);
52 static LIST_HEAD(ec_cmd_q);
53 static DEFINE_SPINLOCK(ec_cmd_q_lock);
55 static struct olpc_ec_driver *ec_driver;
56 static struct olpc_ec_priv *ec_priv;
57 static void *ec_cb_arg;
58 static DEFINE_MUTEX(ec_cb_lock);
60 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
65 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
67 static void olpc_ec_worker(struct work_struct *w)
69 struct ec_cmd_desc *desc = NULL;
72 /* Grab the first pending command from the queue */
73 spin_lock_irqsave(&ec_cmd_q_lock, flags);
74 if (!list_empty(&ec_cmd_q)) {
75 desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node);
76 list_del(&desc->node);
78 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
80 /* Do we actually have anything to do? */
84 /* Protect the EC hw with a mutex; only run one cmd at a time */
85 mutex_lock(&ec_cb_lock);
86 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
87 desc->outbuf, desc->outlen, ec_cb_arg);
88 mutex_unlock(&ec_cb_lock);
90 /* Finished, wake up olpc_ec_cmd() */
91 complete(&desc->finished);
93 /* Run the worker thread again in case there are more cmds pending */
94 schedule_work(&ec_worker);
98 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
99 * locking is pretty critical.
101 static void queue_ec_descriptor(struct ec_cmd_desc *desc)
105 INIT_LIST_HEAD(&desc->node);
107 spin_lock_irqsave(&ec_cmd_q_lock, flags);
108 list_add_tail(&desc->node, &ec_cmd_q);
109 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
111 schedule_work(&ec_worker);
114 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
116 struct olpc_ec_priv *ec = ec_priv;
117 struct ec_cmd_desc desc;
119 /* Ensure a driver and ec hook have been registered */
120 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
126 /* Suspending in the middle of a command hoses things really badly */
127 if (WARN_ON(ec->suspended))
134 desc.outbuf = outbuf;
136 desc.outlen = outlen;
138 init_completion(&desc.finished);
140 queue_ec_descriptor(&desc);
142 /* Timeouts must be handled in the platform-specific EC hook */
143 wait_for_completion(&desc.finished);
145 /* The worker thread dequeues the cmd; no need to do anything here */
148 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
150 #ifdef CONFIG_DEBUG_FS
153 * debugfs support for "generic commands", to allow sending
154 * arbitrary EC commands from userspace.
157 #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
158 #define EC_MAX_CMD_REPLY (8)
160 static DEFINE_MUTEX(ec_dbgfs_lock);
161 static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY];
162 static unsigned int ec_dbgfs_resp_bytes;
164 static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
165 size_t size, loff_t *ppos)
168 unsigned char ec_cmd[EC_MAX_CMD_ARGS];
169 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
173 mutex_lock(&ec_dbgfs_lock);
175 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
177 m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
178 &ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
179 &ec_cmd_int[3], &ec_cmd_int[4], &ec_cmd_int[5]);
180 if (m < 2 || ec_dbgfs_resp_bytes > EC_MAX_CMD_REPLY) {
181 /* reset to prevent overflow on read */
182 ec_dbgfs_resp_bytes = 0;
184 pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
189 /* convert scanf'd ints to char */
190 ec_cmd_bytes = m - 2;
191 for (i = 0; i <= ec_cmd_bytes; i++)
192 ec_cmd[i] = ec_cmd_int[i];
194 pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %02x %02x %02x %02x %02x, want %d returns\n",
195 ec_cmd[0], ec_cmd_bytes, ec_cmd[1], ec_cmd[2],
196 ec_cmd[3], ec_cmd[4], ec_cmd[5], ec_dbgfs_resp_bytes);
198 olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
199 ec_cmd_bytes, ec_dbgfs_resp, ec_dbgfs_resp_bytes);
201 pr_debug("olpc-ec: response %02x %02x %02x %02x %02x %02x %02x %02x (%d bytes expected)\n",
202 ec_dbgfs_resp[0], ec_dbgfs_resp[1], ec_dbgfs_resp[2],
203 ec_dbgfs_resp[3], ec_dbgfs_resp[4], ec_dbgfs_resp[5],
204 ec_dbgfs_resp[6], ec_dbgfs_resp[7],
205 ec_dbgfs_resp_bytes);
208 mutex_unlock(&ec_dbgfs_lock);
212 static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
213 size_t size, loff_t *ppos)
219 mutex_lock(&ec_dbgfs_lock);
221 rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
222 for (i = 1; i < ec_dbgfs_resp_bytes; i++)
223 rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
224 mutex_unlock(&ec_dbgfs_lock);
225 rp += sprintf(rp, "\n");
228 return simple_read_from_buffer(buf, size, ppos, respbuf, r);
231 static const struct file_operations ec_dbgfs_ops = {
232 .write = ec_dbgfs_cmd_write,
233 .read = ec_dbgfs_cmd_read,
236 static struct dentry *olpc_ec_setup_debugfs(void)
238 struct dentry *dbgfs_dir;
240 dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
241 if (IS_ERR_OR_NULL(dbgfs_dir))
244 debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
251 static struct dentry *olpc_ec_setup_debugfs(void)
256 #endif /* CONFIG_DEBUG_FS */
258 static int olpc_ec_probe(struct platform_device *pdev)
260 struct olpc_ec_priv *ec;
266 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
271 platform_set_drvdata(pdev, ec);
273 err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
278 ec->dbgfs_dir = olpc_ec_setup_debugfs();
284 static int olpc_ec_suspend(struct device *dev)
286 struct platform_device *pdev = to_platform_device(dev);
287 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
290 if (ec_driver->suspend)
291 err = ec_driver->suspend(pdev);
293 ec->suspended = true;
298 static int olpc_ec_resume(struct device *dev)
300 struct platform_device *pdev = to_platform_device(dev);
301 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
303 ec->suspended = false;
304 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
307 static const struct dev_pm_ops olpc_ec_pm_ops = {
308 .suspend_late = olpc_ec_suspend,
309 .resume_early = olpc_ec_resume,
312 static struct platform_driver olpc_ec_plat_driver = {
313 .probe = olpc_ec_probe,
316 .pm = &olpc_ec_pm_ops,
320 static int __init olpc_ec_init_module(void)
322 return platform_driver_register(&olpc_ec_plat_driver);
325 module_init(olpc_ec_init_module);
327 MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
328 MODULE_LICENSE("GPL");