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/spinlock.h>
10 #include <linux/mutex.h>
11 #include <linux/workqueue.h>
12 #include <linux/module.h>
13 #include <linux/list.h>
14 #include <linux/olpc-ec.h>
23 struct completion finished;
24 struct list_head node;
29 static void olpc_ec_worker(struct work_struct *w);
31 static DECLARE_WORK(ec_worker, olpc_ec_worker);
32 static LIST_HEAD(ec_cmd_q);
33 static DEFINE_SPINLOCK(ec_cmd_q_lock);
35 static struct olpc_ec_driver *ec_driver;
36 static void *ec_cb_arg;
37 static DEFINE_MUTEX(ec_cb_lock);
39 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
44 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
46 static void olpc_ec_worker(struct work_struct *w)
48 struct ec_cmd_desc *desc = NULL;
51 /* Grab the first pending command from the queue */
52 spin_lock_irqsave(&ec_cmd_q_lock, flags);
53 if (!list_empty(&ec_cmd_q)) {
54 desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node);
55 list_del(&desc->node);
57 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
59 /* Do we actually have anything to do? */
63 /* Protect the EC hw with a mutex; only run one cmd at a time */
64 mutex_lock(&ec_cb_lock);
65 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
66 desc->outbuf, desc->outlen, ec_cb_arg);
67 mutex_unlock(&ec_cb_lock);
69 /* Finished, wake up olpc_ec_cmd() */
70 complete(&desc->finished);
72 /* Run the worker thread again in case there are more cmds pending */
73 schedule_work(&ec_worker);
77 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
78 * locking is pretty critical.
80 static void queue_ec_descriptor(struct ec_cmd_desc *desc)
84 INIT_LIST_HEAD(&desc->node);
86 spin_lock_irqsave(&ec_cmd_q_lock, flags);
87 list_add_tail(&desc->node, &ec_cmd_q);
88 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
90 schedule_work(&ec_worker);
93 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
95 struct ec_cmd_desc desc;
97 /* XXX: this will be removed in later patches */
98 /* Are we using old-style callers? */
99 if (!ec_driver || !ec_driver->ec_cmd)
100 return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen);
102 /* Ensure a driver and ec hook have been registered */
103 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
110 desc.outbuf = outbuf;
112 desc.outlen = outlen;
114 init_completion(&desc.finished);
116 queue_ec_descriptor(&desc);
118 /* Timeouts must be handled in the platform-specific EC hook */
119 wait_for_completion(&desc.finished);
121 /* The worker thread dequeues the cmd; no need to do anything here */
124 EXPORT_SYMBOL_GPL(olpc_ec_cmd);