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 | |
| 41 | static ssize_t interval_msec_show(struct device *dev, |
| 42 | struct device_attribute *attr, char *buf) |
| 43 | { |
| 44 | unsigned long msec = lb_interval_jiffies * 1000 / HZ; |
| 45 | |
| 46 | return scnprintf(buf, PAGE_SIZE, "%lu\n", msec); |
| 47 | } |
| 48 | |
| 49 | static ssize_t interval_msec_store(struct device *dev, |
| 50 | struct device_attribute *attr, |
| 51 | const char *buf, size_t count) |
| 52 | { |
| 53 | unsigned long msec; |
| 54 | |
| 55 | if (kstrtoul(buf, 0, &msec)) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | lb_interval_jiffies = msec * HZ / 1000; |
| 59 | |
| 60 | return count; |
| 61 | } |
| 62 | |
| 63 | static DEFINE_MUTEX(lb_mutex); |
| 64 | /* Return 0 if able to throttle correctly, error otherwise */ |
| 65 | static int lb_throttle(void) |
| 66 | { |
| 67 | static unsigned long last_access; |
| 68 | unsigned long now, next_timeslot; |
| 69 | long delay; |
| 70 | int ret = 0; |
| 71 | |
| 72 | mutex_lock(&lb_mutex); |
| 73 | |
| 74 | now = jiffies; |
| 75 | next_timeslot = last_access + lb_interval_jiffies; |
| 76 | |
| 77 | if (time_before(now, next_timeslot)) { |
| 78 | delay = (long)(next_timeslot) - (long)now; |
| 79 | set_current_state(TASK_INTERRUPTIBLE); |
| 80 | if (schedule_timeout(delay) > 0) { |
| 81 | /* interrupted - just abort */ |
| 82 | ret = -EINTR; |
| 83 | goto out; |
| 84 | } |
| 85 | now = jiffies; |
| 86 | } |
| 87 | |
| 88 | last_access = now; |
| 89 | out: |
| 90 | mutex_unlock(&lb_mutex); |
| 91 | |
| 92 | return ret; |
| 93 | } |
| 94 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 95 | 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] | 96 | { |
| 97 | struct cros_ec_command *msg; |
| 98 | int len; |
| 99 | |
| 100 | len = max(sizeof(struct ec_params_lightbar), |
| 101 | sizeof(struct ec_response_lightbar)); |
| 102 | |
| 103 | msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL); |
| 104 | if (!msg) |
| 105 | return NULL; |
| 106 | |
| 107 | msg->version = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 108 | msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 109 | msg->outsize = sizeof(struct ec_params_lightbar); |
| 110 | msg->insize = sizeof(struct ec_response_lightbar); |
| 111 | |
| 112 | return msg; |
| 113 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 114 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 115 | static int get_lightbar_version(struct cros_ec_dev *ec, |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 116 | uint32_t *ver_ptr, uint32_t *flg_ptr) |
| 117 | { |
| 118 | struct ec_params_lightbar *param; |
| 119 | struct ec_response_lightbar *resp; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 120 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 121 | int ret; |
| 122 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 123 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 124 | if (!msg) |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 125 | return 0; |
| 126 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 127 | param = (struct ec_params_lightbar *)msg->data; |
| 128 | param->cmd = LIGHTBAR_CMD_VERSION; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 129 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 130 | if (ret < 0) { |
| 131 | ret = 0; |
| 132 | goto exit; |
| 133 | } |
| 134 | |
| 135 | switch (msg->result) { |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 136 | case EC_RES_INVALID_PARAM: |
| 137 | /* Pixel had no version command. */ |
| 138 | if (ver_ptr) |
| 139 | *ver_ptr = 0; |
| 140 | if (flg_ptr) |
| 141 | *flg_ptr = 0; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 142 | ret = 1; |
| 143 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 144 | |
| 145 | case EC_RES_SUCCESS: |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 146 | resp = (struct ec_response_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 147 | |
| 148 | /* Future devices w/lightbars should implement this command */ |
| 149 | if (ver_ptr) |
| 150 | *ver_ptr = resp->version.num; |
| 151 | if (flg_ptr) |
| 152 | *flg_ptr = resp->version.flags; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 153 | ret = 1; |
| 154 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */ |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 158 | ret = 0; |
| 159 | exit: |
| 160 | kfree(msg); |
| 161 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static ssize_t version_show(struct device *dev, |
| 165 | struct device_attribute *attr, char *buf) |
| 166 | { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 167 | uint32_t version = 0, flags = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 168 | struct cros_ec_dev *ec = container_of(dev, |
| 169 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 170 | int ret; |
| 171 | |
| 172 | ret = lb_throttle(); |
| 173 | if (ret) |
| 174 | return ret; |
| 175 | |
| 176 | /* This should always succeed, because we check during init. */ |
| 177 | if (!get_lightbar_version(ec, &version, &flags)) |
| 178 | return -EIO; |
| 179 | |
| 180 | return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags); |
| 181 | } |
| 182 | |
| 183 | static ssize_t brightness_store(struct device *dev, |
| 184 | struct device_attribute *attr, |
| 185 | const char *buf, size_t count) |
| 186 | { |
| 187 | struct ec_params_lightbar *param; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 188 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 189 | int ret; |
| 190 | unsigned int val; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 191 | struct cros_ec_dev *ec = container_of(dev, |
| 192 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 193 | |
| 194 | if (kstrtouint(buf, 0, &val)) |
| 195 | return -EINVAL; |
| 196 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 197 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 198 | if (!msg) |
| 199 | return -ENOMEM; |
| 200 | |
| 201 | param = (struct ec_params_lightbar *)msg->data; |
Stephen Barber | 256ab95 | 2015-06-09 13:04:43 +0200 | [diff] [blame] | 202 | param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS; |
| 203 | param->set_brightness.num = val; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 204 | ret = lb_throttle(); |
| 205 | if (ret) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 206 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 207 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 208 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 209 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 210 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 211 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 212 | if (msg->result != EC_RES_SUCCESS) { |
| 213 | ret = -EINVAL; |
| 214 | goto exit; |
| 215 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 216 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 217 | ret = count; |
| 218 | exit: |
| 219 | kfree(msg); |
| 220 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | /* |
| 225 | * We expect numbers, and we'll keep reading until we find them, skipping over |
| 226 | * any whitespace (sysfs guarantees that the input is null-terminated). Every |
| 227 | * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first |
| 228 | * parsing error, if we don't parse any numbers, or if we have numbers left |
| 229 | * over. |
| 230 | */ |
| 231 | static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr, |
| 232 | const char *buf, size_t count) |
| 233 | { |
| 234 | struct ec_params_lightbar *param; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 235 | struct cros_ec_command *msg; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 236 | struct cros_ec_dev *ec = container_of(dev, |
| 237 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 238 | unsigned int val[4]; |
| 239 | int ret, i = 0, j = 0, ok = 0; |
| 240 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 241 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 242 | if (!msg) |
| 243 | return -ENOMEM; |
| 244 | |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 245 | do { |
| 246 | /* Skip any whitespace */ |
| 247 | while (*buf && isspace(*buf)) |
| 248 | buf++; |
| 249 | |
| 250 | if (!*buf) |
| 251 | break; |
| 252 | |
| 253 | ret = sscanf(buf, "%i", &val[i++]); |
| 254 | if (ret == 0) |
Christian Engelmayer | f14ae09 | 2015-07-19 21:43:02 +0200 | [diff] [blame] | 255 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 256 | |
| 257 | if (i == 4) { |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 258 | param = (struct ec_params_lightbar *)msg->data; |
Stephen Barber | 256ab95 | 2015-06-09 13:04:43 +0200 | [diff] [blame] | 259 | param->cmd = LIGHTBAR_CMD_SET_RGB; |
| 260 | param->set_rgb.led = val[0]; |
| 261 | param->set_rgb.red = val[1]; |
| 262 | param->set_rgb.green = val[2]; |
| 263 | param->set_rgb.blue = val[3]; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 264 | /* |
| 265 | * Throttle only the first of every four transactions, |
| 266 | * so that the user can update all four LEDs at once. |
| 267 | */ |
| 268 | if ((j++ % 4) == 0) { |
| 269 | ret = lb_throttle(); |
| 270 | if (ret) |
Christian Engelmayer | f14ae09 | 2015-07-19 21:43:02 +0200 | [diff] [blame] | 271 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 274 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 275 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 276 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 277 | |
Christian Engelmayer | f14ae09 | 2015-07-19 21:43:02 +0200 | [diff] [blame] | 278 | if (msg->result != EC_RES_SUCCESS) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 279 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 280 | |
| 281 | i = 0; |
| 282 | ok = 1; |
| 283 | } |
| 284 | |
| 285 | /* Skip over the number we just read */ |
| 286 | while (*buf && !isspace(*buf)) |
| 287 | buf++; |
| 288 | |
| 289 | } while (*buf); |
| 290 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 291 | exit: |
| 292 | kfree(msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 293 | return (ok && i == 0) ? count : -EINVAL; |
| 294 | } |
| 295 | |
Olof Johansson | 377415a | 2015-03-03 13:22:33 +0100 | [diff] [blame] | 296 | static char const *seqname[] = { |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 297 | "ERROR", "S5", "S3", "S0", "S5S3", "S3S0", |
| 298 | "S0S3", "S3S5", "STOP", "RUN", "PULSE", "TEST", "KONAMI", |
| 299 | }; |
| 300 | |
| 301 | static ssize_t sequence_show(struct device *dev, |
| 302 | struct device_attribute *attr, char *buf) |
| 303 | { |
| 304 | struct ec_params_lightbar *param; |
| 305 | struct ec_response_lightbar *resp; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 306 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 307 | int ret; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 308 | struct cros_ec_dev *ec = container_of(dev, |
| 309 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 310 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 311 | msg = alloc_lightbar_cmd_msg(ec); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 312 | if (!msg) |
| 313 | return -ENOMEM; |
| 314 | |
| 315 | param = (struct ec_params_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 316 | param->cmd = LIGHTBAR_CMD_GET_SEQ; |
| 317 | ret = lb_throttle(); |
| 318 | if (ret) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 319 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 320 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 321 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 322 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 323 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 324 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 325 | if (msg->result != EC_RES_SUCCESS) { |
| 326 | ret = scnprintf(buf, PAGE_SIZE, |
| 327 | "ERROR: EC returned %d\n", msg->result); |
| 328 | goto exit; |
| 329 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 330 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 331 | resp = (struct ec_response_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 332 | if (resp->get_seq.num >= ARRAY_SIZE(seqname)) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 333 | ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 334 | else |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 335 | ret = scnprintf(buf, PAGE_SIZE, "%s\n", |
| 336 | seqname[resp->get_seq.num]); |
| 337 | |
| 338 | exit: |
| 339 | kfree(msg); |
| 340 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static ssize_t sequence_store(struct device *dev, struct device_attribute *attr, |
| 344 | const char *buf, size_t count) |
| 345 | { |
| 346 | struct ec_params_lightbar *param; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 347 | struct cros_ec_command *msg; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 348 | unsigned int num; |
| 349 | int ret, len; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 350 | struct cros_ec_dev *ec = container_of(dev, |
| 351 | struct cros_ec_dev, class_dev); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 352 | |
| 353 | for (len = 0; len < count; len++) |
| 354 | if (!isalnum(buf[len])) |
| 355 | break; |
| 356 | |
| 357 | for (num = 0; num < ARRAY_SIZE(seqname); num++) |
| 358 | if (!strncasecmp(seqname[num], buf, len)) |
| 359 | break; |
| 360 | |
| 361 | if (num >= ARRAY_SIZE(seqname)) { |
| 362 | ret = kstrtouint(buf, 0, &num); |
| 363 | if (ret) |
| 364 | return ret; |
| 365 | } |
| 366 | |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 367 | msg = alloc_lightbar_cmd_msg(ec); |
| 368 | if (!msg) |
| 369 | return -ENOMEM; |
| 370 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 371 | param = (struct ec_params_lightbar *)msg->data; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 372 | param->cmd = LIGHTBAR_CMD_SEQ; |
| 373 | param->seq.num = num; |
| 374 | ret = lb_throttle(); |
| 375 | if (ret) |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 376 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 377 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 378 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 379 | if (ret < 0) |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 380 | goto exit; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 381 | |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 382 | if (msg->result != EC_RES_SUCCESS) { |
| 383 | ret = -EINVAL; |
| 384 | goto exit; |
| 385 | } |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 386 | |
Christian Engelmayer | 88dfb8b | 2015-07-18 19:30:33 +0200 | [diff] [blame] | 387 | ret = count; |
| 388 | exit: |
| 389 | kfree(msg); |
| 390 | return ret; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* Module initialization */ |
| 394 | |
| 395 | static DEVICE_ATTR_RW(interval_msec); |
| 396 | static DEVICE_ATTR_RO(version); |
| 397 | static DEVICE_ATTR_WO(brightness); |
| 398 | static DEVICE_ATTR_WO(led_rgb); |
| 399 | static DEVICE_ATTR_RW(sequence); |
| 400 | static struct attribute *__lb_cmds_attrs[] = { |
| 401 | &dev_attr_interval_msec.attr, |
| 402 | &dev_attr_version.attr, |
| 403 | &dev_attr_brightness.attr, |
| 404 | &dev_attr_led_rgb.attr, |
| 405 | &dev_attr_sequence.attr, |
| 406 | NULL, |
| 407 | }; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 408 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 409 | static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj, |
| 410 | struct attribute *a, int n) |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 411 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 412 | struct device *dev = container_of(kobj, struct device, kobj); |
| 413 | struct cros_ec_dev *ec = container_of(dev, |
| 414 | struct cros_ec_dev, class_dev); |
| 415 | struct platform_device *pdev = container_of(ec->dev, |
| 416 | struct platform_device, dev); |
| 417 | if (pdev->id != 0) |
| 418 | return 0; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 419 | |
| 420 | /* Only instantiate this stuff if the EC has a lightbar */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 421 | if (get_lightbar_version(ec, NULL, NULL)) |
| 422 | return a->mode; |
| 423 | else |
| 424 | return 0; |
Bill Richardson | f3f837e | 2015-02-02 12:26:28 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 427 | struct attribute_group cros_ec_lightbar_attr_group = { |
| 428 | .name = "lightbar", |
| 429 | .attrs = __lb_cmds_attrs, |
| 430 | .is_visible = cros_ec_lightbar_attrs_are_visible, |
| 431 | }; |