drm/sysfs: add audioformat to sysfs
[firefly-linux-kernel-4.4.55.git] / drivers / iio / industrialio-event.c
index 10aa9ef86cece1b80fa09c57d2a52e344e4dfc12..cae332b1d7ea595959c53ecb3c8f51eef9ba095b 100644 (file)
@@ -32,6 +32,7 @@
  * @dev_attr_list:     list of event interface sysfs attribute
  * @flags:             file operations related flags including busy flag.
  * @group:             event interface sysfs attribute group
+ * @read_lock:         lock to protect kfifo read operations
  */
 struct iio_event_interface {
        wait_queue_head_t       wait;
@@ -40,27 +41,34 @@ struct iio_event_interface {
        struct list_head        dev_attr_list;
        unsigned long           flags;
        struct attribute_group  group;
+       struct mutex            read_lock;
 };
 
+/**
+ * iio_push_event() - try to add event to the list for userspace reading
+ * @indio_dev:         IIO device structure
+ * @ev_code:           What event
+ * @timestamp:         When the event occurred
+ *
+ * Note: The caller must make sure that this function is not running
+ * concurrently for the same indio_dev more than once.
+ **/
 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
 {
        struct iio_event_interface *ev_int = indio_dev->event_interface;
        struct iio_event_data ev;
-       unsigned long flags;
        int copied;
 
        /* Does anyone care? */
-       spin_lock_irqsave(&ev_int->wait.lock, flags);
        if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
 
                ev.id = ev_code;
                ev.timestamp = timestamp;
 
-               copied = kfifo_put(&ev_int->det_events, &ev);
+               copied = kfifo_put(&ev_int->det_events, ev);
                if (copied != 0)
-                       wake_up_locked_poll(&ev_int->wait, POLLIN);
+                       wake_up_poll(&ev_int->wait, POLLIN);
        }
-       spin_unlock_irqrestore(&ev_int->wait.lock, flags);
 
        return 0;
 }
@@ -68,19 +76,26 @@ EXPORT_SYMBOL(iio_push_event);
 
 /**
  * iio_event_poll() - poll the event queue to find out if it has data
+ * @filep:     File structure pointer to identify the device
+ * @wait:      Poll table pointer to add the wait queue on
+ *
+ * Return: (POLLIN | POLLRDNORM) if data is available for reading
+ *        or a negative error code on failure
  */
 static unsigned int iio_event_poll(struct file *filep,
                             struct poll_table_struct *wait)
 {
-       struct iio_event_interface *ev_int = filep->private_data;
+       struct iio_dev *indio_dev = filep->private_data;
+       struct iio_event_interface *ev_int = indio_dev->event_interface;
        unsigned int events = 0;
 
+       if (!indio_dev->info)
+               return events;
+
        poll_wait(filep, &ev_int->wait, wait);
 
-       spin_lock_irq(&ev_int->wait.lock);
        if (!kfifo_is_empty(&ev_int->det_events))
                events = POLLIN | POLLRDNORM;
-       spin_unlock_irq(&ev_int->wait.lock);
 
        return events;
 }
