patch-2.2.0-pre6 linux/drivers/char/radio-zoltrix.c

Next file: linux/drivers/char/tty_io.c
Previous file: linux/drivers/char/n_tty.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.2.0-pre5/linux/drivers/char/radio-zoltrix.c linux/drivers/char/radio-zoltrix.c
@@ -12,6 +12,15 @@
  *  to get fine volume control over the low volume range.
  *
  *  Some code derived from code by Frans Brinkman
+ *
+ * 1999-01-05 - (C. van Schaik)
+ *	      - Changed tuning to 1/160Mhz accuracy
+ *	      - Added stereo support
+ *		(card defaults to stereo)
+ *		(can explicitly force mono on the card)
+ *		(can detect if station is in stereo)
+ *	      - Added unmute function
+ *	      - Reworked ioctl functions
  */
 
 #include <linux/module.h>	/* Modules                        */
@@ -35,6 +44,7 @@
 	int curvol;
 	unsigned long curfreq;
 	int muted;
+	unsigned int stereo;
 };
 
 
@@ -54,61 +64,63 @@
 	}
 }
 
+static int zol_setvol(struct zol_device *dev, int vol)
+{
+	dev->curvol = vol;
+	if (dev->muted)
+		return 0;
+
+	if (vol == 0) {
+		outb(0, io);
+		outb(0, io);
+		inb(io + 3);    /* Zoltrix needs to be read to confirm */
+		return 0;
+	}
+
+	outb(dev->curvol-1, io);
+	sleep_delay(10000);
+	inb(io + 2);
+
+	return 0;
+}
+
 static void zol_mute(struct zol_device *dev)
 {
 	dev->muted = 1;
 	outb(0, io);
 	outb(0, io);
-	inb(io + 3);		/* Zoltrix needs to be read to confirm */
+	inb(io + 3);            /* Zoltrix needs to be read to confirm */
 }
 
