V4L/DVB (10459): radio-mr800: fix amradio_set_freq
[safe/jmp/linux-2.6] / drivers / media / radio / radio-sf16fmr2.c
index 0732efd..92f17a3 100644 (file)
  *  For read stereo/mono you must wait 0.1 sec after set frequency and
  *  card unmuted so I set frequency on unmute
  *  Signal handling seem to work only on autoscanning (not implemented)
+ *
+ *  Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  */
 
 #include <linux/module.h>      /* Modules                      */
 #include <linux/init.h>                /* Initdata                     */
-#include <linux/ioport.h>      /* check_region, request_region */
+#include <linux/ioport.h>      /* request_region               */
 #include <linux/delay.h>       /* udelay                       */
 #include <asm/io.h>            /* outb, outb_p                 */
 #include <asm/uaccess.h>       /* copy to/from user            */
-#include <linux/videodev.h>    /* kernel radio structs         */
-#include <asm/semaphore.h>
+#include <linux/videodev2.h>   /* kernel radio structs         */
+#include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
+#include <linux/mutex.h>
+
+static struct mutex lock;
+
+#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
+#define RADIO_VERSION KERNEL_VERSION(0,0,2)
+
+#define AUD_VOL_INDEX 1
 
-static struct semaphore lock;
+static struct v4l2_queryctrl radio_qctrl[] = {
+       {
+               .id            = V4L2_CID_AUDIO_MUTE,
+               .name          = "Mute",
+               .minimum       = 0,
+               .maximum       = 1,
+               .default_value = 1,
+               .type          = V4L2_CTRL_TYPE_BOOLEAN,
+       },
+       [AUD_VOL_INDEX] = {
+               .id            = V4L2_CID_AUDIO_VOLUME,
+               .name          = "Volume",
+               .minimum       = 0,
+               .maximum       = 15,
+               .step          = 1,
+               .default_value = 0,
+               .type          = V4L2_CTRL_TYPE_INTEGER,
+       }
+};
 
 #undef DEBUG
 //#define DEBUG 1
