1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * dvb_frontend.c: DVB frontend tuning interface/thread 4 * 5 * Copyright (C) 1999-2001 Ralph Metzler 6 * Marcus Metzler 7 * Holger Waechtler 8 * for convergence integrated media GmbH 9 * 10 * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup) 11 */ 12 13 /* Enables DVBv3 compatibility bits at the headers */ 14 #define __DVB_CORE__ 15 16 #define pr_fmt(fmt) "dvb_frontend: " fmt 17 18 #include <linux/string.h> 19 #include <linux/kernel.h> 20 #include <linux/sched/signal.h> 21 #include <linux/wait.h> 22 #include <linux/slab.h> 23 #include <linux/poll.h> 24 #include <linux/semaphore.h> 25 #include <linux/module.h> 26 #include <linux/list.h> 27 #include <linux/freezer.h> 28 #include <linux/jiffies.h> 29 #include <linux/kthread.h> 30 #include <linux/ktime.h> 31 #include <linux/compat.h> 32 #include <asm/processor.h> 33 34 #include <media/dvb_frontend.h> 35 #include <media/dvbdev.h> 36 #include <linux/dvb/version.h> 37 38 static int dvb_frontend_debug; 39 static int dvb_shutdown_timeout; 40 static int dvb_force_auto_inversion; 41 static int dvb_override_tune_delay; 42 static int dvb_powerdown_on_sleep = 1; 43 static int dvb_mfe_wait_time = 5; 44 45 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644); 46 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off)."); 47 module_param(dvb_shutdown_timeout, int, 0644); 48 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware"); 49 module_param(dvb_force_auto_inversion, int, 0644); 50 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always"); 51 module_param(dvb_override_tune_delay, int, 0644); 52 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt"); 53 module_param(dvb_powerdown_on_sleep, int, 0644); 54 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)"); 55 module_param(dvb_mfe_wait_time, int, 0644); 56 MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)"); 57 58 #define dprintk(fmt, arg...) \ 59 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg) 60 61 #define FESTATE_IDLE 1 62 #define FESTATE_RETUNE 2 63 #define FESTATE_TUNING_FAST 4 64 #define FESTATE_TUNING_SLOW 8 65 #define FESTATE_TUNED 16 66 #define FESTATE_ZIGZAG_FAST 32 67 #define FESTATE_ZIGZAG_SLOW 64 68 #define FESTATE_DISEQC 128 69 #define FESTATE_ERROR 256 70 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC) 71 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST) 72 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW) 73 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW) 74 75 /* 76 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling. 77 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune. 78 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress. 79 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower. 80 * FESTATE_TUNED. The frontend has successfully locked on. 81 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it. 82 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower. 83 * FESTATE_DISEQC. A DISEQC command has just been issued. 84 * FESTATE_WAITFORLOCK. When we're waiting for a lock. 85 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan. 86 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan. 87 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again. 88 */ 89 90 static DEFINE_MUTEX(frontend_mutex); 91 92 struct dvb_frontend_private { 93 /* thread/frontend values */ 94 struct dvb_device *dvbdev; 95 struct dvb_frontend_parameters parameters_out; 96 struct dvb_fe_events events; 97 struct semaphore sem; 98 struct list_head list_head; 99 wait_queue_head_t wait_queue; 100 struct task_struct *thread; 101 unsigned long release_jiffies; 102 unsigned int wakeup; 103 enum fe_status status; 104 unsigned long tune_mode_flags; 105 unsigned int delay; 106 unsigned int reinitialise; 107 int tone; 108 int voltage; 109 110 /* swzigzag values */ 111 unsigned int state; 112 unsigned int bending; 113 int lnb_drift; 114 unsigned int inversion; 115 unsigned int auto_step; 116 unsigned int auto_sub_step; 117 unsigned int started_auto_step; 118 unsigned int min_delay; 119 unsigned int max_drift; 120 unsigned int step_size; 121 int quality; 122 unsigned int check_wrapped; 123 enum dvbfe_search algo_status; 124 125 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 126 struct media_pipeline pipe; 127 #endif 128 }; 129 130 static void dvb_frontend_invoke_release(struct dvb_frontend *fe, 131 void (*release)(struct dvb_frontend *fe)); 132 133 static void __dvb_frontend_free(struct dvb_frontend *fe) 134 { 135 struct dvb_frontend_private *fepriv = fe->frontend_priv; 136 137 if (fepriv) 138 dvb_free_device(fepriv->dvbdev); 139 140 dvb_frontend_invoke_release(fe, fe->ops.release); 141 142 kfree(fepriv); 143 } 144 145 static void dvb_frontend_free(struct kref *ref) 146 { 147 struct dvb_frontend *fe = 148 container_of(ref, struct dvb_frontend, refcount); 149 150 __dvb_frontend_free(fe); 151 } 152 153 static void dvb_frontend_put(struct dvb_frontend *fe) 154 { 155 /* 156 * Check if the frontend was registered, as otherwise 157 * kref was not initialized yet. 158 */ 159 if (fe->frontend_priv) 160 kref_put(&fe->refcount, dvb_frontend_free); 161 else 162 __dvb_frontend_free(fe); 163 } 164 165 static void dvb_frontend_get(struct dvb_frontend *fe) 166 { 167 kref_get(&fe->refcount); 168 } 169 170 static void dvb_frontend_wakeup(struct dvb_frontend *fe); 171 static int dtv_get_frontend(struct dvb_frontend *fe, 172 struct dtv_frontend_properties *c, 173 struct dvb_frontend_parameters *p_out); 174 static int 175 dtv_property_legacy_params_sync(struct dvb_frontend *fe, 176 const struct dtv_frontend_properties *c, 177 struct dvb_frontend_parameters *p); 178 179 static bool has_get_frontend(struct dvb_frontend *fe) 180 { 181 return fe->ops.get_frontend; 182 } 183 184 /* 185 * Due to DVBv3 API calls, a delivery system should be mapped into one of 186 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC), 187 * otherwise, a DVBv3 call will fail. 188 */ 189 enum dvbv3_emulation_type { 190 DVBV3_UNKNOWN, 191 DVBV3_QPSK, 192 DVBV3_QAM, 193 DVBV3_OFDM, 194 DVBV3_ATSC, 195 }; 196 197 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system) 198 { 199 switch (delivery_system) { 200 case SYS_DVBC_ANNEX_A: 201 case SYS_DVBC_ANNEX_C: 202 return DVBV3_QAM; 203 case SYS_DVBS: 204 case SYS_DVBS2: 205 case SYS_TURBO: 206 case SYS_ISDBS: 207 case SYS_DSS: 208 return DVBV3_QPSK; 209 case SYS_DVBT: 210 case SYS_DVBT2: 211 case SYS_ISDBT: 212 case SYS_DTMB: 213 return DVBV3_OFDM; 214 case SYS_ATSC: 215 case SYS_ATSCMH: 216 case SYS_DVBC_ANNEX_B: 217 return DVBV3_ATSC; 218 case SYS_UNDEFINED: 219 case SYS_ISDBC: 220 case SYS_DVBH: 221 case SYS_DAB: 222 default: 223 /* 224 * Doesn't know how to emulate those types and/or 225 * there's no frontend driver from this type yet 226 * with some emulation code, so, we're not sure yet how 227 * to handle them, or they're not compatible with a DVBv3 call. 228 */ 229 return DVBV3_UNKNOWN; 230 } 231 } 232 233 static void dvb_frontend_add_event(struct dvb_frontend *fe, 234 enum fe_status status) 235 { 236 struct dvb_frontend_private *fepriv = fe->frontend_priv; 237 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 238 struct dvb_fe_events *events = &fepriv->events; 239 struct dvb_frontend_event *e; 240 int wp; 241 242 dev_dbg(fe->dvb->device, "%s:\n", __func__); 243 244 if ((status & FE_HAS_LOCK) && has_get_frontend(fe)) 245 dtv_get_frontend(fe, c, &fepriv->parameters_out); 246 247 mutex_lock(&events->mtx); 248 249 wp = (events->eventw + 1) % MAX_EVENT; 250 if (wp == events->eventr) { 251 events->overflow = 1; 252 events->eventr = (events->eventr + 1) % MAX_EVENT; 253 } 254 255 e = &events->events[events->eventw]; 256 e->status = status; 257 e->parameters = fepriv->parameters_out; 258 259 events->eventw = wp; 260 261 mutex_unlock(&events->mtx); 262 263 wake_up_interruptible(&events->wait_queue); 264 } 265 266 static int dvb_frontend_test_event(struct dvb_frontend_private *fepriv, 267 struct dvb_fe_events *events) 268 { 269 int ret; 270 271 up(&fepriv->sem); 272 ret = events->eventw != events->eventr; 273 down(&fepriv->sem); 274 275 return ret; 276 } 277 278 static int dvb_frontend_get_event(struct dvb_frontend *fe, 279 struct dvb_frontend_event *event, int flags) 280 { 281 struct dvb_frontend_private *fepriv = fe->frontend_priv; 282 struct dvb_fe_events *events = &fepriv->events; 283 284 dev_dbg(fe->dvb->device, "%s:\n", __func__); 285 286 if (events->overflow) { 287 events->overflow = 0; 288 return -EOVERFLOW; 289 } 290 291 if (events->eventw == events->eventr) { 292 int ret; 293 294 if (flags & O_NONBLOCK) 295 return -EWOULDBLOCK; 296 297 ret = wait_event_interruptible(events->wait_queue, 298 dvb_frontend_test_event(fepriv, events)); 299 300 if (ret < 0) 301 return ret; 302 } 303 304 mutex_lock(&events->mtx); 305 *event = events->events[events->eventr]; 306 events->eventr = (events->eventr + 1) % MAX_EVENT; 307 mutex_unlock(&events->mtx); 308 309 return 0; 310 } 311 312 static void dvb_frontend_clear_events(struct dvb_frontend *fe) 313 { 314 struct dvb_frontend_private *fepriv = fe->frontend_priv; 315 struct dvb_fe_events *events = &fepriv->events; 316 317 mutex_lock(&events->mtx); 318 events->eventr = events->eventw; 319 mutex_unlock(&events->mtx); 320 } 321 322 static void dvb_frontend_init(struct dvb_frontend *fe) 323 { 324 dev_dbg(fe->dvb->device, 325 "%s: initialising adapter %i frontend %i (%s)...\n", 326 __func__, fe->dvb->num, fe->id, fe->ops.info.name); 327 328 if (fe->ops.init) 329 fe->ops.init(fe); 330 if (fe->ops.tuner_ops.init) { 331 if (fe->ops.i2c_gate_ctrl) 332 fe->ops.i2c_gate_ctrl(fe, 1); 333 fe->ops.tuner_ops.init(fe); 334 if (fe->ops.i2c_gate_ctrl) 335 fe->ops.i2c_gate_ctrl(fe, 0); 336 } 337 } 338 339 void dvb_frontend_reinitialise(struct dvb_frontend *fe) 340 { 341 struct dvb_frontend_private *fepriv = fe->frontend_priv; 342 343 fepriv->reinitialise = 1; 344 dvb_frontend_wakeup(fe); 345 } 346 EXPORT_SYMBOL(dvb_frontend_reinitialise); 347 348 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked) 349 { 350 int q2; 351 struct dvb_frontend *fe = fepriv->dvbdev->priv; 352 353 dev_dbg(fe->dvb->device, "%s:\n", __func__); 354 355 if (locked) 356 (fepriv->quality) = (fepriv->quality * 220 + 36 * 256) / 256; 357 else 358 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256; 359 360 q2 = fepriv->quality - 128; 361 q2 *= q2; 362 363 fepriv->delay = fepriv->min_delay + q2 * HZ / (128 * 128); 364 } 365 366 /** 367 * dvb_frontend_swzigzag_autotune - Performs automatic twiddling of frontend 368 * parameters. 369 * 370 * @fe: The frontend concerned. 371 * @check_wrapped: Checks if an iteration has completed. 372 * DO NOT SET ON THE FIRST ATTEMPT. 373 * 374 * return: Number of complete iterations that have been performed. 375 */ 376 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped) 377 { 378 int autoinversion; 379 int ready = 0; 380 int fe_set_err = 0; 381 struct dvb_frontend_private *fepriv = fe->frontend_priv; 382 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp; 383 int original_inversion = c->inversion; 384 u32 original_frequency = c->frequency; 385 386 /* are we using autoinversion? */ 387 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) && 388 (c->inversion == INVERSION_AUTO)); 389 390 /* setup parameters correctly */ 391 while (!ready) { 392 /* calculate the lnb_drift */ 393 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size; 394 395 /* wrap the auto_step if we've exceeded the maximum drift */ 396 if (fepriv->lnb_drift > fepriv->max_drift) { 397 fepriv->auto_step = 0; 398 fepriv->auto_sub_step = 0; 399 fepriv->lnb_drift = 0; 400 } 401 402 /* perform inversion and +/- zigzag */ 403 switch (fepriv->auto_sub_step) { 404 case 0: 405 /* try with the current inversion and current drift setting */ 406 ready = 1; 407 break; 408 409 case 1: 410 if (!autoinversion) break; 411 412 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF; 413 ready = 1; 414 break; 415 416 case 2: 417 if (fepriv->lnb_drift == 0) break; 418 419 fepriv->lnb_drift = -fepriv->lnb_drift; 420 ready = 1; 421 break; 422 423 case 3: 424 if (fepriv->lnb_drift == 0) break; 425 if (!autoinversion) break; 426 427 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF; 428 fepriv->lnb_drift = -fepriv->lnb_drift; 429 ready = 1; 430 break; 431 432 default: 433 fepriv->auto_step++; 434 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */ 435 break; 436 } 437 438 if (!ready) fepriv->auto_sub_step++; 439 } 440 441 /* if this attempt would hit where we started, indicate a complete 442 * iteration has occurred */ 443 if ((fepriv->auto_step == fepriv->started_auto_step) && 444 (fepriv->auto_sub_step == 0) && check_wrapped) { 445 return 1; 446 } 447 448 dev_dbg(fe->dvb->device, 449 "%s: drift:%i inversion:%i auto_step:%i auto_sub_step:%i started_auto_step:%i\n", 450 __func__, fepriv->lnb_drift, fepriv->inversion, 451 fepriv->auto_step, fepriv->auto_sub_step, 452 fepriv->started_auto_step); 453 454 /* set the frontend itself */ 455 c->frequency += fepriv->lnb_drift; 456 if (autoinversion) 457 c->inversion = fepriv->inversion; 458 tmp = *c; 459 if (fe->ops.set_frontend) 460 fe_set_err = fe->ops.set_frontend(fe); 461 *c = tmp; 462 if (fe_set_err < 0) { 463 fepriv->state = FESTATE_ERROR; 464 return fe_set_err; 465 } 466 467 c->frequency = original_frequency; 468 c->inversion = original_inversion; 469 470 fepriv->auto_sub_step++; 471 return 0; 472 } 473 474 static void dvb_frontend_swzigzag(struct dvb_frontend *fe) 475 { 476 enum fe_status s = FE_NONE; 477 int retval = 0; 478 struct dvb_frontend_private *fepriv = fe->frontend_priv; 479 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp; 480 481 /* if we've got no parameters, just keep idling */ 482 if (fepriv->state & FESTATE_IDLE) { 483 fepriv->delay = 3 * HZ; 484 fepriv->quality = 0; 485 return; 486 } 487 488 /* in SCAN mode, we just set the frontend when asked and leave it alone */ 489 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) { 490 if (fepriv->state & FESTATE_RETUNE) { 491 tmp = *c; 492 if (fe->ops.set_frontend) 493 retval = fe->ops.set_frontend(fe); 494 *c = tmp; 495 if (retval < 0) 496 fepriv->state = FESTATE_ERROR; 497 else 498 fepriv->state = FESTATE_TUNED; 499 } 500 fepriv->delay = 3 * HZ; 501 fepriv->quality = 0; 502 return; 503 } 504 505 /* get the frontend status */ 506 if (fepriv->state & FESTATE_RETUNE) { 507 s = 0; 508 } else { 509 if (fe->ops.read_status) 510 fe->ops.read_status(fe, &s); 511 if (s != fepriv->status) { 512 dvb_frontend_add_event(fe, s); 513 fepriv->status = s; 514 } 515 } 516 517 /* if we're not tuned, and we have a lock, move to the TUNED state */ 518 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) { 519 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); 520 fepriv->state = FESTATE_TUNED; 521 522 /* if we're tuned, then we have determined the correct inversion */ 523 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) && 524 (c->inversion == INVERSION_AUTO)) { 525 c->inversion = fepriv->inversion; 526 } 527 return; 528 } 529 530 /* if we are tuned already, check we're still locked */ 531 if (fepriv->state & FESTATE_TUNED) { 532 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); 533 534 /* we're tuned, and the lock is still good... */ 535 if (s & FE_HAS_LOCK) { 536 return; 537 } else { /* if we _WERE_ tuned, but now don't have a lock */ 538 fepriv->state = FESTATE_ZIGZAG_FAST; 539 fepriv->started_auto_step = fepriv->auto_step; 540 fepriv->check_wrapped = 0; 541 } 542 } 543 544 /* don't actually do anything if we're in the LOSTLOCK state, 545 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */ 546 if ((fepriv->state & FESTATE_LOSTLOCK) && 547 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) { 548 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); 549 return; 550 } 551 552 /* don't do anything if we're in the DISEQC state, since this 553 * might be someone with a motorized dish controlled by DISEQC. 554 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */ 555 if (fepriv->state & FESTATE_DISEQC) { 556 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); 557 return; 558 } 559 560 /* if we're in the RETUNE state, set everything up for a brand 561 * new scan, keeping the current inversion setting, as the next 562 * tune is _very_ likely to require the same */ 563 if (fepriv->state & FESTATE_RETUNE) { 564 fepriv->lnb_drift = 0; 565 fepriv->auto_step = 0; 566 fepriv->auto_sub_step = 0; 567 fepriv->started_auto_step = 0; 568 fepriv->check_wrapped = 0; 569 } 570 571 /* fast zigzag. */ 572 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) { 573 fepriv->delay = fepriv->min_delay; 574 575 /* perform a tune */ 576 retval = dvb_frontend_swzigzag_autotune(fe, 577 fepriv->check_wrapped); 578 if (retval < 0) { 579 return; 580 } else if (retval) { 581 /* OK, if we've run out of trials at the fast speed. 582 * Drop back to slow for the _next_ attempt */ 583 fepriv->state = FESTATE_SEARCHING_SLOW; 584 fepriv->started_auto_step = fepriv->auto_step; 585 return; 586 } 587 fepriv->check_wrapped = 1; 588 589 /* if we've just re-tuned, enter the ZIGZAG_FAST state. 590 * This ensures we cannot return from an 591 * FE_SET_FRONTEND ioctl before the first frontend tune 592 * occurs */ 593 if (fepriv->state & FESTATE_RETUNE) { 594 fepriv->state = FESTATE_TUNING_FAST; 595 } 596 } 597 598 /* slow zigzag */ 599 if (fepriv->state & FESTATE_SEARCHING_SLOW) { 600 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); 601 602 /* Note: don't bother checking for wrapping; we stay in this 603 * state until we get a lock */ 604 dvb_frontend_swzigzag_autotune(fe, 0); 605 } 606 } 607 608 static int dvb_frontend_is_exiting(struct dvb_frontend *fe) 609 { 610 struct dvb_frontend_private *fepriv = fe->frontend_priv; 611 612 if (fe->exit != DVB_FE_NO_EXIT) 613 return 1; 614 615 if (fepriv->dvbdev->writers == 1) 616 if (time_after_eq(jiffies, fepriv->release_jiffies + 617 dvb_shutdown_timeout * HZ)) 618 return 1; 619 620 return 0; 621 } 622 623 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe) 624 { 625 struct dvb_frontend_private *fepriv = fe->frontend_priv; 626 627 if (fepriv->wakeup) { 628 fepriv->wakeup = 0; 629 return 1; 630 } 631 return dvb_frontend_is_exiting(fe); 632 } 633 634 static void dvb_frontend_wakeup(struct dvb_frontend *fe) 635 { 636 struct dvb_frontend_private *fepriv = fe->frontend_priv; 637 638 fepriv->wakeup = 1; 639 wake_up_interruptible(&fepriv->wait_queue); 640 } 641 642 static int dvb_frontend_thread(void *data) 643 { 644 struct dvb_frontend *fe = data; 645 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 646 struct dvb_frontend_private *fepriv = fe->frontend_priv; 647 enum fe_status s = FE_NONE; 648 enum dvbfe_algo algo; 649 bool re_tune = false; 650 bool semheld = false; 651 652 dev_dbg(fe->dvb->device, "%s:\n", __func__); 653 654 fepriv->check_wrapped = 0; 655 fepriv->quality = 0; 656 fepriv->delay = 3 * HZ; 657 fepriv->status = 0; 658 fepriv->wakeup = 0; 659 fepriv->reinitialise = 0; 660 661 dvb_frontend_init(fe); 662 663 set_freezable(); 664 while (1) { 665 up(&fepriv->sem); /* is locked when we enter the thread... */ 666 restart: 667 wait_event_interruptible_timeout(fepriv->wait_queue, 668 dvb_frontend_should_wakeup(fe) || 669 kthread_should_stop() || 670 freezing(current), 671 fepriv->delay); 672 673 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) { 674 /* got signal or quitting */ 675 if (!down_interruptible(&fepriv->sem)) 676 semheld = true; 677 fe->exit = DVB_FE_NORMAL_EXIT; 678 break; 679 } 680 681 if (try_to_freeze()) 682 goto restart; 683 684 if (down_interruptible(&fepriv->sem)) 685 break; 686 687 if (fepriv->reinitialise) { 688 dvb_frontend_init(fe); 689 if (fe->ops.set_tone && fepriv->tone != -1) 690 fe->ops.set_tone(fe, fepriv->tone); 691 if (fe->ops.set_voltage && fepriv->voltage != -1) 692 fe->ops.set_voltage(fe, fepriv->voltage); 693 fepriv->reinitialise = 0; 694 } 695 696 /* do an iteration of the tuning loop */ 697 if (fe->ops.get_frontend_algo) { 698 algo = fe->ops.get_frontend_algo(fe); 699 switch (algo) { 700 case DVBFE_ALGO_HW: 701 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__); 702 703 if (fepriv->state & FESTATE_RETUNE) { 704 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__); 705 re_tune = true; 706 fepriv->state = FESTATE_TUNED; 707 } else { 708 re_tune = false; 709 } 710 711 if (fe->ops.tune) 712 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s); 713 714 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) { 715 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__); 716 dvb_frontend_add_event(fe, s); 717 fepriv->status = s; 718 } 719 break; 720 case DVBFE_ALGO_SW: 721 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__); 722 dvb_frontend_swzigzag(fe); 723 break; 724 case DVBFE_ALGO_CUSTOM: 725 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state); 726 if (fepriv->state & FESTATE_RETUNE) { 727 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__); 728 fepriv->state = FESTATE_TUNED; 729 } 730 /* Case where we are going to search for a carrier 731 * User asked us to retune again for some reason, possibly 732 * requesting a search with a new set of parameters 733 */ 734 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) { 735 if (fe->ops.search) { 736 fepriv->algo_status = fe->ops.search(fe); 737 /* We did do a search as was requested, the flags are 738 * now unset as well and has the flags wrt to search. 739 */ 740 } else { 741 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN; 742 } 743 } 744 /* Track the carrier if the search was successful */ 745 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) { 746 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN; 747 fepriv->delay = HZ / 2; 748 } 749 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out); 750 fe->ops.read_status(fe, &s); 751 if (s != fepriv->status) { 752 dvb_frontend_add_event(fe, s); /* update event list */ 753 fepriv->status = s; 754 if (!(s & FE_HAS_LOCK)) { 755 fepriv->delay = HZ / 10; 756 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN; 757 } else { 758 fepriv->delay = 60 * HZ; 759 } 760 } 761 break; 762 default: 763 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__); 764 break; 765 } 766 } else { 767 dvb_frontend_swzigzag(fe); 768 } 769 } 770 771 if (dvb_powerdown_on_sleep) { 772 if (fe->ops.set_voltage) 773 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF); 774 if (fe->ops.tuner_ops.sleep) { 775 if (fe->ops.i2c_gate_ctrl) 776 fe->ops.i2c_gate_ctrl(fe, 1); 777 fe->ops.tuner_ops.sleep(fe); 778 if (fe->ops.i2c_gate_ctrl) 779 fe->ops.i2c_gate_ctrl(fe, 0); 780 } 781 if (fe->ops.sleep) 782 fe->ops.sleep(fe); 783 } 784 785 fepriv->thread = NULL; 786 if (kthread_should_stop()) 787 fe->exit = DVB_FE_DEVICE_REMOVED; 788 else 789 fe->exit = DVB_FE_NO_EXIT; 790 mb(); 791 792 if (semheld) 793 up(&fepriv->sem); 794 dvb_frontend_wakeup(fe); 795 return 0; 796 } 797 798 static void dvb_frontend_stop(struct dvb_frontend *fe) 799 { 800 struct dvb_frontend_private *fepriv = fe->frontend_priv; 801 802 dev_dbg(fe->dvb->device, "%s:\n", __func__); 803 804 if (fe->exit != DVB_FE_DEVICE_REMOVED) 805 fe->exit = DVB_FE_NORMAL_EXIT; 806 mb(); 807 808 if (!fepriv->thread) 809 return; 810 811 kthread_stop(fepriv->thread); 812 813 sema_init(&fepriv->sem, 1); 814 fepriv->state = FESTATE_IDLE; 815 816 /* paranoia check in case a signal arrived */ 817 if (fepriv->thread) 818 dev_warn(fe->dvb->device, 819 "dvb_frontend_stop: warning: thread %p won't exit\n", 820 fepriv->thread); 821 } 822 823 /* 824 * Sleep for the amount of time given by add_usec parameter 825 * 826 * This needs to be as precise as possible, as it affects the detection of 827 * the dish tone command at the satellite subsystem. The precision is improved 828 * by using a scheduled msleep followed by udelay for the remainder. 829 */ 830 void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec) 831 { 832 s32 delta; 833 834 *waketime = ktime_add_us(*waketime, add_usec); 835 delta = ktime_us_delta(ktime_get_boottime(), *waketime); 836 if (delta > 2500) { 837 msleep((delta - 1500) / 1000); 838 delta = ktime_us_delta(ktime_get_boottime(), *waketime); 839 } 840 if (delta > 0) 841 udelay(delta); 842 } 843 EXPORT_SYMBOL(dvb_frontend_sleep_until); 844 845 static int dvb_frontend_start(struct dvb_frontend *fe) 846 { 847 int ret; 848 struct dvb_frontend_private *fepriv = fe->frontend_priv; 849 struct task_struct *fe_thread; 850 851 dev_dbg(fe->dvb->device, "%s:\n", __func__); 852 853 if (fepriv->thread) { 854 if (fe->exit == DVB_FE_NO_EXIT) 855 return 0; 856 else 857 dvb_frontend_stop(fe); 858 } 859 860 if (signal_pending(current)) 861 return -EINTR; 862 if (down_interruptible(&fepriv->sem)) 863 return -EINTR; 864 865 fepriv->state = FESTATE_IDLE; 866 fe->exit = DVB_FE_NO_EXIT; 867 fepriv->thread = NULL; 868 mb(); 869 870 fe_thread = kthread_run(dvb_frontend_thread, fe, 871 "kdvb-ad-%i-fe-%i", fe->dvb->num, fe->id); 872 if (IS_ERR(fe_thread)) { 873 ret = PTR_ERR(fe_thread); 874 dev_warn(fe->dvb->device, 875 "dvb_frontend_start: failed to start kthread (%d)\n", 876 ret); 877 up(&fepriv->sem); 878 return ret; 879 } 880 fepriv->thread = fe_thread; 881 return 0; 882 } 883 884 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe, 885 u32 *freq_min, u32 *freq_max, 886 u32 *tolerance) 887 { 888 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 889 u32 tuner_min = fe->ops.tuner_ops.info.frequency_min_hz; 890 u32 tuner_max = fe->ops.tuner_ops.info.frequency_max_hz; 891 u32 frontend_min = fe->ops.info.frequency_min_hz; 892 u32 frontend_max = fe->ops.info.frequency_max_hz; 893 894 *freq_min = max(frontend_min, tuner_min); 895 896 if (frontend_max == 0) 897 *freq_max = tuner_max; 898 else if (tuner_max == 0) 899 *freq_max = frontend_max; 900 else 901 *freq_max = min(frontend_max, tuner_max); 902 903 if (*freq_min == 0 || *freq_max == 0) 904 dev_warn(fe->dvb->device, 905 "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n", 906 fe->dvb->num, fe->id); 907 908 dev_dbg(fe->dvb->device, "frequency interval: tuner: %u...%u, frontend: %u...%u", 909 tuner_min, tuner_max, frontend_min, frontend_max); 910 911 /* If the standard is for satellite, convert frequencies to kHz */ 912 switch (c->delivery_system) { 913 case SYS_DVBS: 914 case SYS_DVBS2: 915 case SYS_TURBO: 916 case SYS_ISDBS: 917 *freq_min /= kHz; 918 *freq_max /= kHz; 919 if (tolerance) 920 *tolerance = fe->ops.info.frequency_tolerance_hz / kHz; 921 922 break; 923 default: 924 if (tolerance) 925 *tolerance = fe->ops.info.frequency_tolerance_hz; 926 break; 927 } 928 } 929 930 static u32 dvb_frontend_get_stepsize(struct dvb_frontend *fe) 931 { 932 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 933 u32 fe_step = fe->ops.info.frequency_stepsize_hz; 934 u32 tuner_step = fe->ops.tuner_ops.info.frequency_step_hz; 935 u32 step = max(fe_step, tuner_step); 936 937 switch (c->delivery_system) { 938 case SYS_DVBS: 939 case SYS_DVBS2: 940 case SYS_TURBO: 941 case SYS_ISDBS: 942 step /= kHz; 943 break; 944 default: 945 break; 946 } 947 948 return step; 949 } 950 951 static int dvb_frontend_check_parameters(struct dvb_frontend *fe) 952 { 953 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 954 u32 freq_min; 955 u32 freq_max; 956 957 /* range check: frequency */ 958 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max, NULL); 959 if ((freq_min && c->frequency < freq_min) || 960 (freq_max && c->frequency > freq_max)) { 961 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n", 962 fe->dvb->num, fe->id, c->frequency, 963 freq_min, freq_max); 964 return -EINVAL; 965 } 966 967 /* range check: symbol rate */ 968 switch (c->delivery_system) { 969 case SYS_DVBS: 970 case SYS_DVBS2: 971 case SYS_TURBO: 972 case SYS_DVBC_ANNEX_A: 973 case SYS_DVBC_ANNEX_C: 974 if ((fe->ops.info.symbol_rate_min && 975 c->symbol_rate < fe->ops.info.symbol_rate_min) || 976 (fe->ops.info.symbol_rate_max && 977 c->symbol_rate > fe->ops.info.symbol_rate_max)) { 978 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n", 979 fe->dvb->num, fe->id, c->symbol_rate, 980 fe->ops.info.symbol_rate_min, 981 fe->ops.info.symbol_rate_max); 982 return -EINVAL; 983 } 984 default: 985 break; 986 } 987 988 return 0; 989 } 990 991 static int dvb_frontend_clear_cache(struct dvb_frontend *fe) 992 { 993 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 994 int i; 995 u32 delsys; 996 997 delsys = c->delivery_system; 998 memset(c, 0, offsetof(struct dtv_frontend_properties, strength)); 999 c->delivery_system = delsys; 1000 1001 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n", 1002 __func__, c->delivery_system); 1003 1004 c->transmission_mode = TRANSMISSION_MODE_AUTO; 1005 c->bandwidth_hz = 0; /* AUTO */ 1006 c->guard_interval = GUARD_INTERVAL_AUTO; 1007 c->hierarchy = HIERARCHY_AUTO; 1008 c->symbol_rate = 0; 1009 c->code_rate_HP = FEC_AUTO; 1010 c->code_rate_LP = FEC_AUTO; 1011 c->fec_inner = FEC_AUTO; 1012 c->rolloff = ROLLOFF_AUTO; 1013 c->voltage = SEC_VOLTAGE_OFF; 1014 c->sectone = SEC_TONE_OFF; 1015 c->pilot = PILOT_AUTO; 1016 1017 c->isdbt_partial_reception = 0; 1018 c->isdbt_sb_mode = 0; 1019 c->isdbt_sb_subchannel = 0; 1020 c->isdbt_sb_segment_idx = 0; 1021 c->isdbt_sb_segment_count = 0; 1022 c->isdbt_layer_enabled = 7; /* All layers (A,B,C) */ 1023 for (i = 0; i < 3; i++) { 1024 c->layer[i].fec = FEC_AUTO; 1025 c->layer[i].modulation = QAM_AUTO; 1026 c->layer[i].interleaving = 0; 1027 c->layer[i].segment_count = 0; 1028 } 1029 1030 c->stream_id = NO_STREAM_ID_FILTER; 1031 c->scrambling_sequence_index = 0;/* default sequence */ 1032 1033 switch (c->delivery_system) { 1034 case SYS_DVBS: 1035 case SYS_DVBS2: 1036 case SYS_TURBO: 1037 c->modulation = QPSK; /* implied for DVB-S in legacy API */ 1038 c->rolloff = ROLLOFF_35;/* implied for DVB-S */ 1039 break; 1040 case SYS_ATSC: 1041 c->modulation = VSB_8; 1042 break; 1043 case SYS_ISDBS: 1044 c->symbol_rate = 28860000; 1045 c->rolloff = ROLLOFF_35; 1046 c->bandwidth_hz = c->symbol_rate / 100 * 135; 1047 break; 1048 default: 1049 c->modulation = QAM_AUTO; 1050 break; 1051 } 1052 1053 c->lna = LNA_AUTO; 1054 1055 return 0; 1056 } 1057 1058 #define _DTV_CMD(n, s, b) \ 1059 [n] = { \ 1060 .name = #n, \ 1061 .cmd = n, \ 1062 .set = s,\ 1063 .buffer = b \ 1064 } 1065 1066 struct dtv_cmds_h { 1067 char *name; /* A display name for debugging purposes */ 1068 1069 __u32 cmd; /* A unique ID */ 1070 1071 /* Flags */ 1072 __u32 set:1; /* Either a set or get property */ 1073 __u32 buffer:1; /* Does this property use the buffer? */ 1074 __u32 reserved:30; /* Align */ 1075 }; 1076 1077 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = { 1078 _DTV_CMD(DTV_TUNE, 1, 0), 1079 _DTV_CMD(DTV_CLEAR, 1, 0), 1080 1081 /* Set */ 1082 _DTV_CMD(DTV_FREQUENCY, 1, 0), 1083 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0), 1084 _DTV_CMD(DTV_MODULATION, 1, 0), 1085 _DTV_CMD(DTV_INVERSION, 1, 0), 1086 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1), 1087 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0), 1088 _DTV_CMD(DTV_INNER_FEC, 1, 0), 1089 _DTV_CMD(DTV_VOLTAGE, 1, 0), 1090 _DTV_CMD(DTV_TONE, 1, 0), 1091 _DTV_CMD(DTV_PILOT, 1, 0), 1092 _DTV_CMD(DTV_ROLLOFF, 1, 0), 1093 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0), 1094 _DTV_CMD(DTV_HIERARCHY, 1, 0), 1095 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0), 1096 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0), 1097 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0), 1098 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0), 1099 _DTV_CMD(DTV_INTERLEAVING, 1, 0), 1100 1101 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0), 1102 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0), 1103 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0), 1104 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0), 1105 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0), 1106 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0), 1107 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0), 1108 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0), 1109 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0), 1110 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0), 1111 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0), 1112 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0), 1113 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0), 1114 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0), 1115 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0), 1116 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0), 1117 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0), 1118 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0), 1119 1120 _DTV_CMD(DTV_STREAM_ID, 1, 0), 1121 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0), 1122 _DTV_CMD(DTV_SCRAMBLING_SEQUENCE_INDEX, 1, 0), 1123 _DTV_CMD(DTV_LNA, 1, 0), 1124 1125 /* Get */ 1126 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1), 1127 _DTV_CMD(DTV_API_VERSION, 0, 0), 1128 1129 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0), 1130 1131 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0), 1132 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0), 1133 1134 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0), 1135 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0), 1136 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0), 1137 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0), 1138 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0), 1139 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0), 1140 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0), 1141 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0), 1142 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0), 1143 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0), 1144 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0), 1145 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0), 1146 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0), 1147 1148 /* Statistics API */ 1149 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0), 1150 _DTV_CMD(DTV_STAT_CNR, 0, 0), 1151 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0), 1152 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0), 1153 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0), 1154 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0), 1155 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0), 1156 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0), 1157 }; 1158 1159 /* Synchronise the legacy tuning parameters into the cache, so that demodulator 1160 * drivers can use a single set_frontend tuning function, regardless of whether 1161 * it's being used for the legacy or new API, reducing code and complexity. 1162 */ 1163 static int dtv_property_cache_sync(struct dvb_frontend *fe, 1164 struct dtv_frontend_properties *c, 1165 const struct dvb_frontend_parameters *p) 1166 { 1167 c->frequency = p->frequency; 1168 c->inversion = p->inversion; 1169 1170 switch (dvbv3_type(c->delivery_system)) { 1171 case DVBV3_QPSK: 1172 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__); 1173 c->symbol_rate = p->u.qpsk.symbol_rate; 1174 c->fec_inner = p->u.qpsk.fec_inner; 1175 break; 1176 case DVBV3_QAM: 1177 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__); 1178 c->symbol_rate = p->u.qam.symbol_rate; 1179 c->fec_inner = p->u.qam.fec_inner; 1180 c->modulation = p->u.qam.modulation; 1181 break; 1182 case DVBV3_OFDM: 1183 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__); 1184 1185 switch (p->u.ofdm.bandwidth) { 1186 case BANDWIDTH_10_MHZ: 1187 c->bandwidth_hz = 10000000; 1188 break; 1189 case BANDWIDTH_8_MHZ: 1190 c->bandwidth_hz = 8000000; 1191 break; 1192 case BANDWIDTH_7_MHZ: 1193 c->bandwidth_hz = 7000000; 1194 break; 1195 case BANDWIDTH_6_MHZ: 1196 c->bandwidth_hz = 6000000; 1197 break; 1198 case BANDWIDTH_5_MHZ: 1199 c->bandwidth_hz = 5000000; 1200 break; 1201 case BANDWIDTH_1_712_MHZ: 1202 c->bandwidth_hz = 1712000; 1203 break; 1204 case BANDWIDTH_AUTO: 1205 c->bandwidth_hz = 0; 1206 } 1207 1208 c->code_rate_HP = p->u.ofdm.code_rate_HP; 1209 c->code_rate_LP = p->u.ofdm.code_rate_LP; 1210 c->modulation = p->u.ofdm.constellation; 1211 c->transmission_mode = p->u.ofdm.transmission_mode; 1212 c->guard_interval = p->u.ofdm.guard_interval; 1213 c->hierarchy = p->u.ofdm.hierarchy_information; 1214 break; 1215 case DVBV3_ATSC: 1216 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__); 1217 c->modulation = p->u.vsb.modulation; 1218 if (c->delivery_system == SYS_ATSCMH) 1219 break; 1220 if ((c->modulation == VSB_8) || (c->modulation == VSB_16)) 1221 c->delivery_system = SYS_ATSC; 1222 else 1223 c->delivery_system = SYS_DVBC_ANNEX_B; 1224 break; 1225 case DVBV3_UNKNOWN: 1226 dev_err(fe->dvb->device, 1227 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n", 1228 __func__, c->delivery_system); 1229 return -EINVAL; 1230 } 1231 1232 return 0; 1233 } 1234 1235 /* Ensure the cached values are set correctly in the frontend 1236 * legacy tuning structures, for the advanced tuning API. 1237 */ 1238 static int 1239 dtv_property_legacy_params_sync(struct dvb_frontend *fe, 1240 const struct dtv_frontend_properties *c, 1241 struct dvb_frontend_parameters *p) 1242 { 1243 p->frequency = c->frequency; 1244 p->inversion = c->inversion; 1245 1246 switch (dvbv3_type(c->delivery_system)) { 1247 case DVBV3_UNKNOWN: 1248 dev_err(fe->dvb->device, 1249 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n", 1250 __func__, c->delivery_system); 1251 return -EINVAL; 1252 case DVBV3_QPSK: 1253 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__); 1254 p->u.qpsk.symbol_rate = c->symbol_rate; 1255 p->u.qpsk.fec_inner = c->fec_inner; 1256 break; 1257 case DVBV3_QAM: 1258 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__); 1259 p->u.qam.symbol_rate = c->symbol_rate; 1260 p->u.qam.fec_inner = c->fec_inner; 1261 p->u.qam.modulation = c->modulation; 1262 break; 1263 case DVBV3_OFDM: 1264 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__); 1265 switch (c->bandwidth_hz) { 1266 case 10000000: 1267 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ; 1268 break; 1269 case 8000000: 1270 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ; 1271 break; 1272 case 7000000: 1273 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ; 1274 break; 1275 case 6000000: 1276 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ; 1277 break; 1278 case 5000000: 1279 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ; 1280 break; 1281 case 1712000: 1282 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ; 1283 break; 1284 case 0: 1285 default: 1286 p->u.ofdm.bandwidth = BANDWIDTH_AUTO; 1287 } 1288 p->u.ofdm.code_rate_HP = c->code_rate_HP; 1289 p->u.ofdm.code_rate_LP = c->code_rate_LP; 1290 p->u.ofdm.constellation = c->modulation; 1291 p->u.ofdm.transmission_mode = c->transmission_mode; 1292 p->u.ofdm.guard_interval = c->guard_interval; 1293 p->u.ofdm.hierarchy_information = c->hierarchy; 1294 break; 1295 case DVBV3_ATSC: 1296 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__); 1297 p->u.vsb.modulation = c->modulation; 1298 break; 1299 } 1300 return 0; 1301 } 1302 1303 /** 1304 * dtv_get_frontend - calls a callback for retrieving DTV parameters 1305 * @fe: struct dvb_frontend pointer 1306 * @c: struct dtv_frontend_properties pointer (DVBv5 cache) 1307 * @p_out: struct dvb_frontend_parameters pointer (DVBv3 FE struct) 1308 * 1309 * This routine calls either the DVBv3 or DVBv5 get_frontend call. 1310 * If c is not null, it will update the DVBv5 cache struct pointed by it. 1311 * If p_out is not null, it will update the DVBv3 params pointed by it. 1312 */ 1313 static int dtv_get_frontend(struct dvb_frontend *fe, 1314 struct dtv_frontend_properties *c, 1315 struct dvb_frontend_parameters *p_out) 1316 { 1317 int r; 1318 1319 if (fe->ops.get_frontend) { 1320 r = fe->ops.get_frontend(fe, c); 1321 if (unlikely(r < 0)) 1322 return r; 1323 if (p_out) 1324 dtv_property_legacy_params_sync(fe, c, p_out); 1325 return 0; 1326 } 1327 1328 /* As everything is in cache, get_frontend fops are always supported */ 1329 return 0; 1330 } 1331 1332 static int dvb_frontend_handle_ioctl(struct file *file, 1333 unsigned int cmd, void *parg); 1334 1335 static int dtv_property_process_get(struct dvb_frontend *fe, 1336 const struct dtv_frontend_properties *c, 1337 struct dtv_property *tvp, 1338 struct file *file) 1339 { 1340 int ncaps; 1341 1342 switch (tvp->cmd) { 1343 case DTV_ENUM_DELSYS: 1344 ncaps = 0; 1345 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1346 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps]; 1347 ncaps++; 1348 } 1349 tvp->u.buffer.len = ncaps; 1350 break; 1351 case DTV_FREQUENCY: 1352 tvp->u.data = c->frequency; 1353 break; 1354 case DTV_MODULATION: 1355 tvp->u.data = c->modulation; 1356 break; 1357 case DTV_BANDWIDTH_HZ: 1358 tvp->u.data = c->bandwidth_hz; 1359 break; 1360 case DTV_INVERSION: 1361 tvp->u.data = c->inversion; 1362 break; 1363 case DTV_SYMBOL_RATE: 1364 tvp->u.data = c->symbol_rate; 1365 break; 1366 case DTV_INNER_FEC: 1367 tvp->u.data = c->fec_inner; 1368 break; 1369 case DTV_PILOT: 1370 tvp->u.data = c->pilot; 1371 break; 1372 case DTV_ROLLOFF: 1373 tvp->u.data = c->rolloff; 1374 break; 1375 case DTV_DELIVERY_SYSTEM: 1376 tvp->u.data = c->delivery_system; 1377 break; 1378 case DTV_VOLTAGE: 1379 tvp->u.data = c->voltage; 1380 break; 1381 case DTV_TONE: 1382 tvp->u.data = c->sectone; 1383 break; 1384 case DTV_API_VERSION: 1385 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR; 1386 break; 1387 case DTV_CODE_RATE_HP: 1388 tvp->u.data = c->code_rate_HP; 1389 break; 1390 case DTV_CODE_RATE_LP: 1391 tvp->u.data = c->code_rate_LP; 1392 break; 1393 case DTV_GUARD_INTERVAL: 1394 tvp->u.data = c->guard_interval; 1395 break; 1396 case DTV_TRANSMISSION_MODE: 1397 tvp->u.data = c->transmission_mode; 1398 break; 1399 case DTV_HIERARCHY: 1400 tvp->u.data = c->hierarchy; 1401 break; 1402 case DTV_INTERLEAVING: 1403 tvp->u.data = c->interleaving; 1404 break; 1405 1406 /* ISDB-T Support here */ 1407 case DTV_ISDBT_PARTIAL_RECEPTION: 1408 tvp->u.data = c->isdbt_partial_reception; 1409 break; 1410 case DTV_ISDBT_SOUND_BROADCASTING: 1411 tvp->u.data = c->isdbt_sb_mode; 1412 break; 1413 case DTV_ISDBT_SB_SUBCHANNEL_ID: 1414 tvp->u.data = c->isdbt_sb_subchannel; 1415 break; 1416 case DTV_ISDBT_SB_SEGMENT_IDX: 1417 tvp->u.data = c->isdbt_sb_segment_idx; 1418 break; 1419 case DTV_ISDBT_SB_SEGMENT_COUNT: 1420 tvp->u.data = c->isdbt_sb_segment_count; 1421 break; 1422 case DTV_ISDBT_LAYER_ENABLED: 1423 tvp->u.data = c->isdbt_layer_enabled; 1424 break; 1425 case DTV_ISDBT_LAYERA_FEC: 1426 tvp->u.data = c->layer[0].fec; 1427 break; 1428 case DTV_ISDBT_LAYERA_MODULATION: 1429 tvp->u.data = c->layer[0].modulation; 1430 break; 1431 case DTV_ISDBT_LAYERA_SEGMENT_COUNT: 1432 tvp->u.data = c->layer[0].segment_count; 1433 break; 1434 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING: 1435 tvp->u.data = c->layer[0].interleaving; 1436 break; 1437 case DTV_ISDBT_LAYERB_FEC: 1438 tvp->u.data = c->layer[1].fec; 1439 break; 1440 case DTV_ISDBT_LAYERB_MODULATION: 1441 tvp->u.data = c->layer[1].modulation; 1442 break; 1443 case DTV_ISDBT_LAYERB_SEGMENT_COUNT: 1444 tvp->u.data = c->layer[1].segment_count; 1445 break; 1446 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING: 1447 tvp->u.data = c->layer[1].interleaving; 1448 break; 1449 case DTV_ISDBT_LAYERC_FEC: 1450 tvp->u.data = c->layer[2].fec; 1451 break; 1452 case DTV_ISDBT_LAYERC_MODULATION: 1453 tvp->u.data = c->layer[2].modulation; 1454 break; 1455 case DTV_ISDBT_LAYERC_SEGMENT_COUNT: 1456 tvp->u.data = c->layer[2].segment_count; 1457 break; 1458 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING: 1459 tvp->u.data = c->layer[2].interleaving; 1460 break; 1461 1462 /* Multistream support */ 1463 case DTV_STREAM_ID: 1464 case DTV_DVBT2_PLP_ID_LEGACY: 1465 tvp->u.data = c->stream_id; 1466 break; 1467 1468 /* Physical layer scrambling support */ 1469 case DTV_SCRAMBLING_SEQUENCE_INDEX: 1470 tvp->u.data = c->scrambling_sequence_index; 1471 break; 1472 1473 /* ATSC-MH */ 1474 case DTV_ATSCMH_FIC_VER: 1475 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver; 1476 break; 1477 case DTV_ATSCMH_PARADE_ID: 1478 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id; 1479 break; 1480 case DTV_ATSCMH_NOG: 1481 tvp->u.data = fe->dtv_property_cache.atscmh_nog; 1482 break; 1483 case DTV_ATSCMH_TNOG: 1484 tvp->u.data = fe->dtv_property_cache.atscmh_tnog; 1485 break; 1486 case DTV_ATSCMH_SGN: 1487 tvp->u.data = fe->dtv_property_cache.atscmh_sgn; 1488 break; 1489 case DTV_ATSCMH_PRC: 1490 tvp->u.data = fe->dtv_property_cache.atscmh_prc; 1491 break; 1492 case DTV_ATSCMH_RS_FRAME_MODE: 1493 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode; 1494 break; 1495 case DTV_ATSCMH_RS_FRAME_ENSEMBLE: 1496 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble; 1497 break; 1498 case DTV_ATSCMH_RS_CODE_MODE_PRI: 1499 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri; 1500 break; 1501 case DTV_ATSCMH_RS_CODE_MODE_SEC: 1502 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec; 1503 break; 1504 case DTV_ATSCMH_SCCC_BLOCK_MODE: 1505 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode; 1506 break; 1507 case DTV_ATSCMH_SCCC_CODE_MODE_A: 1508 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a; 1509 break; 1510 case DTV_ATSCMH_SCCC_CODE_MODE_B: 1511 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b; 1512 break; 1513 case DTV_ATSCMH_SCCC_CODE_MODE_C: 1514 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c; 1515 break; 1516 case DTV_ATSCMH_SCCC_CODE_MODE_D: 1517 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d; 1518 break; 1519 1520 case DTV_LNA: 1521 tvp->u.data = c->lna; 1522 break; 1523 1524 /* Fill quality measures */ 1525 case DTV_STAT_SIGNAL_STRENGTH: 1526 tvp->u.st = c->strength; 1527 break; 1528 case DTV_STAT_CNR: 1529 tvp->u.st = c->cnr; 1530 break; 1531 case DTV_STAT_PRE_ERROR_BIT_COUNT: 1532 tvp->u.st = c->pre_bit_error; 1533 break; 1534 case DTV_STAT_PRE_TOTAL_BIT_COUNT: 1535 tvp->u.st = c->pre_bit_count; 1536 break; 1537 case DTV_STAT_POST_ERROR_BIT_COUNT: 1538 tvp->u.st = c->post_bit_error; 1539 break; 1540 case DTV_STAT_POST_TOTAL_BIT_COUNT: 1541 tvp->u.st = c->post_bit_count; 1542 break; 1543 case DTV_STAT_ERROR_BLOCK_COUNT: 1544 tvp->u.st = c->block_error; 1545 break; 1546 case DTV_STAT_TOTAL_BLOCK_COUNT: 1547 tvp->u.st = c->block_count; 1548 break; 1549 default: 1550 dev_dbg(fe->dvb->device, 1551 "%s: FE property %d doesn't exist\n", 1552 __func__, tvp->cmd); 1553 return -EINVAL; 1554 } 1555 1556 if (!dtv_cmds[tvp->cmd].buffer) 1557 dev_dbg(fe->dvb->device, 1558 "%s: GET cmd 0x%08x (%s) = 0x%08x\n", 1559 __func__, tvp->cmd, dtv_cmds[tvp->cmd].name, 1560 tvp->u.data); 1561 else 1562 dev_dbg(fe->dvb->device, 1563 "%s: GET cmd 0x%08x (%s) len %d: %*ph\n", 1564 __func__, 1565 tvp->cmd, dtv_cmds[tvp->cmd].name, 1566 tvp->u.buffer.len, 1567 tvp->u.buffer.len, tvp->u.buffer.data); 1568 1569 return 0; 1570 } 1571 1572 static int dtv_set_frontend(struct dvb_frontend *fe); 1573 1574 static bool is_dvbv3_delsys(u32 delsys) 1575 { 1576 return (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) || 1577 (delsys == SYS_DVBS) || (delsys == SYS_ATSC); 1578 } 1579 1580 /** 1581 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type 1582 * @fe: struct frontend; 1583 * @delsys: DVBv5 type that will be used for emulation 1584 * 1585 * Provides emulation for delivery systems that are compatible with the old 1586 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows 1587 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontend 1588 * parameters are compatible with DVB-S spec. 1589 */ 1590 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys) 1591 { 1592 int i; 1593 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1594 1595 c->delivery_system = delsys; 1596 1597 /* 1598 * If the call is for ISDB-T, put it into full-seg, auto mode, TV 1599 */ 1600 if (c->delivery_system == SYS_ISDBT) { 1601 dev_dbg(fe->dvb->device, 1602 "%s: Using defaults for SYS_ISDBT\n", 1603 __func__); 1604 1605 if (!c->bandwidth_hz) 1606 c->bandwidth_hz = 6000000; 1607 1608 c->isdbt_partial_reception = 0; 1609 c->isdbt_sb_mode = 0; 1610 c->isdbt_sb_subchannel = 0; 1611 c->isdbt_sb_segment_idx = 0; 1612 c->isdbt_sb_segment_count = 0; 1613 c->isdbt_layer_enabled = 7; 1614 for (i = 0; i < 3; i++) { 1615 c->layer[i].fec = FEC_AUTO; 1616 c->layer[i].modulation = QAM_AUTO; 1617 c->layer[i].interleaving = 0; 1618 c->layer[i].segment_count = 0; 1619 } 1620 } 1621 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n", 1622 __func__, c->delivery_system); 1623 1624 return 0; 1625 } 1626 1627 /** 1628 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call 1629 * @fe: frontend struct 1630 * @desired_system: delivery system requested by the user 1631 * 1632 * A DVBv5 call know what's the desired system it wants. So, set it. 1633 * 1634 * There are, however, a few known issues with early DVBv5 applications that 1635 * are also handled by this logic: 1636 * 1637 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system. 1638 * This is an API violation, but, as we don't want to break userspace, 1639 * convert it to the first supported delivery system. 1640 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for 1641 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of 1642 * ISDB-T provided backward compat with DVB-T. 1643 */ 1644 static int dvbv5_set_delivery_system(struct dvb_frontend *fe, 1645 u32 desired_system) 1646 { 1647 int ncaps; 1648 u32 delsys = SYS_UNDEFINED; 1649 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1650 enum dvbv3_emulation_type type; 1651 1652 /* 1653 * It was reported that some old DVBv5 applications were 1654 * filling delivery_system with SYS_UNDEFINED. If this happens, 1655 * assume that the application wants to use the first supported 1656 * delivery system. 1657 */ 1658 if (desired_system == SYS_UNDEFINED) 1659 desired_system = fe->ops.delsys[0]; 1660 1661 /* 1662 * This is a DVBv5 call. So, it likely knows the supported 1663 * delivery systems. So, check if the desired delivery system is 1664 * supported 1665 */ 1666 ncaps = 0; 1667 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1668 if (fe->ops.delsys[ncaps] == desired_system) { 1669 c->delivery_system = desired_system; 1670 dev_dbg(fe->dvb->device, 1671 "%s: Changing delivery system to %d\n", 1672 __func__, desired_system); 1673 return 0; 1674 } 1675 ncaps++; 1676 } 1677 1678 /* 1679 * The requested delivery system isn't supported. Maybe userspace 1680 * is requesting a DVBv3 compatible delivery system. 1681 * 1682 * The emulation only works if the desired system is one of the 1683 * delivery systems supported by DVBv3 API 1684 */ 1685 if (!is_dvbv3_delsys(desired_system)) { 1686 dev_dbg(fe->dvb->device, 1687 "%s: Delivery system %d not supported.\n", 1688 __func__, desired_system); 1689 return -EINVAL; 1690 } 1691 1692 type = dvbv3_type(desired_system); 1693 1694 /* 1695 * Get the last non-DVBv3 delivery system that has the same type 1696 * of the desired system 1697 */ 1698 ncaps = 0; 1699 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1700 if (dvbv3_type(fe->ops.delsys[ncaps]) == type) 1701 delsys = fe->ops.delsys[ncaps]; 1702 ncaps++; 1703 } 1704 1705 /* There's nothing compatible with the desired delivery system */ 1706 if (delsys == SYS_UNDEFINED) { 1707 dev_dbg(fe->dvb->device, 1708 "%s: Delivery system %d not supported on emulation mode.\n", 1709 __func__, desired_system); 1710 return -EINVAL; 1711 } 1712 1713 dev_dbg(fe->dvb->device, 1714 "%s: Using delivery system %d emulated as if it were %d\n", 1715 __func__, delsys, desired_system); 1716 1717 return emulate_delivery_system(fe, desired_system); 1718 } 1719 1720 /** 1721 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call 1722 * @fe: frontend struct 1723 * 1724 * A DVBv3 call doesn't know what's the desired system it wants. It also 1725 * doesn't allow to switch between different types. Due to that, userspace 1726 * should use DVBv5 instead. 1727 * However, in order to avoid breaking userspace API, limited backward 1728 * compatibility support is provided. 1729 * 1730 * There are some delivery systems that are incompatible with DVBv3 calls. 1731 * 1732 * This routine should work fine for frontends that support just one delivery 1733 * system. 1734 * 1735 * For frontends that support multiple frontends: 1736 * 1) It defaults to use the first supported delivery system. There's an 1737 * userspace application that allows changing it at runtime; 1738 * 1739 * 2) If the current delivery system is not compatible with DVBv3, it gets 1740 * the first one that it is compatible. 1741 * 1742 * NOTE: in order for this to work with applications like Kaffeine that 1743 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to 1744 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the 1745 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back 1746 * to DVB-S. 1747 */ 1748 static int dvbv3_set_delivery_system(struct dvb_frontend *fe) 1749 { 1750 int ncaps; 1751 u32 delsys = SYS_UNDEFINED; 1752 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1753 1754 /* If not set yet, defaults to the first supported delivery system */ 1755 if (c->delivery_system == SYS_UNDEFINED) 1756 c->delivery_system = fe->ops.delsys[0]; 1757 1758 /* 1759 * Trivial case: just use the current one, if it already a DVBv3 1760 * delivery system 1761 */ 1762 if (is_dvbv3_delsys(c->delivery_system)) { 1763 dev_dbg(fe->dvb->device, 1764 "%s: Using delivery system to %d\n", 1765 __func__, c->delivery_system); 1766 return 0; 1767 } 1768 1769 /* 1770 * Seek for the first delivery system that it is compatible with a 1771 * DVBv3 standard 1772 */ 1773 ncaps = 0; 1774 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1775 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) { 1776 delsys = fe->ops.delsys[ncaps]; 1777 break; 1778 } 1779 ncaps++; 1780 } 1781 if (delsys == SYS_UNDEFINED) { 1782 dev_dbg(fe->dvb->device, 1783 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n", 1784 __func__); 1785 return -EINVAL; 1786 } 1787 return emulate_delivery_system(fe, delsys); 1788 } 1789 1790 /** 1791 * dtv_property_process_set - Sets a single DTV property 1792 * @fe: Pointer to &struct dvb_frontend 1793 * @file: Pointer to &struct file 1794 * @cmd: Digital TV command 1795 * @data: An unsigned 32-bits number 1796 * 1797 * This routine assigns the property 1798 * value to the corresponding member of 1799 * &struct dtv_frontend_properties 1800 * 1801 * Returns: 1802 * Zero on success, negative errno on failure. 1803 */ 1804 static int dtv_property_process_set(struct dvb_frontend *fe, 1805 struct file *file, 1806 u32 cmd, u32 data) 1807 { 1808 int r = 0; 1809 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1810 1811 /** Dump DTV command name and value*/ 1812 if (!cmd || cmd > DTV_MAX_COMMAND) 1813 dev_warn(fe->dvb->device, "%s: SET cmd 0x%08x undefined\n", 1814 __func__, cmd); 1815 else 1816 dev_dbg(fe->dvb->device, 1817 "%s: SET cmd 0x%08x (%s) to 0x%08x\n", 1818 __func__, cmd, dtv_cmds[cmd].name, data); 1819 switch (cmd) { 1820 case DTV_CLEAR: 1821 /* 1822 * Reset a cache of data specific to the frontend here. This does 1823 * not effect hardware. 1824 */ 1825 dvb_frontend_clear_cache(fe); 1826 break; 1827 case DTV_TUNE: 1828 /* 1829 * Use the cached Digital TV properties to tune the 1830 * frontend 1831 */ 1832 dev_dbg(fe->dvb->device, 1833 "%s: Setting the frontend from property cache\n", 1834 __func__); 1835 1836 r = dtv_set_frontend(fe); 1837 break; 1838 case DTV_FREQUENCY: 1839 c->frequency = data; 1840 break; 1841 case DTV_MODULATION: 1842 c->modulation = data; 1843 break; 1844 case DTV_BANDWIDTH_HZ: 1845 c->bandwidth_hz = data; 1846 break; 1847 case DTV_INVERSION: 1848 c->inversion = data; 1849 break; 1850 case DTV_SYMBOL_RATE: 1851 c->symbol_rate = data; 1852 break; 1853 case DTV_INNER_FEC: 1854 c->fec_inner = data; 1855 break; 1856 case DTV_PILOT: 1857 c->pilot = data; 1858 break; 1859 case DTV_ROLLOFF: 1860 c->rolloff = data; 1861 break; 1862 case DTV_DELIVERY_SYSTEM: 1863 r = dvbv5_set_delivery_system(fe, data); 1864 break; 1865 case DTV_VOLTAGE: 1866 c->voltage = data; 1867 r = dvb_frontend_handle_ioctl(file, FE_SET_VOLTAGE, 1868 (void *)c->voltage); 1869 break; 1870 case DTV_TONE: 1871 c->sectone = data; 1872 r = dvb_frontend_handle_ioctl(file, FE_SET_TONE, 1873 (void *)c->sectone); 1874 break; 1875 case DTV_CODE_RATE_HP: 1876 c->code_rate_HP = data; 1877 break; 1878 case DTV_CODE_RATE_LP: 1879 c->code_rate_LP = data; 1880 break; 1881 case DTV_GUARD_INTERVAL: 1882 c->guard_interval = data; 1883 break; 1884 case DTV_TRANSMISSION_MODE: 1885 c->transmission_mode = data; 1886 break; 1887 case DTV_HIERARCHY: 1888 c->hierarchy = data; 1889 break; 1890 case DTV_INTERLEAVING: 1891 c->interleaving = data; 1892 break; 1893 1894 /* ISDB-T Support here */ 1895 case DTV_ISDBT_PARTIAL_RECEPTION: 1896 c->isdbt_partial_reception = data; 1897 break; 1898 case DTV_ISDBT_SOUND_BROADCASTING: 1899 c->isdbt_sb_mode = data; 1900 break; 1901 case DTV_ISDBT_SB_SUBCHANNEL_ID: 1902 c->isdbt_sb_subchannel = data; 1903 break; 1904 case DTV_ISDBT_SB_SEGMENT_IDX: 1905 c->isdbt_sb_segment_idx = data; 1906 break; 1907 case DTV_ISDBT_SB_SEGMENT_COUNT: 1908 c->isdbt_sb_segment_count = data; 1909 break; 1910 case DTV_ISDBT_LAYER_ENABLED: 1911 c->isdbt_layer_enabled = data; 1912 break; 1913 case DTV_ISDBT_LAYERA_FEC: 1914 c->layer[0].fec = data; 1915 break; 1916 case DTV_ISDBT_LAYERA_MODULATION: 1917 c->layer[0].modulation = data; 1918 break; 1919 case DTV_ISDBT_LAYERA_SEGMENT_COUNT: 1920 c->layer[0].segment_count = data; 1921 break; 1922 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING: 1923 c->layer[0].interleaving = data; 1924 break; 1925 case DTV_ISDBT_LAYERB_FEC: 1926 c->layer[1].fec = data; 1927 break; 1928 case DTV_ISDBT_LAYERB_MODULATION: 1929 c->layer[1].modulation = data; 1930 break; 1931 case DTV_ISDBT_LAYERB_SEGMENT_COUNT: 1932 c->layer[1].segment_count = data; 1933 break; 1934 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING: 1935 c->layer[1].interleaving = data; 1936 break; 1937 case DTV_ISDBT_LAYERC_FEC: 1938 c->layer[2].fec = data; 1939 break; 1940 case DTV_ISDBT_LAYERC_MODULATION: 1941 c->layer[2].modulation = data; 1942 break; 1943 case DTV_ISDBT_LAYERC_SEGMENT_COUNT: 1944 c->layer[2].segment_count = data; 1945 break; 1946 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING: 1947 c->layer[2].interleaving = data; 1948 break; 1949 1950 /* Multistream support */ 1951 case DTV_STREAM_ID: 1952 case DTV_DVBT2_PLP_ID_LEGACY: 1953 c->stream_id = data; 1954 break; 1955 1956 /* Physical layer scrambling support */ 1957 case DTV_SCRAMBLING_SEQUENCE_INDEX: 1958 c->scrambling_sequence_index = data; 1959 break; 1960 1961 /* ATSC-MH */ 1962 case DTV_ATSCMH_PARADE_ID: 1963 fe->dtv_property_cache.atscmh_parade_id = data; 1964 break; 1965 case DTV_ATSCMH_RS_FRAME_ENSEMBLE: 1966 fe->dtv_property_cache.atscmh_rs_frame_ensemble = data; 1967 break; 1968 1969 case DTV_LNA: 1970 c->lna = data; 1971 if (fe->ops.set_lna) 1972 r = fe->ops.set_lna(fe); 1973 if (r < 0) 1974 c->lna = LNA_AUTO; 1975 break; 1976 1977 default: 1978 return -EINVAL; 1979 } 1980 1981 return r; 1982 } 1983 1984 static int dvb_frontend_do_ioctl(struct file *file, unsigned int cmd, 1985 void *parg) 1986 { 1987 struct dvb_device *dvbdev = file->private_data; 1988 struct dvb_frontend *fe = dvbdev->priv; 1989 struct dvb_frontend_private *fepriv = fe->frontend_priv; 1990 int err; 1991 1992 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd)); 1993 if (down_interruptible(&fepriv->sem)) 1994 return -ERESTARTSYS; 1995 1996 if (fe->exit != DVB_FE_NO_EXIT) { 1997 up(&fepriv->sem); 1998 return -ENODEV; 1999 } 2000 2001 /* 2002 * If the frontend is opened in read-only mode, only the ioctls 2003 * that don't interfere with the tune logic should be accepted. 2004 * That allows an external application to monitor the DVB QoS and 2005 * statistics parameters. 2006 * 2007 * That matches all _IOR() ioctls, except for two special cases: 2008 * - FE_GET_EVENT is part of the tuning logic on a DVB application; 2009 * - FE_DISEQC_RECV_SLAVE_REPLY is part of DiSEqC 2.0 2010 * setup 2011 * So, those two ioctls should also return -EPERM, as otherwise 2012 * reading from them would interfere with a DVB tune application 2013 */ 2014 if ((file->f_flags & O_ACCMODE) == O_RDONLY 2015 && (_IOC_DIR(cmd) != _IOC_READ 2016 || cmd == FE_GET_EVENT 2017 || cmd == FE_DISEQC_RECV_SLAVE_REPLY)) { 2018 up(&fepriv->sem); 2019 return -EPERM; 2020 } 2021 2022 err = dvb_frontend_handle_ioctl(file, cmd, parg); 2023 2024 up(&fepriv->sem); 2025 return err; 2026 } 2027 2028 static long dvb_frontend_ioctl(struct file *file, unsigned int cmd, 2029 unsigned long arg) 2030 { 2031 struct dvb_device *dvbdev = file->private_data; 2032 2033 if (!dvbdev) 2034 return -ENODEV; 2035 2036 return dvb_usercopy(file, cmd, arg, dvb_frontend_do_ioctl); 2037 } 2038 2039 #ifdef CONFIG_COMPAT 2040 struct compat_dtv_property { 2041 __u32 cmd; 2042 __u32 reserved[3]; 2043 union { 2044 __u32 data; 2045 struct dtv_fe_stats st; 2046 struct { 2047 __u8 data[32]; 2048 __u32 len; 2049 __u32 reserved1[3]; 2050 compat_uptr_t reserved2; 2051 } buffer; 2052 } u; 2053 int result; 2054 } __attribute__ ((packed)); 2055 2056 struct compat_dtv_properties { 2057 __u32 num; 2058 compat_uptr_t props; 2059 }; 2060 2061 #define COMPAT_FE_SET_PROPERTY _IOW('o', 82, struct compat_dtv_properties) 2062 #define COMPAT_FE_GET_PROPERTY _IOR('o', 83, struct compat_dtv_properties) 2063 2064 static int dvb_frontend_handle_compat_ioctl(struct file *file, unsigned int cmd, 2065 unsigned long arg) 2066 { 2067 struct dvb_device *dvbdev = file->private_data; 2068 struct dvb_frontend *fe = dvbdev->priv; 2069 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2070 int i, err = 0; 2071 2072 if (cmd == COMPAT_FE_SET_PROPERTY) { 2073 struct compat_dtv_properties prop, *tvps = NULL; 2074 struct compat_dtv_property *tvp = NULL; 2075 2076 if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop))) 2077 return -EFAULT; 2078 2079 tvps = ∝ 2080 2081 /* 2082 * Put an arbitrary limit on the number of messages that can 2083 * be sent at once 2084 */ 2085 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS)) 2086 return -EINVAL; 2087 2088 tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp)); 2089 if (IS_ERR(tvp)) 2090 return PTR_ERR(tvp); 2091 2092 for (i = 0; i < tvps->num; i++) { 2093 err = dtv_property_process_set(fe, file, 2094 (tvp + i)->cmd, 2095 (tvp + i)->u.data); 2096 if (err < 0) { 2097 kfree(tvp); 2098 return err; 2099 } 2100 } 2101 kfree(tvp); 2102 } else if (cmd == COMPAT_FE_GET_PROPERTY) { 2103 struct compat_dtv_properties prop, *tvps = NULL; 2104 struct compat_dtv_property *tvp = NULL; 2105 struct dtv_frontend_properties getp = fe->dtv_property_cache; 2106 2107 if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop))) 2108 return -EFAULT; 2109 2110 tvps = ∝ 2111 2112 /* 2113 * Put an arbitrary limit on the number of messages that can 2114 * be sent at once 2115 */ 2116 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS)) 2117 return -EINVAL; 2118 2119 tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp)); 2120 if (IS_ERR(tvp)) 2121 return PTR_ERR(tvp); 2122 2123 /* 2124 * Let's use our own copy of property cache, in order to 2125 * avoid mangling with DTV zigzag logic, as drivers might 2126 * return crap, if they don't check if the data is available 2127 * before updating the properties cache. 2128 */ 2129 if (fepriv->state != FESTATE_IDLE) { 2130 err = dtv_get_frontend(fe, &getp, NULL); 2131 if (err < 0) { 2132 kfree(tvp); 2133 return err; 2134 } 2135 } 2136 for (i = 0; i < tvps->num; i++) { 2137 err = dtv_property_process_get( 2138 fe, &getp, (struct dtv_property *)(tvp + i), file); 2139 if (err < 0) { 2140 kfree(tvp); 2141 return err; 2142 } 2143 } 2144 2145 if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp, 2146 tvps->num * sizeof(struct compat_dtv_property))) { 2147 kfree(tvp); 2148 return -EFAULT; 2149 } 2150 kfree(tvp); 2151 } 2152 2153 return err; 2154 } 2155 2156 static long dvb_frontend_compat_ioctl(struct file *file, unsigned int cmd, 2157 unsigned long arg) 2158 { 2159 struct dvb_device *dvbdev = file->private_data; 2160 struct dvb_frontend *fe = dvbdev->priv; 2161 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2162 int err; 2163 2164 if (cmd == COMPAT_FE_SET_PROPERTY || cmd == COMPAT_FE_GET_PROPERTY) { 2165 if (down_interruptible(&fepriv->sem)) 2166 return -ERESTARTSYS; 2167 2168 err = dvb_frontend_handle_compat_ioctl(file, cmd, arg); 2169 2170 up(&fepriv->sem); 2171 return err; 2172 } 2173 2174 return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2175 } 2176 #endif 2177 2178 static int dtv_set_frontend(struct dvb_frontend *fe) 2179 { 2180 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2181 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 2182 struct dvb_frontend_tune_settings fetunesettings; 2183 u32 rolloff = 0; 2184 2185 if (dvb_frontend_check_parameters(fe) < 0) 2186 return -EINVAL; 2187 2188 /* 2189 * Initialize output parameters to match the values given by 2190 * the user. FE_SET_FRONTEND triggers an initial frontend event 2191 * with status = 0, which copies output parameters to userspace. 2192 */ 2193 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out); 2194 2195 /* 2196 * Be sure that the bandwidth will be filled for all 2197 * non-satellite systems, as tuners need to know what 2198 * low pass/Nyquist half filter should be applied, in 2199 * order to avoid inter-channel noise. 2200 * 2201 * ISDB-T and DVB-T/T2 already sets bandwidth. 2202 * ATSC and DVB-C don't set, so, the core should fill it. 2203 * 2204 * On DVB-C Annex A and C, the bandwidth is a function of 2205 * the roll-off and symbol rate. Annex B defines different 2206 * roll-off factors depending on the modulation. Fortunately, 2207 * Annex B is only used with 6MHz, so there's no need to 2208 * calculate it. 2209 * 2210 * While not officially supported, a side effect of handling it at 2211 * the cache level is that a program could retrieve the bandwidth 2212 * via DTV_BANDWIDTH_HZ, which may be useful for test programs. 2213 */ 2214 switch (c->delivery_system) { 2215 case SYS_ATSC: 2216 case SYS_DVBC_ANNEX_B: 2217 c->bandwidth_hz = 6000000; 2218 break; 2219 case SYS_DVBC_ANNEX_A: 2220 rolloff = 115; 2221 break; 2222 case SYS_DVBC_ANNEX_C: 2223 rolloff = 113; 2224 break; 2225 case SYS_DVBS: 2226 case SYS_TURBO: 2227 case SYS_ISDBS: 2228 rolloff = 135; 2229 break; 2230 case SYS_DVBS2: 2231 switch (c->rolloff) { 2232 case ROLLOFF_20: 2233 rolloff = 120; 2234 break; 2235 case ROLLOFF_25: 2236 rolloff = 125; 2237 break; 2238 default: 2239 case ROLLOFF_35: 2240 rolloff = 135; 2241 } 2242 break; 2243 default: 2244 break; 2245 } 2246 if (rolloff) 2247 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100); 2248 2249 /* force auto frequency inversion if requested */ 2250 if (dvb_force_auto_inversion) 2251 c->inversion = INVERSION_AUTO; 2252 2253 /* 2254 * without hierarchical coding code_rate_LP is irrelevant, 2255 * so we tolerate the otherwise invalid FEC_NONE setting 2256 */ 2257 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE) 2258 c->code_rate_LP = FEC_AUTO; 2259 2260 /* get frontend-specific tuning settings */ 2261 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings)); 2262 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) { 2263 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000; 2264 fepriv->max_drift = fetunesettings.max_drift; 2265 fepriv->step_size = fetunesettings.step_size; 2266 } else { 2267 /* default values */ 2268 switch (c->delivery_system) { 2269 case SYS_DVBS: 2270 case SYS_DVBS2: 2271 case SYS_ISDBS: 2272 case SYS_TURBO: 2273 case SYS_DVBC_ANNEX_A: 2274 case SYS_DVBC_ANNEX_C: 2275 fepriv->min_delay = HZ / 20; 2276 fepriv->step_size = c->symbol_rate / 16000; 2277 fepriv->max_drift = c->symbol_rate / 2000; 2278 break; 2279 case SYS_DVBT: 2280 case SYS_DVBT2: 2281 case SYS_ISDBT: 2282 case SYS_DTMB: 2283 fepriv->min_delay = HZ / 20; 2284 fepriv->step_size = dvb_frontend_get_stepsize(fe) * 2; 2285 fepriv->max_drift = (dvb_frontend_get_stepsize(fe) * 2) + 1; 2286 break; 2287 default: 2288 /* 2289 * FIXME: This sounds wrong! if freqency_stepsize is 2290 * defined by the frontend, why not use it??? 2291 */ 2292 fepriv->min_delay = HZ / 20; 2293 fepriv->step_size = 0; /* no zigzag */ 2294 fepriv->max_drift = 0; 2295 break; 2296 } 2297 } 2298 if (dvb_override_tune_delay > 0) 2299 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000; 2300 2301 fepriv->state = FESTATE_RETUNE; 2302 2303 /* Request the search algorithm to search */ 2304 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN; 2305 2306 dvb_frontend_clear_events(fe); 2307 dvb_frontend_add_event(fe, 0); 2308 dvb_frontend_wakeup(fe); 2309 fepriv->status = 0; 2310 2311 return 0; 2312 } 2313 2314 static int dvb_get_property(struct dvb_frontend *fe, struct file *file, 2315 struct dtv_properties *tvps) 2316 { 2317 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2318 struct dtv_property *tvp = NULL; 2319 struct dtv_frontend_properties getp; 2320 int i, err; 2321 2322 memcpy(&getp, &fe->dtv_property_cache, sizeof(getp)); 2323 2324 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", 2325 __func__, tvps->num); 2326 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", 2327 __func__, tvps->props); 2328 2329 /* 2330 * Put an arbitrary limit on the number of messages that can 2331 * be sent at once 2332 */ 2333 if (!tvps->num || tvps->num > DTV_IOCTL_MAX_MSGS) 2334 return -EINVAL; 2335 2336 tvp = memdup_user((void __user *)tvps->props, tvps->num * sizeof(*tvp)); 2337 if (IS_ERR(tvp)) 2338 return PTR_ERR(tvp); 2339 2340 /* 2341 * Let's use our own copy of property cache, in order to 2342 * avoid mangling with DTV zigzag logic, as drivers might 2343 * return crap, if they don't check if the data is available 2344 * before updating the properties cache. 2345 */ 2346 if (fepriv->state != FESTATE_IDLE) { 2347 err = dtv_get_frontend(fe, &getp, NULL); 2348 if (err < 0) 2349 goto out; 2350 } 2351 for (i = 0; i < tvps->num; i++) { 2352 err = dtv_property_process_get(fe, &getp, 2353 tvp + i, file); 2354 if (err < 0) 2355 goto out; 2356 } 2357 2358 if (copy_to_user((void __user *)tvps->props, tvp, 2359 tvps->num * sizeof(struct dtv_property))) { 2360 err = -EFAULT; 2361 goto out; 2362 } 2363 2364 err = 0; 2365 out: 2366 kfree(tvp); 2367 return err; 2368 } 2369 2370 static int dvb_get_frontend(struct dvb_frontend *fe, 2371 struct dvb_frontend_parameters *p_out) 2372 { 2373 struct dtv_frontend_properties getp; 2374 2375 /* 2376 * Let's use our own copy of property cache, in order to 2377 * avoid mangling with DTV zigzag logic, as drivers might 2378 * return crap, if they don't check if the data is available 2379 * before updating the properties cache. 2380 */ 2381 memcpy(&getp, &fe->dtv_property_cache, sizeof(getp)); 2382 2383 return dtv_get_frontend(fe, &getp, p_out); 2384 } 2385 2386 static int dvb_frontend_handle_ioctl(struct file *file, 2387 unsigned int cmd, void *parg) 2388 { 2389 struct dvb_device *dvbdev = file->private_data; 2390 struct dvb_frontend *fe = dvbdev->priv; 2391 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2392 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 2393 int i, err = -ENOTSUPP; 2394 2395 dev_dbg(fe->dvb->device, "%s:\n", __func__); 2396 2397 switch (cmd) { 2398 case FE_SET_PROPERTY: { 2399 struct dtv_properties *tvps = parg; 2400 struct dtv_property *tvp = NULL; 2401 2402 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", 2403 __func__, tvps->num); 2404 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", 2405 __func__, tvps->props); 2406 2407 /* 2408 * Put an arbitrary limit on the number of messages that can 2409 * be sent at once 2410 */ 2411 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS)) 2412 return -EINVAL; 2413 2414 tvp = memdup_user((void __user *)tvps->props, tvps->num * sizeof(*tvp)); 2415 if (IS_ERR(tvp)) 2416 return PTR_ERR(tvp); 2417 2418 for (i = 0; i < tvps->num; i++) { 2419 err = dtv_property_process_set(fe, file, 2420 (tvp + i)->cmd, 2421 (tvp + i)->u.data); 2422 if (err < 0) { 2423 kfree(tvp); 2424 return err; 2425 } 2426 } 2427 kfree(tvp); 2428 err = 0; 2429 break; 2430 } 2431 case FE_GET_PROPERTY: 2432 err = dvb_get_property(fe, file, parg); 2433 break; 2434 2435 case FE_GET_INFO: { 2436 struct dvb_frontend_info *info = parg; 2437 memset(info, 0, sizeof(*info)); 2438 2439 strscpy(info->name, fe->ops.info.name, sizeof(info->name)); 2440 info->symbol_rate_min = fe->ops.info.symbol_rate_min; 2441 info->symbol_rate_max = fe->ops.info.symbol_rate_max; 2442 info->symbol_rate_tolerance = fe->ops.info.symbol_rate_tolerance; 2443 info->caps = fe->ops.info.caps; 2444 info->frequency_stepsize = dvb_frontend_get_stepsize(fe); 2445 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, 2446 &info->frequency_max, 2447 &info->frequency_tolerance); 2448 2449 /* 2450 * Associate the 4 delivery systems supported by DVBv3 2451 * API with their DVBv5 counterpart. For the other standards, 2452 * use the closest type, assuming that it would hopefully 2453 * work with a DVBv3 application. 2454 * It should be noticed that, on multi-frontend devices with 2455 * different types (terrestrial and cable, for example), 2456 * a pure DVBv3 application won't be able to use all delivery 2457 * systems. Yet, changing the DVBv5 cache to the other delivery 2458 * system should be enough for making it work. 2459 */ 2460 switch (dvbv3_type(c->delivery_system)) { 2461 case DVBV3_QPSK: 2462 info->type = FE_QPSK; 2463 break; 2464 case DVBV3_ATSC: 2465 info->type = FE_ATSC; 2466 break; 2467 case DVBV3_QAM: 2468 info->type = FE_QAM; 2469 break; 2470 case DVBV3_OFDM: 2471 info->type = FE_OFDM; 2472 break; 2473 default: 2474 dev_err(fe->dvb->device, 2475 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n", 2476 __func__, c->delivery_system); 2477 info->type = FE_OFDM; 2478 } 2479 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n", 2480 __func__, c->delivery_system, info->type); 2481 2482 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */ 2483 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) 2484 info->caps |= FE_CAN_INVERSION_AUTO; 2485 err = 0; 2486 break; 2487 } 2488 2489 case FE_READ_STATUS: { 2490 enum fe_status *status = parg; 2491 2492 /* if retune was requested but hasn't occurred yet, prevent 2493 * that user get signal state from previous tuning */ 2494 if (fepriv->state == FESTATE_RETUNE || 2495 fepriv->state == FESTATE_ERROR) { 2496 err = 0; 2497 *status = 0; 2498 break; 2499 } 2500 2501 if (fe->ops.read_status) 2502 err = fe->ops.read_status(fe, status); 2503 break; 2504 } 2505 2506 case FE_DISEQC_RESET_OVERLOAD: 2507 if (fe->ops.diseqc_reset_overload) { 2508 err = fe->ops.diseqc_reset_overload(fe); 2509 fepriv->state = FESTATE_DISEQC; 2510 fepriv->status = 0; 2511 } 2512 break; 2513 2514 case FE_DISEQC_SEND_MASTER_CMD: 2515 if (fe->ops.diseqc_send_master_cmd) { 2516 struct dvb_diseqc_master_cmd *cmd = parg; 2517 2518 if (cmd->msg_len > sizeof(cmd->msg)) { 2519 err = -EINVAL; 2520 break; 2521 } 2522 err = fe->ops.diseqc_send_master_cmd(fe, cmd); 2523 fepriv->state = FESTATE_DISEQC; 2524 fepriv->status = 0; 2525 } 2526 break; 2527 2528 case FE_DISEQC_SEND_BURST: 2529 if (fe->ops.diseqc_send_burst) { 2530 err = fe->ops.diseqc_send_burst(fe, 2531 (enum fe_sec_mini_cmd)parg); 2532 fepriv->state = FESTATE_DISEQC; 2533 fepriv->status = 0; 2534 } 2535 break; 2536 2537 case FE_SET_TONE: 2538 if (fe->ops.set_tone) { 2539 err = fe->ops.set_tone(fe, 2540 (enum fe_sec_tone_mode)parg); 2541 fepriv->tone = (enum fe_sec_tone_mode)parg; 2542 fepriv->state = FESTATE_DISEQC; 2543 fepriv->status = 0; 2544 } 2545 break; 2546 2547 case FE_SET_VOLTAGE: 2548 if (fe->ops.set_voltage) { 2549 err = fe->ops.set_voltage(fe, 2550 (enum fe_sec_voltage)parg); 2551 fepriv->voltage = (enum fe_sec_voltage)parg; 2552 fepriv->state = FESTATE_DISEQC; 2553 fepriv->status = 0; 2554 } 2555 break; 2556 2557 case FE_DISEQC_RECV_SLAVE_REPLY: 2558 if (fe->ops.diseqc_recv_slave_reply) 2559 err = fe->ops.diseqc_recv_slave_reply(fe, parg); 2560 break; 2561 2562 case FE_ENABLE_HIGH_LNB_VOLTAGE: 2563 if (fe->ops.enable_high_lnb_voltage) 2564 err = fe->ops.enable_high_lnb_voltage(fe, (long)parg); 2565 break; 2566 2567 case FE_SET_FRONTEND_TUNE_MODE: 2568 fepriv->tune_mode_flags = (unsigned long)parg; 2569 err = 0; 2570 break; 2571 /* DEPRECATED dish control ioctls */ 2572 2573 case FE_DISHNETWORK_SEND_LEGACY_CMD: 2574 if (fe->ops.dishnetwork_send_legacy_command) { 2575 err = fe->ops.dishnetwork_send_legacy_command(fe, 2576 (unsigned long)parg); 2577 fepriv->state = FESTATE_DISEQC; 2578 fepriv->status = 0; 2579 } else if (fe->ops.set_voltage) { 2580 /* 2581 * NOTE: This is a fallback condition. Some frontends 2582 * (stv0299 for instance) take longer than 8msec to 2583 * respond to a set_voltage command. Those switches 2584 * need custom routines to switch properly. For all 2585 * other frontends, the following should work ok. 2586 * Dish network legacy switches (as used by Dish500) 2587 * are controlled by sending 9-bit command words 2588 * spaced 8msec apart. 2589 * the actual command word is switch/port dependent 2590 * so it is up to the userspace application to send 2591 * the right command. 2592 * The command must always start with a '0' after 2593 * initialization, so parg is 8 bits and does not 2594 * include the initialization or start bit 2595 */ 2596 unsigned long swcmd = ((unsigned long)parg) << 1; 2597 ktime_t nexttime; 2598 ktime_t tv[10]; 2599 int i; 2600 u8 last = 1; 2601 2602 if (dvb_frontend_debug) 2603 dprintk("switch command: 0x%04lx\n", 2604 swcmd); 2605 nexttime = ktime_get_boottime(); 2606 if (dvb_frontend_debug) 2607 tv[0] = nexttime; 2608 /* before sending a command, initialize by sending 2609 * a 32ms 18V to the switch 2610 */ 2611 fe->ops.set_voltage(fe, SEC_VOLTAGE_18); 2612 dvb_frontend_sleep_until(&nexttime, 32000); 2613 2614 for (i = 0; i < 9; i++) { 2615 if (dvb_frontend_debug) 2616 tv[i + 1] = ktime_get_boottime(); 2617 if ((swcmd & 0x01) != last) { 2618 /* set voltage to (last ? 13V : 18V) */ 2619 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18); 2620 last = (last) ? 0 : 1; 2621 } 2622 swcmd = swcmd >> 1; 2623 if (i != 8) 2624 dvb_frontend_sleep_until(&nexttime, 8000); 2625 } 2626 if (dvb_frontend_debug) { 2627 dprintk("(adapter %d): switch delay (should be 32k followed by all 8k)\n", 2628 fe->dvb->num); 2629 for (i = 1; i < 10; i++) 2630 pr_info("%d: %d\n", i, 2631 (int)ktime_us_delta(tv[i], tv[i - 1])); 2632 } 2633 err = 0; 2634 fepriv->state = FESTATE_DISEQC; 2635 fepriv->status = 0; 2636 } 2637 break; 2638 2639 /* DEPRECATED statistics ioctls */ 2640 2641 case FE_READ_BER: 2642 if (fe->ops.read_ber) { 2643 if (fepriv->thread) 2644 err = fe->ops.read_ber(fe, parg); 2645 else 2646 err = -EAGAIN; 2647 } 2648 break; 2649 2650 case FE_READ_SIGNAL_STRENGTH: 2651 if (fe->ops.read_signal_strength) { 2652 if (fepriv->thread) 2653 err = fe->ops.read_signal_strength(fe, parg); 2654 else 2655 err = -EAGAIN; 2656 } 2657 break; 2658 2659 case FE_READ_SNR: 2660 if (fe->ops.read_snr) { 2661 if (fepriv->thread) 2662 err = fe->ops.read_snr(fe, parg); 2663 else 2664 err = -EAGAIN; 2665 } 2666 break; 2667 2668 case FE_READ_UNCORRECTED_BLOCKS: 2669 if (fe->ops.read_ucblocks) { 2670 if (fepriv->thread) 2671 err = fe->ops.read_ucblocks(fe, parg); 2672 else 2673 err = -EAGAIN; 2674 } 2675 break; 2676 2677 /* DEPRECATED DVBv3 ioctls */ 2678 2679 case FE_SET_FRONTEND: 2680 err = dvbv3_set_delivery_system(fe); 2681 if (err) 2682 break; 2683 2684 err = dtv_property_cache_sync(fe, c, parg); 2685 if (err) 2686 break; 2687 err = dtv_set_frontend(fe); 2688 break; 2689 2690 case FE_GET_EVENT: 2691 err = dvb_frontend_get_event(fe, parg, file->f_flags); 2692 break; 2693 2694 case FE_GET_FRONTEND: 2695 err = dvb_get_frontend(fe, parg); 2696 break; 2697 2698 default: 2699 return -ENOTSUPP; 2700 } /* switch */ 2701 2702 return err; 2703 } 2704 2705 static __poll_t dvb_frontend_poll(struct file *file, struct poll_table_struct *wait) 2706 { 2707 struct dvb_device *dvbdev = file->private_data; 2708 struct dvb_frontend *fe = dvbdev->priv; 2709 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2710 2711 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__); 2712 2713 poll_wait(file, &fepriv->events.wait_queue, wait); 2714 2715 if (fepriv->events.eventw != fepriv->events.eventr) 2716 return (EPOLLIN | EPOLLRDNORM | EPOLLPRI); 2717 2718 return 0; 2719 } 2720 2721 static int dvb_frontend_open(struct inode *inode, struct file *file) 2722 { 2723 struct dvb_device *dvbdev = file->private_data; 2724 struct dvb_frontend *fe = dvbdev->priv; 2725 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2726 struct dvb_adapter *adapter = fe->dvb; 2727 int ret; 2728 2729 dev_dbg(fe->dvb->device, "%s:\n", __func__); 2730 if (fe->exit == DVB_FE_DEVICE_REMOVED) 2731 return -ENODEV; 2732 2733 if (adapter->mfe_shared) { 2734 mutex_lock(&adapter->mfe_lock); 2735 2736 if (!adapter->mfe_dvbdev) 2737 adapter->mfe_dvbdev = dvbdev; 2738 2739 else if (adapter->mfe_dvbdev != dvbdev) { 2740 struct dvb_device 2741 *mfedev = adapter->mfe_dvbdev; 2742 struct dvb_frontend 2743 *mfe = mfedev->priv; 2744 struct dvb_frontend_private 2745 *mfepriv = mfe->frontend_priv; 2746 int mferetry = (dvb_mfe_wait_time << 1); 2747 2748 mutex_unlock(&adapter->mfe_lock); 2749 while (mferetry-- && (mfedev->users != -1 || 2750 mfepriv->thread)) { 2751 if (msleep_interruptible(500)) { 2752 if (signal_pending(current)) 2753 return -EINTR; 2754 } 2755 } 2756 2757 mutex_lock(&adapter->mfe_lock); 2758 if (adapter->mfe_dvbdev != dvbdev) { 2759 mfedev = adapter->mfe_dvbdev; 2760 mfe = mfedev->priv; 2761 mfepriv = mfe->frontend_priv; 2762 if (mfedev->users != -1 || 2763 mfepriv->thread) { 2764 mutex_unlock(&adapter->mfe_lock); 2765 return -EBUSY; 2766 } 2767 adapter->mfe_dvbdev = dvbdev; 2768 } 2769 } 2770 } 2771 2772 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) { 2773 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0) 2774 goto err0; 2775 2776 /* If we took control of the bus, we need to force 2777 reinitialization. This is because many ts_bus_ctrl() 2778 functions strobe the RESET pin on the demod, and if the 2779 frontend thread already exists then the dvb_init() routine 2780 won't get called (which is what usually does initial 2781 register configuration). */ 2782 fepriv->reinitialise = 1; 2783 } 2784 2785 if ((ret = dvb_generic_open(inode, file)) < 0) 2786 goto err1; 2787 2788 if ((file->f_flags & O_ACCMODE) != O_RDONLY) { 2789 /* normal tune mode when opened R/W */ 2790 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT; 2791 fepriv->tone = -1; 2792 fepriv->voltage = -1; 2793 2794 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 2795 mutex_lock(&fe->dvb->mdev_lock); 2796 if (fe->dvb->mdev) { 2797 mutex_lock(&fe->dvb->mdev->graph_mutex); 2798 if (fe->dvb->mdev->enable_source) 2799 ret = fe->dvb->mdev->enable_source( 2800 dvbdev->entity, 2801 &fepriv->pipe); 2802 mutex_unlock(&fe->dvb->mdev->graph_mutex); 2803 if (ret) { 2804 mutex_unlock(&fe->dvb->mdev_lock); 2805 dev_err(fe->dvb->device, 2806 "Tuner is busy. Error %d\n", ret); 2807 goto err2; 2808 } 2809 } 2810 mutex_unlock(&fe->dvb->mdev_lock); 2811 #endif 2812 ret = dvb_frontend_start(fe); 2813 if (ret) 2814 goto err3; 2815 2816 /* empty event queue */ 2817 fepriv->events.eventr = fepriv->events.eventw = 0; 2818 } 2819 2820 dvb_frontend_get(fe); 2821 2822 if (adapter->mfe_shared) 2823 mutex_unlock(&adapter->mfe_lock); 2824 return ret; 2825 2826 err3: 2827 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 2828 mutex_lock(&fe->dvb->mdev_lock); 2829 if (fe->dvb->mdev) { 2830 mutex_lock(&fe->dvb->mdev->graph_mutex); 2831 if (fe->dvb->mdev->disable_source) 2832 fe->dvb->mdev->disable_source(dvbdev->entity); 2833 mutex_unlock(&fe->dvb->mdev->graph_mutex); 2834 } 2835 mutex_unlock(&fe->dvb->mdev_lock); 2836 err2: 2837 #endif 2838 dvb_generic_release(inode, file); 2839 err1: 2840 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) 2841 fe->ops.ts_bus_ctrl(fe, 0); 2842 err0: 2843 if (adapter->mfe_shared) 2844 mutex_unlock(&adapter->mfe_lock); 2845 return ret; 2846 } 2847 2848 static int dvb_frontend_release(struct inode *inode, struct file *file) 2849 { 2850 struct dvb_device *dvbdev = file->private_data; 2851 struct dvb_frontend *fe = dvbdev->priv; 2852 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2853 int ret; 2854 2855 dev_dbg(fe->dvb->device, "%s:\n", __func__); 2856 2857 if ((file->f_flags & O_ACCMODE) != O_RDONLY) { 2858 fepriv->release_jiffies = jiffies; 2859 mb(); 2860 } 2861 2862 ret = dvb_generic_release(inode, file); 2863 2864 if (dvbdev->users == -1) { 2865 wake_up(&fepriv->wait_queue); 2866 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 2867 mutex_lock(&fe->dvb->mdev_lock); 2868 if (fe->dvb->mdev) { 2869 mutex_lock(&fe->dvb->mdev->graph_mutex); 2870 if (fe->dvb->mdev->disable_source) 2871 fe->dvb->mdev->disable_source(dvbdev->entity); 2872 mutex_unlock(&fe->dvb->mdev->graph_mutex); 2873 } 2874 mutex_unlock(&fe->dvb->mdev_lock); 2875 #endif 2876 if (fe->exit != DVB_FE_NO_EXIT) 2877 wake_up(&dvbdev->wait_queue); 2878 if (fe->ops.ts_bus_ctrl) 2879 fe->ops.ts_bus_ctrl(fe, 0); 2880 } 2881 2882 dvb_frontend_put(fe); 2883 2884 return ret; 2885 } 2886 2887 static const struct file_operations dvb_frontend_fops = { 2888 .owner = THIS_MODULE, 2889 .unlocked_ioctl = dvb_frontend_ioctl, 2890 #ifdef CONFIG_COMPAT 2891 .compat_ioctl = dvb_frontend_compat_ioctl, 2892 #endif 2893 .poll = dvb_frontend_poll, 2894 .open = dvb_frontend_open, 2895 .release = dvb_frontend_release, 2896 .llseek = noop_llseek, 2897 }; 2898 2899 int dvb_frontend_suspend(struct dvb_frontend *fe) 2900 { 2901 int ret = 0; 2902 2903 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num, 2904 fe->id); 2905 2906 if (fe->ops.tuner_ops.suspend) 2907 ret = fe->ops.tuner_ops.suspend(fe); 2908 else if (fe->ops.tuner_ops.sleep) 2909 ret = fe->ops.tuner_ops.sleep(fe); 2910 2911 if (fe->ops.sleep) 2912 ret = fe->ops.sleep(fe); 2913 2914 return ret; 2915 } 2916 EXPORT_SYMBOL(dvb_frontend_suspend); 2917 2918 int dvb_frontend_resume(struct dvb_frontend *fe) 2919 { 2920 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2921 int ret = 0; 2922 2923 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num, 2924 fe->id); 2925 2926 fe->exit = DVB_FE_DEVICE_RESUME; 2927 if (fe->ops.init) 2928 ret = fe->ops.init(fe); 2929 2930 if (fe->ops.tuner_ops.resume) 2931 ret = fe->ops.tuner_ops.resume(fe); 2932 else if (fe->ops.tuner_ops.init) 2933 ret = fe->ops.tuner_ops.init(fe); 2934 2935 if (fe->ops.set_tone && fepriv->tone != -1) 2936 fe->ops.set_tone(fe, fepriv->tone); 2937 if (fe->ops.set_voltage && fepriv->voltage != -1) 2938 fe->ops.set_voltage(fe, fepriv->voltage); 2939 2940 fe->exit = DVB_FE_NO_EXIT; 2941 fepriv->state = FESTATE_RETUNE; 2942 dvb_frontend_wakeup(fe); 2943 2944 return ret; 2945 } 2946 EXPORT_SYMBOL(dvb_frontend_resume); 2947 2948 int dvb_register_frontend(struct dvb_adapter *dvb, 2949 struct dvb_frontend *fe) 2950 { 2951 struct dvb_frontend_private *fepriv; 2952 const struct dvb_device dvbdev_template = { 2953 .users = ~0, 2954 .writers = 1, 2955 .readers = (~0) - 1, 2956 .fops = &dvb_frontend_fops, 2957 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 2958 .name = fe->ops.info.name, 2959 #endif 2960 }; 2961 2962 dev_dbg(dvb->device, "%s:\n", __func__); 2963 2964 if (mutex_lock_interruptible(&frontend_mutex)) 2965 return -ERESTARTSYS; 2966 2967 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL); 2968 if (!fe->frontend_priv) { 2969 mutex_unlock(&frontend_mutex); 2970 return -ENOMEM; 2971 } 2972 fepriv = fe->frontend_priv; 2973 2974 kref_init(&fe->refcount); 2975 2976 /* 2977 * After initialization, there need to be two references: one 2978 * for dvb_unregister_frontend(), and another one for 2979 * dvb_frontend_detach(). 2980 */ 2981 dvb_frontend_get(fe); 2982 2983 sema_init(&fepriv->sem, 1); 2984 init_waitqueue_head(&fepriv->wait_queue); 2985 init_waitqueue_head(&fepriv->events.wait_queue); 2986 mutex_init(&fepriv->events.mtx); 2987 fe->dvb = dvb; 2988 fepriv->inversion = INVERSION_OFF; 2989 2990 dev_info(fe->dvb->device, 2991 "DVB: registering adapter %i frontend %i (%s)...\n", 2992 fe->dvb->num, fe->id, fe->ops.info.name); 2993 2994 dvb_register_device(fe->dvb, &fepriv->dvbdev, &dvbdev_template, 2995 fe, DVB_DEVICE_FRONTEND, 0); 2996 2997 /* 2998 * Initialize the cache to the proper values according with the 2999 * first supported delivery system (ops->delsys[0]) 3000 */ 3001 3002 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0]; 3003 dvb_frontend_clear_cache(fe); 3004 3005 mutex_unlock(&frontend_mutex); 3006 return 0; 3007 } 3008 EXPORT_SYMBOL(dvb_register_frontend); 3009 3010 int dvb_unregister_frontend(struct dvb_frontend *fe) 3011 { 3012 struct dvb_frontend_private *fepriv = fe->frontend_priv; 3013 3014 dev_dbg(fe->dvb->device, "%s:\n", __func__); 3015 3016 mutex_lock(&frontend_mutex); 3017 dvb_frontend_stop(fe); 3018 dvb_remove_device(fepriv->dvbdev); 3019 3020 /* fe is invalid now */ 3021 mutex_unlock(&frontend_mutex); 3022 dvb_frontend_put(fe); 3023 return 0; 3024 } 3025 EXPORT_SYMBOL(dvb_unregister_frontend); 3026 3027 static void dvb_frontend_invoke_release(struct dvb_frontend *fe, 3028 void (*release)(struct dvb_frontend *fe)) 3029 { 3030 if (release) { 3031 release(fe); 3032 #ifdef CONFIG_MEDIA_ATTACH 3033 dvb_detach(release); 3034 #endif 3035 } 3036 } 3037 3038 void dvb_frontend_detach(struct dvb_frontend *fe) 3039 { 3040 dvb_frontend_invoke_release(fe, fe->ops.release_sec); 3041 dvb_frontend_invoke_release(fe, fe->ops.tuner_ops.release); 3042 dvb_frontend_invoke_release(fe, fe->ops.analog_ops.release); 3043 dvb_frontend_invoke_release(fe, fe->ops.detach); 3044 dvb_frontend_put(fe); 3045 } 3046 EXPORT_SYMBOL(dvb_frontend_detach); 3047