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