aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-05-09 11:08:29 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-05-09 11:08:29 -0400
commit9d569b1cf7a2a385770835df15f77dde587b58a3 (patch)
tree4106995ec0692ca2dbe1d25157d57d5a34aa4097
parent6da6c0db5316275015e8cc2959f12a17584aeb64 (diff)
parent76974ef9d1bf397b7bb97892a3b3bc516a1fc2c2 (diff)
Merge tag 'iio-fixes-for-4.17a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-linus
Jonathan writes: First round of IIO fixes for the 4.17 cycle. * core - fix up some issues with overflow etc around wrong types for some fo the kfifo handling functions. Seems unlikely this would be triggered in reality but the fixes are simple so let's tidy them up. Second patch deals with checking the userspace value passed for length for potential overflow. * ad7793 - Catch up with changes to the ad_sigma_delta core and use read_raw / write_raw iwth IIO_CHAN_INFO_SAMP_FEW to handle sampling frequency control. * at91-sama5d2 - Channel config for differential channels was completely broken. - Missing Kconfig dependency for buffer support. * hid-sensor - Fix an issue with powering up after resume due to wrong reference counting. * stm32-dfsdm - Fix an issue with second writes of the oversampling settings failing. - Fix an issue with the sample rate being set to half of requested value when particular clock source is used.
-rw-r--r--drivers/iio/adc/Kconfig1
-rw-r--r--drivers/iio/adc/ad7793.c75
-rw-r--r--drivers/iio/adc/at91-sama5d2_adc.c41
-rw-r--r--drivers/iio/adc/stm32-dfsdm-adc.c17
-rw-r--r--drivers/iio/buffer/industrialio-buffer-dma.c2
-rw-r--r--drivers/iio/buffer/kfifo_buf.c11
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-trigger.c8
-rw-r--r--include/linux/iio/buffer_impl.h6
8 files changed, 93 insertions, 68 deletions
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 15606f237480..9da79070357c 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -158,6 +158,7 @@ config AT91_SAMA5D2_ADC
158 depends on ARCH_AT91 || COMPILE_TEST 158 depends on ARCH_AT91 || COMPILE_TEST
159 depends on HAS_IOMEM 159 depends on HAS_IOMEM
160 depends on HAS_DMA 160 depends on HAS_DMA
161 select IIO_BUFFER
161 select IIO_TRIGGERED_BUFFER 162 select IIO_TRIGGERED_BUFFER
162 help 163 help
163 Say yes here to build support for Atmel SAMA5D2 ADC which is 164 Say yes here to build support for Atmel SAMA5D2 ADC which is
diff --git a/drivers/iio/adc/ad7793.c b/drivers/iio/adc/ad7793.c
index 801afb61310b..d4bbe5b53318 100644
--- a/drivers/iio/adc/ad7793.c
+++ b/drivers/iio/adc/ad7793.c
@@ -348,55 +348,6 @@ static const u16 ad7793_sample_freq_avail[16] = {0, 470, 242, 123, 62, 50, 39,
348static const u16 ad7797_sample_freq_avail[16] = {0, 0, 0, 123, 62, 50, 0, 348static const u16 ad7797_sample_freq_avail[16] = {0, 0, 0, 123, 62, 50, 0,
349 33, 0, 17, 16, 12, 10, 8, 6, 4}; 349 33, 0, 17, 16, 12, 10, 8, 6, 4};
350 350
351static ssize_t ad7793_read_frequency(struct device *dev,
352 struct device_attribute *attr,
353 char *buf)
354{
355 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
356 struct ad7793_state *st = iio_priv(indio_dev);
357
358 return sprintf(buf, "%d\n",
359 st->chip_info->sample_freq_avail[AD7793_MODE_RATE(st->mode)]);
360}
361
362static ssize_t ad7793_write_frequency(struct device *dev,
363 struct device_attribute *attr,
364 const char *buf,
365 size_t len)
366{
367 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
368 struct ad7793_state *st = iio_priv(indio_dev);
369 long lval;
370 int i, ret;
371
372 ret = kstrtol(buf, 10, &lval);
373 if (ret)
374 return ret;
375
376 if (lval == 0)
377 return -EINVAL;
378
379 for (i = 0; i < 16; i++)
380 if (lval == st->chip_info->sample_freq_avail[i])
381 break;
382 if (i == 16)
383 return -EINVAL;
384
385 ret = iio_device_claim_direct_mode(indio_dev);
386 if (ret)
387 return ret;
388 st->mode &= ~AD7793_MODE_RATE(-1);
389 st->mode |= AD7793_MODE_RATE(i);
390 ad_sd_write_reg(&st->sd, AD7793_REG_MODE, sizeof(st->mode), st->mode);
391 iio_device_release_direct_mode(indio_dev);
392
393 return len;
394}
395
396static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO,
397 ad7793_read_frequency,
398 ad7793_write_frequency);
399
400static IIO_CONST_ATTR_SAMP_FREQ_AVAIL( 351static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
401 "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4"); 352 "470 242 123 62 50 39 33 19 17 16 12 10 8 6 4");
402 353
@@ -424,7 +375,6 @@ static IIO_DEVICE_ATTR_NAMED(in_m_in_scale_available,
424 ad7793_show_scale_available, NULL, 0); 375 ad7793_show_scale_available, NULL, 0);
425 376
426static struct attribute *ad7793_attributes[] = { 377static struct attribute *ad7793_attributes[] = {
427 &iio_dev_attr_sampling_frequency.dev_attr.attr,
428 &iio_const_attr_sampling_frequency_available.dev_attr.attr, 378 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
429 &iio_dev_attr_in_m_in_scale_available.dev_attr.attr, 379 &iio_dev_attr_in_m_in_scale_available.dev_attr.attr,
430 NULL 380 NULL
@@ -435,7 +385,6 @@ static const struct attribute_group ad7793_attribute_group = {
435}; 385};
436 386
437static struct attribute *ad7797_attributes[] = { 387static struct attribute *ad7797_attributes[] = {
438 &iio_dev_attr_sampling_frequency.dev_attr.attr,
439 &iio_const_attr_sampling_frequency_available_ad7797.dev_attr.attr, 388 &iio_const_attr_sampling_frequency_available_ad7797.dev_attr.attr,
440 NULL 389 NULL
441}; 390};
@@ -505,6 +454,10 @@ static int ad7793_read_raw(struct iio_dev *indio_dev,
505 *val -= offset; 454 *val -= offset;
506 } 455 }
507 return IIO_VAL_INT; 456 return IIO_VAL_INT;
457 case IIO_CHAN_INFO_SAMP_FREQ:
458 *val = st->chip_info
459 ->sample_freq_avail[AD7793_MODE_RATE(st->mode)];
460 return IIO_VAL_INT;
508 } 461 }
509 return -EINVAL; 462 return -EINVAL;
510} 463}
@@ -542,6 +495,26 @@ static int ad7793_write_raw(struct iio_dev *indio_dev,
542 break; 495 break;
543 } 496 }
544 break; 497 break;
498 case IIO_CHAN_INFO_SAMP_FREQ:
499 if (!val) {
500 ret = -EINVAL;
501 break;
502 }
503
504 for (i = 0; i < 16; i++)
505 if (val == st->chip_info->sample_freq_avail[i])
506 break;
507
508 if (i == 16) {
509 ret = -EINVAL;
510 break;
511 }
512
513 st->mode &= ~AD7793_MODE_RATE(-1);
514 st->mode |= AD7793_MODE_RATE(i);
515 ad_sd_write_reg(&st->sd, AD7793_REG_MODE, sizeof(st->mode),
516 st->mode);
517 break;
545 default: 518 default:
546 ret = -EINVAL; 519 ret = -EINVAL;
547 } 520 }
diff --git a/drivers/iio/adc/at91-sama5d2_adc.c b/drivers/iio/adc/at91-sama5d2_adc.c
index 4eff8351ce29..8729d6524b4d 100644
--- a/drivers/iio/adc/at91-sama5d2_adc.c
+++ b/drivers/iio/adc/at91-sama5d2_adc.c
@@ -333,6 +333,27 @@ static const struct iio_chan_spec at91_adc_channels[] = {
333 + AT91_SAMA5D2_DIFF_CHAN_CNT + 1), 333 + AT91_SAMA5D2_DIFF_CHAN_CNT + 1),
334}; 334};
335 335
336static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan)
337{
338 int i;
339
340 for (i = 0; i < indio_dev->num_channels; i++) {
341 if (indio_dev->channels[i].scan_index == chan)
342 return i;
343 }
344 return -EINVAL;
345}
346
347static inline struct iio_chan_spec const *
348at91_adc_chan_get(struct iio_dev *indio_dev, int chan)
349{
350 int index = at91_adc_chan_xlate(indio_dev, chan);
351
352 if (index < 0)
353 return NULL;
354 return indio_dev->channels + index;
355}
356
336static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state) 357static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
337{ 358{
338 struct iio_dev *indio = iio_trigger_get_drvdata(trig); 359 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
@@ -350,8 +371,10 @@ static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
350 at91_adc_writel(st, AT91_SAMA5D2_TRGR, status); 371 at91_adc_writel(st, AT91_SAMA5D2_TRGR, status);
351 372
352 for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) { 373 for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
353 struct iio_chan_spec const *chan = indio->channels + bit; 374 struct iio_chan_spec const *chan = at91_adc_chan_get(indio, bit);
354 375
376 if (!chan)
377 continue;
355 if (state) { 378 if (state) {
356 at91_adc_writel(st, AT91_SAMA5D2_CHER, 379 at91_adc_writel(st, AT91_SAMA5D2_CHER,
357 BIT(chan->channel)); 380 BIT(chan->channel));
@@ -448,7 +471,11 @@ static int at91_adc_dma_start(struct iio_dev *indio_dev)
448 471
449 for_each_set_bit(bit, indio_dev->active_scan_mask, 472 for_each_set_bit(bit, indio_dev->active_scan_mask,
450 indio_dev->num_channels) { 473 indio_dev->num_channels) {
451 struct iio_chan_spec const *chan = indio_dev->channels + bit; 474 struct iio_chan_spec const *chan =
475 at91_adc_chan_get(indio_dev, bit);
476
477 if (!chan)
478 continue;
452 479
453 st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8; 480 st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8;
454 } 481 }
@@ -526,8 +553,11 @@ static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
526 */ 553 */
527 for_each_set_bit(bit, indio_dev->active_scan_mask, 554 for_each_set_bit(bit, indio_dev->active_scan_mask,
528 indio_dev->num_channels) { 555 indio_dev->num_channels) {
529 struct iio_chan_spec const *chan = indio_dev->channels + bit; 556 struct iio_chan_spec const *chan =
557 at91_adc_chan_get(indio_dev, bit);
530 558
559 if (!chan)
560 continue;
531 if (st->dma_st.dma_chan) 561 if (st->dma_st.dma_chan)
532 at91_adc_readl(st, chan->address); 562 at91_adc_readl(st, chan->address);
533 } 563 }
@@ -587,8 +617,11 @@ static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev,
587 617
588 for_each_set_bit(bit, indio_dev->active_scan_mask, 618 for_each_set_bit(bit, indio_dev->active_scan_mask,
589 indio_dev->num_channels) { 619 indio_dev->num_channels) {
590 struct iio_chan_spec const *chan = indio_dev->channels + bit; 620 struct iio_chan_spec const *chan =
621 at91_adc_chan_get(indio_dev, bit);
591 622
623 if (!chan)
624 continue;
592 st->buffer[i] = at91_adc_readl(st, chan->address); 625 st->buffer[i] = at91_adc_readl(st, chan->address);
593 i++; 626 i++;
594 } 627 }
diff --git a/drivers/iio/adc/stm32-dfsdm-adc.c b/drivers/iio/adc/stm32-dfsdm-adc.c
index 01422d11753c..b28a716a23b2 100644
--- a/drivers/iio/adc/stm32-dfsdm-adc.c
+++ b/drivers/iio/adc/stm32-dfsdm-adc.c
@@ -144,6 +144,7 @@ static int stm32_dfsdm_set_osrs(struct stm32_dfsdm_filter *fl,
144 * Leave as soon as if exact resolution if reached. 144 * Leave as soon as if exact resolution if reached.
145 * Otherwise the higher resolution below 32 bits is kept. 145 * Otherwise the higher resolution below 32 bits is kept.
146 */ 146 */
147 fl->res = 0;
147 for (fosr = 1; fosr <= DFSDM_MAX_FL_OVERSAMPLING; fosr++) { 148 for (fosr = 1; fosr <= DFSDM_MAX_FL_OVERSAMPLING; fosr++) {
148 for (iosr = 1; iosr <= DFSDM_MAX_INT_OVERSAMPLING; iosr++) { 149 for (iosr = 1; iosr <= DFSDM_MAX_INT_OVERSAMPLING; iosr++) {
149 if (fast) 150 if (fast)
@@ -193,7 +194,7 @@ static int stm32_dfsdm_set_osrs(struct stm32_dfsdm_filter *fl,
193 } 194 }
194 } 195 }
195 196
196 if (!fl->fosr) 197 if (!fl->res)
197 return -EINVAL; 198 return -EINVAL;
198 199
199 return 0; 200 return 0;
@@ -770,7 +771,7 @@ static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
770 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 771 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev);
771 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; 772 struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id];
772 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel]; 773 struct stm32_dfsdm_channel *ch = &adc->dfsdm->ch_list[chan->channel];
773 unsigned int spi_freq = adc->spi_freq; 774 unsigned int spi_freq;
774 int ret = -EINVAL; 775 int ret = -EINVAL;
775 776
776 switch (mask) { 777 switch (mask) {
@@ -784,8 +785,18 @@ static int stm32_dfsdm_write_raw(struct iio_dev *indio_dev,
784 case IIO_CHAN_INFO_SAMP_FREQ: 785 case IIO_CHAN_INFO_SAMP_FREQ:
785 if (!val) 786 if (!val)
786 return -EINVAL; 787 return -EINVAL;
787 if (ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) 788
789 switch (ch->src) {
790 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL:
788 spi_freq = adc->dfsdm->spi_master_freq; 791 spi_freq = adc->dfsdm->spi_master_freq;
792 break;
793 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_FALLING:
794 case DFSDM_CHANNEL_SPI_CLOCK_INTERNAL_DIV2_RISING:
795 spi_freq = adc->dfsdm->spi_master_freq / 2;
796 break;
797 default:
798 spi_freq = adc->spi_freq;
799 }
789 800
790 if (spi_freq % val) 801 if (spi_freq % val)
791 dev_warn(&indio_dev->dev, 802 dev_warn(&indio_dev->dev,
diff --git a/drivers/iio/buffer/industrialio-buffer-dma.c b/drivers/iio/buffer/industrialio-buffer-dma.c
index 05e0c353e089..b32bf57910ca 100644
--- a/drivers/iio/buffer/industrialio-buffer-dma.c
+++ b/drivers/iio/buffer/industrialio-buffer-dma.c
@@ -587,7 +587,7 @@ EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum);
587 * Should be used as the set_length callback for iio_buffer_access_ops 587 * Should be used as the set_length callback for iio_buffer_access_ops
588 * struct for DMA buffers. 588 * struct for DMA buffers.
589 */ 589 */
590int iio_dma_buffer_set_length(struct iio_buffer *buffer, int length) 590int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length)
591{ 591{
592 /* Avoid an invalid state */ 592 /* Avoid an invalid state */
593 if (length < 2) 593 if (length < 2)
diff --git a/drivers/iio/buffer/kfifo_buf.c b/drivers/iio/buffer/kfifo_buf.c
index 047fe757ab97..70c302a93d7f 100644
--- a/drivers/iio/buffer/kfifo_buf.c
+++ b/drivers/iio/buffer/kfifo_buf.c
@@ -22,11 +22,18 @@ struct iio_kfifo {
22#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer) 22#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
23 23
24static inline int __iio_allocate_kfifo(struct iio_kfifo *buf, 24static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
25 int bytes_per_datum, int length) 25 size_t bytes_per_datum, unsigned int length)
26{ 26{
27 if ((length == 0) || (bytes_per_datum == 0)) 27 if ((length == 0) || (bytes_per_datum == 0))
28 return -EINVAL; 28 return -EINVAL;
29 29
30 /*
31 * Make sure we don't overflow an unsigned int after kfifo rounds up to
32 * the next power of 2.
33 */
34 if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
35 return -EINVAL;
36
30 return __kfifo_alloc((struct __kfifo *)&buf->kf, length, 37 return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
31 bytes_per_datum, GFP_KERNEL); 38 bytes_per_datum, GFP_KERNEL);
32} 39}
@@ -67,7 +74,7 @@ static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
67 return 0; 74 return 0;
68} 75}
69 76
70static int iio_set_length_kfifo(struct iio_buffer *r, int length) 77static int iio_set_length_kfifo(struct iio_buffer *r, unsigned int length)
71{ 78{
72 /* Avoid an invalid state */ 79 /* Avoid an invalid state */
73 if (length < 2) 80 if (length < 2)
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
index cfb6588565ba..4905a997a7ec 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
@@ -178,14 +178,14 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
178#ifdef CONFIG_PM 178#ifdef CONFIG_PM
179 int ret; 179 int ret;
180 180
181 atomic_set(&st->user_requested_state, state);
182
183 if (atomic_add_unless(&st->runtime_pm_enable, 1, 1)) 181 if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
184 pm_runtime_enable(&st->pdev->dev); 182 pm_runtime_enable(&st->pdev->dev);
185 183
186 if (state) 184 if (state) {
185 atomic_inc(&st->user_requested_state);
187 ret = pm_runtime_get_sync(&st->pdev->dev); 186 ret = pm_runtime_get_sync(&st->pdev->dev);
188 else { 187 } else {
188 atomic_dec(&st->user_requested_state);
189 pm_runtime_mark_last_busy(&st->pdev->dev); 189 pm_runtime_mark_last_busy(&st->pdev->dev);
190 pm_runtime_use_autosuspend(&st->pdev->dev); 190 pm_runtime_use_autosuspend(&st->pdev->dev);
191 ret = pm_runtime_put_autosuspend(&st->pdev->dev); 191 ret = pm_runtime_put_autosuspend(&st->pdev->dev);
diff --git a/include/linux/iio/buffer_impl.h b/include/linux/iio/buffer_impl.h
index b9e22b7e2f28..d1171db23742 100644
--- a/include/linux/iio/buffer_impl.h
+++ b/include/linux/iio/buffer_impl.h
@@ -53,7 +53,7 @@ struct iio_buffer_access_funcs {
53 int (*request_update)(struct iio_buffer *buffer); 53 int (*request_update)(struct iio_buffer *buffer);
54 54
55 int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd); 55 int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
56 int (*set_length)(struct iio_buffer *buffer, int length); 56 int (*set_length)(struct iio_buffer *buffer, unsigned int length);
57 57
58 int (*enable)(struct iio_buffer *buffer, struct iio_dev *indio_dev); 58 int (*enable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
59 int (*disable)(struct iio_buffer *buffer, struct iio_dev *indio_dev); 59 int (*disable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
@@ -72,10 +72,10 @@ struct iio_buffer_access_funcs {
72 */ 72 */
73struct iio_buffer { 73struct iio_buffer {
74 /** @length: Number of datums in buffer. */ 74 /** @length: Number of datums in buffer. */
75 int length; 75 unsigned int length;
76 76
77 /** @bytes_per_datum: Size of individual datum including timestamp. */ 77 /** @bytes_per_datum: Size of individual datum including timestamp. */
78 int bytes_per_datum; 78 size_t bytes_per_datum;
79 79
80 /** 80 /**
81 * @access: Buffer access functions associated with the 81 * @access: Buffer access functions associated with the