@@ -35,8 +64,9 @@ static struct semaphore lock;
 /* this should be static vars for module size */
 struct fmr2_device
 {
+       unsigned long in_use;
        int port;
-       int curvol; /* 0-65535, if not volume 0 or 65535 */
+       int curvol; /* 0-15 */
        int mute;
        int stereo; /* card is producing stereo audio */
        unsigned long curfreq; /* freq in kHz */
@@ -48,7 +78,7 @@ static int io = 0x384;
 static int radio_nr = -1;
 
 /* hw precision is 12.5 kHz
- * It is only usefull to give freq in intervall of 200 (=0.0125Mhz),
+ * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
  * other bits will be truncated
  */
 #define RSF16_ENCODE(x)        ((x)/200+856)
@@ -151,221 +181,259 @@ static int fmr2_setfreq(struct fmr2_device *dev)
 /* !!! not tested, in my card this does't work !!! */
 static int fmr2_setvolume(struct fmr2_device *dev)
 {
-       int i,a,n, port = dev->port;
+       int vol[16] = { 0x021, 0x084, 0x090, 0x104,
+                       0x110, 0x204, 0x210, 0x402,
+                       0x404, 0x408, 0x410, 0x801,
+                       0x802, 0x804, 0x808, 0x810 };
+       int i, a, port = dev->port;
+       int n = vol[dev->curvol & 0x0f];
 
-       if (dev->card_type != 11) return 1;
+       if (dev->card_type != 11)
+               return 1;
 
-       switch( (dev->curvol+(1<<11)) >> 12 )
-       {
-       case 0: case 1: n = 0x21; break;
-       case 2: n = 0x84; break;
-       case 3: n = 0x90; break;
-       case 4: n = 0x104; break;
-       case 5: n = 0x110; break;
-       case 6: n = 0x204; break;
-       case 7: n = 0x210; break;
-       case 8: n = 0x402; break;
-       case 9: n = 0x404; break;
-       default:
-       case 10: n = 0x408; break;
-       case 11: n = 0x410; break;
-       case 12: n = 0x801; break;
-       case 13: n = 0x802; break;
-       case 14: n = 0x804; break;
-       case 15: n = 0x808; break;
-       case 16: n = 0x810; break;
-       }
-       for(i=12;--i>=0;)
-       {
+       for (i = 12; --i >= 0; ) {
                a = ((n >> i) & 1) << 6; /* if (a=0) a= 0; else a= 0x40; */
-               outb(a|4, port);
-               wait(4,port);
-               outb(a|0x24, port);
-               wait(4,port);
-               outb(a|4, port);
-               wait(4,port);
+               outb(a | 4, port);
+               wait(4, port);
+               outb(a | 0x24, port);
+               wait(4, port);
+               outb(a | 4, port);
+               wait(4, port);
        }
-       for(i=6;--i>=0;)
-       {
+       for (i = 6; --i >= 0; ) {
                a = ((0x18 >> i) & 1) << 6;
-               outb(a|4, port);
+               outb(a | 4, port);
                wait(4,port);
-               outb(a|0x24, port);
+               outb(a | 0x24, port);
                wait(4,port);
                outb(a|4, port);
                wait(4,port);
        }
-       wait(4,port);
+       wait(4, port);
        outb(0x14, port);
 
        return 0;
 }
 
-static int fmr2_do_ioctl(struct inode *inode, struct file *file,
-                      unsigned int cmd, void *arg)
+static int vidioc_querycap(struct file *file, void  *priv,
+                                       struct v4l2_capability *v)
 {
-       struct video_device *dev = video_devdata(file);
-       struct fmr2_device *fmr2 = dev->priv;
-       debug_print((KERN_DEBUG "freq %ld flags %d vol %d mute %d "
-               "stereo %d type %d\n",
-               fmr2->curfreq, fmr2->flags, fmr2->curvol, fmr2->mute,
-               fmr2->stereo, fmr2->card_type));
-
-       switch(cmd)
-       {
-               case VIDIOCGCAP:
-               {
-                       struct video_capability *v = arg;
-                       memset(v,0,sizeof(*v));
-                       strcpy(v->name, "SF16-FMR2 radio");
-                       v->type=VID_TYPE_TUNER;
-                       v->channels=1;
-                       v->audios=1;
-                       return 0;
-               }
-               case VIDIOCGTUNER:
-               {
-                       struct video_tuner *v = arg;
-                       int mult;
-
-                       if(v->tuner)     /* Only 1 tuner */
-                               return -EINVAL;
-                       strcpy(v->name, "FM");
-                       mult = (fmr2->flags & VIDEO_TUNER_LOW) ? 1 : 1000;
-                       v->rangelow = RSF16_MINFREQ/mult;
-                       v->rangehigh = RSF16_MAXFREQ/mult;
-                       v->flags = fmr2->flags | VIDEO_AUDIO_MUTABLE;
-                       if (fmr2->mute)
-                               v->flags |= VIDEO_AUDIO_MUTE;
-                       v->mode=VIDEO_MODE_AUTO;
-                       down(&lock);
-                       v->signal = fmr2_getsigstr(fmr2);
-                       up(&lock);
-                       return 0;
-               }
-               case VIDIOCSTUNER:
-               {
-                       struct video_tuner *v = arg;
-                       if (v->tuner!=0)
-                               return -EINVAL;
-                       fmr2->flags = v->flags & VIDEO_TUNER_LOW;
-                       return 0;
-               }
-               case VIDIOCGFREQ:
-               {
-                       unsigned long *freq = arg;
-                       *freq = fmr2->curfreq;
-                       if (!(fmr2->flags & VIDEO_TUNER_LOW))
-                               *freq /= 1000;
-                       return 0;
-               }
-               case VIDIOCSFREQ:
-               {
-                       unsigned long *freq = arg;
-                       if (!(fmr2->flags & VIDEO_TUNER_LOW))
-                               *freq *= 1000;
-                       if ( *freq < RSF16_MINFREQ || *freq > RSF16_MAXFREQ )
-                               return -EINVAL;
-                       /* rounding in steps of 200 to match th freq
-                        * that will be used
-                        */
-                       fmr2->curfreq = (*freq/200)*200;
-
-                       /* set card freq (if not muted) */
-                       if (fmr2->curvol && !fmr2->mute)
-                       {
-                               down(&lock);
-                               fmr2_setfreq(fmr2);
-                               up(&lock);
-                       }
-                       return 0;
-               }
-               case VIDIOCGAUDIO:
-               {
-                       struct video_audio *v = arg;
-                       memset(v,0,sizeof(*v));
-                       /* !!! do not return VIDEO_AUDIO_MUTE */
-                       v->flags = VIDEO_AUDIO_MUTABLE;
-                       strcpy(v->name, "Radio");
-                       /* get current stereo mode */
-                       v->mode = fmr2->stereo ? VIDEO_SOUND_STEREO: VIDEO_SOUND_MONO;
-                       /* volume supported ? */
-                       if (fmr2->card_type == 11)
-                       {
-                               v->flags |= VIDEO_AUDIO_VOLUME;
-                               v->step = 1 << 12;
-                               v->volume = fmr2->curvol;
-                       }
-                       debug_print((KERN_DEBUG "Get flags %d vol %d\n", v->flags, v->volume));
+       strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
+       strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
+       sprintf(v->bus_info, "ISA");
+       v->version = RADIO_VERSION;
+       v->capabilities = V4L2_CAP_TUNER;
+       return 0;
+}
+
+static int vidioc_g_tuner(struct file *file, void *priv,
+                                       struct v4l2_tuner *v)
+{
+       int mult;
+       struct fmr2_device *fmr2 = video_drvdata(file);
+
+       if (v->index > 0)
+               return -EINVAL;
+
+       strcpy(v->name, "FM");
+       v->type = V4L2_TUNER_RADIO;
+
+       mult = (fmr2->flags & V4L2_TUNER_CAP_LOW) ? 1 : 1000;
+       v->rangelow = RSF16_MINFREQ/mult;
+       v->rangehigh = RSF16_MAXFREQ/mult;
+       v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_MODE_STEREO;
+       v->capability = fmr2->flags&V4L2_TUNER_CAP_LOW;
+       v->audmode = fmr2->stereo ? V4L2_TUNER_MODE_STEREO:
+                               V4L2_TUNER_MODE_MONO;
+       mutex_lock(&lock);
+       v->signal = fmr2_getsigstr(fmr2);
+       mutex_unlock(&lock);
+       return 0;
+}
+
+static int vidioc_s_tuner(struct file *file, void *priv,
+                                       struct v4l2_tuner *v)
+{
+       if (v->index > 0)
+               return -EINVAL;
+       return 0;
+}
+
+static int vidioc_s_frequency(struct file *file, void *priv,
+                                       struct v4l2_frequency *f)
+{
+       struct fmr2_device *fmr2 = video_drvdata(file);
+
+       if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
+               f->frequency *= 1000;
+       if (f->frequency < RSF16_MINFREQ ||
+                       f->frequency > RSF16_MAXFREQ )
+               return -EINVAL;
+       /*rounding in steps of 200 to match th freq
+       that will be used */
+       fmr2->curfreq = (f->frequency/200)*200;
+
+       /* set card freq (if not muted) */
+       if (fmr2->curvol && !fmr2->mute) {
+               mutex_lock(&lock);
+               fmr2_setfreq(fmr2);
+               mutex_unlock(&lock);
+       }
+       return 0;
+}
+
+static int vidioc_g_frequency(struct file *file, void *priv,
+                                       struct v4l2_frequency *f)
+{
+       struct fmr2_device *fmr2 = video_drvdata(file);
+
+       f->type = V4L2_TUNER_RADIO;
+       f->frequency = fmr2->curfreq;
+       if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
+               f->frequency /= 1000;
+       return 0;
+}
+
+static int vidioc_queryctrl(struct file *file, void *priv,
+                                       struct v4l2_queryctrl *qc)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
+               if (qc->id && qc->id == radio_qctrl[i].id) {
+                       memcpy(qc, &radio_qctrl[i], sizeof(*qc));
                        return 0;
                }
-               case VIDIOCSAUDIO:
-               {
-                       struct video_audio *v = arg;
-                       if(v->audio)
-                               return -EINVAL;
-                       debug_print((KERN_DEBUG "Set flags %d vol %d\n", v->flags, v->volume));
-                       /* set volume */
-                       if (v->flags & VIDEO_AUDIO_VOLUME)
-                               fmr2->curvol = v->volume; /* !!! set with precision */
-                       if (fmr2->card_type != 11) fmr2->curvol = 65535;
-                       fmr2->mute = 0;
-                       if (v->flags & VIDEO_AUDIO_MUTE)
-                               fmr2->mute = 1;
+       }
+       return -EINVAL;
+}
+
+static int vidioc_g_ctrl(struct file *file, void *priv,
+                                       struct v4l2_control *ctrl)
+{
+       struct fmr2_device *fmr2 = video_drvdata(file);
+
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               ctrl->value = fmr2->mute;
+               return 0;
+       case V4L2_CID_AUDIO_VOLUME:
+               ctrl->value = fmr2->curvol;
+               return 0;
+       }
+       return -EINVAL;
+}
+
+static int vidioc_s_ctrl(struct file *file, void *priv,
+                                       struct v4l2_control *ctrl)
+{
+       struct fmr2_device *fmr2 = video_drvdata(file);
+
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               fmr2->mute = ctrl->value;
+               break;
+       case V4L2_CID_AUDIO_VOLUME:
+               if (ctrl->value > radio_qctrl[AUD_VOL_INDEX].maximum)
+                       fmr2->curvol = radio_qctrl[AUD_VOL_INDEX].maximum;
+               else
+                       fmr2->curvol = ctrl->value;
+
+               break;
+       default:
+               return -EINVAL;
+       }
+
 #ifdef DEBUG
-                       if (fmr2->curvol && !fmr2->mute)
-                               printk(KERN_DEBUG "unmute\n");
-                       else
-                               printk(KERN_DEBUG "mute\n");
+       if (fmr2->curvol && !fmr2->mute)
+               printk(KERN_DEBUG "unmute\n");
+       else
+               printk(KERN_DEBUG "mute\n");
 #endif
-                       down(&lock);
-                       if (fmr2->curvol && !fmr2->mute)
-                       {
-                               fmr2_setvolume(fmr2);
-                               fmr2_setfreq(fmr2);
-                       }
-                       else fmr2_mute(fmr2->port);
-                       up(&lock);
-                       return 0;
-               }
-               case VIDIOCGUNIT:
-               {
-                       struct video_unit *v = arg;
-                       v->video=VIDEO_NO_UNIT;
-                       v->vbi=VIDEO_NO_UNIT;
-                       v->radio=dev->minor;
-                       v->audio=0; /* How do we find out this??? */
-                       v->teletext=VIDEO_NO_UNIT;
-                       return 0;
-               }
-               default:
-                       return -ENOIOCTLCMD;
-       }
+
+       mutex_lock(&lock);
+       if (fmr2->curvol && !fmr2->mute) {
+               fmr2_setvolume(fmr2);
+               /* Set frequency and unmute card */
+               fmr2_setfreq(fmr2);
+       } else
+               fmr2_mute(fmr2->port);
+       mutex_unlock(&lock);
+       return 0;
+}
+
+static int vidioc_g_audio(struct file *file, void *priv,
+                                       struct v4l2_audio *a)
+{
+       if (a->index > 1)
+               return -EINVAL;
+
+       strcpy(a->name, "Radio");
+       a->capability = V4L2_AUDCAP_STEREO;
+       return 0;
+}
+
+static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
+{
+       *i = 0;
+       return 0;
+}
+
+static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
+{
+       if (i != 0)
+               return -EINVAL;
+       return 0;
 }
 
