# Bazaar revision bundle v0.9
#
# message:
#   Port radio tuner code over to V4L2 API.
#   
# committer: James Henstridge <james@jamesh.id.au>
# date: Thu 2007-04-12 21:56:47.592999935 +0800

=== modified file src/tech.c
--- src/tech.c
+++ src/tech.c
@@ -23,12 +23,13 @@
 #include <sys/ioctl.h>
 #include <assert.h>
 
-#include <linux/videodev.h>
+#include <linux/videodev2.h>
 #include <sys/soundcard.h>
 
 #include "tech.h"
 
 static int freq_fact, fd = -1, mixer_fd = -1, mixer_src = -1;
+static int radio_rangelow, radio_rangehigh;
 static char *devices[] = SOUND_DEVICE_NAMES;
 
 /*
@@ -177,22 +178,45 @@
  
 int radio_init(char *device)
 {
-	struct video_tuner tuner;
+	struct v4l2_capability caps;
+	struct v4l2_tuner tuner;
 	
 	if ((fd = open(device, O_RDONLY))< 0)
 		return 0;
 	
-	tuner.tuner = 0;
-	if (ioctl (fd, VIDIOCGTUNER, &tuner) < 0)
+	/* does this device provide a tuner? */
+	memset(&caps, 0, sizeof(caps));
+	if (ioctl(fd, VIDIOC_QUERYCAP, &caps) < 0) {
+		perror("VIDIOC_QUERYCAP");
+		close(fd);
+		return 0;
+	}
+	if ((caps.capabilities & V4L2_CAP_TUNER) == 0) {
+		fprintf(stderr, "Not a tuner device\n");
+		close(fd);
+		return 0;
+	}
+
+	/* check the tuner */
+	memset(&tuner, 0, sizeof(tuner));
+	tuner.index = 0;
+	if (ioctl(fd, VIDIOC_G_TUNER, &tuner) < 0) {
+		perror("VIDIOC_G_TUNER");
+		close(fd);
+		return 0;
+	}
+	if (tuner.type != V4L2_TUNER_RADIO) {
+		fprintf(stderr, "Not a radio tuner\n");
+		close(fd);
+		return 0;
+	}
+	/* Does this tuner expect data in 62.5Hz or 62.5kHz multiples? */
+	radio_rangelow = tuner.rangelow;
+	radio_rangehigh = tuner.rangehigh;
+	if ((tuner.capability & V4L2_TUNER_CAP_LOW) != 0)
+		freq_fact = 16000;
+	else
 		freq_fact = 16;
-	else
-	{
-		if ((tuner.flags & VIDEO_TUNER_LOW) == 0)
-			freq_fact = 16;
-		else 
-			freq_fact = 16000;
-	}		
-	
 	radio_unmute();
 	
 	return 1;
@@ -211,22 +235,22 @@
 		close(fd);
 }
 
-int radio_setfreq(float freq)
+int radio_setfreq(float frequency)
 {
-    int ifreq = (freq+1.0/32)*freq_fact;
-    if (fd<0)
-    	return 0;
-    
-#if 0
-	printf("Setting to %i (= %.2f)\n", ifreq, freq);
-#endif
-	
-	if ((freq > 108) || (freq < 65))
+	struct v4l2_frequency freq;
+
+	if (fd<0)
+		return 0;
+
+	memset(&freq, 0, sizeof(freq));
+	freq.tuner = 0;
+	freq.type = V4L2_TUNER_RADIO;
+	freq.frequency = frequency * freq_fact;
+
+	if (freq.frequency < radio_rangelow ||
+	    freq.frequency > radio_rangehigh)
 		return 1;
-
-	assert ((freq <= 108) && (freq > 65));
-
-    return ioctl(fd, VIDIOCSFREQ, &ifreq);
+	return ioctl(fd, VIDIOC_S_FREQUENCY, &freq);
 }
 
 int radio_check_station(float freq)
