olpc-ec.c revision 6cca83d498bda0999302079bd59786370590c5c2
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 20struct ec_cmd_desc { 21 u8 cmd; 22 u8 *inbuf, *outbuf; 23 size_t inlen, outlen; 24 25 int err; 26 struct completion finished; 27 struct list_head node; 28 29 void *priv; 30}; 31 32struct olpc_ec_priv { 33 struct olpc_ec_driver *drv; 34 35 struct dentry *dbgfs_dir; 36 37 /* 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. 45 */ 46 bool suspended; 47}; 48 49static void olpc_ec_worker(struct work_struct *w); 50 51static DECLARE_WORK(ec_worker, olpc_ec_worker); 52static LIST_HEAD(ec_cmd_q); 53static DEFINE_SPINLOCK(ec_cmd_q_lock); 54 55static struct olpc_ec_driver *ec_driver; 56static struct olpc_ec_priv *ec_priv; 57static void *ec_cb_arg; 58static DEFINE_MUTEX(ec_cb_lock); 59 60void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg) 61{ 62 ec_driver = drv; 63 ec_cb_arg = arg; 64} 65EXPORT_SYMBOL_GPL(olpc_ec_driver_register); 66 67static void olpc_ec_worker(struct work_struct *w) 68{ 69 struct ec_cmd_desc *desc = NULL; 70 unsigned long flags; 71 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); 77 } 78 spin_unlock_irqrestore(&ec_cmd_q_lock, flags); 79 80 /* Do we actually have anything to do? */ 81 if (!desc) 82 return; 83 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); 89 90 /* Finished, wake up olpc_ec_cmd() */ 91 complete(&desc->finished); 92 93 /* Run the worker thread again in case there are more cmds pending */ 94 schedule_work(&ec_worker); 95} 96 97/* 98 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so 99 * locking is pretty critical. 100 */ 101static void queue_ec_descriptor(struct ec_cmd_desc *desc) 102{ 103 unsigned long flags; 104 105 INIT_LIST_HEAD(&desc->node); 106 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); 110 111 schedule_work(&ec_worker); 112} 113 114int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen) 115{ 116 struct olpc_ec_priv *ec = ec_priv; 117 struct ec_cmd_desc desc; 118 119 /* Ensure a driver and ec hook have been registered */ 120 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd)) 121 return -ENODEV; 122 123 if (!ec) 124 return -ENOMEM; 125 126 /* Suspending in the middle of a command hoses things really badly */ 127 if (WARN_ON(ec->suspended)) 128 return -EBUSY; 129 130 might_sleep(); 131 132 desc.cmd = cmd; 133 desc.inbuf = inbuf; 134 desc.outbuf = outbuf; 135 desc.inlen = inlen; 136 desc.outlen = outlen; 137 desc.err = 0; 138 init_completion(&desc.finished); 139 140 queue_ec_descriptor(&desc); 141 142 /* Timeouts must be handled in the platform-specific EC hook */ 143 wait_for_completion(&desc.finished); 144 145 /* The worker thread dequeues the cmd; no need to do anything here */ 146 return desc.err; 147} 148EXPORT_SYMBOL_GPL(olpc_ec_cmd); 149 150#ifdef CONFIG_DEBUG_FS 151 152/* 153 * debugfs support for "generic commands", to allow sending 154 * arbitrary EC commands from userspace. 155 */ 156 157#define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */ 158#define EC_MAX_CMD_REPLY (8) 159 160static DEFINE_MUTEX(ec_dbgfs_lock); 161static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY]; 162static unsigned int ec_dbgfs_resp_bytes; 163 164static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf, 165 size_t size, loff_t *ppos) 166{ 167 int i, m; 168 unsigned char ec_cmd[EC_MAX_CMD_ARGS]; 169 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS]; 170 char cmdbuf[64]; 171 int ec_cmd_bytes; 172 173 mutex_lock(&ec_dbgfs_lock); 174 175 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size); 176 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; 183 184 pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n"); 185 size = -EINVAL; 186 goto out; 187 } 188 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]; 193 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); 197 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); 200 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); 206 207out: 208 mutex_unlock(&ec_dbgfs_lock); 209 return size; 210} 211 212static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf, 213 size_t size, loff_t *ppos) 214{ 215 unsigned int i, r; 216 char *rp; 217 char respbuf[64]; 218 219 mutex_lock(&ec_dbgfs_lock); 220 rp = respbuf; 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"); 226 227 r = rp - respbuf; 228 return simple_read_from_buffer(buf, size, ppos, respbuf, r); 229} 230 231static const struct file_operations ec_dbgfs_ops = { 232 .write = ec_dbgfs_cmd_write, 233 .read = ec_dbgfs_cmd_read, 234}; 235 236static struct dentry *olpc_ec_setup_debugfs(void) 237{ 238 struct dentry *dbgfs_dir; 239 240 dbgfs_dir = debugfs_create_dir("olpc-ec", NULL); 241 if (IS_ERR_OR_NULL(dbgfs_dir)) 242 return NULL; 243 244 debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops); 245 246 return dbgfs_dir; 247} 248 249#else 250 251static struct dentry *olpc_ec_setup_debugfs(void) 252{ 253 return NULL; 254} 255 256#endif /* CONFIG_DEBUG_FS */ 257 258static int olpc_ec_probe(struct platform_device *pdev) 259{ 260 struct olpc_ec_priv *ec; 261 int err; 262 263 if (!ec_driver) 264 return -ENODEV; 265 266 ec = kzalloc(sizeof(*ec), GFP_KERNEL); 267 if (!ec) 268 return -ENOMEM; 269 ec->drv = ec_driver; 270 ec_priv = ec; 271 platform_set_drvdata(pdev, ec); 272 273 err = ec_driver->probe ? ec_driver->probe(pdev) : 0; 274 if (err) { 275 ec_priv = NULL; 276 kfree(ec); 277 } else { 278 ec->dbgfs_dir = olpc_ec_setup_debugfs(); 279 } 280 281 return err; 282} 283 284static int olpc_ec_suspend(struct device *dev) 285{ 286 struct platform_device *pdev = to_platform_device(dev); 287 struct olpc_ec_priv *ec = platform_get_drvdata(pdev); 288 int err = 0; 289 290 if (ec_driver->suspend) 291 err = ec_driver->suspend(pdev); 292 if (!err) 293 ec->suspended = true; 294 295 return err; 296} 297 298static int olpc_ec_resume(struct device *dev) 299{ 300 struct platform_device *pdev = to_platform_device(dev); 301 struct olpc_ec_priv *ec = platform_get_drvdata(pdev); 302 303 ec->suspended = false; 304 return ec_driver->resume ? ec_driver->resume(pdev) : 0; 305} 306 307static const struct dev_pm_ops olpc_ec_pm_ops = { 308 .suspend_late = olpc_ec_suspend, 309 .resume_early = olpc_ec_resume, 310}; 311 312static struct platform_driver olpc_ec_plat_driver = { 313 .probe = olpc_ec_probe, 314 .driver = { 315 .name = "olpc-ec", 316 .pm = &olpc_ec_pm_ops, 317 }, 318}; 319 320static int __init olpc_ec_init_module(void) 321{ 322 return platform_driver_register(&olpc_ec_plat_driver); 323} 324 325module_init(olpc_ec_init_module); 326 327MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>"); 328MODULE_LICENSE("GPL"); 329