Merge tag 'iio-fixes-for-3.12a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-linus

Jonathan writes:

First round of IIO fixes for 3.12

A series of wrong 'struct dev' assumptions in suspend/resume callbacks
following on from this issue being identified in a new driver review.
One to watch out for in future.

A number of driver specific fixes
1) at91 - fix a overflow in clock rate computation
2) dummy - Kconfig dependency issue
3) isl29018 - uninitialized value
4) hmc5843 - measurement conversion bug introduced by recent cleanup.
5) ade7854-spi - wrong return value.

Some IIO core fixes
1) Wrong value picked up for event code creation for a modified channel
2) A null dereference on failure to initialize a buffer after no buffer has
been in use, when using the available_scan_masks approach.
3) Sampling not stopped when a device is removed. Effects forced removal
such as hot unplugging.
4) Prevent device going away if a chrdev is still open in userspace.
5) Prevent race on chardev opening and device being freed.
6) Add a missing iio_buffer_init in the call back buffer.

These last few are the first part of a set from Lars-Peter Clausen who
has been taking a closer look at our removal paths and buffer handling
than anyone has for quite some time.

Changed files
+91 -36
drivers
+2 -2
drivers/iio/accel/bma180.c
··· 620 620 #ifdef CONFIG_PM_SLEEP 621 621 static int bma180_suspend(struct device *dev) 622 622 { 623 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 623 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 624 624 struct bma180_data *data = iio_priv(indio_dev); 625 625 int ret; 626 626 ··· 633 633 634 634 static int bma180_resume(struct device *dev) 635 635 { 636 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 636 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 637 637 struct bma180_data *data = iio_priv(indio_dev); 638 638 int ret; 639 639
+6 -5
drivers/iio/adc/at91_adc.c
··· 556 556 557 557 static int at91_adc_probe(struct platform_device *pdev) 558 558 { 559 - unsigned int prsc, mstrclk, ticks, adc_clk, shtim; 559 + unsigned int prsc, mstrclk, ticks, adc_clk, adc_clk_khz, shtim; 560 560 int ret; 561 561 struct iio_dev *idev; 562 562 struct at91_adc_state *st; ··· 649 649 */ 650 650 mstrclk = clk_get_rate(st->clk); 651 651 adc_clk = clk_get_rate(st->adc_clk); 652 + adc_clk_khz = adc_clk / 1000; 652 653 prsc = (mstrclk / (2 * adc_clk)) - 1; 653 654 654 655 if (!st->startup_time) { ··· 663 662 * defined in the electrical characteristics of the board, divided by 8. 664 663 * The formula thus is : Startup Time = (ticks + 1) * 8 / ADC Clock 665 664 */ 666 - ticks = round_up((st->startup_time * adc_clk / 667 - 1000000) - 1, 8) / 8; 665 + ticks = round_up((st->startup_time * adc_clk_khz / 666 + 1000) - 1, 8) / 8; 668 667 /* 669 668 * a minimal Sample and Hold Time is necessary for the ADC to guarantee 670 669 * the best converted final value between two channels selection 671 670 * The formula thus is : Sample and Hold Time = (shtim + 1) / ADCClock 672 671 */ 673 - shtim = round_up((st->sample_hold_time * adc_clk / 674 - 1000000) - 1, 1); 672 + shtim = round_up((st->sample_hold_time * adc_clk_khz / 673 + 1000) - 1, 1); 675 674 676 675 reg = AT91_ADC_PRESCAL_(prsc) & st->registers->mr_prescal_mask; 677 676 reg |= AT91_ADC_STARTUP_(ticks) & st->registers->mr_startup_mask;
+2
drivers/iio/buffer_cb.c
··· 41 41 goto error_ret; 42 42 } 43 43 44 + iio_buffer_init(&cb_buff->buffer); 45 + 44 46 cb_buff->private = private; 45 47 cb_buff->cb = cb; 46 48 cb_buff->buffer.access = &iio_cb_access;
+6 -6
drivers/iio/dac/mcp4725.c
··· 37 37 38 38 static int mcp4725_suspend(struct device *dev) 39 39 { 40 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 41 - struct mcp4725_data *data = iio_priv(indio_dev); 40 + struct mcp4725_data *data = iio_priv(i2c_get_clientdata( 41 + to_i2c_client(dev))); 42 42 u8 outbuf[2]; 43 43 44 44 outbuf[0] = (data->powerdown_mode + 1) << 4; 45 45 outbuf[1] = 0; 46 46 data->powerdown = true; 47 47 48 - return i2c_master_send(to_i2c_client(dev), outbuf, 2); 48 + return i2c_master_send(data->client, outbuf, 2); 49 49 } 50 50 51 51 static int mcp4725_resume(struct device *dev) 52 52 { 53 - struct iio_dev *indio_dev = dev_to_iio_dev(dev); 54 - struct mcp4725_data *data = iio_priv(indio_dev); 53 + struct mcp4725_data *data = iio_priv(i2c_get_clientdata( 54 + to_i2c_client(dev))); 55 55 u8 outbuf[2]; 56 56 57 57 /* restore previous DAC value */ ··· 59 59 outbuf[1] = data->dac_value & 0xff; 60 60 data->powerdown = false; 61 61 62 - return i2c_master_send(to_i2c_client(dev), outbuf, 2); 62 + return i2c_master_send(data->client, outbuf, 2); 63 63 } 64 64 65 65 #ifdef CONFIG_PM_SLEEP
+4
drivers/iio/iio_core.h
··· 49 49 #define iio_buffer_poll_addr (&iio_buffer_poll) 50 50 #define iio_buffer_read_first_n_outer_addr (&iio_buffer_read_first_n_outer) 51 51 52 + void iio_disable_all_buffers(struct iio_dev *indio_dev); 53 + 52 54 #else 53 55 54 56 #define iio_buffer_poll_addr NULL 55 57 #define iio_buffer_read_first_n_outer_addr NULL 58 + 59 + static inline void iio_disable_all_buffers(struct iio_dev *indio_dev) {} 56 60 57 61 #endif 58 62
+28 -2
drivers/iio/industrialio-buffer.c
··· 460 460 return bytes; 461 461 } 462 462 463 + void iio_disable_all_buffers(struct iio_dev *indio_dev) 464 + { 465 + struct iio_buffer *buffer, *_buffer; 466 + 467 + if (list_empty(&indio_dev->buffer_list)) 468 + return; 469 + 470 + if (indio_dev->setup_ops->predisable) 471 + indio_dev->setup_ops->predisable(indio_dev); 472 + 473 + list_for_each_entry_safe(buffer, _buffer, 474 + &indio_dev->buffer_list, buffer_list) 475 + list_del_init(&buffer->buffer_list); 476 + 477 + indio_dev->currentmode = INDIO_DIRECT_MODE; 478 + if (indio_dev->setup_ops->postdisable) 479 + indio_dev->setup_ops->postdisable(indio_dev); 480 + } 481 + 463 482 int iio_update_buffers(struct iio_dev *indio_dev, 464 483 struct iio_buffer *insert_buffer, 465 484 struct iio_buffer *remove_buffer) ··· 547 528 * Note can only occur when adding a buffer. 548 529 */ 549 530 list_del(&insert_buffer->buffer_list); 550 - indio_dev->active_scan_mask = old_mask; 551 - success = -EINVAL; 531 + if (old_mask) { 532 + indio_dev->active_scan_mask = old_mask; 533 + success = -EINVAL; 534 + } 535 + else { 536 + kfree(compound_mask); 537 + ret = -EINVAL; 538 + goto error_ret; 539 + } 552 540 } 553 541 } else { 554 542 indio_dev->active_scan_mask = compound_mask;
+21 -10
drivers/iio/industrialio-core.c
··· 848 848 static void iio_dev_release(struct device *device) 849 849 { 850 850 struct iio_dev *indio_dev = dev_to_iio_dev(device); 851 - if (indio_dev->chrdev.dev) 852 - cdev_del(&indio_dev->chrdev); 853 851 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED) 854 852 iio_device_unregister_trigger_consumer(indio_dev); 855 853 iio_device_unregister_eventset(indio_dev); ··· 968 970 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags)) 969 971 return -EBUSY; 970 972 973 + iio_device_get(indio_dev); 974 + 971 975 filp->private_data = indio_dev; 972 976 973 977 return 0; ··· 983 983 struct iio_dev *indio_dev = container_of(inode->i_cdev, 984 984 struct iio_dev, chrdev); 985 985 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags); 986 + iio_device_put(indio_dev); 987 + 986 988 return 0; 987 989 } 988 990 ··· 1054 1052 indio_dev->setup_ops == NULL) 1055 1053 indio_dev->setup_ops = &noop_ring_setup_ops; 1056 1054 1057 - ret = device_add(&indio_dev->dev); 1058 - if (ret < 0) 1059 - goto error_unreg_eventset; 1060 1055 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops); 1061 1056 indio_dev->chrdev.owner = indio_dev->info->driver_module; 1057 + indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj; 1062 1058 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1); 1063 1059 if (ret < 0) 1064 - goto error_del_device; 1065 - return 0; 1060 + goto error_unreg_eventset; 1066 1061 1067 - error_del_device: 1068 - device_del(&indio_dev->dev); 1062 + ret = device_add(&indio_dev->dev); 1063 + if (ret < 0) 1064 + goto error_cdev_del; 1065 + 1066 + return 0; 1067 + error_cdev_del: 1068 + cdev_del(&indio_dev->chrdev); 1069 1069 error_unreg_eventset: 1070 1070 iio_device_unregister_eventset(indio_dev); 1071 1071 error_free_sysfs: ··· 1082 1078 void iio_device_unregister(struct iio_dev *indio_dev) 1083 1079 { 1084 1080 mutex_lock(&indio_dev->info_exist_lock); 1081 + 1082 + device_del(&indio_dev->dev); 1083 + 1084 + if (indio_dev->chrdev.dev) 1085 + cdev_del(&indio_dev->chrdev); 1086 + 1087 + iio_disable_all_buffers(indio_dev); 1088 + 1085 1089 indio_dev->info = NULL; 1086 1090 mutex_unlock(&indio_dev->info_exist_lock); 1087 - device_del(&indio_dev->dev); 1088 1091 } 1089 1092 EXPORT_SYMBOL(iio_device_unregister); 1090 1093 subsys_initcall(iio_init);
+14 -6
drivers/iio/industrialio-event.c
··· 72 72 static unsigned int iio_event_poll(struct file *filep, 73 73 struct poll_table_struct *wait) 74 74 { 75 - struct iio_event_interface *ev_int = filep->private_data; 75 + struct iio_dev *indio_dev = filep->private_data; 76 + struct iio_event_interface *ev_int = indio_dev->event_interface; 76 77 unsigned int events = 0; 77 78 78 79 poll_wait(filep, &ev_int->wait, wait); ··· 91 90 size_t count, 92 91 loff_t *f_ps) 93 92 { 94 - struct iio_event_interface *ev_int = filep->private_data; 93 + struct iio_dev *indio_dev = filep->private_data; 94 + struct iio_event_interface *ev_int = indio_dev->event_interface; 95 95 unsigned int copied; 96 96 int ret; 97 97 ··· 123 121 124 122 static int iio_event_chrdev_release(struct inode *inode, struct file *filep) 125 123 { 126 - struct iio_event_interface *ev_int = filep->private_data; 124 + struct iio_dev *indio_dev = filep->private_data; 125 + struct iio_event_interface *ev_int = indio_dev->event_interface; 127 126 128 127 spin_lock_irq(&ev_int->wait.lock); 129 128 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); ··· 135 132 */ 136 133 kfifo_reset_out(&ev_int->det_events); 137 134 spin_unlock_irq(&ev_int->wait.lock); 135 + 136 + iio_device_put(indio_dev); 138 137 139 138 return 0; 140 139 } ··· 163 158 return -EBUSY; 164 159 } 165 160 spin_unlock_irq(&ev_int->wait.lock); 166 - fd = anon_inode_getfd("iio:event", 167 - &iio_event_chrdev_fileops, ev_int, O_RDONLY); 161 + iio_device_get(indio_dev); 162 + 163 + fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops, 164 + indio_dev, O_RDONLY); 168 165 if (fd < 0) { 169 166 spin_lock_irq(&ev_int->wait.lock); 170 167 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); 171 168 spin_unlock_irq(&ev_int->wait.lock); 169 + iio_device_put(indio_dev); 172 170 } 173 171 return fd; 174 172 } ··· 284 276 goto error_ret; 285 277 } 286 278 if (chan->modified) 287 - mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel, 279 + mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2, 288 280 i/IIO_EV_DIR_MAX, 289 281 i%IIO_EV_DIR_MAX); 290 282 else if (chan->differential)
+4 -2
drivers/iio/temperature/tmp006.c
··· 255 255 #ifdef CONFIG_PM_SLEEP 256 256 static int tmp006_suspend(struct device *dev) 257 257 { 258 - return tmp006_powerdown(iio_priv(dev_to_iio_dev(dev))); 258 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 259 + return tmp006_powerdown(iio_priv(indio_dev)); 259 260 } 260 261 261 262 static int tmp006_resume(struct device *dev) 262 263 { 263 - struct tmp006_data *data = iio_priv(dev_to_iio_dev(dev)); 264 + struct tmp006_data *data = iio_priv(i2c_get_clientdata( 265 + to_i2c_client(dev))); 264 266 return i2c_smbus_write_word_swapped(data->client, TMP006_CONFIG, 265 267 data->config | TMP006_CONFIG_MOD_MASK); 266 268 }
+1 -1
drivers/staging/iio/Kconfig
··· 37 37 38 38 config IIO_SIMPLE_DUMMY_BUFFER 39 39 boolean "Buffered capture support" 40 - depends on IIO_KFIFO_BUF 40 + select IIO_KFIFO_BUF 41 41 help 42 42 Add buffered data capture to the simple dummy driver. 43 43
+1
drivers/staging/iio/light/isl29018.c
··· 563 563 mutex_init(&chip->lock); 564 564 565 565 chip->lux_scale = 1; 566 + chip->lux_uscale = 0; 566 567 chip->range = 1000; 567 568 chip->adc_bit = 16; 568 569 chip->suspended = false;
+1 -1
drivers/staging/iio/magnetometer/hmc5843.c
··· 229 229 if (result < 0) 230 230 return -EINVAL; 231 231 232 - *val = result; 232 + *val = sign_extend32(result, 15); 233 233 return IIO_VAL_INT; 234 234 } 235 235
+1 -1
drivers/staging/iio/meter/ade7854-spi.c
··· 299 299 if (ret) 300 300 iio_device_free(indio_dev); 301 301 302 - return 0; 302 + return ret; 303 303 } 304 304 305 305 static int ade7854_spi_remove(struct spi_device *spi)