Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * cros_ec_lightbar - expose the Chromebook Pixel lightbar to userspace |
| 3 | * |
| 4 | * Copyright (C) 2014 Google, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #define pr_fmt(fmt) "cros_ec_lightbar: " fmt |
| 21 | |
| 22 | #include <linux/ctype.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/device.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/kobject.h> |
| 27 | #include <linux/mfd/cros_ec.h> |
| 28 | #include <linux/mfd/cros_ec_commands.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/platform_device.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/uaccess.h> |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 34 | #include <linux/slab.h> |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 35 | |
| 36 | #include "cros_ec_dev.h" |
| 37 | |
| 38 | /* Rate-limit the lightbar interface to prevent DoS. */ |
| 39 | static unsigned long lb_interval_jiffies = 50 * HZ / 1000; |
| 40 | |
Eric Caruso | abbb054 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 41 | /* |
| 42 | * Whether or not we have given userspace control of the lightbar. |
| 43 | * If this is true, we won't do anything during suspend/resume. |
| 44 | */ |
| 45 | static bool userspace_control; |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 46 | static struct cros_ec_dev *ec_with_lightbar; |
Eric Caruso | abbb054 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 47 | |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 48 | static ssize_t interval_msec_show(struct device *dev, |
| 49 | struct device_attribute *attr, char *buf) |
| 50 | { |
| 51 | unsigned long msec = lb_interval_jiffies * 1000 / HZ; |
| 52 | |
| 53 | return scnprintf(buf, PAGE_SIZE, "%lu\n", msec); |
| 54 | } |
| 55 | |
| 56 | static ssize_t interval_msec_store(struct device *dev, |
| 57 | struct device_attribute *attr, |
| 58 | const char *buf, size_t count) |
| 59 | { |
| 60 | unsigned long msec; |
| 61 | |
| 62 | if (kstrtoul(buf, 0, &msec)) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | lb_interval_jiffies = msec * HZ / 1000; |
| 66 | |
| 67 | return count; |
| 68 | } |
| 69 | |
| 70 | static DEFINE_MUTEX(lb_mutex); |
| 71 | /* Return 0 if able to throttle correctly, error otherwise */ |
| 72 | static int lb_throttle(void) |
| 73 | { |
| 74 | static unsigned long last_access; |
| 75 | unsigned long now, next_timeslot; |
| 76 | long delay; |
| 77 | int ret = 0; |
| 78 | |
| 79 | mutex_lock(&lb_mutex); |
| 80 | |
| 81 | now = jiffies; |
| 82 | next_timeslot = last_access + lb_interval_jiffies; |
| 83 | |
| 84 | if (time_before(now, next_timeslot)) { |
| 85 | delay = (long)(next_timeslot) - (long)now; |
| 86 | set_current_state(TASK_INTERRUPTIBLE); |
| 87 | if (schedule_timeout(delay) > 0) { |
| 88 | /* interrupted - just abort */ |
| 89 | ret = -EINTR; |
| 90 | goto out; |
| 91 | } |
| 92 | now = jiffies; |
| 93 | } |
| 94 | |
| 95 | last_access = now; |
| 96 | out: |
| 97 | mutex_unlock(&lb_mutex); |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 102 | static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 103 | { |
| 104 | struct cros_ec_command *msg; |
| 105 | int len; |
| 106 | |
| 107 | len = max(sizeof(struct ec_params_lightbar), |
| 108 | sizeof(struct ec_response_lightbar)); |
| 109 | |
| 110 | msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL); |
| 111 | if (!msg) |
| 112 | return NULL; |
| 113 | |
| 114 | msg->version = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 115 | msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 116 | msg->outsize = sizeof(struct ec_params_lightbar); |
| 117 | msg->insize = sizeof(struct ec_response_lightbar); |
| 118 | |
| 119 | return msg; |
| 120 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 121 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 122 | static int get_lightbar_version(struct cros_ec_dev *ec, |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 123 | uint32_t *ver_ptr, uint32_t *flg_ptr) |
| 124 | { |
| 125 | struct ec_params_lightbar *param; |
| 126 | struct ec_response_lightbar *resp; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 127 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 128 | int ret; |
| 129 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 130 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 131 | if (!msg) |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 132 | return 0; |
| 133 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 134 | param = (struct ec_params_lightbar *)msg->data; |
| 135 | param->cmd = LIGHTBAR_CMD_VERSION; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 136 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 137 | if (ret < 0) { |
| 138 | ret = 0; |
| 139 | goto exit; |
| 140 | } |
| 141 | |
| 142 | switch (msg->result) { |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 143 | case EC_RES_INVALID_PARAM: |
| 144 | /* Pixel had no version command. */ |
| 145 | if (ver_ptr) |
| 146 | *ver_ptr = 0; |
| 147 | if (flg_ptr) |
| 148 | *flg_ptr = 0; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 149 | ret = 1; |
| 150 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 151 | |
| 152 | case EC_RES_SUCCESS: |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 153 | resp = (struct ec_response_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 154 | |
| 155 | /* Future devices w/lightbars should implement this command */ |
| 156 | if (ver_ptr) |
| 157 | *ver_ptr = resp->version.num; |
| 158 | if (flg_ptr) |
| 159 | *flg_ptr = resp->version.flags; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 160 | ret = 1; |
| 161 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */ |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 165 | ret = 0; |
| 166 | exit: |
| 167 | kfree(msg); |
| 168 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static ssize_t version_show(struct device *dev, |
| 172 | struct device_attribute *attr, char *buf) |
| 173 | { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 174 | uint32_t version = 0, flags = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 175 | struct cros_ec_dev *ec = container_of(dev, |
| 176 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 177 | int ret; |
| 178 | |
| 179 | ret = lb_throttle(); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | /* This should always succeed, because we check during init. */ |
| 184 | if (!get_lightbar_version(ec, &version, &flags)) |
| 185 | return -EIO; |
| 186 | |
| 187 | return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags); |
| 188 | } |
| 189 | |
| 190 | static ssize_t brightness_store(struct device *dev, |
| 191 | struct device_attribute *attr, |
| 192 | const char *buf, size_t count) |
| 193 | { |
| 194 | struct ec_params_lightbar *param; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 195 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 196 | int ret; |
| 197 | unsigned int val; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 198 | struct cros_ec_dev *ec = container_of(dev, |
| 199 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 200 | |
| 201 | if (kstrtouint(buf, 0, &val)) |
| 202 | return -EINVAL; |
| 203 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 204 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 205 | if (!msg) |
| 206 | return -ENOMEM; |
| 207 | |
| 208 | param = (struct ec_params_lightbar *)msg->data; |
Stephen Barber | 256ab95 | 2015-06-09 13:04:43 +0200 | [diff] [blame] | 209 | param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS; |
| 210 | param->set_brightness.num = val; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 211 | ret = lb_throttle(); |
| 212 | if (ret) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 213 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 214 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 215 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 216 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 217 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 218 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 219 | if (msg->result != EC_RES_SUCCESS) { |
| 220 | ret = -EINVAL; |
| 221 | goto exit; |
| 222 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 223 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 224 | ret = count; |
| 225 | exit: |
| 226 | kfree(msg); |
| 227 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | |
| 231 | /* |
| 232 | * We expect numbers, and we'll keep reading until we find them, skipping over |
| 233 | * any whitespace (sysfs guarantees that the input is null-terminated). Every |
| 234 | * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first |
| 235 | * parsing error, if we don't parse any numbers, or if we have numbers left |
| 236 | * over. |
| 237 | */ |
| 238 | static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, |
| 239 | const char *buf, size_t count) |
| 240 | { |
| 241 | struct ec_params_lightbar *param; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 242 | struct cros_ec_command *msg; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 243 | struct cros_ec_dev *ec = container_of(dev, |
| 244 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 245 | unsigned int val[4]; |
| 246 | int ret, i = 0, j = 0, ok = 0; |
| 247 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 248 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 249 | if (!msg) |
| 250 | return -ENOMEM; |
| 251 | |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 252 | do { |
| 253 | /* Skip any whitespace */ |
| 254 | while (*buf && isspace(*buf)) |
| 255 | buf++; |
| 256 | |
| 257 | if (!*buf) |
| 258 | break; |
| 259 | |
| 260 | ret = sscanf(buf, "%i", &val[i++]); |
| 261 | if (ret == 0) |
Christian Engelmayer | f14ae09 | 2015-07-19 21:43:02 +0200 | [diff] [blame] | 262 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 263 | |
| 264 | if (i == 4) { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 265 | param = (struct ec_params_lightbar *)msg->data; |
Stephen Barber | 256ab95 | 2015-06-09 13:04:43 +0200 | [diff] [blame] | 266 | param->cmd = LIGHTBAR_CMD_SET_RGB; |
| 267 | param->set_rgb.led = val[0]; |
| 268 | param->set_rgb.red = val[1]; |
| 269 | param->set_rgb.green = val[2]; |
| 270 | param->set_rgb.blue = val[3]; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 271 | /* |
| 272 | * Throttle only the first of every four transactions, |
| 273 | * so that the user can update all four LEDs at once. |
| 274 | */ |
| 275 | if ((j++ % 4) == 0) { |
| 276 | ret = lb_throttle(); |
| 277 | if (ret) |
Christian Engelmayer | f14ae09 | 2015-07-19 21:43:02 +0200 | [diff] [blame] | 278 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 281 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 282 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 283 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 284 | |
Christian Engelmayer | f14ae09 | 2015-07-19 21:43:02 +0200 | [diff] [blame] | 285 | if (msg->result != EC_RES_SUCCESS) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 286 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 287 | |
| 288 | i = 0; |
| 289 | ok = 1; |
| 290 | } |
| 291 | |
| 292 | /* Skip over the number we just read */ |
| 293 | while (*buf && !isspace(*buf)) |
| 294 | buf++; |
| 295 | |
| 296 | } while (*buf); |
| 297 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 298 | exit: |
| 299 | kfree(msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 300 | return (ok && i == 0) ? count : -EINVAL; |
| 301 | } |
| 302 | |
Olof Johansson | 377415a | 2015-03-03 13:22:33 +0100 | [diff] [blame] | 303 | static char const *seqname[] = { |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 304 | "ERROR", "S5", "S3", "S0", "S5S3", "S3S0", |
Eric Caruso | be3ebeb | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 305 | "S0S3", "S3S5", "STOP", "RUN", "KONAMI", |
| 306 | "TAP", "PROGRAM", |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | static ssize_t sequence_show(struct device *dev, |
| 310 | struct device_attribute *attr, char *buf) |
| 311 | { |
| 312 | struct ec_params_lightbar *param; |
| 313 | struct ec_response_lightbar *resp; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 314 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 315 | int ret; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 316 | struct cros_ec_dev *ec = container_of(dev, |
| 317 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 318 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 319 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 320 | if (!msg) |
| 321 | return -ENOMEM; |
| 322 | |
| 323 | param = (struct ec_params_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 324 | param->cmd = LIGHTBAR_CMD_GET_SEQ; |
| 325 | ret = lb_throttle(); |
| 326 | if (ret) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 327 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 328 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 329 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 330 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 331 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 332 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 333 | if (msg->result != EC_RES_SUCCESS) { |
| 334 | ret = scnprintf(buf, PAGE_SIZE, |
| 335 | "ERROR: EC returned %d\n", msg->result); |
| 336 | goto exit; |
| 337 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 338 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 339 | resp = (struct ec_response_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 340 | if (resp->get_seq.num >= ARRAY_SIZE(seqname)) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 341 | ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 342 | else |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 343 | ret = scnprintf(buf, PAGE_SIZE, "%s\n", |
| 344 | seqname[resp->get_seq.num]); |
| 345 | |
| 346 | exit: |
| 347 | kfree(msg); |
| 348 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 351 | static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd) |
| 352 | { |
| 353 | struct ec_params_lightbar *param; |
| 354 | struct cros_ec_command *msg; |
| 355 | int ret; |
| 356 | |
| 357 | msg = alloc_lightbar_cmd_msg(ec); |
| 358 | if (!msg) |
| 359 | return -ENOMEM; |
| 360 | |
| 361 | param = (struct ec_params_lightbar *)msg->data; |
| 362 | param->cmd = cmd; |
| 363 | |
| 364 | ret = lb_throttle(); |
| 365 | if (ret) |
| 366 | goto error; |
| 367 | |
| 368 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
| 369 | if (ret < 0) |
| 370 | goto error; |
| 371 | if (msg->result != EC_RES_SUCCESS) { |
| 372 | ret = -EINVAL; |
| 373 | goto error; |
| 374 | } |
| 375 | ret = 0; |
| 376 | error: |
| 377 | kfree(msg); |
| 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable) |
| 383 | { |
| 384 | struct ec_params_lightbar *param; |
| 385 | struct cros_ec_command *msg; |
| 386 | int ret; |
| 387 | |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 388 | if (ec != ec_with_lightbar) |
| 389 | return 0; |
| 390 | |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 391 | msg = alloc_lightbar_cmd_msg(ec); |
| 392 | if (!msg) |
| 393 | return -ENOMEM; |
| 394 | |
| 395 | param = (struct ec_params_lightbar *)msg->data; |
| 396 | |
| 397 | param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL; |
| 398 | param->manual_suspend_ctrl.enable = enable; |
| 399 | |
| 400 | ret = lb_throttle(); |
| 401 | if (ret) |
| 402 | goto error; |
| 403 | |
| 404 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
| 405 | if (ret < 0) |
| 406 | goto error; |
| 407 | if (msg->result != EC_RES_SUCCESS) { |
| 408 | ret = -EINVAL; |
| 409 | goto error; |
| 410 | } |
| 411 | ret = 0; |
| 412 | error: |
| 413 | kfree(msg); |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | int lb_suspend(struct cros_ec_dev *ec) |
| 419 | { |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 420 | if (userspace_control || ec != ec_with_lightbar) |
Eric Caruso | abbb054 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 421 | return 0; |
| 422 | |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 423 | return lb_send_empty_cmd(ec, LIGHTBAR_CMD_SUSPEND); |
| 424 | } |
| 425 | |
| 426 | int lb_resume(struct cros_ec_dev *ec) |
| 427 | { |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 428 | if (userspace_control || ec != ec_with_lightbar) |
Eric Caruso | abbb054 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 429 | return 0; |
| 430 | |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 431 | return lb_send_empty_cmd(ec, LIGHTBAR_CMD_RESUME); |
| 432 | } |
| 433 | |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 434 | static ssize_t sequence_store(struct device *dev, struct device_attribute *attr, |
| 435 | const char *buf, size_t count) |
| 436 | { |
| 437 | struct ec_params_lightbar *param; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 438 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 439 | unsigned int num; |
| 440 | int ret, len; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 441 | struct cros_ec_dev *ec = container_of(dev, |
| 442 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 443 | |
| 444 | for (len = 0; len < count; len++) |
| 445 | if (!isalnum(buf[len])) |
| 446 | break; |
| 447 | |
| 448 | for (num = 0; num < ARRAY_SIZE(seqname); num++) |
| 449 | if (!strncasecmp(seqname[num], buf, len)) |
| 450 | break; |
| 451 | |
| 452 | if (num >= ARRAY_SIZE(seqname)) { |
| 453 | ret = kstrtouint(buf, 0, &num); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | } |
| 457 | |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 458 | msg = alloc_lightbar_cmd_msg(ec); |
| 459 | if (!msg) |
| 460 | return -ENOMEM; |
| 461 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 462 | param = (struct ec_params_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 463 | param->cmd = LIGHTBAR_CMD_SEQ; |
| 464 | param->seq.num = num; |
| 465 | ret = lb_throttle(); |
| 466 | if (ret) |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 467 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 468 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 469 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 470 | if (ret < 0) |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 471 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 472 | |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 473 | if (msg->result != EC_RES_SUCCESS) { |
| 474 | ret = -EINVAL; |
| 475 | goto exit; |
| 476 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 477 | |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 478 | ret = count; |
| 479 | exit: |
| 480 | kfree(msg); |
| 481 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Eric Caruso | be3ebeb | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 484 | static ssize_t program_store(struct device *dev, struct device_attribute *attr, |
| 485 | const char *buf, size_t count) |
| 486 | { |
| 487 | int extra_bytes, max_size, ret; |
| 488 | struct ec_params_lightbar *param; |
| 489 | struct cros_ec_command *msg; |
| 490 | struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev, |
| 491 | class_dev); |
| 492 | |
| 493 | /* |
| 494 | * We might need to reject the program for size reasons. The EC |
| 495 | * enforces a maximum program size, but we also don't want to try |
| 496 | * and send a program that is too big for the protocol. In order |
| 497 | * to ensure the latter, we also need to ensure we have extra bytes |
| 498 | * to represent the rest of the packet. |
| 499 | */ |
| 500 | extra_bytes = sizeof(*param) - sizeof(param->set_program.data); |
| 501 | max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes); |
| 502 | if (count > max_size) { |
| 503 | dev_err(dev, "Program is %u bytes, too long to send (max: %u)", |
| 504 | (unsigned int)count, max_size); |
| 505 | |
| 506 | return -EINVAL; |
| 507 | } |
| 508 | |
| 509 | msg = alloc_lightbar_cmd_msg(ec); |
| 510 | if (!msg) |
| 511 | return -ENOMEM; |
| 512 | |
| 513 | ret = lb_throttle(); |
| 514 | if (ret) |
| 515 | goto exit; |
| 516 | |
| 517 | dev_info(dev, "Copying %zu byte program to EC", count); |
| 518 | |
| 519 | param = (struct ec_params_lightbar *)msg->data; |
| 520 | param->cmd = LIGHTBAR_CMD_SET_PROGRAM; |
| 521 | |
| 522 | param->set_program.size = count; |
| 523 | memcpy(param->set_program.data, buf, count); |
| 524 | |
| 525 | /* |
| 526 | * We need to set the message size manually or else it will use |
| 527 | * EC_LB_PROG_LEN. This might be too long, and the program |
| 528 | * is unlikely to use all of the space. |
| 529 | */ |
| 530 | msg->outsize = count + extra_bytes; |
| 531 | |
| 532 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
| 533 | if (ret < 0) |
| 534 | goto exit; |
| 535 | if (msg->result != EC_RES_SUCCESS) { |
| 536 | ret = -EINVAL; |
| 537 | goto exit; |
| 538 | } |
| 539 | |
| 540 | ret = count; |
| 541 | exit: |
| 542 | kfree(msg); |
| 543 | |
| 544 | return ret; |
| 545 | } |
| 546 | |
Eric Caruso | abbb054 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 547 | static ssize_t userspace_control_show(struct device *dev, |
| 548 | struct device_attribute *attr, |
| 549 | char *buf) |
| 550 | { |
| 551 | return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control); |
| 552 | } |
| 553 | |
| 554 | static ssize_t userspace_control_store(struct device *dev, |
| 555 | struct device_attribute *attr, |
| 556 | const char *buf, |
| 557 | size_t count) |
| 558 | { |
| 559 | bool enable; |
| 560 | int ret; |
| 561 | |
| 562 | ret = strtobool(buf, &enable); |
| 563 | if (ret < 0) |
| 564 | return ret; |
| 565 | |
| 566 | userspace_control = enable; |
| 567 | |
| 568 | return count; |
| 569 | } |
| 570 | |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 571 | /* Module initialization */ |
| 572 | |
| 573 | static DEVICE_ATTR_RW(interval_msec); |
| 574 | static DEVICE_ATTR_RO(version); |
| 575 | static DEVICE_ATTR_WO(brightness); |
| 576 | static DEVICE_ATTR_WO(led_rgb); |
| 577 | static DEVICE_ATTR_RW(sequence); |
Eric Caruso | be3ebeb | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 578 | static DEVICE_ATTR_WO(program); |
Eric Caruso | abbb054 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 579 | static DEVICE_ATTR_RW(userspace_control); |
Eric Caruso | be3ebeb | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 580 | |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 581 | static struct attribute *__lb_cmds_attrs[] = { |
| 582 | &dev_attr_interval_msec.attr, |
| 583 | &dev_attr_version.attr, |
| 584 | &dev_attr_brightness.attr, |
| 585 | &dev_attr_led_rgb.attr, |
| 586 | &dev_attr_sequence.attr, |
Eric Caruso | be3ebeb | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 587 | &dev_attr_program.attr, |
Eric Caruso | abbb054 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 588 | &dev_attr_userspace_control.attr, |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 589 | NULL, |
| 590 | }; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 591 | |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 592 | bool ec_has_lightbar(struct cros_ec_dev *ec) |
| 593 | { |
| 594 | return !!get_lightbar_version(ec, NULL, NULL); |
| 595 | } |
| 596 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 597 | static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj, |
| 598 | struct attribute *a, int n) |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 599 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 600 | struct device *dev = container_of(kobj, struct device, kobj); |
| 601 | struct cros_ec_dev *ec = container_of(dev, |
| 602 | struct cros_ec_dev, class_dev); |
Geliang Tang | 48b9b6d | 2015-12-27 21:15:45 +0800 | [diff] [blame] | 603 | struct platform_device *pdev = to_platform_device(ec->dev); |
Clinton Sprain | d940f30 | 2016-05-11 11:05:35 -0700 | [diff] [blame] | 604 | struct cros_ec_platform *pdata = pdev->dev.platform_data; |
| 605 | int is_cros_ec; |
| 606 | |
| 607 | is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME); |
| 608 | |
| 609 | if (is_cros_ec != 0) |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 610 | return 0; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 611 | |
| 612 | /* Only instantiate this stuff if the EC has a lightbar */ |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 613 | if (ec_has_lightbar(ec)) { |
| 614 | ec_with_lightbar = ec; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 615 | return a->mode; |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 616 | } |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 617 | return 0; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 620 | struct attribute_group cros_ec_lightbar_attr_group = { |
| 621 | .name = "lightbar", |
| 622 | .attrs = __lb_cmds_attrs, |
| 623 | .is_visible = cros_ec_lightbar_attrs_are_visible, |
| 624 | }; |