@@ -90,48 +105,61 @@ static ssize_t iio_event_chrdev_read(struct file *filep,
                                     size_t count,
                                     loff_t *f_ps)
 {
-       struct iio_event_interface *ev_int = filep->private_data;
+       struct iio_dev *indio_dev = filep->private_data;
+       struct iio_event_interface *ev_int = indio_dev->event_interface;
        unsigned int copied;
        int ret;
 
+       if (!indio_dev->info)
+               return -ENODEV;
+
        if (count < sizeof(struct iio_event_data))
                return -EINVAL;
 
-       spin_lock_irq(&ev_int->wait.lock);
-       if (kfifo_is_empty(&ev_int->det_events)) {
-               if (filep->f_flags & O_NONBLOCK) {
-                       ret = -EAGAIN;
-                       goto error_unlock;
+       do {
+               if (kfifo_is_empty(&ev_int->det_events)) {
+                       if (filep->f_flags & O_NONBLOCK)
+                               return -EAGAIN;
+
+                       ret = wait_event_interruptible(ev_int->wait,
+                                       !kfifo_is_empty(&ev_int->det_events) ||
+                                       indio_dev->info == NULL);
+                       if (ret)
+                               return ret;
+                       if (indio_dev->info == NULL)
+                               return -ENODEV;
                }
-               /* Blocking on device; waiting for something to be there */
-               ret = wait_event_interruptible_locked_irq(ev_int->wait,
-                                       !kfifo_is_empty(&ev_int->det_events));
+
+               if (mutex_lock_interruptible(&ev_int->read_lock))
+                       return -ERESTARTSYS;
+               ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
+               mutex_unlock(&ev_int->read_lock);
+
                if (ret)
-                       goto error_unlock;
-               /* Single access device so no one else can get the data */
-       }
+                       return ret;
 
-       ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
+               /*
+                * If we couldn't read anything from the fifo (a different
+                * thread might have been faster) we either return -EAGAIN if
+                * the file descriptor is non-blocking, otherwise we go back to
+                * sleep and wait for more data to arrive.
+                */
+               if (copied == 0 && (filep->f_flags & O_NONBLOCK))
+                       return -EAGAIN;
 
-error_unlock:
-       spin_unlock_irq(&ev_int->wait.lock);
+       } while (copied == 0);
 
-       return ret ? ret : copied;
+       return copied;
 }
 
 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
 {
-       struct iio_event_interface *ev_int = filep->private_data;
-
-       spin_lock_irq(&ev_int->wait.lock);
-       __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
-       /*
-        * In order to maintain a clean state for reopening,
-        * clear out any awaiting events. The mask will prevent
-        * any new __iio_push_event calls running.
-        */
-       kfifo_reset_out(&ev_int->det_events);
-       spin_unlock_irq(&ev_int->wait.lock);
+       struct iio_dev *indio_dev = filep->private_data;
+       struct iio_event_interface *ev_int = indio_dev->event_interface;
+
+       clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
+
+       iio_device_put(indio_dev);
 
        return 0;
 }
@@ -152,19 +180,20 @@ int iio_event_getfd(struct iio_dev *indio_dev)
        if (ev_int == NULL)
                return -ENODEV;
 
-       spin_lock_irq(&ev_int->wait.lock);
-       if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
-               spin_unlock_irq(&ev_int->wait.lock);
+       if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags))
                return -EBUSY;
-       }
-       spin_unlock_irq(&ev_int->wait.lock);
-       fd = anon_inode_getfd("iio:event",
-                               &iio_event_chrdev_fileops, ev_int, O_RDONLY);
+
+       iio_device_get(indio_dev);
+
+       fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
+                               indio_dev, O_RDONLY | O_CLOEXEC);
        if (fd < 0) {
-               spin_lock_irq(&ev_int->wait.lock);
-               __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
-               spin_unlock_irq(&ev_int->wait.lock);
+               clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
+               iio_device_put(indio_dev);
+       } else {
+               kfifo_reset_out(&ev_int->det_events);
        }
+
        return fd;
 }
 
