cros_ec_lightbar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. /* Rate-limit the lightbar interface to prevent DoS. */
  34. static unsigned long lb_interval_jiffies = 50 * HZ / 1000;
  35. /*
  36. * Whether or not we have given userspace control of the lightbar.
  37. * If this is true, we won't do anything during suspend/resume.
  38. */
  39. static bool userspace_control;
  40. static struct cros_ec_dev *ec_with_lightbar;
  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. return scnprintf(buf, PAGE_SIZE, "%lu\n", msec);
  46. }
  47. static ssize_t interval_msec_store(struct device *dev,
  48. struct device_attribute *attr,
  49. const char *buf, size_t count)
  50. {
  51. unsigned long msec;
  52. if (kstrtoul(buf, 0, &msec))
  53. return -EINVAL;
  54. lb_interval_jiffies = msec * HZ / 1000;
  55. return count;
  56. }
  57. static DEFINE_MUTEX(lb_mutex);
  58. /* Return 0 if able to throttle correctly, error otherwise */
  59. static int lb_throttle(void)
  60. {
  61. static unsigned long last_access;
  62. unsigned long now, next_timeslot;
  63. long delay;
  64. int ret = 0;
  65. mutex_lock(&lb_mutex);
  66. now = jiffies;
  67. next_timeslot = last_access + lb_interval_jiffies;
  68. if (time_before(now, next_timeslot)) {
  69. delay = (long)(next_timeslot) - (long)now;
  70. set_current_state(TASK_INTERRUPTIBLE);
  71. if (schedule_timeout(delay) > 0) {
  72. /* interrupted - just abort */
  73. ret = -EINTR;
  74. goto out;
  75. }
  76. now = jiffies;
  77. }
  78. last_access = now;
  79. out:
  80. mutex_unlock(&lb_mutex);
  81. return ret;
  82. }
  83. static struct cros_ec_command *alloc_lightbar_cmd_msg(struct cros_ec_dev *ec)
  84. {
  85. struct cros_ec_command *msg;
  86. int len;
  87. len = max(sizeof(struct ec_params_lightbar),
  88. sizeof(struct ec_response_lightbar));
  89. msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
  90. if (!msg)
  91. return NULL;
  92. msg->version = 0;
  93. msg->command = EC_CMD_LIGHTBAR_CMD + ec->cmd_offset;
  94. msg->outsize = sizeof(struct ec_params_lightbar);
  95. msg->insize = sizeof(struct ec_response_lightbar);
  96. return msg;
  97. }
  98. static int get_lightbar_version(struct cros_ec_dev *ec,
  99. uint32_t *ver_ptr, uint32_t *flg_ptr)
  100. {
  101. struct ec_params_lightbar *param;
  102. struct ec_response_lightbar *resp;
  103. struct cros_ec_command *msg;
  104. int ret;
  105. msg = alloc_lightbar_cmd_msg(ec);
  106. if (!msg)
  107. return 0;
  108. param = (struct ec_params_lightbar *)msg->data;
  109. param->cmd = LIGHTBAR_CMD_VERSION;
  110. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  111. if (ret < 0) {
  112. ret = 0;
  113. goto exit;
  114. }
  115. switch (msg->result) {
  116. case EC_RES_INVALID_PARAM:
  117. /* Pixel had no version command. */
  118. if (ver_ptr)
  119. *ver_ptr = 0;
  120. if (flg_ptr)
  121. *flg_ptr = 0;
  122. ret = 1;
  123. goto exit;
  124. case EC_RES_SUCCESS:
  125. resp = (struct ec_response_lightbar *)msg->data;
  126. /* Future devices w/lightbars should implement this command */
  127. if (ver_ptr)
  128. *ver_ptr = resp->version.num;
  129. if (flg_ptr)
  130. *flg_ptr = resp->version.flags;
  131. ret = 1;
  132. goto exit;
  133. }
  134. /* Anything else (ie, EC_RES_INVALID_COMMAND) - no lightbar */
  135. ret = 0;
  136. exit:
  137. kfree(msg);
  138. return ret;
  139. }
  140. static ssize_t version_show(struct device *dev,
  141. struct device_attribute *attr, char *buf)
  142. {
  143. uint32_t version = 0, flags = 0;
  144. struct cros_ec_dev *ec = container_of(dev,
  145. struct cros_ec_dev, class_dev);
  146. int ret;
  147. ret = lb_throttle();
  148. if (ret)
  149. return ret;
  150. /* This should always succeed, because we check during init. */
  151. if (!get_lightbar_version(ec, &version, &flags))
  152. return -EIO;
  153. return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
  154. }
  155. static ssize_t brightness_store(struct device *dev,
  156. struct device_attribute *attr,
  157. const char *buf, size_t count)
  158. {
  159. struct ec_params_lightbar *param;
  160. struct cros_ec_command *msg;
  161. int ret;
  162. unsigned int val;
  163. struct cros_ec_dev *ec = container_of(dev,
  164. struct cros_ec_dev, class_dev);
  165. if (kstrtouint(buf, 0, &val))
  166. return -EINVAL;
  167. msg = alloc_lightbar_cmd_msg(ec);
  168. if (!msg)
  169. return -ENOMEM;
  170. param = (struct ec_params_lightbar *)msg->data;
  171. param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
  172. param->set_brightness.num = val;
  173. ret = lb_throttle();
  174. if (ret)
  175. goto exit;
  176. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  177. if (ret < 0)
  178. goto exit;
  179. if (msg->result != EC_RES_SUCCESS) {
  180. ret = -EINVAL;
  181. goto exit;
  182. }
  183. ret = count;
  184. exit:
  185. kfree(msg);
  186. return ret;
  187. }
  188. /*
  189. * We expect numbers, and we'll keep reading until we find them, skipping over
  190. * any whitespace (sysfs guarantees that the input is null-terminated). Every
  191. * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
  192. * parsing error, if we don't parse any numbers, or if we have numbers left
  193. * over.
  194. */
  195. static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
  196. const char *buf, size_t count)
  197. {
  198. struct ec_params_lightbar *param;
  199. struct cros_ec_command *msg;
  200. struct cros_ec_dev *ec = container_of(dev,
  201. struct cros_ec_dev, class_dev);
  202. unsigned int val[4];
  203. int ret, i = 0, j = 0, ok = 0;
  204. msg = alloc_lightbar_cmd_msg(ec);
  205. if (!msg)
  206. return -ENOMEM;
  207. do {
  208. /* Skip any whitespace */
  209. while (*buf && isspace(*buf))
  210. buf++;
  211. if (!*buf)
  212. break;
  213. ret = sscanf(buf, "%i", &val[i++]);
  214. if (ret == 0)
  215. goto exit;
  216. if (i == 4) {
  217. param = (struct ec_params_lightbar *)msg->data;
  218. param->cmd = LIGHTBAR_CMD_SET_RGB;
  219. param->set_rgb.led = val[0];
  220. param->set_rgb.red = val[1];
  221. param->set_rgb.green = val[2];
  222. param->set_rgb.blue = val[3];
  223. /*
  224. * Throttle only the first of every four transactions,
  225. * so that the user can update all four LEDs at once.
  226. */
  227. if ((j++ % 4) == 0) {
  228. ret = lb_throttle();
  229. if (ret)
  230. goto exit;
  231. }
  232. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  233. if (ret < 0)
  234. goto exit;
  235. if (msg->result != EC_RES_SUCCESS)
  236. goto exit;
  237. i = 0;
  238. ok = 1;
  239. }
  240. /* Skip over the number we just read */
  241. while (*buf && !isspace(*buf))
  242. buf++;
  243. } while (*buf);
  244. exit:
  245. kfree(msg);
  246. return (ok && i == 0) ? count : -EINVAL;
  247. }
  248. static char const *seqname[] = {
  249. "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
  250. "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
  251. "TAP", "PROGRAM",
  252. };
  253. static ssize_t sequence_show(struct device *dev,
  254. struct device_attribute *attr, char *buf)
  255. {
  256. struct ec_params_lightbar *param;
  257. struct ec_response_lightbar *resp;
  258. struct cros_ec_command *msg;
  259. int ret;
  260. struct cros_ec_dev *ec = container_of(dev,
  261. struct cros_ec_dev, class_dev);
  262. msg = alloc_lightbar_cmd_msg(ec);
  263. if (!msg)
  264. return -ENOMEM;
  265. param = (struct ec_params_lightbar *)msg->data;
  266. param->cmd = LIGHTBAR_CMD_GET_SEQ;
  267. ret = lb_throttle();
  268. if (ret)
  269. goto exit;
  270. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  271. if (ret < 0)
  272. goto exit;
  273. if (msg->result != EC_RES_SUCCESS) {
  274. ret = scnprintf(buf, PAGE_SIZE,
  275. "ERROR: EC returned %d\n", msg->result);
  276. goto exit;
  277. }
  278. resp = (struct ec_response_lightbar *)msg->data;
  279. if (resp->get_seq.num >= ARRAY_SIZE(seqname))
  280. ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
  281. else
  282. ret = scnprintf(buf, PAGE_SIZE, "%s\n",
  283. seqname[resp->get_seq.num]);
  284. exit:
  285. kfree(msg);
  286. return ret;
  287. }
  288. static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
  289. {
  290. struct ec_params_lightbar *param;
  291. struct cros_ec_command *msg;
  292. int ret;
  293. msg = alloc_lightbar_cmd_msg(ec);
  294. if (!msg)
  295. return -ENOMEM;
  296. param = (struct ec_params_lightbar *)msg->data;
  297. param->cmd = cmd;
  298. ret = lb_throttle();
  299. if (ret)
  300. goto error;
  301. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  302. if (ret < 0)
  303. goto error;
  304. if (msg->result != EC_RES_SUCCESS) {
  305. ret = -EINVAL;
  306. goto error;
  307. }
  308. ret = 0;
  309. error:
  310. kfree(msg);
  311. return ret;
  312. }
  313. int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
  314. {
  315. struct ec_params_lightbar *param;
  316. struct cros_ec_command *msg;
  317. int ret;
  318. if (ec != ec_with_lightbar)
  319. return 0;
  320. msg = alloc_lightbar_cmd_msg(ec);
  321. if (!msg)
  322. return -ENOMEM;
  323. param = (struct ec_params_lightbar *)msg->data;
  324. param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
  325. param->manual_suspend_ctrl.enable = enable;
  326. ret = lb_throttle();
  327. if (ret)
  328. goto error;
  329. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  330. if (ret < 0)
  331. goto error;
  332. if (msg->result != EC_RES_SUCCESS) {
  333. ret = -EINVAL;
  334. goto error;
  335. }
  336. ret = 0;
  337. error:
  338. kfree(msg);
  339. return ret;
  340. }
  341. EXPORT_SYMBOL(lb_manual_suspend_ctrl);
  342. int lb_suspend(struct cros_ec_dev *ec)
  343. {
  344. if (userspace_control || ec != ec_with_lightbar)
  345. return 0;
  346. return lb_send_empty_cmd(ec, LIGHTBAR_CMD_SUSPEND);
  347. }
  348. EXPORT_SYMBOL(lb_suspend);
  349. int lb_resume(struct cros_ec_dev *ec)
  350. {
  351. if (userspace_control || ec != ec_with_lightbar)
  352. return 0;
  353. return lb_send_empty_cmd(ec, LIGHTBAR_CMD_RESUME);
  354. }
  355. EXPORT_SYMBOL(lb_resume);
  356. static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
  357. const char *buf, size_t count)
  358. {
  359. struct ec_params_lightbar *param;
  360. struct cros_ec_command *msg;
  361. unsigned int num;
  362. int ret, len;
  363. struct cros_ec_dev *ec = container_of(dev,
  364. struct cros_ec_dev, class_dev);
  365. for (len = 0; len < count; len++)
  366. if (!isalnum(buf[len]))
  367. break;
  368. for (num = 0; num < ARRAY_SIZE(seqname); num++)
  369. if (!strncasecmp(seqname[num], buf, len))
  370. break;
  371. if (num >= ARRAY_SIZE(seqname)) {
  372. ret = kstrtouint(buf, 0, &num);
  373. if (ret)
  374. return ret;
  375. }
  376. msg = alloc_lightbar_cmd_msg(ec);
  377. if (!msg)
  378. return -ENOMEM;
  379. param = (struct ec_params_lightbar *)msg->data;
  380. param->cmd = LIGHTBAR_CMD_SEQ;
  381. param->seq.num = num;
  382. ret = lb_throttle();
  383. if (ret)
  384. goto exit;
  385. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  386. if (ret < 0)
  387. goto exit;
  388. if (msg->result != EC_RES_SUCCESS) {
  389. ret = -EINVAL;
  390. goto exit;
  391. }
  392. ret = count;
  393. exit:
  394. kfree(msg);
  395. return ret;
  396. }
  397. static ssize_t program_store(struct device *dev, struct device_attribute *attr,
  398. const char *buf, size_t count)
  399. {
  400. int extra_bytes, max_size, ret;
  401. struct ec_params_lightbar *param;
  402. struct cros_ec_command *msg;
  403. struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
  404. class_dev);
  405. /*
  406. * We might need to reject the program for size reasons. The EC
  407. * enforces a maximum program size, but we also don't want to try
  408. * and send a program that is too big for the protocol. In order
  409. * to ensure the latter, we also need to ensure we have extra bytes
  410. * to represent the rest of the packet.
  411. */
  412. extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
  413. max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
  414. if (count > max_size) {
  415. dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
  416. (unsigned int)count, max_size);
  417. return -EINVAL;
  418. }
  419. msg = alloc_lightbar_cmd_msg(ec);
  420. if (!msg)
  421. return -ENOMEM;
  422. ret = lb_throttle();
  423. if (ret)
  424. goto exit;
  425. dev_info(dev, "Copying %zu byte program to EC", count);
  426. param = (struct ec_params_lightbar *)msg->data;
  427. param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
  428. param->set_program.size = count;
  429. memcpy(param->set_program.data, buf, count);
  430. /*
  431. * We need to set the message size manually or else it will use
  432. * EC_LB_PROG_LEN. This might be too long, and the program
  433. * is unlikely to use all of the space.
  434. */
  435. msg->outsize = count + extra_bytes;
  436. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  437. if (ret < 0)
  438. goto exit;
  439. if (msg->result != EC_RES_SUCCESS) {
  440. ret = -EINVAL;
  441. goto exit;
  442. }
  443. ret = count;
  444. exit:
  445. kfree(msg);
  446. return ret;
  447. }
  448. static ssize_t userspace_control_show(struct device *dev,
  449. struct device_attribute *attr,
  450. char *buf)
  451. {
  452. return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
  453. }
  454. static ssize_t userspace_control_store(struct device *dev,
  455. struct device_attribute *attr,
  456. const char *buf,
  457. size_t count)
  458. {
  459. bool enable;
  460. int ret;
  461. ret = strtobool(buf, &enable);
  462. if (ret < 0)
  463. return ret;
  464. userspace_control = enable;
  465. return count;
  466. }
  467. /* Module initialization */
  468. static DEVICE_ATTR_RW(interval_msec);
  469. static DEVICE_ATTR_RO(version);
  470. static DEVICE_ATTR_WO(brightness);
  471. static DEVICE_ATTR_WO(led_rgb);
  472. static DEVICE_ATTR_RW(sequence);
  473. static DEVICE_ATTR_WO(program);
  474. static DEVICE_ATTR_RW(userspace_control);
  475. static struct attribute *__lb_cmds_attrs[] = {
  476. &dev_attr_interval_msec.attr,
  477. &dev_attr_version.attr,
  478. &dev_attr_brightness.attr,
  479. &dev_attr_led_rgb.attr,
  480. &dev_attr_sequence.attr,
  481. &dev_attr_program.attr,
  482. &dev_attr_userspace_control.attr,
  483. NULL,
  484. };
  485. bool ec_has_lightbar(struct cros_ec_dev *ec)
  486. {
  487. return !!get_lightbar_version(ec, NULL, NULL);
  488. }
  489. static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj,
  490. struct attribute *a, int n)
  491. {
  492. struct device *dev = container_of(kobj, struct device, kobj);
  493. struct cros_ec_dev *ec = container_of(dev,
  494. struct cros_ec_dev, class_dev);
  495. struct platform_device *pdev = to_platform_device(ec->dev);
  496. struct cros_ec_platform *pdata = pdev->dev.platform_data;
  497. int is_cros_ec;
  498. is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME);
  499. if (is_cros_ec != 0)
  500. return 0;
  501. /* Only instantiate this stuff if the EC has a lightbar */
  502. if (ec_has_lightbar(ec)) {
  503. ec_with_lightbar = ec;
  504. return a->mode;
  505. }
  506. return 0;
  507. }
  508. struct attribute_group cros_ec_lightbar_attr_group = {
  509. .name = "lightbar",
  510. .attrs = __lb_cmds_attrs,
  511. .is_visible = cros_ec_lightbar_attrs_are_visible,
  512. };
  513. EXPORT_SYMBOL(cros_ec_lightbar_attr_group);