vidtv_bridge.c (15653B)
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * The Virtual DTV test driver serves as a reference DVB driver and helps 4 * validate the existing APIs in the media subsystem. It can also aid 5 * developers working on userspace applications. 6 * 7 * When this module is loaded, it will attempt to modprobe 'dvb_vidtv_tuner' 8 * and 'dvb_vidtv_demod'. 9 * 10 * Copyright (C) 2020 Daniel W. S. Almeida 11 */ 12 13#include <linux/dev_printk.h> 14#include <linux/moduleparam.h> 15#include <linux/mutex.h> 16#include <linux/platform_device.h> 17#include <linux/time.h> 18#include <linux/types.h> 19#include <linux/workqueue.h> 20#include <media/dvbdev.h> 21#include <media/media-device.h> 22 23#include "vidtv_bridge.h" 24#include "vidtv_common.h" 25#include "vidtv_demod.h" 26#include "vidtv_mux.h" 27#include "vidtv_ts.h" 28#include "vidtv_tuner.h" 29 30#define MUX_BUF_MIN_SZ 90164 31#define MUX_BUF_MAX_SZ (MUX_BUF_MIN_SZ * 10) 32#define TUNER_DEFAULT_ADDR 0x68 33#define DEMOD_DEFAULT_ADDR 0x60 34#define VIDTV_DEFAULT_NETWORK_ID 0xff44 35#define VIDTV_DEFAULT_NETWORK_NAME "LinuxTV.org" 36#define VIDTV_DEFAULT_TS_ID 0x4081 37 38/* 39 * The LNBf fake parameters here are the ranges used by an 40 * Universal (extended) European LNBf, which is likely the most common LNBf 41 * found on Satellite digital TV system nowadays. 42 */ 43#define LNB_CUT_FREQUENCY 11700000 /* high IF frequency */ 44#define LNB_LOW_FREQ 9750000 /* low IF frequency */ 45#define LNB_HIGH_FREQ 10600000 /* transition frequency */ 46 47static unsigned int drop_tslock_prob_on_low_snr; 48module_param(drop_tslock_prob_on_low_snr, uint, 0); 49MODULE_PARM_DESC(drop_tslock_prob_on_low_snr, 50 "Probability of losing the TS lock if the signal quality is bad"); 51 52static unsigned int recover_tslock_prob_on_good_snr; 53module_param(recover_tslock_prob_on_good_snr, uint, 0); 54MODULE_PARM_DESC(recover_tslock_prob_on_good_snr, 55 "Probability recovering the TS lock when the signal improves"); 56 57static unsigned int mock_power_up_delay_msec; 58module_param(mock_power_up_delay_msec, uint, 0); 59MODULE_PARM_DESC(mock_power_up_delay_msec, "Simulate a power up delay"); 60 61static unsigned int mock_tune_delay_msec; 62module_param(mock_tune_delay_msec, uint, 0); 63MODULE_PARM_DESC(mock_tune_delay_msec, "Simulate a tune delay"); 64 65static unsigned int vidtv_valid_dvb_t_freqs[NUM_VALID_TUNER_FREQS] = { 66 474000000 67}; 68 69module_param_array(vidtv_valid_dvb_t_freqs, uint, NULL, 0); 70MODULE_PARM_DESC(vidtv_valid_dvb_t_freqs, 71 "Valid DVB-T frequencies to simulate, in Hz"); 72 73static unsigned int vidtv_valid_dvb_c_freqs[NUM_VALID_TUNER_FREQS] = { 74 474000000 75}; 76 77module_param_array(vidtv_valid_dvb_c_freqs, uint, NULL, 0); 78MODULE_PARM_DESC(vidtv_valid_dvb_c_freqs, 79 "Valid DVB-C frequencies to simulate, in Hz"); 80 81static unsigned int vidtv_valid_dvb_s_freqs[NUM_VALID_TUNER_FREQS] = { 82 11362000 83}; 84module_param_array(vidtv_valid_dvb_s_freqs, uint, NULL, 0); 85MODULE_PARM_DESC(vidtv_valid_dvb_s_freqs, 86 "Valid DVB-S/S2 frequencies to simulate at Ku-Band, in kHz"); 87 88static unsigned int max_frequency_shift_hz; 89module_param(max_frequency_shift_hz, uint, 0); 90MODULE_PARM_DESC(max_frequency_shift_hz, 91 "Maximum shift in HZ allowed when tuning in a channel"); 92 93DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nums); 94 95/* 96 * Influences the signal acquisition time. See ISO/IEC 13818-1 : 2000. p. 113. 97 */ 98static unsigned int si_period_msec = 40; 99module_param(si_period_msec, uint, 0); 100MODULE_PARM_DESC(si_period_msec, "How often to send SI packets. Default: 40ms"); 101 102static unsigned int pcr_period_msec = 40; 103module_param(pcr_period_msec, uint, 0); 104MODULE_PARM_DESC(pcr_period_msec, 105 "How often to send PCR packets. Default: 40ms"); 106 107static unsigned int mux_rate_kbytes_sec = 4096; 108module_param(mux_rate_kbytes_sec, uint, 0); 109MODULE_PARM_DESC(mux_rate_kbytes_sec, "Mux rate: will pad stream if below"); 110 111static unsigned int pcr_pid = 0x200; 112module_param(pcr_pid, uint, 0); 113MODULE_PARM_DESC(pcr_pid, "PCR PID for all channels: defaults to 0x200"); 114 115static unsigned int mux_buf_sz_pkts; 116module_param(mux_buf_sz_pkts, uint, 0); 117MODULE_PARM_DESC(mux_buf_sz_pkts, 118 "Size for the internal mux buffer in multiples of 188 bytes"); 119 120static u32 vidtv_bridge_mux_buf_sz_for_mux_rate(void) 121{ 122 u32 max_elapsed_time_msecs = VIDTV_MAX_SLEEP_USECS / USEC_PER_MSEC; 123 u32 mux_buf_sz = mux_buf_sz_pkts * TS_PACKET_LEN; 124 u32 nbytes_expected; 125 126 nbytes_expected = mux_rate_kbytes_sec; 127 nbytes_expected *= max_elapsed_time_msecs; 128 129 mux_buf_sz = roundup(nbytes_expected, TS_PACKET_LEN); 130 mux_buf_sz += mux_buf_sz / 10; 131 132 if (mux_buf_sz < MUX_BUF_MIN_SZ) 133 mux_buf_sz = MUX_BUF_MIN_SZ; 134 135 if (mux_buf_sz > MUX_BUF_MAX_SZ) 136 mux_buf_sz = MUX_BUF_MAX_SZ; 137 138 return mux_buf_sz; 139} 140 141static bool vidtv_bridge_check_demod_lock(struct vidtv_dvb *dvb, u32 n) 142{ 143 enum fe_status status; 144 145 dvb->fe[n]->ops.read_status(dvb->fe[n], &status); 146 147 return status == (FE_HAS_SIGNAL | 148 FE_HAS_CARRIER | 149 FE_HAS_VITERBI | 150 FE_HAS_SYNC | 151 FE_HAS_LOCK); 152} 153 154/* 155 * called on a separate thread by the mux when new packets become available 156 */ 157static void vidtv_bridge_on_new_pkts_avail(void *priv, u8 *buf, u32 npkts) 158{ 159 struct vidtv_dvb *dvb = priv; 160 161 /* drop packets if we lose the lock */ 162 if (vidtv_bridge_check_demod_lock(dvb, 0)) 163 dvb_dmx_swfilter_packets(&dvb->demux, buf, npkts); 164} 165 166static int vidtv_start_streaming(struct vidtv_dvb *dvb) 167{ 168 struct vidtv_mux_init_args mux_args = { 169 .mux_rate_kbytes_sec = mux_rate_kbytes_sec, 170 .on_new_packets_available_cb = vidtv_bridge_on_new_pkts_avail, 171 .pcr_period_usecs = pcr_period_msec * USEC_PER_MSEC, 172 .si_period_usecs = si_period_msec * USEC_PER_MSEC, 173 .pcr_pid = pcr_pid, 174 .transport_stream_id = VIDTV_DEFAULT_TS_ID, 175 .network_id = VIDTV_DEFAULT_NETWORK_ID, 176 .network_name = VIDTV_DEFAULT_NETWORK_NAME, 177 .priv = dvb, 178 }; 179 struct device *dev = &dvb->pdev->dev; 180 u32 mux_buf_sz; 181 182 if (dvb->streaming) { 183 dev_warn_ratelimited(dev, "Already streaming. Skipping.\n"); 184 return 0; 185 } 186 187 if (mux_buf_sz_pkts) 188 mux_buf_sz = mux_buf_sz_pkts; 189 else 190 mux_buf_sz = vidtv_bridge_mux_buf_sz_for_mux_rate(); 191 192 mux_args.mux_buf_sz = mux_buf_sz; 193 194 dvb->streaming = true; 195 dvb->mux = vidtv_mux_init(dvb->fe[0], dev, &mux_args); 196 if (!dvb->mux) 197 return -ENOMEM; 198 vidtv_mux_start_thread(dvb->mux); 199 200 dev_dbg_ratelimited(dev, "Started streaming\n"); 201 return 0; 202} 203 204static int vidtv_stop_streaming(struct vidtv_dvb *dvb) 205{ 206 struct device *dev = &dvb->pdev->dev; 207 208 dvb->streaming = false; 209 vidtv_mux_stop_thread(dvb->mux); 210 vidtv_mux_destroy(dvb->mux); 211 dvb->mux = NULL; 212 213 dev_dbg_ratelimited(dev, "Stopped streaming\n"); 214 return 0; 215} 216 217static int vidtv_start_feed(struct dvb_demux_feed *feed) 218{ 219 struct dvb_demux *demux = feed->demux; 220 struct vidtv_dvb *dvb = demux->priv; 221 int ret; 222 int rc; 223 224 if (!demux->dmx.frontend) 225 return -EINVAL; 226 227 mutex_lock(&dvb->feed_lock); 228 229 dvb->nfeeds++; 230 rc = dvb->nfeeds; 231 232 if (dvb->nfeeds == 1) { 233 ret = vidtv_start_streaming(dvb); 234 if (ret < 0) 235 rc = ret; 236 } 237 238 mutex_unlock(&dvb->feed_lock); 239 return rc; 240} 241 242static int vidtv_stop_feed(struct dvb_demux_feed *feed) 243{ 244 struct dvb_demux *demux = feed->demux; 245 struct vidtv_dvb *dvb = demux->priv; 246 int err = 0; 247 248 mutex_lock(&dvb->feed_lock); 249 dvb->nfeeds--; 250 251 if (!dvb->nfeeds) 252 err = vidtv_stop_streaming(dvb); 253 254 mutex_unlock(&dvb->feed_lock); 255 return err; 256} 257 258static struct dvb_frontend *vidtv_get_frontend_ptr(struct i2c_client *c) 259{ 260 struct vidtv_demod_state *state = i2c_get_clientdata(c); 261 262 /* the demod will set this when its probe function runs */ 263 return &state->frontend; 264} 265 266static int vidtv_master_xfer(struct i2c_adapter *i2c_adap, 267 struct i2c_msg msgs[], 268 int num) 269{ 270 /* 271 * Right now, this virtual driver doesn't really send or receive 272 * messages from I2C. A real driver will require an implementation 273 * here. 274 */ 275 return 0; 276} 277 278static u32 vidtv_i2c_func(struct i2c_adapter *adapter) 279{ 280 return I2C_FUNC_I2C; 281} 282 283static const struct i2c_algorithm vidtv_i2c_algorithm = { 284 .master_xfer = vidtv_master_xfer, 285 .functionality = vidtv_i2c_func, 286}; 287 288static int vidtv_bridge_i2c_register_adap(struct vidtv_dvb *dvb) 289{ 290 struct i2c_adapter *i2c_adapter = &dvb->i2c_adapter; 291 292 strscpy(i2c_adapter->name, "vidtv_i2c", sizeof(i2c_adapter->name)); 293 i2c_adapter->owner = THIS_MODULE; 294 i2c_adapter->algo = &vidtv_i2c_algorithm; 295 i2c_adapter->algo_data = NULL; 296 i2c_adapter->timeout = 500; 297 i2c_adapter->retries = 3; 298 i2c_adapter->dev.parent = &dvb->pdev->dev; 299 300 i2c_set_adapdata(i2c_adapter, dvb); 301 return i2c_add_adapter(&dvb->i2c_adapter); 302} 303 304static int vidtv_bridge_register_adap(struct vidtv_dvb *dvb) 305{ 306 int ret = 0; 307 308 ret = dvb_register_adapter(&dvb->adapter, 309 KBUILD_MODNAME, 310 THIS_MODULE, 311 &dvb->i2c_adapter.dev, 312 adapter_nums); 313 314 return ret; 315} 316 317static int vidtv_bridge_dmx_init(struct vidtv_dvb *dvb) 318{ 319 dvb->demux.dmx.capabilities = DMX_TS_FILTERING | 320 DMX_SECTION_FILTERING; 321 322 dvb->demux.priv = dvb; 323 dvb->demux.filternum = 256; 324 dvb->demux.feednum = 256; 325 dvb->demux.start_feed = vidtv_start_feed; 326 dvb->demux.stop_feed = vidtv_stop_feed; 327 328 return dvb_dmx_init(&dvb->demux); 329} 330 331static int vidtv_bridge_dmxdev_init(struct vidtv_dvb *dvb) 332{ 333 dvb->dmx_dev.filternum = 256; 334 dvb->dmx_dev.demux = &dvb->demux.dmx; 335 dvb->dmx_dev.capabilities = 0; 336 337 return dvb_dmxdev_init(&dvb->dmx_dev, &dvb->adapter); 338} 339 340static int vidtv_bridge_probe_demod(struct vidtv_dvb *dvb, u32 n) 341{ 342 struct vidtv_demod_config cfg = { 343 .drop_tslock_prob_on_low_snr = drop_tslock_prob_on_low_snr, 344 .recover_tslock_prob_on_good_snr = recover_tslock_prob_on_good_snr, 345 }; 346 dvb->i2c_client_demod[n] = dvb_module_probe("dvb_vidtv_demod", 347 NULL, 348 &dvb->i2c_adapter, 349 DEMOD_DEFAULT_ADDR, 350 &cfg); 351 352 /* driver will not work anyways so bail out */ 353 if (!dvb->i2c_client_demod[n]) 354 return -ENODEV; 355 356 /* retrieve a ptr to the frontend state */ 357 dvb->fe[n] = vidtv_get_frontend_ptr(dvb->i2c_client_demod[n]); 358 359 return 0; 360} 361 362static int vidtv_bridge_probe_tuner(struct vidtv_dvb *dvb, u32 n) 363{ 364 struct vidtv_tuner_config cfg = { 365 .fe = dvb->fe[n], 366 .mock_power_up_delay_msec = mock_power_up_delay_msec, 367 .mock_tune_delay_msec = mock_tune_delay_msec, 368 }; 369 u32 freq; 370 int i; 371 372 /* TODO: check if the frequencies are at a valid range */ 373 374 memcpy(cfg.vidtv_valid_dvb_t_freqs, 375 vidtv_valid_dvb_t_freqs, 376 sizeof(vidtv_valid_dvb_t_freqs)); 377 378 memcpy(cfg.vidtv_valid_dvb_c_freqs, 379 vidtv_valid_dvb_c_freqs, 380 sizeof(vidtv_valid_dvb_c_freqs)); 381 382 /* 383 * Convert Satellite frequencies from Ku-band in kHZ into S-band 384 * frequencies in Hz. 385 */ 386 for (i = 0; i < ARRAY_SIZE(vidtv_valid_dvb_s_freqs); i++) { 387 freq = vidtv_valid_dvb_s_freqs[i]; 388 if (freq) { 389 if (freq < LNB_CUT_FREQUENCY) 390 freq = abs(freq - LNB_LOW_FREQ); 391 else 392 freq = abs(freq - LNB_HIGH_FREQ); 393 } 394 cfg.vidtv_valid_dvb_s_freqs[i] = freq; 395 } 396 397 cfg.max_frequency_shift_hz = max_frequency_shift_hz; 398 399 dvb->i2c_client_tuner[n] = dvb_module_probe("dvb_vidtv_tuner", 400 NULL, 401 &dvb->i2c_adapter, 402 TUNER_DEFAULT_ADDR, 403 &cfg); 404 405 return (dvb->i2c_client_tuner[n]) ? 0 : -ENODEV; 406} 407 408static int vidtv_bridge_dvb_init(struct vidtv_dvb *dvb) 409{ 410 int ret, i, j; 411 412 ret = vidtv_bridge_i2c_register_adap(dvb); 413 if (ret < 0) 414 goto fail_i2c; 415 416 ret = vidtv_bridge_register_adap(dvb); 417 if (ret < 0) 418 goto fail_adapter; 419 dvb_register_media_controller(&dvb->adapter, &dvb->mdev); 420 421 for (i = 0; i < NUM_FE; ++i) { 422 ret = vidtv_bridge_probe_demod(dvb, i); 423 if (ret < 0) 424 goto fail_demod_probe; 425 426 ret = vidtv_bridge_probe_tuner(dvb, i); 427 if (ret < 0) 428 goto fail_tuner_probe; 429 430 ret = dvb_register_frontend(&dvb->adapter, dvb->fe[i]); 431 if (ret < 0) 432 goto fail_fe; 433 } 434 435 ret = vidtv_bridge_dmx_init(dvb); 436 if (ret < 0) 437 goto fail_dmx; 438 439 ret = vidtv_bridge_dmxdev_init(dvb); 440 if (ret < 0) 441 goto fail_dmx_dev; 442 443 for (j = 0; j < NUM_FE; ++j) { 444 ret = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, 445 &dvb->dmx_fe[j]); 446 if (ret < 0) 447 goto fail_dmx_conn; 448 449 /* 450 * The source of the demux is a frontend connected 451 * to the demux. 452 */ 453 dvb->dmx_fe[j].source = DMX_FRONTEND_0; 454 } 455 456 return ret; 457 458fail_dmx_conn: 459 for (j = j - 1; j >= 0; --j) 460 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, 461 &dvb->dmx_fe[j]); 462fail_dmx_dev: 463 dvb_dmxdev_release(&dvb->dmx_dev); 464fail_dmx: 465 dvb_dmx_release(&dvb->demux); 466fail_fe: 467 for (j = i; j >= 0; --j) 468 dvb_unregister_frontend(dvb->fe[j]); 469fail_tuner_probe: 470 for (j = i; j >= 0; --j) 471 if (dvb->i2c_client_tuner[j]) 472 dvb_module_release(dvb->i2c_client_tuner[j]); 473 474fail_demod_probe: 475 for (j = i; j >= 0; --j) 476 if (dvb->i2c_client_demod[j]) 477 dvb_module_release(dvb->i2c_client_demod[j]); 478 479fail_adapter: 480 dvb_unregister_adapter(&dvb->adapter); 481 482fail_i2c: 483 i2c_del_adapter(&dvb->i2c_adapter); 484 485 return ret; 486} 487 488static int vidtv_bridge_probe(struct platform_device *pdev) 489{ 490 struct vidtv_dvb *dvb; 491 int ret; 492 493 dvb = kzalloc(sizeof(*dvb), GFP_KERNEL); 494 if (!dvb) 495 return -ENOMEM; 496 497 dvb->pdev = pdev; 498 499#ifdef CONFIG_MEDIA_CONTROLLER_DVB 500 dvb->mdev.dev = &pdev->dev; 501 502 strscpy(dvb->mdev.model, "vidtv", sizeof(dvb->mdev.model)); 503 strscpy(dvb->mdev.bus_info, "platform:vidtv", sizeof(dvb->mdev.bus_info)); 504 505 media_device_init(&dvb->mdev); 506#endif 507 508 ret = vidtv_bridge_dvb_init(dvb); 509 if (ret < 0) 510 goto err_dvb; 511 512 mutex_init(&dvb->feed_lock); 513 514 platform_set_drvdata(pdev, dvb); 515 516#ifdef CONFIG_MEDIA_CONTROLLER_DVB 517 ret = media_device_register(&dvb->mdev); 518 if (ret) { 519 dev_err(dvb->mdev.dev, 520 "media device register failed (err=%d)\n", ret); 521 goto err_media_device_register; 522 } 523#endif /* CONFIG_MEDIA_CONTROLLER_DVB */ 524 525 dev_info(&pdev->dev, "Successfully initialized vidtv!\n"); 526 return ret; 527 528#ifdef CONFIG_MEDIA_CONTROLLER_DVB 529err_media_device_register: 530 media_device_cleanup(&dvb->mdev); 531#endif /* CONFIG_MEDIA_CONTROLLER_DVB */ 532err_dvb: 533 kfree(dvb); 534 return ret; 535} 536 537static int vidtv_bridge_remove(struct platform_device *pdev) 538{ 539 struct vidtv_dvb *dvb; 540 u32 i; 541 542 dvb = platform_get_drvdata(pdev); 543 544#ifdef CONFIG_MEDIA_CONTROLLER_DVB 545 media_device_unregister(&dvb->mdev); 546 media_device_cleanup(&dvb->mdev); 547#endif /* CONFIG_MEDIA_CONTROLLER_DVB */ 548 549 mutex_destroy(&dvb->feed_lock); 550 551 for (i = 0; i < NUM_FE; ++i) { 552 dvb_unregister_frontend(dvb->fe[i]); 553 dvb_module_release(dvb->i2c_client_tuner[i]); 554 dvb_module_release(dvb->i2c_client_demod[i]); 555 } 556 557 dvb_dmxdev_release(&dvb->dmx_dev); 558 dvb_dmx_release(&dvb->demux); 559 dvb_unregister_adapter(&dvb->adapter); 560 dev_info(&pdev->dev, "Successfully removed vidtv\n"); 561 562 return 0; 563} 564 565static void vidtv_bridge_dev_release(struct device *dev) 566{ 567 struct vidtv_dvb *dvb; 568 569 dvb = dev_get_drvdata(dev); 570 kfree(dvb); 571} 572 573static struct platform_device vidtv_bridge_dev = { 574 .name = VIDTV_PDEV_NAME, 575 .dev.release = vidtv_bridge_dev_release, 576}; 577 578static struct platform_driver vidtv_bridge_driver = { 579 .driver = { 580 .name = VIDTV_PDEV_NAME, 581 }, 582 .probe = vidtv_bridge_probe, 583 .remove = vidtv_bridge_remove, 584}; 585 586static void __exit vidtv_bridge_exit(void) 587{ 588 platform_driver_unregister(&vidtv_bridge_driver); 589 platform_device_unregister(&vidtv_bridge_dev); 590} 591 592static int __init vidtv_bridge_init(void) 593{ 594 int ret; 595 596 ret = platform_device_register(&vidtv_bridge_dev); 597 if (ret) 598 return ret; 599 600 ret = platform_driver_register(&vidtv_bridge_driver); 601 if (ret) 602 platform_device_unregister(&vidtv_bridge_dev); 603 604 return ret; 605} 606 607module_init(vidtv_bridge_init); 608module_exit(vidtv_bridge_exit); 609 610MODULE_DESCRIPTION("Virtual Digital TV Test Driver"); 611MODULE_AUTHOR("Daniel W. S. Almeida"); 612MODULE_LICENSE("GPL"); 613MODULE_ALIAS("vidtv"); 614MODULE_ALIAS("dvb_vidtv");