V4L/DVB (10135): v4l2: introduce v4l2_file_operations.
[safe/jmp/linux-2.6] / drivers / media / radio / radio-aimslab.c
index b2e88ad..bfa13b8 100644 (file)
@@ -1,7 +1,7 @@
 /* radiotrack (radioreveal) driver for Linux radio support
  * (c) 1997 M. Kirkwood
  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
- * Converted to new API by Alan Cox <Alan.Cox@linux.org>
+ * Converted to new API by Alan Cox <alan@lxorguk.ukuu.org.uk>
  * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
  *
  * History:
@@ -36,7 +36,7 @@
 #include <asm/uaccess.h>       /* copy to/from user            */
 #include <linux/videodev2.h>   /* kernel radio structs         */
 #include <media/v4l2-common.h>
-#include <asm/semaphore.h>     /* Lock for the I/O             */
+#include <media/v4l2-ioctl.h>
 
 #include <linux/version.h>     /* for KERNEL_VERSION MACRO     */
 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
@@ -51,6 +51,7 @@ static struct mutex lock;
 
 struct rt_device
 {
+       unsigned long in_use;
        int port;
        int curvol;
        unsigned long curfreq;
@@ -63,7 +64,7 @@ struct rt_device
 static void sleep_delay(long n)
 {
        /* Sleep nicely for 'n' uS */
-       int d=n/(1000000/HZ);
+       int d=n/msecs_to_jiffies(1000);
        if(!d)
                udelay(n);
        else
@@ -231,149 +232,186 @@ static struct v4l2_queryctrl radio_qctrl[] = {
        }
 };
 
-static int rt_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 rt_device *rt=dev->priv;
+       strlcpy(v->driver, "radio-aimslab", sizeof(v->driver));
+       strlcpy(v->card, "RadioTrack", sizeof(v->card));
+       sprintf(v->bus_info, "ISA");
+       v->version = RADIO_VERSION;
+       v->capabilities = V4L2_CAP_TUNER;
+       return 0;
+}
 
-       switch(cmd)
-       {
-               case VIDIOC_QUERYCAP:
-               {
-                       struct v4l2_capability *v = arg;
-                       memset(v,0,sizeof(*v));
-                       strlcpy(v->driver, "radio-aimslab", sizeof (v->driver));
-                       strlcpy(v->card, "RadioTrack", sizeof (v->card));
-                       sprintf(v->bus_info,"ISA");
-                       v->version = RADIO_VERSION;
-                       v->capabilities = V4L2_CAP_TUNER;
+static int vidioc_g_tuner(struct file *file, void *priv,
+                                       struct v4l2_tuner *v)
+{
+       struct rt_device *rt = video_drvdata(file);
 
-                       return 0;
-               }
-               case VIDIOC_G_TUNER:
-               {
-                       struct v4l2_tuner *v = arg;
+       if (v->index > 0)
+               return -EINVAL;
 
-                       if (v->index > 0)
-                               return -EINVAL;
+       strcpy(v->name, "FM");
+       v->type = V4L2_TUNER_RADIO;
+       v->rangelow = (87*16000);
+       v->rangehigh = (108*16000);
+       v->rxsubchans = V4L2_TUNER_SUB_MONO;
+       v->capability = V4L2_TUNER_CAP_LOW;
+       v->audmode = V4L2_TUNER_MODE_MONO;
+       v->signal = 0xffff*rt_getsigstr(rt);
+       return 0;
+}
 
-                       memset(v,0,sizeof(*v));
-                       strcpy(v->name, "FM");
-                       v->type = V4L2_TUNER_RADIO;
+static int vidioc_s_tuner(struct file *file, void *priv,
+                                       struct v4l2_tuner *v)
+{
+       if (v->index > 0)
+               return -EINVAL;
+       return 0;
+}
 
-                       v->rangelow=(87*16000);
-                       v->rangehigh=(108*16000);
-                       v->rxsubchans =V4L2_TUNER_SUB_MONO;
-                       v->capability=V4L2_TUNER_CAP_LOW;
-                       v->audmode = V4L2_TUNER_MODE_MONO;
-                       v->signal=0xFFFF*rt_getsigstr(rt);
+static int vidioc_s_frequency(struct file *file, void *priv,
+                                       struct v4l2_frequency *f)
+{
+       struct rt_device *rt = video_drvdata(file);
 
-                       return 0;
-               }
-               case VIDIOC_S_TUNER:
-               {
-                       struct v4l2_tuner *v = arg;
+       rt->curfreq = f->frequency;
+       rt_setfreq(rt, rt->curfreq);
+       return 0;
+}
 
-                       if (v->index > 0)
-                               return -EINVAL;
+static int vidioc_g_frequency(struct file *file, void *priv,
+                                       struct v4l2_frequency *f)
+{
+       struct rt_device *rt = video_drvdata(file);
 
-                       return 0;
-               }
-               case VIDIOC_S_FREQUENCY:
-               {
-                       struct v4l2_frequency *f = arg;
+       f->type = V4L2_TUNER_RADIO;
+       f->frequency = rt->curfreq;
+       return 0;
+}
 
-                       rt->curfreq = f->frequency;
-                       rt_setfreq(rt, rt->curfreq);
+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 VIDIOC_G_FREQUENCY:
-               {
-                       struct v4l2_frequency *f = arg;
+       }
+       return -EINVAL;
+}
 
-                       f->type = V4L2_TUNER_RADIO;
-                       f->frequency = rt->curfreq;
+static int vidioc_g_ctrl(struct file *file, void *priv,
+                                       struct v4l2_control *ctrl)
+{
+       struct rt_device *rt = video_drvdata(file);
 
-                       return 0;
-               }
-               case VIDIOC_QUERYCTRL:
-               {
-                       struct v4l2_queryctrl *qc = arg;
-                       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);
-                               }
-                       }
-                       return -EINVAL;
-               }
-               case VIDIOC_G_CTRL:
-               {
-                       struct v4l2_control *ctrl= arg;
-
-                       switch (ctrl->id) {
-                               case V4L2_CID_AUDIO_MUTE:
-                                       ctrl->value=rt->muted;
-                                       return (0);
-                               case V4L2_CID_AUDIO_VOLUME:
-                                       ctrl->value=rt->curvol * 6554;
-                                       return (0);
-                       }
-                       return -EINVAL;
-               }
-               case VIDIOC_S_CTRL:
-               {
-                       struct v4l2_control *ctrl= arg;
-
-                       switch (ctrl->id) {
-                               case V4L2_CID_AUDIO_MUTE:
-                                       if (ctrl->value) {
-                                               rt_mute(rt);
-                                       } else {
-                                               rt_setvol(rt,rt->curvol);
-                                       }
-                                       return (0);
-                               case V4L2_CID_AUDIO_VOLUME:
-                                       rt_setvol(rt,ctrl->value);
-                                       return (0);
-                       }
-                       return -EINVAL;
-               }
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               ctrl->value = rt->muted;
+               return 0;
+       case V4L2_CID_AUDIO_VOLUME:
+               ctrl->value = rt->curvol * 6554;
+               return 0;
+       }
+       return -EINVAL;
+}
 
