public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: "David Härdeman" <david@hardeman.nu>
To: linux-media@vger.kernel.org
Cc: jarod@wilsonet.com, mchehab@infradead.org
Subject: [PATCH 05/10] saa7134: protect the ir user count
Date: Sat, 20 Nov 2010 00:43:02 +0100	[thread overview]
Message-ID: <20101119234302.3511.97440.stgit@localhost.localdomain> (raw)
In-Reply-To: <20101119233959.3511.91287.stgit@localhost.localdomain>

The use of the saa7134_card_ir->users seems racy, so protect the count with
a spinlock. Also use proper data types as arguments to __saa7134_ir_start()
and __saa7134_ir_stop() to remove some unnecessary casts.

Signed-off-by: David Härdeman <david@hardeman.nu>
---
 drivers/media/video/saa7134/saa7134-input.c |   54 ++++++++++++++++-----------
 drivers/media/video/saa7134/saa7134.h       |    1 +
 2 files changed, 33 insertions(+), 22 deletions(-)

diff --git a/drivers/media/video/saa7134/saa7134-input.c b/drivers/media/video/saa7134/saa7134-input.c
index 98678d9..8b80efb 100644
--- a/drivers/media/video/saa7134/saa7134-input.c
+++ b/drivers/media/video/saa7134/saa7134-input.c
@@ -402,17 +402,12 @@ static void ir_raw_decode_timer_end(unsigned long data)
 	ir->active = false;
 }
 
-static int __saa7134_ir_start(void *priv)
+static void __saa7134_ir_start(struct saa7134_dev *dev)
 {
-	struct saa7134_dev *dev = priv;
-	struct saa7134_card_ir *ir;
-
-	if (!dev || !dev->remote)
-		return -EINVAL;
+	struct saa7134_card_ir *ir = dev->remote;
 
-	ir  = dev->remote;
 	if (ir->running)
-		return 0;
+		return;
 
 	ir->running = true;
 	ir->active = false;
@@ -427,19 +422,12 @@ static int __saa7134_ir_start(void *priv)
 		setup_timer(&ir->timer, ir_raw_decode_timer_end,
 			    (unsigned long)dev);
 	}
-
-	return 0;
 }
 
-static void __saa7134_ir_stop(void *priv)
+static void __saa7134_ir_stop(struct saa7134_dev *dev)
 {
-	struct saa7134_dev *dev = priv;
-	struct saa7134_card_ir *ir;
-
-	if (!dev || !dev->remote)
-		return;
+	struct saa7134_card_ir *ir = dev->remote;
 
-	ir  = dev->remote;
 	if (!ir->running)
 		return;
 
@@ -448,39 +436,60 @@ static void __saa7134_ir_stop(void *priv)
 
 	ir->active = false;
 	ir->running = false;
-
-	return;
 }
 
 int saa7134_ir_start(struct saa7134_dev *dev)
 {
+	if (!dev || !dev->remote)
+		return -EINVAL;
+
+	spin_lock(&dev->remote->lock);
 	if (dev->remote->users)
-		return __saa7134_ir_start(dev);
+		__saa7134_ir_start(dev);
+	spin_unlock(&dev->remote->lock);
 
 	return 0;
 }
 
 void saa7134_ir_stop(struct saa7134_dev *dev)
 {
+	if (!dev || !dev->remote)
+		return;
+
+	spin_lock(&dev->remote->lock);
 	if (dev->remote->users)
 		__saa7134_ir_stop(dev);
+	spin_unlock(&dev->remote->lock);
 }
 
 static int saa7134_ir_open(struct rc_dev *rc)
 {
 	struct saa7134_dev *dev = rc->priv;
 
+	if (!dev || !dev->remote)
+		return -EINVAL;
+
+	spin_lock(&dev->remote->lock);
+	if (dev->remote->users == 0)
+		__saa7134_ir_start(dev);
 	dev->remote->users++;
-	return __saa7134_ir_start(dev);
+	spin_unlock(&dev->remote->lock);
+
+	return 0;
 }
 
 static void saa7134_ir_close(struct rc_dev *rc)
 {
 	struct saa7134_dev *dev = rc->priv;
 
+	if (!dev || !dev->remote)
+		return;
+
+	spin_lock(&dev->remote->lock);
 	dev->remote->users--;
-	if (!dev->remote->users)
+	if (dev->remote->users == 0)
 		__saa7134_ir_stop(dev);
+	spin_unlock(&dev->remote->lock);
 }
 
 int saa7134_input_init1(struct saa7134_dev *dev)
@@ -744,6 +753,7 @@ int saa7134_input_init1(struct saa7134_dev *dev)
 	ir->mask_keyup   = mask_keyup;
 	ir->polling      = polling;
 	ir->raw_decode	 = raw_decode;
+	spin_lock_init(&ir->lock);
 
 	/* init input device */
 	snprintf(ir->name, sizeof(ir->name), "saa7134 IR (%s)",
diff --git a/drivers/media/video/saa7134/saa7134.h b/drivers/media/video/saa7134/saa7134.h
index babfbe7..7c836c3 100644
--- a/drivers/media/video/saa7134/saa7134.h
+++ b/drivers/media/video/saa7134/saa7134.h
@@ -125,6 +125,7 @@ struct saa7134_card_ir {
 	char                    name[32];
 	char                    phys[32];
 	unsigned                users;
+	spinlock_t              lock;
 
 	u32			polling;
         u32			last_gpio;


  parent reply	other threads:[~2010-11-19 23:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-11-19 23:42 [PATCH 00/10] rc-core: various cleanups David Härdeman
2010-11-19 23:42 ` [PATCH 01/10] saa7134: remove unused module parameter David Härdeman
2010-11-19 23:42 ` [PATCH 02/10] saa7134: use full keycode for BeholdTV David Härdeman
2010-11-19 23:42 ` [PATCH 03/10] saa7134: some minor cleanups David Härdeman
2010-11-19 23:42 ` [PATCH 04/10] saa7134: merge saa7134_card_ir->timer and saa7134_card_ir->timer_end David Härdeman
2010-11-19 23:43 ` David Härdeman [this message]
2010-11-19 23:43 ` [PATCH 06/10] saa7134: make module parameters boolean David Härdeman
2010-11-20  6:10   ` hermann pitton
2010-11-19 23:43 ` [PATCH 07/10] bttv: rename struct card_ir to bttv_ir David Härdeman
2010-11-19 23:43 ` [PATCH 08/10] bttv: merge ir decoding timers David Härdeman
2010-11-19 23:43 ` [PATCH 09/10] mceusb: int to bool conversion David Härdeman
2010-11-19 23:43 ` [PATCH 10/10] rc-core: fix some leftovers from the renaming patches David Härdeman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101119234302.3511.97440.stgit@localhost.localdomain \
    --to=david@hardeman.nu \
    --cc=jarod@wilsonet.com \
    --cc=linux-media@vger.kernel.org \
    --cc=mchehab@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox