diff options
author | Andres Salomon <dilinger@queued.net> | 2012-07-11 20:40:25 -0400 |
---|---|---|
committer | Andres Salomon <dilinger@queued.net> | 2012-07-31 23:27:30 -0400 |
commit | 3d26c20bae9e97c98f7240184427d3a38515d406 (patch) | |
tree | 987a82438ffe61b675cf08d33b3e11154b605f58 /drivers/platform | |
parent | 3bf9428f220911795edde453a95f9509945004e5 (diff) |
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>
Diffstat (limited to 'drivers/platform')
-rw-r--r-- | drivers/platform/olpc/olpc-ec.c | 112 |
1 files changed, 110 insertions, 2 deletions
diff --git a/drivers/platform/olpc/olpc-ec.c b/drivers/platform/olpc/olpc-ec.c index 42026036cd3e..44e6a4fae79b 100644 --- a/drivers/platform/olpc/olpc-ec.c +++ b/drivers/platform/olpc/olpc-ec.c | |||
@@ -5,12 +5,120 @@ | |||
5 | * | 5 | * |
6 | * Licensed under the GPL v2 or later. | 6 | * Licensed under the GPL v2 or later. |
7 | */ | 7 | */ |
8 | #include <linux/completion.h> | ||
9 | #include <linux/spinlock.h> | ||
10 | #include <linux/mutex.h> | ||
11 | #include <linux/workqueue.h> | ||
8 | #include <linux/module.h> | 12 | #include <linux/module.h> |
13 | #include <linux/list.h> | ||
14 | #include <linux/olpc-ec.h> | ||
9 | #include <asm/olpc.h> | 15 | #include <asm/olpc.h> |
10 | 16 | ||
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 | |||
11 | int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen) | 93 | int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen) |
12 | { | 94 | { |
13 | /* Currently a stub; this will be expanded upon later. */ | 95 | struct ec_cmd_desc desc; |
14 | return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen); | 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; | ||
15 | } | 123 | } |
16 | EXPORT_SYMBOL_GPL(olpc_ec_cmd); | 124 | EXPORT_SYMBOL_GPL(olpc_ec_cmd); |