-               default:
-                       return v4l_compat_translate_ioctl(inode,file,cmd,arg,
-                                                         rt_do_ioctl);
+static int vidioc_s_ctrl(struct file *file, void *priv,
+                                       struct v4l2_control *ctrl)
+{
+       struct rt_device *rt = video_drvdata(file);
+
+       switch (ctrl->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               if (ctrl->value)
+                       rt_mute(rt);
+               else
+                       rt_setvol(rt,rt->curvol);
+               return 0;
+       case V4L2_CID_AUDIO_VOLUME:
+               rt_setvol(rt,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 rt_ioctl(struct inode *inode, struct file *file,
-                   unsigned int cmd, unsigned long arg)
+static int vidioc_s_audio(struct file *file, void *priv,
+                                       struct v4l2_audio *a)
 {
-       return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
+       if (a->index != 0)
+               return -EINVAL;
+       return 0;
 }
 
 static struct rt_device rtrack_unit;
 
-static const struct file_operations rtrack_fops = {
-       .owner          = THIS_MODULE,
-       .open           = video_exclusive_open,
-       .release        = video_exclusive_release,
-       .ioctl          = rt_ioctl,
-       .compat_ioctl   = v4l_compat_ioctl32,
-       .llseek         = no_llseek,
-};
+static int rtrack_exclusive_open(struct file *file)
+{
+       return test_and_set_bit(0, &rtrack_unit.in_use) ? -EBUSY : 0;
+}
 
-static struct video_device rtrack_radio=
+static int rtrack_exclusive_release(struct file *file)
 {
+       clear_bit(0, &rtrack_unit.in_use);
+       return 0;
+}
+
+static const struct v4l2_file_operations rtrack_fops = {
        .owner          = THIS_MODULE,
+       .open           = rtrack_exclusive_open,
+       .release        = rtrack_exclusive_release,
+       .ioctl          = video_ioctl2,
+};
+
+static const struct v4l2_ioctl_ops rtrack_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 rtrack_radio = {
        .name           = "RadioTrack radio",
-       .type           = VID_TYPE_TUNER,
-       .hardware       = 0,
        .fops           = &rtrack_fops,
+       .ioctl_ops      = &rtrack_ioctl_ops,
+       .release        = video_device_release_empty,
 };
 
 static int __init rtrack_init(void)
@@ -390,10 +428,9 @@ static int __init rtrack_init(void)
                return -EBUSY;
        }
 
-       rtrack_radio.priv=&rtrack_unit;
+       video_set_drvdata(&rtrack_radio, &rtrack_unit);
 
-       if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
-       {
+       if (video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr) < 0) {
                release_region(io, 2);
                return -EINVAL;
        }