[svn-commits] sruffell: linux/trunk r9381 - /linux/trunk/drivers/dahdi/dahdi-base.c

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Sep 20 15:34:27 CDT 2010


Author: sruffell
Date: Mon Sep 20 15:34:23 2010
New Revision: 9381

URL: http://svnview.digium.com/svn/dahdi?view=rev&rev=9381
Log:
dahdi: Reduce 'chans' references in dahdi_ctl_ioctl.

Review: https://reviewboard.asterisk.org/r/905/

Signed-off-by: Shaun Ruffell <sruffell at digium.com>

Modified:
    linux/trunk/drivers/dahdi/dahdi-base.c

Modified: linux/trunk/drivers/dahdi/dahdi-base.c
URL: http://svnview.digium.com/svn/dahdi/linux/trunk/drivers/dahdi/dahdi-base.c?view=diff&rev=9381&r1=9380&r2=9381
==============================================================================
--- linux/trunk/drivers/dahdi/dahdi-base.c (original)
+++ linux/trunk/drivers/dahdi/dahdi-base.c Mon Sep 20 15:34:23 2010
@@ -415,23 +415,21 @@
 			false : true;
 }
 
-#define VALID_CHANNEL(j) do { \
-	if (!valid_channo(j)) \
-		return -EINVAL; \
-	if (!chans[j]) \
-		return -ENXIO; \
-} while (0)
-
 /* Protected by chan_lock. */
 static struct dahdi_span *spans[DAHDI_MAX_SPANS];
 static struct dahdi_chan *chans[DAHDI_MAX_CHANNELS];
 
 static int maxspans = 0;
 
