From 3ea07279d4ad687a4a555f309459db680081e7b8 Mon Sep 17 00:00:00 2001 From: Madhusudhan Chikkature Date: Thu, 25 Sep 2008 12:28:22 +0530 Subject: [PATCH] HDQ driver:protect the shared flag This patch moves the shared variable into the local structure and protects its updation. Signed-off-by: Madhusudhan Chikkature Acked-by: Evgeniy Polyakov Acked-by: Felipe Balbi Signed-off-by: Tony Lindgren --- drivers/w1/masters/omap_hdq.c | 52 +++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/drivers/w1/masters/omap_hdq.c b/drivers/w1/masters/omap_hdq.c index 85ce7ec981d..34129e7066e 100644 --- a/drivers/w1/masters/omap_hdq.c +++ b/drivers/w1/masters/omap_hdq.c @@ -61,6 +61,12 @@ struct hdq_data { struct clk *hdq_fck; u8 hdq_irqstatus; spinlock_t hdq_spinlock; + /* + * Used to control the call to omap_hdq_get and omap_hdq_put. + * HDQ Protocol: Write the CMD|REG_address first, followed by + * the data wrire or read. + */ + int init_trans; }; static int omap_hdq_get(struct hdq_data *hdq_data); @@ -504,13 +510,6 @@ omap_hdq_put(struct hdq_data *hdq_data) return ret; } -/* - * Used to control the call to omap_hdq_get and omap_hdq_put. - * HDQ Protocol: Write the CMD|REG_address first, followed by - * the data wrire or read. - */ -static int init_trans; - /* * Read a byte of data from the device. */ @@ -522,14 +521,26 @@ static u8 omap_w1_read_byte(void *_hdq) ret = hdq_read_byte(hdq_data, &val); if (ret) { - init_trans = 0; + ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); + if (ret < 0) { + dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); + return -EINTR; + } + hdq_data->init_trans = 0; + mutex_unlock(&hdq_data->hdq_mutex); omap_hdq_put(hdq_data); return -1; } /* Write followed by a read, release the module */ - if (init_trans) { - init_trans = 0; + if (hdq_data->init_trans) { + ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); + if (ret < 0) { + dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); + return -EINTR; + } + hdq_data->init_trans = 0; + mutex_unlock(&hdq_data->hdq_mutex); omap_hdq_put(hdq_data); } @@ -542,21 +553,34 @@ static u8 omap_w1_read_byte(void *_hdq) static void omap_w1_write_byte(void *_hdq, u8 byte) { struct hdq_data *hdq_data = _hdq; + int ret; u8 status; /* First write to initialize the transfer */ - if (init_trans == 0) + if (hdq_data->init_trans == 0) omap_hdq_get(hdq_data); - init_trans++; + ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); + if (ret < 0) { + dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); + return; + } + hdq_data->init_trans++; + mutex_unlock(&hdq_data->hdq_mutex); hdq_write_byte(hdq_data, byte, &status); dev_dbg(hdq_data->dev, "Ctrl status %x\n", status); /* Second write, data transfered. Release the module */ - if (init_trans > 1) { + if (hdq_data->init_trans > 1) { omap_hdq_put(hdq_data); - init_trans = 0; + ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); + if (ret < 0) { + dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); + return; + } + hdq_data->init_trans = 0; + mutex_unlock(&hdq_data->hdq_mutex); } return; -- 2.41.0