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