cros_ec_lightbar.c 9.9 KB

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