-static struct dahdi_chan *chan_from_file(struct file *file)
-{
-	return (file->private_data) ? file->private_data :
-			((valid_channo(UNIT(file))) ? chans[UNIT(file)] : NULL);
+static inline struct dahdi_chan *chan_from_num(unsigned int channo)
+{
+	return valid_channo(channo) ? chans[channo] : NULL;
+}
+
+static inline struct dahdi_chan *chan_from_file(struct file *file)
+{
+	return (file->private_data) ?
+			file->private_data : chan_from_num(UNIT(file));
 }
 
 static struct dahdi_span *find_span(int spanno)
@@ -941,7 +939,7 @@
 		return;
 
 	for (y = 0; y < maxchans; y++) {
-		struct dahdi_chan *const chan = chans[y];
+		struct dahdi_chan *const chan = chan_from_num(y);
 		const int confmode = chan->confmode & DAHDI_CONF_MODE_MASK;
 		if (chan && (chan->confna == x) &&
 		    (confmode == DAHDI_CONF_CONF ||
@@ -2062,7 +2060,7 @@
 #endif
 	maxchans = 0;
 	for (x = 1; x < DAHDI_MAX_CHANNELS; x++) {
-		struct dahdi_chan *const pos = chans[x];
+		struct dahdi_chan *const pos = chan_from_num(x);
 		if (!pos)
 			continue;
 		maxchans = x + 1;
@@ -3663,14 +3661,8 @@
 		res = -EFAULT;
 		goto cleanup;
 	}
-	if (!gain->chan) {
-		chan = chan_from_file(file);
-	} else {
-		if (valid_channo(gain->chan))
-			chan = chans[gain->chan];
-		else
-			chan = NULL;
-	}
+	chan = (!gain->chan) ? chan_from_file(file) :
+			       chan_from_num(gain->chan);
 	if (chan) {
 		res = -EINVAL;
 		goto cleanup;
@@ -3715,15 +3707,8 @@
 		goto cleanup;
 	}
 
-	if (!gain->chan) {
-		chan = chan_from_file(file);
-	} else {
-		if (valid_channo(gain->chan))
-			chan = chans[gain->chan];
-		else
-			chan = NULL;
-	}
-
+	chan = (!gain->chan) ? chan_from_file(file) :
+			       chan_from_num(gain->chan);
 	if (!chan) {
 		res = -EINVAL;
 		goto cleanup;
@@ -3793,8 +3778,7 @@
 	/* get channel number from user */
 	get_user(channo, (int __user *)data);
 
-	VALID_CHANNEL(channo);
-	chan = chans[channo];
+	chan = chan_from_num(channo);
 	if (!chan)
 		return -EINVAL;
 
@@ -3868,14 +3852,10 @@
 	}
 
 	/* Pick the right channo's */
-	chan = file->private_data;
-	if (!param.channo || chan) {
-		param.channo = chan->channo;
-	} else {
-		/* Check validity of channel */
-		VALID_CHANNEL(param.channo);
-		chan = chans[param.channo];
-	}
+	chan = chan_from_file(file);
+	if (!chan)
+		chan = chan_from_num(param.channo);
+
 	if (!chan)
 		return -EINVAL;
 
@@ -3968,14 +3948,9 @@
 	param.chan_alarms = 0; /* be explicit about the above */
 
 	/* Pick the right channo's */
-	chan = file->private_data;
-	if (!param.channo || chan) {
-		param.channo = chan->channo;
-	} else {
-		/* Check validity of channel */
-		VALID_CHANNEL(param.channo);
-		chan = chans[param.channo];
-	}
+	chan = chan_from_file(file);
+	if (!chan)
+		chan = chan_from_num(param.channo);
 
 	if (!chan)
 		return -EINVAL;
@@ -4266,25 +4241,23 @@
 
 	if (copy_from_user(&ch, (void __user *)data, sizeof(ch)))
 		return -EFAULT;
-	VALID_CHANNEL(ch.chan);
+
+	chan = chan_from_num(ch.chan);
+	if (!chan)
+		return -EINVAL;
+
 	if (ch.sigtype == DAHDI_SIG_SLAVE) {
-		/* We have to use the master's sigtype */
-		if ((ch.master < 1) || (ch.master >= DAHDI_MAX_CHANNELS))
+		newmaster = chan_from_num(ch.master);
+		if (!newmaster)
 			return -EINVAL;
-		if (!chans[ch.master])
-			return -EINVAL;
-		ch.sigtype = chans[ch.master]->sig;
-		newmaster = chans[ch.master];
+		ch.sigtype = newmaster->sig;
 	} else if ((ch.sigtype & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS) {
-		newmaster = chans[ch.chan];
-		if ((ch.idlebits < 1) || (ch.idlebits >= DAHDI_MAX_CHANNELS))
-			return -EINVAL;
-		if (!chans[ch.idlebits])
+		newmaster = chan;
+		if (!chan_from_num(ch.idlebits))
 			return -EINVAL;
 	} else {
-		newmaster = chans[ch.chan];
-	}
-	chan = chans[ch.chan];
+		newmaster = chan;
+	}
 	spin_lock_irqsave(&chan->lock, flags);
 #ifdef CONFIG_DAHDI_NET
 	if (chan->flags & DAHDI_FLAG_NETDEV) {
@@ -4360,8 +4333,7 @@
 			/* Setup conference properly */
 			chan->confmode = DAHDI_CONF_DIGITALMON;
 			chan->confna = ch.idlebits;
-			res = dahdi_chan_dacs(chan,
-					      chans[ch.idlebits]);
+			res = dahdi_chan_dacs(chan, chan_from_num(ch.idlebits));
 		} else {
 			dahdi_disable_dacs(chan);
 		}
@@ -4477,8 +4449,7 @@
 		if (copy_from_user(&ind, (void __user *)data, sizeof(ind)))
 			return -EFAULT;
 
-		VALID_CHANNEL(ind.chan);
-		chan = chans[ind.chan];
+		chan = chan_from_num(ind.chan);
 		if (!chan)
 			return -EINVAL;
 
@@ -4579,7 +4550,9 @@
 		if (copy_from_user(&ae, (void __user *)data, sizeof(ae)))
 			return -EFAULT;
 
-		VALID_CHANNEL(ae.chan);
+		chan = chan_from_num(ae.chan);
+		if (!chan)
+			return -EINVAL;
 
 		ae.echocan[sizeof(ae.echocan) - 1] = 0;
 		if (ae.echocan[0]) {
@@ -4588,10 +4561,10 @@
 			}
 		}
 
-		spin_lock_irqsave(&chans[ae.chan]->lock, flags);
-		old = chans[ae.chan]->ec_factory;
-		chans[ae.chan]->ec_factory = new;
-		spin_unlock_irqrestore(&chans[ae.chan]->lock, flags);
+		spin_lock_irqsave(&chan->lock, flags);
+		old = chan->ec_factory;
+		chan->ec_factory = new;
+		spin_unlock_irqrestore(&chan->lock, flags);
 
 		if (old) {
 			release_echocan(old);
@@ -4608,29 +4581,34 @@
 
 		if (copy_from_user(&sf, (void __user *)data, sizeof(sf)))
 			return -EFAULT;
-		VALID_CHANNEL(sf.chan);
-		if (chans[sf.chan]->sig != DAHDI_SIG_SF) return -EINVAL;
-		spin_lock_irqsave(&chans[sf.chan]->lock, flags);
-		chans[sf.chan]->rxp1 = sf.rxp1;
-		chans[sf.chan]->rxp2 = sf.rxp2;
-		chans[sf.chan]->rxp3 = sf.rxp3;
-		chans[sf.chan]->txtone = sf.txtone;
-		chans[sf.chan]->tx_v2 = sf.tx_v2;
-		chans[sf.chan]->tx_v3 = sf.tx_v3;
-		chans[sf.chan]->toneflags = sf.toneflag;
+		chan = chan_from_num(sf.chan);
+		if (!chan)
+			return -EINVAL;
+
+		if (chan->sig != DAHDI_SIG_SF)
+			return -EINVAL;
+
+		spin_lock_irqsave(&chan->lock, flags);
+		chan->rxp1 = sf.rxp1;
+		chan->rxp2 = sf.rxp2;
+		chan->rxp3 = sf.rxp3;
+		chan->txtone = sf.txtone;
+		chan->tx_v2 = sf.tx_v2;
+		chan->tx_v3 = sf.tx_v3;
+		chan->toneflags = sf.toneflag;
 		if (sf.txtone) /* if set to make tone for tx */
 		{
-			if ((chans[sf.chan]->txhooksig && !(sf.toneflag & DAHDI_REVERSE_TXTONE)) ||
-			 ((!chans[sf.chan]->txhooksig) && (sf.toneflag & DAHDI_REVERSE_TXTONE)))
+			if ((chan->txhooksig && !(sf.toneflag & DAHDI_REVERSE_TXTONE)) ||
+			 ((!chan->txhooksig) && (sf.toneflag & DAHDI_REVERSE_TXTONE)))
 			{
-				set_txtone(chans[sf.chan],sf.txtone,sf.tx_v2,sf.tx_v3);
+				set_txtone(chan, sf.txtone, sf.tx_v2, sf.tx_v3);
 			}
 			else
 			{
-				set_txtone(chans[sf.chan],0,0,0);
+				set_txtone(chan, 0, 0, 0);
 			}
 		}
-		spin_unlock_irqrestore(&chans[sf.chan]->lock, flags);
+		spin_unlock_irqrestore(&chan->lock, flags);
 		return res;
 	}
 	case DAHDI_DEFAULTZONE:
@@ -4891,6 +4869,7 @@
 {
 	struct dahdi_confinfo conf;
 	struct dahdi_chan *chan;
+	struct dahdi_chan *conf_chan = NULL;
 	unsigned long flags;
 	unsigned int confmode;
 	int j;
@@ -4900,25 +4879,17 @@
 
 	confmode = conf.confmode & DAHDI_CONF_MODE_MASK;
 
-	if (conf.chan) {
-		VALID_CHANNEL(conf.chan);
-		chan = chans[conf.chan];
-	} else {
-		if (file->private_data) {
-			chan = file->private_data;
-		} else {
-			VALID_CHANNEL(UNIT(file));
-			chan = chans[UNIT(file)];
-		}
-	}
+	chan = (conf.chan) ? chan_from_num(conf.chan) :
+			     chan_from_file(file);
+	if (!chan)
+		return -EINVAL;
 
 	if (!(chan->flags & DAHDI_FLAG_AUDIO))
 		return -EINVAL;
 
 	if (is_monitor_mode(conf.confmode)) {
-		/* Monitor mode -- it's a channel */
-		if ((conf.confno < 0) || (conf.confno >= DAHDI_MAX_CHANNELS) ||
-		    !chans[conf.confno])
+		conf_chan = chan_from_num(conf.confno);
+		if (!conf_chan)
 			return -EINVAL;
 	} else {
 		/* make sure conf number makes sense, too */
@@ -4959,9 +4930,9 @@
 		if ((confmode == DAHDI_CONF_DIGITALMON) &&
 		    (chan->txgain == defgain) &&
 		    (chan->rxgain == defgain) &&
-		    (chans[conf.confno]->txgain == defgain) &&
-		    (chans[conf.confno]->rxgain == defgain)) {
-			dahdi_chan_dacs(chan, chans[conf.confno]);
+		    (conf_chan->txgain == defgain) &&
+		    (conf_chan->rxgain == defgain)) {
+			dahdi_chan_dacs(chan, conf_chan);
 		} else {
 			dahdi_disable_dacs(chan);
 		}
@@ -4977,16 +4948,16 @@
 		chan->_confn = dahdi_get_conf_alias(conf.confno);
 	}
 
-	if (chans[conf.confno]) {
+	if (conf_chan) {
 		if ((confmode == DAHDI_CONF_MONITOR_RX_PREECHO) ||
 		    (confmode == DAHDI_CONF_MONITOR_TX_PREECHO) ||
 		    (confmode == DAHDI_CONF_MONITORBOTH_PREECHO)) {
 			void *temp = kmalloc(sizeof(*chan->readchunkpreec) *
 					     DAHDI_CHUNKSIZE, GFP_ATOMIC);
-			chans[conf.confno]->readchunkpreec = temp;
+			conf_chan->readchunkpreec = temp;
 		} else {
-			kfree(chans[conf.confno]->readchunkpreec);
-			chans[conf.confno]->readchunkpreec = NULL;
+			kfree(conf_chan->readchunkpreec);
+			conf_chan->readchunkpreec = NULL;
 		}
 	}
 
@@ -5005,13 +4976,9 @@
 	int res = 0;
 	int i;
 
-	if (file->private_data) {
-		chan = file->private_data;
-	} else {
-		VALID_CHANNEL(UNIT(file));
-		chan = chans[UNIT(file)];
-	}
-
+	chan = chan_from_file(file);
+	if (!chan)
+		return -EINVAL;
 	if (!(chan->flags & DAHDI_FLAG_AUDIO))
 		return -EINVAL;
 	if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
@@ -5094,12 +5061,9 @@
 	int c;
 	int k;
 
-	if (file->private_data) {
-		chan = file->private_data;
-	} else {
-		VALID_CHANNEL(UNIT(file));
-		chan = chans[UNIT(file)];
-	}
+	chan = chan_from_file(file);
+	if (!chan)
+		return -EINVAL;
 
 	if (!(chan->flags & DAHDI_FLAG_AUDIO))
 		return -EINVAL;
@@ -5110,7 +5074,7 @@
 	for (i = ((j) ? j : 1); i <= ((j) ? j : DAHDI_MAX_CONF); i++) {
 		c = 0;
 		for (k = 1; k < DAHDI_MAX_CHANNELS; k++) {
-			struct dahdi_chan *const pos = chans[k];
+			struct dahdi_chan *const pos = chan_from_num(k);
 			/* skip if no pointer */
 			if (!pos)
 				continue;
@@ -5156,18 +5120,8 @@
 	if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
 		return -EFAULT;
 
-	if (!conf.chan) {
-		/* If 0, use the current channel. */
-		if (file->private_data) {
-			chan = file->private_data;
-		} else {
-			VALID_CHANNEL(UNIT(file));
-			chan = chans[UNIT(file)];
-		}
-	} else {
-		VALID_CHANNEL(conf.chan);
-		chan = chans[conf.chan];
-	}
+	chan = (!conf.chan) ? chan_from_file(file) :
+			      chan_from_num(conf.chan);
 	if (!chan)
 		return -EINVAL;
 
@@ -6088,11 +6042,9 @@
 	switch(cmd) {
 	case DAHDI_SPECIFY:
 		get_user(channo, (int __user *)data);
-		if (channo < 1)
+		file->private_data = chan_from_num(channo);
+		if (!file->private_data)
 			return -EINVAL;
-		if (channo > DAHDI_MAX_CHANNELS)
-			return -EINVAL;
-		file->private_data = chans[channo];
 		res = dahdi_specchan_open(file);
 		if (res)
 			file->private_data = NULL;




More information about the svn-commits mailing list