@@ -174,6 +203,7 @@ static const char * const iio_ev_type_text[] = {
        [IIO_EV_TYPE_ROC] = "roc",
        [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
        [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
+       [IIO_EV_TYPE_CHANGE] = "change",
 };
 
 static const char * const iio_ev_dir_text[] = {
@@ -182,6 +212,30 @@ static const char * const iio_ev_dir_text[] = {
        [IIO_EV_DIR_FALLING] = "falling"
 };
 
+static const char * const iio_ev_info_text[] = {
+       [IIO_EV_INFO_ENABLE] = "en",
+       [IIO_EV_INFO_VALUE] = "value",
+       [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
+       [IIO_EV_INFO_PERIOD] = "period",
+       [IIO_EV_INFO_HIGH_PASS_FILTER_3DB] = "high_pass_filter_3db",
+       [IIO_EV_INFO_LOW_PASS_FILTER_3DB] = "low_pass_filter_3db",
+};
+
+static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
+{
+       return attr->c->event_spec[attr->address & 0xffff].dir;
+}
+
+static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
+{
+       return attr->c->event_spec[attr->address & 0xffff].type;
+}
+
+static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
+{
+       return (attr->address >> 16) & 0xffff;
+}
+
 static ssize_t iio_ev_state_store(struct device *dev,
                                  struct device_attribute *attr,
                                  const char *buf,
@@ -197,8 +251,9 @@ static ssize_t iio_ev_state_store(struct device *dev,
                return ret;
 
        ret = indio_dev->info->write_event_config(indio_dev,
-                                                 this_attr->address,
-                                                 val);
+               this_attr->c, iio_ev_attr_type(this_attr),
+               iio_ev_attr_dir(this_attr), val);
+
        return (ret < 0) ? ret : len;
 }
 
@@ -208,9 +263,11 @@ static ssize_t iio_ev_state_show(struct device *dev,
 {
        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-       int val = indio_dev->info->read_event_config(indio_dev,
-                                                    this_attr->address);
+       int val;
 
+       val = indio_dev->info->read_event_config(indio_dev,
+               this_attr->c, iio_ev_attr_type(this_attr),
+               iio_ev_attr_dir(this_attr));
        if (val < 0)
                return val;
        else
@@ -223,14 +280,18 @@ static ssize_t iio_ev_value_show(struct device *dev,
 {
        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-       int val, ret;
+       int val, val2, val_arr[2];
+       int ret;
 
        ret = indio_dev->info->read_event_value(indio_dev,
-                                               this_attr->address, &val);
+               this_attr->c, iio_ev_attr_type(this_attr),
+               iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
+               &val, &val2);
        if (ret < 0)
                return ret;
-
-       return sprintf(buf, "%d\n", val);
+       val_arr[0] = val;
+       val_arr[1] = val2;
+       return iio_format_value(buf, ret, 2, val_arr);
 }
 
 static ssize_t iio_ev_value_store(struct device *dev,
@@ -240,113 +301,125 @@ static ssize_t iio_ev_value_store(struct device *dev,
 {
        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-       int val;
+       int val, val2;
        int ret;
 
        if (!indio_dev->info->write_event_value)
                return -EINVAL;
 
-       ret = kstrtoint(buf, 10, &val);
+       ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
        if (ret)
                return ret;
-
-       ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
-                                                val);
+       ret = indio_dev->info->write_event_value(indio_dev,
+               this_attr->c, iio_ev_attr_type(this_attr),
+               iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
+               val, val2);
        if (ret < 0)
                return ret;
 
        return len;
 }
 
-static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
-                                     struct iio_chan_spec const *chan)
+static int iio_device_add_event(struct iio_dev *indio_dev,
+       const struct iio_chan_spec *chan, unsigned int spec_index,
+       enum iio_event_type type, enum iio_event_direction dir,
+       enum iio_shared_by shared_by, const unsigned long *mask)
 {
-       int ret = 0, i, attrcount = 0;
-       u64 mask = 0;
+       ssize_t (*show)(struct device *, struct device_attribute *, char *);
+       ssize_t (*store)(struct device *, struct device_attribute *,
+               const char *, size_t);
+       unsigned int attrcount = 0;
+       unsigned int i;
        char *postfix;
-       if (!chan->event_mask)
-               return 0;
+       int ret;
 
-       for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
-               postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
-                                   iio_ev_type_text[i/IIO_EV_DIR_MAX],
-                                   iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
-               if (postfix == NULL) {
-                       ret = -ENOMEM;
-                       goto error_ret;
-               }
-               if (chan->modified)
-                       mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
-                                                 i/IIO_EV_DIR_MAX,
-                                                 i%IIO_EV_DIR_MAX);
-               else if (chan->differential)
-                       mask = IIO_EVENT_CODE(chan->type,
-                                             0, 0,
-                                             i%IIO_EV_DIR_MAX,
-                                             i/IIO_EV_DIR_MAX,
-                                             0,
-                                             chan->channel,
-                                             chan->channel2);
+       for_each_set_bit(i, mask, sizeof(*mask)*8) {
+               if (i >= ARRAY_SIZE(iio_ev_info_text))
+                       return -EINVAL;
+               if (dir != IIO_EV_DIR_NONE)
+                       postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
+                                       iio_ev_type_text[type],
+                                       iio_ev_dir_text[dir],
+                                       iio_ev_info_text[i]);
                else
-                       mask = IIO_UNMOD_EVENT_CODE(chan->type,
-                                                   chan->channel,
-                                                   i/IIO_EV_DIR_MAX,
-                                                   i%IIO_EV_DIR_MAX);
-
-               ret = __iio_add_chan_devattr(postfix,
-                                            chan,
-                                            &iio_ev_state_show,
-                                            iio_ev_state_store,
-                                            mask,
-                                            0,
-                                            &indio_dev->dev,
-                                            &indio_dev->event_interface->
-                                            dev_attr_list);
-               kfree(postfix);
-               if (ret)
-                       goto error_ret;
-               attrcount++;
-               postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
-                                   iio_ev_type_text[i/IIO_EV_DIR_MAX],
-                                   iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
-               if (postfix == NULL) {
-                       ret = -ENOMEM;
-                       goto error_ret;
+                       postfix = kasprintf(GFP_KERNEL, "%s_%s",
+                                       iio_ev_type_text[type],
+                                       iio_ev_info_text[i]);
+               if (postfix == NULL)
+                       return -ENOMEM;
+
+               if (i == IIO_EV_INFO_ENABLE) {
+                       show = iio_ev_state_show;
+                       store = iio_ev_state_store;
+               } else {
+                       show = iio_ev_value_show;
+                       store = iio_ev_value_store;
                }
-               ret = __iio_add_chan_devattr(postfix, chan,
-                                            iio_ev_value_show,
-                                            iio_ev_value_store,
-                                            mask,
-                                            0,
-                                            &indio_dev->dev,
-                                            &indio_dev->event_interface->
-                                            dev_attr_list);
+
+               ret = __iio_add_chan_devattr(postfix, chan, show, store,
+                        (i << 16) | spec_index, shared_by, &indio_dev->dev,
+                       &indio_dev->event_interface->dev_attr_list);
                kfree(postfix);
+
+               if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
+                       continue;
+
                if (ret)
-                       goto error_ret;
+                       return ret;
+
                attrcount++;
        }
-       ret = attrcount;
-error_ret:
-       return ret;
+
+       return attrcount;
 }
 
-static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
+static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
+       struct iio_chan_spec const *chan)
 {
-       struct iio_dev_attr *p, *n;
-       list_for_each_entry_safe(p, n,
-                                &indio_dev->event_interface->
-                                dev_attr_list, l) {
-               kfree(p->dev_attr.attr.name);
-               kfree(p);
+       int ret = 0, i, attrcount = 0;
+       enum iio_event_direction dir;
+       enum iio_event_type type;
+
+       for (i = 0; i < chan->num_event_specs; i++) {
+               type = chan->event_spec[i].type;
+               dir = chan->event_spec[i].dir;
+
+               ret = iio_device_add_event(indio_dev, chan, i, type, dir,
+                       IIO_SEPARATE, &chan->event_spec[i].mask_separate);
+               if (ret < 0)
+                       return ret;
+               attrcount += ret;
+
+               ret = iio_device_add_event(indio_dev, chan, i, type, dir,
+                       IIO_SHARED_BY_TYPE,
+                       &chan->event_spec[i].mask_shared_by_type);
+               if (ret < 0)
+                       return ret;
+               attrcount += ret;
+
+               ret = iio_device_add_event(indio_dev, chan, i, type, dir,
+                       IIO_SHARED_BY_DIR,
+                       &chan->event_spec[i].mask_shared_by_dir);
+               if (ret < 0)
+                       return ret;
+               attrcount += ret;
+
+               ret = iio_device_add_event(indio_dev, chan, i, type, dir,
+                       IIO_SHARED_BY_ALL,
+                       &chan->event_spec[i].mask_shared_by_all);
+               if (ret < 0)
+                       return ret;
+               attrcount += ret;
        }
+       ret = attrcount;
+       return ret;
 }
 
 static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
 {
        int j, ret, attrcount = 0;
 
-       /* Dynically created from the channels array */
+       /* Dynamically created from the channels array */
        for (j = 0; j < indio_dev->num_channels; j++) {
                ret = iio_device_add_event_sysfs(indio_dev,
                                                 &indio_dev->channels[j]);
@@ -361,9 +434,10 @@ static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
 {
        int j;
 
-       for (j = 0; j < indio_dev->num_channels; j++)
-               if (indio_dev->channels[j].event_mask != 0)
+       for (j = 0; j < indio_dev->num_channels; j++) {
+               if (indio_dev->channels[j].num_event_specs != 0)
                        return true;
+       }
        return false;
 }
 
@@ -371,6 +445,7 @@ static void iio_setup_ev_int(struct iio_event_interface *ev_int)
 {
        INIT_KFIFO(ev_int->det_events);
        init_waitqueue_head(&ev_int->wait);
+       mutex_init(&ev_int->read_lock);
 }
 
 static const char *iio_event_group_name = "events";
@@ -386,10 +461,8 @@ int iio_device_register_eventset(struct iio_dev *indio_dev)
 
        indio_dev->event_interface =
                kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
-       if (indio_dev->event_interface == NULL) {
-               ret = -ENOMEM;
-               goto error_ret;
-       }
+       if (indio_dev->event_interface == NULL)
+               return -ENOMEM;
 
        INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
 
@@ -433,18 +506,31 @@ int iio_device_register_eventset(struct iio_dev *indio_dev)
        return 0;
 
 error_free_setup_event_lines:
-       __iio_remove_event_config_attrs(indio_dev);
+       iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
        kfree(indio_dev->event_interface);
-error_ret:
-
+       indio_dev->event_interface = NULL;
        return ret;
 }
 
+/**
+ * iio_device_wakeup_eventset - Wakes up the event waitqueue
+ * @indio_dev: The IIO device
+ *
+ * Wakes up the event waitqueue used for poll() and blocking read().
+ * Should usually be called when the device is unregistered.
+ */
+void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
+{
+       if (indio_dev->event_interface == NULL)
+               return;
+       wake_up(&indio_dev->event_interface->wait);
+}
+
 void iio_device_unregister_eventset(struct iio_dev *indio_dev)
 {
        if (indio_dev->event_interface == NULL)
                return;
-       __iio_remove_event_config_attrs(indio_dev);
+       iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
        kfree(indio_dev->event_interface->group.attrs);
        kfree(indio_dev->event_interface);
 }