event.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ACPI event handling for Wilco Embedded Controller
  4. *
  5. * Copyright 2019 Google LLC
  6. *
  7. * The Wilco Embedded Controller can create custom events that
  8. * are not handled as standard ACPI objects. These events can
  9. * contain information about changes in EC controlled features,
  10. * such as errors and events in the dock or display. For example,
  11. * an event is triggered if the dock is plugged into a display
  12. * incorrectly. These events are needed for telemetry and
  13. * diagnostics reasons, and for possibly alerting the user.
  14. * These events are triggered by the EC with an ACPI Notify(0x90),
  15. * and then the BIOS reads the event buffer from EC RAM via an
  16. * ACPI method. When the OS receives these events via ACPI,
  17. * it passes them along to this driver. The events are put into
  18. * a queue which can be read by a userspace daemon via a char device
  19. * that implements read() and poll(). The event queue acts as a
  20. * circular buffer of size 64, so if there are no userspace consumers
  21. * the kernel will not run out of memory. The char device will appear at
  22. * /dev/wilco_event{n}, where n is some small non-negative integer,
  23. * starting from 0. Standard ACPI events such as the battery getting
  24. * plugged/unplugged can also come through this path, but they are
  25. * dealt with via other paths, and are ignored here.
  26. * To test, you can tail the binary data with
  27. * $ cat /dev/wilco_event0 | hexdump -ve '1/1 "%x\n"'
  28. * and then create an event by plugging/unplugging the battery.
  29. */
  30. #include <linux/acpi.h>
  31. #include <linux/cdev.h>
  32. #include <linux/device.h>
  33. #include <linux/fs.h>
  34. #include <linux/idr.h>
  35. #include <linux/io.h>
  36. #include <linux/list.h>
  37. #include <linux/module.h>
  38. #include <linux/poll.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/wait.h>
  42. /* ACPI Notify event code indicating event data is available. */
  43. #define EC_ACPI_NOTIFY_EVENT 0x90
  44. /* ACPI Method to execute to retrieve event data buffer from the EC. */
  45. #define EC_ACPI_GET_EVENT "QSET"
  46. /* Maximum number of words in event data returned by the EC. */
  47. #define EC_ACPI_MAX_EVENT_WORDS 6
  48. #define EC_ACPI_MAX_EVENT_SIZE \
  49. (sizeof(struct ec_event) + (EC_ACPI_MAX_EVENT_WORDS) * sizeof(u16))
  50. /* Node will appear in /dev/EVENT_DEV_NAME */
  51. #define EVENT_DEV_NAME "wilco_event"
  52. #define EVENT_CLASS_NAME EVENT_DEV_NAME
  53. #define DRV_NAME EVENT_DEV_NAME
  54. #define EVENT_DEV_NAME_FMT (EVENT_DEV_NAME "%d")
  55. static struct class event_class = {
  56. .name = EVENT_CLASS_NAME,
  57. };
  58. /* Keep track of all the device numbers used. */
  59. #define EVENT_MAX_DEV 128
  60. static int event_major;
  61. static DEFINE_IDA(event_ida);
  62. /* Size of circular queue of events. */
  63. #define MAX_NUM_EVENTS 64
  64. /**
  65. * struct ec_event - Extended event returned by the EC.
  66. * @size: Number of 16bit words in structure after the size word.
  67. * @type: Extended event type, meaningless for us.
  68. * @event: Event data words. Max count is %EC_ACPI_MAX_EVENT_WORDS.
  69. */
  70. struct ec_event {
  71. u16 size;
  72. u16 type;
  73. u16 event[];
  74. } __packed;
  75. #define ec_event_num_words(ev) (ev->size - 1)
  76. #define ec_event_size(ev) (sizeof(*ev) + (ec_event_num_words(ev) * sizeof(u16)))
  77. /**
  78. * struct ec_event_queue - Circular queue for events.
  79. * @capacity: Number of elements the queue can hold.
  80. * @head: Next index to write to.
  81. * @tail: Next index to read from.
  82. * @entries: Array of events.
  83. */
  84. struct ec_event_queue {
  85. int capacity;
  86. int head;
  87. int tail;
  88. struct ec_event *entries[] __counted_by(capacity);
  89. };
  90. /* Maximum number of events to store in ec_event_queue */
  91. static int queue_size = 64;
  92. module_param(queue_size, int, 0644);
  93. static struct ec_event_queue *event_queue_new(int capacity)
  94. {
  95. struct ec_event_queue *q;
  96. q = kzalloc(struct_size(q, entries, capacity), GFP_KERNEL);
  97. if (!q)
  98. return NULL;
  99. q->capacity = capacity;
  100. return q;
  101. }
  102. static inline bool event_queue_empty(struct ec_event_queue *q)
  103. {
  104. /* head==tail when both full and empty, but head==NULL when empty */
  105. return q->head == q->tail && !q->entries[q->head];
  106. }
  107. static inline bool event_queue_full(struct ec_event_queue *q)
  108. {
  109. /* head==tail when both full and empty, but head!=NULL when full */
  110. return q->head == q->tail && q->entries[q->head];
  111. }
  112. static struct ec_event *event_queue_pop(struct ec_event_queue *q)
  113. {
  114. struct ec_event *ev;
  115. if (event_queue_empty(q))
  116. return NULL;
  117. ev = q->entries[q->tail];
  118. q->entries[q->tail] = NULL;
  119. q->tail = (q->tail + 1) % q->capacity;
  120. return ev;
  121. }
  122. /*
  123. * If full, overwrite the oldest event and return it so the caller
  124. * can kfree it. If not full, return NULL.
  125. */
  126. static struct ec_event *event_queue_push(struct ec_event_queue *q,
  127. struct ec_event *ev)
  128. {
  129. struct ec_event *popped = NULL;
  130. if (event_queue_full(q))
  131. popped = event_queue_pop(q);
  132. q->entries[q->head] = ev;
  133. q->head = (q->head + 1) % q->capacity;
  134. return popped;
  135. }
  136. static void event_queue_free(struct ec_event_queue *q)
  137. {
  138. struct ec_event *event;
  139. while ((event = event_queue_pop(q)) != NULL)
  140. kfree(event);
  141. kfree(q);
  142. }
  143. /**
  144. * struct event_device_data - Data for a Wilco EC device that responds to ACPI.
  145. * @events: Circular queue of EC events to be provided to userspace.
  146. * @queue_lock: Protect the queue from simultaneous read/writes.
  147. * @wq: Wait queue to notify processes when events are available or the
  148. * device has been removed.
  149. * @cdev: Char dev that userspace reads() and polls() from.
  150. * @dev: Device associated with the %cdev.
  151. * @exist: Has the device been not been removed? Once a device has been removed,
  152. * writes, reads, and new opens will fail.
  153. * @available: Guarantee only one client can open() file and read from queue.
  154. *
  155. * There will be one of these structs for each ACPI device registered. This data
  156. * is the queue of events received from ACPI that still need to be read from
  157. * userspace, the device and char device that userspace is using, a wait queue
  158. * used to notify different threads when something has changed, plus a flag
  159. * on whether the ACPI device has been removed.
  160. */
  161. struct event_device_data {
  162. struct ec_event_queue *events;
  163. spinlock_t queue_lock;
  164. wait_queue_head_t wq;
  165. struct device dev;
  166. struct cdev cdev;
  167. bool exist;
  168. atomic_t available;
  169. };
  170. /**
  171. * enqueue_events() - Place EC events in queue to be read by userspace.
  172. * @adev: Device the events came from.
  173. * @buf: Buffer of event data.
  174. * @length: Length of event data buffer.
  175. *
  176. * %buf contains a number of ec_event's, packed one after the other.
  177. * Each ec_event is of variable length. Start with the first event, copy it
  178. * into a persistent ec_event, store that entry in the queue, move on
  179. * to the next ec_event in buf, and repeat.
  180. *
  181. * Return: 0 on success or negative error code on failure.
  182. */
  183. static int enqueue_events(struct acpi_device *adev, const u8 *buf, u32 length)
  184. {
  185. struct event_device_data *dev_data = adev->driver_data;
  186. struct ec_event *event, *queue_event, *old_event;
  187. size_t num_words, event_size;
  188. u32 offset = 0;
  189. while (offset < length) {
  190. event = (struct ec_event *)(buf + offset);
  191. num_words = ec_event_num_words(event);
  192. event_size = ec_event_size(event);
  193. if (num_words > EC_ACPI_MAX_EVENT_WORDS) {
  194. dev_err(&adev->dev, "Too many event words: %zu > %d\n",
  195. num_words, EC_ACPI_MAX_EVENT_WORDS);
  196. return -EOVERFLOW;
  197. }
  198. /* Ensure event does not overflow the available buffer */
  199. if ((offset + event_size) > length) {
  200. dev_err(&adev->dev, "Event exceeds buffer: %zu > %d\n",
  201. offset + event_size, length);
  202. return -EOVERFLOW;
  203. }
  204. /* Point to the next event in the buffer */
  205. offset += event_size;
  206. /* Copy event into the queue */
  207. queue_event = kmemdup(event, event_size, GFP_KERNEL);
  208. if (!queue_event)
  209. return -ENOMEM;
  210. spin_lock(&dev_data->queue_lock);
  211. old_event = event_queue_push(dev_data->events, queue_event);
  212. spin_unlock(&dev_data->queue_lock);
  213. kfree(old_event);
  214. wake_up_interruptible(&dev_data->wq);
  215. }
  216. return 0;
  217. }
  218. /**
  219. * event_device_notify() - Callback when EC generates an event over ACPI.
  220. * @adev: The device that the event is coming from.
  221. * @value: Value passed to Notify() in ACPI.
  222. *
  223. * This function will read the events from the device and enqueue them.
  224. */
  225. static void event_device_notify(struct acpi_device *adev, u32 value)
  226. {
  227. struct acpi_buffer event_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  228. union acpi_object *obj;
  229. acpi_status status;
  230. if (value != EC_ACPI_NOTIFY_EVENT) {
  231. dev_err(&adev->dev, "Invalid event: 0x%08x\n", value);
  232. return;
  233. }
  234. /* Execute ACPI method to get event data buffer. */
  235. status = acpi_evaluate_object(adev->handle, EC_ACPI_GET_EVENT,
  236. NULL, &event_buffer);
  237. if (ACPI_FAILURE(status)) {
  238. dev_err(&adev->dev, "Error executing ACPI method %s()\n",
  239. EC_ACPI_GET_EVENT);
  240. return;
  241. }
  242. obj = (union acpi_object *)event_buffer.pointer;
  243. if (!obj) {
  244. dev_err(&adev->dev, "Nothing returned from %s()\n",
  245. EC_ACPI_GET_EVENT);
  246. return;
  247. }
  248. if (obj->type != ACPI_TYPE_BUFFER) {
  249. dev_err(&adev->dev, "Invalid object returned from %s()\n",
  250. EC_ACPI_GET_EVENT);
  251. kfree(obj);
  252. return;
  253. }
  254. if (obj->buffer.length < sizeof(struct ec_event)) {
  255. dev_err(&adev->dev, "Invalid buffer length %d from %s()\n",
  256. obj->buffer.length, EC_ACPI_GET_EVENT);
  257. kfree(obj);
  258. return;
  259. }
  260. enqueue_events(adev, obj->buffer.pointer, obj->buffer.length);
  261. kfree(obj);
  262. }
  263. static int event_open(struct inode *inode, struct file *filp)
  264. {
  265. struct event_device_data *dev_data;
  266. dev_data = container_of(inode->i_cdev, struct event_device_data, cdev);
  267. if (!dev_data->exist)
  268. return -ENODEV;
  269. if (atomic_cmpxchg(&dev_data->available, 1, 0) == 0)
  270. return -EBUSY;
  271. /* Increase refcount on device so dev_data is not freed */
  272. get_device(&dev_data->dev);
  273. stream_open(inode, filp);
  274. filp->private_data = dev_data;
  275. return 0;
  276. }
  277. static __poll_t event_poll(struct file *filp, poll_table *wait)
  278. {
  279. struct event_device_data *dev_data = filp->private_data;
  280. __poll_t mask = 0;
  281. poll_wait(filp, &dev_data->wq, wait);
  282. if (!dev_data->exist)
  283. return EPOLLHUP;
  284. if (!event_queue_empty(dev_data->events))
  285. mask |= EPOLLIN | EPOLLRDNORM | EPOLLPRI;
  286. return mask;
  287. }
  288. /**
  289. * event_read() - Callback for passing event data to userspace via read().
  290. * @filp: The file we are reading from.
  291. * @buf: Pointer to userspace buffer to fill with one event.
  292. * @count: Number of bytes requested. Must be at least EC_ACPI_MAX_EVENT_SIZE.
  293. * @pos: File position pointer, irrelevant since we don't support seeking.
  294. *
  295. * Removes the first event from the queue, places it in the passed buffer.
  296. *
  297. * If there are no events in the queue, then one of two things happens,
  298. * depending on if the file was opened in nonblocking mode: If in nonblocking
  299. * mode, then return -EAGAIN to say there's no data. If in blocking mode, then
  300. * block until an event is available.
  301. *
  302. * Return: Number of bytes placed in buffer, negative error code on failure.
  303. */
  304. static ssize_t event_read(struct file *filp, char __user *buf, size_t count,
  305. loff_t *pos)
  306. {
  307. struct event_device_data *dev_data = filp->private_data;
  308. struct ec_event *event;
  309. ssize_t n_bytes_written = 0;
  310. int err;
  311. /* We only will give them the entire event at once */
  312. if (count != 0 && count < EC_ACPI_MAX_EVENT_SIZE)
  313. return -EINVAL;
  314. spin_lock(&dev_data->queue_lock);
  315. while (event_queue_empty(dev_data->events)) {
  316. spin_unlock(&dev_data->queue_lock);
  317. if (filp->f_flags & O_NONBLOCK)
  318. return -EAGAIN;
  319. err = wait_event_interruptible(dev_data->wq,
  320. !event_queue_empty(dev_data->events) ||
  321. !dev_data->exist);
  322. if (err)
  323. return err;
  324. /* Device was removed as we waited? */
  325. if (!dev_data->exist)
  326. return -ENODEV;
  327. spin_lock(&dev_data->queue_lock);
  328. }
  329. event = event_queue_pop(dev_data->events);
  330. spin_unlock(&dev_data->queue_lock);
  331. n_bytes_written = ec_event_size(event);
  332. if (copy_to_user(buf, event, n_bytes_written))
  333. n_bytes_written = -EFAULT;
  334. kfree(event);
  335. return n_bytes_written;
  336. }
  337. static int event_release(struct inode *inode, struct file *filp)
  338. {
  339. struct event_device_data *dev_data = filp->private_data;
  340. atomic_set(&dev_data->available, 1);
  341. put_device(&dev_data->dev);
  342. return 0;
  343. }
  344. static const struct file_operations event_fops = {
  345. .open = event_open,
  346. .poll = event_poll,
  347. .read = event_read,
  348. .release = event_release,
  349. .owner = THIS_MODULE,
  350. };
  351. /**
  352. * free_device_data() - Callback to free the event_device_data structure.
  353. * @d: The device embedded in our device data, which we have been ref counting.
  354. *
  355. * This is called only after event_device_remove() has been called and all
  356. * userspace programs have called event_release() on all the open file
  357. * descriptors.
  358. */
  359. static void free_device_data(struct device *d)
  360. {
  361. struct event_device_data *dev_data;
  362. dev_data = container_of(d, struct event_device_data, dev);
  363. event_queue_free(dev_data->events);
  364. kfree(dev_data);
  365. }
  366. static void hangup_device(struct event_device_data *dev_data)
  367. {
  368. dev_data->exist = false;
  369. /* Wake up the waiting processes so they can close. */
  370. wake_up_interruptible(&dev_data->wq);
  371. put_device(&dev_data->dev);
  372. }
  373. /**
  374. * event_device_add() - Callback when creating a new device.
  375. * @adev: ACPI device that we will be receiving events from.
  376. *
  377. * This finds a free minor number for the device, allocates and initializes
  378. * some device data, and creates a new device and char dev node.
  379. *
  380. * The device data is freed in free_device_data(), which is called when
  381. * %dev_data->dev is release()ed. This happens after all references to
  382. * %dev_data->dev are dropped, which happens once both event_device_remove()
  383. * has been called and every open()ed file descriptor has been release()ed.
  384. *
  385. * Return: 0 on success, negative error code on failure.
  386. */
  387. static int event_device_add(struct acpi_device *adev)
  388. {
  389. struct event_device_data *dev_data;
  390. int error, minor;
  391. minor = ida_alloc_max(&event_ida, EVENT_MAX_DEV-1, GFP_KERNEL);
  392. if (minor < 0) {
  393. error = minor;
  394. dev_err(&adev->dev, "Failed to find minor number: %d\n", error);
  395. return error;
  396. }
  397. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  398. if (!dev_data) {
  399. error = -ENOMEM;
  400. goto free_minor;
  401. }
  402. /* Initialize the device data. */
  403. adev->driver_data = dev_data;
  404. dev_data->events = event_queue_new(queue_size);
  405. if (!dev_data->events) {
  406. kfree(dev_data);
  407. error = -ENOMEM;
  408. goto free_minor;
  409. }
  410. spin_lock_init(&dev_data->queue_lock);
  411. init_waitqueue_head(&dev_data->wq);
  412. dev_data->exist = true;
  413. atomic_set(&dev_data->available, 1);
  414. /* Initialize the device. */
  415. dev_data->dev.devt = MKDEV(event_major, minor);
  416. dev_data->dev.class = &event_class;
  417. dev_data->dev.release = free_device_data;
  418. dev_set_name(&dev_data->dev, EVENT_DEV_NAME_FMT, minor);
  419. device_initialize(&dev_data->dev);
  420. /* Initialize the character device, and add it to userspace. */
  421. cdev_init(&dev_data->cdev, &event_fops);
  422. error = cdev_device_add(&dev_data->cdev, &dev_data->dev);
  423. if (error)
  424. goto free_dev_data;
  425. return 0;
  426. free_dev_data:
  427. hangup_device(dev_data);
  428. free_minor:
  429. ida_free(&event_ida, minor);
  430. return error;
  431. }
  432. static void event_device_remove(struct acpi_device *adev)
  433. {
  434. struct event_device_data *dev_data = adev->driver_data;
  435. cdev_device_del(&dev_data->cdev, &dev_data->dev);
  436. ida_free(&event_ida, MINOR(dev_data->dev.devt));
  437. hangup_device(dev_data);
  438. }
  439. static const struct acpi_device_id event_acpi_ids[] = {
  440. { "GOOG000D", 0 },
  441. { }
  442. };
  443. MODULE_DEVICE_TABLE(acpi, event_acpi_ids);
  444. static struct acpi_driver event_driver = {
  445. .name = DRV_NAME,
  446. .class = DRV_NAME,
  447. .ids = event_acpi_ids,
  448. .ops = {
  449. .add = event_device_add,
  450. .notify = event_device_notify,
  451. .remove = event_device_remove,
  452. },
  453. };
  454. static int __init event_module_init(void)
  455. {
  456. dev_t dev_num = 0;
  457. int ret;
  458. ret = class_register(&event_class);
  459. if (ret) {
  460. pr_err(DRV_NAME ": Failed registering class: %d\n", ret);
  461. return ret;
  462. }
  463. /* Request device numbers, starting with minor=0. Save the major num. */
  464. ret = alloc_chrdev_region(&dev_num, 0, EVENT_MAX_DEV, EVENT_DEV_NAME);
  465. if (ret) {
  466. pr_err(DRV_NAME ": Failed allocating dev numbers: %d\n", ret);
  467. goto destroy_class;
  468. }
  469. event_major = MAJOR(dev_num);
  470. ret = acpi_bus_register_driver(&event_driver);
  471. if (ret < 0) {
  472. pr_err(DRV_NAME ": Failed registering driver: %d\n", ret);
  473. goto unregister_region;
  474. }
  475. return 0;
  476. unregister_region:
  477. unregister_chrdev_region(MKDEV(event_major, 0), EVENT_MAX_DEV);
  478. destroy_class:
  479. class_unregister(&event_class);
  480. ida_destroy(&event_ida);
  481. return ret;
  482. }
  483. static void __exit event_module_exit(void)
  484. {
  485. acpi_bus_unregister_driver(&event_driver);
  486. unregister_chrdev_region(MKDEV(event_major, 0), EVENT_MAX_DEV);
  487. class_unregister(&event_class);
  488. ida_destroy(&event_ida);
  489. }
  490. module_init(event_module_init);
  491. module_exit(event_module_exit);
  492. MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
  493. MODULE_DESCRIPTION("Wilco EC ACPI event driver");
  494. MODULE_LICENSE("GPL");