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