Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ACPI event handling for Wilco Embedded Controller
0004  *
0005  * Copyright 2019 Google LLC
0006  *
0007  * The Wilco Embedded Controller can create custom events that
0008  * are not handled as standard ACPI objects. These events can
0009  * contain information about changes in EC controlled features,
0010  * such as errors and events in the dock or display. For example,
0011  * an event is triggered if the dock is plugged into a display
0012  * incorrectly. These events are needed for telemetry and
0013  * diagnostics reasons, and for possibly alerting the user.
0014 
0015  * These events are triggered by the EC with an ACPI Notify(0x90),
0016  * and then the BIOS reads the event buffer from EC RAM via an
0017  * ACPI method. When the OS receives these events via ACPI,
0018  * it passes them along to this driver. The events are put into
0019  * a queue which can be read by a userspace daemon via a char device
0020  * that implements read() and poll(). The event queue acts as a
0021  * circular buffer of size 64, so if there are no userspace consumers
0022  * the kernel will not run out of memory. The char device will appear at
0023  * /dev/wilco_event{n}, where n is some small non-negative integer,
0024  * starting from 0. Standard ACPI events such as the battery getting
0025  * plugged/unplugged can also come through this path, but they are
0026  * dealt with via other paths, and are ignored here.
0027 
0028  * To test, you can tail the binary data with
0029  * $ cat /dev/wilco_event0 | hexdump -ve '1/1 "%x\n"'
0030  * and then create an event by plugging/unplugging the battery.
0031  */
0032 
0033 #include <linux/acpi.h>
0034 #include <linux/cdev.h>
0035 #include <linux/device.h>
0036 #include <linux/fs.h>
0037 #include <linux/idr.h>
0038 #include <linux/io.h>
0039 #include <linux/list.h>
0040 #include <linux/module.h>
0041 #include <linux/poll.h>
0042 #include <linux/spinlock.h>
0043 #include <linux/uaccess.h>
0044 #include <linux/wait.h>
0045 
0046 /* ACPI Notify event code indicating event data is available. */
0047 #define EC_ACPI_NOTIFY_EVENT        0x90
0048 /* ACPI Method to execute to retrieve event data buffer from the EC. */
0049 #define EC_ACPI_GET_EVENT       "QSET"
0050 /* Maximum number of words in event data returned by the EC. */
0051 #define EC_ACPI_MAX_EVENT_WORDS     6
0052 #define EC_ACPI_MAX_EVENT_SIZE \
0053     (sizeof(struct ec_event) + (EC_ACPI_MAX_EVENT_WORDS) * sizeof(u16))
0054 
0055 /* Node will appear in /dev/EVENT_DEV_NAME */
0056 #define EVENT_DEV_NAME      "wilco_event"
0057 #define EVENT_CLASS_NAME    EVENT_DEV_NAME
0058 #define DRV_NAME        EVENT_DEV_NAME
0059 #define EVENT_DEV_NAME_FMT  (EVENT_DEV_NAME "%d")
0060 static struct class event_class = {
0061     .owner  = THIS_MODULE,
0062     .name   = EVENT_CLASS_NAME,
0063 };
0064 
0065 /* Keep track of all the device numbers used. */
0066 #define EVENT_MAX_DEV 128
0067 static int event_major;
0068 static DEFINE_IDA(event_ida);
0069 
0070 /* Size of circular queue of events. */
0071 #define MAX_NUM_EVENTS 64
0072 
0073 /**
0074  * struct ec_event - Extended event returned by the EC.
0075  * @size: Number of 16bit words in structure after the size word.
0076  * @type: Extended event type, meaningless for us.
0077  * @event: Event data words.  Max count is %EC_ACPI_MAX_EVENT_WORDS.
0078  */
0079 struct ec_event {
0080     u16 size;
0081     u16 type;
0082     u16 event[];
0083 } __packed;
0084 
0085 #define ec_event_num_words(ev) (ev->size - 1)
0086 #define ec_event_size(ev) (sizeof(*ev) + (ec_event_num_words(ev) * sizeof(u16)))
0087 
0088 /**
0089  * struct ec_event_queue - Circular queue for events.
0090  * @capacity: Number of elements the queue can hold.
0091  * @head: Next index to write to.
0092  * @tail: Next index to read from.
0093  * @entries: Array of events.
0094  */
0095 struct ec_event_queue {
0096     int capacity;
0097     int head;
0098     int tail;
0099     struct ec_event *entries[];
0100 };
0101 
0102 /* Maximum number of events to store in ec_event_queue */
0103 static int queue_size = 64;
0104 module_param(queue_size, int, 0644);
0105 
0106 static struct ec_event_queue *event_queue_new(int capacity)
0107 {
0108     struct ec_event_queue *q;
0109 
0110     q = kzalloc(struct_size(q, entries, capacity), GFP_KERNEL);
0111     if (!q)
0112         return NULL;
0113 
0114     q->capacity = capacity;
0115 
0116     return q;
0117 }
0118 
0119 static inline bool event_queue_empty(struct ec_event_queue *q)
0120 {
0121     /* head==tail when both full and empty, but head==NULL when empty */
0122     return q->head == q->tail && !q->entries[q->head];
0123 }
0124 
0125 static inline bool event_queue_full(struct ec_event_queue *q)
0126 {
0127     /* head==tail when both full and empty, but head!=NULL when full */
0128     return q->head == q->tail && q->entries[q->head];
0129 }
0130 
0131 static struct ec_event *event_queue_pop(struct ec_event_queue *q)
0132 {
0133     struct ec_event *ev;
0134 
0135     if (event_queue_empty(q))
0136         return NULL;
0137 
0138     ev = q->entries[q->tail];
0139     q->entries[q->tail] = NULL;
0140     q->tail = (q->tail + 1) % q->capacity;
0141 
0142     return ev;
0143 }
0144 
0145 /*
0146  * If full, overwrite the oldest event and return it so the caller
0147  * can kfree it. If not full, return NULL.
0148  */
0149 static struct ec_event *event_queue_push(struct ec_event_queue *q,
0150                      struct ec_event *ev)
0151 {
0152     struct ec_event *popped = NULL;
0153 
0154     if (event_queue_full(q))
0155         popped = event_queue_pop(q);
0156     q->entries[q->head] = ev;
0157     q->head = (q->head + 1) % q->capacity;
0158 
0159     return popped;
0160 }
0161 
0162 static void event_queue_free(struct ec_event_queue *q)
0163 {
0164     struct ec_event *event;
0165 
0166     while ((event = event_queue_pop(q)) != NULL)
0167         kfree(event);
0168 
0169     kfree(q);
0170 }
0171 
0172 /**
0173  * struct event_device_data - Data for a Wilco EC device that responds to ACPI.
0174  * @events: Circular queue of EC events to be provided to userspace.
0175  * @queue_lock: Protect the queue from simultaneous read/writes.
0176  * @wq: Wait queue to notify processes when events are available or the
0177  *  device has been removed.
0178  * @cdev: Char dev that userspace reads() and polls() from.
0179  * @dev: Device associated with the %cdev.
0180  * @exist: Has the device been not been removed? Once a device has been removed,
0181  *     writes, reads, and new opens will fail.
0182  * @available: Guarantee only one client can open() file and read from queue.
0183  *
0184  * There will be one of these structs for each ACPI device registered. This data
0185  * is the queue of events received from ACPI that still need to be read from
0186  * userspace, the device and char device that userspace is using, a wait queue
0187  * used to notify different threads when something has changed, plus a flag
0188  * on whether the ACPI device has been removed.
0189  */
0190 struct event_device_data {
0191     struct ec_event_queue *events;
0192     spinlock_t queue_lock;
0193     wait_queue_head_t wq;
0194     struct device dev;
0195     struct cdev cdev;
0196     bool exist;
0197     atomic_t available;
0198 };
0199 
0200 /**
0201  * enqueue_events() - Place EC events in queue to be read by userspace.
0202  * @adev: Device the events came from.
0203  * @buf: Buffer of event data.
0204  * @length: Length of event data buffer.
0205  *
0206  * %buf contains a number of ec_event's, packed one after the other.
0207  * Each ec_event is of variable length. Start with the first event, copy it
0208  * into a persistent ec_event, store that entry in the queue, move on
0209  * to the next ec_event in buf, and repeat.
0210  *
0211  * Return: 0 on success or negative error code on failure.
0212  */
0213 static int enqueue_events(struct acpi_device *adev, const u8 *buf, u32 length)
0214 {
0215     struct event_device_data *dev_data = adev->driver_data;
0216     struct ec_event *event, *queue_event, *old_event;
0217     size_t num_words, event_size;
0218     u32 offset = 0;
0219 
0220     while (offset < length) {
0221         event = (struct ec_event *)(buf + offset);
0222 
0223         num_words = ec_event_num_words(event);
0224         event_size = ec_event_size(event);
0225         if (num_words > EC_ACPI_MAX_EVENT_WORDS) {
0226             dev_err(&adev->dev, "Too many event words: %zu > %d\n",
0227                 num_words, EC_ACPI_MAX_EVENT_WORDS);
0228             return -EOVERFLOW;
0229         }
0230 
0231         /* Ensure event does not overflow the available buffer */
0232         if ((offset + event_size) > length) {
0233             dev_err(&adev->dev, "Event exceeds buffer: %zu > %d\n",
0234                 offset + event_size, length);
0235             return -EOVERFLOW;
0236         }
0237 
0238         /* Point to the next event in the buffer */
0239         offset += event_size;
0240 
0241         /* Copy event into the queue */
0242         queue_event = kmemdup(event, event_size, GFP_KERNEL);
0243         if (!queue_event)
0244             return -ENOMEM;
0245         spin_lock(&dev_data->queue_lock);
0246         old_event = event_queue_push(dev_data->events, queue_event);
0247         spin_unlock(&dev_data->queue_lock);
0248         kfree(old_event);
0249         wake_up_interruptible(&dev_data->wq);
0250     }
0251 
0252     return 0;
0253 }
0254 
0255 /**
0256  * event_device_notify() - Callback when EC generates an event over ACPI.
0257  * @adev: The device that the event is coming from.
0258  * @value: Value passed to Notify() in ACPI.
0259  *
0260  * This function will read the events from the device and enqueue them.
0261  */
0262 static void event_device_notify(struct acpi_device *adev, u32 value)
0263 {
0264     struct acpi_buffer event_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0265     union acpi_object *obj;
0266     acpi_status status;
0267 
0268     if (value != EC_ACPI_NOTIFY_EVENT) {
0269         dev_err(&adev->dev, "Invalid event: 0x%08x\n", value);
0270         return;
0271     }
0272 
0273     /* Execute ACPI method to get event data buffer. */
0274     status = acpi_evaluate_object(adev->handle, EC_ACPI_GET_EVENT,
0275                       NULL, &event_buffer);
0276     if (ACPI_FAILURE(status)) {
0277         dev_err(&adev->dev, "Error executing ACPI method %s()\n",
0278             EC_ACPI_GET_EVENT);
0279         return;
0280     }
0281 
0282     obj = (union acpi_object *)event_buffer.pointer;
0283     if (!obj) {
0284         dev_err(&adev->dev, "Nothing returned from %s()\n",
0285             EC_ACPI_GET_EVENT);
0286         return;
0287     }
0288     if (obj->type != ACPI_TYPE_BUFFER) {
0289         dev_err(&adev->dev, "Invalid object returned from %s()\n",
0290             EC_ACPI_GET_EVENT);
0291         kfree(obj);
0292         return;
0293     }
0294     if (obj->buffer.length < sizeof(struct ec_event)) {
0295         dev_err(&adev->dev, "Invalid buffer length %d from %s()\n",
0296             obj->buffer.length, EC_ACPI_GET_EVENT);
0297         kfree(obj);
0298         return;
0299     }
0300 
0301     enqueue_events(adev, obj->buffer.pointer, obj->buffer.length);
0302     kfree(obj);
0303 }
0304 
0305 static int event_open(struct inode *inode, struct file *filp)
0306 {
0307     struct event_device_data *dev_data;
0308 
0309     dev_data = container_of(inode->i_cdev, struct event_device_data, cdev);
0310     if (!dev_data->exist)
0311         return -ENODEV;
0312 
0313     if (atomic_cmpxchg(&dev_data->available, 1, 0) == 0)
0314         return -EBUSY;
0315 
0316     /* Increase refcount on device so dev_data is not freed */
0317     get_device(&dev_data->dev);
0318     stream_open(inode, filp);
0319     filp->private_data = dev_data;
0320 
0321     return 0;
0322 }
0323 
0324 static __poll_t event_poll(struct file *filp, poll_table *wait)
0325 {
0326     struct event_device_data *dev_data = filp->private_data;
0327     __poll_t mask = 0;
0328 
0329     poll_wait(filp, &dev_data->wq, wait);
0330     if (!dev_data->exist)
0331         return EPOLLHUP;
0332     if (!event_queue_empty(dev_data->events))
0333         mask |= EPOLLIN | EPOLLRDNORM | EPOLLPRI;
0334     return mask;
0335 }
0336 
0337 /**
0338  * event_read() - Callback for passing event data to userspace via read().
0339  * @filp: The file we are reading from.
0340  * @buf: Pointer to userspace buffer to fill with one event.
0341  * @count: Number of bytes requested. Must be at least EC_ACPI_MAX_EVENT_SIZE.
0342  * @pos: File position pointer, irrelevant since we don't support seeking.
0343  *
0344  * Removes the first event from the queue, places it in the passed buffer.
0345  *
0346  * If there are no events in the queue, then one of two things happens,
0347  * depending on if the file was opened in nonblocking mode: If in nonblocking
0348  * mode, then return -EAGAIN to say there's no data. If in blocking mode, then
0349  * block until an event is available.
0350  *
0351  * Return: Number of bytes placed in buffer, negative error code on failure.
0352  */
0353 static ssize_t event_read(struct file *filp, char __user *buf, size_t count,
0354               loff_t *pos)
0355 {
0356     struct event_device_data *dev_data = filp->private_data;
0357     struct ec_event *event;
0358     ssize_t n_bytes_written = 0;
0359     int err;
0360 
0361     /* We only will give them the entire event at once */
0362     if (count != 0 && count < EC_ACPI_MAX_EVENT_SIZE)
0363         return -EINVAL;
0364 
0365     spin_lock(&dev_data->queue_lock);
0366     while (event_queue_empty(dev_data->events)) {
0367         spin_unlock(&dev_data->queue_lock);
0368         if (filp->f_flags & O_NONBLOCK)
0369             return -EAGAIN;
0370 
0371         err = wait_event_interruptible(dev_data->wq,
0372                     !event_queue_empty(dev_data->events) ||
0373                     !dev_data->exist);
0374         if (err)
0375             return err;
0376 
0377         /* Device was removed as we waited? */
0378         if (!dev_data->exist)
0379             return -ENODEV;
0380         spin_lock(&dev_data->queue_lock);
0381     }
0382     event = event_queue_pop(dev_data->events);
0383     spin_unlock(&dev_data->queue_lock);
0384     n_bytes_written = ec_event_size(event);
0385     if (copy_to_user(buf, event, n_bytes_written))
0386         n_bytes_written = -EFAULT;
0387     kfree(event);
0388 
0389     return n_bytes_written;
0390 }
0391 
0392 static int event_release(struct inode *inode, struct file *filp)
0393 {
0394     struct event_device_data *dev_data = filp->private_data;
0395 
0396     atomic_set(&dev_data->available, 1);
0397     put_device(&dev_data->dev);
0398 
0399     return 0;
0400 }
0401 
0402 static const struct file_operations event_fops = {
0403     .open = event_open,
0404     .poll  = event_poll,
0405     .read = event_read,
0406     .release = event_release,
0407     .llseek = no_llseek,
0408     .owner = THIS_MODULE,
0409 };
0410 
0411 /**
0412  * free_device_data() - Callback to free the event_device_data structure.
0413  * @d: The device embedded in our device data, which we have been ref counting.
0414  *
0415  * This is called only after event_device_remove() has been called and all
0416  * userspace programs have called event_release() on all the open file
0417  * descriptors.
0418  */
0419 static void free_device_data(struct device *d)
0420 {
0421     struct event_device_data *dev_data;
0422 
0423     dev_data = container_of(d, struct event_device_data, dev);
0424     event_queue_free(dev_data->events);
0425     kfree(dev_data);
0426 }
0427 
0428 static void hangup_device(struct event_device_data *dev_data)
0429 {
0430     dev_data->exist = false;
0431     /* Wake up the waiting processes so they can close. */
0432     wake_up_interruptible(&dev_data->wq);
0433     put_device(&dev_data->dev);
0434 }
0435 
0436 /**
0437  * event_device_add() - Callback when creating a new device.
0438  * @adev: ACPI device that we will be receiving events from.
0439  *
0440  * This finds a free minor number for the device, allocates and initializes
0441  * some device data, and creates a new device and char dev node.
0442  *
0443  * The device data is freed in free_device_data(), which is called when
0444  * %dev_data->dev is release()ed. This happens after all references to
0445  * %dev_data->dev are dropped, which happens once both event_device_remove()
0446  * has been called and every open()ed file descriptor has been release()ed.
0447  *
0448  * Return: 0 on success, negative error code on failure.
0449  */
0450 static int event_device_add(struct acpi_device *adev)
0451 {
0452     struct event_device_data *dev_data;
0453     int error, minor;
0454 
0455     minor = ida_alloc_max(&event_ida, EVENT_MAX_DEV-1, GFP_KERNEL);
0456     if (minor < 0) {
0457         error = minor;
0458         dev_err(&adev->dev, "Failed to find minor number: %d\n", error);
0459         return error;
0460     }
0461 
0462     dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
0463     if (!dev_data) {
0464         error = -ENOMEM;
0465         goto free_minor;
0466     }
0467 
0468     /* Initialize the device data. */
0469     adev->driver_data = dev_data;
0470     dev_data->events = event_queue_new(queue_size);
0471     if (!dev_data->events) {
0472         kfree(dev_data);
0473         error = -ENOMEM;
0474         goto free_minor;
0475     }
0476     spin_lock_init(&dev_data->queue_lock);
0477     init_waitqueue_head(&dev_data->wq);
0478     dev_data->exist = true;
0479     atomic_set(&dev_data->available, 1);
0480 
0481     /* Initialize the device. */
0482     dev_data->dev.devt = MKDEV(event_major, minor);
0483     dev_data->dev.class = &event_class;
0484     dev_data->dev.release = free_device_data;
0485     dev_set_name(&dev_data->dev, EVENT_DEV_NAME_FMT, minor);
0486     device_initialize(&dev_data->dev);
0487 
0488     /* Initialize the character device, and add it to userspace. */
0489     cdev_init(&dev_data->cdev, &event_fops);
0490     error = cdev_device_add(&dev_data->cdev, &dev_data->dev);
0491     if (error)
0492         goto free_dev_data;
0493 
0494     return 0;
0495 
0496 free_dev_data:
0497     hangup_device(dev_data);
0498 free_minor:
0499     ida_simple_remove(&event_ida, minor);
0500     return error;
0501 }
0502 
0503 static int event_device_remove(struct acpi_device *adev)
0504 {
0505     struct event_device_data *dev_data = adev->driver_data;
0506 
0507     cdev_device_del(&dev_data->cdev, &dev_data->dev);
0508     ida_simple_remove(&event_ida, MINOR(dev_data->dev.devt));
0509     hangup_device(dev_data);
0510 
0511     return 0;
0512 }
0513 
0514 static const struct acpi_device_id event_acpi_ids[] = {
0515     { "GOOG000D", 0 },
0516     { }
0517 };
0518 MODULE_DEVICE_TABLE(acpi, event_acpi_ids);
0519 
0520 static struct acpi_driver event_driver = {
0521     .name = DRV_NAME,
0522     .class = DRV_NAME,
0523     .ids = event_acpi_ids,
0524     .ops = {
0525         .add = event_device_add,
0526         .notify = event_device_notify,
0527         .remove = event_device_remove,
0528     },
0529     .owner = THIS_MODULE,
0530 };
0531 
0532 static int __init event_module_init(void)
0533 {
0534     dev_t dev_num = 0;
0535     int ret;
0536 
0537     ret = class_register(&event_class);
0538     if (ret) {
0539         pr_err(DRV_NAME ": Failed registering class: %d\n", ret);
0540         return ret;
0541     }
0542 
0543     /* Request device numbers, starting with minor=0. Save the major num. */
0544     ret = alloc_chrdev_region(&dev_num, 0, EVENT_MAX_DEV, EVENT_DEV_NAME);
0545     if (ret) {
0546         pr_err(DRV_NAME ": Failed allocating dev numbers: %d\n", ret);
0547         goto destroy_class;
0548     }
0549     event_major = MAJOR(dev_num);
0550 
0551     ret = acpi_bus_register_driver(&event_driver);
0552     if (ret < 0) {
0553         pr_err(DRV_NAME ": Failed registering driver: %d\n", ret);
0554         goto unregister_region;
0555     }
0556 
0557     return 0;
0558 
0559 unregister_region:
0560     unregister_chrdev_region(MKDEV(event_major, 0), EVENT_MAX_DEV);
0561 destroy_class:
0562     class_unregister(&event_class);
0563     ida_destroy(&event_ida);
0564     return ret;
0565 }
0566 
0567 static void __exit event_module_exit(void)
0568 {
0569     acpi_bus_unregister_driver(&event_driver);
0570     unregister_chrdev_region(MKDEV(event_major, 0), EVENT_MAX_DEV);
0571     class_unregister(&event_class);
0572     ida_destroy(&event_ida);
0573 }
0574 
0575 module_init(event_module_init);
0576 module_exit(event_module_exit);
0577 
0578 MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
0579 MODULE_DESCRIPTION("Wilco EC ACPI event driver");
0580 MODULE_LICENSE("GPL");
0581 MODULE_ALIAS("platform:" DRV_NAME);