1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Miro PCM20 radio driver for Linux radio support 4 * (c) 1998 Ruurd Reitsma <R.A.Reitsma@wbmt.tudelft.nl> 5 * Thanks to Norberto Pellici for the ACI device interface specification 6 * The API part is based on the radiotrack driver by M. Kirkwood 7 * This driver relies on the aci mixer provided by the snd-miro 8 * ALSA driver. 9 * Look there for further info... 10 * 11 * From the original miro RDS sources: 12 * 13 * (c) 2001 Robert Siemer <Robert.Siemer@gmx.de> 14 * 15 * Many thanks to Fred Seidel <seidel@metabox.de>, the 16 * designer of the RDS decoder hardware. With his help 17 * I was able to code this driver. 18 * Thanks also to Norberto Pellicci, Dominic Mounteney 19 * <DMounteney@pinnaclesys.com> and www.teleauskunft.de 20 * for good hints on finding Fred. It was somewhat hard 21 * to locate him here in Germany... [: 22 * 23 * This code has been reintroduced and converted to use 24 * the new V4L2 RDS API by: 25 * 26 * Hans Verkuil <hans.verkuil@cisco.com> 27 */ 28 29 #include <linux/module.h> 30 #include <linux/init.h> 31 #include <linux/io.h> 32 #include <linux/delay.h> 33 #include <linux/videodev2.h> 34 #include <linux/kthread.h> 35 #include <media/v4l2-device.h> 36 #include <media/v4l2-ioctl.h> 37 #include <media/v4l2-ctrls.h> 38 #include <media/v4l2-fh.h> 39 #include <media/v4l2-event.h> 40 #include <sound/aci.h> 41 42 #define RDS_DATASHIFT 2 /* Bit 2 */ 43 #define RDS_DATAMASK (1 << RDS_DATASHIFT) 44 #define RDS_BUSYMASK 0x10 /* Bit 4 */ 45 #define RDS_CLOCKMASK 0x08 /* Bit 3 */ 46 #define RDS_DATA(x) (((x) >> RDS_DATASHIFT) & 1) 47 48 #define RDS_STATUS 0x01 49 #define RDS_STATIONNAME 0x02 50 #define RDS_TEXT 0x03 51 #define RDS_ALTFREQ 0x04 52 #define RDS_TIMEDATE 0x05 53 #define RDS_PI_CODE 0x06 54 #define RDS_PTYTATP 0x07 55 #define RDS_RESET 0x08 56 #define RDS_RXVALUE 0x09 57 58 static int radio_nr = -1; 59 module_param(radio_nr, int, 0); 60 MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)"); 61 62 struct pcm20 { 63 struct v4l2_device v4l2_dev; 64 struct video_device vdev; 65 struct v4l2_ctrl_handler ctrl_handler; 66 struct v4l2_ctrl *rds_pty; 67 struct v4l2_ctrl *rds_ps_name; 68 struct v4l2_ctrl *rds_radio_test; 69 struct v4l2_ctrl *rds_ta; 70 struct v4l2_ctrl *rds_tp; 71 struct v4l2_ctrl *rds_ms; 72 /* thread for periodic RDS status checking */ 73 struct task_struct *kthread; 74 unsigned long freq; 75 u32 audmode; 76 struct snd_miro_aci *aci; 77 struct mutex lock; 78 }; 79 80 static struct pcm20 pcm20_card = { 81 .freq = 87 * 16000, 82 .audmode = V4L2_TUNER_MODE_STEREO, 83 }; 84 85 86 static int rds_waitread(struct snd_miro_aci *aci) 87 { 88 u8 byte; 89 int i = 2000; 90 91 do { 92 byte = inb(aci->aci_port + ACI_REG_RDS); 93 i--; 94 } while ((byte & RDS_BUSYMASK) && i); 95 96 /* 97 * It's magic, but without this the data that you read later on 98 * is unreliable and full of bit errors. With this 1 usec delay 99 * everything is fine. 100 */ 101 udelay(1); 102 return i ? byte : -1; 103 } 104 105 static int rds_rawwrite(struct snd_miro_aci *aci, u8 byte) 106 { 107 if (rds_waitread(aci) >= 0) { 108 outb(byte, aci->aci_port + ACI_REG_RDS); 109 return 0; 110 } 111 return -1; 112 } 113 114 static int rds_write(struct snd_miro_aci *aci, u8 byte) 115 { 116 u8 sendbuffer[8]; 117 int i; 118 119 for (i = 7; i >= 0; i--) 120 sendbuffer[7 - i] = (byte & (1 << i)) ? RDS_DATAMASK : 0; 121 sendbuffer[0] |= RDS_CLOCKMASK; 122 123 for (i = 0; i < 8; i++) 124 rds_rawwrite(aci, sendbuffer[i]); 125 return 0; 126 } 127 128 static int rds_readcycle_nowait(struct snd_miro_aci *aci) 129 { 130 outb(0, aci->aci_port + ACI_REG_RDS); 131 return rds_waitread(aci); 132 } 133 134 static int rds_readcycle(struct snd_miro_aci *aci) 135 { 136 if (rds_rawwrite(aci, 0) < 0) 137 return -1; 138 return rds_waitread(aci); 139 } 140 141 static int rds_ack(struct snd_miro_aci *aci) 142 { 143 int i = rds_readcycle(aci); 144 145 if (i < 0) 146 return -1; 147 if (i & RDS_DATAMASK) 148 return 0; /* ACK */ 149 return 1; /* NACK */ 150 } 151 152 static int rds_cmd(struct snd_miro_aci *aci, u8 cmd, u8 databuffer[], u8 datasize) 153 { 154 int i, j; 155 156 rds_write(aci, cmd); 157 158 /* RDS_RESET doesn't need further processing */ 159 if (cmd == RDS_RESET) 160 return 0; 161 if (rds_ack(aci)) 162 return -EIO; 163 if (datasize == 0) 164 return 0; 165 166 /* to be able to use rds_readcycle_nowait() 167 I have to waitread() here */ 168 if (rds_waitread(aci) < 0) 169 return -1; 170 171 memset(databuffer, 0, datasize); 172 173 for (i = 0; i < 8 * datasize; i++) { 174 j = rds_readcycle_nowait(aci); 175 if (j < 0) 176 return -EIO; 177 databuffer[i / 8] |= RDS_DATA(j) << (7 - (i % 8)); 178 } 179 return 0; 180 } 181 182 static int pcm20_setfreq(struct pcm20 *dev, unsigned long freq) 183 { 184 unsigned char freql; 185 unsigned char freqh; 186 struct snd_miro_aci *aci = dev->aci; 187 188 freq /= 160; 189 if (!(aci->aci_version == 0x07 || aci->aci_version >= 0xb0)) 190 freq /= 10; /* I don't know exactly which version 191 * needs this hack */ 192 freql = freq & 0xff; 193 freqh = freq >> 8; 194 195 rds_cmd(aci, RDS_RESET, NULL, 0); 196 return snd_aci_cmd(aci, ACI_WRITE_TUNE, freql, freqh); 197 } 198 199 static int vidioc_querycap(struct file *file, void *priv, 200 struct v4l2_capability *v) 201 { 202 strscpy(v->driver, "Miro PCM20", sizeof(v->driver)); 203 strscpy(v->card, "Miro PCM20", sizeof(v->card)); 204 strscpy(v->bus_info, "ISA:radio-miropcm20", sizeof(v->bus_info)); 205 return 0; 206 } 207 208 static bool sanitize(char *p, int size) 209 { 210 int i; 211 bool ret = true; 212 213 for (i = 0; i < size; i++) { 214 if (p[i] < 32) { 215 p[i] = ' '; 216 ret = false; 217 } 218 } 219 return ret; 220 } 221 222 static int vidioc_g_tuner(struct file *file, void *priv, 223 struct v4l2_tuner *v) 224 { 225 struct pcm20 *dev = video_drvdata(file); 226 int res; 227 u8 buf; 228 229 if (v->index) 230 return -EINVAL; 231 strscpy(v->name, "FM", sizeof(v->name)); 232 v->type = V4L2_TUNER_RADIO; 233 v->rangelow = 87*16000; 234 v->rangehigh = 108*16000; 235 res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTATION, -1, -1); 236 v->signal = (res & 0x80) ? 0 : 0xffff; 237 /* Note: stereo detection does not work if the audio is muted, 238 it will default to mono in that case. */ 239 res = snd_aci_cmd(dev->aci, ACI_READ_TUNERSTEREO, -1, -1); 240 v->rxsubchans = (res & 0x40) ? V4L2_TUNER_SUB_MONO : 241 V4L2_TUNER_SUB_STEREO; 242 v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | 243 V4L2_TUNER_CAP_RDS | V4L2_TUNER_CAP_RDS_CONTROLS; 244 v->audmode = dev->audmode; 245 res = rds_cmd(dev->aci, RDS_RXVALUE, &buf, 1); 246 if (res >= 0 && buf) 247 v->rxsubchans |= V4L2_TUNER_SUB_RDS; 248 return 0; 249 } 250 251 static int vidioc_s_tuner(struct file *file, void *priv, 252 const struct v4l2_tuner *v) 253 { 254 struct pcm20 *dev = video_drvdata(file); 255 256 if (v->index) 257 return -EINVAL; 258 if (v->audmode > V4L2_TUNER_MODE_STEREO) 259 dev->audmode = V4L2_TUNER_MODE_STEREO; 260 else 261 dev->audmode = v->audmode; 262 snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO, 263 dev->audmode == V4L2_TUNER_MODE_MONO, -1); 264 return 0; 265 } 266 267 static int vidioc_g_frequency(struct file *file, void *priv, 268 struct v4l2_frequency *f) 269 { 270 struct pcm20 *dev = video_drvdata(file); 271 272 if (f->tuner != 0) 273 return -EINVAL; 274 275 f->type = V4L2_TUNER_RADIO; 276 f->frequency = dev->freq; 277 return 0; 278 } 279 280 281 static int vidioc_s_frequency(struct file *file, void *priv, 282 const struct v4l2_frequency *f) 283 { 284 struct pcm20 *dev = video_drvdata(file); 285 286 if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO) 287 return -EINVAL; 288 289 dev->freq = clamp_t(u32, f->frequency, 87 * 16000U, 108 * 16000U); 290 pcm20_setfreq(dev, dev->freq); 291 return 0; 292 } 293 294 static int pcm20_s_ctrl(struct v4l2_ctrl *ctrl) 295 { 296 struct pcm20 *dev = container_of(ctrl->handler, struct pcm20, ctrl_handler); 297 298 switch (ctrl->id) { 299 case V4L2_CID_AUDIO_MUTE: 300 snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, ctrl->val, -1); 301 return 0; 302 } 303 return -EINVAL; 304 } 305 306 static int pcm20_thread(void *data) 307 { 308 struct pcm20 *dev = data; 309 const unsigned no_rds_start_counter = 5; 310 const unsigned sleep_msecs = 2000; 311 unsigned no_rds_counter = no_rds_start_counter; 312 313 for (;;) { 314 char text_buffer[66]; 315 u8 buf; 316 int res; 317 318 msleep_interruptible(sleep_msecs); 319 320 if (kthread_should_stop()) 321 break; 322 323 res = rds_cmd(dev->aci, RDS_RXVALUE, &buf, 1); 324 if (res) 325 continue; 326 if (buf == 0) { 327 if (no_rds_counter == 0) 328 continue; 329 no_rds_counter--; 330 if (no_rds_counter) 331 continue; 332 333 /* 334 * No RDS seen for no_rds_start_counter * sleep_msecs 335 * milliseconds, clear all RDS controls to their 336 * default values. 337 */ 338 v4l2_ctrl_s_ctrl_string(dev->rds_ps_name, ""); 339 v4l2_ctrl_s_ctrl(dev->rds_ms, 1); 340 v4l2_ctrl_s_ctrl(dev->rds_ta, 0); 341 v4l2_ctrl_s_ctrl(dev->rds_tp, 0); 342 v4l2_ctrl_s_ctrl(dev->rds_pty, 0); 343 v4l2_ctrl_s_ctrl_string(dev->rds_radio_test, ""); 344 continue; 345 } 346 no_rds_counter = no_rds_start_counter; 347 348 res = rds_cmd(dev->aci, RDS_STATUS, &buf, 1); 349 if (res) 350 continue; 351 if ((buf >> 3) & 1) { 352 res = rds_cmd(dev->aci, RDS_STATIONNAME, text_buffer, 8); 353 text_buffer[8] = 0; 354 if (!res && sanitize(text_buffer, 8)) 355 v4l2_ctrl_s_ctrl_string(dev->rds_ps_name, text_buffer); 356 } 357 if ((buf >> 6) & 1) { 358 u8 pty; 359 360 res = rds_cmd(dev->aci, RDS_PTYTATP, &pty, 1); 361 if (!res) { 362 v4l2_ctrl_s_ctrl(dev->rds_ms, !!(pty & 0x01)); 363 v4l2_ctrl_s_ctrl(dev->rds_ta, !!(pty & 0x02)); 364 v4l2_ctrl_s_ctrl(dev->rds_tp, !!(pty & 0x80)); 365 v4l2_ctrl_s_ctrl(dev->rds_pty, (pty >> 2) & 0x1f); 366 } 367 } 368 if ((buf >> 4) & 1) { 369 res = rds_cmd(dev->aci, RDS_TEXT, text_buffer, 65); 370 text_buffer[65] = 0; 371 if (!res && sanitize(text_buffer + 1, 64)) 372 v4l2_ctrl_s_ctrl_string(dev->rds_radio_test, text_buffer + 1); 373 } 374 } 375 return 0; 376 } 377 378 static int pcm20_open(struct file *file) 379 { 380 struct pcm20 *dev = video_drvdata(file); 381 int res = v4l2_fh_open(file); 382 383 if (!res && v4l2_fh_is_singular_file(file) && 384 IS_ERR_OR_NULL(dev->kthread)) { 385 dev->kthread = kthread_run(pcm20_thread, dev, "%s", 386 dev->v4l2_dev.name); 387 if (IS_ERR(dev->kthread)) { 388 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n"); 389 v4l2_fh_release(file); 390 return PTR_ERR(dev->kthread); 391 } 392 } 393 return res; 394 } 395 396 static int pcm20_release(struct file *file) 397 { 398 struct pcm20 *dev = video_drvdata(file); 399 400 if (v4l2_fh_is_singular_file(file) && !IS_ERR_OR_NULL(dev->kthread)) { 401 kthread_stop(dev->kthread); 402 dev->kthread = NULL; 403 } 404 return v4l2_fh_release(file); 405 } 406 407 static const struct v4l2_file_operations pcm20_fops = { 408 .owner = THIS_MODULE, 409 .open = pcm20_open, 410 .poll = v4l2_ctrl_poll, 411 .release = pcm20_release, 412 .unlocked_ioctl = video_ioctl2, 413 }; 414 415 static const struct v4l2_ioctl_ops pcm20_ioctl_ops = { 416 .vidioc_querycap = vidioc_querycap, 417 .vidioc_g_tuner = vidioc_g_tuner, 418 .vidioc_s_tuner = vidioc_s_tuner, 419 .vidioc_g_frequency = vidioc_g_frequency, 420 .vidioc_s_frequency = vidioc_s_frequency, 421 .vidioc_log_status = v4l2_ctrl_log_status, 422 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 423 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 424 }; 425 426 static const struct v4l2_ctrl_ops pcm20_ctrl_ops = { 427 .s_ctrl = pcm20_s_ctrl, 428 }; 429 430 static int __init pcm20_init(void) 431 { 432 struct pcm20 *dev = &pcm20_card; 433 struct v4l2_device *v4l2_dev = &dev->v4l2_dev; 434 struct v4l2_ctrl_handler *hdl; 435 int res; 436 437 dev->aci = snd_aci_get_aci(); 438 if (dev->aci == NULL) { 439 v4l2_err(v4l2_dev, 440 "you must load the snd-miro driver first!\n"); 441 return -ENODEV; 442 } 443 strscpy(v4l2_dev->name, "radio-miropcm20", sizeof(v4l2_dev->name)); 444 mutex_init(&dev->lock); 445 446 res = v4l2_device_register(NULL, v4l2_dev); 447 if (res < 0) { 448 v4l2_err(v4l2_dev, "could not register v4l2_device\n"); 449 return -EINVAL; 450 } 451 452 hdl = &dev->ctrl_handler; 453 v4l2_ctrl_handler_init(hdl, 7); 454 v4l2_ctrl_new_std(hdl, &pcm20_ctrl_ops, 455 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1); 456 dev->rds_pty = v4l2_ctrl_new_std(hdl, NULL, 457 V4L2_CID_RDS_RX_PTY, 0, 0x1f, 1, 0); 458 dev->rds_ps_name = v4l2_ctrl_new_std(hdl, NULL, 459 V4L2_CID_RDS_RX_PS_NAME, 0, 8, 8, 0); 460 dev->rds_radio_test = v4l2_ctrl_new_std(hdl, NULL, 461 V4L2_CID_RDS_RX_RADIO_TEXT, 0, 64, 64, 0); 462 dev->rds_ta = v4l2_ctrl_new_std(hdl, NULL, 463 V4L2_CID_RDS_RX_TRAFFIC_ANNOUNCEMENT, 0, 1, 1, 0); 464 dev->rds_tp = v4l2_ctrl_new_std(hdl, NULL, 465 V4L2_CID_RDS_RX_TRAFFIC_PROGRAM, 0, 1, 1, 0); 466 dev->rds_ms = v4l2_ctrl_new_std(hdl, NULL, 467 V4L2_CID_RDS_RX_MUSIC_SPEECH, 0, 1, 1, 1); 468 v4l2_dev->ctrl_handler = hdl; 469 if (hdl->error) { 470 res = hdl->error; 471 v4l2_err(v4l2_dev, "Could not register control\n"); 472 goto err_hdl; 473 } 474 strscpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name)); 475 dev->vdev.v4l2_dev = v4l2_dev; 476 dev->vdev.fops = &pcm20_fops; 477 dev->vdev.ioctl_ops = &pcm20_ioctl_ops; 478 dev->vdev.release = video_device_release_empty; 479 dev->vdev.lock = &dev->lock; 480 dev->vdev.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO | 481 V4L2_CAP_RDS_CAPTURE; 482 video_set_drvdata(&dev->vdev, dev); 483 snd_aci_cmd(dev->aci, ACI_SET_TUNERMONO, 484 dev->audmode == V4L2_TUNER_MODE_MONO, -1); 485 pcm20_setfreq(dev, dev->freq); 486 487 if (video_register_device(&dev->vdev, VFL_TYPE_RADIO, radio_nr) < 0) 488 goto err_hdl; 489 490 v4l2_info(v4l2_dev, "Mirosound PCM20 Radio tuner\n"); 491 return 0; 492 err_hdl: 493 v4l2_ctrl_handler_free(hdl); 494 v4l2_device_unregister(v4l2_dev); 495 return -EINVAL; 496 } 497 498 MODULE_AUTHOR("Ruurd Reitsma, Krzysztof Helt"); 499 MODULE_DESCRIPTION("A driver for the Miro PCM20 radio card."); 500 MODULE_LICENSE("GPL"); 501 502 static void __exit pcm20_cleanup(void) 503 { 504 struct pcm20 *dev = &pcm20_card; 505 506 video_unregister_device(&dev->vdev); 507 snd_aci_cmd(dev->aci, ACI_SET_TUNERMUTE, 1, -1); 508 v4l2_ctrl_handler_free(&dev->ctrl_handler); 509 v4l2_device_unregister(&dev->v4l2_dev); 510 } 511 512 module_init(pcm20_init); 513 module_exit(pcm20_cleanup); 514