staging: iio: Use kcalloc instead of kzalloc to allocate array
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / industrialio-core.c
index 326e967d54ef035258e7d4c325c4d6c26d731edc..d8cd9e3c07a6e1d3a32b5b8724a9460c3c0ed652 100644 (file)
@@ -25,8 +25,8 @@
 #include "iio.h"
 #include "iio_core.h"
 #include "iio_core_trigger.h"
-#include "chrdev.h"
 #include "sysfs.h"
+#include "events.h"
 
 /* IDA to assign each registered device a unique id*/
 static DEFINE_IDA(iio_ida);
@@ -77,15 +77,16 @@ static const char * const iio_modifier_names[] = {
 
 /* relies on pairs of these shared then separate */
 static const char * const iio_chan_info_postfix[] = {
-       [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
-       [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
-       [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
-       [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
-       [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
-       [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
-       [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED/2]
-       = "quadrature_correction_raw",
-       [IIO_CHAN_INFO_AVERAGE_RAW_SHARED/2] = "mean_raw",
+       [IIO_CHAN_INFO_SCALE] = "scale",
+       [IIO_CHAN_INFO_OFFSET] = "offset",
+       [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
+       [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
+       [IIO_CHAN_INFO_PEAK] = "peak_raw",
+       [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
+       [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
+       [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
+       [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
+       = "filter_low_pass_3db_frequency",
 };
 
 /**
@@ -169,8 +170,11 @@ static ssize_t iio_event_chrdev_read(struct file *filep,
 {
        struct iio_event_interface *ev_int = filep->private_data;
        struct iio_detected_event_list *el;
+       size_t len = sizeof(el->ev);
        int ret;
-       size_t len;
+
+       if (count < len)
+               return -EINVAL;
 
        mutex_lock(&ev_int->event_list_lock);
        if (list_empty(&ev_int->det_events)) {
@@ -192,7 +196,6 @@ static ssize_t iio_event_chrdev_read(struct file *filep,
        el = list_first_entry(&ev_int->det_events,
                              struct iio_detected_event_list,
                              list);
-       len = sizeof el->ev;
        if (copy_to_user(buf, &(el->ev), len)) {
                ret = -EFAULT;
                goto error_mutex_unlock;
@@ -242,6 +245,8 @@ static const struct file_operations iio_event_chrdev_fileops = {
 
 static int iio_event_getfd(struct iio_dev *indio_dev)
 {
+       int fd;
+
        if (indio_dev->event_interface == NULL)
                return -ENODEV;
 
@@ -252,9 +257,15 @@ static int iio_event_getfd(struct iio_dev *indio_dev)
                return -EBUSY;
        }
        mutex_unlock(&indio_dev->event_interface->event_list_lock);
-       return anon_inode_getfd("iio:event",
+       fd = anon_inode_getfd("iio:event",
                                &iio_event_chrdev_fileops,
                                indio_dev->event_interface, O_RDONLY);
+       if (fd < 0) {
+               mutex_lock(&indio_dev->event_interface->event_list_lock);
+               clear_bit(IIO_BUSY_BIT_POS, &indio_dev->event_interface->flags);
+               mutex_unlock(&indio_dev->event_interface->event_list_lock);
+       }
+       return fd;
 }
 
 static int __init iio_init(void)
@@ -408,7 +419,7 @@ int __iio_device_attr_init(struct device_attribute *dev_attr,
        sysfs_attr_init(&dev_attr->attr);
 
        /* Build up postfix of <extend_name>_<modifier>_postfix */
-       if (chan->modified) {
+       if (chan->modified && !generic) {
                if (chan->extend_name)
                        full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
                                                 iio_modifier_names[chan
@@ -593,7 +604,7 @@ static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
                                             chan,
                                             &iio_read_channel_info,
                                             &iio_write_channel_info,
-                                            (1 << i),
+                                            i/2,
                                             !(i%2),
                                             &indio_dev->dev,
                                             &indio_dev->channel_attr_list);
@@ -658,10 +669,9 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
        if (indio_dev->name)
                attrcount++;
 
-       indio_dev->chan_attr_group.attrs
-               = kzalloc(sizeof(indio_dev->chan_attr_group.attrs[0])*
-                         (attrcount + 1),
-                         GFP_KERNEL);
+       indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
+                                                  sizeof(indio_dev->chan_attr_group.attrs[0]),
+                                                  GFP_KERNEL);
        if (indio_dev->chan_attr_group.attrs == NULL) {
                ret = -ENOMEM;
                goto error_clear_attrs;
@@ -781,6 +791,9 @@ static ssize_t iio_ev_value_store(struct device *dev,
        unsigned long val;
        int ret;
 
+       if (!indio_dev->info->write_event_value)
+               return -EINVAL;
+
        ret = strict_strtoul(buf, 10, &val);
        if (ret)
                return ret;
@@ -951,10 +964,9 @@ static int iio_device_register_eventset(struct iio_dev *indio_dev)
        }
 
        indio_dev->event_interface->group.name = iio_event_group_name;
-       indio_dev->event_interface->group.attrs =
-               kzalloc(sizeof(indio_dev->event_interface->group.attrs[0])
-                       *(attrcount + 1),
-                       GFP_KERNEL);
+       indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
+                                                         sizeof(indio_dev->event_interface->group.attrs[0]),
+                                                         GFP_KERNEL);
        if (indio_dev->event_interface->group.attrs == NULL) {
                ret = -ENOMEM;
                goto error_free_setup_event_lines;