@@ -253,63 +277,58 @@
 
 void radio_unmute(void)
 {
-    struct video_audio vid_aud;
-
-    if (fd<0)
-    	return;
-
-    if (ioctl(fd, VIDIOCGAUDIO, &vid_aud))
-		perror("VIDIOCGAUDIO");
-    /*if (vid_aud.volume == 0)*/
-	vid_aud.volume = 0xFFFF;
-    vid_aud.flags &= ~VIDEO_AUDIO_MUTE;
-	vid_aud.mode = VIDEO_SOUND_STEREO;
-	
-    if (ioctl(fd, VIDIOCSAUDIO, &vid_aud))
-		perror("VIDIOCSAUDIO");
+	struct v4l2_control control;
+
+	if (fd<0)
+		return;
+
+	memset(&control, 0, sizeof(control));
+	control.id = V4L2_CID_AUDIO_MUTE;
+	control.value = 0;
+	if (ioctl(fd, VIDIOC_S_CTRL, &control))
+		perror("VIDIOC_S_CTRL");
 }
 
 void radio_mute(void)
 {
-    struct video_audio vid_aud;
-
-    if (fd<0)
-    	return;
-
-    if (ioctl(fd, VIDIOCGAUDIO, &vid_aud))
-		perror("VIDIOCGAUDIO");
-    vid_aud.flags |= VIDEO_AUDIO_MUTE;
-    if (ioctl(fd, VIDIOCSAUDIO, &vid_aud))
-		perror("VIDIOCSAUDIO");
+	struct v4l2_control control;
+
+	if (fd<0)
+		return;
+
+	memset(&control, 0, sizeof(control));
+	control.id = V4L2_CID_AUDIO_MUTE;
+	control.value = 1;
+	if (ioctl(fd, VIDIOC_S_CTRL, &control))
+		perror("VIDIOC_S_CTRL");
 }
 
 int radio_getstereo()
 {
-    struct video_audio va;
-    va.mode=-1;
-
-    if (fd<0)
-    	return -1;
-    
-    if (ioctl (fd, VIDIOCGAUDIO, &va) < 0)
-		return -1;
-	if (va.mode == VIDEO_SOUND_STEREO)
-		return 1;
-	else 
-		return 0;
+	struct v4l2_tuner tuner;
+
+	if (fd<0)
+		return -1;
+
+	memset(&tuner, 0, sizeof(tuner));
+	tuner.index = 0;
+	if (ioctl(fd, VIDIOC_G_TUNER, &tuner) < 0)
+		return -1;
+
+	return tuner.audmode == V4L2_TUNER_MODE_STEREO;
 }
 
 int radio_getsignal()
 {
-    struct video_tuner vt;
-    int signal;
-
-    if (fd<0)
-    	return -1;
-
-    memset(&vt,0,sizeof(vt));
-    ioctl (fd, VIDIOCGTUNER, &vt);
-    signal=vt.signal>>13;
-
-    return signal;
+	struct v4l2_tuner tuner;
+
+	if (fd<0)
+		return -1;
+
+	memset(&tuner, 0, sizeof(tuner));
+	tuner.index = 0;
+	if (ioctl(fd, VIDIOC_G_TUNER, &tuner) < 0)
+		return -1;
+
+	return tuner.signal >> 13;
 }

=== modified directory  // last-changed:james@jamesh.id.au-20070412135647-bz9dl
... esz0vifvy7m
# revision id: james@jamesh.id.au-20070412135647-bz9dlesz0vifvy7m
# sha1: 15d91a76e8e72cb6235c358beb90e5001bf07d19
# inventory sha1: caa257e758124ea760b4347369926073631057a1
# parent ids:
#   svn-v2:172@ba97a3d1-ec25-0410-b1c6-e06ad936ea6c-trunk
# base id: svn-v2:172@ba97a3d1-ec25-0410-b1c6-e06ad936ea6c-trunk
# properties:
#   branch-nick: gnomeradio

