V4L/DVB (13177): radio: Add support for TEF6862 tuner
[safe/jmp/linux-2.6] / drivers / media / radio / radio-sf16fmr2.c
index e6c125d..a11414f 100644 (file)
 #include <linux/init.h>                /* Initdata                     */
 #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/videodev2.h>   /* kernel radio structs         */
-#include <media/v4l2-common.h>
 #include <linux/mutex.h>
+#include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
+#include <linux/io.h>          /* outb, outb_p                 */
+#include <media/v4l2-device.h>
+#include <media/v4l2-ioctl.h>
+
+MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
+MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
+MODULE_LICENSE("GPL");
 
-static struct mutex lock;
+static int io = 0x384;
+static int radio_nr = -1;
+
+module_param(io, int, 0);
+MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
+module_param(radio_nr, int, 0);
 
-#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          = 1<<12,
-               .default_value = 0xff,
-               .type          = V4L2_CTRL_TYPE_INTEGER,
-       }
-};
+#define AUD_VOL_INDEX 1
 
 #undef DEBUG
 //#define DEBUG 1
@@ -58,171 +50,159 @@ static struct v4l2_queryctrl radio_qctrl[] = {
 #endif
 
 /* this should be static vars for module size */
-struct fmr2_device
+struct fmr2
 {
-       int port;
-       int curvol; /* 0-65535, if not volume 0 or 65535 */
+       struct v4l2_device v4l2_dev;
+       struct video_device vdev;
+       struct mutex lock;
+       int io;
+       int curvol; /* 0-15 */
        int mute;
        int stereo; /* card is producing stereo audio */
        unsigned long curfreq; /* freq in kHz */
        int card_type;
-       __u32 flags;
 };
 
-static int io = 0x384;
-static int radio_nr = -1;
+static struct fmr2 fmr2_card;
 
 /* hw precision is 12.5 kHz
- * It is only useful to give freq in intervall of 200 (=0.0125Mhz),
+ * It is only useful to give freq in interval of 200 (=0.0125Mhz),
  * other bits will be truncated
  */
-#define RSF16_ENCODE(x)        ((x)/200+856)
-#define RSF16_MINFREQ 87*16000
-#define RSF16_MAXFREQ 108*16000
+#define RSF16_ENCODE(x)        ((x) / 200 + 856)
+#define RSF16_MINFREQ (87 * 16000)
+#define RSF16_MAXFREQ (108 * 16000)
 
-static inline void wait(int n,int port)
+static inline void wait(int n, int io)
 {
-       for (;n;--n) inb(port);
+       for (; n; --n)
+               inb(io);
 }
 
-static void outbits(int bits, unsigned int data, int nWait, int port)
+static void outbits(int bits, unsigned int data, int nWait, int io)
 {
        int bit;
-       for(;--bits>=0;) {
-               bit = (data>>bits) & 1;
-               outb(bit,port);
-               wait(nWait,port);
-               outb(bit|2,port);
-               wait(nWait,port);
-               outb(bit,port);
-               wait(nWait,port);
+
+       for (; --bits >= 0;) {
+               bit = (data >> bits) & 1;
+               outb(bit, io);
+               wait(nWait, io);
+               outb(bit | 2, io);
+               wait(nWait, io);
+               outb(bit, io);
+               wait(nWait, io);
        }
 }
 
-static inline void fmr2_mute(int port)
+static inline void fmr2_mute(int io)
 {
-       outb(0x00, port);
-       wait(4,port);
+       outb(0x00, io);
+       wait(4, io);
 }
 
-static inline void fmr2_unmute(int port)
+static inline void fmr2_unmute(int io)
 {
-       outb(0x04, port);
-       wait(4,port);
+       outb(0x04, io);
+       wait(4, io);
 }
 
-static inline int fmr2_stereo_mode(int port)
+static inline int fmr2_stereo_mode(int io)
 {
-       int n = inb(port);
-       outb(6,port);
-       inb(port);
-       n = ((n>>3)&1)^1;
+       int n = inb(io);
+
+       outb(6, io);
+       inb(io);
+       n = ((n >> 3) & 1) ^ 1;
        debug_print((KERN_DEBUG "stereo: %d\n", n));
        return n;
 }
 
-static int fmr2_product_info(struct fmr2_device *dev)
+static int fmr2_product_info(struct fmr2 *dev)
 {
-       int n = inb(dev->port);
+       int n = inb(dev->io);
+
        n &= 0xC1;
-       if (n == 0)
-       {
+       if (n == 0) {
                /* this should support volume set */
                dev->card_type = 12;
                return 0;
        }
        /* not volume (mine is 11) */
-       dev->card_type = (n==128)?11:0;
+       dev->card_type = (n == 128) ? 11 : 0;
        return n;
 }
 
-static inline int fmr2_getsigstr(struct fmr2_device *dev)
+static inline int fmr2_getsigstr(struct fmr2 *dev)
 {
-       /* !!! work only if scanning freq */
-       int port = dev->port, res = 0xffff;
-       outb(5,port);
-       wait(4,port);
-       if (!(inb(port)&1)) res = 0;
+       /* !!! works only if scanning freq */
+       int res = 0xffff;
+
+       outb(5, dev->io);
+       wait(4, dev->io);
+       if (!(inb(dev->io) & 1))
+               res = 0;
        debug_print((KERN_DEBUG "signal: %d\n", res));
        return res;
 }
 
 /* set frequency and unmute card */
-static int fmr2_setfreq(struct fmr2_device *dev)
+static int fmr2_setfreq(struct fmr2 *dev)
 {
-       int port = dev->port;
        unsigned long freq = dev->curfreq;
 
-       fmr2_mute(port);
+       fmr2_mute(dev->io);
 
        /* 0x42 for mono output
         * 0x102 forward scanning
         * 0x182 scansione avanti
         */
-       outbits(9,0x2,3,port);
-       outbits(16,RSF16_ENCODE(freq),2,port);
+       outbits(9, 0x2, 3, dev->io);
+       outbits(16, RSF16_ENCODE(freq), 2, dev->io);
 
-       fmr2_unmute(port);
+       fmr2_unmute(dev->io);
 
        /* wait 0.11 sec */
        msleep(110);
 
        /* NOTE if mute this stop radio
           you must set freq on unmute */
-       dev->stereo = fmr2_stereo_mode(port);
+       dev->stereo = fmr2_stereo_mode(dev->io);
        return 0;
 }
 
 /* !!! not tested, in my card this does't work !!! */
-static int fmr2_setvolume(struct fmr2_device *dev)
+static int fmr2_setvolume(struct fmr2 *dev)
 {
-       int i,a,n, port = dev->port;
-
-       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;)
-       {
-               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);
+       int vol[16] = { 0x021, 0x084, 0x090, 0x104,
+                       0x110, 0x204, 0x210, 0x402,
+                       0x404, 0x408, 0x410, 0x801,
+                       0x802, 0x804, 0x808, 0x810 };
+       int i, a;
+       int n = vol[dev->curvol & 0x0f];
+
+       if (dev->card_type != 11)
+               return 1;
+
+       for (i = 12; --i >= 0; ) {
+               a = ((n >> i) & 1) << 6; /* if (a==0) a = 0; else a = 0x40; */
+               outb(a | 4, dev->io);
+               wait(4, dev->io);
+               outb(a | 0x24, dev->io);
+               wait(4, dev->io);
+               outb(a | 4, dev->io);
+               wait(4, dev->io);
        }
-       for(i=6;--i>=0;)
-       {
+       for (i = 6; --i >= 0; ) {
                a = ((0x18 >> i) & 1) << 6;
-               outb(a|4, port);
-               wait(4,port);
-               outb(a|0x24, port);
-               wait(4,port);
-               outb(a|4, port);
-               wait(4,port);
+               outb(a | 4, dev->io);
+               wait(4, dev->io);
+               outb(a | 0x24, dev->io);
+               wait(4, dev->io);
+               outb(a | 4, dev->io);
+               wait(4, dev->io);
        }
-       wait(4,port);
-       outb(0x14, port);
-
+       wait(4, dev->io);
+       outb(0x14, dev->io);
        return 0;
 }
 
@@ -231,66 +211,58 @@ static int vidioc_querycap(struct file *file, void  *priv,
 {
        strlcpy(v->driver, "radio-sf16fmr2", sizeof(v->driver));
        strlcpy(v->card, "SF16-FMR2 radio", sizeof(v->card));
-       sprintf(v->bus_info, "ISA");
+       strlcpy(v->bus_info, "ISA", sizeof(v->bus_info));
        v->version = RADIO_VERSION;
-       v->capabilities = V4L2_CAP_TUNER;
+       v->capabilities = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
        return 0;
 }
 
 static int vidioc_g_tuner(struct file *file, void *priv,
                                        struct v4l2_tuner *v)
 {
-       int mult;
-       struct video_device *dev = video_devdata(file);
-       struct fmr2_device *fmr2 = dev->priv;
+       struct fmr2 *fmr2 = video_drvdata(file);
 
        if (v->index > 0)
                return -EINVAL;
 
-       strcpy(v->name, "FM");
+       strlcpy(v->name, "FM", sizeof(v->name));
        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->rangelow = RSF16_MINFREQ;
+       v->rangehigh = RSF16_MAXFREQ;
+       v->rxsubchans = fmr2->stereo ? V4L2_TUNER_SUB_STEREO :
+                                       V4L2_TUNER_SUB_MONO;
+       v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
+       v->audmode = V4L2_TUNER_MODE_STEREO;
+       mutex_lock(&fmr2->lock);
        v->signal = fmr2_getsigstr(fmr2);
-       mutex_unlock(&lock);
+       mutex_unlock(&fmr2->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;
+       return v->index ? -EINVAL : 0;
 }
 
 static int vidioc_s_frequency(struct file *file, void *priv,
                                        struct v4l2_frequency *f)
 {
-       struct video_device *dev = video_devdata(file);
-       struct fmr2_device *fmr2 = dev->priv;
+       struct fmr2 *fmr2 = video_drvdata(file);
 
-       if (!(fmr2->flags & V4L2_TUNER_CAP_LOW))
-               f->frequency *= 1000;
        if (f->frequency < RSF16_MINFREQ ||
-                       f->frequency > RSF16_MAXFREQ )
+                       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;
+       /* rounding in steps of 200 to match the 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);
+               mutex_lock(&fmr2->lock);
                fmr2_setfreq(fmr2);
-               mutex_unlock(&lock);
+               mutex_unlock(&fmr2->lock);
        }
        return 0;
 }
@@ -298,32 +270,26 @@ static int vidioc_s_frequency(struct file *file, void *priv,
 static int vidioc_g_frequency(struct file *file, void *priv,
                                        struct v4l2_frequency *f)
 {
-       struct video_device *dev = video_devdata(file);
-       struct fmr2_device *fmr2 = dev->priv;
+       struct fmr2 *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;
-       struct video_device *dev = video_devdata(file);
-       struct fmr2_device *fmr2 = dev->priv;
-
-       for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
-               if ((fmr2->card_type != 11)
-                               && V4L2_CID_AUDIO_VOLUME)
-                       radio_qctrl[i].step = 65535;
-               if (qc->id && qc->id == radio_qctrl[i].id) {
-                       memcpy(qc, &(radio_qctrl[i]),
-                                               sizeof(*qc));
-                       return 0;
-               }
+       struct fmr2 *fmr2 = video_drvdata(file);
+
+       switch (qc->id) {
+       case V4L2_CID_AUDIO_MUTE:
+               return v4l2_ctrl_query_fill(qc, 0, 1, 1, 1);
+       case V4L2_CID_AUDIO_VOLUME:
+               /* Only card_type == 11 implements volume */
+               if (fmr2->card_type == 11)
+                       return v4l2_ctrl_query_fill(qc, 0, 15, 1, 0);
+               return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
        }
        return -EINVAL;
 }
@@ -331,8 +297,7 @@ static int vidioc_queryctrl(struct file *file, void *priv,
 static int vidioc_g_ctrl(struct file *file, void *priv,
                                        struct v4l2_control *ctrl)
 {
-       struct video_device *dev = video_devdata(file);
-       struct fmr2_device *fmr2 = dev->priv;
+       struct fmr2 *fmr2 = video_drvdata(file);
 
        switch (ctrl->id) {
        case V4L2_CID_AUDIO_MUTE:
@@ -348,30 +313,14 @@ static int vidioc_g_ctrl(struct file *file, void *priv,
 static int vidioc_s_ctrl(struct file *file, void *priv,
                                        struct v4l2_control *ctrl)
 {
-       struct video_device *dev = video_devdata(file);
-       struct fmr2_device *fmr2 = dev->priv;
+       struct fmr2 *fmr2 = video_drvdata(file);
 
        switch (ctrl->id) {
        case V4L2_CID_AUDIO_MUTE:
                fmr2->mute = ctrl->value;
-               if (fmr2->card_type != 11) {
-                       if (!fmr2->mute)
-                               fmr2->curvol = 65535;
-                       else
-                               fmr2->curvol = 0;
-               }
                break;
        case V4L2_CID_AUDIO_VOLUME:
                fmr2->curvol = ctrl->value;
-               if (fmr2->card_type != 11) {
-                       if (fmr2->curvol) {
-                               fmr2->curvol = 65535;
-                               fmr2->mute = 0;
-                       } else {
-                               fmr2->curvol = 0;
-                               fmr2->mute = 1;
-                       }
-               }
                break;
        default:
                return -EINVAL;
@@ -384,24 +333,14 @@ static int vidioc_s_ctrl(struct file *file, void *priv,
                printk(KERN_DEBUG "mute\n");
 #endif
 
-       mutex_lock(&lock);
+       mutex_lock(&fmr2->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;
+               fmr2_mute(fmr2->io);
+       mutex_unlock(&fmr2->lock);
        return 0;
 }
 
@@ -413,37 +352,30 @@ static int vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
 
 static int vidioc_s_input(struct file *filp, void *priv, unsigned int i)
 {
-       if (i != 0)
-               return -EINVAL;
-       return 0;
+       return i ? -EINVAL : 0;
 }
 
-static int vidioc_s_audio(struct file *file, void *priv,
+static int vidioc_g_audio(struct file *file, void *priv,
                                        struct v4l2_audio *a)
 {
-       if (a->index != 0)
-               return -EINVAL;
+       a->index = 0;
+       strlcpy(a->name, "Radio", sizeof(a->name));
+       a->capability = V4L2_AUDCAP_STEREO;
        return 0;
 }
 
-static struct fmr2_device fmr2_unit;
+static int vidioc_s_audio(struct file *file, void *priv,
+                                       struct v4l2_audio *a)
+{
+       return a->index ? -EINVAL : 0;
+}
 
-static const struct file_operations fmr2_fops = {
+static const struct v4l2_file_operations fmr2_fops = {
        .owner          = THIS_MODULE,
-       .open           = video_exclusive_open,
-       .release        = video_exclusive_release,
        .ioctl          = video_ioctl2,
-       .compat_ioctl   = v4l_compat_ioctl32,
-       .llseek         = no_llseek,
 };
 
-static struct video_device fmr2_radio=
-{
-       .owner          = THIS_MODULE,
-       .name           = "SF16FMR2 radio",
-       . type          = VID_TYPE_TUNER,
-       .hardware       = 0,
-       .fops           = &fmr2_fops,
+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,
@@ -460,55 +392,61 @@ static struct video_device fmr2_radio=
 
 static int __init fmr2_init(void)
 {
-       fmr2_unit.port = io;
-       fmr2_unit.curvol = 0;
-       fmr2_unit.mute = 0;
-       fmr2_unit.curfreq = 0;
-       fmr2_unit.stereo = 1;
-       fmr2_unit.flags = V4L2_TUNER_CAP_LOW;
-       fmr2_unit.card_type = 0;
-       fmr2_radio.priv = &fmr2_unit;
-
-       mutex_init(&lock);
-
-       if (request_region(io, 2, "sf16fmr2"))
-       {
-               printk(KERN_ERR "fmr2: port 0x%x already in use\n", io);
+       struct fmr2 *fmr2 = &fmr2_card;
+       struct v4l2_device *v4l2_dev = &fmr2->v4l2_dev;
+       int res;
+
+       strlcpy(v4l2_dev->name, "sf16fmr2", sizeof(v4l2_dev->name));
+       fmr2->io = io;
+       fmr2->stereo = 1;
+       mutex_init(&fmr2->lock);
+
+       if (!request_region(fmr2->io, 2, "sf16fmr2")) {
+               v4l2_err(v4l2_dev, "request_region failed!\n");
                return -EBUSY;
        }
 
-       if(video_register_device(&fmr2_radio, VFL_TYPE_RADIO, radio_nr)==-1)
-       {
-               release_region(io, 2);
+       res = v4l2_device_register(NULL, v4l2_dev);
+       if (res < 0) {
+               release_region(fmr2->io, 2);
+               v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
+               return res;
+       }
+
+       strlcpy(fmr2->vdev.name, v4l2_dev->name, sizeof(fmr2->vdev.name));
+       fmr2->vdev.v4l2_dev = v4l2_dev;
+       fmr2->vdev.fops = &fmr2_fops;
+       fmr2->vdev.ioctl_ops = &fmr2_ioctl_ops;
+       fmr2->vdev.release = video_device_release_empty;
+       video_set_drvdata(&fmr2->vdev, fmr2);
+
+       if (video_register_device(&fmr2->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
+               v4l2_device_unregister(v4l2_dev);
+               release_region(fmr2->io, 2);
                return -EINVAL;
        }
 
-       printk(KERN_INFO "SF16FMR2 radio card driver at 0x%x.\n", io);
+       v4l2_info(v4l2_dev, "SF16FMR2 radio card driver at 0x%x.\n", fmr2->io);
        /* mute card - prevents noisy bootups */
-       mutex_lock(&lock);
-       fmr2_mute(io);
-       fmr2_product_info(&fmr2_unit);
-       mutex_unlock(&lock);
-       debug_print((KERN_DEBUG "card_type %d\n", fmr2_unit.card_type));
+       mutex_lock(&fmr2->lock);
+       fmr2_mute(fmr2->io);
+       fmr2_product_info(fmr2);
+       mutex_unlock(&fmr2->lock);
+       debug_print((KERN_DEBUG "card_type %d\n", fmr2->card_type));
        return 0;
 }
 
-MODULE_AUTHOR("Ziglio Frediano, freddy77@angelfire.com");
-MODULE_DESCRIPTION("A driver for the SF16FMR2 radio.");
-MODULE_LICENSE("GPL");
-
-module_param(io, int, 0);
-MODULE_PARM_DESC(io, "I/O address of the SF16FMR2 card (should be 0x384, if do not work try 0x284)");
-module_param(radio_nr, int, 0);
-
-static void __exit fmr2_cleanup_module(void)
+static void __exit fmr2_exit(void)
 {
-       video_unregister_device(&fmr2_radio);
-       release_region(io,2);
+       struct fmr2 *fmr2 = &fmr2_card;
+
+       video_unregister_device(&fmr2->vdev);
+       v4l2_device_unregister(&fmr2->v4l2_dev);
+       release_region(fmr2->io, 2);
 }
 
 module_init(fmr2_init);
-module_exit(fmr2_cleanup_module);
+module_exit(fmr2_exit);
 
 #ifndef MODULE