Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* RadioTrack II driver for Linux radio support (C) 1998 Ben Pfaff |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 2 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Based on RadioTrack I/RadioReveal (C) 1997 M. Kirkwood |
| 4 | * Converted to new API by Alan Cox <Alan.Cox@linux.org> |
| 5 | * Various bugfixes and enhancements by Russell Kroll <rkroll@exploits.org> |
| 6 | * |
| 7 | * TODO: Allow for more than one of these foolish entities :-) |
| 8 | * |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 9 | * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> /* Modules */ |
| 13 | #include <linux/init.h> /* Initdata */ |
Peter Osterlund | fb911ee | 2005-09-13 01:25:15 -0700 | [diff] [blame] | 14 | #include <linux/ioport.h> /* request_region */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/delay.h> /* udelay */ |
| 16 | #include <asm/io.h> /* outb, outb_p */ |
| 17 | #include <asm/uaccess.h> /* copy to/from user */ |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 18 | #include <linux/videodev2.h> /* kernel radio structs */ |
Mauro Carvalho Chehab | 5e87efa | 2006-06-05 10:26:32 -0300 | [diff] [blame] | 19 | #include <media/v4l2-common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
| 21 | |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 22 | #include <linux/version.h> /* for KERNEL_VERSION MACRO */ |
| 23 | #define RADIO_VERSION KERNEL_VERSION(0,0,2) |
| 24 | |
| 25 | static struct v4l2_queryctrl radio_qctrl[] = { |
| 26 | { |
| 27 | .id = V4L2_CID_AUDIO_MUTE, |
| 28 | .name = "Mute", |
| 29 | .minimum = 0, |
| 30 | .maximum = 1, |
| 31 | .default_value = 1, |
| 32 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 33 | },{ |
| 34 | .id = V4L2_CID_AUDIO_VOLUME, |
| 35 | .name = "Volume", |
| 36 | .minimum = 0, |
| 37 | .maximum = 65535, |
| 38 | .step = 65535, |
| 39 | .default_value = 0xff, |
| 40 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 41 | } |
| 42 | }; |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #ifndef CONFIG_RADIO_RTRACK2_PORT |
| 45 | #define CONFIG_RADIO_RTRACK2_PORT -1 |
| 46 | #endif |
| 47 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 48 | static int io = CONFIG_RADIO_RTRACK2_PORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | static int radio_nr = -1; |
| 50 | static spinlock_t lock; |
| 51 | |
| 52 | struct rt_device |
| 53 | { |
| 54 | int port; |
| 55 | unsigned long curfreq; |
| 56 | int muted; |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | /* local things */ |
| 61 | |
| 62 | static void rt_mute(struct rt_device *dev) |
| 63 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 64 | if(dev->muted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return; |
| 66 | spin_lock(&lock); |
| 67 | outb(1, io); |
| 68 | spin_unlock(&lock); |
| 69 | dev->muted = 1; |
| 70 | } |
| 71 | |
| 72 | static void rt_unmute(struct rt_device *dev) |
| 73 | { |
| 74 | if(dev->muted == 0) |
| 75 | return; |
| 76 | spin_lock(&lock); |
| 77 | outb(0, io); |
| 78 | spin_unlock(&lock); |
| 79 | dev->muted = 0; |
| 80 | } |
| 81 | |
| 82 | static void zero(void) |
| 83 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 84 | outb_p(1, io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | outb_p(3, io); |
| 86 | outb_p(1, io); |
| 87 | } |
| 88 | |
| 89 | static void one(void) |
| 90 | { |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 91 | outb_p(5, io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | outb_p(7, io); |
| 93 | outb_p(5, io); |
| 94 | } |
| 95 | |
| 96 | static int rt_setfreq(struct rt_device *dev, unsigned long freq) |
| 97 | { |
| 98 | int i; |
| 99 | |
| 100 | freq = freq / 200 + 856; |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | spin_lock(&lock); |
| 103 | |
| 104 | outb_p(0xc8, io); |
| 105 | outb_p(0xc9, io); |
| 106 | outb_p(0xc9, io); |
| 107 | |
| 108 | for (i = 0; i < 10; i++) |
| 109 | zero (); |
| 110 | |
| 111 | for (i = 14; i >= 0; i--) |
| 112 | if (freq & (1 << i)) |
| 113 | one (); |
| 114 | else |
| 115 | zero (); |
| 116 | |
| 117 | outb_p(0xc8, io); |
| 118 | if (!dev->muted) |
| 119 | outb_p(0, io); |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | spin_unlock(&lock); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int rt_getsigstr(struct rt_device *dev) |
| 126 | { |
| 127 | if (inb(io) & 2) /* bit set = no signal present */ |
| 128 | return 0; |
| 129 | return 1; /* signal present */ |
| 130 | } |
| 131 | |
| 132 | static int rt_do_ioctl(struct inode *inode, struct file *file, |
| 133 | unsigned int cmd, void *arg) |
| 134 | { |
| 135 | struct video_device *dev = video_devdata(file); |
| 136 | struct rt_device *rt=dev->priv; |
| 137 | |
| 138 | switch(cmd) |
| 139 | { |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 140 | case VIDIOC_QUERYCAP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 142 | struct v4l2_capability *v = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | memset(v,0,sizeof(*v)); |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 144 | strlcpy(v->driver, "radio-rtrack2", sizeof (v->driver)); |
| 145 | strlcpy(v->card, "RadioTrack II", sizeof (v->card)); |
| 146 | sprintf(v->bus_info,"ISA"); |
| 147 | v->version = RADIO_VERSION; |
| 148 | v->capabilities = V4L2_CAP_TUNER; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return 0; |
| 151 | } |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 152 | case VIDIOC_G_TUNER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 154 | struct v4l2_tuner *v = arg; |
| 155 | |
| 156 | if (v->index > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | return -EINVAL; |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 158 | |
| 159 | memset(v,0,sizeof(*v)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | strcpy(v->name, "FM"); |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 161 | v->type = V4L2_TUNER_RADIO; |
| 162 | |
| 163 | v->rangelow=(88*16000); |
| 164 | v->rangehigh=(108*16000); |
| 165 | v->rxsubchans =V4L2_TUNER_SUB_MONO; |
| 166 | v->capability=V4L2_TUNER_CAP_LOW; |
| 167 | v->audmode = V4L2_TUNER_MODE_MONO; |
| 168 | v->signal=0xFFFF*rt_getsigstr(rt); |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | } |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 172 | case VIDIOC_S_TUNER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 174 | struct v4l2_tuner *v = arg; |
| 175 | |
| 176 | if (v->index > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return -EINVAL; |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 181 | case VIDIOC_S_FREQUENCY: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 183 | struct v4l2_frequency *f = arg; |
| 184 | |
| 185 | rt->curfreq = f->frequency; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | rt_setfreq(rt, rt->curfreq); |
| 187 | return 0; |
| 188 | } |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 189 | case VIDIOC_G_FREQUENCY: |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 190 | { |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 191 | struct v4l2_frequency *f = arg; |
| 192 | |
| 193 | f->type = V4L2_TUNER_RADIO; |
| 194 | f->frequency = rt->curfreq; |
| 195 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 196 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 198 | case VIDIOC_QUERYCTRL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 200 | struct v4l2_queryctrl *qc = arg; |
| 201 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 203 | for (i = 0; i < ARRAY_SIZE(radio_qctrl); i++) { |
| 204 | if (qc->id && qc->id == radio_qctrl[i].id) { |
| 205 | memcpy(qc, &(radio_qctrl[i]), |
| 206 | sizeof(*qc)); |
| 207 | return (0); |
| 208 | } |
| 209 | } |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | case VIDIOC_G_CTRL: |
| 213 | { |
| 214 | struct v4l2_control *ctrl= arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 216 | switch (ctrl->id) { |
| 217 | case V4L2_CID_AUDIO_MUTE: |
| 218 | ctrl->value=rt->muted; |
| 219 | return (0); |
| 220 | case V4L2_CID_AUDIO_VOLUME: |
| 221 | if (rt->muted) |
| 222 | ctrl->value=0; |
| 223 | else |
| 224 | ctrl->value=65535; |
| 225 | return (0); |
| 226 | } |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | case VIDIOC_S_CTRL: |
| 230 | { |
| 231 | struct v4l2_control *ctrl= arg; |
| 232 | |
| 233 | switch (ctrl->id) { |
| 234 | case V4L2_CID_AUDIO_MUTE: |
| 235 | if (ctrl->value) { |
| 236 | rt_mute(rt); |
| 237 | } else { |
| 238 | rt_unmute(rt); |
| 239 | } |
| 240 | return (0); |
| 241 | case V4L2_CID_AUDIO_VOLUME: |
| 242 | if (ctrl->value) { |
| 243 | rt_unmute(rt); |
| 244 | } else { |
| 245 | rt_mute(rt); |
| 246 | } |
| 247 | return (0); |
| 248 | } |
| 249 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | default: |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 252 | return v4l_compat_translate_ioctl(inode,file,cmd,arg, |
| 253 | rt_do_ioctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | static int rt_ioctl(struct inode *inode, struct file *file, |
| 258 | unsigned int cmd, unsigned long arg) |
| 259 | { |
| 260 | return video_usercopy(inode, file, cmd, arg, rt_do_ioctl); |
| 261 | } |
| 262 | |
| 263 | static struct rt_device rtrack2_unit; |
| 264 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 265 | static const struct file_operations rtrack2_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | .owner = THIS_MODULE, |
| 267 | .open = video_exclusive_open, |
| 268 | .release = video_exclusive_release, |
| 269 | .ioctl = rt_ioctl, |
Arnd Bergmann | 0d0fbf8 | 2006-01-09 15:24:57 -0200 | [diff] [blame] | 270 | .compat_ioctl = v4l_compat_ioctl32, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | .llseek = no_llseek, |
| 272 | }; |
| 273 | |
| 274 | static struct video_device rtrack2_radio= |
| 275 | { |
| 276 | .owner = THIS_MODULE, |
| 277 | .name = "RadioTrack II radio", |
| 278 | .type = VID_TYPE_TUNER, |
Mauro Carvalho Chehab | f8c559f | 2006-08-08 09:10:02 -0300 | [diff] [blame] | 279 | .hardware = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | .fops = &rtrack2_fops, |
| 281 | }; |
| 282 | |
| 283 | static int __init rtrack2_init(void) |
| 284 | { |
| 285 | if(io==-1) |
| 286 | { |
| 287 | printk(KERN_ERR "You must set an I/O address with io=0x20c or io=0x30c\n"); |
| 288 | return -EINVAL; |
| 289 | } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 290 | if (!request_region(io, 4, "rtrack2")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | { |
| 292 | printk(KERN_ERR "rtrack2: port 0x%x already in use\n", io); |
| 293 | return -EBUSY; |
| 294 | } |
| 295 | |
| 296 | rtrack2_radio.priv=&rtrack2_unit; |
| 297 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 298 | spin_lock_init(&lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | if(video_register_device(&rtrack2_radio, VFL_TYPE_RADIO, radio_nr)==-1) |
| 300 | { |
| 301 | release_region(io, 4); |
| 302 | return -EINVAL; |
| 303 | } |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | printk(KERN_INFO "AIMSlab Radiotrack II card driver.\n"); |
| 306 | |
Mauro Carvalho Chehab | 4286c6f | 2006-04-08 16:06:16 -0300 | [diff] [blame] | 307 | /* mute card - prevents noisy bootups */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | outb(1, io); |
| 309 | rtrack2_unit.muted = 1; |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | MODULE_AUTHOR("Ben Pfaff"); |
| 315 | MODULE_DESCRIPTION("A driver for the RadioTrack II radio card."); |
| 316 | MODULE_LICENSE("GPL"); |
| 317 | |
| 318 | module_param(io, int, 0); |
| 319 | MODULE_PARM_DESC(io, "I/O address of the RadioTrack card (0x20c or 0x30c)"); |
| 320 | module_param(radio_nr, int, 0); |
| 321 | |
| 322 | static void __exit rtrack2_cleanup_module(void) |
| 323 | { |
| 324 | video_unregister_device(&rtrack2_radio); |
| 325 | release_region(io,4); |
| 326 | } |
| 327 | |
| 328 | module_init(rtrack2_init); |
| 329 | module_exit(rtrack2_cleanup_module); |
| 330 | |
| 331 | /* |
| 332 | Local variables: |
| 333 | compile-command: "mmake" |
| 334 | End: |
| 335 | */ |