1 /* radiotrack (radioreveal) driver for Linux radio support
 
   3  * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org>
 
   4  * Converted to new API by Alan Cox <Alan.Cox@linux.org>
 
   5  * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org>
 
   8  * 1999-02-24   Russell Kroll <rkroll@exploits.org>
 
   9  *              Fine tuning/VIDEO_TUNER_LOW
 
  10  *              Frequency range expanded to start at 87 MHz
 
  12  * TODO: Allow for more than one of these foolish entities :-)
 
  14  * Notes on the hardware (reverse engineered from other peoples'
 
  15  * reverse engineering of AIMS' code :-)
 
  17  *  Frequency control is done digitally -- ie out(port,encodefreq(95.8));
 
  19  *  The signal strength query is unsurprisingly inaccurate.  And it seems
 
  20  *  to indicate that (on my card, at least) the frequency setting isn't
 
  21  *  too great.  (I have to tune up .025MHz from what the freq should be
 
  22  *  to get a report that the thing is tuned.)
 
  24  *  Volume control is (ugh) analogue:
 
  25  *   out(port, start_increasing_volume);
 
  27  *   out(port, stop_changing_the_volume);
 
  31 #include <linux/module.h>       /* Modules                      */
 
  32 #include <linux/init.h>         /* Initdata                     */
 
  33 #include <linux/ioport.h>       /* request_region               */
 
  34 #include <linux/delay.h>        /* udelay                       */
 
  35 #include <asm/io.h>             /* outb, outb_p                 */
 
  36 #include <asm/uaccess.h>        /* copy to/from user            */
 
  37 #include <linux/videodev2.h>    /* kernel radio structs         */
 
  38 #include <media/v4l2-common.h>
 
  39 #include <asm/semaphore.h>      /* Lock for the I/O             */
 
  41 #include <linux/version.h>      /* for KERNEL_VERSION MACRO     */
 
  42 #define RADIO_VERSION KERNEL_VERSION(0,0,2)
 
  44 #ifndef CONFIG_RADIO_RTRACK_PORT
 
  45 #define CONFIG_RADIO_RTRACK_PORT -1
 
  48 static int io = CONFIG_RADIO_RTRACK_PORT;
 
  49 static int radio_nr = -1;
 
  50 static struct mutex lock;
 
  56         unsigned long curfreq;
 
  63 static void sleep_delay(long n)
 
  65         /* Sleep nicely for 'n' uS */
 
  70                 msleep(jiffies_to_msecs(d));
 
  73 static void rt_decvol(void)
 
  75         outb(0x58, io);         /* volume down + sigstr + on    */
 
  77         outb(0xd8, io);         /* volume steady + sigstr + on  */
 
  80 static void rt_incvol(void)
 
  82         outb(0x98, io);         /* volume up + sigstr + on      */
 
  84         outb(0xd8, io);         /* volume steady + sigstr + on  */
 
  87 static void rt_mute(struct rt_device *dev)
 
  91         outb(0xd0, io);                 /* volume steady, off           */
 
  95 static int rt_setvol(struct rt_device *dev, int vol)
 
 101         if(vol == dev->curvol) {        /* requested volume = current */
 
 102                 if (dev->muted) {       /* user is unmuting the card  */
 
 104                         outb (0xd8, io);        /* enable card */
 
 110         if(vol == 0) {                  /* volume = 0 means mute the card */
 
 111                 outb(0x48, io);         /* volume down but still "on"   */
 
 112                 sleep_delay(2000000);   /* make sure it's totally down  */
 
 113                 outb(0xd0, io);         /* volume steady, off           */
 
 114                 dev->curvol = 0;        /* track the volume state!      */
 
 120         if(vol > dev->curvol)
 
 121                 for(i = dev->curvol; i < vol; i++)
 
 124                 for(i = dev->curvol; i > vol; i--)
 
 132 /* the 128+64 on these outb's is to keep the volume stable while tuning
 
 133  * without them, the volume _will_ creep up with each frequency change
 
 134  * and bit 4 (+16) is to keep the signal strength meter enabled
 
 137 static void send_0_byte(int port, struct rt_device *dev)
 
 139         if ((dev->curvol == 0) || (dev->muted)) {
 
 140                 outb_p(128+64+16+  1, port);   /* wr-enable + data low */
 
 141                 outb_p(128+64+16+2+1, port);   /* clock */
 
 144                 outb_p(128+64+16+8+  1, port);  /* on + wr-enable + data low */
 
 145                 outb_p(128+64+16+8+2+1, port);  /* clock */
 
 150 static void send_1_byte(int port, struct rt_device *dev)
 
 152         if ((dev->curvol == 0) || (dev->muted)) {
 
 153                 outb_p(128+64+16+4  +1, port);   /* wr-enable+data high */
 
 154                 outb_p(128+64+16+4+2+1, port);   /* clock */
 
 157                 outb_p(128+64+16+8+4  +1, port); /* on+wr-enable+data high */
 
 158                 outb_p(128+64+16+8+4+2+1, port); /* clock */
 
 164 static int rt_setfreq(struct rt_device *dev, unsigned long freq)
 
 168         /* adapted from radio-aztech.c */
 
 170         /* now uses VIDEO_TUNER_LOW for fine tuning */
 
 172         freq += 171200;                 /* Add 10.7 MHz IF              */
 
 173         freq /= 800;                    /* Convert to 50 kHz units      */
 
 175         mutex_lock(&lock);                      /* Stop other ops interfering */
 
 177         send_0_byte (io, dev);          /*  0: LSB of frequency         */
 
 179         for (i = 0; i < 13; i++)        /*   : frequency bits (1-13)    */
 
 181                         send_1_byte (io, dev);
 
 183                         send_0_byte (io, dev);
 
 185         send_0_byte (io, dev);          /* 14: test bit - always 0    */
 
 186         send_0_byte (io, dev);          /* 15: test bit - always 0    */
 
 188         send_0_byte (io, dev);          /* 16: band data 0 - always 0 */
 
 189         send_0_byte (io, dev);          /* 17: band data 1 - always 0 */
 
 190         send_0_byte (io, dev);          /* 18: band data 2 - always 0 */
 
 191         send_0_byte (io, dev);          /* 19: time base - always 0   */
 
 193         send_0_byte (io, dev);          /* 20: spacing (0 = 25 kHz)   */
 
 194         send_1_byte (io, dev);          /* 21: spacing (1 = 25 kHz)   */
 
 195         send_0_byte (io, dev);          /* 22: spacing (0 = 25 kHz)   */
 
 196         send_1_byte (io, dev);          /* 23: AM/FM (FM = 1, always) */
 
 198         if ((dev->curvol == 0) || (dev->muted))
 
 199                 outb (0xd0, io);        /* volume steady + sigstr */
 
 201                 outb (0xd8, io);        /* volume steady + sigstr + on */
 
 208 static int rt_getsigstr(struct rt_device *dev)
 
 210         if (inb(io) & 2)        /* bit set = no signal present  */
 
 212         return 1;               /* signal present               */
 
 215 static struct v4l2_queryctrl radio_qctrl[] = {
 
 217                 .id            = V4L2_CID_AUDIO_MUTE,
 
 222                 .type          = V4L2_CTRL_TYPE_BOOLEAN,
 
 224                 .id            = V4L2_CID_AUDIO_VOLUME,
 
 229                 .default_value = 0xff,
 
 230                 .type          = V4L2_CTRL_TYPE_INTEGER,
 
 234 static int rt_do_ioctl(struct inode *inode, struct file *file,
 
 235                        unsigned int cmd, void *arg)
 
 237         struct video_device *dev = video_devdata(file);
 
 238         struct rt_device *rt=dev->priv;
 
 242                 case VIDIOC_QUERYCAP:
 
 244                         struct v4l2_capability *v = arg;
 
 245                         memset(v,0,sizeof(*v));
 
 246                         strlcpy(v->driver, "radio-aimslab", sizeof (v->driver));
 
 247                         strlcpy(v->card, "RadioTrack", sizeof (v->card));
 
 248                         sprintf(v->bus_info,"ISA");
 
 249                         v->version = RADIO_VERSION;
 
 250                         v->capabilities = V4L2_CAP_TUNER;
 
 256                         struct v4l2_tuner *v = arg;
 
 261                         memset(v,0,sizeof(*v));
 
 262                         strcpy(v->name, "FM");
 
 263                         v->type = V4L2_TUNER_RADIO;
 
 265                         v->rangelow=(87*16000);
 
 266                         v->rangehigh=(108*16000);
 
 267                         v->rxsubchans =V4L2_TUNER_SUB_MONO;
 
 268                         v->capability=V4L2_TUNER_CAP_LOW;
 
 269                         v->audmode = V4L2_TUNER_MODE_MONO;
 
 270                         v->signal=0xFFFF*rt_getsigstr(rt);
 
 276                         struct v4l2_tuner *v = arg;
 
 283                 case VIDIOC_S_FREQUENCY:
 
 285                         struct v4l2_frequency *f = arg;
 
 287                         rt->curfreq = f->frequency;
 
 288                         rt_setfreq(rt, rt->curfreq);
 
 291                 case VIDIOC_G_FREQUENCY:
 
 293                         struct v4l2_frequency *f = arg;
 
 295                         f->type = V4L2_TUNER_RADIO;
 
 296                         f->frequency = rt->curfreq;
 
 300                 case VIDIOC_QUERYCTRL:
 
 302                         struct v4l2_queryctrl *qc = arg;
 
 305                         for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) {
 
 306                                 if (qc->id && qc->id == radio_qctrl[i].id) {
 
 307                                         memcpy(qc, &(radio_qctrl[i]),
 
 316                         struct v4l2_control *ctrl= arg;
 
 319                                 case V4L2_CID_AUDIO_MUTE:
 
 320                                         ctrl->value=rt->muted;
 
 322                                 case V4L2_CID_AUDIO_VOLUME:
 
 323                                         ctrl->value=rt->curvol * 6554;
 
 330                         struct v4l2_control *ctrl= arg;
 
 333                                 case V4L2_CID_AUDIO_MUTE:
 
 337                                                 rt_setvol(rt,rt->curvol);
 
 340                                 case V4L2_CID_AUDIO_VOLUME:
 
 341                                         rt_setvol(rt,ctrl->value);
 
 348                         return v4l_compat_translate_ioctl(inode,file,cmd,arg,
 
 353 static int rt_ioctl(struct inode *inode, struct file *file,
 
 354                     unsigned int cmd, unsigned long arg)
 
 356         return video_usercopy(inode, file, cmd, arg, rt_do_ioctl);
 
 359 static struct rt_device rtrack_unit;
 
 361 static const struct file_operations rtrack_fops = {
 
 362         .owner          = THIS_MODULE,
 
 363         .open           = video_exclusive_open,
 
 364         .release        = video_exclusive_release,
 
 366         .compat_ioctl   = v4l_compat_ioctl32,
 
 370 static struct video_device rtrack_radio=
 
 372         .owner          = THIS_MODULE,
 
 373         .name           = "RadioTrack radio",
 
 374         .type           = VID_TYPE_TUNER,
 
 376         .fops           = &rtrack_fops,
 
 379 static int __init rtrack_init(void)
 
 383                 printk(KERN_ERR "You must set an I/O address with io=0x???\n");
 
 387         if (!request_region(io, 2, "rtrack"))
 
 389                 printk(KERN_ERR "rtrack: port 0x%x already in use\n", io);
 
 393         rtrack_radio.priv=&rtrack_unit;
 
 395         if(video_register_device(&rtrack_radio, VFL_TYPE_RADIO, radio_nr)==-1)
 
 397                 release_region(io, 2);
 
 400         printk(KERN_INFO "AIMSlab RadioTrack/RadioReveal card driver.\n");
 
 402         /* Set up the I/O locking */
 
 406         /* mute card - prevents noisy bootups */
 
 408         /* this ensures that the volume is all the way down  */
 
 409         outb(0x48, io);         /* volume down but still "on"   */
 
 410         sleep_delay(2000000);   /* make sure it's totally down  */
 
 411         outb(0xc0, io);         /* steady volume, mute card     */
 
 412         rtrack_unit.curvol = 0;
 
 417 MODULE_AUTHOR("M.Kirkwood");
 
 418 MODULE_DESCRIPTION("A driver for the RadioTrack/RadioReveal radio card.");
 
 419 MODULE_LICENSE("GPL");
 
 421 module_param(io, int, 0);
 
 422 MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20f or 0x30f)");
 
 423 module_param(radio_nr, int, 0);
 
 425 static void __exit cleanup_rtrack_module(void)
 
 427         video_unregister_device(&rtrack_radio);
 
 428         release_region(io,2);
 
 431 module_init(rtrack_init);
 
 432 module_exit(cleanup_rtrack_module);