-static void zol_on(int vol)
+static void zol_unmute(struct zol_device *dev)
 {
-        int l;
-        outb(vol, io);
-        sleep_delay(10000);
-        l = inb(io + 2);
-}
-
-static int zol_setvol(struct zol_device *dev, int vol)
-{
-	if (vol == dev->curvol) {	/* requested volume = current */
-		if (dev->muted) {	/* user is unmuting the card  */
-			dev->muted = 0;
-                        zol_on(vol);
-		}
-		return 0;
-	}
-	if (vol == 0) {		/* volume = 0 means mute the card */
-                zol_mute(dev);
-		return 0;
-	}
 	dev->muted = 0;
-	dev->curvol = vol;
-
-	zol_on(vol);
-
-	return 0;
+	zol_setvol(dev, dev->curvol);
 }
 
 static int zol_setfreq(struct zol_device *dev, unsigned long freq)
 {
 	/* tunes the radio to the desired frequency */
 	unsigned long long bitmask, f, m;
+	unsigned int stereo = dev->stereo;
 	int i;
 
-	m = (freq * 25 / 4 - 8800) * 2;
+	if (freq == 0)
+		return 1;
+	m = (freq / 160 - 8800) * 2;
 	f = (unsigned long long) m + 0x4d1c;
 
 	bitmask = 0xc480402c10080000ull;
 	i = 45;
 
-	zol_mute(dev);
+	outb(0, io);
+	outb(0, io);
+	inb(io + 3);            /* Zoltrix needs to be read to confirm */
 
 	outb(0x40, io);
 	outb(0xc0, io);
 
-	bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( /*stereo */ 0 << 31));
+	bitmask = (bitmask ^ ((f & 0xff) << 47) ^ ((f & 0xff00) << 30) ^ ( stereo << 31));
 	while (i--) {
 		if ((bitmask & 0x8000000000000000ull) != 0) {
 			outb(0x80, io);
@@ -131,7 +143,18 @@
 	outb(0x80, io);
 	outb(0xc0, io);
 	outb(0x40, io);
-        zol_on(dev->curvol);
+	sleep_delay(1000);
+	inb(io+2);
+
+        sleep_delay(1000);
+	if (dev->muted)
+	{
+		outb(0, io);
+		outb(0, io);
+		inb(io + 3);
+		sleep_delay(1000);
+	} else
+        zol_setvol(dev, dev->curvol);
 	return 0;
 }
 
@@ -149,27 +172,42 @@
 	sleep_delay(1000);
 	b = inb(io);
 
-        if ((a == b) && (a == 0xdf))  /* I found this out by playing */
-                                      /* with a binary scanner on the card io */
-		return (1);
-	else
+	if (a != b)
 		return (0);
 
-	if (inb(io) & 2)	/* bit set = no signal present    */
-		return 0;
-	return 1;		/* signal present               */
+        if ((a == 0xcf) || (a == 0xdf)  /* I found this out by playing */
+		|| (a == 0xef))       /* with a binary scanner on the card io */
+		return (1);
+ 	return (0);
+}
+
+int zol_is_stereo (struct zol_device *dev)
+{
+	int x1, x2;
+
+	outb(0x00, io);
+	outb(dev->curvol, io);
+	sleep_delay(20000);
+
+	x1 = inb(io);
+	sleep_delay(1000);
+	x2 = inb(io);
+
+	if ((x1 == x2) && (x1 == 0xcf))
+		return 1;
+	return 0;
 }
 
 static int zol_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
 {
-	struct zol_device *rt = dev->priv;
+	struct zol_device *zol = dev->priv;
 
 	switch (cmd) {
 	case VIDIOCGCAP:
 		{
 			struct video_capability v;
 			v.type = VID_TYPE_TUNER;
-			v.channels = 1;
+			v.channels = 1 + zol->stereo;
 			v.audios = 1;
 			/* No we don't do pictures */
 			v.maxwidth = 0;
@@ -184,15 +222,21 @@
 	case VIDIOCGTUNER:
 		{
 			struct video_tuner v;
-			if (copy_from_user(&v, arg, sizeof(v)) != 0)
+/*
+			if (copy_from_user(&v, arg, sizeof(v)))
 				return -EFAULT;
-			if (v.tuner)	/* Only 1 tuner */
+			if (v.tuner)	
 				return -EINVAL;
-			v.rangelow = (int) (88.0 * 16);
-			v.rangehigh = (int) (108.0 * 16);
-			v.flags = 0;
+*/
+			v.tuner = 0;
+			strcpy(v.name, "Zoltrix Radio");
+			v.rangelow = (int) (88.0 * 16000);
+			v.rangehigh = (int) (108.0 * 16000);
+			v.flags = zol_is_stereo(zol)
+					? VIDEO_TUNER_STEREO_ON : 0;
+			v.flags |= VIDEO_TUNER_LOW;
 			v.mode = VIDEO_MODE_AUTO;
-			v.signal = 0xFFFF * zol_getsigstr(rt);
+			v.signal = 0xFFFF * zol_getsigstr(zol);
 			if (copy_to_user(arg, &v, sizeof(v)))
 				return -EFAULT;
 			return 0;
@@ -208,22 +252,24 @@
 			return 0;
 		}
 	case VIDIOCGFREQ:
-		if (copy_to_user(arg, &rt->curfreq, sizeof(rt->curfreq)))
+		if (copy_to_user(arg, &zol->curfreq, sizeof(zol->curfreq)))
 			return -EFAULT;
 		return 0;
 	case VIDIOCSFREQ:
-		if (copy_from_user(&rt->curfreq, arg, sizeof(rt->curfreq)))
+		if (copy_from_user(&zol->curfreq, arg, sizeof(zol->curfreq)))
 			return -EFAULT;
-		zol_setfreq(rt, rt->curfreq);
+		zol_setfreq(zol, zol->curfreq);
 		return 0;
 	case VIDIOCGAUDIO:
 		{
 			struct video_audio v;
 			memset(&v, 0, sizeof(v));
 			v.flags |= VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME;
-			v.volume = rt->curvol * 4096;
+			v.mode != zol_is_stereo(zol)
+				? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
+			v.volume = zol->curvol * 4096;
 			v.step = 4096;
-			strcpy(v.name, "Radio");
+			strcpy(v.name, "Zoltrix Radio");
 			if (copy_to_user(arg, &v, sizeof(v)))
 				return -EFAULT;
 			return 0;
@@ -237,9 +283,23 @@
 				return -EINVAL;
 
 			if (v.flags & VIDEO_AUDIO_MUTE)
-				zol_mute(rt);
+				zol_mute(zol);
 			else
-				zol_setvol(rt, v.volume / 4096);
+				zol_unmute(zol);
+
+			if (v.flags & VIDEO_AUDIO_VOLUME)
+				zol_setvol(zol, v.volume / 4096);
+
+			if (v.mode & VIDEO_SOUND_STEREO)
+			{
+				zol->stereo = 1;
+				zol_setfreq(zol, zol->curfreq);
+			}
+			if (v.mode & VIDEO_SOUND_MONO)
+			{
+				zol->stereo = 0;
+				zol_setfreq(zol, zol->curfreq);
+			}
 
 			return 0;
 		}
@@ -286,6 +346,10 @@
 		printk(KERN_ERR "zoltrix: port 0x%x already in use\n", io);
 		return -EBUSY;
 	}
+	if ((io != 0x20c) && (io != 0x30c)) {
+		printk(KERN_ERR "zoltrix: invalid port, try 0x20c or 0x30c\n");
+		return -ENXIO;
+	}
 	zoltrix_radio.priv = &zoltrix_unit;
 
 	if (video_register_device(&zoltrix_radio, VFL_TYPE_RADIO) == -1)
@@ -304,6 +368,7 @@
 	inb(io + 3);
 
 	zoltrix_unit.curvol = 0;
+	zoltrix_unit.stereo = 1;
 
 	return 0;
 }

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen, slshen@lbl.gov