cros_ec_lightbar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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 = to_cros_ec_dev(dev);
  145. int ret;
  146. ret = lb_throttle();
  147. if (ret)
  148. return ret;
  149. /* This should always succeed, because we check during init. */
  150. if (!get_lightbar_version(ec, &version, &flags))
  151. return -EIO;
  152. return scnprintf(buf, PAGE_SIZE, "%d %d\n", version, flags);
  153. }
  154. static ssize_t brightness_store(struct device *dev,
  155. struct device_attribute *attr,
  156. const char *buf, size_t count)
  157. {
  158. struct ec_params_lightbar *param;
  159. struct cros_ec_command *msg;
  160. int ret;
  161. unsigned int val;
  162. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  163. if (kstrtouint(buf, 0, &val))
  164. return -EINVAL;
  165. msg = alloc_lightbar_cmd_msg(ec);
  166. if (!msg)
  167. return -ENOMEM;
  168. param = (struct ec_params_lightbar *)msg->data;
  169. param->cmd = LIGHTBAR_CMD_SET_BRIGHTNESS;
  170. param->set_brightness.num = val;
  171. ret = lb_throttle();
  172. if (ret)
  173. goto exit;
  174. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  175. if (ret < 0)
  176. goto exit;
  177. if (msg->result != EC_RES_SUCCESS) {
  178. ret = -EINVAL;
  179. goto exit;
  180. }
  181. ret = count;
  182. exit:
  183. kfree(msg);
  184. return ret;
  185. }
  186. /*
  187. * We expect numbers, and we'll keep reading until we find them, skipping over
  188. * any whitespace (sysfs guarantees that the input is null-terminated). Every
  189. * four numbers are sent to the lightbar as <LED,R,G,B>. We fail at the first
  190. * parsing error, if we don't parse any numbers, or if we have numbers left
  191. * over.
  192. */
  193. static ssize_t led_rgb_store(struct device *dev, struct device_attribute *attr,
  194. const char *buf, size_t count)
  195. {
  196. struct ec_params_lightbar *param;
  197. struct cros_ec_command *msg;
  198. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  199. unsigned int val[4];
  200. int ret, i = 0, j = 0, ok = 0;
  201. msg = alloc_lightbar_cmd_msg(ec);
  202. if (!msg)
  203. return -ENOMEM;
  204. do {
  205. /* Skip any whitespace */
  206. while (*buf && isspace(*buf))
  207. buf++;
  208. if (!*buf)
  209. break;
  210. ret = sscanf(buf, "%i", &val[i++]);
  211. if (ret == 0)
  212. goto exit;
  213. if (i == 4) {
  214. param = (struct ec_params_lightbar *)msg->data;
  215. param->cmd = LIGHTBAR_CMD_SET_RGB;
  216. param->set_rgb.led = val[0];
  217. param->set_rgb.red = val[1];
  218. param->set_rgb.green = val[2];
  219. param->set_rgb.blue = val[3];
  220. /*
  221. * Throttle only the first of every four transactions,
  222. * so that the user can update all four LEDs at once.
  223. */
  224. if ((j++ % 4) == 0) {
  225. ret = lb_throttle();
  226. if (ret)
  227. goto exit;
  228. }
  229. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  230. if (ret < 0)
  231. goto exit;
  232. if (msg->result != EC_RES_SUCCESS)
  233. goto exit;
  234. i = 0;
  235. ok = 1;
  236. }
  237. /* Skip over the number we just read */
  238. while (*buf && !isspace(*buf))
  239. buf++;
  240. } while (*buf);
  241. exit:
  242. kfree(msg);
  243. return (ok && i == 0) ? count : -EINVAL;
  244. }
  245. static char const *seqname[] = {
  246. "ERROR", "S5", "S3", "S0", "S5S3", "S3S0",
  247. "S0S3", "S3S5", "STOP", "RUN", "KONAMI",
  248. "TAP", "PROGRAM",
  249. };
  250. static ssize_t sequence_show(struct device *dev,
  251. struct device_attribute *attr, char *buf)
  252. {
  253. struct ec_params_lightbar *param;
  254. struct ec_response_lightbar *resp;
  255. struct cros_ec_command *msg;
  256. int ret;
  257. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  258. msg = alloc_lightbar_cmd_msg(ec);
  259. if (!msg)
  260. return -ENOMEM;
  261. param = (struct ec_params_lightbar *)msg->data;
  262. param->cmd = LIGHTBAR_CMD_GET_SEQ;
  263. ret = lb_throttle();
  264. if (ret)
  265. goto exit;
  266. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  267. if (ret < 0)
  268. goto exit;
  269. if (msg->result != EC_RES_SUCCESS) {
  270. ret = scnprintf(buf, PAGE_SIZE,
  271. "ERROR: EC returned %d\n", msg->result);
  272. goto exit;
  273. }
  274. resp = (struct ec_response_lightbar *)msg->data;
  275. if (resp->get_seq.num >= ARRAY_SIZE(seqname))
  276. ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->get_seq.num);
  277. else
  278. ret = scnprintf(buf, PAGE_SIZE, "%s\n",
  279. seqname[resp->get_seq.num]);
  280. exit:
  281. kfree(msg);
  282. return ret;
  283. }
  284. static int lb_send_empty_cmd(struct cros_ec_dev *ec, uint8_t cmd)
  285. {
  286. struct ec_params_lightbar *param;
  287. struct cros_ec_command *msg;
  288. int ret;
  289. msg = alloc_lightbar_cmd_msg(ec);
  290. if (!msg)
  291. return -ENOMEM;
  292. param = (struct ec_params_lightbar *)msg->data;
  293. param->cmd = cmd;
  294. ret = lb_throttle();
  295. if (ret)
  296. goto error;
  297. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  298. if (ret < 0)
  299. goto error;
  300. if (msg->result != EC_RES_SUCCESS) {
  301. ret = -EINVAL;
  302. goto error;
  303. }
  304. ret = 0;
  305. error:
  306. kfree(msg);
  307. return ret;
  308. }
  309. int lb_manual_suspend_ctrl(struct cros_ec_dev *ec, uint8_t enable)
  310. {
  311. struct ec_params_lightbar *param;
  312. struct cros_ec_command *msg;
  313. int ret;
  314. if (ec != ec_with_lightbar)
  315. return 0;
  316. msg = alloc_lightbar_cmd_msg(ec);
  317. if (!msg)
  318. return -ENOMEM;
  319. param = (struct ec_params_lightbar *)msg->data;
  320. param->cmd = LIGHTBAR_CMD_MANUAL_SUSPEND_CTRL;
  321. param->manual_suspend_ctrl.enable = enable;
  322. ret = lb_throttle();
  323. if (ret)
  324. goto error;
  325. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  326. if (ret < 0)
  327. goto error;
  328. if (msg->result != EC_RES_SUCCESS) {
  329. ret = -EINVAL;
  330. goto error;
  331. }
  332. ret = 0;
  333. error:
  334. kfree(msg);
  335. return ret;
  336. }
  337. EXPORT_SYMBOL(lb_manual_suspend_ctrl);
  338. int lb_suspend(struct cros_ec_dev *ec)
  339. {
  340. if (userspace_control || ec != ec_with_lightbar)
  341. return 0;
  342. return lb_send_empty_cmd(ec, LIGHTBAR_CMD_SUSPEND);
  343. }
  344. EXPORT_SYMBOL(lb_suspend);
  345. int lb_resume(struct cros_ec_dev *ec)
  346. {
  347. if (userspace_control || ec != ec_with_lightbar)
  348. return 0;
  349. return lb_send_empty_cmd(ec, LIGHTBAR_CMD_RESUME);
  350. }
  351. EXPORT_SYMBOL(lb_resume);
  352. static ssize_t sequence_store(struct device *dev, struct device_attribute *attr,
  353. const char *buf, size_t count)
  354. {
  355. struct ec_params_lightbar *param;
  356. struct cros_ec_command *msg;
  357. unsigned int num;
  358. int ret, len;
  359. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  360. for (len = 0; len < count; len++)
  361. if (!isalnum(buf[len]))
  362. break;
  363. for (num = 0; num < ARRAY_SIZE(seqname); num++)
  364. if (!strncasecmp(seqname[num], buf, len))
  365. break;
  366. if (num >= ARRAY_SIZE(seqname)) {
  367. ret = kstrtouint(buf, 0, &num);
  368. if (ret)
  369. return ret;
  370. }
  371. msg = alloc_lightbar_cmd_msg(ec);
  372. if (!msg)
  373. return -ENOMEM;
  374. param = (struct ec_params_lightbar *)msg->data;
  375. param->cmd = LIGHTBAR_CMD_SEQ;
  376. param->seq.num = num;
  377. ret = lb_throttle();
  378. if (ret)
  379. goto exit;
  380. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  381. if (ret < 0)
  382. goto exit;
  383. if (msg->result != EC_RES_SUCCESS) {
  384. ret = -EINVAL;
  385. goto exit;
  386. }
  387. ret = count;
  388. exit:
  389. kfree(msg);
  390. return ret;
  391. }
  392. static ssize_t program_store(struct device *dev, struct device_attribute *attr,
  393. const char *buf, size_t count)
  394. {
  395. int extra_bytes, max_size, ret;
  396. struct ec_params_lightbar *param;
  397. struct cros_ec_command *msg;
  398. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  399. /*
  400. * We might need to reject the program for size reasons. The EC
  401. * enforces a maximum program size, but we also don't want to try
  402. * and send a program that is too big for the protocol. In order
  403. * to ensure the latter, we also need to ensure we have extra bytes
  404. * to represent the rest of the packet.
  405. */
  406. extra_bytes = sizeof(*param) - sizeof(param->set_program.data);
  407. max_size = min(EC_LB_PROG_LEN, ec->ec_dev->max_request - extra_bytes);
  408. if (count > max_size) {
  409. dev_err(dev, "Program is %u bytes, too long to send (max: %u)",
  410. (unsigned int)count, max_size);
  411. return -EINVAL;
  412. }
  413. msg = alloc_lightbar_cmd_msg(ec);
  414. if (!msg)
  415. return -ENOMEM;
  416. ret = lb_throttle();
  417. if (ret)
  418. goto exit;
  419. dev_info(dev, "Copying %zu byte program to EC", count);
  420. param = (struct ec_params_lightbar *)msg->data;
  421. param->cmd = LIGHTBAR_CMD_SET_PROGRAM;
  422. param->set_program.size = count;
  423. memcpy(param->set_program.data, buf, count);
  424. /*
  425. * We need to set the message size manually or else it will use
  426. * EC_LB_PROG_LEN. This might be too long, and the program
  427. * is unlikely to use all of the space.
  428. */
  429. msg->outsize = count + extra_bytes;
  430. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  431. if (ret < 0)
  432. goto exit;
  433. if (msg->result != EC_RES_SUCCESS) {
  434. ret = -EINVAL;
  435. goto exit;
  436. }
  437. ret = count;
  438. exit:
  439. kfree(msg);
  440. return ret;
  441. }
  442. static ssize_t userspace_control_show(struct device *dev,
  443. struct device_attribute *attr,
  444. char *buf)
  445. {
  446. return scnprintf(buf, PAGE_SIZE, "%d\n", userspace_control);
  447. }
  448. static ssize_t userspace_control_store(struct device *dev,
  449. struct device_attribute *attr,
  450. const char *buf,
  451. size_t count)
  452. {
  453. bool enable;
  454. int ret;
  455. ret = strtobool(buf, &enable);
  456. if (ret < 0)
  457. return ret;
  458. userspace_control = enable;
  459. return count;
  460. }
  461. /* Module initialization */
  462. static DEVICE_ATTR_RW(interval_msec);
  463. static DEVICE_ATTR_RO(version);
  464. static DEVICE_ATTR_WO(brightness);
  465. static DEVICE_ATTR_WO(led_rgb);
  466. static DEVICE_ATTR_RW(sequence);
  467. static DEVICE_ATTR_WO(program);
  468. static DEVICE_ATTR_RW(userspace_control);
  469. static struct attribute *__lb_cmds_attrs[] = {
  470. &dev_attr_interval_msec.attr,
  471. &dev_attr_version.attr,
  472. &dev_attr_brightness.attr,
  473. &dev_attr_led_rgb.attr,
  474. &dev_attr_sequence.attr,
  475. &dev_attr_program.attr,
  476. &dev_attr_userspace_control.attr,
  477. NULL,
  478. };
  479. bool ec_has_lightbar(struct cros_ec_dev *ec)
  480. {
  481. return !!get_lightbar_version(ec, NULL, NULL);
  482. }
  483. static umode_t cros_ec_lightbar_attrs_are_visible(struct kobject *kobj,
  484. struct attribute *a, int n)
  485. {
  486. struct device *dev = container_of(kobj, struct device, kobj);
  487. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  488. struct platform_device *pdev = to_platform_device(ec->dev);
  489. struct cros_ec_platform *pdata = pdev->dev.platform_data;
  490. int is_cros_ec;
  491. is_cros_ec = strcmp(pdata->ec_name, CROS_EC_DEV_NAME);
  492. if (is_cros_ec != 0)
  493. return 0;
  494. /* Only instantiate this stuff if the EC has a lightbar */
  495. if (ec_has_lightbar(ec)) {
  496. ec_with_lightbar = ec;
  497. return a->mode;
  498. }
  499. return 0;
  500. }
  501. struct attribute_group cros_ec_lightbar_attr_group = {
  502. .name = "lightbar",
  503. .attrs = __lb_cmds_attrs,
  504. .is_visible = cros_ec_lightbar_attrs_are_visible,
  505. };
  506. EXPORT_SYMBOL(cros_ec_lightbar_attr_group);