[PATCH] staging: comedi: simplify comedi_set_hw_dev()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Since `get_device()` and `put_device()` can take a NULL device
parameter, `comedi_set_hw_dev()` can be simplified to always call
`get_device()` for the new, possibly NULL hardware device, and
`put_device()` for the old, possibly NULL hardware device.  As long as
we do it in that order, there shouldn't be any problem with
`kref_release()` getting called unexpectedly when the new hardware
device is the old hardware device.

Simplify `comedi_set_hw_dev()` and update the comment because the
function is used for additional purposes since the old comment was
written.

Signed-off-by: Ian Abbott <abbotti@xxxxxxxxx>
---
 drivers/staging/comedi/comedidev.h | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/drivers/staging/comedi/comedidev.h b/drivers/staging/comedi/comedidev.h
index e2b7d60..503dd8c 100644
--- a/drivers/staging/comedi/comedidev.h
+++ b/drivers/staging/comedi/comedidev.h
@@ -429,20 +429,19 @@ static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
 		return sizeof(short);
 }
 
-/* must be used in attach to set dev->hw_dev if you wish to dma directly
-into comedi's buffer */
+/*
+ * Must set dev->hw_dev if you wish to dma directly into comedi's buffer.
+ * Also useful for retrieving a previously configured hardware device of
+ * known bus type.  Set automatically for auto-configured devices.
+ * Automatically set to NULL when detaching hardware device.
+ */
 static inline void comedi_set_hw_dev(struct comedi_device *dev,
 				     struct device *hw_dev)
 {
-	if (dev->hw_dev == hw_dev)
-		return;
-	if (dev->hw_dev)
-		put_device(dev->hw_dev);
-	dev->hw_dev = hw_dev;
-	if (dev->hw_dev) {
-		dev->hw_dev = get_device(dev->hw_dev);
-		BUG_ON(dev->hw_dev == NULL);
-	}
+	struct device *old_hw_dev = dev->hw_dev;
+
+	dev->hw_dev = get_device(hw_dev);
+	put_device(old_hw_dev);
 }
 
 static inline struct pci_dev *comedi_to_pci_dev(struct comedi_device *dev)
-- 
1.8.1.1

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/devel


[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux