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 { 899 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min); 900 901 if (fe->ops.info.frequency_max == 0) 902 *freq_max = fe->ops.tuner_ops.info.frequency_max; 903 else if (fe->ops.tuner_ops.info.frequency_max == 0) 904 *freq_max = fe->ops.info.frequency_max; 905 else 906 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max); 907 908 if (*freq_min == 0 || *freq_max == 0) 909 dev_warn(fe->dvb->device, 910 "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n", 911 fe->dvb->num, fe->id); 912 } 913 914 static int dvb_frontend_check_parameters(struct dvb_frontend *fe) 915 { 916 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 917 u32 freq_min; 918 u32 freq_max; 919 920 /* range check: frequency */ 921 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max); 922 if ((freq_min && c->frequency < freq_min) || 923 (freq_max && c->frequency > freq_max)) { 924 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n", 925 fe->dvb->num, fe->id, c->frequency, 926 freq_min, freq_max); 927 return -EINVAL; 928 } 929 930 /* range check: symbol rate */ 931 switch (c->delivery_system) { 932 case SYS_DVBS: 933 case SYS_DVBS2: 934 case SYS_TURBO: 935 case SYS_DVBC_ANNEX_A: 936 case SYS_DVBC_ANNEX_C: 937 if ((fe->ops.info.symbol_rate_min && 938 c->symbol_rate < fe->ops.info.symbol_rate_min) || 939 (fe->ops.info.symbol_rate_max && 940 c->symbol_rate > fe->ops.info.symbol_rate_max)) { 941 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n", 942 fe->dvb->num, fe->id, c->symbol_rate, 943 fe->ops.info.symbol_rate_min, 944 fe->ops.info.symbol_rate_max); 945 return -EINVAL; 946 } 947 default: 948 break; 949 } 950 951 return 0; 952 } 953 954 static int dvb_frontend_clear_cache(struct dvb_frontend *fe) 955 { 956 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 957 int i; 958 u32 delsys; 959 960 delsys = c->delivery_system; 961 memset(c, 0, offsetof(struct dtv_frontend_properties, strength)); 962 c->delivery_system = delsys; 963 964 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n", 965 __func__, c->delivery_system); 966 967 c->transmission_mode = TRANSMISSION_MODE_AUTO; 968 c->bandwidth_hz = 0; /* AUTO */ 969 c->guard_interval = GUARD_INTERVAL_AUTO; 970 c->hierarchy = HIERARCHY_AUTO; 971 c->symbol_rate = 0; 972 c->code_rate_HP = FEC_AUTO; 973 c->code_rate_LP = FEC_AUTO; 974 c->fec_inner = FEC_AUTO; 975 c->rolloff = ROLLOFF_AUTO; 976 c->voltage = SEC_VOLTAGE_OFF; 977 c->sectone = SEC_TONE_OFF; 978 c->pilot = PILOT_AUTO; 979 980 c->isdbt_partial_reception = 0; 981 c->isdbt_sb_mode = 0; 982 c->isdbt_sb_subchannel = 0; 983 c->isdbt_sb_segment_idx = 0; 984 c->isdbt_sb_segment_count = 0; 985 c->isdbt_layer_enabled = 7; /* All layers (A,B,C) */ 986 for (i = 0; i < 3; i++) { 987 c->layer[i].fec = FEC_AUTO; 988 c->layer[i].modulation = QAM_AUTO; 989 c->layer[i].interleaving = 0; 990 c->layer[i].segment_count = 0; 991 } 992 993 c->stream_id = NO_STREAM_ID_FILTER; 994 c->scrambling_sequence_index = 0;/* default sequence */ 995 996 switch (c->delivery_system) { 997 case SYS_DVBS: 998 case SYS_DVBS2: 999 case SYS_TURBO: 1000 c->modulation = QPSK; /* implied for DVB-S in legacy API */ 1001 c->rolloff = ROLLOFF_35;/* implied for DVB-S */ 1002 break; 1003 case SYS_ATSC: 1004 c->modulation = VSB_8; 1005 break; 1006 case SYS_ISDBS: 1007 c->symbol_rate = 28860000; 1008 c->rolloff = ROLLOFF_35; 1009 c->bandwidth_hz = c->symbol_rate / 100 * 135; 1010 break; 1011 default: 1012 c->modulation = QAM_AUTO; 1013 break; 1014 } 1015 1016 c->lna = LNA_AUTO; 1017 1018 return 0; 1019 } 1020 1021 #define _DTV_CMD(n, s, b) \ 1022 [n] = { \ 1023 .name = #n, \ 1024 .cmd = n, \ 1025 .set = s,\ 1026 .buffer = b \ 1027 } 1028 1029 struct dtv_cmds_h { 1030 char *name; /* A display name for debugging purposes */ 1031 1032 __u32 cmd; /* A unique ID */ 1033 1034 /* Flags */ 1035 __u32 set:1; /* Either a set or get property */ 1036 __u32 buffer:1; /* Does this property use the buffer? */ 1037 __u32 reserved:30; /* Align */ 1038 }; 1039 1040 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = { 1041 _DTV_CMD(DTV_TUNE, 1, 0), 1042 _DTV_CMD(DTV_CLEAR, 1, 0), 1043 1044 /* Set */ 1045 _DTV_CMD(DTV_FREQUENCY, 1, 0), 1046 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0), 1047 _DTV_CMD(DTV_MODULATION, 1, 0), 1048 _DTV_CMD(DTV_INVERSION, 1, 0), 1049 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1), 1050 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0), 1051 _DTV_CMD(DTV_INNER_FEC, 1, 0), 1052 _DTV_CMD(DTV_VOLTAGE, 1, 0), 1053 _DTV_CMD(DTV_TONE, 1, 0), 1054 _DTV_CMD(DTV_PILOT, 1, 0), 1055 _DTV_CMD(DTV_ROLLOFF, 1, 0), 1056 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0), 1057 _DTV_CMD(DTV_HIERARCHY, 1, 0), 1058 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0), 1059 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0), 1060 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0), 1061 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0), 1062 _DTV_CMD(DTV_INTERLEAVING, 1, 0), 1063 1064 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0), 1065 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0), 1066 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0), 1067 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0), 1068 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0), 1069 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0), 1070 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0), 1071 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0), 1072 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0), 1073 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0), 1074 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0), 1075 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0), 1076 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0), 1077 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0), 1078 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0), 1079 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0), 1080 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0), 1081 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0), 1082 1083 _DTV_CMD(DTV_STREAM_ID, 1, 0), 1084 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0), 1085 _DTV_CMD(DTV_SCRAMBLING_SEQUENCE_INDEX, 1, 0), 1086 _DTV_CMD(DTV_LNA, 1, 0), 1087 1088 /* Get */ 1089 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1), 1090 _DTV_CMD(DTV_API_VERSION, 0, 0), 1091 1092 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0), 1093 1094 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0), 1095 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0), 1096 1097 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0), 1098 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0), 1099 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0), 1100 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0), 1101 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0), 1102 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0), 1103 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0), 1104 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0), 1105 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0), 1106 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0), 1107 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0), 1108 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0), 1109 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0), 1110 1111 /* Statistics API */ 1112 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0), 1113 _DTV_CMD(DTV_STAT_CNR, 0, 0), 1114 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0), 1115 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0), 1116 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0), 1117 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0), 1118 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0), 1119 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0), 1120 }; 1121 1122 /* Synchronise the legacy tuning parameters into the cache, so that demodulator 1123 * drivers can use a single set_frontend tuning function, regardless of whether 1124 * it's being used for the legacy or new API, reducing code and complexity. 1125 */ 1126 static int dtv_property_cache_sync(struct dvb_frontend *fe, 1127 struct dtv_frontend_properties *c, 1128 const struct dvb_frontend_parameters *p) 1129 { 1130 c->frequency = p->frequency; 1131 c->inversion = p->inversion; 1132 1133 switch (dvbv3_type(c->delivery_system)) { 1134 case DVBV3_QPSK: 1135 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__); 1136 c->symbol_rate = p->u.qpsk.symbol_rate; 1137 c->fec_inner = p->u.qpsk.fec_inner; 1138 break; 1139 case DVBV3_QAM: 1140 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__); 1141 c->symbol_rate = p->u.qam.symbol_rate; 1142 c->fec_inner = p->u.qam.fec_inner; 1143 c->modulation = p->u.qam.modulation; 1144 break; 1145 case DVBV3_OFDM: 1146 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__); 1147 1148 switch (p->u.ofdm.bandwidth) { 1149 case BANDWIDTH_10_MHZ: 1150 c->bandwidth_hz = 10000000; 1151 break; 1152 case BANDWIDTH_8_MHZ: 1153 c->bandwidth_hz = 8000000; 1154 break; 1155 case BANDWIDTH_7_MHZ: 1156 c->bandwidth_hz = 7000000; 1157 break; 1158 case BANDWIDTH_6_MHZ: 1159 c->bandwidth_hz = 6000000; 1160 break; 1161 case BANDWIDTH_5_MHZ: 1162 c->bandwidth_hz = 5000000; 1163 break; 1164 case BANDWIDTH_1_712_MHZ: 1165 c->bandwidth_hz = 1712000; 1166 break; 1167 case BANDWIDTH_AUTO: 1168 c->bandwidth_hz = 0; 1169 } 1170 1171 c->code_rate_HP = p->u.ofdm.code_rate_HP; 1172 c->code_rate_LP = p->u.ofdm.code_rate_LP; 1173 c->modulation = p->u.ofdm.constellation; 1174 c->transmission_mode = p->u.ofdm.transmission_mode; 1175 c->guard_interval = p->u.ofdm.guard_interval; 1176 c->hierarchy = p->u.ofdm.hierarchy_information; 1177 break; 1178 case DVBV3_ATSC: 1179 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__); 1180 c->modulation = p->u.vsb.modulation; 1181 if (c->delivery_system == SYS_ATSCMH) 1182 break; 1183 if ((c->modulation == VSB_8) || (c->modulation == VSB_16)) 1184 c->delivery_system = SYS_ATSC; 1185 else 1186 c->delivery_system = SYS_DVBC_ANNEX_B; 1187 break; 1188 case DVBV3_UNKNOWN: 1189 dev_err(fe->dvb->device, 1190 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n", 1191 __func__, c->delivery_system); 1192 return -EINVAL; 1193 } 1194 1195 return 0; 1196 } 1197 1198 /* Ensure the cached values are set correctly in the frontend 1199 * legacy tuning structures, for the advanced tuning API. 1200 */ 1201 static int 1202 dtv_property_legacy_params_sync(struct dvb_frontend *fe, 1203 const struct dtv_frontend_properties *c, 1204 struct dvb_frontend_parameters *p) 1205 { 1206 p->frequency = c->frequency; 1207 p->inversion = c->inversion; 1208 1209 switch (dvbv3_type(c->delivery_system)) { 1210 case DVBV3_UNKNOWN: 1211 dev_err(fe->dvb->device, 1212 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n", 1213 __func__, c->delivery_system); 1214 return -EINVAL; 1215 case DVBV3_QPSK: 1216 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__); 1217 p->u.qpsk.symbol_rate = c->symbol_rate; 1218 p->u.qpsk.fec_inner = c->fec_inner; 1219 break; 1220 case DVBV3_QAM: 1221 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__); 1222 p->u.qam.symbol_rate = c->symbol_rate; 1223 p->u.qam.fec_inner = c->fec_inner; 1224 p->u.qam.modulation = c->modulation; 1225 break; 1226 case DVBV3_OFDM: 1227 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__); 1228 switch (c->bandwidth_hz) { 1229 case 10000000: 1230 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ; 1231 break; 1232 case 8000000: 1233 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ; 1234 break; 1235 case 7000000: 1236 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ; 1237 break; 1238 case 6000000: 1239 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ; 1240 break; 1241 case 5000000: 1242 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ; 1243 break; 1244 case 1712000: 1245 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ; 1246 break; 1247 case 0: 1248 default: 1249 p->u.ofdm.bandwidth = BANDWIDTH_AUTO; 1250 } 1251 p->u.ofdm.code_rate_HP = c->code_rate_HP; 1252 p->u.ofdm.code_rate_LP = c->code_rate_LP; 1253 p->u.ofdm.constellation = c->modulation; 1254 p->u.ofdm.transmission_mode = c->transmission_mode; 1255 p->u.ofdm.guard_interval = c->guard_interval; 1256 p->u.ofdm.hierarchy_information = c->hierarchy; 1257 break; 1258 case DVBV3_ATSC: 1259 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__); 1260 p->u.vsb.modulation = c->modulation; 1261 break; 1262 } 1263 return 0; 1264 } 1265 1266 /** 1267 * dtv_get_frontend - calls a callback for retrieving DTV parameters 1268 * @fe: struct dvb_frontend pointer 1269 * @c: struct dtv_frontend_properties pointer (DVBv5 cache) 1270 * @p_out: struct dvb_frontend_parameters pointer (DVBv3 FE struct) 1271 * 1272 * This routine calls either the DVBv3 or DVBv5 get_frontend call. 1273 * If c is not null, it will update the DVBv5 cache struct pointed by it. 1274 * If p_out is not null, it will update the DVBv3 params pointed by it. 1275 */ 1276 static int dtv_get_frontend(struct dvb_frontend *fe, 1277 struct dtv_frontend_properties *c, 1278 struct dvb_frontend_parameters *p_out) 1279 { 1280 int r; 1281 1282 if (fe->ops.get_frontend) { 1283 r = fe->ops.get_frontend(fe, c); 1284 if (unlikely(r < 0)) 1285 return r; 1286 if (p_out) 1287 dtv_property_legacy_params_sync(fe, c, p_out); 1288 return 0; 1289 } 1290 1291 /* As everything is in cache, get_frontend fops are always supported */ 1292 return 0; 1293 } 1294 1295 static int dvb_frontend_handle_ioctl(struct file *file, 1296 unsigned int cmd, void *parg); 1297 1298 static int dtv_property_process_get(struct dvb_frontend *fe, 1299 const struct dtv_frontend_properties *c, 1300 struct dtv_property *tvp, 1301 struct file *file) 1302 { 1303 int ncaps; 1304 1305 switch (tvp->cmd) { 1306 case DTV_ENUM_DELSYS: 1307 ncaps = 0; 1308 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1309 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps]; 1310 ncaps++; 1311 } 1312 tvp->u.buffer.len = ncaps; 1313 break; 1314 case DTV_FREQUENCY: 1315 tvp->u.data = c->frequency; 1316 break; 1317 case DTV_MODULATION: 1318 tvp->u.data = c->modulation; 1319 break; 1320 case DTV_BANDWIDTH_HZ: 1321 tvp->u.data = c->bandwidth_hz; 1322 break; 1323 case DTV_INVERSION: 1324 tvp->u.data = c->inversion; 1325 break; 1326 case DTV_SYMBOL_RATE: 1327 tvp->u.data = c->symbol_rate; 1328 break; 1329 case DTV_INNER_FEC: 1330 tvp->u.data = c->fec_inner; 1331 break; 1332 case DTV_PILOT: 1333 tvp->u.data = c->pilot; 1334 break; 1335 case DTV_ROLLOFF: 1336 tvp->u.data = c->rolloff; 1337 break; 1338 case DTV_DELIVERY_SYSTEM: 1339 tvp->u.data = c->delivery_system; 1340 break; 1341 case DTV_VOLTAGE: 1342 tvp->u.data = c->voltage; 1343 break; 1344 case DTV_TONE: 1345 tvp->u.data = c->sectone; 1346 break; 1347 case DTV_API_VERSION: 1348 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR; 1349 break; 1350 case DTV_CODE_RATE_HP: 1351 tvp->u.data = c->code_rate_HP; 1352 break; 1353 case DTV_CODE_RATE_LP: 1354 tvp->u.data = c->code_rate_LP; 1355 break; 1356 case DTV_GUARD_INTERVAL: 1357 tvp->u.data = c->guard_interval; 1358 break; 1359 case DTV_TRANSMISSION_MODE: 1360 tvp->u.data = c->transmission_mode; 1361 break; 1362 case DTV_HIERARCHY: 1363 tvp->u.data = c->hierarchy; 1364 break; 1365 case DTV_INTERLEAVING: 1366 tvp->u.data = c->interleaving; 1367 break; 1368 1369 /* ISDB-T Support here */ 1370 case DTV_ISDBT_PARTIAL_RECEPTION: 1371 tvp->u.data = c->isdbt_partial_reception; 1372 break; 1373 case DTV_ISDBT_SOUND_BROADCASTING: 1374 tvp->u.data = c->isdbt_sb_mode; 1375 break; 1376 case DTV_ISDBT_SB_SUBCHANNEL_ID: 1377 tvp->u.data = c->isdbt_sb_subchannel; 1378 break; 1379 case DTV_ISDBT_SB_SEGMENT_IDX: 1380 tvp->u.data = c->isdbt_sb_segment_idx; 1381 break; 1382 case DTV_ISDBT_SB_SEGMENT_COUNT: 1383 tvp->u.data = c->isdbt_sb_segment_count; 1384 break; 1385 case DTV_ISDBT_LAYER_ENABLED: 1386 tvp->u.data = c->isdbt_layer_enabled; 1387 break; 1388 case DTV_ISDBT_LAYERA_FEC: 1389 tvp->u.data = c->layer[0].fec; 1390 break; 1391 case DTV_ISDBT_LAYERA_MODULATION: 1392 tvp->u.data = c->layer[0].modulation; 1393 break; 1394 case DTV_ISDBT_LAYERA_SEGMENT_COUNT: 1395 tvp->u.data = c->layer[0].segment_count; 1396 break; 1397 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING: 1398 tvp->u.data = c->layer[0].interleaving; 1399 break; 1400 case DTV_ISDBT_LAYERB_FEC: 1401 tvp->u.data = c->layer[1].fec; 1402 break; 1403 case DTV_ISDBT_LAYERB_MODULATION: 1404 tvp->u.data = c->layer[1].modulation; 1405 break; 1406 case DTV_ISDBT_LAYERB_SEGMENT_COUNT: 1407 tvp->u.data = c->layer[1].segment_count; 1408 break; 1409 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING: 1410 tvp->u.data = c->layer[1].interleaving; 1411 break; 1412 case DTV_ISDBT_LAYERC_FEC: 1413 tvp->u.data = c->layer[2].fec; 1414 break; 1415 case DTV_ISDBT_LAYERC_MODULATION: 1416 tvp->u.data = c->layer[2].modulation; 1417 break; 1418 case DTV_ISDBT_LAYERC_SEGMENT_COUNT: 1419 tvp->u.data = c->layer[2].segment_count; 1420 break; 1421 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING: 1422 tvp->u.data = c->layer[2].interleaving; 1423 break; 1424 1425 /* Multistream support */ 1426 case DTV_STREAM_ID: 1427 case DTV_DVBT2_PLP_ID_LEGACY: 1428 tvp->u.data = c->stream_id; 1429 break; 1430 1431 /* Physical layer scrambling support */ 1432 case DTV_SCRAMBLING_SEQUENCE_INDEX: 1433 tvp->u.data = c->scrambling_sequence_index; 1434 break; 1435 1436 /* ATSC-MH */ 1437 case DTV_ATSCMH_FIC_VER: 1438 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver; 1439 break; 1440 case DTV_ATSCMH_PARADE_ID: 1441 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id; 1442 break; 1443 case DTV_ATSCMH_NOG: 1444 tvp->u.data = fe->dtv_property_cache.atscmh_nog; 1445 break; 1446 case DTV_ATSCMH_TNOG: 1447 tvp->u.data = fe->dtv_property_cache.atscmh_tnog; 1448 break; 1449 case DTV_ATSCMH_SGN: 1450 tvp->u.data = fe->dtv_property_cache.atscmh_sgn; 1451 break; 1452 case DTV_ATSCMH_PRC: 1453 tvp->u.data = fe->dtv_property_cache.atscmh_prc; 1454 break; 1455 case DTV_ATSCMH_RS_FRAME_MODE: 1456 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode; 1457 break; 1458 case DTV_ATSCMH_RS_FRAME_ENSEMBLE: 1459 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble; 1460 break; 1461 case DTV_ATSCMH_RS_CODE_MODE_PRI: 1462 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri; 1463 break; 1464 case DTV_ATSCMH_RS_CODE_MODE_SEC: 1465 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec; 1466 break; 1467 case DTV_ATSCMH_SCCC_BLOCK_MODE: 1468 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode; 1469 break; 1470 case DTV_ATSCMH_SCCC_CODE_MODE_A: 1471 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a; 1472 break; 1473 case DTV_ATSCMH_SCCC_CODE_MODE_B: 1474 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b; 1475 break; 1476 case DTV_ATSCMH_SCCC_CODE_MODE_C: 1477 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c; 1478 break; 1479 case DTV_ATSCMH_SCCC_CODE_MODE_D: 1480 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d; 1481 break; 1482 1483 case DTV_LNA: 1484 tvp->u.data = c->lna; 1485 break; 1486 1487 /* Fill quality measures */ 1488 case DTV_STAT_SIGNAL_STRENGTH: 1489 tvp->u.st = c->strength; 1490 break; 1491 case DTV_STAT_CNR: 1492 tvp->u.st = c->cnr; 1493 break; 1494 case DTV_STAT_PRE_ERROR_BIT_COUNT: 1495 tvp->u.st = c->pre_bit_error; 1496 break; 1497 case DTV_STAT_PRE_TOTAL_BIT_COUNT: 1498 tvp->u.st = c->pre_bit_count; 1499 break; 1500 case DTV_STAT_POST_ERROR_BIT_COUNT: 1501 tvp->u.st = c->post_bit_error; 1502 break; 1503 case DTV_STAT_POST_TOTAL_BIT_COUNT: 1504 tvp->u.st = c->post_bit_count; 1505 break; 1506 case DTV_STAT_ERROR_BLOCK_COUNT: 1507 tvp->u.st = c->block_error; 1508 break; 1509 case DTV_STAT_TOTAL_BLOCK_COUNT: 1510 tvp->u.st = c->block_count; 1511 break; 1512 default: 1513 dev_dbg(fe->dvb->device, 1514 "%s: FE property %d doesn't exist\n", 1515 __func__, tvp->cmd); 1516 return -EINVAL; 1517 } 1518 1519 if (!dtv_cmds[tvp->cmd].buffer) 1520 dev_dbg(fe->dvb->device, 1521 "%s: GET cmd 0x%08x (%s) = 0x%08x\n", 1522 __func__, tvp->cmd, dtv_cmds[tvp->cmd].name, 1523 tvp->u.data); 1524 else 1525 dev_dbg(fe->dvb->device, 1526 "%s: GET cmd 0x%08x (%s) len %d: %*ph\n", 1527 __func__, 1528 tvp->cmd, dtv_cmds[tvp->cmd].name, 1529 tvp->u.buffer.len, 1530 tvp->u.buffer.len, tvp->u.buffer.data); 1531 1532 return 0; 1533 } 1534 1535 static int dtv_set_frontend(struct dvb_frontend *fe); 1536 1537 static bool is_dvbv3_delsys(u32 delsys) 1538 { 1539 return (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) || 1540 (delsys == SYS_DVBS) || (delsys == SYS_ATSC); 1541 } 1542 1543 /** 1544 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type 1545 * @fe: struct frontend; 1546 * @delsys: DVBv5 type that will be used for emulation 1547 * 1548 * Provides emulation for delivery systems that are compatible with the old 1549 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows 1550 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent 1551 * parameters are compatible with DVB-S spec. 1552 */ 1553 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys) 1554 { 1555 int i; 1556 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1557 1558 c->delivery_system = delsys; 1559 1560 /* 1561 * If the call is for ISDB-T, put it into full-seg, auto mode, TV 1562 */ 1563 if (c->delivery_system == SYS_ISDBT) { 1564 dev_dbg(fe->dvb->device, 1565 "%s: Using defaults for SYS_ISDBT\n", 1566 __func__); 1567 1568 if (!c->bandwidth_hz) 1569 c->bandwidth_hz = 6000000; 1570 1571 c->isdbt_partial_reception = 0; 1572 c->isdbt_sb_mode = 0; 1573 c->isdbt_sb_subchannel = 0; 1574 c->isdbt_sb_segment_idx = 0; 1575 c->isdbt_sb_segment_count = 0; 1576 c->isdbt_layer_enabled = 7; 1577 for (i = 0; i < 3; i++) { 1578 c->layer[i].fec = FEC_AUTO; 1579 c->layer[i].modulation = QAM_AUTO; 1580 c->layer[i].interleaving = 0; 1581 c->layer[i].segment_count = 0; 1582 } 1583 } 1584 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n", 1585 __func__, c->delivery_system); 1586 1587 return 0; 1588 } 1589 1590 /** 1591 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call 1592 * @fe: frontend struct 1593 * @desired_system: delivery system requested by the user 1594 * 1595 * A DVBv5 call know what's the desired system it wants. So, set it. 1596 * 1597 * There are, however, a few known issues with early DVBv5 applications that 1598 * are also handled by this logic: 1599 * 1600 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system. 1601 * This is an API violation, but, as we don't want to break userspace, 1602 * convert it to the first supported delivery system. 1603 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for 1604 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of 1605 * ISDB-T provided backward compat with DVB-T. 1606 */ 1607 static int dvbv5_set_delivery_system(struct dvb_frontend *fe, 1608 u32 desired_system) 1609 { 1610 int ncaps; 1611 u32 delsys = SYS_UNDEFINED; 1612 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1613 enum dvbv3_emulation_type type; 1614 1615 /* 1616 * It was reported that some old DVBv5 applications were 1617 * filling delivery_system with SYS_UNDEFINED. If this happens, 1618 * assume that the application wants to use the first supported 1619 * delivery system. 1620 */ 1621 if (desired_system == SYS_UNDEFINED) 1622 desired_system = fe->ops.delsys[0]; 1623 1624 /* 1625 * This is a DVBv5 call. So, it likely knows the supported 1626 * delivery systems. So, check if the desired delivery system is 1627 * supported 1628 */ 1629 ncaps = 0; 1630 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1631 if (fe->ops.delsys[ncaps] == desired_system) { 1632 c->delivery_system = desired_system; 1633 dev_dbg(fe->dvb->device, 1634 "%s: Changing delivery system to %d\n", 1635 __func__, desired_system); 1636 return 0; 1637 } 1638 ncaps++; 1639 } 1640 1641 /* 1642 * The requested delivery system isn't supported. Maybe userspace 1643 * is requesting a DVBv3 compatible delivery system. 1644 * 1645 * The emulation only works if the desired system is one of the 1646 * delivery systems supported by DVBv3 API 1647 */ 1648 if (!is_dvbv3_delsys(desired_system)) { 1649 dev_dbg(fe->dvb->device, 1650 "%s: Delivery system %d not supported.\n", 1651 __func__, desired_system); 1652 return -EINVAL; 1653 } 1654 1655 type = dvbv3_type(desired_system); 1656 1657 /* 1658 * Get the last non-DVBv3 delivery system that has the same type 1659 * of the desired system 1660 */ 1661 ncaps = 0; 1662 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1663 if (dvbv3_type(fe->ops.delsys[ncaps]) == type) 1664 delsys = fe->ops.delsys[ncaps]; 1665 ncaps++; 1666 } 1667 1668 /* There's nothing compatible with the desired delivery system */ 1669 if (delsys == SYS_UNDEFINED) { 1670 dev_dbg(fe->dvb->device, 1671 "%s: Delivery system %d not supported on emulation mode.\n", 1672 __func__, desired_system); 1673 return -EINVAL; 1674 } 1675 1676 dev_dbg(fe->dvb->device, 1677 "%s: Using delivery system %d emulated as if it were %d\n", 1678 __func__, delsys, desired_system); 1679 1680 return emulate_delivery_system(fe, desired_system); 1681 } 1682 1683 /** 1684 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call 1685 * @fe: frontend struct 1686 * 1687 * A DVBv3 call doesn't know what's the desired system it wants. It also 1688 * doesn't allow to switch between different types. Due to that, userspace 1689 * should use DVBv5 instead. 1690 * However, in order to avoid breaking userspace API, limited backward 1691 * compatibility support is provided. 1692 * 1693 * There are some delivery systems that are incompatible with DVBv3 calls. 1694 * 1695 * This routine should work fine for frontends that support just one delivery 1696 * system. 1697 * 1698 * For frontends that support multiple frontends: 1699 * 1) It defaults to use the first supported delivery system. There's an 1700 * userspace application that allows changing it at runtime; 1701 * 1702 * 2) If the current delivery system is not compatible with DVBv3, it gets 1703 * the first one that it is compatible. 1704 * 1705 * NOTE: in order for this to work with applications like Kaffeine that 1706 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to 1707 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the 1708 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back 1709 * to DVB-S. 1710 */ 1711 static int dvbv3_set_delivery_system(struct dvb_frontend *fe) 1712 { 1713 int ncaps; 1714 u32 delsys = SYS_UNDEFINED; 1715 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1716 1717 /* If not set yet, defaults to the first supported delivery system */ 1718 if (c->delivery_system == SYS_UNDEFINED) 1719 c->delivery_system = fe->ops.delsys[0]; 1720 1721 /* 1722 * Trivial case: just use the current one, if it already a DVBv3 1723 * delivery system 1724 */ 1725 if (is_dvbv3_delsys(c->delivery_system)) { 1726 dev_dbg(fe->dvb->device, 1727 "%s: Using delivery system to %d\n", 1728 __func__, c->delivery_system); 1729 return 0; 1730 } 1731 1732 /* 1733 * Seek for the first delivery system that it is compatible with a 1734 * DVBv3 standard 1735 */ 1736 ncaps = 0; 1737 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) { 1738 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) { 1739 delsys = fe->ops.delsys[ncaps]; 1740 break; 1741 } 1742 ncaps++; 1743 } 1744 if (delsys == SYS_UNDEFINED) { 1745 dev_dbg(fe->dvb->device, 1746 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n", 1747 __func__); 1748 return -EINVAL; 1749 } 1750 return emulate_delivery_system(fe, delsys); 1751 } 1752 1753 /** 1754 * dtv_property_process_set - Sets a single DTV property 1755 * @fe: Pointer to &struct dvb_frontend 1756 * @file: Pointer to &struct file 1757 * @cmd: Digital TV command 1758 * @data: An unsigned 32-bits number 1759 * 1760 * This routine assigns the property 1761 * value to the corresponding member of 1762 * &struct dtv_frontend_properties 1763 * 1764 * Returns: 1765 * Zero on success, negative errno on failure. 1766 */ 1767 static int dtv_property_process_set(struct dvb_frontend *fe, 1768 struct file *file, 1769 u32 cmd, u32 data) 1770 { 1771 int r = 0; 1772 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1773 1774 /** Dump DTV command name and value*/ 1775 if (!cmd || cmd > DTV_MAX_COMMAND) 1776 dev_warn(fe->dvb->device, "%s: SET cmd 0x%08x undefined\n", 1777 __func__, cmd); 1778 else 1779 dev_dbg(fe->dvb->device, 1780 "%s: SET cmd 0x%08x (%s) to 0x%08x\n", 1781 __func__, cmd, dtv_cmds[cmd].name, data); 1782 switch (cmd) { 1783 case DTV_CLEAR: 1784 /* 1785 * Reset a cache of data specific to the frontend here. This does 1786 * not effect hardware. 1787 */ 1788 dvb_frontend_clear_cache(fe); 1789 break; 1790 case DTV_TUNE: 1791 /* 1792 * Use the cached Digital TV properties to tune the 1793 * frontend 1794 */ 1795 dev_dbg(fe->dvb->device, 1796 "%s: Setting the frontend from property cache\n", 1797 __func__); 1798 1799 r = dtv_set_frontend(fe); 1800 break; 1801 case DTV_FREQUENCY: 1802 c->frequency = data; 1803 break; 1804 case DTV_MODULATION: 1805 c->modulation = data; 1806 break; 1807 case DTV_BANDWIDTH_HZ: 1808 c->bandwidth_hz = data; 1809 break; 1810 case DTV_INVERSION: 1811 c->inversion = data; 1812 break; 1813 case DTV_SYMBOL_RATE: 1814 c->symbol_rate = data; 1815 break; 1816 case DTV_INNER_FEC: 1817 c->fec_inner = data; 1818 break; 1819 case DTV_PILOT: 1820 c->pilot = data; 1821 break; 1822 case DTV_ROLLOFF: 1823 c->rolloff = data; 1824 break; 1825 case DTV_DELIVERY_SYSTEM: 1826 r = dvbv5_set_delivery_system(fe, data); 1827 break; 1828 case DTV_VOLTAGE: 1829 c->voltage = data; 1830 r = dvb_frontend_handle_ioctl(file, FE_SET_VOLTAGE, 1831 (void *)c->voltage); 1832 break; 1833 case DTV_TONE: 1834 c->sectone = data; 1835 r = dvb_frontend_handle_ioctl(file, FE_SET_TONE, 1836 (void *)c->sectone); 1837 break; 1838 case DTV_CODE_RATE_HP: 1839 c->code_rate_HP = data; 1840 break; 1841 case DTV_CODE_RATE_LP: 1842 c->code_rate_LP = data; 1843 break; 1844 case DTV_GUARD_INTERVAL: 1845 c->guard_interval = data; 1846 break; 1847 case DTV_TRANSMISSION_MODE: 1848 c->transmission_mode = data; 1849 break; 1850 case DTV_HIERARCHY: 1851 c->hierarchy = data; 1852 break; 1853 case DTV_INTERLEAVING: 1854 c->interleaving = data; 1855 break; 1856 1857 /* ISDB-T Support here */ 1858 case DTV_ISDBT_PARTIAL_RECEPTION: 1859 c->isdbt_partial_reception = data; 1860 break; 1861 case DTV_ISDBT_SOUND_BROADCASTING: 1862 c->isdbt_sb_mode = data; 1863 break; 1864 case DTV_ISDBT_SB_SUBCHANNEL_ID: 1865 c->isdbt_sb_subchannel = data; 1866 break; 1867 case DTV_ISDBT_SB_SEGMENT_IDX: 1868 c->isdbt_sb_segment_idx = data; 1869 break; 1870 case DTV_ISDBT_SB_SEGMENT_COUNT: 1871 c->isdbt_sb_segment_count = data; 1872 break; 1873 case DTV_ISDBT_LAYER_ENABLED: 1874 c->isdbt_layer_enabled = data; 1875 break; 1876 case DTV_ISDBT_LAYERA_FEC: 1877 c->layer[0].fec = data; 1878 break; 1879 case DTV_ISDBT_LAYERA_MODULATION: 1880 c->layer[0].modulation = data; 1881 break; 1882 case DTV_ISDBT_LAYERA_SEGMENT_COUNT: 1883 c->layer[0].segment_count = data; 1884 break; 1885 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING: 1886 c->layer[0].interleaving = data; 1887 break; 1888 case DTV_ISDBT_LAYERB_FEC: 1889 c->layer[1].fec = data; 1890 break; 1891 case DTV_ISDBT_LAYERB_MODULATION: 1892 c->layer[1].modulation = data; 1893 break; 1894 case DTV_ISDBT_LAYERB_SEGMENT_COUNT: 1895 c->layer[1].segment_count = data; 1896 break; 1897 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING: 1898 c->layer[1].interleaving = data; 1899 break; 1900 case DTV_ISDBT_LAYERC_FEC: 1901 c->layer[2].fec = data; 1902 break; 1903 case DTV_ISDBT_LAYERC_MODULATION: 1904 c->layer[2].modulation = data; 1905 break; 1906 case DTV_ISDBT_LAYERC_SEGMENT_COUNT: 1907 c->layer[2].segment_count = data; 1908 break; 1909 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING: 1910 c->layer[2].interleaving = data; 1911 break; 1912 1913 /* Multistream support */ 1914 case DTV_STREAM_ID: 1915 case DTV_DVBT2_PLP_ID_LEGACY: 1916 c->stream_id = data; 1917 break; 1918 1919 /* Physical layer scrambling support */ 1920 case DTV_SCRAMBLING_SEQUENCE_INDEX: 1921 c->scrambling_sequence_index = data; 1922 break; 1923 1924 /* ATSC-MH */ 1925 case DTV_ATSCMH_PARADE_ID: 1926 fe->dtv_property_cache.atscmh_parade_id = data; 1927 break; 1928 case DTV_ATSCMH_RS_FRAME_ENSEMBLE: 1929 fe->dtv_property_cache.atscmh_rs_frame_ensemble = data; 1930 break; 1931 1932 case DTV_LNA: 1933 c->lna = data; 1934 if (fe->ops.set_lna) 1935 r = fe->ops.set_lna(fe); 1936 if (r < 0) 1937 c->lna = LNA_AUTO; 1938 break; 1939 1940 default: 1941 return -EINVAL; 1942 } 1943 1944 return r; 1945 } 1946 1947 static int dvb_frontend_do_ioctl(struct file *file, unsigned int cmd, 1948 void *parg) 1949 { 1950 struct dvb_device *dvbdev = file->private_data; 1951 struct dvb_frontend *fe = dvbdev->priv; 1952 struct dvb_frontend_private *fepriv = fe->frontend_priv; 1953 int err; 1954 1955 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd)); 1956 if (down_interruptible(&fepriv->sem)) 1957 return -ERESTARTSYS; 1958 1959 if (fe->exit != DVB_FE_NO_EXIT) { 1960 up(&fepriv->sem); 1961 return -ENODEV; 1962 } 1963 1964 /* 1965 * If the frontend is opened in read-only mode, only the ioctls 1966 * that don't interfere with the tune logic should be accepted. 1967 * That allows an external application to monitor the DVB QoS and 1968 * statistics parameters. 1969 * 1970 * That matches all _IOR() ioctls, except for two special cases: 1971 * - FE_GET_EVENT is part of the tuning logic on a DVB application; 1972 * - FE_DISEQC_RECV_SLAVE_REPLY is part of DiSEqC 2.0 1973 * setup 1974 * So, those two ioctls should also return -EPERM, as otherwise 1975 * reading from them would interfere with a DVB tune application 1976 */ 1977 if ((file->f_flags & O_ACCMODE) == O_RDONLY 1978 && (_IOC_DIR(cmd) != _IOC_READ 1979 || cmd == FE_GET_EVENT 1980 || cmd == FE_DISEQC_RECV_SLAVE_REPLY)) { 1981 up(&fepriv->sem); 1982 return -EPERM; 1983 } 1984 1985 err = dvb_frontend_handle_ioctl(file, cmd, parg); 1986 1987 up(&fepriv->sem); 1988 return err; 1989 } 1990 1991 static long dvb_frontend_ioctl(struct file *file, unsigned int cmd, 1992 unsigned long arg) 1993 { 1994 struct dvb_device *dvbdev = file->private_data; 1995 1996 if (!dvbdev) 1997 return -ENODEV; 1998 1999 return dvb_usercopy(file, cmd, arg, dvb_frontend_do_ioctl); 2000 } 2001 2002 #ifdef CONFIG_COMPAT 2003 struct compat_dtv_property { 2004 __u32 cmd; 2005 __u32 reserved[3]; 2006 union { 2007 __u32 data; 2008 struct dtv_fe_stats st; 2009 struct { 2010 __u8 data[32]; 2011 __u32 len; 2012 __u32 reserved1[3]; 2013 compat_uptr_t reserved2; 2014 } buffer; 2015 } u; 2016 int result; 2017 } __attribute__ ((packed)); 2018 2019 struct compat_dtv_properties { 2020 __u32 num; 2021 compat_uptr_t props; 2022 }; 2023 2024 #define COMPAT_FE_SET_PROPERTY _IOW('o', 82, struct compat_dtv_properties) 2025 #define COMPAT_FE_GET_PROPERTY _IOR('o', 83, struct compat_dtv_properties) 2026 2027 static int dvb_frontend_handle_compat_ioctl(struct file *file, unsigned int cmd, 2028 unsigned long arg) 2029 { 2030 struct dvb_device *dvbdev = file->private_data; 2031 struct dvb_frontend *fe = dvbdev->priv; 2032 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2033 int i, err = 0; 2034 2035 if (cmd == COMPAT_FE_SET_PROPERTY) { 2036 struct compat_dtv_properties prop, *tvps = NULL; 2037 struct compat_dtv_property *tvp = NULL; 2038 2039 if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop))) 2040 return -EFAULT; 2041 2042 tvps = ∝ 2043 2044 /* 2045 * Put an arbitrary limit on the number of messages that can 2046 * be sent at once 2047 */ 2048 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS)) 2049 return -EINVAL; 2050 2051 tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp)); 2052 if (IS_ERR(tvp)) 2053 return PTR_ERR(tvp); 2054 2055 for (i = 0; i < tvps->num; i++) { 2056 err = dtv_property_process_set(fe, file, 2057 (tvp + i)->cmd, 2058 (tvp + i)->u.data); 2059 if (err < 0) { 2060 kfree(tvp); 2061 return err; 2062 } 2063 } 2064 kfree(tvp); 2065 } else if (cmd == COMPAT_FE_GET_PROPERTY) { 2066 struct compat_dtv_properties prop, *tvps = NULL; 2067 struct compat_dtv_property *tvp = NULL; 2068 struct dtv_frontend_properties getp = fe->dtv_property_cache; 2069 2070 if (copy_from_user(&prop, compat_ptr(arg), sizeof(prop))) 2071 return -EFAULT; 2072 2073 tvps = ∝ 2074 2075 /* 2076 * Put an arbitrary limit on the number of messages that can 2077 * be sent at once 2078 */ 2079 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS)) 2080 return -EINVAL; 2081 2082 tvp = memdup_user(compat_ptr(tvps->props), tvps->num * sizeof(*tvp)); 2083 if (IS_ERR(tvp)) 2084 return PTR_ERR(tvp); 2085 2086 /* 2087 * Let's use our own copy of property cache, in order to 2088 * avoid mangling with DTV zigzag logic, as drivers might 2089 * return crap, if they don't check if the data is available 2090 * before updating the properties cache. 2091 */ 2092 if (fepriv->state != FESTATE_IDLE) { 2093 err = dtv_get_frontend(fe, &getp, NULL); 2094 if (err < 0) { 2095 kfree(tvp); 2096 return err; 2097 } 2098 } 2099 for (i = 0; i < tvps->num; i++) { 2100 err = dtv_property_process_get( 2101 fe, &getp, (struct dtv_property *)(tvp + i), file); 2102 if (err < 0) { 2103 kfree(tvp); 2104 return err; 2105 } 2106 } 2107 2108 if (copy_to_user((void __user *)compat_ptr(tvps->props), tvp, 2109 tvps->num * sizeof(struct compat_dtv_property))) { 2110 kfree(tvp); 2111 return -EFAULT; 2112 } 2113 kfree(tvp); 2114 } 2115 2116 return err; 2117 } 2118 2119 static long dvb_frontend_compat_ioctl(struct file *file, unsigned int cmd, 2120 unsigned long arg) 2121 { 2122 struct dvb_device *dvbdev = file->private_data; 2123 struct dvb_frontend *fe = dvbdev->priv; 2124 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2125 int err; 2126 2127 if (cmd == COMPAT_FE_SET_PROPERTY || cmd == COMPAT_FE_GET_PROPERTY) { 2128 if (down_interruptible(&fepriv->sem)) 2129 return -ERESTARTSYS; 2130 2131 err = dvb_frontend_handle_compat_ioctl(file, cmd, arg); 2132 2133 up(&fepriv->sem); 2134 return err; 2135 } 2136 2137 return dvb_frontend_ioctl(file, cmd, (unsigned long)compat_ptr(arg)); 2138 } 2139 #endif 2140 2141 static int dtv_set_frontend(struct dvb_frontend *fe) 2142 { 2143 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2144 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 2145 struct dvb_frontend_tune_settings fetunesettings; 2146 u32 rolloff = 0; 2147 2148 if (dvb_frontend_check_parameters(fe) < 0) 2149 return -EINVAL; 2150 2151 /* 2152 * Initialize output parameters to match the values given by 2153 * the user. FE_SET_FRONTEND triggers an initial frontend event 2154 * with status = 0, which copies output parameters to userspace. 2155 */ 2156 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out); 2157 2158 /* 2159 * Be sure that the bandwidth will be filled for all 2160 * non-satellite systems, as tuners need to know what 2161 * low pass/Nyquist half filter should be applied, in 2162 * order to avoid inter-channel noise. 2163 * 2164 * ISDB-T and DVB-T/T2 already sets bandwidth. 2165 * ATSC and DVB-C don't set, so, the core should fill it. 2166 * 2167 * On DVB-C Annex A and C, the bandwidth is a function of 2168 * the roll-off and symbol rate. Annex B defines different 2169 * roll-off factors depending on the modulation. Fortunately, 2170 * Annex B is only used with 6MHz, so there's no need to 2171 * calculate it. 2172 * 2173 * While not officially supported, a side effect of handling it at 2174 * the cache level is that a program could retrieve the bandwidth 2175 * via DTV_BANDWIDTH_HZ, which may be useful for test programs. 2176 */ 2177 switch (c->delivery_system) { 2178 case SYS_ATSC: 2179 case SYS_DVBC_ANNEX_B: 2180 c->bandwidth_hz = 6000000; 2181 break; 2182 case SYS_DVBC_ANNEX_A: 2183 rolloff = 115; 2184 break; 2185 case SYS_DVBC_ANNEX_C: 2186 rolloff = 113; 2187 break; 2188 case SYS_DVBS: 2189 case SYS_TURBO: 2190 case SYS_ISDBS: 2191 rolloff = 135; 2192 break; 2193 case SYS_DVBS2: 2194 switch (c->rolloff) { 2195 case ROLLOFF_20: 2196 rolloff = 120; 2197 break; 2198 case ROLLOFF_25: 2199 rolloff = 125; 2200 break; 2201 default: 2202 case ROLLOFF_35: 2203 rolloff = 135; 2204 } 2205 break; 2206 default: 2207 break; 2208 } 2209 if (rolloff) 2210 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100); 2211 2212 /* force auto frequency inversion if requested */ 2213 if (dvb_force_auto_inversion) 2214 c->inversion = INVERSION_AUTO; 2215 2216 /* 2217 * without hierarchical coding code_rate_LP is irrelevant, 2218 * so we tolerate the otherwise invalid FEC_NONE setting 2219 */ 2220 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE) 2221 c->code_rate_LP = FEC_AUTO; 2222 2223 /* get frontend-specific tuning settings */ 2224 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings)); 2225 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) { 2226 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000; 2227 fepriv->max_drift = fetunesettings.max_drift; 2228 fepriv->step_size = fetunesettings.step_size; 2229 } else { 2230 /* default values */ 2231 switch (c->delivery_system) { 2232 case SYS_DVBS: 2233 case SYS_DVBS2: 2234 case SYS_ISDBS: 2235 case SYS_TURBO: 2236 case SYS_DVBC_ANNEX_A: 2237 case SYS_DVBC_ANNEX_C: 2238 fepriv->min_delay = HZ / 20; 2239 fepriv->step_size = c->symbol_rate / 16000; 2240 fepriv->max_drift = c->symbol_rate / 2000; 2241 break; 2242 case SYS_DVBT: 2243 case SYS_DVBT2: 2244 case SYS_ISDBT: 2245 case SYS_DTMB: 2246 fepriv->min_delay = HZ / 20; 2247 fepriv->step_size = fe->ops.info.frequency_stepsize * 2; 2248 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1; 2249 break; 2250 default: 2251 /* 2252 * FIXME: This sounds wrong! if freqency_stepsize is 2253 * defined by the frontend, why not use it??? 2254 */ 2255 fepriv->min_delay = HZ / 20; 2256 fepriv->step_size = 0; /* no zigzag */ 2257 fepriv->max_drift = 0; 2258 break; 2259 } 2260 } 2261 if (dvb_override_tune_delay > 0) 2262 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000; 2263 2264 fepriv->state = FESTATE_RETUNE; 2265 2266 /* Request the search algorithm to search */ 2267 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN; 2268 2269 dvb_frontend_clear_events(fe); 2270 dvb_frontend_add_event(fe, 0); 2271 dvb_frontend_wakeup(fe); 2272 fepriv->status = 0; 2273 2274 return 0; 2275 } 2276 2277 static int dvb_frontend_handle_ioctl(struct file *file, 2278 unsigned int cmd, void *parg) 2279 { 2280 struct dvb_device *dvbdev = file->private_data; 2281 struct dvb_frontend *fe = dvbdev->priv; 2282 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2283 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 2284 int i, err = -ENOTSUPP; 2285 2286 dev_dbg(fe->dvb->device, "%s:\n", __func__); 2287 2288 switch (cmd) { 2289 case FE_SET_PROPERTY: { 2290 struct dtv_properties *tvps = parg; 2291 struct dtv_property *tvp = NULL; 2292 2293 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", 2294 __func__, tvps->num); 2295 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", 2296 __func__, tvps->props); 2297 2298 /* 2299 * Put an arbitrary limit on the number of messages that can 2300 * be sent at once 2301 */ 2302 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS)) 2303 return -EINVAL; 2304 2305 tvp = memdup_user((void __user *)tvps->props, tvps->num * sizeof(*tvp)); 2306 if (IS_ERR(tvp)) 2307 return PTR_ERR(tvp); 2308 2309 for (i = 0; i < tvps->num; i++) { 2310 err = dtv_property_process_set(fe, file, 2311 (tvp + i)->cmd, 2312 (tvp + i)->u.data); 2313 if (err < 0) { 2314 kfree(tvp); 2315 return err; 2316 } 2317 } 2318 kfree(tvp); 2319 err = 0; 2320 break; 2321 } 2322 case FE_GET_PROPERTY: { 2323 struct dtv_properties *tvps = parg; 2324 struct dtv_property *tvp = NULL; 2325 struct dtv_frontend_properties getp = fe->dtv_property_cache; 2326 2327 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", 2328 __func__, tvps->num); 2329 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", 2330 __func__, tvps->props); 2331 2332 /* 2333 * Put an arbitrary limit on the number of messages that can 2334 * be sent at once 2335 */ 2336 if (!tvps->num || (tvps->num > DTV_IOCTL_MAX_MSGS)) 2337 return -EINVAL; 2338 2339 tvp = memdup_user((void __user *)tvps->props, tvps->num * sizeof(*tvp)); 2340 if (IS_ERR(tvp)) 2341 return PTR_ERR(tvp); 2342 2343 /* 2344 * Let's use our own copy of property cache, in order to 2345 * avoid mangling with DTV zigzag logic, as drivers might 2346 * return crap, if they don't check if the data is available 2347 * before updating the properties cache. 2348 */ 2349 if (fepriv->state != FESTATE_IDLE) { 2350 err = dtv_get_frontend(fe, &getp, NULL); 2351 if (err < 0) { 2352 kfree(tvp); 2353 return err; 2354 } 2355 } 2356 for (i = 0; i < tvps->num; i++) { 2357 err = dtv_property_process_get(fe, &getp, 2358 tvp + i, file); 2359 if (err < 0) { 2360 kfree(tvp); 2361 return err; 2362 } 2363 } 2364 2365 if (copy_to_user((void __user *)tvps->props, tvp, 2366 tvps->num * sizeof(struct dtv_property))) { 2367 kfree(tvp); 2368 return -EFAULT; 2369 } 2370 kfree(tvp); 2371 err = 0; 2372 break; 2373 } 2374 2375 case FE_GET_INFO: { 2376 struct dvb_frontend_info *info = parg; 2377 2378 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info)); 2379 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max); 2380 2381 /* 2382 * Associate the 4 delivery systems supported by DVBv3 2383 * API with their DVBv5 counterpart. For the other standards, 2384 * use the closest type, assuming that it would hopefully 2385 * work with a DVBv3 application. 2386 * It should be noticed that, on multi-frontend devices with 2387 * different types (terrestrial and cable, for example), 2388 * a pure DVBv3 application won't be able to use all delivery 2389 * systems. Yet, changing the DVBv5 cache to the other delivery 2390 * system should be enough for making it work. 2391 */ 2392 switch (dvbv3_type(c->delivery_system)) { 2393 case DVBV3_QPSK: 2394 info->type = FE_QPSK; 2395 break; 2396 case DVBV3_ATSC: 2397 info->type = FE_ATSC; 2398 break; 2399 case DVBV3_QAM: 2400 info->type = FE_QAM; 2401 break; 2402 case DVBV3_OFDM: 2403 info->type = FE_OFDM; 2404 break; 2405 default: 2406 dev_err(fe->dvb->device, 2407 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n", 2408 __func__, c->delivery_system); 2409 fe->ops.info.type = FE_OFDM; 2410 } 2411 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n", 2412 __func__, c->delivery_system, fe->ops.info.type); 2413 2414 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */ 2415 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) 2416 info->caps |= FE_CAN_INVERSION_AUTO; 2417 err = 0; 2418 break; 2419 } 2420 2421 case FE_READ_STATUS: { 2422 enum fe_status *status = parg; 2423 2424 /* if retune was requested but hasn't occurred yet, prevent 2425 * that user get signal state from previous tuning */ 2426 if (fepriv->state == FESTATE_RETUNE || 2427 fepriv->state == FESTATE_ERROR) { 2428 err = 0; 2429 *status = 0; 2430 break; 2431 } 2432 2433 if (fe->ops.read_status) 2434 err = fe->ops.read_status(fe, status); 2435 break; 2436 } 2437 2438 case FE_DISEQC_RESET_OVERLOAD: 2439 if (fe->ops.diseqc_reset_overload) { 2440 err = fe->ops.diseqc_reset_overload(fe); 2441 fepriv->state = FESTATE_DISEQC; 2442 fepriv->status = 0; 2443 } 2444 break; 2445 2446 case FE_DISEQC_SEND_MASTER_CMD: 2447 if (fe->ops.diseqc_send_master_cmd) { 2448 struct dvb_diseqc_master_cmd *cmd = parg; 2449 2450 if (cmd->msg_len > sizeof(cmd->msg)) { 2451 err = -EINVAL; 2452 break; 2453 } 2454 err = fe->ops.diseqc_send_master_cmd(fe, cmd); 2455 fepriv->state = FESTATE_DISEQC; 2456 fepriv->status = 0; 2457 } 2458 break; 2459 2460 case FE_DISEQC_SEND_BURST: 2461 if (fe->ops.diseqc_send_burst) { 2462 err = fe->ops.diseqc_send_burst(fe, 2463 (enum fe_sec_mini_cmd)parg); 2464 fepriv->state = FESTATE_DISEQC; 2465 fepriv->status = 0; 2466 } 2467 break; 2468 2469 case FE_SET_TONE: 2470 if (fe->ops.set_tone) { 2471 err = fe->ops.set_tone(fe, 2472 (enum fe_sec_tone_mode)parg); 2473 fepriv->tone = (enum fe_sec_tone_mode)parg; 2474 fepriv->state = FESTATE_DISEQC; 2475 fepriv->status = 0; 2476 } 2477 break; 2478 2479 case FE_SET_VOLTAGE: 2480 if (fe->ops.set_voltage) { 2481 err = fe->ops.set_voltage(fe, 2482 (enum fe_sec_voltage)parg); 2483 fepriv->voltage = (enum fe_sec_voltage)parg; 2484 fepriv->state = FESTATE_DISEQC; 2485 fepriv->status = 0; 2486 } 2487 break; 2488 2489 case FE_DISEQC_RECV_SLAVE_REPLY: 2490 if (fe->ops.diseqc_recv_slave_reply) 2491 err = fe->ops.diseqc_recv_slave_reply(fe, parg); 2492 break; 2493 2494 case FE_ENABLE_HIGH_LNB_VOLTAGE: 2495 if (fe->ops.enable_high_lnb_voltage) 2496 err = fe->ops.enable_high_lnb_voltage(fe, (long)parg); 2497 break; 2498 2499 case FE_SET_FRONTEND_TUNE_MODE: 2500 fepriv->tune_mode_flags = (unsigned long)parg; 2501 err = 0; 2502 break; 2503 2504 /* DEPRECATED dish control ioctls */ 2505 2506 case FE_DISHNETWORK_SEND_LEGACY_CMD: 2507 if (fe->ops.dishnetwork_send_legacy_command) { 2508 err = fe->ops.dishnetwork_send_legacy_command(fe, 2509 (unsigned long)parg); 2510 fepriv->state = FESTATE_DISEQC; 2511 fepriv->status = 0; 2512 } else if (fe->ops.set_voltage) { 2513 /* 2514 * NOTE: This is a fallback condition. Some frontends 2515 * (stv0299 for instance) take longer than 8msec to 2516 * respond to a set_voltage command. Those switches 2517 * need custom routines to switch properly. For all 2518 * other frontends, the following should work ok. 2519 * Dish network legacy switches (as used by Dish500) 2520 * are controlled by sending 9-bit command words 2521 * spaced 8msec apart. 2522 * the actual command word is switch/port dependent 2523 * so it is up to the userspace application to send 2524 * the right command. 2525 * The command must always start with a '0' after 2526 * initialization, so parg is 8 bits and does not 2527 * include the initialization or start bit 2528 */ 2529 unsigned long swcmd = ((unsigned long)parg) << 1; 2530 ktime_t nexttime; 2531 ktime_t tv[10]; 2532 int i; 2533 u8 last = 1; 2534 2535 if (dvb_frontend_debug) 2536 dprintk("%s switch command: 0x%04lx\n", 2537 __func__, swcmd); 2538 nexttime = ktime_get_boottime(); 2539 if (dvb_frontend_debug) 2540 tv[0] = nexttime; 2541 /* before sending a command, initialize by sending 2542 * a 32ms 18V to the switch 2543 */ 2544 fe->ops.set_voltage(fe, SEC_VOLTAGE_18); 2545 dvb_frontend_sleep_until(&nexttime, 32000); 2546 2547 for (i = 0; i < 9; i++) { 2548 if (dvb_frontend_debug) 2549 tv[i + 1] = ktime_get_boottime(); 2550 if ((swcmd & 0x01) != last) { 2551 /* set voltage to (last ? 13V : 18V) */ 2552 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18); 2553 last = (last) ? 0 : 1; 2554 } 2555 swcmd = swcmd >> 1; 2556 if (i != 8) 2557 dvb_frontend_sleep_until(&nexttime, 8000); 2558 } 2559 if (dvb_frontend_debug) { 2560 dprintk("%s(%d): switch delay (should be 32k followed by all 8k)\n", 2561 __func__, fe->dvb->num); 2562 for (i = 1; i < 10; i++) 2563 pr_info("%d: %d\n", i, 2564 (int)ktime_us_delta(tv[i], tv[i - 1])); 2565 } 2566 err = 0; 2567 fepriv->state = FESTATE_DISEQC; 2568 fepriv->status = 0; 2569 } 2570 break; 2571 2572 /* DEPRECATED statistics ioctls */ 2573 2574 case FE_READ_BER: 2575 if (fe->ops.read_ber) { 2576 if (fepriv->thread) 2577 err = fe->ops.read_ber(fe, parg); 2578 else 2579 err = -EAGAIN; 2580 } 2581 break; 2582 2583 case FE_READ_SIGNAL_STRENGTH: 2584 if (fe->ops.read_signal_strength) { 2585 if (fepriv->thread) 2586 err = fe->ops.read_signal_strength(fe, parg); 2587 else 2588 err = -EAGAIN; 2589 } 2590 break; 2591 2592 case FE_READ_SNR: 2593 if (fe->ops.read_snr) { 2594 if (fepriv->thread) 2595 err = fe->ops.read_snr(fe, parg); 2596 else 2597 err = -EAGAIN; 2598 } 2599 break; 2600 2601 case FE_READ_UNCORRECTED_BLOCKS: 2602 if (fe->ops.read_ucblocks) { 2603 if (fepriv->thread) 2604 err = fe->ops.read_ucblocks(fe, parg); 2605 else 2606 err = -EAGAIN; 2607 } 2608 break; 2609 2610 /* DEPRECATED DVBv3 ioctls */ 2611 2612 case FE_SET_FRONTEND: 2613 err = dvbv3_set_delivery_system(fe); 2614 if (err) 2615 break; 2616 2617 err = dtv_property_cache_sync(fe, c, parg); 2618 if (err) 2619 break; 2620 err = dtv_set_frontend(fe); 2621 break; 2622 case FE_GET_EVENT: 2623 err = dvb_frontend_get_event(fe, parg, file->f_flags); 2624 break; 2625 2626 case FE_GET_FRONTEND: { 2627 struct dtv_frontend_properties getp = fe->dtv_property_cache; 2628 2629 /* 2630 * Let's use our own copy of property cache, in order to 2631 * avoid mangling with DTV zigzag logic, as drivers might 2632 * return crap, if they don't check if the data is available 2633 * before updating the properties cache. 2634 */ 2635 err = dtv_get_frontend(fe, &getp, parg); 2636 break; 2637 } 2638 2639 default: 2640 return -ENOTSUPP; 2641 } /* switch */ 2642 2643 return err; 2644 } 2645 2646 static __poll_t dvb_frontend_poll(struct file *file, struct poll_table_struct *wait) 2647 { 2648 struct dvb_device *dvbdev = file->private_data; 2649 struct dvb_frontend *fe = dvbdev->priv; 2650 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2651 2652 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__); 2653 2654 poll_wait(file, &fepriv->events.wait_queue, wait); 2655 2656 if (fepriv->events.eventw != fepriv->events.eventr) 2657 return (EPOLLIN | EPOLLRDNORM | EPOLLPRI); 2658 2659 return 0; 2660 } 2661 2662 static int dvb_frontend_open(struct inode *inode, struct file *file) 2663 { 2664 struct dvb_device *dvbdev = file->private_data; 2665 struct dvb_frontend *fe = dvbdev->priv; 2666 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2667 struct dvb_adapter *adapter = fe->dvb; 2668 int ret; 2669 2670 dev_dbg(fe->dvb->device, "%s:\n", __func__); 2671 if (fe->exit == DVB_FE_DEVICE_REMOVED) 2672 return -ENODEV; 2673 2674 if (adapter->mfe_shared) { 2675 mutex_lock(&adapter->mfe_lock); 2676 2677 if (!adapter->mfe_dvbdev) 2678 adapter->mfe_dvbdev = dvbdev; 2679 2680 else if (adapter->mfe_dvbdev != dvbdev) { 2681 struct dvb_device 2682 *mfedev = adapter->mfe_dvbdev; 2683 struct dvb_frontend 2684 *mfe = mfedev->priv; 2685 struct dvb_frontend_private 2686 *mfepriv = mfe->frontend_priv; 2687 int mferetry = (dvb_mfe_wait_time << 1); 2688 2689 mutex_unlock(&adapter->mfe_lock); 2690 while (mferetry-- && (mfedev->users != -1 || 2691 mfepriv->thread)) { 2692 if (msleep_interruptible(500)) { 2693 if (signal_pending(current)) 2694 return -EINTR; 2695 } 2696 } 2697 2698 mutex_lock(&adapter->mfe_lock); 2699 if (adapter->mfe_dvbdev != dvbdev) { 2700 mfedev = adapter->mfe_dvbdev; 2701 mfe = mfedev->priv; 2702 mfepriv = mfe->frontend_priv; 2703 if (mfedev->users != -1 || 2704 mfepriv->thread) { 2705 mutex_unlock(&adapter->mfe_lock); 2706 return -EBUSY; 2707 } 2708 adapter->mfe_dvbdev = dvbdev; 2709 } 2710 } 2711 } 2712 2713 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) { 2714 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0) 2715 goto err0; 2716 2717 /* If we took control of the bus, we need to force 2718 reinitialization. This is because many ts_bus_ctrl() 2719 functions strobe the RESET pin on the demod, and if the 2720 frontend thread already exists then the dvb_init() routine 2721 won't get called (which is what usually does initial 2722 register configuration). */ 2723 fepriv->reinitialise = 1; 2724 } 2725 2726 if ((ret = dvb_generic_open(inode, file)) < 0) 2727 goto err1; 2728 2729 if ((file->f_flags & O_ACCMODE) != O_RDONLY) { 2730 /* normal tune mode when opened R/W */ 2731 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT; 2732 fepriv->tone = -1; 2733 fepriv->voltage = -1; 2734 2735 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 2736 mutex_lock(&fe->dvb->mdev_lock); 2737 if (fe->dvb->mdev) { 2738 mutex_lock(&fe->dvb->mdev->graph_mutex); 2739 if (fe->dvb->mdev->enable_source) 2740 ret = fe->dvb->mdev->enable_source( 2741 dvbdev->entity, 2742 &fepriv->pipe); 2743 mutex_unlock(&fe->dvb->mdev->graph_mutex); 2744 if (ret) { 2745 mutex_unlock(&fe->dvb->mdev_lock); 2746 dev_err(fe->dvb->device, 2747 "Tuner is busy. Error %d\n", ret); 2748 goto err2; 2749 } 2750 } 2751 mutex_unlock(&fe->dvb->mdev_lock); 2752 #endif 2753 ret = dvb_frontend_start(fe); 2754 if (ret) 2755 goto err3; 2756 2757 /* empty event queue */ 2758 fepriv->events.eventr = fepriv->events.eventw = 0; 2759 } 2760 2761 dvb_frontend_get(fe); 2762 2763 if (adapter->mfe_shared) 2764 mutex_unlock(&adapter->mfe_lock); 2765 return ret; 2766 2767 err3: 2768 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 2769 mutex_lock(&fe->dvb->mdev_lock); 2770 if (fe->dvb->mdev) { 2771 mutex_lock(&fe->dvb->mdev->graph_mutex); 2772 if (fe->dvb->mdev->disable_source) 2773 fe->dvb->mdev->disable_source(dvbdev->entity); 2774 mutex_unlock(&fe->dvb->mdev->graph_mutex); 2775 } 2776 mutex_unlock(&fe->dvb->mdev_lock); 2777 err2: 2778 #endif 2779 dvb_generic_release(inode, file); 2780 err1: 2781 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) 2782 fe->ops.ts_bus_ctrl(fe, 0); 2783 err0: 2784 if (adapter->mfe_shared) 2785 mutex_unlock(&adapter->mfe_lock); 2786 return ret; 2787 } 2788 2789 static int dvb_frontend_release(struct inode *inode, struct file *file) 2790 { 2791 struct dvb_device *dvbdev = file->private_data; 2792 struct dvb_frontend *fe = dvbdev->priv; 2793 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2794 int ret; 2795 2796 dev_dbg(fe->dvb->device, "%s:\n", __func__); 2797 2798 if ((file->f_flags & O_ACCMODE) != O_RDONLY) { 2799 fepriv->release_jiffies = jiffies; 2800 mb(); 2801 } 2802 2803 ret = dvb_generic_release(inode, file); 2804 2805 if (dvbdev->users == -1) { 2806 wake_up(&fepriv->wait_queue); 2807 #ifdef CONFIG_MEDIA_CONTROLLER_DVB 2808 mutex_lock(&fe->dvb->mdev_lock); 2809 if (fe->dvb->mdev) { 2810 mutex_lock(&fe->dvb->mdev->graph_mutex); 2811 if (fe->dvb->mdev->disable_source) 2812 fe->dvb->mdev->disable_source(dvbdev->entity); 2813 mutex_unlock(&fe->dvb->mdev->graph_mutex); 2814 } 2815 mutex_unlock(&fe->dvb->mdev_lock); 2816 #endif 2817 if (fe->exit != DVB_FE_NO_EXIT) 2818 wake_up(&dvbdev->wait_queue); 2819 if (fe->ops.ts_bus_ctrl) 2820 fe->ops.ts_bus_ctrl(fe, 0); 2821 } 2822 2823 dvb_frontend_put(fe); 2824 2825 return ret; 2826 } 2827 2828 static const struct file_operations dvb_frontend_fops = { 2829 .owner = THIS_MODULE, 2830 .unlocked_ioctl = dvb_frontend_ioctl, 2831 #ifdef CONFIG_COMPAT 2832 .compat_ioctl = dvb_frontend_compat_ioctl, 2833 #endif 2834 .poll = dvb_frontend_poll, 2835 .open = dvb_frontend_open, 2836 .release = dvb_frontend_release, 2837 .llseek = noop_llseek, 2838 }; 2839 2840 int dvb_frontend_suspend(struct dvb_frontend *fe) 2841 { 2842 int ret = 0; 2843 2844 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num, 2845 fe->id); 2846 2847 if (fe->ops.tuner_ops.suspend) 2848 ret = fe->ops.tuner_ops.suspend(fe); 2849 else if (fe->ops.tuner_ops.sleep) 2850 ret = fe->ops.tuner_ops.sleep(fe); 2851 2852 if (fe->ops.sleep) 2853 ret = fe->ops.sleep(fe); 2854 2855 return ret; 2856 } 2857 EXPORT_SYMBOL(dvb_frontend_suspend); 2858 2859 int dvb_frontend_resume(struct dvb_frontend *fe) 2860 { 2861 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2862 int ret = 0; 2863 2864 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num, 2865 fe->id); 2866 2867 fe->exit = DVB_FE_DEVICE_RESUME; 2868 if (fe->ops.init) 2869 ret = fe->ops.init(fe); 2870 2871 if (fe->ops.tuner_ops.resume) 2872 ret = fe->ops.tuner_ops.resume(fe); 2873 else if (fe->ops.tuner_ops.init) 2874 ret = fe->ops.tuner_ops.init(fe); 2875 2876 if (fe->ops.set_tone && fepriv->tone != -1) 2877 fe->ops.set_tone(fe, fepriv->tone); 2878 if (fe->ops.set_voltage && fepriv->voltage != -1) 2879 fe->ops.set_voltage(fe, fepriv->voltage); 2880 2881 fe->exit = DVB_FE_NO_EXIT; 2882 fepriv->state = FESTATE_RETUNE; 2883 dvb_frontend_wakeup(fe); 2884 2885 return ret; 2886 } 2887 EXPORT_SYMBOL(dvb_frontend_resume); 2888 2889 int dvb_register_frontend(struct dvb_adapter *dvb, 2890 struct dvb_frontend *fe) 2891 { 2892 struct dvb_frontend_private *fepriv; 2893 const struct dvb_device dvbdev_template = { 2894 .users = ~0, 2895 .writers = 1, 2896 .readers = (~0) - 1, 2897 .fops = &dvb_frontend_fops, 2898 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 2899 .name = fe->ops.info.name, 2900 #endif 2901 }; 2902 2903 dev_dbg(dvb->device, "%s:\n", __func__); 2904 2905 if (mutex_lock_interruptible(&frontend_mutex)) 2906 return -ERESTARTSYS; 2907 2908 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL); 2909 if (!fe->frontend_priv) { 2910 mutex_unlock(&frontend_mutex); 2911 return -ENOMEM; 2912 } 2913 fepriv = fe->frontend_priv; 2914 2915 kref_init(&fe->refcount); 2916 2917 /* 2918 * After initialization, there need to be two references: one 2919 * for dvb_unregister_frontend(), and another one for 2920 * dvb_frontend_detach(). 2921 */ 2922 dvb_frontend_get(fe); 2923 2924 sema_init(&fepriv->sem, 1); 2925 init_waitqueue_head(&fepriv->wait_queue); 2926 init_waitqueue_head(&fepriv->events.wait_queue); 2927 mutex_init(&fepriv->events.mtx); 2928 fe->dvb = dvb; 2929 fepriv->inversion = INVERSION_OFF; 2930 2931 dev_info(fe->dvb->device, 2932 "DVB: registering adapter %i frontend %i (%s)...\n", 2933 fe->dvb->num, fe->id, fe->ops.info.name); 2934 2935 dvb_register_device(fe->dvb, &fepriv->dvbdev, &dvbdev_template, 2936 fe, DVB_DEVICE_FRONTEND, 0); 2937 2938 /* 2939 * Initialize the cache to the proper values according with the 2940 * first supported delivery system (ops->delsys[0]) 2941 */ 2942 2943 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0]; 2944 dvb_frontend_clear_cache(fe); 2945 2946 mutex_unlock(&frontend_mutex); 2947 return 0; 2948 } 2949 EXPORT_SYMBOL(dvb_register_frontend); 2950 2951 int dvb_unregister_frontend(struct dvb_frontend *fe) 2952 { 2953 struct dvb_frontend_private *fepriv = fe->frontend_priv; 2954 2955 dev_dbg(fe->dvb->device, "%s:\n", __func__); 2956 2957 mutex_lock(&frontend_mutex); 2958 dvb_frontend_stop(fe); 2959 dvb_remove_device(fepriv->dvbdev); 2960 2961 /* fe is invalid now */ 2962 mutex_unlock(&frontend_mutex); 2963 dvb_frontend_put(fe); 2964 return 0; 2965 } 2966 EXPORT_SYMBOL(dvb_unregister_frontend); 2967 2968 static void dvb_frontend_invoke_release(struct dvb_frontend *fe, 2969 void (*release)(struct dvb_frontend *fe)) 2970 { 2971 if (release) { 2972 release(fe); 2973 #ifdef CONFIG_MEDIA_ATTACH 2974 dvb_detach(release); 2975 #endif 2976 } 2977 } 2978 2979 void dvb_frontend_detach(struct dvb_frontend *fe) 2980 { 2981 dvb_frontend_invoke_release(fe, fe->ops.release_sec); 2982 dvb_frontend_invoke_release(fe, fe->ops.tuner_ops.release); 2983 dvb_frontend_invoke_release(fe, fe->ops.analog_ops.release); 2984 dvb_frontend_invoke_release(fe, fe->ops.detach); 2985 dvb_frontend_put(fe); 2986 } 2987 EXPORT_SYMBOL(dvb_frontend_detach); 2988