1 // SPDX-License-Identifier: GPL-2.0-only 2 /* SF16-FMI, SF16-FMP and SF16-FMD radio driver for Linux radio support 3 * heavily based on rtrack driver... 4 * (c) 1997 M. Kirkwood 5 * (c) 1998 Petr Vandrovec, vandrove@vc.cvut.cz 6 * 7 * Fitted to new interface by Alan Cox <alan@lxorguk.ukuu.org.uk> 8 * Made working and cleaned up functions <mikael.hedin@irf.se> 9 * Support for ISAPnP by Ladislav Michl <ladis@psi.cz> 10 * 11 * Notes on the hardware 12 * 13 * Frequency control is done digitally -- ie out(port,encodefreq(95.8)); 14 * No volume control - only mute/unmute - you have to use line volume 15 * control on SB-part of SF16-FMI/SF16-FMP/SF16-FMD 16 * 17 * Converted to V4L2 API by Mauro Carvalho Chehab <mchehab@kernel.org> 18 */ 19 20 #include <linux/kernel.h> /* __setup */ 21 #include <linux/module.h> /* Modules */ 22 #include <linux/init.h> /* Initdata */ 23 #include <linux/ioport.h> /* request_region */ 24 #include <linux/delay.h> /* udelay */ 25 #include <linux/isapnp.h> 26 #include <linux/mutex.h> 27 #include <linux/videodev2.h> /* kernel radio structs */ 28 #include <linux/io.h> /* outb, outb_p */ 29 #include <media/v4l2-device.h> 30 #include <media/v4l2-ioctl.h> 31 #include <media/v4l2-ctrls.h> 32 #include <media/v4l2-event.h> 33 #include "lm7000.h" 34 35 MODULE_AUTHOR("Petr Vandrovec, vandrove@vc.cvut.cz and M. Kirkwood"); 36 MODULE_DESCRIPTION("A driver for the SF16-FMI, SF16-FMP and SF16-FMD radio."); 37 MODULE_LICENSE("GPL"); 38 MODULE_VERSION("0.0.3"); 39 40 static int io = -1; 41 static int radio_nr = -1; 42 43 module_param(io, int, 0); 44 MODULE_PARM_DESC(io, "I/O address of the SF16-FMI/SF16-FMP/SF16-FMD card (0x284 or 0x384)"); 45 module_param(radio_nr, int, 0); 46 47 struct fmi 48 { 49 struct v4l2_device v4l2_dev; 50 struct v4l2_ctrl_handler hdl; 51 struct video_device vdev; 52 int io; 53 bool mute; 54 u32 curfreq; /* freq in kHz */ 55 struct mutex lock; 56 }; 57 58 static struct fmi fmi_card; 59 static struct pnp_dev *dev; 60 static bool pnp_attached; 61 62 #define RSF16_MINFREQ (87U * 16000) 63 #define RSF16_MAXFREQ (108U * 16000) 64 65 #define FMI_BIT_TUN_CE (1 << 0) 66 #define FMI_BIT_TUN_CLK (1 << 1) 67 #define FMI_BIT_TUN_DATA (1 << 2) 68 #define FMI_BIT_VOL_SW (1 << 3) 69 #define FMI_BIT_TUN_STRQ (1 << 4) 70 71 static void fmi_set_pins(void *handle, u8 pins) 72 { 73 struct fmi *fmi = handle; 74 u8 bits = FMI_BIT_TUN_STRQ; 75 76 if (!fmi->mute) 77 bits |= FMI_BIT_VOL_SW; 78 79 if (pins & LM7000_DATA) 80 bits |= FMI_BIT_TUN_DATA; 81 if (pins & LM7000_CLK) 82 bits |= FMI_BIT_TUN_CLK; 83 if (pins & LM7000_CE) 84 bits |= FMI_BIT_TUN_CE; 85 86 mutex_lock(&fmi->lock); 87 outb_p(bits, fmi->io); 88 mutex_unlock(&fmi->lock); 89 } 90 91 static inline void fmi_mute(struct fmi *fmi) 92 { 93 mutex_lock(&fmi->lock); 94 outb(0x00, fmi->io); 95 mutex_unlock(&fmi->lock); 96 } 97 98 static inline void fmi_unmute(struct fmi *fmi) 99 { 100 mutex_lock(&fmi->lock); 101 outb(0x08, fmi->io); 102 mutex_unlock(&fmi->lock); 103 } 104 105 static inline int fmi_getsigstr(struct fmi *fmi) 106 { 107 int val; 108 int res; 109 110 mutex_lock(&fmi->lock); 111 val = fmi->mute ? 0x00 : 0x08; /* mute/unmute */ 112 outb(val, fmi->io); 113 outb(val | 0x10, fmi->io); 114 msleep(143); /* was schedule_timeout(HZ/7) */ 115 res = (int)inb(fmi->io + 1); 116 outb(val, fmi->io); 117 118 mutex_unlock(&fmi->lock); 119 return (res & 2) ? 0 : 0xFFFF; 120 } 121 122 static void fmi_set_freq(struct fmi *fmi) 123 { 124 fmi->curfreq = clamp(fmi->curfreq, RSF16_MINFREQ, RSF16_MAXFREQ); 125 /* rounding in steps of 800 to match the freq 126 that will be used */ 127 lm7000_set_freq((fmi->curfreq / 800) * 800, fmi, fmi_set_pins); 128 } 129 130 static int vidioc_querycap(struct file *file, void *priv, 131 struct v4l2_capability *v) 132 { 133 strscpy(v->driver, "radio-sf16fmi", sizeof(v->driver)); 134 strscpy(v->card, "SF16-FMI/FMP/FMD radio", sizeof(v->card)); 135 strscpy(v->bus_info, "ISA:radio-sf16fmi", sizeof(v->bus_info)); 136 v->device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO; 137 v->capabilities = v->device_caps | V4L2_CAP_DEVICE_CAPS; 138 return 0; 139 } 140 141 static int vidioc_g_tuner(struct file *file, void *priv, 142 struct v4l2_tuner *v) 143 { 144 struct fmi *fmi = video_drvdata(file); 145 146 if (v->index > 0) 147 return -EINVAL; 148 149 strscpy(v->name, "FM", sizeof(v->name)); 150 v->type = V4L2_TUNER_RADIO; 151 v->rangelow = RSF16_MINFREQ; 152 v->rangehigh = RSF16_MAXFREQ; 153 v->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO; 154 v->capability = V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LOW; 155 v->audmode = V4L2_TUNER_MODE_STEREO; 156 v->signal = fmi_getsigstr(fmi); 157 return 0; 158 } 159 160 static int vidioc_s_tuner(struct file *file, void *priv, 161 const struct v4l2_tuner *v) 162 { 163 return v->index ? -EINVAL : 0; 164 } 165 166 static int vidioc_s_frequency(struct file *file, void *priv, 167 const struct v4l2_frequency *f) 168 { 169 struct fmi *fmi = video_drvdata(file); 170 171 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) 172 return -EINVAL; 173 174 fmi->curfreq = f->frequency; 175 fmi_set_freq(fmi); 176 177 return 0; 178 } 179 180 static int vidioc_g_frequency(struct file *file, void *priv, 181 struct v4l2_frequency *f) 182 { 183 struct fmi *fmi = video_drvdata(file); 184 185 if (f->tuner != 0) 186 return -EINVAL; 187 f->type = V4L2_TUNER_RADIO; 188 f->frequency = fmi->curfreq; 189 return 0; 190 } 191 192 static int fmi_s_ctrl(struct v4l2_ctrl *ctrl) 193 { 194 struct fmi *fmi = container_of(ctrl->handler, struct fmi, hdl); 195 196 switch (ctrl->id) { 197 case V4L2_CID_AUDIO_MUTE: 198 if (ctrl->val) 199 fmi_mute(fmi); 200 else 201 fmi_unmute(fmi); 202 fmi->mute = ctrl->val; 203 return 0; 204 } 205 return -EINVAL; 206 } 207 208 static const struct v4l2_ctrl_ops fmi_ctrl_ops = { 209 .s_ctrl = fmi_s_ctrl, 210 }; 211 212 static const struct v4l2_file_operations fmi_fops = { 213 .owner = THIS_MODULE, 214 .open = v4l2_fh_open, 215 .release = v4l2_fh_release, 216 .poll = v4l2_ctrl_poll, 217 .unlocked_ioctl = video_ioctl2, 218 }; 219 220 static const struct v4l2_ioctl_ops fmi_ioctl_ops = { 221 .vidioc_querycap = vidioc_querycap, 222 .vidioc_g_tuner = vidioc_g_tuner, 223 .vidioc_s_tuner = vidioc_s_tuner, 224 .vidioc_g_frequency = vidioc_g_frequency, 225 .vidioc_s_frequency = vidioc_s_frequency, 226 .vidioc_log_status = v4l2_ctrl_log_status, 227 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 228 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 229 }; 230 231 /* ladis: this is my card. does any other types exist? */ 232 static struct isapnp_device_id id_table[] = { 233 /* SF16-FMI */ 234 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, 235 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad10), 0}, 236 /* SF16-FMD */ 237 { ISAPNP_ANY_ID, ISAPNP_ANY_ID, 238 ISAPNP_VENDOR('M','F','R'), ISAPNP_FUNCTION(0xad12), 0}, 239 { ISAPNP_CARD_END, }, 240 }; 241 242 MODULE_DEVICE_TABLE(isapnp, id_table); 243 244 static int __init isapnp_fmi_probe(void) 245 { 246 int i = 0; 247 248 while (id_table[i].card_vendor != 0 && dev == NULL) { 249 dev = pnp_find_dev(NULL, id_table[i].vendor, 250 id_table[i].function, NULL); 251 i++; 252 } 253 254 if (!dev) 255 return -ENODEV; 256 if (pnp_device_attach(dev) < 0) 257 return -EAGAIN; 258 if (pnp_activate_dev(dev) < 0) { 259 printk(KERN_ERR "radio-sf16fmi: PnP configure failed (out of resources?)\n"); 260 pnp_device_detach(dev); 261 return -ENOMEM; 262 } 263 if (!pnp_port_valid(dev, 0)) { 264 pnp_device_detach(dev); 265 return -ENODEV; 266 } 267 268 i = pnp_port_start(dev, 0); 269 printk(KERN_INFO "radio-sf16fmi: PnP reports card at %#x\n", i); 270 271 return i; 272 } 273 274 static int __init fmi_init(void) 275 { 276 struct fmi *fmi = &fmi_card; 277 struct v4l2_device *v4l2_dev = &fmi->v4l2_dev; 278 struct v4l2_ctrl_handler *hdl = &fmi->hdl; 279 int res, i; 280 int probe_ports[] = { 0, 0x284, 0x384 }; 281 282 if (io < 0) { 283 for (i = 0; i < ARRAY_SIZE(probe_ports); i++) { 284 io = probe_ports[i]; 285 if (io == 0) { 286 io = isapnp_fmi_probe(); 287 if (io < 0) 288 continue; 289 pnp_attached = true; 290 } 291 if (!request_region(io, 2, "radio-sf16fmi")) { 292 if (pnp_attached) 293 pnp_device_detach(dev); 294 io = -1; 295 continue; 296 } 297 if (pnp_attached || 298 ((inb(io) & 0xf9) == 0xf9 && (inb(io) & 0x4) == 0)) 299 break; 300 release_region(io, 2); 301 io = -1; 302 } 303 } else { 304 if (!request_region(io, 2, "radio-sf16fmi")) { 305 printk(KERN_ERR "radio-sf16fmi: port %#x already in use\n", io); 306 return -EBUSY; 307 } 308 if (inb(io) == 0xff) { 309 printk(KERN_ERR "radio-sf16fmi: card not present at %#x\n", io); 310 release_region(io, 2); 311 return -ENODEV; 312 } 313 } 314 if (io < 0) { 315 printk(KERN_ERR "radio-sf16fmi: no cards found\n"); 316 return -ENODEV; 317 } 318 319 strscpy(v4l2_dev->name, "sf16fmi", sizeof(v4l2_dev->name)); 320 fmi->io = io; 321 322 res = v4l2_device_register(NULL, v4l2_dev); 323 if (res < 0) { 324 release_region(fmi->io, 2); 325 if (pnp_attached) 326 pnp_device_detach(dev); 327 v4l2_err(v4l2_dev, "Could not register v4l2_device\n"); 328 return res; 329 } 330 331 v4l2_ctrl_handler_init(hdl, 1); 332 v4l2_ctrl_new_std(hdl, &fmi_ctrl_ops, 333 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); 334 v4l2_dev->ctrl_handler = hdl; 335 if (hdl->error) { 336 res = hdl->error; 337 v4l2_err(v4l2_dev, "Could not register controls\n"); 338 v4l2_ctrl_handler_free(hdl); 339 v4l2_device_unregister(v4l2_dev); 340 return res; 341 } 342 343 strscpy(fmi->vdev.name, v4l2_dev->name, sizeof(fmi->vdev.name)); 344 fmi->vdev.v4l2_dev = v4l2_dev; 345 fmi->vdev.fops = &fmi_fops; 346 fmi->vdev.ioctl_ops = &fmi_ioctl_ops; 347 fmi->vdev.release = video_device_release_empty; 348 video_set_drvdata(&fmi->vdev, fmi); 349 350 mutex_init(&fmi->lock); 351 352 /* mute card and set default frequency */ 353 fmi->mute = true; 354 fmi->curfreq = RSF16_MINFREQ; 355 fmi_set_freq(fmi); 356 357 if (video_register_device(&fmi->vdev, VFL_TYPE_RADIO, radio_nr) < 0) { 358 v4l2_ctrl_handler_free(hdl); 359 v4l2_device_unregister(v4l2_dev); 360 release_region(fmi->io, 2); 361 if (pnp_attached) 362 pnp_device_detach(dev); 363 return -EINVAL; 364 } 365 366 v4l2_info(v4l2_dev, "card driver at 0x%x\n", fmi->io); 367 return 0; 368 } 369 370 static void __exit fmi_exit(void) 371 { 372 struct fmi *fmi = &fmi_card; 373 374 v4l2_ctrl_handler_free(&fmi->hdl); 375 video_unregister_device(&fmi->vdev); 376 v4l2_device_unregister(&fmi->v4l2_dev); 377 release_region(fmi->io, 2); 378 if (dev && pnp_attached) 379 pnp_device_detach(dev); 380 } 381 382 module_init(fmi_init); 383 module_exit(fmi_exit); 384