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