Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* SF16-FMI, SF16-FMP and SF16-FMD radio driver for Linux radio support
0003  * heavily based on rtrack driver...
0004  * (c) 1997 M. Kirkwood
0005  * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz
0006  *
0007  * Fitted to new interface by Alan Cox <alan@lxorguk.ukuu.org.uk>
0008  * Made working and cleaned up functions <mikael.hedin@irf.se>
0009  * Support for ISAPnP by Ladislav Michl <ladis@psi.cz>
0010  *
0011  * Notes on the hardware
0012  *
0013  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
0014  *  No volume control - only mute/unmute - you have to use line volume
0015  *  control on SB-part of SF16-FMI/SF16-FMP/SF16-FMD
0016  *
0017  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org>
0018  */
0019 
0020 #include <linux/kernel.h>   /* __setup          */
0021 #include <linux/module.h>   /* Modules          */
0022 #include <linux/init.h>     /* Initdata         */
0023 #include <linux/ioport.h>   /* request_region       */
0024 #include <linux/delay.h>    /* udelay           */
0025 #include <linux/isapnp.h>
0026 #include <linux/mutex.h>
0027 #include <linux/videodev2.h>    /* kernel radio structs     */
0028 #include <linux/io.h>       /* outb, outb_p         */
0029 #include <media/v4l2-device.h>
0030 #include <media/v4l2-ioctl.h>
0031 #include <media/v4l2-ctrls.h>
0032 #include <media/v4l2-event.h>
0033 #include "lm7000.h"
0034 
0035 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood");
0036 MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio.");
0037 MODULE_LICENSE("GPL");
0038 MODULE_VERSION("0.0.3");
0039 
0040 static int io = -1;
0041 static int radio_nr = -1;
0042 
0043 module_param(io, int, 0);
0044 MODULE_PARM_DESC(io, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)");
0045 module_param(radio_nr, int, 0);
0046 
0047 struct fmi
0048 {
0049     struct v4l2_device v4l2_dev;
0050     struct v4l2_ctrl_handler hdl;
0051     struct video_device vdev;
0052     int io;
0053     bool mute;
0054     u32 curfreq; /* freq in kHz */
0055     struct mutex lock;
0056 };
0057 
0058 static struct fmi fmi_card;
0059 static struct pnp_dev *dev;
0060 static bool pnp_attached;
0061 
0062 #define RSF16_MINFREQ (87U * 16000)
0063 #define RSF16_MAXFREQ (108U * 16000)
0064 
0065 #define FMI_BIT_TUN_CE      (1 << 0)
0066 #define FMI_BIT_TUN_CLK     (1 << 1)
0067 #define FMI_BIT_TUN_DATA    (1 << 2)
0068 #define FMI_BIT_VOL_SW      (1 << 3)
0069 #define FMI_BIT_TUN_STRQ    (1 << 4)
0070 
0071 static void fmi_set_pins(void *handle, u8 pins)
0072 {
0073     struct fmi *fmi = handle;
0074     u8 bits = FMI_BIT_TUN_STRQ;
0075 
0076     if (!fmi->mute)
0077         bits |= FMI_BIT_VOL_SW;
0078 
0079     if (pins & LM7000_DATA)
0080         bits |= FMI_BIT_TUN_DATA;
0081     if (pins & LM7000_CLK)
0082         bits |= FMI_BIT_TUN_CLK;
0083     if (pins & LM7000_CE)
0084         bits |= FMI_BIT_TUN_CE;
0085 
0086     mutex_lock(&fmi->lock);
0087     outb_p(bits, fmi->io);
0088     mutex_unlock(&fmi->lock);
0089 }
0090 
0091 static inline void fmi_mute(struct fmi *fmi)
0092 {
0093     mutex_lock(&fmi->lock);
0094     outb(0x00, fmi->io);
0095     mutex_unlock(&fmi->lock);
0096 }
0097 
0098 static inline void fmi_unmute(struct fmi *fmi)
0099 {
0100     mutex_lock(&fmi->lock);
0101     outb(0x08, fmi->io);
0102     mutex_unlock(&fmi->lock);
0103 }
0104 
0105 static inline int fmi_getsigstr(struct fmi *fmi)
0106 {
0107     int val;
0108     int res;
0109 
0110     mutex_lock(&fmi->lock);
0111     val = fmi->mute ? 0x00 : 0x08;  /* mute/unmute */
0112     outb(val, fmi->io);
0113     outb(val | 0x10, fmi->io);
0114     msleep(143);        /* was schedule_timeout(HZ/7) */
0115     res = (int)inb(fmi->io + 1);
0116     outb(val, fmi->io);
0117 
0118     mutex_unlock(&fmi->lock);
0119     return (res & 2) ? 0 : 0xFFFF;
0120 }
0121 
0122 static void fmi_set_freq(struct fmi *fmi)
0123 {
0124     fmi->curfreq = clamp(fmi->curfreq, RSF16_MINFREQ, RSF16_MAXFREQ);
0125     /* rounding in steps of 800 to match the freq
0126        that will be used */
0127     lm7000_set_freq((fmi->curfreq / 800) * 800, fmi, fmi_set_pins);
0128 }
0129 
0130 static int vidioc_querycap(struct file *file, void  *priv,
0131                     struct v4l2_capability *v)
0132 {
0133     strscpy(v->driver, "radio-sf16fmi", sizeof(v->driver));
0134     strscpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card));
0135     strscpy(v->bus_info, "ISA:radio-sf16fmi", sizeof(v->bus_info));
0136     return 0;
0137 }
0138 
0139 static int vidioc_g_tuner(struct file *file, void *priv,
0140                     struct v4l2_tuner *v)
0141 {
0142     struct fmi *fmi = video_drvdata(file);
0143 
0144     if (v->index > 0)
0145         return -EINVAL;
0146 
0147     strscpy(v->name, "FM", sizeof(v->name));
0148     v->type = V4L2_TUNER_RADIO;
0149     v->rangelow = RSF16_MINFREQ;
0150     v->rangehigh = RSF16_MAXFREQ;
0151     v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
0152     v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW;
0153     v->audmode = V4L2_TUNER_MODE_STEREO;
0154     v->signal = fmi_getsigstr(fmi);
0155     return 0;
0156 }
0157 
0158 static int vidioc_s_tuner(struct file *file, void *priv,
0159                     const struct v4l2_tuner *v)
0160 {
0161     return v->index ? -EINVAL : 0;
0162 }
0163 
0164 static int vidioc_s_frequency(struct file *file, void *priv,
0165                     const struct v4l2_frequency *f)
0166 {
0167     struct fmi *fmi = video_drvdata(file);
0168 
0169     if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
0170         return -EINVAL;
0171 
0172     fmi->curfreq = f->frequency;
0173     fmi_set_freq(fmi);
0174 
0175     return 0;
0176 }
0177 
0178 static int vidioc_g_frequency(struct file *file, void *priv,
0179                     struct v4l2_frequency *f)
0180 {
0181     struct fmi *fmi = video_drvdata(file);
0182 
0183     if (f->tuner != 0)
0184         return -EINVAL;
0185     f->type = V4L2_TUNER_RADIO;
0186     f->frequency = fmi->curfreq;
0187     return 0;
0188 }
0189 
0190 static int fmi_s_ctrl(struct v4l2_ctrl *ctrl)
0191 {
0192     struct fmi *fmi = container_of(ctrl->handler, struct fmi, hdl);
0193 
0194     switch (ctrl->id) {
0195     case V4L2_CID_AUDIO_MUTE:
0196         if (ctrl->val)
0197             fmi_mute(fmi);
0198         else
0199             fmi_unmute(fmi);
0200         fmi->mute = ctrl->val;
0201         return 0;
0202     }
0203     return -EINVAL;
0204 }
0205 
0206 static const struct v4l2_ctrl_ops fmi_ctrl_ops = {
0207     .s_ctrl = fmi_s_ctrl,
0208 };
0209 
0210 static const struct v4l2_file_operations fmi_fops = {
0211     .owner      = THIS_MODULE,
0212     .open       = v4l2_fh_open,
0213     .release    = v4l2_fh_release,
0214     .poll       = v4l2_ctrl_poll,
0215     .unlocked_ioctl = video_ioctl2,
0216 };
0217 
0218 static const struct v4l2_ioctl_ops fmi_ioctl_ops = {
0219     .vidioc_querycap    = vidioc_querycap,
0220     .vidioc_g_tuner     = vidioc_g_tuner,
0221     .vidioc_s_tuner     = vidioc_s_tuner,
0222     .vidioc_g_frequency = vidioc_g_frequency,
0223     .vidioc_s_frequency = vidioc_s_frequency,
0224     .vidioc_log_status  = v4l2_ctrl_log_status,
0225     .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
0226     .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
0227 };
0228 
0229 /* ladis: this is my card. does any other types exist? */
0230 static struct isapnp_device_id id_table[] = {
0231         /* SF16-FMI */
0232     {   ISAPNP_ANY_ID, ISAPNP_ANY_ID,
0233         ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0},
0234         /* SF16-FMD */
0235     {   ISAPNP_ANY_ID, ISAPNP_ANY_ID,
0236         ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0},
0237     {   ISAPNP_CARD_END, },
0238 };
0239 
0240 MODULE_DEVICE_TABLE(isapnp, id_table);
0241 
0242 static int __init isapnp_fmi_probe(void)
0243 {
0244     int i = 0;
0245 
0246     while (id_table[i].card_vendor != 0 && dev == NULL) {
0247         dev = pnp_find_dev(NULL, id_table[i].vendor,
0248                    id_table[i].function, NULL);
0249         i++;
0250     }
0251 
0252     if (!dev)
0253         return -ENODEV;
0254     if (pnp_device_attach(dev) < 0)
0255         return -EAGAIN;
0256     if (pnp_activate_dev(dev) < 0) {
0257         printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n");
0258         pnp_device_detach(dev);
0259         return -ENOMEM;
0260     }
0261     if (!pnp_port_valid(dev, 0)) {
0262         pnp_device_detach(dev);
0263         return -ENODEV;
0264     }
0265 
0266     i = pnp_port_start(dev, 0);
0267     printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i);
0268 
0269     return i;
0270 }
0271 
0272 static int __init fmi_init(void)
0273 {
0274     struct fmi *fmi = &fmi_card;
0275     struct v4l2_device *v4l2_dev = &fmi->v4l2_dev;
0276     struct v4l2_ctrl_handler *hdl = &fmi->hdl;
0277     int res, i;
0278     static const int probe_ports[] = { 0, 0x284, 0x384 };
0279 
0280     if (io < 0) {
0281         for (i = 0; i < ARRAY_SIZE(probe_ports); i++) {
0282             io = probe_ports[i];
0283             if (io == 0) {
0284                 io = isapnp_fmi_probe();
0285                 if (io < 0)
0286                     continue;
0287                 pnp_attached = true;
0288             }
0289             if (!request_region(io, 2, "radio-sf16fmi")) {
0290                 if (pnp_attached)
0291                     pnp_device_detach(dev);
0292                 io = -1;
0293                 continue;
0294             }
0295             if (pnp_attached ||
0296                 ((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0))
0297                 break;
0298             release_region(io, 2);
0299             io = -1;
0300         }
0301     } else {
0302         if (!request_region(io, 2, "radio-sf16fmi")) {
0303             printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io);
0304             return -EBUSY;
0305         }
0306         if (inb(io) == 0xff) {
0307             printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io);
0308             release_region(io, 2);
0309             return -ENODEV;
0310         }
0311     }
0312     if (io < 0) {
0313         printk(KERN_ERR "radio-sf16fmi: no cards found\n");
0314         return -ENODEV;
0315     }
0316 
0317     strscpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name));
0318     fmi->io = io;
0319 
0320     res = v4l2_device_register(NULL, v4l2_dev);
0321     if (res < 0) {
0322         release_region(fmi->io, 2);
0323         if (pnp_attached)
0324             pnp_device_detach(dev);
0325         v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
0326         return res;
0327     }
0328 
0329     v4l2_ctrl_handler_init(hdl, 1);
0330     v4l2_ctrl_new_std(hdl, &fmi_ctrl_ops,
0331             V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
0332     v4l2_dev->ctrl_handler = hdl;
0333     if (hdl->error) {
0334         res = hdl->error;
0335         v4l2_err(v4l2_dev, "Could not register controls\n");
0336         v4l2_ctrl_handler_free(hdl);
0337         v4l2_device_unregister(v4l2_dev);
0338         return res;
0339     }
0340 
0341     strscpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name));
0342     fmi->vdev.v4l2_dev = v4l2_dev;
0343     fmi->vdev.fops = &fmi_fops;
0344     fmi->vdev.ioctl_ops = &fmi_ioctl_ops;
0345     fmi->vdev.release = video_device_release_empty;
0346     fmi->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO;
0347     video_set_drvdata(&fmi->vdev, fmi);
0348 
0349     mutex_init(&fmi->lock);
0350 
0351     /* mute card and set default frequency */
0352     fmi->mute = true;
0353     fmi->curfreq = RSF16_MINFREQ;
0354     fmi_set_freq(fmi);
0355 
0356     if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) {
0357         v4l2_ctrl_handler_free(hdl);
0358         v4l2_device_unregister(v4l2_dev);
0359         release_region(fmi->io, 2);
0360         if (pnp_attached)
0361             pnp_device_detach(dev);
0362         return -EINVAL;
0363     }
0364 
0365     v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io);
0366     return 0;
0367 }
0368 
0369 static void __exit fmi_exit(void)
0370 {
0371     struct fmi *fmi = &fmi_card;
0372 
0373     v4l2_ctrl_handler_free(&fmi->hdl);
0374     video_unregister_device(&fmi->vdev);
0375     v4l2_device_unregister(&fmi->v4l2_dev);
0376     release_region(fmi->io, 2);
0377     if (dev && pnp_attached)
0378         pnp_device_detach(dev);
0379 }
0380 
0381 module_init(fmi_init);
0382 module_exit(fmi_exit);