V4L/DVB (9503): v4l: remove inode argument from video_usercopy
[safe/jmp/linux-2.6] / drivers / media / radio / radio-trust.c
index efcec01..e7b111f 100644 (file)
@@ -12,7 +12,7 @@
  * Scott McGrath    (smcgrath@twilight.vtc.vsc.edu)
  * William McGrath  (wmcgrath@twilight.vtc.vsc.edu)
  *
- * The basis for this code may be found at http://bigbang.vtc.vsc.edu/fmradio/
+ * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
  */
 
 #include <stdarg.h>
 #include <linux/ioport.h>
 #include <asm/io.h>
 #include <asm/uaccess.h>
-#include <linux/videodev.h>
-#include <linux/config.h>      /* CONFIG_RADIO_TRUST_PORT      */
+#include <linux/videodev2.h>
+#include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
+
+#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
+#define RADIO_VERSION KERNEL_VERSION(0,0,2)
+
+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,
+       },{
+               .id            = V4L2_CID_AUDIO_VOLUME,
+               .name          = "Volume",
+               .minimum       = 0,
+               .maximum       = 65535,
+               .step          = 2048,
+               .default_value = 65535,
+               .type          = V4L2_CTRL_TYPE_INTEGER,
+       },{
+               .id            = V4L2_CID_AUDIO_BASS,
+               .name          = "Bass",
+               .minimum       = 0,
+               .maximum       = 65535,
+               .step          = 4370,
+               .default_value = 32768,
+               .type          = V4L2_CTRL_TYPE_INTEGER,
+       },{
+               .id            = V4L2_CID_AUDIO_TREBLE,
+               .name          = "Treble",
+               .minimum       = 0,
+               .maximum       = 65535,
+               .step          = 4370,
+               .default_value = 32768,
+               .type          = V4L2_CTRL_TYPE_INTEGER,
+       },
+};
 
 /* acceptable ports: 0x350 (JP3 shorted), 0x358 (JP3 open) */
 
@@ -39,6 +78,7 @@ static __u16 curtreble;
 static unsigned long curfreq;
 static int curstereo;
 static int curmute;
+static unsigned long in_use;
 
 /* i2c addresses */
 #define TDA7318_ADDR 0x88
@@ -154,118 +194,191 @@ static void tr_setfreq(unsigned long f)
        write_i2c(5, TSA6060T_ADDR, (f << 1) | 1, f >> 7, 0x60 | ((f >> 15) & 1), 0);
 }
 
-static int tr_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)
 {
-       switch(cmd)
-       {
-               case VIDIOCGCAP:
-               {
-                       struct video_capability *v = arg;
+       strlcpy(v->driver, "radio-trust", sizeof(v->driver));
+       strlcpy(v->card, "Trust FM Radio", sizeof(v->card));
+       sprintf(v->bus_info, "ISA");
+       v->version = RADIO_VERSION;
+       v->capabilities = V4L2_CAP_TUNER;
+       return 0;
+}
 
-                       memset(v,0,sizeof(*v));
-                       v->type=VID_TYPE_TUNER;
-                       v->channels=1;
-                       v->audios=1;
-                       strcpy(v->name, "Trust FM Radio");
+static int vidioc_g_tuner(struct file *file, void *priv,
+                               struct v4l2_tuner *v)
+{
+       if (v->index > 0)
+               return -EINVAL;
 
-                       return 0;
-               }
-               case VIDIOCGTUNER:
-               {
-                       struct video_tuner *v = arg;
+       strcpy(v->name, "FM");
+       v->type = V4L2_TUNER_RADIO;
+       v->rangelow = (87.5*16000);
+       v->rangehigh = (108*16000);
+       v->rxsubchans = V4L2_TUNER_SUB_MONO|V4L2_TUNER_SUB_STEREO;
+       v->capability = V4L2_TUNER_CAP_LOW;
+       if (tr_getstereo())
+               v->audmode = V4L2_TUNER_MODE_STEREO;
+       else
+               v->audmode = V4L2_TUNER_MODE_MONO;
+       v->signal = tr_getsigstr();
+       return 0;
+}
 
-                       if(v->tuner)    /* Only 1 tuner */
-                               return -EINVAL;
+static int vidioc_s_tuner(struct file *file, void *priv,
+                               struct v4l2_tuner *v)
+{
+       if (v->index > 0)
+               return -EINVAL;
 
-                       v->rangelow = 87500 * 16;
-                       v->rangehigh = 108000 * 16;
-                       v->flags = VIDEO_TUNER_LOW;
-                       v->mode = VIDEO_MODE_AUTO;
+       return 0;
+}
 
-                       v->signal = tr_getsigstr();
-                       if(tr_getstereo())
-                               v->flags |= VIDEO_TUNER_STEREO_ON;
+static int vidioc_s_frequency(struct file *file, void *priv,
+                               struct v4l2_frequency *f)
+{
+       curfreq = f->frequency;
+       tr_setfreq(curfreq);
+       return 0;
+}
 
-                       strcpy(v->name, "FM");
+static int vidioc_g_frequency(struct file *file, void *priv,
+                               struct v4l2_frequency *f)
+{
+       f->type = V4L2_TUNER_RADIO;
+       f->frequency = curfreq;
+       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 VIDIOCSTUNER:
-               {
-                       struct video_tuner *v = arg;
-                       if(v->tuner != 0)
-                               return -EINVAL;
-                       return 0;
-               }
-               case VIDIOCGFREQ:
-               {
-                       unsigned long *freq = arg;
-                       *freq = curfreq;
-                       return 0;
-               }
-               case VIDIOCSFREQ:
-               {
-                       unsigned long *freq = arg;
-                       tr_setfreq(*freq);
-                       return 0;
-               }
-               case VIDIOCGAUDIO:
-               {
-                       struct video_audio *v = arg;
-
-                       memset(v,0, sizeof(*v));
-                       v->flags = VIDEO_AUDIO_MUTABLE | VIDEO_AUDIO_VOLUME |
-                                 VIDEO_AUDIO_BASS | VIDEO_AUDIO_TREBLE;
-                       v->mode = curstereo? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
-                       v->volume = curvol * 2048;
-                       v->step = 2048;
-                       v->bass = curbass * 4370;
-                       v->treble = curtreble * 4370;
-
-                       strcpy(v->name, "Trust FM Radio");
-                       return 0;
-               }
-               case VIDIOCSAUDIO:
-               {
-                       struct video_audio *v = arg;
-
-                       if(v->audio)
-                               return -EINVAL;
-                       tr_setvol(v->volume);
-                       tr_setbass(v->bass);
-                       tr_settreble(v->treble);
-                       tr_setstereo(v->mode & VIDEO_SOUND_STEREO);
-                       tr_setmute(v->flags & VIDEO_AUDIO_MUTE);
-                       return 0;
-               }
-               default:
-                       return -ENOIOCTLCMD;
        }
+       return -EINVAL;
 }
 
-static int tr_ioctl(struct inode *inode, struct file *file,
-                   unsigned int cmd, unsigned long arg)
+static int vidioc_g_ctrl(struct file *file, void *priv,
+                               struct v4l2_control *ctrl)
 {
-       return video_usercopy(inode, file, cmd, arg, tr_do_ioctl);
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               ctrl->value = curmute;
+               return 0;
+       case V4L2_CID_AUDIO_VOLUME:
+               ctrl->value = curvol * 2048;
+               return 0;
+       case V4L2_CID_AUDIO_BASS:
+               ctrl->value = curbass * 4370;
+               return 0;
+       case V4L2_CID_AUDIO_TREBLE:
+               ctrl->value = curtreble * 4370;
+               return 0;
+       }
+       return -EINVAL;
 }
 
