Skip to content

Commit 3d26c20

Browse files
committed
Platform: OLPC: allow EC cmd to be overridden, and create a workqueue to call it
This provides a new API allows different OLPC architectures to override the EC driver. x86 and ARM OLPC machines use completely different EC backends. The olpc_ec_cmd is synchronous, and waits for the workqueue to send the command to the EC. Multiple callers can run olpc_ec_cmd() at once, and they will by serialized and sleep while only one executes on the EC at a time. We don't provide an unregister function, as that doesn't make sense within the context of OLPC machines - there's only ever 1 EC, it's critical to functionality, and it certainly not hotpluggable. Signed-off-by: Andres Salomon <dilinger@queued.net> Acked-by: Paul Fox <pgf@laptop.org> Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 3bf9428 commit 3d26c20

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

drivers/platform/olpc/olpc-ec.c

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,120 @@
55
*
66
* Licensed under the GPL v2 or later.
77
*/
8+
#include <linux/completion.h>
9+
#include <linux/spinlock.h>
10+
#include <linux/mutex.h>
11+
#include <linux/workqueue.h>
812
#include <linux/module.h>
13+
#include <linux/list.h>
14+
#include <linux/olpc-ec.h>
915
#include <asm/olpc.h>
1016

17+
struct ec_cmd_desc {
18+
u8 cmd;
19+
u8 *inbuf, *outbuf;
20+
size_t inlen, outlen;
21+
22+
int err;
23+
struct completion finished;
24+
struct list_head node;
25+
26+
void *priv;
27+
};
28+
29+
static void olpc_ec_worker(struct work_struct *w);
30+
31+
static DECLARE_WORK(ec_worker, olpc_ec_worker);
32+
static LIST_HEAD(ec_cmd_q);
33+
static DEFINE_SPINLOCK(ec_cmd_q_lock);
34+
35+
static struct olpc_ec_driver *ec_driver;
36+
static void *ec_cb_arg;
37+
static DEFINE_MUTEX(ec_cb_lock);
38+
39+
void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
40+
{
41+
ec_driver = drv;
42+
ec_cb_arg = arg;
43+
}
44+
EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
45+
46+
static void olpc_ec_worker(struct work_struct *w)
47+
{
48+
struct ec_cmd_desc *desc = NULL;
49+
unsigned long flags;
50+
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);
56+
}
57+
spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
58+
59+
/* Do we actually have anything to do? */
60+
if (!desc)
61+
return;
62+
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);
68+
69+
/* Finished, wake up olpc_ec_cmd() */
70+
complete(&desc->finished);
71+
72+
/* Run the worker thread again in case there are more cmds pending */
73+
schedule_work(&ec_worker);
74+
}
75+
76+
/*
77+
* Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
78+
* locking is pretty critical.
79+
*/
80+
static void queue_ec_descriptor(struct ec_cmd_desc *desc)
81+
{
82+
unsigned long flags;
83+
84+
INIT_LIST_HEAD(&desc->node);
85+
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);
89+
90+
schedule_work(&ec_worker);
91+
}
92+
1193
int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
1294
{
13-
/* Currently a stub; this will be expanded upon later. */
14-
return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen);
95+
struct ec_cmd_desc desc;
96+
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);
101+
102+
/* Ensure a driver and ec hook have been registered */
103+
if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
104+
return -ENODEV;
105+
106+
might_sleep();
107+
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+
116+
queue_ec_descriptor(&desc);
117+
118+
/* Timeouts must be handled in the platform-specific EC hook */
119+
wait_for_completion(&desc.finished);
120+
121+
/* The worker thread dequeues the cmd; no need to do anything here */
122+
return desc.err;
15123
}
16124
EXPORT_SYMBOL_GPL(olpc_ec_cmd);

include/linux/olpc-ec.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
#define EC_SCI_QUERY 0x84
1515
#define EC_EXT_SCI_QUERY 0x85
1616

17+
struct olpc_ec_driver {
18+
int (*ec_cmd)(u8, u8 *, size_t, u8 *, size_t, void *);
19+
};
20+
1721
#ifdef CONFIG_OLPC
1822

23+
extern void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg);
24+
1925
extern int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf,
2026
size_t outlen);
2127

0 commit comments

Comments
 (0)