-static int fmr2_ioctl(struct inode *inode, struct file *file,
-                      unsigned int cmd, unsigned long arg)
- {
-       return video_usercopy(inode, file, cmd, arg, fmr2_do_ioctl);
+static int vidioc_s_audio(struct file *file, void *priv,
+                                       struct v4l2_audio *a)
+{
+       if (a->index != 0)
+               return -EINVAL;
+       return 0;
 }
 
 static struct fmr2_device fmr2_unit;
 
-static struct file_operations fmr2_fops = {
+static int fmr2_exclusive_open(struct file *file)
+{
+       return test_and_set_bit(0, &fmr2_unit.in_use) ? -EBUSY : 0;
+}
+
+static int fmr2_exclusive_release(struct file *file)
+{
+       clear_bit(0, &fmr2_unit.in_use);
+       return 0;
+}
+
+static const struct v4l2_file_operations fmr2_fops = {
        .owner          = THIS_MODULE,
-       .open           = video_exclusive_open,
-       .release        = video_exclusive_release,
-       .ioctl          = fmr2_ioctl,
-       .llseek         = no_llseek,
+       .open           = fmr2_exclusive_open,
+       .release        = fmr2_exclusive_release,
+       .ioctl          = video_ioctl2,
 };
 
-static struct video_device fmr2_radio=
-{
-       .owner          = THIS_MODULE,
+static const struct v4l2_ioctl_ops fmr2_ioctl_ops = {
+       .vidioc_querycap    = vidioc_querycap,
+       .vidioc_g_tuner     = vidioc_g_tuner,
+       .vidioc_s_tuner     = vidioc_s_tuner,
+       .vidioc_g_audio     = vidioc_g_audio,
+       .vidioc_s_audio     = vidioc_s_audio,
+       .vidioc_g_input     = vidioc_g_input,
+       .vidioc_s_input     = vidioc_s_input,
+       .vidioc_g_frequency = vidioc_g_frequency,
+       .vidioc_s_frequency = vidioc_s_frequency,
+       .vidioc_queryctrl   = vidioc_queryctrl,
+       .vidioc_g_ctrl      = vidioc_g_ctrl,
+       .vidioc_s_ctrl      = vidioc_s_ctrl,
+};
+
+static struct video_device fmr2_radio = {
        .name           = "SF16FMR2 radio",
-       . type          = VID_TYPE_TUNER,
-       .hardware       = VID_HARDWARE_SF16FMR2,
        .fops           = &fmr2_fops,
+       .ioctl_ops      = &fmr2_ioctl_ops,
+       .release        = video_device_release_empty,
 };
 
 static int __init fmr2_init(void)
@@ -375,32 +443,34 @@ static int __init fmr2_init(void)
        fmr2_unit.mute = 0;
        fmr2_unit.curfreq = 0;
        fmr2_unit.stereo = 1;
-       fmr2_unit.flags = VIDEO_TUNER_LOW;
+       fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
        fmr2_unit.card_type = 0;
-       fmr2_radio.priv = &fmr2_unit;
+       video_set_drvdata(&fmr2_radio, &fmr2_unit);
 
-       init_MUTEX(&lock);
+       mutex_init(&lock);
 
-       if (request_region(io, 2, "sf16fmr2"))
-       {
-               printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
+       if (!request_region(io, 2, "sf16fmr2")) {
+               printk(KERN_ERR "radio-sf16fmr2: request_region failed!\n");
                return -EBUSY;
        }
 
-       if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
-       {
+       if (video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
                release_region(io, 2);
                return -EINVAL;
        }
 
        printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
-       debug_print((KERN_DEBUG "Mute %d Low %d\n",VIDEO_AUDIO_MUTE,VIDEO_TUNER_LOW));
        /* mute card - prevents noisy bootups */
-       down(&lock);
+       mutex_lock(&lock);
        fmr2_mute(io);
        fmr2_product_info(&fmr2_unit);
-       up(&lock);
+       mutex_unlock(&lock);
        debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
+
+       /* Only card_type == 11 implements volume */
+       if (fmr2_unit.card_type != 11)
+               radio_qctrl[AUD_VOL_INDEX].maximum = 1;
+
        return 0;
 }