Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | /* SF16FMI radio driver for Linux radio support * heavily based on rtrack driver... * (c) 1997 M. Kirkwood * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz * * Fitted to new interface by Alan Cox <alan.cox@linux.org> * Made working and cleaned up functions <mikael.hedin@irf.se> * * Notes on the hardware * * Frequency control is done digitally -- ie out(port,encodefreq(95.8)); * No volume control - only mute/unmute - you have to use line volume * control on SB-part of SF16FMI * */ #include <linux/module.h> /* Modules */ #include <linux/init.h> /* Initdata */ #include <linux/ioport.h> /* check_region, request_region */ #include <linux/delay.h> /* udelay */ #include <asm/io.h> /* outb, outb_p */ #include <asm/uaccess.h> /* copy to/from user */ #include <linux/videodev.h> /* kernel radio structs */ #include <linux/config.h> /* CONFIG_RADIO_SF16MI_PORT */ struct fmi_device { int port; int curvol; /* 1 or 0 */ unsigned long curfreq; /* freq in kHz */ __u32 flags; }; #ifndef CONFIG_RADIO_SF16FMI_PORT #define CONFIG_RADIO_SF16FMI_PORT -1 #endif static int io = CONFIG_RADIO_SF16FMI_PORT; static int users = 0; /* freq is in 1/16 kHz to internal number, hw precision is 50 kHz */ /* It is only usefull to give freq in intervall of 800 (=0.05Mhz), * other bits will be truncated, e.g 92.7400016 -> 92.7, but * 92.7400017 -> 92.75 */ #define RSF16_ENCODE(x) ((x)/800+214) #define RSF16_MINFREQ 87*16000 #define RSF16_MAXFREQ 108*16000 static void outbits(int bits, unsigned int data, int port) { while(bits--) { if(data & 1) { outb(5, port); udelay(6); outb(7, port); udelay(6); } else { outb(1, port); udelay(6); outb(3, port); udelay(6); } data>>=1; } } static inline void fmi_mute(int port) { outb(0x00, port); } static inline void fmi_unmute(int port) { outb(0x08, port); } static inline int fmi_setfreq(struct fmi_device *dev) { int myport = dev->port; unsigned long freq = dev->curfreq; int i; outbits(16, RSF16_ENCODE(freq), myport); outbits(8, 0xC0, myport); for(i=0; i< 100; i++) { udelay(1400); if(current->need_resched) schedule(); } /* If this becomes allowed use it ... current->state = TASK_UNINTERRUPTIBLE; schedule_timeout(HZ/7); */ if (dev->curvol) fmi_unmute(myport); return 0; } static inline int fmi_getsigstr(struct fmi_device *dev) { int val; int res; int myport = dev->port; int i; val = dev->curvol ? 0x08 : 0x00; /* unmute/mute */ outb(val, myport); outb(val | 0x10, myport); for(i=0; i< 100; i++) { udelay(1400); if(current->need_resched) schedule(); } /* If this becomes allowed use it ... current->state = TASK_UNINTERRUPTIBLE; schedule_timeout(HZ/7); */ res = (int)inb(myport+1); outb(val, myport); return (res & 2) ? 0 : 0xFFFF; } static int fmi_ioctl(struct video_device *dev, unsigned int cmd, void *arg) { struct fmi_device *fmi=dev->priv; switch(cmd) { case VIDIOCGCAP: { struct video_capability v; strcpy(v.name, "SF16-FMx radio"); v.type=VID_TYPE_TUNER; v.channels=1; v.audios=1; /* No we don't do pictures */ v.maxwidth=0; v.maxheight=0; v.minwidth=0; v.minheight=0; if(copy_to_user(arg,&v,sizeof(v))) return -EFAULT; return 0; } case VIDIOCGTUNER: { struct video_tuner v; int mult; if(copy_from_user(&v, arg,sizeof(v))!=0) return -EFAULT; if(v.tuner) /* Only 1 tuner */ return -EINVAL; strcpy(v.name, "FM"); mult = (fmi->flags & VIDEO_TUNER_LOW) ? 1 : 1000; v.rangelow = RSF16_MINFREQ/mult; v.rangehigh = RSF16_MAXFREQ/mult; v.flags=fmi->flags; v.mode=VIDEO_MODE_AUTO; v.signal = fmi_getsigstr(fmi); if(copy_to_user(arg,&v, sizeof(v))) return -EFAULT; return 0; } case VIDIOCSTUNER: { struct video_tuner v; if(copy_from_user(&v, arg, sizeof(v))) return -EFAULT; if(v.tuner!=0) return -EINVAL; fmi->flags = v.flags & VIDEO_TUNER_LOW; /* Only 1 tuner so no setting needed ! */ return 0; } case VIDIOCGFREQ: { unsigned long tmp = fmi->curfreq; if (!(fmi->flags & VIDEO_TUNER_LOW)) tmp /= 1000; if(copy_to_user(arg, &tmp, sizeof(tmp))) return -EFAULT; return 0; } case VIDIOCSFREQ: { unsigned long tmp; if(copy_from_user(&tmp, arg, sizeof(tmp))) return -EFAULT; if (!(fmi->flags & VIDEO_TUNER_LOW)) tmp *= 1000; if ( tmp<RSF16_MINFREQ || tmp>RSF16_MAXFREQ ) return -EINVAL; /*rounding in steps of 800 to match th freq that will be used */ fmi->curfreq = (tmp/800)*800; fmi_setfreq(fmi); return 0; } case VIDIOCGAUDIO: { struct video_audio v; v.audio=0; v.volume=0; v.bass=0; v.treble=0; v.flags=( (!fmi->curvol)*VIDEO_AUDIO_MUTE | VIDEO_AUDIO_MUTABLE); strcpy(v.name, "Radio"); v.mode=VIDEO_SOUND_STEREO; v.balance=0; v.step=0; /* No volume, just (un)mute */ if(copy_to_user(arg,&v, sizeof(v))) return -EFAULT; return 0; } case VIDIOCSAUDIO: { struct video_audio v; if(copy_from_user(&v, arg, sizeof(v))) return -EFAULT; if(v.audio) return -EINVAL; fmi->curvol= v.flags&VIDEO_AUDIO_MUTE ? 0 : 1; fmi->curvol ? fmi_unmute(fmi->port) : fmi_mute(fmi->port); return 0; } case VIDIOCGUNIT: { struct video_unit v; v.video=VIDEO_NO_UNIT; v.vbi=VIDEO_NO_UNIT; v.radio=dev->minor; v.audio=0; /* How do we find out this??? */ v.teletext=VIDEO_NO_UNIT; if(copy_to_user(arg, &v, sizeof(v))) return -EFAULT; return 0; } default: return -ENOIOCTLCMD; } } static int fmi_open(struct video_device *dev, int flags) { if(users) return -EBUSY; users++; MOD_INC_USE_COUNT; return 0; } static void fmi_close(struct video_device *dev) { users--; MOD_DEC_USE_COUNT; } static struct fmi_device fmi_unit; static struct video_device fmi_radio= { "SF16FMx radio", VID_TYPE_TUNER, VID_HARDWARE_SF16MI, fmi_open, fmi_close, NULL, /* Can't read (no capture ability) */ NULL, /* Can't write */ NULL, /* Can't poll */ fmi_ioctl, NULL, NULL }; __initfunc(int fmi_init(struct video_init *v)) { if (check_region(io, 2)) { printk(KERN_ERR "fmi: port 0x%x already in use\n", io); return -EBUSY; } fmi_unit.port = io; fmi_unit.curvol = 0; fmi_unit.curfreq = 0; fmi_unit.flags = VIDEO_TUNER_LOW; fmi_radio.priv = &fmi_unit; if(video_register_device(&fmi_radio, VFL_TYPE_RADIO)==-1) return -EINVAL; request_region(io, 2, "fmi"); printk(KERN_INFO "SF16FMx radio card driver at 0x%x.\n", io); printk(KERN_INFO "(c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz.\n"); /* mute card - prevents noisy bootups */ fmi_mute(io); return 0; } #ifdef MODULE MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood"); MODULE_DESCRIPTION("A driver for the SF16MI radio."); MODULE_PARM(io, "i"); MODULE_PARM_DESC(io, "I/O address of the SF16MI card (0x284 or 0x384)"); EXPORT_NO_SYMBOLS; int init_module(void) { if(io==-1) { printk(KERN_ERR "You must set an I/O address with io=0x???\n"); return -EINVAL; } return fmi_init(NULL); } void cleanup_module(void) { video_unregister_device(&fmi_radio); release_region(io,2); } #endif |