-static struct file_operations trust_fops = {
+static int vidioc_s_ctrl(struct file *file, void *priv,
+                               struct v4l2_control *ctrl)
+{
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               tr_setmute(ctrl->value);
+               return 0;
+       case V4L2_CID_AUDIO_VOLUME:
+               tr_setvol(ctrl->value);
+               return 0;
+       case V4L2_CID_AUDIO_BASS:
+               tr_setbass(ctrl->value);
+               return 0;
+       case V4L2_CID_AUDIO_TREBLE:
+               tr_settreble(ctrl->value);
+               return 0;
+       }
+       return -EINVAL;
+}
+
+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 vidioc_s_audio(struct file *file, void *priv,
+                               struct v4l2_audio *a)
+{
+       if (a->index != 0)
+               return -EINVAL;
+       return 0;
+}
+
+static int trust_exclusive_open(struct inode *inode, struct file *file)
+{
+       return test_and_set_bit(0, &in_use) ? -EBUSY : 0;
+}
+
+static int trust_exclusive_release(struct inode *inode, struct file *file)
+{
+       clear_bit(0, &in_use);
+       return 0;
+}
+
+static const struct file_operations trust_fops = {
        .owner          = THIS_MODULE,
-       .open           = video_exclusive_open,
-       .release        = video_exclusive_release,
-       .ioctl          = tr_ioctl,
+       .open           = trust_exclusive_open,
+       .release        = trust_exclusive_release,
+       .ioctl          = video_ioctl2,
+#ifdef CONFIG_COMPAT
        .compat_ioctl   = v4l_compat_ioctl32,
+#endif
        .llseek         = no_llseek,
 };
 
-static struct video_device trust_radio=
-{
-       .owner          = THIS_MODULE,
+static const struct v4l2_ioctl_ops trust_ioctl_ops = {
+       .vidioc_querycap    = vidioc_querycap,
+       .vidioc_g_tuner     = vidioc_g_tuner,
+       .vidioc_s_tuner     = vidioc_s_tuner,
+       .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,
+       .vidioc_g_audio     = vidioc_g_audio,
+       .vidioc_s_audio     = vidioc_s_audio,
+       .vidioc_g_input     = vidioc_g_input,
+       .vidioc_s_input     = vidioc_s_input,
+};
+
+static struct video_device trust_radio = {
        .name           = "Trust FM Radio",
-       .type           = VID_TYPE_TUNER,
-       .hardware       = VID_HARDWARE_TRUST,
        .fops           = &trust_fops,
+       .ioctl_ops      = &trust_ioctl_ops,
+       .release        = video_device_release_empty,
 };
 
 static int __init trust_init(void)
@@ -278,8 +391,7 @@ static int __init trust_init(void)
                printk(KERN_ERR "trust: port 0x%x already in use\n", io);
                return -EBUSY;
        }
-       if(video_register_device(&trust_radio, VFL_TYPE_RADIO, radio_nr)==-1)
-       {
+       if (video_register_device(&trust_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
                release_region(io, 2);
                return -EINVAL;
        }