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